@riotprompt/execution-anthropic 0.0.4 → 1.0.1-dev.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/README.md +2 -0
- package/dist/index.js +90 -50
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,34 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
+
import { getRedactor } from "@theunwalked/offrecord";
|
|
3
|
+
import { configureErrorSanitizer, configureSecretGuard, createSafeError } from "@theunwalked/spotclean";
|
|
4
|
+
const redactor = getRedactor();
|
|
5
|
+
redactor.register({
|
|
6
|
+
name: "anthropic",
|
|
7
|
+
patterns: [
|
|
8
|
+
/sk-ant-[a-zA-Z0-9_-]+/g,
|
|
9
|
+
/sk-ant-api\d+-[a-zA-Z0-9_-]+/g
|
|
10
|
+
],
|
|
11
|
+
validator: (key) => /^sk-ant(-api\d+)?-[a-zA-Z0-9_-]+$/.test(key),
|
|
12
|
+
envVar: "ANTHROPIC_API_KEY",
|
|
13
|
+
description: "Anthropic API keys"
|
|
14
|
+
});
|
|
15
|
+
configureErrorSanitizer({
|
|
16
|
+
enabled: true,
|
|
17
|
+
environment: process.env.NODE_ENV === "production" ? "production" : "development",
|
|
18
|
+
includeCorrelationId: true,
|
|
19
|
+
sanitizeStackTraces: process.env.NODE_ENV === "production",
|
|
20
|
+
maxMessageLength: 500
|
|
21
|
+
});
|
|
22
|
+
configureSecretGuard({
|
|
23
|
+
enabled: true,
|
|
24
|
+
redactionText: "[REDACTED]",
|
|
25
|
+
preservePartial: false,
|
|
26
|
+
preserveLength: 0,
|
|
27
|
+
customPatterns: [
|
|
28
|
+
{ name: "anthropic", pattern: /sk-ant-[a-zA-Z0-9_-]+/g, description: "Anthropic API key" },
|
|
29
|
+
{ name: "anthropic-api", pattern: /sk-ant-api\d+-[a-zA-Z0-9_-]+/g, description: "Anthropic API key" }
|
|
30
|
+
]
|
|
31
|
+
});
|
|
2
32
|
class AnthropicProvider {
|
|
3
33
|
name = "anthropic";
|
|
4
34
|
/**
|
|
@@ -13,61 +43,71 @@ class AnthropicProvider {
|
|
|
13
43
|
*/
|
|
14
44
|
async execute(request, options = {}) {
|
|
15
45
|
const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY;
|
|
16
|
-
if (!apiKey)
|
|
17
|
-
|
|
18
|
-
const model = options.model || request.model || "claude-3-opus-20240229";
|
|
19
|
-
let systemPrompt = "";
|
|
20
|
-
const messages = [];
|
|
21
|
-
for (const msg of request.messages) {
|
|
22
|
-
if (msg.role === "system" || msg.role === "developer") {
|
|
23
|
-
systemPrompt += (typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)) + "\n\n";
|
|
24
|
-
} else {
|
|
25
|
-
messages.push({
|
|
26
|
-
role: msg.role,
|
|
27
|
-
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
28
|
-
});
|
|
29
|
-
}
|
|
46
|
+
if (!apiKey) {
|
|
47
|
+
throw new Error("Anthropic API key is required. Set ANTHROPIC_API_KEY environment variable.");
|
|
30
48
|
}
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
const validation = redactor.validateKey(apiKey, "anthropic");
|
|
50
|
+
if (!validation.valid) {
|
|
51
|
+
throw new Error("Invalid Anthropic API key format");
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const client = new Anthropic({ apiKey });
|
|
55
|
+
const model = options.model || request.model || "claude-3-opus-20240229";
|
|
56
|
+
let systemPrompt = "";
|
|
57
|
+
const messages = [];
|
|
58
|
+
for (const msg of request.messages) {
|
|
59
|
+
if (msg.role === "system" || msg.role === "developer") {
|
|
60
|
+
systemPrompt += (typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)) + "\n\n";
|
|
61
|
+
} else {
|
|
62
|
+
messages.push({
|
|
63
|
+
role: msg.role,
|
|
64
|
+
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const response = await client.messages.create({
|
|
69
|
+
model,
|
|
70
|
+
system: systemPrompt.trim() || void 0,
|
|
71
|
+
messages,
|
|
72
|
+
max_tokens: options.maxTokens || 4096,
|
|
73
|
+
temperature: options.temperature,
|
|
74
|
+
...request.responseFormat?.type === "json_schema" ? {
|
|
75
|
+
tools: [
|
|
76
|
+
{
|
|
77
|
+
name: request.responseFormat.json_schema.name,
|
|
78
|
+
description: request.responseFormat.json_schema.description || "Output data in this structured format",
|
|
79
|
+
input_schema: request.responseFormat.json_schema.schema
|
|
80
|
+
}
|
|
81
|
+
],
|
|
82
|
+
tool_choice: {
|
|
83
|
+
type: "tool",
|
|
84
|
+
name: request.responseFormat.json_schema.name
|
|
43
85
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
86
|
+
} : {}
|
|
87
|
+
});
|
|
88
|
+
let text = "";
|
|
89
|
+
if (request.responseFormat?.type === "json_schema") {
|
|
90
|
+
const toolUseBlock = response.content.find(
|
|
91
|
+
(block) => block.type === "tool_use"
|
|
92
|
+
);
|
|
93
|
+
if (toolUseBlock && toolUseBlock.type === "tool_use") {
|
|
94
|
+
text = JSON.stringify(toolUseBlock.input, null, 2);
|
|
48
95
|
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (request.responseFormat?.type === "json_schema") {
|
|
53
|
-
const toolUseBlock = response.content.find(
|
|
54
|
-
(block) => block.type === "tool_use"
|
|
55
|
-
);
|
|
56
|
-
if (toolUseBlock && toolUseBlock.type === "tool_use") {
|
|
57
|
-
text = JSON.stringify(toolUseBlock.input, null, 2);
|
|
96
|
+
} else {
|
|
97
|
+
const contentBlock = response.content[0];
|
|
98
|
+
text = contentBlock.type === "text" ? contentBlock.text : "";
|
|
58
99
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
100
|
+
return {
|
|
101
|
+
content: text,
|
|
102
|
+
model: response.model,
|
|
103
|
+
usage: {
|
|
104
|
+
inputTokens: response.usage.input_tokens,
|
|
105
|
+
outputTokens: response.usage.output_tokens
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throw createSafeError(error, { provider: "anthropic" });
|
|
62
110
|
}
|
|
63
|
-
return {
|
|
64
|
-
content: text,
|
|
65
|
-
model: response.model,
|
|
66
|
-
usage: {
|
|
67
|
-
inputTokens: response.usage.input_tokens,
|
|
68
|
-
outputTokens: response.usage.output_tokens
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
111
|
}
|
|
72
112
|
}
|
|
73
113
|
function createAnthropicProvider() {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Execution Anthropic Package\n *\n * Anthropic provider implementation for LLM execution.\n *\n * @packageDocumentation\n */\n\nimport Anthropic from '@anthropic-ai/sdk';\n\n// ===== INLINE TYPES (from 'execution' package) =====\n\nexport type Model = string;\n\nexport interface Message {\n role: 'user' | 'assistant' | 'system' | 'developer' | 'tool';\n content: string | string[] | null;\n name?: string;\n}\n\nexport interface Request {\n messages: Message[];\n model: Model;\n responseFormat?: any;\n validator?: any;\n addMessage(message: Message): void;\n}\n\nexport interface ProviderResponse {\n content: string;\n model: string;\n usage?: {\n inputTokens: number;\n outputTokens: number;\n };\n toolCalls?: Array<{\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }>;\n}\n\nexport interface ExecutionOptions {\n apiKey?: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n timeout?: number;\n retries?: number;\n}\n\nexport interface Provider {\n readonly name: string;\n execute(request: Request, options?: ExecutionOptions): Promise<ProviderResponse>;\n supportsModel?(model: Model): boolean;\n}\n\n/**\n * Anthropic Provider implementation\n */\nexport class AnthropicProvider implements Provider {\n readonly name = 'anthropic';\n\n /**\n * Check if this provider supports a given model\n */\n supportsModel(model: Model): boolean {\n if (!model) return false;\n return model.startsWith('claude');\n }\n\n /**\n * Execute a request against Anthropic\n */\n async execute(\n request: Request,\n options: ExecutionOptions = {}\n ): Promise<ProviderResponse> {\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY;\n if (!apiKey) throw new Error('Anthropic API key is required');\n\n const client = new Anthropic({ apiKey });\n\n
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Execution Anthropic Package\n *\n * Anthropic provider implementation for LLM execution.\n *\n * @packageDocumentation\n */\n\nimport Anthropic from '@anthropic-ai/sdk';\nimport { getRedactor } from '@theunwalked/offrecord';\nimport { \n createSafeError, \n configureErrorSanitizer,\n configureSecretGuard,\n} from '@theunwalked/spotclean';\n\n// Register Anthropic API key patterns on module load\nconst redactor = getRedactor();\nredactor.register({\n name: 'anthropic',\n patterns: [\n /sk-ant-[a-zA-Z0-9_-]+/g,\n /sk-ant-api\\d+-[a-zA-Z0-9_-]+/g,\n ],\n validator: (key: string) => /^sk-ant(-api\\d+)?-[a-zA-Z0-9_-]+$/.test(key),\n envVar: 'ANTHROPIC_API_KEY',\n description: 'Anthropic API keys',\n});\n\n// Configure spotclean for error sanitization\nconfigureErrorSanitizer({\n enabled: true,\n environment: process.env.NODE_ENV === 'production' ? 'production' : 'development',\n includeCorrelationId: true,\n sanitizeStackTraces: process.env.NODE_ENV === 'production',\n maxMessageLength: 500,\n});\n\nconfigureSecretGuard({\n enabled: true,\n redactionText: '[REDACTED]',\n preservePartial: false,\n preserveLength: 0,\n customPatterns: [\n { name: 'anthropic', pattern: /sk-ant-[a-zA-Z0-9_-]+/g, description: 'Anthropic API key' },\n { name: 'anthropic-api', pattern: /sk-ant-api\\d+-[a-zA-Z0-9_-]+/g, description: 'Anthropic API key' },\n ],\n});\n\n// ===== INLINE TYPES (from 'execution' package) =====\n\nexport type Model = string;\n\nexport interface Message {\n role: 'user' | 'assistant' | 'system' | 'developer' | 'tool';\n content: string | string[] | null;\n name?: string;\n}\n\nexport interface Request {\n messages: Message[];\n model: Model;\n responseFormat?: any;\n validator?: any;\n addMessage(message: Message): void;\n}\n\nexport interface ProviderResponse {\n content: string;\n model: string;\n usage?: {\n inputTokens: number;\n outputTokens: number;\n };\n toolCalls?: Array<{\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }>;\n}\n\nexport interface ExecutionOptions {\n apiKey?: string;\n model?: string;\n temperature?: number;\n maxTokens?: number;\n timeout?: number;\n retries?: number;\n}\n\nexport interface Provider {\n readonly name: string;\n execute(request: Request, options?: ExecutionOptions): Promise<ProviderResponse>;\n supportsModel?(model: Model): boolean;\n}\n\n/**\n * Anthropic Provider implementation\n */\nexport class AnthropicProvider implements Provider {\n readonly name = 'anthropic';\n\n /**\n * Check if this provider supports a given model\n */\n supportsModel(model: Model): boolean {\n if (!model) return false;\n return model.startsWith('claude');\n }\n\n /**\n * Execute a request against Anthropic\n */\n async execute(\n request: Request,\n options: ExecutionOptions = {}\n ): Promise<ProviderResponse> {\n const apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY;\n \n if (!apiKey) {\n throw new Error('Anthropic API key is required. Set ANTHROPIC_API_KEY environment variable.');\n }\n\n // Validate key format\n const validation = redactor.validateKey(apiKey, 'anthropic');\n if (!validation.valid) {\n throw new Error('Invalid Anthropic API key format');\n }\n\n try {\n const client = new Anthropic({ apiKey });\n\n const model = options.model || request.model || 'claude-3-opus-20240229';\n\n // Anthropic separates system prompt from messages\n let systemPrompt = '';\n const messages: Anthropic.MessageParam[] = [];\n\n for (const msg of request.messages) {\n if (msg.role === 'system' || msg.role === 'developer') {\n systemPrompt +=\n (typeof msg.content === 'string'\n ? msg.content\n : JSON.stringify(msg.content)) + '\\n\\n';\n } else {\n messages.push({\n role: msg.role as 'user' | 'assistant',\n content:\n typeof msg.content === 'string'\n ? msg.content\n : JSON.stringify(msg.content),\n });\n }\n }\n\n const response = await client.messages.create({\n model: model,\n system: systemPrompt.trim() || undefined,\n messages: messages,\n max_tokens: options.maxTokens || 4096,\n temperature: options.temperature,\n ...(request.responseFormat?.type === 'json_schema'\n ? {\n tools: [\n {\n name: request.responseFormat.json_schema.name,\n description:\n request.responseFormat.json_schema.description ||\n 'Output data in this structured format',\n input_schema:\n request.responseFormat.json_schema.schema,\n },\n ],\n tool_choice: {\n type: 'tool' as const,\n name: request.responseFormat.json_schema.name,\n },\n }\n : {}),\n });\n\n // Handle ContentBlock\n let text = '';\n\n if (request.responseFormat?.type === 'json_schema') {\n const toolUseBlock = response.content.find(\n (block) => block.type === 'tool_use'\n );\n if (toolUseBlock && toolUseBlock.type === 'tool_use') {\n text = JSON.stringify(toolUseBlock.input, null, 2);\n }\n } else {\n const contentBlock = response.content[0];\n text = contentBlock.type === 'text' ? contentBlock.text : '';\n }\n\n return {\n content: text,\n model: response.model,\n usage: {\n inputTokens: response.usage.input_tokens,\n outputTokens: response.usage.output_tokens,\n },\n };\n } catch (error) {\n // Sanitize error to remove any API keys from error messages\n // Use spotclean for comprehensive error sanitization\n throw createSafeError(error as Error, { provider: 'anthropic' });\n }\n }\n}\n\n/**\n * Create a new Anthropic provider instance\n */\nexport function createAnthropicProvider(): AnthropicProvider {\n return new AnthropicProvider();\n}\n\n/**\n * Package version\n */\nexport const VERSION = '0.0.1';\n\nexport default AnthropicProvider;\n"],"names":[],"mappings":";;;AAiBA,MAAM,WAAW,YAAA;AACjB,SAAS,SAAS;AAAA,EACd,MAAM;AAAA,EACN,UAAU;AAAA,IACN;AAAA,IACA;AAAA,EAAA;AAAA,EAEJ,WAAW,CAAC,QAAgB,oCAAoC,KAAK,GAAG;AAAA,EACxE,QAAQ;AAAA,EACR,aAAa;AACjB,CAAC;AAGD,wBAAwB;AAAA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zD,SAAO,IAAI,kBAAA;AACf;AAKO,MAAM,UAAU;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@riotprompt/execution-anthropic",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.1-dev.0",
|
|
4
4
|
"description": "Anthropic Claude provider for execution interface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"test": "vitest run --coverage",
|
|
21
21
|
"test:coverage": "vitest run --coverage",
|
|
22
22
|
"lint": "eslint src",
|
|
23
|
-
"precommit": "npm run lint && npm run test",
|
|
23
|
+
"precommit": "npm run build && npm run lint && npm run test",
|
|
24
24
|
"prepublishOnly": "npm run clean && npm run build"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
@@ -37,13 +37,16 @@
|
|
|
37
37
|
"url": "https://github.com/kjerneverk/execution-anthropic"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@anthropic-ai/sdk": "^0.52.0"
|
|
40
|
+
"@anthropic-ai/sdk": "^0.52.0",
|
|
41
|
+
"@theunwalked/offrecord": "^0.0.1",
|
|
42
|
+
"@theunwalked/spotclean": "^0.0.3"
|
|
41
43
|
},
|
|
42
44
|
"devDependencies": {
|
|
43
45
|
"@types/node": "^25.0.6",
|
|
44
46
|
"@typescript-eslint/eslint-plugin": "^8.33.1",
|
|
45
47
|
"@typescript-eslint/parser": "^8.33.1",
|
|
46
48
|
"@vitest/coverage-v8": "^3.2.4",
|
|
49
|
+
"ajv": "^8.17.1",
|
|
47
50
|
"eslint": "^9.28.0",
|
|
48
51
|
"globals": "^16.2.0",
|
|
49
52
|
"typescript": "^5.8.3",
|