@alvin0/ai-agent-sdk-provider-anthropic 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +142 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 alvin0 (chaulamdinhai) <chaulamdinhai@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# @alvin0/ai-agent-sdk-provider-anthropic
|
|
2
|
+
|
|
3
|
+
Runtime: **Universal** (Edge/Worker, browser, Deno, Bun, and Node).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @alvin0/ai-agent-sdk-core @alvin0/ai-agent-sdk-provider-anthropic
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Universal Anthropic adapter and transactional provider plugin. Credentials are injected; this package never reads environment variables or files.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { ModelRegistry } from '@alvin0/ai-agent-sdk-core'
|
|
13
|
+
import { anthropicPlugin } from '@alvin0/ai-agent-sdk-provider-anthropic'
|
|
14
|
+
|
|
15
|
+
const registry = new ModelRegistry()
|
|
16
|
+
registry.install(anthropicPlugin({ apiKey: () => secretStore.get('anthropic') }))
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Use `anthropicAdapter()` for manual route registration. Both APIs are Universal
|
|
20
|
+
and require an explicit `apiKey`; environment lookup belongs to a Node wrapper.
|
|
21
|
+
|
|
22
|
+
Composition: `runtime.providers`. Lifecycle: `inert-runtime-owned-registration`;
|
|
23
|
+
the runtime activates and removes the captured provider registration.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ModelProviderPlugin, RetryPolicyConfig } from "@alvin0/ai-agent-sdk-core";
|
|
2
|
+
import { ComposableModelProviderPlugin, CredentialInput, ModelTarget } from "@alvin0/ai-agent-sdk-core/provider";
|
|
3
|
+
import { CredentialSource, HttpModelAdapter, ProviderCatalogModel, ProviderRequestLogger } from "@alvin0/ai-agent-sdk-provider-http";
|
|
4
|
+
import { ANTHROPIC_VERSION, AnthropicDialect, AnthropicReasoningState, DEFAULT_THINKING_BUDGETS, ThinkingBudgets, ThinkingBudgets as ThinkingBudgets$1, anthropicMessagesProtocol } from "@alvin0/ai-agent-sdk-protocol-anthropic-messages";
|
|
5
|
+
//#region src/adapter.d.ts
|
|
6
|
+
/** The Anthropic API base. */
|
|
7
|
+
declare const ANTHROPIC_BASE_URL = "https://api.anthropic.com";
|
|
8
|
+
/** How the API key is obtained. */
|
|
9
|
+
type AnthropicCredential = CredentialSource;
|
|
10
|
+
/** Options for {@link anthropicAdapter}. */
|
|
11
|
+
interface AnthropicAdapterOptions {
|
|
12
|
+
/** Injected API key or resolver. Universal packages never read environment variables. */
|
|
13
|
+
apiKey: AnthropicCredential;
|
|
14
|
+
/** Endpoint base; defaults to {@link ANTHROPIC_BASE_URL}. */
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
/** API version header; defaults to {@link ANTHROPIC_VERSION}. */
|
|
17
|
+
version?: string;
|
|
18
|
+
/** Opt-in beta features, sent as `anthropic-beta`. */
|
|
19
|
+
beta?: readonly string[];
|
|
20
|
+
/**
|
|
21
|
+
* Advisory model catalog.
|
|
22
|
+
*
|
|
23
|
+
* Empty by default: this package cannot know which model ids are current, and a
|
|
24
|
+
* stale built-in list would name retired models.
|
|
25
|
+
*/
|
|
26
|
+
models?: readonly ProviderCatalogModel[];
|
|
27
|
+
/** Effort id to thinking-token budget; defaults to {@link DEFAULT_THINKING_BUDGETS}. */
|
|
28
|
+
thinkingBudgets?: ThinkingBudgets$1;
|
|
29
|
+
/**
|
|
30
|
+
* Output cap when neither caller nor catalog names one.
|
|
31
|
+
*
|
|
32
|
+
* This API REQUIRES `max_tokens`, so a default always has to exist.
|
|
33
|
+
*/
|
|
34
|
+
defaultMaxTokens?: number;
|
|
35
|
+
/** Context capacity assumed for an uncatalogued model. */
|
|
36
|
+
defaultContextWindow?: number;
|
|
37
|
+
/** Idle bound while a stream read is outstanding. */
|
|
38
|
+
streamIdleTimeoutMs?: number;
|
|
39
|
+
requestTimeoutMs?: number;
|
|
40
|
+
maxRequestBytes?: number;
|
|
41
|
+
maxResponseBytes?: number;
|
|
42
|
+
maxResponseChunks?: number;
|
|
43
|
+
maxSseEvents?: number;
|
|
44
|
+
maxSseEventChars?: number;
|
|
45
|
+
maxErrorBodyBytes?: number;
|
|
46
|
+
requestLoggerTimeoutMs?: number;
|
|
47
|
+
/** Retry policy this route owns. */
|
|
48
|
+
retryPolicy?: RetryPolicyConfig;
|
|
49
|
+
/** Optional exact wire-request logger; credentials are redacted. */
|
|
50
|
+
requestLogger?: ProviderRequestLogger;
|
|
51
|
+
fetch?: typeof globalThis.fetch;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create an Anthropic adapter.
|
|
55
|
+
* @param options - credential, endpoint, and thinking-budget overrides.
|
|
56
|
+
* @returns the adapter, ready to register.
|
|
57
|
+
*/
|
|
58
|
+
declare function anthropicAdapter(options: AnthropicAdapterOptions): HttpModelAdapter;
|
|
59
|
+
interface AnthropicPluginOptions extends AnthropicAdapterOptions {
|
|
60
|
+
/** Registry routes installed by the plugin. Defaults to `['anthropic']`. */
|
|
61
|
+
readonly routes?: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
interface AnthropicProviderOptions extends Omit<AnthropicAdapterOptions, 'apiKey'> {
|
|
64
|
+
readonly defaultModel?: string | ModelTarget;
|
|
65
|
+
readonly apiKey: CredentialInput;
|
|
66
|
+
readonly id?: string;
|
|
67
|
+
readonly routes?: readonly string[];
|
|
68
|
+
}
|
|
69
|
+
/** Preferred transactional plugin for installing the Anthropic provider. */
|
|
70
|
+
declare function anthropicPlugin(options: AnthropicProviderOptions): ComposableModelProviderPlugin & {
|
|
71
|
+
readonly family: 'anthropic';
|
|
72
|
+
};
|
|
73
|
+
declare function anthropicPlugin(options: AnthropicPluginOptions): ModelProviderPlugin;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { ANTHROPIC_BASE_URL, ANTHROPIC_VERSION, type AnthropicAdapterOptions, type AnthropicCredential, type AnthropicDialect, type AnthropicPluginOptions, type AnthropicProviderOptions, type AnthropicReasoningState, DEFAULT_THINKING_BUDGETS, type ThinkingBudgets, anthropicAdapter, anthropicMessagesProtocol, anthropicPlugin };
|
|
76
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/adapter.ts"],"mappings":";;;;;;cAsCa;;KAOD,sBAAsB;;UAgBjB;;EAEf,QAAQ;;EAER;;EAEA;;EAEA;;;;;;;EAOA,kBAAkB;;EAElB,kBAAkB;;;;;;EAMlB;;EAEA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EAEA,cAAc;;EAEd,gBAAgB;EAChB,eAAe,WAAW;;;;;;;iBAQZ,iBAAiB,SAAS,0BAA0B;UAoCnD,+BAA+B;;WAErC;;UAGM,iCAAiC,KAAK;WAC5C,wBAAwB;WACxB,QAAQ;WACR;WACA;;;iBAIK,gBACd,SAAS,2BACR;WAA2C;;iBAC9B,gBAAgB,SAAS,yBAAyB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { ReasoningEffortId } from "@alvin0/ai-agent-sdk-core";
|
|
2
|
+
import { defineModelProviderPlugin } from "@alvin0/ai-agent-sdk-core/provider";
|
|
3
|
+
import { createHttpProvider, createRuntimeHttpProvider } from "@alvin0/ai-agent-sdk-provider-http";
|
|
4
|
+
import { ANTHROPIC_VERSION, DEFAULT_THINKING_BUDGETS, anthropicMessagesProtocol, anthropicMessagesProtocol as anthropicMessagesProtocol$1 } from "@alvin0/ai-agent-sdk-protocol-anthropic-messages";
|
|
5
|
+
|
|
6
|
+
//#region src/adapter.ts
|
|
7
|
+
/** The Anthropic API base. */
|
|
8
|
+
const ANTHROPIC_BASE_URL = "https://api.anthropic.com";
|
|
9
|
+
/** Turn the configured budgets into selectable reasoning efforts. */
|
|
10
|
+
function reasoningInfo(budgets) {
|
|
11
|
+
return { efforts: Object.keys(budgets).map((id) => ({
|
|
12
|
+
id: ReasoningEffortId(id),
|
|
13
|
+
name: id.charAt(0).toUpperCase() + id.slice(1),
|
|
14
|
+
description: budgets[id] === 0 ? "No extended thinking." : `Up to ${budgets[id]?.toLocaleString() ?? "?"} thinking tokens.`
|
|
15
|
+
})) };
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create an Anthropic adapter.
|
|
19
|
+
* @param options - credential, endpoint, and thinking-budget overrides.
|
|
20
|
+
* @returns the adapter, ready to register.
|
|
21
|
+
*/
|
|
22
|
+
function anthropicAdapter(options) {
|
|
23
|
+
const dialect = {
|
|
24
|
+
budgets: options.thinkingBudgets ?? DEFAULT_THINKING_BUDGETS,
|
|
25
|
+
...options.version === void 0 ? {} : { version: options.version },
|
|
26
|
+
...options.beta === void 0 ? {} : { beta: options.beta }
|
|
27
|
+
};
|
|
28
|
+
return createHttpProvider({
|
|
29
|
+
displayName: "Anthropic",
|
|
30
|
+
protocol: anthropicMessagesProtocol$1,
|
|
31
|
+
baseUrl: options.baseUrl ?? "https://api.anthropic.com",
|
|
32
|
+
auth: {
|
|
33
|
+
kind: "header",
|
|
34
|
+
name: "x-api-key",
|
|
35
|
+
value: options.apiKey,
|
|
36
|
+
label: "the `apiKey` option"
|
|
37
|
+
},
|
|
38
|
+
dialect,
|
|
39
|
+
describeModel: (info, effective) => ({
|
|
40
|
+
...info,
|
|
41
|
+
reasoning: info.reasoning ?? reasoningInfo(effective.budgets)
|
|
42
|
+
}),
|
|
43
|
+
...options.models === void 0 ? {} : { models: options.models },
|
|
44
|
+
defaultMaxTokens: options.defaultMaxTokens ?? 8192,
|
|
45
|
+
defaultContextWindow: options.defaultContextWindow ?? 2e5,
|
|
46
|
+
...options.streamIdleTimeoutMs === void 0 ? {} : { streamIdleTimeoutMs: options.streamIdleTimeoutMs },
|
|
47
|
+
...transportLimits(options),
|
|
48
|
+
...options.retryPolicy === void 0 ? {} : { retryPolicy: options.retryPolicy },
|
|
49
|
+
...options.requestLogger === void 0 ? {} : { requestLogger: options.requestLogger }
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function anthropicPlugin(options) {
|
|
53
|
+
if (!usesRuntimeComposition(options)) return legacyAnthropicPlugin(options);
|
|
54
|
+
const id = options.id ?? "anthropic";
|
|
55
|
+
const routes = Object.freeze([...options.routes ?? [id]]);
|
|
56
|
+
return defineModelProviderPlugin({
|
|
57
|
+
id,
|
|
58
|
+
family: "anthropic",
|
|
59
|
+
displayName: "Anthropic",
|
|
60
|
+
routes,
|
|
61
|
+
...runtimeDefaultModel(options.defaultModel, routes),
|
|
62
|
+
setup(registrar) {
|
|
63
|
+
const adapter = createRuntimeAnthropicAdapter(options);
|
|
64
|
+
const remove = registrar.registerAdapter(adapter);
|
|
65
|
+
return () => {
|
|
66
|
+
remove();
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function legacyAnthropicPlugin(options) {
|
|
72
|
+
const routes = Object.freeze([...options.routes ?? ["anthropic"]]);
|
|
73
|
+
const adapter = anthropicAdapter(options);
|
|
74
|
+
return Object.freeze({
|
|
75
|
+
id: "anthropic",
|
|
76
|
+
displayName: "Anthropic",
|
|
77
|
+
setup(registrar) {
|
|
78
|
+
registrar.registerAdapter(routes, adapter);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function createRuntimeAnthropicAdapter(options) {
|
|
83
|
+
const dialect = {
|
|
84
|
+
budgets: options.thinkingBudgets ?? DEFAULT_THINKING_BUDGETS,
|
|
85
|
+
...options.version === void 0 ? {} : { version: options.version },
|
|
86
|
+
...options.beta === void 0 ? {} : { beta: options.beta }
|
|
87
|
+
};
|
|
88
|
+
return createRuntimeHttpProvider({
|
|
89
|
+
displayName: "Anthropic",
|
|
90
|
+
protocol: anthropicMessagesProtocol$1,
|
|
91
|
+
baseUrl: options.baseUrl ?? "https://api.anthropic.com",
|
|
92
|
+
auth: {
|
|
93
|
+
kind: "header",
|
|
94
|
+
name: "x-api-key",
|
|
95
|
+
value: options.apiKey,
|
|
96
|
+
label: "the `apiKey` option"
|
|
97
|
+
},
|
|
98
|
+
dialect,
|
|
99
|
+
describeModel: (info, effective) => ({
|
|
100
|
+
...info,
|
|
101
|
+
reasoning: info.reasoning ?? reasoningInfo(effective.budgets)
|
|
102
|
+
}),
|
|
103
|
+
...options.models === void 0 ? {} : { models: options.models },
|
|
104
|
+
defaultMaxTokens: options.defaultMaxTokens ?? 8192,
|
|
105
|
+
defaultContextWindow: options.defaultContextWindow ?? 2e5,
|
|
106
|
+
...options.streamIdleTimeoutMs === void 0 ? {} : { streamIdleTimeoutMs: options.streamIdleTimeoutMs },
|
|
107
|
+
...transportLimits(options),
|
|
108
|
+
...options.retryPolicy === void 0 ? {} : { retryPolicy: options.retryPolicy },
|
|
109
|
+
...options.requestLogger === void 0 ? {} : { requestLogger: options.requestLogger }
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
function usesRuntimeComposition(options) {
|
|
113
|
+
if ("id" in options || "defaultModel" in options) return true;
|
|
114
|
+
if (typeof options.apiKey === "object" && options.apiKey !== null) return true;
|
|
115
|
+
return typeof options.apiKey !== "function";
|
|
116
|
+
}
|
|
117
|
+
function runtimeDefaultModel(value, routes) {
|
|
118
|
+
if (value === void 0) return {};
|
|
119
|
+
if (typeof value !== "string") return { defaultModel: value };
|
|
120
|
+
if (routes.length !== 1) throw new TypeError("A string defaultModel requires exactly one Anthropic route");
|
|
121
|
+
return { defaultModel: Object.freeze({
|
|
122
|
+
provider: routes[0],
|
|
123
|
+
id: value
|
|
124
|
+
}) };
|
|
125
|
+
}
|
|
126
|
+
function transportLimits(options) {
|
|
127
|
+
return {
|
|
128
|
+
...options.requestTimeoutMs === void 0 ? {} : { requestTimeoutMs: options.requestTimeoutMs },
|
|
129
|
+
...options.maxRequestBytes === void 0 ? {} : { maxRequestBytes: options.maxRequestBytes },
|
|
130
|
+
...options.maxResponseBytes === void 0 ? {} : { maxResponseBytes: options.maxResponseBytes },
|
|
131
|
+
...options.maxResponseChunks === void 0 ? {} : { maxResponseChunks: options.maxResponseChunks },
|
|
132
|
+
...options.maxSseEvents === void 0 ? {} : { maxSseEvents: options.maxSseEvents },
|
|
133
|
+
...options.maxSseEventChars === void 0 ? {} : { maxSseEventChars: options.maxSseEventChars },
|
|
134
|
+
...options.maxErrorBodyBytes === void 0 ? {} : { maxErrorBodyBytes: options.maxErrorBodyBytes },
|
|
135
|
+
...options.requestLoggerTimeoutMs === void 0 ? {} : { requestLoggerTimeoutMs: options.requestLoggerTimeoutMs },
|
|
136
|
+
...options.fetch === void 0 ? {} : { fetch: options.fetch }
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
//#endregion
|
|
141
|
+
export { ANTHROPIC_BASE_URL, ANTHROPIC_VERSION, DEFAULT_THINKING_BUDGETS, anthropicAdapter, anthropicMessagesProtocol, anthropicPlugin };
|
|
142
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["anthropicMessagesProtocol"],"sources":["../src/adapter.ts"],"sourcesContent":["/**\n * The Anthropic provider: the Messages API on `api.anthropic.com`.\n *\n * Configuration over {@link createHttpProvider}, like every other endpoint here.\n * The one thing it needs beyond the defaults is `describeModel`, to advertise its\n * thinking budgets as selectable reasoning efforts.\n *\n * @module ai-agent-sdk/providers/anthropic/adapter\n */\n\nimport type { ModelReasoningInfo } from '@alvin0/ai-agent-sdk-core'\nimport type { ModelProviderPlugin, ModelProviderRegistrar, RetryPolicyConfig } from '@alvin0/ai-agent-sdk-core'\nimport { ReasoningEffortId } from '@alvin0/ai-agent-sdk-core'\nimport {\n defineModelProviderPlugin,\n type ComposableModelProviderPlugin,\n type CredentialInput,\n type ModelTarget,\n} from '@alvin0/ai-agent-sdk-core/provider'\nimport type {\n HttpModelAdapter,\n ProviderCatalogModel,\n ProviderRequestLogger,\n} from '@alvin0/ai-agent-sdk-provider-http'\nimport {\n createHttpProvider,\n createRuntimeHttpProvider,\n type CredentialSource,\n} from '@alvin0/ai-agent-sdk-provider-http'\nimport {\n ANTHROPIC_VERSION,\n DEFAULT_THINKING_BUDGETS,\n anthropicMessagesProtocol,\n type AnthropicDialect,\n type ThinkingBudgets,\n} from '@alvin0/ai-agent-sdk-protocol-anthropic-messages'\n\n/** The Anthropic API base. */\nexport const ANTHROPIC_BASE_URL = 'https://api.anthropic.com'\n\n// Re-exported so callers can reach the protocol's constants from the provider\n// they are already importing.\nexport { ANTHROPIC_VERSION, DEFAULT_THINKING_BUDGETS }\n\n/** How the API key is obtained. */\nexport type AnthropicCredential = CredentialSource\n\n/** Turn the configured budgets into selectable reasoning efforts. */\nfunction reasoningInfo(budgets: ThinkingBudgets): ModelReasoningInfo {\n return {\n efforts: Object.keys(budgets).map(id => ({\n id: ReasoningEffortId(id),\n name: id.charAt(0).toUpperCase() + id.slice(1),\n description: budgets[id] === 0\n ? 'No extended thinking.'\n : `Up to ${budgets[id]?.toLocaleString() ?? '?'} thinking tokens.`,\n })),\n }\n}\n\n/** Options for {@link anthropicAdapter}. */\nexport interface AnthropicAdapterOptions {\n /** Injected API key or resolver. Universal packages never read environment variables. */\n apiKey: AnthropicCredential\n /** Endpoint base; defaults to {@link ANTHROPIC_BASE_URL}. */\n baseUrl?: string\n /** API version header; defaults to {@link ANTHROPIC_VERSION}. */\n version?: string\n /** Opt-in beta features, sent as `anthropic-beta`. */\n beta?: readonly string[]\n /**\n * Advisory model catalog.\n *\n * Empty by default: this package cannot know which model ids are current, and a\n * stale built-in list would name retired models.\n */\n models?: readonly ProviderCatalogModel[]\n /** Effort id to thinking-token budget; defaults to {@link DEFAULT_THINKING_BUDGETS}. */\n thinkingBudgets?: ThinkingBudgets\n /**\n * Output cap when neither caller nor catalog names one.\n *\n * This API REQUIRES `max_tokens`, so a default always has to exist.\n */\n defaultMaxTokens?: number\n /** Context capacity assumed for an uncatalogued model. */\n defaultContextWindow?: number\n /** Idle bound while a stream read is outstanding. */\n streamIdleTimeoutMs?: number\n requestTimeoutMs?: number\n maxRequestBytes?: number\n maxResponseBytes?: number\n maxResponseChunks?: number\n maxSseEvents?: number\n maxSseEventChars?: number\n maxErrorBodyBytes?: number\n requestLoggerTimeoutMs?: number\n /** Retry policy this route owns. */\n retryPolicy?: RetryPolicyConfig\n /** Optional exact wire-request logger; credentials are redacted. */\n requestLogger?: ProviderRequestLogger\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Create an Anthropic adapter.\n * @param options - credential, endpoint, and thinking-budget overrides.\n * @returns the adapter, ready to register.\n */\nexport function anthropicAdapter(options: AnthropicAdapterOptions): HttpModelAdapter {\n const budgets = options.thinkingBudgets ?? DEFAULT_THINKING_BUDGETS\n const dialect: Partial<AnthropicDialect> = {\n budgets,\n ...options.version === undefined ? {} : { version: options.version },\n ...options.beta === undefined ? {} : { beta: options.beta },\n }\n\n return createHttpProvider({\n displayName: 'Anthropic',\n protocol: anthropicMessagesProtocol,\n baseUrl: options.baseUrl ?? ANTHROPIC_BASE_URL,\n // This API uses its own header rather than `authorization: Bearer`.\n auth: {\n kind: 'header',\n name: 'x-api-key',\n value: options.apiKey,\n label: 'the `apiKey` option',\n },\n dialect,\n describeModel: (info, effective) => ({\n ...info,\n reasoning: info.reasoning ?? reasoningInfo(effective.budgets),\n }),\n ...options.models === undefined ? {} : { models: options.models },\n defaultMaxTokens: options.defaultMaxTokens ?? 8_192,\n defaultContextWindow: options.defaultContextWindow ?? 200_000,\n ...options.streamIdleTimeoutMs === undefined\n ? {}\n : { streamIdleTimeoutMs: options.streamIdleTimeoutMs },\n ...transportLimits(options),\n ...options.retryPolicy === undefined ? {} : { retryPolicy: options.retryPolicy },\n ...options.requestLogger === undefined ? {} : { requestLogger: options.requestLogger },\n })\n}\n\nexport interface AnthropicPluginOptions extends AnthropicAdapterOptions {\n /** Registry routes installed by the plugin. Defaults to `['anthropic']`. */\n readonly routes?: readonly string[]\n}\n\nexport interface AnthropicProviderOptions extends Omit<AnthropicAdapterOptions, 'apiKey'> {\n readonly defaultModel?: string | ModelTarget\n readonly apiKey: CredentialInput\n readonly id?: string\n readonly routes?: readonly string[]\n}\n\n/** Preferred transactional plugin for installing the Anthropic provider. */\nexport function anthropicPlugin(\n options: AnthropicProviderOptions,\n): ComposableModelProviderPlugin & { readonly family: 'anthropic' }\nexport function anthropicPlugin(options: AnthropicPluginOptions): ModelProviderPlugin\nexport function anthropicPlugin(\n options: AnthropicProviderOptions | AnthropicPluginOptions,\n): ModelProviderPlugin | (ComposableModelProviderPlugin & { readonly family: 'anthropic' }) {\n if (!usesRuntimeComposition(options)) return legacyAnthropicPlugin(options)\n const id = options.id ?? 'anthropic'\n const routes = Object.freeze([...(options.routes ?? [id])])\n return defineModelProviderPlugin({\n id,\n family: 'anthropic',\n displayName: 'Anthropic',\n routes,\n ...runtimeDefaultModel(options.defaultModel, routes),\n setup(registrar) {\n const adapter = createRuntimeAnthropicAdapter(options)\n const remove = registrar.registerAdapter(adapter)\n return () => { remove(); return undefined }\n },\n }) as ComposableModelProviderPlugin & { readonly family: 'anthropic' }\n}\n\nfunction legacyAnthropicPlugin(options: AnthropicPluginOptions): ModelProviderPlugin {\n const routes = Object.freeze([...(options.routes ?? ['anthropic'])])\n const adapter = anthropicAdapter(options)\n return Object.freeze({\n id: 'anthropic',\n displayName: 'Anthropic',\n setup(registrar: ModelProviderRegistrar) {\n registrar.registerAdapter(routes, adapter)\n },\n })\n}\n\nfunction createRuntimeAnthropicAdapter(options: AnthropicProviderOptions): HttpModelAdapter {\n const budgets = options.thinkingBudgets ?? DEFAULT_THINKING_BUDGETS\n const dialect: Partial<AnthropicDialect> = {\n budgets,\n ...(options.version === undefined ? {} : { version: options.version }),\n ...(options.beta === undefined ? {} : { beta: options.beta }),\n }\n return createRuntimeHttpProvider({\n displayName: 'Anthropic',\n protocol: anthropicMessagesProtocol,\n baseUrl: options.baseUrl ?? ANTHROPIC_BASE_URL,\n auth: {\n kind: 'header',\n name: 'x-api-key',\n value: options.apiKey,\n label: 'the `apiKey` option',\n },\n dialect,\n describeModel: (info, effective) => ({\n ...info,\n reasoning: info.reasoning ?? reasoningInfo(effective.budgets),\n }),\n ...(options.models === undefined ? {} : { models: options.models }),\n defaultMaxTokens: options.defaultMaxTokens ?? 8_192,\n defaultContextWindow: options.defaultContextWindow ?? 200_000,\n ...(options.streamIdleTimeoutMs === undefined ? {} : { streamIdleTimeoutMs: options.streamIdleTimeoutMs }),\n ...transportLimits(options),\n ...(options.retryPolicy === undefined ? {} : { retryPolicy: options.retryPolicy }),\n ...(options.requestLogger === undefined ? {} : { requestLogger: options.requestLogger }),\n })\n}\n\nfunction usesRuntimeComposition(\n options: AnthropicProviderOptions | AnthropicPluginOptions,\n): options is AnthropicProviderOptions {\n if ('id' in options || 'defaultModel' in options) return true\n if (typeof options.apiKey === 'object' && options.apiKey !== null) return true\n return typeof options.apiKey !== 'function'\n}\n\nfunction runtimeDefaultModel(\n value: string | ModelTarget | undefined,\n routes: readonly string[],\n): { readonly defaultModel?: ModelTarget } {\n if (value === undefined) return {}\n if (typeof value !== 'string') return { defaultModel: value }\n if (routes.length !== 1) {\n throw new TypeError('A string defaultModel requires exactly one Anthropic route')\n }\n return { defaultModel: Object.freeze({ provider: routes[0]!, id: value }) }\n}\n\nfunction transportLimits(options: AnthropicAdapterOptions | AnthropicProviderOptions) {\n return {\n ...options.requestTimeoutMs === undefined ? {} : { requestTimeoutMs: options.requestTimeoutMs },\n ...options.maxRequestBytes === undefined ? {} : { maxRequestBytes: options.maxRequestBytes },\n ...options.maxResponseBytes === undefined ? {} : { maxResponseBytes: options.maxResponseBytes },\n ...options.maxResponseChunks === undefined ? {} : { maxResponseChunks: options.maxResponseChunks },\n ...options.maxSseEvents === undefined ? {} : { maxSseEvents: options.maxSseEvents },\n ...options.maxSseEventChars === undefined ? {} : { maxSseEventChars: options.maxSseEventChars },\n ...options.maxErrorBodyBytes === undefined ? {} : { maxErrorBodyBytes: options.maxErrorBodyBytes },\n ...options.requestLoggerTimeoutMs === undefined ? {} : { requestLoggerTimeoutMs: options.requestLoggerTimeoutMs },\n ...options.fetch === undefined ? {} : { fetch: options.fetch },\n }\n}\n"],"mappings":";;;;;;;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AQ,kBAAkB;EACjG,GAAG,QAAQ,2BAA2B,SAAY,CAAC,IAAI,EAAE,wBAAwB,QAAQ,uBAAuB;EAChH,GAAG,QAAQ,UAAU,SAAY,CAAC,IAAI,EAAE,OAAO,QAAQ,MAAM;CAC/D;AACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alvin0/ai-agent-sdk-provider-anthropic",
|
|
3
|
+
"author": {
|
|
4
|
+
"name": "alvin0 - chaulamdinhai",
|
|
5
|
+
"email": "chaulamdinhai@gmail.com"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.1.0",
|
|
8
|
+
"description": "Universal Anthropic adapter and provider plugin for ai-agent-sdk",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/alvin0/ai-agent-sdk.git",
|
|
13
|
+
"directory": "packages/provider-anthropic"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/alvin0/ai-agent-sdk/tree/main/packages/provider-anthropic#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/alvin0/ai-agent-sdk/issues"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@alvin0/ai-agent-sdk-provider-http": "^0.1.0",
|
|
42
|
+
"@alvin0/ai-agent-sdk-protocol-anthropic-messages": "^0.1.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@alvin0/ai-agent-sdk-core": "^0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@alvin0/ai-agent-sdk-core": "^0.1.0",
|
|
49
|
+
"@alvin0/ai-agent-sdk-testkit": "^0.1.0",
|
|
50
|
+
"@arethetypeswrong/cli": "0.18.5",
|
|
51
|
+
"playwright": "1.62.1",
|
|
52
|
+
"publint": "0.3.24",
|
|
53
|
+
"tsdown": "0.22.14",
|
|
54
|
+
"typescript": "7.0.2",
|
|
55
|
+
"vitest": "4.1.11",
|
|
56
|
+
"wrangler": "4.127.1"
|
|
57
|
+
},
|
|
58
|
+
"aiAgentSdk": {
|
|
59
|
+
"runtime": "universal",
|
|
60
|
+
"coreApi": 1,
|
|
61
|
+
"roles": [
|
|
62
|
+
"model-provider"
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsdown",
|
|
67
|
+
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true});require('node:fs').rmSync('artifacts',{recursive:true,force:true})\"",
|
|
68
|
+
"typecheck": "tsc --noEmit",
|
|
69
|
+
"test": "vitest run --config vitest.config.ts",
|
|
70
|
+
"pack": "pnpm pack --pack-destination artifacts",
|
|
71
|
+
"test:pack": "node ../../scripts/test-packed-provider.mts provider-anthropic",
|
|
72
|
+
"check:publint": "publint",
|
|
73
|
+
"check:types": "attw --profile esm-only --pack ."
|
|
74
|
+
}
|
|
75
|
+
}
|