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