@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.
package/dist/index.mjs CHANGED
@@ -12,11 +12,12 @@ import {
12
12
  } from "@ai-sdk/provider-utils";
13
13
 
14
14
  // src/version.ts
15
- var VERSION = true ? "3.0.104" : "0.0.0-test";
15
+ var VERSION = true ? "3.0.107" : "0.0.0-test";
16
16
 
17
17
  // src/anthropic-messages-language-model.ts
18
18
  import {
19
- APICallError
19
+ APICallError,
20
+ InvalidResponseDataError
20
21
  } from "@ai-sdk/provider";
21
22
  import {
22
23
  combineHeaders,
@@ -2737,12 +2738,13 @@ async function convertToAnthropicMessagesPrompt({
2737
2738
  // code execution 20250825:
2738
2739
  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")
2739
2740
  ) {
2741
+ const { type: subtoolName, ...input } = part.input;
2740
2742
  anthropicContent.push({
2741
2743
  type: "server_tool_use",
2742
2744
  id: part.toolCallId,
2743
- name: part.input.type,
2745
+ name: subtoolName,
2744
2746
  // map back to subtool name
2745
- input: part.input,
2747
+ input,
2746
2748
  cache_control: cacheControl
2747
2749
  });
2748
2750
  } else if (
@@ -2936,7 +2938,15 @@ async function convertToAnthropicMessagesPrompt({
2936
2938
  type: "bash_code_execution_tool_result",
2937
2939
  tool_use_id: part.toolCallId,
2938
2940
  cache_control: cacheControl,
2939
- content: codeExecutionOutput
2941
+ // Prompt caching requires stable key ordering:
2942
+ // https://platform.claude.com/docs/en/build-with-claude/prompt-caching#troubleshooting-common-issues
2943
+ content: codeExecutionOutput.type === "bash_code_execution_result" ? {
2944
+ type: codeExecutionOutput.type,
2945
+ stdout: codeExecutionOutput.stdout,
2946
+ stderr: codeExecutionOutput.stderr,
2947
+ return_code: codeExecutionOutput.return_code,
2948
+ content: codeExecutionOutput.content
2949
+ } : codeExecutionOutput
2940
2950
  });
2941
2951
  } else {
2942
2952
  anthropicContent.push({
@@ -4508,6 +4518,9 @@ var AnthropicMessagesLanguageModel = class {
4508
4518
  let stopDetails = void 0;
4509
4519
  let container = null;
4510
4520
  let isJsonResponseFromTool = false;
4521
+ let isMessageOpen = false;
4522
+ let activeMessageId;
4523
+ let hasInvalidMessageSequence = false;
4511
4524
  let blockType = void 0;
4512
4525
  const generateId3 = this.generateId;
4513
4526
  const transformedStream = response.pipeThrough(
@@ -4517,6 +4530,9 @@ var AnthropicMessagesLanguageModel = class {
4517
4530
  },
4518
4531
  transform(chunk, controller) {
4519
4532
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
4533
+ if (hasInvalidMessageSequence) {
4534
+ return;
4535
+ }
4520
4536
  if (options.includeRawChunks) {
4521
4537
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4522
4538
  }
@@ -5137,6 +5153,22 @@ var AnthropicMessagesLanguageModel = class {
5137
5153
  }
5138
5154
  }
5139
5155
  case "message_start": {
5156
+ if (isMessageOpen) {
5157
+ if (activeMessageId === value.message.id) {
5158
+ return;
5159
+ }
5160
+ hasInvalidMessageSequence = true;
5161
+ controller.enqueue({
5162
+ type: "error",
5163
+ error: new InvalidResponseDataError({
5164
+ data: value,
5165
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
5166
+ })
5167
+ });
5168
+ return;
5169
+ }
5170
+ isMessageOpen = true;
5171
+ activeMessageId = value.message.id;
5140
5172
  usage.input_tokens = value.message.usage.input_tokens;
5141
5173
  usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
5142
5174
  usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
@@ -5255,6 +5287,8 @@ var AnthropicMessagesLanguageModel = class {
5255
5287
  return;
5256
5288
  }
5257
5289
  case "message_stop": {
5290
+ isMessageOpen = false;
5291
+ activeMessageId = void 0;
5258
5292
  const anthropicMetadata = {
5259
5293
  usage: rawUsage != null ? rawUsage : null,
5260
5294
  cacheCreationInputTokens,