@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 06:00 UTC

main
ts 613 lines 19.8 KB
Raw
1 import { OpenAiClient } from "@effect/ai-openai-compat"
2 import * as Errors from "@effect/ai-openai-compat/internal/errors"
3 import { assert, describe, it } from "@effect/vitest"
4 import { Context, Effect, Layer, Redacted, type Schema, Stream } from "effect"
5 import { HttpClient, type HttpClientError, type HttpClientRequest, HttpClientResponse } from "effect/http"
6
7 describe("OpenAiClient", () => {
8 describe("request behavior", () => {
9 it.effect("sets auth and OpenAI headers on /chat/completions requests", () =>
10 Effect.gen(function*() {
11 const client = yield* OpenAiClient.OpenAiClient
12
13 yield* client.createResponse({
14 model: "gpt-4o-mini",
15 messages: [{ role: "user", content: "hello" }]
16 })
17
18 const requests = yield* MockHttpClient.requests
19 const request = requests[0]
20 const body = yield* getRequestBody(request)
21
22 assert.isTrue(request.url.endsWith("/chat/completions"))
23 assert.isTrue(request.url.startsWith("https://compat.example.test/v1"))
24 assert.strictEqual(request.headers["authorization"], "Bearer sk-test-key")
25 assert.strictEqual(request.headers["openai-organization"], "org_123")
26 assert.strictEqual(request.headers["openai-project"], "proj_456")
27
28 assert.strictEqual(body.messages[0]?.role, "user")
29 assert.strictEqual(body.messages[0]?.content, "hello")
30 }).pipe(Effect.provide(makeTestLayer())))
31
32 it.effect("passes custom chat-completions request properties through", () =>
33 Effect.gen(function*() {
34 const client = yield* OpenAiClient.OpenAiClient
35
36 yield* client.createResponse({
37 model: "gpt-4o-mini",
38 messages: [{ role: "user", content: "hello" }],
39 provider_feature: {
40 enabled: true
41 }
42 })
43
44 const requests = yield* MockHttpClient.requests
45 const request = requests[0]
46 const body = yield* getRequestBody(request)
47 assert.deepStrictEqual(body.provider_feature, {
48 enabled: true
49 })
50 }).pipe(Effect.provide(makeTestLayer())))
51
52 it.effect("uses /embeddings path and decodes permissive embedding payloads", () =>
53 Effect.gen(function*() {
54 const client = yield* OpenAiClient.OpenAiClient
55
56 const embedding = yield* client.createEmbedding({
57 model: "my-custom-embedding-model",
58 input: "embed this"
59 })
60
61 const requests = yield* MockHttpClient.requests
62 const request = requests[0]
63
64 assert.isTrue(request.url.endsWith("/embeddings"))
65 assert.strictEqual(embedding.model, "my-custom-embedding-model")
66 assert.strictEqual(embedding.data[0]?.index, 0)
67 assert.strictEqual(typeof embedding.data[0]?.embedding, "string")
68 }).pipe(Effect.provide(makeTestLayer({
69 _tag: "Json",
70 body: {
71 data: [{
72 embedding: "YmFzZTY0LWRhdGE=",
73 index: 0,
74 object: "embedding",
75 vendor_payload: { future_field: true }
76 }],
77 model: "my-custom-embedding-model",
78 object: "list",
79 usage: {
80 prompt_tokens: 5,
81 total_tokens: 5
82 },
83 unknown_top_level: true
84 }
85 }))))
86
87 it.effect("sets stream=true for createResponseStream and returns chat chunks", () =>
88 Effect.gen(function*() {
89 const client = yield* OpenAiClient.OpenAiClient
90
91 const events = yield* client.createResponseStream({
92 model: "gpt-4o-mini",
93 messages: [{ role: "user", content: "hello" }]
94 }).pipe(
95 Effect.flatMap(([_, stream]) => Stream.runCollect(stream))
96 )
97
98 const requests = yield* MockHttpClient.requests
99 const request = requests[0]
100 const body = yield* getRequestBody(request)
101 assert.strictEqual(body.stream, true)
102 assert.strictEqual(body.stream_options.include_usage, true)
103 assert.isTrue(request.url.endsWith("/chat/completions"))
104
105 assert.propertyVal(events[0], "id", "chatcmpl_test_1")
106 assert.propertyVal(events[1], "id", "chatcmpl_test_1")
107 assert.strictEqual(events[2], "[DONE]")
108 }).pipe(Effect.provide(makeTestLayer({
109 _tag: "Sse",
110 events: [
111 {
112 id: "chatcmpl_test_1",
113 object: "chat.completion.chunk",
114 model: "gpt-4o-mini",
115 created: 1,
116 future_provider_field: { accepted: true },
117 choices: [{
118 index: 0,
119 delta: { content: "Hello" },
120 finish_reason: null
121 }]
122 },
123 {
124 id: "chatcmpl_test_1",
125 object: "chat.completion.chunk",
126 model: "gpt-4o-mini",
127 created: 1,
128 usage: {
129 prompt_tokens: 4,
130 completion_tokens: 2,
131 total_tokens: 6,
132 prompt_tokens_details: { cached_tokens: 1 },
133 completion_tokens_details: { reasoning_tokens: 1 }
134 },
135 choices: [{
136 index: 0,
137 delta: {},
138 finish_reason: "stop"
139 }]
140 },
141 "[DONE]"
142 ]
143 }))))
144
145 it.effect("surfaces schema-mismatched chat chunks and continues streaming", () =>
146 Effect.gen(function*() {
147 const client = yield* OpenAiClient.OpenAiClient
148
149 const events = yield* client.createResponseStream({
150 model: "gpt-4o-mini",
151 messages: [{ role: "user", content: "hello" }]
152 }).pipe(
153 Effect.flatMap(([_, stream]) => Stream.runCollect(stream))
154 )
155
156 assert.deepStrictEqual(events[0], {
157 _tag: "UnknownChatCompletionEvent",
158 data: {
159 type: "provider.chat.completion.delta",
160 provider_payload: { content: "provider-specific" }
161 }
162 })
163 assert.propertyVal(events[1], "id", "chatcmpl_test_2")
164 assert.strictEqual(events[2], "[DONE]")
165 }).pipe(Effect.provide(makeTestLayer({
166 _tag: "Sse",
167 events: [
168 {
169 type: "provider.chat.completion.delta",
170 provider_payload: { content: "provider-specific" }
171 },
172 {
173 id: "chatcmpl_test_2",
174 object: "chat.completion.chunk",
175 model: "gpt-4o-mini",
176 created: 1,
177 choices: [{
178 index: 0,
179 delta: { content: "Hello" },
180 finish_reason: null
181 }]
182 },
183 "[DONE]"
184 ]
185 }))))
186
187 it.effect("drops invalid JSON and streams valid events until [DONE]", () =>
188 Effect.gen(function*() {
189 const client = yield* OpenAiClient.OpenAiClient
190
191 const events = yield* client.createResponseStream({
192 model: "gpt-4o-mini",
193 messages: [{ role: "user", content: "hello" }]
194 }).pipe(
195 Effect.flatMap(([_, stream]) => Stream.runCollect(stream))
196 )
197
198 assert.strictEqual(events.length, 2)
199 assert.propertyVal(events[0], "id", "chatcmpl_test_3")
200 assert.strictEqual(events[1], "[DONE]")
201 }).pipe(Effect.provide(makeTestLayer({
202 _tag: "RawSse",
203 body: [
204 "data: {invalid-json\n\n",
205 `data: ${
206 JSON.stringify({
207 id: "chatcmpl_test_3",
208 object: "chat.completion.chunk",
209 model: "gpt-4o-mini",
210 created: 1,
211 choices: [{
212 index: 0,
213 delta: { content: "Hello" },
214 finish_reason: null
215 }]
216 })
217 }\n\n`,
218 "data: [DONE]\n\n",
219 "data: {\"ignored\":\"after completion\"}\n\n"
220 ].join("")
221 }))))
222
223 it.effect("passes chat-completions tool_choice payload through unchanged", () =>
224 Effect.gen(function*() {
225 const client = yield* OpenAiClient.OpenAiClient
226
227 yield* client.createResponse({
228 model: "gpt-4o-mini",
229 messages: [{ role: "user", content: "hello" }],
230 tool_choice: {
231 type: "function",
232 function: {
233 name: "TestTool"
234 }
235 },
236 tools: [{
237 type: "function",
238 function: {
239 name: "TestTool",
240 parameters: {
241 type: "object",
242 additionalProperties: false,
243 properties: {
244 input: { type: "string" }
245 },
246 required: ["input"]
247 }
248 }
249 }]
250 })
251
252 const requests = yield* MockHttpClient.requests
253 const request = requests[0]
254 const body = yield* getRequestBody(request)
255
256 assert.deepStrictEqual(body.tool_choice, {
257 type: "function",
258 function: { name: "TestTool" }
259 })
260 }).pipe(Effect.provide(makeTestLayer())))
261
262 it.effect("accepts assistant tool-call and tool result chat history", () =>
263 Effect.gen(function*() {
264 const client = yield* OpenAiClient.OpenAiClient
265
266 yield* client.createResponse({
267 model: "gpt-4o-mini",
268 messages: [
269 {
270 role: "assistant",
271 content: null,
272 tool_calls: [{
273 id: "patch_call_1",
274 type: "function",
275 function: {
276 name: "apply_patch",
277 arguments: JSON.stringify({
278 call_id: "patch_call_1",
279 operation: {
280 type: "delete_file",
281 path: "src/obsolete.ts"
282 }
283 })
284 }
285 }]
286 },
287 {
288 role: "tool",
289 tool_call_id: "patch_call_1",
290 content: "deleted"
291 }
292 ]
293 })
294
295 const requests = yield* MockHttpClient.requests
296 const request = requests[0]
297 const body = yield* getRequestBody(request)
298
299 const assistantMessages = body.messages.filter((message: any) => message.role === "assistant")
300 const patchMessage = assistantMessages.find((message: any) =>
301 message.tool_calls?.[0]?.function?.name === "apply_patch"
302 )
303
304 assert.isDefined(patchMessage)
305
306 assert.strictEqual(patchMessage.tool_calls[0].id, "patch_call_1")
307 assert.deepStrictEqual(JSON.parse(patchMessage.tool_calls[0].function.arguments), {
308 call_id: "patch_call_1",
309 operation: {
310 type: "delete_file",
311 path: "src/obsolete.ts"
312 }
313 })
314
315 const toolMessages = body.messages.filter((message: any) => message.role === "tool")
316 const patchOutput = toolMessages.find((message: any) => message.tool_call_id === "patch_call_1")
317 assert.isDefined(patchOutput)
318 assert.strictEqual(patchOutput.content, "deleted")
319 }).pipe(Effect.provide(makeTestLayer())))
320
321 it.effect("redacts OpenAI-specific headers in AI error context", () =>
322 Effect.gen(function*() {
323 const client = yield* OpenAiClient.OpenAiClient
324
325 const result = yield* client.createResponse({
326 model: "gpt-4o-mini",
327 messages: [{ role: "user", content: "hello" }]
328 }).pipe(Effect.flip)
329
330 assert.strictEqual(result.reason._tag, "InvalidRequestError")
331 const output = JSON.stringify(result)
332 assert.notInclude(output, "sk-test-key")
333 assert.notInclude(output, "org_123")
334 assert.notInclude(output, "proj_456")
335 }).pipe(Effect.provide(makeTestLayer({
336 _tag: "Json",
337 status: 400,
338 body: {
339 error: {
340 message: "Bad request",
341 type: "invalid_request_error",
342 code: null
343 }
344 }
345 }))))
346 })
347
348 describe("error mapping", () => {
349 it.effect("maps 400 responses to InvalidRequestError", () =>
350 Effect.gen(function*() {
351 const client = yield* OpenAiClient.OpenAiClient
352
353 const error = yield* client.createResponse({
354 model: "gpt-4o-mini",
355 messages: [{ role: "user", content: "hello" }]
356 }).pipe(Effect.flip)
357
358 assert.strictEqual(error._tag, "AiError")
359 assert.strictEqual(error.method, "createResponse")
360 assert.strictEqual(error.reason._tag, "InvalidRequestError")
361 }).pipe(Effect.provide(makeTestLayer({
362 _tag: "Json",
363 status: 400,
364 body: {
365 error: {
366 message: "Bad request",
367 type: "invalid_request_error",
368 code: null
369 }
370 }
371 }))))
372
373 it.effect("surfaces the provider message on 401 AuthenticationError", () =>
374 Effect.gen(function*() {
375 const client = yield* OpenAiClient.OpenAiClient
376
377 const error = yield* client.createResponse({
378 model: "gpt-4o-mini",
379 messages: [{ role: "user", content: "hello" }]
380 }).pipe(Effect.flip)
381
382 assert.strictEqual(error.reason._tag, "AuthenticationError")
383 if (error.reason._tag !== "AuthenticationError") {
384 return yield* Effect.die(new Error("Expected AuthenticationError"))
385 }
386 assert.strictEqual(error.reason.kind, "InvalidKey")
387 assert.strictEqual(
388 error.reason.description,
389 "Incorrect API key provided (POST https://compat.example.test/v1/chat/completions) [code: invalid_api_key] [requestId: req_openai_compat]"
390 )
391 assert.include(error.reason.message, "Incorrect API key provided")
392 }).pipe(Effect.provide(makeTestLayer({
393 _tag: "Json",
394 status: 401,
395 body: {
396 error: {
397 message: "Incorrect API key provided",
398 type: "invalid_request_error",
399 code: "invalid_api_key"
400 }
401 },
402 headers: { "x-request-id": "req_openai_compat" }
403 }))))
404
405 it("preserves and truncates a fallback HTTP response", () => {
406 const body = `${"a".repeat(200)}b`
407 const reason = Errors.mapStatusCodeToReason({
408 now: 0,
409 status: 400,
410 headers: {},
411 message: undefined,
412 metadata: { errorCode: null, errorType: null, requestId: null },
413 method: "POST",
414 url: "https://compat.example.test/v1/chat/completions",
415 http: { body }
416 })
417
418 assert.strictEqual(reason._tag, "InvalidRequestError")
419 if (reason._tag !== "InvalidRequestError") {
420 throw new Error("Expected InvalidRequestError")
421 }
422 assert.strictEqual(
423 reason.description,
424 `HTTP 400 (POST https://compat.example.test/v1/chat/completions) Response: ${"a".repeat(200)}...`
425 )
426 })
427
428 it.effect("surfaces the provider message on 403 AuthenticationError", () =>
429 Effect.gen(function*() {
430 const client = yield* OpenAiClient.OpenAiClient
431
432 const error = yield* client.createResponse({
433 model: "gpt-4o-mini",
434 messages: [{ role: "user", content: "hello" }]
435 }).pipe(Effect.flip)
436
437 assert.strictEqual(error.reason._tag, "AuthenticationError")
438 if (error.reason._tag !== "AuthenticationError") {
439 return yield* Effect.die(new Error("Expected AuthenticationError"))
440 }
441 assert.strictEqual(error.reason.kind, "InsufficientPermissions")
442 assert.include(error.reason.description ?? "", "Country, region, or territory not supported")
443 assert.include(error.reason.message, "Country, region, or territory not supported")
444 }).pipe(Effect.provide(makeTestLayer({
445 _tag: "Json",
446 status: 403,
447 body: {
448 error: {
449 message: "Country, region, or territory not supported",
450 type: "permission_error",
451 code: null
452 }
453 }
454 }))))
455
456 it.effect("maps insufficient quota errors to QuotaExhaustedError", () =>
457 Effect.gen(function*() {
458 const client = yield* OpenAiClient.OpenAiClient
459
460 const error = yield* client.createResponse({
461 model: "gpt-4o-mini",
462 messages: [{ role: "user", content: "hello" }]
463 }).pipe(Effect.flip)
464
465 assert.strictEqual(error._tag, "AiError")
466 assert.strictEqual(error.method, "createResponse")
467 assert.strictEqual(error.reason._tag, "QuotaExhaustedError")
468 }).pipe(Effect.provide(makeTestLayer({
469 _tag: "Json",
470 status: 429,
471 body: {
472 error: {
473 message: "You exceeded your current quota",
474 type: "insufficient_quota",
475 code: "insufficient_quota"
476 }
477 }
478 }))))
479 })
480 })
481
482 type MockResponse =
483 | {
484 readonly _tag: "Json"
485 readonly body: Schema.Json
486 readonly status?: number | undefined
487 readonly headers?: Record<string, string> | undefined
488 }
489 | {
490 readonly _tag: "Sse"
491 readonly events: ReadonlyArray<Schema.Json>
492 readonly status?: number | undefined
493 readonly headers?: Record<string, string> | undefined
494 }
495 | {
496 readonly _tag: "RawSse"
497 readonly body: string
498 readonly status?: number | undefined
499 readonly headers?: Record<string, string> | undefined
500 }
501
502 class MockOpenAiResponse extends Context.Service<MockOpenAiResponse, {
503 readonly response: MockResponse
504 }>()("MockOpenAiResponse") {}
505
506 class MockHttpClient extends Context.Service<MockHttpClient, {
507 readonly requests: Effect.Effect<ReadonlyArray<HttpClientRequest.HttpClientRequest>>
508 }>()("MockHttpClient") {
509 static requests = MockHttpClient.use((client) => client.requests)
510 }
511
512 const makeHttpClientContext = Effect.gen(function*() {
513 const capturedRequests: Array<HttpClientRequest.HttpClientRequest> = []
514 const mock = yield* MockOpenAiResponse
515
516 const httpClient = HttpClient.makeWith(
517 Effect.fnUntraced(function*(requestEffect) {
518 const request = yield* requestEffect
519 capturedRequests.push(request)
520 return makeResponse(request, mock.response)
521 }),
522 Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never>
523 )
524
525 const mockHttpClient: MockHttpClient["Service"] = {
526 requests: Effect.sync(() => capturedRequests)
527 }
528
529 return Context.make(HttpClient.HttpClient, httpClient).pipe(
530 Context.add(MockHttpClient, mockHttpClient)
531 )
532 })
533
534 const HttpClientLayer = Layer.effectContext(makeHttpClientContext)
535
536 const makeTestLayer = (response: MockResponse = {
537 _tag: "Json",
538 body: makeCreateResponse()
539 }) =>
540 OpenAiClient.layer({
541 apiKey: Redacted.make("sk-test-key"),
542 apiUrl: "https://compat.example.test/v1",
543 organizationId: Redacted.make("org_123"),
544 projectId: Redacted.make("proj_456")
545 }).pipe(
546 Layer.provideMerge(HttpClientLayer),
547 Layer.provide(Layer.succeed(MockOpenAiResponse, {
548 response
549 }))
550 )
551
552 const makeCreateResponse = (
553 overrides: Partial<OpenAiClient.CreateResponse200> = {}
554 ) => ({
555 id: "chatcmpl_test_1",
556 object: "chat.completion",
557 model: "gpt-4o-mini",
558 created: 1,
559 choices: [{
560 index: 0,
561 finish_reason: "stop",
562 message: {
563 role: "assistant",
564 content: "Hello"
565 }
566 }],
567 usage: {
568 prompt_tokens: 1,
569 completion_tokens: 1,
570 total_tokens: 2
571 },
572 ...overrides
573 })
574
575 const makeResponse = (
576 request: HttpClientRequest.HttpClientRequest,
577 response: MockResponse
578 ): HttpClientResponse.HttpClientResponse => {
579 const contentType = response._tag === "Json"
580 ? "application/json"
581 : "text/event-stream"
582 const body = response._tag === "Json"
583 ? JSON.stringify(response.body)
584 : response._tag === "Sse"
585 ? toSseBody(response.events)
586 : response.body
587
588 return HttpClientResponse.fromWeb(
589 request,
590 new Response(body, {
591 status: response.status ?? 200,
592 headers: {
593 "content-type": contentType,
594 ...response.headers
595 }
596 })
597 )
598 }
599
600 const getRequestBody = Effect.fnUntraced(function*(request: HttpClientRequest.HttpClientRequest) {
601 const body = request.body
602 if (body._tag === "Uint8Array") {
603 const text = new TextDecoder().decode(body.body)
604 return JSON.parse(text)
605 }
606 return yield* Effect.die(new Error("Expected Uint8Array body"))
607 })
608
609 const toSseBody = (events: ReadonlyArray<Schema.Json>): string =>
610 events.map((event) => {
611 const data = event === "[DONE]" ? event : JSON.stringify(event)
612 return `data: ${data}\n\n`
613 }).join("")