The best way to control Claude Code, Codex and more from any computer or phone

main
ts 952 lines 31.2 KB
Raw
1 #!/usr/bin/env node
2
3 import { pipe } from "effect/Function";
4 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import { make as makeJsonSchemaGenerator } from "@effect/openapi-generator/JsonSchemaGenerator";
7 import * as Effect from "effect/Effect";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Layer from "effect/Layer";
10 import * as Logger from "effect/Logger";
11 import * as Path from "effect/Path";
12 import * as Schema from "effect/Schema";
13 import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
14 import { ChildProcess, ChildProcessSpawner } from "effect/process";
15
16 const UPSTREAM_REF = "687a119f0fcaace47e1f1abcc77cec6c813fd6da";
17 const USER_AGENT = "effect-codex-app-server-generator";
18 const GITHUB_API_BASE =
19 "https://api.github.com/repos/openai/codex/contents/codex-rs/app-server-protocol";
20
21 const GithubContentEntries = Schema.Array(
22 Schema.Struct({
23 name: Schema.String,
24 path: Schema.String,
25 download_url: Schema.NullOr(Schema.String),
26 type: Schema.String,
27 }),
28 );
29 type GithubContentEntry = (typeof GithubContentEntries.Type)[number];
30
31 const JsonSchemaDocument = Schema.StructWithRest(
32 Schema.Struct({
33 definitions: Schema.optionalKey(Schema.Record(Schema.String, Schema.Json)),
34 }),
35 [Schema.Record(Schema.String, Schema.Json)],
36 );
37 const decodeGithubContentEntries = Schema.decodeEffect(Schema.fromJsonString(GithubContentEntries));
38 const decodeJsonSchemaDocument = Schema.decodeEffect(Schema.fromJsonString(JsonSchemaDocument));
39
40 interface GeneratedPaths {
41 readonly generatedDir: string;
42 readonly schemaOutputPath: string;
43 readonly metaOutputPath: string;
44 readonly namespacesOutputPath: string;
45 }
46
47 interface MethodEntry {
48 readonly method: string;
49 readonly paramsType?: string;
50 }
51
52 interface JsonSchemaFile {
53 readonly namespace?: string;
54 readonly exportName: string;
55 readonly fileName: string;
56 readonly downloadUrl: string;
57 readonly qualifiedName: string;
58 }
59
60 class GeneratorError extends Schema.TaggedError<GeneratorError>()("GeneratorError", {
61 detail: Schema.String,
62 cause: Schema.optional(Schema.Defect()),
63 }) {
64 override get message() {
65 return this.detail;
66 }
67 }
68
69 // Codex leaves experimental requests out of its published schema.
70 const ManualClientRequests: ReadonlyArray<MethodEntry> = [
71 { method: "thread/settings/update", paramsType: "ThreadSettingsUpdateParams" },
72 ];
73
74 const ManualSchemas: Record<string, Schema.Json> = {
75 ThreadSettingsUpdateParams: {
76 type: "object",
77 title: "ThreadSettingsUpdateParams",
78 properties: {
79 threadId: { type: "string" },
80 serviceTier: {
81 anyOf: [{ type: "string" }, { type: "null" }],
82 },
83 },
84 required: ["threadId"],
85 },
86 ThreadSettingsUpdateResponse: {
87 type: "object",
88 title: "ThreadSettingsUpdateResponse",
89 },
90 GetAuthStatusParams: {
91 type: "object",
92 title: "GetAuthStatusParams",
93 properties: {
94 includeToken: {
95 anyOf: [{ type: "boolean" }, { type: "null" }],
96 },
97 refreshToken: {
98 anyOf: [{ type: "boolean" }, { type: "null" }],
99 },
100 },
101 },
102 GetConversationSummaryParams: {
103 title: "GetConversationSummaryParams",
104 oneOf: [
105 {
106 type: "object",
107 properties: {
108 rolloutPath: { type: "string" },
109 },
110 required: ["rolloutPath"],
111 },
112 {
113 type: "object",
114 properties: {
115 conversationId: { type: "string" },
116 },
117 required: ["conversationId"],
118 },
119 ],
120 },
121 GetConversationSummaryResponse: {
122 type: "object",
123 title: "GetConversationSummaryResponse",
124 properties: {
125 summary: {},
126 },
127 required: ["summary"],
128 },
129 GitDiffToRemoteParams: {
130 type: "object",
131 title: "GitDiffToRemoteParams",
132 properties: {
133 cwd: { type: "string" },
134 },
135 required: ["cwd"],
136 },
137 GitDiffToRemoteResponse: {
138 type: "object",
139 title: "GitDiffToRemoteResponse",
140 properties: {
141 sha: { type: "string" },
142 diff: { type: "string" },
143 },
144 required: ["sha", "diff"],
145 },
146 GetAuthStatusResponse: {
147 type: "object",
148 title: "GetAuthStatusResponse",
149 properties: {
150 authMethod: {
151 anyOf: [{}, { type: "null" }],
152 },
153 authToken: {
154 anyOf: [{ type: "string" }, { type: "null" }],
155 },
156 requiresOpenaiAuth: {
157 anyOf: [{ type: "boolean" }, { type: "null" }],
158 },
159 },
160 required: ["authMethod", "authToken", "requiresOpenaiAuth"],
161 },
162 };
163
164 // T3 Code only uses the plan for labels, so an unknown slug must not fail the whole
165 // `account/read` decode and take the provider down with it.
166 const DefinitionOverrides: Record<string, Schema.Json> = {
167 PlanType: { type: "string" },
168 // Newer Codex releases add hook events (e.g. `preModelRequest`); a closed enum drops their notifications.
169 HookEventName: { type: "string" },
170 // Newer custom Codex binaries emit partial answers before additional tools or text.
171 MessagePhase: {
172 description:
173 'Classifies an assistant message as interim commentary or final answer text.\n\nProviders do not emit this consistently, so callers must treat `None` as "phase unknown" and keep compatibility behavior for legacy models.',
174 oneOf: [
175 {
176 type: "string",
177 enum: ["commentary"],
178 description:
179 "Mid-turn assistant text (for example preamble/progress narration).\n\nAdditional tool calls or assistant output may follow before turn completion.",
180 },
181 {
182 type: "string",
183 enum: ["partial_answer"],
184 description: "Stable answer text that may be followed by more assistant output or tools.",
185 },
186 {
187 type: "string",
188 enum: ["final_answer"],
189 description: "The assistant's terminal answer text for the current turn.",
190 },
191 ],
192 },
193 };
194
195 // Codex sends error codes this ref does not list yet (e.g. `modelNotFound`). A closed enum
196 // fails the whole notification decode, which drops a failed `turn/completed` and leaves the turn running.
197 const OpenStringEnumDefinitions = new Set(["CodexErrorInfo"]);
198
199 function openStringEnum(schema: Schema.Json): Schema.Json {
200 if (!isJsonSchemaNode(schema) || !Array.isArray(schema.oneOf)) return schema;
201 return {
202 ...schema,
203 oneOf: schema.oneOf.map((variant: Schema.Json) =>
204 isJsonSchemaNode(variant) && variant.type === "string" && Array.isArray(variant.enum)
205 ? { type: "string" }
206 : variant,
207 ),
208 };
209 }
210
211 // Newer Codex identifies a generated image by `fileId` and omits the inline `result`.
212 // Requiring `result` fails the whole notification decode and drops the finished image.
213 function acceptImageFileIds(schema: Schema.Json): Schema.Json {
214 if (!isJsonSchemaNode(schema) || !Array.isArray(schema.oneOf)) return schema;
215 return {
216 ...schema,
217 oneOf: schema.oneOf.map((variant: Schema.Json) => {
218 if (
219 !isJsonSchemaNode(variant) ||
220 !isJsonSchemaNode(variant.properties) ||
221 !isJsonSchemaNode(variant.properties.type) ||
222 !Array.isArray(variant.properties.type.enum) ||
223 !variant.properties.type.enum.includes("imageGeneration") ||
224 !Array.isArray(variant.required)
225 ) {
226 return variant;
227 }
228 return {
229 ...variant,
230 properties: { ...variant.properties, fileId: { type: "string" } },
231 required: variant.required.filter((key: Schema.Json) => key !== "result"),
232 };
233 }),
234 };
235 }
236
237 const getGeneratedPaths = Effect.fn("getGeneratedPaths")(function* () {
238 const path = yield* Path.Path;
239 const generatedDir = path.join(import.meta.dirname, "..", "src", "_generated");
240 return {
241 generatedDir,
242 schemaOutputPath: path.join(generatedDir, "schema.gen.ts"),
243 metaOutputPath: path.join(generatedDir, "meta.gen.ts"),
244 namespacesOutputPath: path.join(generatedDir, "namespaces.gen.ts"),
245 } satisfies GeneratedPaths;
246 });
247
248 const ensureGeneratedDir = Effect.fn("ensureGeneratedDir")(function* () {
249 const fs = yield* FileSystem.FileSystem;
250 const { generatedDir } = yield* getGeneratedPaths();
251 yield* fs.makeDirectory(generatedDir, { recursive: true });
252 });
253
254 const githubToken = process.env.GITHUB_TOKEN;
255
256 const fetchText = Effect.fn("fetchText")(function* (url: string) {
257 return yield* HttpClientRequest.get(url).pipe(
258 HttpClientRequest.setHeader("user-agent", USER_AGENT),
259 githubToken && url.startsWith("https://api.github.com/")
260 ? HttpClientRequest.bearerToken(githubToken)
261 : (request) => request,
262 HttpClient.execute,
263 Effect.flatMap(HttpClientResponse.filterStatusOk),
264 Effect.flatMap((okResponse) => okResponse.text),
265 Effect.mapError(
266 (cause) =>
267 new GeneratorError({
268 detail: `Failed to fetch ${url}`,
269 cause,
270 }),
271 ),
272 );
273 });
274
275 const fetchDirectoryEntries = Effect.fn("fetchDirectoryEntries")(function* (path: string) {
276 const raw = yield* fetchText(`${GITHUB_API_BASE}/${path}?ref=${UPSTREAM_REF}`);
277 return yield* decodeGithubContentEntries(raw);
278 });
279
280 function collectSchemaEntries(
281 chunk: string,
282 ): ReadonlyArray<{ readonly name: string; readonly code: string }> {
283 const lines = chunk
284 .split("\n")
285 .map((line) => line.trim())
286 .filter((line) => line.length > 0 && !line.startsWith("//"));
287 const entries: Array<{ name: string; code: string }> = [];
288
289 for (let index = 0; index < lines.length; index += 1) {
290 const typeLine = lines[index];
291 if (!typeLine?.startsWith("export type ")) {
292 continue;
293 }
294
295 const constLine = lines[index + 1];
296 if (!constLine?.startsWith("export const ")) {
297 throw new Error(`Malformed generator output near: ${typeLine}`);
298 }
299
300 const match = /^export type ([A-Za-z0-9_]+)/.exec(typeLine);
301 if (!match?.[1]) {
302 throw new Error(`Could not extract schema name from: ${typeLine}`);
303 }
304
305 entries.push({
306 name: match[1],
307 code: `${typeLine}\n${constLine}`,
308 });
309 index += 1;
310 }
311
312 return entries;
313 }
314
315 function normalizeNullableTypes(value: Schema.Json): Schema.Json {
316 if (Array.isArray(value)) {
317 return value.map(normalizeNullableTypes);
318 }
319 if (value === null || typeof value !== "object") {
320 return value;
321 }
322
323 const normalizedEntries = Object.entries(value).map(([key, child]) => [
324 key,
325 normalizeNullableTypes(child),
326 ]);
327 const normalizedObject = Object.fromEntries(normalizedEntries) as Record<string, Schema.Json>;
328 const typeValue = normalizedObject.type;
329
330 if (!Array.isArray(typeValue)) {
331 return normalizedObject;
332 }
333
334 const normalizedTypes = typeValue.filter((entry): entry is string => typeof entry === "string");
335 if (normalizedTypes.length !== typeValue.length || !normalizedTypes.includes("null")) {
336 return normalizedObject;
337 }
338
339 const nonNullTypes = normalizedTypes.filter((entry) => entry !== "null");
340 if (nonNullTypes.length !== 1) {
341 return normalizedObject;
342 }
343 const nonNullType = nonNullTypes[0]!;
344
345 const nextObject: Record<string, Schema.Json> = {};
346 for (const [key, child] of Object.entries(normalizedObject)) {
347 if (key !== "type") {
348 nextObject[key] = child;
349 }
350 }
351
352 return {
353 anyOf: [
354 {
355 ...nextObject,
356 type: nonNullType,
357 },
358 { type: "null" },
359 ],
360 };
361 }
362
363 function stripNullDefaults(value: Schema.Json): Schema.Json {
364 if (Array.isArray(value)) {
365 return value.map(stripNullDefaults);
366 }
367 if (value === null || typeof value !== "object") {
368 return value;
369 }
370
371 return Object.fromEntries(
372 Object.entries(value)
373 .filter(([key, child]) => !(key === "default" && child === null))
374 .map(([key, child]) => [key, stripNullDefaults(child)]),
375 ) as Schema.Json;
376 }
377
378 type JsonSchemaNode = { readonly [key: string]: Schema.Json };
379
380 function isJsonSchemaNode(value: Schema.Json | undefined): value is JsonSchemaNode {
381 return value !== null && typeof value === "object" && !Array.isArray(value);
382 }
383
384 // Visit only schema positions so fields literally named properties stay untouched.
385 // Close Codex structs that omit additionalProperties; Effect otherwise imports open records.
386 // Fold shared fields into each object alternative because Effect cannot intersect them.
387 function adaptSchemaForEffect(value: Schema.Json): Schema.Json {
388 if (!isJsonSchemaNode(value)) {
389 return value;
390 }
391 const node: Record<string, Schema.Json> = { ...value };
392 for (const key of ["items", "additionalProperties"]) {
393 const child = node[key];
394 if (isJsonSchemaNode(child)) node[key] = adaptSchemaForEffect(child);
395 }
396 for (const key of ["anyOf", "oneOf", "allOf"]) {
397 const child = node[key];
398 if (Array.isArray(child)) node[key] = child.map(adaptSchemaForEffect);
399 }
400 for (const key of ["properties", "definitions"]) {
401 const child = node[key];
402 if (isJsonSchemaNode(child)) {
403 node[key] = Object.fromEntries(
404 Object.entries(child).map(([name, schema]) => [name, adaptSchemaForEffect(schema)]),
405 );
406 }
407 }
408
409 const { properties } = node;
410 if (
411 isJsonSchemaNode(properties) &&
412 Object.keys(properties).length > 0 &&
413 !("additionalProperties" in node)
414 ) {
415 node.additionalProperties = false;
416 }
417
418 const alternativesKey = "anyOf" in node ? "anyOf" : "oneOf";
419 const alternatives = node[alternativesKey];
420 if (
421 !isJsonSchemaNode(properties) ||
422 !Array.isArray(alternatives) ||
423 !alternatives.every(
424 (alternative) => isJsonSchemaNode(alternative) && alternative.type === "object",
425 )
426 ) {
427 return node;
428 }
429 const {
430 properties: _shared,
431 required,
432 type: _type,
433 additionalProperties: _closed,
434 ...rest
435 } = node;
436 const sharedRequired = Array.isArray(required) ? required : [];
437 return {
438 ...rest,
439 [alternativesKey]: alternatives.map((alternative) => {
440 const branch = alternative as JsonSchemaNode;
441 const branchProperties = isJsonSchemaNode(branch.properties) ? branch.properties : {};
442 const branchRequired = Array.isArray(branch.required) ? branch.required : [];
443 return {
444 ...branch,
445 properties: { ...properties, ...branchProperties },
446 required: [...new Set([...sharedRequired, ...branchRequired])],
447 };
448 }),
449 };
450 }
451
452 function toPascalCaseMethod(method: string) {
453 return method
454 .split("/")
455 .flatMap((segment) => segment.split(/(?=[A-Z])/))
456 .flatMap((segment) => segment.split(/[-_]/))
457 .filter(Boolean)
458 .map((segment) => segment[0]!.toUpperCase() + segment.slice(1))
459 .join("");
460 }
461
462 function parseRequestEntries(fileContents: string): ReadonlyArray<MethodEntry> {
463 const entryPattern = /\{\s*"method":\s*"([^"]+)",\s*id:\s*RequestId,\s*params(\??):\s*([^,}|]+)/g;
464 const entries: Array<MethodEntry> = [];
465 let match: RegExpExecArray | null;
466 while ((match = entryPattern.exec(fileContents)) !== null) {
467 entries.push({
468 method: match[1]!,
469 paramsType: `${match[2] ? "Nullable" : ""}${match[3]!.trim()}`,
470 });
471 }
472 return entries;
473 }
474
475 function parseNotificationEntries(fileContents: string): ReadonlyArray<MethodEntry> {
476 const entryPattern = /\{\s*"method":\s*"([^"]+)"(?:,\s*"params":\s*([^ }]+))?\s*\}/g;
477 const entries: Array<MethodEntry> = [];
478 let match: RegExpExecArray | null;
479 while ((match = entryPattern.exec(fileContents)) !== null) {
480 entries.push({
481 method: match[1]!,
482 ...(match[2] ? { paramsType: match[2].trim() } : {}),
483 });
484 }
485 return entries;
486 }
487
488 function resolveSchemaTypeName(
489 rawTypeName: string,
490 generatedSchemaNames: ReadonlySet<string>,
491 ): string {
492 if (rawTypeName === "undefined") {
493 return "undefined";
494 }
495
496 const candidates = [
497 rawTypeName,
498 `V2${rawTypeName}`,
499 `V1${rawTypeName}`,
500 `SerdeJson${rawTypeName}`,
501 ];
502 for (const candidate of candidates) {
503 if (generatedSchemaNames.has(candidate)) {
504 return candidate;
505 }
506 }
507
508 throw new Error(`Unable to resolve schema type name: ${rawTypeName}`);
509 }
510
511 function resolveResponseTypeName(
512 method: string,
513 paramsType: string | undefined,
514 generatedSchemaNames: ReadonlySet<string>,
515 ): string {
516 const overrides: Record<string, string> = {
517 "account/gatewayOAuth/cancel": "GatewayOAuthCancelResponse",
518 "account/gatewayOAuth/login": "GatewayOAuthLoginResponse",
519 "account/gatewayOAuth/read": "GatewayOAuthReadResponse",
520 "account/logout": "LogoutAccountResponse",
521 "account/rateLimits/read": "GetAccountRateLimitsResponse",
522 "account/usage/read": "GetAccountTokenUsageResponse",
523 "account/workspaceMessages/read": "GetWorkspaceMessagesResponse",
524 "config/batchWrite": "ConfigWriteResponse",
525 "config/mcpServer/reload": "McpServerRefreshResponse",
526 "config/value/write": "ConfigWriteResponse",
527 "configRequirements/read": "ConfigRequirementsReadResponse",
528 "externalAgentConfig/import/readHistories": "ExternalAgentConfigImportHistoriesReadResponse",
529 };
530
531 const override = overrides[method];
532 if (override) {
533 return resolveSchemaTypeName(override, generatedSchemaNames);
534 }
535
536 if (paramsType && paramsType !== "undefined") {
537 const fromParams = paramsType.replace(/Params$/, "Response");
538 try {
539 return resolveSchemaTypeName(fromParams, generatedSchemaNames);
540 } catch {}
541 }
542
543 return resolveSchemaTypeName(`${toPascalCaseMethod(method)}Response`, generatedSchemaNames);
544 }
545
546 function renderMethodConstants(constantName: string, entries: ReadonlyArray<MethodEntry>) {
547 return [
548 `export const ${constantName} = {`,
549 ...entries.map(
550 (entry) => ` ${JSON.stringify(entry.method)}: ${JSON.stringify(entry.method)},`,
551 ),
552 "} as const;",
553 "",
554 ].join("\n");
555 }
556
557 function renderTypeInterface(
558 interfaceName: string,
559 entries: ReadonlyArray<MethodEntry>,
560 typeName: (entry: MethodEntry) => string,
561 ) {
562 return [
563 `export interface ${interfaceName} {`,
564 ...entries.map((entry) => ` readonly ${JSON.stringify(entry.method)}: ${typeName(entry)};`),
565 "}",
566 "",
567 ].join("\n");
568 }
569
570 function renderSchemaMap(
571 constantName: string,
572 entries: ReadonlyArray<MethodEntry>,
573 typeName: (entry: MethodEntry) => string,
574 ) {
575 return [
576 `export const ${constantName} = {`,
577 ...entries.map((entry) => {
578 const schemaName = typeName(entry);
579 return ` ${JSON.stringify(entry.method)}: ${
580 schemaName === "undefined" ? "undefined" : `CodexSchema.${schemaName}`
581 },`;
582 }),
583 "} as const;",
584 "",
585 ].join("\n");
586 }
587
588 function renderSchemaTypeReference(schemaName: string) {
589 return schemaName === "undefined" ? "undefined" : `CodexSchema.${schemaName}`;
590 }
591
592 function exportNameForPath(filePath: string): string {
593 const relative = filePath.replace(/^schema\/json\//, "").replace(/\.json$/, "");
594 if (!relative.includes("/")) {
595 return relative;
596 }
597
598 const [namespace, name] = relative.split("/", 2) as [string, string];
599 const namespacePrefix = namespace
600 .split(/[^A-Za-z0-9]+/)
601 .filter(Boolean)
602 .map((segment) => segment[0]!.toUpperCase() + segment.slice(1))
603 .join("");
604 return `${namespacePrefix}${name}`;
605 }
606
607 function buildJsonSchemaFiles(
608 entries: ReadonlyArray<GithubContentEntry>,
609 ): ReadonlyArray<JsonSchemaFile> {
610 return entries
611 .filter(
612 (entry) =>
613 entry.type === "file" &&
614 entry.name.endsWith(".json") &&
615 entry.download_url !== null &&
616 !entry.name.startsWith("codex_app_server_protocol."),
617 )
618 .map((entry) => {
619 const relative = entry.path.replace(/^codex-rs\/app-server-protocol\/schema\/json\//, "");
620 const parts = relative.split("/");
621 if (parts.length > 1) {
622 return {
623 namespace: parts[0]!,
624 exportName: exportNameForPath(relative),
625 fileName: entry.name,
626 downloadUrl: entry.download_url!,
627 qualifiedName: relative.replace(/\.json$/, ""),
628 } satisfies JsonSchemaFile;
629 }
630 return {
631 exportName: exportNameForPath(relative),
632 fileName: entry.name,
633 downloadUrl: entry.download_url!,
634 qualifiedName: relative.replace(/\.json$/, ""),
635 } satisfies JsonSchemaFile;
636 });
637 }
638
639 function rewriteExternalRefs(
640 value: Schema.Json,
641 localDefinitionNames: ReadonlyMap<string, string>,
642 currentNamespace: string | undefined,
643 exportNameByQualifiedName: ReadonlyMap<string, string>,
644 ): Schema.Json {
645 if (Array.isArray(value)) {
646 return value.map((entry) =>
647 rewriteExternalRefs(entry, localDefinitionNames, currentNamespace, exportNameByQualifiedName),
648 );
649 }
650 if (value === null || typeof value !== "object") {
651 return value;
652 }
653
654 return Object.fromEntries(
655 Object.entries(value).map(([key, child]) => {
656 if (key === "$ref" && typeof child === "string" && child.startsWith("#/definitions/")) {
657 const definitionName = child.slice("#/definitions/".length);
658 const localRewrite = localDefinitionNames.get(definitionName);
659 if (localRewrite) {
660 return [key, `#/components/schemas/${localRewrite}`];
661 }
662
663 const candidates = [
664 ...(currentNamespace ? [`${currentNamespace}/${definitionName}`] : []),
665 definitionName,
666 definitionName.replace(/^v[12]\//, ""),
667 definitionName.replace(/^serde_json\//, ""),
668 `v2/${definitionName}`,
669 `v1/${definitionName}`,
670 `serde_json/${definitionName}`,
671 ];
672
673 const rewritten = candidates
674 .map((candidate) => exportNameByQualifiedName.get(candidate))
675 .find((candidate) => candidate !== undefined);
676
677 if (!rewritten) {
678 throw new Error(`Missing rewritten definition for ref: ${child}`);
679 }
680
681 return [key, `#/components/schemas/${rewritten}`];
682 }
683
684 return [
685 key,
686 rewriteExternalRefs(
687 child,
688 localDefinitionNames,
689 currentNamespace,
690 exportNameByQualifiedName,
691 ),
692 ];
693 }),
694 ) as Schema.Json;
695 }
696
697 const generateFiles = Effect.fn("generateFiles")(function* () {
698 yield* ensureGeneratedDir();
699
700 const [rootJsonEntries, v1JsonEntries, v2JsonEntries] = yield* Effect.all([
701 fetchDirectoryEntries("schema/json"),
702 fetchDirectoryEntries("schema/json/v1"),
703 fetchDirectoryEntries("schema/json/v2"),
704 ]);
705
706 const jsonSchemaFiles = [
707 ...buildJsonSchemaFiles(rootJsonEntries),
708 ...buildJsonSchemaFiles(v1JsonEntries),
709 ...buildJsonSchemaFiles(v2JsonEntries),
710 ].toSorted((left, right) => left.exportName.localeCompare(right.exportName));
711
712 const exportNameByQualifiedName = new Map(
713 jsonSchemaFiles.map((file) => [file.qualifiedName, file.exportName]),
714 );
715 const aggregateSchemas: Record<string, Schema.Json> = {};
716
717 for (const file of jsonSchemaFiles) {
718 const raw = yield* fetchText(file.downloadUrl);
719 const parsed = yield* decodeJsonSchemaDocument(raw);
720 const localDefinitionNames = new Map(
721 Object.keys(parsed.definitions ?? {}).map((definitionName) => [
722 definitionName,
723 `${file.exportName}__${definitionName.replace(/[^A-Za-z0-9]/g, "")}`,
724 ]),
725 );
726
727 for (const [definitionName, definitionSchema] of Object.entries(parsed.definitions ?? {})) {
728 aggregateSchemas[localDefinitionNames.get(definitionName)!] = pipe(
729 rewriteExternalRefs(
730 DefinitionOverrides[definitionName] ??
731 (OpenStringEnumDefinitions.has(definitionName)
732 ? openStringEnum(definitionSchema)
733 : definitionName === "ThreadItem"
734 ? acceptImageFileIds(definitionSchema)
735 : definitionSchema),
736 localDefinitionNames,
737 file.namespace,
738 exportNameByQualifiedName,
739 ),
740 normalizeNullableTypes,
741 stripNullDefaults,
742 );
743 }
744
745 const topLevelSchema: Record<string, Schema.Json> = {};
746 for (const [key, value] of Object.entries(parsed)) {
747 if (key !== "definitions") {
748 topLevelSchema[key] = value;
749 }
750 }
751
752 aggregateSchemas[file.exportName] = pipe(
753 rewriteExternalRefs(
754 topLevelSchema,
755 localDefinitionNames,
756 file.namespace,
757 exportNameByQualifiedName,
758 ),
759 normalizeNullableTypes,
760 stripNullDefaults,
761 );
762 }
763
764 for (const [name, schema] of Object.entries(ManualSchemas)) {
765 if (!(name in aggregateSchemas)) {
766 aggregateSchemas[name] = stripNullDefaults(normalizeNullableTypes(schema));
767 }
768 }
769
770 const generator = makeJsonSchemaGenerator();
771 for (const [name, schema] of Object.entries(aggregateSchemas).toSorted(([left], [right]) =>
772 left.localeCompare(right),
773 )) {
774 aggregateSchemas[name] = adaptSchemaForEffect(schema);
775 generator.addSchema(name, aggregateSchemas[name] as never);
776 }
777
778 const generatedEntries = new Map<string, string>();
779 const output = generator.generate("openapi-3.1", aggregateSchemas as never, false).trim();
780 if (output.length > 0) {
781 for (const entry of collectSchemaEntries(output)) {
782 if (!generatedEntries.has(entry.name)) {
783 generatedEntries.set(entry.name, entry.code);
784 }
785 }
786 }
787
788 const generatedSchemaNames = new Set(generatedEntries.keys());
789 const clientRequestRaw = yield* fetchText(
790 `https://raw.githubusercontent.com/openai/codex/${UPSTREAM_REF}/codex-rs/app-server-protocol/schema/typescript/ClientRequest.ts`,
791 );
792 const clientNotificationRaw = yield* fetchText(
793 `https://raw.githubusercontent.com/openai/codex/${UPSTREAM_REF}/codex-rs/app-server-protocol/schema/typescript/ClientNotification.ts`,
794 );
795 const serverRequestRaw = yield* fetchText(
796 `https://raw.githubusercontent.com/openai/codex/${UPSTREAM_REF}/codex-rs/app-server-protocol/schema/typescript/ServerRequest.ts`,
797 );
798 const serverNotificationRaw = yield* fetchText(
799 `https://raw.githubusercontent.com/openai/codex/${UPSTREAM_REF}/codex-rs/app-server-protocol/schema/typescript/ServerNotification.ts`,
800 );
801
802 const clientRequestEntries = [...parseRequestEntries(clientRequestRaw), ...ManualClientRequests];
803 const clientNotificationEntries = parseNotificationEntries(clientNotificationRaw);
804 const serverRequestEntries = parseRequestEntries(serverRequestRaw);
805 const serverNotificationEntries = parseNotificationEntries(serverNotificationRaw);
806
807 const prelude = [
808 "// This file is generated by the effect-codex-app-server package. Do not edit manually.",
809 `// Upstream protocol ref: ${UPSTREAM_REF}`,
810 "",
811 ];
812
813 const schemaOutput = [
814 ...prelude,
815 'import * as Schema from "effect/Schema";',
816 "",
817 [...generatedEntries.values()].join("\n\n"),
818 "",
819 ].join("\n");
820
821 const metaOutput = [
822 ...prelude,
823 'import * as CodexSchema from "./schema.gen.ts";',
824 "",
825 renderMethodConstants("CLIENT_REQUEST_METHODS", clientRequestEntries),
826 renderMethodConstants("CLIENT_NOTIFICATION_METHODS", clientNotificationEntries),
827 renderMethodConstants("SERVER_REQUEST_METHODS", serverRequestEntries),
828 renderMethodConstants("SERVER_NOTIFICATION_METHODS", serverNotificationEntries),
829 "export type ClientRequestMethod = keyof typeof CLIENT_REQUEST_METHODS;",
830 "export type ClientNotificationMethod = keyof typeof CLIENT_NOTIFICATION_METHODS;",
831 "export type ServerRequestMethod = keyof typeof SERVER_REQUEST_METHODS;",
832 "export type ServerNotificationMethod = keyof typeof SERVER_NOTIFICATION_METHODS;",
833 "",
834 renderTypeInterface("ClientRequestParamsByMethod", clientRequestEntries, (entry) =>
835 renderSchemaTypeReference(
836 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
837 ),
838 ),
839 renderTypeInterface("ClientRequestResponsesByMethod", clientRequestEntries, (entry) =>
840 renderSchemaTypeReference(
841 resolveResponseTypeName(entry.method, entry.paramsType, generatedSchemaNames),
842 ),
843 ),
844 renderTypeInterface("ClientNotificationParamsByMethod", clientNotificationEntries, (entry) =>
845 renderSchemaTypeReference(
846 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
847 ),
848 ),
849 renderTypeInterface("ServerRequestParamsByMethod", serverRequestEntries, (entry) =>
850 renderSchemaTypeReference(
851 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
852 ),
853 ),
854 renderTypeInterface("ServerRequestResponsesByMethod", serverRequestEntries, (entry) =>
855 renderSchemaTypeReference(
856 resolveResponseTypeName(entry.method, entry.paramsType, generatedSchemaNames),
857 ),
858 ),
859 renderTypeInterface("ServerNotificationParamsByMethod", serverNotificationEntries, (entry) =>
860 renderSchemaTypeReference(
861 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
862 ),
863 ),
864 renderSchemaMap("CLIENT_REQUEST_PARAMS", clientRequestEntries, (entry) =>
865 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
866 ),
867 renderSchemaMap("CLIENT_REQUEST_RESPONSES", clientRequestEntries, (entry) =>
868 resolveResponseTypeName(entry.method, entry.paramsType, generatedSchemaNames),
869 ),
870 renderSchemaMap("CLIENT_NOTIFICATION_PARAMS", clientNotificationEntries, (entry) =>
871 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
872 ),
873 renderSchemaMap("SERVER_REQUEST_PARAMS", serverRequestEntries, (entry) =>
874 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
875 ),
876 renderSchemaMap("SERVER_REQUEST_RESPONSES", serverRequestEntries, (entry) =>
877 resolveResponseTypeName(entry.method, entry.paramsType, generatedSchemaNames),
878 ),
879 renderSchemaMap("SERVER_NOTIFICATION_PARAMS", serverNotificationEntries, (entry) =>
880 resolveSchemaTypeName(entry.paramsType ?? "undefined", generatedSchemaNames),
881 ),
882 ].join("\n");
883
884 const namespaceGroups = new Map<string, Array<JsonSchemaFile>>();
885 for (const file of jsonSchemaFiles) {
886 if (!file.namespace) {
887 continue;
888 }
889 const current = namespaceGroups.get(file.namespace) ?? [];
890 current.push(file);
891 namespaceGroups.set(file.namespace, current);
892 }
893
894 const namespacesOutput = [
895 ...prelude,
896 'import * as CodexSchema from "./schema.gen.ts";',
897 "",
898 ...[...namespaceGroups.entries()]
899 .toSorted(([left], [right]) => left.localeCompare(right))
900 .map(([namespace, files]) => {
901 const constantName = namespace.replace(/[^A-Za-z0-9]/g, "");
902 return [
903 `export const ${constantName} = {`,
904 ...files
905 .toSorted((left, right) => left.fileName.localeCompare(right.fileName))
906 .map(
907 (file) =>
908 ` ${JSON.stringify(file.fileName.replace(/\.json$/, ""))}: CodexSchema.${file.exportName},`,
909 ),
910 "} as const;",
911 "",
912 ].join("\n");
913 }),
914 ].join("\n");
915
916 const fs = yield* FileSystem.FileSystem;
917 const { generatedDir, metaOutputPath, namespacesOutputPath, schemaOutputPath } =
918 yield* getGeneratedPaths();
919 yield* fs.writeFileString(schemaOutputPath, schemaOutput);
920 yield* fs.writeFileString(metaOutputPath, metaOutput);
921 yield* fs.writeFileString(namespacesOutputPath, namespacesOutput);
922
923 yield* Effect.log(`Generated Codex App Server schemas from ${UPSTREAM_REF}`);
924
925 yield* Effect.service(ChildProcessSpawner.ChildProcessSpawner).pipe(
926 Effect.flatMap((spawner) =>
927 spawner.spawn(ChildProcess.make("vp", ["fmt", generatedDir, "--write"])),
928 ),
929 Effect.flatMap((child) => child.exitCode),
930 Effect.tap((code) =>
931 code === 0
932 ? Effect.void
933 : Effect.fail(
934 new GeneratorError({
935 detail: `vp fmt failed with exit code ${code}`,
936 }),
937 ),
938 ),
939 );
940 });
941
942 generateFiles().pipe(
943 Effect.scoped,
944 Effect.provide(
945 Layer.mergeAll(
946 Logger.layer([Logger.consolePretty()]),
947 NodeServices.layer,
948 FetchHttpClient.layer,
949 ),
950 ),
951 NodeRuntime.runMain,
952 );