@ai-sdk/anthropic 2.0.92 → 2.0.94

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.
@@ -1820,12 +1820,13 @@ async function convertToAnthropicMessagesPrompt({
1820
1820
  case "tool-call": {
1821
1821
  if (part.providerExecuted) {
1822
1822
  if (part.toolName === "code_execution" && part.input != null && typeof part.input === "object" && "type" in part.input && typeof part.input.type === "string" && (part.input.type === "bash_code_execution" || part.input.type === "text_editor_code_execution")) {
1823
+ const { type: type2, ...input } = part.input;
1823
1824
  anthropicContent.push({
1824
1825
  type: "server_tool_use",
1825
1826
  id: part.toolCallId,
1826
- name: part.input.type,
1827
+ name: type2,
1827
1828
  // map back to subtool name
1828
- input: part.input,
1829
+ input,
1829
1830
  cache_control: cacheControl
1830
1831
  });
1831
1832
  } else if (part.toolName === "code_execution" || // code execution 20250522
@@ -1897,7 +1898,15 @@ async function convertToAnthropicMessagesPrompt({
1897
1898
  type: "bash_code_execution_tool_result",
1898
1899
  tool_use_id: part.toolCallId,
1899
1900
  cache_control: cacheControl,
1900
- content: codeExecutionOutput
1901
+ // Prompt caching requires stable key ordering:
1902
+ // https://platform.claude.com/docs/en/build-with-claude/prompt-caching#troubleshooting-common-issues
1903
+ content: codeExecutionOutput.type === "bash_code_execution_result" ? {
1904
+ type: codeExecutionOutput.type,
1905
+ stdout: codeExecutionOutput.stdout,
1906
+ stderr: codeExecutionOutput.stderr,
1907
+ return_code: codeExecutionOutput.return_code,
1908
+ content: codeExecutionOutput.content
1909
+ } : codeExecutionOutput
1901
1910
  } : {
1902
1911
  type: "text_editor_code_execution_tool_result",
1903
1912
  tool_use_id: part.toolCallId,
@@ -3082,6 +3091,9 @@ var AnthropicMessagesLanguageModel = class {
3082
3091
  let container = null;
3083
3092
  let iterations = null;
3084
3093
  let contextManagement = null;
3094
+ let isMessageOpen = false;
3095
+ let activeMessageId;
3096
+ let hasInvalidMessageSequence = false;
3085
3097
  let blockType = void 0;
3086
3098
  const generateId2 = this.generateId;
3087
3099
  const transformedStream = response.pipeThrough(
@@ -3091,6 +3103,9 @@ var AnthropicMessagesLanguageModel = class {
3091
3103
  },
3092
3104
  transform(chunk, controller) {
3093
3105
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3106
+ if (hasInvalidMessageSequence) {
3107
+ return;
3108
+ }
3094
3109
  if (options.includeRawChunks) {
3095
3110
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3096
3111
  }
@@ -3494,6 +3509,22 @@ var AnthropicMessagesLanguageModel = class {
3494
3509
  }
3495
3510
  }
3496
3511
  case "message_start": {
3512
+ if (isMessageOpen) {
3513
+ if (activeMessageId === value.message.id) {
3514
+ return;
3515
+ }
3516
+ hasInvalidMessageSequence = true;
3517
+ controller.enqueue({
3518
+ type: "error",
3519
+ error: new import_provider3.InvalidResponseDataError({
3520
+ data: value,
3521
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
3522
+ })
3523
+ });
3524
+ return;
3525
+ }
3526
+ isMessageOpen = true;
3527
+ activeMessageId = value.message.id;
3497
3528
  usage.inputTokens = value.message.usage.input_tokens;
3498
3529
  usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
3499
3530
  rawUsage = {
@@ -3586,6 +3617,8 @@ var AnthropicMessagesLanguageModel = class {
3586
3617
  return;
3587
3618
  }
3588
3619
  case "message_stop": {
3620
+ isMessageOpen = false;
3621
+ activeMessageId = void 0;
3589
3622
  controller.enqueue({
3590
3623
  type: "finish",
3591
3624
  finishReason,