@ai-sdk/anthropic 3.0.18 → 3.0.19

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.js +1 -1
  3. package/dist/index.mjs +1 -1
  4. package/package.json +3 -2
  5. package/src/__fixtures__/anthropic-code-execution-20250825.1.chunks.txt +248 -0
  6. package/src/__fixtures__/anthropic-code-execution-20250825.1.json +70 -0
  7. package/src/__fixtures__/anthropic-code-execution-20250825.2.chunks.txt +984 -0
  8. package/src/__fixtures__/anthropic-code-execution-20250825.2.json +111 -0
  9. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.chunks.txt +691 -0
  10. package/src/__fixtures__/anthropic-code-execution-20250825.pptx-skill.json +1801 -0
  11. package/src/__fixtures__/anthropic-json-other-tool.1.chunks.txt +13 -0
  12. package/src/__fixtures__/anthropic-json-other-tool.1.json +26 -0
  13. package/src/__fixtures__/anthropic-json-output-format.1.chunks.txt +120 -0
  14. package/src/__fixtures__/anthropic-json-output-format.1.json +25 -0
  15. package/src/__fixtures__/anthropic-json-tool.1.chunks.txt +9 -0
  16. package/src/__fixtures__/anthropic-json-tool.1.json +37 -0
  17. package/src/__fixtures__/anthropic-json-tool.2.chunks.txt +14 -0
  18. package/src/__fixtures__/anthropic-mcp.1.chunks.txt +17 -0
  19. package/src/__fixtures__/anthropic-mcp.1.json +39 -0
  20. package/src/__fixtures__/anthropic-memory-20250818.1.json +28 -0
  21. package/src/__fixtures__/anthropic-message-delta-input-tokens.chunks.txt +8 -0
  22. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.chunks.txt +278 -0
  23. package/src/__fixtures__/anthropic-programmatic-tool-calling.1.json +106 -0
  24. package/src/__fixtures__/anthropic-tool-no-args.chunks.txt +13 -0
  25. package/src/__fixtures__/anthropic-tool-no-args.json +31 -0
  26. package/src/__fixtures__/anthropic-tool-search-bm25.1.chunks.txt +47 -0
  27. package/src/__fixtures__/anthropic-tool-search-bm25.1.json +67 -0
  28. package/src/__fixtures__/anthropic-tool-search-regex.1.chunks.txt +51 -0
  29. package/src/__fixtures__/anthropic-tool-search-regex.1.json +65 -0
  30. package/src/__fixtures__/anthropic-web-fetch-tool.1.chunks.txt +64 -0
  31. package/src/__fixtures__/anthropic-web-fetch-tool.1.json +54 -0
  32. package/src/__fixtures__/anthropic-web-fetch-tool.2.json +56 -0
  33. package/src/__fixtures__/anthropic-web-fetch-tool.error.json +46 -0
  34. package/src/__fixtures__/anthropic-web-search-tool.1.chunks.txt +120 -0
  35. package/src/__fixtures__/anthropic-web-search-tool.1.json +181 -0
  36. package/src/__snapshots__/anthropic-messages-language-model.test.ts.snap +16719 -0
  37. package/src/anthropic-error.test.ts +42 -0
  38. package/src/anthropic-error.ts +26 -0
  39. package/src/anthropic-message-metadata.ts +105 -0
  40. package/src/anthropic-messages-api.ts +1188 -0
  41. package/src/anthropic-messages-language-model.test.ts +7170 -0
  42. package/src/anthropic-messages-language-model.ts +2067 -0
  43. package/src/anthropic-messages-options.ts +213 -0
  44. package/src/anthropic-prepare-tools.test.ts +1219 -0
  45. package/src/anthropic-prepare-tools.ts +341 -0
  46. package/src/anthropic-provider.test.ts +162 -0
  47. package/src/anthropic-provider.ts +152 -0
  48. package/src/anthropic-tools.ts +182 -0
  49. package/src/convert-anthropic-messages-usage.ts +32 -0
  50. package/src/convert-to-anthropic-messages-prompt.test.ts +2902 -0
  51. package/src/convert-to-anthropic-messages-prompt.ts +1050 -0
  52. package/src/forward-anthropic-container-id-from-last-step.ts +38 -0
  53. package/src/get-cache-control.ts +63 -0
  54. package/src/index.ts +10 -0
  55. package/src/internal/index.ts +4 -0
  56. package/src/map-anthropic-stop-reason.ts +28 -0
  57. package/src/tool/bash_20241022.ts +33 -0
  58. package/src/tool/bash_20250124.ts +33 -0
  59. package/src/tool/code-execution_20250522.ts +61 -0
  60. package/src/tool/code-execution_20250825.ts +281 -0
  61. package/src/tool/computer_20241022.ts +87 -0
  62. package/src/tool/computer_20250124.ts +130 -0
  63. package/src/tool/memory_20250818.ts +62 -0
  64. package/src/tool/text-editor_20241022.ts +63 -0
  65. package/src/tool/text-editor_20250124.ts +63 -0
  66. package/src/tool/text-editor_20250429.ts +64 -0
  67. package/src/tool/text-editor_20250728.ts +80 -0
  68. package/src/tool/tool-search-bm25_20251119.ts +98 -0
  69. package/src/tool/tool-search-regex_20251119.ts +110 -0
  70. package/src/tool/web-fetch-20250910.ts +145 -0
  71. package/src/tool/web-search_20250305.ts +136 -0
  72. package/src/version.ts +6 -0
@@ -0,0 +1,1188 @@
1
+ import { JSONSchema7 } from '@ai-sdk/provider';
2
+ import { InferSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
3
+ import { z } from 'zod/v4';
4
+
5
+ export type AnthropicMessagesPrompt = {
6
+ system: Array<AnthropicTextContent> | undefined;
7
+ messages: AnthropicMessage[];
8
+ };
9
+
10
+ export type AnthropicMessage = AnthropicUserMessage | AnthropicAssistantMessage;
11
+
12
+ export type AnthropicCacheControl = {
13
+ type: 'ephemeral';
14
+ ttl?: '5m' | '1h';
15
+ };
16
+
17
+ export interface AnthropicUserMessage {
18
+ role: 'user';
19
+ content: Array<
20
+ | AnthropicTextContent
21
+ | AnthropicImageContent
22
+ | AnthropicDocumentContent
23
+ | AnthropicToolResultContent
24
+ >;
25
+ }
26
+
27
+ export interface AnthropicAssistantMessage {
28
+ role: 'assistant';
29
+ content: Array<
30
+ | AnthropicTextContent
31
+ | AnthropicThinkingContent
32
+ | AnthropicRedactedThinkingContent
33
+ | AnthropicToolCallContent
34
+ | AnthropicServerToolUseContent
35
+ | AnthropicCodeExecutionToolResultContent
36
+ | AnthropicWebFetchToolResultContent
37
+ | AnthropicWebSearchToolResultContent
38
+ | AnthropicToolSearchToolResultContent
39
+ | AnthropicBashCodeExecutionToolResultContent
40
+ | AnthropicTextEditorCodeExecutionToolResultContent
41
+ | AnthropicMcpToolUseContent
42
+ | AnthropicMcpToolResultContent
43
+ >;
44
+ }
45
+
46
+ export interface AnthropicTextContent {
47
+ type: 'text';
48
+ text: string;
49
+ cache_control: AnthropicCacheControl | undefined;
50
+ }
51
+
52
+ export interface AnthropicThinkingContent {
53
+ type: 'thinking';
54
+ thinking: string;
55
+ signature: string;
56
+ // Note: thinking blocks cannot be directly cached with cache_control.
57
+ // They are cached implicitly when appearing in previous assistant turns.
58
+ cache_control?: never;
59
+ }
60
+
61
+ export interface AnthropicRedactedThinkingContent {
62
+ type: 'redacted_thinking';
63
+ data: string;
64
+ // Note: redacted thinking blocks cannot be directly cached with cache_control.
65
+ // They are cached implicitly when appearing in previous assistant turns.
66
+ cache_control?: never;
67
+ }
68
+
69
+ type AnthropicContentSource =
70
+ | {
71
+ type: 'base64';
72
+ media_type: string;
73
+ data: string;
74
+ }
75
+ | {
76
+ type: 'url';
77
+ url: string;
78
+ }
79
+ | {
80
+ type: 'text';
81
+ media_type: 'text/plain';
82
+ data: string;
83
+ };
84
+
85
+ export interface AnthropicImageContent {
86
+ type: 'image';
87
+ source: AnthropicContentSource;
88
+ cache_control: AnthropicCacheControl | undefined;
89
+ }
90
+
91
+ export interface AnthropicDocumentContent {
92
+ type: 'document';
93
+ source: AnthropicContentSource;
94
+ title?: string;
95
+ context?: string;
96
+ citations?: { enabled: boolean };
97
+ cache_control: AnthropicCacheControl | undefined;
98
+ }
99
+
100
+ /**
101
+ * The caller information for programmatic tool calling.
102
+ * Present when a tool is called from within code execution.
103
+ */
104
+ export type AnthropicToolCallCaller =
105
+ | {
106
+ type: 'code_execution_20250825';
107
+ tool_id: string;
108
+ }
109
+ | {
110
+ type: 'direct';
111
+ };
112
+
113
+ export interface AnthropicToolCallContent {
114
+ type: 'tool_use';
115
+ id: string;
116
+ name: string;
117
+ input: unknown;
118
+ /**
119
+ * Present when this tool call was triggered by a server-executed tool
120
+ * (e.g., code execution calling a user-defined tool programmatically).
121
+ */
122
+ caller?: AnthropicToolCallCaller;
123
+ cache_control: AnthropicCacheControl | undefined;
124
+ }
125
+
126
+ export interface AnthropicServerToolUseContent {
127
+ type: 'server_tool_use';
128
+ id: string;
129
+ name:
130
+ | 'web_fetch'
131
+ | 'web_search'
132
+ // code execution 20250522:
133
+ | 'code_execution'
134
+ // code execution 20250825:
135
+ | 'bash_code_execution'
136
+ | 'text_editor_code_execution'
137
+ // tool search:
138
+ | 'tool_search_tool_regex'
139
+ | 'tool_search_tool_bm25';
140
+ input: unknown;
141
+ cache_control: AnthropicCacheControl | undefined;
142
+ }
143
+
144
+ // Nested content types for tool results (without cache_control)
145
+ // Sub-content blocks cannot be cached directly according to Anthropic docs
146
+ type AnthropicNestedTextContent = Omit<
147
+ AnthropicTextContent,
148
+ 'cache_control'
149
+ > & {
150
+ cache_control?: never;
151
+ };
152
+
153
+ type AnthropicNestedImageContent = Omit<
154
+ AnthropicImageContent,
155
+ 'cache_control'
156
+ > & {
157
+ cache_control?: never;
158
+ };
159
+
160
+ type AnthropicNestedDocumentContent = Omit<
161
+ AnthropicDocumentContent,
162
+ 'cache_control'
163
+ > & {
164
+ cache_control?: never;
165
+ };
166
+
167
+ export interface AnthropicToolResultContent {
168
+ type: 'tool_result';
169
+ tool_use_id: string;
170
+ content:
171
+ | string
172
+ | Array<
173
+ | AnthropicNestedTextContent
174
+ | AnthropicNestedImageContent
175
+ | AnthropicNestedDocumentContent
176
+ >;
177
+ is_error: boolean | undefined;
178
+ cache_control: AnthropicCacheControl | undefined;
179
+ }
180
+
181
+ export interface AnthropicWebSearchToolResultContent {
182
+ type: 'web_search_tool_result';
183
+ tool_use_id: string;
184
+ content: Array<{
185
+ url: string;
186
+ title: string | null;
187
+ page_age: string | null;
188
+ encrypted_content: string;
189
+ type: string;
190
+ }>;
191
+ cache_control: AnthropicCacheControl | undefined;
192
+ }
193
+
194
+ export interface AnthropicToolSearchToolResultContent {
195
+ type: 'tool_search_tool_result';
196
+ tool_use_id: string;
197
+ content:
198
+ | {
199
+ type: 'tool_search_tool_search_result';
200
+ tool_references: Array<{
201
+ type: 'tool_reference';
202
+ tool_name: string;
203
+ }>;
204
+ }
205
+ | {
206
+ type: 'tool_search_tool_result_error';
207
+ error_code: string;
208
+ };
209
+ cache_control: AnthropicCacheControl | undefined;
210
+ }
211
+
212
+ // code execution results for code_execution_20250522 tool:
213
+ export interface AnthropicCodeExecutionToolResultContent {
214
+ type: 'code_execution_tool_result';
215
+ tool_use_id: string;
216
+ content:
217
+ | {
218
+ type: 'code_execution_result';
219
+ stdout: string;
220
+ stderr: string;
221
+ return_code: number;
222
+ content: Array<{ type: 'code_execution_output'; file_id: string }>;
223
+ }
224
+ | {
225
+ type: 'code_execution_tool_result_error';
226
+ error_code: string;
227
+ };
228
+ cache_control: AnthropicCacheControl | undefined;
229
+ }
230
+
231
+ // text editor code execution results for code_execution_20250825 tool:
232
+ export interface AnthropicTextEditorCodeExecutionToolResultContent {
233
+ type: 'text_editor_code_execution_tool_result';
234
+ tool_use_id: string;
235
+ content:
236
+ | {
237
+ type: 'text_editor_code_execution_tool_result_error';
238
+ error_code: string;
239
+ }
240
+ | {
241
+ type: 'text_editor_code_execution_create_result';
242
+ is_file_update: boolean;
243
+ }
244
+ | {
245
+ type: 'text_editor_code_execution_view_result';
246
+ content: string;
247
+ file_type: string;
248
+ num_lines: number | null;
249
+ start_line: number | null;
250
+ total_lines: number | null;
251
+ }
252
+ | {
253
+ type: 'text_editor_code_execution_str_replace_result';
254
+ lines: string[] | null;
255
+ new_lines: number | null;
256
+ new_start: number | null;
257
+ old_lines: number | null;
258
+ old_start: number | null;
259
+ };
260
+ cache_control: AnthropicCacheControl | undefined;
261
+ }
262
+
263
+ // bash code execution results for code_execution_20250825 tool:
264
+ export interface AnthropicBashCodeExecutionToolResultContent {
265
+ type: 'bash_code_execution_tool_result';
266
+ tool_use_id: string;
267
+ content:
268
+ | {
269
+ type: 'bash_code_execution_result';
270
+ stdout: string;
271
+ stderr: string;
272
+ return_code: number;
273
+ content: {
274
+ type: 'bash_code_execution_output';
275
+ file_id: string;
276
+ }[];
277
+ }
278
+ | {
279
+ type: 'bash_code_execution_tool_result_error';
280
+ error_code: string;
281
+ };
282
+ cache_control: AnthropicCacheControl | undefined;
283
+ }
284
+
285
+ export interface AnthropicWebFetchToolResultContent {
286
+ type: 'web_fetch_tool_result';
287
+ tool_use_id: string;
288
+ content:
289
+ | {
290
+ type: 'web_fetch_result';
291
+ url: string;
292
+ retrieved_at: string | null;
293
+ content: {
294
+ type: 'document';
295
+ title: string | null;
296
+ citations?: { enabled: boolean };
297
+ source:
298
+ | { type: 'base64'; media_type: 'application/pdf'; data: string }
299
+ | { type: 'text'; media_type: 'text/plain'; data: string };
300
+ };
301
+ }
302
+ | {
303
+ type: 'web_fetch_tool_result_error';
304
+ error_code: string;
305
+ };
306
+ cache_control: AnthropicCacheControl | undefined;
307
+ }
308
+ export interface AnthropicMcpToolUseContent {
309
+ type: 'mcp_tool_use';
310
+ id: string;
311
+ name: string;
312
+ server_name: string;
313
+ input: unknown;
314
+ cache_control: AnthropicCacheControl | undefined;
315
+ }
316
+
317
+ export interface AnthropicMcpToolResultContent {
318
+ type: 'mcp_tool_result';
319
+ tool_use_id: string;
320
+ is_error: boolean;
321
+ content: string | Array<{ type: 'text'; text: string }>;
322
+ cache_control: AnthropicCacheControl | undefined;
323
+ }
324
+
325
+ export type AnthropicTool =
326
+ | {
327
+ name: string;
328
+ description: string | undefined;
329
+ input_schema: JSONSchema7;
330
+ cache_control: AnthropicCacheControl | undefined;
331
+ strict?: boolean;
332
+ /**
333
+ * When true, this tool is deferred and will only be loaded when
334
+ * discovered via the tool search tool.
335
+ */
336
+ defer_loading?: boolean;
337
+ /**
338
+ * Programmatic tool calling: specifies which server-executed tools
339
+ * are allowed to call this tool. When set, only the specified callers
340
+ * can invoke this tool programmatically.
341
+ *
342
+ * @example ['code_execution_20250825']
343
+ */
344
+ allowed_callers?: Array<'code_execution_20250825'>;
345
+ }
346
+ | {
347
+ type: 'code_execution_20250522';
348
+ name: string;
349
+ cache_control: AnthropicCacheControl | undefined;
350
+ }
351
+ | {
352
+ type: 'code_execution_20250825';
353
+ name: string;
354
+ }
355
+ | {
356
+ name: string;
357
+ type: 'computer_20250124' | 'computer_20241022';
358
+ display_width_px: number;
359
+ display_height_px: number;
360
+ display_number: number;
361
+ cache_control: AnthropicCacheControl | undefined;
362
+ }
363
+ | {
364
+ name: string;
365
+ type:
366
+ | 'text_editor_20250124'
367
+ | 'text_editor_20241022'
368
+ | 'text_editor_20250429';
369
+ cache_control: AnthropicCacheControl | undefined;
370
+ }
371
+ | {
372
+ name: string;
373
+ type: 'text_editor_20250728';
374
+ max_characters?: number;
375
+ cache_control: AnthropicCacheControl | undefined;
376
+ }
377
+ | {
378
+ name: string;
379
+ type: 'bash_20250124' | 'bash_20241022';
380
+ cache_control: AnthropicCacheControl | undefined;
381
+ }
382
+ | {
383
+ name: string;
384
+ type: 'memory_20250818';
385
+ }
386
+ | {
387
+ type: 'web_fetch_20250910';
388
+ name: string;
389
+ max_uses?: number;
390
+ allowed_domains?: string[];
391
+ blocked_domains?: string[];
392
+ citations?: { enabled: boolean };
393
+ max_content_tokens?: number;
394
+ cache_control: AnthropicCacheControl | undefined;
395
+ }
396
+ | {
397
+ type: 'web_search_20250305';
398
+ name: string;
399
+ max_uses?: number;
400
+ allowed_domains?: string[];
401
+ blocked_domains?: string[];
402
+ user_location?: {
403
+ type: 'approximate';
404
+ city?: string;
405
+ region?: string;
406
+ country?: string;
407
+ timezone?: string;
408
+ };
409
+ cache_control: AnthropicCacheControl | undefined;
410
+ }
411
+ | {
412
+ type: 'tool_search_tool_regex_20251119';
413
+ name: string;
414
+ }
415
+ | {
416
+ type: 'tool_search_tool_bm25_20251119';
417
+ name: string;
418
+ };
419
+
420
+ export type AnthropicToolChoice =
421
+ | { type: 'auto' | 'any'; disable_parallel_tool_use?: boolean }
422
+ | { type: 'tool'; name: string; disable_parallel_tool_use?: boolean };
423
+
424
+ export type AnthropicContainer = {
425
+ id?: string | null;
426
+ skills?: Array<{
427
+ type: 'anthropic' | 'custom';
428
+ skill_id: string;
429
+ version?: string;
430
+ }> | null;
431
+ };
432
+
433
+ export type AnthropicInputTokensTrigger = {
434
+ type: 'input_tokens';
435
+ value: number;
436
+ };
437
+
438
+ export type AnthropicToolUsesTrigger = {
439
+ type: 'tool_uses';
440
+ value: number;
441
+ };
442
+
443
+ export type AnthropicContextManagementTrigger =
444
+ | AnthropicInputTokensTrigger
445
+ | AnthropicToolUsesTrigger;
446
+
447
+ export type AnthropicClearToolUsesEdit = {
448
+ type: 'clear_tool_uses_20250919';
449
+ trigger?: AnthropicContextManagementTrigger;
450
+ keep?: {
451
+ type: 'tool_uses';
452
+ value: number;
453
+ };
454
+ clear_at_least?: {
455
+ type: 'input_tokens';
456
+ value: number;
457
+ };
458
+ clear_tool_inputs?: boolean;
459
+ exclude_tools?: string[];
460
+ };
461
+
462
+ export type AnthropicClearThinkingBlockEdit = {
463
+ type: 'clear_thinking_20251015';
464
+ keep?: 'all' | { type: 'thinking_turns'; value: number };
465
+ };
466
+
467
+ export type AnthropicContextManagementEdit =
468
+ | AnthropicClearToolUsesEdit
469
+ | AnthropicClearThinkingBlockEdit;
470
+
471
+ export type AnthropicContextManagementConfig = {
472
+ edits: AnthropicContextManagementEdit[];
473
+ };
474
+
475
+ export type AnthropicResponseClearToolUsesEdit = {
476
+ type: 'clear_tool_uses_20250919';
477
+ cleared_tool_uses: number;
478
+ cleared_input_tokens: number;
479
+ };
480
+
481
+ export type AnthropicResponseClearThinkingBlockEdit = {
482
+ type: 'clear_thinking_20251015';
483
+ cleared_thinking_turns: number;
484
+ cleared_input_tokens: number;
485
+ };
486
+
487
+ export type AnthropicResponseContextManagementEdit =
488
+ | AnthropicResponseClearToolUsesEdit
489
+ | AnthropicResponseClearThinkingBlockEdit;
490
+
491
+ export type AnthropicResponseContextManagement = {
492
+ applied_edits: AnthropicResponseContextManagementEdit[];
493
+ };
494
+
495
+ // limited version of the schema, focussed on what is needed for the implementation
496
+ // this approach limits breakages when the API changes and increases efficiency
497
+ export const anthropicMessagesResponseSchema = lazySchema(() =>
498
+ zodSchema(
499
+ z.object({
500
+ type: z.literal('message'),
501
+ id: z.string().nullish(),
502
+ model: z.string().nullish(),
503
+ content: z.array(
504
+ z.discriminatedUnion('type', [
505
+ z.object({
506
+ type: z.literal('text'),
507
+ text: z.string(),
508
+ citations: z
509
+ .array(
510
+ z.discriminatedUnion('type', [
511
+ z.object({
512
+ type: z.literal('web_search_result_location'),
513
+ cited_text: z.string(),
514
+ url: z.string(),
515
+ title: z.string(),
516
+ encrypted_index: z.string(),
517
+ }),
518
+ z.object({
519
+ type: z.literal('page_location'),
520
+ cited_text: z.string(),
521
+ document_index: z.number(),
522
+ document_title: z.string().nullable(),
523
+ start_page_number: z.number(),
524
+ end_page_number: z.number(),
525
+ }),
526
+ z.object({
527
+ type: z.literal('char_location'),
528
+ cited_text: z.string(),
529
+ document_index: z.number(),
530
+ document_title: z.string().nullable(),
531
+ start_char_index: z.number(),
532
+ end_char_index: z.number(),
533
+ }),
534
+ ]),
535
+ )
536
+ .optional(),
537
+ }),
538
+ z.object({
539
+ type: z.literal('thinking'),
540
+ thinking: z.string(),
541
+ signature: z.string(),
542
+ }),
543
+ z.object({
544
+ type: z.literal('redacted_thinking'),
545
+ data: z.string(),
546
+ }),
547
+ z.object({
548
+ type: z.literal('tool_use'),
549
+ id: z.string(),
550
+ name: z.string(),
551
+ input: z.unknown(),
552
+ // Programmatic tool calling: caller info when triggered from code execution
553
+ caller: z
554
+ .union([
555
+ z.object({
556
+ type: z.literal('code_execution_20250825'),
557
+ tool_id: z.string(),
558
+ }),
559
+ z.object({
560
+ type: z.literal('direct'),
561
+ }),
562
+ ])
563
+ .optional(),
564
+ }),
565
+ z.object({
566
+ type: z.literal('server_tool_use'),
567
+ id: z.string(),
568
+ name: z.string(),
569
+ input: z.record(z.string(), z.unknown()).nullish(),
570
+ }),
571
+ z.object({
572
+ type: z.literal('mcp_tool_use'),
573
+ id: z.string(),
574
+ name: z.string(),
575
+ input: z.unknown(),
576
+ server_name: z.string(),
577
+ }),
578
+ z.object({
579
+ type: z.literal('mcp_tool_result'),
580
+ tool_use_id: z.string(),
581
+ is_error: z.boolean(),
582
+ content: z.array(
583
+ z.union([
584
+ z.string(),
585
+ z.object({ type: z.literal('text'), text: z.string() }),
586
+ ]),
587
+ ),
588
+ }),
589
+ z.object({
590
+ type: z.literal('web_fetch_tool_result'),
591
+ tool_use_id: z.string(),
592
+ content: z.union([
593
+ z.object({
594
+ type: z.literal('web_fetch_result'),
595
+ url: z.string(),
596
+ retrieved_at: z.string(),
597
+ content: z.object({
598
+ type: z.literal('document'),
599
+ title: z.string().nullable(),
600
+ citations: z.object({ enabled: z.boolean() }).optional(),
601
+ source: z.union([
602
+ z.object({
603
+ type: z.literal('base64'),
604
+ media_type: z.literal('application/pdf'),
605
+ data: z.string(),
606
+ }),
607
+ z.object({
608
+ type: z.literal('text'),
609
+ media_type: z.literal('text/plain'),
610
+ data: z.string(),
611
+ }),
612
+ ]),
613
+ }),
614
+ }),
615
+ z.object({
616
+ type: z.literal('web_fetch_tool_result_error'),
617
+ error_code: z.string(),
618
+ }),
619
+ ]),
620
+ }),
621
+ z.object({
622
+ type: z.literal('web_search_tool_result'),
623
+ tool_use_id: z.string(),
624
+ content: z.union([
625
+ z.array(
626
+ z.object({
627
+ type: z.literal('web_search_result'),
628
+ url: z.string(),
629
+ title: z.string(),
630
+ encrypted_content: z.string(),
631
+ page_age: z.string().nullish(),
632
+ }),
633
+ ),
634
+ z.object({
635
+ type: z.literal('web_search_tool_result_error'),
636
+ error_code: z.string(),
637
+ }),
638
+ ]),
639
+ }),
640
+ // code execution results for code_execution_20250522 tool:
641
+ z.object({
642
+ type: z.literal('code_execution_tool_result'),
643
+ tool_use_id: z.string(),
644
+ content: z.union([
645
+ z.object({
646
+ type: z.literal('code_execution_result'),
647
+ stdout: z.string(),
648
+ stderr: z.string(),
649
+ return_code: z.number(),
650
+ content: z
651
+ .array(
652
+ z.object({
653
+ type: z.literal('code_execution_output'),
654
+ file_id: z.string(),
655
+ }),
656
+ )
657
+ .optional()
658
+ .default([]),
659
+ }),
660
+ z.object({
661
+ type: z.literal('code_execution_tool_result_error'),
662
+ error_code: z.string(),
663
+ }),
664
+ ]),
665
+ }),
666
+ // bash code execution results for code_execution_20250825 tool:
667
+ z.object({
668
+ type: z.literal('bash_code_execution_tool_result'),
669
+ tool_use_id: z.string(),
670
+ content: z.discriminatedUnion('type', [
671
+ z.object({
672
+ type: z.literal('bash_code_execution_result'),
673
+ content: z.array(
674
+ z.object({
675
+ type: z.literal('bash_code_execution_output'),
676
+ file_id: z.string(),
677
+ }),
678
+ ),
679
+ stdout: z.string(),
680
+ stderr: z.string(),
681
+ return_code: z.number(),
682
+ }),
683
+ z.object({
684
+ type: z.literal('bash_code_execution_tool_result_error'),
685
+ error_code: z.string(),
686
+ }),
687
+ ]),
688
+ }),
689
+ // text editor code execution results for code_execution_20250825 tool:
690
+ z.object({
691
+ type: z.literal('text_editor_code_execution_tool_result'),
692
+ tool_use_id: z.string(),
693
+ content: z.discriminatedUnion('type', [
694
+ z.object({
695
+ type: z.literal('text_editor_code_execution_tool_result_error'),
696
+ error_code: z.string(),
697
+ }),
698
+ z.object({
699
+ type: z.literal('text_editor_code_execution_view_result'),
700
+ content: z.string(),
701
+ file_type: z.string(),
702
+ num_lines: z.number().nullable(),
703
+ start_line: z.number().nullable(),
704
+ total_lines: z.number().nullable(),
705
+ }),
706
+ z.object({
707
+ type: z.literal('text_editor_code_execution_create_result'),
708
+ is_file_update: z.boolean(),
709
+ }),
710
+ z.object({
711
+ type: z.literal(
712
+ 'text_editor_code_execution_str_replace_result',
713
+ ),
714
+ lines: z.array(z.string()).nullable(),
715
+ new_lines: z.number().nullable(),
716
+ new_start: z.number().nullable(),
717
+ old_lines: z.number().nullable(),
718
+ old_start: z.number().nullable(),
719
+ }),
720
+ ]),
721
+ }),
722
+ // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
723
+ z.object({
724
+ type: z.literal('tool_search_tool_result'),
725
+ tool_use_id: z.string(),
726
+ content: z.union([
727
+ z.object({
728
+ type: z.literal('tool_search_tool_search_result'),
729
+ tool_references: z.array(
730
+ z.object({
731
+ type: z.literal('tool_reference'),
732
+ tool_name: z.string(),
733
+ }),
734
+ ),
735
+ }),
736
+ z.object({
737
+ type: z.literal('tool_search_tool_result_error'),
738
+ error_code: z.string(),
739
+ }),
740
+ ]),
741
+ }),
742
+ ]),
743
+ ),
744
+ stop_reason: z.string().nullish(),
745
+ stop_sequence: z.string().nullish(),
746
+ usage: z.looseObject({
747
+ input_tokens: z.number(),
748
+ output_tokens: z.number(),
749
+ cache_creation_input_tokens: z.number().nullish(),
750
+ cache_read_input_tokens: z.number().nullish(),
751
+ }),
752
+ container: z
753
+ .object({
754
+ expires_at: z.string(),
755
+ id: z.string(),
756
+ skills: z
757
+ .array(
758
+ z.object({
759
+ type: z.union([z.literal('anthropic'), z.literal('custom')]),
760
+ skill_id: z.string(),
761
+ version: z.string(),
762
+ }),
763
+ )
764
+ .nullish(),
765
+ })
766
+ .nullish(),
767
+ context_management: z
768
+ .object({
769
+ applied_edits: z.array(
770
+ z.union([
771
+ z.object({
772
+ type: z.literal('clear_tool_uses_20250919'),
773
+ cleared_tool_uses: z.number(),
774
+ cleared_input_tokens: z.number(),
775
+ }),
776
+ z.object({
777
+ type: z.literal('clear_thinking_20251015'),
778
+ cleared_thinking_turns: z.number(),
779
+ cleared_input_tokens: z.number(),
780
+ }),
781
+ ]),
782
+ ),
783
+ })
784
+ .nullish(),
785
+ }),
786
+ ),
787
+ );
788
+
789
+ // limited version of the schema, focused on what is needed for the implementation
790
+ // this approach limits breakages when the API changes and increases efficiency
791
+ export const anthropicMessagesChunkSchema = lazySchema(() =>
792
+ zodSchema(
793
+ z.discriminatedUnion('type', [
794
+ z.object({
795
+ type: z.literal('message_start'),
796
+ message: z.object({
797
+ id: z.string().nullish(),
798
+ model: z.string().nullish(),
799
+ role: z.string().nullish(),
800
+ usage: z.looseObject({
801
+ input_tokens: z.number(),
802
+ cache_creation_input_tokens: z.number().nullish(),
803
+ cache_read_input_tokens: z.number().nullish(),
804
+ }),
805
+ // Programmatic tool calling: content may be pre-populated for deferred tool calls
806
+ content: z
807
+ .array(
808
+ z.discriminatedUnion('type', [
809
+ z.object({
810
+ type: z.literal('tool_use'),
811
+ id: z.string(),
812
+ name: z.string(),
813
+ input: z.unknown(),
814
+ caller: z
815
+ .union([
816
+ z.object({
817
+ type: z.literal('code_execution_20250825'),
818
+ tool_id: z.string(),
819
+ }),
820
+ z.object({
821
+ type: z.literal('direct'),
822
+ }),
823
+ ])
824
+ .optional(),
825
+ }),
826
+ ]),
827
+ )
828
+ .nullish(),
829
+ stop_reason: z.string().nullish(),
830
+ container: z
831
+ .object({
832
+ expires_at: z.string(),
833
+ id: z.string(),
834
+ })
835
+ .nullish(),
836
+ }),
837
+ }),
838
+ z.object({
839
+ type: z.literal('content_block_start'),
840
+ index: z.number(),
841
+ content_block: z.discriminatedUnion('type', [
842
+ z.object({
843
+ type: z.literal('text'),
844
+ text: z.string(),
845
+ }),
846
+ z.object({
847
+ type: z.literal('thinking'),
848
+ thinking: z.string(),
849
+ }),
850
+ z.object({
851
+ type: z.literal('tool_use'),
852
+ id: z.string(),
853
+ name: z.string(),
854
+ // Programmatic tool calling: input may be present directly for deferred tool calls
855
+ input: z.record(z.string(), z.unknown()).optional(),
856
+ // Programmatic tool calling: caller info when triggered from code execution
857
+ caller: z
858
+ .union([
859
+ z.object({
860
+ type: z.literal('code_execution_20250825'),
861
+ tool_id: z.string(),
862
+ }),
863
+ z.object({
864
+ type: z.literal('direct'),
865
+ }),
866
+ ])
867
+ .optional(),
868
+ }),
869
+ z.object({
870
+ type: z.literal('redacted_thinking'),
871
+ data: z.string(),
872
+ }),
873
+ z.object({
874
+ type: z.literal('server_tool_use'),
875
+ id: z.string(),
876
+ name: z.string(),
877
+ input: z.record(z.string(), z.unknown()).nullish(),
878
+ }),
879
+ z.object({
880
+ type: z.literal('mcp_tool_use'),
881
+ id: z.string(),
882
+ name: z.string(),
883
+ input: z.unknown(),
884
+ server_name: z.string(),
885
+ }),
886
+ z.object({
887
+ type: z.literal('mcp_tool_result'),
888
+ tool_use_id: z.string(),
889
+ is_error: z.boolean(),
890
+ content: z.array(
891
+ z.union([
892
+ z.string(),
893
+ z.object({ type: z.literal('text'), text: z.string() }),
894
+ ]),
895
+ ),
896
+ }),
897
+ z.object({
898
+ type: z.literal('web_fetch_tool_result'),
899
+ tool_use_id: z.string(),
900
+ content: z.union([
901
+ z.object({
902
+ type: z.literal('web_fetch_result'),
903
+ url: z.string(),
904
+ retrieved_at: z.string(),
905
+ content: z.object({
906
+ type: z.literal('document'),
907
+ title: z.string().nullable(),
908
+ citations: z.object({ enabled: z.boolean() }).optional(),
909
+ source: z.union([
910
+ z.object({
911
+ type: z.literal('base64'),
912
+ media_type: z.literal('application/pdf'),
913
+ data: z.string(),
914
+ }),
915
+ z.object({
916
+ type: z.literal('text'),
917
+ media_type: z.literal('text/plain'),
918
+ data: z.string(),
919
+ }),
920
+ ]),
921
+ }),
922
+ }),
923
+ z.object({
924
+ type: z.literal('web_fetch_tool_result_error'),
925
+ error_code: z.string(),
926
+ }),
927
+ ]),
928
+ }),
929
+ z.object({
930
+ type: z.literal('web_search_tool_result'),
931
+ tool_use_id: z.string(),
932
+ content: z.union([
933
+ z.array(
934
+ z.object({
935
+ type: z.literal('web_search_result'),
936
+ url: z.string(),
937
+ title: z.string(),
938
+ encrypted_content: z.string(),
939
+ page_age: z.string().nullish(),
940
+ }),
941
+ ),
942
+ z.object({
943
+ type: z.literal('web_search_tool_result_error'),
944
+ error_code: z.string(),
945
+ }),
946
+ ]),
947
+ }),
948
+ // code execution results for code_execution_20250522 tool:
949
+ z.object({
950
+ type: z.literal('code_execution_tool_result'),
951
+ tool_use_id: z.string(),
952
+ content: z.union([
953
+ z.object({
954
+ type: z.literal('code_execution_result'),
955
+ stdout: z.string(),
956
+ stderr: z.string(),
957
+ return_code: z.number(),
958
+ content: z
959
+ .array(
960
+ z.object({
961
+ type: z.literal('code_execution_output'),
962
+ file_id: z.string(),
963
+ }),
964
+ )
965
+ .optional()
966
+ .default([]),
967
+ }),
968
+ z.object({
969
+ type: z.literal('code_execution_tool_result_error'),
970
+ error_code: z.string(),
971
+ }),
972
+ ]),
973
+ }),
974
+ // bash code execution results for code_execution_20250825 tool:
975
+ z.object({
976
+ type: z.literal('bash_code_execution_tool_result'),
977
+ tool_use_id: z.string(),
978
+ content: z.discriminatedUnion('type', [
979
+ z.object({
980
+ type: z.literal('bash_code_execution_result'),
981
+ content: z.array(
982
+ z.object({
983
+ type: z.literal('bash_code_execution_output'),
984
+ file_id: z.string(),
985
+ }),
986
+ ),
987
+ stdout: z.string(),
988
+ stderr: z.string(),
989
+ return_code: z.number(),
990
+ }),
991
+ z.object({
992
+ type: z.literal('bash_code_execution_tool_result_error'),
993
+ error_code: z.string(),
994
+ }),
995
+ ]),
996
+ }),
997
+ // text editor code execution results for code_execution_20250825 tool:
998
+ z.object({
999
+ type: z.literal('text_editor_code_execution_tool_result'),
1000
+ tool_use_id: z.string(),
1001
+ content: z.discriminatedUnion('type', [
1002
+ z.object({
1003
+ type: z.literal('text_editor_code_execution_tool_result_error'),
1004
+ error_code: z.string(),
1005
+ }),
1006
+ z.object({
1007
+ type: z.literal('text_editor_code_execution_view_result'),
1008
+ content: z.string(),
1009
+ file_type: z.string(),
1010
+ num_lines: z.number().nullable(),
1011
+ start_line: z.number().nullable(),
1012
+ total_lines: z.number().nullable(),
1013
+ }),
1014
+ z.object({
1015
+ type: z.literal('text_editor_code_execution_create_result'),
1016
+ is_file_update: z.boolean(),
1017
+ }),
1018
+ z.object({
1019
+ type: z.literal(
1020
+ 'text_editor_code_execution_str_replace_result',
1021
+ ),
1022
+ lines: z.array(z.string()).nullable(),
1023
+ new_lines: z.number().nullable(),
1024
+ new_start: z.number().nullable(),
1025
+ old_lines: z.number().nullable(),
1026
+ old_start: z.number().nullable(),
1027
+ }),
1028
+ ]),
1029
+ }),
1030
+ // tool search tool results for tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119:
1031
+ z.object({
1032
+ type: z.literal('tool_search_tool_result'),
1033
+ tool_use_id: z.string(),
1034
+ content: z.union([
1035
+ z.object({
1036
+ type: z.literal('tool_search_tool_search_result'),
1037
+ tool_references: z.array(
1038
+ z.object({
1039
+ type: z.literal('tool_reference'),
1040
+ tool_name: z.string(),
1041
+ }),
1042
+ ),
1043
+ }),
1044
+ z.object({
1045
+ type: z.literal('tool_search_tool_result_error'),
1046
+ error_code: z.string(),
1047
+ }),
1048
+ ]),
1049
+ }),
1050
+ ]),
1051
+ }),
1052
+ z.object({
1053
+ type: z.literal('content_block_delta'),
1054
+ index: z.number(),
1055
+ delta: z.discriminatedUnion('type', [
1056
+ z.object({
1057
+ type: z.literal('input_json_delta'),
1058
+ partial_json: z.string(),
1059
+ }),
1060
+ z.object({
1061
+ type: z.literal('text_delta'),
1062
+ text: z.string(),
1063
+ }),
1064
+ z.object({
1065
+ type: z.literal('thinking_delta'),
1066
+ thinking: z.string(),
1067
+ }),
1068
+ z.object({
1069
+ type: z.literal('signature_delta'),
1070
+ signature: z.string(),
1071
+ }),
1072
+ z.object({
1073
+ type: z.literal('citations_delta'),
1074
+ citation: z.discriminatedUnion('type', [
1075
+ z.object({
1076
+ type: z.literal('web_search_result_location'),
1077
+ cited_text: z.string(),
1078
+ url: z.string(),
1079
+ title: z.string(),
1080
+ encrypted_index: z.string(),
1081
+ }),
1082
+ z.object({
1083
+ type: z.literal('page_location'),
1084
+ cited_text: z.string(),
1085
+ document_index: z.number(),
1086
+ document_title: z.string().nullable(),
1087
+ start_page_number: z.number(),
1088
+ end_page_number: z.number(),
1089
+ }),
1090
+ z.object({
1091
+ type: z.literal('char_location'),
1092
+ cited_text: z.string(),
1093
+ document_index: z.number(),
1094
+ document_title: z.string().nullable(),
1095
+ start_char_index: z.number(),
1096
+ end_char_index: z.number(),
1097
+ }),
1098
+ ]),
1099
+ }),
1100
+ ]),
1101
+ }),
1102
+ z.object({
1103
+ type: z.literal('content_block_stop'),
1104
+ index: z.number(),
1105
+ }),
1106
+ z.object({
1107
+ type: z.literal('error'),
1108
+ error: z.object({
1109
+ type: z.string(),
1110
+ message: z.string(),
1111
+ }),
1112
+ }),
1113
+ z.object({
1114
+ type: z.literal('message_delta'),
1115
+ delta: z.object({
1116
+ stop_reason: z.string().nullish(),
1117
+ stop_sequence: z.string().nullish(),
1118
+ container: z
1119
+ .object({
1120
+ expires_at: z.string(),
1121
+ id: z.string(),
1122
+ skills: z
1123
+ .array(
1124
+ z.object({
1125
+ type: z.union([
1126
+ z.literal('anthropic'),
1127
+ z.literal('custom'),
1128
+ ]),
1129
+ skill_id: z.string(),
1130
+ version: z.string(),
1131
+ }),
1132
+ )
1133
+ .nullish(),
1134
+ })
1135
+ .nullish(),
1136
+ context_management: z
1137
+ .object({
1138
+ applied_edits: z.array(
1139
+ z.union([
1140
+ z.object({
1141
+ type: z.literal('clear_tool_uses_20250919'),
1142
+ cleared_tool_uses: z.number(),
1143
+ cleared_input_tokens: z.number(),
1144
+ }),
1145
+ z.object({
1146
+ type: z.literal('clear_thinking_20251015'),
1147
+ cleared_thinking_turns: z.number(),
1148
+ cleared_input_tokens: z.number(),
1149
+ }),
1150
+ ]),
1151
+ ),
1152
+ })
1153
+ .nullish(),
1154
+ }),
1155
+ usage: z.looseObject({
1156
+ input_tokens: z.number().nullish(),
1157
+ output_tokens: z.number(),
1158
+ cache_creation_input_tokens: z.number().nullish(),
1159
+ }),
1160
+ }),
1161
+ z.object({
1162
+ type: z.literal('message_stop'),
1163
+ }),
1164
+ z.object({
1165
+ type: z.literal('ping'),
1166
+ }),
1167
+ ]),
1168
+ ),
1169
+ );
1170
+
1171
+ export const anthropicReasoningMetadataSchema = lazySchema(() =>
1172
+ zodSchema(
1173
+ z.object({
1174
+ signature: z.string().optional(),
1175
+ redactedData: z.string().optional(),
1176
+ }),
1177
+ ),
1178
+ );
1179
+
1180
+ export type AnthropicReasoningMetadata = InferSchema<
1181
+ typeof anthropicReasoningMetadataSchema
1182
+ >;
1183
+
1184
+ export type Citation = NonNullable<
1185
+ (InferSchema<typeof anthropicMessagesResponseSchema>['content'][number] & {
1186
+ type: 'text';
1187
+ })['citations']
1188
+ >[number];