@sashabogi/foundation 0.1.6 → 0.1.7
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/cli/setup-wizard.d.ts +30 -0
- package/dist/cli/setup-wizard.d.ts.map +1 -0
- package/dist/cli/setup-wizard.js +1645 -0
- package/dist/cli/setup-wizard.js.map +1 -0
- package/dist/cli/test-connection.d.ts +76 -0
- package/dist/cli/test-connection.d.ts.map +1 -0
- package/dist/cli/test-connection.js +697 -0
- package/dist/cli/test-connection.js.map +1 -0
- package/dist/cli.d.ts +3 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +47 -4
- package/dist/cli.js.map +1 -1
- package/dist/providers/anthropic.d.ts +178 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +514 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/base.d.ts +154 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +227 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/deepseek.d.ts +23 -0
- package/dist/providers/deepseek.d.ts.map +1 -0
- package/dist/providers/deepseek.js +31 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/fireworks.d.ts +23 -0
- package/dist/providers/fireworks.d.ts.map +1 -0
- package/dist/providers/fireworks.js +31 -0
- package/dist/providers/fireworks.js.map +1 -0
- package/dist/providers/gemini.d.ts +85 -0
- package/dist/providers/gemini.d.ts.map +1 -0
- package/dist/providers/gemini.js +414 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/groq.d.ts +23 -0
- package/dist/providers/groq.d.ts.map +1 -0
- package/dist/providers/groq.js +31 -0
- package/dist/providers/groq.js.map +1 -0
- package/dist/providers/index.d.ts +23 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +27 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/kimi-code.d.ts +32 -0
- package/dist/providers/kimi-code.d.ts.map +1 -0
- package/dist/providers/kimi-code.js +46 -0
- package/dist/providers/kimi-code.js.map +1 -0
- package/dist/providers/kimi.d.ts +19 -0
- package/dist/providers/kimi.d.ts.map +1 -0
- package/dist/providers/kimi.js +27 -0
- package/dist/providers/kimi.js.map +1 -0
- package/dist/providers/manager.d.ts +144 -0
- package/dist/providers/manager.d.ts.map +1 -0
- package/dist/providers/manager.js +279 -0
- package/dist/providers/manager.js.map +1 -0
- package/dist/providers/ollama.d.ts +83 -0
- package/dist/providers/ollama.d.ts.map +1 -0
- package/dist/providers/ollama.js +450 -0
- package/dist/providers/ollama.js.map +1 -0
- package/dist/providers/openai.d.ts +91 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +445 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +23 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +31 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/providers/perplexity.d.ts +34 -0
- package/dist/providers/perplexity.d.ts.map +1 -0
- package/dist/providers/perplexity.js +58 -0
- package/dist/providers/perplexity.js.map +1 -0
- package/dist/providers/together.d.ts +23 -0
- package/dist/providers/together.d.ts.map +1 -0
- package/dist/providers/together.js +31 -0
- package/dist/providers/together.js.map +1 -0
- package/dist/providers/types.d.ts +229 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +73 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/providers/zai.d.ts +19 -0
- package/dist/providers/zai.d.ts.map +1 -0
- package/dist/providers/zai.js +27 -0
- package/dist/providers/zai.js.map +1 -0
- package/dist/services/provider.service.d.ts +28 -0
- package/dist/services/provider.service.d.ts.map +1 -1
- package/dist/services/provider.service.js +137 -13
- package/dist/services/provider.service.js.map +1 -1
- package/dist/tools/demerzel/engine.d.ts +67 -0
- package/dist/tools/demerzel/engine.d.ts.map +1 -0
- package/dist/tools/demerzel/engine.js +401 -0
- package/dist/tools/demerzel/engine.js.map +1 -0
- package/dist/tools/demerzel/enhanced-snapshot.d.ts +67 -0
- package/dist/tools/demerzel/enhanced-snapshot.d.ts.map +1 -0
- package/dist/tools/demerzel/enhanced-snapshot.js +481 -0
- package/dist/tools/demerzel/enhanced-snapshot.js.map +1 -0
- package/dist/tools/demerzel/index.d.ts +11 -0
- package/dist/tools/demerzel/index.d.ts.map +1 -1
- package/dist/tools/demerzel/index.js +656 -85
- package/dist/tools/demerzel/index.js.map +1 -1
- package/dist/tools/demerzel/prompts.d.ts +26 -0
- package/dist/tools/demerzel/prompts.d.ts.map +1 -0
- package/dist/tools/demerzel/prompts.js +181 -0
- package/dist/tools/demerzel/prompts.js.map +1 -0
- package/dist/tools/demerzel/semantic-search.d.ts +54 -0
- package/dist/tools/demerzel/semantic-search.d.ts.map +1 -0
- package/dist/tools/demerzel/semantic-search.js +205 -0
- package/dist/tools/demerzel/semantic-search.js.map +1 -0
- package/dist/tools/demerzel/snapshot.d.ts +30 -0
- package/dist/tools/demerzel/snapshot.d.ts.map +1 -0
- package/dist/tools/demerzel/snapshot.js +169 -0
- package/dist/tools/demerzel/snapshot.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Provider Implementation
|
|
3
|
+
*
|
|
4
|
+
* Provider adapter for the Anthropic Claude API.
|
|
5
|
+
* Handles message formatting, streaming, and tool use for Claude models.
|
|
6
|
+
*
|
|
7
|
+
* Supports two access modes:
|
|
8
|
+
* - 'api': Direct HTTP calls to Anthropic API (requires API key)
|
|
9
|
+
* - 'session': Delegates to the current Claude Code session (zero-cost, in-process)
|
|
10
|
+
* - 'auto': Automatically selects the best available mode (session > api)
|
|
11
|
+
*/
|
|
12
|
+
import { BaseProvider } from './base.js';
|
|
13
|
+
// ============================================================================
|
|
14
|
+
// Session Detection
|
|
15
|
+
// ============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Check if running inside a Claude Code session.
|
|
18
|
+
* When true, we can use session delegation instead of API calls.
|
|
19
|
+
*
|
|
20
|
+
* @returns true if running in a Claude Code MCP context
|
|
21
|
+
*/
|
|
22
|
+
function isRunningInClaudeSession() {
|
|
23
|
+
return process.env.CLAUDE_CODE_ENTRY === 'mcp' ||
|
|
24
|
+
process.env.CLAUDE_CODE_SESSION === 'true';
|
|
25
|
+
}
|
|
26
|
+
// ============================================================================
|
|
27
|
+
// Anthropic Provider
|
|
28
|
+
// ============================================================================
|
|
29
|
+
/**
|
|
30
|
+
* Anthropic Claude API provider adapter.
|
|
31
|
+
*
|
|
32
|
+
* Implements the Provider interface for Anthropic's Claude models.
|
|
33
|
+
* Handles:
|
|
34
|
+
* - Message format translation (our format -> Anthropic format)
|
|
35
|
+
* - Streaming SSE event parsing
|
|
36
|
+
* - Tool use formatting
|
|
37
|
+
* - Authentication via x-api-key header
|
|
38
|
+
*/
|
|
39
|
+
export class AnthropicProvider extends BaseProvider {
|
|
40
|
+
name = 'anthropic';
|
|
41
|
+
/** Default Anthropic API base URL */
|
|
42
|
+
DEFAULT_BASE_URL = 'https://api.anthropic.com';
|
|
43
|
+
/** Required API version header value */
|
|
44
|
+
API_VERSION = '2023-06-01';
|
|
45
|
+
/** Default max tokens if not specified in request */
|
|
46
|
+
DEFAULT_MAX_TOKENS = 4096;
|
|
47
|
+
/**
|
|
48
|
+
* Build Anthropic-specific HTTP headers.
|
|
49
|
+
* Uses x-api-key instead of Bearer token for authentication.
|
|
50
|
+
*/
|
|
51
|
+
buildHeaders() {
|
|
52
|
+
const headers = {
|
|
53
|
+
'Content-Type': 'application/json',
|
|
54
|
+
'anthropic-version': this.API_VERSION,
|
|
55
|
+
};
|
|
56
|
+
// Anthropic uses x-api-key header instead of Bearer token
|
|
57
|
+
if (this.config.api_key) {
|
|
58
|
+
headers['x-api-key'] = this.config.api_key;
|
|
59
|
+
}
|
|
60
|
+
// Add any custom headers from config
|
|
61
|
+
if (this.config.headers) {
|
|
62
|
+
Object.assign(headers, this.config.headers);
|
|
63
|
+
}
|
|
64
|
+
return headers;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Execute a completion request and return the full response.
|
|
68
|
+
*
|
|
69
|
+
* Supports two modes:
|
|
70
|
+
* - API mode (access_mode: 'api'): Direct HTTP calls to Anthropic API
|
|
71
|
+
* - Session mode (access_mode: 'session'): Delegates to current Claude Code session
|
|
72
|
+
* - Auto mode (access_mode: 'auto'): Selects best available (session > api)
|
|
73
|
+
*
|
|
74
|
+
* @param request - The completion request
|
|
75
|
+
* @returns Promise resolving to the normalized completion response or session delegation
|
|
76
|
+
*/
|
|
77
|
+
async complete(request) {
|
|
78
|
+
const accessMode = this.resolveAccessMode();
|
|
79
|
+
if (accessMode === 'session') {
|
|
80
|
+
return this.completeViaSession(request);
|
|
81
|
+
}
|
|
82
|
+
// Default: API mode with direct HTTP calls
|
|
83
|
+
const url = `${this.getBaseUrl(this.DEFAULT_BASE_URL)}/v1/messages`;
|
|
84
|
+
const anthropicRequest = this.translateRequest(request);
|
|
85
|
+
const response = await this.makeRequest({
|
|
86
|
+
method: 'POST',
|
|
87
|
+
url,
|
|
88
|
+
headers: this.buildHeaders(),
|
|
89
|
+
body: anthropicRequest,
|
|
90
|
+
timeoutMs: request.timeout_ms ?? this.defaultTimeoutMs,
|
|
91
|
+
});
|
|
92
|
+
return this.translateResponse(response);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolve the effective access mode based on config and environment.
|
|
96
|
+
*
|
|
97
|
+
* Priority for 'auto' mode:
|
|
98
|
+
* 1. session - if running inside Claude Code session
|
|
99
|
+
* 2. api - if API key is configured
|
|
100
|
+
*
|
|
101
|
+
* @returns The resolved access mode to use
|
|
102
|
+
* @throws Error if no valid access mode is available
|
|
103
|
+
*/
|
|
104
|
+
resolveAccessMode() {
|
|
105
|
+
const configMode = this.config.access_mode;
|
|
106
|
+
if (configMode === 'auto') {
|
|
107
|
+
// Priority: session > api
|
|
108
|
+
if (isRunningInClaudeSession()) {
|
|
109
|
+
return 'session';
|
|
110
|
+
}
|
|
111
|
+
if (this.config.api_key) {
|
|
112
|
+
return 'api';
|
|
113
|
+
}
|
|
114
|
+
throw new Error('No valid access mode available. Set ANTHROPIC_API_KEY or run inside Claude Code.');
|
|
115
|
+
}
|
|
116
|
+
// Handle explicit session mode
|
|
117
|
+
if (configMode === 'session') {
|
|
118
|
+
return 'session';
|
|
119
|
+
}
|
|
120
|
+
// Default to API mode
|
|
121
|
+
return configMode || 'api';
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Return a session delegation response instead of making an API call.
|
|
125
|
+
* The current Claude session will handle the request directly.
|
|
126
|
+
*
|
|
127
|
+
* This is the most efficient mode when running inside Claude Code:
|
|
128
|
+
* - Zero additional API costs (uses current session's quota)
|
|
129
|
+
* - No subprocess spawning overhead
|
|
130
|
+
* - Direct integration with the calling Claude instance
|
|
131
|
+
*
|
|
132
|
+
* @param request - The completion request
|
|
133
|
+
* @returns SessionDelegationResponse for the caller to handle
|
|
134
|
+
*/
|
|
135
|
+
completeViaSession(request) {
|
|
136
|
+
// Extract the task from messages (last user message)
|
|
137
|
+
const lastUserMessage = [...request.messages].reverse().find(m => m.role === 'user');
|
|
138
|
+
const task = typeof lastUserMessage?.content === 'string'
|
|
139
|
+
? lastUserMessage.content
|
|
140
|
+
: lastUserMessage?.content?.map(b => b.type === 'text' ? b.text : '').join('') || '';
|
|
141
|
+
// Get system prompt from config (system messages are not part of Message type,
|
|
142
|
+
// they're passed through config.system_prompt by the role resolver)
|
|
143
|
+
const systemPrompt = this.config.system_prompt || '';
|
|
144
|
+
// Get role name from config
|
|
145
|
+
const role = this.config.role_name || 'assistant';
|
|
146
|
+
return {
|
|
147
|
+
delegation: true,
|
|
148
|
+
role,
|
|
149
|
+
system_prompt: systemPrompt,
|
|
150
|
+
task,
|
|
151
|
+
model: request.model,
|
|
152
|
+
instructions: `You are now acting as the '${role}' agent. Apply the following system prompt as your persona and execute the task.\n\n---\nSYSTEM PROMPT:\n${systemPrompt}\n---\n\nTASK:\n${task}`,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Execute a streaming completion request.
|
|
157
|
+
* Yields normalized StreamChunk events as they arrive.
|
|
158
|
+
*
|
|
159
|
+
* @param request - The completion request
|
|
160
|
+
* @returns Async iterable of stream chunks
|
|
161
|
+
*/
|
|
162
|
+
async *completeStream(request) {
|
|
163
|
+
const url = `${this.getBaseUrl(this.DEFAULT_BASE_URL)}/v1/messages`;
|
|
164
|
+
const anthropicRequest = this.translateRequest(request);
|
|
165
|
+
anthropicRequest.stream = true;
|
|
166
|
+
const response = await this.makeStreamingRequest({
|
|
167
|
+
method: 'POST',
|
|
168
|
+
url,
|
|
169
|
+
headers: this.buildHeaders(),
|
|
170
|
+
body: anthropicRequest,
|
|
171
|
+
timeoutMs: request.timeout_ms ?? this.defaultTimeoutMs,
|
|
172
|
+
stream: true,
|
|
173
|
+
});
|
|
174
|
+
if (!response.body) {
|
|
175
|
+
throw new Error('Response body is null');
|
|
176
|
+
}
|
|
177
|
+
// Parse SSE stream
|
|
178
|
+
yield* this.parseSSEStream(response.body);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Check if the Anthropic API is available and credentials are valid.
|
|
182
|
+
*
|
|
183
|
+
* For each access mode:
|
|
184
|
+
* - session: Checks if running in Claude Code context
|
|
185
|
+
* - api: Makes a minimal API request to verify credentials
|
|
186
|
+
*
|
|
187
|
+
* @throws Error if the health check fails
|
|
188
|
+
*/
|
|
189
|
+
async healthCheck() {
|
|
190
|
+
const accessMode = this.resolveAccessMode();
|
|
191
|
+
// For session mode, verify we're in a Claude Code context
|
|
192
|
+
if (accessMode === 'session') {
|
|
193
|
+
if (!isRunningInClaudeSession()) {
|
|
194
|
+
throw new Error('Session mode requires running inside a Claude Code session');
|
|
195
|
+
}
|
|
196
|
+
return; // In session context, assume healthy
|
|
197
|
+
}
|
|
198
|
+
// API mode: Make a minimal request to verify credentials
|
|
199
|
+
const request = {
|
|
200
|
+
model: 'claude-3-haiku-20240307',
|
|
201
|
+
messages: [{ role: 'user', content: 'ping' }],
|
|
202
|
+
max_tokens: 1,
|
|
203
|
+
};
|
|
204
|
+
const url = `${this.getBaseUrl(this.DEFAULT_BASE_URL)}/v1/messages`;
|
|
205
|
+
const anthropicRequest = this.translateRequest(request);
|
|
206
|
+
await this.makeRequest({
|
|
207
|
+
method: 'POST',
|
|
208
|
+
url,
|
|
209
|
+
headers: this.buildHeaders(),
|
|
210
|
+
body: anthropicRequest,
|
|
211
|
+
timeoutMs: request.timeout_ms ?? this.defaultTimeoutMs,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Translate our normalized request format to Anthropic's format.
|
|
216
|
+
*
|
|
217
|
+
* @param request - Normalized completion request
|
|
218
|
+
* @returns Anthropic API request body
|
|
219
|
+
*/
|
|
220
|
+
translateRequest(request) {
|
|
221
|
+
const anthropicRequest = {
|
|
222
|
+
model: request.model,
|
|
223
|
+
messages: this.translateMessages(request.messages),
|
|
224
|
+
max_tokens: request.max_tokens ?? this.DEFAULT_MAX_TOKENS,
|
|
225
|
+
};
|
|
226
|
+
// Add optional parameters
|
|
227
|
+
if (request.temperature !== undefined) {
|
|
228
|
+
anthropicRequest.temperature = request.temperature;
|
|
229
|
+
}
|
|
230
|
+
// Extract system prompt from messages if present
|
|
231
|
+
const systemPrompt = this.extractSystemPrompt();
|
|
232
|
+
if (systemPrompt) {
|
|
233
|
+
anthropicRequest.system = systemPrompt;
|
|
234
|
+
}
|
|
235
|
+
// Translate tools if present
|
|
236
|
+
if (request.tools && request.tools.length > 0) {
|
|
237
|
+
anthropicRequest.tools = this.translateTools(request.tools);
|
|
238
|
+
}
|
|
239
|
+
if (request.stream) {
|
|
240
|
+
anthropicRequest.stream = true;
|
|
241
|
+
}
|
|
242
|
+
return anthropicRequest;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Translate normalized messages to Anthropic format.
|
|
246
|
+
* Filters out system messages (handled separately).
|
|
247
|
+
*
|
|
248
|
+
* @param messages - Normalized messages
|
|
249
|
+
* @returns Anthropic-formatted messages
|
|
250
|
+
*/
|
|
251
|
+
translateMessages(messages) {
|
|
252
|
+
return messages
|
|
253
|
+
.filter((msg) => msg.role === 'user' || msg.role === 'assistant')
|
|
254
|
+
.map((msg) => ({
|
|
255
|
+
role: msg.role,
|
|
256
|
+
content: this.translateMessageContent(msg.content),
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Translate message content to Anthropic format.
|
|
261
|
+
*
|
|
262
|
+
* @param content - String or content blocks
|
|
263
|
+
* @returns Anthropic-formatted content
|
|
264
|
+
*/
|
|
265
|
+
translateMessageContent(content) {
|
|
266
|
+
if (typeof content === 'string') {
|
|
267
|
+
return content;
|
|
268
|
+
}
|
|
269
|
+
// Translate content blocks
|
|
270
|
+
return content.map((block) => this.translateContentBlock(block));
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Translate a single content block to Anthropic format.
|
|
274
|
+
*
|
|
275
|
+
* @param block - Normalized content block
|
|
276
|
+
* @returns Anthropic content block
|
|
277
|
+
*/
|
|
278
|
+
translateContentBlock(block) {
|
|
279
|
+
switch (block.type) {
|
|
280
|
+
case 'text':
|
|
281
|
+
return {
|
|
282
|
+
type: 'text',
|
|
283
|
+
text: block.text ?? '',
|
|
284
|
+
};
|
|
285
|
+
case 'tool_use':
|
|
286
|
+
return {
|
|
287
|
+
type: 'tool_use',
|
|
288
|
+
id: block.id ?? '',
|
|
289
|
+
name: block.name ?? '',
|
|
290
|
+
input: block.input ?? {},
|
|
291
|
+
};
|
|
292
|
+
case 'tool_result':
|
|
293
|
+
return {
|
|
294
|
+
type: 'tool_result',
|
|
295
|
+
tool_use_id: block.tool_use_id ?? '',
|
|
296
|
+
content: block.content ?? '',
|
|
297
|
+
};
|
|
298
|
+
default:
|
|
299
|
+
return {
|
|
300
|
+
type: 'text',
|
|
301
|
+
text: '',
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Extract system prompt from messages.
|
|
307
|
+
* Our format may include it in messages; Anthropic uses a separate parameter.
|
|
308
|
+
*
|
|
309
|
+
* @returns System prompt string or undefined
|
|
310
|
+
*/
|
|
311
|
+
extractSystemPrompt() {
|
|
312
|
+
// The system prompt should be passed via the role config, not in messages
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Translate tools to Anthropic format.
|
|
317
|
+
* Our tool format is already compatible with Anthropic's format.
|
|
318
|
+
*
|
|
319
|
+
* @param tools - Normalized tools
|
|
320
|
+
* @returns Anthropic tool definitions
|
|
321
|
+
*/
|
|
322
|
+
translateTools(tools) {
|
|
323
|
+
return tools.map((tool) => ({
|
|
324
|
+
name: tool.name,
|
|
325
|
+
description: tool.description,
|
|
326
|
+
input_schema: tool.input_schema,
|
|
327
|
+
}));
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Translate Anthropic response to our normalized format.
|
|
331
|
+
*
|
|
332
|
+
* @param response - Anthropic API response
|
|
333
|
+
* @returns Normalized completion response
|
|
334
|
+
*/
|
|
335
|
+
translateResponse(response) {
|
|
336
|
+
return {
|
|
337
|
+
id: response.id,
|
|
338
|
+
content: this.translateResponseContent(response.content),
|
|
339
|
+
model: response.model,
|
|
340
|
+
stop_reason: response.stop_reason,
|
|
341
|
+
usage: this.translateUsage(response.usage),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Translate Anthropic content blocks to our normalized format.
|
|
346
|
+
*
|
|
347
|
+
* @param content - Anthropic content blocks
|
|
348
|
+
* @returns Normalized content blocks
|
|
349
|
+
*/
|
|
350
|
+
translateResponseContent(content) {
|
|
351
|
+
return content.map((block) => {
|
|
352
|
+
switch (block.type) {
|
|
353
|
+
case 'text':
|
|
354
|
+
return {
|
|
355
|
+
type: 'text',
|
|
356
|
+
text: block.text ?? '',
|
|
357
|
+
};
|
|
358
|
+
case 'tool_use':
|
|
359
|
+
return {
|
|
360
|
+
type: 'tool_use',
|
|
361
|
+
id: block.id ?? '',
|
|
362
|
+
name: block.name ?? '',
|
|
363
|
+
input: block.input ?? {},
|
|
364
|
+
};
|
|
365
|
+
case 'tool_result':
|
|
366
|
+
return {
|
|
367
|
+
type: 'tool_result',
|
|
368
|
+
tool_use_id: block.tool_use_id ?? '',
|
|
369
|
+
content: block.content ?? '',
|
|
370
|
+
};
|
|
371
|
+
default:
|
|
372
|
+
return {
|
|
373
|
+
type: 'text',
|
|
374
|
+
text: '',
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Translate Anthropic usage info to our normalized format.
|
|
381
|
+
*
|
|
382
|
+
* @param usage - Anthropic usage info
|
|
383
|
+
* @returns Normalized usage info
|
|
384
|
+
*/
|
|
385
|
+
translateUsage(usage) {
|
|
386
|
+
const result = {
|
|
387
|
+
input_tokens: usage.input_tokens,
|
|
388
|
+
output_tokens: usage.output_tokens,
|
|
389
|
+
};
|
|
390
|
+
// Only add cache tokens if they are defined
|
|
391
|
+
if (usage.cache_creation_input_tokens !== undefined) {
|
|
392
|
+
result.cache_creation_input_tokens = usage.cache_creation_input_tokens;
|
|
393
|
+
}
|
|
394
|
+
if (usage.cache_read_input_tokens !== undefined) {
|
|
395
|
+
result.cache_read_input_tokens = usage.cache_read_input_tokens;
|
|
396
|
+
}
|
|
397
|
+
return result;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Parse Anthropic SSE stream and yield normalized stream chunks.
|
|
401
|
+
*
|
|
402
|
+
* @param body - ReadableStream from fetch response
|
|
403
|
+
* @returns Async generator of normalized stream chunks
|
|
404
|
+
*/
|
|
405
|
+
async *parseSSEStream(body) {
|
|
406
|
+
const reader = body.getReader();
|
|
407
|
+
const decoder = new TextDecoder();
|
|
408
|
+
let buffer = '';
|
|
409
|
+
try {
|
|
410
|
+
while (true) {
|
|
411
|
+
const { done, value } = await reader.read();
|
|
412
|
+
if (done) {
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
buffer += decoder.decode(value, { stream: true });
|
|
416
|
+
// Process complete events from buffer
|
|
417
|
+
const lines = buffer.split('\n');
|
|
418
|
+
buffer = lines.pop() ?? ''; // Keep incomplete line in buffer
|
|
419
|
+
for (const line of lines) {
|
|
420
|
+
const trimmedLine = line.trim();
|
|
421
|
+
// Skip empty lines and comments
|
|
422
|
+
if (!trimmedLine || trimmedLine.startsWith(':')) {
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
// Parse SSE data line
|
|
426
|
+
if (trimmedLine.startsWith('data: ')) {
|
|
427
|
+
const data = trimmedLine.slice(6);
|
|
428
|
+
// Handle stream end marker
|
|
429
|
+
if (data === '[DONE]') {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
433
|
+
const event = JSON.parse(data);
|
|
434
|
+
const chunk = this.translateStreamEvent(event);
|
|
435
|
+
if (chunk) {
|
|
436
|
+
yield chunk;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
// Skip malformed JSON
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
finally {
|
|
447
|
+
reader.releaseLock();
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Translate an Anthropic SSE event to our normalized StreamChunk format.
|
|
452
|
+
*
|
|
453
|
+
* @param event - Anthropic stream event
|
|
454
|
+
* @returns Normalized stream chunk or undefined to skip
|
|
455
|
+
*/
|
|
456
|
+
translateStreamEvent(event) {
|
|
457
|
+
switch (event.type) {
|
|
458
|
+
case 'content_block_start':
|
|
459
|
+
return {
|
|
460
|
+
type: 'content_block_start',
|
|
461
|
+
index: event.index,
|
|
462
|
+
content_block: this.translateContentBlockFromStream(event.content_block),
|
|
463
|
+
};
|
|
464
|
+
case 'content_block_delta': {
|
|
465
|
+
const delta = {
|
|
466
|
+
type: event.delta.type,
|
|
467
|
+
};
|
|
468
|
+
if (event.delta.text !== undefined) {
|
|
469
|
+
delta.text = event.delta.text;
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
type: 'content_block_delta',
|
|
473
|
+
index: event.index,
|
|
474
|
+
delta,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
case 'message_stop':
|
|
478
|
+
return {
|
|
479
|
+
type: 'message_stop',
|
|
480
|
+
};
|
|
481
|
+
// Skip other events (message_start, content_block_stop, ping, etc.)
|
|
482
|
+
default:
|
|
483
|
+
return undefined;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Translate a content block from a stream event.
|
|
488
|
+
*
|
|
489
|
+
* @param block - Anthropic content block from stream
|
|
490
|
+
* @returns Normalized content block
|
|
491
|
+
*/
|
|
492
|
+
translateContentBlockFromStream(block) {
|
|
493
|
+
switch (block.type) {
|
|
494
|
+
case 'text':
|
|
495
|
+
return {
|
|
496
|
+
type: 'text',
|
|
497
|
+
text: block.text ?? '',
|
|
498
|
+
};
|
|
499
|
+
case 'tool_use':
|
|
500
|
+
return {
|
|
501
|
+
type: 'tool_use',
|
|
502
|
+
id: block.id ?? '',
|
|
503
|
+
name: block.name ?? '',
|
|
504
|
+
input: block.input ?? {},
|
|
505
|
+
};
|
|
506
|
+
default:
|
|
507
|
+
return {
|
|
508
|
+
type: 'text',
|
|
509
|
+
text: '',
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;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qBAAqB;oBAC3B,KAAK,EAAE,KAAK,CAAC,KAAK;oBAClB,KAAK;iBACN,CAAC;YACJ,CAAC;YAED,KAAK,cAAc;gBACjB,OAAO;oBACL,IAAI,EAAE,cAAc;iBACrB,CAAC;YAEJ,oEAAoE;YACpE;gBACE,OAAO,SAAS,CAAC;QACrB,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACK,+BAA+B,CAAC,KAA4B;QAClE,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;YACnB,KAAK,MAAM;gBACT,OAAO;oBACL,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,EAAE;iBACvB,CAAC;YAEJ,KAAK,UAAU;gBACb,OAAO;oBACL,IAAI,EAAE,UAAU;oBAChB,EAAE,EAAE,KAAK,CAAC,EAAE,IAAI,EAAE;oBAClB,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,EAAE;oBACtB,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,EAAE;iBACzB,CAAC;YAEJ;gBACE,OAAO;oBACL,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,EAAE;iBACT,CAAC;QACN,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Provider Implementation
|
|
3
|
+
*
|
|
4
|
+
* Abstract base class that implements common functionality for all LLM providers.
|
|
5
|
+
* Handles authentication headers, error handling, timeout management, and
|
|
6
|
+
* provides the interface that concrete providers must implement.
|
|
7
|
+
*/
|
|
8
|
+
import { type Provider, type ProviderConfig, type CompletionRequest, type CompletionResponse, type SessionDelegationResponse, type StreamChunk } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* Options for making HTTP requests to provider APIs.
|
|
11
|
+
*/
|
|
12
|
+
export interface RequestOptions {
|
|
13
|
+
/** HTTP method */
|
|
14
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
15
|
+
/** Request URL */
|
|
16
|
+
url: string;
|
|
17
|
+
/** Request headers */
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
/** Request body (will be JSON stringified) */
|
|
20
|
+
body?: unknown;
|
|
21
|
+
/** Timeout in milliseconds */
|
|
22
|
+
timeoutMs?: number;
|
|
23
|
+
/** Whether this is a streaming request */
|
|
24
|
+
stream?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error response structure from provider APIs.
|
|
28
|
+
*/
|
|
29
|
+
export interface ProviderErrorResponse {
|
|
30
|
+
error?: {
|
|
31
|
+
message?: string;
|
|
32
|
+
type?: string;
|
|
33
|
+
code?: string;
|
|
34
|
+
};
|
|
35
|
+
message?: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Abstract base class for LLM providers.
|
|
39
|
+
*
|
|
40
|
+
* Provides common functionality including:
|
|
41
|
+
* - Authentication header management
|
|
42
|
+
* - HTTP request helpers with timeout support
|
|
43
|
+
* - Standardized error handling
|
|
44
|
+
* - Response parsing utilities
|
|
45
|
+
*
|
|
46
|
+
* Concrete providers must implement:
|
|
47
|
+
* - complete(): Execute a completion request
|
|
48
|
+
* - completeStream(): Execute a streaming completion request
|
|
49
|
+
* - healthCheck(): Verify provider connectivity
|
|
50
|
+
*/
|
|
51
|
+
export declare abstract class BaseProvider implements Provider {
|
|
52
|
+
/** Provider name identifier */
|
|
53
|
+
abstract readonly name: string;
|
|
54
|
+
/** Provider configuration */
|
|
55
|
+
protected readonly config: ProviderConfig;
|
|
56
|
+
/** Default timeout in milliseconds (3 minutes for complex LLM operations) */
|
|
57
|
+
protected readonly defaultTimeoutMs: number;
|
|
58
|
+
/**
|
|
59
|
+
* Create a new provider instance.
|
|
60
|
+
*
|
|
61
|
+
* @param config - Provider-specific configuration (API keys, URLs, etc.)
|
|
62
|
+
*/
|
|
63
|
+
constructor(config: ProviderConfig);
|
|
64
|
+
/**
|
|
65
|
+
* Execute a completion request and return the full response.
|
|
66
|
+
* Must be implemented by concrete providers.
|
|
67
|
+
*
|
|
68
|
+
* For providers that support session delegation (e.g., Anthropic in 'session' mode),
|
|
69
|
+
* this may return a SessionDelegationResponse instead of making an API call.
|
|
70
|
+
*
|
|
71
|
+
* @param request - The completion request
|
|
72
|
+
* @returns Promise resolving to the completion response or session delegation
|
|
73
|
+
*/
|
|
74
|
+
abstract complete(request: CompletionRequest): Promise<CompletionResponse | SessionDelegationResponse>;
|
|
75
|
+
/**
|
|
76
|
+
* Execute a streaming completion request.
|
|
77
|
+
* Must be implemented by concrete providers.
|
|
78
|
+
*
|
|
79
|
+
* @param request - The completion request
|
|
80
|
+
* @returns Async iterable of stream chunks
|
|
81
|
+
*/
|
|
82
|
+
abstract completeStream(request: CompletionRequest): AsyncIterable<StreamChunk>;
|
|
83
|
+
/**
|
|
84
|
+
* Check if the provider is available and configured correctly.
|
|
85
|
+
* Must be implemented by concrete providers.
|
|
86
|
+
*
|
|
87
|
+
* @throws ProviderError if the health check fails
|
|
88
|
+
*/
|
|
89
|
+
abstract healthCheck(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Build common HTTP headers for provider requests.
|
|
92
|
+
* Includes authentication and content-type headers.
|
|
93
|
+
*
|
|
94
|
+
* @returns Headers object for HTTP requests
|
|
95
|
+
*/
|
|
96
|
+
protected buildHeaders(): Record<string, string>;
|
|
97
|
+
/**
|
|
98
|
+
* Make an HTTP request to the provider API with timeout support.
|
|
99
|
+
*
|
|
100
|
+
* @param options - Request options
|
|
101
|
+
* @returns Promise resolving to the parsed JSON response
|
|
102
|
+
* @throws ProviderError for API errors
|
|
103
|
+
* @throws TimeoutError if the request times out
|
|
104
|
+
*/
|
|
105
|
+
protected makeRequest<T>(options: RequestOptions): Promise<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Make a streaming HTTP request to the provider API.
|
|
108
|
+
*
|
|
109
|
+
* @param options - Request options
|
|
110
|
+
* @returns Response object for streaming
|
|
111
|
+
* @throws ProviderError for API errors
|
|
112
|
+
* @throws TimeoutError if the connection times out
|
|
113
|
+
*/
|
|
114
|
+
protected makeStreamingRequest(options: RequestOptions): Promise<Response>;
|
|
115
|
+
/**
|
|
116
|
+
* Handle HTTP error responses from provider APIs.
|
|
117
|
+
* Throws appropriate error types based on status code.
|
|
118
|
+
*
|
|
119
|
+
* @param response - The HTTP response object
|
|
120
|
+
* @throws RateLimitError for 429 responses
|
|
121
|
+
* @throws AuthenticationError for 401 responses
|
|
122
|
+
* @throws ProviderError for other error responses
|
|
123
|
+
*/
|
|
124
|
+
protected handleErrorResponse(response: Response): Promise<never>;
|
|
125
|
+
/**
|
|
126
|
+
* Handle request-level errors (network, timeout, etc.).
|
|
127
|
+
*
|
|
128
|
+
* @param error - The caught error
|
|
129
|
+
* @param timeoutMs - The timeout that was configured
|
|
130
|
+
* @throws TimeoutError for abort errors
|
|
131
|
+
* @throws ProviderError for other errors
|
|
132
|
+
*/
|
|
133
|
+
protected handleRequestError(error: unknown, timeoutMs: number): never;
|
|
134
|
+
/**
|
|
135
|
+
* Get the base URL for API requests.
|
|
136
|
+
* Uses configured base_url or returns the default for the provider.
|
|
137
|
+
*
|
|
138
|
+
* @param defaultUrl - Default URL if not configured
|
|
139
|
+
* @returns The base URL to use for requests
|
|
140
|
+
*/
|
|
141
|
+
protected getBaseUrl(defaultUrl: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Create an AbortController with a timeout.
|
|
144
|
+
* Useful for streaming requests where we need to track the controller.
|
|
145
|
+
*
|
|
146
|
+
* @param timeoutMs - Timeout in milliseconds
|
|
147
|
+
* @returns Object containing the controller and a cleanup function
|
|
148
|
+
*/
|
|
149
|
+
protected createTimeoutController(timeoutMs: number): {
|
|
150
|
+
controller: AbortController;
|
|
151
|
+
cleanup: () => void;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/providers/base.ts"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