@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/dist/index.mjs CHANGED
@@ -435,8 +435,24 @@ function extractImageOutputs(additionalKwargs) {
435
435
  if (!Array.isArray(toolOutputs)) return [];
436
436
  return toolOutputs.filter(isImageGenerationOutput);
437
437
  }
438
+ function getOrCreateMessageSeen(messageSeen, msgId) {
439
+ let seen = messageSeen.get(msgId);
440
+ if (!seen) {
441
+ seen = {};
442
+ messageSeen.set(msgId, seen);
443
+ }
444
+ return seen;
445
+ }
446
+ function getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId) {
447
+ let toolCallInfo = toolCallInfoByIndex.get(msgId);
448
+ if (!toolCallInfo) {
449
+ toolCallInfo = /* @__PURE__ */ new Map();
450
+ toolCallInfoByIndex.set(msgId, toolCallInfo);
451
+ }
452
+ return toolCallInfo;
453
+ }
438
454
  function processLangGraphEvent(event, state, controller) {
439
- 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;
455
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
440
456
  const {
441
457
  messageSeen,
442
458
  messageConcat,
@@ -477,16 +493,16 @@ function processLangGraphEvent(event, state, controller) {
477
493
  const langgraphStep = typeof (metadata == null ? void 0 : metadata.langgraph_step) === "number" ? metadata.langgraph_step : null;
478
494
  if (langgraphStep !== null && langgraphStep !== state.currentStep) {
479
495
  if (state.currentStep !== null) {
480
- for (const [id, seen] of Object.entries(messageSeen)) {
496
+ for (const [id, seen] of messageSeen) {
481
497
  if (seen.text) {
482
498
  controller.enqueue({ type: "text-end", id });
483
499
  }
484
500
  if (seen.reasoning) {
485
501
  controller.enqueue({ type: "reasoning-end", id });
486
502
  }
487
- delete messageSeen[id];
488
- delete messageConcat[id];
489
- delete messageReasoningIds[id];
503
+ messageSeen.delete(id);
504
+ messageConcat.delete(id);
505
+ messageReasoningIds.delete(id);
490
506
  }
491
507
  controller.enqueue({ type: "finish-step" });
492
508
  }
@@ -494,16 +510,18 @@ function processLangGraphEvent(event, state, controller) {
494
510
  state.currentStep = langgraphStep;
495
511
  }
496
512
  if (AIMessageChunk.isInstance(msg)) {
497
- if (messageConcat[msgId]) {
498
- messageConcat[msgId] = messageConcat[msgId].concat(
499
- msg
513
+ const existingMessage = messageConcat.get(msgId);
514
+ if (existingMessage) {
515
+ messageConcat.set(
516
+ msgId,
517
+ existingMessage.concat(msg)
500
518
  );
501
519
  } else {
502
- messageConcat[msgId] = msg;
520
+ messageConcat.set(msgId, msg);
503
521
  }
504
522
  }
505
523
  if (isAIMessageChunk(msg)) {
506
- const concatChunk = messageConcat[msgId];
524
+ const concatChunk = messageConcat.get(msgId);
507
525
  const msgObj = msg;
508
526
  const dataSource = msgObj.type === "constructor" && msgObj.kwargs && typeof msgObj.kwargs === "object" ? msgObj.kwargs : msgObj;
509
527
  const additionalKwargs = dataSource.additional_kwargs;
@@ -524,28 +542,34 @@ function processLangGraphEvent(event, state, controller) {
524
542
  for (const toolCallChunk of toolCallChunks) {
525
543
  const idx = (_a = toolCallChunk.index) != null ? _a : 0;
526
544
  if (toolCallChunk.id) {
527
- (_b = toolCallInfoByIndex[msgId]) != null ? _b : toolCallInfoByIndex[msgId] = {};
528
- toolCallInfoByIndex[msgId][idx] = {
529
- id: toolCallChunk.id,
530
- name: toolCallChunk.name || ((_d = (_c = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _c[idx]) == null ? void 0 : _d.name) || "unknown"
531
- };
545
+ getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId).set(
546
+ idx,
547
+ {
548
+ id: toolCallChunk.id,
549
+ name: toolCallChunk.name || ((_c = (_b = concatChunk == null ? void 0 : concatChunk.tool_call_chunks) == null ? void 0 : _b[idx]) == null ? void 0 : _c.name) || "unknown"
550
+ }
551
+ );
532
552
  }
533
- 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);
553
+ const storedToolCallInfo = (_d = toolCallInfoByIndex.get(msgId)) == null ? void 0 : _d.get(idx);
554
+ 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);
534
555
  if (!toolCallId) {
535
556
  continue;
536
557
  }
537
- 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";
538
- if (!((_n = (_m = messageSeen[msgId]) == null ? void 0 : _m.tool) == null ? void 0 : _n[toolCallId])) {
539
- controller.enqueue({
540
- type: "tool-input-start",
541
- toolCallId,
542
- toolName,
543
- dynamic: true
544
- });
545
- (_o = messageSeen[msgId]) != null ? _o : messageSeen[msgId] = {};
546
- (_q = (_p = messageSeen[msgId]).tool) != null ? _q : _p.tool = {};
547
- messageSeen[msgId].tool[toolCallId] = true;
548
- emittedToolCalls.add(toolCallId);
558
+ 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";
559
+ const seen = messageSeen.get(msgId);
560
+ if (!((_i = seen == null ? void 0 : seen.tool) == null ? void 0 : _i.has(toolCallId))) {
561
+ const updatedSeen = getOrCreateMessageSeen(messageSeen, msgId);
562
+ (_j = updatedSeen.tool) != null ? _j : updatedSeen.tool = /* @__PURE__ */ new Set();
563
+ updatedSeen.tool.add(toolCallId);
564
+ if (!emittedToolCalls.has(toolCallId)) {
565
+ emittedToolCalls.add(toolCallId);
566
+ controller.enqueue({
567
+ type: "tool-input-start",
568
+ toolCallId,
569
+ toolName,
570
+ dynamic: true
571
+ });
572
+ }
549
573
  }
550
574
  if (toolCallChunk.args) {
551
575
  controller.enqueue({
@@ -559,18 +583,18 @@ function processLangGraphEvent(event, state, controller) {
559
583
  }
560
584
  const chunkReasoningId = extractReasoningId(msg);
561
585
  if (chunkReasoningId) {
562
- if (!messageReasoningIds[msgId]) {
563
- messageReasoningIds[msgId] = chunkReasoningId;
586
+ if (!messageReasoningIds.has(msgId)) {
587
+ messageReasoningIds.set(msgId, chunkReasoningId);
564
588
  }
565
589
  emittedReasoningIds.add(chunkReasoningId);
566
590
  }
567
591
  const reasoning = extractReasoningFromContentBlocks(msg);
568
592
  if (reasoning) {
569
- const reasoningId = (_s = (_r = messageReasoningIds[msgId]) != null ? _r : chunkReasoningId) != null ? _s : msgId;
570
- if (!((_t = messageSeen[msgId]) == null ? void 0 : _t.reasoning)) {
593
+ const reasoningId = (_l = (_k = messageReasoningIds.get(msgId)) != null ? _k : chunkReasoningId) != null ? _l : msgId;
594
+ const seen = messageSeen.get(msgId);
595
+ if (!(seen == null ? void 0 : seen.reasoning)) {
571
596
  controller.enqueue({ type: "reasoning-start", id: msgId });
572
- (_u = messageSeen[msgId]) != null ? _u : messageSeen[msgId] = {};
573
- messageSeen[msgId].reasoning = true;
597
+ getOrCreateMessageSeen(messageSeen, msgId).reasoning = true;
574
598
  }
575
599
  controller.enqueue({
576
600
  type: "reasoning-delta",
@@ -581,10 +605,10 @@ function processLangGraphEvent(event, state, controller) {
581
605
  }
582
606
  const text = getMessageText(msg);
583
607
  if (text) {
584
- if (!((_v = messageSeen[msgId]) == null ? void 0 : _v.text)) {
608
+ const seen = messageSeen.get(msgId);
609
+ if (!(seen == null ? void 0 : seen.text)) {
585
610
  controller.enqueue({ type: "text-start", id: msgId });
586
- (_w = messageSeen[msgId]) != null ? _w : messageSeen[msgId] = {};
587
- messageSeen[msgId].text = true;
611
+ getOrCreateMessageSeen(messageSeen, msgId).text = true;
588
612
  }
589
613
  controller.enqueue({
590
614
  type: "text-delta",
@@ -616,15 +640,15 @@ function processLangGraphEvent(event, state, controller) {
616
640
  return;
617
641
  }
618
642
  case "values": {
619
- for (const [id, seen] of Object.entries(messageSeen)) {
643
+ for (const [id, seen] of messageSeen) {
620
644
  if (seen.text) controller.enqueue({ type: "text-end", id });
621
645
  if (seen.tool) {
622
- for (const [toolCallId, toolCallSeen] of Object.entries(seen.tool)) {
623
- const concatMsg = messageConcat[id];
624
- const toolCall = (_x = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _x.find(
646
+ for (const toolCallId of seen.tool) {
647
+ const concatMsg = messageConcat.get(id);
648
+ const toolCall = (_m = concatMsg == null ? void 0 : concatMsg.tool_calls) == null ? void 0 : _m.find(
625
649
  (call) => call.id === toolCallId
626
650
  );
627
- if (toolCallSeen && toolCall) {
651
+ if (toolCall) {
628
652
  emittedToolCalls.add(toolCallId);
629
653
  const toolCallKey = `${toolCall.name}:${JSON.stringify(toolCall.args)}`;
630
654
  emittedToolCallsByKey.set(toolCallKey, toolCallId);
@@ -641,9 +665,9 @@ function processLangGraphEvent(event, state, controller) {
641
665
  if (seen.reasoning) {
642
666
  controller.enqueue({ type: "reasoning-end", id });
643
667
  }
644
- delete messageSeen[id];
645
- delete messageConcat[id];
646
- delete messageReasoningIds[id];
668
+ messageSeen.delete(id);
669
+ messageConcat.delete(id);
670
+ messageReasoningIds.delete(id);
647
671
  }
648
672
  if (data != null && typeof data === "object" && "messages" in data) {
649
673
  const messages = data.messages;
@@ -723,7 +747,7 @@ function processLangGraphEvent(event, state, controller) {
723
747
  }
724
748
  }
725
749
  const reasoningId = extractReasoningId(msg);
726
- const wasStreamedThisRequest = !!messageSeen[msgId];
750
+ const wasStreamedThisRequest = messageSeen.has(msgId);
727
751
  const hasToolCalls = toolCalls && toolCalls.length > 0;
728
752
  const shouldEmitReasoning = reasoningId && !emittedReasoningIds.has(reasoningId) && (wasStreamedThisRequest || !hasToolCalls);
729
753
  if (shouldEmitReasoning) {
@@ -938,13 +962,13 @@ function toUIMessageStream(stream, callbacks) {
938
962
  reasoningMessageId: null
939
963
  };
940
964
  const langGraphState = {
941
- messageSeen: {},
942
- messageConcat: {},
965
+ messageSeen: /* @__PURE__ */ new Map(),
966
+ messageConcat: /* @__PURE__ */ new Map(),
943
967
  emittedToolCalls: /* @__PURE__ */ new Set(),
944
968
  emittedImages: /* @__PURE__ */ new Set(),
945
969
  emittedReasoningIds: /* @__PURE__ */ new Set(),
946
- messageReasoningIds: {},
947
- toolCallInfoByIndex: {},
970
+ messageReasoningIds: /* @__PURE__ */ new Map(),
971
+ toolCallInfoByIndex: /* @__PURE__ */ new Map(),
948
972
  currentStep: null,
949
973
  emittedToolCallsByKey: /* @__PURE__ */ new Map()
950
974
  };
@@ -1039,7 +1063,7 @@ function toUIMessageStream(stream, callbacks) {
1039
1063
  }
1040
1064
  controller.enqueue({ type: "finish" });
1041
1065
  } else if (streamType === "langgraph") {
1042
- for (const [id, seen] of Object.entries(langGraphState.messageSeen)) {
1066
+ for (const [id, seen] of langGraphState.messageSeen) {
1043
1067
  if (seen.text) {
1044
1068
  controller.enqueue({ type: "text-end", id });
1045
1069
  }