@ai-sdk/anthropic 2.0.0-alpha.12 → 2.0.0-alpha.13
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/CHANGELOG.md +10 -0
- package/dist/index.js +101 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +101 -52
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +101 -52
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +101 -52
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @ai-sdk/anthropic
|
|
2
2
|
|
|
3
|
+
## 2.0.0-alpha.13
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8dfcb11: feat(anthropic/citation): text support for citations
|
|
8
|
+
- ee5a9c0: feat: streamText onChunk raw chunk support
|
|
9
|
+
- Updated dependencies [68ecf2f]
|
|
10
|
+
- @ai-sdk/provider@2.0.0-alpha.13
|
|
11
|
+
- @ai-sdk/provider-utils@3.0.0-alpha.13
|
|
12
|
+
|
|
3
13
|
## 2.0.0-alpha.12
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -253,12 +253,28 @@ function prepareTools({
|
|
|
253
253
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
254
254
|
var import_provider2 = require("@ai-sdk/provider");
|
|
255
255
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
256
|
+
function convertToString(data) {
|
|
257
|
+
if (typeof data === "string") {
|
|
258
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
259
|
+
}
|
|
260
|
+
if (data instanceof Uint8Array) {
|
|
261
|
+
return new TextDecoder().decode(data);
|
|
262
|
+
}
|
|
263
|
+
if (data instanceof URL) {
|
|
264
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
265
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
269
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
270
|
+
});
|
|
271
|
+
}
|
|
256
272
|
async function convertToAnthropicMessagesPrompt({
|
|
257
273
|
prompt,
|
|
258
274
|
sendReasoning,
|
|
259
275
|
warnings
|
|
260
276
|
}) {
|
|
261
|
-
var _a, _b, _c, _d;
|
|
277
|
+
var _a, _b, _c, _d, _e;
|
|
262
278
|
const betas = /* @__PURE__ */ new Set();
|
|
263
279
|
const blocks = groupIntoBlocks(prompt);
|
|
264
280
|
let system = void 0;
|
|
@@ -365,6 +381,30 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
365
381
|
},
|
|
366
382
|
cache_control: cacheControl
|
|
367
383
|
});
|
|
384
|
+
} else if (part.mediaType === "text/plain") {
|
|
385
|
+
const enableCitations = await shouldEnableCitations(
|
|
386
|
+
part.providerOptions
|
|
387
|
+
);
|
|
388
|
+
const metadata = await getDocumentMetadata(
|
|
389
|
+
part.providerOptions
|
|
390
|
+
);
|
|
391
|
+
anthropicContent.push({
|
|
392
|
+
type: "document",
|
|
393
|
+
source: part.data instanceof URL ? {
|
|
394
|
+
type: "url",
|
|
395
|
+
url: part.data.toString()
|
|
396
|
+
} : {
|
|
397
|
+
type: "text",
|
|
398
|
+
media_type: "text/plain",
|
|
399
|
+
data: convertToString(part.data)
|
|
400
|
+
},
|
|
401
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
402
|
+
...metadata.context && { context: metadata.context },
|
|
403
|
+
...enableCitations && {
|
|
404
|
+
citations: { enabled: true }
|
|
405
|
+
},
|
|
406
|
+
cache_control: cacheControl
|
|
407
|
+
});
|
|
368
408
|
} else {
|
|
369
409
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
370
410
|
functionality: `media type: ${part.mediaType}`
|
|
@@ -380,7 +420,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
380
420
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
381
421
|
const part = content[i2];
|
|
382
422
|
const isLastPart = i2 === content.length - 1;
|
|
383
|
-
const cacheControl = (
|
|
423
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
384
424
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
385
425
|
var _a2;
|
|
386
426
|
switch (part2.type) {
|
|
@@ -430,7 +470,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
430
470
|
for (let k = 0; k < content.length; k++) {
|
|
431
471
|
const part = content[k];
|
|
432
472
|
const isLastContentPart = k === content.length - 1;
|
|
433
|
-
const cacheControl = (
|
|
473
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
434
474
|
switch (part.type) {
|
|
435
475
|
case "text": {
|
|
436
476
|
anthropicContent.push({
|
|
@@ -579,8 +619,42 @@ function mapAnthropicStopReason({
|
|
|
579
619
|
}
|
|
580
620
|
|
|
581
621
|
// src/anthropic-messages-language-model.ts
|
|
582
|
-
|
|
583
|
-
|
|
622
|
+
var citationSchemas = {
|
|
623
|
+
webSearchResult: import_zod3.z.object({
|
|
624
|
+
type: import_zod3.z.literal("web_search_result_location"),
|
|
625
|
+
cited_text: import_zod3.z.string(),
|
|
626
|
+
url: import_zod3.z.string(),
|
|
627
|
+
title: import_zod3.z.string(),
|
|
628
|
+
encrypted_index: import_zod3.z.string()
|
|
629
|
+
}),
|
|
630
|
+
pageLocation: import_zod3.z.object({
|
|
631
|
+
type: import_zod3.z.literal("page_location"),
|
|
632
|
+
cited_text: import_zod3.z.string(),
|
|
633
|
+
document_index: import_zod3.z.number(),
|
|
634
|
+
document_title: import_zod3.z.string().nullable(),
|
|
635
|
+
start_page_number: import_zod3.z.number(),
|
|
636
|
+
end_page_number: import_zod3.z.number()
|
|
637
|
+
}),
|
|
638
|
+
charLocation: import_zod3.z.object({
|
|
639
|
+
type: import_zod3.z.literal("char_location"),
|
|
640
|
+
cited_text: import_zod3.z.string(),
|
|
641
|
+
document_index: import_zod3.z.number(),
|
|
642
|
+
document_title: import_zod3.z.string().nullable(),
|
|
643
|
+
start_char_index: import_zod3.z.number(),
|
|
644
|
+
end_char_index: import_zod3.z.number()
|
|
645
|
+
})
|
|
646
|
+
};
|
|
647
|
+
var citationSchema = import_zod3.z.discriminatedUnion("type", [
|
|
648
|
+
citationSchemas.webSearchResult,
|
|
649
|
+
citationSchemas.pageLocation,
|
|
650
|
+
citationSchemas.charLocation
|
|
651
|
+
]);
|
|
652
|
+
var documentCitationSchema = import_zod3.z.discriminatedUnion("type", [
|
|
653
|
+
citationSchemas.pageLocation,
|
|
654
|
+
citationSchemas.charLocation
|
|
655
|
+
]);
|
|
656
|
+
function processCitation(citation, citationDocuments, generateId3, onSource) {
|
|
657
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
584
658
|
const source = createCitationSource(
|
|
585
659
|
citation,
|
|
586
660
|
citationDocuments,
|
|
@@ -597,6 +671,15 @@ function createCitationSource(citation, citationDocuments, generateId3) {
|
|
|
597
671
|
if (!documentInfo) {
|
|
598
672
|
return null;
|
|
599
673
|
}
|
|
674
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
675
|
+
citedText: citation.cited_text,
|
|
676
|
+
startPageNumber: citation.start_page_number,
|
|
677
|
+
endPageNumber: citation.end_page_number
|
|
678
|
+
} : {
|
|
679
|
+
citedText: citation.cited_text,
|
|
680
|
+
startCharIndex: citation.start_char_index,
|
|
681
|
+
endCharIndex: citation.end_char_index
|
|
682
|
+
};
|
|
600
683
|
return {
|
|
601
684
|
type: "source",
|
|
602
685
|
sourceType: "document",
|
|
@@ -605,11 +688,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
|
|
|
605
688
|
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
606
689
|
filename: documentInfo.filename,
|
|
607
690
|
providerMetadata: {
|
|
608
|
-
anthropic:
|
|
609
|
-
citedText: citation.cited_text,
|
|
610
|
-
startPageNumber: citation.start_page_number,
|
|
611
|
-
endPageNumber: citation.end_page_number
|
|
612
|
-
}
|
|
691
|
+
anthropic: providerMetadata
|
|
613
692
|
}
|
|
614
693
|
};
|
|
615
694
|
}
|
|
@@ -811,15 +890,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
811
890
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
812
891
|
}
|
|
813
892
|
extractCitationDocuments(prompt) {
|
|
814
|
-
const
|
|
893
|
+
const isCitationPart = (part) => {
|
|
815
894
|
var _a, _b;
|
|
895
|
+
if (part.type !== "file") {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
899
|
+
return false;
|
|
900
|
+
}
|
|
816
901
|
const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
817
902
|
const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
|
|
818
903
|
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
819
904
|
};
|
|
820
|
-
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
|
|
821
|
-
(part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
|
|
822
|
-
).map((part) => {
|
|
905
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
823
906
|
var _a;
|
|
824
907
|
const filePart = part;
|
|
825
908
|
return {
|
|
@@ -856,7 +939,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
856
939
|
content.push({ type: "text", text: part.text });
|
|
857
940
|
if (part.citations) {
|
|
858
941
|
for (const citation of part.citations) {
|
|
859
|
-
|
|
942
|
+
processCitation(
|
|
860
943
|
citation,
|
|
861
944
|
citationDocuments,
|
|
862
945
|
this.generateId,
|
|
@@ -1159,7 +1242,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1159
1242
|
}
|
|
1160
1243
|
case "citations_delta": {
|
|
1161
1244
|
const citation = value.delta.citation;
|
|
1162
|
-
|
|
1245
|
+
processCitation(
|
|
1163
1246
|
citation,
|
|
1164
1247
|
citationDocuments,
|
|
1165
1248
|
generateId3,
|
|
@@ -1234,25 +1317,7 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
1234
1317
|
import_zod3.z.object({
|
|
1235
1318
|
type: import_zod3.z.literal("text"),
|
|
1236
1319
|
text: import_zod3.z.string(),
|
|
1237
|
-
citations: import_zod3.z.array(
|
|
1238
|
-
import_zod3.z.discriminatedUnion("type", [
|
|
1239
|
-
import_zod3.z.object({
|
|
1240
|
-
type: import_zod3.z.literal("web_search_result_location"),
|
|
1241
|
-
cited_text: import_zod3.z.string(),
|
|
1242
|
-
url: import_zod3.z.string(),
|
|
1243
|
-
title: import_zod3.z.string(),
|
|
1244
|
-
encrypted_index: import_zod3.z.string()
|
|
1245
|
-
}),
|
|
1246
|
-
import_zod3.z.object({
|
|
1247
|
-
type: import_zod3.z.literal("page_location"),
|
|
1248
|
-
cited_text: import_zod3.z.string(),
|
|
1249
|
-
document_index: import_zod3.z.number(),
|
|
1250
|
-
document_title: import_zod3.z.string().nullable(),
|
|
1251
|
-
start_page_number: import_zod3.z.number(),
|
|
1252
|
-
end_page_number: import_zod3.z.number()
|
|
1253
|
-
})
|
|
1254
|
-
])
|
|
1255
|
-
).optional()
|
|
1320
|
+
citations: import_zod3.z.array(citationSchema).optional()
|
|
1256
1321
|
}),
|
|
1257
1322
|
import_zod3.z.object({
|
|
1258
1323
|
type: import_zod3.z.literal("thinking"),
|
|
@@ -1391,23 +1456,7 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1391
1456
|
}),
|
|
1392
1457
|
import_zod3.z.object({
|
|
1393
1458
|
type: import_zod3.z.literal("citations_delta"),
|
|
1394
|
-
citation:
|
|
1395
|
-
import_zod3.z.object({
|
|
1396
|
-
type: import_zod3.z.literal("web_search_result_location"),
|
|
1397
|
-
cited_text: import_zod3.z.string(),
|
|
1398
|
-
url: import_zod3.z.string(),
|
|
1399
|
-
title: import_zod3.z.string(),
|
|
1400
|
-
encrypted_index: import_zod3.z.string()
|
|
1401
|
-
}),
|
|
1402
|
-
import_zod3.z.object({
|
|
1403
|
-
type: import_zod3.z.literal("page_location"),
|
|
1404
|
-
cited_text: import_zod3.z.string(),
|
|
1405
|
-
document_index: import_zod3.z.number(),
|
|
1406
|
-
document_title: import_zod3.z.string().nullable(),
|
|
1407
|
-
start_page_number: import_zod3.z.number(),
|
|
1408
|
-
end_page_number: import_zod3.z.number()
|
|
1409
|
-
})
|
|
1410
|
-
])
|
|
1459
|
+
citation: citationSchema
|
|
1411
1460
|
})
|
|
1412
1461
|
])
|
|
1413
1462
|
}),
|