@glossic/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/dist/index.d.ts +23 -0
- package/dist/index.js +136 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rody Huancas
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import { Provider } from '@glossic/schema';
|
|
3
|
+
|
|
4
|
+
declare const anthropicProviderName = "anthropic";
|
|
5
|
+
declare const ANTHROPIC_DEFAULT_MODEL = "claude-opus-5";
|
|
6
|
+
/** Whether this model will take a temperature, rather than refusing the request. */
|
|
7
|
+
declare const acceptsTemperature: (model: string) => boolean;
|
|
8
|
+
interface AnthropicProviderOptions {
|
|
9
|
+
apiKey?: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
model?: string;
|
|
12
|
+
maxTokens?: number;
|
|
13
|
+
createClient?: (apiKey: string) => Anthropic;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Provider backed by the Anthropic API. Reports itself unavailable when no key
|
|
17
|
+
* is set, which is how auto-detection falls through to the next one.
|
|
18
|
+
*/
|
|
19
|
+
declare const createAnthropicProvider: (options?: AnthropicProviderOptions) => Provider;
|
|
20
|
+
/** The default-configured instance, for callers with nothing to override. */
|
|
21
|
+
declare const anthropicProvider: Provider;
|
|
22
|
+
|
|
23
|
+
export { ANTHROPIC_DEFAULT_MODEL, type AnthropicProviderOptions, acceptsTemperature, anthropicProvider, anthropicProviderName, createAnthropicProvider, anthropicProvider as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import { ProviderError, isProviderError } from '@glossic/schema';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
var anthropicProviderName = "anthropic";
|
|
6
|
+
var ANTHROPIC_DEFAULT_MODEL = "claude-opus-5";
|
|
7
|
+
var DEFAULT_MAX_TOKENS = 16e3;
|
|
8
|
+
var SAMPLING_REJECTED_BY = [
|
|
9
|
+
"claude-fable-",
|
|
10
|
+
"claude-mythos-",
|
|
11
|
+
"claude-opus-5",
|
|
12
|
+
"claude-opus-4-7",
|
|
13
|
+
"claude-opus-4-8",
|
|
14
|
+
"claude-sonnet-5"
|
|
15
|
+
];
|
|
16
|
+
var acceptsTemperature = (model) => !SAMPLING_REJECTED_BY.some((prefix) => model.startsWith(prefix));
|
|
17
|
+
var readApiKey = (options) => {
|
|
18
|
+
const key = options.apiKey ?? process.env.ANTHROPIC_API_KEY;
|
|
19
|
+
return key === void 0 || key.trim() === "" ? void 0 : key;
|
|
20
|
+
};
|
|
21
|
+
var toProviderError = (cause) => {
|
|
22
|
+
if (isProviderError(cause)) return cause;
|
|
23
|
+
if (cause instanceof Anthropic.AuthenticationError) {
|
|
24
|
+
return new ProviderError({
|
|
25
|
+
provider: anthropicProviderName,
|
|
26
|
+
code: "unauthenticated",
|
|
27
|
+
message: "ANTHROPIC_API_KEY was rejected",
|
|
28
|
+
detail: cause.message,
|
|
29
|
+
cause
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (cause instanceof Anthropic.RateLimitError) {
|
|
33
|
+
return new ProviderError({
|
|
34
|
+
provider: anthropicProviderName,
|
|
35
|
+
code: "rate-limit",
|
|
36
|
+
message: "the Anthropic API rate limit was hit",
|
|
37
|
+
detail: cause.message,
|
|
38
|
+
cause
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (cause instanceof Anthropic.APIConnectionError) {
|
|
42
|
+
return new ProviderError({
|
|
43
|
+
provider: anthropicProviderName,
|
|
44
|
+
code: "timeout",
|
|
45
|
+
message: "could not reach the Anthropic API",
|
|
46
|
+
detail: cause.message,
|
|
47
|
+
cause
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (cause instanceof Anthropic.APIError) {
|
|
51
|
+
const status = cause.status ?? 0;
|
|
52
|
+
return new ProviderError({
|
|
53
|
+
provider: anthropicProviderName,
|
|
54
|
+
code: status >= 500 ? "server" : "api",
|
|
55
|
+
message: `the Anthropic API returned ${cause.status ?? "an error"}`,
|
|
56
|
+
detail: cause.message,
|
|
57
|
+
cause
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return new ProviderError({
|
|
61
|
+
provider: anthropicProviderName,
|
|
62
|
+
code: "api",
|
|
63
|
+
message: "the Anthropic API call failed",
|
|
64
|
+
detail: cause instanceof Error ? cause.message : void 0,
|
|
65
|
+
cause
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
var extractText = (message) => {
|
|
69
|
+
return message.content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
70
|
+
};
|
|
71
|
+
var createAnthropicProvider = (options = {}) => {
|
|
72
|
+
const model = options.model ?? ANTHROPIC_DEFAULT_MODEL;
|
|
73
|
+
const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
74
|
+
const createClient = options.createClient ?? ((apiKey) => new Anthropic({
|
|
75
|
+
apiKey,
|
|
76
|
+
...options.baseUrl === void 0 ? {} : { baseURL: options.baseUrl }
|
|
77
|
+
}));
|
|
78
|
+
let client;
|
|
79
|
+
const requireClient = () => {
|
|
80
|
+
const apiKey = readApiKey(options);
|
|
81
|
+
if (apiKey === void 0) {
|
|
82
|
+
throw new ProviderError({
|
|
83
|
+
provider: anthropicProviderName,
|
|
84
|
+
code: "unauthenticated",
|
|
85
|
+
message: "ANTHROPIC_API_KEY is not set",
|
|
86
|
+
detail: "Create a key at https://console.anthropic.com/settings/keys and export it."
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
client ??= createClient(apiKey);
|
|
90
|
+
return client;
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
name: anthropicProviderName,
|
|
94
|
+
available: async () => readApiKey(options) !== void 0,
|
|
95
|
+
complete: async (request) => {
|
|
96
|
+
const requestModel = request.model ?? model;
|
|
97
|
+
const temperature = request.temperature ?? 0;
|
|
98
|
+
let message;
|
|
99
|
+
try {
|
|
100
|
+
message = await requireClient().messages.create({
|
|
101
|
+
model: requestModel,
|
|
102
|
+
max_tokens: request.maxTokens ?? maxTokens,
|
|
103
|
+
...request.system === void 0 ? {} : { system: request.system },
|
|
104
|
+
...acceptsTemperature(requestModel) ? { temperature } : {},
|
|
105
|
+
messages: [{ role: "user", content: request.prompt }]
|
|
106
|
+
});
|
|
107
|
+
} catch (cause) {
|
|
108
|
+
throw toProviderError(cause);
|
|
109
|
+
}
|
|
110
|
+
if (message.stop_reason === "refusal") {
|
|
111
|
+
throw new ProviderError({
|
|
112
|
+
provider: anthropicProviderName,
|
|
113
|
+
code: "refused",
|
|
114
|
+
message: "the model declined to answer",
|
|
115
|
+
detail: `stop_reason=refusal on ${message.model}`
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
text: extractText(message),
|
|
120
|
+
model: message.model,
|
|
121
|
+
usage: {
|
|
122
|
+
inputTokens: message.usage.input_tokens,
|
|
123
|
+
outputTokens: message.usage.output_tokens,
|
|
124
|
+
...message.usage.cache_read_input_tokens === null || message.usage.cache_read_input_tokens === void 0 ? {} : { cacheReadTokens: message.usage.cache_read_input_tokens }
|
|
125
|
+
},
|
|
126
|
+
raw: message
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
var anthropicProvider = createAnthropicProvider();
|
|
132
|
+
var index_default = anthropicProvider;
|
|
133
|
+
|
|
134
|
+
export { ANTHROPIC_DEFAULT_MODEL, acceptsTemperature, anthropicProvider, anthropicProviderName, createAnthropicProvider, index_default as default };
|
|
135
|
+
//# sourceMappingURL=index.js.map
|
|
136
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;AAIO,IAAM,qBAAA,GAA0B;AAChC,IAAM,uBAAA,GAA0B;AACvC,IAAO,kBAAA,GAAgC,IAAA;AAGvC,IAAM,oBAAA,GAAuB;AAAA,EAC3B,eAAA;AAAA,EACA,gBAAA;AAAA,EACA,eAAA;AAAA,EACA,iBAAA;AAAA,EACA,iBAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,kBAAA,GAAqB,CAAC,KAAA,KACjC,CAAC,oBAAA,CAAqB,IAAA,CAAK,CAAC,MAAA,KAAW,KAAA,CAAM,UAA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AA,EAAW;AACrC,QAAA,MAAM,IAAI,aAAA,CAAc;AAAA,UACtB,QAAA,EAAU,qBAAA;AAAA,UACV,IAAA,EAAU,SAAA;AAAA,UACV,OAAA,EAAU,8BAAA;AAAA,UACV,MAAA,EAAU,CAAA,uBAAA,EAA0B,OAAA,CAAQ,KAAK,CAAA;AAAA,SAClD,CAAA;AAAA,MACH;AAEA,MAAA,OAAO;AAAA,QACL,IAAA,EAAO,YAAY,OAAO,CAAA;AAAA,QAC1B,OAAO,OAAA,CAAQ,KAAA;AAAA,QACf,KAAA,EAAO;AAAA,UACL,WAAA,EAAc,QAAQ,KAAA,CAAM,YAAA;AAAA,UAC5B,YAAA,EAAc,QAAQ,KAAA,CAAM,aAAA;AAAA,UAC5B,GAAI,OAAA,CAAQ,KAAA,CAAM,uBAAA,KAA4B,QAC9C,OAAA,CAAQ,KAAA,CAAM,uBAAA,KAA4B,MAAA,GACtC,EAAC,GACD,EAAE,eAAA,EAAiB,OAAA,CAAQ,MAAM,uBAAA;AAAwB,SAC/D;AAAA,QACA,GAAA,EAAK;AAAA,OACP;AAAA,IACF;AAAA,GACF;AACF;AAGO,IAAM,oBAA8B,uBAAA;AAE3C,IAAO,aAAA,GAAQ","file":"index.js","sourcesContent":["import Anthropic from \"@anthropic-ai/sdk\";\nimport type { CompletionRequest, CompletionResult, Provider } from \"@glossic/schema\";\nimport { isProviderError, ProviderError } from \"@glossic/schema\";\n\nexport const anthropicProviderName = \"anthropic\";\nexport const ANTHROPIC_DEFAULT_MODEL = \"claude-opus-5\";\nconst DEFAULT_MAX_TOKENS = 16_000;\n\n/** Model prefixes that reject a sampling parameter outright, so temperature is dropped. */\nconst SAMPLING_REJECTED_BY = [\n \"claude-fable-\",\n \"claude-mythos-\",\n \"claude-opus-5\",\n \"claude-opus-4-7\",\n \"claude-opus-4-8\",\n \"claude-sonnet-5\",\n];\n\n/** Whether this model will take a temperature, rather than refusing the request. */\nexport const acceptsTemperature = (model: string): boolean =>\n !SAMPLING_REJECTED_BY.some((prefix) => model.startsWith(prefix));\n\nexport interface AnthropicProviderOptions {\n apiKey ?: string;\n baseUrl ?: string;\n model ?: string;\n maxTokens ?: number;\n createClient?: (apiKey: string) => Anthropic;\n}\n\n/** The API key from the options or the environment, empty treated as absent. */\nconst readApiKey = (options: AnthropicProviderOptions): string | undefined => {\n const key = options.apiKey ?? process.env.ANTHROPIC_API_KEY;\n\n return key === undefined || key.trim() === \"\" ? undefined : key;\n};\n\n/** Maps an SDK failure onto the shared error vocabulary, so retry knows what to do. */\nconst toProviderError = (cause: unknown): ProviderError => {\n if (isProviderError(cause)) return cause;\n\n if (cause instanceof Anthropic.AuthenticationError) {\n return new ProviderError({\n provider: anthropicProviderName,\n code : \"unauthenticated\",\n message : \"ANTHROPIC_API_KEY was rejected\",\n detail : cause.message,\n cause,\n });\n }\n\n if (cause instanceof Anthropic.RateLimitError) {\n return new ProviderError({\n provider: anthropicProviderName,\n code : \"rate-limit\",\n message : \"the Anthropic API rate limit was hit\",\n detail : cause.message,\n cause,\n });\n }\n\n if (cause instanceof Anthropic.APIConnectionError) {\n return new ProviderError({\n provider: anthropicProviderName,\n code : \"timeout\",\n message : \"could not reach the Anthropic API\",\n detail : cause.message,\n cause,\n });\n }\n\n if (cause instanceof Anthropic.APIError) {\n const status = cause.status ?? 0;\n return new ProviderError({\n provider: anthropicProviderName,\n code : status >= 500 ? \"server\" : \"api\",\n message : `the Anthropic API returned ${cause.status ?? \"an error\"}`,\n detail : cause.message,\n cause,\n });\n }\n\n return new ProviderError({\n provider: anthropicProviderName,\n code : \"api\",\n message : \"the Anthropic API call failed\",\n detail : cause instanceof Error ? cause.message: undefined,\n cause,\n });\n};\n\n/** The text blocks of a response, joined; other block types are dropped. */\nconst extractText = (message: Anthropic.Message): string => {\n return message.content\n .filter((block): block is Anthropic.TextBlock => block.type === \"text\")\n .map((block) => block.text)\n .join(\"\");\n}\n\n\n/**\n * Provider backed by the Anthropic API. Reports itself unavailable when no key\n * is set, which is how auto-detection falls through to the next one.\n */\nexport const createAnthropicProvider = (options: AnthropicProviderOptions = {}): Provider => {\n const model = options.model ?? ANTHROPIC_DEFAULT_MODEL;\n const maxTokens = options.maxTokens ?? DEFAULT_MAX_TOKENS;\n \n const createClient = options.createClient ?? ((apiKey: string) => new Anthropic({\n apiKey,\n ...(options.baseUrl === undefined ? {} : { baseURL: options.baseUrl }),\n }));\n\n let client: Anthropic | undefined;\n\n const requireClient = (): Anthropic => {\n const apiKey = readApiKey(options);\n\n if (apiKey === undefined) {\n throw new ProviderError({\n provider: anthropicProviderName,\n code : \"unauthenticated\",\n message : \"ANTHROPIC_API_KEY is not set\",\n detail : \"Create a key at https://console.anthropic.com/settings/keys and export it.\",\n });\n }\n\n client ??= createClient(apiKey);\n return client;\n };\n\n return {\n name: anthropicProviderName,\n\n available: async (): Promise<boolean> => readApiKey(options) !== undefined,\n\n complete: async (request: CompletionRequest): Promise<CompletionResult> => {\n const requestModel = request.model ?? model;\n const temperature = request.temperature ?? 0;\n\n let message: Anthropic.Message;\n try {\n message = await requireClient().messages.create({\n model : requestModel,\n max_tokens: request.maxTokens ?? maxTokens,\n ...(request.system === undefined ? {} : { system: request.system }),\n ...(acceptsTemperature(requestModel) ? { temperature } : {}),\n messages: [{ role: \"user\", content: request.prompt }],\n });\n } catch (cause) {\n throw toProviderError(cause);\n }\n\n if (message.stop_reason === \"refusal\") {\n throw new ProviderError({\n provider: anthropicProviderName,\n code : \"refused\",\n message : \"the model declined to answer\",\n detail : `stop_reason=refusal on ${message.model}`,\n });\n }\n\n return {\n text : extractText(message),\n model: message.model,\n usage: {\n inputTokens : message.usage.input_tokens,\n outputTokens: message.usage.output_tokens,\n ...(message.usage.cache_read_input_tokens === null ||\n message.usage.cache_read_input_tokens === undefined\n ? {}\n : { cacheReadTokens: message.usage.cache_read_input_tokens }),\n },\n raw: message,\n };\n },\n };\n};\n\n/** The default-configured instance, for callers with nothing to override. */\nexport const anthropicProvider: Provider = createAnthropicProvider();\n\nexport default anthropicProvider;\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@glossic/provider-anthropic",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Completion provider backed by the Anthropic API",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"documentation",
|
|
7
|
+
"docs",
|
|
8
|
+
"docs-as-code",
|
|
9
|
+
"glossic",
|
|
10
|
+
"provider",
|
|
11
|
+
"anthropic",
|
|
12
|
+
"claude",
|
|
13
|
+
"llm"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Rody Huancas",
|
|
17
|
+
"homepage": "https://github.com/rody-huancas/glosik#readme",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/rody-huancas/glosik.git",
|
|
21
|
+
"directory": "packages/providers/anthropic"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/rody-huancas/glosik/issues"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"main": "./dist/index.js",
|
|
37
|
+
"module": "./dist/index.js",
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"exports": {
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"import": "./dist/index.js",
|
|
43
|
+
"default": "./dist/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./package.json": "./package.json"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@anthropic-ai/sdk": "^0.71.0",
|
|
49
|
+
"@glossic/schema": "0.1.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"dev": "tsup --watch",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"typecheck": "tsc --noEmit"
|
|
56
|
+
}
|
|
57
|
+
}
|