@ai-sdk/langchain 2.0.266 → 2.0.268
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 +16 -0
- package/dist/index.js +102 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +102 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/adapter.ts +4 -3
- package/src/types.ts +6 -4
- package/src/utils.ts +134 -35
package/dist/index.mjs
CHANGED
|
@@ -595,6 +595,38 @@ function getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId) {
|
|
|
595
595
|
function getLangGraphNamespaceKey(namespace) {
|
|
596
596
|
return JSON.stringify(namespace != null ? namespace : []);
|
|
597
597
|
}
|
|
598
|
+
function getOrCreateNamespaceSet(setsByNamespace, namespace) {
|
|
599
|
+
let values = setsByNamespace.get(namespace);
|
|
600
|
+
if (!values) {
|
|
601
|
+
values = /* @__PURE__ */ new Set();
|
|
602
|
+
setsByNamespace.set(namespace, values);
|
|
603
|
+
}
|
|
604
|
+
return values;
|
|
605
|
+
}
|
|
606
|
+
function hasEmittedToolCallInCurrentStep(state, toolCallId, namespace) {
|
|
607
|
+
var _a;
|
|
608
|
+
return !state.currentStepsByNamespace.has(namespace) ? state.emittedToolCalls.has(toolCallId) : ((_a = state.emittedToolCallsInCurrentStepByNamespace.get(namespace)) == null ? void 0 : _a.has(toolCallId)) === true;
|
|
609
|
+
}
|
|
610
|
+
function markToolCallEmitted(state, toolCallId, namespace) {
|
|
611
|
+
state.emittedToolCalls.add(toolCallId);
|
|
612
|
+
if (state.currentStepsByNamespace.has(namespace)) {
|
|
613
|
+
getOrCreateNamespaceSet(
|
|
614
|
+
state.emittedToolCallsInCurrentStepByNamespace,
|
|
615
|
+
namespace
|
|
616
|
+
).add(toolCallId);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function findMessageCurrentStepNamespace(state, messageId) {
|
|
620
|
+
for (const [
|
|
621
|
+
namespace,
|
|
622
|
+
messageIds
|
|
623
|
+
] of state.messageIdsInCurrentStepByNamespace) {
|
|
624
|
+
if (messageIds.has(messageId)) {
|
|
625
|
+
return namespace;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return void 0;
|
|
629
|
+
}
|
|
598
630
|
function closeStepNamespaceMessages(state, stepNamespace, controller) {
|
|
599
631
|
let hasConcurrentMessageParts = false;
|
|
600
632
|
for (const [id, seen] of state.messageSeen) {
|
|
@@ -621,7 +653,7 @@ function closeStepNamespaceMessages(state, stepNamespace, controller) {
|
|
|
621
653
|
return hasConcurrentMessageParts;
|
|
622
654
|
}
|
|
623
655
|
function processLangGraphEvent(event, state, controller) {
|
|
624
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
656
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
625
657
|
const {
|
|
626
658
|
messageSeen,
|
|
627
659
|
messageConcat,
|
|
@@ -634,6 +666,7 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
634
666
|
} = state;
|
|
635
667
|
const [rawNamespace, type, data] = parseLangGraphEvent(event);
|
|
636
668
|
const namespace = Array.isArray(rawNamespace) && rawNamespace.every((segment) => typeof segment === "string") ? rawNamespace : void 0;
|
|
669
|
+
const eventNamespace = getLangGraphNamespaceKey(namespace);
|
|
637
670
|
controller = createLangGraphNamespaceController(controller, state, namespace);
|
|
638
671
|
switch (type) {
|
|
639
672
|
case "custom": {
|
|
@@ -665,25 +698,47 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
665
698
|
state.messageNamespaces.set(msgId, namespace);
|
|
666
699
|
}
|
|
667
700
|
const langgraphStep = typeof (metadata == null ? void 0 : metadata.langgraph_step) === "number" ? metadata.langgraph_step : null;
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
701
|
+
if (langgraphStep !== null) {
|
|
702
|
+
const currentStep = (_a = state.currentStepsByNamespace.get(eventNamespace)) != null ? _a : null;
|
|
703
|
+
if (currentStep === null) {
|
|
704
|
+
if (state.currentStepsByNamespace.size === 0) {
|
|
705
|
+
controller.enqueue({ type: "start-step" });
|
|
706
|
+
}
|
|
707
|
+
state.currentStepsByNamespace.set(eventNamespace, langgraphStep);
|
|
708
|
+
state.messageIdsInCurrentStepByNamespace.set(
|
|
709
|
+
eventNamespace,
|
|
710
|
+
/* @__PURE__ */ new Set()
|
|
711
|
+
);
|
|
712
|
+
state.emittedToolCallsInCurrentStepByNamespace.set(
|
|
713
|
+
eventNamespace,
|
|
714
|
+
/* @__PURE__ */ new Set()
|
|
715
|
+
);
|
|
716
|
+
} else if (langgraphStep !== currentStep) {
|
|
674
717
|
const hasConcurrentMessageParts = closeStepNamespaceMessages(
|
|
675
718
|
state,
|
|
676
|
-
|
|
719
|
+
eventNamespace,
|
|
677
720
|
controller
|
|
678
721
|
);
|
|
679
722
|
if (!hasConcurrentMessageParts) {
|
|
680
723
|
controller.enqueue({ type: "finish-step" });
|
|
681
|
-
controller.enqueue({ type: "start-step" });
|
|
682
724
|
}
|
|
683
|
-
} else {
|
|
684
725
|
controller.enqueue({ type: "start-step" });
|
|
726
|
+
state.currentStepsByNamespace.set(eventNamespace, langgraphStep);
|
|
727
|
+
state.messageIdsInCurrentStepByNamespace.set(
|
|
728
|
+
eventNamespace,
|
|
729
|
+
/* @__PURE__ */ new Set()
|
|
730
|
+
);
|
|
731
|
+
state.emittedToolCallsInCurrentStepByNamespace.set(
|
|
732
|
+
eventNamespace,
|
|
733
|
+
/* @__PURE__ */ new Set()
|
|
734
|
+
);
|
|
685
735
|
}
|
|
686
|
-
|
|
736
|
+
}
|
|
737
|
+
if (state.currentStepsByNamespace.has(eventNamespace)) {
|
|
738
|
+
getOrCreateNamespaceSet(
|
|
739
|
+
state.messageIdsInCurrentStepByNamespace,
|
|
740
|
+
eventNamespace
|
|
741
|
+
).add(msgId);
|
|
687
742
|
}
|
|
688
743
|
if (AIMessageChunk.isInstance(msg)) {
|
|
689
744
|
const existingMessage = messageConcat.get(msgId);
|
|
@@ -716,29 +771,33 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
716
771
|
const toolCallChunks = dataSource.tool_call_chunks;
|
|
717
772
|
if (toolCallChunks == null ? void 0 : toolCallChunks.length) {
|
|
718
773
|
for (const toolCallChunk of toolCallChunks) {
|
|
719
|
-
const idx = (
|
|
774
|
+
const idx = (_b = toolCallChunk.index) != null ? _b : 0;
|
|
720
775
|
if (toolCallChunk.id) {
|
|
721
776
|
getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId).set(
|
|
722
777
|
idx,
|
|
723
778
|
{
|
|
724
779
|
id: toolCallChunk.id,
|
|
725
|
-
name: toolCallChunk.name || ((
|
|
780
|
+
name: toolCallChunk.name || ((_d = (_c = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _c[idx]) == null ? void 0 : _d.name) || "unknown"
|
|
726
781
|
}
|
|
727
782
|
);
|
|
728
783
|
}
|
|
729
|
-
const storedToolCallInfo = (
|
|
730
|
-
const toolCallId = toolCallChunk.id || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.id) || ((
|
|
784
|
+
const storedToolCallInfo = (_e = toolCallInfoByIndex.get(msgId)) == null ? void 0 : _e.get(idx);
|
|
785
|
+
const toolCallId = toolCallChunk.id || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.id) || ((_g = (_f = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _f[idx]) == null ? void 0 : _g.id);
|
|
731
786
|
if (!toolCallId) {
|
|
732
787
|
continue;
|
|
733
788
|
}
|
|
734
|
-
const toolName = toolCallChunk.name || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.name) || ((
|
|
789
|
+
const toolName = toolCallChunk.name || (storedToolCallInfo == null ? void 0 : storedToolCallInfo.name) || ((_i = (_h = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _h[idx]) == null ? void 0 : _i.name) || "unknown";
|
|
735
790
|
const seen = messageSeen.get(msgId);
|
|
736
|
-
if (!((
|
|
791
|
+
if (!((_j = seen == null ? void 0 : seen.tool) == null ? void 0 : _j.has(toolCallId))) {
|
|
737
792
|
const updatedSeen = getOrCreateMessageSeen(messageSeen, msgId);
|
|
738
|
-
(
|
|
793
|
+
(_k = updatedSeen.tool) != null ? _k : updatedSeen.tool = /* @__PURE__ */ new Set();
|
|
739
794
|
updatedSeen.tool.add(toolCallId);
|
|
740
|
-
if (!
|
|
741
|
-
|
|
795
|
+
if (!hasEmittedToolCallInCurrentStep(
|
|
796
|
+
state,
|
|
797
|
+
toolCallId,
|
|
798
|
+
eventNamespace
|
|
799
|
+
)) {
|
|
800
|
+
markToolCallEmitted(state, toolCallId, eventNamespace);
|
|
742
801
|
controller.enqueue({
|
|
743
802
|
type: "tool-input-start",
|
|
744
803
|
toolCallId,
|
|
@@ -766,7 +825,7 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
766
825
|
}
|
|
767
826
|
const reasoning = extractReasoningFromContentBlocks(msg);
|
|
768
827
|
if (reasoning) {
|
|
769
|
-
const reasoningId = (
|
|
828
|
+
const reasoningId = (_m = (_l = messageReasoningIds.get(msgId)) != null ? _l : chunkReasoningId) != null ? _m : msgId;
|
|
770
829
|
const seen = messageSeen.get(msgId);
|
|
771
830
|
if (!(seen == null ? void 0 : seen.reasoning)) {
|
|
772
831
|
controller.enqueue({ type: "reasoning-start", id: msgId });
|
|
@@ -830,15 +889,18 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
830
889
|
}
|
|
831
890
|
case "values": {
|
|
832
891
|
for (const [id, seen] of messageSeen) {
|
|
892
|
+
const messageNamespace = getLangGraphNamespaceKey(
|
|
893
|
+
state.messageNamespaces.get(id)
|
|
894
|
+
);
|
|
833
895
|
if (seen.text) controller.enqueue({ type: "text-end", id });
|
|
834
896
|
if (seen.tool) {
|
|
835
897
|
for (const toolCallId of seen.tool) {
|
|
836
898
|
const concatMsg = messageConcat.get(id);
|
|
837
|
-
const toolCall = (
|
|
899
|
+
const toolCall = (_n = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _n.find(
|
|
838
900
|
(call) => call.id === toolCallId
|
|
839
901
|
);
|
|
840
902
|
if (toolCall) {
|
|
841
|
-
|
|
903
|
+
markToolCallEmitted(state, toolCallId, messageNamespace);
|
|
842
904
|
const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`;
|
|
843
905
|
emittedToolCallsByKey.set(toolCallKey, toolCallId);
|
|
844
906
|
controller.enqueue({
|
|
@@ -916,8 +978,18 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
916
978
|
}
|
|
917
979
|
if (toolCalls && toolCalls.length > 0) {
|
|
918
980
|
for (const toolCall of toolCalls) {
|
|
919
|
-
|
|
920
|
-
|
|
981
|
+
const messageNamespace = findMessageCurrentStepNamespace(
|
|
982
|
+
state,
|
|
983
|
+
msgId
|
|
984
|
+
);
|
|
985
|
+
const lifecycleNamespace = messageNamespace != null ? messageNamespace : eventNamespace;
|
|
986
|
+
const wasObservedInCurrentStep = messageNamespace !== void 0;
|
|
987
|
+
if (toolCall.id && !hasEmittedToolCallInCurrentStep(
|
|
988
|
+
state,
|
|
989
|
+
toolCall.id,
|
|
990
|
+
lifecycleNamespace
|
|
991
|
+
) && (wasObservedInCurrentStep || !emittedToolCalls.has(toolCall.id) && !completedToolCallIds.has(toolCall.id))) {
|
|
992
|
+
markToolCallEmitted(state, toolCall.id, lifecycleNamespace);
|
|
921
993
|
const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`;
|
|
922
994
|
emittedToolCallsByKey.set(toolCallKey, toolCall.id);
|
|
923
995
|
controller.enqueue({
|
|
@@ -982,7 +1054,7 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
982
1054
|
const toolCallKey = `${toolName}:${JSON.stringify(input)}`;
|
|
983
1055
|
const toolCallId = emittedToolCallsByKey.get(toolCallKey) || actionRequest.id || `hitl-${toolName}-${Date.now()}`;
|
|
984
1056
|
if (!emittedToolCalls.has(toolCallId)) {
|
|
985
|
-
|
|
1057
|
+
markToolCallEmitted(state, toolCallId, eventNamespace);
|
|
986
1058
|
emittedToolCallsByKey.set(toolCallKey, toolCallId);
|
|
987
1059
|
controller.enqueue({
|
|
988
1060
|
type: "tool-input-start",
|
|
@@ -1193,13 +1265,14 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1193
1265
|
messageSeen: /* @__PURE__ */ new Map(),
|
|
1194
1266
|
messageNamespaces: /* @__PURE__ */ new Map(),
|
|
1195
1267
|
messageConcat: /* @__PURE__ */ new Map(),
|
|
1268
|
+
messageIdsInCurrentStepByNamespace: /* @__PURE__ */ new Map(),
|
|
1196
1269
|
emittedToolCalls: /* @__PURE__ */ new Set(),
|
|
1270
|
+
emittedToolCallsInCurrentStepByNamespace: /* @__PURE__ */ new Map(),
|
|
1197
1271
|
emittedImages: /* @__PURE__ */ new Set(),
|
|
1198
1272
|
emittedReasoningIds: /* @__PURE__ */ new Set(),
|
|
1199
1273
|
messageReasoningIds: /* @__PURE__ */ new Map(),
|
|
1200
1274
|
toolCallInfoByIndex: /* @__PURE__ */ new Map(),
|
|
1201
|
-
|
|
1202
|
-
stepNamespace: null,
|
|
1275
|
+
currentStepsByNamespace: /* @__PURE__ */ new Map(),
|
|
1203
1276
|
emittedToolCallsByKey: /* @__PURE__ */ new Map(),
|
|
1204
1277
|
emittedSourceIds: /* @__PURE__ */ new Set()
|
|
1205
1278
|
};
|
|
@@ -1313,7 +1386,7 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1313
1386
|
});
|
|
1314
1387
|
}
|
|
1315
1388
|
}
|
|
1316
|
-
if (langGraphState.
|
|
1389
|
+
if (langGraphState.currentStepsByNamespace.size > 0) {
|
|
1317
1390
|
controller.enqueue({ type: "finish-step" });
|
|
1318
1391
|
}
|
|
1319
1392
|
controller.enqueue({ type: "finish" });
|