Open History to see the last commit
1import * as Deferred from "effect/Deferred";
2import * as Effect from "effect/Effect";
3import * as Fiber from "effect/Fiber";
4import * as Queue from "effect/Queue";
5import * as Schema from "effect/Schema";
6import * as Stream from "effect/Stream";
7
8import * as NodeServices from "@effect/platform-node/NodeServices";
9import { assert, it } from "@effect/vitest";
10
11import * as CodexError from "./errors.ts";
12import * as CodexProtocol from "./protocol.ts";
13import { makeInMemoryStdio } from "./_internal/stdio.ts";
14const encodeUnknownJsonString = Schema.encodeUnknownSync(Schema.UnknownFromJsonString);
15
16const encoder = new TextEncoder();
17
18const encodeJsonl = (value: unknown) => encoder.encode(`${encodeUnknownJsonString(value)}\n`);
19
20const decodeJson = Schema.decodeEffect(Schema.UnknownFromJsonString);
21
22it.layer(NodeServices.layer)("effect-codex-app-server protocol", (it) => {
23 it.effect(
24 "encodes requests without a jsonrpc field and routes inbound requests and notifications",
25 () =>
26 Effect.gen(function* () {
27 const { stdio, input, output } = yield* makeInMemoryStdio();
28 const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio });
29
30 const notificationDeferred =
31 yield* Deferred.make<ReadonlyArray<CodexProtocol.CodexAppServerIncomingNotification>>();
32 const requestDeferred =
33 yield* Deferred.make<ReadonlyArray<CodexProtocol.CodexAppServerIncomingRequest>>();
34
35 yield* transport.incomingNotifications.pipe(
36 Stream.take(1),
37 Stream.runCollect,
38 Effect.flatMap((notifications) => Deferred.succeed(notificationDeferred, notifications)),
39 Effect.forkScoped,
40 );
41
42 yield* transport.incomingRequests.pipe(
43 Stream.take(1),
44 Stream.runCollect,
45 Effect.flatMap((requests) => Deferred.succeed(requestDeferred, requests)),
46 Effect.forkScoped,
47 );
48
49 yield* transport.notify("initialized");
50 assert.equal(yield* Queue.take(output), '{"method":"initialized"}\n');
51
52 const initializeParams = {
53 clientInfo: {
54 name: "effect-codex-app-server-test",
55 title: "Effect Codex App Server Test",
56 version: "0.0.0",
57 },
58 capabilities: {
59 experimentalApi: true,
60 optOutNotificationMethods: null,
61 },
62 };
63
64 const pendingInitialize = yield* transport
65 .request("initialize", initializeParams)
66 .pipe(Effect.forkScoped);
67 assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
68 id: 1,
69 method: "initialize",
70 params: initializeParams,
71 });
72
73 yield* Queue.offer(
74 input,
75 encodeJsonl({
76 method: "item/agentMessage/delta",
77 params: {
78 delta: "Hello from the mock peer.",
79 itemId: "item-1",
80 threadId: "thread-1",
81 turnId: "turn-1",
82 },
83 }),
84 );
85 yield* Queue.offer(
86 input,
87 encodeJsonl({
88 id: 77,
89 method: "item/tool/requestUserInput",
90 params: {
91 itemId: "item-approval-1",
92 threadId: "thread-1",
93 turnId: "turn-1",
94 questions: [
95 {
96 id: "approved",
97 header: "Approve",
98 question: "Continue?",
99 },
100 ],
101 },
102 }),
103 );
104 yield* Queue.offer(
105 input,
106 encodeJsonl({
107 id: 1,
108 result: {
109 userAgent: "mock-codex-app-server",
110 codexHome: "/tmp/codex-home",
111 platformFamily: "unix",
112 platformOs: "macos",
113 },
114 }),
115 );
116
117 assert.deepEqual(yield* Fiber.join(pendingInitialize), {
118 userAgent: "mock-codex-app-server",
119 codexHome: "/tmp/codex-home",
120 platformFamily: "unix",
121 platformOs: "macos",
122 });
123 assert.deepEqual(yield* Deferred.await(notificationDeferred), [
124 {
125 method: "item/agentMessage/delta",
126 params: {
127 delta: "Hello from the mock peer.",
128 itemId: "item-1",
129 threadId: "thread-1",
130 turnId: "turn-1",
131 },
132 },
133 ]);
134 assert.deepEqual(yield* Deferred.await(requestDeferred), [
135 {
136 id: 77,
137 method: "item/tool/requestUserInput",
138 params: {
139 itemId: "item-approval-1",
140 threadId: "thread-1",
141 turnId: "turn-1",
142 questions: [
143 {
144 id: "approved",
145 header: "Approve",
146 question: "Continue?",
147 },
148 ],
149 },
150 },
151 ]);
152
153 yield* transport.respond(77, {
154 answers: {
155 approved: {
156 answers: ["yes"],
157 },
158 },
159 });
160 assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
161 id: 77,
162 result: {
163 answers: {
164 approved: {
165 answers: ["yes"],
166 },
167 },
168 },
169 });
170
171 yield* transport.respondError(
172 78,
173 CodexError.CodexAppServerRequestError.methodNotFound("x/test"),
174 );
175 assert.deepEqual(yield* decodeJson(yield* Queue.take(output)), {
176 id: 78,
177 error: {
178 code: -32601,
179 message: "Method not found: x/test",
180 },
181 });
182 }),
183 );
184
185 it.effect("surfaces JSON encoding failures as protocol parse errors", () =>
186 Effect.gen(function* () {
187 const { stdio } = yield* makeInMemoryStdio();
188 const transport = yield* CodexProtocol.makeCodexAppServerPatchedProtocol({ stdio });
189
190 const bigintError = yield* transport.notify("x/test", 1n).pipe(Effect.flip);
191 assert.instanceOf(bigintError, CodexError.CodexAppServerProtocolParseError);
192 assert.equal(bigintError.detail, "Failed to encode Codex App Server message");
193
194 const circular: Record<string, unknown> = {};
195 circular.self = circular;
196 const circularError = yield* transport.notify("x/test", circular).pipe(Effect.flip);
197 assert.instanceOf(circularError, CodexError.CodexAppServerProtocolParseError);
198 assert.equal(circularError.detail, "Failed to encode Codex App Server message");
199 }),
200 );
201});