The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as Deferred from "effect/Deferred"; |
| 2 | import * as Exit from "effect/Exit"; |
| 3 | import * as Fiber from "effect/Fiber"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Logger from "effect/Logger"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Queue from "effect/Queue"; |
| 9 | import * as Ref from "effect/Ref"; |
| 10 | import * as References from "effect/References"; |
| 11 | import * as Scope from "effect/Scope"; |
| 12 | import { ChildProcess, ChildProcessSpawner } from "effect/process"; |
| 13 | |
| 14 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 15 | import { assert, expect, it } from "@effect/vitest"; |
| 16 | |
| 17 | import * as CodexClient from "./client.ts"; |
| 18 | import { makeInMemoryStdio } from "./_internal/stdio.ts"; |
| 19 | import * as CodexError from "./errors.ts"; |
| 20 | |
| 21 | const mockPeerPath = Effect.map(Effect.service(Path.Path), (path) => |
| 22 | path.join(import.meta.dirname, "../test/fixtures/codex-app-server-mock-peer.ts"), |
| 23 | ); |
| 24 | const mockPeerArgs = (path: string) => [path]; |
| 25 | |
| 26 | it.layer(NodeServices.layer)("effect-codex-app-server client", (it) => { |
| 27 | const makeHandle = (env?: Record<string, string>) => |
| 28 | Effect.gen(function* () { |
| 29 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 30 | const path = yield* Path.Path; |
| 31 | const peerCwd = path.join(import.meta.dirname, ".."); |
| 32 | const command = ChildProcess.make(process.execPath, mockPeerArgs(yield* mockPeerPath), { |
| 33 | cwd: peerCwd, |
| 34 | ...(env ? { env: { ...process.env, ...env } } : {}), |
| 35 | }); |
| 36 | return yield* spawner.spawn(command); |
| 37 | }); |
| 38 | |
| 39 | it.effect("initializes, handles typed server requests, and reads account and skills data", () => |
| 40 | Effect.gen(function* () { |
| 41 | const userInputRequests = yield* Ref.make<Array<unknown>>([]); |
| 42 | const messageDeltas = yield* Ref.make<Array<unknown>>([]); |
| 43 | const handle = yield* makeHandle(); |
| 44 | const scope = yield* Scope.make(); |
| 45 | const layerClient = CodexClient.layerChildProcess(handle); |
| 46 | const context = yield* Layer.buildWithScope(layerClient, scope); |
| 47 | |
| 48 | const result = yield* Effect.gen(function* () { |
| 49 | const client = yield* CodexClient.CodexAppServerClient; |
| 50 | |
| 51 | yield* client.handleServerRequest("item/tool/requestUserInput", (payload) => |
| 52 | Ref.update(userInputRequests, (current) => [...current, payload]).pipe( |
| 53 | Effect.as({ |
| 54 | answers: { |
| 55 | approved: { |
| 56 | answers: ["yes"], |
| 57 | }, |
| 58 | }, |
| 59 | }), |
| 60 | ), |
| 61 | ); |
| 62 | |
| 63 | yield* client.handleServerNotification("item/agentMessage/delta", (payload) => |
| 64 | Ref.update(messageDeltas, (current) => [...current, payload]), |
| 65 | ); |
| 66 | |
| 67 | const initialized = yield* client.request("initialize", { |
| 68 | clientInfo: { |
| 69 | name: "effect-codex-app-server-test", |
| 70 | title: "Effect Codex App Server Test", |
| 71 | version: "0.0.0", |
| 72 | }, |
| 73 | capabilities: { |
| 74 | experimentalApi: true, |
| 75 | optOutNotificationMethods: null, |
| 76 | }, |
| 77 | }); |
| 78 | assert.equal(initialized.userAgent, "mock-codex-app-server"); |
| 79 | |
| 80 | yield* client.notify("initialized", undefined); |
| 81 | |
| 82 | const account = yield* client.request("account/read", {}); |
| 83 | assert.equal(account.requiresOpenaiAuth, false); |
| 84 | assert.deepEqual(account.account, { |
| 85 | type: "chatgpt", |
| 86 | email: "mock@example.com", |
| 87 | planType: "plus", |
| 88 | }); |
| 89 | |
| 90 | const path = yield* Path.Path; |
| 91 | const peerCwd = path.join(import.meta.dirname, ".."); |
| 92 | const skills = yield* client.request("skills/list", { cwds: [peerCwd] }); |
| 93 | assert.equal(skills.data.length, 1); |
| 94 | assert.equal(skills.data[0]?.cwd, peerCwd); |
| 95 | |
| 96 | return { |
| 97 | account, |
| 98 | skills, |
| 99 | }; |
| 100 | }).pipe(Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void))); |
| 101 | |
| 102 | assert.equal(result.skills.data[0]?.skills.length, 0); |
| 103 | assert.deepEqual(yield* Ref.get(userInputRequests), [ |
| 104 | { |
| 105 | isBlocking: true, |
| 106 | itemId: "item-approval-1", |
| 107 | threadId: "thread-1", |
| 108 | turnId: "turn-1", |
| 109 | questions: [ |
| 110 | { |
| 111 | id: "approved", |
| 112 | header: "Approve", |
| 113 | question: "Continue with the mock skills request?", |
| 114 | options: [ |
| 115 | { |
| 116 | label: "yes", |
| 117 | description: "Approve the request", |
| 118 | }, |
| 119 | ], |
| 120 | }, |
| 121 | ], |
| 122 | }, |
| 123 | ]); |
| 124 | assert.deepEqual(yield* Ref.get(messageDeltas), [ |
| 125 | { |
| 126 | delta: "Mock server is ready.", |
| 127 | itemId: "item-1", |
| 128 | threadId: "thread-1", |
| 129 | turnId: "turn-1", |
| 130 | }, |
| 131 | ]); |
| 132 | }), |
| 133 | ); |
| 134 | it.effect("drains child stderr so large diagnostics cannot block protocol responses", () => |
| 135 | Effect.gen(function* () { |
| 136 | const handle = yield* makeHandle({ |
| 137 | CODEX_APP_SERVER_TEST_STDERR_BYTES: String(512 * 1024), |
| 138 | }); |
| 139 | const scope = yield* Scope.make(); |
| 140 | const layerClient = CodexClient.layerChildProcess(handle); |
| 141 | const context = yield* Layer.buildWithScope(layerClient, scope); |
| 142 | |
| 143 | const initialized = yield* Effect.gen(function* () { |
| 144 | const client = yield* CodexClient.CodexAppServerClient; |
| 145 | return yield* client.request("initialize", { |
| 146 | clientInfo: { |
| 147 | name: "effect-codex-app-server-test", |
| 148 | title: "Effect Codex App Server Test", |
| 149 | version: "0.0.0", |
| 150 | }, |
| 151 | capabilities: { |
| 152 | experimentalApi: true, |
| 153 | optOutNotificationMethods: null, |
| 154 | }, |
| 155 | }); |
| 156 | }).pipe( |
| 157 | Effect.timeout("5 seconds"), |
| 158 | Effect.provide(context), |
| 159 | Effect.ensuring(Scope.close(scope, Exit.void)), |
| 160 | ); |
| 161 | |
| 162 | assert.equal(initialized.userAgent, "mock-codex-app-server"); |
| 163 | }), |
| 164 | ); |
| 165 | |
| 166 | it.effect("warns once about a notification that fails to decode and keeps dispatching", () => |
| 167 | Effect.gen(function* () { |
| 168 | const secret = "codex-notification-secret-sentinel"; |
| 169 | const { stdio, input } = yield* makeInMemoryStdio(); |
| 170 | const warnings: Array<unknown> = []; |
| 171 | const client = yield* CodexClient.make(stdio).pipe( |
| 172 | Effect.provide( |
| 173 | Logger.layer([ |
| 174 | Logger.make(({ logLevel, fiber }) => { |
| 175 | if (logLevel === "Warn") |
| 176 | warnings.push(fiber.getRef(References.CurrentLogAnnotations)); |
| 177 | }), |
| 178 | ]), |
| 179 | ), |
| 180 | ); |
| 181 | const delivered = yield* Deferred.make<unknown>(); |
| 182 | yield* client.handleServerNotification("item/agentMessage/delta", (payload) => |
| 183 | Deferred.succeed(delivered, payload).pipe(Effect.asVoid), |
| 184 | ); |
| 185 | |
| 186 | const send = (params: unknown) => |
| 187 | Queue.offer( |
| 188 | input, |
| 189 | new TextEncoder().encode( |
| 190 | `${JSON.stringify({ method: "item/agentMessage/delta", params })}\n`, |
| 191 | ), |
| 192 | ); |
| 193 | const malformed = { itemId: "item-1", threadId: "thread-1", turnId: secret }; |
| 194 | yield* send(malformed); |
| 195 | yield* send(malformed); |
| 196 | yield* send({ delta: "ok", itemId: "item-1", threadId: "thread-1", turnId: "turn-1" }); |
| 197 | |
| 198 | assert.deepEqual(yield* Deferred.await(delivered), { |
| 199 | delta: "ok", |
| 200 | itemId: "item-1", |
| 201 | threadId: "thread-1", |
| 202 | turnId: "turn-1", |
| 203 | }); |
| 204 | assert.lengthOf(warnings, 1); |
| 205 | expect(warnings[0]).toMatchObject({ |
| 206 | operation: "decode-notification-payload", |
| 207 | method: "item/agentMessage/delta", |
| 208 | issueKinds: expect.arrayContaining(["MissingKey"]), |
| 209 | }); |
| 210 | assert.notInclude(JSON.stringify(warnings), secret); |
| 211 | }), |
| 212 | ); |
| 213 | |
| 214 | it.effect("a dying notification handler leaves no request waiting on a live app-server", () => |
| 215 | Effect.gen(function* () { |
| 216 | const handle = yield* makeHandle(); |
| 217 | const client = yield* Layer.build(CodexClient.layerChildProcess(handle)).pipe( |
| 218 | Effect.flatMap((context) => |
| 219 | Effect.service(CodexClient.CodexAppServerClient).pipe(Effect.provide(context)), |
| 220 | ), |
| 221 | ); |
| 222 | // A Codex error type must still surface as a transport failure, not as Codex's answer. |
| 223 | const handlerFailure = CodexError.CodexAppServerRequestError.internalError("Injected"); |
| 224 | yield* client.handleServerNotification("error", () => |
| 225 | Effect.fail(handlerFailure).pipe(Effect.orDie), |
| 226 | ); |
| 227 | const termination = yield* client.awaitTermination.pipe(Effect.flip, Effect.forkScoped); |
| 228 | |
| 229 | // The peer answers right after this notification and stays alive, so only the reader's |
| 230 | // own termination can settle the request. |
| 231 | const failure = yield* client.raw |
| 232 | .request("mock/notifyThenRespond", { |
| 233 | notification: { |
| 234 | method: "error", |
| 235 | params: { |
| 236 | error: { |
| 237 | message: "Fatal error: application network permission was revoked", |
| 238 | codexErrorInfo: "other", |
| 239 | additionalDetails: null, |
| 240 | }, |
| 241 | willRetry: false, |
| 242 | threadId: "thread-1", |
| 243 | turnId: "turn-1", |
| 244 | }, |
| 245 | }, |
| 246 | }) |
| 247 | .pipe( |
| 248 | Effect.match({ |
| 249 | onFailure: (error) => error, |
| 250 | onSuccess: () => assert.fail("Expected the dead reader to fail the request"), |
| 251 | }), |
| 252 | ); |
| 253 | |
| 254 | assert.instanceOf(failure, CodexError.CodexAppServerTransportError); |
| 255 | assert.equal(failure.operation, "read-input-stream"); |
| 256 | assert.strictEqual(failure.cause, handlerFailure); |
| 257 | assert.strictEqual(yield* Fiber.join(termination), failure); |
| 258 | assert.strictEqual( |
| 259 | yield* client.raw.request("thread/read", {}).pipe( |
| 260 | Effect.match({ |
| 261 | onFailure: (error) => error, |
| 262 | onSuccess: () => assert.fail("Expected the retired transport to reject requests"), |
| 263 | }), |
| 264 | ), |
| 265 | failure, |
| 266 | ); |
| 267 | // Retiring the transport closes the app-server's stdin, so it does not live on unread. |
| 268 | assert.equal(yield* handle.exitCode, 0); |
| 269 | }), |
| 270 | ); |
| 271 | }); |
| 272 | |
| 273 | it.live("reports the stderr error when the process exits before initialize", () => |
| 274 | Effect.gen(function* () { |
| 275 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 276 | const handle = yield* spawner.spawn( |
| 277 | ChildProcess.make(process.execPath, [ |
| 278 | "-e", |
| 279 | 'throw new Error("Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest")', |
| 280 | ]), |
| 281 | ); |
| 282 | const scope = yield* Scope.make(); |
| 283 | const context = yield* Layer.buildWithScope(CodexClient.layerChildProcess(handle), scope); |
| 284 | |
| 285 | const error = yield* Effect.gen(function* () { |
| 286 | const client = yield* CodexClient.CodexAppServerClient; |
| 287 | return yield* client.request("initialize", { |
| 288 | clientInfo: { |
| 289 | name: "effect-codex-app-server-test", |
| 290 | title: "Effect Codex App Server Test", |
| 291 | version: "0.0.0", |
| 292 | }, |
| 293 | capabilities: { |
| 294 | experimentalApi: true, |
| 295 | optOutNotificationMethods: null, |
| 296 | }, |
| 297 | }); |
| 298 | }).pipe(Effect.flip, Effect.provide(context), Effect.ensuring(Scope.close(scope, Exit.void))); |
| 299 | |
| 300 | assert.instanceOf(error, CodexError.CodexAppServerProcessExitedError); |
| 301 | assert.equal( |
| 302 | error.message, |
| 303 | "Codex App Server process exited with code 1: Error: Missing optional dependency @openai/codex-darwin-arm64. Reinstall Codex: npm install -g @openai/codex@latest", |
| 304 | ); |
| 305 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 306 | ); |