| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476 |
- import { describe, expect } from "bun:test"
- import { Effect } from "effect"
- import { HttpClientRequest } from "effect/unstable/http"
- import { CacheHint, LLM, AIError, LLMRequest, Message, ToolCallPart, ToolDefinition, Usage } from "../../src/index.js"
- import { Auth, LLMClient } from "../../src/route.js"
- import { compileRequest } from "../../src/route/client.js"
- import * as AnthropicMessages from "../../src/protocols/anthropic-messages.js"
- import { continuationRequest, nativeAnthropicMessagesContinuation } from "../continuation-scenarios.js"
- import { it } from "../lib/effect.js"
- import { dynamicResponse, fixedResponse } from "../lib/http.js"
- import { sseEvents } from "../lib/sse.js"
- const model = AnthropicMessages.route
- .with({ endpoint: { baseURL: "https://api.anthropic.test/v1/" }, auth: Auth.header("x-api-key", "test") })
- .model({ id: "claude-sonnet-4-5" })
- const opus48 = AnthropicMessages.route
- .with({ endpoint: { baseURL: "https://api.anthropic.test/v1/" }, auth: Auth.header("x-api-key", "test") })
- .model({ id: "claude-opus-4-8" })
- const request = LLM.request({
- id: "req_1",
- model,
- system: { type: "text", text: "You are concise.", cache: new CacheHint({ type: "ephemeral" }) },
- prompt: "Say hello.",
- // This fixture predates the `cache: "auto"` default; pin the policy off so
- // existing wire-shape assertions only see the manual hint on the system part.
- cache: "none",
- generation: { maxTokens: 20, temperature: 0 },
- })
- type AnthropicToolResult = Extract<
- AnthropicMessages.AnthropicMessagesBody["messages"][number]["content"][number],
- { readonly type: "tool_result" }
- >
- const expectToolResult = (body: AnthropicMessages.AnthropicMessagesBody): AnthropicToolResult => {
- const result = body.messages
- .flatMap((message) => (message.role === "user" ? message.content : []))
- .find((block): block is AnthropicToolResult => block.type === "tool_result")
- expect(result).toBeDefined()
- return result!
- }
- describe("Anthropic Messages route", () => {
- it.effect("prepares Anthropic Messages target", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(request)
- expect(prepared.body).toEqual({
- model: "claude-sonnet-4-5",
- system: [{ type: "text", text: "You are concise.", cache_control: { type: "ephemeral" } }],
- messages: [{ role: "user", content: [{ type: "text", text: "Say hello." }] }],
- stream: true,
- max_tokens: 20,
- temperature: 0,
- })
- }),
- )
- it.effect("lowers adaptive thinking settings with effort", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLMRequest.update(request, {
- providerOptions: {
- anthropic: { thinking: { type: "adaptive", display: "summarized" }, effort: "low" },
- },
- }),
- )
- expect(prepared.body).toMatchObject({
- thinking: { type: "adaptive", display: "summarized" },
- output_config: { effort: "low" },
- })
- }),
- )
- it.effect("normalizes enabled and disabled thinking settings", () =>
- Effect.gen(function* () {
- const enabled = yield* compileRequest(
- LLMRequest.update(request, {
- providerOptions: { anthropic: { thinking: { type: "enabled", budgetTokens: 1_024 } } },
- }),
- )
- const legacy = yield* compileRequest(
- LLMRequest.update(request, {
- providerOptions: { anthropic: { thinking: { type: "enabled", budget_tokens: 2_048 } } },
- }),
- )
- const disabled = yield* compileRequest(
- LLMRequest.update(request, {
- providerOptions: { anthropic: { thinking: { type: "disabled" } } },
- }),
- )
- expect(enabled.body.thinking).toEqual({ type: "enabled", budget_tokens: 1_024 })
- expect(legacy.body.thinking).toEqual({ type: "enabled", budget_tokens: 2_048 })
- expect(disabled.body.thinking).toEqual({ type: "disabled" })
- }),
- )
- it.effect("rejects enabled thinking without a budget", () =>
- Effect.gen(function* () {
- const error = yield* compileRequest(
- LLMRequest.update(request, {
- providerOptions: { anthropic: { thinking: { type: "enabled" } } },
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("Anthropic thinking provider option requires budgetTokens")
- }),
- )
- it.effect("lowers chronological system updates natively for Claude Opus 4.8 with cache hints", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model: opus48,
- messages: [
- Message.user("Before."),
- Message.system([{ type: "text", text: "Operator update.", cache: new CacheHint({ type: "ephemeral" }) }]),
- Message.assistant("After."),
- ],
- cache: "none",
- }),
- )
- expect(prepared.body.messages).toEqual([
- { role: "user", content: [{ type: "text", text: "Before." }] },
- {
- role: "system",
- content: [{ type: "text", text: "Operator update.", cache_control: { type: "ephemeral" } }],
- },
- { role: "assistant", content: [{ type: "text", text: "After." }] },
- ])
- }),
- )
- it.effect("lowers chronological system updates to wrapped user text for unsupported Anthropic models", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- messages: [
- Message.user("Before."),
- Message.system("Treat </system-update> literally."),
- Message.assistant("After."),
- ],
- cache: "none",
- }),
- )
- expect(prepared.body.messages).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Before." },
- { type: "text", text: "<system-update>\nTreat </system-update> literally.\n</system-update>" },
- ],
- },
- { role: "assistant", content: [{ type: "text", text: "After." }] },
- ])
- }),
- )
- it.effect("rejects non-text chronological system update content before send", () =>
- Effect.gen(function* () {
- const error = yield* compileRequest(
- LLM.request({
- model: opus48,
- messages: [
- Message.user("Before."),
- Message.make({ role: "system", content: { type: "media", mediaType: "image/png", data: "AAECAw==" } }),
- ],
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("Anthropic Messages system messages only support text content for now")
- }),
- )
- it.effect("falls back for unsupported native chronological system update placement", () =>
- Effect.gen(function* () {
- expect(
- (yield* compileRequest(
- LLM.request({
- model: opus48,
- messages: [Message.assistant("Plain."), Message.system("After plain assistant.")],
- cache: "none",
- }),
- )).body.messages,
- ).toEqual([
- { role: "assistant", content: [{ type: "text", text: "Plain." }] },
- {
- role: "user",
- content: [{ type: "text", text: "<system-update>\nAfter plain assistant.\n</system-update>" }],
- },
- ])
- expect(
- (yield* compileRequest(LLM.request({ model: opus48, messages: [Message.system("First.")], cache: "none" })))
- .body.messages,
- ).toEqual([{ role: "user", content: [{ type: "text", text: "<system-update>\nFirst.\n</system-update>" }] }])
- expect(
- (yield* compileRequest(
- LLM.request({
- model: opus48,
- messages: [Message.user("Before."), Message.system("One."), Message.system("Two.")],
- cache: "none",
- }),
- )).body.messages,
- ).toEqual([
- {
- role: "user",
- content: [
- { type: "text", text: "Before." },
- { type: "text", text: "<system-update>\nOne.\n</system-update>" },
- { type: "text", text: "<system-update>\nTwo.\n</system-update>" },
- ],
- },
- ])
- }),
- )
- it.effect("rejects a system update between a local tool call and its result", () =>
- Effect.gen(function* () {
- const error = yield* compileRequest(
- LLM.request({
- model: opus48,
- messages: [
- Message.user("Use the tool."),
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: {} })]),
- Message.system("Too early."),
- Message.tool({ id: "call_1", name: "lookup", result: "Done." }),
- ],
- cache: "none",
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("system updates cannot split a local tool call from its tool result")
- }),
- )
- it.effect("prepares tool call and tool result messages", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- id: "req_tool_result",
- model,
- messages: [
- Message.user("What is the weather?"),
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
- Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
- ],
- cache: "none",
- }),
- )
- expect(prepared.body).toEqual({
- model: "claude-sonnet-4-5",
- messages: [
- { role: "user", content: [{ type: "text", text: "What is the weather?" }] },
- {
- role: "assistant",
- content: [{ type: "tool_use", id: "call_1", name: "lookup", input: { query: "weather" } }],
- },
- { role: "user", content: [{ type: "tool_result", tool_use_id: "call_1", content: '{"forecast":"sunny"}' }] },
- ],
- stream: true,
- max_tokens: 4096,
- })
- }),
- )
- it.effect("batches parallel tool results into one Anthropic user message", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- messages: [
- Message.user("Check both cities."),
- Message.assistant([
- { type: "text", text: "I'll check both." },
- ToolCallPart.make({ id: "call_paris", name: "weather", input: { city: "Paris" } }),
- ToolCallPart.make({ id: "call_london", name: "weather", input: { city: "London" } }),
- ]),
- Message.tool({ id: "call_paris", name: "weather", result: { temperature: 22 } }),
- Message.tool({ id: "call_london", name: "weather", result: { temperature: 18 } }),
- ],
- cache: "none",
- }),
- )
- expect(prepared.body.messages).toMatchObject([
- { role: "user", content: [{ type: "text", text: "Check both cities." }] },
- {
- role: "assistant",
- content: [
- { type: "text", text: "I'll check both." },
- { type: "tool_use", id: "call_paris", name: "weather", input: { city: "Paris" } },
- { type: "tool_use", id: "call_london", name: "weather", input: { city: "London" } },
- ],
- },
- {
- role: "user",
- content: [
- { type: "tool_result", tool_use_id: "call_paris", content: '{"temperature":22}' },
- { type: "tool_result", tool_use_id: "call_london", content: '{"temperature":18}' },
- ],
- },
- ])
- expect(prepared.body.messages).toHaveLength(3)
- }),
- )
- it.effect("keeps tools and sends tool_choice none", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- id: "req_tool_choice_none",
- model,
- tools: [{ name: "lookup", description: "Look things up", inputSchema: { type: "object", properties: {} } }],
- messages: [
- Message.user("What is the weather?"),
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: { query: "weather" } })]),
- Message.tool({ id: "call_1", name: "lookup", result: { forecast: "sunny" } }),
- ],
- toolChoice: "none",
- cache: "none",
- }),
- )
- expect(prepared.body.tools).toEqual([
- {
- name: "lookup",
- description: "Look things up",
- input_schema: { type: "object", properties: {} },
- },
- ])
- expect(prepared.body.tool_choice).toEqual({ type: "none" })
- }),
- )
- // Regression: read tool results must stay structured so base64 media data is
- // not JSON-stringified into `tool_result.content`.
- it.effect("lowers media tool-result content as structured blocks", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- id: "req_tool_result_image",
- model,
- messages: [
- Message.user("Show me the screenshot."),
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "read", input: { filePath: "shot.png" } })]),
- Message.tool({
- id: "call_1",
- name: "read",
- resultType: "content",
- result: [
- { type: "text", text: "Image read successfully" },
- { type: "file", uri: "data:image/png;base64,AAECAw==", mime: "image/png" },
- { type: "file", uri: "data:application/pdf;base64,JVBERi0xLjQ=", mime: "application/pdf" },
- ],
- }),
- ],
- cache: "none",
- }),
- )
- expect(expectToolResult(prepared.body).content).toEqual([
- { type: "text", text: "Image read successfully" },
- { type: "image", source: { type: "base64", media_type: "image/png", data: "AAECAw==" } },
- { type: "document", source: { type: "base64", media_type: "application/pdf", data: "JVBERi0xLjQ=" } },
- ])
- }),
- )
- it.effect("lowers single-image tool-result content as a structured image block", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- id: "req_tool_result_image_only",
- model,
- messages: [
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "screenshot", input: {} })]),
- Message.tool({
- id: "call_1",
- name: "screenshot",
- resultType: "content",
- result: [{ type: "file", uri: "data:image/jpeg;base64,/9j/AA==", mime: "image/jpeg" }],
- }),
- ],
- cache: "none",
- }),
- )
- expect(expectToolResult(prepared.body).content).toEqual([
- { type: "image", source: { type: "base64", media_type: "image/jpeg", data: "/9j/AA==" } },
- ])
- }),
- )
- it.effect("rejects unsupported media in tool-result content with a clear error", () =>
- Effect.gen(function* () {
- const error = yield* compileRequest(
- LLM.request({
- id: "req_tool_result_unsupported_media",
- model,
- messages: [
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "fetch", input: {} })]),
- Message.tool({
- id: "call_1",
- name: "fetch",
- resultType: "content",
- result: [{ type: "file", uri: "data:audio/mpeg;base64,AAECAw==", mime: "audio/mpeg" }],
- }),
- ],
- cache: "none",
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("Anthropic Messages")
- expect(error.message).toContain("audio/mpeg")
- }),
- )
- it.effect("prepares the composed native continuation request", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- continuationRequest({
- id: "req_native_continuation_anthropic",
- model,
- features: nativeAnthropicMessagesContinuation,
- }),
- )
- expect(prepared.body).toMatchObject({
- system: [{ type: "text", text: "You are concise. Continue from the provided history." }],
- messages: [
- {
- role: "user",
- content: [
- { type: "text", text: "What is shown here?" },
- { type: "image", source: { type: "base64", media_type: "image/png", data: "AAECAw==" } },
- ],
- },
- {
- role: "assistant",
- content: [
- { type: "thinking", thinking: "I inspected the previous turn.", signature: "sig_continuation_1" },
- { type: "text", text: "It shows a small test image." },
- ],
- },
- { role: "user", content: [{ type: "text", text: "Check the weather in Paris before continuing." }] },
- {
- role: "assistant",
- content: [{ type: "tool_use", id: "call_weather_1", name: "get_weather", input: { city: "Paris" } }],
- },
- {
- role: "user",
- content: [{ type: "tool_result", tool_use_id: "call_weather_1", content: '{"temperature":22}' }],
- },
- { role: "assistant", content: [{ type: "text", text: "Paris is 22 degrees." }] },
- { role: "user", content: [{ type: "text", text: "Continue from this conversation in one short sentence." }] },
- ],
- })
- expect(prepared.body.tools).toEqual([expect.objectContaining({ name: "get_weather" })])
- }),
- )
- it.effect("lowers preserved Anthropic reasoning signature metadata", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- messages: [
- Message.assistant([
- { type: "reasoning", text: "thinking", providerMetadata: { anthropic: { signature: "sig_1" } } },
- ]),
- ],
- }),
- )
- expect(prepared.body).toMatchObject({
- messages: [{ role: "assistant", content: [{ type: "thinking", thinking: "thinking", signature: "sig_1" }] }],
- })
- }),
- )
- it.effect("round-trips redacted thinking as redacted_thinking blocks", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- messages: [
- Message.assistant([
- { type: "reasoning", text: "", providerMetadata: { anthropic: { redactedData: "opaque_1" } } },
- { type: "reasoning", text: "visible", providerMetadata: { anthropic: { signature: "sig_1" } } },
- ]),
- ],
- }),
- )
- expect(prepared.body).toMatchObject({
- messages: [
- {
- role: "assistant",
- content: [
- { type: "redacted_thinking", data: "opaque_1" },
- { type: "thinking", thinking: "visible", signature: "sig_1" },
- ],
- },
- ],
- })
- }),
- )
- it.effect("parses text, reasoning, and usage stream fixtures", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5, cache_read_input_tokens: 1 } } },
- { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
- { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } },
- { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "!" } },
- { type: "content_block_stop", index: 0 },
- { type: "content_block_start", index: 1, content_block: { type: "thinking", thinking: "" } },
- { type: "content_block_delta", index: 1, delta: { type: "thinking_delta", thinking: "thinking" } },
- { type: "content_block_delta", index: 1, delta: { type: "signature_delta", signature: "sig_1" } },
- { type: "content_block_stop", index: 1 },
- {
- type: "message_delta",
- delta: { stop_reason: "end_turn", stop_sequence: "\n\nHuman:" },
- usage: { output_tokens: 2 },
- },
- { type: "message_stop" },
- )
- const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
- expect(response.text).toBe("Hello!")
- expect(response.reasoning).toBe("thinking")
- expect(response.usage).toMatchObject({
- inputTokens: 6,
- outputTokens: 2,
- nonCachedInputTokens: 5,
- cacheReadInputTokens: 1,
- totalTokens: 8,
- })
- expect(response.events.find((event) => event.type === "reasoning-end")).toMatchObject({
- providerMetadata: { anthropic: { signature: "sig_1" } },
- })
- expect(response.events.find((event) => event.type === "reasoning-delta" && event.text === "")).toBeUndefined()
- expect(response.message.content).toEqual([
- { type: "text", text: "Hello!" },
- { type: "reasoning", text: "thinking", providerMetadata: { anthropic: { signature: "sig_1" } } },
- ])
- expect(response.events.at(-1)).toMatchObject({
- type: "finish",
- reason: { normalized: "stop", raw: "end_turn" },
- providerMetadata: { anthropic: { stopSequence: "\n\nHuman:" } },
- })
- }),
- )
- it.effect("requires message_stop before completing a streamed message", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
- { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
- ),
- ),
- ),
- Effect.flip,
- )
- expect(error.reason).toMatchObject({
- _tag: "InvalidProviderOutput",
- classification: "incomplete-stream",
- message: "The provider response ended unexpectedly.",
- })
- }),
- )
- it.effect("maps thinking tokens and preserves unknown Anthropic usage fields", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- {
- type: "message_start",
- message: {
- usage: {
- input_tokens: 5,
- cache_read_input_tokens: 2,
- service_tier: "standard",
- cache_creation: { ephemeral_5m_input_tokens: 1 },
- server_tool_use: { web_search_requests: 1, start_counter: 2 },
- output_tokens_details: { thinking_tokens: 3, start_detail: "preserved" },
- },
- },
- },
- {
- type: "message_delta",
- delta: { stop_reason: "end_turn" },
- usage: {
- output_tokens: 8,
- server_tool_use: { web_search_requests: 2, terminal_counter: 3 },
- output_tokens_details: { terminal_detail: "preserved" },
- future_terminal: { requests: 4 },
- },
- },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.usage).toMatchObject({
- inputTokens: 7,
- outputTokens: 8,
- reasoningTokens: 3,
- totalTokens: 15,
- providerMetadata: {
- anthropic: {
- input_tokens: 5,
- cache_read_input_tokens: 2,
- service_tier: "standard",
- cache_creation: { ephemeral_5m_input_tokens: 1 },
- server_tool_use: { web_search_requests: 2, start_counter: 2, terminal_counter: 3 },
- output_tokens: 8,
- output_tokens_details: {
- thinking_tokens: 3,
- start_detail: "preserved",
- terminal_detail: "preserved",
- },
- future_terminal: { requests: 4 },
- },
- },
- })
- }),
- )
- it.effect("round-trips omitted thinking carried only by a signature delta", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "thinking", thinking: "", signature: "" },
- },
- { type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: "sig_1" } },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.message.content).toEqual([
- { type: "reasoning", text: "", providerMetadata: { anthropic: { signature: "sig_1" } } },
- ])
- const prepared = yield* compileRequest(LLM.request({ model, messages: [response.message], cache: "none" }))
- expect(prepared.body.messages).toEqual([
- { role: "assistant", content: [{ type: "thinking", thinking: "", signature: "sig_1" }] },
- ])
- }),
- )
- it.effect("retains a thinking signature supplied in content_block_start", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "thinking", thinking: "", signature: "sig_1" },
- },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.message.content).toEqual([
- { type: "reasoning", text: "", providerMetadata: { anthropic: { signature: "sig_1" } } },
- ])
- expect(response.events.find((event) => event.type === "reasoning-end")).toMatchObject({
- providerMetadata: { anthropic: { signature: "sig_1" } },
- })
- }),
- )
- it.effect("retains complete tool input from content_block_start", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "tool_use", id: "call_1", name: "lookup", input: { query: "weather" } },
- },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.toolCalls).toMatchObject([{ id: "call_1", name: "lookup", input: { query: "weather" } }])
- }),
- )
- it.effect("retains empty text blocks", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.message.content).toEqual([{ type: "text", text: "" }])
- }),
- )
- it.effect("parses redacted thinking into empty reasoning with redactedData metadata", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- { type: "content_block_start", index: 0, content_block: { type: "redacted_thinking", data: "opaque_1" } },
- { type: "content_block_stop", index: 0 },
- { type: "content_block_start", index: 1, content_block: { type: "text", text: "" } },
- { type: "content_block_delta", index: 1, delta: { type: "text_delta", text: "Hello" } },
- { type: "content_block_stop", index: 1 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 2 } },
- { type: "message_stop" },
- )
- const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))
- expect(response.events.find((event) => event.type === "reasoning-start")).toMatchObject({
- providerMetadata: { anthropic: { redactedData: "opaque_1" } },
- })
- expect(response.message.content).toEqual([
- { type: "reasoning", text: "", providerMetadata: { anthropic: { redactedData: "opaque_1" } } },
- { type: "text", text: "Hello" },
- ])
- }),
- )
- it.effect("round-trips streamed redacted thinking with tool use into a continuation request", () =>
- Effect.gen(function* () {
- // Anthropic types `redacted_thinking.data` as an opaque string. Its
- // contents are provider-owned and must be replayed without inspection.
- const redactedData = "cmVkYWN0ZWQtdGhpbmtpbmc="
- const response = yield* LLMClient.generate(
- LLMRequest.update(request, {
- tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
- }),
- ).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "redacted_thinking", data: redactedData },
- },
- { type: "content_block_stop", index: 0 },
- {
- type: "content_block_start",
- index: 1,
- content_block: { type: "tool_use", id: "call_1", name: "lookup" },
- },
- {
- type: "content_block_delta",
- index: 1,
- delta: { type: "input_json_delta", partial_json: '{"query":"weather"}' },
- },
- { type: "content_block_stop", index: 1 },
- { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- messages: [
- Message.user("Say hello."),
- response.message,
- Message.tool({ id: "call_1", name: "lookup", result: "sunny", resultType: "text" }),
- ],
- tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
- cache: "none",
- }),
- )
- expect(prepared.body.messages).toEqual([
- { role: "user", content: [{ type: "text", text: "Say hello." }] },
- {
- role: "assistant",
- content: [
- { type: "redacted_thinking", data: redactedData },
- { type: "tool_use", id: "call_1", name: "lookup", input: { query: "weather" } },
- ],
- },
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- tool_use_id: "call_1",
- content: "sunny",
- is_error: undefined,
- cache_control: undefined,
- },
- ],
- },
- ])
- }),
- )
- it.effect("maps context-window truncation to length", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "message_delta",
- delta: { stop_reason: "model_context_window_exceeded" },
- usage: { output_tokens: 1 },
- },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.finishReason).toEqual({ normalized: "length", raw: "model_context_window_exceeded" })
- }),
- )
- it.effect("preserves pause_turn while normalizing it to stop", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- { type: "message_delta", delta: { stop_reason: "pause_turn" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.finishReason).toEqual({ normalized: "stop", raw: "pause_turn" })
- }),
- )
- it.effect("assembles streamed tool call input", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "call_1", name: "lookup" } },
- { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: '{"query"' } },
- { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: ':"weather"}' } },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- )
- const response = yield* LLMClient.generate(
- LLMRequest.update(request, {
- tools: [ToolDefinition.make({ name: "lookup", description: "Lookup data", inputSchema: { type: "object" } })],
- }),
- ).pipe(Effect.provide(fixedResponse(body)))
- const usage = new Usage({
- inputTokens: 5,
- outputTokens: 1,
- nonCachedInputTokens: 5,
- cacheReadInputTokens: undefined,
- cacheWriteInputTokens: undefined,
- totalTokens: 6,
- providerMetadata: { anthropic: { input_tokens: 5, output_tokens: 1 } },
- })
- expect(response.toolCalls).toEqual([
- {
- type: "tool-call",
- id: "call_1",
- name: "lookup",
- input: { query: "weather" },
- providerExecuted: undefined,
- providerMetadata: undefined,
- },
- ])
- expect(response.events).toEqual([
- { type: "step-start", index: 0 },
- { type: "tool-input-start", id: "call_1", name: "lookup" },
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: '{"query"' },
- { type: "tool-input-delta", id: "call_1", name: "lookup", text: ':"weather"}' },
- { type: "tool-input-end", id: "call_1", name: "lookup", providerMetadata: undefined },
- {
- type: "tool-call",
- id: "call_1",
- name: "lookup",
- input: { query: "weather" },
- providerExecuted: undefined,
- providerMetadata: undefined,
- },
- {
- type: "step-finish",
- index: 0,
- reason: { normalized: "tool-calls", raw: "tool_use" },
- usage,
- providerMetadata: undefined,
- },
- {
- type: "finish",
- reason: { normalized: "tool-calls", raw: "tool_use" },
- providerMetadata: undefined,
- usage,
- },
- ])
- }),
- )
- it.effect("assembles and persists multiple tool calls from one Anthropic response", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "tool_use", id: "call_paris", name: "weather", input: {} },
- },
- {
- type: "content_block_delta",
- index: 0,
- delta: { type: "input_json_delta", partial_json: '{"city":"Paris"}' },
- },
- { type: "content_block_stop", index: 0 },
- {
- type: "content_block_start",
- index: 1,
- content_block: { type: "tool_use", id: "call_london", name: "weather", input: {} },
- },
- {
- type: "content_block_delta",
- index: 1,
- delta: { type: "input_json_delta", partial_json: '{"city":"London"}' },
- },
- { type: "content_block_stop", index: 1 },
- { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 2 } },
- { type: "message_stop" },
- ),
- ),
- ),
- )
- expect(response.toolCalls).toMatchObject([
- { id: "call_paris", name: "weather", input: { city: "Paris" } },
- { id: "call_london", name: "weather", input: { city: "London" } },
- ])
- expect(response.message.content).toMatchObject([
- { type: "tool-call", id: "call_paris", name: "weather", input: { city: "Paris" } },
- { type: "tool-call", id: "call_london", name: "weather", input: { city: "London" } },
- ])
- expect(response.finishReason).toEqual({ normalized: "tool-calls", raw: "tool_use" })
- }),
- )
- it.effect("keeps malformed server tool input terminal", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "server_tool_use", id: "call_1", name: "web_search" },
- },
- {
- type: "content_block_delta",
- index: 0,
- delta: { type: "input_json_delta", partial_json: '{"query":"partial' },
- },
- { type: "content_block_stop", index: 0 },
- )
- const error = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)), Effect.flip)
- expect(error).toBeInstanceOf(AIError)
- expect(error.message).toContain("Invalid JSON input for anthropic-messages tool call web_search")
- }),
- )
- it.effect("fails with a typed provider error for stream error frames", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(sseEvents({ type: "error", error: { type: "overloaded_error", message: "Overloaded" } })),
- ),
- Effect.flip,
- )
- expect(error.reason).toMatchObject({ _tag: "ProviderInternal", message: "overloaded_error: Overloaded" })
- }),
- )
- it.effect("classifies prompt-too-long provider errors", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse(
- sseEvents({
- type: "error",
- error: { type: "invalid_request_error", message: "prompt is too long: 210000 tokens" },
- }),
- ),
- ),
- Effect.flip,
- )
- expect(error.reason).toMatchObject({
- _tag: "InvalidRequest",
- message: "invalid_request_error: prompt is too long: 210000 tokens",
- classification: "context-overflow",
- })
- }),
- )
- it.effect("falls back to error type when no message is present", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(fixedResponse(sseEvents({ type: "error", error: { type: "overloaded_error", message: "" } }))),
- Effect.flip,
- )
- expect(error.reason).toMatchObject({ _tag: "ProviderInternal", message: "overloaded_error" })
- }),
- )
- it.effect("falls back to a stable default when error payload is absent", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(fixedResponse(sseEvents({ type: "error" }))),
- Effect.flip,
- )
- expect(error.reason).toMatchObject({ _tag: "UnknownProvider", message: "Anthropic Messages stream error" })
- }),
- )
- it.effect("fails HTTP provider errors before stream parsing", () =>
- Effect.gen(function* () {
- const error = yield* LLMClient.generate(request).pipe(
- Effect.provide(
- fixedResponse('{"type":"error","error":{"type":"invalid_request_error","message":"Bad request"}}', {
- status: 400,
- headers: { "content-type": "application/json" },
- }),
- ),
- Effect.flip,
- )
- expect(error).toBeInstanceOf(AIError)
- expect(error.reason).toMatchObject({ _tag: "InvalidRequest", message: "Bad request" })
- }),
- )
- it.effect("decodes server_tool_use + web_search_tool_result as provider-executed events", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "server_tool_use", id: "srvtoolu_abc", name: "web_search" },
- },
- {
- type: "content_block_delta",
- index: 0,
- delta: { type: "input_json_delta", partial_json: '{"query":"effect 4"}' },
- },
- { type: "content_block_stop", index: 0 },
- {
- type: "content_block_start",
- index: 1,
- content_block: {
- type: "web_search_tool_result",
- tool_use_id: "srvtoolu_abc",
- content: [{ type: "web_search_result", url: "https://example.com", title: "Example" }],
- },
- },
- { type: "content_block_stop", index: 1 },
- { type: "content_block_start", index: 2, content_block: { type: "text", text: "" } },
- { type: "content_block_delta", index: 2, delta: { type: "text_delta", text: "Found it." } },
- { type: "content_block_stop", index: 2 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 8 } },
- { type: "message_stop" },
- )
- const response = yield* LLMClient.generate(
- LLMRequest.update(request, {
- tools: [
- ToolDefinition.make({ name: "web_search", description: "Web search", inputSchema: { type: "object" } }),
- ],
- }),
- ).pipe(Effect.provide(fixedResponse(body)))
- const toolCall = response.events.find((event) => event.type === "tool-call")
- expect(toolCall).toEqual({
- type: "tool-call",
- id: "srvtoolu_abc",
- name: "web_search",
- input: { query: "effect 4" },
- providerExecuted: true,
- })
- const toolResult = response.events.find((event) => event.type === "tool-result")
- expect(toolResult).toEqual({
- type: "tool-result",
- id: "srvtoolu_abc",
- name: "web_search",
- result: { type: "json", value: [{ type: "web_search_result", url: "https://example.com", title: "Example" }] },
- providerExecuted: true,
- // The complete payload rides in provider metadata as irreducible replay
- // state for later stateless requests.
- providerMetadata: {
- anthropic: {
- blockType: "web_search_tool_result",
- result: [{ type: "web_search_result", url: "https://example.com", title: "Example" }],
- },
- },
- })
- expect(response.text).toBe("Found it.")
- expect(response.events.at(-1)).toMatchObject({
- type: "finish",
- reason: { normalized: "stop", raw: "end_turn" },
- })
- }),
- )
- it.effect("decodes web_search_tool_result_error as provider-executed error result", () =>
- Effect.gen(function* () {
- const body = sseEvents(
- { type: "message_start", message: { usage: { input_tokens: 5 } } },
- {
- type: "content_block_start",
- index: 0,
- content_block: { type: "server_tool_use", id: "srvtoolu_x", name: "web_search" },
- },
- { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: '{"query":"q"}' } },
- { type: "content_block_stop", index: 0 },
- {
- type: "content_block_start",
- index: 1,
- content_block: {
- type: "web_search_tool_result",
- tool_use_id: "srvtoolu_x",
- content: { type: "web_search_tool_result_error", error_code: "max_uses_exceeded" },
- },
- },
- { type: "content_block_stop", index: 1 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } },
- { type: "message_stop" },
- )
- const response = yield* LLMClient.generate(
- LLMRequest.update(request, {
- tools: [
- ToolDefinition.make({ name: "web_search", description: "Web search", inputSchema: { type: "object" } }),
- ],
- }),
- ).pipe(Effect.provide(fixedResponse(body)))
- const toolResult = response.events.find((event) => event.type === "tool-result")
- expect(toolResult).toMatchObject({
- type: "tool-result",
- id: "srvtoolu_x",
- name: "web_search",
- result: { type: "error" },
- providerExecuted: true,
- })
- }),
- )
- it.effect("round-trips provider-executed assistant content into server tool blocks", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- id: "req_round_trip",
- model,
- messages: [
- Message.user("Search for something."),
- Message.assistant([
- {
- type: "tool-call",
- id: "srvtoolu_abc",
- name: "web_search",
- input: { query: "effect 4" },
- providerExecuted: true,
- },
- {
- type: "tool-result",
- id: "srvtoolu_abc",
- name: "web_search",
- result: { type: "json", value: [{ url: "https://example.com" }] },
- providerExecuted: true,
- },
- { type: "text", text: "Found it." },
- ]),
- Message.user("Thanks."),
- ],
- }),
- )
- expect(prepared.body).toMatchObject({
- messages: [
- { role: "user", content: [{ type: "text", text: "Search for something." }] },
- {
- role: "assistant",
- content: [
- { type: "server_tool_use", id: "srvtoolu_abc", name: "web_search", input: { query: "effect 4" } },
- {
- type: "web_search_tool_result",
- tool_use_id: "srvtoolu_abc",
- content: [{ url: "https://example.com" }],
- },
- { type: "text", text: "Found it." },
- ],
- },
- { role: "user", content: [{ type: "text", text: "Thanks." }] },
- ],
- })
- }),
- )
- it.effect("rejects round-trip for unknown server tool names", () =>
- Effect.gen(function* () {
- const error = yield* compileRequest(
- LLM.request({
- id: "req_unknown_server_tool",
- model,
- messages: [
- Message.assistant([
- {
- type: "tool-result",
- id: "srvtoolu_abc",
- name: "future_server_tool",
- result: { type: "json", value: {} },
- providerExecuted: true,
- },
- ]),
- ],
- }),
- ).pipe(Effect.flip)
- expect(error.message).toContain("future_server_tool")
- }),
- )
- it.effect("continues a conversation with user media content", () =>
- Effect.gen(function* () {
- const response = yield* LLMClient.generate(
- LLM.request({
- id: "req_media",
- model,
- messages: [
- Message.user([
- { type: "text", text: "What is in this image?" },
- { type: "media", mediaType: "image/png", data: "AAECAw==" },
- { type: "media", mediaType: "application/pdf", data: "JVBERi0xLjQ=", filename: "report.pdf" },
- ]),
- ],
- }),
- ).pipe(
- Effect.provide(
- dynamicResponse((input) =>
- Effect.gen(function* () {
- const web = yield* HttpClientRequest.toWeb(input.request).pipe(Effect.orDie)
- expect(yield* Effect.promise(() => web.json())).toMatchObject({
- messages: [
- {
- role: "user",
- content: [
- { type: "text", text: "What is in this image?" },
- { type: "image", source: { type: "base64", media_type: "image/png", data: "AAECAw==" } },
- {
- type: "document",
- source: { type: "base64", media_type: "application/pdf", data: "JVBERi0xLjQ=" },
- },
- ],
- },
- ],
- })
- return input.respond(
- sseEvents(
- { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
- { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "An image." } },
- { type: "content_block_stop", index: 0 },
- { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } },
- { type: "message_stop" },
- ),
- { headers: { "content-type": "text/event-stream" } },
- )
- }),
- ),
- ),
- )
- expect(response.text).toBe("An image.")
- }),
- )
- it.effect("maps ttlSeconds >= 3600 to cache_control ttl: '1h'", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- system: { type: "text", text: "system", cache: new CacheHint({ type: "ephemeral", ttlSeconds: 3600 }) },
- prompt: "hi",
- }),
- )
- expect(prepared.body).toMatchObject({
- system: [{ type: "text", text: "system", cache_control: { type: "ephemeral", ttl: "1h" } }],
- })
- }),
- )
- it.effect("emits cache_control on tool definitions and tool-result blocks", () =>
- Effect.gen(function* () {
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- tools: [
- {
- name: "lookup",
- description: "lookup tool",
- inputSchema: { type: "object", properties: {} },
- cache: new CacheHint({ type: "ephemeral" }),
- },
- ],
- messages: [
- Message.user("What's the weather?"),
- Message.assistant([ToolCallPart.make({ id: "call_1", name: "lookup", input: {} })]),
- Message.tool({
- id: "call_1",
- name: "lookup",
- result: { temp: 72 },
- cache: new CacheHint({ type: "ephemeral" }),
- }),
- ],
- }),
- )
- expect(prepared.body).toMatchObject({
- tools: [{ name: "lookup", cache_control: { type: "ephemeral" } }],
- messages: [
- { role: "user", content: [{ type: "text", text: "What's the weather?" }] },
- { role: "assistant", content: [{ type: "tool_use", id: "call_1", name: "lookup" }] },
- {
- role: "user",
- content: [{ type: "tool_result", tool_use_id: "call_1", cache_control: { type: "ephemeral" } }],
- },
- ],
- })
- }),
- )
- it.effect("drops cache_control breakpoints past the 4-per-request cap", () =>
- Effect.gen(function* () {
- const hint = new CacheHint({ type: "ephemeral" })
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- system: [
- { type: "text", text: "a", cache: hint },
- { type: "text", text: "b", cache: hint },
- { type: "text", text: "c", cache: hint },
- { type: "text", text: "d", cache: hint },
- { type: "text", text: "e", cache: hint },
- { type: "text", text: "f", cache: hint },
- ],
- prompt: "hi",
- }),
- )
- const system = (prepared.body as { system: Array<{ cache_control?: unknown }> }).system
- const marked = system.filter((part) => part.cache_control !== undefined)
- expect(marked).toHaveLength(4)
- expect(system[4]?.cache_control).toBeUndefined()
- expect(system[5]?.cache_control).toBeUndefined()
- }),
- )
- it.effect("spends breakpoint budget on tools before system before messages", () =>
- Effect.gen(function* () {
- const hint = new CacheHint({ type: "ephemeral" })
- const prepared = yield* compileRequest(
- LLM.request({
- model,
- tools: [
- {
- name: "t1",
- description: "t1",
- inputSchema: { type: "object", properties: {} },
- cache: hint,
- },
- {
- name: "t2",
- description: "t2",
- inputSchema: { type: "object", properties: {} },
- cache: hint,
- },
- {
- name: "t3",
- description: "t3",
- inputSchema: { type: "object", properties: {} },
- cache: hint,
- },
- {
- name: "t4",
- description: "t4",
- inputSchema: { type: "object", properties: {} },
- cache: hint,
- },
- ],
- system: [{ type: "text", text: "system-tail", cache: hint }],
- messages: [Message.user([{ type: "text", text: "message-tail", cache: hint }])],
- }),
- )
- const body = prepared.body as {
- tools: Array<{ cache_control?: unknown }>
- system: Array<{ cache_control?: unknown }>
- messages: Array<{ content: Array<{ cache_control?: unknown }> }>
- }
- expect(body.tools.every((t) => t.cache_control !== undefined)).toBe(true)
- expect(body.system[0]?.cache_control).toBeUndefined()
- expect(body.messages[0]?.content[0]?.cache_control).toBeUndefined()
- }),
- )
- })
|