@ai-sdk/anthropic 2.0.0-alpha.10 → 2.0.0-alpha.11

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/dist/index.mjs CHANGED
@@ -48,6 +48,29 @@ var webSearchLocationSchema = z2.object({
48
48
  country: z2.string(),
49
49
  timezone: z2.string().optional()
50
50
  });
51
+ var anthropicFilePartProviderOptions = z2.object({
52
+ /**
53
+ * Citation configuration for this document.
54
+ * When enabled, this document will generate citations in the response.
55
+ */
56
+ citations: z2.object({
57
+ /**
58
+ * Enable citations for this document
59
+ */
60
+ enabled: z2.boolean()
61
+ }).optional(),
62
+ /**
63
+ * Custom title for the document.
64
+ * If not provided, the filename will be used.
65
+ */
66
+ title: z2.string().optional(),
67
+ /**
68
+ * Context about the document that will be passed to the model
69
+ * but not used towards cited content.
70
+ * Useful for storing document metadata as text or stringified JSON.
71
+ */
72
+ context: z2.string().optional()
73
+ });
51
74
  var anthropicProviderOptions = z2.object({
52
75
  /**
53
76
  Include reasoning content in requests sent to the model. Defaults to `true`.
@@ -229,7 +252,7 @@ async function convertToAnthropicMessagesPrompt({
229
252
  sendReasoning,
230
253
  warnings
231
254
  }) {
232
- var _a, _b, _c;
255
+ var _a, _b, _c, _d;
233
256
  const betas = /* @__PURE__ */ new Set();
234
257
  const blocks = groupIntoBlocks(prompt);
235
258
  let system = void 0;
@@ -240,6 +263,26 @@ async function convertToAnthropicMessagesPrompt({
240
263
  const cacheControlValue = (_a2 = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a2 : anthropic2 == null ? void 0 : anthropic2.cache_control;
241
264
  return cacheControlValue;
242
265
  }
266
+ async function shouldEnableCitations(providerMetadata) {
267
+ var _a2, _b2;
268
+ const anthropicOptions = await parseProviderOptions({
269
+ provider: "anthropic",
270
+ providerOptions: providerMetadata,
271
+ schema: anthropicFilePartProviderOptions
272
+ });
273
+ return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
274
+ }
275
+ async function getDocumentMetadata(providerMetadata) {
276
+ const anthropicOptions = await parseProviderOptions({
277
+ provider: "anthropic",
278
+ providerOptions: providerMetadata,
279
+ schema: anthropicFilePartProviderOptions
280
+ });
281
+ return {
282
+ title: anthropicOptions == null ? void 0 : anthropicOptions.title,
283
+ context: anthropicOptions == null ? void 0 : anthropicOptions.context
284
+ };
285
+ }
243
286
  for (let i = 0; i < blocks.length; i++) {
244
287
  const block = blocks[i];
245
288
  const isLastBlock = i === blocks.length - 1;
@@ -293,6 +336,12 @@ async function convertToAnthropicMessagesPrompt({
293
336
  });
294
337
  } else if (part.mediaType === "application/pdf") {
295
338
  betas.add("pdfs-2024-09-25");
339
+ const enableCitations = await shouldEnableCitations(
340
+ part.providerOptions
341
+ );
342
+ const metadata = await getDocumentMetadata(
343
+ part.providerOptions
344
+ );
296
345
  anthropicContent.push({
297
346
  type: "document",
298
347
  source: part.data instanceof URL ? {
@@ -303,6 +352,11 @@ async function convertToAnthropicMessagesPrompt({
303
352
  media_type: "application/pdf",
304
353
  data: convertToBase64(part.data)
305
354
  },
355
+ title: (_b = metadata.title) != null ? _b : part.filename,
356
+ ...metadata.context && { context: metadata.context },
357
+ ...enableCitations && {
358
+ citations: { enabled: true }
359
+ },
306
360
  cache_control: cacheControl
307
361
  });
308
362
  } else {
@@ -320,7 +374,7 @@ async function convertToAnthropicMessagesPrompt({
320
374
  for (let i2 = 0; i2 < content.length; i2++) {
321
375
  const part = content[i2];
322
376
  const isLastPart = i2 === content.length - 1;
323
- const cacheControl = (_b = getCacheControl(part.providerOptions)) != null ? _b : isLastPart ? getCacheControl(message.providerOptions) : void 0;
377
+ const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
324
378
  const toolResultContent = part.content != null ? part.content.map((part2) => {
325
379
  var _a2;
326
380
  switch (part2.type) {
@@ -370,7 +424,7 @@ async function convertToAnthropicMessagesPrompt({
370
424
  for (let k = 0; k < content.length; k++) {
371
425
  const part = content[k];
372
426
  const isLastContentPart = k === content.length - 1;
373
- const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
427
+ const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
374
428
  switch (part.type) {
375
429
  case "text": {
376
430
  anthropicContent.push({
@@ -519,6 +573,40 @@ function mapAnthropicStopReason({
519
573
  }
520
574
 
521
575
  // src/anthropic-messages-language-model.ts
576
+ function processPageLocationCitation(citation, citationDocuments, generateId3, onSource) {
577
+ if (citation.type === "page_location") {
578
+ const source = createCitationSource(
579
+ citation,
580
+ citationDocuments,
581
+ generateId3
582
+ );
583
+ if (source) {
584
+ onSource(source);
585
+ }
586
+ }
587
+ }
588
+ function createCitationSource(citation, citationDocuments, generateId3) {
589
+ var _a;
590
+ const documentInfo = citationDocuments[citation.document_index];
591
+ if (!documentInfo) {
592
+ return null;
593
+ }
594
+ return {
595
+ type: "source",
596
+ sourceType: "document",
597
+ id: generateId3(),
598
+ mediaType: documentInfo.mediaType,
599
+ title: (_a = citation.document_title) != null ? _a : documentInfo.title,
600
+ filename: documentInfo.filename,
601
+ providerMetadata: {
602
+ anthropic: {
603
+ citedText: citation.cited_text,
604
+ startPageNumber: citation.start_page_number,
605
+ endPageNumber: citation.end_page_number
606
+ }
607
+ }
608
+ };
609
+ }
522
610
  var AnthropicMessagesLanguageModel = class {
523
611
  constructor(modelId, config) {
524
612
  this.specificationVersion = "v2";
@@ -716,9 +804,29 @@ var AnthropicMessagesLanguageModel = class {
716
804
  var _a, _b, _c;
717
805
  return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
718
806
  }
807
+ extractCitationDocuments(prompt) {
808
+ const isCitationEnabled = (part) => {
809
+ var _a, _b;
810
+ const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
811
+ const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
812
+ return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
813
+ };
814
+ return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
815
+ (part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
816
+ ).map((part) => {
817
+ var _a;
818
+ const filePart = part;
819
+ return {
820
+ title: (_a = filePart.filename) != null ? _a : "Untitled Document",
821
+ filename: filePart.filename,
822
+ mediaType: filePart.mediaType
823
+ };
824
+ });
825
+ }
719
826
  async doGenerate(options) {
720
827
  var _a, _b, _c, _d, _e;
721
828
  const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
829
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
722
830
  const {
723
831
  responseHeaders,
724
832
  value: response,
@@ -740,6 +848,16 @@ var AnthropicMessagesLanguageModel = class {
740
848
  case "text": {
741
849
  if (jsonResponseTool == null) {
742
850
  content.push({ type: "text", text: part.text });
851
+ if (part.citations) {
852
+ for (const citation of part.citations) {
853
+ processPageLocationCitation(
854
+ citation,
855
+ citationDocuments,
856
+ this.generateId,
857
+ (source) => content.push(source)
858
+ );
859
+ }
860
+ }
743
861
  }
744
862
  break;
745
863
  }
@@ -846,6 +964,7 @@ var AnthropicMessagesLanguageModel = class {
846
964
  }
847
965
  async doStream(options) {
848
966
  const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
967
+ const citationDocuments = this.extractCitationDocuments(options.prompt);
849
968
  const body = { ...args, stream: true };
850
969
  const { responseHeaders, value: response } = await postJsonToApi({
851
970
  url: this.buildRequestUrl(true),
@@ -1030,6 +1149,13 @@ var AnthropicMessagesLanguageModel = class {
1030
1149
  return;
1031
1150
  }
1032
1151
  case "citations_delta": {
1152
+ const citation = value.delta.citation;
1153
+ processPageLocationCitation(
1154
+ citation,
1155
+ citationDocuments,
1156
+ generateId3,
1157
+ (source) => controller.enqueue(source)
1158
+ );
1033
1159
  return;
1034
1160
  }
1035
1161
  default: {
@@ -1098,7 +1224,26 @@ var anthropicMessagesResponseSchema = z3.object({
1098
1224
  z3.discriminatedUnion("type", [
1099
1225
  z3.object({
1100
1226
  type: z3.literal("text"),
1101
- text: z3.string()
1227
+ text: z3.string(),
1228
+ citations: z3.array(
1229
+ z3.discriminatedUnion("type", [
1230
+ z3.object({
1231
+ type: z3.literal("web_search_result_location"),
1232
+ cited_text: z3.string(),
1233
+ url: z3.string(),
1234
+ title: z3.string(),
1235
+ encrypted_index: z3.string()
1236
+ }),
1237
+ z3.object({
1238
+ type: z3.literal("page_location"),
1239
+ cited_text: z3.string(),
1240
+ document_index: z3.number(),
1241
+ document_title: z3.string().nullable(),
1242
+ start_page_number: z3.number(),
1243
+ end_page_number: z3.number()
1244
+ })
1245
+ ])
1246
+ ).optional()
1102
1247
  }),
1103
1248
  z3.object({
1104
1249
  type: z3.literal("thinking"),
@@ -1237,13 +1382,23 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1237
1382
  }),
1238
1383
  z3.object({
1239
1384
  type: z3.literal("citations_delta"),
1240
- citation: z3.object({
1241
- type: z3.literal("web_search_result_location"),
1242
- cited_text: z3.string(),
1243
- url: z3.string(),
1244
- title: z3.string(),
1245
- encrypted_index: z3.string()
1246
- })
1385
+ citation: z3.discriminatedUnion("type", [
1386
+ z3.object({
1387
+ type: z3.literal("web_search_result_location"),
1388
+ cited_text: z3.string(),
1389
+ url: z3.string(),
1390
+ title: z3.string(),
1391
+ encrypted_index: z3.string()
1392
+ }),
1393
+ z3.object({
1394
+ type: z3.literal("page_location"),
1395
+ cited_text: z3.string(),
1396
+ document_index: z3.number(),
1397
+ document_title: z3.string().nullable(),
1398
+ start_page_number: z3.number(),
1399
+ end_page_number: z3.number()
1400
+ })
1401
+ ])
1247
1402
  })
1248
1403
  ])
1249
1404
  }),