@ai-sdk/langchain 2.0.301 → 2.0.303

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 CHANGED
@@ -1,5 +1,20 @@
1
1
  # @ai-sdk/langchain
2
2
 
3
+ ## 2.0.303
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [0b38b6b]
8
+ - ai@6.0.295
9
+
10
+ ## 2.0.302
11
+
12
+ ### Patch Changes
13
+
14
+ - e33fecc: fix(langchain): start bare messages-mode tool lifecycles before emitting outputs
15
+ - Updated dependencies [16a04d7]
16
+ - ai@6.0.294
17
+
3
18
  ## 2.0.301
4
19
 
5
20
  ### Patch Changes
package/dist/index.js CHANGED
@@ -636,6 +636,16 @@ function markToolCallEmitted(state, toolCallId, namespace) {
636
636
  ).add(toolCallId);
637
637
  }
638
638
  }
639
+ function markToolCallStarted(state, toolCallId, namespace) {
640
+ markToolCallEmitted(state, toolCallId, namespace);
641
+ getOrCreateNamespaceSet(state.unfinishedToolCallsByNamespace, namespace).add(
642
+ toolCallId
643
+ );
644
+ }
645
+ function hasUnfinishedToolCall(state, toolCallId, namespace) {
646
+ var _a;
647
+ return ((_a = state.unfinishedToolCallsByNamespace.get(namespace)) == null ? void 0 : _a.has(toolCallId)) === true;
648
+ }
639
649
  function findMessageCurrentStepNamespace(state, messageId) {
640
650
  for (const [
641
651
  namespace,
@@ -673,7 +683,7 @@ function closeStepNamespaceMessages(state, stepNamespace, controller) {
673
683
  return hasConcurrentMessageParts;
674
684
  }
675
685
  function processLangGraphEvent(event, state, controller) {
676
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
686
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
677
687
  const {
678
688
  messageSeen,
679
689
  messageConcat,
@@ -717,12 +727,14 @@ function processLangGraphEvent(event, state, controller) {
717
727
  if (namespace !== void 0) {
718
728
  state.messageNamespaces.set(msgId, namespace);
719
729
  }
730
+ let startedUIReducerStep = false;
720
731
  const langgraphStep = typeof (metadata == null ? void 0 : metadata.langgraph_step) === "number" ? metadata.langgraph_step : null;
721
732
  if (langgraphStep !== null) {
722
733
  const currentStep = (_a = state.currentStepsByNamespace.get(eventNamespace)) != null ? _a : null;
723
734
  if (currentStep === null) {
724
735
  if (state.currentStepsByNamespace.size === 0) {
725
736
  controller.enqueue({ type: "start-step" });
737
+ startedUIReducerStep = true;
726
738
  }
727
739
  state.currentStepsByNamespace.set(eventNamespace, langgraphStep);
728
740
  state.messageIdsInCurrentStepByNamespace.set(
@@ -743,6 +755,7 @@ function processLangGraphEvent(event, state, controller) {
743
755
  controller.enqueue({ type: "finish-step" });
744
756
  }
745
757
  controller.enqueue({ type: "start-step" });
758
+ startedUIReducerStep = true;
746
759
  state.currentStepsByNamespace.set(eventNamespace, langgraphStep);
747
760
  state.messageIdsInCurrentStepByNamespace.set(
748
761
  eventNamespace,
@@ -817,7 +830,7 @@ function processLangGraphEvent(event, state, controller) {
817
830
  toolCallId,
818
831
  eventNamespace
819
832
  )) {
820
- markToolCallEmitted(state, toolCallId, eventNamespace);
833
+ markToolCallStarted(state, toolCallId, eventNamespace);
821
834
  controller.enqueue({
822
835
  type: "tool-input-start",
823
836
  toolCallId,
@@ -890,6 +903,25 @@ function processLangGraphEvent(event, state, controller) {
890
903
  const toolCallId = dataSource.tool_call_id;
891
904
  const status = dataSource.status;
892
905
  if (toolCallId) {
906
+ const wasEmittedInCurrentStep = hasEmittedToolCallInCurrentStep(
907
+ state,
908
+ toolCallId,
909
+ eventNamespace
910
+ );
911
+ const isDelayedOutputForPreviousLifecycle = !wasEmittedInCurrentStep && hasUnfinishedToolCall(state, toolCallId, eventNamespace);
912
+ if (!wasEmittedInCurrentStep && !isDelayedOutputForPreviousLifecycle) {
913
+ if (!startedUIReducerStep && state.currentStepsByNamespace.has(eventNamespace) && emittedToolCalls.has(toolCallId)) {
914
+ controller.enqueue({ type: "start-step" });
915
+ }
916
+ markToolCallStarted(state, toolCallId, eventNamespace);
917
+ controller.enqueue({
918
+ type: "tool-input-start",
919
+ toolCallId,
920
+ toolName: typeof dataSource.name === "string" ? dataSource.name : "unknown",
921
+ dynamic: true
922
+ });
923
+ }
924
+ (_n = state.unfinishedToolCallsByNamespace.get(eventNamespace)) == null ? void 0 : _n.delete(toolCallId);
893
925
  if (status === "error") {
894
926
  controller.enqueue({
895
927
  type: "tool-output-error",
@@ -916,7 +948,7 @@ function processLangGraphEvent(event, state, controller) {
916
948
  if (seen.tool) {
917
949
  for (const toolCallId of seen.tool) {
918
950
  const concatMsg = messageConcat.get(id);
919
- const toolCall = (_n = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _n.find(
951
+ const toolCall = (_o = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _o.find(
920
952
  (call) => call.id === toolCallId
921
953
  );
922
954
  if (toolCall) {
@@ -1009,7 +1041,7 @@ function processLangGraphEvent(event, state, controller) {
1009
1041
  toolCall.id,
1010
1042
  lifecycleNamespace
1011
1043
  ) && (wasObservedInCurrentStep || !emittedToolCalls.has(toolCall.id) && !completedToolCallIds.has(toolCall.id))) {
1012
- markToolCallEmitted(state, toolCall.id, lifecycleNamespace);
1044
+ markToolCallStarted(state, toolCall.id, lifecycleNamespace);
1013
1045
  const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`;
1014
1046
  emittedToolCallsByKey.set(toolCallKey, toolCall.id);
1015
1047
  controller.enqueue({
@@ -1074,7 +1106,7 @@ function processLangGraphEvent(event, state, controller) {
1074
1106
  const toolCallKey = `${toolName}:${JSON.stringify(input)}`;
1075
1107
  const toolCallId = emittedToolCallsByKey.get(toolCallKey) || actionRequest.id || `hitl-${toolName}-${Date.now()}`;
1076
1108
  if (!emittedToolCalls.has(toolCallId)) {
1077
- markToolCallEmitted(state, toolCallId, eventNamespace);
1109
+ markToolCallStarted(state, toolCallId, eventNamespace);
1078
1110
  emittedToolCallsByKey.set(toolCallKey, toolCallId);
1079
1111
  controller.enqueue({
1080
1112
  type: "tool-input-start",
@@ -1288,6 +1320,7 @@ function toUIMessageStream(stream, callbacks) {
1288
1320
  messageIdsInCurrentStepByNamespace: /* @__PURE__ */ new Map(),
1289
1321
  emittedToolCalls: /* @__PURE__ */ new Set(),
1290
1322
  emittedToolCallsInCurrentStepByNamespace: /* @__PURE__ */ new Map(),
1323
+ unfinishedToolCallsByNamespace: /* @__PURE__ */ new Map(),
1291
1324
  emittedImages: /* @__PURE__ */ new Set(),
1292
1325
  emittedReasoningIds: /* @__PURE__ */ new Set(),
1293
1326
  messageReasoningIds: /* @__PURE__ */ new Map(),