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 { assert, it } from "@effect/vitest"; |
| 2 | import * as Schema from "effect/Schema"; |
| 3 | |
| 4 | import * as CodexSchema from "./schema.ts"; |
| 5 | |
| 6 | const isGetAccountResponse = Schema.is(CodexSchema.V2GetAccountResponse); |
| 7 | const isThreadReadResponse = Schema.is(CodexSchema.V2ThreadReadResponse); |
| 8 | const isThreadResumeResponse = Schema.is(CodexSchema.V2ThreadResumeResponse); |
| 9 | const isThreadForkResponse = Schema.is(CodexSchema.V2ThreadForkResponse); |
| 10 | const isTurnCompletedNotification = Schema.is(CodexSchema.V2TurnCompletedNotification); |
| 11 | const isHookStartedNotification = Schema.is(CodexSchema.V2HookStartedNotification); |
| 12 | const isHookCompletedNotification = Schema.is(CodexSchema.V2HookCompletedNotification); |
| 13 | const decodeThreadResumeResponse = Schema.decodeUnknownSync(CodexSchema.V2ThreadResumeResponse); |
| 14 | |
| 15 | it("keeps async questions in live notifications and thread history", () => { |
| 16 | const item = { |
| 17 | type: "agentMessage", |
| 18 | id: "question-1", |
| 19 | text: "Which package?\n- pnpm\n- npm\n\nWhat should it be named?", |
| 20 | phase: "final_answer", |
| 21 | delivery: "async", |
| 22 | questions: [ |
| 23 | { title: "Which package manager?", options: ["pnpm", "npm"] }, |
| 24 | { title: "What should it be named?" }, |
| 25 | ], |
| 26 | } as const; |
| 27 | for (const schema of [ |
| 28 | CodexSchema.ServerNotification__ThreadItem, |
| 29 | CodexSchema.V2ItemStartedNotification__ThreadItem, |
| 30 | CodexSchema.V2ItemCompletedNotification__ThreadItem, |
| 31 | CodexSchema.V2ThreadReadResponse__ThreadItem, |
| 32 | CodexSchema.V2ThreadResumeResponse__ThreadItem, |
| 33 | ]) { |
| 34 | assert.deepEqual(Schema.decodeSync(schema)(item), item); |
| 35 | } |
| 36 | }); |
| 37 | |
| 38 | it("accepts generated images identified by fileId without inline result", () => { |
| 39 | const item = { |
| 40 | type: "imageGeneration", |
| 41 | id: "image-1", |
| 42 | status: "completed", |
| 43 | fileId: "file-1", |
| 44 | savedPath: "/tmp/generated.png", |
| 45 | revisedPrompt: null, |
| 46 | } as const; |
| 47 | for (const schema of [ |
| 48 | CodexSchema.ServerNotification__ThreadItem, |
| 49 | CodexSchema.V2ItemStartedNotification__ThreadItem, |
| 50 | CodexSchema.V2ItemCompletedNotification__ThreadItem, |
| 51 | CodexSchema.V2ThreadReadResponse__ThreadItem, |
| 52 | CodexSchema.V2ThreadResumeResponse__ThreadItem, |
| 53 | CodexSchema.V2ThreadForkResponse__ThreadItem, |
| 54 | CodexSchema.V2TurnCompletedNotification__ThreadItem, |
| 55 | ]) { |
| 56 | assert.deepEqual(Schema.decodeSync(schema)(item), item); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | it("keeps partial answers in notifications and persisted thread histories", () => { |
| 61 | const item = { |
| 62 | type: "agentMessage", |
| 63 | id: "partial-1", |
| 64 | text: "A stable answer before more tool work.", |
| 65 | phase: "partial_answer", |
| 66 | } as const; |
| 67 | for (const schema of [ |
| 68 | CodexSchema.ServerNotification__ThreadItem, |
| 69 | CodexSchema.V2ItemStartedNotification__ThreadItem, |
| 70 | CodexSchema.V2ItemCompletedNotification__ThreadItem, |
| 71 | CodexSchema.V2ThreadReadResponse__ThreadItem, |
| 72 | CodexSchema.V2ThreadResumeResponse__ThreadItem, |
| 73 | CodexSchema.V2ThreadForkResponse__ThreadItem, |
| 74 | CodexSchema.V2TurnCompletedNotification__ThreadItem, |
| 75 | ]) { |
| 76 | assert.deepEqual(Schema.decodeSync(schema)(item), item); |
| 77 | } |
| 78 | const response = { |
| 79 | approvalPolicy: "never", |
| 80 | approvalsReviewer: "user", |
| 81 | cwd: "/tmp/project", |
| 82 | model: "gpt-5.6-sol", |
| 83 | modelProvider: "openai", |
| 84 | sandbox: { type: "dangerFullAccess" }, |
| 85 | thread: { |
| 86 | cliVersion: "0.162.0", |
| 87 | createdAt: 0, |
| 88 | updatedAt: 0, |
| 89 | cwd: "/tmp/project", |
| 90 | ephemeral: false, |
| 91 | id: "thread-1", |
| 92 | modelProvider: "openai", |
| 93 | preview: "", |
| 94 | projectId: null, |
| 95 | sessionId: "session-1", |
| 96 | source: "cli", |
| 97 | status: { type: "idle" }, |
| 98 | turns: [{ id: "turn-1", status: "completed", items: [item] }], |
| 99 | }, |
| 100 | }; |
| 101 | assert.equal(isThreadForkResponse(response), true); |
| 102 | assert.equal(isThreadResumeResponse(response), true); |
| 103 | assert.equal(isThreadReadResponse({ thread: response.thread }), true); |
| 104 | }); |
| 105 | |
| 106 | it("accepts Codex 0.150 multi-agent values", () => { |
| 107 | const schemas = [ |
| 108 | CodexSchema.ServerNotification__SubAgentActivityKind, |
| 109 | CodexSchema.V2ItemStartedNotification__SubAgentActivityKind, |
| 110 | CodexSchema.V2ItemCompletedNotification__SubAgentActivityKind, |
| 111 | CodexSchema.V2ThreadReadResponse__SubAgentActivityKind, |
| 112 | CodexSchema.V2ThreadResumeResponse__SubAgentActivityKind, |
| 113 | ]; |
| 114 | |
| 115 | for (const schema of schemas) { |
| 116 | assert.equal(Schema.is(schema)("completed"), true); |
| 117 | } |
| 118 | const isServerNotificationCollabAgentTool = Schema.is( |
| 119 | CodexSchema.ServerNotification__CollabAgentTool, |
| 120 | ); |
| 121 | |
| 122 | const isV2ThreadResumeResponseCollabAgentTool = Schema.is( |
| 123 | CodexSchema.V2ThreadResumeResponse__CollabAgentTool, |
| 124 | ); |
| 125 | |
| 126 | for (const tool of ["sendMessage", "followupTask", "interruptAgent", "listAgents"]) { |
| 127 | assert.equal(isServerNotificationCollabAgentTool(tool), true); |
| 128 | assert.equal(isV2ThreadResumeResponseCollabAgentTool(tool), true); |
| 129 | } |
| 130 | const isServerNotificationCollabAgentToolCallStatus = Schema.is( |
| 131 | CodexSchema.ServerNotification__CollabAgentToolCallStatus, |
| 132 | ); |
| 133 | |
| 134 | assert.equal(isServerNotificationCollabAgentToolCallStatus("interrupted"), true); |
| 135 | const isV2ThreadResumeResponseCollabAgentToolCallStatus = Schema.is( |
| 136 | CodexSchema.V2ThreadResumeResponse__CollabAgentToolCallStatus, |
| 137 | ); |
| 138 | |
| 139 | assert.equal(isV2ThreadResumeResponseCollabAgentToolCallStatus("interrupted"), true); |
| 140 | |
| 141 | const resumeResponse = { |
| 142 | approvalPolicy: "never", |
| 143 | approvalsReviewer: "user", |
| 144 | cwd: "/tmp/project", |
| 145 | model: "gpt-5.6-sol", |
| 146 | modelProvider: "openai", |
| 147 | sandbox: { type: "dangerFullAccess" }, |
| 148 | thread: { |
| 149 | cliVersion: "0.150.0", |
| 150 | createdAt: 0, |
| 151 | cwd: "/tmp/project", |
| 152 | ephemeral: false, |
| 153 | id: "root-thread", |
| 154 | modelProvider: "openai", |
| 155 | preview: "", |
| 156 | projectId: null, |
| 157 | sessionId: "session-1", |
| 158 | source: "cli", |
| 159 | status: { type: "idle" }, |
| 160 | turns: [ |
| 161 | { |
| 162 | id: "turn-1", |
| 163 | status: "completed", |
| 164 | items: [ |
| 165 | { |
| 166 | agentsStates: {}, |
| 167 | id: "item-1", |
| 168 | receiverThreadIds: ["child-thread"], |
| 169 | senderThreadId: "root-thread", |
| 170 | status: "interrupted", |
| 171 | tool: "followupTask", |
| 172 | type: "collabAgentToolCall", |
| 173 | }, |
| 174 | ], |
| 175 | }, |
| 176 | ], |
| 177 | updatedAt: 0, |
| 178 | }, |
| 179 | }; |
| 180 | const isV2ThreadResumeResponse = Schema.is(CodexSchema.V2ThreadResumeResponse); |
| 181 | |
| 182 | assert.equal(isV2ThreadResumeResponse(resumeResponse), true); |
| 183 | }); |
| 184 | |
| 185 | it("accepts Codex misalignment policy errors for thread responses", () => { |
| 186 | const failedThread = { |
| 187 | cliVersion: "0.150.0", |
| 188 | createdAt: 0, |
| 189 | cwd: "/tmp/project", |
| 190 | ephemeral: false, |
| 191 | id: "thread-1", |
| 192 | modelProvider: "openai", |
| 193 | preview: "", |
| 194 | projectId: null, |
| 195 | sessionId: "session-1", |
| 196 | source: "cli", |
| 197 | status: { type: "idle" }, |
| 198 | turns: [ |
| 199 | { |
| 200 | error: { |
| 201 | codexErrorInfo: "misalignmentPolicyViolation", |
| 202 | message: "Misalignment policy violation", |
| 203 | }, |
| 204 | id: "turn-1", |
| 205 | items: [], |
| 206 | status: "failed", |
| 207 | }, |
| 208 | ], |
| 209 | updatedAt: 0, |
| 210 | }; |
| 211 | const resumeLikeResponse = { |
| 212 | approvalPolicy: "never", |
| 213 | approvalsReviewer: "user", |
| 214 | cwd: "/tmp/project", |
| 215 | model: "gpt-5.6-sol", |
| 216 | modelProvider: "openai", |
| 217 | sandbox: { type: "dangerFullAccess" }, |
| 218 | thread: failedThread, |
| 219 | }; |
| 220 | assert.equal(isThreadReadResponse({ thread: failedThread }), true); |
| 221 | assert.equal(isThreadResumeResponse(resumeLikeResponse), true); |
| 222 | assert.equal(isThreadForkResponse(resumeLikeResponse), true); |
| 223 | const decodedResume = decodeThreadResumeResponse(resumeLikeResponse); |
| 224 | assert.equal(decodedResume.thread.turns[0]?.error?.codexErrorInfo, "misalignmentPolicyViolation"); |
| 225 | assert.equal( |
| 226 | isTurnCompletedNotification({ |
| 227 | threadId: "thread-1", |
| 228 | turn: { |
| 229 | error: { |
| 230 | codexErrorInfo: "misalignmentPolicyViolation", |
| 231 | message: "Misalignment policy violation", |
| 232 | }, |
| 233 | id: "turn-1", |
| 234 | items: [], |
| 235 | status: "failed", |
| 236 | }, |
| 237 | }), |
| 238 | true, |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | it("accepts account plan slugs newer than the pinned protocol", () => { |
| 243 | const planTypes = [ |
| 244 | "self_serve_business_prolite", |
| 245 | "ent26", |
| 246 | "enterprise_cbp_automation", |
| 247 | "edu_plus", |
| 248 | "edu_pro", |
| 249 | "promax", |
| 250 | "some_future_plan", |
| 251 | ]; |
| 252 | |
| 253 | for (const planType of planTypes) { |
| 254 | const accountResponse = { |
| 255 | account: { |
| 256 | email: "user@example.com", |
| 257 | planType, |
| 258 | type: "chatgpt", |
| 259 | }, |
| 260 | requiresOpenaiAuth: true, |
| 261 | }; |
| 262 | |
| 263 | assert.equal(isGetAccountResponse(accountResponse), true); |
| 264 | } |
| 265 | }); |
| 266 | |
| 267 | it("accepts Codex model-request hook runs", () => { |
| 268 | for (const eventName of ["preModelRequest", "postModelRequest"]) { |
| 269 | const notification = { |
| 270 | threadId: "thread-1", |
| 271 | turnId: "turn-1", |
| 272 | run: { |
| 273 | id: `hook-${eventName}`, |
| 274 | eventName, |
| 275 | executionMode: "sync", |
| 276 | handlerType: "command", |
| 277 | scope: "turn", |
| 278 | sourcePath: "/tmp/project/.codex/hooks.json", |
| 279 | startedAt: 1, |
| 280 | status: "stopped", |
| 281 | displayOrder: 0, |
| 282 | entries: [{ kind: "stop", text: "Stop reason supplied by hook" }], |
| 283 | }, |
| 284 | }; |
| 285 | assert.equal(isHookStartedNotification(notification), true); |
| 286 | assert.equal(isHookCompletedNotification(notification), true); |
| 287 | } |
| 288 | }); |