-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostprocess.js
More file actions
442 lines (370 loc) · 12.4 KB
/
postprocess.js
File metadata and controls
442 lines (370 loc) · 12.4 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
431
432
433
434
435
436
437
438
439
440
441
442
import { promises as fspromises } from 'fs';
import path from 'path';
const EXTRACTION_TIMEOUT_MS = 30000;
const REGISTRY_DETECTION_RETRIES = 3;
const REGISTRY_DETECTION_DELAY_MS = 500;
const MIN_SERVICES_THRESHOLD = 3;
const exitTimer = setTimeout(() => {
console.error(`Force exit: extraction timed out after ${EXTRACTION_TIMEOUT_MS / 1000} seconds`);
process.exit(1);
}, EXTRACTION_TIMEOUT_MS);
const typeRegistry = new Map();
const enumRegistry = new Map();
const serviceRegistry = new Map();
const processingTypes = new Set();
/**
* Maps protobuf scalar type numbers to their string names.
* @param {number} scalar - The scalar type number
* @returns {string} The scalar type name
* @throws {Error} If scalar type is unknown
*/
const getScalarName = (scalar) => {
const scalarMap = {
1: "double",
2: "float",
3: "int64",
4: "uint64",
5: "int32",
6: "fixed64",
7: "fixed32",
8: "bool",
9: "string",
12: "bytes",
13: "uint32",
15: "sfixed32",
16: "sfixed64",
17: "sint32",
18: "sint64",
};
const name = scalarMap[scalar];
if (name) {
return name;
}
console.warn(`Unknown scalar type: ${scalar}, using 'bytes' as fallback`);
return "bytes";
};
/**
* Registers an enum definition and returns its processed form.
*/
const registerEnum = (prefix, enumDef, shouldRegisterGlobal = true) => {
if (!enumDef?.typeName) {
console.warn('Invalid enum definition: missing typeName');
return { name: 'UnknownEnum', fields: [], lines: ['enum UnknownEnum {}'] };
}
if (enumRegistry.has(enumDef.typeName)) {
return enumRegistry.get(enumDef.typeName);
}
const nameSpl = enumDef.typeName.replace(prefix, "").split(".");
const name = nameSpl[nameSpl.length - 1];
const e = {
name: name,
fields: (enumDef.values || []).map((val, i) => `${val.name} = ${i};`),
};
e.lines = [`enum ${name} { // ${enumDef.typeName}`];
e.lines.push(...e.fields.map((f) => `\t${f}`));
e.lines.push("}");
if (shouldRegisterGlobal) {
enumRegistry.set(enumDef.typeName, e);
}
return e;
};
/**
* Registers a type definition and returns its processed form.
* Handles circular references by tracking types currently being processed.
*/
const registerTypeDefinition = (prefix, typeDef, shouldRegisterGlobal = true) => {
if (!typeDef?.typeName) {
console.warn('Invalid type definition: missing typeName');
return { name: 'UnknownType', fields: [], lines: ['message UnknownType {}'], locals: new Map() };
}
// Check if already registered
if (typeRegistry.has(typeDef.typeName)) {
return typeRegistry.get(typeDef.typeName);
}
// Check for circular reference
if (processingTypes.has(typeDef.typeName)) {
const nameSpl = typeDef.typeName.replace(prefix, "").split(".");
const name = nameSpl[nameSpl.length - 1];
return { name: name, fields: [], lines: [], locals: new Map() };
}
processingTypes.add(typeDef.typeName);
try {
const nameSpl = typeDef.typeName.replace(prefix, "").split(".");
const name = nameSpl[nameSpl.length - 1];
const fields = [];
const typeObj = {
name: name,
locals: new Map(),
};
if (shouldRegisterGlobal) {
typeRegistry.set(typeDef.typeName, typeObj);
}
const preamble = [];
const fieldsList = typeDef.fields?._fields?.() || [];
for (const field of fieldsList) {
if (!field) continue;
let fieldType = null;
const repeated = field.repeated;
const opt = field.opt;
const fieldPrefix = repeated ? "repeated " : opt ? "optional " : "";
if (field.kind === "message") {
if (!field.T?.typeName) {
console.warn(`Field ${field.name} has message kind but missing T.typeName`);
continue;
}
if (field.T.typeName === typeDef.typeName) {
fieldType = name;
} else if (!typeRegistry.has(field.T.typeName)) {
let registerGlobalNested = true;
const newFieldName = field.T.typeName.replace(prefix, "");
if (newFieldName.split(".").length > 1) {
registerGlobalNested = false;
}
const newFieldDef = registerTypeDefinition(prefix, field.T, registerGlobalNested);
if (!registerGlobalNested) {
if (!typeObj.locals.has(field.T.typeName)) {
typeObj.locals.set(field.T.typeName, true);
preamble.push(...newFieldDef.lines.map((l) => `\t${l}`));
}
}
fieldType = newFieldDef.name;
} else {
fieldType = typeRegistry.get(field.T.typeName).name;
}
} else if (field.kind === "scalar") {
fieldType = getScalarName(field.T);
} else if (field.kind === "enum") {
if (!field.T?.typeName) {
console.warn(`Field ${field.name} has enum kind but missing T.typeName`);
continue;
}
const enumName = field.T.typeName.replace(prefix, "");
let registerGlobalEnum = true;
if (enumName.split(".").length > 1) {
registerGlobalEnum = false;
}
const newEnum = registerEnum(prefix, field.T, registerGlobalEnum);
if (!registerGlobalEnum) {
if (!typeObj.locals.has(field.T.typeName)) {
typeObj.locals.set(field.T.typeName, newEnum);
preamble.push(...newEnum.lines.map((l) => `\t${l}`));
}
}
fieldType = newEnum.name;
} else if (field.kind === "map") {
const keyType = getScalarName(field.K);
let valueType;
if (field.V?.kind === "scalar") {
valueType = getScalarName(field.V.T);
} else if (field.V?.kind === "message" && field.V?.T?.typeName) {
valueType = field.V.T.typeName.split(".").pop();
} else {
valueType = "bytes";
}
fieldType = `map<${keyType}, ${valueType}>`;
fields.push(`${fieldType} ${field.name} = ${field.no};`);
continue;
} else {
continue;
}
if (fieldType) {
fields.push(`${fieldPrefix}${fieldType} ${field.name} = ${field.no};`);
}
}
typeObj.fields = fields;
typeObj.lines = [`message ${name} { // ${typeDef.typeName}`];
typeObj.lines.push(...preamble.map((pl) => `\t${pl}`));
typeObj.lines.push(...fields.map((f) => `\t${f}`));
typeObj.lines.push("}");
return typeObj;
} finally {
processingTypes.delete(typeDef.typeName);
}
};
/**
* Registers a service definition.
*/
const registerService = (service) => {
if (!service?.typeName || !service?.methods) {
console.warn('Invalid service definition');
return;
}
const typeName = service.typeName;
const parts = typeName.split(".");
const serviceName = parts[parts.length - 1];
const packageName = parts.slice(0, parts.length - 1).join(".");
const lines = [`service ${serviceName} {`];
for (const [, method] of Object.entries(service.methods)) {
if (!method?.I || !method?.O) {
console.warn(`Invalid method in service ${serviceName}`);
continue;
}
const inType = registerTypeDefinition(packageName + ".", method.I);
const outType = registerTypeDefinition(packageName + ".", method.O);
const streaming = method.kind === 1;
lines.push(`\trpc ${method.name}(${inType.name}) returns (${streaming ? "stream " : ""}${outType.name}) {}`);
}
lines.push("}");
serviceRegistry.set(typeName, {
package: packageName,
name: serviceName,
lines: lines,
});
};
/**
* Finds all service registries in globalThis.
*/
function findAllServiceRegistries() {
const registries = [];
const seenKeys = new Set();
for (const [key, value] of Object.entries(globalThis)) {
if (seenKeys.has(key)) continue;
if (!value || typeof value !== 'object' || Array.isArray(value)) continue;
try {
const entries = Object.entries(value);
let serviceCount = 0;
let aiserviceCount = 0;
let agentServiceCount = 0;
for (const [k, v] of entries) {
if (k?.includes && v?.typeName === k && v?.methods) {
serviceCount++;
if (k.includes('aiserver.v1.')) aiserviceCount++;
if (k.includes('agent.v1.')) agentServiceCount++;
}
}
if (serviceCount >= MIN_SERVICES_THRESHOLD) {
console.log(`Found service registry '${key}' with ${serviceCount} services (aiserver: ${aiserviceCount}, agent: ${agentServiceCount})`);
registries.push(value);
seenKeys.add(key);
}
} catch (err) {
console.warn(`Error inspecting globalThis.${key}: ${err.message}`);
}
}
return registries;
}
/**
* Merges multiple service registries into one.
*/
function mergeRegistries(registries) {
const merged = {};
for (const reg of registries) {
Object.assign(merged, reg);
}
return merged;
}
/**
* Attempts to find service registries with retries.
*/
async function findRegistriesWithRetry() {
for (let attempt = 1; attempt <= REGISTRY_DETECTION_RETRIES; attempt++) {
const registries = findAllServiceRegistries();
if (registries.length > 0) {
console.log(`Found ${registries.length} registry(ies) on attempt ${attempt}`);
return mergeRegistries(registries);
}
if (globalThis.serviceRegistry) {
console.log('Found registry in globalThis.serviceRegistry');
return globalThis.serviceRegistry;
}
if (attempt < REGISTRY_DETECTION_RETRIES) {
console.log(`No registries found, retrying in ${REGISTRY_DETECTION_DELAY_MS}ms (attempt ${attempt}/${REGISTRY_DETECTION_RETRIES})...`);
await new Promise(resolve => setTimeout(resolve, REGISTRY_DETECTION_DELAY_MS));
}
}
return null;
}
/**
* Ensures a directory exists, creating if necessary.
*/
async function ensureDir(dirPath) {
try {
await fspromises.access(dirPath);
} catch {
await fspromises.mkdir(dirPath, { recursive: true });
console.log(`Created directory: ${dirPath}`);
}
}
/**
* Extracts package name from registered services.
*/
function getPrimaryPackage() {
const packages = new Set();
for (const service of serviceRegistry.values()) {
if (service.package) {
packages.add(service.package);
}
}
if (packages.has('aiserver.v1')) {
return 'aiserver.v1';
}
return packages.size > 0 ? Array.from(packages)[0] : 'aiserver.v1';
}
/**
* Main extraction function.
*/
async function runExtraction(registry) {
console.log('Processing services...');
let processedCount = 0;
let errorCount = 0;
for (const [, serviceDefinition] of Object.entries(registry)) {
if (!serviceDefinition?.typeName || !serviceDefinition?.methods) continue;
try {
registerService(serviceDefinition);
processedCount++;
} catch (error) {
console.error(`Error processing service ${serviceDefinition.typeName}: ${error.message}`);
errorCount++;
}
}
console.log(`Processed ${processedCount} services (${errorCount} errors)`);
const outPath = process.argv[process.argv.length - 1];
if (!outPath || outPath.startsWith('-')) {
throw new Error('Invalid output path. Usage: node postprocess.js <output-path>');
}
const primaryPackage = getPrimaryPackage();
const lines = [
'syntax = "proto3";',
`package ${primaryPackage};`,
`option go_package = "cursor/gen/${primaryPackage.replace('.', '/')};${primaryPackage.replace('.', '')}";`,
'',
];
for (const e of enumRegistry.values()) {
lines.push(...e.lines, '');
}
for (const t of typeRegistry.values()) {
lines.push(...t.lines, '');
}
for (const s of serviceRegistry.values()) {
lines.push(...s.lines, '');
}
const protoContent = lines.join('\n');
try {
await ensureDir(outPath);
const dirPath = path.join(outPath, "aiserver", "v1");
await ensureDir(dirPath);
const protoFilePath = path.join(dirPath, 'aiserver.proto');
await fspromises.writeFile(protoFilePath, protoContent);
console.log(`Generated ${protoFilePath}`);
console.log(` - ${enumRegistry.size} enums`);
console.log(` - ${typeRegistry.size} messages`);
console.log(` - ${serviceRegistry.size} services`);
clearTimeout(exitTimer);
process.exit(0);
} catch (error) {
throw new Error(`Failed to write proto file: ${error.message}`);
}
}
console.log('Starting service registry detection...');
findRegistriesWithRetry()
.then(registry => {
if (!registry) {
console.error('Could not find any service registries after all retries');
process.exit(1);
}
return runExtraction(registry);
})
.catch(error => {
console.error(`Extraction failed: ${error.message}`);
process.exit(1);
});