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