@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.
- package/AGENTS.md +321 -0
- package/DESIGN.md +1114 -0
- package/README.md +131 -0
- package/example/call-sites.md +591 -0
- package/example/tutorial.ts +255 -0
- package/package.json +51 -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 +33 -0
- package/src/llm.ts +186 -0
- package/src/protocols/anthropic-messages.ts +855 -0
- package/src/protocols/bedrock-converse.ts +674 -0
- package/src/protocols/bedrock-event-stream.ts +87 -0
- package/src/protocols/gemini.ts +512 -0
- package/src/protocols/index.ts +6 -0
- package/src/protocols/openai-chat.ts +506 -0
- package/src/protocols/openai-compatible-chat.ts +24 -0
- package/src/protocols/openai-responses.ts +1022 -0
- package/src/protocols/shared.ts +326 -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 +90 -0
- package/src/protocols/utils/cache.ts +16 -0
- package/src/protocols/utils/gemini-tool-schema.ts +99 -0
- package/src/protocols/utils/lifecycle.ts +102 -0
- package/src/protocols/utils/openai-options.ts +93 -0
- package/src/protocols/utils/tool-schema.ts +86 -0
- package/src/protocols/utils/tool-stream.ts +218 -0
- package/src/provider-error.ts +43 -0
- package/src/provider.ts +36 -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 +69 -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 +83 -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 +436 -0
- package/src/route/endpoint.ts +53 -0
- package/src/route/executor.ts +385 -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 +155 -0
- package/src/route/transport/index.ts +33 -0
- package/src/route/transport/websocket.ts +280 -0
- package/src/schema/errors.ts +207 -0
- package/src/schema/events.ts +618 -0
- package/src/schema/ids.ts +43 -0
- package/src/schema/index.ts +5 -0
- package/src/schema/messages.ts +312 -0
- package/src/schema/options.ts +276 -0
- package/src/tool-runtime.ts +78 -0
- package/src/tool.ts +253 -0
- package/src/utils/record.ts +3 -0
- package/sst-env.d.ts +10 -0
- package/test/adapter.test.ts +171 -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 +458 -0
- package/test/exports.test.ts +76 -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 +199 -0
- package/test/prepare.test.ts +178 -0
- package/test/provider/anthropic-messages-cache.recorded.test.ts +53 -0
- package/test/provider/anthropic-messages.recorded.test.ts +45 -0
- package/test/provider/anthropic-messages.test.ts +895 -0
- package/test/provider/bedrock-converse-cache.recorded.test.ts +54 -0
- package/test/provider/bedrock-converse.test.ts +744 -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 +584 -0
- package/test/provider/golden.recorded.test.ts +223 -0
- package/test/provider/openai-chat.test.ts +674 -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 +1472 -0
- package/test/provider/openrouter.test.ts +56 -0
- package/test/provider-error.test.ts +30 -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 +94 -0
- package/test/recorded-utils.ts +56 -0
- package/test/recorded-websocket.ts +26 -0
- package/test/response.test.ts +98 -0
- package/test/route.test.ts +43 -0
- package/test/schema.test.ts +86 -0
- package/test/tool-runtime.test.ts +818 -0
- package/test/tool-schema-projection.test.ts +117 -0
- package/test/tool-stream.test.ts +99 -0
- package/test/tool.types.ts +40 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HttpRecorderInternal } from "@neurocode-ai/http-recorder/internal"
|
|
2
|
+
import { Effect, Layer } from "effect"
|
|
3
|
+
import { WebSocketExecutor } from "../src/route"
|
|
4
|
+
import type { Service as WebSocketExecutorService } from "../src/route/transport/websocket"
|
|
5
|
+
|
|
6
|
+
const liveWebSocket = WebSocketExecutor.open
|
|
7
|
+
|
|
8
|
+
export const webSocketCassetteLayer = (
|
|
9
|
+
cassette: string,
|
|
10
|
+
input: { readonly metadata?: Record<string, unknown>; readonly mode: HttpRecorderInternal.RecordReplayMode },
|
|
11
|
+
): Layer.Layer<WebSocketExecutorService, never, HttpRecorderInternal.Cassette.Service> =>
|
|
12
|
+
Layer.effect(
|
|
13
|
+
WebSocketExecutor.Service,
|
|
14
|
+
Effect.gen(function* () {
|
|
15
|
+
const cassetteService = yield* HttpRecorderInternal.Cassette.Service
|
|
16
|
+
const executor = yield* HttpRecorderInternal.makeWebSocketExecutor({
|
|
17
|
+
name: cassette,
|
|
18
|
+
mode: input.mode,
|
|
19
|
+
metadata: input.metadata,
|
|
20
|
+
cassette: cassetteService,
|
|
21
|
+
live: { open: liveWebSocket },
|
|
22
|
+
compareClientMessagesAsJson: true,
|
|
23
|
+
})
|
|
24
|
+
return WebSocketExecutor.Service.of(executor)
|
|
25
|
+
}),
|
|
26
|
+
)
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { LLMEvent, LLMResponse } from "../src"
|
|
3
|
+
|
|
4
|
+
const reduce = (events: ReadonlyArray<LLMEvent>) => events.reduce(LLMResponse.reduce, LLMResponse.empty())
|
|
5
|
+
const finishEvents = (events: ReadonlyArray<LLMEvent>) => events.filter(LLMEvent.is.finish)
|
|
6
|
+
|
|
7
|
+
describe("LLMResponse reducer", () => {
|
|
8
|
+
test("assembles interleaved reasoning and text with end metadata", () => {
|
|
9
|
+
const events = [
|
|
10
|
+
LLMEvent.reasoningStart({ id: "r1" }),
|
|
11
|
+
LLMEvent.reasoningDelta({ id: "r1", text: "I should " }),
|
|
12
|
+
LLMEvent.textStart({ id: "t1" }),
|
|
13
|
+
LLMEvent.reasoningDelta({ id: "r1", text: "compare..." }),
|
|
14
|
+
LLMEvent.reasoningEnd({ id: "r1", providerMetadata: { anthropic: { signature: "sig" } } }),
|
|
15
|
+
LLMEvent.textDelta({ id: "t1", text: "Answer" }),
|
|
16
|
+
LLMEvent.textEnd({ id: "t1" }),
|
|
17
|
+
LLMEvent.finish({ reason: "stop", usage: { outputTokens: 5 } }),
|
|
18
|
+
]
|
|
19
|
+
const response = LLMResponse.fromEvents(events)
|
|
20
|
+
|
|
21
|
+
expect(response?.finishReason).toBe("stop")
|
|
22
|
+
expect(response?.usage).toMatchObject({ outputTokens: 5 })
|
|
23
|
+
expect(response?.events).toEqual(events)
|
|
24
|
+
expect(response?.events.map((event) => event.type)).toEqual([
|
|
25
|
+
"reasoning-start",
|
|
26
|
+
"reasoning-delta",
|
|
27
|
+
"text-start",
|
|
28
|
+
"reasoning-delta",
|
|
29
|
+
"reasoning-end",
|
|
30
|
+
"text-delta",
|
|
31
|
+
"text-end",
|
|
32
|
+
"finish",
|
|
33
|
+
])
|
|
34
|
+
expect(finishEvents(response?.events ?? [])).toHaveLength(1)
|
|
35
|
+
expect(response?.message.content).toEqual([
|
|
36
|
+
{
|
|
37
|
+
type: "reasoning",
|
|
38
|
+
text: "I should compare...",
|
|
39
|
+
providerMetadata: { anthropic: { signature: "sig" } },
|
|
40
|
+
},
|
|
41
|
+
{ type: "text", text: "Answer" },
|
|
42
|
+
])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test("preserves partial content without completing a failed stream", () => {
|
|
46
|
+
const state = reduce([LLMEvent.textStart({ id: "t1" }), LLMEvent.textDelta({ id: "t1", text: "partial" })])
|
|
47
|
+
|
|
48
|
+
expect(LLMResponse.complete(state)).toBeUndefined()
|
|
49
|
+
expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test("does not complete ended content without a terminal finish", () => {
|
|
53
|
+
const state = reduce([
|
|
54
|
+
LLMEvent.textStart({ id: "t1" }),
|
|
55
|
+
LLMEvent.textDelta({ id: "t1", text: "partial" }),
|
|
56
|
+
LLMEvent.textEnd({ id: "t1" }),
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
expect(LLMResponse.complete(state)).toBeUndefined()
|
|
60
|
+
expect(state.message.content).toEqual([{ type: "text", text: "partial" }])
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test("uses terminal usage when present and keeps prior usage when finish omits it", () => {
|
|
64
|
+
const withFinishUsage = LLMResponse.fromEvents([
|
|
65
|
+
LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
|
|
66
|
+
LLMEvent.finish({ reason: "stop", usage: { outputTokens: 2 } }),
|
|
67
|
+
])
|
|
68
|
+
const withoutFinishUsage = LLMResponse.fromEvents([
|
|
69
|
+
LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 3 } }),
|
|
70
|
+
LLMEvent.finish({ reason: "stop" }),
|
|
71
|
+
])
|
|
72
|
+
|
|
73
|
+
expect(withFinishUsage?.usage).toMatchObject({ outputTokens: 2 })
|
|
74
|
+
expect(withoutFinishUsage?.usage).toMatchObject({ inputTokens: 3 })
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test("assembles tool-call content only after the completed tool call event", () => {
|
|
78
|
+
const pending = reduce([
|
|
79
|
+
LLMEvent.toolInputStart({ id: "call_1", name: "lookup" }),
|
|
80
|
+
LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: '{"query"' }),
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
expect(pending.message.content).toEqual([])
|
|
84
|
+
expect(pending.toolInputs.call_1?.text).toBe('{"query"')
|
|
85
|
+
|
|
86
|
+
const response = LLMResponse.fromEvents([
|
|
87
|
+
...pending.events,
|
|
88
|
+
LLMEvent.toolInputDelta({ id: "call_1", name: "lookup", text: ':"weather"}' }),
|
|
89
|
+
LLMEvent.toolInputEnd({ id: "call_1", name: "lookup" }),
|
|
90
|
+
LLMEvent.toolCall({ id: "call_1", name: "lookup", input: { query: "weather" } }),
|
|
91
|
+
LLMEvent.finish({ reason: "tool-calls" }),
|
|
92
|
+
])
|
|
93
|
+
|
|
94
|
+
expect(response?.message.content).toEqual([
|
|
95
|
+
{ type: "tool-call", id: "call_1", name: "lookup", input: { query: "weather" } },
|
|
96
|
+
])
|
|
97
|
+
})
|
|
98
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import * as OpenAIChat from "../src/protocols/openai-chat"
|
|
3
|
+
import { Auth } from "../src/route"
|
|
4
|
+
|
|
5
|
+
describe("Route.with", () => {
|
|
6
|
+
test("merges endpoint query and header defaults while replacing auth and id", () => {
|
|
7
|
+
const auth = Auth.headers({ "x-auth": "new" })
|
|
8
|
+
const route = OpenAIChat.route
|
|
9
|
+
.with({
|
|
10
|
+
id: "base-chat",
|
|
11
|
+
endpoint: {
|
|
12
|
+
baseURL: "https://api.example.test/v1",
|
|
13
|
+
query: { keep: "base", base: "1" },
|
|
14
|
+
},
|
|
15
|
+
headers: { "x-base": "base", "x-override": "base" },
|
|
16
|
+
auth: Auth.headers({ "x-auth": "old" }),
|
|
17
|
+
})
|
|
18
|
+
.with({
|
|
19
|
+
id: "patched-chat",
|
|
20
|
+
endpoint: { query: { keep: "patch", patch: "1" } },
|
|
21
|
+
headers: { "x-override": "patch", "x-patch": "patch" },
|
|
22
|
+
auth,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
expect(route.id).toBe("patched-chat")
|
|
26
|
+
expect(route.auth).toBe(auth)
|
|
27
|
+
expect(route.endpoint).toMatchObject({
|
|
28
|
+
baseURL: "https://api.example.test/v1",
|
|
29
|
+
path: "/chat/completions",
|
|
30
|
+
query: { keep: "patch", base: "1", patch: "1" },
|
|
31
|
+
})
|
|
32
|
+
expect(route.defaults.headers).toEqual({
|
|
33
|
+
"x-base": "base",
|
|
34
|
+
"x-override": "patch",
|
|
35
|
+
"x-patch": "patch",
|
|
36
|
+
})
|
|
37
|
+
expect(route.defaults.http?.headers).toEqual({
|
|
38
|
+
"x-base": "base",
|
|
39
|
+
"x-override": "patch",
|
|
40
|
+
"x-patch": "patch",
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { Schema } from "effect"
|
|
3
|
+
import * as OpenAIChat from "../src/protocols/openai-chat"
|
|
4
|
+
import * as OpenAIResponses from "../src/protocols/openai-responses"
|
|
5
|
+
import { ContentPart, LLMEvent, LLMRequest, Model, ModelID, ProviderID, Usage } from "../src/schema"
|
|
6
|
+
import { ProviderShared } from "../src/protocols/shared"
|
|
7
|
+
|
|
8
|
+
const model = new Model({
|
|
9
|
+
id: ModelID.make("fake-model"),
|
|
10
|
+
provider: ProviderID.make("fake-provider"),
|
|
11
|
+
route: OpenAIChat.route,
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const decodeLLMRequest = Schema.decodeUnknownSync(LLMRequest as unknown as Schema.Decoder<LLMRequest>)
|
|
15
|
+
const decodeLLMEvent = Schema.decodeUnknownSync(LLMEvent as unknown as Schema.Decoder<LLMEvent>)
|
|
16
|
+
|
|
17
|
+
describe("llm schema", () => {
|
|
18
|
+
test("decodes a minimal request", () => {
|
|
19
|
+
const input: unknown = {
|
|
20
|
+
id: "req_1",
|
|
21
|
+
model,
|
|
22
|
+
system: [{ type: "text", text: "You are terse." }],
|
|
23
|
+
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
|
|
24
|
+
tools: [],
|
|
25
|
+
generation: {},
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const decoded = decodeLLMRequest(input)
|
|
29
|
+
|
|
30
|
+
expect(decoded.id).toBe("req_1")
|
|
31
|
+
expect(decoded.messages[0]?.content[0]?.type).toBe("text")
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test("accepts custom route ids", () => {
|
|
35
|
+
const decoded = decodeLLMRequest({
|
|
36
|
+
model: Model.update(model, { route: OpenAIResponses.route }),
|
|
37
|
+
system: [],
|
|
38
|
+
messages: [],
|
|
39
|
+
tools: [],
|
|
40
|
+
generation: {},
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
expect(decoded.model.route.id).toBe("openai-responses")
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test("rejects invalid event type", () => {
|
|
47
|
+
expect(() => decodeLLMEvent({ type: "bogus" })).toThrow()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test("finish constructors accept usage input", () => {
|
|
51
|
+
expect(LLMEvent.stepFinish({ index: 0, reason: "stop", usage: { inputTokens: 1 } }).usage).toBeInstanceOf(Usage)
|
|
52
|
+
expect(LLMEvent.finish({ reason: "stop", usage: { outputTokens: 2 } }).usage).toBeInstanceOf(Usage)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("content part tagged union exposes guards", () => {
|
|
56
|
+
expect(ContentPart.guards.text({ type: "text", text: "hi" })).toBe(true)
|
|
57
|
+
expect(ContentPart.guards.media({ type: "text", text: "hi" })).toBe(false)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe("LLM.Usage", () => {
|
|
62
|
+
test("subtractTokens clamps non-sensical breakdowns to zero", () => {
|
|
63
|
+
// Defense against a provider reporting cached_tokens > prompt_tokens or
|
|
64
|
+
// reasoning_tokens > completion_tokens — the negative would otherwise
|
|
65
|
+
// round-trip through the pipeline and crash strict downstream schemas.
|
|
66
|
+
expect(ProviderShared.subtractTokens(5, 3)).toBe(2)
|
|
67
|
+
expect(ProviderShared.subtractTokens(5, 10)).toBe(0)
|
|
68
|
+
expect(ProviderShared.subtractTokens(5, undefined)).toBe(5)
|
|
69
|
+
expect(ProviderShared.subtractTokens(undefined, 3)).toBeUndefined()
|
|
70
|
+
expect(ProviderShared.subtractTokens(undefined, undefined)).toBeUndefined()
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test("sumTokens returns undefined only when every input is undefined", () => {
|
|
74
|
+
expect(ProviderShared.sumTokens(1, 2, 3)).toBe(6)
|
|
75
|
+
expect(ProviderShared.sumTokens(1, undefined, 3)).toBe(4)
|
|
76
|
+
expect(ProviderShared.sumTokens(undefined, undefined, undefined)).toBeUndefined()
|
|
77
|
+
expect(ProviderShared.sumTokens()).toBeUndefined()
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test("visibleOutputTokens clamps reasoning > output to zero", () => {
|
|
81
|
+
expect(new Usage({ outputTokens: 10, reasoningTokens: 4 }).visibleOutputTokens).toBe(6)
|
|
82
|
+
expect(new Usage({ outputTokens: 10 }).visibleOutputTokens).toBe(10)
|
|
83
|
+
expect(new Usage({ outputTokens: 4, reasoningTokens: 10 }).visibleOutputTokens).toBe(0)
|
|
84
|
+
expect(new Usage({}).visibleOutputTokens).toBe(0)
|
|
85
|
+
})
|
|
86
|
+
})
|