@neurocode-ai/llm 1.18.8

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.
Files changed (152) hide show
  1. package/AGENTS.md +321 -0
  2. package/DESIGN.md +1114 -0
  3. package/README.md +131 -0
  4. package/example/call-sites.md +591 -0
  5. package/example/tutorial.ts +255 -0
  6. package/package.json +51 -0
  7. package/script/recording-cost-report.ts +250 -0
  8. package/script/setup-recording-env.ts +542 -0
  9. package/src/cache-policy.ts +111 -0
  10. package/src/index.ts +33 -0
  11. package/src/llm.ts +186 -0
  12. package/src/protocols/anthropic-messages.ts +855 -0
  13. package/src/protocols/bedrock-converse.ts +674 -0
  14. package/src/protocols/bedrock-event-stream.ts +87 -0
  15. package/src/protocols/gemini.ts +512 -0
  16. package/src/protocols/index.ts +6 -0
  17. package/src/protocols/openai-chat.ts +506 -0
  18. package/src/protocols/openai-compatible-chat.ts +24 -0
  19. package/src/protocols/openai-responses.ts +1022 -0
  20. package/src/protocols/shared.ts +326 -0
  21. package/src/protocols/utils/bedrock-auth.ts +70 -0
  22. package/src/protocols/utils/bedrock-cache.ts +37 -0
  23. package/src/protocols/utils/bedrock-media.ts +90 -0
  24. package/src/protocols/utils/cache.ts +16 -0
  25. package/src/protocols/utils/gemini-tool-schema.ts +99 -0
  26. package/src/protocols/utils/lifecycle.ts +102 -0
  27. package/src/protocols/utils/openai-options.ts +93 -0
  28. package/src/protocols/utils/tool-schema.ts +86 -0
  29. package/src/protocols/utils/tool-stream.ts +218 -0
  30. package/src/provider-error.ts +43 -0
  31. package/src/provider.ts +36 -0
  32. package/src/providers/amazon-bedrock.ts +43 -0
  33. package/src/providers/anthropic.ts +35 -0
  34. package/src/providers/azure.ts +110 -0
  35. package/src/providers/cloudflare.ts +127 -0
  36. package/src/providers/github-copilot.ts +69 -0
  37. package/src/providers/google.ts +35 -0
  38. package/src/providers/index.ts +11 -0
  39. package/src/providers/openai-compatible-profile.ts +20 -0
  40. package/src/providers/openai-compatible.ts +65 -0
  41. package/src/providers/openai-options.ts +83 -0
  42. package/src/providers/openai.ts +63 -0
  43. package/src/providers/openrouter.ts +98 -0
  44. package/src/providers/xai.ts +56 -0
  45. package/src/route/auth-options.ts +57 -0
  46. package/src/route/auth.ts +156 -0
  47. package/src/route/client.ts +436 -0
  48. package/src/route/endpoint.ts +53 -0
  49. package/src/route/executor.ts +385 -0
  50. package/src/route/framing.ts +27 -0
  51. package/src/route/index.ts +25 -0
  52. package/src/route/protocol.ts +84 -0
  53. package/src/route/transport/http.ts +155 -0
  54. package/src/route/transport/index.ts +33 -0
  55. package/src/route/transport/websocket.ts +280 -0
  56. package/src/schema/errors.ts +207 -0
  57. package/src/schema/events.ts +618 -0
  58. package/src/schema/ids.ts +43 -0
  59. package/src/schema/index.ts +5 -0
  60. package/src/schema/messages.ts +312 -0
  61. package/src/schema/options.ts +276 -0
  62. package/src/tool-runtime.ts +78 -0
  63. package/src/tool.ts +253 -0
  64. package/src/utils/record.ts +3 -0
  65. package/sst-env.d.ts +10 -0
  66. package/test/adapter.test.ts +171 -0
  67. package/test/auth-options.types.ts +168 -0
  68. package/test/auth.test.ts +103 -0
  69. package/test/cache-policy.test.ts +262 -0
  70. package/test/continuation-scenarios.ts +104 -0
  71. package/test/endpoint.test.ts +58 -0
  72. package/test/executor.test.ts +458 -0
  73. package/test/exports.test.ts +76 -0
  74. package/test/fixtures/media/restroom.png +0 -0
  75. package/test/fixtures/recordings/anthropic-messages/accepts-malformed-assistant-tool-order-with-default-patch.json +29 -0
  76. package/test/fixtures/recordings/anthropic-messages/anthropic-opus-4-7-image-tool-result.json +43 -0
  77. package/test/fixtures/recordings/anthropic-messages/claude-opus-4-7-drives-a-tool-loop.json +56 -0
  78. package/test/fixtures/recordings/anthropic-messages/rejects-malformed-assistant-tool-order-without-patch.json +29 -0
  79. package/test/fixtures/recordings/anthropic-messages/streams-text.json +29 -0
  80. package/test/fixtures/recordings/anthropic-messages/streams-tool-call.json +29 -0
  81. package/test/fixtures/recordings/anthropic-messages-cache/writes-then-reads-cache-control-on-identical-second-call.json +48 -0
  82. package/test/fixtures/recordings/bedrock-converse/drives-a-tool-loop.json +55 -0
  83. package/test/fixtures/recordings/bedrock-converse/streams-a-tool-call.json +29 -0
  84. package/test/fixtures/recordings/bedrock-converse/streams-text.json +29 -0
  85. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
  86. package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-llama-3-1-8b-text.json +32 -0
  87. package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
  88. package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-llama-3-1-8b-text.json +32 -0
  89. package/test/fixtures/recordings/gemini/gemini-2-5-flash-image.json +32 -0
  90. package/test/fixtures/recordings/gemini/streams-text.json +28 -0
  91. package/test/fixtures/recordings/gemini/streams-tool-call.json +28 -0
  92. package/test/fixtures/recordings/gemini-cache/reports-cachedcontenttokencount-on-identical-second-call.json +46 -0
  93. package/test/fixtures/recordings/openai-chat/continues-after-tool-result.json +28 -0
  94. package/test/fixtures/recordings/openai-chat/drives-a-tool-loop-end-to-end.json +46 -0
  95. package/test/fixtures/recordings/openai-chat/streams-text.json +28 -0
  96. package/test/fixtures/recordings/openai-chat/streams-tool-call.json +28 -0
  97. package/test/fixtures/recordings/openai-compatible-chat/deepseek-streams-text.json +28 -0
  98. package/test/fixtures/recordings/openai-compatible-chat/groq-llama-3-3-70b-drives-a-tool-loop.json +53 -0
  99. package/test/fixtures/recordings/openai-compatible-chat/groq-streams-text.json +28 -0
  100. package/test/fixtures/recordings/openai-compatible-chat/groq-streams-tool-call.json +28 -0
  101. package/test/fixtures/recordings/openai-compatible-chat/openrouter-claude-opus-4-7-drives-a-tool-loop.json +54 -0
  102. package/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-4o-mini-drives-a-tool-loop.json +53 -0
  103. package/test/fixtures/recordings/openai-compatible-chat/openrouter-gpt-5-5-drives-a-tool-loop.json +54 -0
  104. package/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-text.json +28 -0
  105. package/test/fixtures/recordings/openai-compatible-chat/openrouter-streams-tool-call.json +28 -0
  106. package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-text.json +28 -0
  107. package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-tool-call.json +28 -0
  108. package/test/fixtures/recordings/openai-responses/gpt-5-5-drives-a-tool-loop.json +54 -0
  109. package/test/fixtures/recordings/openai-responses/gpt-5-5-streams-text.json +28 -0
  110. package/test/fixtures/recordings/openai-responses/gpt-5-5-streams-tool-call.json +28 -0
  111. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-image-tool-result.json +42 -0
  112. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning-continuation.json +58 -0
  113. package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning.json +32 -0
  114. package/test/fixtures/recordings/openai-responses-cache/reports-cached-tokens-on-identical-second-call.json +46 -0
  115. package/test/generate-object.test.ts +184 -0
  116. package/test/lib/effect.ts +50 -0
  117. package/test/lib/http.ts +98 -0
  118. package/test/lib/openai-chunks.ts +27 -0
  119. package/test/lib/sse.ts +17 -0
  120. package/test/lib/tool-runtime.ts +146 -0
  121. package/test/llm.test.ts +199 -0
  122. package/test/prepare.test.ts +178 -0
  123. package/test/provider/anthropic-messages-cache.recorded.test.ts +53 -0
  124. package/test/provider/anthropic-messages.recorded.test.ts +45 -0
  125. package/test/provider/anthropic-messages.test.ts +895 -0
  126. package/test/provider/bedrock-converse-cache.recorded.test.ts +54 -0
  127. package/test/provider/bedrock-converse.test.ts +744 -0
  128. package/test/provider/cloudflare.test.ts +230 -0
  129. package/test/provider/gemini-cache.recorded.test.ts +48 -0
  130. package/test/provider/gemini.test.ts +584 -0
  131. package/test/provider/golden.recorded.test.ts +223 -0
  132. package/test/provider/openai-chat.test.ts +674 -0
  133. package/test/provider/openai-compatible-chat.test.ts +238 -0
  134. package/test/provider/openai-responses-cache.recorded.test.ts +46 -0
  135. package/test/provider/openai-responses.test.ts +1472 -0
  136. package/test/provider/openrouter.test.ts +56 -0
  137. package/test/provider-error.test.ts +30 -0
  138. package/test/provider.types.ts +41 -0
  139. package/test/recorded-golden.ts +97 -0
  140. package/test/recorded-runner.ts +100 -0
  141. package/test/recorded-scenarios.ts +531 -0
  142. package/test/recorded-test.ts +94 -0
  143. package/test/recorded-utils.ts +56 -0
  144. package/test/recorded-websocket.ts +26 -0
  145. package/test/response.test.ts +98 -0
  146. package/test/route.test.ts +43 -0
  147. package/test/schema.test.ts +86 -0
  148. package/test/tool-runtime.test.ts +818 -0
  149. package/test/tool-schema-projection.test.ts +117 -0
  150. package/test/tool-stream.test.ts +99 -0
  151. package/test/tool.types.ts +40 -0
  152. package/tsconfig.json +15 -0
@@ -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
+ })
@@ -0,0 +1,46 @@
1
+ import { describe, expect } from "bun:test"
2
+ import { Effect } from "effect"
3
+ import { LLM } from "../../src"
4
+ import { LLMClient } from "../../src/route"
5
+ import * as OpenAI from "../../src/providers/openai"
6
+ import { LARGE_CACHEABLE_SYSTEM } from "../recorded-scenarios"
7
+ import { recordedTests } from "../recorded-test"
8
+
9
+ const model = OpenAI.configure({
10
+ apiKey: process.env.OPENAI_API_KEY ?? "fixture",
11
+ }).responses("gpt-4.1-mini")
12
+
13
+ // OpenAI caches prefixes automatically once they cross the 1024-token threshold;
14
+ // `CacheHint` is a no-op for the wire body. The stable signal is the
15
+ // `prompt_cache_key` routing hint, which keeps repeated calls on the same shard
16
+ // so cache hits are observable.
17
+ const cacheRequest = LLM.request({
18
+ id: "recorded_openai_responses_cache",
19
+ model,
20
+ system: LARGE_CACHEABLE_SYSTEM,
21
+ prompt: "Say hi.",
22
+ generation: { maxTokens: 16, temperature: 0 },
23
+ providerOptions: { openai: { promptCacheKey: "recorded-cache-test" } },
24
+ })
25
+
26
+ const recorded = recordedTests({
27
+ prefix: "openai-responses-cache",
28
+ provider: "openai",
29
+ protocol: "openai-responses",
30
+ requires: ["OPENAI_API_KEY"],
31
+ // Two identical requests in one cassette — replay walks the cassette in
32
+ // recording order so the second call replays the cached-hit interaction,
33
+ // not the cold-miss one.
34
+ })
35
+
36
+ describe("OpenAI Responses cache recorded", () => {
37
+ recorded.effect.with("reports cached_tokens on identical second call", { tags: ["cache"] }, () =>
38
+ Effect.gen(function* () {
39
+ const first = yield* LLMClient.generate(cacheRequest)
40
+ expect(first.usage?.cacheReadInputTokens ?? 0).toBeGreaterThanOrEqual(0)
41
+
42
+ const second = yield* LLMClient.generate(cacheRequest)
43
+ expect(second.usage?.cacheReadInputTokens ?? 0).toBeGreaterThan(0)
44
+ }),
45
+ )
46
+ })