@ai-sdk/anthropic 2.0.0-alpha.10 → 2.0.0-alpha.12
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 +18 -0
- package/dist/index.js +169 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +169 -11
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +1 -0
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +169 -11
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +169 -11
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/internal/index.mjs
CHANGED
|
@@ -38,6 +38,29 @@ var webSearchLocationSchema = z2.object({
|
|
|
38
38
|
country: z2.string(),
|
|
39
39
|
timezone: z2.string().optional()
|
|
40
40
|
});
|
|
41
|
+
var anthropicFilePartProviderOptions = z2.object({
|
|
42
|
+
/**
|
|
43
|
+
* Citation configuration for this document.
|
|
44
|
+
* When enabled, this document will generate citations in the response.
|
|
45
|
+
*/
|
|
46
|
+
citations: z2.object({
|
|
47
|
+
/**
|
|
48
|
+
* Enable citations for this document
|
|
49
|
+
*/
|
|
50
|
+
enabled: z2.boolean()
|
|
51
|
+
}).optional(),
|
|
52
|
+
/**
|
|
53
|
+
* Custom title for the document.
|
|
54
|
+
* If not provided, the filename will be used.
|
|
55
|
+
*/
|
|
56
|
+
title: z2.string().optional(),
|
|
57
|
+
/**
|
|
58
|
+
* Context about the document that will be passed to the model
|
|
59
|
+
* but not used towards cited content.
|
|
60
|
+
* Useful for storing document metadata as text or stringified JSON.
|
|
61
|
+
*/
|
|
62
|
+
context: z2.string().optional()
|
|
63
|
+
});
|
|
41
64
|
var anthropicProviderOptions = z2.object({
|
|
42
65
|
/**
|
|
43
66
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -219,7 +242,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
219
242
|
sendReasoning,
|
|
220
243
|
warnings
|
|
221
244
|
}) {
|
|
222
|
-
var _a, _b, _c;
|
|
245
|
+
var _a, _b, _c, _d;
|
|
223
246
|
const betas = /* @__PURE__ */ new Set();
|
|
224
247
|
const blocks = groupIntoBlocks(prompt);
|
|
225
248
|
let system = void 0;
|
|
@@ -230,6 +253,26 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
230
253
|
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
231
254
|
return cacheControlValue;
|
|
232
255
|
}
|
|
256
|
+
async function shouldEnableCitations(providerMetadata) {
|
|
257
|
+
var _a2, _b2;
|
|
258
|
+
const anthropicOptions = await parseProviderOptions({
|
|
259
|
+
provider: "anthropic",
|
|
260
|
+
providerOptions: providerMetadata,
|
|
261
|
+
schema: anthropicFilePartProviderOptions
|
|
262
|
+
});
|
|
263
|
+
return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
|
|
264
|
+
}
|
|
265
|
+
async function getDocumentMetadata(providerMetadata) {
|
|
266
|
+
const anthropicOptions = await parseProviderOptions({
|
|
267
|
+
provider: "anthropic",
|
|
268
|
+
providerOptions: providerMetadata,
|
|
269
|
+
schema: anthropicFilePartProviderOptions
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
title: anthropicOptions == null ? void 0 : anthropicOptions.title,
|
|
273
|
+
context: anthropicOptions == null ? void 0 : anthropicOptions.context
|
|
274
|
+
};
|
|
275
|
+
}
|
|
233
276
|
for (let i = 0; i < blocks.length; i++) {
|
|
234
277
|
const block = blocks[i];
|
|
235
278
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -283,6 +326,12 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
283
326
|
});
|
|
284
327
|
} else if (part.mediaType === "application/pdf") {
|
|
285
328
|
betas.add("pdfs-2024-09-25");
|
|
329
|
+
const enableCitations = await shouldEnableCitations(
|
|
330
|
+
part.providerOptions
|
|
331
|
+
);
|
|
332
|
+
const metadata = await getDocumentMetadata(
|
|
333
|
+
part.providerOptions
|
|
334
|
+
);
|
|
286
335
|
anthropicContent.push({
|
|
287
336
|
type: "document",
|
|
288
337
|
source: part.data instanceof URL ? {
|
|
@@ -293,6 +342,11 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
293
342
|
media_type: "application/pdf",
|
|
294
343
|
data: convertToBase64(part.data)
|
|
295
344
|
},
|
|
345
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
346
|
+
...metadata.context && { context: metadata.context },
|
|
347
|
+
...enableCitations && {
|
|
348
|
+
citations: { enabled: true }
|
|
349
|
+
},
|
|
296
350
|
cache_control: cacheControl
|
|
297
351
|
});
|
|
298
352
|
} else {
|
|
@@ -310,7 +364,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
310
364
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
311
365
|
const part = content[i2];
|
|
312
366
|
const isLastPart = i2 === content.length - 1;
|
|
313
|
-
const cacheControl = (
|
|
367
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
314
368
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
315
369
|
var _a2;
|
|
316
370
|
switch (part2.type) {
|
|
@@ -360,7 +414,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
360
414
|
for (let k = 0; k < content.length; k++) {
|
|
361
415
|
const part = content[k];
|
|
362
416
|
const isLastContentPart = k === content.length - 1;
|
|
363
|
-
const cacheControl = (
|
|
417
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
364
418
|
switch (part.type) {
|
|
365
419
|
case "text": {
|
|
366
420
|
anthropicContent.push({
|
|
@@ -509,6 +563,40 @@ function mapAnthropicStopReason({
|
|
|
509
563
|
}
|
|
510
564
|
|
|
511
565
|
// src/anthropic-messages-language-model.ts
|
|
566
|
+
function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
|
|
567
|
+
if (citation.type === "page_location") {
|
|
568
|
+
const source = createCitationSource(
|
|
569
|
+
citation,
|
|
570
|
+
citationDocuments,
|
|
571
|
+
generateId2
|
|
572
|
+
);
|
|
573
|
+
if (source) {
|
|
574
|
+
onSource(source);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function createCitationSource(citation, citationDocuments, generateId2) {
|
|
579
|
+
var _a;
|
|
580
|
+
const documentInfo = citationDocuments[citation.document_index];
|
|
581
|
+
if (!documentInfo) {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
type: "source",
|
|
586
|
+
sourceType: "document",
|
|
587
|
+
id: generateId2(),
|
|
588
|
+
mediaType: documentInfo.mediaType,
|
|
589
|
+
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
590
|
+
filename: documentInfo.filename,
|
|
591
|
+
providerMetadata: {
|
|
592
|
+
anthropic: {
|
|
593
|
+
citedText: citation.cited_text,
|
|
594
|
+
startPageNumber: citation.start_page_number,
|
|
595
|
+
endPageNumber: citation.end_page_number
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
512
600
|
var AnthropicMessagesLanguageModel = class {
|
|
513
601
|
constructor(modelId, config) {
|
|
514
602
|
this.specificationVersion = "v2";
|
|
@@ -706,9 +794,29 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
706
794
|
var _a, _b, _c;
|
|
707
795
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
708
796
|
}
|
|
797
|
+
extractCitationDocuments(prompt) {
|
|
798
|
+
const isCitationEnabled = (part) => {
|
|
799
|
+
var _a, _b;
|
|
800
|
+
const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
801
|
+
const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
|
|
802
|
+
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
803
|
+
};
|
|
804
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
|
|
805
|
+
(part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
|
|
806
|
+
).map((part) => {
|
|
807
|
+
var _a;
|
|
808
|
+
const filePart = part;
|
|
809
|
+
return {
|
|
810
|
+
title: (_a = filePart.filename) != null ? _a : "Untitled Document",
|
|
811
|
+
filename: filePart.filename,
|
|
812
|
+
mediaType: filePart.mediaType
|
|
813
|
+
};
|
|
814
|
+
});
|
|
815
|
+
}
|
|
709
816
|
async doGenerate(options) {
|
|
710
817
|
var _a, _b, _c, _d, _e;
|
|
711
818
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
819
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
712
820
|
const {
|
|
713
821
|
responseHeaders,
|
|
714
822
|
value: response,
|
|
@@ -730,6 +838,16 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
730
838
|
case "text": {
|
|
731
839
|
if (jsonResponseTool == null) {
|
|
732
840
|
content.push({ type: "text", text: part.text });
|
|
841
|
+
if (part.citations) {
|
|
842
|
+
for (const citation of part.citations) {
|
|
843
|
+
processPageLocationCitation(
|
|
844
|
+
citation,
|
|
845
|
+
citationDocuments,
|
|
846
|
+
this.generateId,
|
|
847
|
+
(source) => content.push(source)
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
733
851
|
}
|
|
734
852
|
break;
|
|
735
853
|
}
|
|
@@ -836,6 +954,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
836
954
|
}
|
|
837
955
|
async doStream(options) {
|
|
838
956
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
957
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
839
958
|
const body = { ...args, stream: true };
|
|
840
959
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
841
960
|
url: this.buildRequestUrl(true),
|
|
@@ -867,6 +986,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
867
986
|
},
|
|
868
987
|
transform(chunk, controller) {
|
|
869
988
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
989
|
+
if (options.includeRawChunks) {
|
|
990
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
991
|
+
}
|
|
870
992
|
if (!chunk.success) {
|
|
871
993
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
872
994
|
return;
|
|
@@ -1020,6 +1142,13 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1020
1142
|
return;
|
|
1021
1143
|
}
|
|
1022
1144
|
case "citations_delta": {
|
|
1145
|
+
const citation = value.delta.citation;
|
|
1146
|
+
processPageLocationCitation(
|
|
1147
|
+
citation,
|
|
1148
|
+
citationDocuments,
|
|
1149
|
+
generateId2,
|
|
1150
|
+
(source) => controller.enqueue(source)
|
|
1151
|
+
);
|
|
1023
1152
|
return;
|
|
1024
1153
|
}
|
|
1025
1154
|
default: {
|
|
@@ -1088,7 +1217,26 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
1088
1217
|
z3.discriminatedUnion("type", [
|
|
1089
1218
|
z3.object({
|
|
1090
1219
|
type: z3.literal("text"),
|
|
1091
|
-
text: z3.string()
|
|
1220
|
+
text: z3.string(),
|
|
1221
|
+
citations: z3.array(
|
|
1222
|
+
z3.discriminatedUnion("type", [
|
|
1223
|
+
z3.object({
|
|
1224
|
+
type: z3.literal("web_search_result_location"),
|
|
1225
|
+
cited_text: z3.string(),
|
|
1226
|
+
url: z3.string(),
|
|
1227
|
+
title: z3.string(),
|
|
1228
|
+
encrypted_index: z3.string()
|
|
1229
|
+
}),
|
|
1230
|
+
z3.object({
|
|
1231
|
+
type: z3.literal("page_location"),
|
|
1232
|
+
cited_text: z3.string(),
|
|
1233
|
+
document_index: z3.number(),
|
|
1234
|
+
document_title: z3.string().nullable(),
|
|
1235
|
+
start_page_number: z3.number(),
|
|
1236
|
+
end_page_number: z3.number()
|
|
1237
|
+
})
|
|
1238
|
+
])
|
|
1239
|
+
).optional()
|
|
1092
1240
|
}),
|
|
1093
1241
|
z3.object({
|
|
1094
1242
|
type: z3.literal("thinking"),
|
|
@@ -1227,13 +1375,23 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1227
1375
|
}),
|
|
1228
1376
|
z3.object({
|
|
1229
1377
|
type: z3.literal("citations_delta"),
|
|
1230
|
-
citation: z3.
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1378
|
+
citation: z3.discriminatedUnion("type", [
|
|
1379
|
+
z3.object({
|
|
1380
|
+
type: z3.literal("web_search_result_location"),
|
|
1381
|
+
cited_text: z3.string(),
|
|
1382
|
+
url: z3.string(),
|
|
1383
|
+
title: z3.string(),
|
|
1384
|
+
encrypted_index: z3.string()
|
|
1385
|
+
}),
|
|
1386
|
+
z3.object({
|
|
1387
|
+
type: z3.literal("page_location"),
|
|
1388
|
+
cited_text: z3.string(),
|
|
1389
|
+
document_index: z3.number(),
|
|
1390
|
+
document_title: z3.string().nullable(),
|
|
1391
|
+
start_page_number: z3.number(),
|
|
1392
|
+
end_page_number: z3.number()
|
|
1393
|
+
})
|
|
1394
|
+
])
|
|
1237
1395
|
})
|
|
1238
1396
|
])
|
|
1239
1397
|
}),
|