@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
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What `codex app-server` says to its client, read as typed messages.
|
|
3
|
+
*
|
|
4
|
+
* The schemas are generated from the installed codex (`./generated/app-server.ts`, by
|
|
5
|
+
* `scripts/gen-codex.ts`). This module reads one line of the output stream into one `Frame`:
|
|
6
|
+
* a notification or a request that the schemas know, the answer to a call of ours, or a message
|
|
7
|
+
* that is not in the snapshot. A message that is not in the snapshot never fails a session: it
|
|
8
|
+
* comes back as an `unknown` frame that carries the message as it arrived.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
import { JsonValueSchema } from "@niwa-labs/core";
|
|
14
|
+
import type { JsonValue } from "@niwa-labs/core";
|
|
15
|
+
|
|
16
|
+
import { ProtocolNameSchema, VariantNameSchema } from "@niwa-labs/harness";
|
|
17
|
+
import type { ProtocolName, VariantName } from "@niwa-labs/harness";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
JSONRPCErrorSchema,
|
|
21
|
+
JSONRPCNotificationSchema,
|
|
22
|
+
JSONRPCRequestSchema,
|
|
23
|
+
JSONRPCResponseSchema,
|
|
24
|
+
ServerNotificationSchema,
|
|
25
|
+
ServerRequestSchema,
|
|
26
|
+
ThreadItemSchema,
|
|
27
|
+
} from "./generated/app-server.ts";
|
|
28
|
+
import type {
|
|
29
|
+
JSONRPCErrorError,
|
|
30
|
+
RequestId,
|
|
31
|
+
ServerNotification,
|
|
32
|
+
ServerRequest,
|
|
33
|
+
} from "./generated/app-server.ts";
|
|
34
|
+
|
|
35
|
+
/** The name of this wire protocol in a record of an unknown variant. */
|
|
36
|
+
export const CODEX_PROTOCOL: ProtocolName = ProtocolNameSchema.parse("codex app-server");
|
|
37
|
+
|
|
38
|
+
/** One line of the output stream, read. */
|
|
39
|
+
export type Frame =
|
|
40
|
+
| { readonly kind: "response"; readonly id: RequestId; readonly result: JsonValue }
|
|
41
|
+
| { readonly kind: "failure"; readonly id: RequestId; readonly error: JSONRPCErrorError }
|
|
42
|
+
| { readonly kind: "notification"; readonly notification: ServerNotification }
|
|
43
|
+
| { readonly kind: "request"; readonly id: RequestId; readonly request: ServerRequest }
|
|
44
|
+
| UnknownFrame
|
|
45
|
+
| { readonly kind: "unreadable"; readonly reason: string };
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A notification or a request that the schemas do not read: its method is not known, or its
|
|
49
|
+
* parameters are not what they were when the snapshot was taken. `id` is there for a request,
|
|
50
|
+
* which must be answered all the same.
|
|
51
|
+
*/
|
|
52
|
+
export interface UnknownFrame {
|
|
53
|
+
readonly kind: "unknown";
|
|
54
|
+
readonly method: string;
|
|
55
|
+
readonly id: RequestId | undefined;
|
|
56
|
+
/** What is not known: the method, or the type of the item inside an `item/*` notification. */
|
|
57
|
+
readonly variant: VariantName;
|
|
58
|
+
readonly params: JsonValue | undefined;
|
|
59
|
+
readonly raw: JsonValue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The values of the `type` of an item that this snapshot knows. */
|
|
63
|
+
const ITEM_TYPES: ReadonlySet<string> = new Set(ThreadItemSchema.options.map((option) => option.shape.type.value));
|
|
64
|
+
|
|
65
|
+
const ItemTypeSchema = z.looseObject({ item: z.looseObject({ type: z.string() }) });
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* What a message that is not read is called: its method, or, when the method is one the schemas
|
|
69
|
+
* know and what they do not know is the type of the item it carries, the method and that type.
|
|
70
|
+
*/
|
|
71
|
+
function variantOf(method: string, params: JsonValue | undefined): VariantName {
|
|
72
|
+
if (method !== "item/started" && method !== "item/completed") return VariantNameSchema.parse(method);
|
|
73
|
+
const item = ItemTypeSchema.safeParse(params);
|
|
74
|
+
return VariantNameSchema.parse(item.success && !ITEM_TYPES.has(item.data.item.type) ? `${method}:${item.data.item.type}` : method);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Reads one line of the output stream. It never throws. */
|
|
78
|
+
export function readFrame(line: string): Frame {
|
|
79
|
+
let raw: JsonValue;
|
|
80
|
+
try {
|
|
81
|
+
raw = JsonValueSchema.parse(JSON.parse(line));
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return { kind: "unreadable", reason: `a line of the output stream could not be read: ${String(error)}` };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const request = JSONRPCRequestSchema.safeParse(raw);
|
|
87
|
+
if (request.success) {
|
|
88
|
+
const { id, method, params } = request.data;
|
|
89
|
+
const typed = ServerRequestSchema.safeParse(raw);
|
|
90
|
+
if (typed.success) return { kind: "request", id, request: typed.data };
|
|
91
|
+
return { kind: "unknown", method, id, variant: variantOf(method, params), params, raw };
|
|
92
|
+
}
|
|
93
|
+
const notification = JSONRPCNotificationSchema.safeParse(raw);
|
|
94
|
+
if (notification.success) {
|
|
95
|
+
const { method, params } = notification.data;
|
|
96
|
+
const typed = ServerNotificationSchema.safeParse(raw);
|
|
97
|
+
if (typed.success) return { kind: "notification", notification: typed.data };
|
|
98
|
+
return { kind: "unknown", method, id: undefined, variant: variantOf(method, params), params, raw };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const failure = JSONRPCErrorSchema.safeParse(raw);
|
|
102
|
+
if (failure.success) return { kind: "failure", id: failure.data.id, error: failure.data.error };
|
|
103
|
+
const response = JSONRPCResponseSchema.safeParse(raw);
|
|
104
|
+
if (response.success) return { kind: "response", id: response.data.id, result: response.data.result };
|
|
105
|
+
return { kind: "unreadable", reason: "a line of the output stream is not a JSON-RPC message" };
|
|
106
|
+
}
|