The additive contract delivers value at the mapper boundary — every field is non-overlapping and non-negative, so any caller summing arbitrary subsets is correct by construction. Two-line helpers that just sum three or two known fields add API surface without paying for themselves, and there are no in-tree consumers today. If v2 wants them at integration time, the right place is a getter on the `Schema.Class` (matching the `LLMResponse.text` / `reasoning` / `toolCalls` pattern in the same file), not a static namespace helper.
64 lines
2.2 KiB
TypeScript
64 lines
2.2 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { Schema } from "effect"
|
|
import { ContentPart, LLMEvent, LLMRequest, ModelID, ModelLimits, ModelRef, ProviderID } from "../src/schema"
|
|
import { ProviderShared } from "../src/protocols/shared"
|
|
|
|
const model = new ModelRef({
|
|
id: ModelID.make("fake-model"),
|
|
provider: ProviderID.make("fake-provider"),
|
|
route: "openai-chat",
|
|
baseURL: "https://fake.local",
|
|
limits: new ModelLimits({}),
|
|
})
|
|
|
|
describe("llm schema", () => {
|
|
test("decodes a minimal request", () => {
|
|
const input: unknown = {
|
|
id: "req_1",
|
|
model,
|
|
system: [{ type: "text", text: "You are terse." }],
|
|
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
|
|
tools: [],
|
|
generation: {},
|
|
}
|
|
|
|
const decoded = Schema.decodeUnknownSync(LLMRequest)(input)
|
|
|
|
expect(decoded.id).toBe("req_1")
|
|
expect(decoded.messages[0]?.content[0]?.type).toBe("text")
|
|
})
|
|
|
|
test("accepts custom route ids", () => {
|
|
const decoded = Schema.decodeUnknownSync(LLMRequest)({
|
|
model: { ...model, route: "custom-route" },
|
|
system: [],
|
|
messages: [],
|
|
tools: [],
|
|
generation: {},
|
|
})
|
|
|
|
expect(decoded.model.route).toBe("custom-route")
|
|
})
|
|
|
|
test("rejects invalid event type", () => {
|
|
expect(() => Schema.decodeUnknownSync(LLMEvent)({ type: "bogus" })).toThrow()
|
|
})
|
|
|
|
test("content part tagged union exposes guards", () => {
|
|
expect(ContentPart.guards.text({ type: "text", text: "hi" })).toBe(true)
|
|
expect(ContentPart.guards.media({ type: "text", text: "hi" })).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe("LLM.Usage additive contract", () => {
|
|
test("subtractTokens clamps non-sensical breakdowns to zero", () => {
|
|
// Defense against a provider reporting cached_tokens > prompt_tokens or
|
|
// reasoning_tokens > completion_tokens — the negative would otherwise
|
|
// round-trip through the pipeline and crash strict downstream schemas.
|
|
expect(ProviderShared.subtractTokens(5, 3)).toBe(2)
|
|
expect(ProviderShared.subtractTokens(5, 10)).toBe(0)
|
|
expect(ProviderShared.subtractTokens(5, undefined)).toBe(5)
|
|
expect(ProviderShared.subtractTokens(undefined, 3)).toBeUndefined()
|
|
expect(ProviderShared.subtractTokens(undefined, undefined)).toBeUndefined()
|
|
})
|
|
})
|