-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathpython.ts
More file actions
430 lines (368 loc) · 17 KB
/
python.ts
File metadata and controls
430 lines (368 loc) · 17 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/**
* Python code generator for session-events and RPC types.
*/
import fs from "fs/promises";
import type { JSONSchema7 } from "json-schema";
import { FetchingJSONSchemaStore, InputData, JSONSchemaInput, quicktype } from "quicktype-core";
import {
getApiSchemaPath,
getSessionEventsSchemaPath,
isRpcMethod,
postProcessSchema,
writeGeneratedFile,
isRpcMethod,
isNodeFullyExperimental,
type ApiSchema,
type RpcMethod,
} from "./utils.js";
// ── Utilities ───────────────────────────────────────────────────────────────
/**
* Modernize quicktype's Python 3.7 output to Python 3.11+ syntax:
* - Optional[T] → T | None
* - List[T] → list[T]
* - Dict[K, V] → dict[K, V]
* - Type[T] → type[T]
* - Callable from collections.abc instead of typing
* - Clean up unused typing imports
*/
function replaceBalancedBrackets(code: string, prefix: string, replacer: (inner: string) => string): string {
let result = "";
let i = 0;
while (i < code.length) {
const idx = code.indexOf(prefix + "[", i);
if (idx === -1) {
result += code.slice(i);
break;
}
result += code.slice(i, idx);
const start = idx + prefix.length + 1; // after '['
let depth = 1;
let j = start;
while (j < code.length && depth > 0) {
if (code[j] === "[") depth++;
else if (code[j] === "]") depth--;
j++;
}
const inner = code.slice(start, j - 1);
result += replacer(inner);
i = j;
}
return result;
}
function modernizePython(code: string): string {
// Replace Optional[X] with X | None (handles arbitrarily nested brackets)
code = replaceBalancedBrackets(code, "Optional", (inner) => `${inner} | None`);
// Replace Union[X, Y] with X | Y
code = replaceBalancedBrackets(code, "Union", (inner) => {
return inner.split(",").map((s: string) => s.trim()).join(" | ");
});
// Replace List[X] with list[X]
code = code.replace(/\bList\[/g, "list[");
// Replace Dict[K, V] with dict[K, V]
code = code.replace(/\bDict\[/g, "dict[");
// Replace Type[T] with type[T]
code = code.replace(/\bType\[/g, "type[");
// Move Callable from typing to collections.abc
code = code.replace(
/from typing import (.*), Callable$/m,
"from typing import $1\nfrom collections.abc import Callable"
);
code = code.replace(
/from typing import Callable, (.*)$/m,
"from typing import $1\nfrom collections.abc import Callable"
);
// Remove now-unused imports from typing (Optional, List, Dict, Type)
code = code.replace(/from typing import (.+)$/m, (_match, imports: string) => {
const items = imports.split(",").map((s: string) => s.trim());
const remove = new Set(["Optional", "List", "Dict", "Type", "Union"]);
const kept = items.filter((i: string) => !remove.has(i));
return `from typing import ${kept.join(", ")}`;
});
return code;
}
function toSnakeCase(s: string): string {
return s
.replace(/([a-z])([A-Z])/g, "$1_$2")
.replace(/[._]/g, "_")
.toLowerCase();
}
function toPascalCase(s: string): string {
return s
.split(/[._]/)
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join("");
}
function collectRpcMethods(node: Record<string, unknown>): RpcMethod[] {
const results: RpcMethod[] = [];
for (const value of Object.values(node)) {
if (isRpcMethod(value)) {
results.push(value);
} else if (typeof value === "object" && value !== null) {
results.push(...collectRpcMethods(value as Record<string, unknown>));
}
}
return results;
}
// ── Session Events ──────────────────────────────────────────────────────────
async function generateSessionEvents(schemaPath?: string): Promise<void> {
console.log("Python: generating session-events...");
const resolvedPath = schemaPath ?? (await getSessionEventsSchemaPath());
const schema = JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as JSONSchema7;
const resolvedSchema = (schema.definitions?.SessionEvent as JSONSchema7) || schema;
const processed = postProcessSchema(resolvedSchema);
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
await schemaInput.addSource({ name: "SessionEvent", schema: JSON.stringify(processed) });
const inputData = new InputData();
inputData.addInput(schemaInput);
const result = await quicktype({
inputData,
lang: "python",
rendererOptions: { "python-version": "3.7" },
});
let code = result.lines.join("\n");
// Fix dataclass field ordering (Any fields need defaults)
code = code.replace(/: Any$/gm, ": Any = None");
// Fix bare except: to use Exception (required by ruff/pylint)
code = code.replace(/except:/g, "except Exception:");
// Modernize to Python 3.11+ syntax
code = modernizePython(code);
// Add UNKNOWN enum value for forward compatibility
code = code.replace(
/^(class SessionEventType\(Enum\):.*?)(^\s*\n@dataclass)/ms,
`$1 # UNKNOWN is used for forward compatibility
UNKNOWN = "unknown"
@classmethod
def _missing_(cls, value: object) -> "SessionEventType":
"""Handle unknown event types gracefully for forward compatibility."""
return cls.UNKNOWN
$2`
);
const banner = `"""
AUTO-GENERATED FILE - DO NOT EDIT
Generated from: session-events.schema.json
"""
`;
const outPath = await writeGeneratedFile("python/copilot/generated/session_events.py", banner + code);
console.log(` ✓ ${outPath}`);
}
// ── RPC Types ───────────────────────────────────────────────────────────────
async function generateRpc(schemaPath?: string): Promise<void> {
console.log("Python: generating RPC types...");
const resolvedPath = schemaPath ?? (await getApiSchemaPath());
const schema = JSON.parse(await fs.readFile(resolvedPath, "utf-8")) as ApiSchema;
const allMethods = [...collectRpcMethods(schema.server || {}), ...collectRpcMethods(schema.session || {})];
// Build a combined schema for quicktype
const combinedSchema: JSONSchema7 = {
$schema: "http://json-schema.org/draft-07/schema#",
definitions: {},
};
for (const method of allMethods) {
const baseName = toPascalCase(method.rpcMethod);
if (method.result) {
combinedSchema.definitions![baseName + "Result"] = method.result;
}
if (method.params?.properties && Object.keys(method.params.properties).length > 0) {
if (method.rpcMethod.startsWith("session.")) {
const filtered: JSONSchema7 = {
...method.params,
properties: Object.fromEntries(
Object.entries(method.params.properties).filter(([k]) => k !== "sessionId")
),
required: method.params.required?.filter((r) => r !== "sessionId"),
};
if (Object.keys(filtered.properties!).length > 0) {
combinedSchema.definitions![baseName + "Params"] = filtered;
}
} else {
combinedSchema.definitions![baseName + "Params"] = method.params;
}
}
}
// Generate types via quicktype
const schemaInput = new JSONSchemaInput(new FetchingJSONSchemaStore());
for (const [name, def] of Object.entries(combinedSchema.definitions!)) {
await schemaInput.addSource({ name, schema: JSON.stringify(def) });
}
const inputData = new InputData();
inputData.addInput(schemaInput);
const qtResult = await quicktype({
inputData,
lang: "python",
rendererOptions: { "python-version": "3.7" },
});
let typesCode = qtResult.lines.join("\n");
// Fix dataclass field ordering
typesCode = typesCode.replace(/: Any$/gm, ": Any = None");
// Fix bare except: to use Exception (required by ruff/pylint)
typesCode = typesCode.replace(/except:/g, "except Exception:");
// Remove unnecessary pass when class has methods (quicktype generates pass for empty schemas)
typesCode = typesCode.replace(/^(\s*)pass\n\n(\s*@staticmethod)/gm, "$2");
// Modernize to Python 3.11+ syntax
typesCode = modernizePython(typesCode);
// Annotate experimental data types
const experimentalTypeNames = new Set<string>();
for (const method of allMethods) {
if (method.stability !== "experimental") continue;
experimentalTypeNames.add(toPascalCase(method.rpcMethod) + "Result");
const baseName = toPascalCase(method.rpcMethod);
if (combinedSchema.definitions![baseName + "Params"]) {
experimentalTypeNames.add(baseName + "Params");
}
}
for (const typeName of experimentalTypeNames) {
typesCode = typesCode.replace(
new RegExp(`^(@dataclass\\n)?class ${typeName}[:(]`, "m"),
(match) => `# Experimental: this type is part of an experimental API and may change or be removed.\n${match}`
);
}
// Extract actual class names generated by quicktype (may differ from toPascalCase,
// e.g. quicktype produces "SessionMCPList" not "SessionMcpList")
const actualTypeNames = new Map<string, string>();
const classRe = /^class\s+(\w+)\b/gm;
let cm;
while ((cm = classRe.exec(typesCode)) !== null) {
actualTypeNames.set(cm[1].toLowerCase(), cm[1]);
}
const resolveType = (name: string): string => actualTypeNames.get(name.toLowerCase()) ?? name;
const lines: string[] = [];
lines.push(`"""
AUTO-GENERATED FILE - DO NOT EDIT
Generated from: api.schema.json
"""
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .._jsonrpc import JsonRpcClient
`);
lines.push(typesCode);
lines.push(`
def _timeout_kwargs(timeout: float | None) -> dict:
"""Build keyword arguments for optional timeout forwarding."""
if timeout is not None:
return {"timeout": timeout}
return {}
`);
// Emit RPC wrapper classes
if (schema.server) {
emitRpcWrapper(lines, schema.server, false, resolveType);
}
if (schema.session) {
emitRpcWrapper(lines, schema.session, true, resolveType);
}
const outPath = await writeGeneratedFile("python/copilot/generated/rpc.py", lines.join("\n"));
console.log(` ✓ ${outPath}`);
}
function emitRpcWrapper(lines: string[], node: Record<string, unknown>, isSession: boolean, resolveType: (name: string) => string): void {
const groups = Object.entries(node).filter(([, v]) => typeof v === "object" && v !== null && !isRpcMethod(v));
const topLevelMethods = Object.entries(node).filter(([, v]) => isRpcMethod(v));
const wrapperName = isSession ? "SessionRpc" : "ServerRpc";
// Emit API classes for groups
for (const [groupName, groupNode] of groups) {
const prefix = isSession ? "" : "Server";
const apiName = prefix + toPascalCase(groupName) + "Api";
const groupExperimental = isNodeFullyExperimental(groupNode as Record<string, unknown>);
if (isSession) {
if (groupExperimental) {
lines.push(`# Experimental: this API group is experimental and may change or be removed.`);
}
lines.push(`class ${apiName}:`);
lines.push(` def __init__(self, client: "JsonRpcClient", session_id: str):`);
lines.push(` self._client = client`);
lines.push(` self._session_id = session_id`);
} else {
if (groupExperimental) {
lines.push(`# Experimental: this API group is experimental and may change or be removed.`);
}
lines.push(`class ${apiName}:`);
lines.push(` def __init__(self, client: "JsonRpcClient"):`);
lines.push(` self._client = client`);
}
lines.push(``);
for (const [key, value] of Object.entries(groupNode as Record<string, unknown>)) {
if (!isRpcMethod(value)) continue;
emitMethod(lines, key, value, isSession, resolveType, groupExperimental);
}
lines.push(``);
}
// Emit wrapper class
if (isSession) {
lines.push(`class ${wrapperName}:`);
lines.push(` """Typed session-scoped RPC methods."""`);
lines.push(` def __init__(self, client: "JsonRpcClient", session_id: str):`);
lines.push(` self._client = client`);
lines.push(` self._session_id = session_id`);
for (const [groupName] of groups) {
lines.push(` self.${toSnakeCase(groupName)} = ${toPascalCase(groupName)}Api(client, session_id)`);
}
} else {
lines.push(`class ${wrapperName}:`);
lines.push(` """Typed server-scoped RPC methods."""`);
lines.push(` def __init__(self, client: "JsonRpcClient"):`);
lines.push(` self._client = client`);
for (const [groupName] of groups) {
lines.push(` self.${toSnakeCase(groupName)} = Server${toPascalCase(groupName)}Api(client)`);
}
}
lines.push(``);
// Top-level methods
for (const [key, value] of topLevelMethods) {
if (!isRpcMethod(value)) continue;
emitMethod(lines, key, value, isSession, resolveType, false);
}
lines.push(``);
}
function emitMethod(lines: string[], name: string, method: RpcMethod, isSession: boolean, resolveType: (name: string) => string, groupExperimental = false): void {
const methodName = toSnakeCase(name);
const resultType = resolveType(toPascalCase(method.rpcMethod) + "Result");
const paramProps = method.params?.properties || {};
const nonSessionParams = Object.keys(paramProps).filter((k) => k !== "sessionId");
const hasParams = isSession ? nonSessionParams.length > 0 : Object.keys(paramProps).length > 0;
const paramsType = resolveType(toPascalCase(method.rpcMethod) + "Params");
// Build signature with typed params + optional timeout
const sig = hasParams
? ` async def ${methodName}(self, params: ${paramsType}, *, timeout: float | None = None) -> ${resultType}:`
: ` async def ${methodName}(self, *, timeout: float | None = None) -> ${resultType}:`;
lines.push(sig);
if (method.stability === "experimental" && !groupExperimental) {
lines.push(` """.. warning:: This API is experimental and may change or be removed in future versions."""`);
}
// Build request body with proper serialization/deserialization
if (isSession) {
if (hasParams) {
lines.push(` params_dict = {k: v for k, v in params.to_dict().items() if v is not None}`);
lines.push(` params_dict["sessionId"] = self._session_id`);
lines.push(` return ${resultType}.from_dict(await self._client.request("${method.rpcMethod}", params_dict, **_timeout_kwargs(timeout)))`);
} else {
lines.push(` return ${resultType}.from_dict(await self._client.request("${method.rpcMethod}", {"sessionId": self._session_id}, **_timeout_kwargs(timeout)))`);
}
} else {
if (hasParams) {
lines.push(` params_dict = {k: v for k, v in params.to_dict().items() if v is not None}`);
lines.push(` return ${resultType}.from_dict(await self._client.request("${method.rpcMethod}", params_dict, **_timeout_kwargs(timeout)))`);
} else {
lines.push(` return ${resultType}.from_dict(await self._client.request("${method.rpcMethod}", {}, **_timeout_kwargs(timeout)))`);
}
}
lines.push(``);
}
// ── Main ────────────────────────────────────────────────────────────────────
async function generate(sessionSchemaPath?: string, apiSchemaPath?: string): Promise<void> {
await generateSessionEvents(sessionSchemaPath);
try {
await generateRpc(apiSchemaPath);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === "ENOENT" && !apiSchemaPath) {
console.log("Python: skipping RPC (api.schema.json not found)");
} else {
throw err;
}
}
}
const sessionArg = process.argv[2] || undefined;
const apiArg = process.argv[3] || undefined;
generate(sessionArg, apiArg).catch((err) => {
console.error("Python generation failed:", err);
process.exit(1);
});