@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/src/fixtures.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The fixtures of the conformance suite for Codex: the four scenarios written the way Codex writes
|
|
3
|
+
* them (a rollout file of JSONL records under `sessions/YYYY/MM/DD`). `home` is the directory the
|
|
4
|
+
* reading side is opened on.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { appendFile, mkdir, rm, writeFile } from "node:fs/promises";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
|
|
10
|
+
import type { FixtureResult, FixtureWriter, Scenario } from "@niwa-labs/harness";
|
|
11
|
+
import { harnessId, sessionId } from "@niwa-labs/harness";
|
|
12
|
+
|
|
13
|
+
/** The installation id the fixtures are written for; the suite opens its reading side with it. */
|
|
14
|
+
export const FIXTURE_HARNESS = harnessId("codex");
|
|
15
|
+
|
|
16
|
+
/** The uuid of the session of a scenario: the name of a rollout file carries it. */
|
|
17
|
+
const NATIVE_IDS: Record<Scenario, string> = {
|
|
18
|
+
running: "00000000-0000-4000-8000-000000000001",
|
|
19
|
+
nested: "00000000-0000-4000-8000-000000000002",
|
|
20
|
+
compaction: "00000000-0000-4000-8000-000000000003",
|
|
21
|
+
broken: "00000000-0000-4000-8000-000000000004",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** The uuid of the session spawned inside the nested scenario. */
|
|
25
|
+
const CHILD_ID = "00000000-0000-4000-8000-0000000000ff";
|
|
26
|
+
|
|
27
|
+
/** One native record of a rollout. */
|
|
28
|
+
function record(ordinal: number, type: string, payload: unknown): string {
|
|
29
|
+
return `${JSON.stringify({ timestamp: `2026-01-01T00:00:0${ordinal}.000Z`, ordinal, type, payload })}\n`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A message of the conversation layer. */
|
|
33
|
+
function message(ordinal: number, role: string, text: string): string {
|
|
34
|
+
return record(ordinal, "response_item", {
|
|
35
|
+
type: "message",
|
|
36
|
+
role,
|
|
37
|
+
content: [{ type: role === "assistant" ? "output_text" : "input_text", text }],
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The half-written line is cut inside a string: it does not parse until the rest arrives. */
|
|
42
|
+
const FINAL_ANSWER = message(9, "assistant", "the finished answer");
|
|
43
|
+
const CUT = FINAL_ANSWER.length - 12;
|
|
44
|
+
|
|
45
|
+
export const writeCodexFixtures: FixtureWriter = async (home, scenario) => {
|
|
46
|
+
const native = NATIVE_IDS[scenario];
|
|
47
|
+
const dir = join(home, "sessions", "2026", "01", "01");
|
|
48
|
+
await mkdir(dir, { recursive: true });
|
|
49
|
+
const file = join(dir, `rollout-2026-01-01T00-00-00-${native}.jsonl`);
|
|
50
|
+
const body = bodyOf(scenario);
|
|
51
|
+
await writeFile(file, body.text);
|
|
52
|
+
|
|
53
|
+
const result: FixtureResult = {
|
|
54
|
+
session: sessionId(FIXTURE_HARNESS, native),
|
|
55
|
+
...(body.child === undefined ? {} : { child: sessionId(FIXTURE_HARNESS, body.child) }),
|
|
56
|
+
...(body.summary === undefined ? {} : { summary: body.summary }),
|
|
57
|
+
...(scenario === "running" ? { finish: () => appendFile(file, FINAL_ANSWER.slice(CUT)) } : {}),
|
|
58
|
+
makeUnreadable: async () => {
|
|
59
|
+
await rm(file);
|
|
60
|
+
await mkdir(file);
|
|
61
|
+
},
|
|
62
|
+
remove: () => rm(dir, { recursive: true, force: true }),
|
|
63
|
+
};
|
|
64
|
+
return result;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function bodyOf(scenario: Scenario): { text: string; child?: string; summary?: string } {
|
|
68
|
+
const head = record(0, "session_meta", {
|
|
69
|
+
id: NATIVE_IDS[scenario],
|
|
70
|
+
session_id: NATIVE_IDS[scenario],
|
|
71
|
+
timestamp: "2026-01-01T00:00:00.000Z",
|
|
72
|
+
cwd: "/tmp/niwa-conformance",
|
|
73
|
+
});
|
|
74
|
+
const start = head + message(1, "user", "hello") + message(2, "assistant", "hi there");
|
|
75
|
+
|
|
76
|
+
switch (scenario) {
|
|
77
|
+
case "running":
|
|
78
|
+
return { text: start + FINAL_ANSWER.slice(0, CUT) };
|
|
79
|
+
case "nested":
|
|
80
|
+
return {
|
|
81
|
+
text:
|
|
82
|
+
start +
|
|
83
|
+
record(3, "event_msg", {
|
|
84
|
+
type: "item_completed",
|
|
85
|
+
item: {
|
|
86
|
+
type: "SubAgentActivity",
|
|
87
|
+
kind: "started",
|
|
88
|
+
id: "call_1",
|
|
89
|
+
agent_thread_id: CHILD_ID,
|
|
90
|
+
agent_path: "/root/worker",
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
child: CHILD_ID,
|
|
94
|
+
};
|
|
95
|
+
case "compaction":
|
|
96
|
+
return {
|
|
97
|
+
text: start + record(3, "compacted", { message: "what happened so far" }),
|
|
98
|
+
summary: "what happened so far",
|
|
99
|
+
};
|
|
100
|
+
case "broken":
|
|
101
|
+
return { text: `${start}{"unfamiliar":"shape"}\nnot json at all\n` };
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The native format of a Codex rollout: what one line of the file says.
|
|
3
|
+
*
|
|
4
|
+
* A rollout is JSONL: one record per line, each line `{timestamp, ordinal, type, payload}`. The
|
|
5
|
+
* record types that matter here:
|
|
6
|
+
*
|
|
7
|
+
* - `session_meta` — the session: its id, its working directory, when it started.
|
|
8
|
+
* - `turn_context` — the turn: its model, its id.
|
|
9
|
+
* - `response_item` — the conversation: what the model was given and what it produced.
|
|
10
|
+
* - `event_msg` — the interface of Codex: the conversation rendered as items.
|
|
11
|
+
* - `compacted` — the context was replaced by a summary.
|
|
12
|
+
* - `realtime_item` — the voice mode: transcript segments of the conversation.
|
|
13
|
+
*
|
|
14
|
+
* Two layers live in one file, and they are not the same thing. The conversation layer
|
|
15
|
+
* (`response_item`) is what the model was actually given and produced. The event layer
|
|
16
|
+
* (`event_msg`) is how Codex shows it, and it repeats most of the conversation under its own ids.
|
|
17
|
+
*
|
|
18
|
+
* The format grows without notice, so an unfamiliar record is kept whole and never fails a read.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
|
|
23
|
+
import { timestamp } from "@niwa-labs/core";
|
|
24
|
+
import type { Timestamp } from "@niwa-labs/core";
|
|
25
|
+
import type { ImageMark } from "@niwa-labs/harness";
|
|
26
|
+
|
|
27
|
+
/** The wrapper Codex writes around every payload. */
|
|
28
|
+
export const NativeRecordSchema = z.object({
|
|
29
|
+
timestamp: z.string().optional(),
|
|
30
|
+
ordinal: z.number().optional(),
|
|
31
|
+
type: z.string(),
|
|
32
|
+
payload: z.unknown().optional(),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/** A native record of a rollout. */
|
|
36
|
+
export type NativeRecord = z.output<typeof NativeRecordSchema>;
|
|
37
|
+
|
|
38
|
+
/** A native record, or `null` when the value is not one. */
|
|
39
|
+
export function parseNativeRecord(value: unknown): NativeRecord | null {
|
|
40
|
+
const result = NativeRecordSchema.safeParse(value);
|
|
41
|
+
return result.success ? result.data : null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The payload of a record, as an object; an empty object when it is not one. */
|
|
45
|
+
export function payloadOf(record: NativeRecord): Record<string, unknown> {
|
|
46
|
+
return typeof record.payload === "object" && record.payload !== null
|
|
47
|
+
? (record.payload as Record<string, unknown>)
|
|
48
|
+
: {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The moment of a native timestamp, in milliseconds; `undefined` when it cannot be read. */
|
|
52
|
+
export function instantOf(value: unknown): Timestamp | undefined {
|
|
53
|
+
if (typeof value !== "string") return undefined;
|
|
54
|
+
const ms = Date.parse(value);
|
|
55
|
+
if (!Number.isFinite(ms) || ms < 0) return undefined;
|
|
56
|
+
return timestamp(Math.floor(ms));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The texts of a content list: Codex writes `{type, text}` parts. */
|
|
60
|
+
export function textsOf(content: unknown): string {
|
|
61
|
+
if (typeof content === "string") return content;
|
|
62
|
+
if (!Array.isArray(content)) return "";
|
|
63
|
+
const parts: string[] = [];
|
|
64
|
+
for (const part of content) {
|
|
65
|
+
if (typeof part !== "object" || part === null) continue;
|
|
66
|
+
const text = (part as Record<string, unknown>)["text"];
|
|
67
|
+
if (typeof text === "string" && text.length > 0) parts.push(text);
|
|
68
|
+
}
|
|
69
|
+
return parts.join("\n");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** What a part of Codex's content says of itself when it is an image: its media type when its address is a data URL. */
|
|
73
|
+
function imageOf(part: Record<string, unknown>): { mime?: string } | undefined {
|
|
74
|
+
if (part["type"] !== "input_image" && part["type"] !== "output_image" && part["type"] !== "image") return undefined;
|
|
75
|
+
const source = part["image_url"];
|
|
76
|
+
const url = typeof source === "string" ? source : typeof source === "object" && source !== null ? (source as Record<string, unknown>)["url"] : undefined;
|
|
77
|
+
const mime = typeof url === "string" ? /^data:([^;,]+)[;,]/.exec(url)?.[1] : undefined;
|
|
78
|
+
return mime === undefined ? {} : { mime };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The parts of a message or a result joined into text, one line each, with the images taken out and
|
|
83
|
+
* marked where they stood in that text. `render` says what a part that is not an image is as text.
|
|
84
|
+
*/
|
|
85
|
+
function joinParts(content: readonly unknown[], render: (part: Record<string, unknown>) => string | undefined): { text: string; images: ImageMark[] } {
|
|
86
|
+
let text = "";
|
|
87
|
+
let any = false;
|
|
88
|
+
const images: ImageMark[] = [];
|
|
89
|
+
for (const part of content) {
|
|
90
|
+
if (typeof part !== "object" || part === null) continue;
|
|
91
|
+
const record = part as Record<string, unknown>;
|
|
92
|
+
const image = imageOf(record);
|
|
93
|
+
if (image !== undefined) {
|
|
94
|
+
images.push({ at: text.length, ...image });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const piece = render(record);
|
|
98
|
+
if (piece === undefined) continue;
|
|
99
|
+
text = any ? `${text}\n${piece}` : piece;
|
|
100
|
+
any = true;
|
|
101
|
+
}
|
|
102
|
+
return { text, images };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** What a person or the model said in a message of Codex: its text parts, and the images that stood among them. */
|
|
106
|
+
export function messageOf(content: unknown): { text: string; images: ImageMark[] } {
|
|
107
|
+
if (typeof content === "string") return { text: content, images: [] };
|
|
108
|
+
if (!Array.isArray(content)) return { text: "", images: [] };
|
|
109
|
+
return joinParts(content, (part) => (typeof part["text"] === "string" && part["text"].length > 0 ? part["text"] : undefined));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The arguments of a tool call, as Codex gave them.
|
|
114
|
+
*
|
|
115
|
+
* A JSON string is parsed; anything else is kept as it is. The result is parsed but not
|
|
116
|
+
* validated: niwa does not know the schema of every tool Codex has.
|
|
117
|
+
*/
|
|
118
|
+
export function argumentsOf(raw: unknown): unknown {
|
|
119
|
+
if (typeof raw !== "string") return raw ?? undefined;
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(raw);
|
|
122
|
+
} catch {
|
|
123
|
+
return raw;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The output of a tool result, as Codex gave it: a string or a list of parts joined into text. The
|
|
129
|
+
* images of the parts are not in the text: they are marks where they stood.
|
|
130
|
+
*/
|
|
131
|
+
export function outputOf(raw: unknown): { output: unknown; images: ImageMark[] } {
|
|
132
|
+
if (!Array.isArray(raw)) return { output: raw, images: [] };
|
|
133
|
+
const { text, images } = joinParts(raw, (part) => (typeof part["text"] === "string" ? part["text"] : JSON.stringify(part)));
|
|
134
|
+
return { output: text, images };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Whether a tool result failed.
|
|
139
|
+
*
|
|
140
|
+
* Codex says a failure in three ways: an explicit `error`, a non-zero exit code in the metadata,
|
|
141
|
+
* and a failed status. A result that says none of these succeeded. A result that says nothing at
|
|
142
|
+
* all is unknown: success is never claimed for an outcome that was not reported.
|
|
143
|
+
*/
|
|
144
|
+
export function outcomeOf(
|
|
145
|
+
output: unknown,
|
|
146
|
+
status: unknown,
|
|
147
|
+
): { readonly ok: boolean; readonly text: string } | undefined {
|
|
148
|
+
const text = typeof output === "string" ? output : textsOf(output);
|
|
149
|
+
if (typeof status === "string" && status === "failed") return { ok: false, text };
|
|
150
|
+
let parsed: unknown;
|
|
151
|
+
if (typeof output === "string") {
|
|
152
|
+
try {
|
|
153
|
+
parsed = JSON.parse(output);
|
|
154
|
+
} catch {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
parsed = output;
|
|
159
|
+
}
|
|
160
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
161
|
+
const record = parsed as Record<string, unknown>;
|
|
162
|
+
if (record["error"] !== undefined && record["error"] !== null) return { ok: false, text };
|
|
163
|
+
const metadata = record["metadata"];
|
|
164
|
+
const meta =
|
|
165
|
+
typeof metadata === "object" && metadata !== null ? (metadata as Record<string, unknown>) : {};
|
|
166
|
+
const exit = meta["exit_code"];
|
|
167
|
+
if (typeof exit === "number") return { ok: exit === 0, text };
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The summary a compaction carries.
|
|
173
|
+
*
|
|
174
|
+
* Codex writes the messages it kept, not a prose summary: `retained_context` holds the owner's
|
|
175
|
+
* and the assistant's messages by order. Those are what survived the compaction, so they are the
|
|
176
|
+
* summary. A file without them says so with an empty summary rather than a made-up one.
|
|
177
|
+
*/
|
|
178
|
+
export function compactionSummary(payload: Record<string, unknown>): string {
|
|
179
|
+
const message = payload["message"];
|
|
180
|
+
if (typeof message === "string" && message.trim().length > 0) return message;
|
|
181
|
+
const retained = payload["retained_context"];
|
|
182
|
+
if (typeof retained !== "object" || retained === null) return "";
|
|
183
|
+
const context = retained as Record<string, unknown>;
|
|
184
|
+
const parts: string[] = [];
|
|
185
|
+
for (const key of ["user_messages", "assistant_messages"] as const) {
|
|
186
|
+
const messages = context[key];
|
|
187
|
+
if (!Array.isArray(messages)) continue;
|
|
188
|
+
for (const entry of messages) {
|
|
189
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
190
|
+
const text = (entry as Record<string, unknown>)["text"];
|
|
191
|
+
if (typeof text !== "string" || text.length === 0) continue;
|
|
192
|
+
parts.push(`${key === "user_messages" ? "owner" : "assistant"}: ${text}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return parts.join("\n");
|
|
196
|
+
}
|
package/src/incoming.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a notification of `codex app-server` means to the session that reads it.
|
|
3
|
+
*
|
|
4
|
+
* Every notification the schemas know, and every item an `item/*` notification carries, is
|
|
5
|
+
* mapped here on purpose: to the start or the end of a turn, to something the control side
|
|
6
|
+
* remembers, or to a sign of life. What says nothing about the work of the session is ignored, and
|
|
7
|
+
* the reason is written where it is ignored. The switches end in `assertNever`, so a variant that
|
|
8
|
+
* the next generation of the schemas adds does not compile until it is given a meaning.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
import { assertNever } from "@niwa-labs/core";
|
|
14
|
+
import type { JsonValue } from "@niwa-labs/core";
|
|
15
|
+
|
|
16
|
+
import type { RequestId, ServerNotification, ThreadItem } from "./protocol/generated/app-server.ts";
|
|
17
|
+
|
|
18
|
+
/** A turn as it ended: Codex's id of it when it gave one, the status Codex gave it, and the reason when it gave one. */
|
|
19
|
+
export interface EndedTurn {
|
|
20
|
+
readonly id: string | undefined;
|
|
21
|
+
readonly status: string;
|
|
22
|
+
readonly message: string | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** What the control side does with a notification. */
|
|
26
|
+
export type Incoming =
|
|
27
|
+
| { readonly kind: "turn-started"; readonly turnId: string }
|
|
28
|
+
| { readonly kind: "turn-ended"; readonly turn: EndedTurn }
|
|
29
|
+
/** An item that changes files was announced: an approval of it names no path, so the paths are kept. */
|
|
30
|
+
| { readonly kind: "file-change"; readonly detail: string; readonly itemId: string; readonly paths: readonly string[] }
|
|
31
|
+
/** The final answer of the turn, as it stands. */
|
|
32
|
+
| { readonly kind: "answer"; readonly detail: string; readonly text: string }
|
|
33
|
+
/** Codex answered a request of its own, or stopped waiting for it: nobody waits for an answer any more. */
|
|
34
|
+
| { readonly kind: "request-resolved"; readonly detail: string; readonly requestId: RequestId }
|
|
35
|
+
/** Codex moved the turn to another model: the model the session runs from now on. */
|
|
36
|
+
| { readonly kind: "rerouted"; readonly detail: string; readonly toModel: string }
|
|
37
|
+
/** The session is at work: nothing to keep, only a sign of life. */
|
|
38
|
+
| { readonly kind: "sign"; readonly detail: string }
|
|
39
|
+
/** Not about the work of this session. */
|
|
40
|
+
| { readonly kind: "ignored" };
|
|
41
|
+
|
|
42
|
+
const IGNORED: Incoming = { kind: "ignored" };
|
|
43
|
+
|
|
44
|
+
function sign(detail: string): Incoming {
|
|
45
|
+
return { kind: "sign", detail };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The meaning of an item that was announced (`started`) or is done (`completed`). */
|
|
49
|
+
function incomingOfItem(method: "item/started" | "item/completed", item: ThreadItem): Incoming {
|
|
50
|
+
const detail = `${method} ${item.type}`;
|
|
51
|
+
switch (item.type) {
|
|
52
|
+
case "agentMessage":
|
|
53
|
+
// Only the final answer of a turn is its answer: a commentary or a partial message is not.
|
|
54
|
+
// An item that does not say which it is, is the answer.
|
|
55
|
+
if (method === "item/completed" && (item.phase === null || item.phase === "final_answer") && item.text !== "") {
|
|
56
|
+
return { kind: "answer", detail, text: item.text };
|
|
57
|
+
}
|
|
58
|
+
return sign(detail);
|
|
59
|
+
case "fileChange":
|
|
60
|
+
if (method === "item/started") {
|
|
61
|
+
return { kind: "file-change", detail, itemId: item.id, paths: item.changes.map((change) => change.path) };
|
|
62
|
+
}
|
|
63
|
+
return sign(detail);
|
|
64
|
+
// The rest of the items are the work of the turn. None is kept: the turn's answer is the
|
|
65
|
+
// agent's final message, and what the other items did is in Codex's own rollout.
|
|
66
|
+
case "userMessage":
|
|
67
|
+
case "hookPrompt":
|
|
68
|
+
case "functionCallOutput":
|
|
69
|
+
case "plan":
|
|
70
|
+
case "reasoning":
|
|
71
|
+
case "commandExecution":
|
|
72
|
+
case "mcpToolCall":
|
|
73
|
+
case "dynamicToolCall":
|
|
74
|
+
case "collabAgentToolCall":
|
|
75
|
+
case "subAgentActivity":
|
|
76
|
+
case "webSearch":
|
|
77
|
+
case "imageView":
|
|
78
|
+
case "sleep":
|
|
79
|
+
case "imageGeneration":
|
|
80
|
+
case "enteredReviewMode":
|
|
81
|
+
case "exitedReviewMode":
|
|
82
|
+
case "contextCompaction":
|
|
83
|
+
return sign(detail);
|
|
84
|
+
default:
|
|
85
|
+
return assertNever(item, "codex thread item");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The meaning of a notification the schemas read. */
|
|
90
|
+
export function incomingOf(notification: ServerNotification): Incoming {
|
|
91
|
+
switch (notification.method) {
|
|
92
|
+
case "turn/started":
|
|
93
|
+
return { kind: "turn-started", turnId: notification.params.turn.id };
|
|
94
|
+
case "turn/completed": {
|
|
95
|
+
const { id, status, error } = notification.params.turn;
|
|
96
|
+
return { kind: "turn-ended", turn: { id, status, message: error?.message } };
|
|
97
|
+
}
|
|
98
|
+
case "item/started":
|
|
99
|
+
case "item/completed":
|
|
100
|
+
return incomingOfItem(notification.method, notification.params.item);
|
|
101
|
+
case "serverRequest/resolved":
|
|
102
|
+
return { kind: "request-resolved", detail: notification.method, requestId: notification.params.requestId };
|
|
103
|
+
case "model/rerouted":
|
|
104
|
+
return { kind: "rerouted", detail: notification.method, toModel: notification.params.toModel };
|
|
105
|
+
case "error":
|
|
106
|
+
return sign(`error: ${notification.params.error.message}`);
|
|
107
|
+
case "warning":
|
|
108
|
+
case "guardianWarning":
|
|
109
|
+
return sign(`${notification.method}: ${notification.params.message}`);
|
|
110
|
+
|
|
111
|
+
// The thread is working, or what it did is told: each is a sign of life.
|
|
112
|
+
case "thread/started":
|
|
113
|
+
case "thread/status/changed":
|
|
114
|
+
case "thread/tokenUsage/updated":
|
|
115
|
+
case "thread/compacted":
|
|
116
|
+
case "thread/closed":
|
|
117
|
+
case "thread/goal/updated":
|
|
118
|
+
case "thread/goal/cleared":
|
|
119
|
+
case "turn/diff/updated":
|
|
120
|
+
case "turn/plan/updated":
|
|
121
|
+
case "hook/started":
|
|
122
|
+
case "hook/completed":
|
|
123
|
+
case "item/autoApprovalReview/started":
|
|
124
|
+
case "item/autoApprovalReview/completed":
|
|
125
|
+
case "autoApprovalReview/strictReviewRequired":
|
|
126
|
+
case "item/agentMessage/delta":
|
|
127
|
+
case "item/plan/delta":
|
|
128
|
+
case "command/exec/outputDelta":
|
|
129
|
+
case "process/outputDelta":
|
|
130
|
+
case "process/exited":
|
|
131
|
+
case "item/commandExecution/outputDelta":
|
|
132
|
+
case "item/commandExecution/terminalInteraction":
|
|
133
|
+
case "item/fileChange/outputDelta":
|
|
134
|
+
case "item/fileChange/patchUpdated":
|
|
135
|
+
case "item/mcpToolCall/progress":
|
|
136
|
+
case "item/reasoning/summaryTextDelta":
|
|
137
|
+
case "item/reasoning/summaryPartAdded":
|
|
138
|
+
case "item/reasoning/textDelta":
|
|
139
|
+
case "model/verification":
|
|
140
|
+
case "modelProvider/authRecoveryStarted":
|
|
141
|
+
case "modelProvider/authRecoveryCompleted":
|
|
142
|
+
case "turn/moderationMetadata":
|
|
143
|
+
case "model/safetyBuffering/updated":
|
|
144
|
+
case "mcpServer/startupStatus/updated":
|
|
145
|
+
case "mcpServer/event/stream/notification":
|
|
146
|
+
return sign(notification.method);
|
|
147
|
+
|
|
148
|
+
// The bookkeeping of a thread, for the screens that list threads: it says nothing about the work.
|
|
149
|
+
case "thread/readState/changed":
|
|
150
|
+
case "thread/archived":
|
|
151
|
+
case "thread/deleted":
|
|
152
|
+
case "thread/unarchived":
|
|
153
|
+
case "thread/reverted":
|
|
154
|
+
case "thread/name/updated":
|
|
155
|
+
case "thread/section/changed":
|
|
156
|
+
case "thread/attachment/updated":
|
|
157
|
+
case "thread/prediction/updated":
|
|
158
|
+
case "thread/queue/changed":
|
|
159
|
+
case "thread/settings/updated":
|
|
160
|
+
case "thread/environment/connected":
|
|
161
|
+
case "thread/environment/disconnected":
|
|
162
|
+
case "project/changed":
|
|
163
|
+
case "thread/project/updated":
|
|
164
|
+
return IGNORED;
|
|
165
|
+
|
|
166
|
+
// The installation, the account and the machine, not the session: a sign of life of the
|
|
167
|
+
// process at most, and an idle session gets them as well.
|
|
168
|
+
case "skills/changed":
|
|
169
|
+
case "mcpServer/oauthLogin/completed":
|
|
170
|
+
case "account/updated":
|
|
171
|
+
case "account/gatewayOAuth/changed":
|
|
172
|
+
case "account/rateLimits/updated":
|
|
173
|
+
case "account/login/completed":
|
|
174
|
+
case "app/list/updated":
|
|
175
|
+
case "remoteControl/status/changed":
|
|
176
|
+
case "externalAgentConfig/import/progress":
|
|
177
|
+
case "externalAgentConfig/import/completed":
|
|
178
|
+
case "fs/changed":
|
|
179
|
+
case "deprecationNotice":
|
|
180
|
+
case "configWarning":
|
|
181
|
+
case "fuzzyFileSearch/sessionUpdated":
|
|
182
|
+
case "fuzzyFileSearch/sessionCompleted":
|
|
183
|
+
case "windows/worldWritableWarning":
|
|
184
|
+
case "windowsSandbox/setupCompleted":
|
|
185
|
+
return IGNORED;
|
|
186
|
+
|
|
187
|
+
// A realtime (voice) conversation is a thread of its own kind; niwa starts none, so none is a turn of this session.
|
|
188
|
+
case "thread/realtime/started":
|
|
189
|
+
case "thread/realtime/itemAdded":
|
|
190
|
+
case "thread/realtime/item/started":
|
|
191
|
+
case "thread/realtime/item/transcript/delta":
|
|
192
|
+
case "thread/realtime/item/completed":
|
|
193
|
+
case "thread/realtime/transcript/delta":
|
|
194
|
+
case "thread/realtime/transcript/done":
|
|
195
|
+
case "thread/realtime/outputAudio/delta":
|
|
196
|
+
case "thread/realtime/sdp":
|
|
197
|
+
case "thread/realtime/error":
|
|
198
|
+
case "thread/realtime/closed":
|
|
199
|
+
return IGNORED;
|
|
200
|
+
default:
|
|
201
|
+
return assertNever(notification, "codex notification");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** What the books of a turn need from a notification of it that the schemas no longer read. */
|
|
206
|
+
const DriftedTurnSchema = z.looseObject({
|
|
207
|
+
turn: z.looseObject({
|
|
208
|
+
id: z.string().optional(),
|
|
209
|
+
status: z.string().optional(),
|
|
210
|
+
error: z.looseObject({ message: z.string().optional() }).nullish(),
|
|
211
|
+
}),
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const DriftedReroutedSchema = z.looseObject({ toModel: z.string() });
|
|
215
|
+
|
|
216
|
+
const DriftedResolvedSchema = z.looseObject({ requestId: z.union([z.string(), z.number()]) });
|
|
217
|
+
|
|
218
|
+
/** What the control side keeps of an item: the agent's words, and the paths of a change of files. */
|
|
219
|
+
const DriftedItemSchema = z.looseObject({
|
|
220
|
+
item: z.looseObject({
|
|
221
|
+
type: z.string(),
|
|
222
|
+
id: z.string().optional(),
|
|
223
|
+
text: z.string().optional(),
|
|
224
|
+
phase: z.string().nullish(),
|
|
225
|
+
changes: z.array(z.looseObject({ path: z.string() })).optional(),
|
|
226
|
+
}),
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The meaning of a notification that is not in the snapshot, as far as a turn's books need it.
|
|
231
|
+
*
|
|
232
|
+
* A turn that began or ended must be known to have, whatever else about the message changed: a
|
|
233
|
+
* turn whose end is lost waits for its timeout. An end that cannot be read is not a success. The
|
|
234
|
+
* answer of the turn and the paths of a file change are kept the same way: a field a newer Codex
|
|
235
|
+
* adds to an item must not leave a turn without its answer.
|
|
236
|
+
*/
|
|
237
|
+
export function incomingOfDrifted(method: string, params: JsonValue | undefined): Incoming | undefined {
|
|
238
|
+
switch (method) {
|
|
239
|
+
case "turn/started": {
|
|
240
|
+
const turn = DriftedTurnSchema.safeParse(params);
|
|
241
|
+
return turn.success && turn.data.turn.id !== undefined ? { kind: "turn-started", turnId: turn.data.turn.id } : undefined;
|
|
242
|
+
}
|
|
243
|
+
case "turn/completed": {
|
|
244
|
+
const turn = DriftedTurnSchema.safeParse(params);
|
|
245
|
+
const read = turn.success ? turn.data.turn : undefined;
|
|
246
|
+
return { kind: "turn-ended", turn: { id: read?.id, status: read?.status ?? "was not readable", message: read?.error?.message } };
|
|
247
|
+
}
|
|
248
|
+
case "item/started":
|
|
249
|
+
case "item/completed": {
|
|
250
|
+
const read = DriftedItemSchema.safeParse(params);
|
|
251
|
+
if (!read.success) return undefined;
|
|
252
|
+
const { item } = read.data;
|
|
253
|
+
const detail = `${method} ${item.type}`;
|
|
254
|
+
if (method === "item/completed" && item.type === "agentMessage" && (item.phase == null || item.phase === "final_answer") && item.text !== undefined && item.text !== "") {
|
|
255
|
+
return { kind: "answer", detail, text: item.text };
|
|
256
|
+
}
|
|
257
|
+
if (method === "item/started" && item.type === "fileChange" && item.id !== undefined && item.changes !== undefined) {
|
|
258
|
+
return { kind: "file-change", detail, itemId: item.id, paths: item.changes.map((change) => change.path) };
|
|
259
|
+
}
|
|
260
|
+
return sign(detail);
|
|
261
|
+
}
|
|
262
|
+
case "model/rerouted": {
|
|
263
|
+
const rerouted = DriftedReroutedSchema.safeParse(params);
|
|
264
|
+
return rerouted.success ? { kind: "rerouted", detail: method, toModel: rerouted.data.toModel } : undefined;
|
|
265
|
+
}
|
|
266
|
+
case "serverRequest/resolved": {
|
|
267
|
+
const resolved = DriftedResolvedSchema.safeParse(params);
|
|
268
|
+
return resolved.success ? { kind: "request-resolved", detail: method, requestId: resolved.data.requestId } : undefined;
|
|
269
|
+
}
|
|
270
|
+
default:
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
}
|