@llumiverse/drivers 1.2.0 → 1.3.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/lib/cjs/anthropic/index.js +64 -0
- package/lib/cjs/anthropic/index.js.map +1 -0
- package/lib/cjs/index.js +1 -0
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/openai/index.js +12 -6
- package/lib/cjs/openai/index.js.map +1 -1
- package/lib/cjs/shared/claude-messages.js +737 -0
- package/lib/cjs/shared/claude-messages.js.map +1 -0
- package/lib/cjs/vertexai/index.js.map +1 -1
- package/lib/cjs/vertexai/models/claude.js +27 -872
- package/lib/cjs/vertexai/models/claude.js.map +1 -1
- package/lib/cjs/vertexai/models/gemini.js +18 -12
- package/lib/cjs/vertexai/models/gemini.js.map +1 -1
- package/lib/esm/anthropic/index.js +57 -0
- package/lib/esm/anthropic/index.js.map +1 -0
- package/lib/esm/index.js +1 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/openai/index.js +12 -7
- package/lib/esm/openai/index.js.map +1 -1
- package/lib/esm/shared/claude-messages.js +716 -0
- package/lib/esm/shared/claude-messages.js.map +1 -0
- package/lib/esm/vertexai/index.js.map +1 -1
- package/lib/esm/vertexai/models/claude.js +27 -865
- package/lib/esm/vertexai/models/claude.js.map +1 -1
- package/lib/esm/vertexai/models/gemini.js +18 -12
- package/lib/esm/vertexai/models/gemini.js.map +1 -1
- package/lib/types/anthropic/index.d.ts +21 -0
- package/lib/types/anthropic/index.d.ts.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.d.ts.map +1 -1
- package/lib/types/openai/index.d.ts +1 -0
- package/lib/types/openai/index.d.ts.map +1 -1
- package/lib/types/shared/claude-messages.d.ts +75 -0
- package/lib/types/shared/claude-messages.d.ts.map +1 -0
- package/lib/types/vertexai/index.d.ts +4 -4
- package/lib/types/vertexai/index.d.ts.map +1 -1
- package/lib/types/vertexai/models/claude.d.ts +3 -106
- package/lib/types/vertexai/models/claude.d.ts.map +1 -1
- package/lib/types/vertexai/models/gemini.d.ts +1 -1
- package/lib/types/vertexai/models/gemini.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/anthropic/index.ts +104 -0
- package/src/index.ts +1 -0
- package/src/openai/index.ts +13 -8
- package/src/shared/claude-messages.ts +879 -0
- package/src/vertexai/index.ts +18 -19
- package/src/vertexai/models/claude-error-handling.test.ts +3 -3
- package/src/vertexai/models/claude.ts +44 -1016
- package/src/vertexai/models/gemini.ts +27 -14
|
@@ -0,0 +1,879 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utilities for Anthropic SDK-based drivers.
|
|
3
|
+
*
|
|
4
|
+
* Used by both the native AnthropicDriver (drivers/src/anthropic/) and the
|
|
5
|
+
* VertexAI Claude pathway (drivers/src/vertexai/models/claude.ts). Both use
|
|
6
|
+
* the same Anthropic Messages API surface — the only difference is the client
|
|
7
|
+
* (Anthropic vs AnthropicVertex) and how auth is wired up.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type Anthropic from '@anthropic-ai/sdk';
|
|
11
|
+
import {
|
|
12
|
+
AnthropicError,
|
|
13
|
+
APIConnectionError,
|
|
14
|
+
APIConnectionTimeoutError,
|
|
15
|
+
APIError,
|
|
16
|
+
APIUserAbortError,
|
|
17
|
+
AuthenticationError,
|
|
18
|
+
BadRequestError,
|
|
19
|
+
ConflictError,
|
|
20
|
+
InternalServerError,
|
|
21
|
+
NotFoundError,
|
|
22
|
+
PermissionDeniedError,
|
|
23
|
+
RateLimitError,
|
|
24
|
+
UnprocessableEntityError,
|
|
25
|
+
} from '@anthropic-ai/sdk/error';
|
|
26
|
+
import type {
|
|
27
|
+
ContentBlock,
|
|
28
|
+
ContentBlockParam,
|
|
29
|
+
DocumentBlockParam,
|
|
30
|
+
ImageBlockParam,
|
|
31
|
+
Message,
|
|
32
|
+
MessageParam,
|
|
33
|
+
TextBlockParam,
|
|
34
|
+
ToolResultBlockParam,
|
|
35
|
+
} from '@anthropic-ai/sdk/resources/index.js';
|
|
36
|
+
import type { MessageStreamParams } from '@anthropic-ai/sdk/resources/index.mjs';
|
|
37
|
+
import type {
|
|
38
|
+
MessageCreateParamsBase,
|
|
39
|
+
RawMessageStreamEvent,
|
|
40
|
+
} from '@anthropic-ai/sdk/resources/messages.js';
|
|
41
|
+
import type AnthropicVertex from '@anthropic-ai/vertex-sdk';
|
|
42
|
+
import { getClaudeMaxTokensLimit } from '@llumiverse/common';
|
|
43
|
+
import {
|
|
44
|
+
type Completion,
|
|
45
|
+
type CompletionChunkObject,
|
|
46
|
+
type CompletionResult,
|
|
47
|
+
type ExecutionOptions,
|
|
48
|
+
type ExecutionTokenUsage,
|
|
49
|
+
getConversationMeta,
|
|
50
|
+
incrementConversationTurn,
|
|
51
|
+
type JSONObject,
|
|
52
|
+
LlumiverseError,
|
|
53
|
+
type LlumiverseErrorContext,
|
|
54
|
+
PromptRole,
|
|
55
|
+
type PromptSegment,
|
|
56
|
+
readStreamAsBase64,
|
|
57
|
+
readStreamAsString,
|
|
58
|
+
type StatelessExecutionOptions,
|
|
59
|
+
stripBase64ImagesFromConversation,
|
|
60
|
+
stripHeartbeatsFromConversation,
|
|
61
|
+
type ToolUse,
|
|
62
|
+
truncateLargeTextInConversation,
|
|
63
|
+
} from '@llumiverse/core';
|
|
64
|
+
import { asyncMap } from '@llumiverse/core/async';
|
|
65
|
+
import { resolveClaudeThinking } from './claude-thinking.js';
|
|
66
|
+
|
|
67
|
+
// ============================================================================
|
|
68
|
+
// Types
|
|
69
|
+
// ============================================================================
|
|
70
|
+
|
|
71
|
+
export interface ClaudePrompt {
|
|
72
|
+
messages: MessageParam[];
|
|
73
|
+
system?: TextBlockParam[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface AnthropicUsageLike {
|
|
77
|
+
input_tokens: number;
|
|
78
|
+
output_tokens: number;
|
|
79
|
+
cache_read_input_tokens?: number | null;
|
|
80
|
+
cache_creation_input_tokens?: number | null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Duck-typed options interface accepted by the shared Claude utilities.
|
|
85
|
+
* Both `AnthropicClaudeOptions` and `VertexAIClaudeOptions` satisfy this structurally.
|
|
86
|
+
*/
|
|
87
|
+
export interface ClaudeBaseOptions {
|
|
88
|
+
_option_id?: string;
|
|
89
|
+
max_tokens?: number;
|
|
90
|
+
temperature?: number;
|
|
91
|
+
top_p?: number;
|
|
92
|
+
top_k?: number;
|
|
93
|
+
stop_sequence?: string[];
|
|
94
|
+
effort?: string;
|
|
95
|
+
thinking_budget_tokens?: number;
|
|
96
|
+
include_thoughts?: boolean;
|
|
97
|
+
cache_enabled?: boolean;
|
|
98
|
+
cache_ttl?: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface RequestOptions {
|
|
102
|
+
headers?: Record<string, string>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
type ClaudeTool = NonNullable<MessageCreateParamsBase['tools']>[number];
|
|
106
|
+
|
|
107
|
+
// ============================================================================
|
|
108
|
+
// Token usage
|
|
109
|
+
// ============================================================================
|
|
110
|
+
|
|
111
|
+
export function anthropicUsageToTokenUsage(usage: AnthropicUsageLike): ExecutionTokenUsage {
|
|
112
|
+
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
113
|
+
const cacheWrite = usage.cache_creation_input_tokens ?? 0;
|
|
114
|
+
return {
|
|
115
|
+
prompt_new: usage.input_tokens,
|
|
116
|
+
prompt: usage.input_tokens + cacheRead + cacheWrite,
|
|
117
|
+
result: usage.output_tokens,
|
|
118
|
+
total: usage.input_tokens + usage.output_tokens + cacheRead + cacheWrite,
|
|
119
|
+
prompt_cached: usage.cache_read_input_tokens ?? undefined,
|
|
120
|
+
prompt_cache_write: usage.cache_creation_input_tokens ?? undefined,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ============================================================================
|
|
125
|
+
// Finish reason
|
|
126
|
+
// ============================================================================
|
|
127
|
+
|
|
128
|
+
export function claudeFinishReason(reason: string | undefined): string | undefined {
|
|
129
|
+
if (!reason) return undefined;
|
|
130
|
+
switch (reason) {
|
|
131
|
+
case 'end_turn': return 'stop';
|
|
132
|
+
case 'max_tokens': return 'length';
|
|
133
|
+
default: return reason; // stop_sequence, tool_use
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ============================================================================
|
|
138
|
+
// Content extraction
|
|
139
|
+
// ============================================================================
|
|
140
|
+
|
|
141
|
+
export function collectClaudeTools(content: ContentBlock[]): ToolUse[] | undefined {
|
|
142
|
+
const out: ToolUse[] = [];
|
|
143
|
+
for (const block of content) {
|
|
144
|
+
if (block.type === 'tool_use') {
|
|
145
|
+
out.push({
|
|
146
|
+
id: block.id,
|
|
147
|
+
tool_name: block.name,
|
|
148
|
+
tool_input: block.input as JSONObject,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return out.length > 0 ? out : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function collectAllTextContent(content: ContentBlock[], includeThoughts = false): string {
|
|
156
|
+
const textParts: string[] = [];
|
|
157
|
+
|
|
158
|
+
if (includeThoughts) {
|
|
159
|
+
for (const block of content) {
|
|
160
|
+
if (block.type === 'thinking' && block.thinking) {
|
|
161
|
+
textParts.push(block.thinking);
|
|
162
|
+
} else if (block.type === 'redacted_thinking' && block.data) {
|
|
163
|
+
textParts.push(`[Redacted thinking: ${block.data}]`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (textParts.length > 0) {
|
|
167
|
+
textParts.push('');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for (const block of content) {
|
|
172
|
+
if (block.type === 'text' && block.text) {
|
|
173
|
+
textParts.push(block.text);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return textParts.join('\n');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ============================================================================
|
|
181
|
+
// Max tokens
|
|
182
|
+
// ============================================================================
|
|
183
|
+
|
|
184
|
+
export function claudeMaxTokens(option: StatelessExecutionOptions): number {
|
|
185
|
+
const modelOptions = option.model_options as ClaudeBaseOptions | undefined;
|
|
186
|
+
if (modelOptions && typeof modelOptions.max_tokens === 'number') {
|
|
187
|
+
return modelOptions.max_tokens;
|
|
188
|
+
}
|
|
189
|
+
let maxSupportedTokens = getClaudeMaxTokensLimit(option.model);
|
|
190
|
+
// Claude 3.7 supports up to 128k with a beta header; default to 64k when no budget is set.
|
|
191
|
+
if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) < 48000) {
|
|
192
|
+
maxSupportedTokens = 64000;
|
|
193
|
+
}
|
|
194
|
+
return maxSupportedTokens;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ============================================================================
|
|
198
|
+
// File / multimodal block helpers
|
|
199
|
+
// ============================================================================
|
|
200
|
+
|
|
201
|
+
async function collectFileBlocks(segment: PromptSegment, restrictedTypes: true): Promise<Array<TextBlockParam | ImageBlockParam>>;
|
|
202
|
+
async function collectFileBlocks(segment: PromptSegment, restrictedTypes?: false): Promise<ContentBlockParam[]>;
|
|
203
|
+
async function collectFileBlocks(segment: PromptSegment, restrictedTypes = false): Promise<ContentBlockParam[]> {
|
|
204
|
+
const contentBlocks: ContentBlockParam[] = [];
|
|
205
|
+
|
|
206
|
+
for (const file of segment.files || []) {
|
|
207
|
+
if (file.mime_type?.startsWith('image/')) {
|
|
208
|
+
const allowedTypes = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'];
|
|
209
|
+
if (!allowedTypes.includes(file.mime_type)) {
|
|
210
|
+
throw new Error(`Unsupported image type: ${file.mime_type}`);
|
|
211
|
+
}
|
|
212
|
+
const mimeType = String(file.mime_type) as 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp';
|
|
213
|
+
contentBlocks.push({
|
|
214
|
+
type: 'image',
|
|
215
|
+
source: {
|
|
216
|
+
type: 'base64',
|
|
217
|
+
data: await readStreamAsBase64(await file.getStream()),
|
|
218
|
+
media_type: mimeType,
|
|
219
|
+
},
|
|
220
|
+
} satisfies ImageBlockParam);
|
|
221
|
+
} else if (!restrictedTypes) {
|
|
222
|
+
if (file.mime_type === 'application/pdf') {
|
|
223
|
+
contentBlocks.push({
|
|
224
|
+
title: file.name,
|
|
225
|
+
type: 'document',
|
|
226
|
+
source: {
|
|
227
|
+
type: 'base64',
|
|
228
|
+
data: await readStreamAsBase64(await file.getStream()),
|
|
229
|
+
media_type: 'application/pdf',
|
|
230
|
+
},
|
|
231
|
+
} satisfies DocumentBlockParam);
|
|
232
|
+
} else if (file.mime_type?.startsWith('text/')) {
|
|
233
|
+
contentBlocks.push({
|
|
234
|
+
title: file.name,
|
|
235
|
+
type: 'document',
|
|
236
|
+
source: {
|
|
237
|
+
type: 'text',
|
|
238
|
+
data: await readStreamAsString(await file.getStream()),
|
|
239
|
+
media_type: 'text/plain',
|
|
240
|
+
},
|
|
241
|
+
} satisfies DocumentBlockParam);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return contentBlocks;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// ============================================================================
|
|
250
|
+
// Prompt formatting (PromptSegment[] → ClaudePrompt)
|
|
251
|
+
// ============================================================================
|
|
252
|
+
|
|
253
|
+
export async function formatClaudePrompt(segments: PromptSegment[], options: ExecutionOptions): Promise<ClaudePrompt> {
|
|
254
|
+
let system: TextBlockParam[] | undefined = segments
|
|
255
|
+
.filter((s) => s.role === PromptRole.system)
|
|
256
|
+
.map((s) => ({ text: s.content, type: 'text' as const }));
|
|
257
|
+
|
|
258
|
+
if (options.result_schema) {
|
|
259
|
+
const schemaText = options.tools && options.tools.length > 0
|
|
260
|
+
? 'When not calling tools, the answer must be a JSON object using the following JSON Schema:\n' + JSON.stringify(options.result_schema)
|
|
261
|
+
: 'The answer must be a JSON object using the following JSON Schema:\n' + JSON.stringify(options.result_schema);
|
|
262
|
+
system.push({ text: schemaText, type: 'text' as const });
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
let messages: MessageParam[] = [];
|
|
266
|
+
const safetyMessages: MessageParam[] = [];
|
|
267
|
+
|
|
268
|
+
for (const segment of segments) {
|
|
269
|
+
if (segment.role === PromptRole.system) continue;
|
|
270
|
+
|
|
271
|
+
if (segment.role === PromptRole.tool) {
|
|
272
|
+
if (!segment.tool_use_id) {
|
|
273
|
+
throw new Error('Tool prompt segment must have a tool use ID');
|
|
274
|
+
}
|
|
275
|
+
const contentBlocks: Array<TextBlockParam | ImageBlockParam> = [];
|
|
276
|
+
if (segment.content) {
|
|
277
|
+
contentBlocks.push({ type: 'text', text: segment.content } satisfies TextBlockParam);
|
|
278
|
+
}
|
|
279
|
+
contentBlocks.push(...(await collectFileBlocks(segment, true)));
|
|
280
|
+
messages.push({
|
|
281
|
+
role: 'user',
|
|
282
|
+
content: [{
|
|
283
|
+
type: 'tool_result',
|
|
284
|
+
tool_use_id: segment.tool_use_id,
|
|
285
|
+
content: contentBlocks,
|
|
286
|
+
} satisfies ToolResultBlockParam],
|
|
287
|
+
});
|
|
288
|
+
} else {
|
|
289
|
+
const contentBlocks: ContentBlockParam[] = [];
|
|
290
|
+
if (segment.content) {
|
|
291
|
+
contentBlocks.push({ type: 'text', text: segment.content } satisfies TextBlockParam);
|
|
292
|
+
}
|
|
293
|
+
contentBlocks.push(...(await collectFileBlocks(segment, false)));
|
|
294
|
+
if (contentBlocks.length === 0) continue;
|
|
295
|
+
|
|
296
|
+
const messageParam: MessageParam = {
|
|
297
|
+
role: segment.role === PromptRole.assistant ? 'assistant' : 'user',
|
|
298
|
+
content: contentBlocks,
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
if (segment.role === PromptRole.safety) {
|
|
302
|
+
safetyMessages.push(messageParam);
|
|
303
|
+
} else {
|
|
304
|
+
messages.push(messageParam);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
messages = messages.concat(safetyMessages);
|
|
310
|
+
if (system && system.length === 0) system = undefined;
|
|
311
|
+
|
|
312
|
+
return { messages, system };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// ============================================================================
|
|
316
|
+
// Conversation management
|
|
317
|
+
// ============================================================================
|
|
318
|
+
|
|
319
|
+
export function createPromptFromResponse(response: Message): ClaudePrompt {
|
|
320
|
+
return {
|
|
321
|
+
messages: [{ role: response.role, content: response.content }],
|
|
322
|
+
system: undefined,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function mergeConsecutiveUserMessages(messages: MessageParam[]): MessageParam[] {
|
|
327
|
+
if (messages.length === 0) return [];
|
|
328
|
+
|
|
329
|
+
const needsMerging = messages.some((msg, i) =>
|
|
330
|
+
i < messages.length - 1 && msg.role === 'user' && messages[i + 1].role === 'user'
|
|
331
|
+
);
|
|
332
|
+
if (!needsMerging) return messages;
|
|
333
|
+
|
|
334
|
+
const result: MessageParam[] = [];
|
|
335
|
+
let i = 0;
|
|
336
|
+
while (i < messages.length) {
|
|
337
|
+
const current = messages[i];
|
|
338
|
+
if (current.role === 'user') {
|
|
339
|
+
const mergedContent: MessageParam['content'] = [];
|
|
340
|
+
while (i < messages.length && messages[i].role === 'user') {
|
|
341
|
+
const userMsg = messages[i];
|
|
342
|
+
if (Array.isArray(userMsg.content)) {
|
|
343
|
+
mergedContent.push(...userMsg.content);
|
|
344
|
+
} else if (typeof userMsg.content === 'string') {
|
|
345
|
+
mergedContent.push({ type: 'text', text: userMsg.content });
|
|
346
|
+
}
|
|
347
|
+
i++;
|
|
348
|
+
}
|
|
349
|
+
result.push({ role: 'user', content: mergedContent });
|
|
350
|
+
} else {
|
|
351
|
+
result.push(current);
|
|
352
|
+
i++;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return result;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function sanitizeMessages(messages: MessageParam[]): MessageParam[] {
|
|
359
|
+
const result: MessageParam[] = [];
|
|
360
|
+
for (const message of messages) {
|
|
361
|
+
if (typeof message.content === 'string') {
|
|
362
|
+
if (message.content.trim()) result.push(message);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
const filteredContent = message.content.filter((block) => {
|
|
366
|
+
if (block.type === 'text') return block.text && block.text.trim().length > 0;
|
|
367
|
+
return true;
|
|
368
|
+
});
|
|
369
|
+
if (filteredContent.length > 0) {
|
|
370
|
+
result.push({ ...message, content: filteredContent });
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return result;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
export function fixOrphanedToolUse(messages: MessageParam[]): MessageParam[] {
|
|
377
|
+
if (messages.length < 2) return messages;
|
|
378
|
+
const result: MessageParam[] = [];
|
|
379
|
+
for (let i = 0; i < messages.length; i++) {
|
|
380
|
+
const current = messages[i];
|
|
381
|
+
result.push(current);
|
|
382
|
+
|
|
383
|
+
if (current.role === 'assistant' && Array.isArray(current.content)) {
|
|
384
|
+
const toolUseBlocks = current.content.filter(
|
|
385
|
+
(block): block is ContentBlockParam & { type: 'tool_use'; id: string; name: string } =>
|
|
386
|
+
block.type === 'tool_use'
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
if (toolUseBlocks.length > 0) {
|
|
390
|
+
const nextMessage = messages[i + 1];
|
|
391
|
+
|
|
392
|
+
if (nextMessage && nextMessage.role === 'user' && Array.isArray(nextMessage.content)) {
|
|
393
|
+
const toolResultIds = new Set(
|
|
394
|
+
nextMessage.content
|
|
395
|
+
.filter((block): block is ToolResultBlockParam => block.type === 'tool_result')
|
|
396
|
+
.map((block) => block.tool_use_id)
|
|
397
|
+
);
|
|
398
|
+
const orphaned = toolUseBlocks.filter((block) => !toolResultIds.has(block.id));
|
|
399
|
+
if (orphaned.length > 0) {
|
|
400
|
+
const syntheticResults: ToolResultBlockParam[] = orphaned.map((block) => ({
|
|
401
|
+
type: 'tool_result',
|
|
402
|
+
tool_use_id: block.id,
|
|
403
|
+
content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`,
|
|
404
|
+
}));
|
|
405
|
+
messages[i + 1] = { ...nextMessage, content: [...syntheticResults, ...nextMessage.content] };
|
|
406
|
+
}
|
|
407
|
+
} else if (nextMessage && nextMessage.role === 'user') {
|
|
408
|
+
const syntheticResults: ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
|
|
409
|
+
type: 'tool_result',
|
|
410
|
+
tool_use_id: block.id,
|
|
411
|
+
content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`,
|
|
412
|
+
}));
|
|
413
|
+
const textContent: TextBlockParam = typeof nextMessage.content === 'string'
|
|
414
|
+
? { type: 'text', text: nextMessage.content }
|
|
415
|
+
: { type: 'text', text: '' };
|
|
416
|
+
messages[i + 1] = { role: 'user', content: [...syntheticResults, textContent] };
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function updateClaudeConversation(conversation: ClaudePrompt | undefined | null, prompt: ClaudePrompt): ClaudePrompt {
|
|
425
|
+
const baseSystemMessages = conversation?.system || [];
|
|
426
|
+
const baseMessages = conversation?.messages || [];
|
|
427
|
+
const system = baseSystemMessages.concat(prompt.system || []);
|
|
428
|
+
const combined = sanitizeMessages(baseMessages.concat(prompt.messages || []));
|
|
429
|
+
const mergedMessages = mergeConsecutiveUserMessages(combined);
|
|
430
|
+
return {
|
|
431
|
+
messages: mergedMessages,
|
|
432
|
+
system: system.length > 0 ? system : undefined,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function claudeMessagesContainToolBlocks(messages: MessageParam[]): boolean {
|
|
437
|
+
for (const msg of messages) {
|
|
438
|
+
if (!Array.isArray(msg.content)) continue;
|
|
439
|
+
for (const block of msg.content) {
|
|
440
|
+
if (typeof block === 'object' && block !== null && 'type' in block) {
|
|
441
|
+
if (block.type === 'tool_use' || block.type === 'tool_result') return true;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function convertClaudeToolBlocksToText(messages: MessageParam[]): MessageParam[] {
|
|
449
|
+
return messages.map((msg) => {
|
|
450
|
+
if (!Array.isArray(msg.content)) return msg;
|
|
451
|
+
let hasToolBlocks = false;
|
|
452
|
+
for (const block of msg.content) {
|
|
453
|
+
if (typeof block === 'object' && block !== null && 'type' in block &&
|
|
454
|
+
(block.type === 'tool_use' || block.type === 'tool_result')) {
|
|
455
|
+
hasToolBlocks = true;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (!hasToolBlocks) return msg;
|
|
460
|
+
|
|
461
|
+
const newContent: MessageParam['content'] = [];
|
|
462
|
+
for (const block of msg.content) {
|
|
463
|
+
if (typeof block === 'string') { newContent.push(block); continue; }
|
|
464
|
+
if (block.type === 'tool_use') {
|
|
465
|
+
const inputStr = block.input ? JSON.stringify(block.input) : '';
|
|
466
|
+
const truncated = inputStr.length > 500 ? inputStr.substring(0, 500) + '...' : inputStr;
|
|
467
|
+
(newContent as Array<{ type: 'text'; text: string }>).push({ type: 'text', text: `[Tool call: ${block.name}(${truncated})]` });
|
|
468
|
+
} else if (block.type === 'tool_result') {
|
|
469
|
+
let resultStr = 'No content';
|
|
470
|
+
if (typeof block.content === 'string') {
|
|
471
|
+
resultStr = block.content.length > 500 ? block.content.substring(0, 500) + '...' : block.content;
|
|
472
|
+
} else if (Array.isArray(block.content)) {
|
|
473
|
+
const texts = block.content
|
|
474
|
+
.filter((c): c is { type: 'text'; text: string } => c.type === 'text')
|
|
475
|
+
.map((c) => (c.text.length > 500 ? c.text.substring(0, 500) + '...' : c.text));
|
|
476
|
+
resultStr = texts.join('\n') || 'No text content';
|
|
477
|
+
}
|
|
478
|
+
(newContent as Array<{ type: 'text'; text: string }>).push({ type: 'text', text: `[Tool result: ${resultStr}]` });
|
|
479
|
+
} else {
|
|
480
|
+
newContent.push(block as ContentBlockParam);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return { ...msg, content: newContent };
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ============================================================================
|
|
488
|
+
// Cache control stripping
|
|
489
|
+
// ============================================================================
|
|
490
|
+
|
|
491
|
+
function stripClaudeCacheControlFromBlock<T extends ContentBlockParam>(block: T): T {
|
|
492
|
+
if (typeof block === 'object' && block !== null && 'cache_control' in block) {
|
|
493
|
+
const { cache_control: _cc, ...rest } = block as T & { cache_control: unknown };
|
|
494
|
+
return rest as T;
|
|
495
|
+
}
|
|
496
|
+
return block;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function stripClaudeCacheControlFromMessages(messages: MessageParam[]): MessageParam[] {
|
|
500
|
+
return messages.map((msg) => {
|
|
501
|
+
if (!Array.isArray(msg.content)) return msg;
|
|
502
|
+
return { ...msg, content: msg.content.map(stripClaudeCacheControlFromBlock) };
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function stripClaudeCacheControlFromSystem(system?: TextBlockParam[]): TextBlockParam[] | undefined {
|
|
507
|
+
if (!system) return undefined;
|
|
508
|
+
return system.map(stripClaudeCacheControlFromBlock);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function stripClaudeCacheControlFromTools(
|
|
512
|
+
tools?: MessageCreateParamsBase['tools']
|
|
513
|
+
): MessageCreateParamsBase['tools'] | undefined {
|
|
514
|
+
if (!tools) return undefined;
|
|
515
|
+
return tools.map((tool) => {
|
|
516
|
+
if ('cache_control' in tool) {
|
|
517
|
+
const { cache_control: _cc, ...rest } = tool as ClaudeTool & { cache_control: unknown };
|
|
518
|
+
return rest as ClaudeTool;
|
|
519
|
+
}
|
|
520
|
+
return tool;
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// ============================================================================
|
|
525
|
+
// Payload builder
|
|
526
|
+
// ============================================================================
|
|
527
|
+
|
|
528
|
+
export function getClaudePayload(
|
|
529
|
+
options: ExecutionOptions,
|
|
530
|
+
prompt: ClaudePrompt
|
|
531
|
+
): { payload: MessageCreateParamsBase; requestOptions: RequestOptions | undefined } {
|
|
532
|
+
const modelName = options.model;
|
|
533
|
+
const model_options = options.model_options as ClaudeBaseOptions | undefined;
|
|
534
|
+
|
|
535
|
+
let requestOptions: RequestOptions | undefined;
|
|
536
|
+
if (modelName.includes('claude-3-7-sonnet') &&
|
|
537
|
+
((model_options?.max_tokens ?? 0) > 64000 || (model_options?.thinking_budget_tokens ?? 0) > 64000)) {
|
|
538
|
+
requestOptions = { headers: { 'anthropic-beta': 'output-128k-2025-02-19' } };
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const fixedMessages = fixOrphanedToolUse(prompt.messages);
|
|
542
|
+
let sanitizedMessages = sanitizeMessages(fixedMessages);
|
|
543
|
+
|
|
544
|
+
if (options.tools) {
|
|
545
|
+
for (const tool of options.tools) {
|
|
546
|
+
if (tool.input_schema.type !== 'object') {
|
|
547
|
+
throw new Error(`Tool "${tool.name}" has invalid input_schema.type: expected "object", got "${tool.input_schema.type}"`);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const hasTools = options.tools && options.tools.length > 0;
|
|
553
|
+
if (!hasTools && claudeMessagesContainToolBlocks(sanitizedMessages)) {
|
|
554
|
+
sanitizedMessages = convertClaudeToolBlocksToText(sanitizedMessages);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
sanitizedMessages = stripClaudeCacheControlFromMessages(sanitizedMessages);
|
|
558
|
+
const sanitizedSystem = stripClaudeCacheControlFromSystem(prompt.system);
|
|
559
|
+
const sanitizedTools = hasTools
|
|
560
|
+
? stripClaudeCacheControlFromTools(options.tools as MessageCreateParamsBase['tools'])
|
|
561
|
+
: undefined;
|
|
562
|
+
|
|
563
|
+
const cacheEnabled = model_options?.cache_enabled === true;
|
|
564
|
+
if (cacheEnabled) {
|
|
565
|
+
const cacheTtl = model_options?.cache_ttl as '5m' | '1h' | undefined;
|
|
566
|
+
const cacheControl = { type: 'ephemeral' as const, ...(cacheTtl && { ttl: cacheTtl }) };
|
|
567
|
+
|
|
568
|
+
if (sanitizedSystem && sanitizedSystem.length > 0) {
|
|
569
|
+
const lastBlock = sanitizedSystem[sanitizedSystem.length - 1] as TextBlockParam & { cache_control?: unknown };
|
|
570
|
+
lastBlock.cache_control = cacheControl;
|
|
571
|
+
}
|
|
572
|
+
if (sanitizedTools && sanitizedTools.length > 0) {
|
|
573
|
+
const lastTool = sanitizedTools[sanitizedTools.length - 1] as ClaudeTool & { cache_control?: unknown };
|
|
574
|
+
lastTool.cache_control = cacheControl;
|
|
575
|
+
}
|
|
576
|
+
if (sanitizedMessages.length >= 4) {
|
|
577
|
+
const pivotMsg = sanitizedMessages[sanitizedMessages.length - 2];
|
|
578
|
+
if (Array.isArray(pivotMsg.content) && pivotMsg.content.length > 0) {
|
|
579
|
+
const lastBlock = pivotMsg.content[pivotMsg.content.length - 1];
|
|
580
|
+
if (typeof lastBlock === 'object' && lastBlock !== null && 'type' in lastBlock &&
|
|
581
|
+
lastBlock.type !== 'thinking' && lastBlock.type !== 'redacted_thinking') {
|
|
582
|
+
(lastBlock as TextBlockParam).cache_control = cacheControl;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const { thinking, outputConfig, hasSamplingRestriction } = resolveClaudeThinking(modelName, model_options as Parameters<typeof resolveClaudeThinking>[1]);
|
|
589
|
+
|
|
590
|
+
const payload: MessageCreateParamsBase = {
|
|
591
|
+
messages: sanitizedMessages,
|
|
592
|
+
system: sanitizedSystem,
|
|
593
|
+
tools: sanitizedTools,
|
|
594
|
+
temperature: hasSamplingRestriction ? undefined : model_options?.temperature,
|
|
595
|
+
model: modelName,
|
|
596
|
+
max_tokens: claudeMaxTokens(options),
|
|
597
|
+
top_p: hasSamplingRestriction ? undefined : (model_options?.temperature != null ? undefined : model_options?.top_p),
|
|
598
|
+
top_k: hasSamplingRestriction ? undefined : model_options?.top_k,
|
|
599
|
+
stop_sequences: model_options?.stop_sequence,
|
|
600
|
+
thinking,
|
|
601
|
+
stream: true,
|
|
602
|
+
...(outputConfig && { output_config: outputConfig }),
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
return { payload, requestOptions };
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// ============================================================================
|
|
609
|
+
// Streaming conversation builder (called after stream completes)
|
|
610
|
+
// ============================================================================
|
|
611
|
+
|
|
612
|
+
export function buildClaudeStreamingConversation(
|
|
613
|
+
prompt: ClaudePrompt,
|
|
614
|
+
result: unknown[],
|
|
615
|
+
toolUse: unknown[] | undefined,
|
|
616
|
+
options: ExecutionOptions
|
|
617
|
+
): ClaudePrompt {
|
|
618
|
+
const completionResults = result as CompletionResult[];
|
|
619
|
+
const text = completionResults
|
|
620
|
+
.filter((r) => r.type === 'text')
|
|
621
|
+
.map((r) => r.value as string)
|
|
622
|
+
.join('');
|
|
623
|
+
|
|
624
|
+
let conversation = updateClaudeConversation(options.conversation as ClaudePrompt | undefined, prompt);
|
|
625
|
+
|
|
626
|
+
if (text) {
|
|
627
|
+
const assistantMsg: MessageParam = { role: 'assistant', content: text };
|
|
628
|
+
conversation = updateClaudeConversation(conversation, { messages: [assistantMsg] });
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
if (toolUse && toolUse.length > 0) {
|
|
632
|
+
const toolBlocks: ContentBlockParam[] = (toolUse as ToolUse[]).map((t) => ({
|
|
633
|
+
type: 'tool_use' as const,
|
|
634
|
+
id: t.id,
|
|
635
|
+
name: t.tool_name,
|
|
636
|
+
input: t.tool_input ?? {},
|
|
637
|
+
}));
|
|
638
|
+
const assistantToolMsg: MessageParam = { role: 'assistant', content: toolBlocks };
|
|
639
|
+
conversation = updateClaudeConversation(conversation, { messages: [assistantToolMsg] });
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
conversation = incrementConversationTurn(conversation) as ClaudePrompt;
|
|
643
|
+
const currentTurn = getConversationMeta(conversation).turnNumber;
|
|
644
|
+
const stripOptions = {
|
|
645
|
+
keepForTurns: options.stripImagesAfterTurns ?? Infinity,
|
|
646
|
+
currentTurn,
|
|
647
|
+
textMaxTokens: options.stripTextMaxTokens,
|
|
648
|
+
};
|
|
649
|
+
let processed = stripBase64ImagesFromConversation(conversation, stripOptions);
|
|
650
|
+
processed = truncateLargeTextInConversation(processed, stripOptions);
|
|
651
|
+
processed = stripHeartbeatsFromConversation(processed, {
|
|
652
|
+
keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
|
|
653
|
+
currentTurn,
|
|
654
|
+
});
|
|
655
|
+
return processed as ClaudePrompt;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// ============================================================================
|
|
659
|
+
// Execution helpers (standalone, take a client parameter)
|
|
660
|
+
// ============================================================================
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Execute a non-streaming Claude completion.
|
|
664
|
+
* Works with any Anthropic-compatible client (Anthropic or AnthropicVertex).
|
|
665
|
+
*/
|
|
666
|
+
export async function executeClaudeCompletion(
|
|
667
|
+
client: Anthropic | AnthropicVertex,
|
|
668
|
+
prompt: ClaudePrompt,
|
|
669
|
+
options: ExecutionOptions,
|
|
670
|
+
): Promise<Completion> {
|
|
671
|
+
const model_options = options.model_options as ClaudeBaseOptions | undefined;
|
|
672
|
+
|
|
673
|
+
let conversation = updateClaudeConversation(options.conversation as ClaudePrompt | undefined, prompt);
|
|
674
|
+
|
|
675
|
+
const { payload, requestOptions } = getClaudePayload(options, conversation);
|
|
676
|
+
|
|
677
|
+
const result: Message = await client.messages.stream(payload, requestOptions).finalMessage();
|
|
678
|
+
|
|
679
|
+
const includeThoughts = model_options?.include_thoughts ?? false;
|
|
680
|
+
const text = collectAllTextContent(result.content, includeThoughts);
|
|
681
|
+
const tool_use = collectClaudeTools(result.content);
|
|
682
|
+
|
|
683
|
+
conversation = updateClaudeConversation(conversation, createPromptFromResponse(result));
|
|
684
|
+
conversation = incrementConversationTurn(conversation) as ClaudePrompt;
|
|
685
|
+
const currentTurn = getConversationMeta(conversation).turnNumber;
|
|
686
|
+
const stripOpts = {
|
|
687
|
+
keepForTurns: options.stripImagesAfterTurns ?? Infinity,
|
|
688
|
+
currentTurn,
|
|
689
|
+
textMaxTokens: options.stripTextMaxTokens,
|
|
690
|
+
};
|
|
691
|
+
let processedConversation = stripBase64ImagesFromConversation(conversation, stripOpts);
|
|
692
|
+
processedConversation = truncateLargeTextInConversation(processedConversation, stripOpts);
|
|
693
|
+
processedConversation = stripHeartbeatsFromConversation(processedConversation, {
|
|
694
|
+
keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
|
|
695
|
+
currentTurn,
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
return {
|
|
699
|
+
result: text ? [{ type: 'text', value: text }] : [{ type: 'text', value: '' }],
|
|
700
|
+
tool_use,
|
|
701
|
+
token_usage: anthropicUsageToTokenUsage(result.usage),
|
|
702
|
+
finish_reason: tool_use ? 'tool_use' : claudeFinishReason(result?.stop_reason ?? ''),
|
|
703
|
+
conversation: processedConversation,
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* Execute a streaming Claude completion.
|
|
709
|
+
* Works with any Anthropic-compatible client (Anthropic or AnthropicVertex).
|
|
710
|
+
*/
|
|
711
|
+
export async function streamClaudeCompletion(
|
|
712
|
+
client: Anthropic | AnthropicVertex,
|
|
713
|
+
prompt: ClaudePrompt,
|
|
714
|
+
options: ExecutionOptions,
|
|
715
|
+
): Promise<AsyncIterable<CompletionChunkObject>> {
|
|
716
|
+
const model_options = options.model_options as ClaudeBaseOptions | undefined;
|
|
717
|
+
const conversation = updateClaudeConversation(options.conversation as ClaudePrompt | undefined, prompt);
|
|
718
|
+
|
|
719
|
+
const { payload, requestOptions } = getClaudePayload(options, conversation);
|
|
720
|
+
const streamingPayload: MessageStreamParams = { ...payload, stream: true };
|
|
721
|
+
|
|
722
|
+
const response_stream = await client.messages.stream(streamingPayload, requestOptions);
|
|
723
|
+
|
|
724
|
+
let currentToolUse: { id: string; name: string; inputJson: string } | null = null;
|
|
725
|
+
let pendingSpacing = false;
|
|
726
|
+
|
|
727
|
+
const stream = asyncMap(response_stream, async (streamEvent: RawMessageStreamEvent) => {
|
|
728
|
+
switch (streamEvent.type) {
|
|
729
|
+
case 'message_start':
|
|
730
|
+
return {
|
|
731
|
+
result: [{ type: 'text', value: '' }],
|
|
732
|
+
token_usage: anthropicUsageToTokenUsage(streamEvent.message.usage as AnthropicUsageLike),
|
|
733
|
+
} satisfies CompletionChunkObject;
|
|
734
|
+
case 'message_delta':
|
|
735
|
+
return {
|
|
736
|
+
result: [{ type: 'text', value: '' }],
|
|
737
|
+
token_usage: { result: streamEvent.usage.output_tokens },
|
|
738
|
+
finish_reason: claudeFinishReason(streamEvent.delta.stop_reason ?? undefined),
|
|
739
|
+
} satisfies CompletionChunkObject;
|
|
740
|
+
case 'content_block_start':
|
|
741
|
+
if (streamEvent.content_block.type === 'tool_use') {
|
|
742
|
+
currentToolUse = { id: streamEvent.content_block.id, name: streamEvent.content_block.name, inputJson: '' };
|
|
743
|
+
return {
|
|
744
|
+
result: [],
|
|
745
|
+
tool_use: [{
|
|
746
|
+
id: streamEvent.content_block.id,
|
|
747
|
+
tool_name: streamEvent.content_block.name,
|
|
748
|
+
tool_input: '' as unknown as JSONObject,
|
|
749
|
+
}],
|
|
750
|
+
} satisfies CompletionChunkObject;
|
|
751
|
+
}
|
|
752
|
+
if (streamEvent.content_block.type === 'redacted_thinking' && model_options?.include_thoughts) {
|
|
753
|
+
return {
|
|
754
|
+
result: [{ type: 'text', value: `[Redacted thinking: ${streamEvent.content_block.data}]` }],
|
|
755
|
+
} satisfies CompletionChunkObject;
|
|
756
|
+
}
|
|
757
|
+
break;
|
|
758
|
+
case 'content_block_delta':
|
|
759
|
+
switch (streamEvent.delta.type) {
|
|
760
|
+
case 'text_delta': {
|
|
761
|
+
const prefix = pendingSpacing ? '\n\n' : '';
|
|
762
|
+
pendingSpacing = false;
|
|
763
|
+
return {
|
|
764
|
+
result: streamEvent.delta.text ? [{ type: 'text', value: prefix + streamEvent.delta.text }] : [],
|
|
765
|
+
} satisfies CompletionChunkObject;
|
|
766
|
+
}
|
|
767
|
+
case 'input_json_delta':
|
|
768
|
+
if (currentToolUse && streamEvent.delta.partial_json) {
|
|
769
|
+
return {
|
|
770
|
+
result: [],
|
|
771
|
+
tool_use: [{
|
|
772
|
+
id: currentToolUse.id,
|
|
773
|
+
tool_name: '',
|
|
774
|
+
tool_input: streamEvent.delta.partial_json as unknown as JSONObject,
|
|
775
|
+
}],
|
|
776
|
+
} satisfies CompletionChunkObject;
|
|
777
|
+
}
|
|
778
|
+
break;
|
|
779
|
+
case 'thinking_delta':
|
|
780
|
+
if (model_options?.include_thoughts) {
|
|
781
|
+
return {
|
|
782
|
+
result: streamEvent.delta.thinking ? [{ type: 'text', value: streamEvent.delta.thinking }] : [],
|
|
783
|
+
} satisfies CompletionChunkObject;
|
|
784
|
+
}
|
|
785
|
+
break;
|
|
786
|
+
case 'signature_delta':
|
|
787
|
+
if (model_options?.include_thoughts) {
|
|
788
|
+
pendingSpacing = true;
|
|
789
|
+
}
|
|
790
|
+
break;
|
|
791
|
+
}
|
|
792
|
+
break;
|
|
793
|
+
case 'content_block_stop':
|
|
794
|
+
if (currentToolUse) {
|
|
795
|
+
currentToolUse = null;
|
|
796
|
+
pendingSpacing = false;
|
|
797
|
+
}
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
return { result: [] } satisfies CompletionChunkObject;
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
return stream;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// ============================================================================
|
|
808
|
+
// Error handling
|
|
809
|
+
// ============================================================================
|
|
810
|
+
|
|
811
|
+
export function formatAnthropicLlumiverseError(error: unknown, context: LlumiverseErrorContext): LlumiverseError {
|
|
812
|
+
if (error instanceof AnthropicError && !(error instanceof APIError)) {
|
|
813
|
+
// Client-side SDK error (e.g. "Streaming is required for operations that may take longer than 10 minutes").
|
|
814
|
+
// These are structural/configuration errors — retrying will never succeed.
|
|
815
|
+
const errorName = error.constructor?.name || 'AnthropicError';
|
|
816
|
+
return new LlumiverseError(`[${context.provider}] ${error.message}`, false, context, error, undefined, errorName);
|
|
817
|
+
}
|
|
818
|
+
if (!(error instanceof APIError)) {
|
|
819
|
+
// Not an Anthropic error — rethrow for default handling
|
|
820
|
+
throw error;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
const apiError = error as APIError;
|
|
824
|
+
const httpStatusCode = apiError.status;
|
|
825
|
+
let message = apiError.message || String(error);
|
|
826
|
+
let errorType: string | undefined;
|
|
827
|
+
|
|
828
|
+
if (apiError.error && typeof apiError.error === 'object') {
|
|
829
|
+
const nested = apiError.error as Record<string, unknown>;
|
|
830
|
+
if (nested['error'] && typeof nested['error'] === 'object') {
|
|
831
|
+
const innerError = nested['error'] as Record<string, unknown>;
|
|
832
|
+
errorType = innerError['type'] as string | undefined;
|
|
833
|
+
if (typeof innerError['message'] === 'string') {
|
|
834
|
+
message = innerError['message'];
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
let userMessage = message;
|
|
840
|
+
if (httpStatusCode) userMessage = `[${httpStatusCode}] ${userMessage}`;
|
|
841
|
+
if (errorType && errorType !== 'error') userMessage = `${errorType}: ${userMessage}`;
|
|
842
|
+
if (apiError.requestID) userMessage += ` (Request ID: ${apiError.requestID})`;
|
|
843
|
+
|
|
844
|
+
const retryable = isClaudeErrorRetryable(error, httpStatusCode, errorType, apiError.headers ?? undefined);
|
|
845
|
+
const errorName = error.constructor?.name || 'AnthropicError';
|
|
846
|
+
|
|
847
|
+
return new LlumiverseError(`[${context.provider}] ${userMessage}`, retryable, context, error, httpStatusCode, errorName);
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
export function isClaudeErrorRetryable(
|
|
851
|
+
error: unknown,
|
|
852
|
+
httpStatusCode: number | undefined,
|
|
853
|
+
errorType: string | undefined,
|
|
854
|
+
headers?: Headers | undefined,
|
|
855
|
+
): boolean | undefined {
|
|
856
|
+
// Honour the server's explicit retry directive first (mirrors SDK shouldRetry logic).
|
|
857
|
+
const shouldRetryHeader = headers?.get('x-should-retry');
|
|
858
|
+
if (shouldRetryHeader === 'true') return true;
|
|
859
|
+
if (shouldRetryHeader === 'false') return false;
|
|
860
|
+
|
|
861
|
+
if (error instanceof APIUserAbortError) return false;
|
|
862
|
+
if (error instanceof RateLimitError) return true;
|
|
863
|
+
if (error instanceof InternalServerError) return true;
|
|
864
|
+
if (error instanceof APIConnectionTimeoutError) return true;
|
|
865
|
+
if (error instanceof BadRequestError) return false;
|
|
866
|
+
if (error instanceof AuthenticationError) return false;
|
|
867
|
+
if (error instanceof PermissionDeniedError) return false;
|
|
868
|
+
if (error instanceof NotFoundError) return false;
|
|
869
|
+
if (error instanceof ConflictError) return true; // SDK retries 409 (lock timeouts)
|
|
870
|
+
if (error instanceof UnprocessableEntityError) return false;
|
|
871
|
+
if (errorType === 'invalid_request_error') return false;
|
|
872
|
+
if (httpStatusCode !== undefined) {
|
|
873
|
+
if (httpStatusCode === 429 || httpStatusCode === 408 || httpStatusCode === 529) return true;
|
|
874
|
+
if (httpStatusCode >= 500 && httpStatusCode < 600) return true;
|
|
875
|
+
if (httpStatusCode >= 400 && httpStatusCode < 500) return false;
|
|
876
|
+
}
|
|
877
|
+
if (error instanceof APIConnectionError && !(error instanceof APIConnectionTimeoutError)) return true;
|
|
878
|
+
return undefined;
|
|
879
|
+
}
|