@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,223 @@
1
+ import * as Anthropic from "../../src/providers/anthropic"
2
+ import { CloudflareAIGateway, CloudflareWorkersAI } from "../../src/providers/cloudflare"
3
+ import * as Google from "../../src/providers/google"
4
+ import * as OpenAI from "../../src/providers/openai"
5
+ import * as OpenAICompatible from "../../src/providers/openai-compatible"
6
+ import * as OpenRouter from "../../src/providers/openrouter"
7
+ import * as XAI from "../../src/providers/xai"
8
+ import { describeRecordedGoldenScenarios } from "../recorded-golden"
9
+
10
+ const openAI = OpenAI.configure({
11
+ apiKey: process.env.OPENAI_API_KEY ?? "fixture",
12
+ })
13
+ const openAIChat = openAI.chat("gpt-4o-mini")
14
+ const openAIResponses = openAI.responses("gpt-5.5")
15
+ const openAIResponsesWebSocket = openAI.responsesWebSocket("gpt-4.1-mini")
16
+ const anthropic = Anthropic.configure({
17
+ apiKey: process.env.ANTHROPIC_API_KEY ?? "fixture",
18
+ })
19
+ const anthropicHaiku = anthropic.model("claude-haiku-4-5-20251001")
20
+ const anthropicOpus = anthropic.model("claude-opus-4-7")
21
+ const google = Google.configure({ apiKey: process.env.GOOGLE_GENERATIVE_AI_API_KEY ?? "fixture" })
22
+ const gemini = google.model("gemini-2.5-flash")
23
+ const xai = XAI.configure({ apiKey: process.env.XAI_API_KEY ?? "fixture" })
24
+ const xaiBasic = xai.model("grok-3-mini")
25
+ const xaiFlagship = xai.model("grok-4.3")
26
+ const cloudflareAIGateway = CloudflareAIGateway.configure({
27
+ accountId: process.env.CLOUDFLARE_ACCOUNT_ID ?? "fixture-account",
28
+ gatewayId:
29
+ process.env.CLOUDFLARE_GATEWAY_ID && process.env.CLOUDFLARE_GATEWAY_ID !== process.env.CLOUDFLARE_ACCOUNT_ID
30
+ ? process.env.CLOUDFLARE_GATEWAY_ID
31
+ : undefined,
32
+ gatewayApiKey: process.env.CLOUDFLARE_API_TOKEN ?? "fixture",
33
+ })
34
+ const cloudflareWorkers = CloudflareWorkersAI.configure({
35
+ accountId: process.env.CLOUDFLARE_ACCOUNT_ID ?? "fixture-account",
36
+ apiKey: process.env.CLOUDFLARE_API_KEY ?? "fixture",
37
+ })
38
+ const cloudflareAIGatewayWorkers = cloudflareAIGateway.model("workers-ai/@cf/meta/llama-3.1-8b-instruct")
39
+ const cloudflareAIGatewayWorkersTools = cloudflareAIGateway.model("workers-ai/@cf/openai/gpt-oss-20b")
40
+ const cloudflareWorkersAI = cloudflareWorkers.model("@cf/meta/llama-3.1-8b-instruct")
41
+ const cloudflareWorkersAITools = cloudflareWorkers.model("@cf/openai/gpt-oss-20b")
42
+ const deepseek = OpenAICompatible.deepseek
43
+ .configure({ apiKey: process.env.DEEPSEEK_API_KEY ?? "fixture" })
44
+ .model("deepseek-chat")
45
+ const together = OpenAICompatible.togetherai
46
+ .configure({
47
+ apiKey: process.env.TOGETHER_AI_API_KEY ?? "fixture",
48
+ })
49
+ .model("meta-llama/Llama-3.3-70B-Instruct-Turbo")
50
+ const groq = OpenAICompatible.groq
51
+ .configure({ apiKey: process.env.GROQ_API_KEY ?? "fixture" })
52
+ .model("llama-3.3-70b-versatile")
53
+ const openRouter = OpenRouter.configure({ apiKey: process.env.OPENROUTER_API_KEY ?? "fixture" })
54
+ const openrouter = openRouter.model("openai/gpt-4o-mini")
55
+ const openrouterGpt55 = openRouter.model("openai/gpt-5.5")
56
+ const openrouterOpus = OpenRouter.configure({
57
+ apiKey: process.env.OPENROUTER_API_KEY ?? "fixture",
58
+ }).model("anthropic/claude-opus-4.7")
59
+
60
+ const redactCloudflareURL = (url: string) =>
61
+ url
62
+ .replace(/\/client\/v4\/accounts\/[^/]+\/ai\/v1\//, "/client/v4/accounts/{account}/ai/v1/")
63
+ .replace(/\/v1\/[^/]+\/[^/]+\/compat\//, "/v1/{account}/{gateway}/compat/")
64
+
65
+ const cloudflareOptions = {
66
+ redact: { url: redactCloudflareURL },
67
+ }
68
+
69
+ describeRecordedGoldenScenarios([
70
+ {
71
+ name: "OpenAI Chat gpt-4o-mini",
72
+ prefix: "openai-chat",
73
+ model: openAIChat,
74
+ requires: ["OPENAI_API_KEY"],
75
+ scenarios: ["text", "tool-call", "tool-loop", { id: "image-tool-result", maxTokens: 40 }],
76
+ },
77
+ {
78
+ name: "OpenAI Responses gpt-5.5",
79
+ prefix: "openai-responses",
80
+ model: openAIResponses,
81
+ requires: ["OPENAI_API_KEY"],
82
+ tags: ["flagship"],
83
+ scenarios: [
84
+ { id: "text", temperature: false },
85
+ { id: "reasoning", temperature: false },
86
+ { id: "reasoning-continuation", temperature: false },
87
+ { id: "tool-call", temperature: false },
88
+ { id: "tool-loop", temperature: false },
89
+ { id: "image-tool-result", temperature: false, maxTokens: 40 },
90
+ ],
91
+ },
92
+ {
93
+ name: "OpenAI Responses WebSocket gpt-4.1-mini",
94
+ prefix: "openai-responses-websocket",
95
+ model: openAIResponsesWebSocket,
96
+ transport: "websocket",
97
+ requires: ["OPENAI_API_KEY"],
98
+ scenarios: ["tool-loop"],
99
+ },
100
+ {
101
+ name: "Anthropic Haiku 4.5",
102
+ prefix: "anthropic-messages",
103
+ model: anthropicHaiku,
104
+ requires: ["ANTHROPIC_API_KEY"],
105
+ options: { redact: { allowRequestHeaders: ["anthropic-version"] } },
106
+ scenarios: ["text", "tool-call"],
107
+ },
108
+ {
109
+ name: "Anthropic Opus 4.7",
110
+ prefix: "anthropic-messages",
111
+ model: anthropicOpus,
112
+ requires: ["ANTHROPIC_API_KEY"],
113
+ tags: ["flagship"],
114
+ options: { redact: { allowRequestHeaders: ["anthropic-version"] } },
115
+ scenarios: [
116
+ { id: "tool-loop", temperature: false },
117
+ { id: "image-tool-result", temperature: false, maxTokens: 40 },
118
+ ],
119
+ },
120
+ {
121
+ name: "Gemini 2.5 Flash",
122
+ prefix: "gemini",
123
+ model: gemini,
124
+ requires: ["GOOGLE_GENERATIVE_AI_API_KEY"],
125
+ scenarios: [
126
+ { id: "text", maxTokens: 80 },
127
+ "tool-call",
128
+ { id: "image", maxTokens: 160 },
129
+ { id: "image-tool-result", maxTokens: 40 },
130
+ ],
131
+ },
132
+ {
133
+ name: "xAI Grok 3 Mini",
134
+ prefix: "xai",
135
+ model: xaiBasic,
136
+ requires: ["XAI_API_KEY"],
137
+ scenarios: ["text", "tool-call"],
138
+ },
139
+ {
140
+ name: "xAI Grok 4.3",
141
+ prefix: "xai",
142
+ model: xaiFlagship,
143
+ requires: ["XAI_API_KEY"],
144
+ tags: ["flagship"],
145
+ scenarios: [{ id: "tool-loop", timeout: 30_000 }],
146
+ },
147
+ {
148
+ name: "Cloudflare AI Gateway Workers AI Llama 3.1 8B",
149
+ prefix: "cloudflare-ai-gateway",
150
+ model: cloudflareAIGatewayWorkers,
151
+ requires: ["CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_API_TOKEN"],
152
+ options: cloudflareOptions,
153
+ scenarios: ["text"],
154
+ },
155
+ {
156
+ name: "Cloudflare AI Gateway Workers AI GPT OSS 20B Tools",
157
+ prefix: "cloudflare-ai-gateway",
158
+ model: cloudflareAIGatewayWorkersTools,
159
+ requires: ["CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_API_TOKEN"],
160
+ options: cloudflareOptions,
161
+ scenarios: [{ id: "tool-call", maxTokens: 120 }],
162
+ },
163
+ {
164
+ name: "Cloudflare Workers AI Llama 3.1 8B",
165
+ prefix: "cloudflare-workers-ai",
166
+ model: cloudflareWorkersAI,
167
+ requires: ["CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_API_KEY"],
168
+ options: cloudflareOptions,
169
+ scenarios: ["text"],
170
+ },
171
+ {
172
+ name: "Cloudflare Workers AI GPT OSS 20B Tools",
173
+ prefix: "cloudflare-workers-ai",
174
+ model: cloudflareWorkersAITools,
175
+ requires: ["CLOUDFLARE_ACCOUNT_ID", "CLOUDFLARE_API_KEY"],
176
+ options: cloudflareOptions,
177
+ scenarios: [{ id: "tool-call", maxTokens: 120 }],
178
+ },
179
+ {
180
+ name: "DeepSeek Chat",
181
+ prefix: "openai-compatible-chat",
182
+ model: deepseek,
183
+ requires: ["DEEPSEEK_API_KEY"],
184
+ scenarios: ["text"],
185
+ },
186
+ {
187
+ name: "TogetherAI Llama 3.3 70B",
188
+ prefix: "openai-compatible-chat",
189
+ model: together,
190
+ requires: ["TOGETHER_AI_API_KEY"],
191
+ scenarios: ["text", "tool-call"],
192
+ },
193
+ {
194
+ name: "Groq Llama 3.3 70B",
195
+ prefix: "openai-compatible-chat",
196
+ model: groq,
197
+ requires: ["GROQ_API_KEY"],
198
+ scenarios: ["text", "tool-call", { id: "tool-loop", timeout: 30_000 }],
199
+ },
200
+ {
201
+ name: "OpenRouter gpt-4o-mini",
202
+ prefix: "openai-compatible-chat",
203
+ model: openrouter,
204
+ requires: ["OPENROUTER_API_KEY"],
205
+ scenarios: ["text", "tool-call", "tool-loop"],
206
+ },
207
+ {
208
+ name: "OpenRouter gpt-5.5",
209
+ prefix: "openai-compatible-chat",
210
+ model: openrouterGpt55,
211
+ requires: ["OPENROUTER_API_KEY"],
212
+ tags: ["flagship"],
213
+ scenarios: ["tool-loop"],
214
+ },
215
+ {
216
+ name: "OpenRouter Claude Opus 4.7",
217
+ prefix: "openai-compatible-chat",
218
+ model: openrouterOpus,
219
+ requires: ["OPENROUTER_API_KEY"],
220
+ tags: ["flagship"],
221
+ scenarios: ["tool-loop"],
222
+ },
223
+ ])