@ai-sdk/langchain 2.0.210 → 2.0.212
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 +13 -0
- package/dist/index.js +75 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +75 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +5 -11
- package/src/types.ts +11 -11
- package/src/utils.ts +95 -54
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -455,8 +455,24 @@ function extractImageOutputs(additionalKwargs) {
|
|
|
455
455
|
if (!Array.isArray(toolOutputs)) return [];
|
|
456
456
|
return toolOutputs.filter(isImageGenerationOutput);
|
|
457
457
|
}
|
|
458
|
+
function getOrCreateMessageSeen(messageSeen, msgId) {
|
|
459
|
+
let seen = messageSeen.get(msgId);
|
|
460
|
+
if (!seen) {
|
|
461
|
+
seen = {};
|
|
462
|
+
messageSeen.set(msgId, seen);
|
|
463
|
+
}
|
|
464
|
+
return seen;
|
|
465
|
+
}
|
|
466
|
+
function getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId) {
|
|
467
|
+
let toolCallInfo = toolCallInfoByIndex.get(msgId);
|
|
468
|
+
if (!toolCallInfo) {
|
|
469
|
+
toolCallInfo = /* @__PURE__ */ new Map();
|
|
470
|
+
toolCallInfoByIndex.set(msgId, toolCallInfo);
|
|
471
|
+
}
|
|
472
|
+
return toolCallInfo;
|
|
473
|
+
}
|
|
458
474
|
function processLangGraphEvent(event, state, controller) {
|
|
459
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m
|
|
475
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
460
476
|
const {
|
|
461
477
|
messageSeen,
|
|
462
478
|
messageConcat,
|
|
@@ -497,16 +513,16 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
497
513
|
const langgraphStep = typeof (metadata == null ? void 0 : metadata.langgraph_step) === "number" ? metadata.langgraph_step : null;
|
|
498
514
|
if (langgraphStep !== null && langgraphStep !== state.currentStep) {
|
|
499
515
|
if (state.currentStep !== null) {
|
|
500
|
-
for (const [id, seen] of
|
|
516
|
+
for (const [id, seen] of messageSeen) {
|
|
501
517
|
if (seen.text) {
|
|
502
518
|
controller.enqueue({ type: "text-end", id });
|
|
503
519
|
}
|
|
504
520
|
if (seen.reasoning) {
|
|
505
521
|
controller.enqueue({ type: "reasoning-end", id });
|
|
506
522
|
}
|
|
507
|
-
delete
|
|
508
|
-
delete
|
|
509
|
-
delete
|
|
523
|
+
messageSeen.delete(id);
|
|
524
|
+
messageConcat.delete(id);
|
|
525
|
+
messageReasoningIds.delete(id);
|
|
510
526
|
}
|
|
511
527
|
controller.enqueue({ type: "finish-step" });
|
|
512
528
|
}
|
|
@@ -514,16 +530,18 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
514
530
|
state.currentStep = langgraphStep;
|
|
515
531
|
}
|
|
516
532
|
if (import_messages.AIMessageChunk.isInstance(msg)) {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
533
|
+
const existingMessage = messageConcat.get(msgId);
|
|
534
|
+
if (existingMessage) {
|
|
535
|
+
messageConcat.set(
|
|
536
|
+
msgId,
|
|
537
|
+
existingMessage.concat(msg)
|
|
520
538
|
);
|
|
521
539
|
} else {
|
|
522
|
-
messageConcat
|
|
540
|
+
messageConcat.set(msgId, msg);
|
|
523
541
|
}
|
|
524
542
|
}
|
|
525
543
|
if (isAIMessageChunk(msg)) {
|
|
526
|
-
const concatChunk = messageConcat
|
|
544
|
+
const concatChunk = messageConcat.get(msgId);
|
|
527
545
|
const msgObj = msg;
|
|
528
546
|
const dataSource = msgObj.type === "constructor" && msgObj.kwargs && typeof msgObj.kwargs === "object" ? msgObj.kwargs : msgObj;
|
|
529
547
|
const additionalKwargs = dataSource.additional_kwargs;
|
|
@@ -544,28 +562,34 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
544
562
|
for (const toolCallChunk of toolCallChunks) {
|
|
545
563
|
const idx = (_a = toolCallChunk.index) != null ? _a : 0;
|
|
546
564
|
if (toolCallChunk.id) {
|
|
547
|
-
(
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
565
|
+
getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId).set(
|
|
566
|
+
idx,
|
|
567
|
+
{
|
|
568
|
+
id: toolCallChunk.id,
|
|
569
|
+
name: toolCallChunk.name || ((_c = (_b = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _b[idx]) == null ? void 0 : _c.name) || "unknown"
|
|
570
|
+
}
|
|
571
|
+
);
|
|
552
572
|
}
|
|
553
|
-
const
|
|
573
|
+
const storedToolCallInfo = (_d = toolCallInfoByIndex.get(msgId)) == null ? void 0 : _d.get(idx);
|
|
574
|
+
const toolCallId = toolCallChunk.id || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.id) || ((_f = (_e = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _e[idx]) == null ? void 0 : _f.id);
|
|
554
575
|
if (!toolCallId) {
|
|
555
576
|
continue;
|
|
556
577
|
}
|
|
557
|
-
const toolName = toolCallChunk.name || (
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
578
|
+
const toolName = toolCallChunk.name || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.name) || ((_h = (_g = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _g[idx]) == null ? void 0 : _h.name) || "unknown";
|
|
579
|
+
const seen = messageSeen.get(msgId);
|
|
580
|
+
if (!((_i = seen == null ? void 0 : seen.tool) == null ? void 0 : _i.has(toolCallId))) {
|
|
581
|
+
const updatedSeen = getOrCreateMessageSeen(messageSeen, msgId);
|
|
582
|
+
(_j = updatedSeen.tool) != null ? _j : updatedSeen.tool = /* @__PURE__ */ new Set();
|
|
583
|
+
updatedSeen.tool.add(toolCallId);
|
|
584
|
+
if (!emittedToolCalls.has(toolCallId)) {
|
|
585
|
+
emittedToolCalls.add(toolCallId);
|
|
586
|
+
controller.enqueue({
|
|
587
|
+
type: "tool-input-start",
|
|
588
|
+
toolCallId,
|
|
589
|
+
toolName,
|
|
590
|
+
dynamic: true
|
|
591
|
+
});
|
|
592
|
+
}
|
|
569
593
|
}
|
|
570
594
|
if (toolCallChunk.args) {
|
|
571
595
|
controller.enqueue({
|
|
@@ -579,18 +603,18 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
579
603
|
}
|
|
580
604
|
const chunkReasoningId = extractReasoningId(msg);
|
|
581
605
|
if (chunkReasoningId) {
|
|
582
|
-
if (!messageReasoningIds
|
|
583
|
-
messageReasoningIds
|
|
606
|
+
if (!messageReasoningIds.has(msgId)) {
|
|
607
|
+
messageReasoningIds.set(msgId, chunkReasoningId);
|
|
584
608
|
}
|
|
585
609
|
emittedReasoningIds.add(chunkReasoningId);
|
|
586
610
|
}
|
|
587
611
|
const reasoning = extractReasoningFromContentBlocks(msg);
|
|
588
612
|
if (reasoning) {
|
|
589
|
-
const reasoningId = (
|
|
590
|
-
|
|
613
|
+
const reasoningId = (_l = (_k = messageReasoningIds.get(msgId)) != null ? _k : chunkReasoningId) != null ? _l : msgId;
|
|
614
|
+
const seen = messageSeen.get(msgId);
|
|
615
|
+
if (!(seen == null ? void 0 : seen.reasoning)) {
|
|
591
616
|
controller.enqueue({ type: "reasoning-start", id: msgId });
|
|
592
|
-
(
|
|
593
|
-
messageSeen[msgId].reasoning = true;
|
|
617
|
+
getOrCreateMessageSeen(messageSeen, msgId).reasoning = true;
|
|
594
618
|
}
|
|
595
619
|
controller.enqueue({
|
|
596
620
|
type: "reasoning-delta",
|
|
@@ -601,10 +625,10 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
601
625
|
}
|
|
602
626
|
const text = getMessageText(msg);
|
|
603
627
|
if (text) {
|
|
604
|
-
|
|
628
|
+
const seen = messageSeen.get(msgId);
|
|
629
|
+
if (!(seen == null ? void 0 : seen.text)) {
|
|
605
630
|
controller.enqueue({ type: "text-start", id: msgId });
|
|
606
|
-
(
|
|
607
|
-
messageSeen[msgId].text = true;
|
|
631
|
+
getOrCreateMessageSeen(messageSeen, msgId).text = true;
|
|
608
632
|
}
|
|
609
633
|
controller.enqueue({
|
|
610
634
|
type: "text-delta",
|
|
@@ -636,15 +660,15 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
636
660
|
return;
|
|
637
661
|
}
|
|
638
662
|
case "values": {
|
|
639
|
-
for (const [id, seen] of
|
|
663
|
+
for (const [id, seen] of messageSeen) {
|
|
640
664
|
if (seen.text) controller.enqueue({ type: "text-end", id });
|
|
641
665
|
if (seen.tool) {
|
|
642
|
-
for (const
|
|
643
|
-
const concatMsg = messageConcat
|
|
644
|
-
const toolCall = (
|
|
666
|
+
for (const toolCallId of seen.tool) {
|
|
667
|
+
const concatMsg = messageConcat.get(id);
|
|
668
|
+
const toolCall = (_m = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _m.find(
|
|
645
669
|
(call) => call.id === toolCallId
|
|
646
670
|
);
|
|
647
|
-
if (
|
|
671
|
+
if (toolCall) {
|
|
648
672
|
emittedToolCalls.add(toolCallId);
|
|
649
673
|
const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`;
|
|
650
674
|
emittedToolCallsByKey.set(toolCallKey, toolCallId);
|
|
@@ -661,9 +685,9 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
661
685
|
if (seen.reasoning) {
|
|
662
686
|
controller.enqueue({ type: "reasoning-end", id });
|
|
663
687
|
}
|
|
664
|
-
delete
|
|
665
|
-
delete
|
|
666
|
-
delete
|
|
688
|
+
messageSeen.delete(id);
|
|
689
|
+
messageConcat.delete(id);
|
|
690
|
+
messageReasoningIds.delete(id);
|
|
667
691
|
}
|
|
668
692
|
if (data != null && typeof data === "object" && "messages" in data) {
|
|
669
693
|
const messages = data.messages;
|
|
@@ -743,7 +767,7 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
743
767
|
}
|
|
744
768
|
}
|
|
745
769
|
const reasoningId = extractReasoningId(msg);
|
|
746
|
-
const wasStreamedThisRequest =
|
|
770
|
+
const wasStreamedThisRequest = messageSeen.has(msgId);
|
|
747
771
|
const hasToolCalls = toolCalls && toolCalls.length > 0;
|
|
748
772
|
const shouldEmitReasoning = reasoningId && !emittedReasoningIds.has(reasoningId) && (wasStreamedThisRequest || !hasToolCalls);
|
|
749
773
|
if (shouldEmitReasoning) {
|
|
@@ -958,13 +982,13 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
958
982
|
reasoningMessageId: null
|
|
959
983
|
};
|
|
960
984
|
const langGraphState = {
|
|
961
|
-
messageSeen:
|
|
962
|
-
messageConcat:
|
|
985
|
+
messageSeen: /* @__PURE__ */ new Map(),
|
|
986
|
+
messageConcat: /* @__PURE__ */ new Map(),
|
|
963
987
|
emittedToolCalls: /* @__PURE__ */ new Set(),
|
|
964
988
|
emittedImages: /* @__PURE__ */ new Set(),
|
|
965
989
|
emittedReasoningIds: /* @__PURE__ */ new Set(),
|
|
966
|
-
messageReasoningIds:
|
|
967
|
-
toolCallInfoByIndex:
|
|
990
|
+
messageReasoningIds: /* @__PURE__ */ new Map(),
|
|
991
|
+
toolCallInfoByIndex: /* @__PURE__ */ new Map(),
|
|
968
992
|
currentStep: null,
|
|
969
993
|
emittedToolCallsByKey: /* @__PURE__ */ new Map()
|
|
970
994
|
};
|
|
@@ -1059,7 +1083,7 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1059
1083
|
}
|
|
1060
1084
|
controller.enqueue({ type: "finish" });
|
|
1061
1085
|
} else if (streamType === "langgraph") {
|
|
1062
|
-
for (const [id, seen] of
|
|
1086
|
+
for (const [id, seen] of langGraphState.messageSeen) {
|
|
1063
1087
|
if (seen.text) {
|
|
1064
1088
|
controller.enqueue({ type: "text-end", id });
|
|
1065
1089
|
}
|