@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.
@@ -1,5 +1,6 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
+ InvalidResponseDataError,
3
4
  APICallError
4
5
  } from "@ai-sdk/provider";
5
6
  import {
@@ -1836,12 +1837,13 @@ async function convertToAnthropicMessagesPrompt({
1836
1837
  case "tool-call": {
1837
1838
  if (part.providerExecuted) {
1838
1839
  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")) {
1840
+ const { type: type2, ...input } = part.input;
1839
1841
  anthropicContent.push({
1840
1842
  type: "server_tool_use",
1841
1843
  id: part.toolCallId,
1842
- name: part.input.type,
1844
+ name: type2,
1843
1845
  // map back to subtool name
1844
- input: part.input,
1846
+ input,
1845
1847
  cache_control: cacheControl
1846
1848
  });
1847
1849
  } else if (part.toolName === "code_execution" || // code execution 20250522
@@ -1913,7 +1915,15 @@ async function convertToAnthropicMessagesPrompt({
1913
1915
  type: "bash_code_execution_tool_result",
1914
1916
  tool_use_id: part.toolCallId,
1915
1917
  cache_control: cacheControl,
1916
- content: codeExecutionOutput
1918
+ // Prompt caching requires stable key ordering:
1919
+ // https://platform.claude.com/docs/en/build-with-claude/prompt-caching#troubleshooting-common-issues
1920
+ content: codeExecutionOutput.type === "bash_code_execution_result" ? {
1921
+ type: codeExecutionOutput.type,
1922
+ stdout: codeExecutionOutput.stdout,
1923
+ stderr: codeExecutionOutput.stderr,
1924
+ return_code: codeExecutionOutput.return_code,
1925
+ content: codeExecutionOutput.content
1926
+ } : codeExecutionOutput
1917
1927
  } : {
1918
1928
  type: "text_editor_code_execution_tool_result",
1919
1929
  tool_use_id: part.toolCallId,
@@ -3098,6 +3108,9 @@ var AnthropicMessagesLanguageModel = class {
3098
3108
  let container = null;
3099
3109
  let iterations = null;
3100
3110
  let contextManagement = null;
3111
+ let isMessageOpen = false;
3112
+ let activeMessageId;
3113
+ let hasInvalidMessageSequence = false;
3101
3114
  let blockType = void 0;
3102
3115
  const generateId2 = this.generateId;
3103
3116
  const transformedStream = response.pipeThrough(
@@ -3107,6 +3120,9 @@ var AnthropicMessagesLanguageModel = class {
3107
3120
  },
3108
3121
  transform(chunk, controller) {
3109
3122
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3123
+ if (hasInvalidMessageSequence) {
3124
+ return;
3125
+ }
3110
3126
  if (options.includeRawChunks) {
3111
3127
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
3112
3128
  }
@@ -3510,6 +3526,22 @@ var AnthropicMessagesLanguageModel = class {
3510
3526
  }
3511
3527
  }
3512
3528
  case "message_start": {
3529
+ if (isMessageOpen) {
3530
+ if (activeMessageId === value.message.id) {
3531
+ return;
3532
+ }
3533
+ hasInvalidMessageSequence = true;
3534
+ controller.enqueue({
3535
+ type: "error",
3536
+ error: new InvalidResponseDataError({
3537
+ data: value,
3538
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
3539
+ })
3540
+ });
3541
+ return;
3542
+ }
3543
+ isMessageOpen = true;
3544
+ activeMessageId = value.message.id;
3513
3545
  usage.inputTokens = value.message.usage.input_tokens;
3514
3546
  usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
3515
3547
  rawUsage = {
@@ -3602,6 +3634,8 @@ var AnthropicMessagesLanguageModel = class {
3602
3634
  return;
3603
3635
  }
3604
3636
  case "message_stop": {
3637
+ isMessageOpen = false;
3638
+ activeMessageId = void 0;
3605
3639
  controller.enqueue({
3606
3640
  type: "finish",
3607
3641
  finishReason,