@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,117 @@
1
+ import { describe, expect, test } from "bun:test"
2
+ import { Effect } from "effect"
3
+ import { LLM } from "../src"
4
+ import { OpenAIChat } from "../src/protocols"
5
+ import { ToolSchemaProjection } from "../src/protocols/utils/tool-schema"
6
+ import { Auth, LLMClient } from "../src/route"
7
+ import { it } from "./lib/effect"
8
+
9
+ describe("tool schema projections", () => {
10
+ test("moonshot strips $ref siblings and converts tuple arrays to a schema object", () => {
11
+ expect(
12
+ ToolSchemaProjection.moonshot({
13
+ type: "object",
14
+ properties: {
15
+ linked: { $ref: "#/$defs/Linked", description: "drop me" },
16
+ tuple: { type: "array", items: [{ type: "string" }, { type: "number" }] },
17
+ prefixTuple: { type: "array", prefixItems: [{ type: "boolean" }, { type: "string" }] },
18
+ },
19
+ }),
20
+ ).toEqual({
21
+ type: "object",
22
+ properties: {
23
+ linked: { $ref: "#/$defs/Linked" },
24
+ tuple: { type: "array", items: { anyOf: [{ type: "string" }, { type: "number" }] } },
25
+ prefixTuple: { type: "array", items: { anyOf: [{ type: "boolean" }, { type: "string" }] } },
26
+ },
27
+ })
28
+ })
29
+
30
+ test("gemini handles numeric enums, dangling required fields, untyped arrays, and scalar object keys", () => {
31
+ expect(
32
+ ToolSchemaProjection.gemini({
33
+ type: "object",
34
+ required: ["status", "missing"],
35
+ properties: {
36
+ status: { type: "integer", enum: [1, 2] },
37
+ tags: { type: "array" },
38
+ name: { type: "string", properties: { ignored: { type: "string" } }, required: ["ignored"] },
39
+ },
40
+ }),
41
+ ).toEqual({
42
+ type: "object",
43
+ required: ["status"],
44
+ properties: {
45
+ status: { type: "string", enum: ["1", "2"] },
46
+ tags: { type: "array", items: { type: "string" } },
47
+ name: { type: "string" },
48
+ },
49
+ })
50
+ })
51
+
52
+ test("openai keeps one flat object top-level schema", () => {
53
+ expect(
54
+ ToolSchemaProjection.openAI({
55
+ anyOf: [
56
+ {
57
+ type: "object",
58
+ properties: {
59
+ path: { type: "string" },
60
+ maybe: { anyOf: [{ type: "string" }, { type: "null" }] },
61
+ },
62
+ },
63
+ { type: "object", properties: { resource: { type: "string" } } },
64
+ ],
65
+ }),
66
+ ).toEqual({
67
+ type: "object",
68
+ properties: {
69
+ path: { type: "string" },
70
+ maybe: { type: "string" },
71
+ resource: { type: "string" },
72
+ },
73
+ additionalProperties: false,
74
+ })
75
+ })
76
+
77
+ it.effect("applies model compatibility before protocol projection", () =>
78
+ Effect.gen(function* () {
79
+ const model = OpenAIChat.route
80
+ .with({ endpoint: { baseURL: "https://api.openai.test/v1/" }, auth: Auth.bearer("test") })
81
+ .model({ id: "kimi-k2", compatibility: { toolSchema: "moonshot" } })
82
+ const prepared = yield* LLMClient.prepare<OpenAIChat.OpenAIChatBody>(
83
+ LLM.request({
84
+ model,
85
+ prompt: "Use the tool.",
86
+ tools: [
87
+ {
88
+ name: "lookup",
89
+ description: "Lookup data.",
90
+ inputSchema: {
91
+ type: "object",
92
+ anyOf: [
93
+ {
94
+ type: "object",
95
+ properties: {
96
+ tuple: { type: "array", items: [{ type: "string" }, { type: "number" }] },
97
+ linked: { $ref: "#/$defs/Linked", description: "drop me" },
98
+ },
99
+ },
100
+ ],
101
+ },
102
+ },
103
+ ],
104
+ }),
105
+ )
106
+
107
+ expect(prepared.body.tools?.[0]?.function.parameters).toEqual({
108
+ type: "object",
109
+ properties: {
110
+ tuple: { type: "array", items: { anyOf: [{ type: "string" }, { type: "number" }] } },
111
+ linked: { $ref: "#/$defs/Linked" },
112
+ },
113
+ additionalProperties: false,
114
+ })
115
+ }),
116
+ )
117
+ })
@@ -0,0 +1,99 @@
1
+ import { describe, expect } from "bun:test"
2
+ import { Effect } from "effect"
3
+ import { LLMError } from "../src/schema"
4
+ import { ToolStream } from "../src/protocols/utils/tool-stream"
5
+ import { it } from "./lib/effect"
6
+
7
+ const ADAPTER = "test-route"
8
+
9
+ describe("ToolStream", () => {
10
+ it.effect("starts from OpenAI-style deltas and finalizes parsed input", () =>
11
+ Effect.gen(function* () {
12
+ const first = ToolStream.appendOrStart(
13
+ ADAPTER,
14
+ ToolStream.empty<number>(),
15
+ 0,
16
+ { id: "call_1", name: "lookup", text: '{"query"' },
17
+ "missing tool",
18
+ )
19
+ if (ToolStream.isError(first)) return yield* first
20
+ const second = ToolStream.appendOrStart(ADAPTER, first.tools, 0, { text: ':"weather"}' }, "missing tool")
21
+ if (ToolStream.isError(second)) return yield* second
22
+ const finished = yield* ToolStream.finish(ADAPTER, second.tools, 0)
23
+
24
+ expect(first.events).toEqual([
25
+ { type: "tool-input-start", id: "call_1", name: "lookup" },
26
+ { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
27
+ ])
28
+ expect(second.events).toEqual([{ type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' }])
29
+ expect(finished).toEqual({
30
+ tools: {},
31
+ events: [
32
+ { type: "tool-input-end", id: "call_1", name: "lookup" },
33
+ { type: "tool-call", id: "call_1", name: "lookup", input: { query: "weather" } },
34
+ ],
35
+ })
36
+ }),
37
+ )
38
+
39
+ it.effect("fails appendExisting when the provider skipped the tool start", () =>
40
+ Effect.gen(function* () {
41
+ const error = ToolStream.appendExisting(ADAPTER, ToolStream.empty<number>(), 0, "{}", "missing tool")
42
+
43
+ expect(error).toBeInstanceOf(LLMError)
44
+ if (ToolStream.isError(error)) expect(error.reason.message).toBe("missing tool")
45
+ }),
46
+ )
47
+
48
+ it.effect("uses final input override without losing accumulated deltas", () =>
49
+ Effect.gen(function* () {
50
+ const tools = ToolStream.start(ToolStream.empty<string>(), "item_1", {
51
+ id: "call_1",
52
+ name: "lookup",
53
+ input: '{"query":"partial"}',
54
+ })
55
+ const finished = yield* ToolStream.finishWithInput(ADAPTER, tools, "item_1", '{"query":"final"}')
56
+
57
+ expect(finished).toEqual({
58
+ tools: {},
59
+ events: [
60
+ { type: "tool-input-end", id: "call_1", name: "lookup" },
61
+ { type: "tool-call", id: "call_1", name: "lookup", input: { query: "final" } },
62
+ ],
63
+ })
64
+ }),
65
+ )
66
+
67
+ it.effect("preserves providerExecuted and clears all tools", () =>
68
+ Effect.gen(function* () {
69
+ const first: ToolStream.State<number> = ToolStream.start(ToolStream.empty<number>(), 0, {
70
+ id: "call_1",
71
+ name: "lookup",
72
+ input: "{}",
73
+ })
74
+ const tools = ToolStream.start(first, 1, {
75
+ id: "call_2",
76
+ name: "web_search",
77
+ input: '{"query":"docs"}',
78
+ providerExecuted: true,
79
+ })
80
+ const finished = yield* ToolStream.finishAll(ADAPTER, tools)
81
+
82
+ expect(finished).toEqual({
83
+ tools: {},
84
+ events: [
85
+ { type: "tool-input-end", id: "call_1", name: "lookup" },
86
+ { type: "tool-call", id: "call_1", name: "lookup", input: {} },
87
+ { type: "tool-input-end", id: "call_2", name: "web_search" },
88
+ {
89
+ type: "tool-call",
90
+ id: "call_2",
91
+ name: "web_search",
92
+ input: { query: "docs" },
93
+ providerExecuted: true,
94
+ },
95
+ ],
96
+ })
97
+ }),
98
+ )
99
+ })
@@ -0,0 +1,40 @@
1
+ import { Effect, Schema } from "effect"
2
+ import { LLM, LLMRequest, ToolRuntime, toDefinitions } from "../src"
3
+ import * as OpenAIChat from "../src/protocols/openai-chat"
4
+ import { Auth } from "../src/route"
5
+ import { Tool } from "../src/tool"
6
+
7
+ const request = LLM.request({
8
+ model: OpenAIChat.route.with({ auth: Auth.bearer("fixture") }).model({ id: "gpt-4o-mini" }),
9
+ prompt: "Use the tool.",
10
+ })
11
+
12
+ const executable = Tool.make({
13
+ description: "Get weather.",
14
+ parameters: Schema.Struct({ city: Schema.String }),
15
+ success: Schema.Struct({ forecast: Schema.String }),
16
+ execute: (input) => Effect.succeed({ forecast: input.city }),
17
+ })
18
+
19
+ const schemaOnly = Tool.make({
20
+ description: "Get weather.",
21
+ parameters: Schema.Struct({ city: Schema.String }),
22
+ success: Schema.Struct({ forecast: Schema.String }),
23
+ })
24
+
25
+ Tool.make({
26
+ description: "Encode success before projection.",
27
+ parameters: Schema.Struct({ city: Schema.String }),
28
+ success: Schema.Struct({ forecast: Schema.NumberFromString }),
29
+ execute: () => Effect.succeed({ forecast: 1 }),
30
+ toModelOutput: ({ callID, parameters, output }) => [
31
+ { type: "text", text: `${callID}:${parameters.city}:${output.forecast}` },
32
+ ],
33
+ })
34
+
35
+ LLM.stream(request)
36
+ LLM.generate(LLMRequest.update(request, { tools: toDefinitions({ schemaOnly }) }))
37
+ ToolRuntime.dispatch({ executable }, { type: "tool-call", id: "call_1", name: "executable", input: { city: "Paris" } })
38
+
39
+ // @ts-expect-error High-level tool orchestration overloads are intentionally not supported.
40
+ LLM.stream({ request, tools: { schemaOnly } })
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "extends": "@tsconfig/bun/tsconfig.json",
4
+ "compilerOptions": {
5
+ "lib": ["ESNext", "DOM", "DOM.Iterable"],
6
+ "noUncheckedIndexedAccess": false,
7
+ "plugins": [
8
+ {
9
+ "name": "@effect/language-service",
10
+ "transform": "@effect/language-service/transform",
11
+ "namespaceImportPackages": ["effect", "@effect/*"]
12
+ }
13
+ ]
14
+ }
15
+ }