@sembl/provider-anthropic 0.1.0 → 0.2.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 +221 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +162 -0
- package/package.json +14 -6
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
AnthropicProvider: () => AnthropicProvider,
|
|
34
|
+
AnthropicProviderError: () => AnthropicProviderError,
|
|
35
|
+
DEFAULT_MAX_RETRIES: () => DEFAULT_MAX_RETRIES,
|
|
36
|
+
DEFAULT_MAX_TOKENS: () => DEFAULT_MAX_TOKENS,
|
|
37
|
+
DEFAULT_TIMEOUT_MS: () => DEFAULT_TIMEOUT_MS,
|
|
38
|
+
toInputSchema: () => toInputSchema,
|
|
39
|
+
toToolName: () => toToolName
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
42
|
+
|
|
43
|
+
// src/anthropic-provider.ts
|
|
44
|
+
var import_sdk2 = __toESM(require("@anthropic-ai/sdk"), 1);
|
|
45
|
+
|
|
46
|
+
// src/anthropic-config.ts
|
|
47
|
+
var DEFAULT_MAX_TOKENS = 4096;
|
|
48
|
+
var DEFAULT_MAX_RETRIES = 2;
|
|
49
|
+
var DEFAULT_TIMEOUT_MS = 12e4;
|
|
50
|
+
|
|
51
|
+
// src/errors.ts
|
|
52
|
+
var import_sdk = require("@anthropic-ai/sdk");
|
|
53
|
+
var AnthropicProviderError = class extends Error {
|
|
54
|
+
/** What class of failure this is. */
|
|
55
|
+
kind;
|
|
56
|
+
/**
|
|
57
|
+
* Whether another attempt could plausibly succeed. The SDK has already
|
|
58
|
+
* retried retryable transport failures (see `maxRetries`); this says only
|
|
59
|
+
* that the failure was transient in nature, so a caller running a queue can
|
|
60
|
+
* re-enqueue the item rather than dead-letter it.
|
|
61
|
+
*/
|
|
62
|
+
retryable;
|
|
63
|
+
/** HTTP status, when the failure came back as an API error. */
|
|
64
|
+
status;
|
|
65
|
+
/** Anthropic's `stop_reason`, when the call returned a message we rejected. */
|
|
66
|
+
stopReason;
|
|
67
|
+
constructor(message, options) {
|
|
68
|
+
super(message, { cause: options.cause });
|
|
69
|
+
this.name = "AnthropicProviderError";
|
|
70
|
+
this.kind = options.kind;
|
|
71
|
+
this.retryable = options.retryable;
|
|
72
|
+
this.status = options.status;
|
|
73
|
+
this.stopReason = options.stopReason;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
function toProviderError(error) {
|
|
77
|
+
if (error instanceof import_sdk.APIError) {
|
|
78
|
+
const status = error.status;
|
|
79
|
+
const retryable = error instanceof import_sdk.APIConnectionError || status === void 0 || status === 408 || status === 409 || status === 429 || status >= 500;
|
|
80
|
+
return new AnthropicProviderError(
|
|
81
|
+
`Anthropic request failed${status ? ` (${status})` : ""}: ${error.message}`,
|
|
82
|
+
{ kind: "api", retryable, status, cause: error }
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return new AnthropicProviderError(
|
|
86
|
+
`Anthropic request failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
87
|
+
{ kind: "api", retryable: false, cause: error }
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/schema-converter.ts
|
|
92
|
+
var import_core = require("@sembl/core");
|
|
93
|
+
function toToolName(schemaId) {
|
|
94
|
+
const cleaned = schemaId.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 57);
|
|
95
|
+
return `extract_${cleaned || "schema"}`.slice(0, 64);
|
|
96
|
+
}
|
|
97
|
+
function toInputSchema(schema, bundle, resolvedEnums) {
|
|
98
|
+
return (0, import_core.runtimeSchemaToJsonSchema)(schema, bundle, {
|
|
99
|
+
dialect: "standard",
|
|
100
|
+
resolvedEnums
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/anthropic-provider.ts
|
|
105
|
+
var AnthropicProvider = class {
|
|
106
|
+
client;
|
|
107
|
+
config;
|
|
108
|
+
callOptions;
|
|
109
|
+
constructor(config) {
|
|
110
|
+
this.config = config;
|
|
111
|
+
if (config.client) {
|
|
112
|
+
this.client = config.client;
|
|
113
|
+
this.callOptions = config.maxRetries === void 0 && config.timeoutMs === void 0 ? void 0 : { maxRetries: config.maxRetries, timeout: config.timeoutMs };
|
|
114
|
+
} else {
|
|
115
|
+
this.client = new import_sdk2.default({
|
|
116
|
+
apiKey: config.apiKey,
|
|
117
|
+
baseURL: config.baseURL,
|
|
118
|
+
maxRetries: config.maxRetries ?? DEFAULT_MAX_RETRIES,
|
|
119
|
+
timeout: config.timeoutMs ?? DEFAULT_TIMEOUT_MS
|
|
120
|
+
});
|
|
121
|
+
this.callOptions = void 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async complete(request) {
|
|
125
|
+
const toolName = this.config.toolName ?? toToolName(request.schema.id);
|
|
126
|
+
const maxTokens = this.config.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
127
|
+
const inputSchema = toInputSchema(
|
|
128
|
+
request.schema,
|
|
129
|
+
request.bundle,
|
|
130
|
+
request.resolvedEnums
|
|
131
|
+
);
|
|
132
|
+
const message = await this.send(
|
|
133
|
+
{
|
|
134
|
+
model: this.config.model,
|
|
135
|
+
max_tokens: maxTokens,
|
|
136
|
+
temperature: this.config.temperature ?? 0,
|
|
137
|
+
system: this.buildSystem(request.systemPrompt),
|
|
138
|
+
messages: [{ role: "user", content: request.userInput }],
|
|
139
|
+
tools: [
|
|
140
|
+
{
|
|
141
|
+
name: toolName,
|
|
142
|
+
description: request.schema.description,
|
|
143
|
+
input_schema: inputSchema
|
|
144
|
+
}
|
|
145
|
+
],
|
|
146
|
+
tool_choice: { type: "tool", name: toolName }
|
|
147
|
+
},
|
|
148
|
+
this.callOptions
|
|
149
|
+
);
|
|
150
|
+
const toolUse = message.content.find(
|
|
151
|
+
(block) => block.type === "tool_use" && block.name === toolName
|
|
152
|
+
);
|
|
153
|
+
if (!toolUse) {
|
|
154
|
+
if (message.stop_reason === "max_tokens") {
|
|
155
|
+
throw new AnthropicProviderError(
|
|
156
|
+
`Anthropic hit the ${maxTokens}-token output cap before completing the "${toolName}" call. Raise maxTokens, or coerce into a smaller schema.`,
|
|
157
|
+
{ kind: "truncated", retryable: false, stopReason: "max_tokens" }
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
throw new AnthropicProviderError(
|
|
161
|
+
`Anthropic returned no "${toolName}" tool call (stop_reason: ${message.stop_reason ?? "unknown"})`,
|
|
162
|
+
{
|
|
163
|
+
kind: "no_output",
|
|
164
|
+
retryable: false,
|
|
165
|
+
stopReason: message.stop_reason ?? void 0
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
data: toolUse.input,
|
|
171
|
+
usage: {
|
|
172
|
+
promptTokens: message.usage.input_tokens,
|
|
173
|
+
completionTokens: message.usage.output_tokens,
|
|
174
|
+
totalTokens: message.usage.input_tokens + message.usage.output_tokens,
|
|
175
|
+
...message.usage.cache_read_input_tokens != null && {
|
|
176
|
+
cacheReadTokens: message.usage.cache_read_input_tokens
|
|
177
|
+
},
|
|
178
|
+
...message.usage.cache_creation_input_tokens != null && {
|
|
179
|
+
cacheWriteTokens: message.usage.cache_creation_input_tokens
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The system prompt, marked as a cache breakpoint when caching is on.
|
|
186
|
+
*
|
|
187
|
+
* One breakpoint is enough: the API renders `tools` before `system`, so a
|
|
188
|
+
* marker on the trailing system block covers the tool definition too — and
|
|
189
|
+
* the user input, the only part that changes between calls, sits after it
|
|
190
|
+
* in `messages` where it invalidates nothing.
|
|
191
|
+
*/
|
|
192
|
+
buildSystem(systemPrompt) {
|
|
193
|
+
if (!this.config.cachePrompt) return systemPrompt;
|
|
194
|
+
return [
|
|
195
|
+
{
|
|
196
|
+
type: "text",
|
|
197
|
+
text: systemPrompt,
|
|
198
|
+
cache_control: { type: "ephemeral", ttl: this.config.cacheTtl ?? "5m" }
|
|
199
|
+
}
|
|
200
|
+
];
|
|
201
|
+
}
|
|
202
|
+
/** Issue the call, translating SDK failures into typed provider errors. */
|
|
203
|
+
async send(body, options) {
|
|
204
|
+
try {
|
|
205
|
+
return await this.client.messages.create(body, options);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
throw toProviderError(error);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
+
0 && (module.exports = {
|
|
213
|
+
AnthropicProvider,
|
|
214
|
+
AnthropicProviderError,
|
|
215
|
+
DEFAULT_MAX_RETRIES,
|
|
216
|
+
DEFAULT_MAX_TOKENS,
|
|
217
|
+
DEFAULT_TIMEOUT_MS,
|
|
218
|
+
toInputSchema,
|
|
219
|
+
toToolName
|
|
220
|
+
});
|
|
221
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +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} 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 const message = await this.send(\n {\n model: this.config.model,\n max_tokens: maxTokens,\n temperature: this.config.temperature ?? 0,\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 },\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 * 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/** 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;AA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;","names":["import_sdk","Anthropic"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { ProviderConfig, Provider, ProviderRequest, ProviderResponse, RuntimeSchema, SchemaBundle, ResolvedEnums } from '@sembl/core';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration specific to the Anthropic provider.
|
|
6
|
+
*
|
|
7
|
+
* Supply either `client` or `apiKey`. Prefer `client` when the host app
|
|
8
|
+
* already resolves credentials its own way (Secret Manager, Vault, Bedrock,
|
|
9
|
+
* a Vertex client) — the provider will reuse that client as-is rather than
|
|
10
|
+
* constructing its own.
|
|
11
|
+
*/
|
|
12
|
+
interface AnthropicProviderConfig extends ProviderConfig {
|
|
13
|
+
/**
|
|
14
|
+
* A pre-built Anthropic client. Takes precedence over `apiKey`/`baseURL`.
|
|
15
|
+
* Also accepts an `AnthropicBedrock` / `AnthropicVertex` client — anything
|
|
16
|
+
* exposing a compatible `messages.create`.
|
|
17
|
+
*/
|
|
18
|
+
client?: Pick<Anthropic, "messages">;
|
|
19
|
+
/** Anthropic API key. Ignored when `client` is supplied. */
|
|
20
|
+
apiKey?: string;
|
|
21
|
+
/** Base URL override. Ignored when `client` is supplied. */
|
|
22
|
+
baseURL?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Name given to the extraction tool the model is forced to call.
|
|
25
|
+
* Defaults to a sanitized form of the schema id. Only override this if a
|
|
26
|
+
* name shows up somewhere you care about (logs, prompt-cache keys).
|
|
27
|
+
*/
|
|
28
|
+
toolName?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Mark the stable prefix of the request — the tool definition and the
|
|
31
|
+
* system prompt — as cacheable, so a run of calls against the same schema
|
|
32
|
+
* pays to process it once instead of once per call.
|
|
33
|
+
*
|
|
34
|
+
* Off by default: a cache write costs more than an ordinary read of the
|
|
35
|
+
* same tokens, so a single call, or a prefix below the model's minimum
|
|
36
|
+
* cacheable length, comes out slightly behind. Turn it on for batches.
|
|
37
|
+
* `ProviderResponse.usage.cacheReadTokens` says whether it is paying off.
|
|
38
|
+
*/
|
|
39
|
+
cachePrompt?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Lifetime of the cached prefix. Ignored unless `cachePrompt` is set.
|
|
42
|
+
*
|
|
43
|
+
* `"5m"` (the default) is refreshed by every read, so back-to-back calls
|
|
44
|
+
* keep it alive indefinitely and it is the cheaper write. Choose `"1h"`
|
|
45
|
+
* only for traffic with gaps longer than five minutes between calls — it
|
|
46
|
+
* survives the gap, but the write costs roughly twice as much.
|
|
47
|
+
*/
|
|
48
|
+
cacheTtl?: "5m" | "1h";
|
|
49
|
+
/**
|
|
50
|
+
* How many times the SDK retries a failed call before giving up. The SDK
|
|
51
|
+
* retries connection errors, 408/409/429 and 5xx with exponential backoff
|
|
52
|
+
* and honours `retry-after`, so there is nothing to hand-roll here.
|
|
53
|
+
*
|
|
54
|
+
* Defaults to {@link DEFAULT_MAX_RETRIES}. When a `client` is supplied,
|
|
55
|
+
* leaving this unset keeps that client's own policy.
|
|
56
|
+
*/
|
|
57
|
+
maxRetries?: number;
|
|
58
|
+
/**
|
|
59
|
+
* Timeout for a single attempt, in milliseconds. Retries each get their
|
|
60
|
+
* own attempt, so the worst-case wall clock is roughly
|
|
61
|
+
* `timeoutMs * (maxRetries + 1)` plus backoff.
|
|
62
|
+
*
|
|
63
|
+
* Defaults to {@link DEFAULT_TIMEOUT_MS}. When a `client` is supplied,
|
|
64
|
+
* leaving this unset keeps that client's own policy.
|
|
65
|
+
*/
|
|
66
|
+
timeoutMs?: number;
|
|
67
|
+
}
|
|
68
|
+
/** Anthropic requires an explicit output budget; this is used when none is set. */
|
|
69
|
+
declare const DEFAULT_MAX_TOKENS = 4096;
|
|
70
|
+
/** Matches the SDK's own default; stated here so it survives an SDK change. */
|
|
71
|
+
declare const DEFAULT_MAX_RETRIES = 2;
|
|
72
|
+
/**
|
|
73
|
+
* Two minutes per attempt. The SDK's own default is ten, which is a long time
|
|
74
|
+
* for a backend import to sit on one listing when the retry is cheap.
|
|
75
|
+
*/
|
|
76
|
+
declare const DEFAULT_TIMEOUT_MS = 120000;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Anthropic provider implementation.
|
|
80
|
+
*
|
|
81
|
+
* Structured output is obtained by declaring the target schema as a single
|
|
82
|
+
* tool and forcing the model to call it (`tool_choice: { type: "tool" }`), so
|
|
83
|
+
* the arguments come back already parsed and shape-checked by the API — no
|
|
84
|
+
* JSON scraped out of prose.
|
|
85
|
+
*
|
|
86
|
+
* Retries and timeouts are the SDK's (exponential backoff, `retry-after`
|
|
87
|
+
* aware); this class only chooses the numbers and translates whatever comes
|
|
88
|
+
* back out into an {@link AnthropicProviderError}.
|
|
89
|
+
*/
|
|
90
|
+
declare class AnthropicProvider implements Provider {
|
|
91
|
+
private client;
|
|
92
|
+
private config;
|
|
93
|
+
private callOptions;
|
|
94
|
+
constructor(config: AnthropicProviderConfig);
|
|
95
|
+
complete(request: ProviderRequest): Promise<ProviderResponse>;
|
|
96
|
+
/**
|
|
97
|
+
* The system prompt, marked as a cache breakpoint when caching is on.
|
|
98
|
+
*
|
|
99
|
+
* One breakpoint is enough: the API renders `tools` before `system`, so a
|
|
100
|
+
* marker on the trailing system block covers the tool definition too — and
|
|
101
|
+
* the user input, the only part that changes between calls, sits after it
|
|
102
|
+
* in `messages` where it invalidates nothing.
|
|
103
|
+
*/
|
|
104
|
+
private buildSystem;
|
|
105
|
+
/** Issue the call, translating SDK failures into typed provider errors. */
|
|
106
|
+
private send;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Why a provider call failed, in the terms a caller can act on.
|
|
111
|
+
*
|
|
112
|
+
* A batch import wants to route these differently: `"api"` failures are worth
|
|
113
|
+
* re-queueing, `"truncated"` needs a bigger output budget, and `"no_output"`
|
|
114
|
+
* is a property of that one listing's content — retrying it changes nothing.
|
|
115
|
+
*
|
|
116
|
+
* The same three kinds are used by `@sembl/provider-openai`, so a caller that
|
|
117
|
+
* branches on `kind` keeps working when the provider is swapped.
|
|
118
|
+
*/
|
|
119
|
+
type ProviderErrorKind = "api" | "truncated" | "no_output";
|
|
120
|
+
/**
|
|
121
|
+
* Error thrown by the Anthropic provider.
|
|
122
|
+
*
|
|
123
|
+
* Branch on `kind` rather than matching the message — messages stay
|
|
124
|
+
* diagnostic and are free to change.
|
|
125
|
+
*/
|
|
126
|
+
declare class AnthropicProviderError extends Error {
|
|
127
|
+
/** What class of failure this is. */
|
|
128
|
+
readonly kind: ProviderErrorKind;
|
|
129
|
+
/**
|
|
130
|
+
* Whether another attempt could plausibly succeed. The SDK has already
|
|
131
|
+
* retried retryable transport failures (see `maxRetries`); this says only
|
|
132
|
+
* that the failure was transient in nature, so a caller running a queue can
|
|
133
|
+
* re-enqueue the item rather than dead-letter it.
|
|
134
|
+
*/
|
|
135
|
+
readonly retryable: boolean;
|
|
136
|
+
/** HTTP status, when the failure came back as an API error. */
|
|
137
|
+
readonly status?: number;
|
|
138
|
+
/** Anthropic's `stop_reason`, when the call returned a message we rejected. */
|
|
139
|
+
readonly stopReason?: string;
|
|
140
|
+
constructor(message: string, options: {
|
|
141
|
+
kind: ProviderErrorKind;
|
|
142
|
+
retryable: boolean;
|
|
143
|
+
status?: number;
|
|
144
|
+
stopReason?: string;
|
|
145
|
+
cause?: unknown;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Anthropic tool names must match `^[a-zA-Z0-9_-]{1,64}$`. */
|
|
150
|
+
declare function toToolName(schemaId: string): string;
|
|
151
|
+
/**
|
|
152
|
+
* Convert a RuntimeSchema to an Anthropic tool `input_schema`.
|
|
153
|
+
*
|
|
154
|
+
* Unlike OpenAI structured outputs, Anthropic takes ordinary JSON Schema, so
|
|
155
|
+
* optional fields are left out of `required` instead of being made nullable.
|
|
156
|
+
* That keeps the model from inventing explicit `null`s for fields the source
|
|
157
|
+
* text simply never mentioned — which matters for partial coercion, where an
|
|
158
|
+
* absent field and a null field mean different things to the caller.
|
|
159
|
+
*/
|
|
160
|
+
declare function toInputSchema(schema: RuntimeSchema, bundle?: SchemaBundle, resolvedEnums?: ResolvedEnums): Record<string, unknown>;
|
|
161
|
+
|
|
162
|
+
export { AnthropicProvider, type AnthropicProviderConfig, AnthropicProviderError, DEFAULT_MAX_RETRIES, DEFAULT_MAX_TOKENS, DEFAULT_TIMEOUT_MS, type ProviderErrorKind, toInputSchema, toToolName };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sembl/provider-anthropic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Anthropic provider for SEMBL, using forced tool calls for structured output.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"llm",
|
|
@@ -22,13 +22,21 @@
|
|
|
22
22
|
"directory": "packages/provider-anthropic"
|
|
23
23
|
},
|
|
24
24
|
"type": "module",
|
|
25
|
-
"main": "./dist/index.
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"module": "./dist/index.js",
|
|
26
27
|
"types": "./dist/index.d.ts",
|
|
27
28
|
"exports": {
|
|
28
29
|
".": {
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/index.d.cts",
|
|
36
|
+
"default": "./dist/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
32
40
|
},
|
|
33
41
|
"files": [
|
|
34
42
|
"dist",
|
|
@@ -44,7 +52,7 @@
|
|
|
44
52
|
"provenance": true
|
|
45
53
|
},
|
|
46
54
|
"dependencies": {
|
|
47
|
-
"@sembl/core": "0.
|
|
55
|
+
"@sembl/core": "0.2.0"
|
|
48
56
|
},
|
|
49
57
|
"peerDependencies": {
|
|
50
58
|
"@anthropic-ai/sdk": ">=0.30.0"
|