@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, 04:45 UTC

main
ts 550 lines 18.6 KB
Raw
1 import { makeLanguageModel, type LanguageModelOptions } from "@alchemy.run/neon/LanguageModel";
2 import { expect, test } from "@alchemy.run/test";
3 import { RuntimeContext } from "alchemy/RuntimeContext";
4 import { LanguageModel, Tool, Toolkit } from "effect/ai";
5 import * as Effect from "effect/Effect";
6 import * as HttpClient from "effect/http/HttpClient";
7 import * as HttpClientResponse from "effect/http/HttpClientResponse";
8 import * as Layer from "effect/Layer";
9 import * as Redacted from "effect/Redacted";
10 import * as Result from "effect/Result";
11 import * as Schema from "effect/Schema";
12 import * as Stream from "effect/Stream";
13
14 const secret = "nt_live_SECRET_SENTINEL";
15 const Sum = Tool.make("sum", {
16 parameters: Schema.Struct({ a: Schema.Number, b: Schema.Number }),
17 success: Schema.Number,
18 });
19 const Tools = Toolkit.make(Sum);
20 const tools = Tools.toLayer({ sum: ({ a, b }) => Effect.succeed(a + b) });
21 const toolCall = {
22 id: "call_1",
23 type: "function",
24 function: { name: "sum", arguments: '{"a":2,"b":3}' },
25 };
26 const completion = (message: unknown, finish = "stop", usage?: unknown) => ({
27 choices: [{ message, finish_reason: finish }],
28 usage,
29 });
30 const chunk = (delta: unknown, finish: string | null = null, usage?: unknown) => ({
31 choices: [{ index: 0, delta, finish_reason: finish }],
32 usage,
33 });
34 const sse = (...events: Array<unknown>) =>
35 events
36 .map((value) => `data: ${typeof value === "string" ? value : JSON.stringify(value)}\n\n`)
37 .join("");
38
39 const harness = (
40 respond: () => globalThis.Response,
41 parameters?: LanguageModelOptions["parameters"],
42 ) => {
43 const requests: Array<{ url: string; authorization: string | undefined; body: unknown }> = [];
44 const http = HttpClient.make((request) =>
45 Effect.sync(() => {
46 if (request.body._tag !== "Uint8Array") throw new Error("Expected a JSON request");
47 requests.push({
48 url: request.url,
49 authorization: request.headers.authorization,
50 body: JSON.parse(new TextDecoder().decode(request.body.body)),
51 });
52 return HttpClientResponse.fromWeb(request, respond());
53 }),
54 );
55 const layer = Layer.effect(
56 LanguageModel.LanguageModel,
57 makeLanguageModel({
58 model: "test-model",
59 parameters,
60 client: {
61 chatBaseUrl: Effect.succeed("https://branch.invalid/v1/"),
62 token: Effect.succeed(Redacted.make(secret)),
63 },
64 }),
65 ).pipe(
66 Layer.provide(Layer.succeed(HttpClient.HttpClient, http)),
67 Layer.provide(RuntimeContext.phantom),
68 );
69 return { layer, requests };
70 };
71
72 test.effect(
73 "Chat route, scoped bearer, parameters and detailed usage",
74 () =>
75 Effect.gen(function* () {
76 const { layer, requests } = harness(
77 () =>
78 Response.json(
79 completion({ content: "Hello" }, "stop", {
80 prompt_tokens: 20,
81 completion_tokens: 12,
82 prompt_tokens_details: { cached_tokens: 5 },
83 completion_tokens_details: { reasoning_tokens: 7 },
84 }),
85 ),
86 { temperature: 0.2, maxTokens: 30, seed: 4 },
87 );
88 expect(requests).toHaveLength(0);
89 const response = yield* LanguageModel.generateText({ prompt: "Hello" }).pipe(
90 Effect.provide(layer),
91 );
92 expect(response.text).toBe("Hello");
93 expect(response.usage.inputTokens).toMatchObject({ total: 20, uncached: 15, cacheRead: 5 });
94 expect(response.usage.outputTokens).toMatchObject({ total: 12, text: 5, reasoning: 7 });
95 expect(requests[0]).toMatchObject({
96 url: "https://branch.invalid/v1/chat/completions",
97 authorization: `Bearer ${secret}`,
98 body: { model: "test-model", stream: false, max_tokens: 30, temperature: 0.2, seed: 4 },
99 });
100 }),
101 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
102 );
103
104 test.effect(
105 "content blocks and absent usage are not fabricated as zero",
106 () =>
107 Effect.gen(function* () {
108 const { layer } = harness(() =>
109 Response.json(
110 completion({
111 content: [
112 { type: "text", text: "Hello" },
113 { type: "text", text: " world" },
114 ],
115 }),
116 ),
117 );
118 const response = yield* LanguageModel.generateText({ prompt: "Hi" }).pipe(
119 Effect.provide(layer),
120 );
121 expect(response.text).toBe("Hello world");
122 expect(response.usage.inputTokens.total).toBeUndefined();
123 }),
124 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
125 );
126
127 test.effect(
128 "schema output sends strict JSON Schema and decodes the object",
129 () =>
130 Effect.gen(function* () {
131 const { layer, requests } = harness(() =>
132 Response.json(completion({ content: '{"greeting":"Hello"}' })),
133 );
134 const response = yield* LanguageModel.generateObject({
135 prompt: "Greet",
136 objectName: "Greeting",
137 schema: Schema.Struct({ greeting: Schema.String }),
138 }).pipe(Effect.provide(layer));
139 expect(response.value).toEqual({ greeting: "Hello" });
140 expect(requests[0]?.body).toMatchObject({
141 response_format: {
142 type: "json_schema",
143 json_schema: {
144 name: "Greeting",
145 strict: true,
146 schema: { type: "object", additionalProperties: false, required: ["greeting"] },
147 },
148 },
149 });
150 }),
151 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
152 );
153
154 test.effect(
155 "function tools execute and forced choice uses the OpenAI wire shape",
156 () =>
157 Effect.gen(function* () {
158 const { layer, requests } = harness(() =>
159 Response.json(completion({ content: null, tool_calls: [toolCall] }, "tool_calls")),
160 );
161 const response = yield* LanguageModel.generateText({
162 prompt: "Add two and three",
163 toolkit: Tools,
164 toolChoice: { tool: "sum" },
165 }).pipe(Effect.provide(layer), Effect.provide(tools));
166 expect(response.toolCalls[0]?.params).toEqual({ a: 2, b: 3 });
167 expect(response.toolResults[0]?.result).toBe(5);
168 expect(response.finishReason).toBe("tool-calls");
169 expect(requests[0]?.body).toMatchObject({
170 tool_choice: { type: "function", function: { name: "sum" } },
171 tools: [{ type: "function", function: { name: "sum" } }],
172 });
173 }),
174 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
175 );
176
177 test.effect(
178 "prompt round-trips assistant calls and tool results",
179 () =>
180 Effect.gen(function* () {
181 const { layer, requests } = harness(() => Response.json(completion({ content: "Five" })));
182 yield* LanguageModel.generateText({
183 prompt: [
184 { role: "system", content: "Calculate" },
185 { role: "user", content: [{ type: "text", text: "Add" }] },
186 {
187 role: "assistant",
188 content: [{ type: "tool-call", id: "call_1", name: "sum", params: { a: 2, b: 3 } }],
189 },
190 {
191 role: "tool",
192 content: [
193 { type: "tool-result", id: "call_1", name: "sum", result: 5, isFailure: false },
194 ],
195 },
196 ],
197 }).pipe(Effect.provide(layer));
198 expect(requests[0]?.body).toMatchObject({
199 messages: [
200 { role: "system", content: "Calculate" },
201 { role: "user", content: [{ type: "text", text: "Add" }] },
202 { role: "assistant", content: null, tool_calls: [toolCall] },
203 { role: "tool", tool_call_id: "call_1", content: "5" },
204 ],
205 });
206 }),
207 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
208 );
209
210 test.effect(
211 "SSE emits text ordering, late usage and one finish",
212 () =>
213 Effect.gen(function* () {
214 const { layer, requests } = harness(
215 () =>
216 new Response(
217 sse(
218 chunk({ role: "assistant", content: "Hel" }),
219 chunk({ content: "lo" }),
220 chunk({}, "stop"),
221 { choices: [], usage: { prompt_tokens: 4, completion_tokens: 2 } },
222 "[DONE]",
223 ),
224 { headers: { "content-type": "text/event-stream" } },
225 ),
226 );
227 const parts = yield* LanguageModel.streamText({ prompt: "Hi" }).pipe(
228 Stream.provide(layer),
229 Stream.runCollect,
230 );
231 expect(parts.map((part) => part.type)).toEqual([
232 "text-start",
233 "text-delta",
234 "text-delta",
235 "text-end",
236 "finish",
237 ]);
238 expect(parts.find((part) => part.type === "finish")?.usage.inputTokens.total).toBe(4);
239 expect(requests[0]?.body).toMatchObject({
240 stream: true,
241 stream_options: { include_usage: true },
242 });
243 }),
244 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
245 );
246
247 test.effect(
248 "interleaved streamed tools append fragments and execute once per call",
249 () =>
250 Effect.gen(function* () {
251 const { layer } = harness(
252 () =>
253 new Response(
254 sse(
255 chunk({
256 tool_calls: [
257 {
258 index: 0,
259 id: "one",
260 type: "function",
261 function: { name: "sum", arguments: '{"a":2,' },
262 },
263 {
264 index: 1,
265 id: "two",
266 type: "function",
267 function: { name: "sum", arguments: '{"a":4,' },
268 },
269 ],
270 }),
271 chunk({
272 tool_calls: [
273 { index: 1, function: { arguments: '"b":5}' } },
274 { index: 0, function: { arguments: '"b":3}' } },
275 ],
276 }),
277 chunk({}, "tool_calls"),
278 "[DONE]",
279 ),
280 ),
281 );
282 const parts = yield* LanguageModel.streamText({
283 prompt: "Add",
284 toolkit: Tools,
285 toolChoice: "required",
286 }).pipe(Stream.provide(layer), Stream.provide(tools), Stream.runCollect);
287 expect(parts.filter((part) => part.type === "tool-call").map((part) => part.params)).toEqual([
288 { a: 2, b: 3 },
289 { a: 4, b: 5 },
290 ]);
291 expect(
292 parts
293 .filter((part) => part.type === "tool-result")
294 .map((part) => part.result)
295 .sort(),
296 ).toEqual([5, 9]);
297 expect(parts.filter((part) => part.type === "tool-params-start")).toHaveLength(2);
298 expect(parts.filter((part) => part.type === "finish")).toHaveLength(1);
299 }),
300 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
301 );
302
303 for (const [name, body] of [
304 ["malformed JSON", "data: not-json\n\n"],
305 ["premature EOF", sse(chunk({ content: "partial" }))],
306 ["missing DONE", sse(chunk({ content: "partial" }, "stop"))],
307 ["empty stream", ""],
308 ["invalid schema", sse({ choices: [{ delta: { content: 42 }, index: 0 }] }, "[DONE]")],
309 ["error event", 'event: error\ndata: {"error":{"message":"nt_live_SECRET_SENTINEL"}}\n\n'],
310 ] as const) {
311 test.effect(
312 `stream rejects ${name} without disclosing upstream payload`,
313 () =>
314 Effect.gen(function* () {
315 const { layer } = harness(() => new Response(body));
316 const result = yield* LanguageModel.streamText({ prompt: "Hi" }).pipe(
317 Stream.provide(layer),
318 Stream.runCollect,
319 Effect.result,
320 );
321 expect(Result.isFailure(result)).toBe(true);
322 expect(JSON.stringify(result)).not.toContain(secret);
323 }),
324 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
325 );
326 }
327
328 for (const [status, code, tag] of [
329 [401, undefined, "AuthenticationError"],
330 [403, undefined, "AuthenticationError"],
331 [402, undefined, "QuotaExhaustedError"],
332 [429, "REQUEST_LIMIT_EXCEEDED", "QuotaExhaustedError"],
333 [429, undefined, "RateLimitError"],
334 [502, undefined, "InternalProviderError"],
335 [400, undefined, "InvalidRequestError"],
336 ] as const) {
337 test.effect(
338 `HTTP ${status} ${code ?? ""} maps to ${tag} and scrubs secrets`,
339 () =>
340 Effect.gen(function* () {
341 const { layer } = harness(() =>
342 Response.json(
343 { error: { code, message: `Bearer ${secret}` } },
344 { status, headers: { "retry-after": "2" } },
345 ),
346 );
347 const result = yield* LanguageModel.generateText({ prompt: "Hi" }).pipe(
348 Effect.provide(layer),
349 Effect.result,
350 );
351 expect(Result.isFailure(result)).toBe(true);
352 if (Result.isFailure(result)) expect(result.failure.reason._tag).toBe(tag);
353 expect(JSON.stringify(result)).not.toContain(secret);
354 }),
355 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
356 );
357 }
358
359 test.effect(
360 "malformed tool JSON fails rather than executing a partial tool",
361 () =>
362 Effect.gen(function* () {
363 const { layer } = harness(() =>
364 Response.json(
365 completion(
366 { tool_calls: [{ ...toolCall, function: { ...toolCall.function, arguments: "{" } }] },
367 "tool_calls",
368 ),
369 ),
370 );
371 const result = yield* LanguageModel.generateText({ prompt: "Hi", toolkit: Tools }).pipe(
372 Effect.provide(layer),
373 Effect.provide(tools),
374 Effect.result,
375 );
376 expect(Result.isFailure(result)).toBe(true);
377 }),
378 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
379 );
380
381 test.effect(
382 "invalid JSON schema responses fail Effect's structured-output validation",
383 () =>
384 Effect.gen(function* () {
385 const { layer } = harness(() => Response.json(completion({ content: '{"greeting":42}' })));
386 const result = yield* LanguageModel.generateObject({
387 prompt: "Greet",
388 schema: Schema.Struct({ greeting: Schema.String }),
389 }).pipe(Effect.provide(layer), Effect.result);
390 expect(Result.isFailure(result)).toBe(true);
391 }),
392 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
393 );
394
395 test.effect(
396 "disabled-account rejection is non-retryable quota exhaustion, not a bad token",
397 () =>
398 Effect.gen(function* () {
399 const { layer } = harness(() =>
400 Response.json(
401 { error: { message: "ai gateway not enabled for account" } },
402 { status: 403 },
403 ),
404 );
405 const result = yield* LanguageModel.generateText({ prompt: "Hi" }).pipe(
406 Effect.provide(layer),
407 Effect.result,
408 );
409 expect(Result.isFailure(result)).toBe(true);
410 if (Result.isFailure(result)) {
411 expect(result.failure.reason._tag).toBe("QuotaExhaustedError");
412 expect(result.failure.reason.isRetryable).toBe(false);
413 }
414 }),
415 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
416 );
417
418 test.effect(
419 "fragmented UTF-8 SSE chunks preserve multibyte text",
420 () =>
421 Effect.gen(function* () {
422 const { layer } = harness(
423 () =>
424 new Response(
425 new ReadableStream<Uint8Array>({
426 start(controller) {
427 const bytes = new TextEncoder().encode(
428 sse(chunk({ content: "héllo 🌍" }, "stop"), "[DONE]"),
429 );
430 for (const byte of bytes) controller.enqueue(new Uint8Array([byte]));
431 controller.close();
432 },
433 }),
434 ),
435 );
436 const parts = yield* LanguageModel.streamText({ prompt: "Hi" }).pipe(
437 Stream.provide(layer),
438 Stream.runCollect,
439 );
440 expect(
441 parts
442 .filter((part) => part.type === "text-delta")
443 .map((part) => part.delta)
444 .join(""),
445 ).toBe("héllo 🌍");
446 }),
447 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
448 );
449
450 test.effect(
451 "truncated streamed tools never run a handler",
452 () =>
453 Effect.gen(function* () {
454 const { layer } = harness(
455 () =>
456 new Response(
457 sse(
458 chunk({
459 tool_calls: [
460 {
461 index: 0,
462 id: "one",
463 type: "function",
464 function: { name: "sum", arguments: '{"a":2,' },
465 },
466 ],
467 }),
468 chunk({}, "length"),
469 "[DONE]",
470 ),
471 ),
472 );
473 const parts = yield* LanguageModel.streamText({ prompt: "Add", toolkit: Tools }).pipe(
474 Stream.provide(layer),
475 Stream.provide(tools),
476 Stream.runCollect,
477 );
478 expect(parts.filter((part) => part.type === "tool-result")).toHaveLength(0);
479 expect(parts.find((part) => part.type === "finish")?.reason).toBe("length");
480 }),
481 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
482 );
483
484 test.effect(
485 "image inputs preserve order while audio fails before HTTP",
486 () =>
487 Effect.gen(function* () {
488 const { layer, requests } = harness(() => Response.json(completion({ content: "Image" })));
489 yield* LanguageModel.generateText({
490 prompt: [
491 {
492 role: "user",
493 content: [
494 { type: "text", text: "Before" },
495 { type: "file", mediaType: "image/png", data: new Uint8Array([1, 2]) },
496 { type: "text", text: "After" },
497 ],
498 },
499 ],
500 }).pipe(Effect.provide(layer));
501 expect(requests[0]?.body).toMatchObject({
502 messages: [
503 {
504 role: "user",
505 content: [
506 { type: "text", text: "Before" },
507 { type: "image_url", image_url: { url: "data:image/png;base64,AQI=" } },
508 { type: "text", text: "After" },
509 ],
510 },
511 ],
512 });
513 const result = yield* LanguageModel.generateText({
514 prompt: [
515 { role: "user", content: [{ type: "file", mediaType: "audio/wav", data: "AA==" }] },
516 ],
517 }).pipe(Effect.provide(layer), Effect.result);
518 expect(Result.isFailure(result)).toBe(true);
519 expect(requests).toHaveLength(1);
520 }),
521 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
522 );
523
524 test.effect(
525 "downstream cancellation closes the HTTP stream without waiting for DONE",
526 () =>
527 Effect.gen(function* () {
528 let cancelled = false;
529 const { layer } = harness(
530 () =>
531 new Response(
532 new ReadableStream<Uint8Array>({
533 start(controller) {
534 controller.enqueue(new TextEncoder().encode(sse(chunk({ content: "Hello" }))));
535 },
536 cancel() {
537 cancelled = true;
538 },
539 }),
540 ),
541 );
542 yield* LanguageModel.streamText({ prompt: "Hi" }).pipe(
543 Stream.provide(layer),
544 Stream.take(1),
545 Stream.runDrain,
546 );
547 expect(cancelled).toBe(true);
548 }),
549 { tags: ["unit", "provider:neon", "provider:neon:languagemodel", "local"] },
550 );