@ai-sdk/langchain 2.0.214 → 2.0.216
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 +14 -0
- package/dist/index.js +122 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +15 -0
- package/src/types.ts +14 -0
- package/src/utils.ts +191 -7
package/dist/index.mjs
CHANGED
|
@@ -209,6 +209,9 @@ function processModelChunk(chunk, state, controller) {
|
|
|
209
209
|
if (!state.emittedImages) {
|
|
210
210
|
state.emittedImages = /* @__PURE__ */ new Set();
|
|
211
211
|
}
|
|
212
|
+
if (!state.emittedSourceIds) {
|
|
213
|
+
state.emittedSourceIds = /* @__PURE__ */ new Set();
|
|
214
|
+
}
|
|
212
215
|
if (chunk.id) {
|
|
213
216
|
state.messageId = chunk.id;
|
|
214
217
|
}
|
|
@@ -264,6 +267,15 @@ function processModelChunk(chunk, state, controller) {
|
|
|
264
267
|
id: (_c = state.textMessageId) != null ? _c : state.messageId
|
|
265
268
|
});
|
|
266
269
|
}
|
|
270
|
+
const citations = extractCitationsFromContentBlocks(chunk);
|
|
271
|
+
if (citations.length > 0) {
|
|
272
|
+
emitSourceChunks(
|
|
273
|
+
citations,
|
|
274
|
+
state.messageId,
|
|
275
|
+
state.emittedSourceIds,
|
|
276
|
+
controller
|
|
277
|
+
);
|
|
278
|
+
}
|
|
267
279
|
}
|
|
268
280
|
function isPlainMessageObject(msg) {
|
|
269
281
|
if (msg == null || typeof msg !== "object") return false;
|
|
@@ -426,6 +438,85 @@ function extractReasoningFromValuesMessage(msg) {
|
|
|
426
438
|
}
|
|
427
439
|
return void 0;
|
|
428
440
|
}
|
|
441
|
+
function isCitationContentBlock(obj) {
|
|
442
|
+
return obj != null && typeof obj === "object" && "type" in obj && obj.type === "citation";
|
|
443
|
+
}
|
|
444
|
+
function extractCitationsFromContentBlocks(msg) {
|
|
445
|
+
if (msg == null || typeof msg !== "object") return [];
|
|
446
|
+
const msgObj = msg;
|
|
447
|
+
const kwargs = msgObj.kwargs && typeof msgObj.kwargs === "object" ? msgObj.kwargs : msgObj;
|
|
448
|
+
const contentBlocks = kwargs.contentBlocks;
|
|
449
|
+
const content = kwargs.content;
|
|
450
|
+
const blockSources = Array.isArray(contentBlocks) ? contentBlocks : Array.isArray(content) ? content : [];
|
|
451
|
+
const citations = [];
|
|
452
|
+
for (const block of blockSources) {
|
|
453
|
+
if (block == null || typeof block !== "object") continue;
|
|
454
|
+
const annotations = block.annotations;
|
|
455
|
+
if (!Array.isArray(annotations)) continue;
|
|
456
|
+
for (const annotation of annotations) {
|
|
457
|
+
if (!isCitationContentBlock(annotation)) continue;
|
|
458
|
+
citations.push({
|
|
459
|
+
url: annotation.url,
|
|
460
|
+
title: annotation.title,
|
|
461
|
+
source: annotation.source,
|
|
462
|
+
citedText: annotation.citedText,
|
|
463
|
+
startIndex: annotation.startIndex,
|
|
464
|
+
endIndex: annotation.endIndex
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return citations;
|
|
469
|
+
}
|
|
470
|
+
function buildSourceProviderMetadata(citation) {
|
|
471
|
+
const langchain = {};
|
|
472
|
+
if (typeof citation.citedText === "string") {
|
|
473
|
+
langchain.citedText = citation.citedText;
|
|
474
|
+
}
|
|
475
|
+
if (typeof citation.startIndex === "number") {
|
|
476
|
+
langchain.startIndex = citation.startIndex;
|
|
477
|
+
}
|
|
478
|
+
if (typeof citation.endIndex === "number") {
|
|
479
|
+
langchain.endIndex = citation.endIndex;
|
|
480
|
+
}
|
|
481
|
+
if (typeof citation.source === "string") {
|
|
482
|
+
langchain.source = citation.source;
|
|
483
|
+
}
|
|
484
|
+
if (Object.keys(langchain).length === 0) {
|
|
485
|
+
return void 0;
|
|
486
|
+
}
|
|
487
|
+
return { langchain };
|
|
488
|
+
}
|
|
489
|
+
function emitSourceChunks(citations, messageId, emittedSourceIds, controller) {
|
|
490
|
+
var _a, _b, _c, _d;
|
|
491
|
+
for (const citation of citations) {
|
|
492
|
+
if (citation.url) {
|
|
493
|
+
if (emittedSourceIds.has(citation.url)) continue;
|
|
494
|
+
emittedSourceIds.add(citation.url);
|
|
495
|
+
const providerMetadata2 = buildSourceProviderMetadata(citation);
|
|
496
|
+
controller.enqueue({
|
|
497
|
+
type: "source-url",
|
|
498
|
+
sourceId: citation.url,
|
|
499
|
+
url: citation.url,
|
|
500
|
+
...citation.title ? { title: citation.title } : {},
|
|
501
|
+
...providerMetadata2 ? { providerMetadata: providerMetadata2 } : {}
|
|
502
|
+
});
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
const title = (_a = citation.title) != null ? _a : citation.source;
|
|
506
|
+
if (!title) continue;
|
|
507
|
+
const sourceId = `${messageId}:${title}:${(_b = citation.citedText) != null ? _b : ""}:${(_c = citation.startIndex) != null ? _c : ""}:${(_d = citation.endIndex) != null ? _d : ""}`;
|
|
508
|
+
if (emittedSourceIds.has(sourceId)) continue;
|
|
509
|
+
emittedSourceIds.add(sourceId);
|
|
510
|
+
const providerMetadata = buildSourceProviderMetadata(citation);
|
|
511
|
+
controller.enqueue({
|
|
512
|
+
type: "source-document",
|
|
513
|
+
sourceId,
|
|
514
|
+
mediaType: "text/plain",
|
|
515
|
+
title,
|
|
516
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
}
|
|
429
520
|
function isImageGenerationOutput(obj) {
|
|
430
521
|
return obj != null && typeof obj === "object" && "type" in obj && obj.type === "image_generation_call";
|
|
431
522
|
}
|
|
@@ -616,6 +707,15 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
616
707
|
id: msgId
|
|
617
708
|
});
|
|
618
709
|
}
|
|
710
|
+
const citations = extractCitationsFromContentBlocks(msg);
|
|
711
|
+
if (citations.length > 0) {
|
|
712
|
+
emitSourceChunks(
|
|
713
|
+
citations,
|
|
714
|
+
msgId,
|
|
715
|
+
state.emittedSourceIds,
|
|
716
|
+
controller
|
|
717
|
+
);
|
|
718
|
+
}
|
|
619
719
|
} else if (isToolMessageType(msg)) {
|
|
620
720
|
const msgObj = msg;
|
|
621
721
|
const dataSource = msgObj.type === "constructor" && msgObj.kwargs && typeof msgObj.kwargs === "object" ? msgObj.kwargs : msgObj;
|
|
@@ -763,6 +863,15 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
763
863
|
emittedReasoningIds.add(reasoningId);
|
|
764
864
|
}
|
|
765
865
|
}
|
|
866
|
+
const valuesCitations = extractCitationsFromContentBlocks(msg);
|
|
867
|
+
if (valuesCitations.length > 0) {
|
|
868
|
+
emitSourceChunks(
|
|
869
|
+
valuesCitations,
|
|
870
|
+
msgId,
|
|
871
|
+
state.emittedSourceIds,
|
|
872
|
+
controller
|
|
873
|
+
);
|
|
874
|
+
}
|
|
766
875
|
}
|
|
767
876
|
}
|
|
768
877
|
}
|
|
@@ -912,6 +1021,15 @@ function processStreamEventsEvent(event, state, controller) {
|
|
|
912
1021
|
id: (_c = state.textMessageId) != null ? _c : state.messageId
|
|
913
1022
|
});
|
|
914
1023
|
}
|
|
1024
|
+
const citations = extractCitationsFromContentBlocks(chunk);
|
|
1025
|
+
if (citations.length > 0) {
|
|
1026
|
+
emitSourceChunks(
|
|
1027
|
+
citations,
|
|
1028
|
+
state.messageId,
|
|
1029
|
+
state.emittedSourceIds,
|
|
1030
|
+
controller
|
|
1031
|
+
);
|
|
1032
|
+
}
|
|
915
1033
|
}
|
|
916
1034
|
break;
|
|
917
1035
|
}
|
|
@@ -959,7 +1077,8 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
959
1077
|
/** Track the ID used for text-start to ensure text-end uses the same ID */
|
|
960
1078
|
textMessageId: null,
|
|
961
1079
|
/** Track the ID used for reasoning-start to ensure reasoning-end uses the same ID */
|
|
962
|
-
reasoningMessageId: null
|
|
1080
|
+
reasoningMessageId: null,
|
|
1081
|
+
emittedSourceIds: /* @__PURE__ */ new Set()
|
|
963
1082
|
};
|
|
964
1083
|
const langGraphState = {
|
|
965
1084
|
messageSeen: /* @__PURE__ */ new Map(),
|
|
@@ -970,7 +1089,8 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
970
1089
|
messageReasoningIds: /* @__PURE__ */ new Map(),
|
|
971
1090
|
toolCallInfoByIndex: /* @__PURE__ */ new Map(),
|
|
972
1091
|
currentStep: null,
|
|
973
|
-
emittedToolCallsByKey: /* @__PURE__ */ new Map()
|
|
1092
|
+
emittedToolCallsByKey: /* @__PURE__ */ new Map(),
|
|
1093
|
+
emittedSourceIds: /* @__PURE__ */ new Set()
|
|
974
1094
|
};
|
|
975
1095
|
let streamType = null;
|
|
976
1096
|
const getAsyncIterator = () => {
|