@effect-uai/anthropic 0.10.0 → 0.11.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.
@@ -1,6 +1,7 @@
1
1
  import { ThinkingConfig } from "./codec.mjs";
2
2
  import { AnthropicModel } from "./models.mjs";
3
3
  import { ProviderEvent } from "./streamEvents.mjs";
4
+ import { UserLocation, WebSearchOptions, codeExecutionTool, webSearchTool } from "./AnthropicTools.mjs";
4
5
  import { Context, Effect, Layer, Redacted, Stream } from "effect";
5
6
  import { HttpClient } from "effect/unstable/http";
6
7
  import * as AiError from "@effect-uai/core/AiError";
@@ -9,7 +10,7 @@ import { Turn, TurnEvent } from "@effect-uai/core/Turn";
9
10
 
10
11
  //#region src/Anthropic.d.ts
11
12
  declare namespace Anthropic_d_exports {
12
- export { Anthropic, AnthropicRequest, AnthropicService, Config, layer, make, toCanonical };
13
+ export { Anthropic, AnthropicRequest, AnthropicService, Config, UserLocation, WebSearchOptions, codeExecutionTool, layer, make, toCanonical, webSearchTool };
13
14
  }
14
15
  type AnthropicRequest = Omit<CommonRequest, "model"> & {
15
16
  /**
@@ -98,5 +99,5 @@ declare const make: (cfg: Config) => Effect.Effect<AnthropicService, never, Http
98
99
  */
99
100
  declare const layer: (cfg: Config) => Layer.Layer<Anthropic | LanguageModel, never, HttpClient.HttpClient>;
100
101
  //#endregion
101
- export { Anthropic, AnthropicRequest, AnthropicService, Config, layer, make, Anthropic_d_exports as t, toCanonical };
102
+ export { Anthropic, AnthropicRequest, AnthropicService, Config, type UserLocation, type WebSearchOptions, codeExecutionTool, layer, make, Anthropic_d_exports as t, toCanonical, webSearchTool };
102
103
  //# sourceMappingURL=Anthropic.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Anthropic.d.mts","names":[],"sources":["../src/Anthropic.ts"],"mappings":";;;;;;;;;;;;;KAqCY,gBAAA,GAAmB,IAAA,CAAK,aAAA;;;;;WAKzB,KAAA,EAAO,cAAA;;;;;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GACX,GAAA,EAAK,MAAA,KACJ,KAAA,CAAM,KAAA,CAAM,SAAA,GAAY,aAAA,SAAsB,UAAA,CAAW,UAAA"}
@@ -1,4 +1,5 @@
1
1
  import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
2
+ import { codeExecutionTool, renderProviderTools, webSearchTool } from "./AnthropicTools.mjs";
2
3
  import { accumulatorToTurn, buildRequestBody, emptyAccumulator } from "./codec.mjs";
3
4
  import { KnownProviderEvent, applyEvent } from "./streamEvents.mjs";
4
5
  import { Context, Effect, Layer, Match, Option, Redacted, Result, Schema, Stream, pipe } from "effect";
@@ -6,14 +7,16 @@ import { HttpClient, HttpClientRequest } from "effect/unstable/http";
6
7
  import * as AiError from "@effect-uai/core/AiError";
7
8
  import { LanguageModel, turnFromStream } from "@effect-uai/core/LanguageModel";
8
9
  import * as SSE from "@effect-uai/core/SSE";
9
- import { descriptorsOf } from "@effect-uai/core/Tool";
10
+ import { descriptorsOf, providerToolsOf } from "@effect-uai/core/Tool";
10
11
  import { TurnEvent } from "@effect-uai/core/Turn";
11
12
  //#region src/Anthropic.ts
12
13
  var Anthropic_exports = /* @__PURE__ */ __exportAll({
13
14
  Anthropic: () => Anthropic,
15
+ codeExecutionTool: () => codeExecutionTool,
14
16
  layer: () => layer,
15
17
  make: () => make,
16
- toCanonical: () => toCanonical
18
+ toCanonical: () => toCanonical,
19
+ webSearchTool: () => webSearchTool
17
20
  });
18
21
  /**
19
22
  * Provider-typed service tag. Yield this when you want Anthropic-specific
@@ -30,13 +33,19 @@ const outputConfig = (request) => pipe(Option.fromUndefinedOr(request.structured
30
33
  schema: format.schema["~standard"].jsonSchema.input({ target: "draft-2020-12" })
31
34
  } })));
32
35
  const resolvedMaxTokens = (cfg, request) => request.maxOutputTokens ?? cfg.defaultMaxTokens ?? FALLBACK_MAX_TOKENS;
33
- const toolDescriptors = (request) => {
34
- const descriptors = descriptorsOf(request.tools);
35
- return descriptors.length > 0 ? Option.some(descriptors.map((t) => ({
36
- name: t.name,
37
- description: t.description,
38
- input_schema: t.inputSchema
39
- }))) : Option.none();
36
+ const functionToolDescriptors = (request) => descriptorsOf(request.tools).map((t) => ({
37
+ name: t.name,
38
+ description: t.description,
39
+ input_schema: t.inputSchema
40
+ }));
41
+ /**
42
+ * Merge the custom function-tool declarations with the native hosted-tool
43
+ * entries into the single `tools` array Anthropic expects. `providerWire` is
44
+ * pre-rendered (and validated) by the caller.
45
+ */
46
+ const toolsWire = (request, providerWire) => {
47
+ const tools = [...functionToolDescriptors(request), ...providerWire];
48
+ return tools.length > 0 ? Option.some(tools) : Option.none();
40
49
  };
41
50
  const toolChoiceWire = (request) => pipe(Option.fromUndefinedOr(request.toolChoice), Option.map((choice) => choice === "auto" ? { type: "auto" } : choice === "required" ? { type: "any" } : choice === "none" ? { type: "none" } : {
42
51
  type: "tool",
@@ -136,6 +145,14 @@ const buildNativeStream = (cfg) => {
136
145
  const url = `${cfg.baseUrl ?? "https://api.anthropic.com"}/v1/messages`;
137
146
  return (request) => Stream.unwrap(Effect.gen(function* () {
138
147
  const structured = outputConfig(request);
148
+ const providerWire = yield* Result.match(renderProviderTools(providerToolsOf(request.tools)), {
149
+ onFailure: (e) => Effect.fail(new AiError.Unsupported({
150
+ provider: "anthropic",
151
+ capability: e.capability,
152
+ reason: e.reason
153
+ })),
154
+ onSuccess: (wire) => Effect.succeed(wire)
155
+ });
139
156
  const bodyResult = buildRequestBody({
140
157
  model: request.model,
141
158
  history: request.history,
@@ -145,7 +162,7 @@ const buildNativeStream = (cfg) => {
145
162
  topK: Option.fromUndefinedOr(request.topK),
146
163
  stopSequences: Option.fromUndefinedOr(request.stopSequences),
147
164
  thinking: Option.fromUndefinedOr(request.thinking),
148
- tools: toolDescriptors(request),
165
+ tools: toolsWire(request, providerWire),
149
166
  toolChoice: toolChoiceWire(request),
150
167
  userId: Option.fromUndefinedOr(request.user),
151
168
  outputConfig: structured
@@ -190,7 +207,7 @@ const toCanonical = (s) => s.pipe(Stream.mapAccum(() => emptyAccumulator, (acc,
190
207
  /**
191
208
  * Build an `AnthropicService` value. For Layer-based setup, prefer `layer`.
192
209
  */
193
- const make = (cfg) => Effect.map(HttpClient.HttpClient.asEffect(), (client) => {
210
+ const make = (cfg) => Effect.map(HttpClient.HttpClient, (client) => {
194
211
  const streamNative = (request) => buildNativeStream(cfg)(request).pipe(Stream.provideService(HttpClient.HttpClient, client));
195
212
  const streamTurn = (request) => toCanonical(streamNative(request));
196
213
  return {
@@ -216,6 +233,6 @@ const layer = (cfg) => {
216
233
  return Layer.merge(typed, generic);
217
234
  };
218
235
  //#endregion
219
- export { Anthropic, layer, make, Anthropic_exports as t, toCanonical };
236
+ export { Anthropic, codeExecutionTool, layer, make, Anthropic_exports as t, toCanonical, webSearchTool };
220
237
 
221
238
  //# sourceMappingURL=Anthropic.mjs.map
@@ -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 } from \"@effect-uai/core/Tool\"\nimport { type Turn, 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 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\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 const descriptors = descriptorsOf(request.tools)\n return descriptors.length > 0\n ? Option.some(\n descriptors.map((t) => ({\n name: t.name,\n description: t.description,\n input_schema: t.inputSchema,\n })),\n )\n : 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 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 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":";;;;;;;;;;;;;;;;;;;;;;;AAoGA,IAAa,YAAb,cAA+B,QAAQ,SAAsC,CAC3E,qDACD,CAAC;AAaF,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,CAAC;CACjF,EACF,EAAE,CACJ;AAEH,MAAM,qBAAqB,KAAa,YACtC,QAAQ,mBAAmB,IAAI,oBAAoB;AAErD,MAAM,mBACJ,YAC0D;CAC1D,MAAM,cAAc,cAAc,QAAQ,MAAM;AAChD,QAAO,YAAY,SAAS,IACxB,OAAO,KACL,YAAY,KAAK,O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AAI,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 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\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 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 })\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":";;;;;;;;;;;;;;;;;;;;;;;;;;AA4GA,IAAa,YAAb,cAA+B,QAAQ,SAAsC,CAC3E,qDACD,CAAC;AAaF,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,CAAC;CACjF,EACF,EAAE,CACJ;AAEH,MAAM,qBAAqB,KAAa,YACtC,QAAQ,mBAAmB,IAAI,oB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ACjB,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"}
@@ -0,0 +1,60 @@
1
+ import { Result, Schema } from "effect";
2
+ import * as Tool from "@effect-uai/core/Tool";
3
+
4
+ //#region src/AnthropicTools.d.ts
5
+ declare namespace AnthropicTools_d_exports {
6
+ export { AnthropicToolConfig, ToolRenderError, UserLocation, WebSearchOptions, codeExecutionTool, renderProviderTools, webSearchTool };
7
+ }
8
+ declare const UserLocation: Schema.Struct<{
9
+ readonly city: Schema.optional<Schema.String>;
10
+ readonly region: Schema.optional<Schema.String>;
11
+ readonly country: Schema.optional<Schema.String>;
12
+ readonly timezone: Schema.optional<Schema.String>;
13
+ }>;
14
+ type UserLocation = typeof UserLocation.Type;
15
+ /** Discriminated config for an Anthropic hosted tool. */
16
+ declare const AnthropicToolConfig: Schema.Union<readonly [Schema.Struct<{
17
+ readonly kind: Schema.Literal<"web_search">;
18
+ readonly type: Schema.String;
19
+ readonly maxUses: Schema.optional<Schema.Number>;
20
+ readonly allowedDomains: Schema.optional<Schema.$Array<Schema.String>>;
21
+ readonly blockedDomains: Schema.optional<Schema.$Array<Schema.String>>;
22
+ readonly userLocation: Schema.optional<Schema.Struct<{
23
+ readonly city: Schema.optional<Schema.String>;
24
+ readonly region: Schema.optional<Schema.String>;
25
+ readonly country: Schema.optional<Schema.String>;
26
+ readonly timezone: Schema.optional<Schema.String>;
27
+ }>>;
28
+ }>, Schema.Struct<{
29
+ readonly kind: Schema.Literal<"code_execution">;
30
+ readonly type: Schema.String;
31
+ }>]>;
32
+ type AnthropicToolConfig = typeof AnthropicToolConfig.Type;
33
+ type WebSearchOptions = {
34
+ readonly maxUses?: number;
35
+ readonly allowedDomains?: ReadonlyArray<string>;
36
+ readonly blockedDomains?: ReadonlyArray<string>;
37
+ readonly userLocation?: UserLocation;
38
+ };
39
+ /** Anthropic-hosted web search. */
40
+ declare const webSearchTool: (opts?: WebSearchOptions) => any;
41
+ /** Anthropic-hosted code execution (Python sandbox). */
42
+ declare const codeExecutionTool: any;
43
+ /**
44
+ * A provider tool this adapter cannot render — a foreign `provider` or an
45
+ * unrecognized `config`. Carried as data so `Anthropic.ts` can lift it into a
46
+ * typed `AiError.Unsupported`.
47
+ */
48
+ type ToolRenderError = {
49
+ readonly capability: string;
50
+ readonly reason: string;
51
+ };
52
+ /**
53
+ * Render a toolkit's Anthropic provider tools to native `tools` entries. Fails
54
+ * with a `ToolRenderError` on the first foreign or unrecognized tool rather
55
+ * than dropping it.
56
+ */
57
+ declare const renderProviderTools: (tools: ReadonlyArray<Tool.ProviderTool<string, any, string, any>>) => Result.Result<ReadonlyArray<Record<string, unknown>>, ToolRenderError>;
58
+ //#endregion
59
+ export { AnthropicToolConfig, ToolRenderError, UserLocation, WebSearchOptions, codeExecutionTool, renderProviderTools, AnthropicTools_d_exports as t, webSearchTool };
60
+ //# sourceMappingURL=AnthropicTools.d.mts.map
@@ -0,0 +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;;cAUvB,iBAAA;;;;;;KAaD,eAAA;EAAA,SACD,UAAA;EAAA,SACA,MAAA;AAAA;;;;;;cAkDE,mBAAA,GACX,KAAA,EAAO,aAAA,CAAc,IAAA,CAAK,YAAA,gCACzB,MAAA,CAAO,MAAA,CAAO,aAAA,CAAc,MAAA,oBAA0B,eAAA"}
@@ -0,0 +1,94 @@
1
+ import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
2
+ import { Match, Result, Schema } from "effect";
3
+ import * as Tool from "@effect-uai/core/Tool";
4
+ //#region src/AnthropicTools.ts
5
+ var AnthropicTools_exports = /* @__PURE__ */ __exportAll({
6
+ AnthropicToolConfig: () => AnthropicToolConfig,
7
+ codeExecutionTool: () => codeExecutionTool,
8
+ renderProviderTools: () => renderProviderTools,
9
+ webSearchTool: () => webSearchTool
10
+ });
11
+ const WEB_SEARCH_TYPE = "web_search_20250305";
12
+ const CODE_EXECUTION_TYPE = "code_execution_20250522";
13
+ const UserLocation = Schema.Struct({
14
+ city: Schema.optional(Schema.String),
15
+ region: Schema.optional(Schema.String),
16
+ country: Schema.optional(Schema.String),
17
+ timezone: Schema.optional(Schema.String)
18
+ });
19
+ const WebSearchConfig = Schema.Struct({
20
+ kind: Schema.Literal("web_search"),
21
+ type: Schema.String,
22
+ maxUses: Schema.optional(Schema.Number),
23
+ allowedDomains: Schema.optional(Schema.Array(Schema.String)),
24
+ blockedDomains: Schema.optional(Schema.Array(Schema.String)),
25
+ userLocation: Schema.optional(UserLocation)
26
+ });
27
+ const CodeExecutionConfig = Schema.Struct({
28
+ kind: Schema.Literal("code_execution"),
29
+ type: Schema.String
30
+ });
31
+ /** Discriminated config for an Anthropic hosted tool. */
32
+ const AnthropicToolConfig = Schema.Union([WebSearchConfig, CodeExecutionConfig]);
33
+ /** Anthropic-hosted web search. */
34
+ const webSearchTool = (opts = {}) => Tool.provider({
35
+ name: "web_search",
36
+ description: "Search the web, executed by Anthropic.",
37
+ inputSchema: Tool.noInput,
38
+ provider: "anthropic",
39
+ config: {
40
+ kind: "web_search",
41
+ type: WEB_SEARCH_TYPE,
42
+ ...opts
43
+ }
44
+ });
45
+ /** Anthropic-hosted code execution (Python sandbox). */
46
+ const codeExecutionTool = Tool.provider({
47
+ name: "code_execution",
48
+ description: "Run Python in a sandbox, executed by Anthropic.",
49
+ inputSchema: Tool.noInput,
50
+ provider: "anthropic",
51
+ config: {
52
+ kind: "code_execution",
53
+ type: CODE_EXECUTION_TYPE
54
+ }
55
+ });
56
+ const userLocationWire = (loc) => ({
57
+ type: "approximate",
58
+ ...loc.city !== void 0 && { city: loc.city },
59
+ ...loc.region !== void 0 && { region: loc.region },
60
+ ...loc.country !== void 0 && { country: loc.country },
61
+ ...loc.timezone !== void 0 && { timezone: loc.timezone }
62
+ });
63
+ const configToWire = Match.type().pipe(Match.discriminatorsExhaustive("kind")({
64
+ web_search: (c) => ({
65
+ type: c.type,
66
+ name: "web_search",
67
+ ...c.maxUses !== void 0 && { max_uses: c.maxUses },
68
+ ...c.allowedDomains !== void 0 && { allowed_domains: c.allowedDomains },
69
+ ...c.blockedDomains !== void 0 && { blocked_domains: c.blockedDomains },
70
+ ...c.userLocation !== void 0 && { user_location: userLocationWire(c.userLocation) }
71
+ }),
72
+ code_execution: (c) => ({
73
+ type: c.type,
74
+ name: "code_execution"
75
+ })
76
+ }));
77
+ const decodeConfig = Schema.decodeUnknownResult(AnthropicToolConfig);
78
+ const renderOne = (tool) => tool.provider !== "anthropic" ? Result.fail({
79
+ capability: `providerTool:${tool.provider}`,
80
+ reason: `tool "${tool.name}" is hosted by "${tool.provider}", not anthropic`
81
+ }) : decodeConfig(tool.config).pipe(Result.map(configToWire), Result.mapError(() => ({
82
+ capability: "providerTool",
83
+ reason: `unrecognized Anthropic provider tool config on "${tool.name}"`
84
+ })));
85
+ /**
86
+ * Render a toolkit's Anthropic provider tools to native `tools` entries. Fails
87
+ * with a `ToolRenderError` on the first foreign or unrecognized tool rather
88
+ * than dropping it.
89
+ */
90
+ const renderProviderTools = (tools) => Result.all(tools.map(renderOne));
91
+ //#endregion
92
+ export { AnthropicToolConfig, codeExecutionTool, renderProviderTools, AnthropicTools_exports as t, webSearchTool };
93
+
94
+ //# sourceMappingURL=AnthropicTools.mjs.map
@@ -0,0 +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,gBAA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