@ai-sdk/anthropic 2.0.0-alpha.9 → 2.0.0-beta.10

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,5 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
- APICallError,
4
3
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
5
4
  } from "@ai-sdk/provider";
6
5
  import {
@@ -12,11 +11,11 @@ import {
12
11
  postJsonToApi,
13
12
  resolve
14
13
  } from "@ai-sdk/provider-utils";
15
- import { z as z3 } from "zod";
14
+ import { z as z4 } from "zod/v4";
16
15
 
17
16
  // src/anthropic-error.ts
18
17
  import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
19
- import { z } from "zod";
18
+ import { z } from "zod/v4";
20
19
  var anthropicErrorDataSchema = z.object({
21
20
  type: z.literal("error"),
22
21
  error: z.object({
@@ -30,62 +29,112 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
30
29
  });
31
30
 
32
31
  // src/anthropic-messages-options.ts
33
- import { z as z2 } from "zod";
34
- var webSearchLocationSchema = z2.object({
35
- type: z2.literal("approximate"),
36
- city: z2.string().optional(),
37
- region: z2.string().optional(),
38
- country: z2.string(),
39
- timezone: z2.string().optional()
32
+ import { z as z2 } from "zod/v4";
33
+ var anthropicFilePartProviderOptions = z2.object({
34
+ /**
35
+ * Citation configuration for this document.
36
+ * When enabled, this document will generate citations in the response.
37
+ */
38
+ citations: z2.object({
39
+ /**
40
+ * Enable citations for this document
41
+ */
42
+ enabled: z2.boolean()
43
+ }).optional(),
44
+ /**
45
+ * Custom title for the document.
46
+ * If not provided, the filename will be used.
47
+ */
48
+ title: z2.string().optional(),
49
+ /**
50
+ * Context about the document that will be passed to the model
51
+ * but not used towards cited content.
52
+ * Useful for storing document metadata as text or stringified JSON.
53
+ */
54
+ context: z2.string().optional()
40
55
  });
41
56
  var anthropicProviderOptions = z2.object({
42
- /**
43
- Include reasoning content in requests sent to the model. Defaults to `true`.
44
-
45
- If you are experiencing issues with the model handling requests involving
46
- */
47
57
  sendReasoning: z2.boolean().optional(),
48
58
  thinking: z2.object({
49
59
  type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
50
60
  budgetTokens: z2.number().optional()
51
61
  }).optional(),
52
62
  /**
53
- * Web search tool configuration for Claude models that support it.
54
- * When provided, automatically adds the web search tool to the request.
63
+ * Whether to disable parallel function calling during tool use. Default is false.
64
+ * When set to true, Claude will use at most one tool per response.
55
65
  */
56
- webSearch: z2.object({
57
- /**
58
- * Limit the number of searches per request (optional)
59
- * Defaults to 5 if not specified
60
- */
61
- maxUses: z2.number().min(1).max(20).optional(),
62
- /**
63
- * Only include results from these domains (optional)
64
- * Cannot be used with blockedDomains
65
- */
66
- allowedDomains: z2.array(z2.string()).optional(),
67
- /**
68
- * Never include results from these domains (optional)
69
- * Cannot be used with allowedDomains
70
- */
71
- blockedDomains: z2.array(z2.string()).optional(),
72
- /**
73
- * Localize search results based on user location (optional)
74
- */
75
- userLocation: webSearchLocationSchema.optional()
76
- }).optional()
66
+ disableParallelToolUse: z2.boolean().optional()
77
67
  });
78
68
 
79
69
  // src/anthropic-prepare-tools.ts
80
70
  import {
81
71
  UnsupportedFunctionalityError
82
72
  } from "@ai-sdk/provider";
73
+
74
+ // src/get-cache-control.ts
75
+ function getCacheControl(providerMetadata) {
76
+ var _a;
77
+ const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
78
+ const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
79
+ return cacheControlValue;
80
+ }
81
+
82
+ // src/tool/web-search_20250305.ts
83
+ import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
84
+ import { z as z3 } from "zod/v4";
85
+ var webSearch_20250305ArgsSchema = z3.object({
86
+ /**
87
+ * Maximum number of web searches Claude can perform during the conversation.
88
+ */
89
+ maxUses: z3.number().optional(),
90
+ /**
91
+ * Optional list of domains that Claude is allowed to search.
92
+ */
93
+ allowedDomains: z3.array(z3.string()).optional(),
94
+ /**
95
+ * Optional list of domains that Claude should avoid when searching.
96
+ */
97
+ blockedDomains: z3.array(z3.string()).optional(),
98
+ /**
99
+ * Optional user location information to provide geographically relevant search results.
100
+ */
101
+ userLocation: z3.object({
102
+ type: z3.literal("approximate"),
103
+ city: z3.string().optional(),
104
+ region: z3.string().optional(),
105
+ country: z3.string().optional(),
106
+ timezone: z3.string().optional()
107
+ }).optional()
108
+ });
109
+ var webSearch_20250305OutputSchema = z3.array(
110
+ z3.object({
111
+ url: z3.string(),
112
+ title: z3.string(),
113
+ pageAge: z3.string().nullable(),
114
+ encryptedContent: z3.string(),
115
+ type: z3.string()
116
+ })
117
+ );
118
+ var factory = createProviderDefinedToolFactoryWithOutputSchema({
119
+ id: "anthropic.web_search_20250305",
120
+ name: "web_search",
121
+ inputSchema: z3.object({
122
+ query: z3.string()
123
+ }),
124
+ outputSchema: webSearch_20250305OutputSchema
125
+ });
126
+ var webSearch_20250305 = (args = {}) => {
127
+ return factory(args);
128
+ };
129
+
130
+ // src/anthropic-prepare-tools.ts
83
131
  function isWebSearchTool(tool) {
84
132
  return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
85
133
  }
86
134
  function prepareTools({
87
135
  tools,
88
- toolChoice
136
+ toolChoice,
137
+ disableParallelToolUse
89
138
  }) {
90
139
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
91
140
  const toolWarnings = [];
@@ -101,10 +150,12 @@ function prepareTools({
101
150
  }
102
151
  switch (tool.type) {
103
152
  case "function":
153
+ const cacheControl = getCacheControl(tool.providerOptions);
104
154
  anthropicTools2.push({
105
155
  name: tool.name,
106
156
  description: tool.description,
107
- input_schema: tool.parameters
157
+ input_schema: tool.inputSchema,
158
+ cache_control: cacheControl
108
159
  });
109
160
  break;
110
161
  case "provider-defined":
@@ -112,7 +163,7 @@ function prepareTools({
112
163
  case "anthropic.computer_20250124":
113
164
  betas.add("computer-use-2025-01-24");
114
165
  anthropicTools2.push({
115
- name: tool.name,
166
+ name: "computer",
116
167
  type: "computer_20250124",
117
168
  display_width_px: tool.args.displayWidthPx,
118
169
  display_height_px: tool.args.displayHeightPx,
@@ -122,7 +173,7 @@ function prepareTools({
122
173
  case "anthropic.computer_20241022":
123
174
  betas.add("computer-use-2024-10-22");
124
175
  anthropicTools2.push({
125
- name: tool.name,
176
+ name: "computer",
126
177
  type: "computer_20241022",
127
178
  display_width_px: tool.args.displayWidthPx,
128
179
  display_height_px: tool.args.displayHeightPx,
@@ -132,31 +183,50 @@ function prepareTools({
132
183
  case "anthropic.text_editor_20250124":
133
184
  betas.add("computer-use-2025-01-24");
134
185
  anthropicTools2.push({
135
- name: tool.name,
186
+ name: "str_replace_editor",
136
187
  type: "text_editor_20250124"
137
188
  });
138
189
  break;
139
190
  case "anthropic.text_editor_20241022":
140
191
  betas.add("computer-use-2024-10-22");
141
192
  anthropicTools2.push({
142
- name: tool.name,
193
+ name: "str_replace_editor",
143
194
  type: "text_editor_20241022"
144
195
  });
145
196
  break;
197
+ case "anthropic.text_editor_20250429":
198
+ betas.add("computer-use-2025-01-24");
199
+ anthropicTools2.push({
200
+ name: "str_replace_based_edit_tool",
201
+ type: "text_editor_20250429"
202
+ });
203
+ break;
146
204
  case "anthropic.bash_20250124":
147
205
  betas.add("computer-use-2025-01-24");
148
206
  anthropicTools2.push({
149
- name: tool.name,
207
+ name: "bash",
150
208
  type: "bash_20250124"
151
209
  });
152
210
  break;
153
211
  case "anthropic.bash_20241022":
154
212
  betas.add("computer-use-2024-10-22");
155
213
  anthropicTools2.push({
156
- name: tool.name,
214
+ name: "bash",
157
215
  type: "bash_20241022"
158
216
  });
159
217
  break;
218
+ case "anthropic.web_search_20250305": {
219
+ const args = webSearch_20250305ArgsSchema.parse(tool.args);
220
+ anthropicTools2.push({
221
+ type: "web_search_20250305",
222
+ name: "web_search",
223
+ max_uses: args.maxUses,
224
+ allowed_domains: args.allowedDomains,
225
+ blocked_domains: args.blockedDomains,
226
+ user_location: args.userLocation
227
+ });
228
+ break;
229
+ }
160
230
  default:
161
231
  toolWarnings.push({ type: "unsupported-tool", tool });
162
232
  break;
@@ -170,7 +240,7 @@ function prepareTools({
170
240
  if (toolChoice == null) {
171
241
  return {
172
242
  tools: anthropicTools2,
173
- toolChoice: void 0,
243
+ toolChoice: disableParallelToolUse ? { type: "auto", disable_parallel_tool_use: disableParallelToolUse } : void 0,
174
244
  toolWarnings,
175
245
  betas
176
246
  };
@@ -180,14 +250,20 @@ function prepareTools({
180
250
  case "auto":
181
251
  return {
182
252
  tools: anthropicTools2,
183
- toolChoice: { type: "auto" },
253
+ toolChoice: {
254
+ type: "auto",
255
+ disable_parallel_tool_use: disableParallelToolUse
256
+ },
184
257
  toolWarnings,
185
258
  betas
186
259
  };
187
260
  case "required":
188
261
  return {
189
262
  tools: anthropicTools2,
190
- toolChoice: { type: "any" },
263
+ toolChoice: {
264
+ type: "any",
265
+ disable_parallel_tool_use: disableParallelToolUse
266
+ },
191
267
  toolWarnings,
192
268
  betas
193
269
  };
@@ -196,7 +272,11 @@ function prepareTools({
196
272
  case "tool":
197
273
  return {
198
274
  tools: anthropicTools2,
199
- toolChoice: { type: "tool", name: toolChoice.toolName },
275
+ toolChoice: {
276
+ type: "tool",
277
+ name: toolChoice.toolName,
278
+ disable_parallel_tool_use: disableParallelToolUse
279
+ },
200
280
  toolWarnings,
201
281
  betas
202
282
  };
@@ -214,21 +294,51 @@ import {
214
294
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
215
295
  } from "@ai-sdk/provider";
216
296
  import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
297
+ function convertToString(data) {
298
+ if (typeof data === "string") {
299
+ return Buffer.from(data, "base64").toString("utf-8");
300
+ }
301
+ if (data instanceof Uint8Array) {
302
+ return new TextDecoder().decode(data);
303
+ }
304
+ if (data instanceof URL) {
305
+ throw new UnsupportedFunctionalityError2({
306
+ functionality: "URL-based text documents are not supported for citations"
307
+ });
308
+ }
309
+ throw new UnsupportedFunctionalityError2({
310
+ functionality: `unsupported data type for text documents: ${typeof data}`
311
+ });
312
+ }
217
313
  async function convertToAnthropicMessagesPrompt({
218
314
  prompt,
219
315
  sendReasoning,
220
316
  warnings
221
317
  }) {
222
- var _a, _b, _c;
318
+ var _a, _b, _c, _d, _e;
223
319
  const betas = /* @__PURE__ */ new Set();
224
320
  const blocks = groupIntoBlocks(prompt);
225
321
  let system = void 0;
226
322
  const messages = [];
227
- function getCacheControl(providerMetadata) {
228
- var _a2;
229
- const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
230
- const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
231
- return cacheControlValue;
323
+ async function shouldEnableCitations(providerMetadata) {
324
+ var _a2, _b2;
325
+ const anthropicOptions = await parseProviderOptions({
326
+ provider: "anthropic",
327
+ providerOptions: providerMetadata,
328
+ schema: anthropicFilePartProviderOptions
329
+ });
330
+ return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
331
+ }
332
+ async function getDocumentMetadata(providerMetadata) {
333
+ const anthropicOptions = await parseProviderOptions({
334
+ provider: "anthropic",
335
+ providerOptions: providerMetadata,
336
+ schema: anthropicFilePartProviderOptions
337
+ });
338
+ return {
339
+ title: anthropicOptions == null ? void 0 : anthropicOptions.title,
340
+ context: anthropicOptions == null ? void 0 : anthropicOptions.context
341
+ };
232
342
  }
233
343
  for (let i = 0; i < blocks.length; i++) {
234
344
  const block = blocks[i];
@@ -283,6 +393,12 @@ async function convertToAnthropicMessagesPrompt({
283
393
  });
284
394
  } else if (part.mediaType === "application/pdf") {
285
395
  betas.add("pdfs-2024-09-25");
396
+ const enableCitations = await shouldEnableCitations(
397
+ part.providerOptions
398
+ );
399
+ const metadata = await getDocumentMetadata(
400
+ part.providerOptions
401
+ );
286
402
  anthropicContent.push({
287
403
  type: "document",
288
404
  source: part.data instanceof URL ? {
@@ -293,6 +409,35 @@ async function convertToAnthropicMessagesPrompt({
293
409
  media_type: "application/pdf",
294
410
  data: convertToBase64(part.data)
295
411
  },
412
+ title: (_b = metadata.title) != null ? _b : part.filename,
413
+ ...metadata.context && { context: metadata.context },
414
+ ...enableCitations && {
415
+ citations: { enabled: true }
416
+ },
417
+ cache_control: cacheControl
418
+ });
419
+ } else if (part.mediaType === "text/plain") {
420
+ const enableCitations = await shouldEnableCitations(
421
+ part.providerOptions
422
+ );
423
+ const metadata = await getDocumentMetadata(
424
+ part.providerOptions
425
+ );
426
+ anthropicContent.push({
427
+ type: "document",
428
+ source: part.data instanceof URL ? {
429
+ type: "url",
430
+ url: part.data.toString()
431
+ } : {
432
+ type: "text",
433
+ media_type: "text/plain",
434
+ data: convertToString(part.data)
435
+ },
436
+ title: (_c = metadata.title) != null ? _c : part.filename,
437
+ ...metadata.context && { context: metadata.context },
438
+ ...enableCitations && {
439
+ citations: { enabled: true }
440
+ },
296
441
  cache_control: cacheControl
297
442
  });
298
443
  } else {
@@ -310,33 +455,53 @@ async function convertToAnthropicMessagesPrompt({
310
455
  for (let i2 = 0; i2 < content.length; i2++) {
311
456
  const part = content[i2];
312
457
  const isLastPart = i2 === content.length - 1;
313
- const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
314
- const toolResultContent = part.content != null ? part.content.map((part2) => {
315
- var _a2;
316
- switch (part2.type) {
317
- case "text":
318
- return {
319
- type: "text",
320
- text: part2.text,
321
- cache_control: void 0
322
- };
323
- case "image":
324
- return {
325
- type: "image",
326
- source: {
327
- type: "base64",
328
- media_type: (_a2 = part2.mediaType) != null ? _a2 : "image/jpeg",
329
- data: part2.data
330
- },
331
- cache_control: void 0
332
- };
333
- }
334
- }) : JSON.stringify(part.result);
458
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
459
+ const output = part.output;
460
+ let contentValue;
461
+ switch (output.type) {
462
+ case "content":
463
+ contentValue = output.value.map((contentPart) => {
464
+ switch (contentPart.type) {
465
+ case "text":
466
+ return {
467
+ type: "text",
468
+ text: contentPart.text,
469
+ cache_control: void 0
470
+ };
471
+ case "media": {
472
+ if (contentPart.mediaType.startsWith("image/")) {
473
+ return {
474
+ type: "image",
475
+ source: {
476
+ type: "base64",
477
+ media_type: contentPart.mediaType,
478
+ data: contentPart.data
479
+ },
480
+ cache_control: void 0
481
+ };
482
+ }
483
+ throw new UnsupportedFunctionalityError2({
484
+ functionality: `media type: ${contentPart.mediaType}`
485
+ });
486
+ }
487
+ }
488
+ });
489
+ break;
490
+ case "text":
491
+ case "error-text":
492
+ contentValue = output.value;
493
+ break;
494
+ case "json":
495
+ case "error-json":
496
+ default:
497
+ contentValue = JSON.stringify(output.value);
498
+ break;
499
+ }
335
500
  anthropicContent.push({
336
501
  type: "tool_result",
337
502
  tool_use_id: part.toolCallId,
338
- content: toolResultContent,
339
- is_error: part.isError,
503
+ content: contentValue,
504
+ is_error: output.type === "error-text" || output.type === "error-json" ? true : void 0,
340
505
  cache_control: cacheControl
341
506
  });
342
507
  }
@@ -360,7 +525,7 @@ async function convertToAnthropicMessagesPrompt({
360
525
  for (let k = 0; k < content.length; k++) {
361
526
  const part = content[k];
362
527
  const isLastContentPart = k === content.length - 1;
363
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
528
+ const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
364
529
  switch (part.type) {
365
530
  case "text": {
366
531
  anthropicContent.push({
@@ -417,15 +582,65 @@ async function convertToAnthropicMessagesPrompt({
417
582
  break;
418
583
  }
419
584
  case "tool-call": {
585
+ if (part.providerExecuted) {
586
+ if (part.toolName === "web_search") {
587
+ anthropicContent.push({
588
+ type: "server_tool_use",
589
+ id: part.toolCallId,
590
+ name: "web_search",
591
+ input: part.input,
592
+ cache_control: cacheControl
593
+ });
594
+ break;
595
+ }
596
+ warnings.push({
597
+ type: "other",
598
+ message: `provider executed tool call for tool ${part.toolName} is not supported`
599
+ });
600
+ break;
601
+ }
420
602
  anthropicContent.push({
421
603
  type: "tool_use",
422
604
  id: part.toolCallId,
423
605
  name: part.toolName,
424
- input: part.args,
606
+ input: part.input,
425
607
  cache_control: cacheControl
426
608
  });
427
609
  break;
428
610
  }
611
+ case "tool-result": {
612
+ if (part.toolName === "web_search") {
613
+ const output = part.output;
614
+ if (output.type !== "json") {
615
+ warnings.push({
616
+ type: "other",
617
+ message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
618
+ });
619
+ break;
620
+ }
621
+ const webSearchOutput = webSearch_20250305OutputSchema.parse(
622
+ output.value
623
+ );
624
+ anthropicContent.push({
625
+ type: "web_search_tool_result",
626
+ tool_use_id: part.toolCallId,
627
+ content: webSearchOutput.map((result) => ({
628
+ url: result.url,
629
+ title: result.title,
630
+ page_age: result.pageAge,
631
+ encrypted_content: result.encryptedContent,
632
+ type: result.type
633
+ })),
634
+ cache_control: cacheControl
635
+ });
636
+ break;
637
+ }
638
+ warnings.push({
639
+ type: "other",
640
+ message: `provider executed tool result for tool ${part.toolName} is not supported`
641
+ });
642
+ break;
643
+ }
429
644
  }
430
645
  }
431
646
  }
@@ -509,6 +724,79 @@ function mapAnthropicStopReason({
509
724
  }
510
725
 
511
726
  // src/anthropic-messages-language-model.ts
727
+ var citationSchemas = {
728
+ webSearchResult: z4.object({
729
+ type: z4.literal("web_search_result_location"),
730
+ cited_text: z4.string(),
731
+ url: z4.string(),
732
+ title: z4.string(),
733
+ encrypted_index: z4.string()
734
+ }),
735
+ pageLocation: z4.object({
736
+ type: z4.literal("page_location"),
737
+ cited_text: z4.string(),
738
+ document_index: z4.number(),
739
+ document_title: z4.string().nullable(),
740
+ start_page_number: z4.number(),
741
+ end_page_number: z4.number()
742
+ }),
743
+ charLocation: z4.object({
744
+ type: z4.literal("char_location"),
745
+ cited_text: z4.string(),
746
+ document_index: z4.number(),
747
+ document_title: z4.string().nullable(),
748
+ start_char_index: z4.number(),
749
+ end_char_index: z4.number()
750
+ })
751
+ };
752
+ var citationSchema = z4.discriminatedUnion("type", [
753
+ citationSchemas.webSearchResult,
754
+ citationSchemas.pageLocation,
755
+ citationSchemas.charLocation
756
+ ]);
757
+ var documentCitationSchema = z4.discriminatedUnion("type", [
758
+ citationSchemas.pageLocation,
759
+ citationSchemas.charLocation
760
+ ]);
761
+ function processCitation(citation, citationDocuments, generateId2, onSource) {
762
+ if (citation.type === "page_location" || citation.type === "char_location") {
763
+ const source = createCitationSource(
764
+ citation,
765
+ citationDocuments,
766
+ generateId2
767
+ );
768
+ if (source) {
769
+ onSource(source);
770
+ }
771
+ }
772
+ }
773
+ function createCitationSource(citation, citationDocuments, generateId2) {
774
+ var _a;
775
+ const documentInfo = citationDocuments[citation.document_index];
776
+ if (!documentInfo) {
777
+ return null;
778
+ }
779
+ const providerMetadata = citation.type === "page_location" ? {
780
+ citedText: citation.cited_text,
781
+ startPageNumber: citation.start_page_number,
782
+ endPageNumber: citation.end_page_number
783
+ } : {
784
+ citedText: citation.cited_text,
785
+ startCharIndex: citation.start_char_index,
786
+ endCharIndex: citation.end_char_index
787
+ };
788
+ return {
789
+ type: "source",
790
+ sourceType: "document",
791
+ id: generateId2(),
792
+ mediaType: documentInfo.mediaType,
793
+ title: (_a = citation.document_title) != null ? _a : documentInfo.title,
794
+ filename: documentInfo.filename,
795
+ providerMetadata: {
796
+ anthropic: providerMetadata
797
+ }
798
+ };
799
+ }
512
800
  var AnthropicMessagesLanguageModel = class {
513
801
  constructor(modelId, config) {
514
802
  this.specificationVersion = "v2";
@@ -582,7 +870,7 @@ var AnthropicMessagesLanguageModel = class {
582
870
  type: "function",
583
871
  name: "json",
584
872
  description: "Respond with a JSON object.",
585
- parameters: responseFormat.schema
873
+ inputSchema: responseFormat.schema
586
874
  } : void 0;
587
875
  const anthropicOptions = await parseProviderOptions2({
588
876
  provider: "anthropic",
@@ -645,27 +933,6 @@ var AnthropicMessagesLanguageModel = class {
645
933
  }
646
934
  baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
647
935
  }
648
- let modifiedTools = tools;
649
- let modifiedToolChoice = toolChoice;
650
- if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
651
- const webSearchTool = {
652
- type: "web_search_20250305",
653
- name: "web_search",
654
- max_uses: anthropicOptions.webSearch.maxUses,
655
- allowed_domains: anthropicOptions.webSearch.allowedDomains,
656
- blocked_domains: anthropicOptions.webSearch.blockedDomains,
657
- ...anthropicOptions.webSearch.userLocation && {
658
- user_location: {
659
- type: anthropicOptions.webSearch.userLocation.type,
660
- country: anthropicOptions.webSearch.userLocation.country,
661
- city: anthropicOptions.webSearch.userLocation.city,
662
- region: anthropicOptions.webSearch.userLocation.region,
663
- timezone: anthropicOptions.webSearch.userLocation.timezone
664
- }
665
- }
666
- };
667
- modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
668
- }
669
936
  const {
670
937
  tools: anthropicTools2,
671
938
  toolChoice: anthropicToolChoice,
@@ -674,8 +941,13 @@ var AnthropicMessagesLanguageModel = class {
674
941
  } = prepareTools(
675
942
  jsonResponseTool != null ? {
676
943
  tools: [jsonResponseTool],
677
- toolChoice: { type: "tool", toolName: jsonResponseTool.name }
678
- } : { tools: modifiedTools, toolChoice: modifiedToolChoice }
944
+ toolChoice: { type: "tool", toolName: jsonResponseTool.name },
945
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
946
+ } : {
947
+ tools: tools != null ? tools : [],
948
+ toolChoice,
949
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
950
+ }
679
951
  );
680
952
  return {
681
953
  args: {
@@ -685,7 +957,7 @@ var AnthropicMessagesLanguageModel = class {
685
957
  },
686
958
  warnings: [...warnings, ...toolWarnings],
687
959
  betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
688
- jsonResponseTool
960
+ usesJsonResponseTool: jsonResponseTool != null
689
961
  };
690
962
  }
691
963
  async getHeaders({
@@ -706,9 +978,33 @@ var AnthropicMessagesLanguageModel = class {
706
978
  var _a, _b, _c;
707
979
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
708
980
  }
981
+ extractCitationDocuments(prompt) {
982
+ const isCitationPart = (part) => {
983
+ var _a, _b;
984
+ if (part.type !== "file") {
985
+ return false;
986
+ }
987
+ if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
988
+ return false;
989
+ }
990
+ const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
991
+ const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
992
+ return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
993
+ };
994
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
995
+ var _a;
996
+ const filePart = part;
997
+ return {
998
+ title: (_a = filePart.filename) != null ? _a : "Untitled Document",
999
+ filename: filePart.filename,
1000
+ mediaType: filePart.mediaType
1001
+ };
1002
+ });
1003
+ }
709
1004
  async doGenerate(options) {
710
1005
  var _a, _b, _c, _d, _e;
711
- const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
1006
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1007
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
712
1008
  const {
713
1009
  responseHeaders,
714
1010
  value: response,
@@ -728,8 +1024,18 @@ var AnthropicMessagesLanguageModel = class {
728
1024
  for (const part of response.content) {
729
1025
  switch (part.type) {
730
1026
  case "text": {
731
- if (jsonResponseTool == null) {
1027
+ if (!usesJsonResponseTool) {
732
1028
  content.push({ type: "text", text: part.text });
1029
+ if (part.citations) {
1030
+ for (const citation of part.citations) {
1031
+ processCitation(
1032
+ citation,
1033
+ citationDocuments,
1034
+ this.generateId,
1035
+ (source) => content.push(source)
1036
+ );
1037
+ }
1038
+ }
733
1039
  }
734
1040
  break;
735
1041
  }
@@ -760,47 +1066,73 @@ var AnthropicMessagesLanguageModel = class {
760
1066
  case "tool_use": {
761
1067
  content.push(
762
1068
  // when a json response tool is used, the tool call becomes the text:
763
- jsonResponseTool != null ? {
1069
+ usesJsonResponseTool ? {
764
1070
  type: "text",
765
1071
  text: JSON.stringify(part.input)
766
1072
  } : {
767
1073
  type: "tool-call",
768
- toolCallType: "function",
769
1074
  toolCallId: part.id,
770
1075
  toolName: part.name,
771
- args: JSON.stringify(part.input)
1076
+ input: JSON.stringify(part.input)
772
1077
  }
773
1078
  );
774
1079
  break;
775
1080
  }
776
1081
  case "server_tool_use": {
777
- continue;
1082
+ if (part.name === "web_search") {
1083
+ content.push({
1084
+ type: "tool-call",
1085
+ toolCallId: part.id,
1086
+ toolName: part.name,
1087
+ input: JSON.stringify(part.input),
1088
+ providerExecuted: true
1089
+ });
1090
+ }
1091
+ break;
778
1092
  }
779
1093
  case "web_search_tool_result": {
780
1094
  if (Array.isArray(part.content)) {
781
- for (const result of part.content) {
782
- if (result.type === "web_search_result") {
783
- content.push({
784
- type: "source",
785
- sourceType: "url",
786
- id: this.generateId(),
1095
+ content.push({
1096
+ type: "tool-result",
1097
+ toolCallId: part.tool_use_id,
1098
+ toolName: "web_search",
1099
+ result: part.content.map((result) => {
1100
+ var _a2;
1101
+ return {
787
1102
  url: result.url,
788
1103
  title: result.title,
789
- providerMetadata: {
790
- anthropic: {
791
- encryptedContent: result.encrypted_content,
792
- pageAge: (_a = result.page_age) != null ? _a : null
793
- }
1104
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
1105
+ encryptedContent: result.encrypted_content,
1106
+ type: result.type
1107
+ };
1108
+ }),
1109
+ providerExecuted: true
1110
+ });
1111
+ for (const result of part.content) {
1112
+ content.push({
1113
+ type: "source",
1114
+ sourceType: "url",
1115
+ id: this.generateId(),
1116
+ url: result.url,
1117
+ title: result.title,
1118
+ providerMetadata: {
1119
+ anthropic: {
1120
+ pageAge: (_a = result.page_age) != null ? _a : null
794
1121
  }
795
- });
796
- }
1122
+ }
1123
+ });
797
1124
  }
798
- } else if (part.content.type === "web_search_tool_result_error") {
799
- throw new APICallError({
800
- message: `Web search failed: ${part.content.error_code}`,
801
- url: "web_search_api",
802
- requestBodyValues: { tool_use_id: part.tool_use_id },
803
- data: { error_code: part.content.error_code }
1125
+ } else {
1126
+ content.push({
1127
+ type: "tool-result",
1128
+ toolCallId: part.tool_use_id,
1129
+ toolName: "web_search",
1130
+ isError: true,
1131
+ result: {
1132
+ type: "web_search_tool_result_error",
1133
+ errorCode: part.content.error_code
1134
+ },
1135
+ providerExecuted: true
804
1136
  });
805
1137
  }
806
1138
  break;
@@ -811,7 +1143,7 @@ var AnthropicMessagesLanguageModel = class {
811
1143
  content,
812
1144
  finishReason: mapAnthropicStopReason({
813
1145
  finishReason: response.stop_reason,
814
- isJsonResponseFromTool: jsonResponseTool != null
1146
+ isJsonResponseFromTool: usesJsonResponseTool
815
1147
  }),
816
1148
  usage: {
817
1149
  inputTokens: response.usage.input_tokens,
@@ -829,13 +1161,15 @@ var AnthropicMessagesLanguageModel = class {
829
1161
  warnings,
830
1162
  providerMetadata: {
831
1163
  anthropic: {
1164
+ usage: response.usage,
832
1165
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
833
1166
  }
834
1167
  }
835
1168
  };
836
1169
  }
837
1170
  async doStream(options) {
838
- const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
1171
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
1172
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
839
1173
  const body = { ...args, stream: true };
840
1174
  const { responseHeaders, value: response } = await postJsonToApi({
841
1175
  url: this.buildRequestUrl(true),
@@ -854,10 +1188,9 @@ var AnthropicMessagesLanguageModel = class {
854
1188
  outputTokens: void 0,
855
1189
  totalTokens: void 0
856
1190
  };
857
- const toolCallContentBlocks = {};
1191
+ const contentBlocks = {};
858
1192
  let providerMetadata = void 0;
859
1193
  let blockType = void 0;
860
- const config = this.config;
861
1194
  const generateId2 = this.generateId;
862
1195
  return {
863
1196
  stream: response.pipeThrough(
@@ -867,6 +1200,9 @@ var AnthropicMessagesLanguageModel = class {
867
1200
  },
868
1201
  transform(chunk, controller) {
869
1202
  var _a, _b, _c, _d, _e, _f, _g;
1203
+ if (options.includeRawChunks) {
1204
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1205
+ }
870
1206
  if (!chunk.success) {
871
1207
  controller.enqueue({ type: "error", error: chunk.error });
872
1208
  return;
@@ -880,61 +1216,113 @@ var AnthropicMessagesLanguageModel = class {
880
1216
  const contentBlockType = value.content_block.type;
881
1217
  blockType = contentBlockType;
882
1218
  switch (contentBlockType) {
883
- case "text":
1219
+ case "text": {
1220
+ contentBlocks[value.index] = { type: "text" };
1221
+ controller.enqueue({
1222
+ type: "text-start",
1223
+ id: String(value.index)
1224
+ });
1225
+ return;
1226
+ }
884
1227
  case "thinking": {
1228
+ contentBlocks[value.index] = { type: "reasoning" };
1229
+ controller.enqueue({
1230
+ type: "reasoning-start",
1231
+ id: String(value.index)
1232
+ });
885
1233
  return;
886
1234
  }
887
1235
  case "redacted_thinking": {
1236
+ contentBlocks[value.index] = { type: "reasoning" };
888
1237
  controller.enqueue({
889
- type: "reasoning",
890
- text: "",
1238
+ type: "reasoning-start",
1239
+ id: String(value.index),
891
1240
  providerMetadata: {
892
1241
  anthropic: {
893
1242
  redactedData: value.content_block.data
894
1243
  }
895
1244
  }
896
1245
  });
897
- controller.enqueue({ type: "reasoning-part-finish" });
898
1246
  return;
899
1247
  }
900
1248
  case "tool_use": {
901
- toolCallContentBlocks[value.index] = {
1249
+ contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
1250
+ type: "tool-call",
902
1251
  toolCallId: value.content_block.id,
903
1252
  toolName: value.content_block.name,
904
- jsonText: ""
1253
+ input: ""
905
1254
  };
1255
+ controller.enqueue(
1256
+ usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
1257
+ type: "tool-input-start",
1258
+ id: value.content_block.id,
1259
+ toolName: value.content_block.name
1260
+ }
1261
+ );
906
1262
  return;
907
1263
  }
908
1264
  case "server_tool_use": {
1265
+ if (value.content_block.name === "web_search") {
1266
+ contentBlocks[value.index] = {
1267
+ type: "tool-call",
1268
+ toolCallId: value.content_block.id,
1269
+ toolName: value.content_block.name,
1270
+ input: "",
1271
+ providerExecuted: true
1272
+ };
1273
+ controller.enqueue({
1274
+ type: "tool-input-start",
1275
+ id: value.content_block.id,
1276
+ toolName: value.content_block.name,
1277
+ providerExecuted: true
1278
+ });
1279
+ }
909
1280
  return;
910
1281
  }
911
1282
  case "web_search_tool_result": {
912
- if (Array.isArray(value.content_block.content)) {
913
- for (const result of value.content_block.content) {
914
- if (result.type === "web_search_result") {
915
- controller.enqueue({
916
- type: "source",
917
- sourceType: "url",
918
- id: generateId2(),
1283
+ const part = value.content_block;
1284
+ if (Array.isArray(part.content)) {
1285
+ controller.enqueue({
1286
+ type: "tool-result",
1287
+ toolCallId: part.tool_use_id,
1288
+ toolName: "web_search",
1289
+ result: part.content.map((result) => {
1290
+ var _a2;
1291
+ return {
919
1292
  url: result.url,
920
1293
  title: result.title,
921
- providerMetadata: {
922
- anthropic: {
923
- encryptedContent: result.encrypted_content,
924
- pageAge: (_a = result.page_age) != null ? _a : null
925
- }
1294
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
1295
+ encryptedContent: result.encrypted_content,
1296
+ type: result.type
1297
+ };
1298
+ }),
1299
+ providerExecuted: true
1300
+ });
1301
+ for (const result of part.content) {
1302
+ controller.enqueue({
1303
+ type: "source",
1304
+ sourceType: "url",
1305
+ id: generateId2(),
1306
+ url: result.url,
1307
+ title: result.title,
1308
+ providerMetadata: {
1309
+ anthropic: {
1310
+ pageAge: (_a = result.page_age) != null ? _a : null
926
1311
  }
927
- });
928
- }
1312
+ }
1313
+ });
929
1314
  }
930
- } else if (value.content_block.content.type === "web_search_tool_result_error") {
1315
+ } else {
931
1316
  controller.enqueue({
932
- type: "error",
933
- error: {
934
- type: "web-search-error",
935
- message: `Web search failed: ${value.content_block.content.error_code}`,
936
- code: value.content_block.content.error_code
937
- }
1317
+ type: "tool-result",
1318
+ toolCallId: part.tool_use_id,
1319
+ toolName: "web_search",
1320
+ isError: true,
1321
+ result: {
1322
+ type: "web_search_tool_result_error",
1323
+ errorCode: part.content.error_code
1324
+ },
1325
+ providerExecuted: true
938
1326
  });
939
1327
  }
940
1328
  return;
@@ -948,18 +1336,34 @@ var AnthropicMessagesLanguageModel = class {
948
1336
  }
949
1337
  }
950
1338
  case "content_block_stop": {
951
- if (toolCallContentBlocks[value.index] != null) {
952
- const contentBlock = toolCallContentBlocks[value.index];
953
- if (jsonResponseTool == null) {
954
- controller.enqueue({
955
- type: "tool-call",
956
- toolCallType: "function",
957
- toolCallId: contentBlock.toolCallId,
958
- toolName: contentBlock.toolName,
959
- args: contentBlock.jsonText
960
- });
1339
+ if (contentBlocks[value.index] != null) {
1340
+ const contentBlock = contentBlocks[value.index];
1341
+ switch (contentBlock.type) {
1342
+ case "text": {
1343
+ controller.enqueue({
1344
+ type: "text-end",
1345
+ id: String(value.index)
1346
+ });
1347
+ break;
1348
+ }
1349
+ case "reasoning": {
1350
+ controller.enqueue({
1351
+ type: "reasoning-end",
1352
+ id: String(value.index)
1353
+ });
1354
+ break;
1355
+ }
1356
+ case "tool-call":
1357
+ if (!usesJsonResponseTool) {
1358
+ controller.enqueue({
1359
+ type: "tool-input-end",
1360
+ id: contentBlock.toolCallId
1361
+ });
1362
+ controller.enqueue(contentBlock);
1363
+ }
1364
+ break;
961
1365
  }
962
- delete toolCallContentBlocks[value.index];
1366
+ delete contentBlocks[value.index];
963
1367
  }
964
1368
  blockType = void 0;
965
1369
  return;
@@ -968,58 +1372,72 @@ var AnthropicMessagesLanguageModel = class {
968
1372
  const deltaType = value.delta.type;
969
1373
  switch (deltaType) {
970
1374
  case "text_delta": {
971
- if (jsonResponseTool != null) {
1375
+ if (usesJsonResponseTool) {
972
1376
  return;
973
1377
  }
974
1378
  controller.enqueue({
975
- type: "text",
976
- text: value.delta.text
1379
+ type: "text-delta",
1380
+ id: String(value.index),
1381
+ delta: value.delta.text
977
1382
  });
978
1383
  return;
979
1384
  }
980
1385
  case "thinking_delta": {
981
1386
  controller.enqueue({
982
- type: "reasoning",
983
- text: value.delta.thinking
1387
+ type: "reasoning-delta",
1388
+ id: String(value.index),
1389
+ delta: value.delta.thinking
984
1390
  });
985
1391
  return;
986
1392
  }
987
1393
  case "signature_delta": {
988
1394
  if (blockType === "thinking") {
989
1395
  controller.enqueue({
990
- type: "reasoning",
991
- text: "",
1396
+ type: "reasoning-delta",
1397
+ id: String(value.index),
1398
+ delta: "",
992
1399
  providerMetadata: {
993
1400
  anthropic: {
994
1401
  signature: value.delta.signature
995
1402
  }
996
1403
  }
997
1404
  });
998
- controller.enqueue({ type: "reasoning-part-finish" });
999
1405
  }
1000
1406
  return;
1001
1407
  }
1002
1408
  case "input_json_delta": {
1003
- const contentBlock = toolCallContentBlocks[value.index];
1004
- if (!contentBlock) {
1005
- return;
1006
- }
1007
- controller.enqueue(
1008
- jsonResponseTool != null ? {
1009
- type: "text",
1010
- text: value.delta.partial_json
1011
- } : {
1012
- type: "tool-call-delta",
1013
- toolCallType: "function",
1014
- toolCallId: contentBlock.toolCallId,
1015
- toolName: contentBlock.toolName,
1016
- argsTextDelta: value.delta.partial_json
1409
+ const contentBlock = contentBlocks[value.index];
1410
+ const delta = value.delta.partial_json;
1411
+ if (usesJsonResponseTool) {
1412
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
1413
+ return;
1017
1414
  }
1018
- );
1019
- contentBlock.jsonText += value.delta.partial_json;
1415
+ controller.enqueue({
1416
+ type: "text-delta",
1417
+ id: String(value.index),
1418
+ delta
1419
+ });
1420
+ } else {
1421
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
1422
+ return;
1423
+ }
1424
+ controller.enqueue({
1425
+ type: "tool-input-delta",
1426
+ id: contentBlock.toolCallId,
1427
+ delta
1428
+ });
1429
+ contentBlock.input += delta;
1430
+ }
1020
1431
  return;
1021
1432
  }
1022
1433
  case "citations_delta": {
1434
+ const citation = value.delta.citation;
1435
+ processCitation(
1436
+ citation,
1437
+ citationDocuments,
1438
+ generateId2,
1439
+ (source) => controller.enqueue(source)
1440
+ );
1023
1441
  return;
1024
1442
  }
1025
1443
  default: {
@@ -1035,6 +1453,7 @@ var AnthropicMessagesLanguageModel = class {
1035
1453
  usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
1036
1454
  providerMetadata = {
1037
1455
  anthropic: {
1456
+ usage: value.message.usage,
1038
1457
  cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
1039
1458
  }
1040
1459
  };
@@ -1050,7 +1469,7 @@ var AnthropicMessagesLanguageModel = class {
1050
1469
  usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
1051
1470
  finishReason = mapAnthropicStopReason({
1052
1471
  finishReason: value.delta.stop_reason,
1053
- isJsonResponseFromTool: jsonResponseTool != null
1472
+ isJsonResponseFromTool: usesJsonResponseTool
1054
1473
  });
1055
1474
  return;
1056
1475
  }
@@ -1080,336 +1499,378 @@ var AnthropicMessagesLanguageModel = class {
1080
1499
  };
1081
1500
  }
1082
1501
  };
1083
- var anthropicMessagesResponseSchema = z3.object({
1084
- type: z3.literal("message"),
1085
- id: z3.string().nullish(),
1086
- model: z3.string().nullish(),
1087
- content: z3.array(
1088
- z3.discriminatedUnion("type", [
1089
- z3.object({
1090
- type: z3.literal("text"),
1091
- text: z3.string()
1502
+ var anthropicMessagesResponseSchema = z4.object({
1503
+ type: z4.literal("message"),
1504
+ id: z4.string().nullish(),
1505
+ model: z4.string().nullish(),
1506
+ content: z4.array(
1507
+ z4.discriminatedUnion("type", [
1508
+ z4.object({
1509
+ type: z4.literal("text"),
1510
+ text: z4.string(),
1511
+ citations: z4.array(citationSchema).optional()
1092
1512
  }),
1093
- z3.object({
1094
- type: z3.literal("thinking"),
1095
- thinking: z3.string(),
1096
- signature: z3.string()
1513
+ z4.object({
1514
+ type: z4.literal("thinking"),
1515
+ thinking: z4.string(),
1516
+ signature: z4.string()
1097
1517
  }),
1098
- z3.object({
1099
- type: z3.literal("redacted_thinking"),
1100
- data: z3.string()
1518
+ z4.object({
1519
+ type: z4.literal("redacted_thinking"),
1520
+ data: z4.string()
1101
1521
  }),
1102
- z3.object({
1103
- type: z3.literal("tool_use"),
1104
- id: z3.string(),
1105
- name: z3.string(),
1106
- input: z3.unknown()
1522
+ z4.object({
1523
+ type: z4.literal("tool_use"),
1524
+ id: z4.string(),
1525
+ name: z4.string(),
1526
+ input: z4.unknown()
1107
1527
  }),
1108
- z3.object({
1109
- type: z3.literal("server_tool_use"),
1110
- id: z3.string(),
1111
- name: z3.string(),
1112
- input: z3.record(z3.unknown()).nullish()
1528
+ z4.object({
1529
+ type: z4.literal("server_tool_use"),
1530
+ id: z4.string(),
1531
+ name: z4.string(),
1532
+ input: z4.record(z4.string(), z4.unknown()).nullish()
1113
1533
  }),
1114
- z3.object({
1115
- type: z3.literal("web_search_tool_result"),
1116
- tool_use_id: z3.string(),
1117
- content: z3.union([
1118
- z3.array(
1119
- z3.object({
1120
- type: z3.literal("web_search_result"),
1121
- url: z3.string(),
1122
- title: z3.string(),
1123
- encrypted_content: z3.string(),
1124
- page_age: z3.string().nullish()
1534
+ z4.object({
1535
+ type: z4.literal("web_search_tool_result"),
1536
+ tool_use_id: z4.string(),
1537
+ content: z4.union([
1538
+ z4.array(
1539
+ z4.object({
1540
+ type: z4.literal("web_search_result"),
1541
+ url: z4.string(),
1542
+ title: z4.string(),
1543
+ encrypted_content: z4.string(),
1544
+ page_age: z4.string().nullish()
1125
1545
  })
1126
1546
  ),
1127
- z3.object({
1128
- type: z3.literal("web_search_tool_result_error"),
1129
- error_code: z3.string()
1547
+ z4.object({
1548
+ type: z4.literal("web_search_tool_result_error"),
1549
+ error_code: z4.string()
1130
1550
  })
1131
1551
  ])
1132
1552
  })
1133
1553
  ])
1134
1554
  ),
1135
- stop_reason: z3.string().nullish(),
1136
- usage: z3.object({
1137
- input_tokens: z3.number(),
1138
- output_tokens: z3.number(),
1139
- cache_creation_input_tokens: z3.number().nullish(),
1140
- cache_read_input_tokens: z3.number().nullish(),
1141
- server_tool_use: z3.object({
1142
- web_search_requests: z3.number()
1143
- }).nullish()
1555
+ stop_reason: z4.string().nullish(),
1556
+ usage: z4.looseObject({
1557
+ input_tokens: z4.number(),
1558
+ output_tokens: z4.number(),
1559
+ cache_creation_input_tokens: z4.number().nullish(),
1560
+ cache_read_input_tokens: z4.number().nullish()
1144
1561
  })
1145
1562
  });
1146
- var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1147
- z3.object({
1148
- type: z3.literal("message_start"),
1149
- message: z3.object({
1150
- id: z3.string().nullish(),
1151
- model: z3.string().nullish(),
1152
- usage: z3.object({
1153
- input_tokens: z3.number(),
1154
- output_tokens: z3.number(),
1155
- cache_creation_input_tokens: z3.number().nullish(),
1156
- cache_read_input_tokens: z3.number().nullish()
1563
+ var anthropicMessagesChunkSchema = z4.discriminatedUnion("type", [
1564
+ z4.object({
1565
+ type: z4.literal("message_start"),
1566
+ message: z4.object({
1567
+ id: z4.string().nullish(),
1568
+ model: z4.string().nullish(),
1569
+ usage: z4.looseObject({
1570
+ input_tokens: z4.number(),
1571
+ output_tokens: z4.number(),
1572
+ cache_creation_input_tokens: z4.number().nullish(),
1573
+ cache_read_input_tokens: z4.number().nullish()
1157
1574
  })
1158
1575
  })
1159
1576
  }),
1160
- z3.object({
1161
- type: z3.literal("content_block_start"),
1162
- index: z3.number(),
1163
- content_block: z3.discriminatedUnion("type", [
1164
- z3.object({
1165
- type: z3.literal("text"),
1166
- text: z3.string()
1577
+ z4.object({
1578
+ type: z4.literal("content_block_start"),
1579
+ index: z4.number(),
1580
+ content_block: z4.discriminatedUnion("type", [
1581
+ z4.object({
1582
+ type: z4.literal("text"),
1583
+ text: z4.string()
1167
1584
  }),
1168
- z3.object({
1169
- type: z3.literal("thinking"),
1170
- thinking: z3.string()
1585
+ z4.object({
1586
+ type: z4.literal("thinking"),
1587
+ thinking: z4.string()
1171
1588
  }),
1172
- z3.object({
1173
- type: z3.literal("tool_use"),
1174
- id: z3.string(),
1175
- name: z3.string()
1589
+ z4.object({
1590
+ type: z4.literal("tool_use"),
1591
+ id: z4.string(),
1592
+ name: z4.string()
1176
1593
  }),
1177
- z3.object({
1178
- type: z3.literal("redacted_thinking"),
1179
- data: z3.string()
1594
+ z4.object({
1595
+ type: z4.literal("redacted_thinking"),
1596
+ data: z4.string()
1180
1597
  }),
1181
- z3.object({
1182
- type: z3.literal("server_tool_use"),
1183
- id: z3.string(),
1184
- name: z3.string(),
1185
- input: z3.record(z3.unknown()).nullish()
1598
+ z4.object({
1599
+ type: z4.literal("server_tool_use"),
1600
+ id: z4.string(),
1601
+ name: z4.string(),
1602
+ input: z4.record(z4.string(), z4.unknown()).nullish()
1186
1603
  }),
1187
- z3.object({
1188
- type: z3.literal("web_search_tool_result"),
1189
- tool_use_id: z3.string(),
1190
- content: z3.union([
1191
- z3.array(
1192
- z3.object({
1193
- type: z3.literal("web_search_result"),
1194
- url: z3.string(),
1195
- title: z3.string(),
1196
- encrypted_content: z3.string(),
1197
- page_age: z3.string().nullish()
1604
+ z4.object({
1605
+ type: z4.literal("web_search_tool_result"),
1606
+ tool_use_id: z4.string(),
1607
+ content: z4.union([
1608
+ z4.array(
1609
+ z4.object({
1610
+ type: z4.literal("web_search_result"),
1611
+ url: z4.string(),
1612
+ title: z4.string(),
1613
+ encrypted_content: z4.string(),
1614
+ page_age: z4.string().nullish()
1198
1615
  })
1199
1616
  ),
1200
- z3.object({
1201
- type: z3.literal("web_search_tool_result_error"),
1202
- error_code: z3.string()
1617
+ z4.object({
1618
+ type: z4.literal("web_search_tool_result_error"),
1619
+ error_code: z4.string()
1203
1620
  })
1204
1621
  ])
1205
1622
  })
1206
1623
  ])
1207
1624
  }),
1208
- z3.object({
1209
- type: z3.literal("content_block_delta"),
1210
- index: z3.number(),
1211
- delta: z3.discriminatedUnion("type", [
1212
- z3.object({
1213
- type: z3.literal("input_json_delta"),
1214
- partial_json: z3.string()
1625
+ z4.object({
1626
+ type: z4.literal("content_block_delta"),
1627
+ index: z4.number(),
1628
+ delta: z4.discriminatedUnion("type", [
1629
+ z4.object({
1630
+ type: z4.literal("input_json_delta"),
1631
+ partial_json: z4.string()
1215
1632
  }),
1216
- z3.object({
1217
- type: z3.literal("text_delta"),
1218
- text: z3.string()
1633
+ z4.object({
1634
+ type: z4.literal("text_delta"),
1635
+ text: z4.string()
1219
1636
  }),
1220
- z3.object({
1221
- type: z3.literal("thinking_delta"),
1222
- thinking: z3.string()
1637
+ z4.object({
1638
+ type: z4.literal("thinking_delta"),
1639
+ thinking: z4.string()
1223
1640
  }),
1224
- z3.object({
1225
- type: z3.literal("signature_delta"),
1226
- signature: z3.string()
1641
+ z4.object({
1642
+ type: z4.literal("signature_delta"),
1643
+ signature: z4.string()
1227
1644
  }),
1228
- z3.object({
1229
- type: z3.literal("citations_delta"),
1230
- citation: z3.object({
1231
- type: z3.literal("web_search_result_location"),
1232
- cited_text: z3.string(),
1233
- url: z3.string(),
1234
- title: z3.string(),
1235
- encrypted_index: z3.string()
1236
- })
1645
+ z4.object({
1646
+ type: z4.literal("citations_delta"),
1647
+ citation: citationSchema
1237
1648
  })
1238
1649
  ])
1239
1650
  }),
1240
- z3.object({
1241
- type: z3.literal("content_block_stop"),
1242
- index: z3.number()
1651
+ z4.object({
1652
+ type: z4.literal("content_block_stop"),
1653
+ index: z4.number()
1243
1654
  }),
1244
- z3.object({
1245
- type: z3.literal("error"),
1246
- error: z3.object({
1247
- type: z3.string(),
1248
- message: z3.string()
1655
+ z4.object({
1656
+ type: z4.literal("error"),
1657
+ error: z4.object({
1658
+ type: z4.string(),
1659
+ message: z4.string()
1249
1660
  })
1250
1661
  }),
1251
- z3.object({
1252
- type: z3.literal("message_delta"),
1253
- delta: z3.object({ stop_reason: z3.string().nullish() }),
1254
- usage: z3.object({ output_tokens: z3.number() })
1662
+ z4.object({
1663
+ type: z4.literal("message_delta"),
1664
+ delta: z4.object({ stop_reason: z4.string().nullish() }),
1665
+ usage: z4.object({ output_tokens: z4.number() })
1255
1666
  }),
1256
- z3.object({
1257
- type: z3.literal("message_stop")
1667
+ z4.object({
1668
+ type: z4.literal("message_stop")
1258
1669
  }),
1259
- z3.object({
1260
- type: z3.literal("ping")
1670
+ z4.object({
1671
+ type: z4.literal("ping")
1261
1672
  })
1262
1673
  ]);
1263
- var anthropicReasoningMetadataSchema = z3.object({
1264
- signature: z3.string().optional(),
1265
- redactedData: z3.string().optional()
1674
+ var anthropicReasoningMetadataSchema = z4.object({
1675
+ signature: z4.string().optional(),
1676
+ redactedData: z4.string().optional()
1266
1677
  });
1267
1678
 
1268
- // src/anthropic-tools.ts
1269
- import { z as z4 } from "zod";
1270
- var Bash20241022Parameters = z4.object({
1271
- command: z4.string(),
1272
- restart: z4.boolean().optional()
1679
+ // src/tool/bash_20241022.ts
1680
+ import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
1681
+ import z5 from "zod/v4";
1682
+ var bash_20241022 = createProviderDefinedToolFactory({
1683
+ id: "anthropic.bash_20241022",
1684
+ name: "bash",
1685
+ inputSchema: z5.object({
1686
+ command: z5.string(),
1687
+ restart: z5.boolean().optional()
1688
+ })
1273
1689
  });
1274
- function bashTool_20241022(options = {}) {
1275
- return {
1276
- type: "provider-defined",
1277
- id: "anthropic.bash_20241022",
1278
- args: {},
1279
- parameters: Bash20241022Parameters,
1280
- execute: options.execute,
1281
- experimental_toToolResultContent: options.experimental_toToolResultContent
1282
- };
1283
- }
1284
- var Bash20250124Parameters = z4.object({
1285
- command: z4.string(),
1286
- restart: z4.boolean().optional()
1690
+
1691
+ // src/tool/bash_20250124.ts
1692
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
1693
+ import z6 from "zod/v4";
1694
+ var bash_20250124 = createProviderDefinedToolFactory2({
1695
+ id: "anthropic.bashTool_20250124",
1696
+ name: "bash",
1697
+ inputSchema: z6.object({
1698
+ command: z6.string(),
1699
+ restart: z6.boolean().optional()
1700
+ })
1287
1701
  });
1288
- function bashTool_20250124(options = {}) {
1289
- return {
1290
- type: "provider-defined",
1291
- id: "anthropic.bash_20250124",
1292
- args: {},
1293
- parameters: Bash20250124Parameters,
1294
- execute: options.execute,
1295
- experimental_toToolResultContent: options.experimental_toToolResultContent
1296
- };
1297
- }
1298
- var TextEditor20241022Parameters = z4.object({
1299
- command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1300
- path: z4.string(),
1301
- file_text: z4.string().optional(),
1302
- insert_line: z4.number().int().optional(),
1303
- new_str: z4.string().optional(),
1304
- old_str: z4.string().optional(),
1305
- view_range: z4.array(z4.number().int()).optional()
1702
+
1703
+ // src/tool/computer_20241022.ts
1704
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory3 } from "@ai-sdk/provider-utils";
1705
+ import { z as z7 } from "zod/v4";
1706
+ var computer_20241022 = createProviderDefinedToolFactory3({
1707
+ id: "anthropic.computer_20241022",
1708
+ name: "computer",
1709
+ inputSchema: z7.object({
1710
+ action: z7.enum([
1711
+ "key",
1712
+ "type",
1713
+ "mouse_move",
1714
+ "left_click",
1715
+ "left_click_drag",
1716
+ "right_click",
1717
+ "middle_click",
1718
+ "double_click",
1719
+ "screenshot",
1720
+ "cursor_position"
1721
+ ]),
1722
+ coordinate: z7.array(z7.number().int()).optional(),
1723
+ text: z7.string().optional()
1724
+ })
1306
1725
  });
1307
- function textEditorTool_20241022(options = {}) {
1308
- return {
1309
- type: "provider-defined",
1310
- id: "anthropic.text_editor_20241022",
1311
- args: {},
1312
- parameters: TextEditor20241022Parameters,
1313
- execute: options.execute,
1314
- experimental_toToolResultContent: options.experimental_toToolResultContent
1315
- };
1316
- }
1317
- var TextEditor20250124Parameters = z4.object({
1318
- command: z4.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1319
- path: z4.string(),
1320
- file_text: z4.string().optional(),
1321
- insert_line: z4.number().int().optional(),
1322
- new_str: z4.string().optional(),
1323
- old_str: z4.string().optional(),
1324
- view_range: z4.array(z4.number().int()).optional()
1726
+
1727
+ // src/tool/computer_20250124.ts
1728
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory4 } from "@ai-sdk/provider-utils";
1729
+ import { z as z8 } from "zod/v4";
1730
+ var computer_20250124 = createProviderDefinedToolFactory4({
1731
+ id: "anthropic.computer_20250124",
1732
+ name: "computer",
1733
+ inputSchema: z8.object({
1734
+ action: z8.enum([
1735
+ "key",
1736
+ "hold_key",
1737
+ "type",
1738
+ "cursor_position",
1739
+ "mouse_move",
1740
+ "left_mouse_down",
1741
+ "left_mouse_up",
1742
+ "left_click",
1743
+ "left_click_drag",
1744
+ "right_click",
1745
+ "middle_click",
1746
+ "double_click",
1747
+ "triple_click",
1748
+ "scroll",
1749
+ "wait",
1750
+ "screenshot"
1751
+ ]),
1752
+ coordinate: z8.tuple([z8.number().int(), z8.number().int()]).optional(),
1753
+ duration: z8.number().optional(),
1754
+ scroll_amount: z8.number().optional(),
1755
+ scroll_direction: z8.enum(["up", "down", "left", "right"]).optional(),
1756
+ start_coordinate: z8.tuple([z8.number().int(), z8.number().int()]).optional(),
1757
+ text: z8.string().optional()
1758
+ })
1325
1759
  });
1326
- function textEditorTool_20250124(options = {}) {
1327
- return {
1328
- type: "provider-defined",
1329
- id: "anthropic.text_editor_20250124",
1330
- args: {},
1331
- parameters: TextEditor20250124Parameters,
1332
- execute: options.execute,
1333
- experimental_toToolResultContent: options.experimental_toToolResultContent
1334
- };
1335
- }
1336
- var Computer20241022Parameters = z4.object({
1337
- action: z4.enum([
1338
- "key",
1339
- "type",
1340
- "mouse_move",
1341
- "left_click",
1342
- "left_click_drag",
1343
- "right_click",
1344
- "middle_click",
1345
- "double_click",
1346
- "screenshot",
1347
- "cursor_position"
1348
- ]),
1349
- coordinate: z4.array(z4.number().int()).optional(),
1350
- text: z4.string().optional()
1760
+
1761
+ // src/tool/text-editor_20241022.ts
1762
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory5 } from "@ai-sdk/provider-utils";
1763
+ import { z as z9 } from "zod/v4";
1764
+ var textEditor_20241022 = createProviderDefinedToolFactory5({
1765
+ id: "anthropic.text_editor_20241022",
1766
+ name: "str_replace_editor",
1767
+ inputSchema: z9.object({
1768
+ command: z9.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1769
+ path: z9.string(),
1770
+ file_text: z9.string().optional(),
1771
+ insert_line: z9.number().int().optional(),
1772
+ new_str: z9.string().optional(),
1773
+ old_str: z9.string().optional(),
1774
+ view_range: z9.array(z9.number().int()).optional()
1775
+ })
1351
1776
  });
1352
- function computerTool_20241022(options) {
1353
- return {
1354
- type: "provider-defined",
1355
- id: "anthropic.computer_20241022",
1356
- args: {
1357
- displayWidthPx: options.displayWidthPx,
1358
- displayHeightPx: options.displayHeightPx,
1359
- displayNumber: options.displayNumber
1360
- },
1361
- parameters: Computer20241022Parameters,
1362
- execute: options.execute,
1363
- experimental_toToolResultContent: options.experimental_toToolResultContent
1364
- };
1365
- }
1366
- var Computer20250124Parameters = z4.object({
1367
- action: z4.enum([
1368
- "key",
1369
- "hold_key",
1370
- "type",
1371
- "cursor_position",
1372
- "mouse_move",
1373
- "left_mouse_down",
1374
- "left_mouse_up",
1375
- "left_click",
1376
- "left_click_drag",
1377
- "right_click",
1378
- "middle_click",
1379
- "double_click",
1380
- "triple_click",
1381
- "scroll",
1382
- "wait",
1383
- "screenshot"
1384
- ]),
1385
- coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1386
- duration: z4.number().optional(),
1387
- scroll_amount: z4.number().optional(),
1388
- scroll_direction: z4.enum(["up", "down", "left", "right"]).optional(),
1389
- start_coordinate: z4.tuple([z4.number().int(), z4.number().int()]).optional(),
1390
- text: z4.string().optional()
1777
+
1778
+ // src/tool/text-editor_20250124.ts
1779
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory6 } from "@ai-sdk/provider-utils";
1780
+ import { z as z10 } from "zod/v4";
1781
+ var textEditor_20250124 = createProviderDefinedToolFactory6({
1782
+ id: "anthropic.text_editor_20250124",
1783
+ name: "str_replace_editor",
1784
+ inputSchema: z10.object({
1785
+ command: z10.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
1786
+ path: z10.string(),
1787
+ file_text: z10.string().optional(),
1788
+ insert_line: z10.number().int().optional(),
1789
+ new_str: z10.string().optional(),
1790
+ old_str: z10.string().optional(),
1791
+ view_range: z10.array(z10.number().int()).optional()
1792
+ })
1391
1793
  });
1392
- function computerTool_20250124(options) {
1393
- return {
1394
- type: "provider-defined",
1395
- id: "anthropic.computer_20250124",
1396
- args: {
1397
- displayWidthPx: options.displayWidthPx,
1398
- displayHeightPx: options.displayHeightPx,
1399
- displayNumber: options.displayNumber
1400
- },
1401
- parameters: Computer20250124Parameters,
1402
- execute: options.execute,
1403
- experimental_toToolResultContent: options.experimental_toToolResultContent
1404
- };
1405
- }
1794
+
1795
+ // src/tool/text-editor_20250429.ts
1796
+ import { createProviderDefinedToolFactory as createProviderDefinedToolFactory7 } from "@ai-sdk/provider-utils";
1797
+ import { z as z11 } from "zod/v4";
1798
+ var textEditor_20250429 = createProviderDefinedToolFactory7({
1799
+ id: "anthropic.text_editor_20250429",
1800
+ name: "str_replace_based_edit_tool",
1801
+ inputSchema: z11.object({
1802
+ command: z11.enum(["view", "create", "str_replace", "insert"]),
1803
+ path: z11.string(),
1804
+ file_text: z11.string().optional(),
1805
+ insert_line: z11.number().int().optional(),
1806
+ new_str: z11.string().optional(),
1807
+ old_str: z11.string().optional(),
1808
+ view_range: z11.array(z11.number().int()).optional()
1809
+ })
1810
+ });
1811
+
1812
+ // src/anthropic-tools.ts
1406
1813
  var anthropicTools = {
1407
- bash_20241022: bashTool_20241022,
1408
- bash_20250124: bashTool_20250124,
1409
- textEditor_20241022: textEditorTool_20241022,
1410
- textEditor_20250124: textEditorTool_20250124,
1411
- computer_20241022: computerTool_20241022,
1412
- computer_20250124: computerTool_20250124
1814
+ /**
1815
+ * Creates a tool for running a bash command. Must have name "bash".
1816
+ *
1817
+ * Image results are supported.
1818
+ *
1819
+ * @param execute - The function to execute the tool. Optional.
1820
+ */
1821
+ bash_20241022,
1822
+ /**
1823
+ * Creates a tool for running a bash command. Must have name "bash".
1824
+ *
1825
+ * Image results are supported.
1826
+ *
1827
+ * @param execute - The function to execute the tool. Optional.
1828
+ */
1829
+ bash_20250124,
1830
+ /**
1831
+ * Creates a tool for editing text. Must have name "str_replace_editor".
1832
+ */
1833
+ textEditor_20241022,
1834
+ /**
1835
+ * Creates a tool for editing text. Must have name "str_replace_editor".
1836
+ */
1837
+ textEditor_20250124,
1838
+ /**
1839
+ * Creates a tool for editing text. Must have name "str_replace_based_edit_tool".
1840
+ * Note: This version does not support the "undo_edit" command.
1841
+ */
1842
+ textEditor_20250429,
1843
+ /**
1844
+ * Creates a tool for executing actions on a computer. Must have name "computer".
1845
+ *
1846
+ * Image results are supported.
1847
+ *
1848
+ * @param displayWidthPx - The width of the display being controlled by the model in pixels.
1849
+ * @param displayHeightPx - The height of the display being controlled by the model in pixels.
1850
+ * @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
1851
+ */
1852
+ computer_20241022,
1853
+ /**
1854
+ * Creates a tool for executing actions on a computer. Must have name "computer".
1855
+ *
1856
+ * Image results are supported.
1857
+ *
1858
+ * @param displayWidthPx - The width of the display being controlled by the model in pixels.
1859
+ * @param displayHeightPx - The height of the display being controlled by the model in pixels.
1860
+ * @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
1861
+ * @param execute - The function to execute the tool. Optional.
1862
+ */
1863
+ computer_20250124,
1864
+ /**
1865
+ * Creates a web search tool that gives Claude direct access to real-time web content.
1866
+ * Must have name "web_search".
1867
+ *
1868
+ * @param maxUses - Maximum number of web searches Claude can perform during the conversation.
1869
+ * @param allowedDomains - Optional list of domains that Claude is allowed to search.
1870
+ * @param blockedDomains - Optional list of domains that Claude should avoid when searching.
1871
+ * @param userLocation - Optional user location information to provide geographically relevant search results.
1872
+ */
1873
+ webSearch_20250305
1413
1874
  };
1414
1875
  export {
1415
1876
  AnthropicMessagesLanguageModel,