@ai-sdk/anthropic 4.0.29 → 4.0.32

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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 4.0.32
4
+
5
+ ### Patch Changes
6
+
7
+ - 8b96941: Reject spliced Anthropic generations while allowing duplicate message start events for the active message.
8
+
9
+ ## 4.0.31
10
+
11
+ ### Patch Changes
12
+
13
+ - 7a9da75: Add `maxTokens` to the `advisor_20260301` tool, forward it as `max_tokens` to cap each advisor sub-inference independently of executor output, and preserve advisor `stopReason` metadata for truncation detection and multi-turn replay.
14
+ - Updated dependencies [2b60826]
15
+ - @ai-sdk/provider-utils@5.0.22
16
+
17
+ ## 4.0.30
18
+
19
+ ### Patch Changes
20
+
21
+ - Updated dependencies [1bec07d]
22
+ - @ai-sdk/provider-utils@5.0.21
23
+
3
24
  ## 4.0.29
4
25
 
5
26
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -406,6 +406,8 @@ declare const anthropicTools: {
406
406
  *
407
407
  * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
408
408
  * @param maxUses - Maximum advisor calls per request (per-request cap).
409
+ * @param maxTokens - Maximum advisor output tokens per call, including
410
+ * thinking and text. Minimum 1024; Anthropic recommends starting with 2048.
409
411
  * @param caching - Enables prompt caching for the advisor's transcript
410
412
  * across calls within a conversation. Worthwhile from ~3 advisor calls
411
413
  * per conversation.
@@ -413,15 +415,18 @@ declare const anthropicTools: {
413
415
  advisor_20260301: (args: Parameters<_ai_sdk_provider_utils.ProviderExecutedToolFactory<{}, {
414
416
  type: "advisor_result";
415
417
  text: string;
418
+ stopReason?: string;
416
419
  } | {
417
420
  type: "advisor_redacted_result";
418
421
  encryptedContent: string;
422
+ stopReason?: string;
419
423
  } | {
420
424
  type: "advisor_tool_result_error";
421
425
  errorCode: string;
422
426
  }, {
423
427
  model: string;
424
428
  maxUses?: number;
429
+ maxTokens?: number;
425
430
  caching?: {
426
431
  type: "ephemeral";
427
432
  ttl: "5m" | "1h";
@@ -429,9 +434,11 @@ declare const anthropicTools: {
429
434
  }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{}, {
430
435
  type: "advisor_result";
431
436
  text: string;
437
+ stopReason?: string;
432
438
  } | {
433
439
  type: "advisor_redacted_result";
434
440
  encryptedContent: string;
441
+ stopReason?: string;
435
442
  } | {
436
443
  type: "advisor_tool_result_error";
437
444
  errorCode: string;
package/dist/index.js CHANGED
@@ -114,7 +114,8 @@ var AnthropicFiles = class {
114
114
 
115
115
  // src/anthropic-language-model.ts
116
116
  import {
117
- APICallError
117
+ APICallError,
118
+ InvalidResponseDataError
118
119
  } from "@ai-sdk/provider";
119
120
  import {
120
121
  combineHeaders as combineHeaders2,
@@ -421,11 +422,13 @@ var anthropicResponseSchema = lazySchema3(
421
422
  content: z3.discriminatedUnion("type", [
422
423
  z3.object({
423
424
  type: z3.literal("advisor_result"),
424
- text: z3.string()
425
+ text: z3.string(),
426
+ stop_reason: z3.string().nullish()
425
427
  }),
426
428
  z3.object({
427
429
  type: z3.literal("advisor_redacted_result"),
428
- encrypted_content: z3.string()
430
+ encrypted_content: z3.string(),
431
+ stop_reason: z3.string().nullish()
429
432
  }),
430
433
  z3.object({
431
434
  type: z3.literal("advisor_tool_result_error"),
@@ -789,11 +792,13 @@ var anthropicChunkSchema = lazySchema3(
789
792
  content: z3.discriminatedUnion("type", [
790
793
  z3.object({
791
794
  type: z3.literal("advisor_result"),
792
- text: z3.string()
795
+ text: z3.string(),
796
+ stop_reason: z3.string().nullish()
793
797
  }),
794
798
  z3.object({
795
799
  type: z3.literal("advisor_redacted_result"),
796
- encrypted_content: z3.string()
800
+ encrypted_content: z3.string(),
801
+ stop_reason: z3.string().nullish()
797
802
  }),
798
803
  z3.object({
799
804
  type: z3.literal("advisor_tool_result_error"),
@@ -1303,6 +1308,7 @@ var advisor_20260301ArgsSchema = lazySchema4(
1303
1308
  z5.object({
1304
1309
  model: z5.string(),
1305
1310
  maxUses: z5.number().optional(),
1311
+ maxTokens: z5.number().int().min(1024).optional(),
1306
1312
  caching: z5.object({
1307
1313
  type: z5.literal("ephemeral"),
1308
1314
  ttl: z5.union([z5.literal("5m"), z5.literal("1h")])
@@ -1315,11 +1321,13 @@ var advisor_20260301OutputSchema = lazySchema4(
1315
1321
  z5.discriminatedUnion("type", [
1316
1322
  z5.object({
1317
1323
  type: z5.literal("advisor_result"),
1318
- text: z5.string()
1324
+ text: z5.string(),
1325
+ stopReason: z5.string().optional()
1319
1326
  }),
1320
1327
  z5.object({
1321
1328
  type: z5.literal("advisor_redacted_result"),
1322
- encryptedContent: z5.string()
1329
+ encryptedContent: z5.string(),
1330
+ stopReason: z5.string().optional()
1323
1331
  }),
1324
1332
  z5.object({
1325
1333
  type: z5.literal("advisor_tool_result_error"),
@@ -1889,6 +1897,9 @@ async function prepareTools({
1889
1897
  name: "advisor",
1890
1898
  model: args.model,
1891
1899
  ...args.maxUses !== void 0 && { max_uses: args.maxUses },
1900
+ ...args.maxTokens !== void 0 && {
1901
+ max_tokens: args.maxTokens
1902
+ },
1892
1903
  ...args.caching !== void 0 && { caching: args.caching }
1893
1904
  });
1894
1905
  break;
@@ -3295,7 +3306,10 @@ async function convertToAnthropicPrompt({
3295
3306
  tool_use_id: part.toolCallId,
3296
3307
  content: {
3297
3308
  type: "advisor_result",
3298
- text: advisorOutput.text
3309
+ text: advisorOutput.text,
3310
+ ...advisorOutput.stopReason !== void 0 && {
3311
+ stop_reason: advisorOutput.stopReason
3312
+ }
3299
3313
  },
3300
3314
  cache_control: cacheControl
3301
3315
  });
@@ -3305,7 +3319,10 @@ async function convertToAnthropicPrompt({
3305
3319
  tool_use_id: part.toolCallId,
3306
3320
  content: {
3307
3321
  type: "advisor_redacted_result",
3308
- encrypted_content: advisorOutput.encryptedContent
3322
+ encrypted_content: advisorOutput.encryptedContent,
3323
+ ...advisorOutput.stopReason !== void 0 && {
3324
+ stop_reason: advisorOutput.stopReason
3325
+ }
3309
3326
  },
3310
3327
  cache_control: cacheControl
3311
3328
  });
@@ -4602,7 +4619,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4602
4619
  toolName: advisorToolName,
4603
4620
  result: {
4604
4621
  type: "advisor_result",
4605
- text: part.content.text
4622
+ text: part.content.text,
4623
+ ...part.content.stop_reason != null && {
4624
+ stopReason: part.content.stop_reason
4625
+ }
4606
4626
  }
4607
4627
  });
4608
4628
  } else if (part.content.type === "advisor_redacted_result") {
@@ -4612,7 +4632,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4612
4632
  toolName: advisorToolName,
4613
4633
  result: {
4614
4634
  type: "advisor_redacted_result",
4615
- encryptedContent: part.content.encrypted_content
4635
+ encryptedContent: part.content.encrypted_content,
4636
+ ...part.content.stop_reason != null && {
4637
+ stopReason: part.content.stop_reason
4638
+ }
4616
4639
  }
4617
4640
  });
4618
4641
  } else {
@@ -4751,6 +4774,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4751
4774
  let stopDetails = void 0;
4752
4775
  let container = null;
4753
4776
  let isJsonResponseFromTool = false;
4777
+ let isMessageOpen = false;
4778
+ let activeMessageId;
4779
+ let hasInvalidMessageSequence = false;
4754
4780
  let blockType = void 0;
4755
4781
  const generateId3 = this.generateId;
4756
4782
  const transformedStream = response.pipeThrough(
@@ -4760,6 +4786,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4760
4786
  },
4761
4787
  transform(chunk, controller) {
4762
4788
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
4789
+ if (hasInvalidMessageSequence) {
4790
+ return;
4791
+ }
4763
4792
  if (options.includeRawChunks) {
4764
4793
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4765
4794
  }
@@ -5139,7 +5168,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5139
5168
  toolName: advisorToolName,
5140
5169
  result: {
5141
5170
  type: "advisor_result",
5142
- text: part.content.text
5171
+ text: part.content.text,
5172
+ ...part.content.stop_reason != null && {
5173
+ stopReason: part.content.stop_reason
5174
+ }
5143
5175
  }
5144
5176
  });
5145
5177
  } else if (part.content.type === "advisor_redacted_result") {
@@ -5149,7 +5181,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5149
5181
  toolName: advisorToolName,
5150
5182
  result: {
5151
5183
  type: "advisor_redacted_result",
5152
- encryptedContent: part.content.encrypted_content
5184
+ encryptedContent: part.content.encrypted_content,
5185
+ ...part.content.stop_reason != null && {
5186
+ stopReason: part.content.stop_reason
5187
+ }
5153
5188
  }
5154
5189
  });
5155
5190
  } else {
@@ -5380,6 +5415,22 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5380
5415
  }
5381
5416
  }
5382
5417
  case "message_start": {
5418
+ if (isMessageOpen) {
5419
+ if (activeMessageId === value.message.id) {
5420
+ return;
5421
+ }
5422
+ hasInvalidMessageSequence = true;
5423
+ controller.enqueue({
5424
+ type: "error",
5425
+ error: new InvalidResponseDataError({
5426
+ data: value,
5427
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
5428
+ })
5429
+ });
5430
+ return;
5431
+ }
5432
+ isMessageOpen = true;
5433
+ activeMessageId = value.message.id;
5383
5434
  usage.input_tokens = value.message.usage.input_tokens;
5384
5435
  usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
5385
5436
  usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
@@ -5496,6 +5547,8 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5496
5547
  return;
5497
5548
  }
5498
5549
  case "message_stop": {
5550
+ isMessageOpen = false;
5551
+ activeMessageId = void 0;
5499
5552
  const anthropicMetadata = {
5500
5553
  usage: rawUsage != null ? rawUsage : null,
5501
5554
  stopSequence,
@@ -6184,6 +6237,8 @@ var anthropicTools = {
6184
6237
  *
6185
6238
  * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
6186
6239
  * @param maxUses - Maximum advisor calls per request (per-request cap).
6240
+ * @param maxTokens - Maximum advisor output tokens per call, including
6241
+ * thinking and text. Minimum 1024; Anthropic recommends starting with 2048.
6187
6242
  * @param caching - Enables prompt caching for the advisor's transcript
6188
6243
  * across calls within a conversation. Worthwhile from ~3 advisor calls
6189
6244
  * per conversation.
@@ -6520,7 +6575,7 @@ var AnthropicSkills = class {
6520
6575
  };
6521
6576
 
6522
6577
  // src/version.ts
6523
- var VERSION = true ? "4.0.29" : "0.0.0-test";
6578
+ var VERSION = true ? "4.0.32" : "0.0.0-test";
6524
6579
 
6525
6580
  // src/anthropic-provider.ts
6526
6581
  var ANTHROPIC_API_URL = "https://api.anthropic.com";