@llumiverse/drivers 1.1.1-dev.20260505.151157Z → 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.
Files changed (49) hide show
  1. package/lib/cjs/anthropic/index.js +64 -0
  2. package/lib/cjs/anthropic/index.js.map +1 -0
  3. package/lib/cjs/index.js +1 -0
  4. package/lib/cjs/index.js.map +1 -1
  5. package/lib/cjs/openai/index.js +12 -6
  6. package/lib/cjs/openai/index.js.map +1 -1
  7. package/lib/cjs/shared/claude-messages.js +737 -0
  8. package/lib/cjs/shared/claude-messages.js.map +1 -0
  9. package/lib/cjs/vertexai/index.js.map +1 -1
  10. package/lib/cjs/vertexai/models/claude.js +27 -872
  11. package/lib/cjs/vertexai/models/claude.js.map +1 -1
  12. package/lib/cjs/vertexai/models/gemini.js +18 -12
  13. package/lib/cjs/vertexai/models/gemini.js.map +1 -1
  14. package/lib/esm/anthropic/index.js +57 -0
  15. package/lib/esm/anthropic/index.js.map +1 -0
  16. package/lib/esm/index.js +1 -0
  17. package/lib/esm/index.js.map +1 -1
  18. package/lib/esm/openai/index.js +12 -7
  19. package/lib/esm/openai/index.js.map +1 -1
  20. package/lib/esm/shared/claude-messages.js +716 -0
  21. package/lib/esm/shared/claude-messages.js.map +1 -0
  22. package/lib/esm/vertexai/index.js.map +1 -1
  23. package/lib/esm/vertexai/models/claude.js +27 -865
  24. package/lib/esm/vertexai/models/claude.js.map +1 -1
  25. package/lib/esm/vertexai/models/gemini.js +18 -12
  26. package/lib/esm/vertexai/models/gemini.js.map +1 -1
  27. package/lib/types/anthropic/index.d.ts +21 -0
  28. package/lib/types/anthropic/index.d.ts.map +1 -0
  29. package/lib/types/index.d.ts +1 -0
  30. package/lib/types/index.d.ts.map +1 -1
  31. package/lib/types/openai/index.d.ts +1 -0
  32. package/lib/types/openai/index.d.ts.map +1 -1
  33. package/lib/types/shared/claude-messages.d.ts +75 -0
  34. package/lib/types/shared/claude-messages.d.ts.map +1 -0
  35. package/lib/types/vertexai/index.d.ts +4 -4
  36. package/lib/types/vertexai/index.d.ts.map +1 -1
  37. package/lib/types/vertexai/models/claude.d.ts +3 -106
  38. package/lib/types/vertexai/models/claude.d.ts.map +1 -1
  39. package/lib/types/vertexai/models/gemini.d.ts +1 -1
  40. package/lib/types/vertexai/models/gemini.d.ts.map +1 -1
  41. package/package.json +7 -6
  42. package/src/anthropic/index.ts +104 -0
  43. package/src/index.ts +1 -0
  44. package/src/openai/index.ts +13 -8
  45. package/src/shared/claude-messages.ts +879 -0
  46. package/src/vertexai/index.ts +18 -19
  47. package/src/vertexai/models/claude-error-handling.test.ts +3 -3
  48. package/src/vertexai/models/claude.ts +44 -1016
  49. package/src/vertexai/models/gemini.ts +27 -14
@@ -1,17 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ClaudeModelDefinition = exports.NON_GLOBAL_ANTHROPIC_MODELS = exports.ANTHROPIC_REGIONS = void 0;
4
- exports.collectTools = collectTools;
5
- exports.mergeConsecutiveUserMessages = mergeConsecutiveUserMessages;
6
- exports.updateConversation = updateConversation;
7
- exports.sanitizeMessages = sanitizeMessages;
8
- exports.fixOrphanedToolUse = fixOrphanedToolUse;
9
- exports.claudeMessagesContainToolBlocks = claudeMessagesContainToolBlocks;
10
- exports.convertClaudeToolBlocksToText = convertClaudeToolBlocksToText;
11
- const error_1 = require("@anthropic-ai/sdk/error");
12
4
  const core_1 = require("@llumiverse/core");
13
- const async_1 = require("@llumiverse/core/async");
14
- const claude_thinking_js_1 = require("../../shared/claude-thinking.js");
5
+ const claude_messages_js_1 = require("../../shared/claude-messages.js");
15
6
  exports.ANTHROPIC_REGIONS = {
16
7
  us: "us-east5",
17
8
  europe: "europe-west1",
@@ -21,123 +12,18 @@ exports.NON_GLOBAL_ANTHROPIC_MODELS = [
21
12
  "claude-3-5",
22
13
  "claude-3",
23
14
  ];
24
- function anthropicUsageToTokenUsage(usage) {
25
- const cacheRead = usage.cache_read_input_tokens ?? 0;
26
- const cacheWrite = usage.cache_creation_input_tokens ?? 0;
27
- return {
28
- prompt_new: usage.input_tokens,
29
- prompt: usage.input_tokens + cacheRead + cacheWrite,
30
- result: usage.output_tokens,
31
- total: usage.input_tokens + usage.output_tokens + cacheRead + cacheWrite,
32
- prompt_cached: usage.cache_read_input_tokens ?? undefined,
33
- prompt_cache_write: usage.cache_creation_input_tokens ?? undefined,
34
- };
35
- }
36
- function claudeFinishReason(reason) {
37
- if (!reason)
38
- return undefined;
39
- switch (reason) {
40
- case 'end_turn': return "stop";
41
- case 'max_tokens': return "length";
42
- default: return reason; //stop_sequence
43
- }
44
- }
45
- function collectTools(content) {
46
- const out = [];
47
- for (const block of content) {
48
- if (block.type === "tool_use") {
49
- out.push({
50
- id: block.id,
51
- tool_name: block.name,
52
- tool_input: block.input,
53
- });
54
- }
55
- }
56
- return out.length > 0 ? out : undefined;
57
- }
58
- function collectAllTextContent(content, includeThoughts = false) {
59
- const textParts = [];
60
- // First pass: collect thinking blocks
61
- if (includeThoughts) {
62
- for (const block of content) {
63
- if (block.type === 'thinking' && block.thinking) {
64
- textParts.push(block.thinking);
65
- }
66
- else if (block.type === 'redacted_thinking' && block.data) {
67
- textParts.push(`[Redacted thinking: ${block.data}]`);
68
- }
69
- }
70
- if (textParts.length > 0) {
71
- textParts.push(''); // Create a new line after thinking blocks
72
- }
73
- }
74
- // Second pass: collect text blocks
75
- for (const block of content) {
76
- if (block.type === 'text' && block.text) {
77
- textParts.push(block.text);
78
- }
79
- }
80
- return textParts.join('\n');
81
- }
82
- //Used to get a max_token value when not specified in the model options. Claude requires it to be set.
83
- function maxToken(option) {
84
- const modelOptions = option.model_options;
85
- if (modelOptions && typeof modelOptions.max_tokens === "number") {
86
- return modelOptions.max_tokens;
87
- }
88
- else {
89
- let maxSupportedTokens = (0, core_1.getMaxTokensLimitVertexAi)(option.model);
90
- // Fallback to the default max tokens limit for the model
91
- if (option.model.includes('claude-3-7-sonnet') && (modelOptions?.thinking_budget_tokens ?? 0) < 48000) {
92
- maxSupportedTokens = 64000; // Claude 3.7 can go up to 128k with a beta header, but when no max tokens is specified, we default to 64k.
93
- }
94
- return maxSupportedTokens;
95
- }
96
- }
97
- async function collectFileBlocks(segment, restrictedTypes = false) {
98
- const contentBlocks = [];
99
- for (const file of segment.files || []) {
100
- if (file.mime_type?.startsWith("image/")) {
101
- const allowedTypes = ["image/png", "image/jpeg", "image/gif", "image/webp"];
102
- if (!allowedTypes.includes(file.mime_type)) {
103
- throw new Error(`Unsupported image type: ${file.mime_type}`);
104
- }
105
- const mimeType = String(file.mime_type);
106
- contentBlocks.push({
107
- type: 'image',
108
- source: {
109
- type: 'base64',
110
- data: await (0, core_1.readStreamAsBase64)(await file.getStream()),
111
- media_type: mimeType
112
- }
113
- });
114
- }
115
- else if (!restrictedTypes) {
116
- if (file.mime_type === "application/pdf") {
117
- contentBlocks.push({
118
- title: file.name,
119
- type: 'document',
120
- source: {
121
- type: 'base64',
122
- data: await (0, core_1.readStreamAsBase64)(await file.getStream()),
123
- media_type: 'application/pdf'
124
- }
125
- });
126
- }
127
- else if (file.mime_type?.startsWith("text/")) {
128
- contentBlocks.push({
129
- title: file.name,
130
- type: 'document',
131
- source: {
132
- type: 'text',
133
- data: await (0, core_1.readStreamAsString)(await file.getStream()),
134
- media_type: 'text/plain'
135
- }
136
- });
137
- }
138
- }
15
+ /**
16
+ * Parse a VertexAI model path (e.g. "locations/us-east5/claude-3-5-sonnet") into
17
+ * its region and model name components.
18
+ */
19
+ function resolveVertexAIModelPath(options) {
20
+ const splits = options.model.split("/");
21
+ let region;
22
+ if (splits[0] === "locations" && splits.length >= 2) {
23
+ region = splits[1];
139
24
  }
140
- return contentBlocks;
25
+ const modelName = splits[splits.length - 1];
26
+ return { modelName, region, options: { ...options, model: modelName } };
141
27
  }
142
28
  class ClaudeModelDefinition {
143
29
  model;
@@ -151,767 +37,36 @@ class ClaudeModelDefinition {
151
37
  };
152
38
  }
153
39
  async createPrompt(_driver, segments, options) {
154
- // Convert the prompt to the format expected by the Claude API
155
- let system = segments
156
- .filter(segment => segment.role === core_1.PromptRole.system)
157
- .map(segment => ({
158
- text: segment.content,
159
- type: 'text'
160
- }));
161
- if (options.result_schema) {
162
- let schemaText = '';
163
- if (options.tools && options.tools.length > 0) {
164
- schemaText = "When not calling tools, the answer must be a JSON object using the following JSON Schema:\n" + JSON.stringify(options.result_schema);
165
- }
166
- else {
167
- schemaText = "The answer must be a JSON object using the following JSON Schema:\n" + JSON.stringify(options.result_schema);
168
- }
169
- const schemaSegments = {
170
- text: schemaText,
171
- type: 'text'
172
- };
173
- system.push(schemaSegments);
174
- }
175
- let messages = [];
176
- const safetyMessages = [];
177
- for (const segment of segments) {
178
- if (segment.role === core_1.PromptRole.system) {
179
- continue;
180
- }
181
- if (segment.role === core_1.PromptRole.tool) {
182
- if (!segment.tool_use_id) {
183
- throw new Error("Tool prompt segment must have a tool use ID");
184
- }
185
- // Build content blocks for tool results (restricted types)
186
- const contentBlocks = [];
187
- if (segment.content) {
188
- contentBlocks.push({
189
- type: 'text',
190
- text: segment.content
191
- });
192
- }
193
- // Collect file blocks with type safety
194
- const fileBlocks = await collectFileBlocks(segment, true);
195
- contentBlocks.push(...fileBlocks);
196
- messages.push({
197
- role: 'user',
198
- content: [{
199
- type: 'tool_result',
200
- tool_use_id: segment.tool_use_id,
201
- content: contentBlocks,
202
- }]
203
- });
204
- }
205
- else {
206
- // Build content blocks for regular messages (all types allowed)
207
- const contentBlocks = [];
208
- if (segment.content) {
209
- contentBlocks.push({
210
- type: 'text',
211
- text: segment.content
212
- });
213
- }
214
- // Collect file blocks without restrictions
215
- const fileBlocks = await collectFileBlocks(segment, false);
216
- contentBlocks.push(...fileBlocks);
217
- if (contentBlocks.length === 0) {
218
- continue; // skip empty segments
219
- }
220
- const messageParam = {
221
- role: segment.role === core_1.PromptRole.assistant ? 'assistant' : 'user',
222
- content: contentBlocks
223
- };
224
- if (segment.role === core_1.PromptRole.safety) {
225
- safetyMessages.push(messageParam);
226
- }
227
- else {
228
- messages.push(messageParam);
229
- }
230
- }
231
- }
232
- messages = messages.concat(safetyMessages);
233
- if (system && system.length === 0) {
234
- system = undefined; // If system is empty, set to undefined
235
- }
236
- return {
237
- messages: messages,
238
- system: system
239
- };
40
+ return (0, claude_messages_js_1.formatClaudePrompt)(segments, options);
240
41
  }
241
42
  async requestTextCompletion(driver, prompt, options) {
242
- const splits = options.model.split("/");
243
- let region = undefined;
244
- if (splits[0] === "locations" && splits.length >= 2) {
245
- region = splits[1];
246
- }
247
- const modelName = splits[splits.length - 1];
248
- options = { ...options, model: modelName };
43
+ const { region, options: resolvedOptions } = resolveVertexAIModelPath(options);
249
44
  const client = await driver.getAnthropicClient(region);
250
- const model_options = options.model_options;
45
+ const model_options = resolvedOptions.model_options;
251
46
  if (model_options?._option_id !== undefined &&
252
47
  model_options?._option_id !== "vertexai-claude" &&
253
48
  model_options?._option_id !== "text-fallback") {
254
- driver.logger.debug({ options: options.model_options }, "Unexpected option id");
49
+ driver.logger.debug({ options: resolvedOptions.model_options }, "Unexpected option id");
255
50
  }
256
- let conversation = updateConversation(options.conversation, prompt);
257
- const { payload, requestOptions } = getClaudePayload(options, conversation);
258
- // disable streaming, the create function is overloaded so payload type matters.
259
- const nonStreamingPayload = { ...payload, stream: false };
260
- const result = await client.messages.create(nonStreamingPayload, requestOptions);
261
- // Use the new function to collect text content, including thinking if enabled
262
- const includeThoughts = model_options?.include_thoughts ?? false;
263
- const text = collectAllTextContent(result.content, includeThoughts);
264
- const tool_use = collectTools(result.content);
265
- conversation = updateConversation(conversation, createPromptFromResponse(result));
266
- // Increment turn counter and apply stripping (same pattern as other drivers)
267
- conversation = (0, core_1.incrementConversationTurn)(conversation);
268
- const currentTurn = (0, core_1.getConversationMeta)(conversation).turnNumber;
269
- const stripOptions = {
270
- keepForTurns: options.stripImagesAfterTurns ?? Infinity,
271
- currentTurn,
272
- textMaxTokens: options.stripTextMaxTokens,
273
- };
274
- let processedConversation = (0, core_1.stripBase64ImagesFromConversation)(conversation, stripOptions);
275
- processedConversation = (0, core_1.truncateLargeTextInConversation)(processedConversation, stripOptions);
276
- processedConversation = (0, core_1.stripHeartbeatsFromConversation)(processedConversation, {
277
- keepForTurns: options.stripHeartbeatsAfterTurns ?? 1,
278
- currentTurn,
279
- });
280
- return {
281
- result: text ? [{ type: "text", value: text }] : [{ type: "text", value: '' }],
282
- tool_use,
283
- token_usage: anthropicUsageToTokenUsage(result.usage),
284
- // make sure we set finish_reason to the correct value (claude is normally setting this by itself)
285
- finish_reason: tool_use ? "tool_use" : claudeFinishReason(result?.stop_reason ?? ''),
286
- conversation: processedConversation
287
- };
51
+ return (0, claude_messages_js_1.executeClaudeCompletion)(client, prompt, resolvedOptions);
288
52
  }
289
53
  async requestTextCompletionStream(driver, prompt, options) {
290
- const splits = options.model.split("/");
291
- let region = undefined;
292
- if (splits[0] === "locations" && splits.length >= 2) {
293
- region = splits[1];
294
- }
295
- const modelName = splits[splits.length - 1];
296
- options = { ...options, model: modelName };
54
+ const { region, options: resolvedOptions } = resolveVertexAIModelPath(options);
297
55
  const client = await driver.getAnthropicClient(region);
298
- const model_options = options.model_options;
299
- if ((model_options?._option_id !== undefined &&
56
+ const model_options = resolvedOptions.model_options;
57
+ if (model_options?._option_id !== undefined &&
300
58
  model_options?._option_id !== "vertexai-claude" &&
301
- model_options?._option_id !== "text-fallback")) {
302
- driver.logger.debug({ options: options.model_options }, "Unexpected option id");
303
- }
304
- // Include conversation history (same as non-streaming)
305
- const conversation = updateConversation(options.conversation, prompt);
306
- const { payload, requestOptions } = getClaudePayload(options, conversation);
307
- const streamingPayload = { ...payload, stream: true };
308
- const response_stream = await client.messages.stream(streamingPayload, requestOptions);
309
- // Track current tool use being built from streaming
310
- let currentToolUse = null;
311
- // Deferred spacing after a thinking block — emitted only when real text follows,
312
- // so it doesn't leak into the output when a tool call comes after thinking.
313
- let pendingSpacing = false;
314
- const stream = (0, async_1.asyncMap)(response_stream, async (streamEvent) => {
315
- switch (streamEvent.type) {
316
- case "message_start":
317
- return {
318
- result: [{ type: "text", value: '' }],
319
- token_usage: anthropicUsageToTokenUsage(streamEvent.message.usage),
320
- };
321
- case "message_delta":
322
- return {
323
- result: [{ type: "text", value: '' }],
324
- token_usage: {
325
- result: streamEvent.usage.output_tokens
326
- },
327
- finish_reason: claudeFinishReason(streamEvent.delta.stop_reason ?? undefined),
328
- };
329
- case "content_block_start":
330
- // Handle tool_use blocks
331
- if (streamEvent.content_block.type === "tool_use") {
332
- currentToolUse = {
333
- id: streamEvent.content_block.id,
334
- name: streamEvent.content_block.name,
335
- inputJson: ''
336
- };
337
- return {
338
- result: [],
339
- tool_use: [{
340
- id: streamEvent.content_block.id,
341
- tool_name: streamEvent.content_block.name,
342
- tool_input: '' // Will be accumulated via input_json_delta
343
- }]
344
- };
345
- }
346
- // Handle redacted thinking blocks
347
- if (streamEvent.content_block.type === "redacted_thinking" && model_options?.include_thoughts) {
348
- return {
349
- result: [{ type: "text", value: `[Redacted thinking: ${streamEvent.content_block.data}]` }]
350
- };
351
- }
352
- break;
353
- case "content_block_delta":
354
- // Handle different delta types
355
- switch (streamEvent.delta.type) {
356
- case "text_delta": {
357
- const prefix = pendingSpacing ? '\n\n' : '';
358
- pendingSpacing = false;
359
- return {
360
- result: streamEvent.delta.text ? [{ type: "text", value: prefix + streamEvent.delta.text }] : []
361
- };
362
- }
363
- case "input_json_delta":
364
- // Accumulate tool input JSON
365
- if (currentToolUse && streamEvent.delta.partial_json) {
366
- return {
367
- result: [],
368
- tool_use: [{
369
- id: currentToolUse.id,
370
- tool_name: '', // Name already sent in content_block_start
371
- tool_input: streamEvent.delta.partial_json
372
- }]
373
- };
374
- }
375
- break;
376
- case "thinking_delta":
377
- if (model_options?.include_thoughts) {
378
- return {
379
- result: streamEvent.delta.thinking ? [{ type: "text", value: streamEvent.delta.thinking }] : [],
380
- };
381
- }
382
- break;
383
- case "signature_delta":
384
- // End of thinking block — defer spacing until real text follows,
385
- // so it doesn't leak when a tool call comes next.
386
- if (model_options?.include_thoughts) {
387
- pendingSpacing = true;
388
- }
389
- break;
390
- }
391
- break;
392
- case "content_block_stop":
393
- // Reset tool use tracking; spacing is handled via pendingSpacing
394
- if (currentToolUse) {
395
- currentToolUse = null;
396
- // Tool call followed thinking — discard any pending spacing so it doesn't leak
397
- pendingSpacing = false;
398
- }
399
- break;
400
- }
401
- // Default case for all other event types
402
- return {
403
- result: []
404
- };
405
- });
406
- return stream;
407
- }
408
- /**
409
- * Format Anthropic API errors into LlumiverseError with proper status codes and retryability.
410
- *
411
- * Anthropic API errors have a specific structure:
412
- * - APIError.status: HTTP status code (400, 401, 403, 404, 409, 422, 429, 500+)
413
- * - APIError.error: Nested error object with type and message
414
- * - APIError.requestID: Request ID for support (can be null)
415
- *
416
- * Common error types:
417
- * - BadRequestError (400): Invalid request parameters
418
- * - AuthenticationError (401): Authentication required
419
- * - PermissionDeniedError (403): Insufficient permissions
420
- * - NotFoundError (404): Resource not found
421
- * - ConflictError (409): Resource conflict
422
- * - UnprocessableEntityError (422): Validation error
423
- * - RateLimitError (429): Rate limit exceeded
424
- * - InternalServerError (500+): Server-side errors
425
- * - APIConnectionError: Connection issues (no status code)
426
- * - APIConnectionTimeoutError: Request timeout (no status code)
427
- *
428
- * @see https://docs.anthropic.com/en/api/errors
429
- */
430
- formatLlumiverseError(_driver, error, context) {
431
- // Check if it's an Anthropic API error
432
- const isAnthropicError = this.isAnthropicApiError(error);
433
- if (!isAnthropicError) {
434
- // Not an Anthropic API error, use default handling
435
- throw error;
436
- }
437
- const apiError = error;
438
- const httpStatusCode = apiError.status;
439
- // Extract error message and nested error details
440
- let message = apiError.message || String(error);
441
- // Extract error type from nested error object if available
442
- let errorType;
443
- if (apiError.error && typeof apiError.error === 'object') {
444
- const nestedError = apiError.error;
445
- if (nestedError.error && typeof nestedError.error === 'object') {
446
- errorType = nestedError.error.type;
447
- // Use the nested error message if it's more specific
448
- if (nestedError.error.message) {
449
- message = nestedError.error.message;
450
- }
451
- }
452
- }
453
- // Build user-facing message with status code
454
- let userMessage = message;
455
- // Include status code in message (for end-user visibility)
456
- if (httpStatusCode) {
457
- userMessage = `[${httpStatusCode}] ${userMessage}`;
458
- }
459
- // Include error type if available
460
- if (errorType && errorType !== 'error') {
461
- userMessage = `${errorType}: ${userMessage}`;
462
- }
463
- // Add request ID if available (useful for Anthropic support)
464
- if (apiError.requestID) {
465
- userMessage += ` (Request ID: ${apiError.requestID})`;
59
+ model_options?._option_id !== "text-fallback") {
60
+ driver.logger.debug({ options: resolvedOptions.model_options }, "Unexpected option id");
466
61
  }
467
- // Determine retryability based on Anthropic error types
468
- const retryable = this.isClaudeErrorRetryable(error, httpStatusCode, errorType);
469
- // Use the error constructor name as the error name
470
- const errorName = error.constructor?.name || 'AnthropicError';
471
- return new core_1.LlumiverseError(`[${context.provider}] ${userMessage}`, retryable, context, error, httpStatusCode, errorName);
62
+ return (0, claude_messages_js_1.streamClaudeCompletion)(client, prompt, resolvedOptions);
472
63
  }
473
- /**
474
- * Type guard to check if error is an Anthropic API error.
475
- */
476
- isAnthropicApiError(error) {
477
- return (error !== null &&
478
- typeof error === 'object' &&
479
- error instanceof error_1.APIError);
480
- }
481
- /**
482
- * Determine if an Anthropic API error is retryable.
483
- *
484
- * Retryable errors:
485
- * - RateLimitError (429): Rate limit exceeded, retry with backoff
486
- * - InternalServerError (500+): Server-side errors
487
- * - APIConnectionTimeoutError: Request timeout
488
- * - 408 (Request Timeout): Request timeout
489
- * - 529 (Overloaded): Service overloaded
490
- *
491
- * Non-retryable errors:
492
- * - BadRequestError (400): Invalid request parameters
493
- * - AuthenticationError (401): Authentication failure
494
- * - PermissionDeniedError (403): Insufficient permissions
495
- * - NotFoundError (404): Resource not found
496
- * - ConflictError (409): Resource conflict
497
- * - UnprocessableEntityError (422): Validation error
498
- * - Other 4xx client errors
499
- * - invalid_request_error: Invalid request structure
500
- *
501
- * @param error - The error object
502
- * @param httpStatusCode - The HTTP status code if available
503
- * @param errorType - The nested error type if available
504
- * @returns True if retryable, false if not retryable, undefined if unknown
505
- */
506
64
  isClaudeErrorRetryable(error, httpStatusCode, errorType) {
507
- // Check specific Anthropic error types by class
508
- if (error instanceof error_1.RateLimitError)
509
- return true;
510
- if (error instanceof error_1.InternalServerError)
511
- return true;
512
- if (error instanceof error_1.APIConnectionTimeoutError)
513
- return true;
514
- // Non-retryable by error type
515
- if (error instanceof error_1.BadRequestError)
516
- return false;
517
- if (error instanceof error_1.AuthenticationError)
518
- return false;
519
- if (error instanceof error_1.PermissionDeniedError)
520
- return false;
521
- if (error instanceof error_1.NotFoundError)
522
- return false;
523
- if (error instanceof error_1.ConflictError)
524
- return false;
525
- if (error instanceof error_1.UnprocessableEntityError)
526
- return false;
527
- // Check nested error type
528
- if (errorType === 'invalid_request_error')
529
- return false;
530
- // Use HTTP status code
531
- if (httpStatusCode !== undefined) {
532
- if (httpStatusCode === 429)
533
- return true; // Rate limit
534
- if (httpStatusCode === 408)
535
- return true; // Request timeout
536
- if (httpStatusCode === 529)
537
- return true; // Overloaded
538
- if (httpStatusCode >= 500 && httpStatusCode < 600)
539
- return true; // Server errors
540
- if (httpStatusCode >= 400 && httpStatusCode < 500)
541
- return false; // Client errors
542
- }
543
- // Connection errors without status codes
544
- if (error instanceof error_1.APIConnectionError && !(error instanceof error_1.APIConnectionTimeoutError)) {
545
- // Generic connection errors might be retryable (network issues)
546
- return true;
547
- }
548
- // Unknown error type - let consumer decide retry strategy
549
- return undefined;
550
- }
551
- }
552
- exports.ClaudeModelDefinition = ClaudeModelDefinition;
553
- function createPromptFromResponse(response) {
554
- return {
555
- messages: [{
556
- role: response.role,
557
- content: response.content,
558
- }],
559
- system: undefined
560
- };
561
- }
562
- /**
563
- * Merge consecutive user messages in the conversation.
564
- * This is required because Anthropic's API expects all tool_result blocks
565
- * from a single assistant turn to be in one user message.
566
- * When multiple tool results are added as separate user messages,
567
- * we need to merge them before sending to the API.
568
- */
569
- function mergeConsecutiveUserMessages(messages) {
570
- if (messages.length === 0)
571
- return [];
572
- // Check if any merging is needed
573
- const needsMerging = messages.some((msg, i) => i < messages.length - 1 &&
574
- msg.role === 'user' &&
575
- messages[i + 1].role === 'user');
576
- if (!needsMerging) {
577
- return messages;
578
- }
579
- const result = [];
580
- let i = 0;
581
- while (i < messages.length) {
582
- const current = messages[i];
583
- if (current.role === 'user') {
584
- // Collect all consecutive user messages
585
- const mergedContent = [];
586
- while (i < messages.length && messages[i].role === 'user') {
587
- const userMsg = messages[i];
588
- if (Array.isArray(userMsg.content)) {
589
- mergedContent.push(...userMsg.content);
590
- }
591
- else if (typeof userMsg.content === 'string') {
592
- mergedContent.push({ type: 'text', text: userMsg.content });
593
- }
594
- i++;
595
- }
596
- result.push({
597
- role: 'user',
598
- content: mergedContent
599
- });
600
- }
601
- else {
602
- result.push(current);
603
- i++;
604
- }
65
+ return (0, claude_messages_js_1.isClaudeErrorRetryable)(error, httpStatusCode, errorType);
605
66
  }
606
- return result;
607
- }
608
- /**
609
- * Update the conversation messages
610
- * @param prompt
611
- * @param response
612
- * @returns
613
- */
614
- function updateConversation(conversation, prompt) {
615
- const baseSystemMessages = conversation?.system || [];
616
- const baseMessages = conversation?.messages || [];
617
- const system = baseSystemMessages.concat(prompt.system || []);
618
- // Sanitize first, then merge. Order matters: an empty assistant message (e.g. from interrupted
619
- // streaming) between two tool-result user messages acts as a false separator. If merge runs
620
- // first, those messages look non-consecutive and fixOrphanedToolUse injects a synthetic result
621
- // into the first one; when sanitize later removes the empty assistant, the second user message
622
- // ends up with an orphaned tool_result that Vertex AI rejects:
623
- // "unexpected tool_use_id found in tool_result blocks".
624
- const combined = sanitizeMessages(baseMessages.concat(prompt.messages || []));
625
- const mergedMessages = mergeConsecutiveUserMessages(combined);
626
- return {
627
- messages: mergedMessages,
628
- system: system.length > 0 ? system : undefined // If system is empty, set to undefined
629
- };
630
- }
631
- /**
632
- * Sanitize messages by removing empty text blocks.
633
- * Claude API rejects messages with empty text content blocks ("text content blocks must be non-empty").
634
- * This handles cases where streaming was interrupted and left empty text blocks.
635
- *
636
- * - Filters out empty text blocks from each message's content
637
- * - Removes messages entirely if they have no content after filtering
638
- */
639
- function sanitizeMessages(messages) {
640
- const result = [];
641
- for (const message of messages) {
642
- if (typeof message.content === 'string') {
643
- // String content - keep only if non-empty
644
- if (message.content.trim()) {
645
- result.push(message);
646
- }
647
- continue;
648
- }
649
- // Array content - filter out empty text blocks
650
- const filteredContent = message.content.filter(block => {
651
- if (block.type === 'text') {
652
- return block.text && block.text.trim().length > 0;
653
- }
654
- // Keep all non-text blocks (tool_use, tool_result, image, etc.)
655
- return true;
656
- });
657
- // Only include message if it has content after filtering
658
- if (filteredContent.length > 0) {
659
- result.push({
660
- ...message,
661
- content: filteredContent
662
- });
663
- }
664
- }
665
- return result;
666
- }
667
- /**
668
- * Fix orphaned tool_use blocks in the conversation.
669
- * @exported for testing
670
- *
671
- * When an agent is stopped mid-tool-execution, the assistant message contains tool_use blocks
672
- * but no corresponding tool_result was added. The Anthropic API requires that every tool_use
673
- * must be followed by a tool_result in the next user message.
674
- *
675
- * This function detects such cases and injects synthetic tool_result blocks indicating
676
- * the tools were interrupted, allowing the conversation to continue.
677
- */
678
- function fixOrphanedToolUse(messages) {
679
- if (messages.length < 2)
680
- return messages;
681
- const result = [];
682
- for (let i = 0; i < messages.length; i++) {
683
- const current = messages[i];
684
- result.push(current);
685
- // Check if this is an assistant message with tool_use blocks
686
- if (current.role === 'assistant' && Array.isArray(current.content)) {
687
- const toolUseBlocks = current.content.filter((block) => block.type === 'tool_use');
688
- if (toolUseBlocks.length > 0) {
689
- // Check if the next message is a user message with matching tool_results
690
- const nextMessage = messages[i + 1];
691
- if (nextMessage && nextMessage.role === 'user' && Array.isArray(nextMessage.content)) {
692
- // Get tool_result IDs from the next message
693
- const toolResultIds = new Set(nextMessage.content
694
- .filter((block) => block.type === 'tool_result')
695
- .map(block => block.tool_use_id));
696
- // Find orphaned tool_use blocks (no matching tool_result)
697
- const orphanedToolUse = toolUseBlocks.filter(block => !toolResultIds.has(block.id));
698
- if (orphanedToolUse.length > 0) {
699
- // Inject synthetic tool_results for orphaned tool_use
700
- const syntheticResults = orphanedToolUse.map(block => ({
701
- type: 'tool_result',
702
- tool_use_id: block.id,
703
- content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`
704
- }));
705
- // Prepend synthetic results to the next user message
706
- const updatedNextMessage = {
707
- ...nextMessage,
708
- content: [...syntheticResults, ...nextMessage.content]
709
- };
710
- // Replace the next message in our iteration
711
- messages[i + 1] = updatedNextMessage;
712
- }
713
- }
714
- else if (nextMessage && nextMessage.role === 'user') {
715
- // Next message is a user message but not array content (plain text)
716
- // We need to convert it and add tool_results
717
- const syntheticResults = toolUseBlocks.map(block => ({
718
- type: 'tool_result',
719
- tool_use_id: block.id,
720
- content: `[Tool interrupted: The user stopped the operation before "${block.name}" could execute.]`
721
- }));
722
- const textContent = typeof nextMessage.content === 'string'
723
- ? { type: 'text', text: nextMessage.content }
724
- : { type: 'text', text: '' };
725
- const updatedNextMessage = {
726
- role: 'user',
727
- content: [...syntheticResults, textContent]
728
- };
729
- messages[i + 1] = updatedNextMessage;
730
- }
731
- // Note: If there's no nextMessage, we leave the conversation as-is.
732
- // The tool_use blocks are expected to be there - the next turn will provide tool_results.
733
- }
734
- }
735
- }
736
- return result;
737
- }
738
- function stripClaudeCacheControlFromMessages(messages) {
739
- return messages.map(message => {
740
- if (typeof message.content === 'string') {
741
- return message;
742
- }
743
- return {
744
- ...message,
745
- content: message.content.map(block => stripClaudeCacheControlFromBlock(block)),
746
- };
747
- });
748
- }
749
- function stripClaudeCacheControlFromBlock(block) {
750
- const cloned = { ...block };
751
- delete cloned.cache_control;
752
- return cloned;
753
- }
754
- function stripClaudeCacheControlFromSystem(system) {
755
- return system?.map(block => {
756
- const { cache_control: _cacheControl, ...rest } = block;
757
- return rest;
758
- });
759
- }
760
- function stripClaudeCacheControlFromTools(tools) {
761
- return tools?.map(tool => {
762
- const cloned = { ...tool };
763
- delete cloned.cache_control;
764
- return cloned;
765
- });
766
- }
767
- function getClaudePayload(options, prompt) {
768
- const modelName = options.model; // Model name is already extracted in the calling methods
769
- const model_options = options.model_options;
770
- // Add beta header for Claude 3.7 models to enable 128k output tokens
771
- let requestOptions = undefined;
772
- if (modelName.includes('claude-3-7-sonnet') &&
773
- ((model_options?.max_tokens ?? 0) > 64000 || (model_options?.thinking_budget_tokens ?? 0) > 64000)) {
774
- requestOptions = {
775
- headers: {
776
- 'anthropic-beta': 'output-128k-2025-02-19'
777
- }
778
- };
779
- }
780
- // Fix orphaned tool_use blocks (can occur when agent is stopped mid-tool-execution)
781
- const fixedMessages = fixOrphanedToolUse(prompt.messages);
782
- // Sanitize messages to remove empty text blocks (can occur from interrupted streaming)
783
- let sanitizedMessages = sanitizeMessages(fixedMessages);
784
- // Validate tools have input_schema.type set to 'object' as required by the Anthropic SDK
785
- if (options.tools) {
786
- for (const tool of options.tools) {
787
- if (tool.input_schema.type !== 'object') {
788
- throw new Error(`Tool "${tool.name}" has invalid input_schema.type: expected "object", got "${tool.input_schema.type}"`);
789
- }
790
- }
791
- }
792
- // When no tools are provided but conversation contains tool_use/tool_result blocks
793
- // (e.g. checkpoint summary calls), convert tool blocks to text to avoid API errors
794
- const hasTools = options.tools && options.tools.length > 0;
795
- if (!hasTools && claudeMessagesContainToolBlocks(sanitizedMessages)) {
796
- sanitizedMessages = convertClaudeToolBlocksToText(sanitizedMessages);
797
- }
798
- sanitizedMessages = stripClaudeCacheControlFromMessages(sanitizedMessages);
799
- const sanitizedSystem = stripClaudeCacheControlFromSystem(prompt.system);
800
- const sanitizedTools = hasTools
801
- ? stripClaudeCacheControlFromTools(options.tools)
802
- : undefined;
803
- // Prompt caching: use three breakpoints so stable system prompt, tool definitions,
804
- // and the conversation history prefix can all be reused across calls.
805
- const cacheEnabled = model_options?.cache_enabled === true;
806
- if (cacheEnabled) {
807
- const cacheTtl = model_options?.cache_ttl;
808
- const cacheControl = { type: 'ephemeral', ...(cacheTtl && { ttl: cacheTtl }) };
809
- if (sanitizedSystem && sanitizedSystem.length > 0) {
810
- const lastSystemBlock = sanitizedSystem[sanitizedSystem.length - 1];
811
- lastSystemBlock.cache_control = cacheControl;
812
- }
813
- if (sanitizedTools && sanitizedTools.length > 0) {
814
- const lastTool = sanitizedTools[sanitizedTools.length - 1];
815
- lastTool.cache_control = cacheControl;
816
- }
817
- if (sanitizedMessages.length >= 4) {
818
- const pivotMsg = sanitizedMessages[sanitizedMessages.length - 2];
819
- if (Array.isArray(pivotMsg.content) && pivotMsg.content.length > 0) {
820
- const lastBlock = pivotMsg.content[pivotMsg.content.length - 1];
821
- if (typeof lastBlock === 'object' && lastBlock !== null &&
822
- 'type' in lastBlock && lastBlock.type !== 'thinking' && lastBlock.type !== 'redacted_thinking') {
823
- lastBlock.cache_control = cacheControl;
824
- }
825
- }
826
- }
827
- }
828
- // Resolve thinking, effort, and sampling restriction using shared Claude helper
829
- const { thinking, outputConfig, hasSamplingRestriction } = (0, claude_thinking_js_1.resolveClaudeThinking)(modelName, model_options);
830
- const payload = {
831
- messages: sanitizedMessages,
832
- system: sanitizedSystem,
833
- tools: sanitizedTools,
834
- temperature: hasSamplingRestriction ? undefined : model_options?.temperature,
835
- model: modelName,
836
- max_tokens: maxToken(options),
837
- top_p: hasSamplingRestriction ? undefined : (model_options?.temperature != null ? undefined : model_options?.top_p),
838
- top_k: hasSamplingRestriction ? undefined : model_options?.top_k,
839
- stop_sequences: model_options?.stop_sequence,
840
- thinking,
841
- ...(outputConfig && { output_config: outputConfig }),
842
- };
843
- return { payload, requestOptions };
844
- }
845
- /**
846
- * Checks whether any Claude message contains tool_use or tool_result content blocks.
847
- */
848
- function claudeMessagesContainToolBlocks(messages) {
849
- for (const msg of messages) {
850
- if (!Array.isArray(msg.content))
851
- continue;
852
- for (const block of msg.content) {
853
- if (typeof block === 'object' && block !== null && 'type' in block) {
854
- if (block.type === 'tool_use' || block.type === 'tool_result')
855
- return true;
856
- }
857
- }
67
+ formatLlumiverseError(_driver, error, context) {
68
+ return (0, claude_messages_js_1.formatAnthropicLlumiverseError)(error, context);
858
69
  }
859
- return false;
860
- }
861
- /**
862
- * Converts tool_use and tool_result blocks to text in Claude messages.
863
- * Preserves tool call information while removing structured blocks that
864
- * require tools to be defined in the API request.
865
- */
866
- function convertClaudeToolBlocksToText(messages) {
867
- return messages.map(msg => {
868
- if (!Array.isArray(msg.content))
869
- return msg;
870
- let hasToolBlocks = false;
871
- for (const block of msg.content) {
872
- if (typeof block === 'object' && block !== null && 'type' in block &&
873
- (block.type === 'tool_use' || block.type === 'tool_result')) {
874
- hasToolBlocks = true;
875
- break;
876
- }
877
- }
878
- if (!hasToolBlocks)
879
- return msg;
880
- const newContent = [];
881
- for (const block of msg.content) {
882
- if (typeof block === 'string') {
883
- newContent.push(block);
884
- continue;
885
- }
886
- if (block.type === 'tool_use') {
887
- const inputStr = block.input ? JSON.stringify(block.input) : '';
888
- const truncated = inputStr.length > 500 ? inputStr.substring(0, 500) + '...' : inputStr;
889
- newContent.push({
890
- type: 'text',
891
- text: `[Tool call: ${block.name}(${truncated})]`,
892
- });
893
- }
894
- else if (block.type === 'tool_result') {
895
- let resultStr = 'No content';
896
- if (typeof block.content === 'string') {
897
- resultStr = block.content.length > 500 ? block.content.substring(0, 500) + '...' : block.content;
898
- }
899
- else if (Array.isArray(block.content)) {
900
- const texts = block.content
901
- .filter((c) => c.type === 'text')
902
- .map(c => c.text.length > 500 ? c.text.substring(0, 500) + '...' : c.text);
903
- resultStr = texts.join('\n') || 'No text content';
904
- }
905
- newContent.push({
906
- type: 'text',
907
- text: `[Tool result: ${resultStr}]`,
908
- });
909
- }
910
- else {
911
- newContent.push(block);
912
- }
913
- }
914
- return { ...msg, content: newContent };
915
- });
916
70
  }
71
+ exports.ClaudeModelDefinition = ClaudeModelDefinition;
917
72
  //# sourceMappingURL=claude.js.map