@ai-sdk/anthropic 3.0.104 → 3.0.107

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,6 +1,7 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
- APICallError
3
+ APICallError,
4
+ InvalidResponseDataError
4
5
  } from "@ai-sdk/provider";
5
6
  import {
6
7
  combineHeaders,
@@ -2721,12 +2722,13 @@ async function convertToAnthropicMessagesPrompt({
2721
2722
  // code execution 20250825:
2722
2723
  providerToolName === "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")
2723
2724
  ) {
2725
+ const { type: subtoolName, ...input } = part.input;
2724
2726
  anthropicContent.push({
2725
2727
  type: "server_tool_use",
2726
2728
  id: part.toolCallId,
2727
- name: part.input.type,
2729
+ name: subtoolName,
2728
2730
  // map back to subtool name
2729
- input: part.input,
2731
+ input,
2730
2732
  cache_control: cacheControl
2731
2733
  });
2732
2734
  } else if (
@@ -2920,7 +2922,15 @@ async function convertToAnthropicMessagesPrompt({
2920
2922
  type: "bash_code_execution_tool_result",
2921
2923
  tool_use_id: part.toolCallId,
2922
2924
  cache_control: cacheControl,
2923
- content: codeExecutionOutput
2925
+ // Prompt caching requires stable key ordering:
2926
+ // https://platform.claude.com/docs/en/build-with-claude/prompt-caching#troubleshooting-common-issues
2927
+ content: codeExecutionOutput.type === "bash_code_execution_result" ? {
2928
+ type: codeExecutionOutput.type,
2929
+ stdout: codeExecutionOutput.stdout,
2930
+ stderr: codeExecutionOutput.stderr,
2931
+ return_code: codeExecutionOutput.return_code,
2932
+ content: codeExecutionOutput.content
2933
+ } : codeExecutionOutput
2924
2934
  });
2925
2935
  } else {
2926
2936
  anthropicContent.push({
@@ -4492,6 +4502,9 @@ var AnthropicMessagesLanguageModel = class {
4492
4502
  let stopDetails = void 0;
4493
4503
  let container = null;
4494
4504
  let isJsonResponseFromTool = false;
4505
+ let isMessageOpen = false;
4506
+ let activeMessageId;
4507
+ let hasInvalidMessageSequence = false;
4495
4508
  let blockType = void 0;
4496
4509
  const generateId2 = this.generateId;
4497
4510
  const transformedStream = response.pipeThrough(
@@ -4501,6 +4514,9 @@ var AnthropicMessagesLanguageModel = class {
4501
4514
  },
4502
4515
  transform(chunk, controller) {
4503
4516
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
4517
+ if (hasInvalidMessageSequence) {
4518
+ return;
4519
+ }
4504
4520
  if (options.includeRawChunks) {
4505
4521
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4506
4522
  }
@@ -5121,6 +5137,22 @@ var AnthropicMessagesLanguageModel = class {
5121
5137
  }
5122
5138
  }
5123
5139
  case "message_start": {
5140
+ if (isMessageOpen) {
5141
+ if (activeMessageId === value.message.id) {
5142
+ return;
5143
+ }
5144
+ hasInvalidMessageSequence = true;
5145
+ controller.enqueue({
5146
+ type: "error",
5147
+ error: new InvalidResponseDataError({
5148
+ data: value,
5149
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
5150
+ })
5151
+ });
5152
+ return;
5153
+ }
5154
+ isMessageOpen = true;
5155
+ activeMessageId = value.message.id;
5124
5156
  usage.input_tokens = value.message.usage.input_tokens;
5125
5157
  usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
5126
5158
  usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
@@ -5239,6 +5271,8 @@ var AnthropicMessagesLanguageModel = class {
5239
5271
  return;
5240
5272
  }
5241
5273
  case "message_stop": {
5274
+ isMessageOpen = false;
5275
+ activeMessageId = void 0;
5242
5276
  const anthropicMetadata = {
5243
5277
  usage: rawUsage != null ? rawUsage : null,
5244
5278
  cacheCreationInputTokens,