@ai-sdk/langchain 2.0.210 → 2.0.211

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,12 @@
1
1
  # @ai-sdk/langchain
2
2
 
3
+ ## 2.0.211
4
+
5
+ ### Patch Changes
6
+
7
+ - b51014f: fix(langchain): prevent polluting global object.prototype
8
+ - ai@6.0.204
9
+
3
10
  ## 2.0.210
4
11
 
5
12
  ### Patch Changes
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, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
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 Object.entries(messageSeen)) {
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 messageSeen[id];
508
- delete messageConcat[id];
509
- delete messageReasoningIds[id];
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
- if (messageConcat[msgId]) {
518
- messageConcat[msgId] = messageConcat[msgId].concat(
519
- msg
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[msgId] = msg;
540
+ messageConcat.set(msgId, msg);
523
541
  }
524
542
  }
525
543
  if (isAIMessageChunk(msg)) {
526
- const concatChunk = messageConcat[msgId];
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
- (_b = toolCallInfoByIndex[msgId]) != null ? _b : toolCallInfoByIndex[msgId] = {};
548
- toolCallInfoByIndex[msgId][idx] = {
549
- id: toolCallChunk.id,
550
- name: toolCallChunk.name || ((_d = (_c = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _c[idx]) == null ? void 0 : _d.name) || "unknown"
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 toolCallId = toolCallChunk.id || ((_f = (_e = toolCallInfoByIndex[msgId]) == null ? void 0 : _e[idx]) == null ? void 0 : _f.id) || ((_h = (_g = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _g[idx]) == null ? void 0 : _h.id);
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 || ((_j = (_i = toolCallInfoByIndex[msgId]) == null ? void 0 : _i[idx]) == null ? void 0 : _j.name) || ((_l = (_k = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _k[idx]) == null ? void 0 : _l.name) || "unknown";
558
- if (!((_n = (_m = messageSeen[msgId]) == null ? void 0 : _m.tool) == null ? void 0 : _n[toolCallId])) {
559
- controller.enqueue({
560
- type: "tool-input-start",
561
- toolCallId,
562
- toolName,
563
- dynamic: true
564
- });
565
- (_o = messageSeen[msgId]) != null ? _o : messageSeen[msgId] = {};
566
- (_q = (_p = messageSeen[msgId]).tool) != null ? _q : _p.tool = {};
567
- messageSeen[msgId].tool[toolCallId] = true;
568
- emittedToolCalls.add(toolCallId);
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[msgId]) {
583
- messageReasoningIds[msgId] = chunkReasoningId;
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 = (_s = (_r = messageReasoningIds[msgId]) != null ? _r : chunkReasoningId) != null ? _s : msgId;
590
- if (!((_t = messageSeen[msgId]) == null ? void 0 : _t.reasoning)) {
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
- (_u = messageSeen[msgId]) != null ? _u : messageSeen[msgId] = {};
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
- if (!((_v = messageSeen[msgId]) == null ? void 0 : _v.text)) {
628
+ const seen = messageSeen.get(msgId);
629
+ if (!(seen == null ? void 0 : seen.text)) {
605
630
  controller.enqueue({ type: "text-start", id: msgId });
606
- (_w = messageSeen[msgId]) != null ? _w : messageSeen[msgId] = {};
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 Object.entries(messageSeen)) {
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 [toolCallId, toolCallSeen] of Object.entries(seen.tool)) {
643
- const concatMsg = messageConcat[id];
644
- const toolCall = (_x = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _x.find(
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 (toolCallSeen && toolCall) {
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 messageSeen[id];
665
- delete messageConcat[id];
666
- delete messageReasoningIds[id];
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 = !!messageSeen[msgId];
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 Object.entries(langGraphState.messageSeen)) {
1086
+ for (const [id, seen] of langGraphState.messageSeen) {
1063
1087
  if (seen.text) {
1064
1088
  controller.enqueue({ type: "text-end", id });
1065
1089
  }