@ai-sdk/anthropic 3.0.96 → 3.0.98

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.96",
3
+ "version": "3.0.98",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@ai-sdk/provider": "3.0.14",
40
- "@ai-sdk/provider-utils": "4.0.38"
40
+ "@ai-sdk/provider-utils": "4.0.40"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -66,6 +66,7 @@ export interface AnthropicCompactionContent {
66
66
  export interface AnthropicTextContent {
67
67
  type: 'text';
68
68
  text: string;
69
+ citations?: Citation[];
69
70
  cache_control: AnthropicCacheControl | undefined;
70
71
  }
71
72
 
@@ -873,7 +873,22 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
873
873
  switch (part.type) {
874
874
  case 'text': {
875
875
  if (!usesJsonResponseTool) {
876
- content.push({ type: 'text', text: part.text });
876
+ const webSearchCitations = part.citations?.filter(
877
+ citation => citation.type === 'web_search_result_location',
878
+ );
879
+
880
+ content.push({
881
+ type: 'text',
882
+ text: part.text,
883
+ ...(webSearchCitations != null &&
884
+ webSearchCitations.length > 0 && {
885
+ providerMetadata: {
886
+ anthropic: {
887
+ citations: webSearchCitations,
888
+ },
889
+ },
890
+ }),
891
+ });
877
892
 
878
893
  // Process citations if present
879
894
  if (part.citations) {
@@ -1456,7 +1471,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1456
1471
  toolId?: string;
1457
1472
  };
1458
1473
  }
1459
- | { type: 'text' | 'reasoning' }
1474
+ | { type: 'text'; citations: Citation[] }
1475
+ | { type: 'reasoning' }
1460
1476
  > = {};
1461
1477
  const mcpToolCalls: Record<string, LanguageModelV3ToolCall> = {};
1462
1478
  const serverToolCalls: Record<string, string> = {}; // tool_use_id -> provider tool name
@@ -1538,7 +1554,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1538
1554
  return;
1539
1555
  }
1540
1556
 
1541
- contentBlocks[value.index] = { type: 'text' };
1557
+ contentBlocks[value.index] = {
1558
+ type: 'text',
1559
+ citations: [],
1560
+ };
1542
1561
  controller.enqueue({
1543
1562
  type: 'text-start',
1544
1563
  id: String(value.index),
@@ -1570,7 +1589,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1570
1589
  }
1571
1590
 
1572
1591
  case 'compaction': {
1573
- contentBlocks[value.index] = { type: 'text' };
1592
+ contentBlocks[value.index] = {
1593
+ type: 'text',
1594
+ citations: [],
1595
+ };
1574
1596
  controller.enqueue({
1575
1597
  type: 'text-start',
1576
1598
  id: String(value.index),
@@ -1590,7 +1612,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1590
1612
  if (isJsonResponseTool) {
1591
1613
  isJsonResponseFromTool = true;
1592
1614
 
1593
- contentBlocks[value.index] = { type: 'text' };
1615
+ contentBlocks[value.index] = {
1616
+ type: 'text',
1617
+ citations: [],
1618
+ };
1594
1619
 
1595
1620
  controller.enqueue({
1596
1621
  type: 'text-start',
@@ -2049,6 +2074,13 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
2049
2074
  controller.enqueue({
2050
2075
  type: 'text-end',
2051
2076
  id: String(value.index),
2077
+ ...(contentBlock.citations.length > 0 && {
2078
+ providerMetadata: {
2079
+ anthropic: {
2080
+ citations: contentBlock.citations,
2081
+ },
2082
+ },
2083
+ }),
2052
2084
  });
2053
2085
  break;
2054
2086
  }
@@ -2239,6 +2271,15 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
2239
2271
 
2240
2272
  case 'citations_delta': {
2241
2273
  const citation = value.delta.citation;
2274
+ const contentBlock = contentBlocks[value.index];
2275
+
2276
+ if (
2277
+ contentBlock?.type === 'text' &&
2278
+ citation.type === 'web_search_result_location'
2279
+ ) {
2280
+ contentBlock.citations.push(citation);
2281
+ }
2282
+
2242
2283
  const source = createCitationSource(
2243
2284
  citation,
2244
2285
  citationDocuments,
@@ -23,6 +23,7 @@ import {
23
23
  type AnthropicToolResultContent,
24
24
  type AnthropicUserMessage,
25
25
  type AnthropicWebFetchToolResultContent,
26
+ type Citation,
26
27
  } from './anthropic-messages-api';
27
28
  import { anthropicFilePartProviderOptions } from './anthropic-messages-options';
28
29
  import { CacheControlValidator } from './get-cache-control';
@@ -507,7 +508,7 @@ export async function convertToAnthropicMessagesPrompt({
507
508
  case 'text': {
508
509
  // Check if this is a compaction block (via providerMetadata)
509
510
  const textMetadata = part.providerOptions?.anthropic as
510
- | { type?: string }
511
+ | { type?: string; citations?: Citation[] }
511
512
  | undefined;
512
513
 
513
514
  if (textMetadata?.type === 'compaction') {
@@ -526,7 +527,9 @@ export async function convertToAnthropicMessagesPrompt({
526
527
  isLastBlock && isLastMessage && isLastContentPart
527
528
  ? part.text.trim()
528
529
  : part.text,
529
-
530
+ ...(textMetadata?.citations != null && {
531
+ citations: textMetadata.citations,
532
+ }),
530
533
  cache_control: cacheControl,
531
534
  });
532
535
  }