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