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