Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenRouterLanguageModel.test.ts

899 lines34.3 KB

1import { Generated, OpenRouterClient, OpenRouterLanguageModel } from "@effect/ai-openrouter"
2import { assert, describe, it } from "@effect/vitest"
3import { deepStrictEqual, strictEqual } from "@effect/vitest/utils"
4import { Array, Context, Effect, Layer, Redacted, Ref, Schema, Stream } from "effect"
5import { LanguageModel, Model, Prompt, Tool, Toolkit } from "effect/ai"
6import { HttpClient, type HttpClientError, type HttpClientRequest, HttpClientResponse } from "effect/http"
7​
8describe("OpenRouterLanguageModel", () => {
9 describe("strictJsonSchema", () => {
10 for (
11 const [model, strict] of [
12 ["openai/gpt-4o-mini", true],
13 ["google/gemini-2.5-flash", null]
14 ] as const
15 ) {
16 it.effect(`defaults response strictness to ${strict} for ${model}`, () =>
17 Effect.gen(function*() {
18 yield* LanguageModel.generateObject({
19 prompt: "Give me a name",
20 schema: Schema.Struct({ name: Schema.String })
21 }).pipe(Effect.provide(OpenRouterLanguageModel.model(model)))
22​
23 const requests = yield* MockHttpClient.requests
24 const body = yield* getRequestBody(requests[0])
25 strictEqual(body.response_format.json_schema.strict, strict)
26 }).pipe(Effect.provide(makeTestLayer({
27 body: {
28 choices: [{
29 finish_reason: "stop",
30 index: 0,
31 message: { role: "assistant", content: JSON.stringify({ name: "Alice" }) }
32 }]
33 }
34 }))))
35 }
36​
37 it.effect("omits false from requests while preserving response strictness", () =>
38 Effect.gen(function*() {
39 yield* LanguageModel.generateObject({
40 prompt: "Give me a name",
41 schema: Schema.Struct({ name: Schema.String })
42 }).pipe(Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini", { strictJsonSchema: false })))
43​
44 const requests = yield* MockHttpClient.requests
45 const body = yield* getRequestBody(requests[0])
46 strictEqual(body.response_format.json_schema.strict, false)
47 assert.notProperty(body, "strictJsonSchema")
48 }).pipe(Effect.provide(makeTestLayer({
49 body: {
50 choices: [{
51 finish_reason: "stop",
52 index: 0,
53 message: { role: "assistant", content: JSON.stringify({ name: "Alice" }) }
54 }]
55 }
56 }))))
57​
58 it.effect("omits true from streaming requests while preserving tool strictness", () =>
59 Effect.gen(function*() {
60 const tool = Tool.make("FlexibleTool", { parameters: Schema.Struct({ query: Schema.String }) })
61 .annotate(Tool.Strict, false)
62 yield* LanguageModel.streamText({
63 prompt: "Use a tool",
64 toolkit: Toolkit.make(tool),
65 disableToolCallResolution: true
66 }).pipe(
67 Stream.runDrain,
68 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini", { strictJsonSchema: true }))
69 )
70​
71 const requests = yield* MockHttpClient.requests
72 const body = yield* getRequestBody(requests[0])
73 strictEqual(body.stream, true)
74 strictEqual(body.tools[0].function.strict, false)
75 assert.notProperty(body, "strictJsonSchema")
76 }).pipe(Effect.provide(makeStreamTestLayer([]))))
77 })
78​
79 it.effect("model provides openrouter provider metadata", () =>
80 Effect.gen(function*() {
81 strictEqual(yield* Model.ProviderName, "openrouter")
82 }).pipe(
83 Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")),
84 Effect.provide(makeTestLayer())
85 ))
86​
87 describe("generateText", () => {
88 describe("message preparation", () => {
89 describe("audio file parts", () => {
90 it.effect("converts audio bytes to input_audio", () =>
91 Effect.gen(function*() {
92 const audioData = new Uint8Array([0x49, 0x44, 0x33, 0x04]) // ID3v2 magic bytes
93​
94 yield* LanguageModel.generateText({
95 prompt: Prompt.make([{
96 role: "user",
97 content: [
98 Prompt.filePart({
99 mediaType: "audio/mpeg",
100 data: audioData
101 })
102 ]
103 }])
104 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
105​
106 const requests = yield* MockHttpClient.requests
107 const body = yield* getRequestBody(requests[0])
108​
109 const userMessage = body.messages.find((message: any) => message.role === "user")
110 deepStrictEqual(userMessage.content, [{
111 type: "input_audio",
112 input_audio: {
113 data: "SUQzBA==",
114 format: "mp3"
115 }
116 }])
117 }).pipe(Effect.provide(makeTestLayer())))
118​
119 it.effect("converts base64 data url audio to input_audio", () =>
120 Effect.gen(function*() {
121 yield* LanguageModel.generateText({
122 prompt: Prompt.make([{
123 role: "user",
124 content: [
125 Prompt.filePart({
126 mediaType: "audio/wav",
127 data: "data:audio/wav;base64,UklGRg=="
128 })
129 ]
130 }])
131 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
132​
133 const requests = yield* MockHttpClient.requests
134 const body = yield* getRequestBody(requests[0])
135​
136 const userMessage = body.messages.find((message: any) => message.role === "user")
137 deepStrictEqual(userMessage.content, [{
138 type: "input_audio",
139 input_audio: {
140 data: "UklGRg==",
141 format: "wav"
142 }
143 }])
144 }).pipe(Effect.provide(makeTestLayer())))
145​
146 it.effect("maps audio media types to OpenRouter audio formats", () =>
147 Effect.gen(function*() {
148 const mediaTypes: ReadonlyArray<readonly [mediaType: string, format: string]> = [
149 ["audio/aac", "aac"],
150 ["audio/x-aiff", "aiff"],
151 ["audio/flac", "flac"],
152 ["audio/L16", "pcm16"],
153 ["audio/mp4", "m4a"],
154 ["audio/mp3", "mp3"],
155 ["audio/ogg", "ogg"],
156 ["audio/x-wav", "wav"]
157 ]
158​
159 yield* LanguageModel.generateText({
160 prompt: Prompt.make([{
161 role: "user",
162 content: mediaTypes.map(([mediaType]) =>
163 Prompt.filePart({
164 mediaType,
165 data: new Uint8Array([0x00])
166 })
167 )
168 }])
169 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
170​
171 const requests = yield* MockHttpClient.requests
172 const body = yield* getRequestBody(requests[0])
173​
174 const userMessage = body.messages.find((message: any) => message.role === "user")
175 deepStrictEqual(
176 userMessage.content.map((item: any) => item.input_audio.format),
177 mediaTypes.map(([, format]) => format)
178 )
179 }).pipe(Effect.provide(makeTestLayer())))
180​
181 it.effect("fails on unsupported audio media types", () =>
182 Effect.gen(function*() {
183 const error = yield* LanguageModel.generateText({
184 prompt: Prompt.make([{
185 role: "user",
186 content: [
187 Prompt.filePart({
188 mediaType: "audio/webm",
189 data: new Uint8Array([0x00])
190 })
191 ]
192 }])
193 }).pipe(
194 Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")),
195 Effect.flip
196 )
197​
198 strictEqual(error.reason._tag, "InvalidUserInputError")
199 assert.include(error.message, "audio/webm")
200 }).pipe(Effect.provide(makeTestLayer())))
201​
202 it.effect("fails on audio URLs", () =>
203 Effect.gen(function*() {
204 const error = yield* LanguageModel.generateText({
205 prompt: Prompt.make([{
206 role: "user",
207 content: [
208 Prompt.filePart({
209 mediaType: "audio/mpeg",
210 data: new URL("https://example.com/audio.mp3")
211 })
212 ]
213 }])
214 }).pipe(
215 Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")),
216 Effect.flip
217 )
218​
219 strictEqual(error.reason._tag, "InvalidUserInputError")
220 }).pipe(Effect.provide(makeTestLayer())))
221​
222 it.effect("converts non-audio files to file blocks", () =>
223 Effect.gen(function*() {
224 const pdfData = new Uint8Array([0x25, 0x50, 0x44, 0x46]) // %PDF
225​
226 yield* LanguageModel.generateText({
227 prompt: Prompt.make([{
228 role: "user",
229 content: [
230 Prompt.filePart({
231 mediaType: "application/pdf",
232 fileName: "document.pdf",
233 data: pdfData
234 })
235 ]
236 }])
237 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
238​
239 const requests = yield* MockHttpClient.requests
240 const body = yield* getRequestBody(requests[0])
241​
242 const userMessage = body.messages.find((message: any) => message.role === "user")
243 deepStrictEqual(userMessage.content, [{
244 type: "file",
245 file: {
246 filename: "document.pdf",
247 file_data: "data:application/pdf;base64,JVBERg=="
248 }
249 }])
250 }).pipe(Effect.provide(makeTestLayer())))
251 })
252​
253 it.effect("preserves string tool results", () =>
254 Effect.gen(function*() {
255 yield* LanguageModel.generateText({
256 prompt: Prompt.make([
257 { role: "user", content: "Use the tool" },
258 {
259 role: "assistant",
260 content: [Prompt.toolCallPart({
261 id: "call_text",
262 name: "text_tool",
263 params: {},
264 providerExecuted: false
265 })]
266 },
267 {
268 role: "tool",
269 content: [Prompt.toolResultPart({
270 id: "call_text",
271 name: "text_tool",
272 result: "PLAIN_TEXT_SENTINEL\n",
273 isFailure: false,
274 providerExecuted: false
275 })]
276 }
277 ]),
278 disableToolCallResolution: true
279 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
280​
281 const requests = yield* MockHttpClient.requests
282 const body = yield* getRequestBody(requests[0])
283 const toolResult = body.messages.find((message: any) => message.role === "tool")
284​
285 assert.isDefined(toolResult)
286 strictEqual(toolResult.content, "PLAIN_TEXT_SENTINEL\n")
287 }).pipe(Effect.provide(makeTestLayer())))
288​
289 const modelOutputCases = [
290 {
291 label: "as text with file placeholders",
292 modelOutput: [
293 Prompt.textPart({ text: "Screenshot" }),
294 Prompt.filePart({ mediaType: "image/png", fileName: "screenshot.png", data: "iVBORw0KGgo=" })
295 ],
296 content: "Screenshot\n[image/png screenshot.png omitted]"
297 },
298 { label: "as an empty string when it is empty", modelOutput: [], content: "" }
299 ]
300​
301 for (const { content, label, modelOutput } of modelOutputCases) {
302 it.effect(`sends tool result model output ${label}`, () =>
303 Effect.gen(function*() {
304 yield* LanguageModel.generateText({
305 prompt: Prompt.make([
306 { role: "user", content: "Use the tool" },
307 {
308 role: "assistant",
309 content: [Prompt.toolCallPart({
310 id: "call_image",
311 name: "image_tool",
312 params: {},
313 providerExecuted: false
314 })]
315 },
316 {
317 role: "tool",
318 content: [Prompt.toolResultPart({
319 id: "call_image",
320 name: "image_tool",
321 result: { output: "APPLICATION_ONLY" },
322 modelOutput,
323 isFailure: false,
324 providerExecuted: false
325 })]
326 }
327 ]),
328 disableToolCallResolution: true
329 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
330​
331 const requests = yield* MockHttpClient.requests
332 const body = yield* getRequestBody(requests[0])
333 const toolResult = body.messages.find((message: any) => message.role === "tool")
334​
335 assert.isDefined(toolResult)
336 strictEqual(toolResult.content, content)
337 }).pipe(Effect.provide(makeTestLayer())))
338 }
339​
340 describe("cache control", () => {
341 const ephemeral = { type: "ephemeral" } as const
342 const oneHour = { type: "ephemeral", ttl: "1h" } as const
343 const cached = { openrouter: { cacheControl: ephemeral } }
344 const partCached = { openrouter: { cacheControl: oneHour } }
345​
346 it.effect("sends cached void tool results without throwing", () =>
347 Effect.gen(function*() {
348 yield* LanguageModel.generateText({
349 prompt: Prompt.make([{
350 role: "tool",
351 content: [Prompt.toolResultPart({
352 id: "call_void",
353 name: "void_tool",
354 result: undefined,
355 isFailure: false,
356 providerExecuted: false
357 })],
358 options: cached
359 }]),
360 disableToolCallResolution: true
361 }).pipe(Effect.provide(OpenRouterLanguageModel.model("anthropic/claude-sonnet-4.5")))
362​
363 const requests = yield* MockHttpClient.requests
364 const body = yield* getRequestBody(requests[0])
365 const toolResult = body.messages.find((message: any) => message.role === "tool")
366 strictEqual(toolResult.tool_call_id, "call_void")
367 }).pipe(Effect.provide(makeTestLayer())))
368​
369 for (const partLevel of [false, true]) {
370 it.effect(
371 partLevel
372 ? "preserves part breakpoints and prefers them to message breakpoints"
373 : "puts message breakpoints on the last assistant text and tool result",
374 () =>
375 Effect.gen(function*() {
376 yield* LanguageModel.generateText({
377 prompt: Prompt.make([
378 { role: "user", content: "Look up both" },
379 {
380 role: "assistant",
381 content: [
382 Prompt.reasoningPart({ text: "Thinking" }),
383 Prompt.textPart({ text: "First", options: partLevel ? partCached : undefined }),
384 Prompt.textPart({ text: "Second" }),
385 Prompt.textPart({ text: "Last", options: partLevel ? partCached : undefined }),
386 ...["a", "b", "c"].map((id) =>
387 Prompt.toolCallPart({ id, name: "lookup", params: {}, providerExecuted: false })
388 )
389 ],
390 options: cached
391 },
392 {
393 role: "tool",
394 content: [
395 ...["a", "b", "c"].map((id) =>
396 Prompt.toolResultPart({
397 id,
398 name: "lookup",
399 result: id === "c" ? { value: 42 } : id,
400 isFailure: false,
401 providerExecuted: false,
402 options: partLevel && id !== "b" ? partCached : undefined
403 })
404 ),
405 Prompt.toolApprovalResponsePart({ approvalId: "approval", approved: true })
406 ],
407 options: cached
408 }
409 ]),
410 disableToolCallResolution: true
411 }).pipe(Effect.provide(OpenRouterLanguageModel.model("anthropic/claude-sonnet-4.5")))
412​
413 const requests = yield* MockHttpClient.requests
414 const body = yield* getRequestBody(requests[0])
415 const assistant = body.messages.find((message: any) => message.role === "assistant")
416 deepStrictEqual(assistant.content, [
417 partLevel
418 ? { type: "text", text: "First", cache_control: oneHour }
419 : { type: "text", text: "First" },
420 { type: "text", text: "Second" },
421 { type: "text", text: "Last", cache_control: partLevel ? oneHour : ephemeral }
422 ])
423 strictEqual(assistant.reasoning, "Thinking")
424 deepStrictEqual(assistant.tool_calls, [
425 { id: "a", type: "function", function: { name: "lookup", arguments: "{}" } },
426 { id: "b", type: "function", function: { name: "lookup", arguments: "{}" } },
427 { id: "c", type: "function", function: { name: "lookup", arguments: "{}" } }
428 ])
429 deepStrictEqual(body.messages.filter((message: any) => message.role === "tool"), [
430 {
431 role: "tool",
432 tool_call_id: "a",
433 content: partLevel ? [{ type: "text", text: "a", cache_control: oneHour }] : "a"
434 },
435 { role: "tool", tool_call_id: "b", content: "b" },
436 {
437 role: "tool",
438 tool_call_id: "c",
439 content: [{ type: "text", text: "{\"value\":42}", cache_control: partLevel ? oneHour : ephemeral }]
440 }
441 ])
442 }).pipe(Effect.provide(makeTestLayer()))
443 )
444 }
445 })
446 })
447​
448 describe("tool preparation", () => {
449 it.effect("passes raw JSON schema for dynamic tools", () =>
450 Effect.gen(function*() {
451 const inputSchema = {
452 properties: {
453 query: { type: "string" }
454 }
455 } as const
456​
457 const DynamicTool = Tool.dynamic("DynamicTool", {
458 description: "A dynamic tool",
459 parameters: inputSchema
460 })
461​
462 yield* LanguageModel.generateText({
463 prompt: "Use the dynamic tool",
464 toolkit: Toolkit.make(DynamicTool),
465 disableToolCallResolution: true
466 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
467​
468 const requests = yield* MockHttpClient.requests
469 const body = yield* getRequestBody(requests[0])
470​
471 const tool = body.tools?.find((entry: any) =>
472 entry.type === "function" && entry.function.name === "DynamicTool"
473 )
474 assert.isDefined(tool)
475 strictEqual(tool.function.description, "A dynamic tool")
476 deepStrictEqual(tool.function.parameters, inputSchema)
477 }).pipe(Effect.provide(makeTestLayer())))
478​
479 it.effect("passes object union parameters through", () =>
480 Effect.gen(function*() {
481 const UnionTool = Tool.make("UnionTool", {
482 parameters: Schema.Union([
483 Schema.Struct({ query: Schema.String }).annotate({ identifier: "Query" }),
484 Schema.Struct({ id: Schema.String })
485 ])
486 })
487​
488 yield* LanguageModel.generateText({
489 prompt: "Use the tool",
490 toolkit: Toolkit.make(UnionTool),
491 disableToolCallResolution: true
492 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")))
493​
494 const requests = yield* MockHttpClient.requests
495 const body = yield* getRequestBody(requests[0])
496​
497 deepStrictEqual(body.tools[0].function.parameters, {
498 anyOf: [
499 { $ref: "#/$defs/Query" },
500 { type: "object", properties: { id: { type: "string" } }, required: ["id"], additionalProperties: true }
501 ],
502 $defs: {
503 Query: {
504 type: "object",
505 properties: { query: { type: "string" } },
506 required: ["query"],
507 additionalProperties: true
508 }
509 }
510 })
511 }).pipe(Effect.provide(makeTestLayer())))
512​
513 it.effect("names the tool when its parameters are not an object schema", () =>
514 Effect.gen(function*() {
515 const error = yield* LanguageModel.generateText({
516 prompt: "Use the tool",
517 toolkit: Toolkit.make(Tool.make("NoParams", { parameters: Schema.Struct({}) })),
518 disableToolCallResolution: true
519 }).pipe(Effect.provide(OpenRouterLanguageModel.model("google/gemini-2.5-flash")), Effect.flip)
520​
521 strictEqual(error.reason._tag, "UnsupportedSchemaError")
522 assert.include(error.message, `"NoParams"`)
523 assert.include(error.message, "Tool.EmptyParams")
524 }).pipe(Effect.provide(makeTestLayer())))
525 })
526​
527 describe("usage", () => {
528 it.effect("derives text and uncached tokens when details are subsets of their totals", () =>
529 Effect.gen(function*() {
530 const result = yield* LanguageModel.generateText({ prompt: "Hello" }).pipe(
531 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini"))
532 )
533​
534 deepStrictEqual(
535 result.usage.inputTokens,
536 { uncached: 70, total: 100, cacheRead: 30, cacheWrite: 0 },
537 "subset input usage"
538 )
539 deepStrictEqual(result.usage.outputTokens, { total: 50, text: 30, reasoning: 20 }, "subset output usage")
540 }).pipe(Effect.provide(makeTestLayer({
541 body: {
542 usage: {
543 prompt_tokens: 100,
544 prompt_tokens_details: { cached_tokens: 30 },
545 completion_tokens: 50,
546 completion_tokens_details: { reasoning_tokens: 20 },
547 total_tokens: 150
548 }
549 }
550 }))))
551​
552 it.effect("preserves totals when detail counts equal their parent counts", () =>
553 Effect.gen(function*() {
554 const result = yield* LanguageModel.generateText({ prompt: "Hello" }).pipe(
555 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini"))
556 )
557​
558 deepStrictEqual(
559 result.usage.inputTokens,
560 { uncached: 0, total: 100, cacheRead: 100, cacheWrite: 0 },
561 "input usage at equality"
562 )
563 deepStrictEqual(result.usage.outputTokens, { total: 20, text: 0, reasoning: 20 }, "output usage at equality")
564 }).pipe(Effect.provide(makeTestLayer({
565 body: {
566 usage: {
567 prompt_tokens: 100,
568 prompt_tokens_details: { cached_tokens: 100 },
569 completion_tokens: 20,
570 completion_tokens_details: { reasoning_tokens: 20 },
571 total_tokens: 120
572 }
573 }
574 }))))
575​
576 it.effect("treats reasoning tokens as disjoint when they exceed completion tokens", () =>
577 Effect.gen(function*() {
578 const result = yield* LanguageModel.generateText({ prompt: "Hello" }).pipe(
579 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini"))
580 )
581​
582 deepStrictEqual(result.usage.outputTokens, { total: 30, text: 10, reasoning: 20 }, "disjoint reasoning usage")
583 }).pipe(Effect.provide(makeTestLayer({
584 body: {
585 usage: {
586 prompt_tokens: 100,
587 completion_tokens: 10,
588 completion_tokens_details: { reasoning_tokens: 20 },
589 total_tokens: 110
590 }
591 }
592 }))))
593​
594 it.effect("treats cached tokens as disjoint when they exceed prompt tokens", () =>
595 Effect.gen(function*() {
596 const result = yield* LanguageModel.generateText({ prompt: "Hello" }).pipe(
597 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini"))
598 )
599​
600 deepStrictEqual(
601 result.usage.inputTokens,
602 { uncached: 100, total: 400, cacheRead: 300, cacheWrite: 0 },
603 "disjoint cached usage"
604 )
605 }).pipe(Effect.provide(makeTestLayer({
606 body: {
607 usage: {
608 prompt_tokens: 100,
609 prompt_tokens_details: { cached_tokens: 300 },
610 completion_tokens: 10,
611 total_tokens: 110
612 }
613 }
614 }))))
615 })
616 })
617​
618 describe("streamText", () => {
619 describe("usage", () => {
620 it.effect("treats streamed reasoning tokens as disjoint when they exceed completion tokens", () =>
621 Effect.gen(function*() {
622 const parts = yield* LanguageModel.streamText({ prompt: "Hello" }).pipe(
623 Stream.runCollect,
624 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini")),
625 Effect.provide(makeStreamTestLayer([{
626 id: "response-1",
627 object: "chat.completion.chunk",
628 model: "openai/gpt-4o-mini",
629 created: 1,
630 choices: [],
631 usage: {
632 prompt_tokens: 100,
633 completion_tokens: 10,
634 completion_tokens_details: { reasoning_tokens: 20 },
635 total_tokens: 110
636 }
637 }]))
638 )
639​
640 const finishPart = parts.find((part) => part.type === "finish")
641 deepStrictEqual(
642 finishPart?.usage.outputTokens,
643 { total: 30, text: 10, reasoning: 20 },
644 "streamed disjoint reasoning usage"
645 )
646 }))
647 })
648​
649 it.effect("preserves streamed citation start and end indexes", () =>
650 Effect.gen(function*() {
651 const parts = yield* LanguageModel.streamText({ prompt: "cite a source" }).pipe(
652 Stream.runCollect,
653 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini")),
654 Effect.provide(makeStreamTestLayer([{
655 id: "response-1",
656 object: "chat.completion.chunk",
657 model: "openai/gpt-4o-mini",
658 created: 1,
659 choices: [{
660 index: 0,
661 delta: {
662 annotations: [{
663 type: "url_citation",
664 url_citation: {
665 url: "https://example.com/source",
666 title: "source",
667 start_index: 2,
668 end_index: 9
669 }
670 }]
671 }
672 }]
673 }]))
674 )
675​
676 const source = globalThis.Array.from(parts).find((part) => part.type === "source")
677 assert.isDefined(source)
678 if (source?.type === "source") {
679 assert.deepStrictEqual(source.metadata, {
680 openrouter: { startIndex: 2, endIndex: 9 }
681 })
682 }
683 }))
684​
685 it.effect("uses lowercase openrouter reasoning-end metadata", () =>
686 Effect.gen(function*() {
687 const reasoningDetails = [{
688 type: "reasoning.text",
689 text: "thinking",
690 signature: "signature-final",
691 format: "unknown"
692 }] as const
693 const parts = yield* LanguageModel.streamText({ prompt: "reason then answer" }).pipe(
694 Stream.runCollect,
695 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini")),
696 Effect.provide(makeStreamTestLayer([
697 {
698 id: "response-1",
699 object: "chat.completion.chunk",
700 model: "openai/gpt-4o-mini",
701 created: 1,
702 choices: [{ index: 0, delta: { reasoning_details: reasoningDetails } }]
703 },
704 {
705 id: "response-1",
706 object: "chat.completion.chunk",
707 model: "openai/gpt-4o-mini",
708 created: 1,
709 choices: [{ index: 0, finish_reason: "stop", delta: { content: "answer" } }]
710 }
711 ]))
712 )
713​
714 const reasoningEnd = parts.find((part) => part.type === "reasoning-end")
715 deepStrictEqual(reasoningEnd?.metadata, { openrouter: { reasoningDetails } })
716 }))
717​
718 it.effect("emits incremental tool parameter fragments", () =>
719 Effect.gen(function*() {
720 const ProbeTool = Tool.make("ProbeTool", {
721 parameters: Schema.Struct({ a: Schema.Number }),
722 success: Schema.String
723 })
724 const toolkit = Toolkit.make(ProbeTool)
725 const parts = yield* LanguageModel.streamText({
726 prompt: "call the tool",
727 toolkit,
728 disableToolCallResolution: true
729 }).pipe(
730 Stream.runCollect,
731 Effect.provide(OpenRouterLanguageModel.model("openai/gpt-4o-mini")),
732 Effect.provide(toolkit.toLayer({ ProbeTool: () => Effect.succeed("ok") })),
733 Effect.provide(makeStreamTestLayer([
734 {
735 id: "response-1",
736 object: "chat.completion.chunk",
737 model: "openai/gpt-4o-mini",
738 created: 1,
739 choices: [{
740 index: 0,
741 delta: {
742 tool_calls: [{
743 index: 0,
744 id: "call-1",
745 type: "function",
746 function: { name: "ProbeTool", arguments: "{\"a\":" }
747 }]
748 }
749 }]
750 },
751 {
752 id: "response-1",
753 object: "chat.completion.chunk",
754 model: "openai/gpt-4o-mini",
755 created: 1,
756 choices: [{
757 index: 0,
758 delta: { tool_calls: [{ index: 0 }] }
759 }]
760 },
761 {
762 id: "response-1",
763 object: "chat.completion.chunk",
764 model: "openai/gpt-4o-mini",
765 created: 1,
766 choices: [{
767 index: 0,
768 finish_reason: "tool_calls",
769 delta: { tool_calls: [{ index: 0, function: { arguments: "1}" } }] }
770 }]
771 }
772 ]))
773 )
774​
775 deepStrictEqual(
776 globalThis.Array.from(parts)
777 .filter((part) => part.type === "tool-params-delta")
778 .map((part) => part.delta),
779 ["{\"a\":", "1}"]
780 )
781 }))
782 })
783})
784​
785// =============================================================================
786// Test Infrastructure
787// =============================================================================
788​
789class MockOpenRouterResponse extends Context.Service<MockOpenRouterResponse, {
790 readonly status: number
791 readonly body: typeof Generated.SendChatCompletionRequest200.Type
792 readonly headers?: Record<string, string> | undefined
793}>()("MockOpenRouterResponse") {}
794​
795class MockHttpClient extends Context.Service<MockHttpClient, {
796 readonly requests: Effect.Effect<ReadonlyArray<HttpClientRequest.HttpClientRequest>>
797}>()("MockHttpClient") {
798 static requests = Effect.service(MockHttpClient).pipe(
799 Effect.flatMap((client) => client.requests)
800 )
802​
803const encodeResponse = Schema.encodeEffect(Generated.SendChatCompletionRequest200)
804​
805const makeHttpClient = Effect.gen(function*() {
806 const capturedRequests = yield* Ref.make<ReadonlyArray<HttpClientRequest.HttpClientRequest>>([])
807 const response = yield* MockOpenRouterResponse
808 const body = yield* Effect.orDie(encodeResponse(response.body))
809​
810 const httpClient = HttpClient.makeWith(
811 Effect.fnUntraced(function*(requestEffect) {
812 const request = yield* requestEffect
813 yield* Ref.update(capturedRequests, Array.append(request))
814 return HttpClientResponse.fromWeb(
815 request,
816 new Response(JSON.stringify(body), {
817 headers: response.headers ?? {},
818 status: response.status
819 })
820 )
821 }),
822 Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never>
823 )
824​
825 return Context.make(HttpClient.HttpClient, httpClient).pipe(
826 Context.add(MockHttpClient, MockHttpClient.of({ requests: Ref.get(capturedRequests) }))
827 )
828})
829​
830const HttpClientLayer = Layer.effectContext(makeHttpClient)
831​
832const makeDefaultResponse = (
833 overrides: Partial<typeof Generated.SendChatCompletionRequest200.Type> = {}
834): typeof Generated.SendChatCompletionRequest200.Type => ({
835 id: "gen-test123",
836 choices: [{
837 finish_reason: "stop",
838 index: 0,
839 message: {
840 role: "assistant",
841 content: "Hello!"
842 }
843 }],
844 created: 1234567890,
845 model: "google/gemini-2.5-flash",
846 object: "chat.completion",
847 system_fingerprint: null,
848 ...overrides
849})
850​
851const makeTestLayer = (options: {
852 readonly body?: Partial<typeof Generated.SendChatCompletionRequest200.Type>
853 readonly status?: number
854 readonly headers?: Record<string, string>
855} = {}) =>
856 OpenRouterClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe(
857 Layer.provideMerge(HttpClientLayer),
858 Layer.provide(Layer.succeed(MockOpenRouterResponse, {
859 body: makeDefaultResponse(options.body),
860 status: options.status ?? 200,
861 headers: options.headers ?? {}
862 }))
863 )
864​
865const getRequestBody = Effect.fnUntraced(function*(request: HttpClientRequest.HttpClientRequest) {
866 const body = request.body
867 if (body._tag === "Uint8Array") {
868 const text = new TextDecoder().decode(body.body)
869 return JSON.parse(text)
870 }
871 return yield* Effect.die(new Error("Expected Uint8Array body"))
872})
873​
874const makeStreamTestLayer = (events: ReadonlyArray<typeof Generated.ChatStreamChunk.Encoded>) => {
875 const body = events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join("") + "data: [DONE]\n\n"
876 const httpClientLayer = Layer.effectContext(Effect.gen(function*() {
877 const capturedRequests = yield* Ref.make<ReadonlyArray<HttpClientRequest.HttpClientRequest>>([])
878 const httpClient = HttpClient.makeWith(
879 Effect.fnUntraced(function*(requestEffect) {
880 const request = yield* requestEffect
881 yield* Ref.update(capturedRequests, Array.append(request))
882 return HttpClientResponse.fromWeb(
883 request,
884 new Response(body, {
885 status: 200,
886 headers: { "content-type": "text/event-stream" }
887 })
888 )
889 }),
890 Effect.succeed as HttpClient.HttpClient.Preprocess<HttpClientError.HttpClientError, never>
891 )
892 return Context.make(HttpClient.HttpClient, httpClient).pipe(
893 Context.add(MockHttpClient, MockHttpClient.of({ requests: Ref.get(capturedRequests) }))
894 )
895 }))
896 return OpenRouterClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe(
897 Layer.provideMerge(httpClientLayer)
898 )