-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvscode.ts
More file actions
196 lines (170 loc) · 6.04 KB
/
vscode.ts
File metadata and controls
196 lines (170 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* VS Code-specific telemetry helpers.
* This file contains all vscode imports to keep the rest of telemetry editor-agnostic.
*/
import * as vscode from "vscode"
import { getExtensionVersion } from "../../extension"
import { client } from "./client"
import type { ClientInfo } from "./types"
const USER_ID_KEY = "fastapi.telemetry.userId"
export function getClientInfo(): ClientInfo {
const appName = vscode.env.appName
const appHost = vscode.env.appHost
const uiKind = vscode.env.uiKind
const remoteName = vscode.env.remoteName
let clientType = "unknown"
const appNameLower = appName.toLowerCase()
if (appNameLower.includes("cursor")) {
clientType = "cursor"
} else if (appNameLower.includes("windsurf")) {
clientType = "windsurf"
} else if (appNameLower.includes("vscodium")) {
clientType = "vscodium"
} else if (appNameLower.includes("gitpod")) {
clientType = "gitpod"
} else if (appNameLower.includes("antigravity")) {
clientType = "antigravity"
} else if (appHost === "codespaces" || remoteName === "codespaces") {
clientType = "codespaces"
} else if (uiKind === vscode.UIKind.Web) {
clientType = "vscode-web"
} else if (appNameLower.includes("visual studio code")) {
clientType = "vscode-desktop"
}
return {
client: clientType,
app_name: appName,
app_host: appHost,
is_remote: remoteName !== undefined,
remote_name: remoteName,
platform: process.platform,
arch: process.arch,
}
}
/** Check if telemetry is enabled based on both VS Code and extension settings. */
export function isTelemetryEnabled(): boolean {
const vscodeTelemetryEnabled = vscode.env.isTelemetryEnabled
const config = vscode.workspace.getConfiguration("fastapi")
const extensionTelemetryEnabled = config.get<boolean>(
"telemetry.enabled",
true,
)
// Telemetry is enabled only if both VS Code and extension settings allow it
return vscodeTelemetryEnabled && extensionTelemetryEnabled
}
export async function getOrCreateUserId(
context: vscode.ExtensionContext,
): Promise<string> {
let userId = context.globalState.get<string>(USER_ID_KEY)
if (!userId) {
userId = crypto.randomUUID()
await context.globalState.update(USER_ID_KEY, userId)
}
return userId
}
/**
* Initialize telemetry for VS Code environment.
* Call this once during extension activation.
*/
export async function initVSCodeTelemetry(
context: vscode.ExtensionContext,
): Promise<void> {
// Skip telemetry entirely in development mode
if (context.extensionMode === vscode.ExtensionMode.Development) {
return
}
// Skip telemetry in browser environments (vscode.dev, github.dev)
// PostHog Node.js client requires Node APIs that aren't available in browsers
if (vscode.env.uiKind === vscode.UIKind.Web) {
return
}
const userId = await getOrCreateUserId(context)
const extensionVersion = getExtensionVersion()
await client.init({
userId,
clientInfo: getClientInfo(),
extensionVersion,
isEnabled: isTelemetryEnabled,
})
}
/**
* Fetch package versions using the Python interpreter.
* Runs all version checks in parallel for better performance.
*/
async function fetchPackageVersions(
pythonPath: string,
packages: readonly string[],
): Promise<{ [key: string]: string | undefined }> {
const { promisify } = await import("node:util")
const { execFile } = await import("node:child_process")
const execFileAsync = promisify(execFile)
// Fetch all package versions in parallel
const results = await Promise.allSettled(
packages.map(async (pkg) => {
const importName = pkg.replace(/-/g, "_")
const { stdout } = await execFileAsync(
pythonPath,
["-c", `import ${importName}; print(${importName}.__version__)`],
{ timeout: 5000 },
)
return { key: `${importName}_version`, version: stdout.trim() }
}),
)
// Collect results into a single object
const versions: { [key: string]: string | undefined } = {}
results.forEach((result, index) => {
const importName = packages[index].replace(/-/g, "_")
const key = `${importName}_version`
versions[key] =
result.status === "fulfilled" ? result.value.version : undefined
})
return versions
}
/**
* Get actual Python and package versions installed in the current workspace.
* This requires the Python extension to be installed and activated.
* @returns An object with python_version and package versions (e.g., fastapi_version, pydantic_version).
*/
export async function getInstalledVersions(
packages: readonly string[] = [],
): Promise<{ python_version?: string; [key: string]: string | undefined }> {
try {
// Get Python extension API
const pythonExtension = vscode.extensions.getExtension("ms-python.python")
// Don't activate the extension just for telemetry - only use it if already active
if (!pythonExtension || !pythonExtension.isActive) {
return {}
}
const pythonApi = pythonExtension.exports
// Get active interpreter details
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]
if (!workspaceFolder) {
return {}
}
const environment = await pythonApi.environments.resolveEnvironment(
await pythonApi.environments.getActiveEnvironmentPath(
workspaceFolder.uri,
),
)
if (!environment?.version?.major) {
return {}
}
// Extract Python version including patch version (e.g., "3.11.5")
const python_version = environment.version.micro
? `${environment.version.major}.${environment.version.minor}.${environment.version.micro}`
: `${environment.version.major}.${environment.version.minor}`
// Try to fetch package versions if we have an executable path
if (!environment.executable?.uri) {
return { python_version }
}
const pythonPath = environment.executable.uri.fsPath
const packageVersions = await fetchPackageVersions(
pythonPath,
packages,
).catch(() => ({}))
return { python_version, ...packageVersions }
} catch {
// If Python extension is not available or any error occurs, return empty
return {}
}
}