@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.
@@ -167,6 +167,8 @@ declare const anthropicTools: {
167
167
  *
168
168
  * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
169
169
  * @param maxUses - Maximum advisor calls per request (per-request cap).
170
+ * @param maxTokens - Maximum advisor output tokens per call, including
171
+ * thinking and text. Minimum 1024; Anthropic recommends starting with 2048.
170
172
  * @param caching - Enables prompt caching for the advisor's transcript
171
173
  * across calls within a conversation. Worthwhile from ~3 advisor calls
172
174
  * per conversation.
@@ -174,15 +176,18 @@ declare const anthropicTools: {
174
176
  advisor_20260301: (args: Parameters<_ai_sdk_provider_utils.ProviderExecutedToolFactory<{}, {
175
177
  type: "advisor_result";
176
178
  text: string;
179
+ stopReason?: string;
177
180
  } | {
178
181
  type: "advisor_redacted_result";
179
182
  encryptedContent: string;
183
+ stopReason?: string;
180
184
  } | {
181
185
  type: "advisor_tool_result_error";
182
186
  errorCode: string;
183
187
  }, {
184
188
  model: string;
185
189
  maxUses?: number;
190
+ maxTokens?: number;
186
191
  caching?: {
187
192
  type: "ephemeral";
188
193
  ttl: "5m" | "1h";
@@ -190,9 +195,11 @@ declare const anthropicTools: {
190
195
  }, {}>>[0]) => _ai_sdk_provider_utils.ProviderExecutedTool<{}, {
191
196
  type: "advisor_result";
192
197
  text: string;
198
+ stopReason?: string;
193
199
  } | {
194
200
  type: "advisor_redacted_result";
195
201
  encryptedContent: string;
202
+ stopReason?: string;
196
203
  } | {
197
204
  type: "advisor_tool_result_error";
198
205
  errorCode: string;
@@ -1074,6 +1081,7 @@ type AnthropicTool = {
1074
1081
  name: 'advisor';
1075
1082
  model: string;
1076
1083
  max_uses?: number;
1084
+ max_tokens?: number;
1077
1085
  caching?: {
1078
1086
  type: 'ephemeral';
1079
1087
  ttl: '5m' | '1h';
@@ -100,7 +100,8 @@ var AnthropicFiles = class {
100
100
 
101
101
  // src/anthropic-language-model.ts
102
102
  import {
103
- APICallError
103
+ APICallError,
104
+ InvalidResponseDataError
104
105
  } from "@ai-sdk/provider";
105
106
  import {
106
107
  combineHeaders as combineHeaders2,
@@ -407,11 +408,13 @@ var anthropicResponseSchema = lazySchema3(
407
408
  content: z3.discriminatedUnion("type", [
408
409
  z3.object({
409
410
  type: z3.literal("advisor_result"),
410
- text: z3.string()
411
+ text: z3.string(),
412
+ stop_reason: z3.string().nullish()
411
413
  }),
412
414
  z3.object({
413
415
  type: z3.literal("advisor_redacted_result"),
414
- encrypted_content: z3.string()
416
+ encrypted_content: z3.string(),
417
+ stop_reason: z3.string().nullish()
415
418
  }),
416
419
  z3.object({
417
420
  type: z3.literal("advisor_tool_result_error"),
@@ -775,11 +778,13 @@ var anthropicChunkSchema = lazySchema3(
775
778
  content: z3.discriminatedUnion("type", [
776
779
  z3.object({
777
780
  type: z3.literal("advisor_result"),
778
- text: z3.string()
781
+ text: z3.string(),
782
+ stop_reason: z3.string().nullish()
779
783
  }),
780
784
  z3.object({
781
785
  type: z3.literal("advisor_redacted_result"),
782
- encrypted_content: z3.string()
786
+ encrypted_content: z3.string(),
787
+ stop_reason: z3.string().nullish()
783
788
  }),
784
789
  z3.object({
785
790
  type: z3.literal("advisor_tool_result_error"),
@@ -1289,6 +1294,7 @@ var advisor_20260301ArgsSchema = lazySchema4(
1289
1294
  z5.object({
1290
1295
  model: z5.string(),
1291
1296
  maxUses: z5.number().optional(),
1297
+ maxTokens: z5.number().int().min(1024).optional(),
1292
1298
  caching: z5.object({
1293
1299
  type: z5.literal("ephemeral"),
1294
1300
  ttl: z5.union([z5.literal("5m"), z5.literal("1h")])
@@ -1301,11 +1307,13 @@ var advisor_20260301OutputSchema = lazySchema4(
1301
1307
  z5.discriminatedUnion("type", [
1302
1308
  z5.object({
1303
1309
  type: z5.literal("advisor_result"),
1304
- text: z5.string()
1310
+ text: z5.string(),
1311
+ stopReason: z5.string().optional()
1305
1312
  }),
1306
1313
  z5.object({
1307
1314
  type: z5.literal("advisor_redacted_result"),
1308
- encryptedContent: z5.string()
1315
+ encryptedContent: z5.string(),
1316
+ stopReason: z5.string().optional()
1309
1317
  }),
1310
1318
  z5.object({
1311
1319
  type: z5.literal("advisor_tool_result_error"),
@@ -1875,6 +1883,9 @@ async function prepareTools({
1875
1883
  name: "advisor",
1876
1884
  model: args.model,
1877
1885
  ...args.maxUses !== void 0 && { max_uses: args.maxUses },
1886
+ ...args.maxTokens !== void 0 && {
1887
+ max_tokens: args.maxTokens
1888
+ },
1878
1889
  ...args.caching !== void 0 && { caching: args.caching }
1879
1890
  });
1880
1891
  break;
@@ -3281,7 +3292,10 @@ async function convertToAnthropicPrompt({
3281
3292
  tool_use_id: part.toolCallId,
3282
3293
  content: {
3283
3294
  type: "advisor_result",
3284
- text: advisorOutput.text
3295
+ text: advisorOutput.text,
3296
+ ...advisorOutput.stopReason !== void 0 && {
3297
+ stop_reason: advisorOutput.stopReason
3298
+ }
3285
3299
  },
3286
3300
  cache_control: cacheControl
3287
3301
  });
@@ -3291,7 +3305,10 @@ async function convertToAnthropicPrompt({
3291
3305
  tool_use_id: part.toolCallId,
3292
3306
  content: {
3293
3307
  type: "advisor_redacted_result",
3294
- encrypted_content: advisorOutput.encryptedContent
3308
+ encrypted_content: advisorOutput.encryptedContent,
3309
+ ...advisorOutput.stopReason !== void 0 && {
3310
+ stop_reason: advisorOutput.stopReason
3311
+ }
3295
3312
  },
3296
3313
  cache_control: cacheControl
3297
3314
  });
@@ -4588,7 +4605,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4588
4605
  toolName: advisorToolName,
4589
4606
  result: {
4590
4607
  type: "advisor_result",
4591
- text: part.content.text
4608
+ text: part.content.text,
4609
+ ...part.content.stop_reason != null && {
4610
+ stopReason: part.content.stop_reason
4611
+ }
4592
4612
  }
4593
4613
  });
4594
4614
  } else if (part.content.type === "advisor_redacted_result") {
@@ -4598,7 +4618,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4598
4618
  toolName: advisorToolName,
4599
4619
  result: {
4600
4620
  type: "advisor_redacted_result",
4601
- encryptedContent: part.content.encrypted_content
4621
+ encryptedContent: part.content.encrypted_content,
4622
+ ...part.content.stop_reason != null && {
4623
+ stopReason: part.content.stop_reason
4624
+ }
4602
4625
  }
4603
4626
  });
4604
4627
  } else {
@@ -4737,6 +4760,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4737
4760
  let stopDetails = void 0;
4738
4761
  let container = null;
4739
4762
  let isJsonResponseFromTool = false;
4763
+ let isMessageOpen = false;
4764
+ let activeMessageId;
4765
+ let hasInvalidMessageSequence = false;
4740
4766
  let blockType = void 0;
4741
4767
  const generateId2 = this.generateId;
4742
4768
  const transformedStream = response.pipeThrough(
@@ -4746,6 +4772,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4746
4772
  },
4747
4773
  transform(chunk, controller) {
4748
4774
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
4775
+ if (hasInvalidMessageSequence) {
4776
+ return;
4777
+ }
4749
4778
  if (options.includeRawChunks) {
4750
4779
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4751
4780
  }
@@ -5125,7 +5154,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5125
5154
  toolName: advisorToolName,
5126
5155
  result: {
5127
5156
  type: "advisor_result",
5128
- text: part.content.text
5157
+ text: part.content.text,
5158
+ ...part.content.stop_reason != null && {
5159
+ stopReason: part.content.stop_reason
5160
+ }
5129
5161
  }
5130
5162
  });
5131
5163
  } else if (part.content.type === "advisor_redacted_result") {
@@ -5135,7 +5167,10 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5135
5167
  toolName: advisorToolName,
5136
5168
  result: {
5137
5169
  type: "advisor_redacted_result",
5138
- encryptedContent: part.content.encrypted_content
5170
+ encryptedContent: part.content.encrypted_content,
5171
+ ...part.content.stop_reason != null && {
5172
+ stopReason: part.content.stop_reason
5173
+ }
5139
5174
  }
5140
5175
  });
5141
5176
  } else {
@@ -5366,6 +5401,22 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5366
5401
  }
5367
5402
  }
5368
5403
  case "message_start": {
5404
+ if (isMessageOpen) {
5405
+ if (activeMessageId === value.message.id) {
5406
+ return;
5407
+ }
5408
+ hasInvalidMessageSequence = true;
5409
+ controller.enqueue({
5410
+ type: "error",
5411
+ error: new InvalidResponseDataError({
5412
+ data: value,
5413
+ message: `Received message_start for message ${JSON.stringify(value.message.id)} while message ${JSON.stringify(activeMessageId)} is still open.`
5414
+ })
5415
+ });
5416
+ return;
5417
+ }
5418
+ isMessageOpen = true;
5419
+ activeMessageId = value.message.id;
5369
5420
  usage.input_tokens = value.message.usage.input_tokens;
5370
5421
  usage.cache_read_input_tokens = (_e = value.message.usage.cache_read_input_tokens) != null ? _e : 0;
5371
5422
  usage.cache_creation_input_tokens = (_f = value.message.usage.cache_creation_input_tokens) != null ? _f : 0;
@@ -5482,6 +5533,8 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5482
5533
  return;
5483
5534
  }
5484
5535
  case "message_stop": {
5536
+ isMessageOpen = false;
5537
+ activeMessageId = void 0;
5485
5538
  const anthropicMetadata = {
5486
5539
  usage: rawUsage != null ? rawUsage : null,
5487
5540
  stopSequence,
@@ -6170,6 +6223,8 @@ var anthropicTools = {
6170
6223
  *
6171
6224
  * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
6172
6225
  * @param maxUses - Maximum advisor calls per request (per-request cap).
6226
+ * @param maxTokens - Maximum advisor output tokens per call, including
6227
+ * thinking and text. Minimum 1024; Anthropic recommends starting with 2048.
6173
6228
  * @param caching - Enables prompt caching for the advisor's transcript
6174
6229
  * across calls within a conversation. Worthwhile from ~3 advisor calls
6175
6230
  * per conversation.