@robota-sdk/agent-provider 3.0.0-beta.64
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/browser/index.d.ts +1104 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +7 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/loggers/index.cjs +1 -0
- package/dist/loggers/index.d.ts +151 -0
- package/dist/loggers/index.d.ts.map +1 -0
- package/dist/loggers/index.js +2 -0
- package/dist/loggers/index.js.map +1 -0
- package/dist/node/anthropic/index.cjs +1 -0
- package/dist/node/anthropic/index.d.ts +158 -0
- package/dist/node/anthropic/index.d.ts.map +1 -0
- package/dist/node/anthropic/index.js +1 -0
- package/dist/node/anthropic--1vgLC-e.js +5 -0
- package/dist/node/anthropic--1vgLC-e.js.map +1 -0
- package/dist/node/anthropic-BFQ6DSCP.cjs +4 -0
- package/dist/node/bytedance/index.cjs +1 -0
- package/dist/node/bytedance/index.d.ts +74 -0
- package/dist/node/bytedance/index.d.ts.map +1 -0
- package/dist/node/bytedance/index.js +1 -0
- package/dist/node/bytedance-C_0sF_pJ.js +2 -0
- package/dist/node/bytedance-C_0sF_pJ.js.map +1 -0
- package/dist/node/bytedance-DVPxqEiC.cjs +1 -0
- package/dist/node/chunk-Bmb41Sf3.cjs +1 -0
- package/dist/node/deepseek/index.cjs +1 -0
- package/dist/node/deepseek/index.d.ts +2 -0
- package/dist/node/deepseek/index.js +1 -0
- package/dist/node/deepseek-_8Ixx7rA.js +2 -0
- package/dist/node/deepseek-_8Ixx7rA.js.map +1 -0
- package/dist/node/deepseek-oA2Y6bD0.cjs +1 -0
- package/dist/node/gemini/index.cjs +1 -0
- package/dist/node/gemini/index.d.ts +173 -0
- package/dist/node/gemini/index.d.ts.map +1 -0
- package/dist/node/gemini/index.js +1 -0
- package/dist/node/gemini-Bh2U87MY.js +4 -0
- package/dist/node/gemini-Bh2U87MY.js.map +1 -0
- package/dist/node/gemini-DSaNCxZj.cjs +3 -0
- package/dist/node/gemma/index.cjs +1 -0
- package/dist/node/gemma/index.d.ts +2 -0
- package/dist/node/gemma/index.js +1 -0
- package/dist/node/gemma-Dp_AfCUR.js +2 -0
- package/dist/node/gemma-Dp_AfCUR.js.map +1 -0
- package/dist/node/gemma-G-Pf_PnX.cjs +1 -0
- package/dist/node/google/index.cjs +1 -0
- package/dist/node/google/index.d.ts +14 -0
- package/dist/node/google/index.d.ts.map +1 -0
- package/dist/node/google/index.js +2 -0
- package/dist/node/google/index.js.map +1 -0
- package/dist/node/index-B6PnlDMd.d.ts +82 -0
- package/dist/node/index-B6PnlDMd.d.ts.map +1 -0
- package/dist/node/index-B7UvPJcI.d.ts +315 -0
- package/dist/node/index-B7UvPJcI.d.ts.map +1 -0
- package/dist/node/index-BLPOTNb5.d.ts +98 -0
- package/dist/node/index-BLPOTNb5.d.ts.map +1 -0
- package/dist/node/index-BqixM_XD.d.ts +231 -0
- package/dist/node/index-BqixM_XD.d.ts.map +1 -0
- package/dist/node/index-C3beaqKO.d.ts +231 -0
- package/dist/node/index-C3beaqKO.d.ts.map +1 -0
- package/dist/node/index-Cp2XRh9G.d.ts +82 -0
- package/dist/node/index-Cp2XRh9G.d.ts.map +1 -0
- package/dist/node/index-DSv5xruI.d.ts +98 -0
- package/dist/node/index-DSv5xruI.d.ts.map +1 -0
- package/dist/node/index-w0bV1uaP.d.ts +315 -0
- package/dist/node/index-w0bV1uaP.d.ts.map +1 -0
- package/dist/node/index.cjs +1 -0
- package/dist/node/index.d.ts +8 -0
- package/dist/node/index.js +1 -0
- package/dist/node/openai/index.cjs +1 -0
- package/dist/node/openai/index.d.ts +2 -0
- package/dist/node/openai/index.js +1 -0
- package/dist/node/openai-CRQjg4xF.js +2 -0
- package/dist/node/openai-CRQjg4xF.js.map +1 -0
- package/dist/node/openai-compatible-BYfyY5lb.cjs +1 -0
- package/dist/node/openai-compatible-Dm4Sof9e.js +2 -0
- package/dist/node/openai-compatible-Dm4Sof9e.js.map +1 -0
- package/dist/node/openai-xWC6pY7r.cjs +1 -0
- package/dist/node/qwen/index.cjs +1 -0
- package/dist/node/qwen/index.d.ts +2 -0
- package/dist/node/qwen/index.js +1 -0
- package/dist/node/qwen-ChUZobTL.js +2 -0
- package/dist/node/qwen-ChUZobTL.js.map +1 -0
- package/dist/node/qwen-CjT71vSM.cjs +1 -0
- package/package.json +157 -0
- package/src/anthropic/__tests__/abort-streaming.test.ts +199 -0
- package/src/anthropic/__tests__/model-catalog-refresh.test.ts +92 -0
- package/src/anthropic/__tests__/provider-definition.test.ts +55 -0
- package/src/anthropic/__tests__/provider.test.ts +1357 -0
- package/src/anthropic/__tests__/response-parser.test.ts +326 -0
- package/src/anthropic/index.ts +22 -0
- package/src/anthropic/message-converter.ts +181 -0
- package/src/anthropic/model-catalog-refresh.ts +128 -0
- package/src/anthropic/parsers/response-parser.ts +184 -0
- package/src/anthropic/provider-definition.ts +93 -0
- package/src/anthropic/provider.ts +290 -0
- package/src/anthropic/streaming-handler.ts +204 -0
- package/src/anthropic/types/api-types.ts +158 -0
- package/src/anthropic/types.ts +79 -0
- package/src/bytedance/http-client.test.ts +288 -0
- package/src/bytedance/http-client.ts +163 -0
- package/src/bytedance/index.ts +2 -0
- package/src/bytedance/provider.spec.ts +320 -0
- package/src/bytedance/provider.ts +171 -0
- package/src/bytedance/status-mapper.test.ts +299 -0
- package/src/bytedance/status-mapper.ts +141 -0
- package/src/bytedance/types.ts +68 -0
- package/src/deepseek/defaults.ts +4 -0
- package/src/deepseek/index.ts +22 -0
- package/src/deepseek/model-catalog-refresh.test.ts +57 -0
- package/src/deepseek/model-catalog-refresh.ts +105 -0
- package/src/deepseek/model-catalog.ts +55 -0
- package/src/deepseek/provider-definition.test.ts +109 -0
- package/src/deepseek/provider-definition.ts +132 -0
- package/src/deepseek/provider.test.ts +324 -0
- package/src/deepseek/provider.ts +298 -0
- package/src/deepseek/types.ts +37 -0
- package/src/gemini/execution-helpers.ts +233 -0
- package/src/gemini/genai-transport.test.ts +208 -0
- package/src/gemini/image-operations.test.ts +448 -0
- package/src/gemini/image-operations.ts +261 -0
- package/src/gemini/index.ts +11 -0
- package/src/gemini/message-converter.test.ts +616 -0
- package/src/gemini/message-converter.ts +140 -0
- package/src/gemini/model-catalog-refresh.test.ts +107 -0
- package/src/gemini/model-catalog-refresh.ts +92 -0
- package/src/gemini/provider-definition.test.ts +70 -0
- package/src/gemini/provider-definition.ts +78 -0
- package/src/gemini/provider-extended.test.ts +898 -0
- package/src/gemini/provider.spec.ts +216 -0
- package/src/gemini/provider.ts +279 -0
- package/src/gemini/request-converter.ts +226 -0
- package/src/gemini/tool-schema-converter.ts +78 -0
- package/src/gemini/types/api-types.ts +235 -0
- package/src/gemini/types.ts +121 -0
- package/src/gemma/index.ts +5 -0
- package/src/gemma/message-factory.ts +38 -0
- package/src/gemma/provider-definition.test.ts +43 -0
- package/src/gemma/provider-definition.ts +84 -0
- package/src/gemma/provider-projection.ts +49 -0
- package/src/gemma/provider.test.ts +628 -0
- package/src/gemma/provider.ts +308 -0
- package/src/gemma/pseudo-command-envelope.ts +58 -0
- package/src/gemma/pseudo-tool-call-projector.ts +243 -0
- package/src/gemma/pseudo-tool-call-tag-parser.ts +153 -0
- package/src/gemma/pseudo-tool-call-types.ts +31 -0
- package/src/gemma/reasoning-projector.test.ts +52 -0
- package/src/gemma/reasoning-projector.ts +144 -0
- package/src/gemma/streaming-projection.ts +79 -0
- package/src/gemma/tool-call-argument-parser.ts +126 -0
- package/src/gemma/tool-call-projector.test.ts +227 -0
- package/src/gemma/tool-call-projector.ts +264 -0
- package/src/gemma/types.ts +27 -0
- package/src/google/index.ts +11 -0
- package/src/google/provider-compat.test.ts +19 -0
- package/src/google/provider-definition.ts +6 -0
- package/src/google/provider.ts +10 -0
- package/src/google/types.ts +5 -0
- package/src/index.ts +9 -0
- package/src/openai/adapter.test.ts +494 -0
- package/src/openai/adapter.ts +145 -0
- package/src/openai/chat-completions-chat.ts +189 -0
- package/src/openai/executor-integration.test.ts +206 -0
- package/src/openai/index.ts +21 -0
- package/src/openai/interfaces/payload-logger.ts +48 -0
- package/src/openai/loggers/console-payload-logger.test.ts +173 -0
- package/src/openai/loggers/console-payload-logger.ts +94 -0
- package/src/openai/loggers/console.ts +9 -0
- package/src/openai/loggers/file-payload-logger.test.ts +238 -0
- package/src/openai/loggers/file-payload-logger.ts +112 -0
- package/src/openai/loggers/file.ts +9 -0
- package/src/openai/loggers/index.ts +12 -0
- package/src/openai/loggers/sanitize-openai-log-data.test.ts +89 -0
- package/src/openai/loggers/sanitize-openai-log-data.ts +14 -0
- package/src/openai/message-converter.ts +22 -0
- package/src/openai/model-catalog-refresh.test.ts +92 -0
- package/src/openai/model-catalog-refresh.ts +115 -0
- package/src/openai/openai-request-format.ts +92 -0
- package/src/openai/parsers/response-parser.test.ts +407 -0
- package/src/openai/parsers/response-parser.ts +47 -0
- package/src/openai/provider-definition.test.ts +75 -0
- package/src/openai/provider-definition.ts +132 -0
- package/src/openai/provider.test.ts +1402 -0
- package/src/openai/provider.ts +237 -0
- package/src/openai/responses-chat.ts +258 -0
- package/src/openai/responses-converter.ts +112 -0
- package/src/openai/responses-parser.ts +285 -0
- package/src/openai/responses-stream-utils.ts +45 -0
- package/src/openai/responses-types.ts +195 -0
- package/src/openai/streaming/stream-assembler.ts +3 -0
- package/src/openai/streaming/stream-handler.test.ts +367 -0
- package/src/openai/streaming/stream-handler.ts +119 -0
- package/src/openai/types/api-types.ts +112 -0
- package/src/openai/types.ts +194 -0
- package/src/qwen/defaults.ts +26 -0
- package/src/qwen/index.ts +5 -0
- package/src/qwen/model-catalog-refresh.test.ts +91 -0
- package/src/qwen/model-catalog-refresh.ts +97 -0
- package/src/qwen/provider-capabilities.ts +34 -0
- package/src/qwen/provider-definition.test.ts +139 -0
- package/src/qwen/provider-definition.ts +173 -0
- package/src/qwen/provider-streaming-assembly.ts +40 -0
- package/src/qwen/provider.test.ts +640 -0
- package/src/qwen/provider.ts +293 -0
- package/src/qwen/responses-chat.ts +194 -0
- package/src/qwen/responses-converter.ts +104 -0
- package/src/qwen/responses-parser.ts +299 -0
- package/src/qwen/responses-stream-utils.ts +38 -0
- package/src/qwen/types.ts +228 -0
- package/src/shared/openai-compatible/endpoint-probe.test.ts +52 -0
- package/src/shared/openai-compatible/endpoint-probe.ts +43 -0
- package/src/shared/openai-compatible/index.ts +6 -0
- package/src/shared/openai-compatible/message-converter.test.ts +111 -0
- package/src/shared/openai-compatible/message-converter.ts +84 -0
- package/src/shared/openai-compatible/native-payload-observer.test.ts +43 -0
- package/src/shared/openai-compatible/native-payload-observer.ts +26 -0
- package/src/shared/openai-compatible/response-parser.test.ts +172 -0
- package/src/shared/openai-compatible/response-parser.ts +180 -0
- package/src/shared/openai-compatible/stream-assembler.test.ts +266 -0
- package/src/shared/openai-compatible/stream-assembler.ts +248 -0
- package/src/shared/openai-compatible/types.ts +59 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
import type {
|
|
4
|
+
TProviderNativeRawPayloadCallback,
|
|
5
|
+
TUniversalMessage,
|
|
6
|
+
TTextDeltaCallback,
|
|
7
|
+
} from '@robota-sdk/agent-core';
|
|
8
|
+
import { formatWebSearchResults } from './message-converter';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Stream the Anthropic API response and assemble a complete TUniversalMessage.
|
|
12
|
+
*
|
|
13
|
+
* Calls onTextDelta for each text chunk as it arrives.
|
|
14
|
+
* Returns the fully assembled TUniversalMessage when the stream is done.
|
|
15
|
+
*/
|
|
16
|
+
export async function streamAndAssemble(
|
|
17
|
+
client: Anthropic,
|
|
18
|
+
params: Anthropic.MessageCreateParamsNonStreaming,
|
|
19
|
+
onTextDelta: TTextDeltaCallback,
|
|
20
|
+
onServerToolUse: ((toolName: string, input: Record<string, string>) => void) | undefined,
|
|
21
|
+
signal: AbortSignal | undefined,
|
|
22
|
+
onProviderNativeRawPayload?: TProviderNativeRawPayloadCallback,
|
|
23
|
+
): Promise<TUniversalMessage> {
|
|
24
|
+
const streamParams: Anthropic.MessageCreateParamsStreaming = {
|
|
25
|
+
...params,
|
|
26
|
+
stream: true,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
onProviderNativeRawPayload?.({
|
|
30
|
+
provider: 'anthropic',
|
|
31
|
+
apiSurface: 'anthropic-messages',
|
|
32
|
+
payloadKind: 'request',
|
|
33
|
+
payload: streamParams,
|
|
34
|
+
});
|
|
35
|
+
const stream = await client.messages.create(streamParams, signal ? { signal } : undefined);
|
|
36
|
+
|
|
37
|
+
// Accumulate the full response from stream events
|
|
38
|
+
const textParts: string[] = [];
|
|
39
|
+
const toolCalls: Array<{
|
|
40
|
+
id: string;
|
|
41
|
+
type: 'function';
|
|
42
|
+
function: { name: string; arguments: string };
|
|
43
|
+
}> = [];
|
|
44
|
+
let currentToolId = '';
|
|
45
|
+
let currentToolName = '';
|
|
46
|
+
let currentToolJson = '';
|
|
47
|
+
let usage = { input_tokens: 0, output_tokens: 0 };
|
|
48
|
+
let model = '';
|
|
49
|
+
let stopReason: string | null = null;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
let sequence = 0;
|
|
53
|
+
for await (const event of streamWithAbort(stream, signal)) {
|
|
54
|
+
onProviderNativeRawPayload?.({
|
|
55
|
+
provider: 'anthropic',
|
|
56
|
+
apiSurface: 'anthropic-messages',
|
|
57
|
+
payloadKind: 'stream_event',
|
|
58
|
+
sequence,
|
|
59
|
+
payload: event,
|
|
60
|
+
});
|
|
61
|
+
sequence++;
|
|
62
|
+
switch (event.type) {
|
|
63
|
+
case 'message_start':
|
|
64
|
+
usage = event.message.usage;
|
|
65
|
+
model = event.message.model;
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case 'content_block_start':
|
|
69
|
+
if (event.content_block.type === 'tool_use') {
|
|
70
|
+
currentToolId = event.content_block.id;
|
|
71
|
+
currentToolName = event.content_block.name;
|
|
72
|
+
currentToolJson = '';
|
|
73
|
+
} else if (event.content_block.type === 'server_tool_use') {
|
|
74
|
+
const serverBlock = event.content_block as {
|
|
75
|
+
name?: string;
|
|
76
|
+
input?: { query?: string };
|
|
77
|
+
};
|
|
78
|
+
const query = serverBlock.input?.query ?? '';
|
|
79
|
+
const toolLabel = query
|
|
80
|
+
? `\n🔍 Searching: "${query}"\n`
|
|
81
|
+
: `\n🔍 [${serverBlock.name ?? 'server_tool'}]\n`;
|
|
82
|
+
textParts.push(toolLabel);
|
|
83
|
+
onTextDelta(toolLabel);
|
|
84
|
+
if (onServerToolUse) {
|
|
85
|
+
onServerToolUse(serverBlock.name ?? 'server_tool', { query });
|
|
86
|
+
}
|
|
87
|
+
} else if (event.content_block.type === 'web_search_tool_result') {
|
|
88
|
+
const resultBlock = event.content_block as Anthropic.Messages.WebSearchToolResultBlock;
|
|
89
|
+
const formatted = formatWebSearchResults(resultBlock);
|
|
90
|
+
if (formatted) {
|
|
91
|
+
textParts.push(`\n${formatted}\n\n`);
|
|
92
|
+
onTextDelta(`\n${formatted}\n\n`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
|
|
97
|
+
case 'content_block_delta':
|
|
98
|
+
if (event.delta.type === 'text_delta') {
|
|
99
|
+
textParts.push(event.delta.text);
|
|
100
|
+
onTextDelta(event.delta.text);
|
|
101
|
+
} else if (event.delta.type === 'input_json_delta') {
|
|
102
|
+
currentToolJson += event.delta.partial_json;
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
|
|
106
|
+
case 'content_block_stop':
|
|
107
|
+
if (currentToolId) {
|
|
108
|
+
toolCalls.push({
|
|
109
|
+
id: currentToolId,
|
|
110
|
+
type: 'function' as const,
|
|
111
|
+
function: {
|
|
112
|
+
name: currentToolName,
|
|
113
|
+
arguments: currentToolJson || '{}',
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
currentToolId = '';
|
|
117
|
+
currentToolName = '';
|
|
118
|
+
currentToolJson = '';
|
|
119
|
+
}
|
|
120
|
+
break;
|
|
121
|
+
|
|
122
|
+
case 'message_delta':
|
|
123
|
+
if (event.usage) {
|
|
124
|
+
usage.output_tokens = event.usage.output_tokens;
|
|
125
|
+
}
|
|
126
|
+
stopReason = event.delta.stop_reason;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} catch (err) {
|
|
131
|
+
if (err instanceof Error && err.name === 'AbortError') {
|
|
132
|
+
return buildPartialResult(textParts, toolCalls, usage, model);
|
|
133
|
+
}
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// If aborted via break (not via catch), return partial response
|
|
138
|
+
if (signal?.aborted) {
|
|
139
|
+
return buildPartialResult(textParts, toolCalls, usage, model);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const textContent = textParts.join('') || '';
|
|
143
|
+
|
|
144
|
+
const result: TUniversalMessage = {
|
|
145
|
+
id: randomUUID(),
|
|
146
|
+
role: 'assistant',
|
|
147
|
+
content: textContent,
|
|
148
|
+
state: 'complete' as const,
|
|
149
|
+
timestamp: new Date(),
|
|
150
|
+
...(toolCalls.length > 0 && { toolCalls }),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
result.metadata = {
|
|
154
|
+
inputTokens: usage.input_tokens,
|
|
155
|
+
outputTokens: usage.output_tokens,
|
|
156
|
+
model,
|
|
157
|
+
};
|
|
158
|
+
if (stopReason) {
|
|
159
|
+
result.metadata['stopReason'] = stopReason;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function buildPartialResult(
|
|
166
|
+
textParts: string[],
|
|
167
|
+
toolCalls: Array<{
|
|
168
|
+
id: string;
|
|
169
|
+
type: 'function';
|
|
170
|
+
function: { name: string; arguments: string };
|
|
171
|
+
}>,
|
|
172
|
+
usage: { input_tokens: number; output_tokens: number },
|
|
173
|
+
model: string,
|
|
174
|
+
): TUniversalMessage {
|
|
175
|
+
const partialText = textParts.join('') || '';
|
|
176
|
+
const partialResult: TUniversalMessage = {
|
|
177
|
+
id: randomUUID(),
|
|
178
|
+
role: 'assistant',
|
|
179
|
+
content: partialText,
|
|
180
|
+
state: 'complete' as const,
|
|
181
|
+
timestamp: new Date(),
|
|
182
|
+
...(toolCalls.length > 0 && { toolCalls }),
|
|
183
|
+
};
|
|
184
|
+
partialResult.metadata = {
|
|
185
|
+
inputTokens: usage.input_tokens,
|
|
186
|
+
outputTokens: usage.output_tokens,
|
|
187
|
+
model,
|
|
188
|
+
stopReason: 'aborted',
|
|
189
|
+
};
|
|
190
|
+
return partialResult;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Wrap a stream to support abort signal interruption.
|
|
195
|
+
*/
|
|
196
|
+
async function* streamWithAbort(
|
|
197
|
+
stream: AsyncIterable<Anthropic.MessageStreamEvent>,
|
|
198
|
+
signal?: AbortSignal,
|
|
199
|
+
): AsyncIterable<Anthropic.MessageStreamEvent> {
|
|
200
|
+
for await (const event of stream) {
|
|
201
|
+
if (signal?.aborted) break;
|
|
202
|
+
yield event;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic API-specific type definitions
|
|
3
|
+
*
|
|
4
|
+
* This file contains type definitions specific to Anthropic's Claude API,
|
|
5
|
+
* ensuring complete type safety without any/unknown types.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Anthropic Message Types
|
|
9
|
+
export interface IAnthropicMessage {
|
|
10
|
+
id: string;
|
|
11
|
+
type: 'message';
|
|
12
|
+
role: 'assistant' | 'user';
|
|
13
|
+
content: IAnthropicContent[];
|
|
14
|
+
model: string;
|
|
15
|
+
stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | null;
|
|
16
|
+
stop_sequence: string | null;
|
|
17
|
+
usage: IAnthropicUsage;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IAnthropicContent {
|
|
21
|
+
type: 'text' | 'tool_use';
|
|
22
|
+
text?: string;
|
|
23
|
+
id?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
input?: Record<string, string | number | boolean | object>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface IAnthropicUsage {
|
|
29
|
+
input_tokens: number;
|
|
30
|
+
output_tokens: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Request Types
|
|
34
|
+
export interface IAnthropicChatRequestParams {
|
|
35
|
+
model: string;
|
|
36
|
+
max_tokens: number;
|
|
37
|
+
messages: IAnthropicRequestMessage[];
|
|
38
|
+
system?: string;
|
|
39
|
+
temperature?: number;
|
|
40
|
+
top_p?: number;
|
|
41
|
+
top_k?: number;
|
|
42
|
+
stop_sequences?: string[];
|
|
43
|
+
tools?: IAnthropicTool[];
|
|
44
|
+
stream?: false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface IAnthropicStreamRequestParams extends Omit<IAnthropicChatRequestParams, 'stream'> {
|
|
48
|
+
stream: true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface IAnthropicRequestMessage {
|
|
52
|
+
role: 'user' | 'assistant';
|
|
53
|
+
content: string | IAnthropicRequestContent[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface IAnthropicRequestContent {
|
|
57
|
+
type: 'text' | 'tool_use' | 'tool_result';
|
|
58
|
+
text?: string;
|
|
59
|
+
id?: string;
|
|
60
|
+
name?: string;
|
|
61
|
+
input?: Record<string, string | number | boolean | object>;
|
|
62
|
+
content?: string;
|
|
63
|
+
tool_use_id?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Tool Types
|
|
67
|
+
export interface IAnthropicTool {
|
|
68
|
+
name: string;
|
|
69
|
+
description?: string;
|
|
70
|
+
input_schema: {
|
|
71
|
+
type: 'object';
|
|
72
|
+
properties: Record<string, IAnthropicToolProperty>;
|
|
73
|
+
required?: string[];
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface IAnthropicToolProperty {
|
|
78
|
+
type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
|
|
79
|
+
description?: string;
|
|
80
|
+
enum?: string[];
|
|
81
|
+
items?: IAnthropicToolProperty;
|
|
82
|
+
properties?: Record<string, IAnthropicToolProperty>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface IAnthropicToolCall {
|
|
86
|
+
id: string;
|
|
87
|
+
type: 'tool_use';
|
|
88
|
+
name: string;
|
|
89
|
+
input: Record<string, string | number | boolean | object>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Streaming Types
|
|
93
|
+
export interface IAnthropicStreamChunk {
|
|
94
|
+
type:
|
|
95
|
+
| 'message_start'
|
|
96
|
+
| 'message_delta'
|
|
97
|
+
| 'content_block_start'
|
|
98
|
+
| 'content_block_delta'
|
|
99
|
+
| 'content_block_stop'
|
|
100
|
+
| 'message_stop';
|
|
101
|
+
message?: Partial<IAnthropicMessage>;
|
|
102
|
+
delta?: IAnthropicStreamDelta;
|
|
103
|
+
content_block?: IAnthropicContent;
|
|
104
|
+
index?: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface IAnthropicStreamDelta {
|
|
108
|
+
type?: 'text_delta' | 'input_json_delta';
|
|
109
|
+
text?: string;
|
|
110
|
+
partial_json?: string;
|
|
111
|
+
stop_reason?: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use';
|
|
112
|
+
stop_sequence?: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Error Types
|
|
116
|
+
export interface IAnthropicError {
|
|
117
|
+
type: 'error';
|
|
118
|
+
error: {
|
|
119
|
+
type:
|
|
120
|
+
| 'invalid_request_error'
|
|
121
|
+
| 'authentication_error'
|
|
122
|
+
| 'permission_error'
|
|
123
|
+
| 'not_found_error'
|
|
124
|
+
| 'rate_limit_error'
|
|
125
|
+
| 'api_error'
|
|
126
|
+
| 'overloaded_error';
|
|
127
|
+
message: string;
|
|
128
|
+
param?: string;
|
|
129
|
+
code?: string;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Provider Configuration
|
|
134
|
+
export interface IAnthropicLogData {
|
|
135
|
+
model: string;
|
|
136
|
+
messagesCount: number;
|
|
137
|
+
hasTools: boolean;
|
|
138
|
+
maxTokens?: number;
|
|
139
|
+
temperature?: number;
|
|
140
|
+
timestamp: string;
|
|
141
|
+
requestId?: string;
|
|
142
|
+
usage?: IAnthropicUsage;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Response Types for Internal Processing
|
|
146
|
+
export interface IAnthropicProviderResponse {
|
|
147
|
+
message: IAnthropicMessage;
|
|
148
|
+
usage: IAnthropicUsage;
|
|
149
|
+
model: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Stream Handler Types
|
|
153
|
+
export interface IAnthropicStreamContext {
|
|
154
|
+
currentMessage: string;
|
|
155
|
+
currentToolCalls: IAnthropicToolCall[];
|
|
156
|
+
isComplete: boolean;
|
|
157
|
+
usage?: IAnthropicUsage;
|
|
158
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import type { IExecutor, TProviderOptionValueBase } from '@robota-sdk/agent-core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Valid provider option value types
|
|
6
|
+
*/
|
|
7
|
+
export type TAnthropicProviderOptionValue =
|
|
8
|
+
| string
|
|
9
|
+
| number
|
|
10
|
+
| boolean
|
|
11
|
+
| undefined
|
|
12
|
+
| null
|
|
13
|
+
| Anthropic
|
|
14
|
+
| IExecutor
|
|
15
|
+
| TProviderOptionValueBase
|
|
16
|
+
| TAnthropicProviderOptionValue[]
|
|
17
|
+
| { [key: string]: TAnthropicProviderOptionValue };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Anthropic provider options
|
|
21
|
+
*
|
|
22
|
+
* Note: Anthropic API doesn't support response format configuration.
|
|
23
|
+
* JSON output can be requested through prompt instructions.
|
|
24
|
+
*/
|
|
25
|
+
export interface IAnthropicProviderOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Additional provider-specific options
|
|
28
|
+
*/
|
|
29
|
+
[key: string]: TAnthropicProviderOptionValue;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Anthropic API key (required when client and executor are not provided)
|
|
33
|
+
*/
|
|
34
|
+
apiKey?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* API request timeout (milliseconds)
|
|
38
|
+
*/
|
|
39
|
+
timeout?: number;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* API base URL
|
|
43
|
+
*/
|
|
44
|
+
baseURL?: string;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Anthropic client instance (optional: will be created from apiKey if not provided)
|
|
48
|
+
* Use this path for advanced Anthropic SDK authentication that is outside
|
|
49
|
+
* Robota's normal API-key setup flow.
|
|
50
|
+
*/
|
|
51
|
+
client?: Anthropic;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Optional executor for handling AI requests
|
|
55
|
+
*
|
|
56
|
+
* When provided, the provider will delegate all chat operations to this executor
|
|
57
|
+
* instead of making direct API calls. This enables remote execution capabilities.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* import { LocalExecutor, RemoteExecutor } from '@robota-sdk/agent-core';
|
|
62
|
+
*
|
|
63
|
+
* // Local execution (registers this provider)
|
|
64
|
+
* const localExecutor = new LocalExecutor();
|
|
65
|
+
* localExecutor.registerProvider('anthropic', new AnthropicProvider({ apiKey: 'sk-ant-...' }));
|
|
66
|
+
*
|
|
67
|
+
* // Remote execution
|
|
68
|
+
* const remoteExecutor = new RemoteExecutor({
|
|
69
|
+
* serverUrl: 'https://api.robota.io',
|
|
70
|
+
* userApiKey: 'user-token-123'
|
|
71
|
+
* });
|
|
72
|
+
*
|
|
73
|
+
* const provider = new AnthropicProvider({
|
|
74
|
+
* executor: remoteExecutor // No direct API key needed
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
executor?: IExecutor;
|
|
79
|
+
}
|