@niwa-labs/harness-codex 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -2
- package/package.json +20 -4
- package/scripts/gen-codex.ts +352 -0
- package/src/control.ts +1596 -0
- package/src/fixtures.ts +103 -0
- package/src/format.ts +196 -0
- package/src/incoming.ts +273 -0
- package/src/index.ts +128 -0
- package/src/paths.ts +42 -0
- package/src/protocol/generated/app-server.ts +2818 -0
- package/src/protocol/index.ts +106 -0
- package/src/reading.ts +940 -0
- package/src/refusals.ts +72 -0
- package/src/reverse.ts +243 -0
- package/src/skills.ts +6 -0
- package/src/tools.ts +69 -0
- package/test/codex-conformance.test.ts +29 -0
- package/test/codex-control-process.test.ts +1239 -0
- package/test/codex-control.test.ts +217 -0
- package/test/codex-elicitation.test.ts +71 -0
- package/test/codex-protocol.test.ts +245 -0
- package/test/codex-reading.test.ts +513 -0
- package/test/fixtures/codex-app-server/captured.jsonl +21 -0
- package/test/fixtures/codex-frames.ts +88 -0
- package/test/fixtures/zod-instance.ts +35 -0
- package/test/profile.test.ts +48 -0
- package/tsconfig.json +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Niwa Labs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @niwa-labs/harness-codex
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Codex harness kind. The reading side lists and reads the rollouts Codex writes to
|
|
4
|
+
`~/.codex`; the control side drives `codex app-server` over JSON-RPC. It has no insert side.
|
|
5
|
+
|
|
6
|
+
The zod schemas of the app-server protocol in `src/protocol/generated/` are generated from the
|
|
7
|
+
installed codex with `bun run gen:codex`.
|
|
8
|
+
|
|
9
|
+
Codex supports the default tool mode and `tools: "all"`. Its app-server thread API
|
|
10
|
+
cannot enforce an exact tool list across built-ins, feature groups and configured
|
|
11
|
+
MCP servers. Starts and cold resumes requesting `niwa-only` or `{ allow: [...] }`
|
|
12
|
+
return `Unsupported` before launching a process.
|
|
13
|
+
|
|
14
|
+
For a session-supplied MCP server, Codex uses the exact tool names Niwa supplies
|
|
15
|
+
as that server's `enabled_tools` on start and cold resume. An absent list leaves
|
|
16
|
+
the server's tool selection to Codex. An empty list exposes no tools from that
|
|
17
|
+
server.
|
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@niwa-labs/harness-codex",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.ts"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"gen:codex": "bun scripts/gen-codex.ts"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@niwa-labs/core": "0.0.0",
|
|
14
|
+
"@niwa-labs/harness": "0.0.0",
|
|
15
|
+
"@niwa-labs/sandbox": "0.0.0",
|
|
16
|
+
"zod": "^4.6.7"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@niwa-labs/testing": "0.0.0",
|
|
20
|
+
"json-schema-to-zod": "2.8.1"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates `src/protocol/generated/app-server.ts`: the zod schemas and types of
|
|
3
|
+
* what `codex app-server` sends to its client, from the JSON Schema of the installed codex.
|
|
4
|
+
*
|
|
5
|
+
* bun scripts/gen-codex.ts
|
|
6
|
+
*
|
|
7
|
+
* The schema is read with `codex app-server generate-json-schema --out <dir>`, the stable surface
|
|
8
|
+
* (no `--experimental`): this is the surface a client that does not opt into experimental methods
|
|
9
|
+
* is served. Nothing generated is edited by hand; a new codex is one more run of this script.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { spawnSync } from "node:child_process";
|
|
13
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
14
|
+
import { tmpdir } from "node:os";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
|
|
17
|
+
import { jsonSchemaToZod } from "json-schema-to-zod";
|
|
18
|
+
import type { JsonSchema, JsonSchemaObject, ParserOverride } from "json-schema-to-zod";
|
|
19
|
+
|
|
20
|
+
const OUTPUT = new URL("../src/protocol/generated/app-server.ts", import.meta.url);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The roots of what niwa reads or writes: what the server notifies and requests, the replies a
|
|
24
|
+
* client gives to those requests, and the JSON-RPC envelope that carries them. Each file brings
|
|
25
|
+
* the definitions it refers to.
|
|
26
|
+
*/
|
|
27
|
+
const ROOTS = [
|
|
28
|
+
"JSONRPCMessage.json",
|
|
29
|
+
"ServerNotification.json",
|
|
30
|
+
"ServerRequest.json",
|
|
31
|
+
"CommandExecutionRequestApprovalResponse.json",
|
|
32
|
+
"FileChangeRequestApprovalResponse.json",
|
|
33
|
+
"PermissionsRequestApprovalResponse.json",
|
|
34
|
+
"ToolRequestUserInputResponse.json",
|
|
35
|
+
"McpServerElicitationRequestResponse.json",
|
|
36
|
+
"DynamicToolCallResponse.json",
|
|
37
|
+
"ApplyPatchApprovalResponse.json",
|
|
38
|
+
"ExecCommandApprovalResponse.json",
|
|
39
|
+
] as const;
|
|
40
|
+
|
|
41
|
+
/** The widest line of the generated file before a call is broken over several. */
|
|
42
|
+
const WIDTH = 110;
|
|
43
|
+
|
|
44
|
+
const DEFINITION_REF = /^#\/definitions\/([A-Za-z_][A-Za-z0-9_]*)$/;
|
|
45
|
+
|
|
46
|
+
function isObject(schema: JsonSchema | undefined): schema is JsonSchemaObject {
|
|
47
|
+
return typeof schema === "object";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The name of the zod schema a definition is generated as. */
|
|
51
|
+
function schemaName(definition: string): string {
|
|
52
|
+
return `${definition}Schema`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The definition a `$ref` names. */
|
|
56
|
+
function refTarget(ref: string): string {
|
|
57
|
+
const match = DEFINITION_REF.exec(ref);
|
|
58
|
+
if (match?.[1] === undefined) throw new Error(`a reference this generator does not read: ${ref}`);
|
|
59
|
+
return match[1];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// -- the schema, shaped for json-schema-to-zod --------------------------------
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The property of a union that tells its variants apart: present in every variant as a string with
|
|
66
|
+
* one value, required, and with a different value in each variant. Zod refuses a discriminated
|
|
67
|
+
* union that has the same value twice, so a union that cannot say it is left a plain union.
|
|
68
|
+
*/
|
|
69
|
+
function discriminatorOf(branches: readonly JsonSchemaObject[]): string | undefined {
|
|
70
|
+
const [first] = branches;
|
|
71
|
+
if (first?.properties === undefined) return undefined;
|
|
72
|
+
for (const property of Object.keys(first.properties)) {
|
|
73
|
+
const values = new Set<unknown>();
|
|
74
|
+
const tells = branches.every((branch) => {
|
|
75
|
+
const schema = branch.properties?.[property];
|
|
76
|
+
const only = isObject(schema) && schema.type === "string" && schema.enum?.length === 1 ? schema.enum[0] : undefined;
|
|
77
|
+
if (only === undefined || !Array.isArray(branch.required) || !branch.required.includes(property)) return false;
|
|
78
|
+
values.add(only);
|
|
79
|
+
return true;
|
|
80
|
+
});
|
|
81
|
+
if (tells && values.size === branches.length) return property;
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The schema as the converter reads it. Objects are open, so a field a newer codex adds does not
|
|
88
|
+
* fail a message. A `oneOf` that carries properties of its own gives them to every variant. A
|
|
89
|
+
* `oneOf` is a discriminated union when its variants can be told apart by one property, and a
|
|
90
|
+
* plain union otherwise.
|
|
91
|
+
*/
|
|
92
|
+
function shaped(schema: JsonSchema): JsonSchema {
|
|
93
|
+
if (schema === true) return {};
|
|
94
|
+
if (!isObject(schema)) return schema;
|
|
95
|
+
const out: JsonSchemaObject = { ...schema };
|
|
96
|
+
if (out.additionalProperties === false) delete out.additionalProperties;
|
|
97
|
+
else if (isObject(out.additionalProperties)) out.additionalProperties = shaped(out.additionalProperties);
|
|
98
|
+
if (out.properties !== undefined) {
|
|
99
|
+
out.properties = Object.fromEntries(Object.entries(out.properties).map(([key, value]) => [key, shaped(value)]));
|
|
100
|
+
}
|
|
101
|
+
if (Array.isArray(out.items)) out.items = out.items.map(shaped);
|
|
102
|
+
else if (out.items !== undefined) out.items = shaped(out.items);
|
|
103
|
+
// An object that must have one of several sets of fields reads as having all of them, each optional.
|
|
104
|
+
if (out.type === "object" && Array.isArray(out.anyOf) && out.anyOf.every((branch) => isObject(branch) && branch.type === "object")) {
|
|
105
|
+
const alternatives = out.anyOf.filter(isObject).flatMap((branch) => Object.entries(branch.properties ?? {}));
|
|
106
|
+
out.properties = { ...out.properties, ...Object.fromEntries(alternatives.map(([key, value]) => [key, shaped(value)])) };
|
|
107
|
+
delete out.anyOf;
|
|
108
|
+
}
|
|
109
|
+
for (const key of ["anyOf", "allOf"] as const) {
|
|
110
|
+
const branches = out[key];
|
|
111
|
+
if (Array.isArray(branches)) out[key] = branches.map(shaped);
|
|
112
|
+
}
|
|
113
|
+
if (!Array.isArray(out.oneOf)) return out;
|
|
114
|
+
|
|
115
|
+
const shared = out.properties ?? {};
|
|
116
|
+
const sharedRequired = Array.isArray(out.required) ? out.required : [];
|
|
117
|
+
const branches = out.oneOf.map((branch) => {
|
|
118
|
+
const given = shaped(branch);
|
|
119
|
+
if (Object.keys(shared).length === 0 && sharedRequired.length === 0) return given;
|
|
120
|
+
if (!isObject(given) || given.type !== "object") throw new Error("a oneOf with properties of its own has a variant that is not an object");
|
|
121
|
+
return {
|
|
122
|
+
...given,
|
|
123
|
+
properties: { ...shared, ...given.properties },
|
|
124
|
+
required: [...new Set([...(Array.isArray(given.required) ? given.required : []), ...sharedRequired])],
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
delete out.oneOf;
|
|
128
|
+
delete out.properties;
|
|
129
|
+
delete out.required;
|
|
130
|
+
delete out.type;
|
|
131
|
+
const objects = branches.filter(isObject);
|
|
132
|
+
const discriminator = objects.length === branches.length && objects.every((branch) => branch.type === "object") ? discriminatorOf(objects) : undefined;
|
|
133
|
+
if (discriminator === undefined) out.anyOf = branches;
|
|
134
|
+
else {
|
|
135
|
+
out.oneOf = branches;
|
|
136
|
+
out.discriminator = { propertyName: discriminator };
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The definitions a schema refers to. */
|
|
142
|
+
function refsOf(schema: unknown, found: Set<string>): void {
|
|
143
|
+
if (Array.isArray(schema)) {
|
|
144
|
+
for (const item of schema) refsOf(item, found);
|
|
145
|
+
} else if (typeof schema === "object" && schema !== null) {
|
|
146
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
147
|
+
if (key === "$ref" && typeof value === "string") found.add(refTarget(value));
|
|
148
|
+
else refsOf(value, found);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A schema that says nothing about the shape of its value: any JSON. */
|
|
154
|
+
function sayingNothing(schema: JsonSchemaObject): boolean {
|
|
155
|
+
const KEYS = ["type", "$ref", "anyOf", "oneOf", "allOf", "enum", "const", "not", "properties", "items"];
|
|
156
|
+
return KEYS.every((key) => schema[key] === undefined);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const parserOverride: ParserOverride = (schema) => {
|
|
160
|
+
if (typeof schema.$ref === "string") return schemaName(refTarget(schema.$ref));
|
|
161
|
+
if (sayingNothing(schema)) return "z.json()";
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// -- the definitions ------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
type Definitions = Map<string, JsonSchema>;
|
|
167
|
+
|
|
168
|
+
/** The definitions of every root, each under its name; a name given twice must mean the same. */
|
|
169
|
+
async function readDefinitions(dir: string): Promise<Definitions> {
|
|
170
|
+
const all: Definitions = new Map();
|
|
171
|
+
const add = (name: string, schema: JsonSchema, from: string): void => {
|
|
172
|
+
const known = all.get(name);
|
|
173
|
+
if (known !== undefined && JSON.stringify(known) !== JSON.stringify(schema)) {
|
|
174
|
+
throw new Error(`${name} is defined twice, differently (the second time in ${from})`);
|
|
175
|
+
}
|
|
176
|
+
all.set(name, schema);
|
|
177
|
+
};
|
|
178
|
+
for (const file of ROOTS) {
|
|
179
|
+
const root = JSON.parse(await readFile(join(dir, file), "utf8")) as JsonSchemaObject;
|
|
180
|
+
const { definitions, $schema: _schema, ...self } = root;
|
|
181
|
+
if (typeof self.title !== "string") throw new Error(`${file} has no title to name it by`);
|
|
182
|
+
for (const [name, schema] of Object.entries((definitions ?? {}) as Record<string, JsonSchema>)) add(name, schema, file);
|
|
183
|
+
add(self.title, self, file);
|
|
184
|
+
}
|
|
185
|
+
return all;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The definitions in an order in which each comes after the ones it refers to. */
|
|
189
|
+
function inOrder(all: Definitions): string[] {
|
|
190
|
+
const order: string[] = [];
|
|
191
|
+
const state = new Map<string, "open" | "done">();
|
|
192
|
+
const visit = (name: string, via: readonly string[]): void => {
|
|
193
|
+
if (state.get(name) === "done") return;
|
|
194
|
+
if (state.get(name) === "open") throw new Error(`the definitions refer to each other in a circle: ${[...via, name].join(" -> ")}`);
|
|
195
|
+
const schema = all.get(name);
|
|
196
|
+
if (schema === undefined) throw new Error(`${via.at(-1) ?? "a root"} refers to ${name}, which is not defined`);
|
|
197
|
+
state.set(name, "open");
|
|
198
|
+
const needs = new Set<string>();
|
|
199
|
+
refsOf(schema, needs);
|
|
200
|
+
for (const need of [...needs].sort()) visit(need, [...via, name]);
|
|
201
|
+
state.set(name, "done");
|
|
202
|
+
order.push(name);
|
|
203
|
+
};
|
|
204
|
+
for (const name of [...all.keys()].sort()) visit(name, []);
|
|
205
|
+
return order;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// -- the layout ------------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
type Part = string | Group;
|
|
211
|
+
interface Group {
|
|
212
|
+
readonly open: string;
|
|
213
|
+
readonly close: string;
|
|
214
|
+
readonly args: Part[][];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const CLOSER: Readonly<Record<string, string>> = { "(": ")", "[": "]", "{": "}" };
|
|
218
|
+
|
|
219
|
+
/** The expression as text and groups: what is between brackets is split at its top-level commas. */
|
|
220
|
+
function partsOf(expression: string): Part[] {
|
|
221
|
+
let at = 0;
|
|
222
|
+
const sequence = (closer: string | undefined): Part[][] => {
|
|
223
|
+
const args: Part[][] = [[]];
|
|
224
|
+
let text = "";
|
|
225
|
+
const flush = (): void => {
|
|
226
|
+
if (text !== "") args[args.length - 1]?.push(text);
|
|
227
|
+
text = "";
|
|
228
|
+
};
|
|
229
|
+
while (at < expression.length) {
|
|
230
|
+
const char = expression.charAt(at);
|
|
231
|
+
if (char === closer) {
|
|
232
|
+
flush();
|
|
233
|
+
at += 1;
|
|
234
|
+
return args;
|
|
235
|
+
}
|
|
236
|
+
if (char === '"') {
|
|
237
|
+
const start = at;
|
|
238
|
+
at += 1;
|
|
239
|
+
while (expression.charAt(at) !== '"') at += expression.charAt(at) === "\\" ? 2 : 1;
|
|
240
|
+
at += 1;
|
|
241
|
+
text += expression.slice(start, at);
|
|
242
|
+
} else if (CLOSER[char] !== undefined) {
|
|
243
|
+
flush();
|
|
244
|
+
at += 1;
|
|
245
|
+
const close = CLOSER[char];
|
|
246
|
+
args[args.length - 1]?.push({ open: char, close, args: sequence(close) });
|
|
247
|
+
} else if (char === ",") {
|
|
248
|
+
flush();
|
|
249
|
+
at += 1;
|
|
250
|
+
args.push([]);
|
|
251
|
+
} else {
|
|
252
|
+
text += char;
|
|
253
|
+
at += 1;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
flush();
|
|
257
|
+
return args;
|
|
258
|
+
};
|
|
259
|
+
return sequence(undefined)[0] ?? [];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function flat(parts: readonly Part[]): string {
|
|
263
|
+
return parts.map((part) => (typeof part === "string" ? part : flatGroup(part))).join("");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function flatGroup(group: Group): string {
|
|
267
|
+
const inside = group.args
|
|
268
|
+
.map((arg) => flat(arg).trim())
|
|
269
|
+
.filter((arg) => arg !== "")
|
|
270
|
+
.join(", ");
|
|
271
|
+
if (inside === "") return `${group.open}${group.close}`;
|
|
272
|
+
return group.open === "{" ? `{ ${inside} }` : `${group.open}${inside}${group.close}`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Lays an expression out: a group that does not fit on its line is broken, one argument to a line. */
|
|
276
|
+
function layout(parts: readonly Part[], indent: number, startColumn: number): string {
|
|
277
|
+
let out = "";
|
|
278
|
+
const column = (): number => (out.includes("\n") ? out.length - out.lastIndexOf("\n") - 1 : startColumn + out.length);
|
|
279
|
+
for (const part of parts) {
|
|
280
|
+
if (typeof part === "string") {
|
|
281
|
+
out += part;
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const inline = flatGroup(part);
|
|
285
|
+
if (column() + inline.length <= WIDTH) {
|
|
286
|
+
out += inline;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const args = part.args
|
|
290
|
+
.map((arg) => arg.filter((piece) => typeof piece !== "string" || piece.trim() !== ""))
|
|
291
|
+
.filter((arg) => arg.length > 0);
|
|
292
|
+
const last = args.at(-1);
|
|
293
|
+
if (last?.length === 1 && typeof last[0] === "object") {
|
|
294
|
+
// `z.object({ ... })` and `z.union([ ... ])`: the last argument opens on the line of the call.
|
|
295
|
+
const head = args.slice(0, -1).map((arg) => `${flat(arg).trim()}, `);
|
|
296
|
+
out += part.open + head.join("") + layout(last, indent, column() + 1 + head.join("").length) + part.close;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
out += `${part.open}\n`;
|
|
300
|
+
for (const arg of args) {
|
|
301
|
+
const trimmed = arg.map((piece, index) => {
|
|
302
|
+
if (typeof piece !== "string") return piece;
|
|
303
|
+
const start = index === 0 ? piece.trimStart() : piece;
|
|
304
|
+
return index === arg.length - 1 ? start.trimEnd() : start;
|
|
305
|
+
});
|
|
306
|
+
out += `${" ".repeat(indent + 2)}${layout(trimmed, indent + 2, indent + 2)},\n`;
|
|
307
|
+
}
|
|
308
|
+
out += `${" ".repeat(indent)}${part.close}`;
|
|
309
|
+
}
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// -- the file --------------------------------------------------------------------
|
|
314
|
+
|
|
315
|
+
function codexVersion(): string {
|
|
316
|
+
const run = spawnSync("codex", ["--version"], { encoding: "utf8" });
|
|
317
|
+
if (run.status !== 0) throw new Error(`codex --version failed: ${run.stderr}`);
|
|
318
|
+
return run.stdout.trim();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async function main(): Promise<void> {
|
|
322
|
+
const version = codexVersion();
|
|
323
|
+
const dir = await mkdtemp(join(tmpdir(), "codex-schema-"));
|
|
324
|
+
try {
|
|
325
|
+
const run = spawnSync("codex", ["app-server", "generate-json-schema", "--out", dir], { encoding: "utf8" });
|
|
326
|
+
if (run.status !== 0) throw new Error(`codex app-server generate-json-schema failed: ${run.stderr}`);
|
|
327
|
+
const all = await readDefinitions(dir);
|
|
328
|
+
const blocks = inOrder(all).map((name) => {
|
|
329
|
+
const schema = all.get(name);
|
|
330
|
+
if (schema === undefined) throw new Error(`${name} vanished`);
|
|
331
|
+
const expression = jsonSchemaToZod(shaped(schema), { module: "none", zodVersion: 4, withoutDescribes: true, parserOverride });
|
|
332
|
+
return `export const ${schemaName(name)} = ${layout(partsOf(expression), 0, `export const ${schemaName(name)} = `.length)};\nexport type ${name} = z.infer<typeof ${schemaName(name)}>;\n`;
|
|
333
|
+
});
|
|
334
|
+
const header = [
|
|
335
|
+
"/**",
|
|
336
|
+
" * The messages of `codex app-server`, as zod schemas and types.",
|
|
337
|
+
" *",
|
|
338
|
+
` * GENERATED by scripts/gen-codex.ts from ${version}. Do not edit: run \`bun scripts/gen-codex.ts\`.`,
|
|
339
|
+
" * Command: codex app-server generate-json-schema --out <dir> (the stable surface, no --experimental)",
|
|
340
|
+
" * Objects are open: a field a newer codex adds does not fail a message. Descriptions are in the upstream schema.",
|
|
341
|
+
" */",
|
|
342
|
+
"",
|
|
343
|
+
'import { z } from "zod";',
|
|
344
|
+
"",
|
|
345
|
+
];
|
|
346
|
+
await writeFile(OUTPUT, `${header.join("\n")}\n${blocks.join("\n")}`);
|
|
347
|
+
} finally {
|
|
348
|
+
await rm(dir, { recursive: true, force: true });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
await main();
|