@ai-sdk/anthropic 1.2.10 → 2.0.0-alpha.1

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.
@@ -6,11 +6,11 @@ import {
6
6
  combineHeaders,
7
7
  createEventSourceResponseHandler,
8
8
  createJsonResponseHandler,
9
- parseProviderOptions,
9
+ parseProviderOptions as parseProviderOptions2,
10
10
  postJsonToApi,
11
11
  resolve
12
12
  } from "@ai-sdk/provider-utils";
13
- import { z as z2 } from "zod";
13
+ import { z as z3 } from "zod";
14
14
 
15
15
  // src/anthropic-error.ts
16
16
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
@@ -27,17 +27,34 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
27
27
  errorToMessage: (data) => data.error.message
28
28
  });
29
29
 
30
+ // src/anthropic-messages-options.ts
31
+ import { z as z2 } from "zod";
32
+ var anthropicProviderOptions = z2.object({
33
+ /**
34
+ Include reasoning content in requests sent to the model. Defaults to `true`.
35
+
36
+ If you are experiencing issues with the model handling requests involving
37
+ */
38
+ sendReasoning: z2.boolean().optional(),
39
+ thinking: z2.object({
40
+ type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
41
+ budgetTokens: z2.number().optional()
42
+ }).optional()
43
+ });
44
+
30
45
  // src/anthropic-prepare-tools.ts
31
46
  import {
32
47
  UnsupportedFunctionalityError
33
48
  } from "@ai-sdk/provider";
34
- function prepareTools(mode) {
35
- var _a;
36
- const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
49
+ function prepareTools({
50
+ tools,
51
+ toolChoice
52
+ }) {
53
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
37
54
  const toolWarnings = [];
38
55
  const betas = /* @__PURE__ */ new Set();
39
56
  if (tools == null) {
40
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
57
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
41
58
  }
42
59
  const anthropicTools2 = [];
43
60
  for (const tool of tools) {
@@ -109,11 +126,10 @@ function prepareTools(mode) {
109
126
  break;
110
127
  }
111
128
  }
112
- const toolChoice = mode.toolChoice;
113
129
  if (toolChoice == null) {
114
130
  return {
115
131
  tools: anthropicTools2,
116
- tool_choice: void 0,
132
+ toolChoice: void 0,
117
133
  toolWarnings,
118
134
  betas
119
135
  };
@@ -123,30 +139,30 @@ function prepareTools(mode) {
123
139
  case "auto":
124
140
  return {
125
141
  tools: anthropicTools2,
126
- tool_choice: { type: "auto" },
142
+ toolChoice: { type: "auto" },
127
143
  toolWarnings,
128
144
  betas
129
145
  };
130
146
  case "required":
131
147
  return {
132
148
  tools: anthropicTools2,
133
- tool_choice: { type: "any" },
149
+ toolChoice: { type: "any" },
134
150
  toolWarnings,
135
151
  betas
136
152
  };
137
153
  case "none":
138
- return { tools: void 0, tool_choice: void 0, toolWarnings, betas };
154
+ return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
139
155
  case "tool":
140
156
  return {
141
157
  tools: anthropicTools2,
142
- tool_choice: { type: "tool", name: toolChoice.toolName },
158
+ toolChoice: { type: "tool", name: toolChoice.toolName },
143
159
  toolWarnings,
144
160
  betas
145
161
  };
146
162
  default: {
147
163
  const _exhaustiveCheck = type;
148
164
  throw new UnsupportedFunctionalityError({
149
- functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
165
+ functionality: `tool choice type: ${_exhaustiveCheck}`
150
166
  });
151
167
  }
152
168
  }
@@ -156,13 +172,13 @@ function prepareTools(mode) {
156
172
  import {
157
173
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
158
174
  } from "@ai-sdk/provider";
159
- import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
160
- function convertToAnthropicMessagesPrompt({
175
+ import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
176
+ async function convertToAnthropicMessagesPrompt({
161
177
  prompt,
162
178
  sendReasoning,
163
179
  warnings
164
180
  }) {
165
- var _a, _b, _c, _d;
181
+ var _a, _b, _c;
166
182
  const betas = /* @__PURE__ */ new Set();
167
183
  const blocks = groupIntoBlocks(prompt);
168
184
  let system = void 0;
@@ -184,10 +200,10 @@ function convertToAnthropicMessagesPrompt({
184
200
  functionality: "Multiple system messages that are separated by user/assistant messages"
185
201
  });
186
202
  }
187
- system = block.messages.map(({ content, providerMetadata }) => ({
203
+ system = block.messages.map(({ content, providerOptions }) => ({
188
204
  type: "text",
189
205
  text: content,
190
- cache_control: getCacheControl(providerMetadata)
206
+ cache_control: getCacheControl(providerOptions)
191
207
  }));
192
208
  break;
193
209
  }
@@ -200,7 +216,7 @@ function convertToAnthropicMessagesPrompt({
200
216
  for (let j = 0; j < content.length; j++) {
201
217
  const part = content[j];
202
218
  const isLastPart = j === content.length - 1;
203
- const cacheControl = (_a = getCacheControl(part.providerMetadata)) != null ? _a : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
219
+ const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
204
220
  switch (part.type) {
205
221
  case "text": {
206
222
  anthropicContent.push({
@@ -210,40 +226,39 @@ function convertToAnthropicMessagesPrompt({
210
226
  });
211
227
  break;
212
228
  }
213
- case "image": {
214
- anthropicContent.push({
215
- type: "image",
216
- source: part.image instanceof URL ? {
217
- type: "url",
218
- url: part.image.toString()
219
- } : {
220
- type: "base64",
221
- media_type: (_b = part.mimeType) != null ? _b : "image/jpeg",
222
- data: convertUint8ArrayToBase64(part.image)
223
- },
224
- cache_control: cacheControl
225
- });
226
- break;
227
- }
228
229
  case "file": {
229
- if (part.mimeType !== "application/pdf") {
230
+ if (part.mediaType.startsWith("image/")) {
231
+ anthropicContent.push({
232
+ type: "image",
233
+ source: part.data instanceof URL ? {
234
+ type: "url",
235
+ url: part.data.toString()
236
+ } : {
237
+ type: "base64",
238
+ media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
239
+ data: convertToBase64(part.data)
240
+ },
241
+ cache_control: cacheControl
242
+ });
243
+ } else if (part.mediaType === "application/pdf") {
244
+ betas.add("pdfs-2024-09-25");
245
+ anthropicContent.push({
246
+ type: "document",
247
+ source: part.data instanceof URL ? {
248
+ type: "url",
249
+ url: part.data.toString()
250
+ } : {
251
+ type: "base64",
252
+ media_type: "application/pdf",
253
+ data: convertToBase64(part.data)
254
+ },
255
+ cache_control: cacheControl
256
+ });
257
+ } else {
230
258
  throw new UnsupportedFunctionalityError2({
231
- functionality: "Non-PDF files in user messages"
259
+ functionality: `media type: ${part.mediaType}`
232
260
  });
233
261
  }
234
- betas.add("pdfs-2024-09-25");
235
- anthropicContent.push({
236
- type: "document",
237
- source: part.data instanceof URL ? {
238
- type: "url",
239
- url: part.data.toString()
240
- } : {
241
- type: "base64",
242
- media_type: "application/pdf",
243
- data: part.data
244
- },
245
- cache_control: cacheControl
246
- });
247
262
  break;
248
263
  }
249
264
  }
@@ -254,7 +269,7 @@ function convertToAnthropicMessagesPrompt({
254
269
  for (let i2 = 0; i2 < content.length; i2++) {
255
270
  const part = content[i2];
256
271
  const isLastPart = i2 === content.length - 1;
257
- const cacheControl = (_c = getCacheControl(part.providerMetadata)) != null ? _c : isLastPart ? getCacheControl(message.providerMetadata) : void 0;
272
+ const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
258
273
  const toolResultContent = part.content != null ? part.content.map((part2) => {
259
274
  var _a2;
260
275
  switch (part2.type) {
@@ -269,7 +284,7 @@ function convertToAnthropicMessagesPrompt({
269
284
  type: "image",
270
285
  source: {
271
286
  type: "base64",
272
- media_type: (_a2 = part2.mimeType) != null ? _a2 : "image/jpeg",
287
+ media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
273
288
  data: part2.data
274
289
  },
275
290
  cache_control: void 0
@@ -304,7 +319,7 @@ function convertToAnthropicMessagesPrompt({
304
319
  for (let k = 0; k < content.length; k++) {
305
320
  const part = content[k];
306
321
  const isLastContentPart = k === content.length - 1;
307
- const cacheControl = (_d = getCacheControl(part.providerMetadata)) != null ? _d : isLastContentPart ? getCacheControl(message.providerMetadata) : void 0;
322
+ const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
308
323
  switch (part.type) {
309
324
  case "text": {
310
325
  anthropicContent.push({
@@ -321,12 +336,37 @@ function convertToAnthropicMessagesPrompt({
321
336
  }
322
337
  case "reasoning": {
323
338
  if (sendReasoning) {
324
- anthropicContent.push({
325
- type: "thinking",
326
- thinking: part.text,
327
- signature: part.signature,
328
- cache_control: cacheControl
339
+ const reasoningMetadata = await parseProviderOptions({
340
+ provider: "anthropic",
341
+ providerOptions: part.providerOptions,
342
+ schema: anthropicReasoningMetadataSchema
329
343
  });
344
+ if (reasoningMetadata != null) {
345
+ if (reasoningMetadata.signature != null) {
346
+ anthropicContent.push({
347
+ type: "thinking",
348
+ thinking: part.text,
349
+ signature: reasoningMetadata.signature,
350
+ cache_control: cacheControl
351
+ });
352
+ } else if (reasoningMetadata.redactedData != null) {
353
+ anthropicContent.push({
354
+ type: "redacted_thinking",
355
+ data: reasoningMetadata.redactedData,
356
+ cache_control: cacheControl
357
+ });
358
+ } else {
359
+ warnings.push({
360
+ type: "other",
361
+ message: "unsupported reasoning metadata"
362
+ });
363
+ }
364
+ } else {
365
+ warnings.push({
366
+ type: "other",
367
+ message: "unsupported reasoning metadata"
368
+ });
369
+ }
330
370
  } else {
331
371
  warnings.push({
332
372
  type: "other",
@@ -335,14 +375,6 @@ function convertToAnthropicMessagesPrompt({
335
375
  }
336
376
  break;
337
377
  }
338
- case "redacted-reasoning": {
339
- anthropicContent.push({
340
- type: "redacted_thinking",
341
- data: part.data,
342
- cache_control: cacheControl
343
- });
344
- break;
345
- }
346
378
  case "tool-call": {
347
379
  anthropicContent.push({
348
380
  type: "tool_use",
@@ -361,7 +393,7 @@ function convertToAnthropicMessagesPrompt({
361
393
  }
362
394
  default: {
363
395
  const _exhaustiveCheck = type;
364
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
396
+ throw new Error(`content type: ${_exhaustiveCheck}`);
365
397
  }
366
398
  }
367
399
  }
@@ -434,11 +466,9 @@ function mapAnthropicStopReason(finishReason) {
434
466
 
435
467
  // src/anthropic-messages-language-model.ts
436
468
  var AnthropicMessagesLanguageModel = class {
437
- constructor(modelId, settings, config) {
438
- this.specificationVersion = "v1";
439
- this.defaultObjectGenerationMode = "tool";
469
+ constructor(modelId, config) {
470
+ this.specificationVersion = "v2";
440
471
  this.modelId = modelId;
441
- this.settings = settings;
442
472
  this.config = config;
443
473
  }
444
474
  supportsUrl(url) {
@@ -447,13 +477,13 @@ var AnthropicMessagesLanguageModel = class {
447
477
  get provider() {
448
478
  return this.config.provider;
449
479
  }
450
- get supportsImageUrls() {
451
- return this.config.supportsImageUrls;
480
+ get supportedUrls() {
481
+ var _a, _b, _c;
482
+ return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
452
483
  }
453
484
  async getArgs({
454
- mode,
455
485
  prompt,
456
- maxTokens = 4096,
486
+ maxOutputTokens = 4096,
457
487
  // 4096: max model output tokens TODO update default in v5
458
488
  temperature,
459
489
  topP,
@@ -463,10 +493,11 @@ var AnthropicMessagesLanguageModel = class {
463
493
  stopSequences,
464
494
  responseFormat,
465
495
  seed,
466
- providerMetadata: providerOptions
496
+ tools,
497
+ toolChoice,
498
+ providerOptions
467
499
  }) {
468
500
  var _a, _b, _c;
469
- const type = mode.type;
470
501
  const warnings = [];
471
502
  if (frequencyPenalty != null) {
472
503
  warnings.push({
@@ -493,15 +524,15 @@ var AnthropicMessagesLanguageModel = class {
493
524
  details: "JSON response format is not supported."
494
525
  });
495
526
  }
496
- const { prompt: messagesPrompt, betas: messagesBetas } = convertToAnthropicMessagesPrompt({
497
- prompt,
498
- sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
499
- warnings
500
- });
501
- const anthropicOptions = parseProviderOptions({
527
+ const anthropicOptions = await parseProviderOptions2({
502
528
  provider: "anthropic",
503
529
  providerOptions,
504
- schema: anthropicProviderOptionsSchema
530
+ schema: anthropicProviderOptions
531
+ });
532
+ const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
533
+ prompt,
534
+ sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
535
+ warnings
505
536
  });
506
537
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
507
538
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -509,7 +540,7 @@ var AnthropicMessagesLanguageModel = class {
509
540
  // model id:
510
541
  model: this.modelId,
511
542
  // standardized settings:
512
- max_tokens: maxTokens,
543
+ max_tokens: maxOutputTokens,
513
544
  temperature,
514
545
  top_k: topK,
515
546
  top_p: topP,
@@ -552,44 +583,23 @@ var AnthropicMessagesLanguageModel = class {
552
583
  details: "topP is not supported when thinking is enabled"
553
584
  });
554
585
  }
555
- baseArgs.max_tokens = maxTokens + thinkingBudget;
556
- }
557
- switch (type) {
558
- case "regular": {
559
- const {
560
- tools,
561
- tool_choice,
562
- toolWarnings,
563
- betas: toolsBetas
564
- } = prepareTools(mode);
565
- return {
566
- args: { ...baseArgs, tools, tool_choice },
567
- warnings: [...warnings, ...toolWarnings],
568
- betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
569
- };
570
- }
571
- case "object-json": {
572
- throw new UnsupportedFunctionalityError3({
573
- functionality: "json-mode object generation"
574
- });
575
- }
576
- case "object-tool": {
577
- const { name, description, parameters } = mode.tool;
578
- return {
579
- args: {
580
- ...baseArgs,
581
- tools: [{ name, description, input_schema: parameters }],
582
- tool_choice: { type: "tool", name }
583
- },
584
- warnings,
585
- betas: messagesBetas
586
- };
587
- }
588
- default: {
589
- const _exhaustiveCheck = type;
590
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
591
- }
586
+ baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
592
587
  }
588
+ const {
589
+ tools: anthropicTools2,
590
+ toolChoice: anthropicToolChoice,
591
+ toolWarnings,
592
+ betas: toolsBetas
593
+ } = prepareTools({ tools, toolChoice });
594
+ return {
595
+ args: {
596
+ ...baseArgs,
597
+ tools: anthropicTools2,
598
+ tool_choice: anthropicToolChoice
599
+ },
600
+ warnings: [...warnings, ...toolWarnings],
601
+ betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
602
+ };
593
603
  }
594
604
  async getHeaders({
595
605
  betas,
@@ -627,65 +637,71 @@ var AnthropicMessagesLanguageModel = class {
627
637
  abortSignal: options.abortSignal,
628
638
  fetch: this.config.fetch
629
639
  });
630
- const { messages: rawPrompt, ...rawSettings } = args;
631
- let text = "";
632
- for (const content of response.content) {
633
- if (content.type === "text") {
634
- text += content.text;
635
- }
636
- }
637
- let toolCalls = void 0;
638
- if (response.content.some((content) => content.type === "tool_use")) {
639
- toolCalls = [];
640
- for (const content of response.content) {
641
- if (content.type === "tool_use") {
642
- toolCalls.push({
640
+ const content = [];
641
+ for (const part of response.content) {
642
+ switch (part.type) {
643
+ case "text": {
644
+ content.push({ type: "text", text: part.text });
645
+ break;
646
+ }
647
+ case "thinking": {
648
+ content.push({
649
+ type: "reasoning",
650
+ text: part.thinking,
651
+ providerMetadata: {
652
+ anthropic: {
653
+ signature: part.signature
654
+ }
655
+ }
656
+ });
657
+ break;
658
+ }
659
+ case "redacted_thinking": {
660
+ content.push({
661
+ type: "reasoning",
662
+ text: "",
663
+ providerMetadata: {
664
+ anthropic: {
665
+ redactedData: part.data
666
+ }
667
+ }
668
+ });
669
+ break;
670
+ }
671
+ case "tool_use": {
672
+ content.push({
673
+ type: "tool-call",
643
674
  toolCallType: "function",
644
- toolCallId: content.id,
645
- toolName: content.name,
646
- args: JSON.stringify(content.input)
675
+ toolCallId: part.id,
676
+ toolName: part.name,
677
+ args: JSON.stringify(part.input)
647
678
  });
679
+ break;
648
680
  }
649
681
  }
650
682
  }
651
- const reasoning = response.content.filter(
652
- (content) => content.type === "redacted_thinking" || content.type === "thinking"
653
- ).map(
654
- (content) => content.type === "thinking" ? {
655
- type: "text",
656
- text: content.thinking,
657
- signature: content.signature
658
- } : {
659
- type: "redacted",
660
- data: content.data
661
- }
662
- );
663
683
  return {
664
- text,
665
- reasoning: reasoning.length > 0 ? reasoning : void 0,
666
- toolCalls,
684
+ content,
667
685
  finishReason: mapAnthropicStopReason(response.stop_reason),
668
686
  usage: {
669
- promptTokens: response.usage.input_tokens,
670
- completionTokens: response.usage.output_tokens
687
+ inputTokens: response.usage.input_tokens,
688
+ outputTokens: response.usage.output_tokens,
689
+ totalTokens: response.usage.input_tokens + response.usage.output_tokens,
690
+ cachedInputTokens: (_a = response.usage.cache_read_input_tokens) != null ? _a : void 0
671
691
  },
672
- rawCall: { rawPrompt, rawSettings },
673
- rawResponse: {
692
+ request: { body: args },
693
+ response: {
694
+ id: (_b = response.id) != null ? _b : void 0,
695
+ modelId: (_c = response.model) != null ? _c : void 0,
674
696
  headers: responseHeaders,
675
697
  body: rawResponse
676
698
  },
677
- response: {
678
- id: (_a = response.id) != null ? _a : void 0,
679
- modelId: (_b = response.model) != null ? _b : void 0
680
- },
681
699
  warnings,
682
700
  providerMetadata: {
683
701
  anthropic: {
684
- cacheCreationInputTokens: (_c = response.usage.cache_creation_input_tokens) != null ? _c : null,
685
- cacheReadInputTokens: (_d = response.usage.cache_read_input_tokens) != null ? _d : null
702
+ cacheCreationInputTokens: (_d = response.usage.cache_creation_input_tokens) != null ? _d : null
686
703
  }
687
- },
688
- request: { body: JSON.stringify(args) }
704
+ }
689
705
  };
690
706
  }
691
707
  async doStream(options) {
@@ -702,11 +718,11 @@ var AnthropicMessagesLanguageModel = class {
702
718
  abortSignal: options.abortSignal,
703
719
  fetch: this.config.fetch
704
720
  });
705
- const { messages: rawPrompt, ...rawSettings } = args;
706
721
  let finishReason = "unknown";
707
722
  const usage = {
708
- promptTokens: Number.NaN,
709
- completionTokens: Number.NaN
723
+ inputTokens: void 0,
724
+ outputTokens: void 0,
725
+ totalTokens: void 0
710
726
  };
711
727
  const toolCallContentBlocks = {};
712
728
  let providerMetadata = void 0;
@@ -714,8 +730,11 @@ var AnthropicMessagesLanguageModel = class {
714
730
  return {
715
731
  stream: response.pipeThrough(
716
732
  new TransformStream({
733
+ start(controller) {
734
+ controller.enqueue({ type: "stream-start", warnings });
735
+ },
717
736
  transform(chunk, controller) {
718
- var _a, _b, _c, _d;
737
+ var _a, _b, _c, _d, _e, _f;
719
738
  if (!chunk.success) {
720
739
  controller.enqueue({ type: "error", error: chunk.error });
721
740
  return;
@@ -735,9 +754,15 @@ var AnthropicMessagesLanguageModel = class {
735
754
  }
736
755
  case "redacted_thinking": {
737
756
  controller.enqueue({
738
- type: "redacted-reasoning",
739
- data: value.content_block.data
757
+ type: "reasoning",
758
+ text: "",
759
+ providerMetadata: {
760
+ anthropic: {
761
+ redactedData: value.content_block.data
762
+ }
763
+ }
740
764
  });
765
+ controller.enqueue({ type: "reasoning-part-finish" });
741
766
  return;
742
767
  }
743
768
  case "tool_use": {
@@ -776,24 +801,30 @@ var AnthropicMessagesLanguageModel = class {
776
801
  switch (deltaType) {
777
802
  case "text_delta": {
778
803
  controller.enqueue({
779
- type: "text-delta",
780
- textDelta: value.delta.text
804
+ type: "text",
805
+ text: value.delta.text
781
806
  });
782
807
  return;
783
808
  }
784
809
  case "thinking_delta": {
785
810
  controller.enqueue({
786
811
  type: "reasoning",
787
- textDelta: value.delta.thinking
812
+ text: value.delta.thinking
788
813
  });
789
814
  return;
790
815
  }
791
816
  case "signature_delta": {
792
817
  if (blockType === "thinking") {
793
818
  controller.enqueue({
794
- type: "reasoning-signature",
795
- signature: value.delta.signature
819
+ type: "reasoning",
820
+ text: "",
821
+ providerMetadata: {
822
+ anthropic: {
823
+ signature: value.delta.signature
824
+ }
825
+ }
796
826
  });
827
+ controller.enqueue({ type: "reasoning-part-finish" });
797
828
  }
798
829
  return;
799
830
  }
@@ -818,12 +849,11 @@ var AnthropicMessagesLanguageModel = class {
818
849
  }
819
850
  }
820
851
  case "message_start": {
821
- usage.promptTokens = value.message.usage.input_tokens;
822
- usage.completionTokens = value.message.usage.output_tokens;
852
+ usage.inputTokens = value.message.usage.input_tokens;
853
+ usage.cachedInputTokens = (_a = value.message.usage.cache_read_input_tokens) != null ? _a : void 0;
823
854
  providerMetadata = {
824
855
  anthropic: {
825
- cacheCreationInputTokens: (_a = value.message.usage.cache_creation_input_tokens) != null ? _a : null,
826
- cacheReadInputTokens: (_b = value.message.usage.cache_read_input_tokens) != null ? _b : null
856
+ cacheCreationInputTokens: (_b = value.message.usage.cache_creation_input_tokens) != null ? _b : null
827
857
  }
828
858
  };
829
859
  controller.enqueue({
@@ -834,7 +864,8 @@ var AnthropicMessagesLanguageModel = class {
834
864
  return;
835
865
  }
836
866
  case "message_delta": {
837
- usage.completionTokens = value.usage.output_tokens;
867
+ usage.outputTokens = value.usage.output_tokens;
868
+ usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = value.usage.output_tokens) != null ? _f : 0);
838
869
  finishReason = mapAnthropicStopReason(value.delta.stop_reason);
839
870
  return;
840
871
  }
@@ -859,142 +890,138 @@ var AnthropicMessagesLanguageModel = class {
859
890
  }
860
891
  })
861
892
  ),
862
- rawCall: { rawPrompt, rawSettings },
863
- rawResponse: { headers: responseHeaders },
864
- warnings,
865
- request: { body: JSON.stringify(body) }
893
+ request: { body },
894
+ response: { headers: responseHeaders }
866
895
  };
867
896
  }
868
897
  };
869
- var anthropicMessagesResponseSchema = z2.object({
870
- type: z2.literal("message"),
871
- id: z2.string().nullish(),
872
- model: z2.string().nullish(),
873
- content: z2.array(
874
- z2.discriminatedUnion("type", [
875
- z2.object({
876
- type: z2.literal("text"),
877
- text: z2.string()
898
+ var anthropicMessagesResponseSchema = z3.object({
899
+ type: z3.literal("message"),
900
+ id: z3.string().nullish(),
901
+ model: z3.string().nullish(),
902
+ content: z3.array(
903
+ z3.discriminatedUnion("type", [
904
+ z3.object({
905
+ type: z3.literal("text"),
906
+ text: z3.string()
878
907
  }),
879
- z2.object({
880
- type: z2.literal("thinking"),
881
- thinking: z2.string(),
882
- signature: z2.string()
908
+ z3.object({
909
+ type: z3.literal("thinking"),
910
+ thinking: z3.string(),
911
+ signature: z3.string()
883
912
  }),
884
- z2.object({
885
- type: z2.literal("redacted_thinking"),
886
- data: z2.string()
913
+ z3.object({
914
+ type: z3.literal("redacted_thinking"),
915
+ data: z3.string()
887
916
  }),
888
- z2.object({
889
- type: z2.literal("tool_use"),
890
- id: z2.string(),
891
- name: z2.string(),
892
- input: z2.unknown()
917
+ z3.object({
918
+ type: z3.literal("tool_use"),
919
+ id: z3.string(),
920
+ name: z3.string(),
921
+ input: z3.unknown()
893
922
  })
894
923
  ])
895
924
  ),
896
- stop_reason: z2.string().nullish(),
897
- usage: z2.object({
898
- input_tokens: z2.number(),
899
- output_tokens: z2.number(),
900
- cache_creation_input_tokens: z2.number().nullish(),
901
- cache_read_input_tokens: z2.number().nullish()
925
+ stop_reason: z3.string().nullish(),
926
+ usage: z3.object({
927
+ input_tokens: z3.number(),
928
+ output_tokens: z3.number(),
929
+ cache_creation_input_tokens: z3.number().nullish(),
930
+ cache_read_input_tokens: z3.number().nullish()
902
931
  })
903
932
  });
904
- var anthropicMessagesChunkSchema = z2.discriminatedUnion("type", [
905
- z2.object({
906
- type: z2.literal("message_start"),
907
- message: z2.object({
908
- id: z2.string().nullish(),
909
- model: z2.string().nullish(),
910
- usage: z2.object({
911
- input_tokens: z2.number(),
912
- output_tokens: z2.number(),
913
- cache_creation_input_tokens: z2.number().nullish(),
914
- cache_read_input_tokens: z2.number().nullish()
933
+ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
934
+ z3.object({
935
+ type: z3.literal("message_start"),
936
+ message: z3.object({
937
+ id: z3.string().nullish(),
938
+ model: z3.string().nullish(),
939
+ usage: z3.object({
940
+ input_tokens: z3.number(),
941
+ output_tokens: z3.number(),
942
+ cache_creation_input_tokens: z3.number().nullish(),
943
+ cache_read_input_tokens: z3.number().nullish()
915
944
  })
916
945
  })
917
946
  }),
918
- z2.object({
919
- type: z2.literal("content_block_start"),
920
- index: z2.number(),
921
- content_block: z2.discriminatedUnion("type", [
922
- z2.object({
923
- type: z2.literal("text"),
924
- text: z2.string()
947
+ z3.object({
948
+ type: z3.literal("content_block_start"),
949
+ index: z3.number(),
950
+ content_block: z3.discriminatedUnion("type", [
951
+ z3.object({
952
+ type: z3.literal("text"),
953
+ text: z3.string()
925
954
  }),
926
- z2.object({
927
- type: z2.literal("thinking"),
928
- thinking: z2.string()
955
+ z3.object({
956
+ type: z3.literal("thinking"),
957
+ thinking: z3.string()
929
958
  }),
930
- z2.object({
931
- type: z2.literal("tool_use"),
932
- id: z2.string(),
933
- name: z2.string()
959
+ z3.object({
960
+ type: z3.literal("tool_use"),
961
+ id: z3.string(),
962
+ name: z3.string()
934
963
  }),
935
- z2.object({
936
- type: z2.literal("redacted_thinking"),
937
- data: z2.string()
964
+ z3.object({
965
+ type: z3.literal("redacted_thinking"),
966
+ data: z3.string()
938
967
  })
939
968
  ])
940
969
  }),
941
- z2.object({
942
- type: z2.literal("content_block_delta"),
943
- index: z2.number(),
944
- delta: z2.discriminatedUnion("type", [
945
- z2.object({
946
- type: z2.literal("input_json_delta"),
947
- partial_json: z2.string()
970
+ z3.object({
971
+ type: z3.literal("content_block_delta"),
972
+ index: z3.number(),
973
+ delta: z3.discriminatedUnion("type", [
974
+ z3.object({
975
+ type: z3.literal("input_json_delta"),
976
+ partial_json: z3.string()
948
977
  }),
949
- z2.object({
950
- type: z2.literal("text_delta"),
951
- text: z2.string()
978
+ z3.object({
979
+ type: z3.literal("text_delta"),
980
+ text: z3.string()
952
981
  }),
953
- z2.object({
954
- type: z2.literal("thinking_delta"),
955
- thinking: z2.string()
982
+ z3.object({
983
+ type: z3.literal("thinking_delta"),
984
+ thinking: z3.string()
956
985
  }),
957
- z2.object({
958
- type: z2.literal("signature_delta"),
959
- signature: z2.string()
986
+ z3.object({
987
+ type: z3.literal("signature_delta"),
988
+ signature: z3.string()
960
989
  })
961
990
  ])
962
991
  }),
963
- z2.object({
964
- type: z2.literal("content_block_stop"),
965
- index: z2.number()
992
+ z3.object({
993
+ type: z3.literal("content_block_stop"),
994
+ index: z3.number()
966
995
  }),
967
- z2.object({
968
- type: z2.literal("error"),
969
- error: z2.object({
970
- type: z2.string(),
971
- message: z2.string()
996
+ z3.object({
997
+ type: z3.literal("error"),
998
+ error: z3.object({
999
+ type: z3.string(),
1000
+ message: z3.string()
972
1001
  })
973
1002
  }),
974
- z2.object({
975
- type: z2.literal("message_delta"),
976
- delta: z2.object({ stop_reason: z2.string().nullish() }),
977
- usage: z2.object({ output_tokens: z2.number() })
1003
+ z3.object({
1004
+ type: z3.literal("message_delta"),
1005
+ delta: z3.object({ stop_reason: z3.string().nullish() }),
1006
+ usage: z3.object({ output_tokens: z3.number() })
978
1007
  }),
979
- z2.object({
980
- type: z2.literal("message_stop")
1008
+ z3.object({
1009
+ type: z3.literal("message_stop")
981
1010
  }),
982
- z2.object({
983
- type: z2.literal("ping")
1011
+ z3.object({
1012
+ type: z3.literal("ping")
984
1013
  })
985
1014
  ]);
986
- var anthropicProviderOptionsSchema = z2.object({
987
- thinking: z2.object({
988
- type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
989
- budgetTokens: z2.number().optional()
990
- }).optional()
1015
+ var anthropicReasoningMetadataSchema = z3.object({
1016
+ signature: z3.string().optional(),
1017
+ redactedData: z3.string().optional()
991
1018
  });
992
1019
 
993
1020
  // src/anthropic-tools.ts
994
- import { z as z3 } from "zod";
995
- var Bash20241022Parameters = z3.object({
996
- command: z3.string(),
997
- restart: z3.boolean().optional()
1021
+ import { z as z4 } from "zod";
1022
+ var Bash20241022Parameters = z4.object({
1023
+ command: z4.string(),
1024
+ restart: z4.boolean().optional()
998
1025
  });
999
1026
  function bashTool_20241022(options = {}) {
1000
1027
  return {
@@ -1006,9 +1033,9 @@ function bashTool_20241022(options = {}) {
1006
1033
  experimental_toToolResultContent: options.experimental_toToolResultContent
1007
1034
  };
1008
1035
  }
1009
- var Bash20250124Parameters = z3.object({
1010
- command: z3.string(),
1011
- restart: z3.boolean().optional()
1036
+ var Bash20250124Parameters = z4.object({
1037
+ command: z4.string(),
1038
+ restart: z4.boolean().optional()
1012
1039
  });
1013
1040
  function bashTool_20250124(options = {}) {
1014
1041
  return {
@@ -1020,14 +1047,14 @@ function bashTool_20250124(options = {}) {
1020
1047
  experimental_toToolResultContent: options.experimental_toToolResultContent
1021
1048
  };
1022
1049
  }
1023
- var TextEditor20241022Parameters = z3.object({
1024
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1025
- path: z3.string(),
1026
- file_text: z3.string().optional(),
1027
- insert_line: z3.number().int().optional(),
1028
- new_str: z3.string().optional(),
1029
- old_str: z3.string().optional(),
1030
- view_range: z3.array(z3.number().int()).optional()
1050
+ var TextEditor20241022Parameters = z4.object({
1051
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1052
+ path: z4.string(),
1053
+ file_text: z4.string().optional(),
1054
+ insert_line: z4.number().int().optional(),
1055
+ new_str: z4.string().optional(),
1056
+ old_str: z4.string().optional(),
1057
+ view_range: z4.array(z4.number().int()).optional()
1031
1058
  });
1032
1059
  function textEditorTool_20241022(options = {}) {
1033
1060
  return {
@@ -1039,14 +1066,14 @@ function textEditorTool_20241022(options = {}) {
1039
1066
  experimental_toToolResultContent: options.experimental_toToolResultContent
1040
1067
  };
1041
1068
  }
1042
- var TextEditor20250124Parameters = z3.object({
1043
- command: z3.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1044
- path: z3.string(),
1045
- file_text: z3.string().optional(),
1046
- insert_line: z3.number().int().optional(),
1047
- new_str: z3.string().optional(),
1048
- old_str: z3.string().optional(),
1049
- view_range: z3.array(z3.number().int()).optional()
1069
+ var TextEditor20250124Parameters = z4.object({
1070
+ command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1071
+ path: z4.string(),
1072
+ file_text: z4.string().optional(),
1073
+ insert_line: z4.number().int().optional(),
1074
+ new_str: z4.string().optional(),
1075
+ old_str: z4.string().optional(),
1076
+ view_range: z4.array(z4.number().int()).optional()
1050
1077
  });
1051
1078
  function textEditorTool_20250124(options = {}) {
1052
1079
  return {
@@ -1058,8 +1085,8 @@ function textEditorTool_20250124(options = {}) {
1058
1085
  experimental_toToolResultContent: options.experimental_toToolResultContent
1059
1086
  };
1060
1087
  }
1061
- var Computer20241022Parameters = z3.object({
1062
- action: z3.enum([
1088
+ var Computer20241022Parameters = z4.object({
1089
+ action: z4.enum([
1063
1090
  "key",
1064
1091
  "type",
1065
1092
  "mouse_move",
@@ -1071,8 +1098,8 @@ var Computer20241022Parameters = z3.object({
1071
1098
  "screenshot",
1072
1099
  "cursor_position"
1073
1100
  ]),
1074
- coordinate: z3.array(z3.number().int()).optional(),
1075
- text: z3.string().optional()
1101
+ coordinate: z4.array(z4.number().int()).optional(),
1102
+ text: z4.string().optional()
1076
1103
  });
1077
1104
  function computerTool_20241022(options) {
1078
1105
  return {
@@ -1088,8 +1115,8 @@ function computerTool_20241022(options) {
1088
1115
  experimental_toToolResultContent: options.experimental_toToolResultContent
1089
1116
  };
1090
1117
  }
1091
- var Computer20250124Parameters = z3.object({
1092
- action: z3.enum([
1118
+ var Computer20250124Parameters = z4.object({
1119
+ action: z4.enum([
1093
1120
  "key",
1094
1121
  "hold_key",
1095
1122
  "type",
@@ -1107,12 +1134,12 @@ var Computer20250124Parameters = z3.object({
1107
1134
  "wait",
1108
1135
  "screenshot"
1109
1136
  ]),
1110
- coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1111
- duration: z3.number().optional(),
1112
- scroll_amount: z3.number().optional(),
1113
- scroll_direction: z3.enum(["up", "down", "left", "right"]).optional(),
1114
- start_coordinate: z3.tuple([z3.number().int(), z3.number().int()]).optional(),
1115
- text: z3.string().optional()
1137
+ coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1138
+ duration: z4.number().optional(),
1139
+ scroll_amount: z4.number().optional(),
1140
+ scroll_direction: z4.enum(["up", "down", "left", "right"]).optional(),
1141
+ start_coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1142
+ text: z4.string().optional()
1116
1143
  });
1117
1144
  function computerTool_20250124(options) {
1118
1145
  return {