@effect-uai/anthropic 0.1.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.
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/chunk-CfYAbeIz.mjs +13 -0
- package/dist/index.d.mts +438 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +649 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
- package/src/Anthropic.ts +390 -0
- package/src/codec.ts +628 -0
- package/src/index.ts +4 -0
- package/src/models.ts +34 -0
- package/src/streamEvents.ts +221 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["Arr"],"sources":["../src/codec.ts","../src/streamEvents.ts","../src/Anthropic.ts"],"sourcesContent":["import { Array as Arr, Match, Option, Order, Result, Schema, pipe } from \"effect\"\nimport * as Items from \"@effect-uai/core/Items\"\nimport { JsonParseError } from \"@effect-uai/core/JSONL\"\nimport { matchType } from \"@effect-uai/core/Match\"\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\ninterface RequestTextContent {\n readonly type: \"text\"\n readonly text: string\n}\n\ninterface RequestToolResultContent {\n readonly type: \"tool_result\"\n readonly tool_use_id: string\n readonly content: string\n}\n\ninterface RequestToolUseContent {\n readonly type: \"tool_use\"\n readonly id: string\n readonly name: string\n readonly input: unknown\n}\n\ninterface RequestThinkingContent {\n readonly type: \"thinking\"\n readonly thinking: string\n readonly signature?: string\n}\n\ninterface RequestRedactedThinkingContent {\n readonly type: \"redacted_thinking\"\n readonly data: string\n}\n\ninterface 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\ninterface RequestUserMessage {\n readonly role: \"user\"\n readonly content: ReadonlyArray<RequestUserContentBlock>\n}\n\ninterface 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 matchType(\"input_text\", (b) => b.text),\n matchType(\"input_image\", () => \"\"),\n matchType(\"output_text\", (b) => b.text),\n matchType(\"refusal\", (b) => b.text),\n Match.exhaustive,\n)\n\nconst messageText = (message: Items.Message): string => message.content.map(blockText).join(\"\")\n\nconst userContentBlock = (\n block: Items.ContentBlock,\n): Result.Result<RequestUserContentBlock, void> =>\n Match.value(block).pipe(\n matchType(\"input_text\", (b) =>\n b.text.length === 0\n ? Result.failVoid\n : Result.succeed({ type: \"text\" as const, text: b.text }),\n ),\n matchType(\"input_image\", (b) =>\n Result.succeed({\n type: \"image\" as const,\n source:\n b.source._tag === \"url\"\n ? { type: \"url\" as const, url: b.source.url }\n : {\n type: \"base64\" as const,\n media_type: b.source.media_type,\n data: b.source.data,\n },\n }),\n ),\n // Assistant content; never appears on a user message in practice. Skip.\n matchType(\"output_text\", () => Result.failVoid),\n matchType(\"refusal\", () => Result.failVoid),\n Match.exhaustive,\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.Item): RoleBucket =>\n Match.value(item).pipe(\n matchType(\"message\", (m) => m.role),\n matchType(\"function_call\", () => \"assistant\" as const),\n matchType(\"function_call_output\", () => \"user\" as const),\n matchType(\"reasoning\", () => \"assistant\" as const),\n Match.exhaustive,\n )\n\nconst itemToUserBlocks = (item: Items.Item): ReadonlyArray<RequestUserContentBlock> =>\n Match.value(item).pipe(\n matchType(\n \"message\",\n (m): ReadonlyArray<RequestUserContentBlock> =>\n m.role === \"user\" ? pipe(m.content, Arr.filterMap(userContentBlock)) : [],\n ),\n matchType(\"function_call\", (): ReadonlyArray<RequestUserContentBlock> => []),\n matchType(\n \"function_call_output\",\n (o): ReadonlyArray<RequestUserContentBlock> => [\n { type: \"tool_result\", tool_use_id: o.call_id, content: o.output },\n ],\n ),\n matchType(\"reasoning\", (): ReadonlyArray<RequestUserContentBlock> => []),\n Match.exhaustive,\n )\n\nconst itemToAssistantBlocks = (\n item: Items.Item,\n): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> =>\n Match.value(item).pipe(\n matchType(\n \"message\",\n (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 ),\n matchType(\"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 ),\n matchType(\n \"function_call_output\",\n (): Result.Result<ReadonlyArray<RequestAssistantContentBlock>, JsonParseError> =>\n Result.succeed([]),\n ),\n matchType(\"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 Match.exhaustive,\n )\n\ninterface 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 = (acc: GroupAcc, item: Items.Item): 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.Item>,\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.Item): item is Items.Message =>\n item.type === \"message\" && item.role === \"system\"\n\nconst systemFromHistory = (history: ReadonlyArray<Items.Item>): 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 interface ThinkingConfig {\n readonly type: \"enabled\"\n readonly budget_tokens: number\n}\n\nexport interface 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 stream: true\n}\n\nexport const buildRequestBody = (params: {\n readonly model: string\n readonly history: ReadonlyArray<Items.Item>\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}): 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 stream: true,\n }),\n ),\n )\n\n// ---------------------------------------------------------------------------\n// Stream-level state - assemble content blocks index-by-index, then emit\n// our `Items.Item[]` when `message_stop` lands.\n// ---------------------------------------------------------------------------\n\ninterface 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 interface 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 matchType(\"text\", () => replaceBlock(acc, index, emptyBlock(\"text\"))),\n matchType(\"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 ),\n matchType(\"thinking\", () => replaceBlock(acc, index, emptyBlock(\"thinking\"))),\n matchType(\"redacted_thinking\", (b) =>\n replaceBlock(acc, index, {\n ...emptyBlock(\"redacted_thinking\"),\n redactedData: Option.some(b.data),\n }),\n ),\n Match.exhaustive,\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\nconst cachedFromWire = (wire: WireUsage): Option.Option<number> =>\n Option.fromNullishOr(wire.cache_read_input_tokens)\n\nexport const mergeUsage = (acc: Accumulator, wire: WireUsage): Accumulator => {\n const cached = cachedFromWire(wire)\n const usage: Items.Usage = {\n ...acc.usage,\n ...(wire.input_tokens !== undefined && { input_tokens: wire.input_tokens }),\n ...(wire.output_tokens !== undefined && { output_tokens: wire.output_tokens }),\n ...(wire.input_tokens !== undefined &&\n wire.output_tokens !== undefined && {\n total_tokens: wire.input_tokens + wire.output_tokens,\n }),\n ...Option.match(cached, {\n onNone: () => ({}),\n onSome: (cached_tokens) => ({ input_tokens_details: { cached_tokens } }),\n }),\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.Item> =>\n Match.value(block.type).pipe(\n Match.when(\n \"text\",\n (): ReadonlyArray<Items.Item> =>\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.Item> => [\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.Item> => [\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.Item> => [\n {\n type: \"reasoning\",\n ...Option.match(block.redactedData, {\n onNone: () => ({}),\n onSome: (signature) => ({ signature }),\n }),\n },\n ],\n ),\n Match.exhaustive,\n )\n\ninterface MergeAcc {\n readonly out: ReadonlyArray<Items.Item>\n}\n\nconst mergeStep = (acc: MergeAcc, item: Items.Item): 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 = (items: ReadonlyArray<Items.Item>): ReadonlyArray<Items.Item> =>\n 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","import { Match, Schema } from \"effect\"\nimport { matchType } from \"@effect-uai/core/Match\"\nimport {\n type Accumulator,\n WireContentBlock,\n type WireUsage,\n appendInputJsonDelta,\n appendSignatureDelta,\n appendTextDelta,\n appendThinkingDelta,\n mergeUsage,\n setStopReason,\n startBlock,\n} from \"./codec.js\"\n\n// ---------------------------------------------------------------------------\n// Wire schemas for the SSE events we map onto our `Accumulator`. Anthropic's\n// streaming protocol uses named SSE events; we match on the inner JSON\n// `type` field which mirrors the event name.\n// ---------------------------------------------------------------------------\n\nconst WireMessageStartUsage = 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})\n\nconst MessageStart = Schema.Struct({\n type: Schema.Literal(\"message_start\"),\n message: Schema.Struct({\n id: Schema.optional(Schema.String),\n usage: Schema.optional(WireMessageStartUsage),\n }),\n})\n\nconst ContentBlockStart = Schema.Struct({\n type: Schema.Literal(\"content_block_start\"),\n index: Schema.Number,\n content_block: WireContentBlock,\n})\n\nconst TextDelta = Schema.Struct({\n type: Schema.Literal(\"text_delta\"),\n text: Schema.String,\n})\n\nconst InputJsonDelta = Schema.Struct({\n type: Schema.Literal(\"input_json_delta\"),\n partial_json: Schema.String,\n})\n\nconst ThinkingDelta = Schema.Struct({\n type: Schema.Literal(\"thinking_delta\"),\n thinking: Schema.String,\n})\n\nconst SignatureDelta = Schema.Struct({\n type: Schema.Literal(\"signature_delta\"),\n signature: Schema.String,\n})\n\nconst Delta = Schema.Union([TextDelta, InputJsonDelta, ThinkingDelta, SignatureDelta])\n\nconst ContentBlockDelta = Schema.Struct({\n type: Schema.Literal(\"content_block_delta\"),\n index: Schema.Number,\n delta: Delta,\n})\n\nconst ContentBlockStop = Schema.Struct({\n type: Schema.Literal(\"content_block_stop\"),\n index: Schema.Number,\n})\n\nconst MessageDelta = Schema.Struct({\n type: Schema.Literal(\"message_delta\"),\n delta: Schema.Struct({\n stop_reason: Schema.optional(Schema.NullOr(Schema.String)),\n stop_sequence: Schema.optional(Schema.NullOr(Schema.String)),\n }),\n usage: Schema.optional(WireMessageStartUsage),\n})\n\nconst MessageStop = Schema.Struct({\n type: Schema.Literal(\"message_stop\"),\n})\n\nconst Ping = Schema.Struct({\n type: Schema.Literal(\"ping\"),\n})\n\nconst ErrorEvent = Schema.Struct({\n type: Schema.Literal(\"error\"),\n error: Schema.optional(\n Schema.Struct({\n type: Schema.optional(Schema.String),\n message: Schema.optional(Schema.String),\n }),\n ),\n})\n\n/**\n * Catch-all variant for wire events that fail to decode against any known\n * schema, plus events that fail to JSON-parse. The decoder never produces\n * this directly - it's synthesized by `sseEventToProviderEvent` when\n * `decodeKnown` fails.\n */\nconst Unknown = Schema.Struct({\n type: Schema.Literal(\"_unknown\"),\n raw: Schema.Unknown,\n})\n\n/**\n * Internal: union of variants we actually know how to decode from the wire.\n * Used as the decode target; failures are caught and re-emitted as `Unknown`.\n */\nexport const KnownProviderEvent = Schema.Union([\n MessageStart,\n ContentBlockStart,\n ContentBlockDelta,\n ContentBlockStop,\n MessageDelta,\n MessageStop,\n Ping,\n ErrorEvent,\n])\n\n/**\n * Public: every event the native stream can emit. Discriminated on `type`.\n * The `_unknown` branch closes the cardinality so downstream `Match.exhaustive`\n * cannot silently miss a wire event we didn't model.\n */\nexport const ProviderEvent = Schema.Union([\n MessageStart,\n ContentBlockStart,\n ContentBlockDelta,\n ContentBlockStop,\n MessageDelta,\n MessageStop,\n Ping,\n ErrorEvent,\n Unknown,\n])\nexport type ProviderEvent = typeof ProviderEvent.Type\n\n// ---------------------------------------------------------------------------\n// Apply event → Accumulator. Pure: the mapping never fails. Caller wires\n// this into `Stream.mapAccum` and emits text/turn_complete deltas based on\n// the diff.\n// ---------------------------------------------------------------------------\n\nconst mergeOptionalUsage = (acc: Accumulator, wire: WireUsage | undefined): Accumulator =>\n wire === undefined ? acc : mergeUsage(acc, wire)\n\nexport const applyEvent = (acc: Accumulator, event: ProviderEvent): Accumulator =>\n Match.value(event).pipe(\n matchType(\"message_start\", (e) => mergeOptionalUsage(acc, e.message.usage)),\n matchType(\"content_block_start\", (e) => startBlock(acc, e.index, e.content_block)),\n matchType(\"content_block_delta\", (e) =>\n Match.value(e.delta).pipe(\n matchType(\"text_delta\", (d) => appendTextDelta(acc, e.index, d.text)),\n matchType(\"input_json_delta\", (d) => appendInputJsonDelta(acc, e.index, d.partial_json)),\n matchType(\"thinking_delta\", (d) => appendThinkingDelta(acc, e.index, d.thinking)),\n matchType(\"signature_delta\", (d) => appendSignatureDelta(acc, e.index, d.signature)),\n Match.exhaustive,\n ),\n ),\n matchType(\"content_block_stop\", () => acc),\n matchType(\"message_delta\", (e) => {\n const withUsage = mergeOptionalUsage(acc, e.usage)\n const reason = e.delta.stop_reason\n return reason === undefined || reason === null ? withUsage : setStopReason(withUsage, reason)\n }),\n matchType(\"message_stop\", () => acc),\n matchType(\"ping\", () => acc),\n matchType(\"error\", () => acc),\n // No silent drops: unknown wire events flow through `streamNative` but\n // produce no accumulator change. Step 3 (canonical `other` event) will\n // also forward them to `TurnEvent`.\n matchType(\"_unknown\", () => acc),\n Match.exhaustive,\n )\n\n// ---------------------------------------------------------------------------\n// Helpers for producing TurnEvent from a step.\n// ---------------------------------------------------------------------------\n\nexport const isTextDeltaEvent = (\n event: ProviderEvent,\n): event is Extract<\n ProviderEvent,\n { type: \"content_block_delta\"; delta: { type: \"text_delta\" } }\n> => event.type === \"content_block_delta\" && event.delta.type === \"text_delta\"\n\nexport const isThinkingDeltaEvent = (\n event: ProviderEvent,\n): event is Extract<\n ProviderEvent,\n { type: \"content_block_delta\"; delta: { type: \"thinking_delta\" } }\n> => event.type === \"content_block_delta\" && event.delta.type === \"thinking_delta\"\n\nexport const isInputJsonDeltaEvent = (\n event: ProviderEvent,\n): event is Extract<\n ProviderEvent,\n { type: \"content_block_delta\"; delta: { type: \"input_json_delta\" } }\n> => event.type === \"content_block_delta\" && event.delta.type === \"input_json_delta\"\n\nexport const isToolUseStartEvent = (\n event: ProviderEvent,\n): event is Extract<\n ProviderEvent,\n { type: \"content_block_start\"; content_block: { type: \"tool_use\" } }\n> => event.type === \"content_block_start\" && event.content_block.type === \"tool_use\"\n\nexport const isMessageStop = (event: ProviderEvent): boolean => event.type === \"message_stop\"\n\nexport const isErrorEvent = (\n event: ProviderEvent,\n): event is Extract<ProviderEvent, { type: \"error\" }> => event.type === \"error\"\n","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} from \"@effect-uai/core/LanguageModel\"\nimport { matchType } from \"@effect-uai/core/Match\"\nimport * as SSE from \"@effect-uai/core/SSE\"\nimport type { TurnEvent } from \"@effect-uai/core/Turn\"\nimport {\n type Accumulator,\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\n// ---------------------------------------------------------------------------\n// Public types\n// ---------------------------------------------------------------------------\n\nexport interface AnthropicRequest extends 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 interface 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 * 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 interface 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\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\nconst toolDescriptors = (\n request: AnthropicRequest,\n): Option.Option<ReadonlyArray<Record<string, unknown>>> =>\n request.tools !== undefined && request.tools.length > 0\n ? Option.some(\n request.tools.map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n })),\n )\n : Option.none()\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)\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.\n */\nconst sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect<ProviderEvent> =>\n Effect.try({\n try: () => JSON.parse(ev.data) as unknown,\n catch: () => ev.data,\n }).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 matchType(\"content_block_start\", (e) =>\n e.content_block.type === \"tool_use\"\n ? [\n {\n type: \"tool_call_start\" as const,\n call_id: e.content_block.id,\n name: e.content_block.name,\n },\n ]\n : [],\n ),\n matchType(\"content_block_delta\", (e) =>\n Match.value(e.delta).pipe(\n matchType(\"text_delta\", (d) => [{ type: \"text_delta\" as const, text: d.text }]),\n matchType(\"thinking_delta\", (d) => [\n { type: \"reasoning_delta\" as const, text: d.thinking, kind: \"trace\" as const },\n ]),\n matchType(\"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 : [\n {\n type: \"tool_call_args_delta\" as const,\n call_id: callId,\n delta: d.partial_json,\n },\n ]\n }),\n // Encrypted reasoning state - flows through `streamNative` but has\n // no canonical representation.\n matchType(\"signature_delta\", () => []),\n Match.exhaustive,\n ),\n ),\n matchType(\"message_start\", (e) =>\n e.message.usage === undefined ? [] : [{ type: \"usage_update\" as const, usage: next.usage }],\n ),\n matchType(\"message_delta\", (e) =>\n e.usage === undefined ? [] : [{ type: \"usage_update\" as const, usage: next.usage }],\n ),\n matchType(\"message_stop\", () => [\n { type: \"turn_complete\" as const, turn: accumulatorToTurn(next) },\n ]),\n matchType(\"content_block_stop\", () => []),\n matchType(\"ping\", () => []),\n matchType(\"error\", () => []),\n matchType(\"_unknown\", () => []),\n Match.exhaustive,\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 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: toolDescriptors(request),\n toolChoice: toolChoiceWire(request),\n userId: Option.fromUndefinedOr(request.user),\n outputConfig: structured,\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.asEffect(), (client) => {\n const streamNative: AnthropicService[\"streamNative\"] = (request) =>\n buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client))\n return {\n streamNative,\n streamTurn: (request) => toCanonical(streamNative(request)),\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 }),\n ),\n )\n return Layer.merge(typed, generic)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAWA,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;CAC5C,eAAe,OAAO,SAAS,OAAO,OAAO;CAC7C,6BAA6B,OAAO,SAAS,OAAO,OAAO,OAAO,OAAO,CAAC;CAC1E,yBAAyB,OAAO,SAAS,OAAO,OAAO,OAAO,OAAO,CAAC;CACvE,CAAC;AA+DF,MAAM,YAAY,MAAM,MAA0B,CAAC,KACjD,UAAU,eAAe,MAAM,EAAE,KAAK,EACtC,UAAU,qBAAqB,GAAG,EAClC,UAAU,gBAAgB,MAAM,EAAE,KAAK,EACvC,UAAU,YAAY,MAAM,E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CAAC,QAAQ,CAAC,KAAK,OAAO,eAAe,WAAW,YAAY,OAAO,CAAC;AAC5F,QAAO;EACL;EACA,aAAa,YAAY,YAAY,aAAa,QAAQ,CAAC;EAC3D;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,EACnE,EACF,CACF;AACD,QAAO,MAAM,MAAM,OAAO,QAAQ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect-uai/anthropic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Anthropic Messages API provider for @effect-uai/core.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ai",
|
|
7
|
+
"anthropic",
|
|
8
|
+
"claude",
|
|
9
|
+
"effect",
|
|
10
|
+
"llm"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/betalyra/effect-uai/tree/main/packages/providers/anthropic",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/betalyra/effect-uai/issues"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Betalyra",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/betalyra/effect-uai",
|
|
21
|
+
"directory": "packages/providers/anthropic"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.mts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"import": "./dist/index.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"src",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"effect": "4.0.0-beta.57",
|
|
43
|
+
"@effect-uai/core": "^0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"effect": "4.0.0-beta.57",
|
|
47
|
+
"typescript": "^6.0.3",
|
|
48
|
+
"@effect-uai/core": "0.1.0"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"typecheck": "tsc --noEmit"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/Anthropic.ts
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Context,
|
|
3
|
+
Effect,
|
|
4
|
+
Layer,
|
|
5
|
+
Match,
|
|
6
|
+
Option,
|
|
7
|
+
Redacted,
|
|
8
|
+
Result,
|
|
9
|
+
Schema,
|
|
10
|
+
Stream,
|
|
11
|
+
pipe,
|
|
12
|
+
} from "effect"
|
|
13
|
+
import { HttpClient, HttpClientRequest } from "effect/unstable/http"
|
|
14
|
+
import * as AiError from "@effect-uai/core/AiError"
|
|
15
|
+
import {
|
|
16
|
+
type CommonRequest,
|
|
17
|
+
LanguageModel,
|
|
18
|
+
type LanguageModelService,
|
|
19
|
+
} from "@effect-uai/core/LanguageModel"
|
|
20
|
+
import { matchType } from "@effect-uai/core/Match"
|
|
21
|
+
import * as SSE from "@effect-uai/core/SSE"
|
|
22
|
+
import type { TurnEvent } from "@effect-uai/core/Turn"
|
|
23
|
+
import {
|
|
24
|
+
type Accumulator,
|
|
25
|
+
type ThinkingConfig,
|
|
26
|
+
accumulatorToTurn,
|
|
27
|
+
buildRequestBody,
|
|
28
|
+
emptyAccumulator,
|
|
29
|
+
} from "./codec.js"
|
|
30
|
+
import type { AnthropicModel } from "./models.js"
|
|
31
|
+
import { KnownProviderEvent, ProviderEvent, applyEvent } from "./streamEvents.js"
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Public types
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
export interface AnthropicRequest extends Omit<CommonRequest, "model"> {
|
|
38
|
+
/**
|
|
39
|
+
* Narrows `CommonRequest.model` (`string`) to the typed `AnthropicModel`
|
|
40
|
+
* literal union for autocomplete.
|
|
41
|
+
*/
|
|
42
|
+
readonly model: AnthropicModel
|
|
43
|
+
/**
|
|
44
|
+
* Top-K nucleus sampling parameter. Anthropic-specific; not exposed on the
|
|
45
|
+
* common surface.
|
|
46
|
+
*/
|
|
47
|
+
readonly topK?: number
|
|
48
|
+
/** Stop sequences that abort generation when matched. */
|
|
49
|
+
readonly stopSequences?: ReadonlyArray<string>
|
|
50
|
+
/**
|
|
51
|
+
* Extended thinking configuration. `0` budget is equivalent to disabled.
|
|
52
|
+
* Only the `claude-sonnet-4-x`, `claude-haiku-4-x`, and pre-Opus-4.7
|
|
53
|
+
* model lines support extended thinking.
|
|
54
|
+
*/
|
|
55
|
+
readonly thinking?: ThinkingConfig
|
|
56
|
+
/**
|
|
57
|
+
* `metadata.user_id` on the wire. End-user tracking identifier.
|
|
58
|
+
*/
|
|
59
|
+
readonly user?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface AnthropicService {
|
|
63
|
+
/**
|
|
64
|
+
* Stream the provider's native event vocabulary (post-SSE-decode).
|
|
65
|
+
* Use this when you need full vendor fidelity (e.g. `signature_delta` for
|
|
66
|
+
* encrypted reasoning state). For provider-portable code, use `streamTurn`.
|
|
67
|
+
*/
|
|
68
|
+
readonly streamNative: (
|
|
69
|
+
request: AnthropicRequest,
|
|
70
|
+
) => Stream.Stream<ProviderEvent, AiError.AiError>
|
|
71
|
+
/**
|
|
72
|
+
* Stream canonical `TurnEvent`s. Implemented as
|
|
73
|
+
* `streamNative |> toCanonical`.
|
|
74
|
+
*/
|
|
75
|
+
readonly streamTurn: (request: AnthropicRequest) => Stream.Stream<TurnEvent, AiError.AiError>
|
|
76
|
+
/**
|
|
77
|
+
* Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.
|
|
78
|
+
* Stateful (threads an `Accumulator` for tool-call lookup and
|
|
79
|
+
* accumulator-to-Turn assembly).
|
|
80
|
+
*/
|
|
81
|
+
readonly toCanonical: <E, R>(
|
|
82
|
+
s: Stream.Stream<ProviderEvent, E, R>,
|
|
83
|
+
) => Stream.Stream<TurnEvent, E, R>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Provider-typed service tag. Yield this when you want Anthropic-specific
|
|
88
|
+
* options (`topK`, `stopSequences`, `thinking`); yield the generic
|
|
89
|
+
* `LanguageModel` tag for provider-portable code. Both are registered by
|
|
90
|
+
* `layer`.
|
|
91
|
+
*/
|
|
92
|
+
export class Anthropic extends Context.Service<Anthropic, AnthropicService>()(
|
|
93
|
+
"@betalyra/effect-uai/providers/anthropic/Anthropic",
|
|
94
|
+
) {}
|
|
95
|
+
|
|
96
|
+
export interface Config {
|
|
97
|
+
readonly apiKey: Redacted.Redacted
|
|
98
|
+
readonly baseUrl?: string
|
|
99
|
+
/**
|
|
100
|
+
* Default `max_tokens` for requests that don't override via
|
|
101
|
+
* `request.maxOutputTokens`. Anthropic requires this field; we default to
|
|
102
|
+
* 4096 if neither is set.
|
|
103
|
+
*/
|
|
104
|
+
readonly defaultMaxTokens?: number
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const ANTHROPIC_VERSION = "2023-06-01"
|
|
108
|
+
const STRUCTURED_OUTPUTS_BETA = "structured-outputs-2025-11-13"
|
|
109
|
+
const FALLBACK_MAX_TOKENS = 4096
|
|
110
|
+
|
|
111
|
+
const outputConfig = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>
|
|
112
|
+
pipe(
|
|
113
|
+
Option.fromUndefinedOr(request.structured),
|
|
114
|
+
Option.map((format) => ({
|
|
115
|
+
format: {
|
|
116
|
+
type: "json_schema",
|
|
117
|
+
schema: format.schema["~standard"].jsonSchema.input({ target: "draft-2020-12" }),
|
|
118
|
+
},
|
|
119
|
+
})),
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
const resolvedMaxTokens = (cfg: Config, request: AnthropicRequest): number =>
|
|
123
|
+
request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS
|
|
124
|
+
|
|
125
|
+
const toolDescriptors = (
|
|
126
|
+
request: AnthropicRequest,
|
|
127
|
+
): Option.Option<ReadonlyArray<Record<string, unknown>>> =>
|
|
128
|
+
request.tools !== undefined && request.tools.length > 0
|
|
129
|
+
? Option.some(
|
|
130
|
+
request.tools.map((t) => ({
|
|
131
|
+
name: t.name,
|
|
132
|
+
description: t.description,
|
|
133
|
+
input_schema: t.inputSchema,
|
|
134
|
+
})),
|
|
135
|
+
)
|
|
136
|
+
: Option.none()
|
|
137
|
+
|
|
138
|
+
const toolChoiceWire = (request: AnthropicRequest): Option.Option<Record<string, unknown>> =>
|
|
139
|
+
pipe(
|
|
140
|
+
Option.fromUndefinedOr(request.toolChoice),
|
|
141
|
+
Option.map((choice) =>
|
|
142
|
+
choice === "auto"
|
|
143
|
+
? { type: "auto" }
|
|
144
|
+
: choice === "required"
|
|
145
|
+
? { type: "any" }
|
|
146
|
+
: choice === "none"
|
|
147
|
+
? { type: "none" }
|
|
148
|
+
: { type: "tool", name: choice.name },
|
|
149
|
+
),
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
// ---------------------------------------------------------------------------
|
|
153
|
+
// SSE event → ProviderEvent
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
|
|
156
|
+
const decodeKnown = Schema.decodeUnknownEffect(KnownProviderEvent)
|
|
157
|
+
|
|
158
|
+
const makeUnknown = (raw: unknown): ProviderEvent => ({ type: "_unknown", raw })
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Parse one SSE event's `data` payload into a typed `ProviderEvent`. Never
|
|
162
|
+
* fails: JSON-parse and schema-decode failures both produce a synthesized
|
|
163
|
+
* `_unknown` event so consumers of `streamNative` never silently miss a
|
|
164
|
+
* wire event we didn't model.
|
|
165
|
+
*/
|
|
166
|
+
const sseEventToProviderEvent = (ev: SSE.Event): Effect.Effect<ProviderEvent> =>
|
|
167
|
+
Effect.try({
|
|
168
|
+
try: () => JSON.parse(ev.data) as unknown,
|
|
169
|
+
catch: () => ev.data,
|
|
170
|
+
}).pipe(
|
|
171
|
+
Effect.flatMap((parsed) =>
|
|
172
|
+
decodeKnown(parsed).pipe(Effect.orElseSucceed(() => makeUnknown(parsed))),
|
|
173
|
+
),
|
|
174
|
+
Effect.orElseSucceed(() => makeUnknown(ev.data)),
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// Per-event derivation of TurnEvents. Drives off the new accumulator and the
|
|
179
|
+
// raw event, since some deltas (`tool_call_args_delta`) need the call_id
|
|
180
|
+
// which lives on the accumulator's per-index block.
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
const deltasFromEvent = (next: Accumulator, event: ProviderEvent): ReadonlyArray<TurnEvent> =>
|
|
184
|
+
Match.value(event).pipe(
|
|
185
|
+
matchType("content_block_start", (e) =>
|
|
186
|
+
e.content_block.type === "tool_use"
|
|
187
|
+
? [
|
|
188
|
+
{
|
|
189
|
+
type: "tool_call_start" as const,
|
|
190
|
+
call_id: e.content_block.id,
|
|
191
|
+
name: e.content_block.name,
|
|
192
|
+
},
|
|
193
|
+
]
|
|
194
|
+
: [],
|
|
195
|
+
),
|
|
196
|
+
matchType("content_block_delta", (e) =>
|
|
197
|
+
Match.value(e.delta).pipe(
|
|
198
|
+
matchType("text_delta", (d) => [{ type: "text_delta" as const, text: d.text }]),
|
|
199
|
+
matchType("thinking_delta", (d) => [
|
|
200
|
+
{ type: "reasoning_delta" as const, text: d.thinking, kind: "trace" as const },
|
|
201
|
+
]),
|
|
202
|
+
matchType("input_json_delta", (d) => {
|
|
203
|
+
const block = next.blocks[e.index]
|
|
204
|
+
if (block === undefined) return []
|
|
205
|
+
const callId = Option.getOrElse(block.id, () => "")
|
|
206
|
+
return callId.length === 0
|
|
207
|
+
? []
|
|
208
|
+
: [
|
|
209
|
+
{
|
|
210
|
+
type: "tool_call_args_delta" as const,
|
|
211
|
+
call_id: callId,
|
|
212
|
+
delta: d.partial_json,
|
|
213
|
+
},
|
|
214
|
+
]
|
|
215
|
+
}),
|
|
216
|
+
// Encrypted reasoning state - flows through `streamNative` but has
|
|
217
|
+
// no canonical representation.
|
|
218
|
+
matchType("signature_delta", () => []),
|
|
219
|
+
Match.exhaustive,
|
|
220
|
+
),
|
|
221
|
+
),
|
|
222
|
+
matchType("message_start", (e) =>
|
|
223
|
+
e.message.usage === undefined ? [] : [{ type: "usage_update" as const, usage: next.usage }],
|
|
224
|
+
),
|
|
225
|
+
matchType("message_delta", (e) =>
|
|
226
|
+
e.usage === undefined ? [] : [{ type: "usage_update" as const, usage: next.usage }],
|
|
227
|
+
),
|
|
228
|
+
matchType("message_stop", () => [
|
|
229
|
+
{ type: "turn_complete" as const, turn: accumulatorToTurn(next) },
|
|
230
|
+
]),
|
|
231
|
+
matchType("content_block_stop", () => []),
|
|
232
|
+
matchType("ping", () => []),
|
|
233
|
+
matchType("error", () => []),
|
|
234
|
+
matchType("_unknown", () => []),
|
|
235
|
+
Match.exhaustive,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
// ---------------------------------------------------------------------------
|
|
239
|
+
// Service implementation
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
|
|
242
|
+
const httpStatusError = (status: number, body: string): AiError.AiError => {
|
|
243
|
+
const provider = "anthropic"
|
|
244
|
+
const raw = body
|
|
245
|
+
if (status === 429) return new AiError.RateLimited({ provider, raw })
|
|
246
|
+
if (status === 408 || status === 504) return new AiError.Timeout({ provider, raw })
|
|
247
|
+
if (status === 401) return new AiError.AuthFailed({ provider, subtype: "auth", raw })
|
|
248
|
+
if (status === 403) return new AiError.AuthFailed({ provider, subtype: "permission", raw })
|
|
249
|
+
if (status === 402) return new AiError.AuthFailed({ provider, subtype: "billing", raw })
|
|
250
|
+
if (status === 413) return new AiError.ContextLengthExceeded({ provider, raw })
|
|
251
|
+
if (status === 529) return new AiError.Unavailable({ provider, status, raw })
|
|
252
|
+
if (status >= 500) return new AiError.Unavailable({ provider, status, raw })
|
|
253
|
+
return new AiError.InvalidRequest({ provider, raw })
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const buildNativeStream = (cfg: Config) => {
|
|
257
|
+
const baseUrl = cfg.baseUrl ?? "https://api.anthropic.com"
|
|
258
|
+
const url = `${baseUrl}/v1/messages`
|
|
259
|
+
return (
|
|
260
|
+
request: AnthropicRequest,
|
|
261
|
+
): Stream.Stream<ProviderEvent, AiError.AiError, HttpClient.HttpClient> =>
|
|
262
|
+
Stream.unwrap(
|
|
263
|
+
Effect.gen(function* () {
|
|
264
|
+
const structured = outputConfig(request)
|
|
265
|
+
const bodyResult = buildRequestBody({
|
|
266
|
+
model: request.model,
|
|
267
|
+
history: request.history,
|
|
268
|
+
maxTokens: resolvedMaxTokens(cfg, request),
|
|
269
|
+
temperature: Option.fromUndefinedOr(request.temperature),
|
|
270
|
+
topP: Option.fromUndefinedOr(request.topP),
|
|
271
|
+
topK: Option.fromUndefinedOr(request.topK),
|
|
272
|
+
stopSequences: Option.fromUndefinedOr(request.stopSequences),
|
|
273
|
+
thinking: Option.fromUndefinedOr(request.thinking),
|
|
274
|
+
tools: toolDescriptors(request),
|
|
275
|
+
toolChoice: toolChoiceWire(request),
|
|
276
|
+
userId: Option.fromUndefinedOr(request.user),
|
|
277
|
+
outputConfig: structured,
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
const body = yield* Result.match(bodyResult, {
|
|
281
|
+
onFailure: (cause) =>
|
|
282
|
+
Effect.fail(
|
|
283
|
+
new AiError.InvalidRequest({
|
|
284
|
+
provider: "anthropic",
|
|
285
|
+
param: "input.function_call.arguments",
|
|
286
|
+
raw: cause,
|
|
287
|
+
}),
|
|
288
|
+
),
|
|
289
|
+
onSuccess: (b) => Effect.succeed(b),
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
const client = yield* HttpClient.HttpClient
|
|
293
|
+
const baseRequest = HttpClientRequest.post(url).pipe(
|
|
294
|
+
HttpClientRequest.setHeader("x-api-key", Redacted.value(cfg.apiKey)),
|
|
295
|
+
HttpClientRequest.setHeader("anthropic-version", ANTHROPIC_VERSION),
|
|
296
|
+
HttpClientRequest.bodyJsonUnsafe(body),
|
|
297
|
+
HttpClientRequest.accept("text/event-stream"),
|
|
298
|
+
)
|
|
299
|
+
const httpRequest = Option.isSome(structured)
|
|
300
|
+
? baseRequest.pipe(HttpClientRequest.setHeader("anthropic-beta", STRUCTURED_OUTPUTS_BETA))
|
|
301
|
+
: baseRequest
|
|
302
|
+
const response = yield* client
|
|
303
|
+
.execute(httpRequest)
|
|
304
|
+
.pipe(
|
|
305
|
+
Effect.mapError(
|
|
306
|
+
(cause): AiError.AiError =>
|
|
307
|
+
new AiError.Unavailable({ provider: "anthropic", raw: cause }),
|
|
308
|
+
),
|
|
309
|
+
)
|
|
310
|
+
if (response.status >= 400) {
|
|
311
|
+
const text = yield* response.text.pipe(Effect.orElseSucceed(() => ""))
|
|
312
|
+
return Stream.fail(httpStatusError(response.status, text))
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return response.stream.pipe(
|
|
316
|
+
Stream.mapError(
|
|
317
|
+
(cause): AiError.AiError =>
|
|
318
|
+
new AiError.Unavailable({ provider: "anthropic", raw: cause }),
|
|
319
|
+
),
|
|
320
|
+
SSE.fromBytes,
|
|
321
|
+
Stream.mapEffect(sseEventToProviderEvent),
|
|
322
|
+
Stream.flatMap((event) =>
|
|
323
|
+
event.type === "error"
|
|
324
|
+
? Stream.fail(new AiError.Unavailable({ provider: "anthropic", raw: event }))
|
|
325
|
+
: Stream.succeed(event),
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
}),
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Project a stream of native `ProviderEvent`s into canonical `TurnEvent`s.
|
|
334
|
+
* Threads a fresh `Accumulator` per stream so tool-call lookup and
|
|
335
|
+
* `accumulatorToTurn` assembly work correctly across the run.
|
|
336
|
+
*/
|
|
337
|
+
export const toCanonical = <E, R>(
|
|
338
|
+
s: Stream.Stream<ProviderEvent, E, R>,
|
|
339
|
+
): Stream.Stream<TurnEvent, E, R> =>
|
|
340
|
+
s.pipe(
|
|
341
|
+
Stream.mapAccum(
|
|
342
|
+
() => emptyAccumulator,
|
|
343
|
+
(acc, event) => {
|
|
344
|
+
const next = applyEvent(acc, event)
|
|
345
|
+
const deltas = deltasFromEvent(next, event)
|
|
346
|
+
return [next, deltas] as const
|
|
347
|
+
},
|
|
348
|
+
),
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
// ---------------------------------------------------------------------------
|
|
352
|
+
// Constructors
|
|
353
|
+
// ---------------------------------------------------------------------------
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Build an `AnthropicService` value. For Layer-based setup, prefer `layer`.
|
|
357
|
+
*/
|
|
358
|
+
export const make = (cfg: Config): Effect.Effect<AnthropicService, never, HttpClient.HttpClient> =>
|
|
359
|
+
Effect.map(HttpClient.HttpClient.asEffect(), (client) => {
|
|
360
|
+
const streamNative: AnthropicService["streamNative"] = (request) =>
|
|
361
|
+
buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client))
|
|
362
|
+
return {
|
|
363
|
+
streamNative,
|
|
364
|
+
streamTurn: (request) => toCanonical(streamNative(request)),
|
|
365
|
+
toCanonical,
|
|
366
|
+
}
|
|
367
|
+
})
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Layer that registers both the provider-specific `Anthropic` tag and the
|
|
371
|
+
* generic `LanguageModel` tag, sharing one underlying implementation.
|
|
372
|
+
*
|
|
373
|
+
* The generic tag accepts `CommonRequest`; the typed tag accepts the full
|
|
374
|
+
* `AnthropicRequest` surface.
|
|
375
|
+
*/
|
|
376
|
+
export const layer = (
|
|
377
|
+
cfg: Config,
|
|
378
|
+
): Layer.Layer<Anthropic | LanguageModel, never, HttpClient.HttpClient> => {
|
|
379
|
+
const typed = Layer.effect(Anthropic, make(cfg))
|
|
380
|
+
const generic = Layer.effect(
|
|
381
|
+
LanguageModel,
|
|
382
|
+
Effect.map(
|
|
383
|
+
make(cfg),
|
|
384
|
+
(s): LanguageModelService => ({
|
|
385
|
+
streamTurn: (request) => s.streamTurn(request as AnthropicRequest),
|
|
386
|
+
}),
|
|
387
|
+
),
|
|
388
|
+
)
|
|
389
|
+
return Layer.merge(typed, generic)
|
|
390
|
+
}
|