import { Context, Effect, Layer, Match, Option, Redacted, Result, Schema, Stream, pipe, } from "effect" import { HttpClient, HttpClientRequest } from "effect/http" import * as AiError from "@effect-uai/core/AiError" import * as Capabilities from "@effect-uai/core/Capabilities" import { type CommonRequest, LanguageModel, type LanguageModelService, turnFromStream, } from "@effect-uai/core/LanguageModel" import * as SSE from "@effect-uai/core/SSE" import { descriptorsOf, providerToolsOf } from "@effect-uai/core/Tool" import { type Turn, TurnEvent } from "@effect-uai/core/Turn" import { renderProviderTools } from "./AnthropicTools.js" import { type Accumulator, type CacheControl, type CacheTtl, type ThinkingConfig, accumulatorToTurn, buildRequestBody, emptyAccumulator, } from "./codec.js" import type { AnthropicModel } from "./models.js" import { KnownProviderEvent, ProviderEvent, applyEvent } from "./streamEvents.js" export { codeExecutionTool, webSearchTool, type WebSearchOptions, type UserLocation, } from "./AnthropicTools.js" // --------------------------------------------------------------------------- // Public types // --------------------------------------------------------------------------- export type AnthropicRequest = Omit & { /** * Narrows `CommonRequest.model` (`string`) to the typed `AnthropicModel` * literal union for autocomplete. */ readonly model: AnthropicModel /** * Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the * common surface. */ readonly topK?: number /** Stop sequences that abort generation when matched. */ readonly stopSequences?: ReadonlyArray /** * Extended thinking configuration. `0` budget is equivalent to disabled. * Only the `claude-sonnet-4-x`, `claude-haiku-4-x`, and pre-Opus-4.7 * model lines support extended thinking. */ readonly thinking?: ThinkingConfig /** * `metadata.user_id` on the wire. End-user tracking identifier. */ readonly user?: string } export type AnthropicService = { /** * Stream the provider's native event vocabulary (post-SSE-decode). * Use this when you need full vendor fidelity (e.g. `signature_delta` for * encrypted reasoning state). For provider-portable code, use `streamTurn`. */ readonly streamNative: ( request: AnthropicRequest, ) => Stream.Stream /** * Stream canonical `TurnEvent`s. Implemented as * `streamNative |> toCanonical`. */ readonly streamTurn: (request: AnthropicRequest) => Stream.Stream /** * Drain a single turn and return the assembled `Turn`. Derived from * `streamTurn` — Anthropic's non-streaming `/messages` endpoint * differs from the streamed one in subtle ways (no incremental * thinking deltas), so we route everything through streaming for * consistency. */ readonly turn: (request: AnthropicRequest) => Effect.Effect /** * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s. * Stateful (threads an `Accumulator` for tool-call lookup and * accumulator-to-Turn assembly). */ readonly toCanonical: ( s: Stream.Stream, ) => Stream.Stream } /** * Provider-typed service tag. Yield this when you want Anthropic-specific * options (`topK`, `stopSequences`, `thinking`); yield the generic * `LanguageModel` tag for provider-portable code. Both are registered by * `layer`. */ export class Anthropic extends Context.Service()( "@betalyra/effect-uai/providers/anthropic/Anthropic", ) {} export type Config = { readonly apiKey: Redacted.Redacted readonly baseUrl?: string /** * Default `max_tokens` for requests that don't override via * `request.maxOutputTokens`. Anthropic requires this field; we default to * 4096 if neither is set. */ readonly defaultMaxTokens?: number /** * Prompt caching. Off by default, so the library stays unopinionated about * a setting that changes billing. `true` is `{}`: cache with the 5-minute * default TTL. * * Anthropic caches the `tools` → `system` → `messages` prefix and moves the * breakpoint forward as the conversation grows, so a long system prompt and * tool definitions are paid for once rather than on every turn. Read * `usage.input_tokens_details.cached_tokens` to see it working. * * Mutating earlier history (rewriting an old message, compaction) breaks * the prefix from that point on. Anthropic falls back to the longest * unchanged prefix, so this costs savings but never correctness. */ readonly promptCaching?: boolean | { readonly ttl?: CacheTtl } } const ANTHROPIC_VERSION = "2023-06-01" const STRUCTURED_OUTPUTS_BETA = "structured-outputs-2025-11-13" const FALLBACK_MAX_TOKENS = 4096 const outputConfig = (request: AnthropicRequest): Option.Option> => pipe( Option.fromUndefinedOr(request.structured), Option.map((format) => ({ format: { type: "json_schema", schema: format.schema["~standard"].jsonSchema.input({ target: "draft-2020-12" }), }, })), ) const resolvedMaxTokens = (cfg: Config, request: AnthropicRequest): number => request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS /** * One request-level breakpoint when caching is on. Anthropic places it on the * last cacheable block itself, so nothing has to be marked per-block and the * breakpoint stays correct as history grows. */ const cacheControlOf = (cfg: Config): Option.Option => pipe( Option.fromUndefinedOr(cfg.promptCaching), Option.flatMap((opt) => opt === false ? Option.none() : Option.some({ type: "ephemeral", ...(opt !== true && opt.ttl !== undefined && { ttl: opt.ttl }), }), ), ) const functionToolDescriptors = ( request: AnthropicRequest, ): ReadonlyArray> => descriptorsOf(request.tools).map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema, })) /** * Merge the custom function-tool declarations with the native hosted-tool * entries into the single `tools` array Anthropic expects. `providerWire` is * pre-rendered (and validated) by the caller. */ const toolsWire = ( request: AnthropicRequest, providerWire: ReadonlyArray>, ): Option.Option>> => { const tools = [...functionToolDescriptors(request), ...providerWire] return tools.length > 0 ? Option.some(tools) : Option.none() } const toolChoiceWire = (request: AnthropicRequest): Option.Option> => pipe( Option.fromUndefinedOr(request.toolChoice), Option.map((choice) => choice === "auto" ? { type: "auto" } : choice === "required" ? { type: "any" } : choice === "none" ? { type: "none" } : { type: "tool", name: choice.name }, ), ) // --------------------------------------------------------------------------- // SSE event → ProviderEvent // --------------------------------------------------------------------------- const decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent) const parseJsonUnknown = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)) const makeUnknown = (raw: unknown): ProviderEvent => ({ type: "_unknown", raw }) /** * Parse one SSE event's `data` payload into a typed `ProviderEvent`. Never * fails: JSON-parse and schema-decode failures both produce a synthesized * `_unknown` event so consumers of `streamNative` never silently miss a * wire event we didn't model. When the payload IS valid JSON but doesn't * match the known-event schema, `_unknown.raw` carries the parsed value; * when JSON parsing fails outright, it carries the raw string. */ const sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect => parseJsonUnknown(ev.data).pipe( Effect.flatMap((parsed) => decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed))), ), Effect.orElseSucceed(() => makeUnknown(ev.data)), ) // --------------------------------------------------------------------------- // Per-event derivation of TurnEvents. Drives off the new accumulator and the // raw event, since some deltas (`tool_call_args_delta`) need the call_id // which lives on the accumulator's per-index block. // --------------------------------------------------------------------------- const deltasFromEvent = (next: Accumulator, event: ProviderEvent): ReadonlyArray => Match.value(event).pipe( Match.discriminatorsExhaustive("type")({ content_block_start: (e) => e.content_block.type === "tool_use" ? [ TurnEvent.ToolCallStart({ call_id: e.content_block.id, name: e.content_block.name, }), ] : [], content_block_delta: (e) => Match.value(e.delta).pipe( Match.discriminatorsExhaustive("type")({ text_delta: (d) => [TurnEvent.TextDelta({ text: d.text })], thinking_delta: (d) => [TurnEvent.ReasoningDelta({ text: d.thinking, kind: "trace" })], input_json_delta: (d) => { const block = next.blocks[e.index] if (block === undefined) return [] const callId = Option.getOrElse(block.id, () => "") return callId.length === 0 ? [] : [TurnEvent.ToolCallArgsDelta({ call_id: callId, delta: d.partial_json })] }, // Encrypted reasoning state - flows through `streamNative` but has // no canonical representation. signature_delta: () => [], }), ), message_start: (e) => e.message.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })], message_delta: (e) => e.usage === undefined ? [] : [TurnEvent.UsageUpdate({ usage: next.usage })], message_stop: () => [TurnEvent.TurnComplete({ turn: accumulatorToTurn(next) })], content_block_stop: () => [], ping: () => [], error: () => [], _unknown: () => [], }), ) // --------------------------------------------------------------------------- // Service implementation // --------------------------------------------------------------------------- const httpStatusError = (status: number, body: string): AiError.AiError => { const provider = "anthropic" const raw = body if (status === 429) return new AiError.RateLimited({ provider, raw }) if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw }) if (status === 401) return new AiError.AuthFailed({ provider, subtype: "auth", raw }) if (status === 403) return new AiError.AuthFailed({ provider, subtype: "permission", raw }) if (status === 402) return new AiError.AuthFailed({ provider, subtype: "billing", raw }) if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw }) if (status === 529) return new AiError.Unavailable({ provider, status, raw }) if (status >= 500) return new AiError.Unavailable({ provider, status, raw }) return new AiError.InvalidRequest({ provider, raw }) } const buildNativeStream = (cfg: Config) => { const baseUrl = cfg.baseUrl ?? "https://api.anthropic.com" const url = `${baseUrl}/v1/messages` return ( request: AnthropicRequest, ): Stream.Stream => Stream.unwrap( Effect.gen(function* () { const structured = outputConfig(request) const providerWire = yield* Result.match( renderProviderTools(providerToolsOf(request.tools)), { onFailure: (e) => Effect.fail( new AiError.Unsupported({ provider: "anthropic", capability: e.capability, reason: e.reason, }), ), onSuccess: (wire) => Effect.succeed(wire), }, ) yield* Capabilities.warnDroppedBlocks(request.history, "output_image", { provider: "anthropic", capability: "output_image", reason: "Assistant messages carry no image on this wire. Use `Turn.imagesAsInput` to resend it as user content.", }) const bodyResult = buildRequestBody({ model: request.model, history: request.history, maxTokens: resolvedMaxTokens(cfg, request), temperature: Option.fromUndefinedOr(request.temperature), topP: Option.fromUndefinedOr(request.topP), topK: Option.fromUndefinedOr(request.topK), stopSequences: Option.fromUndefinedOr(request.stopSequences), thinking: Option.fromUndefinedOr(request.thinking), tools: toolsWire(request, providerWire), toolChoice: toolChoiceWire(request), userId: Option.fromUndefinedOr(request.user), outputConfig: structured, cacheControl: cacheControlOf(cfg), }) const body = yield* Result.match(bodyResult, { onFailure: (cause) => Effect.fail( new AiError.InvalidRequest({ provider: "anthropic", param: "input.function_call.arguments", raw: cause, }), ), onSuccess: (b) => Effect.succeed(b), }) const client = yield* HttpClient.HttpClient const baseRequest = HttpClientRequest.post(url).pipe( HttpClientRequest.setHeader("x-api-key", Redacted.value(cfg.apiKey)), HttpClientRequest.setHeader("anthropic-version", ANTHROPIC_VERSION), HttpClientRequest.bodyJsonUnsafe(body), HttpClientRequest.accept("text/event-stream"), ) const httpRequest = Option.isSome(structured) ? baseRequest.pipe(HttpClientRequest.setHeader("anthropic-beta", STRUCTURED_OUTPUTS_BETA)) : baseRequest const response = yield* client .execute(httpRequest) .pipe( Effect.mapError( (cause): AiError.AiError => new AiError.Unavailable({ provider: "anthropic", raw: cause }), ), ) if (response.status >= 400) { const text = yield* response.text.pipe(Effect.orElseSucceed(() => "")) return Stream.fail(httpStatusError(response.status, text)) } return response.stream.pipe( Stream.mapError( (cause): AiError.AiError => new AiError.Unavailable({ provider: "anthropic", raw: cause }), ), SSE.fromBytes, Stream.mapEffect(sseEventToProviderEvent), Stream.flatMap((event) => event.type === "error" ? Stream.fail(new AiError.Unavailable({ provider: "anthropic", raw: event })) : Stream.succeed(event), ), ) }), ) } /** * Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s. * Threads a fresh `Accumulator` per stream so tool-call lookup and * `accumulatorToTurn` assembly work correctly across the run. */ export const toCanonical = ( s: Stream.Stream, ): Stream.Stream => s.pipe( Stream.mapAccum( () => emptyAccumulator, (acc, event) => { const next = applyEvent(acc, event) const deltas = deltasFromEvent(next, event) return [next, deltas] as const }, ), ) // --------------------------------------------------------------------------- // Constructors // --------------------------------------------------------------------------- /** * Build an `AnthropicService` value. For Layer-based setup, prefer `layer`. */ export const make = (cfg: Config): Effect.Effect => Effect.map(HttpClient.HttpClient, (client) => { const streamNative: AnthropicService["streamNative"] = (request) => buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client)) const streamTurn: AnthropicService["streamTurn"] = (request) => toCanonical(streamNative(request)) return { streamNative, streamTurn, turn: turnFromStream(streamTurn), toCanonical, } }) /** * Layer that registers both the provider-specific `Anthropic` tag and the * generic `LanguageModel` tag, sharing one underlying implementation. * * The generic tag accepts `CommonRequest`; the typed tag accepts the full * `AnthropicRequest` surface. */ export const layer = ( cfg: Config, ): Layer.Layer => { const typed = Layer.effect(Anthropic, make(cfg)) const generic = Layer.effect( LanguageModel, Effect.map(make(cfg), (s): LanguageModelService => ({ streamTurn: (request) => s.streamTurn(request as AnthropicRequest), turn: (request) => s.turn(request as AnthropicRequest), })), ) return Layer.merge(typed, generic) }