@fairyhunter13/ai-anthropic 3.0.58-fork.1

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