@sembl/provider-anthropic 0.4.0 → 0.5.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/dist/index.cjs +27 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +27 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -105,7 +105,30 @@ function toInputSchema(schema, bundle, resolvedEnums) {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
// src/anthropic-provider.ts
|
|
108
|
+
function renderHistory(history, toolName) {
|
|
109
|
+
const messages = [];
|
|
110
|
+
let lastToolUseId = "";
|
|
111
|
+
history.forEach((turn, i) => {
|
|
112
|
+
if (turn.role === "assistant") {
|
|
113
|
+
lastToolUseId = `toolu_sembl_${i}`;
|
|
114
|
+
messages.push({
|
|
115
|
+
role: "assistant",
|
|
116
|
+
content: [{ type: "tool_use", id: lastToolUseId, name: toolName, input: turn.data }]
|
|
117
|
+
});
|
|
118
|
+
} else if (lastToolUseId) {
|
|
119
|
+
messages.push({
|
|
120
|
+
role: "user",
|
|
121
|
+
content: [{ type: "tool_result", tool_use_id: lastToolUseId, content: turn.text, is_error: true }]
|
|
122
|
+
});
|
|
123
|
+
} else {
|
|
124
|
+
messages.push({ role: "user", content: turn.text });
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return messages;
|
|
128
|
+
}
|
|
108
129
|
var AnthropicProvider = class {
|
|
130
|
+
/** Repair turns are rendered as a tool call and its (failed) result. */
|
|
131
|
+
supportsHistory = true;
|
|
109
132
|
client;
|
|
110
133
|
config;
|
|
111
134
|
callOptions;
|
|
@@ -140,7 +163,10 @@ var AnthropicProvider = class {
|
|
|
140
163
|
...this.config.temperature !== void 0 ? { temperature: this.config.temperature } : {},
|
|
141
164
|
...thinking ? { thinking } : {},
|
|
142
165
|
system: this.buildSystem(request.systemPrompt),
|
|
143
|
-
messages: [
|
|
166
|
+
messages: [
|
|
167
|
+
{ role: "user", content: request.userInput },
|
|
168
|
+
...renderHistory(request.history ?? [], toolName)
|
|
169
|
+
],
|
|
144
170
|
tools: [
|
|
145
171
|
{
|
|
146
172
|
name: toolName,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/anthropic-provider.ts","../src/anthropic-config.ts","../src/errors.ts","../src/schema-converter.ts"],"sourcesContent":["export { AnthropicProvider } from \"./anthropic-provider.js\";\nexport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nexport {\n DEFAULT_MAX_TOKENS,\n DEFAULT_MAX_RETRIES,\n DEFAULT_TIMEOUT_MS,\n} from \"./anthropic-config.js\";\nexport { AnthropicProviderError } from \"./errors.js\";\nexport type { ProviderErrorKind } from \"./errors.js\";\nexport { toInputSchema, toToolName } from \"./schema-converter.js\";\n","import Anthropic from \"@anthropic-ai/sdk\";\nimport type { Provider, ProviderRequest, ProviderResponse } from \"@sembl/core\";\nimport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nimport {\n DEFAULT_MAX_RETRIES,\n DEFAULT_MAX_TOKENS,\n DEFAULT_TIMEOUT_MS,\n isClaude5Model,\n} from \"./anthropic-config.js\";\nimport { AnthropicProviderError, toProviderError } from \"./errors.js\";\nimport { toInputSchema, toToolName } from \"./schema-converter.js\";\n\n/** Per-call overrides handed to the SDK alongside the request body. */\ninterface CallOptions {\n maxRetries?: number;\n timeout?: number;\n}\n\n/**\n * Anthropic provider implementation.\n *\n * Structured output is obtained by declaring the target schema as a single\n * tool and forcing the model to call it (`tool_choice: { type: \"tool\" }`), so\n * the arguments come back already parsed and shape-checked by the API — no\n * JSON scraped out of prose.\n *\n * Retries and timeouts are the SDK's (exponential backoff, `retry-after`\n * aware); this class only chooses the numbers and translates whatever comes\n * back out into an {@link AnthropicProviderError}.\n */\nexport class AnthropicProvider implements Provider {\n private client: Pick<Anthropic, \"messages\">;\n private config: AnthropicProviderConfig;\n private callOptions: CallOptions | undefined;\n\n constructor(config: AnthropicProviderConfig) {\n this.config = config;\n\n if (config.client) {\n // The host owns this client's transport policy, so only override it\n // per-call where the caller asked for something specific.\n this.client = config.client;\n this.callOptions =\n config.maxRetries === undefined && config.timeoutMs === undefined\n ? undefined\n : { maxRetries: config.maxRetries, timeout: config.timeoutMs };\n } else {\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseURL,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n });\n this.callOptions = undefined;\n }\n }\n\n async complete(request: ProviderRequest): Promise<ProviderResponse> {\n const toolName = this.config.toolName ?? toToolName(request.schema.id);\n const maxTokens = this.config.maxTokens ?? DEFAULT_MAX_TOKENS;\n const inputSchema = toInputSchema(\n request.schema,\n request.bundle,\n request.resolvedEnums,\n );\n\n // Sampling parameters go only when configured: newer models reject\n // `temperature` (and `top_p`/`top_k`) outright, and structured\n // extraction does not need them.\n const thinking = this.config.thinking ?? (isClaude5Model(this.config.model) ? { type: \"disabled\" as const } : undefined);\n const message = await this.send(\n {\n model: this.config.model,\n max_tokens: maxTokens,\n ...(this.config.temperature !== undefined ? { temperature: this.config.temperature } : {}),\n ...(thinking ? { thinking } : {}),\n system: this.buildSystem(request.systemPrompt),\n messages: [{ role: \"user\", content: request.userInput }],\n tools: [\n {\n name: toolName,\n description: request.schema.description,\n input_schema: inputSchema as Anthropic.Tool[\"input_schema\"],\n },\n ],\n tool_choice: { type: \"tool\", name: toolName },\n ...this.config.requestOverrides,\n },\n this.callOptions,\n );\n\n const toolUse = message.content.find(\n (block): block is Anthropic.ToolUseBlock =>\n block.type === \"tool_use\" && block.name === toolName,\n );\n\n if (!toolUse) {\n if (message.stop_reason === \"max_tokens\") {\n throw new AnthropicProviderError(\n `Anthropic hit the ${maxTokens}-token output cap before completing the \"${toolName}\" call. ` +\n \"Raise maxTokens, or coerce into a smaller schema.\",\n { kind: \"truncated\", retryable: false, stopReason: \"max_tokens\" },\n );\n }\n throw new AnthropicProviderError(\n `Anthropic returned no \"${toolName}\" tool call (stop_reason: ${message.stop_reason ?? \"unknown\"})`,\n {\n kind: \"no_output\",\n retryable: false,\n stopReason: message.stop_reason ?? undefined,\n },\n );\n }\n\n return {\n data: toolUse.input as Record<string, unknown>,\n usage: {\n promptTokens: message.usage.input_tokens,\n completionTokens: message.usage.output_tokens,\n totalTokens: message.usage.input_tokens + message.usage.output_tokens,\n ...(message.usage.cache_read_input_tokens != null && {\n cacheReadTokens: message.usage.cache_read_input_tokens,\n }),\n ...(message.usage.cache_creation_input_tokens != null && {\n cacheWriteTokens: message.usage.cache_creation_input_tokens,\n }),\n },\n };\n }\n\n /**\n * The system prompt, marked as a cache breakpoint when caching is on.\n *\n * One breakpoint is enough: the API renders `tools` before `system`, so a\n * marker on the trailing system block covers the tool definition too — and\n * the user input, the only part that changes between calls, sits after it\n * in `messages` where it invalidates nothing.\n */\n private buildSystem(\n systemPrompt: string,\n ): string | Anthropic.TextBlockParam[] {\n if (!this.config.cachePrompt) return systemPrompt;\n\n return [\n {\n type: \"text\",\n text: systemPrompt,\n cache_control: { type: \"ephemeral\", ttl: this.config.cacheTtl ?? \"5m\" },\n },\n ];\n }\n\n /** Issue the call, translating SDK failures into typed provider errors. */\n private async send(\n body: Anthropic.MessageCreateParamsNonStreaming,\n options: CallOptions | undefined,\n ): Promise<Anthropic.Message> {\n try {\n return await this.client.messages.create(body, options);\n } catch (error) {\n throw toProviderError(error);\n }\n }\n}\n","import type Anthropic from \"@anthropic-ai/sdk\";\nimport type { ProviderConfig } from \"@sembl/core\";\n\n/**\n * Configuration specific to the Anthropic provider.\n *\n * Supply either `client` or `apiKey`. Prefer `client` when the host app\n * already resolves credentials its own way (Secret Manager, Vault, Bedrock,\n * a Vertex client) — the provider will reuse that client as-is rather than\n * constructing its own.\n */\nexport interface AnthropicProviderConfig extends ProviderConfig {\n /**\n * A pre-built Anthropic client. Takes precedence over `apiKey`/`baseURL`.\n * Also accepts an `AnthropicBedrock` / `AnthropicVertex` client — anything\n * exposing a compatible `messages.create`.\n */\n client?: Pick<Anthropic, \"messages\">;\n /** Anthropic API key. Ignored when `client` is supplied. */\n apiKey?: string;\n /** Base URL override. Ignored when `client` is supplied. */\n baseURL?: string;\n /**\n * Name given to the extraction tool the model is forced to call.\n * Defaults to a sanitized form of the schema id. Only override this if a\n * name shows up somewhere you care about (logs, prompt-cache keys).\n */\n toolName?: string;\n /**\n * Mark the stable prefix of the request — the tool definition and the\n * system prompt — as cacheable, so a run of calls against the same schema\n * pays to process it once instead of once per call.\n *\n * Off by default: a cache write costs more than an ordinary read of the\n * same tokens, so a single call, or a prefix below the model's minimum\n * cacheable length, comes out slightly behind. Turn it on for batches.\n * `ProviderResponse.usage.cacheReadTokens` says whether it is paying off.\n */\n cachePrompt?: boolean;\n /**\n * Lifetime of the cached prefix. Ignored unless `cachePrompt` is set.\n *\n * `\"5m\"` (the default) is refreshed by every read, so back-to-back calls\n * keep it alive indefinitely and it is the cheaper write. Choose `\"1h\"`\n * only for traffic with gaps longer than five minutes between calls — it\n * survives the gap, but the write costs roughly twice as much.\n */\n cacheTtl?: \"5m\" | \"1h\";\n /**\n * Extended-thinking setting sent with every call.\n *\n * Claude 5 models turn adaptive thinking on by default, and a forced tool\n * call is rejected while it is on, so for those models the provider sends\n * `{ type: \"disabled\" }` unless this is set. Older models reject the\n * parameter outright, so nothing is sent for them by default. Set it\n * explicitly to override either behaviour.\n */\n thinking?: Anthropic.ThinkingConfigParam;\n /**\n * Fields merged into the request body last, after everything the provider\n * builds. The escape hatch for the next model-specific parameter, so a\n * caller does not have to wait for a library release — or wrap the client\n * — to send it.\n */\n requestOverrides?: Partial<Anthropic.MessageCreateParamsNonStreaming>;\n /**\n * How many times the SDK retries a failed call before giving up. The SDK\n * retries connection errors, 408/409/429 and 5xx with exponential backoff\n * and honours `retry-after`, so there is nothing to hand-roll here.\n *\n * Defaults to {@link DEFAULT_MAX_RETRIES}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n maxRetries?: number;\n /**\n * Timeout for a single attempt, in milliseconds. Retries each get their\n * own attempt, so the worst-case wall clock is roughly\n * `timeoutMs * (maxRetries + 1)` plus backoff.\n *\n * Defaults to {@link DEFAULT_TIMEOUT_MS}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n timeoutMs?: number;\n}\n\n/** Anthropic requires an explicit output budget; this is used when none is set. */\nexport const DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Whether a model id names a Claude 5 model. Those enable adaptive thinking\n * by default, which a forced tool call cannot be combined with.\n */\nexport function isClaude5Model(model: string): boolean {\n return /claude-(?:fable|opus|sonnet|haiku)-5(?:[.-]|$)/i.test(model);\n}\n\n/** Matches the SDK's own default; stated here so it survives an SDK change. */\nexport const DEFAULT_MAX_RETRIES = 2;\n\n/**\n * Two minutes per attempt. The SDK's own default is ten, which is a long time\n * for a backend import to sit on one listing when the retry is cheap.\n */\nexport const DEFAULT_TIMEOUT_MS = 120_000;\n","import { APIConnectionError, APIError } from \"@anthropic-ai/sdk\";\n\n/**\n * Why a provider call failed, in the terms a caller can act on.\n *\n * A batch import wants to route these differently: `\"api\"` failures are worth\n * re-queueing, `\"truncated\"` needs a bigger output budget, and `\"no_output\"`\n * is a property of that one listing's content — retrying it changes nothing.\n *\n * The same three kinds are used by `@sembl/provider-openai`, so a caller that\n * branches on `kind` keeps working when the provider is swapped.\n */\nexport type ProviderErrorKind = \"api\" | \"truncated\" | \"no_output\";\n\n/**\n * Error thrown by the Anthropic provider.\n *\n * Branch on `kind` rather than matching the message — messages stay\n * diagnostic and are free to change.\n */\nexport class AnthropicProviderError extends Error {\n /** What class of failure this is. */\n public readonly kind: ProviderErrorKind;\n /**\n * Whether another attempt could plausibly succeed. The SDK has already\n * retried retryable transport failures (see `maxRetries`); this says only\n * that the failure was transient in nature, so a caller running a queue can\n * re-enqueue the item rather than dead-letter it.\n */\n public readonly retryable: boolean;\n /** HTTP status, when the failure came back as an API error. */\n public readonly status?: number;\n /** Anthropic's `stop_reason`, when the call returned a message we rejected. */\n public readonly stopReason?: string;\n\n constructor(\n message: string,\n options: {\n kind: ProviderErrorKind;\n retryable: boolean;\n status?: number;\n stopReason?: string;\n cause?: unknown;\n },\n ) {\n super(message, { cause: options.cause });\n this.name = \"AnthropicProviderError\";\n this.kind = options.kind;\n this.retryable = options.retryable;\n this.status = options.status;\n this.stopReason = options.stopReason;\n }\n}\n\n/**\n * Wrap an SDK-level failure as an `AnthropicProviderError`.\n *\n * Retryability is read off the SDK's own error classes rather than the\n * message: connection failures and timeouts are transient by construction,\n * and of the status codes only 408/409/429 and 5xx are worth another attempt —\n * the same set the SDK itself retries internally.\n */\nexport function toProviderError(error: unknown): AnthropicProviderError {\n if (error instanceof APIError) {\n const status = error.status;\n const retryable =\n error instanceof APIConnectionError ||\n status === undefined ||\n status === 408 ||\n status === 409 ||\n status === 429 ||\n status >= 500;\n\n return new AnthropicProviderError(\n `Anthropic request failed${status ? ` (${status})` : \"\"}: ${error.message}`,\n { kind: \"api\", retryable, status, cause: error },\n );\n }\n\n // Anything else (an AbortError, a bug in a caller-supplied client) is\n // surfaced with the same shape so callers only need one catch.\n return new AnthropicProviderError(\n `Anthropic request failed: ${error instanceof Error ? error.message : String(error)}`,\n { kind: \"api\", retryable: false, cause: error },\n );\n}\n","import type { RuntimeSchema, ResolvedEnums, SchemaBundle } from \"@sembl/core\";\nimport { runtimeSchemaToJsonSchema } from \"@sembl/core\";\n\n/** Anthropic tool names must match `^[a-zA-Z0-9_-]{1,64}$`. */\nexport function toToolName(schemaId: string): string {\n const cleaned = schemaId.replace(/[^a-zA-Z0-9_-]/g, \"_\").slice(0, 57);\n return `extract_${cleaned || \"schema\"}`.slice(0, 64);\n}\n\n/**\n * Convert a RuntimeSchema to an Anthropic tool `input_schema`.\n *\n * Unlike OpenAI structured outputs, Anthropic takes ordinary JSON Schema, so\n * optional fields are left out of `required` instead of being made nullable.\n * That keeps the model from inventing explicit `null`s for fields the source\n * text simply never mentioned — which matters for partial coercion, where an\n * absent field and a null field mean different things to the caller.\n */\nexport function toInputSchema(\n schema: RuntimeSchema,\n bundle?: SchemaBundle,\n resolvedEnums?: ResolvedEnums,\n): Record<string, unknown> {\n return runtimeSchemaToJsonSchema(schema, bundle, {\n dialect: \"standard\",\n resolvedEnums,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,cAAsB;;;ACsFf,IAAM,qBAAqB;AAM3B,SAAS,eAAe,OAAwB;AACrD,SAAO,kDAAkD,KAAK,KAAK;AACrE;AAGO,IAAM,sBAAsB;AAM5B,IAAM,qBAAqB;;;ACvGlC,iBAA6C;AAoBtC,IAAM,yBAAN,cAAqC,MAAM;AAAA;AAAA,EAEhC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA,EAEhB,YACE,SACA,SAOA;AACA,UAAM,SAAS,EAAE,OAAO,QAAQ,MAAM,CAAC;AACvC,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ;AACpB,SAAK,YAAY,QAAQ;AACzB,SAAK,SAAS,QAAQ;AACtB,SAAK,aAAa,QAAQ;AAAA,EAC5B;AACF;AAUO,SAAS,gBAAgB,OAAwC;AACtE,MAAI,iBAAiB,qBAAU;AAC7B,UAAM,SAAS,MAAM;AACrB,UAAM,YACJ,iBAAiB,iCACjB,WAAW,UACX,WAAW,OACX,WAAW,OACX,WAAW,OACX,UAAU;AAEZ,WAAO,IAAI;AAAA,MACT,2BAA2B,SAAS,KAAK,MAAM,MAAM,EAAE,KAAK,MAAM,OAAO;AAAA,MACzE,EAAE,MAAM,OAAO,WAAW,QAAQ,OAAO,MAAM;AAAA,IACj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M;AAAA,QACxD,GAAI,QAAQ,MAAM,2BAA2B,QAAQ;AAAA,UACnD,iBAAiB,QAAQ,MAAM;AAAA,QACjC;AAAA,QACA,GAAI,QAAQ,MAAM,+BAA+B,QAAQ;AAAA,UACvD,kBAAkB,QAAQ,MAAM;AAAA,QAClC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,YACN,cACqC;AACrC,QAAI,CAAC,KAAK,OAAO,YAAa,QAAO;AAErC,WAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,MAAM;AAAA,QACN,eAAe,EAAE,MAAM,aAAa,KAAK,KAAK,OAAO,YAAY,KAAK;AAAA,MACxE;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,MAAc,KACZ,MACA,SAC4B;AAC5B,QAAI;AACF,aAAO,MAAM,KAAK,OAAO,SAAS,OAAO,MAAM,OAAO;AAAA,IACxD,SAAS,OAAO;AACd,YAAM,gBAAgB,KAAK;AAAA,IAC7B;AAAA,EACF;AACF;","names":["import_sdk","Anthropic"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/anthropic-provider.ts","../src/anthropic-config.ts","../src/errors.ts","../src/schema-converter.ts"],"sourcesContent":["export { AnthropicProvider } from \"./anthropic-provider.js\";\nexport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nexport {\n DEFAULT_MAX_TOKENS,\n DEFAULT_MAX_RETRIES,\n DEFAULT_TIMEOUT_MS,\n} from \"./anthropic-config.js\";\nexport { AnthropicProviderError } from \"./errors.js\";\nexport type { ProviderErrorKind } from \"./errors.js\";\nexport { toInputSchema, toToolName } from \"./schema-converter.js\";\n","import Anthropic from \"@anthropic-ai/sdk\";\nimport type { Provider, ProviderRequest, ProviderResponse, ProviderTurn } from \"@sembl/core\";\nimport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nimport {\n DEFAULT_MAX_RETRIES,\n DEFAULT_MAX_TOKENS,\n DEFAULT_TIMEOUT_MS,\n isClaude5Model,\n} from \"./anthropic-config.js\";\nimport { AnthropicProviderError, toProviderError } from \"./errors.js\";\nimport { toInputSchema, toToolName } from \"./schema-converter.js\";\n\n/**\n * Earlier turns as the API saw them: the model's rejected output as the\n * tool call it made, the correction as that call's result flagged as an\n * error. The forced tool choice then makes the model call the tool again,\n * which is the corrected attempt. Ids only have to match within the\n * conversation, so they are synthesised.\n */\nfunction renderHistory(history: readonly ProviderTurn[], toolName: string): Anthropic.MessageParam[] {\n const messages: Anthropic.MessageParam[] = [];\n let lastToolUseId = \"\";\n history.forEach((turn, i) => {\n if (turn.role === \"assistant\") {\n lastToolUseId = `toolu_sembl_${i}`;\n messages.push({\n role: \"assistant\",\n content: [{ type: \"tool_use\", id: lastToolUseId, name: toolName, input: turn.data }],\n });\n } else if (lastToolUseId) {\n messages.push({\n role: \"user\",\n content: [{ type: \"tool_result\", tool_use_id: lastToolUseId, content: turn.text, is_error: true }],\n });\n } else {\n messages.push({ role: \"user\", content: turn.text });\n }\n });\n return messages;\n}\n\n/** Per-call overrides handed to the SDK alongside the request body. */\ninterface CallOptions {\n maxRetries?: number;\n timeout?: number;\n}\n\n/**\n * Anthropic provider implementation.\n *\n * Structured output is obtained by declaring the target schema as a single\n * tool and forcing the model to call it (`tool_choice: { type: \"tool\" }`), so\n * the arguments come back already parsed and shape-checked by the API — no\n * JSON scraped out of prose.\n *\n * Retries and timeouts are the SDK's (exponential backoff, `retry-after`\n * aware); this class only chooses the numbers and translates whatever comes\n * back out into an {@link AnthropicProviderError}.\n */\nexport class AnthropicProvider implements Provider {\n /** Repair turns are rendered as a tool call and its (failed) result. */\n readonly supportsHistory = true;\n\n private client: Pick<Anthropic, \"messages\">;\n private config: AnthropicProviderConfig;\n private callOptions: CallOptions | undefined;\n\n constructor(config: AnthropicProviderConfig) {\n this.config = config;\n\n if (config.client) {\n // The host owns this client's transport policy, so only override it\n // per-call where the caller asked for something specific.\n this.client = config.client;\n this.callOptions =\n config.maxRetries === undefined && config.timeoutMs === undefined\n ? undefined\n : { maxRetries: config.maxRetries, timeout: config.timeoutMs };\n } else {\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseURL,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n });\n this.callOptions = undefined;\n }\n }\n\n async complete(request: ProviderRequest): Promise<ProviderResponse> {\n const toolName = this.config.toolName ?? toToolName(request.schema.id);\n const maxTokens = this.config.maxTokens ?? DEFAULT_MAX_TOKENS;\n const inputSchema = toInputSchema(\n request.schema,\n request.bundle,\n request.resolvedEnums,\n );\n\n // Sampling parameters go only when configured: newer models reject\n // `temperature` (and `top_p`/`top_k`) outright, and structured\n // extraction does not need them.\n const thinking = this.config.thinking ?? (isClaude5Model(this.config.model) ? { type: \"disabled\" as const } : undefined);\n const message = await this.send(\n {\n model: this.config.model,\n max_tokens: maxTokens,\n ...(this.config.temperature !== undefined ? { temperature: this.config.temperature } : {}),\n ...(thinking ? { thinking } : {}),\n system: this.buildSystem(request.systemPrompt),\n messages: [\n { role: \"user\", content: request.userInput },\n ...renderHistory(request.history ?? [], toolName),\n ],\n tools: [\n {\n name: toolName,\n description: request.schema.description,\n input_schema: inputSchema as Anthropic.Tool[\"input_schema\"],\n },\n ],\n tool_choice: { type: \"tool\", name: toolName },\n ...this.config.requestOverrides,\n },\n this.callOptions,\n );\n\n const toolUse = message.content.find(\n (block): block is Anthropic.ToolUseBlock =>\n block.type === \"tool_use\" && block.name === toolName,\n );\n\n if (!toolUse) {\n if (message.stop_reason === \"max_tokens\") {\n throw new AnthropicProviderError(\n `Anthropic hit the ${maxTokens}-token output cap before completing the \"${toolName}\" call. ` +\n \"Raise maxTokens, or coerce into a smaller schema.\",\n { kind: \"truncated\", retryable: false, stopReason: \"max_tokens\" },\n );\n }\n throw new AnthropicProviderError(\n `Anthropic returned no \"${toolName}\" tool call (stop_reason: ${message.stop_reason ?? \"unknown\"})`,\n {\n kind: \"no_output\",\n retryable: false,\n stopReason: message.stop_reason ?? undefined,\n },\n );\n }\n\n return {\n data: toolUse.input as Record<string, unknown>,\n usage: {\n promptTokens: message.usage.input_tokens,\n completionTokens: message.usage.output_tokens,\n totalTokens: message.usage.input_tokens + message.usage.output_tokens,\n ...(message.usage.cache_read_input_tokens != null && {\n cacheReadTokens: message.usage.cache_read_input_tokens,\n }),\n ...(message.usage.cache_creation_input_tokens != null && {\n cacheWriteTokens: message.usage.cache_creation_input_tokens,\n }),\n },\n };\n }\n\n /**\n * The system prompt, marked as a cache breakpoint when caching is on.\n *\n * One breakpoint is enough: the API renders `tools` before `system`, so a\n * marker on the trailing system block covers the tool definition too — and\n * the user input, the only part that changes between calls, sits after it\n * in `messages` where it invalidates nothing.\n */\n private buildSystem(\n systemPrompt: string,\n ): string | Anthropic.TextBlockParam[] {\n if (!this.config.cachePrompt) return systemPrompt;\n\n return [\n {\n type: \"text\",\n text: systemPrompt,\n cache_control: { type: \"ephemeral\", ttl: this.config.cacheTtl ?? \"5m\" },\n },\n ];\n }\n\n /** Issue the call, translating SDK failures into typed provider errors. */\n private async send(\n body: Anthropic.MessageCreateParamsNonStreaming,\n options: CallOptions | undefined,\n ): Promise<Anthropic.Message> {\n try {\n return await this.client.messages.create(body, options);\n } catch (error) {\n throw toProviderError(error);\n }\n }\n}\n","import type Anthropic from \"@anthropic-ai/sdk\";\nimport type { ProviderConfig } from \"@sembl/core\";\n\n/**\n * Configuration specific to the Anthropic provider.\n *\n * Supply either `client` or `apiKey`. Prefer `client` when the host app\n * already resolves credentials its own way (Secret Manager, Vault, Bedrock,\n * a Vertex client) — the provider will reuse that client as-is rather than\n * constructing its own.\n */\nexport interface AnthropicProviderConfig extends ProviderConfig {\n /**\n * A pre-built Anthropic client. Takes precedence over `apiKey`/`baseURL`.\n * Also accepts an `AnthropicBedrock` / `AnthropicVertex` client — anything\n * exposing a compatible `messages.create`.\n */\n client?: Pick<Anthropic, \"messages\">;\n /** Anthropic API key. Ignored when `client` is supplied. */\n apiKey?: string;\n /** Base URL override. Ignored when `client` is supplied. */\n baseURL?: string;\n /**\n * Name given to the extraction tool the model is forced to call.\n * Defaults to a sanitized form of the schema id. Only override this if a\n * name shows up somewhere you care about (logs, prompt-cache keys).\n */\n toolName?: string;\n /**\n * Mark the stable prefix of the request — the tool definition and the\n * system prompt — as cacheable, so a run of calls against the same schema\n * pays to process it once instead of once per call.\n *\n * Off by default: a cache write costs more than an ordinary read of the\n * same tokens, so a single call, or a prefix below the model's minimum\n * cacheable length, comes out slightly behind. Turn it on for batches.\n * `ProviderResponse.usage.cacheReadTokens` says whether it is paying off.\n */\n cachePrompt?: boolean;\n /**\n * Lifetime of the cached prefix. Ignored unless `cachePrompt` is set.\n *\n * `\"5m\"` (the default) is refreshed by every read, so back-to-back calls\n * keep it alive indefinitely and it is the cheaper write. Choose `\"1h\"`\n * only for traffic with gaps longer than five minutes between calls — it\n * survives the gap, but the write costs roughly twice as much.\n */\n cacheTtl?: \"5m\" | \"1h\";\n /**\n * Extended-thinking setting sent with every call.\n *\n * Claude 5 models turn adaptive thinking on by default, and a forced tool\n * call is rejected while it is on, so for those models the provider sends\n * `{ type: \"disabled\" }` unless this is set. Older models reject the\n * parameter outright, so nothing is sent for them by default. Set it\n * explicitly to override either behaviour.\n */\n thinking?: Anthropic.ThinkingConfigParam;\n /**\n * Fields merged into the request body last, after everything the provider\n * builds. The escape hatch for the next model-specific parameter, so a\n * caller does not have to wait for a library release — or wrap the client\n * — to send it.\n */\n requestOverrides?: Partial<Anthropic.MessageCreateParamsNonStreaming>;\n /**\n * How many times the SDK retries a failed call before giving up. The SDK\n * retries connection errors, 408/409/429 and 5xx with exponential backoff\n * and honours `retry-after`, so there is nothing to hand-roll here.\n *\n * Defaults to {@link DEFAULT_MAX_RETRIES}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n maxRetries?: number;\n /**\n * Timeout for a single attempt, in milliseconds. Retries each get their\n * own attempt, so the worst-case wall clock is roughly\n * `timeoutMs * (maxRetries + 1)` plus backoff.\n *\n * Defaults to {@link DEFAULT_TIMEOUT_MS}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n timeoutMs?: number;\n}\n\n/** Anthropic requires an explicit output budget; this is used when none is set. */\nexport const DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Whether a model id names a Claude 5 model. Those enable adaptive thinking\n * by default, which a forced tool call cannot be combined with.\n */\nexport function isClaude5Model(model: string): boolean {\n return /claude-(?:fable|opus|sonnet|haiku)-5(?:[.-]|$)/i.test(model);\n}\n\n/** Matches the SDK's own default; stated here so it survives an SDK change. */\nexport const DEFAULT_MAX_RETRIES = 2;\n\n/**\n * Two minutes per attempt. The SDK's own default is ten, which is a long time\n * for a backend import to sit on one listing when the retry is cheap.\n */\nexport const DEFAULT_TIMEOUT_MS = 120_000;\n","import { APIConnectionError, APIError } from \"@anthropic-ai/sdk\";\n\n/**\n * Why a provider call failed, in the terms a caller can act on.\n *\n * A batch import wants to route these differently: `\"api\"` failures are worth\n * re-queueing, `\"truncated\"` needs a bigger output budget, and `\"no_output\"`\n * is a property of that one listing's content — retrying it changes nothing.\n *\n * The same three kinds are used by `@sembl/provider-openai`, so a caller that\n * branches on `kind` keeps working when the provider is swapped.\n */\nexport type ProviderErrorKind = \"api\" | \"truncated\" | \"no_output\";\n\n/**\n * Error thrown by the Anthropic provider.\n *\n * Branch on `kind` rather than matching the message — messages stay\n * diagnostic and are free to change.\n */\nexport class AnthropicProviderError extends Error {\n /** What class of failure this is. */\n public readonly kind: ProviderErrorKind;\n /**\n * Whether another attempt could plausibly succeed. The SDK has already\n * retried retryable transport failures (see `maxRetries`); this says only\n * that the failure was transient in nature, so a caller running a queue can\n * re-enqueue the item rather than dead-letter it.\n */\n public readonly retryable: boolean;\n /** HTTP status, when the failure came back as an API error. */\n public readonly status?: number;\n /** Anthropic's `stop_reason`, when the call returned a message we rejected. */\n public readonly stopReason?: string;\n\n constructor(\n message: string,\n options: {\n kind: ProviderErrorKind;\n retryable: boolean;\n status?: number;\n stopReason?: string;\n cause?: unknown;\n },\n ) {\n super(message, { cause: options.cause });\n this.name = \"AnthropicProviderError\";\n this.kind = options.kind;\n this.retryable = options.retryable;\n this.status = options.status;\n this.stopReason = options.stopReason;\n }\n}\n\n/**\n * Wrap an SDK-level failure as an `AnthropicProviderError`.\n *\n * Retryability is read off the SDK's own error classes rather than the\n * message: connection failures and timeouts are transient by construction,\n * and of the status codes only 408/409/429 and 5xx are worth another attempt —\n * the same set the SDK itself retries internally.\n */\nexport function toProviderError(error: unknown): AnthropicProviderError {\n if (error instanceof APIError) {\n const status = error.status;\n const retryable =\n error instanceof APIConnectionError ||\n status === undefined ||\n status === 408 ||\n status === 409 ||\n status === 429 ||\n status >= 500;\n\n return new AnthropicProviderError(\n `Anthropic request failed${status ? ` (${status})` : \"\"}: ${error.message}`,\n { kind: \"api\", retryable, status, cause: error },\n );\n }\n\n // Anything else (an AbortError, a bug in a caller-supplied client) is\n // surfaced with the same shape so callers only need one catch.\n return new AnthropicProviderError(\n `Anthropic request failed: ${error instanceof Error ? error.message : String(error)}`,\n { kind: \"api\", retryable: false, cause: error },\n );\n}\n","import type { RuntimeSchema, ResolvedEnums, SchemaBundle } from \"@sembl/core\";\nimport { runtimeSchemaToJsonSchema } from \"@sembl/core\";\n\n/** Anthropic tool names must match `^[a-zA-Z0-9_-]{1,64}$`. */\nexport function toToolName(schemaId: string): string {\n const cleaned = schemaId.replace(/[^a-zA-Z0-9_-]/g, \"_\").slice(0, 57);\n return `extract_${cleaned || \"schema\"}`.slice(0, 64);\n}\n\n/**\n * Convert a RuntimeSchema to an Anthropic tool `input_schema`.\n *\n * Unlike OpenAI structured outputs, Anthropic takes ordinary JSON Schema, so\n * optional fields are left out of `required` instead of being made nullable.\n * That keeps the model from inventing explicit `null`s for fields the source\n * text simply never mentioned — which matters for partial coercion, where an\n * absent field and a null field mean different things to the caller.\n */\nexport function toInputSchema(\n schema: RuntimeSchema,\n bundle?: SchemaBundle,\n resolvedEnums?: ResolvedEnums,\n): Record<string, unknown> {\n return runtimeSchemaToJsonSchema(schema, bundle, {\n dialect: \"standard\",\n resolvedEnums,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,cAAsB;;;ACsFf,IAAM,qBAAqB;AAM3B,SAAS,eAAe,OAAwB;AACrD,SAAO,kDAAkD,KAAK,KAAK;AACrE;AAGO,IAAM,sBAAsB;AAM5B,IAAM,qBAAqB;;;ACvGlC,iBAA6C;AAoBtC,IAAM,yBAAN,cAAqC,MAAM;AAAA;AAAA,EAEhC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA,EAEhB,YACE,SACA,SAOA;AACA,UAAM,SAAS,EAAE,OAAO,QAAQ,MAAM,CAAC;AACvC,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ;AACpB,SAAK,YAAY,QAAQ;AACzB,SAAK,SAAS,QAAQ;AACtB,SAAK,aAAa,QAAQ;AAAA,EAC5B;AACF;AAUO,SAAS,gBAAgB,OAAwC;AACtE,MAAI,iBAAiB,qBAAU;AAC7B,UAAM,SAAS,MAAM;AACrB,UAAM,YACJ,iBAAiB,iCACjB,WAAW,UACX,WAAW,OACX,WAAW,OACX,WAAW,OACX,UAAU;AAEZ,WAAO,IAAI;AAAA,MACT,2BAA2B,SAAS,KAAK,MAAM,MAAM,EAAE,KAAK,MAAM,OAAO;AAAA,MACzE,EAAE,MAAM,OAAO,WAAW,QAAQ,OAAO,MAAM;AAAA,IACjD;AAAA,EACF;AAIA,SAAO,IAAI;AAAA,IACT,6BAA6B,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,CAAC;AAAA,IACnF,EAAE,MAAM,OAAO,WAAW,OA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A,EACF;AACF;","names":["import_sdk","Anthropic"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -105,6 +105,8 @@ declare const DEFAULT_TIMEOUT_MS = 120000;
|
|
|
105
105
|
* back out into an {@link AnthropicProviderError}.
|
|
106
106
|
*/
|
|
107
107
|
declare class AnthropicProvider implements Provider {
|
|
108
|
+
/** Repair turns are rendered as a tool call and its (failed) result. */
|
|
109
|
+
readonly supportsHistory = true;
|
|
108
110
|
private client;
|
|
109
111
|
private config;
|
|
110
112
|
private callOptions;
|
package/dist/index.d.ts
CHANGED
|
@@ -105,6 +105,8 @@ declare const DEFAULT_TIMEOUT_MS = 120000;
|
|
|
105
105
|
* back out into an {@link AnthropicProviderError}.
|
|
106
106
|
*/
|
|
107
107
|
declare class AnthropicProvider implements Provider {
|
|
108
|
+
/** Repair turns are rendered as a tool call and its (failed) result. */
|
|
109
|
+
readonly supportsHistory = true;
|
|
108
110
|
private client;
|
|
109
111
|
private config;
|
|
110
112
|
private callOptions;
|
package/dist/index.js
CHANGED
|
@@ -63,7 +63,30 @@ function toInputSchema(schema, bundle, resolvedEnums) {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/anthropic-provider.ts
|
|
66
|
+
function renderHistory(history, toolName) {
|
|
67
|
+
const messages = [];
|
|
68
|
+
let lastToolUseId = "";
|
|
69
|
+
history.forEach((turn, i) => {
|
|
70
|
+
if (turn.role === "assistant") {
|
|
71
|
+
lastToolUseId = `toolu_sembl_${i}`;
|
|
72
|
+
messages.push({
|
|
73
|
+
role: "assistant",
|
|
74
|
+
content: [{ type: "tool_use", id: lastToolUseId, name: toolName, input: turn.data }]
|
|
75
|
+
});
|
|
76
|
+
} else if (lastToolUseId) {
|
|
77
|
+
messages.push({
|
|
78
|
+
role: "user",
|
|
79
|
+
content: [{ type: "tool_result", tool_use_id: lastToolUseId, content: turn.text, is_error: true }]
|
|
80
|
+
});
|
|
81
|
+
} else {
|
|
82
|
+
messages.push({ role: "user", content: turn.text });
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return messages;
|
|
86
|
+
}
|
|
66
87
|
var AnthropicProvider = class {
|
|
88
|
+
/** Repair turns are rendered as a tool call and its (failed) result. */
|
|
89
|
+
supportsHistory = true;
|
|
67
90
|
client;
|
|
68
91
|
config;
|
|
69
92
|
callOptions;
|
|
@@ -98,7 +121,10 @@ var AnthropicProvider = class {
|
|
|
98
121
|
...this.config.temperature !== void 0 ? { temperature: this.config.temperature } : {},
|
|
99
122
|
...thinking ? { thinking } : {},
|
|
100
123
|
system: this.buildSystem(request.systemPrompt),
|
|
101
|
-
messages: [
|
|
124
|
+
messages: [
|
|
125
|
+
{ role: "user", content: request.userInput },
|
|
126
|
+
...renderHistory(request.history ?? [], toolName)
|
|
127
|
+
],
|
|
102
128
|
tools: [
|
|
103
129
|
{
|
|
104
130
|
name: toolName,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/anthropic-provider.ts","../src/anthropic-config.ts","../src/errors.ts","../src/schema-converter.ts"],"sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type { Provider, ProviderRequest, ProviderResponse } from \"@sembl/core\";\nimport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nimport {\n DEFAULT_MAX_RETRIES,\n DEFAULT_MAX_TOKENS,\n DEFAULT_TIMEOUT_MS,\n isClaude5Model,\n} from \"./anthropic-config.js\";\nimport { AnthropicProviderError, toProviderError } from \"./errors.js\";\nimport { toInputSchema, toToolName } from \"./schema-converter.js\";\n\n/** Per-call overrides handed to the SDK alongside the request body. */\ninterface CallOptions {\n maxRetries?: number;\n timeout?: number;\n}\n\n/**\n * Anthropic provider implementation.\n *\n * Structured output is obtained by declaring the target schema as a single\n * tool and forcing the model to call it (`tool_choice: { type: \"tool\" }`), so\n * the arguments come back already parsed and shape-checked by the API — no\n * JSON scraped out of prose.\n *\n * Retries and timeouts are the SDK's (exponential backoff, `retry-after`\n * aware); this class only chooses the numbers and translates whatever comes\n * back out into an {@link AnthropicProviderError}.\n */\nexport class AnthropicProvider implements Provider {\n private client: Pick<Anthropic, \"messages\">;\n private config: AnthropicProviderConfig;\n private callOptions: CallOptions | undefined;\n\n constructor(config: AnthropicProviderConfig) {\n this.config = config;\n\n if (config.client) {\n // The host owns this client's transport policy, so only override it\n // per-call where the caller asked for something specific.\n this.client = config.client;\n this.callOptions =\n config.maxRetries === undefined && config.timeoutMs === undefined\n ? undefined\n : { maxRetries: config.maxRetries, timeout: config.timeoutMs };\n } else {\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseURL,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n });\n this.callOptions = undefined;\n }\n }\n\n async complete(request: ProviderRequest): Promise<ProviderResponse> {\n const toolName = this.config.toolName ?? toToolName(request.schema.id);\n const maxTokens = this.config.maxTokens ?? DEFAULT_MAX_TOKENS;\n const inputSchema = toInputSchema(\n request.schema,\n request.bundle,\n request.resolvedEnums,\n );\n\n // Sampling parameters go only when configured: newer models reject\n // `temperature` (and `top_p`/`top_k`) outright, and structured\n // extraction does not need them.\n const thinking = this.config.thinking ?? (isClaude5Model(this.config.model) ? { type: \"disabled\" as const } : undefined);\n const message = await this.send(\n {\n model: this.config.model,\n max_tokens: maxTokens,\n ...(this.config.temperature !== undefined ? { temperature: this.config.temperature } : {}),\n ...(thinking ? { thinking } : {}),\n system: this.buildSystem(request.systemPrompt),\n messages: [{ role: \"user\", content: request.userInput }],\n tools: [\n {\n name: toolName,\n description: request.schema.description,\n input_schema: inputSchema as Anthropic.Tool[\"input_schema\"],\n },\n ],\n tool_choice: { type: \"tool\", name: toolName },\n ...this.config.requestOverrides,\n },\n this.callOptions,\n );\n\n const toolUse = message.content.find(\n (block): block is Anthropic.ToolUseBlock =>\n block.type === \"tool_use\" && block.name === toolName,\n );\n\n if (!toolUse) {\n if (message.stop_reason === \"max_tokens\") {\n throw new AnthropicProviderError(\n `Anthropic hit the ${maxTokens}-token output cap before completing the \"${toolName}\" call. ` +\n \"Raise maxTokens, or coerce into a smaller schema.\",\n { kind: \"truncated\", retryable: false, stopReason: \"max_tokens\" },\n );\n }\n throw new AnthropicProviderError(\n `Anthropic returned no \"${toolName}\" tool call (stop_reason: ${message.stop_reason ?? \"unknown\"})`,\n {\n kind: \"no_output\",\n retryable: false,\n stopReason: message.stop_reason ?? undefined,\n },\n );\n }\n\n return {\n data: toolUse.input as Record<string, unknown>,\n usage: {\n promptTokens: message.usage.input_tokens,\n completionTokens: message.usage.output_tokens,\n totalTokens: message.usage.input_tokens + message.usage.output_tokens,\n ...(message.usage.cache_read_input_tokens != null && {\n cacheReadTokens: message.usage.cache_read_input_tokens,\n }),\n ...(message.usage.cache_creation_input_tokens != null && {\n cacheWriteTokens: message.usage.cache_creation_input_tokens,\n }),\n },\n };\n }\n\n /**\n * The system prompt, marked as a cache breakpoint when caching is on.\n *\n * One breakpoint is enough: the API renders `tools` before `system`, so a\n * marker on the trailing system block covers the tool definition too — and\n * the user input, the only part that changes between calls, sits after it\n * in `messages` where it invalidates nothing.\n */\n private buildSystem(\n systemPrompt: string,\n ): string | Anthropic.TextBlockParam[] {\n if (!this.config.cachePrompt) return systemPrompt;\n\n return [\n {\n type: \"text\",\n text: systemPrompt,\n cache_control: { type: \"ephemeral\", ttl: this.config.cacheTtl ?? \"5m\" },\n },\n ];\n }\n\n /** Issue the call, translating SDK failures into typed provider errors. */\n private async send(\n body: Anthropic.MessageCreateParamsNonStreaming,\n options: CallOptions | undefined,\n ): Promise<Anthropic.Message> {\n try {\n return await this.client.messages.create(body, options);\n } catch (error) {\n throw toProviderError(error);\n }\n }\n}\n","import type Anthropic from \"@anthropic-ai/sdk\";\nimport type { ProviderConfig } from \"@sembl/core\";\n\n/**\n * Configuration specific to the Anthropic provider.\n *\n * Supply either `client` or `apiKey`. Prefer `client` when the host app\n * already resolves credentials its own way (Secret Manager, Vault, Bedrock,\n * a Vertex client) — the provider will reuse that client as-is rather than\n * constructing its own.\n */\nexport interface AnthropicProviderConfig extends ProviderConfig {\n /**\n * A pre-built Anthropic client. Takes precedence over `apiKey`/`baseURL`.\n * Also accepts an `AnthropicBedrock` / `AnthropicVertex` client — anything\n * exposing a compatible `messages.create`.\n */\n client?: Pick<Anthropic, \"messages\">;\n /** Anthropic API key. Ignored when `client` is supplied. */\n apiKey?: string;\n /** Base URL override. Ignored when `client` is supplied. */\n baseURL?: string;\n /**\n * Name given to the extraction tool the model is forced to call.\n * Defaults to a sanitized form of the schema id. Only override this if a\n * name shows up somewhere you care about (logs, prompt-cache keys).\n */\n toolName?: string;\n /**\n * Mark the stable prefix of the request — the tool definition and the\n * system prompt — as cacheable, so a run of calls against the same schema\n * pays to process it once instead of once per call.\n *\n * Off by default: a cache write costs more than an ordinary read of the\n * same tokens, so a single call, or a prefix below the model's minimum\n * cacheable length, comes out slightly behind. Turn it on for batches.\n * `ProviderResponse.usage.cacheReadTokens` says whether it is paying off.\n */\n cachePrompt?: boolean;\n /**\n * Lifetime of the cached prefix. Ignored unless `cachePrompt` is set.\n *\n * `\"5m\"` (the default) is refreshed by every read, so back-to-back calls\n * keep it alive indefinitely and it is the cheaper write. Choose `\"1h\"`\n * only for traffic with gaps longer than five minutes between calls — it\n * survives the gap, but the write costs roughly twice as much.\n */\n cacheTtl?: \"5m\" | \"1h\";\n /**\n * Extended-thinking setting sent with every call.\n *\n * Claude 5 models turn adaptive thinking on by default, and a forced tool\n * call is rejected while it is on, so for those models the provider sends\n * `{ type: \"disabled\" }` unless this is set. Older models reject the\n * parameter outright, so nothing is sent for them by default. Set it\n * explicitly to override either behaviour.\n */\n thinking?: Anthropic.ThinkingConfigParam;\n /**\n * Fields merged into the request body last, after everything the provider\n * builds. The escape hatch for the next model-specific parameter, so a\n * caller does not have to wait for a library release — or wrap the client\n * — to send it.\n */\n requestOverrides?: Partial<Anthropic.MessageCreateParamsNonStreaming>;\n /**\n * How many times the SDK retries a failed call before giving up. The SDK\n * retries connection errors, 408/409/429 and 5xx with exponential backoff\n * and honours `retry-after`, so there is nothing to hand-roll here.\n *\n * Defaults to {@link DEFAULT_MAX_RETRIES}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n maxRetries?: number;\n /**\n * Timeout for a single attempt, in milliseconds. Retries each get their\n * own attempt, so the worst-case wall clock is roughly\n * `timeoutMs * (maxRetries + 1)` plus backoff.\n *\n * Defaults to {@link DEFAULT_TIMEOUT_MS}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n timeoutMs?: number;\n}\n\n/** Anthropic requires an explicit output budget; this is used when none is set. */\nexport const DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Whether a model id names a Claude 5 model. Those enable adaptive thinking\n * by default, which a forced tool call cannot be combined with.\n */\nexport function isClaude5Model(model: string): boolean {\n return /claude-(?:fable|opus|sonnet|haiku)-5(?:[.-]|$)/i.test(model);\n}\n\n/** Matches the SDK's own default; stated here so it survives an SDK change. */\nexport const DEFAULT_MAX_RETRIES = 2;\n\n/**\n * Two minutes per attempt. The SDK's own default is ten, which is a long time\n * for a backend import to sit on one listing when the retry is cheap.\n */\nexport const DEFAULT_TIMEOUT_MS = 120_000;\n","import { APIConnectionError, APIError } from \"@anthropic-ai/sdk\";\n\n/**\n * Why a provider call failed, in the terms a caller can act on.\n *\n * A batch import wants to route these differently: `\"api\"` failures are worth\n * re-queueing, `\"truncated\"` needs a bigger output budget, and `\"no_output\"`\n * is a property of that one listing's content — retrying it changes nothing.\n *\n * The same three kinds are used by `@sembl/provider-openai`, so a caller that\n * branches on `kind` keeps working when the provider is swapped.\n */\nexport type ProviderErrorKind = \"api\" | \"truncated\" | \"no_output\";\n\n/**\n * Error thrown by the Anthropic provider.\n *\n * Branch on `kind` rather than matching the message — messages stay\n * diagnostic and are free to change.\n */\nexport class AnthropicProviderError extends Error {\n /** What class of failure this is. */\n public readonly kind: ProviderErrorKind;\n /**\n * Whether another attempt could plausibly succeed. The SDK has already\n * retried retryable transport failures (see `maxRetries`); this says only\n * that the failure was transient in nature, so a caller running a queue can\n * re-enqueue the item rather than dead-letter it.\n */\n public readonly retryable: boolean;\n /** HTTP status, when the failure came back as an API error. */\n public readonly status?: number;\n /** Anthropic's `stop_reason`, when the call returned a message we rejected. */\n public readonly stopReason?: string;\n\n constructor(\n message: string,\n options: {\n kind: ProviderErrorKind;\n retryable: boolean;\n status?: number;\n stopReason?: string;\n cause?: unknown;\n },\n ) {\n super(message, { cause: options.cause });\n this.name = \"AnthropicProviderError\";\n this.kind = options.kind;\n this.retryable = options.retryable;\n this.status = options.status;\n this.stopReason = options.stopReason;\n }\n}\n\n/**\n * Wrap an SDK-level failure as an `AnthropicProviderError`.\n *\n * Retryability is read off the SDK's own error classes rather than the\n * message: connection failures and timeouts are transient by construction,\n * and of the status codes only 408/409/429 and 5xx are worth another attempt —\n * the same set the SDK itself retries internally.\n */\nexport function toProviderError(error: unknown): AnthropicProviderError {\n if (error instanceof APIError) {\n const status = error.status;\n const retryable =\n error instanceof APIConnectionError ||\n status === undefined ||\n status === 408 ||\n status === 409 ||\n status === 429 ||\n status >= 500;\n\n return new AnthropicProviderError(\n `Anthropic request failed${status ? ` (${status})` : \"\"}: ${error.message}`,\n { kind: \"api\", retryable, status, cause: error },\n );\n }\n\n // Anything else (an AbortError, a bug in a caller-supplied client) is\n // surfaced with the same shape so callers only need one catch.\n return new AnthropicProviderError(\n `Anthropic request failed: ${error instanceof Error ? error.message : String(error)}`,\n { kind: \"api\", retryable: false, cause: error },\n );\n}\n","import type { RuntimeSchema, ResolvedEnums, SchemaBundle } from \"@sembl/core\";\nimport { runtimeSchemaToJsonSchema } from \"@sembl/core\";\n\n/** Anthropic tool names must match `^[a-zA-Z0-9_-]{1,64}$`. */\nexport function toToolName(schemaId: string): string {\n const cleaned = schemaId.replace(/[^a-zA-Z0-9_-]/g, \"_\").slice(0, 57);\n return `extract_${cleaned || \"schema\"}`.slice(0, 64);\n}\n\n/**\n * Convert a RuntimeSchema to an Anthropic tool `input_schema`.\n *\n * Unlike OpenAI structured outputs, Anthropic takes ordinary JSON Schema, so\n * optional fields are left out of `required` instead of being made nullable.\n * That keeps the model from inventing explicit `null`s for fields the source\n * text simply never mentioned — which matters for partial coercion, where an\n * absent field and a null field mean different things to the caller.\n */\nexport function toInputSchema(\n schema: RuntimeSchema,\n bundle?: SchemaBundle,\n resolvedEnums?: ResolvedEnums,\n): Record<string, unknown> {\n return runtimeSchemaToJsonSchema(schema, bundle, {\n dialect: \"standard\",\n resolvedEnums,\n });\n}\n"],"mappings":";AAAA,OAAO,eAAe;;;ACsFf,IAAM,qBAAqB;AAM3B,SAAS,eAAe,OAAwB;AACrD,SAAO,kDAAkD,KAAK,KAAK;AACrE;AAGO,IAAM,sBAAsB;AAM5B,IAAM,qBAAqB;;;ACvGlC,SAAS,oBAAoB,gBAAgB;AAoBtC,IAAM,yBAAN,cAAqC,MAAM;AAAA;AAAA,EAEhC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA;AAAA;AAAA,EAEA;AAAA;AAAA,EAEA;AAAA,EAEhB,YACE,SACA,SAOA;AACA,UAAM,SAAS,EAAE,OAAO,QAAQ,MAAM,CAAC;AACvC,SAAK,OAAO;AACZ,SAAK,OAAO,QAAQ;AACpB,SAAK,YAAY,QAAQ;AACzB,SAAK,SAAS,QAAQ;AACtB,SAAK,aAAa,QAAQ;AAAA,EAC5B;AACF;AAUO,SAAS,gBAAgB,OAAwC;AACtE,MAAI,iBAAiB,UAAU;AAC7B,UAAM,SAAS,MAAM;AACrB,UAAM,YACJ,iBAAiB,sBACjB,WAAW,UACX,WAAW,OACX,WAAW,OACX,WAAW,OACX,UAAU;AAEZ,WAAO,IAAI;AAAA,MACT,2BAA2B,SAAS,KAAK,MAAM,MAAM,EAAE,KAAK,MAAM,OAAO;AAAA,MACzE,EAAE,MAAM,OAAO,WAAW,QAAQ,OAAO,MAAM;AAAA,IACjD;AAAA,EACF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,GAAI,QAAQ,MAAM,2BAA2B,QAAQ;AAAA,UACnD,iBAAiB,QAAQ,MAAM;AAAA,QACjC;AAAA,QACA,GAAI,QAAQ,MAAM,+BAA+B,QAAQ;AAAA,UACvD,kBAAkB,QAAQ,MAAM;AAAA,QAClC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,YACN,cACqC;AACrC,QAAI,CAAC,KAAK,OAAO,YAAa,QAAO;AAErC,WAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,MAAM;AAAA,QACN,eAAe,EAAE,MAAM,aAAa,KAAK,KAAK,OAAO,YAAY,KAAK;AAAA,MACxE;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,MAAc,KACZ,MACA,SAC4B;AAC5B,QAAI;AACF,aAAO,MAAM,KAAK,OAAO,SAAS,OAAO,MAAM,OAAO;AAAA,IACxD,SAAS,OAAO;AACd,YAAM,gBAAgB,KAAK;AAAA,IAC7B;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/anthropic-provider.ts","../src/anthropic-config.ts","../src/errors.ts","../src/schema-converter.ts"],"sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type { Provider, ProviderRequest, ProviderResponse, ProviderTurn } from \"@sembl/core\";\nimport type { AnthropicProviderConfig } from \"./anthropic-config.js\";\nimport {\n DEFAULT_MAX_RETRIES,\n DEFAULT_MAX_TOKENS,\n DEFAULT_TIMEOUT_MS,\n isClaude5Model,\n} from \"./anthropic-config.js\";\nimport { AnthropicProviderError, toProviderError } from \"./errors.js\";\nimport { toInputSchema, toToolName } from \"./schema-converter.js\";\n\n/**\n * Earlier turns as the API saw them: the model's rejected output as the\n * tool call it made, the correction as that call's result flagged as an\n * error. The forced tool choice then makes the model call the tool again,\n * which is the corrected attempt. Ids only have to match within the\n * conversation, so they are synthesised.\n */\nfunction renderHistory(history: readonly ProviderTurn[], toolName: string): Anthropic.MessageParam[] {\n const messages: Anthropic.MessageParam[] = [];\n let lastToolUseId = \"\";\n history.forEach((turn, i) => {\n if (turn.role === \"assistant\") {\n lastToolUseId = `toolu_sembl_${i}`;\n messages.push({\n role: \"assistant\",\n content: [{ type: \"tool_use\", id: lastToolUseId, name: toolName, input: turn.data }],\n });\n } else if (lastToolUseId) {\n messages.push({\n role: \"user\",\n content: [{ type: \"tool_result\", tool_use_id: lastToolUseId, content: turn.text, is_error: true }],\n });\n } else {\n messages.push({ role: \"user\", content: turn.text });\n }\n });\n return messages;\n}\n\n/** Per-call overrides handed to the SDK alongside the request body. */\ninterface CallOptions {\n maxRetries?: number;\n timeout?: number;\n}\n\n/**\n * Anthropic provider implementation.\n *\n * Structured output is obtained by declaring the target schema as a single\n * tool and forcing the model to call it (`tool_choice: { type: \"tool\" }`), so\n * the arguments come back already parsed and shape-checked by the API — no\n * JSON scraped out of prose.\n *\n * Retries and timeouts are the SDK's (exponential backoff, `retry-after`\n * aware); this class only chooses the numbers and translates whatever comes\n * back out into an {@link AnthropicProviderError}.\n */\nexport class AnthropicProvider implements Provider {\n /** Repair turns are rendered as a tool call and its (failed) result. */\n readonly supportsHistory = true;\n\n private client: Pick<Anthropic, \"messages\">;\n private config: AnthropicProviderConfig;\n private callOptions: CallOptions | undefined;\n\n constructor(config: AnthropicProviderConfig) {\n this.config = config;\n\n if (config.client) {\n // The host owns this client's transport policy, so only override it\n // per-call where the caller asked for something specific.\n this.client = config.client;\n this.callOptions =\n config.maxRetries === undefined && config.timeoutMs === undefined\n ? undefined\n : { maxRetries: config.maxRetries, timeout: config.timeoutMs };\n } else {\n this.client = new Anthropic({\n apiKey: config.apiKey,\n baseURL: config.baseURL,\n maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,\n timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,\n });\n this.callOptions = undefined;\n }\n }\n\n async complete(request: ProviderRequest): Promise<ProviderResponse> {\n const toolName = this.config.toolName ?? toToolName(request.schema.id);\n const maxTokens = this.config.maxTokens ?? DEFAULT_MAX_TOKENS;\n const inputSchema = toInputSchema(\n request.schema,\n request.bundle,\n request.resolvedEnums,\n );\n\n // Sampling parameters go only when configured: newer models reject\n // `temperature` (and `top_p`/`top_k`) outright, and structured\n // extraction does not need them.\n const thinking = this.config.thinking ?? (isClaude5Model(this.config.model) ? { type: \"disabled\" as const } : undefined);\n const message = await this.send(\n {\n model: this.config.model,\n max_tokens: maxTokens,\n ...(this.config.temperature !== undefined ? { temperature: this.config.temperature } : {}),\n ...(thinking ? { thinking } : {}),\n system: this.buildSystem(request.systemPrompt),\n messages: [\n { role: \"user\", content: request.userInput },\n ...renderHistory(request.history ?? [], toolName),\n ],\n tools: [\n {\n name: toolName,\n description: request.schema.description,\n input_schema: inputSchema as Anthropic.Tool[\"input_schema\"],\n },\n ],\n tool_choice: { type: \"tool\", name: toolName },\n ...this.config.requestOverrides,\n },\n this.callOptions,\n );\n\n const toolUse = message.content.find(\n (block): block is Anthropic.ToolUseBlock =>\n block.type === \"tool_use\" && block.name === toolName,\n );\n\n if (!toolUse) {\n if (message.stop_reason === \"max_tokens\") {\n throw new AnthropicProviderError(\n `Anthropic hit the ${maxTokens}-token output cap before completing the \"${toolName}\" call. ` +\n \"Raise maxTokens, or coerce into a smaller schema.\",\n { kind: \"truncated\", retryable: false, stopReason: \"max_tokens\" },\n );\n }\n throw new AnthropicProviderError(\n `Anthropic returned no \"${toolName}\" tool call (stop_reason: ${message.stop_reason ?? \"unknown\"})`,\n {\n kind: \"no_output\",\n retryable: false,\n stopReason: message.stop_reason ?? undefined,\n },\n );\n }\n\n return {\n data: toolUse.input as Record<string, unknown>,\n usage: {\n promptTokens: message.usage.input_tokens,\n completionTokens: message.usage.output_tokens,\n totalTokens: message.usage.input_tokens + message.usage.output_tokens,\n ...(message.usage.cache_read_input_tokens != null && {\n cacheReadTokens: message.usage.cache_read_input_tokens,\n }),\n ...(message.usage.cache_creation_input_tokens != null && {\n cacheWriteTokens: message.usage.cache_creation_input_tokens,\n }),\n },\n };\n }\n\n /**\n * The system prompt, marked as a cache breakpoint when caching is on.\n *\n * One breakpoint is enough: the API renders `tools` before `system`, so a\n * marker on the trailing system block covers the tool definition too — and\n * the user input, the only part that changes between calls, sits after it\n * in `messages` where it invalidates nothing.\n */\n private buildSystem(\n systemPrompt: string,\n ): string | Anthropic.TextBlockParam[] {\n if (!this.config.cachePrompt) return systemPrompt;\n\n return [\n {\n type: \"text\",\n text: systemPrompt,\n cache_control: { type: \"ephemeral\", ttl: this.config.cacheTtl ?? \"5m\" },\n },\n ];\n }\n\n /** Issue the call, translating SDK failures into typed provider errors. */\n private async send(\n body: Anthropic.MessageCreateParamsNonStreaming,\n options: CallOptions | undefined,\n ): Promise<Anthropic.Message> {\n try {\n return await this.client.messages.create(body, options);\n } catch (error) {\n throw toProviderError(error);\n }\n }\n}\n","import type Anthropic from \"@anthropic-ai/sdk\";\nimport type { ProviderConfig } from \"@sembl/core\";\n\n/**\n * Configuration specific to the Anthropic provider.\n *\n * Supply either `client` or `apiKey`. Prefer `client` when the host app\n * already resolves credentials its own way (Secret Manager, Vault, Bedrock,\n * a Vertex client) — the provider will reuse that client as-is rather than\n * constructing its own.\n */\nexport interface AnthropicProviderConfig extends ProviderConfig {\n /**\n * A pre-built Anthropic client. Takes precedence over `apiKey`/`baseURL`.\n * Also accepts an `AnthropicBedrock` / `AnthropicVertex` client — anything\n * exposing a compatible `messages.create`.\n */\n client?: Pick<Anthropic, \"messages\">;\n /** Anthropic API key. Ignored when `client` is supplied. */\n apiKey?: string;\n /** Base URL override. Ignored when `client` is supplied. */\n baseURL?: string;\n /**\n * Name given to the extraction tool the model is forced to call.\n * Defaults to a sanitized form of the schema id. Only override this if a\n * name shows up somewhere you care about (logs, prompt-cache keys).\n */\n toolName?: string;\n /**\n * Mark the stable prefix of the request — the tool definition and the\n * system prompt — as cacheable, so a run of calls against the same schema\n * pays to process it once instead of once per call.\n *\n * Off by default: a cache write costs more than an ordinary read of the\n * same tokens, so a single call, or a prefix below the model's minimum\n * cacheable length, comes out slightly behind. Turn it on for batches.\n * `ProviderResponse.usage.cacheReadTokens` says whether it is paying off.\n */\n cachePrompt?: boolean;\n /**\n * Lifetime of the cached prefix. Ignored unless `cachePrompt` is set.\n *\n * `\"5m\"` (the default) is refreshed by every read, so back-to-back calls\n * keep it alive indefinitely and it is the cheaper write. Choose `\"1h\"`\n * only for traffic with gaps longer than five minutes between calls — it\n * survives the gap, but the write costs roughly twice as much.\n */\n cacheTtl?: \"5m\" | \"1h\";\n /**\n * Extended-thinking setting sent with every call.\n *\n * Claude 5 models turn adaptive thinking on by default, and a forced tool\n * call is rejected while it is on, so for those models the provider sends\n * `{ type: \"disabled\" }` unless this is set. Older models reject the\n * parameter outright, so nothing is sent for them by default. Set it\n * explicitly to override either behaviour.\n */\n thinking?: Anthropic.ThinkingConfigParam;\n /**\n * Fields merged into the request body last, after everything the provider\n * builds. The escape hatch for the next model-specific parameter, so a\n * caller does not have to wait for a library release — or wrap the client\n * — to send it.\n */\n requestOverrides?: Partial<Anthropic.MessageCreateParamsNonStreaming>;\n /**\n * How many times the SDK retries a failed call before giving up. The SDK\n * retries connection errors, 408/409/429 and 5xx with exponential backoff\n * and honours `retry-after`, so there is nothing to hand-roll here.\n *\n * Defaults to {@link DEFAULT_MAX_RETRIES}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n maxRetries?: number;\n /**\n * Timeout for a single attempt, in milliseconds. Retries each get their\n * own attempt, so the worst-case wall clock is roughly\n * `timeoutMs * (maxRetries + 1)` plus backoff.\n *\n * Defaults to {@link DEFAULT_TIMEOUT_MS}. When a `client` is supplied,\n * leaving this unset keeps that client's own policy.\n */\n timeoutMs?: number;\n}\n\n/** Anthropic requires an explicit output budget; this is used when none is set. */\nexport const DEFAULT_MAX_TOKENS = 4096;\n\n/**\n * Whether a model id names a Claude 5 model. Those enable adaptive thinking\n * by default, which a forced tool call cannot be combined with.\n */\nexport function isClaude5Model(model: string): boolean {\n return /claude-(?:fable|opus|sonnet|haiku)-5(?:[.-]|$)/i.test(model);\n}\n\n/** Matches the SDK's own default; stated here so it survives an SDK change. */\nexport const DEFAULT_MAX_RETRIES = 2;\n\n/**\n * Two minutes per attempt. The SDK's own default is ten, which is a long time\n * for a backend import to sit on one listing when the retry is cheap.\n */\nexport const DEFAULT_TIMEOUT_MS = 120_000;\n","import { APIConnectionError, APIError } from \"@anthropic-ai/sdk\";\n\n/**\n * Why a provider call failed, in the terms a caller can act on.\n *\n * A batch import wants to route these differently: `\"api\"` failures are worth\n * re-queueing, `\"truncated\"` needs a bigger output budget, and `\"no_output\"`\n * is a property of that one listing's content — retrying it changes nothing.\n *\n * The same three kinds are used by `@sembl/provider-openai`, so a caller that\n * branches on `kind` keeps working when the provider is swapped.\n */\nexport type ProviderErrorKind = \"api\" | \"truncated\" | \"no_output\";\n\n/**\n * Error thrown by the Anthropic provider.\n *\n * Branch on `kind` rather than matching the message — messages stay\n * diagnostic and are free to change.\n */\nexport class AnthropicProviderError extends Error {\n /** What class of failure this is. */\n public readonly kind: ProviderErrorKind;\n /**\n * Whether another attempt could plausibly succeed. The SDK has already\n * retried retryable transport failures (see `maxRetries`); this says only\n * that the failure was transient in nature, so a caller running a queue can\n * re-enqueue the item rather than dead-letter it.\n */\n public readonly retryable: boolean;\n /** HTTP status, when the failure came back as an API error. */\n public readonly status?: number;\n /** Anthropic's `stop_reason`, when the call returned a message we rejected. */\n public readonly stopReason?: string;\n\n constructor(\n message: string,\n options: {\n kind: ProviderErrorKind;\n retryable: boolean;\n status?: number;\n stopReason?: string;\n cause?: unknown;\n },\n ) {\n super(message, { cause: options.cause });\n this.name = \"AnthropicProviderError\";\n this.kind = options.kind;\n this.retryable = options.retryable;\n this.status = options.status;\n this.stopReason = options.stopReason;\n }\n}\n\n/**\n * Wrap an SDK-level failure as an `AnthropicProviderError`.\n *\n * Retryability is read off the SDK's own error classes rather than the\n * message: connection failures and timeouts are transient by construction,\n * and of the status codes only 408/409/429 and 5xx are worth another attempt —\n * the same set the SDK itself retries internally.\n */\nexport function toProviderError(error: unknown): AnthropicProviderError {\n if (error instanceof APIError) {\n const status = error.status;\n const retryable =\n error instanceof APIConnectionError ||\n status === undefined ||\n status === 408 ||\n status === 409 ||\n status === 429 ||\n status >= 500;\n\n return new AnthropicProviderError(\n `Anthropic request failed${status ? ` (${status})` : \"\"}: ${error.message}`,\n { kind: \"api\", retryable, status, cause: error },\n );\n }\n\n // Anything else (an AbortError, a bug in a caller-supplied client) is\n // surfaced with the same shape so callers only need one catch.\n return new AnthropicProviderError(\n `Anthropic request failed: ${error instanceof Error ? error.message : String(error)}`,\n { kind: \"api\", retryable: false, cause: error },\n );\n}\n","import type { RuntimeSchema, ResolvedEnums, SchemaBundle } from \"@sembl/core\";\nimport { runtimeSchemaToJsonSchema } from \"@sembl/core\";\n\n/** Anthropic tool names must match `^[a-zA-Z0-9_-]{1,64}$`. */\nexport function toToolName(schemaId: string): string {\n const cleaned = schemaId.replace(/[^a-zA-Z0-9_-]/g, \"_\").slice(0, 57);\n return `extract_${cleaned || \"schema\"}`.slice(0, 64);\n}\n\n/**\n * Convert a RuntimeSchema to an Anthropic tool `input_schema`.\n *\n * Unlike OpenAI structured outputs, Anthropic takes ordinary JSON Schema, so\n * optional fields are left out of `required` instead of being made nullable.\n * That keeps the model from inventing explicit `null`s for fields the source\n * text simply never mentioned — which matters for partial coercion, where an\n * absent field and a null field mean different things to the caller.\n */\nexport function toInputSchema(\n schema: RuntimeSchema,\n bundle?: SchemaBundle,\n resolvedEnums?: ResolvedEnums,\n): Record<string, unknown> {\n return runtimeSchemaToJsonSchema(schema, bundle, {\n dialect: \"standard\",\n resolvedEnums,\n });\n}\n"],"mappings":";AAAA,OAAO,eAAe;;;ACsFf,IAAM,qBAAqB;AAM3B,SAAS,eAAe,OAAwB;AACrD,SAAO,kDAAkD,KAAK,KAAK;AACrE;AAGO,IAAM,sBAAsB;AAM5B,IAAM,qBAAqB;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IACF;AAEA,WAAO;AAAA,MACL,MAAM,QAAQ;AAAA,MACd,OAAO;AAAA,QACL,cAAc,QAAQ,MAAM;AAAA,QAC5B,kBAAkB,QAAQ,MAAM;AAAA,QAChC,aAAa,QAAQ,MAAM,eAAe,QAAQ,MAAM;AAAA,QACxD,GAAI,QAAQ,MAAM,2BAA2B,QAAQ;AAAA,UACnD,iBAAiB,QAAQ,MAAM;AAAA,QACjC;AAAA,QACA,GAAI,QAAQ,MAAM,+BAA+B,QAAQ;AAAA,UACvD,kBAAkB,QAAQ,MAAM;AAAA,QAClC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUQ,YACN,cACqC;AACrC,QAAI,CAAC,KAAK,OAAO,YAAa,QAAO;AAErC,WAAO;AAAA,MACL;AAAA,QACE,MAAM;AAAA,QACN,MAAM;AAAA,QACN,eAAe,EAAE,MAAM,aAAa,KAAK,KAAK,OAAO,YAAY,KAAK;AAAA,MACxE;AAAA,IACF;AAAA,EACF;AAAA;AAAA,EAGA,MAAc,KACZ,MACA,SAC4B;AAC5B,QAAI;AACF,aAAO,MAAM,KAAK,OAAO,SAAS,OAAO,MAAM,OAAO;AAAA,IACxD,SAAS,OAAO;AACd,YAAM,gBAAgB,KAAK;AAAA,IAC7B;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sembl/provider-anthropic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Anthropic provider for SEMBL, using forced tool calls for structured output.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"provenance": true
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@sembl/core": "0.
|
|
55
|
+
"@sembl/core": "0.5.0"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
58
|
"@anthropic-ai/sdk": ">=0.30.0"
|