@effect-uai/anthropic 0.12.0 → 0.13.0

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.
@@ -7,8 +7,6 @@ import { HttpClient } from "effect/unstable/http";
7
7
  import * as AiError from "@effect-uai/core/AiError";
8
8
  import { CommonRequest, LanguageModel } from "@effect-uai/core/LanguageModel";
9
9
  import { Turn, TurnEvent } from "@effect-uai/core/Turn";
10
-
11
- //#region src/Anthropic.d.ts
12
10
  declare namespace Anthropic_d_exports {
13
11
  export { Anthropic, AnthropicRequest, AnthropicService, Config, UserLocation, WebSearchOptions, codeExecutionTool, layer, make, toCanonical, webSearchTool };
14
12
  }
@@ -22,7 +20,8 @@ type AnthropicRequest = Omit<CommonRequest, "model"> & {
22
20
  * Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the
23
21
  * common surface.
24
22
  */
25
- readonly topK?: number; /** Stop sequences that abort generation when matched. */
23
+ readonly topK?: number;
24
+ /** Stop sequences that abort generation when matched. */
26
25
  readonly stopSequences?: ReadonlyArray<string>;
27
26
  /**
28
27
  * Extended thinking configuration. `0` budget is equivalent to disabled.
@@ -1 +1 @@
1
- {"version":3,"file":"Anthropic.d.mts","names":[],"sources":["../src/Anthropic.ts"],"mappings":";;;;;;;;;;;;;;KA+CY,gBAAA,GAAmB,IAAA,CAAK,aAAA;;;;;WAKzB,KAAA,EAAO,cAAA;;;;;WAKP,IAAA;WAEA,aAAA,GAAgB,aAAA;;;;AAZ3B;;WAkBW,QAAA,GAAW,cAAA;EAlBc;;;EAAA,SAsBzB,IAAA;AAAA;AAAA,KAGC,gBAAA;EAPwB;;;;;EAAA,SAazB,YAAA,GACP,OAAA,EAAS,gBAAA,KACN,MAAA,CAAO,MAAA,CAAO,aAAA,EAAe,OAAA,CAAQ,OAAA;EArBjC;;;;EAAA,SA0BA,UAAA,GAAa,OAAA,EAAS,gBAAA,KAAqB,MAAA,CAAO,MAAA,CAAO,SAAA,EAAW,OAAA,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Q;;;;;;;;WAQ5E,OAAO,SAAS,qBAAqB,OAAO,OAAO,MAAM,QAAQ;;;;;;WAMjE,cAAc,GAAG,GACxB,GAAG,OAAO,OAAO,eAAe,GAAG,OAChC,OAAO,OAAO,WAAW,GAAG;;;;;;;;;cAStB,kBAAkB;KAInB;WACD,QAAQ,SAAS;WACjB;;;;;;WAMA;;;;;;;;;;;;;;;WAeA;aAAqC,MAAM;;;;;;;;cAsQzC,cAAe,GAAG,GAAC,GAC3B,OAAO,OAAO,eAAe,GAAG,OAClC,OAAO,OAAO,WAAW,GAAG;;;;cAmBlB,OAAI,KAAS,WAAS,OAAO,OAAO,yBAAyB,WAAW;;;;;;;;cAqBxE,QAAK,KACX,WACJ,MAAM,MAAM,YAAY,sBAAsB,WAAW"}
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { codeExecutionTool, renderProviderTools, webSearchTool } from "./AnthropicTools.mjs";
3
3
  import { accumulatorToTurn, buildRequestBody, emptyAccumulator } from "./codec.mjs";
4
4
  import { KnownProviderEvent, applyEvent } from "./streamEvents.mjs";
@@ -1 +1 @@
1
- {"version":3,"file":"Anthropic.mjs","names":[],"sources":["../src/Anthropic.ts"],"sourcesContent":["import {\n Context,\n Effect,\n Layer,\n Match,\n Option,\n Redacted,\n Result,\n Schema,\n Stream,\n pipe,\n} from \"effect\"\nimport { HttpClient, HttpClientRequest } from \"effect/unstable/http\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport {\n type CommonRequest,\n LanguageModel,\n type LanguageModelService,\n turnFromStream,\n} from \"@effect-uai/core/LanguageModel\"\nimport * as SSE from \"@effect-uai/core/SSE\"\nimport { descriptorsOf, providerToolsOf } from \"@effect-uai/core/Tool\"\nimport { type Turn, TurnEvent } from \"@effect-uai/core/Turn\"\nimport { renderProviderTools } from \"./AnthropicTools.js\"\nimport {\n type Accumulator,\n type CacheControl,\n type CacheTtl,\n type ThinkingConfig,\n accumulatorToTurn,\n buildRequestBody,\n emptyAccumulator,\n} from \"./codec.js\"\nimport type { AnthropicModel } from \"./models.js\"\nimport { KnownProviderEvent, ProviderEvent, applyEvent } from \"./streamEvents.js\"\n\nexport {\n codeExecutionTool,\n webSearchTool,\n type WebSearchOptions,\n type UserLocation,\n} from \"./AnthropicTools.js\"\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\nexport type AnthropicRequest = Omit<CommonRequest, \"model\"> & {\n /**\n * Narrows `CommonRequest.model` (`string`) to the typed `AnthropicModel`\n * literal union for autocomplete.\n */\n readonly model: AnthropicModel\n /**\n * Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the\n * common surface.\n */\n readonly topK?: number\n /** Stop sequences that abort generation when matched. */\n readonly stopSequences?: ReadonlyArray<string>\n /**\n * Extended thinking configuration. `0` budget is equivalent to disabled.\n * Only the `claude-sonnet-4-x`, `claude-haiku-4-x`, and pre-Opus-4.7\n * model lines support extended thinking.\n */\n readonly thinking?: ThinkingConfig\n /**\n * `metadata.user_id` on the wire. End-user tracking identifier.\n */\n readonly user?: string\n}\n\nexport type AnthropicService = {\n /**\n * Stream the provider's native event vocabulary (post-SSE-decode).\n * Use this when you need full vendor fidelity (e.g. `signature_delta` for\n * encrypted reasoning state). For provider-portable code, use `streamTurn`.\n */\n readonly streamNative: (\n request: AnthropicRequest,\n ) => Stream.Stream<ProviderEvent, AiError.AiError>\n /**\n * Stream canonical `TurnEvent`s. Implemented as\n * `streamNative |> toCanonical`.\n */\n readonly streamTurn: (request: AnthropicRequest) => Stream.Stream<TurnEvent, AiError.AiError>\n /**\n * Drain a single turn and return the assembled `Turn`. Derived from\n * `streamTurn` — Anthropic's non-streaming `/messages` endpoint\n * differs from the streamed one in subtle ways (no incremental\n * thinking deltas), so we route everything through streaming for\n * consistency.\n */\n readonly turn: (request: AnthropicRequest) => Effect.Effect<Turn, AiError.AiError>\n /**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Stateful (threads an `Accumulator` for tool-call lookup and\n * accumulator-to-Turn assembly).\n */\n readonly toCanonical: <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n ) => Stream.Stream<TurnEvent, E, R>\n}\n\n/**\n * Provider-typed service tag. Yield this when you want Anthropic-specific\n * options (`topK`, `stopSequences`, `thinking`); yield the generic\n * `LanguageModel` tag for provider-portable code. Both are registered by\n * `layer`.\n */\nexport class Anthropic extends Context.Service<Anthropic, AnthropicService>()(\n \"@betalyra/effect-uai/providers/anthropic/Anthropic\",\n) {}\n\nexport type Config = {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n /**\n * Default `max_tokens` for requests that don't override via\n * `request.maxOutputTokens`. Anthropic requires this field; we default to\n * 4096 if neither is set.\n */\n readonly defaultMaxTokens?: number\n /**\n * Prompt caching. Off by default, so the library stays unopinionated about\n * a setting that changes billing. `true` is `{}`: cache with the 5-minute\n * default TTL.\n *\n * Anthropic caches the `tools` → `system` → `messages` prefix and moves the\n * breakpoint forward as the conversation grows, so a long system prompt and\n * tool definitions are paid for once rather than on every turn. Read\n * `usage.input_tokens_details.cached_tokens` to see it working.\n *\n * Mutating earlier history (rewriting an old message, compaction) breaks\n * the prefix from that point on. Anthropic falls back to the longest\n * unchanged prefix, so this costs savings but never correctness.\n */\n readonly promptCaching?: boolean | { readonly ttl?: CacheTtl }\n}\n\nconst ANTHROPIC_VERSION = \"2023-06-01\"\nconst STRUCTURED_OUTPUTS_BETA = \"structured-outputs-2025-11-13\"\nconst FALLBACK_MAX_TOKENS = 4096\n\nconst outputConfig = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>\n pipe(\n Option.fromUndefinedOr(request.structured),\n Option.map((format) => ({\n format: {\n type: \"json_schema\",\n schema: format.schema[\"~standard\"].jsonSchema.input({ target: \"draft-2020-12\" }),\n },\n })),\n )\n\nconst resolvedMaxTokens = (cfg: Config, request: AnthropicRequest): number =>\n request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS\n\n/**\n * One request-level breakpoint when caching is on. Anthropic places it on the\n * last cacheable block itself, so nothing has to be marked per-block and the\n * breakpoint stays correct as history grows.\n */\nconst cacheControlOf = (cfg: Config): Option.Option<CacheControl> =>\n pipe(\n Option.fromUndefinedOr(cfg.promptCaching),\n Option.flatMap((opt) =>\n opt === false\n ? Option.none<CacheControl>()\n : Option.some<CacheControl>({\n type: \"ephemeral\",\n ...(opt !== true && opt.ttl !== undefined && { ttl: opt.ttl }),\n }),\n ),\n )\n\nconst functionToolDescriptors = (\n request: AnthropicRequest,\n): ReadonlyArray<Record<string, unknown>> =>\n descriptorsOf(request.tools).map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n }))\n\n/**\n * Merge the custom function-tool declarations with the native hosted-tool\n * entries into the single `tools` array Anthropic expects. `providerWire` is\n * pre-rendered (and validated) by the caller.\n */\nconst toolsWire = (\n request: AnthropicRequest,\n providerWire: ReadonlyArray<Record<string, unknown>>,\n): Option.Option<ReadonlyArray<Record<string, unknown>>> => {\n const tools = [...functionToolDescriptors(request), ...providerWire]\n return tools.length > 0 ? Option.some(tools) : Option.none()\n}\n\nconst toolChoiceWire = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>\n pipe(\n Option.fromUndefinedOr(request.toolChoice),\n Option.map((choice) =>\n choice === \"auto\"\n ? { type: \"auto\" }\n : choice === \"required\"\n ? { type: \"any\" }\n : choice === \"none\"\n ? { type: \"none\" }\n : { type: \"tool\", name: choice.name },\n ),\n )\n\n// ---------------------------------------------------------------------------\n// SSE event → ProviderEvent\n// ---------------------------------------------------------------------------\n\nconst decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent)\nconst parseJsonUnknown = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown))\n\nconst makeUnknown = (raw: unknown): ProviderEvent => ({ type: \"_unknown\", raw })\n\n/**\n * Parse one SSE event's `data` payload into a typed `ProviderEvent`. Never\n * fails: JSON-parse and schema-decode failures both produce a synthesized\n * `_unknown` event so consumers of `streamNative` never silently miss a\n * wire event we didn't model. When the payload IS valid JSON but doesn't\n * match the known-event schema, `_unknown.raw` carries the parsed value;\n * when JSON parsing fails outright, it carries the raw string.\n */\nconst sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect<ProviderEvent> =>\n parseJsonUnknown(ev.data).pipe(\n Effect.flatMap((parsed) =>\n decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed))),\n ),\n Effect.orElseSucceed(() => makeUnknown(ev.data)),\n )\n\n// ---------------------------------------------------------------------------\n// Per-event derivation of TurnEvents. Drives off the new accumulator and the\n// raw event, since some deltas (`tool_call_args_delta`) need the call_id\n// which lives on the accumulator's per-index block.\n// ---------------------------------------------------------------------------\n\nconst deltasFromEvent = (next: Accumulator, event: ProviderEvent): ReadonlyArray<TurnEvent> =>\n Match.value(event).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n content_block_start: (e) =>\n e.content_block.type === \"tool_use\"\n ? [\n TurnEvent.ToolCallStart({\n call_id: e.content_block.id,\n name: e.content_block.name,\n }),\n ]\n : [],\n content_block_delta: (e) =>\n Match.value(e.delta).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n text_delta: (d) => [TurnEvent.TextDelta({ text: d.text })],\n thinking_delta: (d) => [TurnEvent.ReasoningDelta({ text: d.thinking, kind: \"trace\" })],\n input_json_delta: (d) => {\n const block = next.blocks[e.index]\n if (block === undefined) return []\n const callId = Option.getOrElse(block.id, () => \"\")\n return callId.length === 0\n ? []\n : [TurnEvent.ToolCallArgsDelta({ call_id: callId, delta: d.partial_json })]\n },\n // Encrypted reasoning state - flows through `streamNative` but has\n // no canonical representation.\n signature_delta: () => [],\n }),\n ),\n message_start: (e) =>\n e.message.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })],\n message_delta: (e) =>\n e.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })],\n message_stop: () => [TurnEvent.TurnComplete({ turn: accumulatorToTurn(next) })],\n content_block_stop: () => [],\n ping: () => [],\n error: () => [],\n _unknown: () => [],\n }),\n )\n\n// ---------------------------------------------------------------------------\n// Service implementation\n// ---------------------------------------------------------------------------\n\nconst httpStatusError = (status: number, body: string): AiError.AiError => {\n const provider = \"anthropic\"\n const raw = body\n if (status === 429) return new AiError.RateLimited({ provider, raw })\n if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw })\n if (status === 401) return new AiError.AuthFailed({ provider, subtype: \"auth\", raw })\n if (status === 403) return new AiError.AuthFailed({ provider, subtype: \"permission\", raw })\n if (status === 402) return new AiError.AuthFailed({ provider, subtype: \"billing\", raw })\n if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw })\n if (status === 529) return new AiError.Unavailable({ provider, status, raw })\n if (status >= 500) return new AiError.Unavailable({ provider, status, raw })\n return new AiError.InvalidRequest({ provider, raw })\n}\n\nconst buildNativeStream = (cfg: Config) => {\n const baseUrl = cfg.baseUrl ?? \"https://api.anthropic.com\"\n const url = `${baseUrl}/v1/messages`\n return (\n request: AnthropicRequest,\n ): Stream.Stream<ProviderEvent, AiError.AiError, HttpClient.HttpClient> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const structured = outputConfig(request)\n const providerWire = yield* Result.match(\n renderProviderTools(providerToolsOf(request.tools)),\n {\n onFailure: (e) =>\n Effect.fail(\n new AiError.Unsupported({\n provider: \"anthropic\",\n capability: e.capability,\n reason: e.reason,\n }),\n ),\n onSuccess: (wire) => Effect.succeed(wire),\n },\n )\n const bodyResult = buildRequestBody({\n model: request.model,\n history: request.history,\n maxTokens: resolvedMaxTokens(cfg, request),\n temperature: Option.fromUndefinedOr(request.temperature),\n topP: Option.fromUndefinedOr(request.topP),\n topK: Option.fromUndefinedOr(request.topK),\n stopSequences: Option.fromUndefinedOr(request.stopSequences),\n thinking: Option.fromUndefinedOr(request.thinking),\n tools: toolsWire(request, providerWire),\n toolChoice: toolChoiceWire(request),\n userId: Option.fromUndefinedOr(request.user),\n outputConfig: structured,\n cacheControl: cacheControlOf(cfg),\n })\n\n const body = yield* Result.match(bodyResult, {\n onFailure: (cause) =>\n Effect.fail(\n new AiError.InvalidRequest({\n provider: \"anthropic\",\n param: \"input.function_call.arguments\",\n raw: cause,\n }),\n ),\n onSuccess: (b) => Effect.succeed(b),\n })\n\n const client = yield* HttpClient.HttpClient\n const baseRequest = HttpClientRequest.post(url).pipe(\n HttpClientRequest.setHeader(\"x-api-key\", Redacted.value(cfg.apiKey)),\n HttpClientRequest.setHeader(\"anthropic-version\", ANTHROPIC_VERSION),\n HttpClientRequest.bodyJsonUnsafe(body),\n HttpClientRequest.accept(\"text/event-stream\"),\n )\n const httpRequest = Option.isSome(structured)\n ? baseRequest.pipe(HttpClientRequest.setHeader(\"anthropic-beta\", STRUCTURED_OUTPUTS_BETA))\n : baseRequest\n const response = yield* client\n .execute(httpRequest)\n .pipe(\n Effect.mapError(\n (cause): AiError.AiError =>\n new AiError.Unavailable({ provider: \"anthropic\", raw: cause }),\n ),\n )\n if (response.status >= 400) {\n const text = yield* response.text.pipe(Effect.orElseSucceed(() => \"\"))\n return Stream.fail(httpStatusError(response.status, text))\n }\n\n return response.stream.pipe(\n Stream.mapError(\n (cause): AiError.AiError =>\n new AiError.Unavailable({ provider: \"anthropic\", raw: cause }),\n ),\n SSE.fromBytes,\n Stream.mapEffect(sseEventToProviderEvent),\n Stream.flatMap((event) =>\n event.type === \"error\"\n ? Stream.fail(new AiError.Unavailable({ provider: \"anthropic\", raw: event }))\n : Stream.succeed(event),\n ),\n )\n }),\n )\n}\n\n/**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Threads a fresh `Accumulator` per stream so tool-call lookup and\n * `accumulatorToTurn` assembly work correctly across the run.\n */\nexport const toCanonical = <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n): Stream.Stream<TurnEvent, E, R> =>\n s.pipe(\n Stream.mapAccum(\n () => emptyAccumulator,\n (acc, event) => {\n const next = applyEvent(acc, event)\n const deltas = deltasFromEvent(next, event)\n return [next, deltas] as const\n },\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Constructors\n// ---------------------------------------------------------------------------\n\n/**\n * Build an `AnthropicService` value. For Layer-based setup, prefer `layer`.\n */\nexport const make = (cfg: Config): Effect.Effect<AnthropicService, never, HttpClient.HttpClient> =>\n Effect.map(HttpClient.HttpClient, (client) => {\n const streamNative: AnthropicService[\"streamNative\"] = (request) =>\n buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client))\n const streamTurn: AnthropicService[\"streamTurn\"] = (request) =>\n toCanonical(streamNative(request))\n return {\n streamNative,\n streamTurn,\n turn: turnFromStream(streamTurn),\n toCanonical,\n }\n })\n\n/**\n * Layer that registers both the provider-specific `Anthropic` tag and the\n * generic `LanguageModel` tag, sharing one underlying implementation.\n *\n * The generic tag accepts `CommonRequest`; the typed tag accepts the full\n * `AnthropicRequest` surface.\n */\nexport const layer = (\n cfg: Config,\n): Layer.Layer<Anthropic | LanguageModel, never, HttpClient.HttpClient> => {\n const typed = Layer.effect(Anthropic, make(cfg))\n const generic = Layer.effect(\n LanguageModel,\n Effect.map(\n make(cfg),\n (s): LanguageModelService => ({\n streamTurn: (request) => s.streamTurn(request as AnthropicRequest),\n turn: (request) => s.turn(request as AnthropicRequest),\n }),\n ),\n )\n return Layer.merge(typed, generic)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8GA,IAAa,YAAb,cAA+B,QAAQ,SAAsC,CAC3E,qDACD,CAAC;AA4BF,MAAM,oBAAoB;AAC1B,MAAM,0BAA0B;AAChC,MAAM,sBAAsB;AAE5B,MAAM,gBAAgB,YACpB,KACE,OAAO,gBAAgB,QAAQ,WAAW,EAC1C,OAAO,KAAK,YAAY,EACtB,QAAQ;CACN,MAAM;CACN,QAAQ,OAAO,OAAO,aAAa,WAAW,MAAM,EAAE,QAAQ,iBAAiB,C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AC,CACjE,EACD,IAAI,WACJ,OAAO,UAAU,wBAAwB,EACzC,OAAO,SAAS,UACd,MAAM,SAAS,UACX,OAAO,KAAK,IAAI,QAAQ,YAAY;GAAE,UAAU;GAAa,KAAK;GAAO,CAAC,CAAC,GAC3E,OAAO,QAAQ,MAAM,CAC1B,CACF;GACD,CACH;;;;;;;AAQL,MAAa,eACX,MAEA,EAAE,KACA,OAAO,eACC,mBACL,KAAK,UAAU;CACd,MAAM,OAAO,WAAW,KAAK,MAAM;AAEnC,QAAO,CAAC,MADO,gBAAgB,MAAM,MACjB,CAAC;EAExB,CACF;;;;AASH,MAAa,QAAQ,QACnB,OAAO,IAAI,WAAW,aAAa,WAAW;CAC5C,MAAM,gBAAkD,YACtD,kBAAkB,IAAI,CAAC,QAAQ,CAAC,KAAK,OAAO,eAAe,WAAW,YAAY,OAAO,CAAC;CAC5F,MAAM,cAA8C,YAClD,YAAY,aAAa,QAAQ,CAAC;AACpC,QAAO;EACL;EACA;EACA,MAAM,eAAe,WAAW;EAChC;EACD;EACD;;;;;;;;AASJ,MAAa,SACX,QACyE;CACzE,MAAM,QAAQ,MAAM,OAAO,WAAW,KAAK,IAAI,CAAC;CAChD,MAAM,UAAU,MAAM,OACpB,eACA,OAAO,IACL,KAAK,IAAI,GACR,OAA6B;EAC5B,aAAa,YAAY,EAAE,WAAW,QAA4B;EAClE,OAAO,YAAY,EAAE,KAAK,QAA4B;EACvD,EACF,CACF;AACD,QAAO,MAAM,MAAM,OAAO,QAAQ"}
1
+ {"version":3,"file":"Anthropic.mjs","names":[],"sources":["../src/Anthropic.ts"],"sourcesContent":["import {\n Context,\n Effect,\n Layer,\n Match,\n Option,\n Redacted,\n Result,\n Schema,\n Stream,\n pipe,\n} from \"effect\"\nimport { HttpClient, HttpClientRequest } from \"effect/unstable/http\"\nimport * as AiError from \"@effect-uai/core/AiError\"\nimport {\n type CommonRequest,\n LanguageModel,\n type LanguageModelService,\n turnFromStream,\n} from \"@effect-uai/core/LanguageModel\"\nimport * as SSE from \"@effect-uai/core/SSE\"\nimport { descriptorsOf, providerToolsOf } from \"@effect-uai/core/Tool\"\nimport { type Turn, TurnEvent } from \"@effect-uai/core/Turn\"\nimport { renderProviderTools } from \"./AnthropicTools.js\"\nimport {\n type Accumulator,\n type CacheControl,\n type CacheTtl,\n type ThinkingConfig,\n accumulatorToTurn,\n buildRequestBody,\n emptyAccumulator,\n} from \"./codec.js\"\nimport type { AnthropicModel } from \"./models.js\"\nimport { KnownProviderEvent, ProviderEvent, applyEvent } from \"./streamEvents.js\"\n\nexport {\n codeExecutionTool,\n webSearchTool,\n type WebSearchOptions,\n type UserLocation,\n} from \"./AnthropicTools.js\"\n\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\nexport type AnthropicRequest = Omit<CommonRequest, \"model\"> & {\n /**\n * Narrows `CommonRequest.model` (`string`) to the typed `AnthropicModel`\n * literal union for autocomplete.\n */\n readonly model: AnthropicModel\n /**\n * Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the\n * common surface.\n */\n readonly topK?: number\n /** Stop sequences that abort generation when matched. */\n readonly stopSequences?: ReadonlyArray<string>\n /**\n * Extended thinking configuration. `0` budget is equivalent to disabled.\n * Only the `claude-sonnet-4-x`, `claude-haiku-4-x`, and pre-Opus-4.7\n * model lines support extended thinking.\n */\n readonly thinking?: ThinkingConfig\n /**\n * `metadata.user_id` on the wire. End-user tracking identifier.\n */\n readonly user?: string\n}\n\nexport type AnthropicService = {\n /**\n * Stream the provider's native event vocabulary (post-SSE-decode).\n * Use this when you need full vendor fidelity (e.g. `signature_delta` for\n * encrypted reasoning state). For provider-portable code, use `streamTurn`.\n */\n readonly streamNative: (\n request: AnthropicRequest,\n ) => Stream.Stream<ProviderEvent, AiError.AiError>\n /**\n * Stream canonical `TurnEvent`s. Implemented as\n * `streamNative |> toCanonical`.\n */\n readonly streamTurn: (request: AnthropicRequest) => Stream.Stream<TurnEvent, AiError.AiError>\n /**\n * Drain a single turn and return the assembled `Turn`. Derived from\n * `streamTurn` — Anthropic's non-streaming `/messages` endpoint\n * differs from the streamed one in subtle ways (no incremental\n * thinking deltas), so we route everything through streaming for\n * consistency.\n */\n readonly turn: (request: AnthropicRequest) => Effect.Effect<Turn, AiError.AiError>\n /**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Stateful (threads an `Accumulator` for tool-call lookup and\n * accumulator-to-Turn assembly).\n */\n readonly toCanonical: <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n ) => Stream.Stream<TurnEvent, E, R>\n}\n\n/**\n * Provider-typed service tag. Yield this when you want Anthropic-specific\n * options (`topK`, `stopSequences`, `thinking`); yield the generic\n * `LanguageModel` tag for provider-portable code. Both are registered by\n * `layer`.\n */\nexport class Anthropic extends Context.Service<Anthropic, AnthropicService>()(\n \"@betalyra/effect-uai/providers/anthropic/Anthropic\",\n) {}\n\nexport type Config = {\n readonly apiKey: Redacted.Redacted\n readonly baseUrl?: string\n /**\n * Default `max_tokens` for requests that don't override via\n * `request.maxOutputTokens`. Anthropic requires this field; we default to\n * 4096 if neither is set.\n */\n readonly defaultMaxTokens?: number\n /**\n * Prompt caching. Off by default, so the library stays unopinionated about\n * a setting that changes billing. `true` is `{}`: cache with the 5-minute\n * default TTL.\n *\n * Anthropic caches the `tools` → `system` → `messages` prefix and moves the\n * breakpoint forward as the conversation grows, so a long system prompt and\n * tool definitions are paid for once rather than on every turn. Read\n * `usage.input_tokens_details.cached_tokens` to see it working.\n *\n * Mutating earlier history (rewriting an old message, compaction) breaks\n * the prefix from that point on. Anthropic falls back to the longest\n * unchanged prefix, so this costs savings but never correctness.\n */\n readonly promptCaching?: boolean | { readonly ttl?: CacheTtl }\n}\n\nconst ANTHROPIC_VERSION = \"2023-06-01\"\nconst STRUCTURED_OUTPUTS_BETA = \"structured-outputs-2025-11-13\"\nconst FALLBACK_MAX_TOKENS = 4096\n\nconst outputConfig = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>\n pipe(\n Option.fromUndefinedOr(request.structured),\n Option.map((format) => ({\n format: {\n type: \"json_schema\",\n schema: format.schema[\"~standard\"].jsonSchema.input({ target: \"draft-2020-12\" }),\n },\n })),\n )\n\nconst resolvedMaxTokens = (cfg: Config, request: AnthropicRequest): number =>\n request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS\n\n/**\n * One request-level breakpoint when caching is on. Anthropic places it on the\n * last cacheable block itself, so nothing has to be marked per-block and the\n * breakpoint stays correct as history grows.\n */\nconst cacheControlOf = (cfg: Config): Option.Option<CacheControl> =>\n pipe(\n Option.fromUndefinedOr(cfg.promptCaching),\n Option.flatMap((opt) =>\n opt === false\n ? Option.none<CacheControl>()\n : Option.some<CacheControl>({\n type: \"ephemeral\",\n ...(opt !== true && opt.ttl !== undefined && { ttl: opt.ttl }),\n }),\n ),\n )\n\nconst functionToolDescriptors = (\n request: AnthropicRequest,\n): ReadonlyArray<Record<string, unknown>> =>\n descriptorsOf(request.tools).map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n }))\n\n/**\n * Merge the custom function-tool declarations with the native hosted-tool\n * entries into the single `tools` array Anthropic expects. `providerWire` is\n * pre-rendered (and validated) by the caller.\n */\nconst toolsWire = (\n request: AnthropicRequest,\n providerWire: ReadonlyArray<Record<string, unknown>>,\n): Option.Option<ReadonlyArray<Record<string, unknown>>> => {\n const tools = [...functionToolDescriptors(request), ...providerWire]\n return tools.length > 0 ? Option.some(tools) : Option.none()\n}\n\nconst toolChoiceWire = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>\n pipe(\n Option.fromUndefinedOr(request.toolChoice),\n Option.map((choice) =>\n choice === \"auto\"\n ? { type: \"auto\" }\n : choice === \"required\"\n ? { type: \"any\" }\n : choice === \"none\"\n ? { type: \"none\" }\n : { type: \"tool\", name: choice.name },\n ),\n )\n\n// ---------------------------------------------------------------------------\n// SSE event → ProviderEvent\n// ---------------------------------------------------------------------------\n\nconst decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent)\nconst parseJsonUnknown = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown))\n\nconst makeUnknown = (raw: unknown): ProviderEvent => ({ type: \"_unknown\", raw })\n\n/**\n * Parse one SSE event's `data` payload into a typed `ProviderEvent`. Never\n * fails: JSON-parse and schema-decode failures both produce a synthesized\n * `_unknown` event so consumers of `streamNative` never silently miss a\n * wire event we didn't model. When the payload IS valid JSON but doesn't\n * match the known-event schema, `_unknown.raw` carries the parsed value;\n * when JSON parsing fails outright, it carries the raw string.\n */\nconst sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect<ProviderEvent> =>\n parseJsonUnknown(ev.data).pipe(\n Effect.flatMap((parsed) =>\n decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed))),\n ),\n Effect.orElseSucceed(() => makeUnknown(ev.data)),\n )\n\n// ---------------------------------------------------------------------------\n// Per-event derivation of TurnEvents. Drives off the new accumulator and the\n// raw event, since some deltas (`tool_call_args_delta`) need the call_id\n// which lives on the accumulator's per-index block.\n// ---------------------------------------------------------------------------\n\nconst deltasFromEvent = (next: Accumulator, event: ProviderEvent): ReadonlyArray<TurnEvent> =>\n Match.value(event).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n content_block_start: (e) =>\n e.content_block.type === \"tool_use\"\n ? [\n TurnEvent.ToolCallStart({\n call_id: e.content_block.id,\n name: e.content_block.name,\n }),\n ]\n : [],\n content_block_delta: (e) =>\n Match.value(e.delta).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n text_delta: (d) => [TurnEvent.TextDelta({ text: d.text })],\n thinking_delta: (d) => [TurnEvent.ReasoningDelta({ text: d.thinking, kind: \"trace\" })],\n input_json_delta: (d) => {\n const block = next.blocks[e.index]\n if (block === undefined) return []\n const callId = Option.getOrElse(block.id, () => \"\")\n return callId.length === 0\n ? []\n : [TurnEvent.ToolCallArgsDelta({ call_id: callId, delta: d.partial_json })]\n },\n // Encrypted reasoning state - flows through `streamNative` but has\n // no canonical representation.\n signature_delta: () => [],\n }),\n ),\n message_start: (e) =>\n e.message.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })],\n message_delta: (e) =>\n e.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })],\n message_stop: () => [TurnEvent.TurnComplete({ turn: accumulatorToTurn(next) })],\n content_block_stop: () => [],\n ping: () => [],\n error: () => [],\n _unknown: () => [],\n }),\n )\n\n// ---------------------------------------------------------------------------\n// Service implementation\n// ---------------------------------------------------------------------------\n\nconst httpStatusError = (status: number, body: string): AiError.AiError => {\n const provider = \"anthropic\"\n const raw = body\n if (status === 429) return new AiError.RateLimited({ provider, raw })\n if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw })\n if (status === 401) return new AiError.AuthFailed({ provider, subtype: \"auth\", raw })\n if (status === 403) return new AiError.AuthFailed({ provider, subtype: \"permission\", raw })\n if (status === 402) return new AiError.AuthFailed({ provider, subtype: \"billing\", raw })\n if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw })\n if (status === 529) return new AiError.Unavailable({ provider, status, raw })\n if (status >= 500) return new AiError.Unavailable({ provider, status, raw })\n return new AiError.InvalidRequest({ provider, raw })\n}\n\nconst buildNativeStream = (cfg: Config) => {\n const baseUrl = cfg.baseUrl ?? \"https://api.anthropic.com\"\n const url = `${baseUrl}/v1/messages`\n return (\n request: AnthropicRequest,\n ): Stream.Stream<ProviderEvent, AiError.AiError, HttpClient.HttpClient> =>\n Stream.unwrap(\n Effect.gen(function* () {\n const structured = outputConfig(request)\n const providerWire = yield* Result.match(\n renderProviderTools(providerToolsOf(request.tools)),\n {\n onFailure: (e) =>\n Effect.fail(\n new AiError.Unsupported({\n provider: \"anthropic\",\n capability: e.capability,\n reason: e.reason,\n }),\n ),\n onSuccess: (wire) => Effect.succeed(wire),\n },\n )\n const bodyResult = buildRequestBody({\n model: request.model,\n history: request.history,\n maxTokens: resolvedMaxTokens(cfg, request),\n temperature: Option.fromUndefinedOr(request.temperature),\n topP: Option.fromUndefinedOr(request.topP),\n topK: Option.fromUndefinedOr(request.topK),\n stopSequences: Option.fromUndefinedOr(request.stopSequences),\n thinking: Option.fromUndefinedOr(request.thinking),\n tools: toolsWire(request, providerWire),\n toolChoice: toolChoiceWire(request),\n userId: Option.fromUndefinedOr(request.user),\n outputConfig: structured,\n cacheControl: cacheControlOf(cfg),\n })\n\n const body = yield* Result.match(bodyResult, {\n onFailure: (cause) =>\n Effect.fail(\n new AiError.InvalidRequest({\n provider: \"anthropic\",\n param: \"input.function_call.arguments\",\n raw: cause,\n }),\n ),\n onSuccess: (b) => Effect.succeed(b),\n })\n\n const client = yield* HttpClient.HttpClient\n const baseRequest = HttpClientRequest.post(url).pipe(\n HttpClientRequest.setHeader(\"x-api-key\", Redacted.value(cfg.apiKey)),\n HttpClientRequest.setHeader(\"anthropic-version\", ANTHROPIC_VERSION),\n HttpClientRequest.bodyJsonUnsafe(body),\n HttpClientRequest.accept(\"text/event-stream\"),\n )\n const httpRequest = Option.isSome(structured)\n ? baseRequest.pipe(HttpClientRequest.setHeader(\"anthropic-beta\", STRUCTURED_OUTPUTS_BETA))\n : baseRequest\n const response = yield* client\n .execute(httpRequest)\n .pipe(\n Effect.mapError(\n (cause): AiError.AiError =>\n new AiError.Unavailable({ provider: \"anthropic\", raw: cause }),\n ),\n )\n if (response.status >= 400) {\n const text = yield* response.text.pipe(Effect.orElseSucceed(() => \"\"))\n return Stream.fail(httpStatusError(response.status, text))\n }\n\n return response.stream.pipe(\n Stream.mapError(\n (cause): AiError.AiError =>\n new AiError.Unavailable({ provider: \"anthropic\", raw: cause }),\n ),\n SSE.fromBytes,\n Stream.mapEffect(sseEventToProviderEvent),\n Stream.flatMap((event) =>\n event.type === \"error\"\n ? Stream.fail(new AiError.Unavailable({ provider: \"anthropic\", raw: event }))\n : Stream.succeed(event),\n ),\n )\n }),\n )\n}\n\n/**\n * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.\n * Threads a fresh `Accumulator` per stream so tool-call lookup and\n * `accumulatorToTurn` assembly work correctly across the run.\n */\nexport const toCanonical = <E, R>(\n s: Stream.Stream<ProviderEvent, E, R>,\n): Stream.Stream<TurnEvent, E, R> =>\n s.pipe(\n Stream.mapAccum(\n () => emptyAccumulator,\n (acc, event) => {\n const next = applyEvent(acc, event)\n const deltas = deltasFromEvent(next, event)\n return [next, deltas] as const\n },\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Constructors\n// ---------------------------------------------------------------------------\n\n/**\n * Build an `AnthropicService` value. For Layer-based setup, prefer `layer`.\n */\nexport const make = (cfg: Config): Effect.Effect<AnthropicService, never, HttpClient.HttpClient> =>\n Effect.map(HttpClient.HttpClient, (client) => {\n const streamNative: AnthropicService[\"streamNative\"] = (request) =>\n buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client))\n const streamTurn: AnthropicService[\"streamTurn\"] = (request) =>\n toCanonical(streamNative(request))\n return {\n streamNative,\n streamTurn,\n turn: turnFromStream(streamTurn),\n toCanonical,\n }\n })\n\n/**\n * Layer that registers both the provider-specific `Anthropic` tag and the\n * generic `LanguageModel` tag, sharing one underlying implementation.\n *\n * The generic tag accepts `CommonRequest`; the typed tag accepts the full\n * `AnthropicRequest` surface.\n */\nexport const layer = (\n cfg: Config,\n): Layer.Layer<Anthropic | LanguageModel, never, HttpClient.HttpClient> => {\n const typed = Layer.effect(Anthropic, make(cfg))\n const generic = Layer.effect(\n LanguageModel,\n Effect.map(make(cfg), (s): LanguageModelService => ({\n streamTurn: (request) => s.streamTurn(request as AnthropicRequest),\n turn: (request) => s.turn(request as AnthropicRequest),\n })),\n )\n return Layer.merge(typed, generic)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA8GA,IAAa,YAAb,cAA+B,QAAQ,QAAqC,CAAC,CAC3E,oDACF,CAAC,CAAC,CAAC;AA4BH,MAAM,oBAAoB;AAC1B,MAAM,0BAA0B;AAChC,MAAM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,5 @@
1
1
  import { Result, Schema } from "effect";
2
2
  import * as Tool from "@effect-uai/core/Tool";
3
-
4
- //#region src/AnthropicTools.d.ts
5
3
  declare namespace AnthropicTools_d_exports {
6
4
  export { AnthropicToolConfig, ToolRenderError, UserLocation, WebSearchOptions, codeExecutionTool, renderProviderTools, webSearchTool };
7
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"AnthropicTools.d.mts","names":[],"sources":["../src/AnthropicTools.ts"],"mappings":";;;;;;;cAeM,YAAA,EAAY,MAAA,CAAA,MAAA;EAAA;;;;;KAMN,YAAA,UAAsB,YAAA,CAAa,IAAA;;cAiBlC,mBAAA,EAAmB,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;KACpB,mBAAA,UAA6B,mBAAA,CAAoB,IAAA;AAAA,KAEjD,gBAAA;EAAA,SACD,OAAA;EAAA,SACA,cAAA,GAAiB,aAAA;EAAA,SACjB,cAAA,GAAiB,aAAA;EAAA,SACjB,YAAA,GAAe,YAAA;AAAA;;cAIb,aAAA,GAAiB,IAAA,GAAM,gBAAA,KAAqB,IAAA,CAAA,YAAA,eAAA,MAAA;EAAA;4BAN7B,aAAA;EAAA,0BACA,aAAA;EAAA,wBACF,YAAA;EAAA;;;;cAcb,iBAAA,EAAiB,IAAA,CAAA,YAAA,mBAAA,MAAA;EAAA;;;;;;;;KAalB,eAAA;EAAA,SACD,UAAA;EAAA,SACA,MAAA;AAAA;;AApCX;;;;cAsFa,mBAAA,GACX,KAAA,EAAO,aAAA,CAAc,IAAA,CAAK,YAAA,gCACzB,MAAA,CAAO,MAAA,CAAO,aAAA,CAAc,MAAA,oBAA0B,eAAA"}
1
+ {"version":3,"file":"AnthropicTools.d.mts","names":[],"sources":["../src/AnthropicTools.ts"],"mappings":";;;;;cAeM,cAAY,OAAA;;;;;;KAMN,sBAAsB,aAAa;;cAiBlC,qBAAmB,OAAA,gBAAA,OAAA;;;;;;;;;;;;;;;;KACpB,6BAA6B,oBAAoB;KAEjD;WACD;WACA,iBAAiB;WACjB,iBAAiB;WACjB,eAAe;;;cAIb,gBAAa,OAAU,qBAAgB,KAAA,2BAAA;WAP/B;WACO,iBAAA;WACA,iBAAA;WACF,eAAA;WAUN;;;;cAIP,mBAAiB,KAAA,+BAAA;WAKZ;;;;;;;;KAQN;WACD;WACA;;;;;;;cAgDE,sBAAmB,OACvB,cAAc,KAAK,4CACzB,OAAO,OAAO,cAAc,0BAA0B"}
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { Match, Result, Schema } from "effect";
3
3
  import * as Tool from "@effect-uai/core/Tool";
4
4
  //#region src/AnthropicTools.ts
@@ -1 +1 @@
1
- {"version":3,"file":"AnthropicTools.mjs","names":[],"sources":["../src/AnthropicTools.ts"],"sourcesContent":["import { Match, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\n\n// ---------------------------------------------------------------------------\n// Anthropic provider-hosted tools. Each is a `Tool.provider` whose `config`\n// carries the version-dated wire `type` string plus its options; the renderer\n// maps it to a native entry appended to the `tools` array alongside custom\n// tools. The `inputSchema` is never sent (provider tools are partitioned out\n// of the custom-tool declarations), so `Tool.noInput` suffices.\n// Reference: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool\n// ---------------------------------------------------------------------------\n\nconst WEB_SEARCH_TYPE = \"web_search_20250305\"\nconst CODE_EXECUTION_TYPE = \"code_execution_20250522\"\n\nconst UserLocation = Schema.Struct({\n city: Schema.optional(Schema.String),\n region: Schema.optional(Schema.String),\n country: Schema.optional(Schema.String),\n timezone: Schema.optional(Schema.String),\n})\nexport type UserLocation = typeof UserLocation.Type\n\nconst WebSearchConfig = Schema.Struct({\n kind: Schema.Literal(\"web_search\"),\n type: Schema.String,\n maxUses: Schema.optional(Schema.Number),\n allowedDomains: Schema.optional(Schema.Array(Schema.String)),\n blockedDomains: Schema.optional(Schema.Array(Schema.String)),\n userLocation: Schema.optional(UserLocation),\n})\n\nconst CodeExecutionConfig = Schema.Struct({\n kind: Schema.Literal(\"code_execution\"),\n type: Schema.String,\n})\n\n/** Discriminated config for an Anthropic hosted tool. */\nexport const AnthropicToolConfig = Schema.Union([WebSearchConfig, CodeExecutionConfig])\nexport type AnthropicToolConfig = typeof AnthropicToolConfig.Type\n\nexport type WebSearchOptions = {\n readonly maxUses?: number\n readonly allowedDomains?: ReadonlyArray<string>\n readonly blockedDomains?: ReadonlyArray<string>\n readonly userLocation?: UserLocation\n}\n\n/** Anthropic-hosted web search. */\nexport const webSearchTool = (opts: WebSearchOptions = {}) =>\n Tool.provider({\n name: \"web_search\",\n description: \"Search the web, executed by Anthropic.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"web_search\", type: WEB_SEARCH_TYPE, ...opts } as const,\n })\n\n/** Anthropic-hosted code execution (Python sandbox). */\nexport const codeExecutionTool = Tool.provider({\n name: \"code_execution\",\n description: \"Run Python in a sandbox, executed by Anthropic.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"code_execution\", type: CODE_EXECUTION_TYPE } as const,\n})\n\n/**\n * A provider tool this adapter cannot render — a foreign `provider` or an\n * unrecognized `config`. Carried as data so `Anthropic.ts` can lift it into a\n * typed `AiError.Unsupported`.\n */\nexport type ToolRenderError = {\n readonly capability: string\n readonly reason: string\n}\n\nconst userLocationWire = (loc: UserLocation): Record<string, unknown> => ({\n type: \"approximate\",\n ...(loc.city !== undefined && { city: loc.city }),\n ...(loc.region !== undefined && { region: loc.region }),\n ...(loc.country !== undefined && { country: loc.country }),\n ...(loc.timezone !== undefined && { timezone: loc.timezone }),\n})\n\nconst configToWire = Match.type<AnthropicToolConfig>().pipe(\n Match.discriminatorsExhaustive(\"kind\")({\n web_search: (c): Record<string, unknown> => ({\n type: c.type,\n name: \"web_search\",\n ...(c.maxUses !== undefined && { max_uses: c.maxUses }),\n ...(c.allowedDomains !== undefined && { allowed_domains: c.allowedDomains }),\n ...(c.blockedDomains !== undefined && { blocked_domains: c.blockedDomains }),\n ...(c.userLocation !== undefined && { user_location: userLocationWire(c.userLocation) }),\n }),\n code_execution: (c): Record<string, unknown> => ({ type: c.type, name: \"code_execution\" }),\n }),\n)\n\nconst decodeConfig = Schema.decodeUnknownResult(AnthropicToolConfig)\n\nconst renderOne = (\n tool: Tool.ProviderTool<string, any, string, any>,\n): Result.Result<Record<string, unknown>, ToolRenderError> =>\n tool.provider !== \"anthropic\"\n ? Result.fail({\n capability: `providerTool:${tool.provider}`,\n reason: `tool \"${tool.name}\" is hosted by \"${tool.provider}\", not anthropic`,\n })\n : decodeConfig(tool.config).pipe(\n Result.map(configToWire),\n Result.mapError(\n (): ToolRenderError => ({\n capability: \"providerTool\",\n reason: `unrecognized Anthropic provider tool config on \"${tool.name}\"`,\n }),\n ),\n )\n\n/**\n * Render a toolkit's Anthropic provider tools to native `tools` entries. Fails\n * with a `ToolRenderError` on the first foreign or unrecognized tool rather\n * than dropping it.\n */\nexport const renderProviderTools = (\n tools: ReadonlyArray<Tool.ProviderTool<string, any, string, any>>,\n): Result.Result<ReadonlyArray<Record<string, unknown>>, ToolRenderError> =>\n Result.all(tools.map(renderOne))\n"],"mappings":";;;;;;;;;;AAYA,MAAM,kBAAkB;AACxB,MAAM,sBAAsB;AAE5B,MAAM,eAAe,OAAO,OAAO;CACjC,MAAM,OAAO,SAAS,OAAO,OAAO;CACpC,QAAQ,OAAO,SAAS,OAAO,OAAO;CACtC,SAAS,OAAO,SAAS,OAAO,OAAO;CACvC,UAAU,OAAO,SAAS,OAAO,OAAO;CACzC,CAAC;AAGF,MAAM,kBAAkB,OAAO,OAAO;CACpC,MAAM,OAAO,QAAQ,aAAa;CAClC,MAAM,OAAO;CACb,SAAS,OAAO,SAAS,OAAO,OAAO;CACvC,gBAAgB,OAAO,SAAS,OAAO,MAAM,OAAO,OAAO,CAAC;CAC5D,g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,EAAE,aAAa,EAAE;EACxF;CACD,iBAAiB,OAAgC;EAAE,MAAM,EAAE;EAAM,MAAM;EAAkB;CAC1F,CAAC,CACH;AAED,MAAM,eAAe,OAAO,oBAAoB,oBAAoB;AAEpE,MAAM,aACJ,SAEA,KAAK,aAAa,cACd,OAAO,KAAK;CACV,YAAY,gBAAgB,KAAK;CACjC,QAAQ,SAAS,KAAK,KAAK,kBAAkB,KAAK,SAAS;CAC5D,CAAC,GACF,aAAa,KAAK,OAAO,CAAC,KACxB,OAAO,IAAI,aAAa,EACxB,OAAO,gBACmB;CACtB,YAAY;CACZ,QAAQ,mDAAmD,KAAK,KAAK;CACtE,EACF,CACF;;;;;;AAOP,MAAa,uBACX,UAEA,OAAO,IAAI,MAAM,IAAI,UAAU,CAAC"}
1
+ {"version":3,"file":"AnthropicTools.mjs","names":[],"sources":["../src/AnthropicTools.ts"],"sourcesContent":["import { Match, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\n\n// ---------------------------------------------------------------------------\n// Anthropic provider-hosted tools. Each is a `Tool.provider` whose `config`\n// carries the version-dated wire `type` string plus its options; the renderer\n// maps it to a native entry appended to the `tools` array alongside custom\n// tools. The `inputSchema` is never sent (provider tools are partitioned out\n// of the custom-tool declarations), so `Tool.noInput` suffices.\n// Reference: https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool\n// ---------------------------------------------------------------------------\n\nconst WEB_SEARCH_TYPE = \"web_search_20250305\"\nconst CODE_EXECUTION_TYPE = \"code_execution_20250522\"\n\nconst UserLocation = Schema.Struct({\n city: Schema.optional(Schema.String),\n region: Schema.optional(Schema.String),\n country: Schema.optional(Schema.String),\n timezone: Schema.optional(Schema.String),\n})\nexport type UserLocation = typeof UserLocation.Type\n\nconst WebSearchConfig = Schema.Struct({\n kind: Schema.Literal(\"web_search\"),\n type: Schema.String,\n maxUses: Schema.optional(Schema.Number),\n allowedDomains: Schema.optional(Schema.Array(Schema.String)),\n blockedDomains: Schema.optional(Schema.Array(Schema.String)),\n userLocation: Schema.optional(UserLocation),\n})\n\nconst CodeExecutionConfig = Schema.Struct({\n kind: Schema.Literal(\"code_execution\"),\n type: Schema.String,\n})\n\n/** Discriminated config for an Anthropic hosted tool. */\nexport const AnthropicToolConfig = Schema.Union([WebSearchConfig, CodeExecutionConfig])\nexport type AnthropicToolConfig = typeof AnthropicToolConfig.Type\n\nexport type WebSearchOptions = {\n readonly maxUses?: number\n readonly allowedDomains?: ReadonlyArray<string>\n readonly blockedDomains?: ReadonlyArray<string>\n readonly userLocation?: UserLocation\n}\n\n/** Anthropic-hosted web search. */\nexport const webSearchTool = (opts: WebSearchOptions = {}) =>\n Tool.provider({\n name: \"web_search\",\n description: \"Search the web, executed by Anthropic.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"web_search\", type: WEB_SEARCH_TYPE, ...opts } as const,\n })\n\n/** Anthropic-hosted code execution (Python sandbox). */\nexport const codeExecutionTool = Tool.provider({\n name: \"code_execution\",\n description: \"Run Python in a sandbox, executed by Anthropic.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"code_execution\", type: CODE_EXECUTION_TYPE } as const,\n})\n\n/**\n * A provider tool this adapter cannot render — a foreign `provider` or an\n * unrecognized `config`. Carried as data so `Anthropic.ts` can lift it into a\n * typed `AiError.Unsupported`.\n */\nexport type ToolRenderError = {\n readonly capability: string\n readonly reason: string\n}\n\nconst userLocationWire = (loc: UserLocation): Record<string, unknown> => ({\n type: \"approximate\",\n ...(loc.city !== undefined && { city: loc.city }),\n ...(loc.region !== undefined && { region: loc.region }),\n ...(loc.country !== undefined && { country: loc.country }),\n ...(loc.timezone !== undefined && { timezone: loc.timezone }),\n})\n\nconst configToWire = Match.type<AnthropicToolConfig>().pipe(\n Match.discriminatorsExhaustive(\"kind\")({\n web_search: (c): Record<string, unknown> => ({\n type: c.type,\n name: \"web_search\",\n ...(c.maxUses !== undefined && { max_uses: c.maxUses }),\n ...(c.allowedDomains !== undefined && { allowed_domains: c.allowedDomains }),\n ...(c.blockedDomains !== undefined && { blocked_domains: c.blockedDomains }),\n ...(c.userLocation !== undefined && { user_location: userLocationWire(c.userLocation) }),\n }),\n code_execution: (c): Record<string, unknown> => ({ type: c.type, name: \"code_execution\" }),\n }),\n)\n\nconst decodeConfig = Schema.decodeUnknownResult(AnthropicToolConfig)\n\nconst renderOne = (\n tool: Tool.ProviderTool<string, any, string, any>,\n): Result.Result<Record<string, unknown>, ToolRenderError> =>\n tool.provider !== \"anthropic\"\n ? Result.fail({\n capability: `providerTool:${tool.provider}`,\n reason: `tool \"${tool.name}\" is hosted by \"${tool.provider}\", not anthropic`,\n })\n : decodeConfig(tool.config).pipe(\n Result.map(configToWire),\n Result.mapError((): ToolRenderError => ({\n capability: \"providerTool\",\n reason: `unrecognized Anthropic provider tool config on \"${tool.name}\"`,\n })),\n )\n\n/**\n * Render a toolkit's Anthropic provider tools to native `tools` entries. Fails\n * with a `ToolRenderError` on the first foreign or unrecognized tool rather\n * than dropping it.\n */\nexport const renderProviderTools = (\n tools: ReadonlyArray<Tool.ProviderTool<string, any, string, any>>,\n): Result.Result<ReadonlyArray<Record<string, unknown>>, ToolRenderError> =>\n Result.all(tools.map(renderOne))\n"],"mappings":";;;;;;;;;;AAYA,MAAM,kBAAkB;AACxB,MAAM,sBAAsB;AAE5B,MAAM,eAAe,OAAO,OAAO;CACjC,MAAM,OAAO,SAAS,OAAO,MAAM;CACnC,QAAQ,OAAO,SAAS,OAAO,MAAM;CACrC,SAAS,OAAO,SAAS,OAAO,MAAM;CACtC,UAAU,OAAO,SAAS,OAAO,MAAM;AACzC,CAAC;AAGD,MAAM,kBAAkB,OAAO,OAAO;CACpC,MAAM,OAAO,QAAQ,YAAY;CACjC,MAAM,OAAO;CACb,SAAS,OAAO,SAAS,OAAO,MAAM;CACtC,gBAAgB,OAAO,SAAS,OAAO,MAAM,OAAO,MAAM,CAAC;CAC3D,gBAAgB,OAAO,SAAS,OAA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AAE;CACxF;CACA,iBAAiB,OAAgC;EAAE,MAAM,EAAE;EAAM,MAAM;CAAiB;AAC1F,CAAC,CACH;AAEA,MAAM,eAAe,OAAO,oBAAoB,mBAAmB;AAEnE,MAAM,aACJ,SAEA,KAAK,aAAa,cACd,OAAO,KAAK;CACV,YAAY,gBAAgB,KAAK;CACjC,QAAQ,SAAS,KAAK,KAAK,kBAAkB,KAAK,SAAS;AAC7D,CAAC,IACD,aAAa,KAAK,MAAM,CAAC,CAAC,KACxB,OAAO,IAAI,YAAY,GACvB,OAAO,gBAAiC;CACtC,YAAY;CACZ,QAAQ,mDAAmD,KAAK,KAAK;AACvE,EAAE,CACJ;;;;;;AAON,MAAa,uBACX,UAEA,OAAO,IAAI,MAAM,IAAI,SAAS,CAAC"}
@@ -1 +1 @@
1
- export { };
1
+ export {}
@@ -1,7 +1,7 @@
1
1
  import { codeExecutionTool, renderProviderTools, webSearchTool } from "./AnthropicTools.mjs";
2
2
  import { accumulatorToTurn, buildRequestBody, emptyAccumulator } from "./codec.mjs";
3
3
  import { KnownProviderEvent, applyEvent } from "./streamEvents.mjs";
4
- import { n as describe, r as it, t as globalExpect } from "./test.DNmyFkvJ-CPKukVWS.mjs";
4
+ import { n as describe, r as it, t as globalExpect } from "./test.DNmyFkvJ-N3QqMRwZ.mjs";
5
5
  import { Effect, Option, Result, Schema } from "effect";
6
6
  import * as Tool from "@effect-uai/core/Tool";
7
7
  import { descriptorsOf, providerToolsOf } from "@effect-uai/core/Tool";
@@ -45,10 +45,11 @@ const buildWith = (toolkit, toolChoice = Option.none()) => {
45
45
  };
46
46
  describe("Anthropic provider tools", () => {
47
47
  it("renders web_search to the native wire entry with options", () => {
48
- globalExpect(wireFor({ web_search: webSearchTool({
48
+ const wire = wireFor({ web_search: webSearchTool({
49
49
  maxUses: 3,
50
50
  allowedDomains: ["example.com"]
51
- }) })).toEqual([{
51
+ }) });
52
+ globalExpect(wire).toEqual([{
52
53
  type: "web_search_20250305",
53
54
  name: "web_search",
54
55
  max_uses: 3,
@@ -62,10 +63,11 @@ describe("Anthropic provider tools", () => {
62
63
  }]);
63
64
  });
64
65
  it("emits both a custom tool and the native entry in the same tools array", () => {
65
- globalExpect(buildWith({
66
+ const body = buildWith({
66
67
  get_weather: localTool,
67
68
  web_search: webSearchTool()
68
- }).tools).toEqual([globalExpect.objectContaining({ name: "get_weather" }), {
69
+ });
70
+ globalExpect(body.tools).toEqual([globalExpect.objectContaining({ name: "get_weather" }), {
69
71
  type: "web_search_20250305",
70
72
  name: "web_search"
71
73
  }]);
@@ -111,7 +113,7 @@ describe("Anthropic hosted-tool response tolerance", () => {
111
113
  globalExpect(Result.isFailure(decoded)).toBe(true);
112
114
  });
113
115
  it("builds the text turn when hosted-tool blocks arrive as _unknown", () => {
114
- globalExpect(accumulatorToTurn([
116
+ const acc = [
115
117
  {
116
118
  type: "_unknown",
117
119
  raw: {
@@ -140,7 +142,9 @@ describe("Anthropic hosted-tool response tolerance", () => {
140
142
  index: 1
141
143
  },
142
144
  { type: "message_stop" }
143
- ].reduce(applyEvent, emptyAccumulator)).items).toEqual([{
145
+ ].reduce(applyEvent, emptyAccumulator);
146
+ const turn = accumulatorToTurn(acc);
147
+ globalExpect(turn.items).toEqual([{
144
148
  type: "message",
145
149
  role: "assistant",
146
150
  content: [{
@@ -1 +1 @@
1
- {"version":3,"file":"anthropicTools.test.mjs","names":["expect"],"sources":["../src/anthropicTools.test.ts"],"sourcesContent":["import { Effect, Option, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\nimport { descriptorsOf, providerToolsOf } from \"@effect-uai/core/Tool\"\nimport { describe, expect, it } from \"vitest\"\nimport { codeExecutionTool, renderProviderTools, webSearchTool } from \"./AnthropicTools.js\"\nimport { accumulatorToTurn, buildRequestBody, emptyAccumulator } from \"./codec.js\"\nimport { KnownProviderEvent, applyEvent } from \"./streamEvents.js\"\n\nconst localTool = Tool.make({\n name: \"get_weather\",\n description: \"Look up the weather.\",\n inputSchema: Tool.fromEffectSchema(Schema.Struct({ city: Schema.String })),\n run: ({ city }) => Effect.succeed(`sunny in ${city}`),\n})\n\nconst wireFor = (toolkit: Record<string, Tool.AnyTool>) =>\n Result.getOrThrow(renderProviderTools(providerToolsOf(toolkit)))\n\nconst functionWire = (toolkit: Record<string, Tool.AnyTool>) =>\n descriptorsOf(toolkit).map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n }))\n\nconst buildWith = (\n toolkit: Record<string, Tool.AnyTool>,\n toolChoice: Option.Option<Record<string, unknown>> = Option.none(),\n) => {\n const tools = [...functionWire(toolkit), ...wireFor(toolkit)]\n return Result.getOrThrow(\n buildRequestBody({\n model: \"claude-sonnet-4-5\",\n history: [{ type: \"message\", role: \"user\", content: [{ type: \"input_text\", text: \"hi\" }] }],\n maxTokens: 1024,\n temperature: Option.none(),\n topP: Option.none(),\n topK: Option.none(),\n stopSequences: Option.none(),\n thinking: Option.none(),\n tools: tools.length > 0 ? Option.some(tools) : Option.none(),\n toolChoice,\n userId: Option.none(),\n outputConfig: Option.none(),\n cacheControl: Option.none(),\n }),\n )\n}\n\ndescribe(\"Anthropic provider tools\", () => {\n it(\"renders web_search to the native wire entry with options\", () => {\n const wire = wireFor({\n web_search: webSearchTool({ maxUses: 3, allowedDomains: [\"example.com\"] }),\n })\n expect(wire).toEqual([\n {\n type: \"web_search_20250305\",\n name: \"web_search\",\n max_uses: 3,\n allowed_domains: [\"example.com\"],\n },\n ])\n })\n\n it(\"renders code_execution to its native wire entry\", () => {\n expect(wireFor({ code_execution: codeExecutionTool })).toEqual([\n { type: \"code_execution_20250522\", name: \"code_execution\" },\n ])\n })\n\n it(\"emits both a custom tool and the native entry in the same tools array\", () => {\n const body = buildWith({ get_weather: localTool, web_search: webSearchTool() })\n expect(body.tools).toEqual([\n expect.objectContaining({ name: \"get_weather\" }),\n { type: \"web_search_20250305\", name: \"web_search\" },\n ])\n })\n\n it(\"omits tool_choice for a native-only request\", () => {\n const body = buildWith({ web_search: webSearchTool() })\n expect(body.tools).toHaveLength(1)\n expect(body.tool_choice).toBeUndefined()\n })\n\n it(\"fails on a foreign provider tool\", () => {\n const foreign = Tool.provider({\n name: \"google_search\",\n description: \"Gemini grounding.\",\n inputSchema: Tool.noInput,\n provider: \"gemini\",\n config: { kind: \"google_search\" },\n })\n expect(Result.isFailure(renderProviderTools(providerToolsOf({ google_search: foreign })))).toBe(\n true,\n )\n })\n\n it(\"fails on an unrecognized config\", () => {\n const bogus = Tool.provider({\n name: \"mystery\",\n description: \"Unknown Anthropic tool.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"telepathy\" },\n })\n expect(Result.isFailure(renderProviderTools(providerToolsOf({ mystery: bogus })))).toBe(true)\n })\n})\n\ndescribe(\"Anthropic hosted-tool response tolerance\", () => {\n it(\"does not decode a server_tool_use content block as a known event\", () => {\n const serverToolUse = {\n type: \"content_block_start\",\n index: 0,\n content_block: { type: \"server_tool_use\", id: \"srvtoolu_1\", name: \"web_search\", input: {} },\n }\n const decoded = Schema.decodeUnknownResult(KnownProviderEvent)(serverToolUse)\n // Fails to decode → the SSE layer synthesizes `_unknown`, which is ignored.\n expect(Result.isFailure(decoded)).toBe(true)\n })\n\n it(\"builds the text turn when hosted-tool blocks arrive as _unknown\", () => {\n // Sequence: server_tool_use + web_search_tool_result blocks flow through as\n // `_unknown` (no accumulator change); the assistant's text answer is a\n // normal text block and survives into the turn.\n const events = [\n { type: \"_unknown\", raw: { type: \"content_block_start\", content_block: \"server_tool_use\" } },\n { type: \"content_block_start\", index: 1, content_block: { type: \"text\", text: \"\" } },\n {\n type: \"content_block_delta\",\n index: 1,\n delta: { type: \"text_delta\", text: \"Grounded answer.\" },\n },\n { type: \"content_block_stop\", index: 1 },\n { type: \"message_stop\" },\n ] as const\n const acc = events.reduce(applyEvent, emptyAccumulator)\n const turn = accumulatorToTurn(acc)\n expect(turn.items).toEqual([\n {\n type: \"message\",\n role: \"assistant\",\n content: [{ type: \"output_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,EAAE;IAAE;GAEjB,CAAC;AAE7E,eAAO,OAAO,UAAU,QAAQ,CAAC,CAAC,KAAK,KAAK;GAC5C;AAEF,IAAG,yEAAyE;AAiB1E,eADa,kBADD;GAVV;IAAE,MAAM;IAAY,KAAK;KAAE,MAAM;KAAuB,eAAe;KAAmB;IAAE;GAC5F;IAAE,MAAM;IAAuB,OAAO;IAAG,eAAe;KAAE,MAAM;KAAQ,MAAM;KAAI;IAAE;GACpF;IACE,MAAM;IACN,OAAO;IACP,OAAO;KAAE,MAAM;KAAc,MAAM;KAAoB;IACxD;GACD;IAAE,MAAM;IAAsB,OAAO;IAAG;GACxC,EAAE,MAAM,gBAAgB;GAER,CAAC,OAAO,YAAY,iBACJ,CACvB,CAAC,MAAM,CAAC,QAAQ,CACzB;GACE,MAAM;GACN,MAAM;GACN,SAAS,CAAC;IAAE,MAAM;IAAe,MAAM;IAAoB,CAAC;GAC7D,CACF,CAAC;GACF;EACF"}
1
+ {"version":3,"file":"anthropicTools.test.mjs","names":["expect"],"sources":["../src/anthropicTools.test.ts"],"sourcesContent":["import { Effect, Option, Result, Schema } from \"effect\"\nimport * as Tool from \"@effect-uai/core/Tool\"\nimport { descriptorsOf, providerToolsOf } from \"@effect-uai/core/Tool\"\nimport { describe, expect, it } from \"vitest\"\nimport { codeExecutionTool, renderProviderTools, webSearchTool } from \"./AnthropicTools.js\"\nimport { accumulatorToTurn, buildRequestBody, emptyAccumulator } from \"./codec.js\"\nimport { KnownProviderEvent, applyEvent } from \"./streamEvents.js\"\n\nconst localTool = Tool.make({\n name: \"get_weather\",\n description: \"Look up the weather.\",\n inputSchema: Tool.fromEffectSchema(Schema.Struct({ city: Schema.String })),\n run: ({ city }) => Effect.succeed(`sunny in ${city}`),\n})\n\nconst wireFor = (toolkit: Record<string, Tool.AnyTool>) =>\n Result.getOrThrow(renderProviderTools(providerToolsOf(toolkit)))\n\nconst functionWire = (toolkit: Record<string, Tool.AnyTool>) =>\n descriptorsOf(toolkit).map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n }))\n\nconst buildWith = (\n toolkit: Record<string, Tool.AnyTool>,\n toolChoice: Option.Option<Record<string, unknown>> = Option.none(),\n) => {\n const tools = [...functionWire(toolkit), ...wireFor(toolkit)]\n return Result.getOrThrow(\n buildRequestBody({\n model: \"claude-sonnet-4-5\",\n history: [{ type: \"message\", role: \"user\", content: [{ type: \"input_text\", text: \"hi\" }] }],\n maxTokens: 1024,\n temperature: Option.none(),\n topP: Option.none(),\n topK: Option.none(),\n stopSequences: Option.none(),\n thinking: Option.none(),\n tools: tools.length > 0 ? Option.some(tools) : Option.none(),\n toolChoice,\n userId: Option.none(),\n outputConfig: Option.none(),\n cacheControl: Option.none(),\n }),\n )\n}\n\ndescribe(\"Anthropic provider tools\", () => {\n it(\"renders web_search to the native wire entry with options\", () => {\n const wire = wireFor({\n web_search: webSearchTool({ maxUses: 3, allowedDomains: [\"example.com\"] }),\n })\n expect(wire).toEqual([\n {\n type: \"web_search_20250305\",\n name: \"web_search\",\n max_uses: 3,\n allowed_domains: [\"example.com\"],\n },\n ])\n })\n\n it(\"renders code_execution to its native wire entry\", () => {\n expect(wireFor({ code_execution: codeExecutionTool })).toEqual([\n { type: \"code_execution_20250522\", name: \"code_execution\" },\n ])\n })\n\n it(\"emits both a custom tool and the native entry in the same tools array\", () => {\n const body = buildWith({ get_weather: localTool, web_search: webSearchTool() })\n expect(body.tools).toEqual([\n expect.objectContaining({ name: \"get_weather\" }),\n { type: \"web_search_20250305\", name: \"web_search\" },\n ])\n })\n\n it(\"omits tool_choice for a native-only request\", () => {\n const body = buildWith({ web_search: webSearchTool() })\n expect(body.tools).toHaveLength(1)\n expect(body.tool_choice).toBeUndefined()\n })\n\n it(\"fails on a foreign provider tool\", () => {\n const foreign = Tool.provider({\n name: \"google_search\",\n description: \"Gemini grounding.\",\n inputSchema: Tool.noInput,\n provider: \"gemini\",\n config: { kind: \"google_search\" },\n })\n expect(Result.isFailure(renderProviderTools(providerToolsOf({ google_search: foreign })))).toBe(\n true,\n )\n })\n\n it(\"fails on an unrecognized config\", () => {\n const bogus = Tool.provider({\n name: \"mystery\",\n description: \"Unknown Anthropic tool.\",\n inputSchema: Tool.noInput,\n provider: \"anthropic\",\n config: { kind: \"telepathy\" },\n })\n expect(Result.isFailure(renderProviderTools(providerToolsOf({ mystery: bogus })))).toBe(true)\n })\n})\n\ndescribe(\"Anthropic hosted-tool response tolerance\", () => {\n it(\"does not decode a server_tool_use content block as a known event\", () => {\n const serverToolUse = {\n type: \"content_block_start\",\n index: 0,\n content_block: { type: \"server_tool_use\", id: \"srvtoolu_1\", name: \"web_search\", input: {} },\n }\n const decoded = Schema.decodeUnknownResult(KnownProviderEvent)(serverToolUse)\n // Fails to decode → the SSE layer synthesizes `_unknown`, which is ignored.\n expect(Result.isFailure(decoded)).toBe(true)\n })\n\n it(\"builds the text turn when hosted-tool blocks arrive as _unknown\", () => {\n // Sequence: server_tool_use + web_search_tool_result blocks flow through as\n // `_unknown` (no accumulator change); the assistant's text answer is a\n // normal text block and survives into the turn.\n const events = [\n { type: \"_unknown\", raw: { type: \"content_block_start\", content_block: \"server_tool_use\" } },\n { type: \"content_block_start\", index: 1, content_block: { type: \"text\", text: \"\" } },\n {\n type: \"content_block_delta\",\n index: 1,\n delta: { type: \"text_delta\", text: \"Grounded answer.\" },\n },\n { type: \"content_block_stop\", index: 1 },\n { type: \"message_stop\" },\n ] as const\n const acc = events.reduce(applyEvent, emptyAccumulator)\n const turn = accumulatorToTurn(acc)\n expect(turn.items).toEqual([\n {\n type: \"message\",\n role: \"assistant\",\n content: [{ type: \"output_text\", text: \"Grounded answer.\" }],\n },\n ])\n })\n})\n"],"mappings":";;;;;;;;AAQA,MAAM,YAAY,KAAK,KAAK;CAC1B,MAAM;CACN,aAAa;CACb,aAAa,KAAK,iBAAiB,OAAO,OAAO,EAAE,MAAM,OAAO,OAAO,CAAC,CAAC;CACzE,MAAM,EAAE,WAAW,OAAO,QAAQ,YAAY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AED,GAAG,yEAAyE;EAe1E,MAAM,MAAM;GAVV;IAAE,MAAM;IAAY,KAAK;KAAE,MAAM;KAAuB,eAAe;IAAkB;GAAE;GAC3F;IAAE,MAAM;IAAuB,OAAO;IAAG,eAAe;KAAE,MAAM;KAAQ,MAAM;IAAG;GAAE;GACnF;IACE,MAAM;IACN,OAAO;IACP,OAAO;KAAE,MAAM;KAAc,MAAM;IAAmB;GACxD;GACA;IAAE,MAAM;IAAsB,OAAO;GAAE;GACvC,EAAE,MAAM,eAAe;EAER,CAAC,CAAC,OAAO,YAAY,gBAAgB;EACtD,MAAM,OAAO,kBAAkB,GAAG;EAClC,aAAO,KAAK,KAAK,CAAC,CAAC,QAAQ,CACzB;GACE,MAAM;GACN,MAAM;GACN,SAAS,CAAC;IAAE,MAAM;IAAe,MAAM;GAAmB,CAAC;EAC7D,CACF,CAAC;CACH,CAAC;AACH,CAAC"}
package/dist/codec.d.mts CHANGED
@@ -2,8 +2,6 @@ import { Option, Result, Schema } from "effect";
2
2
  import { Turn } from "@effect-uai/core/Turn";
3
3
  import { JsonParseError } from "@effect-uai/core/JSONL";
4
4
  import * as Items from "@effect-uai/core/Items";
5
-
6
- //#region src/codec.d.ts
7
5
  declare namespace codec_d_exports {
8
6
  export { Accumulator, CacheControl, CacheTtl, RequestBody, ThinkingConfig, WireContentBlock, WireUsage, accumulatorToTurn, appendInputJsonDelta, appendSignatureDelta, appendTextDelta, appendThinkingDelta, buildRequestBody, emptyAccumulator, mergeUsage, setStopReason, startBlock };
9
7
  }
@@ -1 +1 @@
1
- {"version":3,"file":"codec.d.mts","names":[],"sources":["../src/codec.ts"],"mappings":";;;;;;;;;cAiCa,gBAAA,EAAgB,MAAA,CAAA,KAAA,WAAA,MAAA,CAAA,MAAA;EAAA;;;;;;;;;;;;;;;KAMjB,gBAAA,UAA0B,gBAAA,CAAiB,IAAA;AAAA,cAEjD,SAAA,EAAS,MAAA,CAAA,MAAA;EAAA;;;;;KAMH,SAAA,UAAmB,SAA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;WACA;;;KAIC;;;;;;;KAQA;WACD;WACA,MAAM;;KAGL;WACD;WACA,UAAU,cAAc;WACxB;WACA;WACA;WACA;WACA;WACA,iBAAiB;WACjB,WAAW;WACX,QAAQ,cAAc;WACtB,cAAc;WACd;aAAsB;;WACtB,gBAAgB;WAChB,gBAAgB;WAChB;;cAGE,mBAAgB;WAClB;WACA,SAAS,cAAc,MAAM;WAC7B;WACA,aAAa,OAAO;WACpB,MAAM,OAAO;WACb,MAAM,OAAO;WACb,eAAe,OAAO,OAAO;WAC7B,UAAU,OAAO,OAAO;WACxB,OAAO,OAAO,OAAO,cAAc;WACnC,YAAY,OAAO,OAAO;WAC1B,QAAQ,OAAO;WACf,cAAc,OAAO,OAAO;WAC5B,cAAc,OAAO,OAAO;MACnC,OAAO,OAAO,aAAa;KA4D1B;WACM,MAAM;WACN;WACA;WACA;WACA;WACA,IAAI,OAAO;WACX,MAAM,OAAO;WACb,cAAc,OAAO;;KAcpB;WACD,QAAQ,SAAS,eAAe;WAChC,YAAY,OAAO;WACnB,OAAO,MAAM;;cAGX,kBAAkB;cAiBlB,aAAU,KAAS,aAAW,eAAe,OAAS,qBAAmB;cAoBzE,kBAAe,KAAS,aAAW,eAAe,iBAAiB;cAGnE,uBAAoB,KAC1B,aAAW,eACH,oBAEZ;cAEU,sBAAmB,KACzB,aAAW,eACH,qBAEZ;cAEU,uBAAoB,KAC1B,aAAW,eACH,sBAEZ;cAEU,gBAAa,KAAS,aAAW,mBAAmB;cAWpD,aAAU,KAAS,aAAW,MAAQ,cAAY;cA+GlD,oBAAiB,KAAS,gBAAc"}
package/dist/codec.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
1
+ import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { Array, Encoding, Match, Option, Order, Result, Schema, pipe } from "effect";
3
3
  import { JsonParseError } from "@effect-uai/core/JSONL";
4
4
  //#region src/codec.ts
@@ -1 +1 @@
1
- {"version":3,"file":"codec.mjs","names":["Arr"],"sources":["../src/codec.ts"],"sourcesContent":["import { Array as Arr, Encoding, Match, Option, Order, Result, Schema, pipe } from \"effect\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport { JsonParseError } from \"@effect-uai/core/JSONL\"\nimport type { Turn } from \"@effect-uai/core/Turn\"\n\n// ---------------------------------------------------------------------------\n// Wire schemas - subset of Anthropic Messages API we consume.\n// Reference: https://platform.claude.com/docs/en/api/messages\n// ---------------------------------------------------------------------------\n\nconst WireTextBlock = Schema.Struct({\n type: Schema.Literal(\"text\"),\n text: Schema.String,\n})\n\nconst WireToolUseBlock = Schema.Struct({\n type: Schema.Literal(\"tool_use\"),\n id: Schema.String,\n name: Schema.String,\n input: Schema.Unknown,\n})\n\nconst WireThinkingBlock = Schema.Struct({\n type: Schema.Literal(\"thinking\"),\n thinking: Schema.String,\n signature: Schema.optional(Schema.String),\n})\n\nconst WireRedactedThinkingBlock = Schema.Struct({\n type: Schema.Literal(\"redacted_thinking\"),\n data: Schema.String,\n})\n\nexport const WireContentBlock = Schema.Union([\n WireTextBlock,\n WireToolUseBlock,\n WireThinkingBlock,\n WireRedactedThinkingBlock,\n])\nexport type WireContentBlock = typeof WireContentBlock.Type\n\nconst WireUsage = Schema.Struct({\n input_tokens: Schema.optional(Schema.Number),\n output_tokens: Schema.optional(Schema.Number),\n cache_creation_input_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n cache_read_input_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n})\nexport type WireUsage = typeof WireUsage.Type\n\n// ---------------------------------------------------------------------------\n// History → request body\n// ---------------------------------------------------------------------------\n\ntype RequestTextContent = {\n readonly type: \"text\"\n readonly text: string\n}\n\ntype RequestToolResultContent = {\n readonly type: \"tool_result\"\n readonly tool_use_id: string\n readonly content: string\n}\n\ntype RequestToolUseContent = {\n readonly type: \"tool_use\"\n readonly id: string\n readonly name: string\n readonly input: unknown\n}\n\ntype RequestThinkingContent = {\n readonly type: \"thinking\"\n readonly thinking: string\n readonly signature?: string\n}\n\ntype RequestRedactedThinkingContent = {\n readonly type: \"redacted_thinking\"\n readonly data: string\n}\n\ntype RequestImageContent = {\n readonly type: \"image\"\n readonly source:\n | { readonly type: \"url\"; readonly url: string }\n | { readonly type: \"base64\"; readonly media_type: string; readonly data: string }\n}\n\ntype RequestUserContentBlock = RequestTextContent | RequestToolResultContent | RequestImageContent\n\ntype RequestAssistantContentBlock =\n | RequestTextContent\n | RequestToolUseContent\n | RequestThinkingContent\n | RequestRedactedThinkingContent\n\ntype RequestUserMessage = {\n readonly role: \"user\"\n readonly content: ReadonlyArray<RequestUserContentBlock>\n}\n\ntype RequestAssistantMessage = {\n readonly role: \"assistant\"\n readonly content: ReadonlyArray<RequestAssistantContentBlock>\n}\n\ntype RequestMessage = RequestUserMessage | RequestAssistantMessage\n\nconst blockText = Match.type<Items.ContentBlock>().pipe(\n Match.discriminatorsExhaustive(\"type\")({\n input_text: (b) => b.text,\n input_image: () => \"\",\n output_text: (b) => b.text,\n refusal: (b) => b.text,\n }),\n)\n\nconst messageText = (message: Items.Message): string => message.content.map(blockText).join(\"\")\n\nconst imageSourceToWire = Match.type<Items.InputImage[\"source\"]>().pipe(\n Match.tag(\"url\", (s): RequestImageContent[\"source\"] => ({ type: \"url\", url: s.url })),\n Match.tag(\"base64\", (s): RequestImageContent[\"source\"] => ({\n type: \"base64\",\n media_type: s.mimeType,\n data: s.base64,\n })),\n Match.tag(\"bytes\", (s): RequestImageContent[\"source\"] => ({\n type: \"base64\",\n media_type: s.mimeType,\n data: Encoding.encodeBase64(s.bytes),\n })),\n Match.exhaustive,\n)\n\nconst userContentBlock = (\n block: Items.ContentBlock,\n): Result.Result<RequestUserContentBlock, void> =>\n Match.value(block).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n input_text: (b) =>\n b.text.length === 0\n ? Result.failVoid\n : Result.succeed({ type: \"text\" as const, text: b.text }),\n input_image: (b) =>\n Result.succeed({ type: \"image\" as const, source: imageSourceToWire(b.source) }),\n // Assistant content; never appears on a user message in practice. Skip.\n output_text: () => Result.failVoid,\n refusal: () => Result.failVoid,\n }),\n )\n\nconst parseJson = (s: string): Result.Result<unknown, JsonParseError> =>\n Result.try({\n try: () => JSON.parse(s) as unknown,\n catch: (cause) => new JsonParseError({ line: s, cause }),\n })\n\ntype RoleBucket = \"user\" | \"assistant\" | \"system\"\n\nconst roleBucket = (item: Items.HistoryItem): RoleBucket =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m) => m.role,\n function_call: () => \"assistant\" as const,\n function_call_output: () => \"user\" as const,\n reasoning: () => \"assistant\" as const,\n }),\n )\n\nconst itemToUserBlocks = (item: Items.HistoryItem): ReadonlyArray<RequestUserContentBlock> =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m): ReadonlyArray<RequestUserContentBlock> =>\n m.role === \"user\" ? pipe(m.content, Arr.filterMap(userContentBlock)) : [],\n function_call: (): ReadonlyArray<RequestUserContentBlock> => [],\n function_call_output: (o): ReadonlyArray<RequestUserContentBlock> => [\n { type: \"tool_result\", tool_use_id: o.call_id, content: o.output },\n ],\n reasoning: (): ReadonlyArray<RequestUserContentBlock> => [],\n }),\n )\n\nconst itemToAssistantBlocks = (\n item: Items.HistoryItem,\n): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> => {\n const text = messageText(m)\n return Result.succeed(\n m.role === \"assistant\" && text.length > 0 ? [{ type: \"text\", text }] : [],\n )\n },\n function_call: (f) =>\n pipe(\n parseJson(f.arguments),\n Result.map(\n (input): ReadonlyArray<RequestAssistantContentBlock> => [\n { type: \"tool_use\", id: f.call_id, name: f.name, input },\n ],\n ),\n ),\n function_call_output: (): Result.Result<\n ReadonlyArray<RequestAssistantContentBlock>,\n JsonParseError\n > => Result.succeed([]),\n reasoning: (r) => {\n const blocks: ReadonlyArray<RequestAssistantContentBlock> =\n r.summary !== undefined\n ? [\n {\n type: \"thinking\",\n thinking: r.summary,\n ...(r.signature !== undefined && { signature: r.signature }),\n },\n ]\n : r.signature !== undefined\n ? [{ type: \"redacted_thinking\", data: r.signature }]\n : []\n return Result.succeed(blocks)\n },\n }),\n )\n\ntype GroupAcc = {\n readonly messages: ReadonlyArray<RequestMessage>\n readonly currentRole: Option.Option<\"user\" | \"assistant\">\n readonly userBuf: ReadonlyArray<RequestUserContentBlock>\n readonly assistantBuf: ReadonlyArray<RequestAssistantContentBlock>\n}\n\nconst flushAcc = (acc: GroupAcc): ReadonlyArray<RequestMessage> =>\n Option.match(acc.currentRole, {\n onNone: () => acc.messages,\n onSome: (role) =>\n role === \"user\" && acc.userBuf.length > 0\n ? [...acc.messages, { role: \"user\", content: acc.userBuf }]\n : role === \"assistant\" && acc.assistantBuf.length > 0\n ? [...acc.messages, { role: \"assistant\", content: acc.assistantBuf }]\n : acc.messages,\n })\n\nconst appendUser = (acc: GroupAcc, blocks: ReadonlyArray<RequestUserContentBlock>): GroupAcc =>\n blocks.length === 0\n ? acc\n : Option.isSome(acc.currentRole) && acc.currentRole.value === \"user\"\n ? { ...acc, userBuf: [...acc.userBuf, ...blocks] }\n : {\n messages: flushAcc(acc),\n currentRole: Option.some(\"user\"),\n userBuf: blocks,\n assistantBuf: [],\n }\n\nconst appendAssistant = (\n acc: GroupAcc,\n blocks: ReadonlyArray<RequestAssistantContentBlock>,\n): GroupAcc =>\n blocks.length === 0\n ? acc\n : Option.isSome(acc.currentRole) && acc.currentRole.value === \"assistant\"\n ? { ...acc, assistantBuf: [...acc.assistantBuf, ...blocks] }\n : {\n messages: flushAcc(acc),\n currentRole: Option.some(\"assistant\"),\n userBuf: [],\n assistantBuf: blocks,\n }\n\nconst groupStep = (\n acc: GroupAcc,\n item: Items.HistoryItem,\n): Result.Result<GroupAcc, JsonParseError> => {\n const bucket = roleBucket(item)\n if (bucket === \"system\") return Result.succeed(acc)\n if (bucket === \"user\") {\n return Result.succeed(appendUser(acc, itemToUserBlocks(item)))\n }\n return pipe(\n itemToAssistantBlocks(item),\n Result.map((blocks) => appendAssistant(acc, blocks)),\n )\n}\n\n/**\n * Group consecutive same-role items into Anthropic-shaped messages.\n * Anthropic requires strict user/assistant alternation; consecutive items\n * from the same role are folded into one message's `content`. Fails if any\n * `function_call.arguments` is not valid JSON, since Anthropic's wire shape\n * requires an object input.\n */\nconst groupedMessages = (\n history: ReadonlyArray<Items.HistoryItem>,\n): Result.Result<ReadonlyArray<RequestMessage>, JsonParseError> => {\n const initial: Result.Result<GroupAcc, JsonParseError> = Result.succeed({\n messages: [],\n currentRole: Option.none(),\n userBuf: [],\n assistantBuf: [],\n })\n return pipe(\n Arr.reduce(history, initial, (acc, item) => Result.flatMap(acc, (a) => groupStep(a, item))),\n Result.map(flushAcc),\n )\n}\n\nconst isSystemMessage = (item: Items.HistoryItem): item is Items.Message =>\n item.type === \"message\" && item.role === \"system\"\n\nconst systemFromHistory = (history: ReadonlyArray<Items.HistoryItem>): Option.Option<string> => {\n const texts = pipe(\n history,\n Arr.filterMap((item) =>\n isSystemMessage(item) ? Result.succeed(messageText(item)) : Result.failVoid,\n ),\n Arr.filter((s) => s.length > 0),\n )\n return texts.length === 0 ? Option.none() : Option.some(texts.join(\"\\n\"))\n}\n\nexport type ThinkingConfig = {\n readonly type: \"enabled\"\n readonly budget_tokens: number\n}\n\n/** Cache lifetime. `1h` writes cost 2x base input; `5m` costs 1.25x. */\nexport type CacheTtl = \"5m\" | \"1h\"\n\n/**\n * Request-level cache breakpoint. Anthropic applies it to the last cacheable\n * block and moves it forward as the conversation grows, so the cached prefix\n * (`tools` → `system` → `messages`) follows the conversation without the\n * caller re-placing anything.\n */\nexport type CacheControl = {\n readonly type: \"ephemeral\"\n readonly ttl?: CacheTtl\n}\n\nexport type RequestBody = {\n readonly model: string\n readonly messages: ReadonlyArray<RequestMessage>\n readonly max_tokens: number\n readonly system?: string\n readonly temperature?: number\n readonly top_p?: number\n readonly top_k?: number\n readonly stop_sequences?: ReadonlyArray<string>\n readonly thinking?: ThinkingConfig\n readonly tools?: ReadonlyArray<Record<string, unknown>>\n readonly tool_choice?: Record<string, unknown>\n readonly metadata?: { readonly user_id: string }\n readonly output_config?: Record<string, unknown>\n readonly cache_control?: CacheControl\n readonly stream: true\n}\n\nexport const buildRequestBody = (params: {\n readonly model: string\n readonly history: ReadonlyArray<Items.HistoryItem>\n readonly maxTokens: number\n readonly temperature: Option.Option<number>\n readonly topP: Option.Option<number>\n readonly topK: Option.Option<number>\n readonly stopSequences: Option.Option<ReadonlyArray<string>>\n readonly thinking: Option.Option<ThinkingConfig>\n readonly tools: Option.Option<ReadonlyArray<Record<string, unknown>>>\n readonly toolChoice: Option.Option<Record<string, unknown>>\n readonly userId: Option.Option<string>\n readonly outputConfig: Option.Option<Record<string, unknown>>\n readonly cacheControl: Option.Option<CacheControl>\n}): Result.Result<RequestBody, JsonParseError> =>\n pipe(\n groupedMessages(params.history),\n Result.map(\n (messages): RequestBody => ({\n model: params.model,\n messages,\n max_tokens: params.maxTokens,\n ...Option.match(systemFromHistory(params.history), {\n onNone: () => ({}),\n onSome: (system) => ({ system }),\n }),\n ...Option.match(params.temperature, {\n onNone: () => ({}),\n onSome: (temperature) => ({ temperature }),\n }),\n ...Option.match(params.topP, {\n onNone: () => ({}),\n onSome: (top_p) => ({ top_p }),\n }),\n ...Option.match(params.topK, {\n onNone: () => ({}),\n onSome: (top_k) => ({ top_k }),\n }),\n ...Option.match(params.stopSequences, {\n onNone: () => ({}),\n onSome: (stop_sequences) => ({ stop_sequences }),\n }),\n ...Option.match(params.thinking, {\n onNone: () => ({}),\n onSome: (thinking) => ({ thinking }),\n }),\n ...Option.match(params.tools, {\n onNone: () => ({}),\n onSome: (tools) => ({ tools }),\n }),\n ...Option.match(params.toolChoice, {\n onNone: () => ({}),\n onSome: (tool_choice) => ({ tool_choice }),\n }),\n ...Option.match(params.userId, {\n onNone: () => ({}),\n onSome: (user_id) => ({ metadata: { user_id } }),\n }),\n ...Option.match(params.outputConfig, {\n onNone: () => ({}),\n onSome: (output_config) => ({ output_config }),\n }),\n ...Option.match(params.cacheControl, {\n onNone: () => ({}),\n onSome: (cache_control) => ({ cache_control }),\n }),\n stream: true,\n }),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Stream-level state - assemble content blocks index-by-index, then emit\n// our `Items.HistoryItem[]` when `message_stop` lands.\n// ---------------------------------------------------------------------------\n\ntype BlockBuffer = {\n readonly type: WireContentBlock[\"type\"]\n readonly text: string\n readonly inputJson: string\n readonly thinking: string\n readonly signature: string\n readonly id: Option.Option<string>\n readonly name: Option.Option<string>\n readonly redactedData: Option.Option<string>\n}\n\nconst emptyBlock = (type: WireContentBlock[\"type\"]): BlockBuffer => ({\n type,\n text: \"\",\n inputJson: \"\",\n thinking: \"\",\n signature: \"\",\n id: Option.none(),\n name: Option.none(),\n redactedData: Option.none(),\n})\n\nexport type Accumulator = {\n readonly blocks: Readonly<Record<number, BlockBuffer>>\n readonly stopReason: Option.Option<string>\n readonly usage: Items.Usage\n}\n\nexport const emptyAccumulator: Accumulator = {\n blocks: {},\n stopReason: Option.none(),\n usage: {},\n}\n\nconst replaceBlock = (acc: Accumulator, index: number, block: BlockBuffer): Accumulator => ({\n ...acc,\n blocks: { ...acc.blocks, [index]: block },\n})\n\nconst updateBlock = (\n acc: Accumulator,\n index: number,\n patch: (block: BlockBuffer) => BlockBuffer,\n): Accumulator => replaceBlock(acc, index, patch(acc.blocks[index] ?? emptyBlock(\"text\")))\n\nexport const startBlock = (acc: Accumulator, index: number, block: WireContentBlock): Accumulator =>\n Match.value(block).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n text: () => replaceBlock(acc, index, emptyBlock(\"text\")),\n tool_use: (b) =>\n replaceBlock(acc, index, {\n ...emptyBlock(\"tool_use\"),\n id: Option.some(b.id),\n name: Option.some(b.name),\n inputJson: typeof b.input === \"string\" ? b.input : \"\",\n }),\n thinking: () => replaceBlock(acc, index, emptyBlock(\"thinking\")),\n redacted_thinking: (b) =>\n replaceBlock(acc, index, {\n ...emptyBlock(\"redacted_thinking\"),\n redactedData: Option.some(b.data),\n }),\n }),\n )\n\nexport const appendTextDelta = (acc: Accumulator, index: number, text: string): Accumulator =>\n updateBlock(acc, index, (b) => ({ ...b, text: b.text + text }))\n\nexport const appendInputJsonDelta = (\n acc: Accumulator,\n index: number,\n partial: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, inputJson: b.inputJson + partial }))\n\nexport const appendThinkingDelta = (\n acc: Accumulator,\n index: number,\n thinking: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, thinking: b.thinking + thinking }))\n\nexport const appendSignatureDelta = (\n acc: Accumulator,\n index: number,\n signature: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, signature: b.signature + signature }))\n\nexport const setStopReason = (acc: Accumulator, reason: string): Accumulator => ({\n ...acc,\n stopReason: Option.some(reason),\n})\n\n// Anthropic splits input across three billed buckets: `input_tokens`\n// (post-breakpoint), cache reads, and cache writes. All three are kept;\n// `input_tokens` is not the input total. Each field carries over from the\n// prior value since a streamed turn spreads usage across events (input +\n// cache on message_start, output_tokens on message_delta), so total_tokens is\n// summed from the accumulated buckets, not one event.\nexport const mergeUsage = (acc: Accumulator, wire: WireUsage): Accumulator => {\n const prev = acc.usage\n const input_tokens = wire.input_tokens ?? prev.input_tokens\n const output_tokens = wire.output_tokens ?? prev.output_tokens\n const cached_tokens = wire.cache_read_input_tokens ?? prev.input_tokens_details?.cached_tokens\n const cache_write_tokens =\n wire.cache_creation_input_tokens ?? prev.input_tokens_details?.cache_write_tokens\n\n const buckets = [input_tokens, cached_tokens, cache_write_tokens, output_tokens]\n const total_tokens = buckets.every((v) => v === undefined)\n ? undefined\n : buckets.reduce<number>((sum, v) => sum + (v ?? 0), 0)\n\n const details: Items.InputTokensDetails = {\n ...(cached_tokens !== undefined && { cached_tokens }),\n ...(cache_write_tokens !== undefined && { cache_write_tokens }),\n }\n const usage: Items.Usage = {\n ...(input_tokens !== undefined && { input_tokens }),\n ...(output_tokens !== undefined && { output_tokens }),\n ...(total_tokens !== undefined && { total_tokens }),\n ...(Object.keys(details).length > 0 && { input_tokens_details: details }),\n }\n return { ...acc, usage }\n}\n\nconst stopReasonFromAnthropic = (reason: Option.Option<string>): Turn[\"stop_reason\"] =>\n Option.match(reason, {\n onNone: () => \"stop\" as const,\n onSome: (r) =>\n Match.value(r).pipe(\n Match.when(\"tool_use\", () => \"tool_calls\" as const),\n Match.when(\"max_tokens\", () => \"max_tokens\" as const),\n Match.orElse(() => \"stop\" as const),\n ),\n })\n\nconst blocksByIndex = (acc: Accumulator): ReadonlyArray<BlockBuffer> =>\n pipe(\n Object.keys(acc.blocks),\n Arr.map((k) => Number(k)),\n Arr.sort(Order.Number),\n Arr.map((i) => acc.blocks[i]!),\n )\n\nconst blockToItems = (block: BlockBuffer): ReadonlyArray<Items.HistoryItem> =>\n Match.value(block.type).pipe(\n Match.when(\n \"text\",\n (): ReadonlyArray<Items.HistoryItem> =>\n block.text.length === 0\n ? []\n : [\n {\n type: \"message\",\n role: \"assistant\",\n content: [{ type: \"output_text\", text: block.text }],\n },\n ],\n ),\n Match.when(\n \"tool_use\",\n (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"function_call\",\n call_id: Option.getOrElse(block.id, () => \"\"),\n name: Option.getOrElse(block.name, () => \"\"),\n arguments: block.inputJson,\n },\n ],\n ),\n Match.when(\n \"thinking\",\n (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"reasoning\",\n ...(block.thinking.length > 0 && { summary: block.thinking }),\n ...(block.signature.length > 0 && { signature: block.signature }),\n },\n ],\n ),\n Match.when(\n \"redacted_thinking\",\n (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"reasoning\",\n ...Option.match(block.redactedData, {\n onNone: () => ({}),\n onSome: (signature) => ({ signature }),\n }),\n },\n ],\n ),\n Match.exhaustive,\n )\n\ntype MergeAcc = {\n readonly out: ReadonlyArray<Items.HistoryItem>\n}\n\nconst mergeStep = (acc: MergeAcc, item: Items.HistoryItem): MergeAcc => {\n const last = Arr.last(acc.out)\n if (\n Option.isSome(last) &&\n last.value.type === \"message\" &&\n last.value.role === \"assistant\" &&\n item.type === \"message\" &&\n item.role === \"assistant\"\n ) {\n const merged: Items.Message = {\n ...last.value,\n content: [...last.value.content, ...item.content],\n }\n return { out: [...acc.out.slice(0, -1), merged] }\n }\n return { out: [...acc.out, item] }\n}\n\nconst mergeAdjacentAssistantText = (\n items: ReadonlyArray<Items.HistoryItem>,\n): ReadonlyArray<Items.HistoryItem> => Arr.reduce(items, { out: [] } as MergeAcc, mergeStep).out\n\nexport const accumulatorToTurn = (acc: Accumulator): Turn => ({\n items: pipe(blocksByIndex(acc), Arr.flatMap(blockToItems), mergeAdjacentAssistantText),\n usage: acc.usage,\n stop_reason: stopReasonFromAnthropic(acc.stopReason),\n})\n"],"mappings":";;;;;;;;;;;;;;;;;AAUA,MAAM,gBAAgB,OAAO,OAAO;CAClC,MAAM,OAAO,QAAQ,OAAO;CAC5B,MAAM,OAAO;CACd,CAAC;AAEF,MAAM,mBAAmB,OAAO,OAAO;CACrC,MAAM,OAAO,QAAQ,WAAW;CAChC,IAAI,OAAO;CACX,MAAM,OAAO;CACb,OAAO,OAAO;CACf,CAAC;AAEF,MAAM,oBAAoB,OAAO,OAAO;CACtC,MAAM,OAAO,QAAQ,WAAW;CAChC,UAAU,OAAO;CACjB,WAAW,OAAO,SAAS,OAAO,OAAO;CAC1C,CAAC;AAEF,MAAM,4BAA4B,OAAO,OAAO;CAC9C,MAAM,OAAO,QAAQ,oBAAoB;CACzC,MAAM,OAAO;CACd,CAAC;AAEF,MAAa,mBAAmB,OAAO,MAAM;CAC3C;CACA;CACA;CACA;CACD,CAAC;AAGgB,OAAO,OAAO;CAC9B,cAAc,OAAO,SAAS,OAAO,OAAO;CAC5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AI,MAAM,UAAU,SAAS,KAAK,EAAE,WAAW,MAAM,WAAW;CACjE,CACF,CACF,EACD,MAAM,KACJ,2BACwC,CACtC;CACE,MAAM;CACN,GAAG,OAAO,MAAM,MAAM,cAAc;EAClC,eAAe,EAAE;EACjB,SAAS,eAAe,EAAE,WAAW;EACtC,CAAC;CACH,CACF,CACF,EACD,MAAM,WACP;AAMH,MAAM,aAAa,KAAe,SAAsC;CACtE,MAAM,OAAOA,MAAI,KAAK,IAAI,IAAI;AAC9B,KACE,OAAO,OAAO,KAAK,IACnB,KAAK,MAAM,SAAS,aACpB,KAAK,MAAM,SAAS,eACpB,KAAK,SAAS,aACd,KAAK,SAAS,aACd;EACA,MAAM,SAAwB;GAC5B,GAAG,KAAK;GACR,SAAS,CAAC,GAAG,KAAK,MAAM,SAAS,GAAG,KAAK,QAAQ;GAClD;AACD,SAAO,EAAE,KAAK,CAAC,GAAG,IAAI,IAAI,MAAM,GAAG,GAAG,EAAE,OAAO,EAAE;;AAEnD,QAAO,EAAE,KAAK,CAAC,GAAG,IAAI,KAAK,KAAK,EAAE;;AAGpC,MAAM,8BACJ,UACqCA,MAAI,OAAO,OAAO,EAAE,KAAK,EAAE,EAAE,EAAc,UAAU,CAAC;AAE7F,MAAa,qBAAqB,SAA4B;CAC5D,OAAO,KAAK,cAAc,IAAI,EAAEA,MAAI,QAAQ,aAAa,EAAE,2BAA2B;CACtF,OAAO,IAAI;CACX,aAAa,wBAAwB,IAAI,WAAW;CACrD"}
1
+ {"version":3,"file":"codec.mjs","names":["Arr"],"sources":["../src/codec.ts"],"sourcesContent":["import { Array as Arr, Encoding, Match, Option, Order, Result, Schema, pipe } from \"effect\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport { JsonParseError } from \"@effect-uai/core/JSONL\"\nimport type { Turn } from \"@effect-uai/core/Turn\"\n\n// ---------------------------------------------------------------------------\n// Wire schemas - subset of Anthropic Messages API we consume.\n// Reference: https://platform.claude.com/docs/en/api/messages\n// ---------------------------------------------------------------------------\n\nconst WireTextBlock = Schema.Struct({\n type: Schema.Literal(\"text\"),\n text: Schema.String,\n})\n\nconst WireToolUseBlock = Schema.Struct({\n type: Schema.Literal(\"tool_use\"),\n id: Schema.String,\n name: Schema.String,\n input: Schema.Unknown,\n})\n\nconst WireThinkingBlock = Schema.Struct({\n type: Schema.Literal(\"thinking\"),\n thinking: Schema.String,\n signature: Schema.optional(Schema.String),\n})\n\nconst WireRedactedThinkingBlock = Schema.Struct({\n type: Schema.Literal(\"redacted_thinking\"),\n data: Schema.String,\n})\n\nexport const WireContentBlock = Schema.Union([\n WireTextBlock,\n WireToolUseBlock,\n WireThinkingBlock,\n WireRedactedThinkingBlock,\n])\nexport type WireContentBlock = typeof WireContentBlock.Type\n\nconst WireUsage = Schema.Struct({\n input_tokens: Schema.optional(Schema.Number),\n output_tokens: Schema.optional(Schema.Number),\n cache_creation_input_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n cache_read_input_tokens: Schema.optional(Schema.NullOr(Schema.Number)),\n})\nexport type WireUsage = typeof WireUsage.Type\n\n// ---------------------------------------------------------------------------\n// History → request body\n// ---------------------------------------------------------------------------\n\ntype RequestTextContent = {\n readonly type: \"text\"\n readonly text: string\n}\n\ntype RequestToolResultContent = {\n readonly type: \"tool_result\"\n readonly tool_use_id: string\n readonly content: string\n}\n\ntype RequestToolUseContent = {\n readonly type: \"tool_use\"\n readonly id: string\n readonly name: string\n readonly input: unknown\n}\n\ntype RequestThinkingContent = {\n readonly type: \"thinking\"\n readonly thinking: string\n readonly signature?: string\n}\n\ntype RequestRedactedThinkingContent = {\n readonly type: \"redacted_thinking\"\n readonly data: string\n}\n\ntype RequestImageContent = {\n readonly type: \"image\"\n readonly source:\n | { readonly type: \"url\"; readonly url: string }\n | { readonly type: \"base64\"; readonly media_type: string; readonly data: string }\n}\n\ntype RequestUserContentBlock = RequestTextContent | RequestToolResultContent | RequestImageContent\n\ntype RequestAssistantContentBlock =\n | RequestTextContent\n | RequestToolUseContent\n | RequestThinkingContent\n | RequestRedactedThinkingContent\n\ntype RequestUserMessage = {\n readonly role: \"user\"\n readonly content: ReadonlyArray<RequestUserContentBlock>\n}\n\ntype RequestAssistantMessage = {\n readonly role: \"assistant\"\n readonly content: ReadonlyArray<RequestAssistantContentBlock>\n}\n\ntype RequestMessage = RequestUserMessage | RequestAssistantMessage\n\nconst blockText = Match.type<Items.ContentBlock>().pipe(\n Match.discriminatorsExhaustive(\"type\")({\n input_text: (b) => b.text,\n input_image: () => \"\",\n output_text: (b) => b.text,\n refusal: (b) => b.text,\n }),\n)\n\nconst messageText = (message: Items.Message): string => message.content.map(blockText).join(\"\")\n\nconst imageSourceToWire = Match.type<Items.InputImage[\"source\"]>().pipe(\n Match.tag(\"url\", (s): RequestImageContent[\"source\"] => ({ type: \"url\", url: s.url })),\n Match.tag(\"base64\", (s): RequestImageContent[\"source\"] => ({\n type: \"base64\",\n media_type: s.mimeType,\n data: s.base64,\n })),\n Match.tag(\"bytes\", (s): RequestImageContent[\"source\"] => ({\n type: \"base64\",\n media_type: s.mimeType,\n data: Encoding.encodeBase64(s.bytes),\n })),\n Match.exhaustive,\n)\n\nconst userContentBlock = (\n block: Items.ContentBlock,\n): Result.Result<RequestUserContentBlock, void> =>\n Match.value(block).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n input_text: (b) =>\n b.text.length === 0\n ? Result.failVoid\n : Result.succeed({ type: \"text\" as const, text: b.text }),\n input_image: (b) =>\n Result.succeed({ type: \"image\" as const, source: imageSourceToWire(b.source) }),\n // Assistant content; never appears on a user message in practice. Skip.\n output_text: () => Result.failVoid,\n refusal: () => Result.failVoid,\n }),\n )\n\nconst parseJson = (s: string): Result.Result<unknown, JsonParseError> =>\n Result.try({\n try: () => JSON.parse(s) as unknown,\n catch: (cause) => new JsonParseError({ line: s, cause }),\n })\n\ntype RoleBucket = \"user\" | \"assistant\" | \"system\"\n\nconst roleBucket = (item: Items.HistoryItem): RoleBucket =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m) => m.role,\n function_call: () => \"assistant\" as const,\n function_call_output: () => \"user\" as const,\n reasoning: () => \"assistant\" as const,\n }),\n )\n\nconst itemToUserBlocks = (item: Items.HistoryItem): ReadonlyArray<RequestUserContentBlock> =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m): ReadonlyArray<RequestUserContentBlock> =>\n m.role === \"user\" ? pipe(m.content, Arr.filterMap(userContentBlock)) : [],\n function_call: (): ReadonlyArray<RequestUserContentBlock> => [],\n function_call_output: (o): ReadonlyArray<RequestUserContentBlock> => [\n { type: \"tool_result\", tool_use_id: o.call_id, content: o.output },\n ],\n reasoning: (): ReadonlyArray<RequestUserContentBlock> => [],\n }),\n )\n\nconst itemToAssistantBlocks = (\n item: Items.HistoryItem,\n): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> =>\n Match.value(item).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n message: (m): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> => {\n const text = messageText(m)\n return Result.succeed(\n m.role === \"assistant\" && text.length > 0 ? [{ type: \"text\", text }] : [],\n )\n },\n function_call: (f) =>\n pipe(\n parseJson(f.arguments),\n Result.map((input): ReadonlyArray<RequestAssistantContentBlock> => [\n { type: \"tool_use\", id: f.call_id, name: f.name, input },\n ]),\n ),\n function_call_output: (): Result.Result<\n ReadonlyArray<RequestAssistantContentBlock>,\n JsonParseError\n > => Result.succeed([]),\n reasoning: (r) => {\n const blocks: ReadonlyArray<RequestAssistantContentBlock> =\n r.summary !== undefined\n ? [\n {\n type: \"thinking\",\n thinking: r.summary,\n ...(r.signature !== undefined && { signature: r.signature }),\n },\n ]\n : r.signature !== undefined\n ? [{ type: \"redacted_thinking\", data: r.signature }]\n : []\n return Result.succeed(blocks)\n },\n }),\n )\n\ntype GroupAcc = {\n readonly messages: ReadonlyArray<RequestMessage>\n readonly currentRole: Option.Option<\"user\" | \"assistant\">\n readonly userBuf: ReadonlyArray<RequestUserContentBlock>\n readonly assistantBuf: ReadonlyArray<RequestAssistantContentBlock>\n}\n\nconst flushAcc = (acc: GroupAcc): ReadonlyArray<RequestMessage> =>\n Option.match(acc.currentRole, {\n onNone: () => acc.messages,\n onSome: (role) =>\n role === \"user\" && acc.userBuf.length > 0\n ? [...acc.messages, { role: \"user\", content: acc.userBuf }]\n : role === \"assistant\" && acc.assistantBuf.length > 0\n ? [...acc.messages, { role: \"assistant\", content: acc.assistantBuf }]\n : acc.messages,\n })\n\nconst appendUser = (acc: GroupAcc, blocks: ReadonlyArray<RequestUserContentBlock>): GroupAcc =>\n blocks.length === 0\n ? acc\n : Option.isSome(acc.currentRole) && acc.currentRole.value === \"user\"\n ? { ...acc, userBuf: [...acc.userBuf, ...blocks] }\n : {\n messages: flushAcc(acc),\n currentRole: Option.some(\"user\"),\n userBuf: blocks,\n assistantBuf: [],\n }\n\nconst appendAssistant = (\n acc: GroupAcc,\n blocks: ReadonlyArray<RequestAssistantContentBlock>,\n): GroupAcc =>\n blocks.length === 0\n ? acc\n : Option.isSome(acc.currentRole) && acc.currentRole.value === \"assistant\"\n ? { ...acc, assistantBuf: [...acc.assistantBuf, ...blocks] }\n : {\n messages: flushAcc(acc),\n currentRole: Option.some(\"assistant\"),\n userBuf: [],\n assistantBuf: blocks,\n }\n\nconst groupStep = (\n acc: GroupAcc,\n item: Items.HistoryItem,\n): Result.Result<GroupAcc, JsonParseError> => {\n const bucket = roleBucket(item)\n if (bucket === \"system\") return Result.succeed(acc)\n if (bucket === \"user\") {\n return Result.succeed(appendUser(acc, itemToUserBlocks(item)))\n }\n return pipe(\n itemToAssistantBlocks(item),\n Result.map((blocks) => appendAssistant(acc, blocks)),\n )\n}\n\n/**\n * Group consecutive same-role items into Anthropic-shaped messages.\n * Anthropic requires strict user/assistant alternation; consecutive items\n * from the same role are folded into one message's `content`. Fails if any\n * `function_call.arguments` is not valid JSON, since Anthropic's wire shape\n * requires an object input.\n */\nconst groupedMessages = (\n history: ReadonlyArray<Items.HistoryItem>,\n): Result.Result<ReadonlyArray<RequestMessage>, JsonParseError> => {\n const initial: Result.Result<GroupAcc, JsonParseError> = Result.succeed({\n messages: [],\n currentRole: Option.none(),\n userBuf: [],\n assistantBuf: [],\n })\n return pipe(\n Arr.reduce(history, initial, (acc, item) => Result.flatMap(acc, (a) => groupStep(a, item))),\n Result.map(flushAcc),\n )\n}\n\nconst isSystemMessage = (item: Items.HistoryItem): item is Items.Message =>\n item.type === \"message\" && item.role === \"system\"\n\nconst systemFromHistory = (history: ReadonlyArray<Items.HistoryItem>): Option.Option<string> => {\n const texts = pipe(\n history,\n Arr.filterMap((item) =>\n isSystemMessage(item) ? Result.succeed(messageText(item)) : Result.failVoid,\n ),\n Arr.filter((s) => s.length > 0),\n )\n return texts.length === 0 ? Option.none() : Option.some(texts.join(\"\\n\"))\n}\n\nexport type ThinkingConfig = {\n readonly type: \"enabled\"\n readonly budget_tokens: number\n}\n\n/** Cache lifetime. `1h` writes cost 2x base input; `5m` costs 1.25x. */\nexport type CacheTtl = \"5m\" | \"1h\"\n\n/**\n * Request-level cache breakpoint. Anthropic applies it to the last cacheable\n * block and moves it forward as the conversation grows, so the cached prefix\n * (`tools` → `system` → `messages`) follows the conversation without the\n * caller re-placing anything.\n */\nexport type CacheControl = {\n readonly type: \"ephemeral\"\n readonly ttl?: CacheTtl\n}\n\nexport type RequestBody = {\n readonly model: string\n readonly messages: ReadonlyArray<RequestMessage>\n readonly max_tokens: number\n readonly system?: string\n readonly temperature?: number\n readonly top_p?: number\n readonly top_k?: number\n readonly stop_sequences?: ReadonlyArray<string>\n readonly thinking?: ThinkingConfig\n readonly tools?: ReadonlyArray<Record<string, unknown>>\n readonly tool_choice?: Record<string, unknown>\n readonly metadata?: { readonly user_id: string }\n readonly output_config?: Record<string, unknown>\n readonly cache_control?: CacheControl\n readonly stream: true\n}\n\nexport const buildRequestBody = (params: {\n readonly model: string\n readonly history: ReadonlyArray<Items.HistoryItem>\n readonly maxTokens: number\n readonly temperature: Option.Option<number>\n readonly topP: Option.Option<number>\n readonly topK: Option.Option<number>\n readonly stopSequences: Option.Option<ReadonlyArray<string>>\n readonly thinking: Option.Option<ThinkingConfig>\n readonly tools: Option.Option<ReadonlyArray<Record<string, unknown>>>\n readonly toolChoice: Option.Option<Record<string, unknown>>\n readonly userId: Option.Option<string>\n readonly outputConfig: Option.Option<Record<string, unknown>>\n readonly cacheControl: Option.Option<CacheControl>\n}): Result.Result<RequestBody, JsonParseError> =>\n pipe(\n groupedMessages(params.history),\n Result.map((messages): RequestBody => ({\n model: params.model,\n messages,\n max_tokens: params.maxTokens,\n ...Option.match(systemFromHistory(params.history), {\n onNone: () => ({}),\n onSome: (system) => ({ system }),\n }),\n ...Option.match(params.temperature, {\n onNone: () => ({}),\n onSome: (temperature) => ({ temperature }),\n }),\n ...Option.match(params.topP, {\n onNone: () => ({}),\n onSome: (top_p) => ({ top_p }),\n }),\n ...Option.match(params.topK, {\n onNone: () => ({}),\n onSome: (top_k) => ({ top_k }),\n }),\n ...Option.match(params.stopSequences, {\n onNone: () => ({}),\n onSome: (stop_sequences) => ({ stop_sequences }),\n }),\n ...Option.match(params.thinking, {\n onNone: () => ({}),\n onSome: (thinking) => ({ thinking }),\n }),\n ...Option.match(params.tools, {\n onNone: () => ({}),\n onSome: (tools) => ({ tools }),\n }),\n ...Option.match(params.toolChoice, {\n onNone: () => ({}),\n onSome: (tool_choice) => ({ tool_choice }),\n }),\n ...Option.match(params.userId, {\n onNone: () => ({}),\n onSome: (user_id) => ({ metadata: { user_id } }),\n }),\n ...Option.match(params.outputConfig, {\n onNone: () => ({}),\n onSome: (output_config) => ({ output_config }),\n }),\n ...Option.match(params.cacheControl, {\n onNone: () => ({}),\n onSome: (cache_control) => ({ cache_control }),\n }),\n stream: true,\n })),\n )\n\n// ---------------------------------------------------------------------------\n// Stream-level state - assemble content blocks index-by-index, then emit\n// our `Items.HistoryItem[]` when `message_stop` lands.\n// ---------------------------------------------------------------------------\n\ntype BlockBuffer = {\n readonly type: WireContentBlock[\"type\"]\n readonly text: string\n readonly inputJson: string\n readonly thinking: string\n readonly signature: string\n readonly id: Option.Option<string>\n readonly name: Option.Option<string>\n readonly redactedData: Option.Option<string>\n}\n\nconst emptyBlock = (type: WireContentBlock[\"type\"]): BlockBuffer => ({\n type,\n text: \"\",\n inputJson: \"\",\n thinking: \"\",\n signature: \"\",\n id: Option.none(),\n name: Option.none(),\n redactedData: Option.none(),\n})\n\nexport type Accumulator = {\n readonly blocks: Readonly<Record<number, BlockBuffer>>\n readonly stopReason: Option.Option<string>\n readonly usage: Items.Usage\n}\n\nexport const emptyAccumulator: Accumulator = {\n blocks: {},\n stopReason: Option.none(),\n usage: {},\n}\n\nconst replaceBlock = (acc: Accumulator, index: number, block: BlockBuffer): Accumulator => ({\n ...acc,\n blocks: { ...acc.blocks, [index]: block },\n})\n\nconst updateBlock = (\n acc: Accumulator,\n index: number,\n patch: (block: BlockBuffer) => BlockBuffer,\n): Accumulator => replaceBlock(acc, index, patch(acc.blocks[index] ?? emptyBlock(\"text\")))\n\nexport const startBlock = (acc: Accumulator, index: number, block: WireContentBlock): Accumulator =>\n Match.value(block).pipe(\n Match.discriminatorsExhaustive(\"type\")({\n text: () => replaceBlock(acc, index, emptyBlock(\"text\")),\n tool_use: (b) =>\n replaceBlock(acc, index, {\n ...emptyBlock(\"tool_use\"),\n id: Option.some(b.id),\n name: Option.some(b.name),\n inputJson: typeof b.input === \"string\" ? b.input : \"\",\n }),\n thinking: () => replaceBlock(acc, index, emptyBlock(\"thinking\")),\n redacted_thinking: (b) =>\n replaceBlock(acc, index, {\n ...emptyBlock(\"redacted_thinking\"),\n redactedData: Option.some(b.data),\n }),\n }),\n )\n\nexport const appendTextDelta = (acc: Accumulator, index: number, text: string): Accumulator =>\n updateBlock(acc, index, (b) => ({ ...b, text: b.text + text }))\n\nexport const appendInputJsonDelta = (\n acc: Accumulator,\n index: number,\n partial: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, inputJson: b.inputJson + partial }))\n\nexport const appendThinkingDelta = (\n acc: Accumulator,\n index: number,\n thinking: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, thinking: b.thinking + thinking }))\n\nexport const appendSignatureDelta = (\n acc: Accumulator,\n index: number,\n signature: string,\n): Accumulator => updateBlock(acc, index, (b) => ({ ...b, signature: b.signature + signature }))\n\nexport const setStopReason = (acc: Accumulator, reason: string): Accumulator => ({\n ...acc,\n stopReason: Option.some(reason),\n})\n\n// Anthropic splits input across three billed buckets: `input_tokens`\n// (post-breakpoint), cache reads, and cache writes. All three are kept;\n// `input_tokens` is not the input total. Each field carries over from the\n// prior value since a streamed turn spreads usage across events (input +\n// cache on message_start, output_tokens on message_delta), so total_tokens is\n// summed from the accumulated buckets, not one event.\nexport const mergeUsage = (acc: Accumulator, wire: WireUsage): Accumulator => {\n const prev = acc.usage\n const input_tokens = wire.input_tokens ?? prev.input_tokens\n const output_tokens = wire.output_tokens ?? prev.output_tokens\n const cached_tokens = wire.cache_read_input_tokens ?? prev.input_tokens_details?.cached_tokens\n const cache_write_tokens =\n wire.cache_creation_input_tokens ?? prev.input_tokens_details?.cache_write_tokens\n\n const buckets = [input_tokens, cached_tokens, cache_write_tokens, output_tokens]\n const total_tokens = buckets.every((v) => v === undefined)\n ? undefined\n : buckets.reduce<number>((sum, v) => sum + (v ?? 0), 0)\n\n const details: Items.InputTokensDetails = {\n ...(cached_tokens !== undefined && { cached_tokens }),\n ...(cache_write_tokens !== undefined && { cache_write_tokens }),\n }\n const usage: Items.Usage = {\n ...(input_tokens !== undefined && { input_tokens }),\n ...(output_tokens !== undefined && { output_tokens }),\n ...(total_tokens !== undefined && { total_tokens }),\n ...(Object.keys(details).length > 0 && { input_tokens_details: details }),\n }\n return { ...acc, usage }\n}\n\nconst stopReasonFromAnthropic = (reason: Option.Option<string>): Turn[\"stop_reason\"] =>\n Option.match(reason, {\n onNone: () => \"stop\" as const,\n onSome: (r) =>\n Match.value(r).pipe(\n Match.when(\"tool_use\", () => \"tool_calls\" as const),\n Match.when(\"max_tokens\", () => \"max_tokens\" as const),\n Match.orElse(() => \"stop\" as const),\n ),\n })\n\nconst blocksByIndex = (acc: Accumulator): ReadonlyArray<BlockBuffer> =>\n pipe(\n Object.keys(acc.blocks),\n Arr.map((k) => Number(k)),\n Arr.sort(Order.Number),\n Arr.map((i) => acc.blocks[i]!),\n )\n\nconst blockToItems = (block: BlockBuffer): ReadonlyArray<Items.HistoryItem> =>\n Match.value(block.type).pipe(\n Match.when(\"text\", (): ReadonlyArray<Items.HistoryItem> =>\n block.text.length === 0\n ? []\n : [\n {\n type: \"message\",\n role: \"assistant\",\n content: [{ type: \"output_text\", text: block.text }],\n },\n ],\n ),\n Match.when(\"tool_use\", (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"function_call\",\n call_id: Option.getOrElse(block.id, () => \"\"),\n name: Option.getOrElse(block.name, () => \"\"),\n arguments: block.inputJson,\n },\n ]),\n Match.when(\"thinking\", (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"reasoning\",\n ...(block.thinking.length > 0 && { summary: block.thinking }),\n ...(block.signature.length > 0 && { signature: block.signature }),\n },\n ]),\n Match.when(\"redacted_thinking\", (): ReadonlyArray<Items.HistoryItem> => [\n {\n type: \"reasoning\",\n ...Option.match(block.redactedData, {\n onNone: () => ({}),\n onSome: (signature) => ({ signature }),\n }),\n },\n ]),\n Match.exhaustive,\n )\n\ntype MergeAcc = {\n readonly out: ReadonlyArray<Items.HistoryItem>\n}\n\nconst mergeStep = (acc: MergeAcc, item: Items.HistoryItem): MergeAcc => {\n const last = Arr.last(acc.out)\n if (\n Option.isSome(last) &&\n last.value.type === \"message\" &&\n last.value.role === \"assistant\" &&\n item.type === \"message\" &&\n item.role === \"assistant\"\n ) {\n const merged: Items.Message = {\n ...last.value,\n content: [...last.value.content, ...item.content],\n }\n return { out: [...acc.out.slice(0, -1), merged] }\n }\n return { out: [...acc.out, item] }\n}\n\nconst mergeAdjacentAssistantText = (\n items: ReadonlyArray<Items.HistoryItem>,\n): ReadonlyArray<Items.HistoryItem> => Arr.reduce(items, { out: [] } as MergeAcc, mergeStep).out\n\nexport const accumulatorToTurn = (acc: Accumulator): Turn => ({\n items: pipe(blocksByIndex(acc), Arr.flatMap(blockToItems), mergeAdjacentAssistantText),\n usage: acc.usage,\n stop_reason: stopReasonFromAnthropic(acc.stopReason),\n})\n"],"mappings":";;;;;;;;;;;;;;;;;AAUA,MAAM,gBAAgB,OAAO,OAAO;CAClC,MAAM,OAAO,QAAQ,MAAM;CAC3B,MAAM,OAAO;AACf,CAAC;AAED,MAAM,mBAAmB,OAAO,OAAO;CACrC,MAAM,OAAO,QAAQ,UAAU;CAC/B,IAAI,OAAO;CACX,MAAM,OAAO;CACb,OAAO,OAAO;AAChB,CAAC;AAED,MAAM,oBAAoB,OAAO,OAAO;CACtC,MAAM,OAAO,QAAQ,UAAU;CAC/B,UAAU,OAAO;CACjB,WAAW,OAAO,SAAS,OAAO,MAAM;AAC1C,CAAC;AAED,MAAM,4BAA4B,OAAO,OAAO;CAC9C,MAAM,OAAO,QAAQ,mBAAmB;CACxC,MAAM,OAAO;AACf,CAAC;AAED,MAAa,mBAAmB,OAAO,MAAM;CAC3C;CACA;CACA;CACA;AACF,CAAC;AAGiB,OAAO,OAAO;CAC9B,cAAc,OAAO,SAAS,OAAO,MAAM;CAC3C,eAAe,OAAO,SAAS,OAAO,MAAM;CAC5C,6BAA6B,OAAO,SAAS,OAAO,OAAO,OAAO,MAAM,CAAC;CACzE,yBAAyB,OAAO,SAAS,OAAO,OAAO,OAAO,MAAM,CAAC;AACvE,CAAC;AA+DD,MAAM,YAAY,MAAM,KAAyB,CAAC,CAAC,KACjD,MAAM,yBAAyB,MAAM,CAAC,CAAC;CACrC,aAAa,MAAM,EAAE;CACrB,mBAAmB;CACnB,cAAc,MAAM,EAAE;CACtB,UAAU,MAAM,EAAE;AACpB,CAAC,CACH;AAEA,MAAM,eAA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R;AAMF,MAAM,aAAa,KAAe,SAAsC;CACtE,MAAM,OAAOA,MAAI,KAAK,IAAI,GAAG;CAC7B,IACE,OAAO,OAAO,IAAI,KAClB,KAAK,MAAM,SAAS,aACpB,KAAK,MAAM,SAAS,eACpB,KAAK,SAAS,aACd,KAAK,SAAS,aACd;EACA,MAAM,SAAwB;GAC5B,GAAG,KAAK;GACR,SAAS,CAAC,GAAG,KAAK,MAAM,SAAS,GAAG,KAAK,OAAO;EAClD;EACA,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,IAAI,MAAM,GAAG,EAAE,GAAG,MAAM,EAAE;CAClD;CACA,OAAO,EAAE,KAAK,CAAC,GAAG,IAAI,KAAK,IAAI,EAAE;AACnC;AAEA,MAAM,8BACJ,UACqCA,MAAI,OAAO,OAAO,EAAE,KAAK,CAAC,EAAE,GAAe,SAAS,CAAC,CAAC;AAE7F,MAAa,qBAAqB,SAA4B;CAC5D,OAAO,KAAK,cAAc,GAAG,GAAGA,MAAI,QAAQ,YAAY,GAAG,0BAA0B;CACrF,OAAO,IAAI;CACX,aAAa,wBAAwB,IAAI,UAAU;AACrD"}
@@ -2,8 +2,8 @@
2
2
  var comma = ",".charCodeAt(0);
3
3
  var semicolon = ";".charCodeAt(0);
4
4
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5
- var intToChar = new Uint8Array(64);
6
- var charToInt = new Uint8Array(128);
5
+ var intToChar = /* @__PURE__ */ new Uint8Array(64);
6
+ var charToInt = /* @__PURE__ */ new Uint8Array(128);
7
7
  for (let i = 0; i < chars.length; i++) {
8
8
  const c = chars.charCodeAt(i);
9
9
  intToChar[i] = c;
@@ -20,7 +20,7 @@ function encodeInteger(builder, num, relative) {
20
20
  } while (delta > 0);
21
21
  return num;
22
22
  }
23
- var bufLength = 1024 * 16;
23
+ var bufLength = 16384;
24
24
  var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? { decode(buf) {
25
25
  return Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString();
26
26
  } } : { decode(buf) {
@@ -218,7 +218,7 @@ function getBtoa() {
218
218
  throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
219
219
  };
220
220
  }
221
- const btoa = /* @__PURE__ */ getBtoa();
221
+ const btoa = /*#__PURE__*/ getBtoa();
222
222
  var SourceMap = class {
223
223
  constructor(properties) {
224
224
  this.version = 3;
@@ -355,16 +355,17 @@ var Mappings = class {
355
355
  loc.line,
356
356
  loc.column
357
357
  ];
358
- if (this.hires === "boundary") if (wordRegex.test(original[originalCharIndex])) {
359
- if (!charInHiresBoundary) {
358
+ if (this.hires === "boundary") {
359
+ if (wordRegex.test(original[originalCharIndex])) {
360
+ if (!charInHiresBoundary) {
361
+ this.rawSegments.push(segment);
362
+ charInHiresBoundary = true;
363
+ }
364
+ } else {
360
365
  this.rawSegments.push(segment);
361
- charInHiresBoundary = true;
366
+ charInHiresBoundary = false;
362
367
  }
363
- } else {
364
- this.rawSegments.push(segment);
365
- charInHiresBoundary = false;
366
- }
367
- else this.rawSegments.push(segment);
368
+ } else this.rawSegments.push(segment);
368
369
  }
369
370
  loc.column += 1;
370
371
  this.generatedCodeColumn += 1;
@@ -1010,4 +1011,4 @@ var MagicString = class MagicString {
1010
1011
  //#endregion
1011
1012
  export { MagicString as default };
1012
1013
 
1013
- //# sourceMappingURL=magic-string.es-CaWgF-3p.mjs.map
1014
+ //# sourceMappingURL=magic-string.es-CUAnjKYC.mjs.map