@codilore/llm 1.15.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +321 -0
- package/README.md +131 -0
- package/example/call-sites.md +591 -0
- package/example/tutorial.ts +255 -0
- package/package.json +50 -0
- package/script/recording-cost-report.ts +250 -0
- package/script/setup-recording-env.ts +542 -0
- package/src/cache-policy.ts +111 -0
- package/src/index.ts +32 -0
- package/src/llm.ts +186 -0
- package/src/protocols/anthropic-messages.ts +841 -0
- package/src/protocols/bedrock-converse.ts +649 -0
- package/src/protocols/bedrock-event-stream.ts +87 -0
- package/src/protocols/gemini.ts +465 -0
- package/src/protocols/index.ts +6 -0
- package/src/protocols/openai-chat.ts +431 -0
- package/src/protocols/openai-compatible-chat.ts +24 -0
- package/src/protocols/openai-responses.ts +987 -0
- package/src/protocols/shared.ts +283 -0
- package/src/protocols/utils/bedrock-auth.ts +70 -0
- package/src/protocols/utils/bedrock-cache.ts +37 -0
- package/src/protocols/utils/bedrock-media.ts +80 -0
- package/src/protocols/utils/cache.ts +16 -0
- package/src/protocols/utils/gemini-tool-schema.ts +101 -0
- package/src/protocols/utils/lifecycle.ts +102 -0
- package/src/protocols/utils/openai-options.ts +84 -0
- package/src/protocols/utils/tool-stream.ts +218 -0
- package/src/provider.ts +37 -0
- package/src/providers/amazon-bedrock.ts +43 -0
- package/src/providers/anthropic.ts +35 -0
- package/src/providers/azure.ts +110 -0
- package/src/providers/cloudflare.ts +127 -0
- package/src/providers/github-copilot.ts +66 -0
- package/src/providers/google.ts +35 -0
- package/src/providers/index.ts +11 -0
- package/src/providers/openai-compatible-profile.ts +20 -0
- package/src/providers/openai-compatible.ts +65 -0
- package/src/providers/openai-options.ts +81 -0
- package/src/providers/openai.ts +63 -0
- package/src/providers/openrouter.ts +98 -0
- package/src/providers/xai.ts +56 -0
- package/src/route/auth-options.ts +57 -0
- package/src/route/auth.ts +156 -0
- package/src/route/client.ts +434 -0
- package/src/route/endpoint.ts +53 -0
- package/src/route/executor.ts +374 -0
- package/src/route/framing.ts +27 -0
- package/src/route/index.ts +25 -0
- package/src/route/protocol.ts +84 -0
- package/src/route/transport/http.ts +108 -0
- package/src/route/transport/index.ts +33 -0
- package/src/route/transport/websocket.ts +280 -0
- package/src/schema/errors.ts +203 -0
- package/src/schema/events.ts +370 -0
- package/src/schema/ids.ts +43 -0
- package/src/schema/index.ts +5 -0
- package/src/schema/messages.ts +404 -0
- package/src/schema/options.ts +221 -0
- package/src/tool-runtime.ts +78 -0
- package/src/tool.ts +241 -0
- package/src/utils/record.ts +3 -0
- package/sst-env.d.ts +10 -0
- package/test/adapter.test.ts +164 -0
- package/test/auth-options.types.ts +168 -0
- package/test/auth.test.ts +103 -0
- package/test/cache-policy.test.ts +262 -0
- package/test/continuation-scenarios.ts +104 -0
- package/test/endpoint.test.ts +58 -0
- package/test/executor.test.ts +418 -0
- package/test/exports.test.ts +62 -0
- package/test/fixtures/media/restroom.png +0 -0
- package/test/fixtures/recordings/anthropic-messages/accepts-malformed-assistant-tool-order-with-default-patch.json +29 -0
- package/test/fixtures/recordings/anthropic-messages/anthropic-opus-4-7-image-tool-result.json +43 -0
- package/test/fixtures/recordings/anthropic-messages/claude-opus-4-7-drives-a-tool-loop.json +56 -0
- package/test/fixtures/recordings/anthropic-messages/rejects-malformed-assistant-tool-order-without-patch.json +29 -0
- package/test/fixtures/recordings/anthropic-messages/streams-text.json +29 -0
- package/test/fixtures/recordings/anthropic-messages/streams-tool-call.json +29 -0
- package/test/fixtures/recordings/anthropic-messages-cache/writes-then-reads-cache-control-on-identical-second-call.json +48 -0
- package/test/fixtures/recordings/bedrock-converse/drives-a-tool-loop.json +55 -0
- package/test/fixtures/recordings/bedrock-converse/streams-a-tool-call.json +29 -0
- package/test/fixtures/recordings/bedrock-converse/streams-text.json +29 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/gemini/gemini-2-5-flash-image.json +32 -0
- package/test/fixtures/recordings/gemini/streams-text.json +28 -0
- package/test/fixtures/recordings/gemini/streams-tool-call.json +28 -0
- package/test/fixtures/recordings/gemini-cache/reports-cachedcontenttokencount-on-identical-second-call.json +46 -0
- package/test/fixtures/recordings/openai-chat/continues-after-tool-result.json +28 -0
- package/test/fixtures/recordings/openai-chat/drives-a-tool-loop-end-to-end.json +46 -0
- package/test/fixtures/recordings/openai-chat/streams-text.json +28 -0
- package/test/fixtures/recordings/openai-chat/streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/deepseek-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/groq-llama-3-3-70b-drives-a-tool-loop.json +53 -0
- package/test/fixtures/recordings/openai-compatible-chat/groq-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/groq-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/openrouter-claude-opus-4-7-drives-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-4o-mini-drives-a-tool-loop.json +53 -0
- package/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-5-5-drives-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-responses/gpt-5-5-drives-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/openai-responses/gpt-5-5-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-responses/gpt-5-5-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-image-tool-result.json +42 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning-continuation.json +58 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning.json +32 -0
- package/test/fixtures/recordings/openai-responses-cache/reports-cached-tokens-on-identical-second-call.json +46 -0
- package/test/generate-object.test.ts +184 -0
- package/test/lib/effect.ts +50 -0
- package/test/lib/http.ts +98 -0
- package/test/lib/openai-chunks.ts +27 -0
- package/test/lib/sse.ts +17 -0
- package/test/lib/tool-runtime.ts +146 -0
- package/test/llm.test.ts +167 -0
- package/test/provider/anthropic-messages-cache.recorded.test.ts +54 -0
- package/test/provider/anthropic-messages.recorded.test.ts +46 -0
- package/test/provider/anthropic-messages.test.ts +829 -0
- package/test/provider/bedrock-converse-cache.recorded.test.ts +54 -0
- package/test/provider/bedrock-converse.test.ts +707 -0
- package/test/provider/cloudflare.test.ts +230 -0
- package/test/provider/gemini-cache.recorded.test.ts +48 -0
- package/test/provider/gemini.test.ts +476 -0
- package/test/provider/golden.recorded.test.ts +219 -0
- package/test/provider/openai-chat.test.ts +446 -0
- package/test/provider/openai-compatible-chat.test.ts +238 -0
- package/test/provider/openai-responses-cache.recorded.test.ts +46 -0
- package/test/provider/openai-responses.test.ts +1322 -0
- package/test/provider/openrouter.test.ts +56 -0
- package/test/provider.types.ts +41 -0
- package/test/recorded-golden.ts +97 -0
- package/test/recorded-runner.ts +100 -0
- package/test/recorded-scenarios.ts +531 -0
- package/test/recorded-test.ts +74 -0
- package/test/recorded-utils.ts +56 -0
- package/test/recorded-websocket.ts +26 -0
- package/test/route.test.ts +43 -0
- package/test/schema.test.ts +97 -0
- package/test/tool-runtime.test.ts +802 -0
- package/test/tool-stream.test.ts +99 -0
- package/test/tool.types.ts +40 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { describe, expect } from "bun:test"
|
|
2
|
+
import { Effect, Schema, Stream } from "effect"
|
|
3
|
+
import { HttpClientRequest } from "effect/unstable/http"
|
|
4
|
+
import { LLM, LLMError, Message, Model, ToolCallPart, Usage } from "../../src"
|
|
5
|
+
import * as Azure from "../../src/providers/azure"
|
|
6
|
+
import * as OpenAI from "../../src/providers/openai"
|
|
7
|
+
import * as OpenAIChat from "../../src/protocols/openai-chat"
|
|
8
|
+
import { Auth, LLMClient } from "../../src/route"
|
|
9
|
+
import { it } from "../lib/effect"
|
|
10
|
+
import { dynamicResponse, fixedResponse, truncatedStream } from "../lib/http"
|
|
11
|
+
import { deltaChunk, usageChunk } from "../lib/openai-chunks"
|
|
12
|
+
import { sseEvents } from "../lib/sse"
|
|
13
|
+
|
|
14
|
+
const TargetJson = Schema.fromJsonString(Schema.Unknown)
|
|
15
|
+
const encodeJson = Schema.encodeSync(TargetJson)
|
|
16
|
+
const decodeJson = Schema.decodeUnknownSync(TargetJson)
|
|
17
|
+
|
|
18
|
+
const model = OpenAIChat.route
|
|
19
|
+
.with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
|
|
20
|
+
.model({ id: "gpt-4o-mini" })
|
|
21
|
+
|
|
22
|
+
const request = LLM.request({
|
|
23
|
+
id: "req_1",
|
|
24
|
+
model,
|
|
25
|
+
system: "You are concise.",
|
|
26
|
+
prompt: "Say hello.",
|
|
27
|
+
generation: { maxTokens: 20, temperature: 0 },
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
describe("OpenAI Chat route", () => {
|
|
31
|
+
it.effect("prepares OpenAI Chat payload", () =>
|
|
32
|
+
Effect.gen(function* () {
|
|
33
|
+
// Pass the OpenAIChat payload type so `prepared.body` is statically
|
|
34
|
+
// typed to the route's native shape — the assertions below read field
|
|
35
|
+
// names without `unknown` casts.
|
|
36
|
+
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(request)
|
|
37
|
+
const _typed: { readonly model: string; readonly stream: true } = prepared.body
|
|
38
|
+
|
|
39
|
+
expect(prepared.body).toEqual({
|
|
40
|
+
model: "gpt-4o-mini",
|
|
41
|
+
messages: [
|
|
42
|
+
{ role: "system", content: "You are concise." },
|
|
43
|
+
{ role: "user", content: "Say hello." },
|
|
44
|
+
],
|
|
45
|
+
stream: true,
|
|
46
|
+
stream_options: { include_usage: true },
|
|
47
|
+
max_tokens: 20,
|
|
48
|
+
temperature: 0,
|
|
49
|
+
})
|
|
50
|
+
}),
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
it.effect("lowers chronological system updates to escaped user wrappers in order", () =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
|
|
56
|
+
LLM.request({
|
|
57
|
+
model,
|
|
58
|
+
messages: [
|
|
59
|
+
Message.user("Before."),
|
|
60
|
+
Message.system("Treat <admin> & data literally."),
|
|
61
|
+
Message.assistant("After."),
|
|
62
|
+
],
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
expect(prepared.body.messages).toEqual([
|
|
67
|
+
{
|
|
68
|
+
role: "user",
|
|
69
|
+
content: "Before.\n<system-update>\nTreat <admin> & data literally.\n</system-update>",
|
|
70
|
+
},
|
|
71
|
+
{ role: "assistant", content: "After." },
|
|
72
|
+
])
|
|
73
|
+
}),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
it.effect("replays canonical reasoning as OpenAI-compatible reasoning_content", () =>
|
|
77
|
+
Effect.gen(function* () {
|
|
78
|
+
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
|
|
79
|
+
LLM.request({
|
|
80
|
+
model,
|
|
81
|
+
messages: [
|
|
82
|
+
Message.assistant([
|
|
83
|
+
{ type: "reasoning", text: "thinking" },
|
|
84
|
+
{ type: "text", text: "Hello" },
|
|
85
|
+
]),
|
|
86
|
+
],
|
|
87
|
+
}),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
expect(prepared.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }])
|
|
91
|
+
}),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
it.effect("maps OpenAI provider options to Chat options", () =>
|
|
95
|
+
Effect.gen(function* () {
|
|
96
|
+
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
|
|
97
|
+
LLM.request({
|
|
98
|
+
model: OpenAI.configure({ baseURL: "https://api.openai.test/v1/", apiKey: "test" }).chat("gpt-4o-mini"),
|
|
99
|
+
prompt: "think",
|
|
100
|
+
providerOptions: { openai: { reasoningEffort: "low" } },
|
|
101
|
+
}),
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
expect(prepared.body.store).toBe(false)
|
|
105
|
+
expect(prepared.body.reasoning_effort).toBe("low")
|
|
106
|
+
}),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
it.effect("adds native query params to the Chat Completions URL", () =>
|
|
110
|
+
LLMClient.generate(
|
|
111
|
+
LLM.updateRequest(request, {
|
|
112
|
+
model: Model.update(model, { route: model.route.with({ endpoint: { query: { "api-version": "v1" } } }) }),
|
|
113
|
+
}),
|
|
114
|
+
).pipe(
|
|
115
|
+
Effect.provide(
|
|
116
|
+
dynamicResponse((input) =>
|
|
117
|
+
Effect.gen(function* () {
|
|
118
|
+
const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
|
|
119
|
+
expect(web.url).toBe("https://api.openai.test/v1/chat/completions?api-version=v1")
|
|
120
|
+
return input.respond(sseEvents(deltaChunk({}, "stop")), {
|
|
121
|
+
headers: { "content-type": "text/event-stream" },
|
|
122
|
+
})
|
|
123
|
+
}),
|
|
124
|
+
),
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
it.effect("uses Azure api-key header for static OpenAI Chat keys", () =>
|
|
130
|
+
LLMClient.generate(
|
|
131
|
+
LLM.updateRequest(request, {
|
|
132
|
+
model: Azure.configure({
|
|
133
|
+
baseURL: "https://Codilore-test.openai.azure.com/openai/v1/",
|
|
134
|
+
apiKey: "azure-key",
|
|
135
|
+
headers: { authorization: "Bearer stale" },
|
|
136
|
+
}).chat("gpt-4o-mini"),
|
|
137
|
+
}),
|
|
138
|
+
).pipe(
|
|
139
|
+
Effect.provide(
|
|
140
|
+
dynamicResponse((input) =>
|
|
141
|
+
Effect.gen(function* () {
|
|
142
|
+
const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
|
|
143
|
+
expect(web.url).toBe("https://Codilore-test.openai.azure.com/openai/v1/chat/completions?api-version=v1")
|
|
144
|
+
expect(web.headers.get("api-key")).toBe("azure-key")
|
|
145
|
+
expect(web.headers.get("authorization")).toBeNull()
|
|
146
|
+
return input.respond(sseEvents(deltaChunk({}, "stop")), {
|
|
147
|
+
headers: { "content-type": "text/event-stream" },
|
|
148
|
+
})
|
|
149
|
+
}),
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
it.effect("applies serializable HTTP overlays after payload lowering", () =>
|
|
156
|
+
LLMClient.generate(
|
|
157
|
+
LLM.updateRequest(request, {
|
|
158
|
+
model: model.route
|
|
159
|
+
.with({ auth: Auth.bearer("fresh-key"), headers: { authorization: "Bearer stale" } })
|
|
160
|
+
.model({ id: model.id }),
|
|
161
|
+
http: {
|
|
162
|
+
body: { metadata: { source: "test" } },
|
|
163
|
+
headers: { authorization: "Bearer request", "x-custom": "yes" },
|
|
164
|
+
query: { debug: "1" },
|
|
165
|
+
},
|
|
166
|
+
}),
|
|
167
|
+
).pipe(
|
|
168
|
+
Effect.provide(
|
|
169
|
+
dynamicResponse((input) =>
|
|
170
|
+
Effect.gen(function* () {
|
|
171
|
+
const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
|
|
172
|
+
expect(web.url).toBe("https://api.openai.test/v1/chat/completions?debug=1")
|
|
173
|
+
expect(web.headers.get("authorization")).toBe("Bearer fresh-key")
|
|
174
|
+
expect(web.headers.get("x-custom")).toBe("yes")
|
|
175
|
+
expect(decodeJson(input.text)).toMatchObject({
|
|
176
|
+
stream: true,
|
|
177
|
+
stream_options: { include_usage: true },
|
|
178
|
+
metadata: { source: "test" },
|
|
179
|
+
})
|
|
180
|
+
return input.respond(sseEvents(deltaChunk({}, "stop")), {
|
|
181
|
+
headers: { "content-type": "text/event-stream" },
|
|
182
|
+
})
|
|
183
|
+
}),
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
),
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
it.effect("prepares assistant tool-call and tool-result messages", () =>
|
|
190
|
+
Effect.gen(function* () {
|
|
191
|
+
const prepared = yield* LLMClient.prepare(
|
|
192
|
+
LLM.request({
|
|
193
|
+
id: "req_tool_result",
|
|
194
|
+
model,
|
|
195
|
+
messages: [
|
|
196
|
+
Message.user("What is the weather?"),
|
|
197
|
+
Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
|
|
198
|
+
Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
|
|
199
|
+
],
|
|
200
|
+
}),
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
expect(prepared.body).toEqual({
|
|
204
|
+
model: "gpt-4o-mini",
|
|
205
|
+
messages: [
|
|
206
|
+
{ role: "user", content: "What is the weather?" },
|
|
207
|
+
{
|
|
208
|
+
role: "assistant",
|
|
209
|
+
content: null,
|
|
210
|
+
tool_calls: [
|
|
211
|
+
{
|
|
212
|
+
id: "call_1",
|
|
213
|
+
type: "function",
|
|
214
|
+
function: { name: "lookup", arguments: encodeJson({ query: "weather" }) },
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
{ role: "tool", tool_call_id: "call_1", content: encodeJson({ forecast: "sunny" }) },
|
|
219
|
+
],
|
|
220
|
+
stream: true,
|
|
221
|
+
stream_options: { include_usage: true },
|
|
222
|
+
})
|
|
223
|
+
}),
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
it.effect("rejects unsupported user media content", () =>
|
|
227
|
+
Effect.gen(function* () {
|
|
228
|
+
const error = yield* LLMClient.prepare(
|
|
229
|
+
LLM.request({
|
|
230
|
+
id: "req_media",
|
|
231
|
+
model,
|
|
232
|
+
messages: [Message.user({ type: "media", mediaType: "image/png", data: "AAECAw==" })],
|
|
233
|
+
}),
|
|
234
|
+
).pipe(Effect.flip)
|
|
235
|
+
|
|
236
|
+
expect(error.message).toContain("OpenAI Chat user messages only support text content for now")
|
|
237
|
+
}),
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
it.effect("lowers reasoning-only assistant history", () =>
|
|
241
|
+
Effect.gen(function* () {
|
|
242
|
+
const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
|
|
243
|
+
LLM.request({
|
|
244
|
+
id: "req_reasoning",
|
|
245
|
+
model,
|
|
246
|
+
messages: [Message.assistant({ type: "reasoning", text: "hidden" })],
|
|
247
|
+
}),
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
expect(prepared.body.messages).toEqual([{ role: "assistant", content: null, reasoning_content: "hidden" }])
|
|
251
|
+
}),
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
it.effect("parses text and usage stream fixtures", () =>
|
|
255
|
+
Effect.gen(function* () {
|
|
256
|
+
const body = sseEvents(
|
|
257
|
+
deltaChunk({ role: "assistant", content: "Hello" }),
|
|
258
|
+
deltaChunk({ content: "!" }),
|
|
259
|
+
deltaChunk({}, "stop"),
|
|
260
|
+
usageChunk({
|
|
261
|
+
prompt_tokens: 5,
|
|
262
|
+
completion_tokens: 2,
|
|
263
|
+
total_tokens: 7,
|
|
264
|
+
prompt_tokens_details: { cached_tokens: 1 },
|
|
265
|
+
completion_tokens_details: { reasoning_tokens: 0 },
|
|
266
|
+
}),
|
|
267
|
+
)
|
|
268
|
+
const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
|
|
269
|
+
const usage = new Usage({
|
|
270
|
+
inputTokens: 5,
|
|
271
|
+
outputTokens: 2,
|
|
272
|
+
nonCachedInputTokens: 4,
|
|
273
|
+
cacheReadInputTokens: 1,
|
|
274
|
+
reasoningTokens: 0,
|
|
275
|
+
totalTokens: 7,
|
|
276
|
+
providerMetadata: {
|
|
277
|
+
openai: {
|
|
278
|
+
prompt_tokens: 5,
|
|
279
|
+
completion_tokens: 2,
|
|
280
|
+
total_tokens: 7,
|
|
281
|
+
prompt_tokens_details: { cached_tokens: 1 },
|
|
282
|
+
completion_tokens_details: { reasoning_tokens: 0 },
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
expect(response.text).toBe("Hello!")
|
|
288
|
+
expect(response.events).toEqual([
|
|
289
|
+
{ type: "step-start", index: 0 },
|
|
290
|
+
{ type: "text-start", id: "text-0" },
|
|
291
|
+
{ type: "text-delta", id: "text-0", text: "Hello" },
|
|
292
|
+
{ type: "text-delta", id: "text-0", text: "!" },
|
|
293
|
+
{ type: "text-end", id: "text-0" },
|
|
294
|
+
{ type: "step-finish", index: 0, reason: "stop", usage, providerMetadata: undefined },
|
|
295
|
+
{
|
|
296
|
+
type: "finish",
|
|
297
|
+
reason: "stop",
|
|
298
|
+
usage,
|
|
299
|
+
},
|
|
300
|
+
])
|
|
301
|
+
}),
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
it.effect("parses OpenAI-compatible reasoning content deltas", () =>
|
|
305
|
+
Effect.gen(function* () {
|
|
306
|
+
const body = sseEvents(
|
|
307
|
+
{ choices: [{ delta: { reasoning_content: "thinking" } }] },
|
|
308
|
+
{ choices: [{ delta: { content: "Hello" } }] },
|
|
309
|
+
{ choices: [{ delta: {}, finish_reason: "stop" }] },
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
|
|
313
|
+
|
|
314
|
+
expect(response.reasoning).toBe("thinking")
|
|
315
|
+
expect(response.text).toBe("Hello")
|
|
316
|
+
expect(response.events).toMatchObject([
|
|
317
|
+
{ type: "step-start", index: 0 },
|
|
318
|
+
{ type: "reasoning-start", id: "reasoning-0" },
|
|
319
|
+
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
|
|
320
|
+
{ type: "text-start", id: "text-0" },
|
|
321
|
+
{ type: "text-delta", id: "text-0", text: "Hello" },
|
|
322
|
+
{ type: "reasoning-end", id: "reasoning-0" },
|
|
323
|
+
{ type: "text-end", id: "text-0" },
|
|
324
|
+
{ type: "step-finish", index: 0, reason: "stop" },
|
|
325
|
+
{ type: "finish", reason: "stop" },
|
|
326
|
+
])
|
|
327
|
+
}),
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
it.effect("assembles streamed tool call input", () =>
|
|
331
|
+
Effect.gen(function* () {
|
|
332
|
+
const body = sseEvents(
|
|
333
|
+
deltaChunk({
|
|
334
|
+
role: "assistant",
|
|
335
|
+
tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
|
|
336
|
+
}),
|
|
337
|
+
deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
|
|
338
|
+
deltaChunk({}, "tool_calls"),
|
|
339
|
+
)
|
|
340
|
+
const response = yield* LLMClient.generate(
|
|
341
|
+
LLM.updateRequest(request, {
|
|
342
|
+
tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
|
|
343
|
+
}),
|
|
344
|
+
).pipe(Effect.provide(fixedResponse(body)))
|
|
345
|
+
|
|
346
|
+
expect(response.events).toEqual([
|
|
347
|
+
{ type: "step-start", index: 0 },
|
|
348
|
+
{ type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: undefined },
|
|
349
|
+
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
|
|
350
|
+
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
|
|
351
|
+
{ type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: undefined },
|
|
352
|
+
{
|
|
353
|
+
type: "tool-call",
|
|
354
|
+
id: "call_1",
|
|
355
|
+
name: "lookup",
|
|
356
|
+
input: { query: "weather" },
|
|
357
|
+
providerExecuted: undefined,
|
|
358
|
+
providerMetadata: undefined,
|
|
359
|
+
},
|
|
360
|
+
{ type: "step-finish", index: 0, reason: "tool-calls", usage: undefined, providerMetadata: undefined },
|
|
361
|
+
{ type: "finish", reason: "tool-calls", usage: undefined },
|
|
362
|
+
])
|
|
363
|
+
}),
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
it.effect("does not finalize streamed tool calls without a finish reason", () =>
|
|
367
|
+
Effect.gen(function* () {
|
|
368
|
+
const body = sseEvents(
|
|
369
|
+
deltaChunk({
|
|
370
|
+
role: "assistant",
|
|
371
|
+
tool_calls: [{ index: 0, id: "call_1", function: { name: "lookup", arguments: '{"query"' } }],
|
|
372
|
+
}),
|
|
373
|
+
deltaChunk({ tool_calls: [{ index: 0, function: { arguments: ':"weather"}' } }] }),
|
|
374
|
+
)
|
|
375
|
+
const response = yield* LLMClient.generate(
|
|
376
|
+
LLM.updateRequest(request, {
|
|
377
|
+
tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
|
|
378
|
+
}),
|
|
379
|
+
).pipe(Effect.provide(fixedResponse(body)))
|
|
380
|
+
|
|
381
|
+
expect(response.events).toEqual([
|
|
382
|
+
{ type: "step-start", index: 0 },
|
|
383
|
+
{ type: "tool-input-start", id: "call_1", name: "lookup", providerMetadata: undefined },
|
|
384
|
+
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
|
|
385
|
+
{ type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
|
|
386
|
+
])
|
|
387
|
+
expect(response.toolCalls).toEqual([])
|
|
388
|
+
}),
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
it.effect("fails on malformed stream events", () =>
|
|
392
|
+
Effect.gen(function* () {
|
|
393
|
+
const body = sseEvents(deltaChunk({ content: 123 }))
|
|
394
|
+
const error = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)), Effect.flip)
|
|
395
|
+
|
|
396
|
+
expect(error.message).toContain("Invalid openai/openai-chat stream event")
|
|
397
|
+
}),
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
it.effect("surfaces transport errors that occur mid-stream", () =>
|
|
401
|
+
Effect.gen(function* () {
|
|
402
|
+
const layer = truncatedStream([
|
|
403
|
+
`data: ${JSON.stringify(deltaChunk({ role: "assistant", content: "Hello" }))}\n\n`,
|
|
404
|
+
])
|
|
405
|
+
const error = yield* LLMClient.generate(request).pipe(Effect.provide(layer), Effect.flip)
|
|
406
|
+
|
|
407
|
+
expect(error.message).toContain("Failed to read openai/openai-chat stream")
|
|
408
|
+
}),
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
it.effect("fails HTTP provider errors before stream parsing", () =>
|
|
412
|
+
Effect.gen(function* () {
|
|
413
|
+
const error = yield* LLMClient.generate(request).pipe(
|
|
414
|
+
Effect.provide(
|
|
415
|
+
fixedResponse('{"error":{"message":"Bad request","type":"invalid_request_error"}}', {
|
|
416
|
+
status: 400,
|
|
417
|
+
headers: { "content-type": "application/json" },
|
|
418
|
+
}),
|
|
419
|
+
),
|
|
420
|
+
Effect.flip,
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
expect(error).toBeInstanceOf(LLMError)
|
|
424
|
+
expect(error.reason).toMatchObject({ _tag: "InvalidRequest" })
|
|
425
|
+
expect(error.message).toContain("HTTP 400")
|
|
426
|
+
}),
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
it.effect("short-circuits the upstream stream when the consumer takes a prefix", () =>
|
|
430
|
+
Effect.gen(function* () {
|
|
431
|
+
// The body has more chunks than we'll consume. If `Stream.take(1)` did
|
|
432
|
+
// not interrupt the upstream HTTP body the test would hang waiting for
|
|
433
|
+
// the rest of the stream to drain.
|
|
434
|
+
const body = sseEvents(
|
|
435
|
+
deltaChunk({ role: "assistant", content: "Hello" }),
|
|
436
|
+
deltaChunk({ content: " world" }),
|
|
437
|
+
deltaChunk({}, "stop"),
|
|
438
|
+
)
|
|
439
|
+
|
|
440
|
+
const events = Array.from(
|
|
441
|
+
yield* LLMClient.stream(request).pipe(Stream.take(1), Stream.runCollect, Effect.provide(fixedResponse(body))),
|
|
442
|
+
)
|
|
443
|
+
expect(events.map((event) => event.type)).toEqual(["step-start"])
|
|
444
|
+
}),
|
|
445
|
+
)
|
|
446
|
+
})
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { describe, expect } from "bun:test"
|
|
2
|
+
import { Effect, Schema } from "effect"
|
|
3
|
+
import { HttpClientRequest } from "effect/unstable/http"
|
|
4
|
+
import { LLM, Message, ToolCallPart } from "../../src"
|
|
5
|
+
import { Auth, LLMClient } from "../../src/route"
|
|
6
|
+
import * as OpenAICompatible from "../../src/providers/openai-compatible"
|
|
7
|
+
import * as OpenAICompatibleChat from "../../src/protocols/openai-compatible-chat"
|
|
8
|
+
import { it } from "../lib/effect"
|
|
9
|
+
import { dynamicResponse } from "../lib/http"
|
|
10
|
+
import { sseEvents } from "../lib/sse"
|
|
11
|
+
|
|
12
|
+
const Json = Schema.fromJsonString(Schema.Unknown)
|
|
13
|
+
const decodeJson = Schema.decodeUnknownSync(Json)
|
|
14
|
+
|
|
15
|
+
const model = OpenAICompatibleChat.route
|
|
16
|
+
.with({
|
|
17
|
+
provider: "deepseek",
|
|
18
|
+
endpoint: { baseURL: "https://api.deepseek.test/v1/", query: { "api-version": "2026-01-01" } },
|
|
19
|
+
auth: Auth.bearer("test-key"),
|
|
20
|
+
})
|
|
21
|
+
.model({ id: "deepseek-chat" })
|
|
22
|
+
|
|
23
|
+
const request = LLM.request({
|
|
24
|
+
id: "req_1",
|
|
25
|
+
model,
|
|
26
|
+
system: "You are concise.",
|
|
27
|
+
prompt: "Say hello.",
|
|
28
|
+
generation: { maxTokens: 20, temperature: 0 },
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const deltaChunk = (delta: object, finishReason: string | null = null) => ({
|
|
32
|
+
id: "chatcmpl_fixture",
|
|
33
|
+
choices: [{ delta, finish_reason: finishReason }],
|
|
34
|
+
usage: null,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const usageChunk = (usage: object) => ({
|
|
38
|
+
id: "chatcmpl_fixture",
|
|
39
|
+
choices: [],
|
|
40
|
+
usage,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const providerFamilies = [
|
|
44
|
+
["baseten", OpenAICompatible.baseten, "https://inference.baseten.co/v1"],
|
|
45
|
+
["cerebras", OpenAICompatible.cerebras, "https://api.cerebras.ai/v1"],
|
|
46
|
+
["deepinfra", OpenAICompatible.deepinfra, "https://api.deepinfra.com/v1/openai"],
|
|
47
|
+
["deepseek", OpenAICompatible.deepseek, "https://api.deepseek.com/v1"],
|
|
48
|
+
["fireworks", OpenAICompatible.fireworks, "https://api.fireworks.ai/inference/v1"],
|
|
49
|
+
["togetherai", OpenAICompatible.togetherai, "https://api.together.xyz/v1"],
|
|
50
|
+
] as const
|
|
51
|
+
|
|
52
|
+
describe("OpenAI-compatible Chat route", () => {
|
|
53
|
+
it.effect("prepares generic Chat target", () =>
|
|
54
|
+
Effect.gen(function* () {
|
|
55
|
+
const prepared = yield* LLMClient.prepare(
|
|
56
|
+
LLM.updateRequest(request, {
|
|
57
|
+
tools: [{ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } }],
|
|
58
|
+
toolChoice: { type: "required" },
|
|
59
|
+
}),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
expect(prepared.route).toBe("openai-compatible-chat")
|
|
63
|
+
expect(prepared.model).toMatchObject({
|
|
64
|
+
id: "deepseek-chat",
|
|
65
|
+
provider: "deepseek",
|
|
66
|
+
route: { id: "openai-compatible-chat" },
|
|
67
|
+
})
|
|
68
|
+
expect(prepared.model.route.endpoint).toMatchObject({
|
|
69
|
+
baseURL: "https://api.deepseek.test/v1/",
|
|
70
|
+
query: { "api-version": "2026-01-01" },
|
|
71
|
+
})
|
|
72
|
+
expect(prepared.body).toEqual({
|
|
73
|
+
model: "deepseek-chat",
|
|
74
|
+
messages: [
|
|
75
|
+
{ role: "system", content: "You are concise." },
|
|
76
|
+
{ role: "user", content: "Say hello." },
|
|
77
|
+
],
|
|
78
|
+
tools: [
|
|
79
|
+
{
|
|
80
|
+
type: "function",
|
|
81
|
+
function: { name: "lookup", description: "Lookup data", parameters: { type: "object" } },
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
tool_choice: "required",
|
|
85
|
+
stream: true,
|
|
86
|
+
stream_options: { include_usage: true },
|
|
87
|
+
max_tokens: 20,
|
|
88
|
+
temperature: 0,
|
|
89
|
+
})
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
it.effect("provides model helpers for compatible provider families", () =>
|
|
94
|
+
Effect.gen(function* () {
|
|
95
|
+
expect(
|
|
96
|
+
providerFamilies.map(([provider, family]) => {
|
|
97
|
+
const model = family.configure({ apiKey: "test-key" }).model(`${provider}-model`)
|
|
98
|
+
return {
|
|
99
|
+
id: String(model.id),
|
|
100
|
+
provider: String(model.provider),
|
|
101
|
+
route: model.route.id,
|
|
102
|
+
baseURL: model.route.endpoint.baseURL,
|
|
103
|
+
}
|
|
104
|
+
}),
|
|
105
|
+
).toEqual(
|
|
106
|
+
providerFamilies.map(([provider, _, baseURL]) => ({
|
|
107
|
+
id: `${provider}-model`,
|
|
108
|
+
provider,
|
|
109
|
+
route: "openai-compatible-chat",
|
|
110
|
+
baseURL,
|
|
111
|
+
})),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const custom = OpenAICompatible.deepseek
|
|
115
|
+
.configure({
|
|
116
|
+
apiKey: "test-key",
|
|
117
|
+
baseURL: "https://custom.deepseek.test/v1",
|
|
118
|
+
})
|
|
119
|
+
.model("deepseek-chat")
|
|
120
|
+
expect(custom).toMatchObject({
|
|
121
|
+
provider: "deepseek",
|
|
122
|
+
route: { id: "openai-compatible-chat" },
|
|
123
|
+
})
|
|
124
|
+
expect(custom.route.endpoint.baseURL).toBe("https://custom.deepseek.test/v1")
|
|
125
|
+
}),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
it.effect("matches AI SDK compatible basic request body fixture", () =>
|
|
129
|
+
Effect.gen(function* () {
|
|
130
|
+
const prepared = yield* LLMClient.prepare(request)
|
|
131
|
+
|
|
132
|
+
expect(prepared.body).toEqual({
|
|
133
|
+
model: "deepseek-chat",
|
|
134
|
+
messages: [
|
|
135
|
+
{ role: "system", content: "You are concise." },
|
|
136
|
+
{ role: "user", content: "Say hello." },
|
|
137
|
+
],
|
|
138
|
+
stream: true,
|
|
139
|
+
stream_options: { include_usage: true },
|
|
140
|
+
max_tokens: 20,
|
|
141
|
+
temperature: 0,
|
|
142
|
+
})
|
|
143
|
+
}),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
it.effect("matches AI SDK compatible tool request body fixture", () =>
|
|
147
|
+
Effect.gen(function* () {
|
|
148
|
+
const prepared = yield* LLMClient.prepare(
|
|
149
|
+
LLM.request({
|
|
150
|
+
id: "req_tool_parity",
|
|
151
|
+
model,
|
|
152
|
+
tools: [
|
|
153
|
+
{
|
|
154
|
+
name: "lookup",
|
|
155
|
+
description: "Lookup data",
|
|
156
|
+
inputSchema: { type: "object", properties: { query: { type: "string" } }, required: ["query"] },
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
toolChoice: "lookup",
|
|
160
|
+
messages: [
|
|
161
|
+
Message.user("What is the weather?"),
|
|
162
|
+
Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
|
|
163
|
+
Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
|
|
164
|
+
],
|
|
165
|
+
}),
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
expect(prepared.body).toEqual({
|
|
169
|
+
model: "deepseek-chat",
|
|
170
|
+
messages: [
|
|
171
|
+
{ role: "user", content: "What is the weather?" },
|
|
172
|
+
{
|
|
173
|
+
role: "assistant",
|
|
174
|
+
content: null,
|
|
175
|
+
tool_calls: [
|
|
176
|
+
{
|
|
177
|
+
id: "call_1",
|
|
178
|
+
type: "function",
|
|
179
|
+
function: { name: "lookup", arguments: '{"query":"weather"}' },
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
{ role: "tool", tool_call_id: "call_1", content: '{"forecast":"sunny"}' },
|
|
184
|
+
],
|
|
185
|
+
tools: [
|
|
186
|
+
{
|
|
187
|
+
type: "function",
|
|
188
|
+
function: {
|
|
189
|
+
name: "lookup",
|
|
190
|
+
description: "Lookup data",
|
|
191
|
+
parameters: { type: "object", properties: { query: { type: "string" } }, required: ["query"] },
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
tool_choice: { type: "function", function: { name: "lookup" } },
|
|
196
|
+
stream: true,
|
|
197
|
+
stream_options: { include_usage: true },
|
|
198
|
+
})
|
|
199
|
+
}),
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
it.effect("posts to the configured compatible endpoint and parses text usage", () =>
|
|
203
|
+
Effect.gen(function* () {
|
|
204
|
+
const response = yield* LLMClient.generate(request).pipe(
|
|
205
|
+
Effect.provide(
|
|
206
|
+
dynamicResponse((input) =>
|
|
207
|
+
Effect.gen(function* () {
|
|
208
|
+
const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
|
|
209
|
+
expect(web.url).toBe("https://api.deepseek.test/v1/chat/completions?api-version=2026-01-01")
|
|
210
|
+
expect(web.headers.get("authorization")).toBe("Bearer test-key")
|
|
211
|
+
expect(decodeJson(input.text)).toMatchObject({
|
|
212
|
+
model: "deepseek-chat",
|
|
213
|
+
stream: true,
|
|
214
|
+
messages: [
|
|
215
|
+
{ role: "system", content: "You are concise." },
|
|
216
|
+
{ role: "user", content: "Say hello." },
|
|
217
|
+
],
|
|
218
|
+
})
|
|
219
|
+
return input.respond(
|
|
220
|
+
sseEvents(
|
|
221
|
+
deltaChunk({ role: "assistant", content: "Hello" }),
|
|
222
|
+
deltaChunk({ content: "!" }),
|
|
223
|
+
deltaChunk({}, "stop"),
|
|
224
|
+
usageChunk({ prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }),
|
|
225
|
+
),
|
|
226
|
+
{ headers: { "content-type": "text/event-stream" } },
|
|
227
|
+
)
|
|
228
|
+
}),
|
|
229
|
+
),
|
|
230
|
+
),
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
expect(response.text).toBe("Hello!")
|
|
234
|
+
expect(response.usage).toMatchObject({ inputTokens: 5, outputTokens: 2, totalTokens: 7 })
|
|
235
|
+
expect(response.events.at(-1)).toMatchObject({ type: "finish", reason: "stop" })
|
|
236
|
+
}),
|
|
237
|
+
)
|
|
238
|
+
})
|