@ai-sdk/langchain 2.0.255 → 2.0.257
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 +15 -0
- package/dist/index.js +137 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +137 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/adapter.ts +17 -3
- package/src/types.ts +4 -0
- package/src/utils.ts +177 -22
package/dist/index.mjs
CHANGED
|
@@ -14,7 +14,77 @@ import {
|
|
|
14
14
|
AIMessageChunk
|
|
15
15
|
} from "@langchain/core/messages";
|
|
16
16
|
function parseLangGraphEvent(event) {
|
|
17
|
-
return event.length === 3 ? [event[1], event[2]] : [event[0], event[1]];
|
|
17
|
+
return event.length === 3 ? [event[0], event[1], event[2]] : [void 0, event[0], event[1]];
|
|
18
|
+
}
|
|
19
|
+
function getLangGraphProviderMetadata(namespace) {
|
|
20
|
+
return namespace === void 0 ? void 0 : {
|
|
21
|
+
langchain: {
|
|
22
|
+
namespace
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function addLangGraphNamespace(chunk, namespace) {
|
|
27
|
+
var _a;
|
|
28
|
+
if (namespace === void 0) {
|
|
29
|
+
return chunk;
|
|
30
|
+
}
|
|
31
|
+
switch (chunk.type) {
|
|
32
|
+
case "text-start":
|
|
33
|
+
case "text-delta":
|
|
34
|
+
case "text-end":
|
|
35
|
+
case "reasoning-start":
|
|
36
|
+
case "reasoning-delta":
|
|
37
|
+
case "reasoning-end":
|
|
38
|
+
case "tool-input-start":
|
|
39
|
+
case "tool-input-available":
|
|
40
|
+
case "tool-input-error":
|
|
41
|
+
case "tool-output-available":
|
|
42
|
+
case "tool-output-error":
|
|
43
|
+
case "source-url":
|
|
44
|
+
case "source-document":
|
|
45
|
+
case "file": {
|
|
46
|
+
return {
|
|
47
|
+
...chunk,
|
|
48
|
+
providerMetadata: {
|
|
49
|
+
...chunk.providerMetadata,
|
|
50
|
+
langchain: {
|
|
51
|
+
...(_a = chunk.providerMetadata) == null ? void 0 : _a.langchain,
|
|
52
|
+
namespace
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
return chunk;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function createLangGraphNamespaceController(controller, state, eventNamespace) {
|
|
62
|
+
return {
|
|
63
|
+
get desiredSize() {
|
|
64
|
+
return controller.desiredSize;
|
|
65
|
+
},
|
|
66
|
+
close: () => controller.close(),
|
|
67
|
+
error: (reason) => controller.error(reason),
|
|
68
|
+
enqueue: (chunk) => {
|
|
69
|
+
if (chunk === void 0) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let messageNamespace;
|
|
73
|
+
switch (chunk.type) {
|
|
74
|
+
case "text-start":
|
|
75
|
+
case "text-delta":
|
|
76
|
+
case "text-end":
|
|
77
|
+
case "reasoning-start":
|
|
78
|
+
case "reasoning-delta":
|
|
79
|
+
case "reasoning-end":
|
|
80
|
+
messageNamespace = state.messageNamespaces.get(chunk.id);
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
controller.enqueue(
|
|
84
|
+
addLangGraphNamespace(chunk, messageNamespace != null ? messageNamespace : eventNamespace)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
18
88
|
}
|
|
19
89
|
function convertToolResultPart(block) {
|
|
20
90
|
const content = (() => {
|
|
@@ -522,6 +592,34 @@ function getOrCreateToolCallInfoByIndex(toolCallInfoByIndex, msgId) {
|
|
|
522
592
|
}
|
|
523
593
|
return toolCallInfo;
|
|
524
594
|
}
|
|
595
|
+
function getLangGraphNamespaceKey(namespace) {
|
|
596
|
+
return JSON.stringify(namespace != null ? namespace : []);
|
|
597
|
+
}
|
|
598
|
+
function closeStepNamespaceMessages(state, stepNamespace, controller) {
|
|
599
|
+
let hasConcurrentMessageParts = false;
|
|
600
|
+
for (const [id, seen] of state.messageSeen) {
|
|
601
|
+
const messageNamespace = getLangGraphNamespaceKey(
|
|
602
|
+
state.messageNamespaces.get(id)
|
|
603
|
+
);
|
|
604
|
+
if (messageNamespace !== stepNamespace) {
|
|
605
|
+
if (seen.text || seen.reasoning) {
|
|
606
|
+
hasConcurrentMessageParts = true;
|
|
607
|
+
}
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
if (seen.text) {
|
|
611
|
+
controller.enqueue({ type: "text-end", id });
|
|
612
|
+
}
|
|
613
|
+
if (seen.reasoning) {
|
|
614
|
+
controller.enqueue({ type: "reasoning-end", id });
|
|
615
|
+
}
|
|
616
|
+
state.messageSeen.delete(id);
|
|
617
|
+
state.messageNamespaces.delete(id);
|
|
618
|
+
state.messageConcat.delete(id);
|
|
619
|
+
state.messageReasoningIds.delete(id);
|
|
620
|
+
}
|
|
621
|
+
return hasConcurrentMessageParts;
|
|
622
|
+
}
|
|
525
623
|
function processLangGraphEvent(event, state, controller) {
|
|
526
624
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
|
|
527
625
|
const {
|
|
@@ -534,7 +632,9 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
534
632
|
toolCallInfoByIndex,
|
|
535
633
|
emittedToolCallsByKey
|
|
536
634
|
} = state;
|
|
537
|
-
const [type, data] = parseLangGraphEvent(event);
|
|
635
|
+
const [rawNamespace, type, data] = parseLangGraphEvent(event);
|
|
636
|
+
const namespace = Array.isArray(rawNamespace) && rawNamespace.every((segment) => typeof segment === "string") ? rawNamespace : void 0;
|
|
637
|
+
controller = createLangGraphNamespaceController(controller, state, namespace);
|
|
538
638
|
switch (type) {
|
|
539
639
|
case "custom": {
|
|
540
640
|
let customTypeName = "custom";
|
|
@@ -561,23 +661,28 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
561
661
|
const msg = rawMsg;
|
|
562
662
|
const msgId = getMessageId(msg);
|
|
563
663
|
if (!msgId) return;
|
|
664
|
+
if (namespace !== void 0) {
|
|
665
|
+
state.messageNamespaces.set(msgId, namespace);
|
|
666
|
+
}
|
|
564
667
|
const langgraphStep = typeof (metadata == null ? void 0 : metadata.langgraph_step) === "number" ? metadata.langgraph_step : null;
|
|
565
|
-
|
|
668
|
+
const eventNamespace = getLangGraphNamespaceKey(namespace);
|
|
669
|
+
if (langgraphStep !== null && state.stepNamespace === null) {
|
|
670
|
+
state.stepNamespace = eventNamespace;
|
|
671
|
+
}
|
|
672
|
+
if (langgraphStep !== null && eventNamespace === state.stepNamespace && langgraphStep !== state.currentStep) {
|
|
566
673
|
if (state.currentStep !== null) {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
messageConcat.delete(id);
|
|
576
|
-
messageReasoningIds.delete(id);
|
|
674
|
+
const hasConcurrentMessageParts = closeStepNamespaceMessages(
|
|
675
|
+
state,
|
|
676
|
+
state.stepNamespace,
|
|
677
|
+
controller
|
|
678
|
+
);
|
|
679
|
+
if (!hasConcurrentMessageParts) {
|
|
680
|
+
controller.enqueue({ type: "finish-step" });
|
|
681
|
+
controller.enqueue({ type: "start-step" });
|
|
577
682
|
}
|
|
578
|
-
|
|
683
|
+
} else {
|
|
684
|
+
controller.enqueue({ type: "start-step" });
|
|
579
685
|
}
|
|
580
|
-
controller.enqueue({ type: "start-step" });
|
|
581
686
|
state.currentStep = langgraphStep;
|
|
582
687
|
}
|
|
583
688
|
if (AIMessageChunk.isInstance(msg)) {
|
|
@@ -750,6 +855,7 @@ function processLangGraphEvent(event, state, controller) {
|
|
|
750
855
|
controller.enqueue({ type: "reasoning-end", id });
|
|
751
856
|
}
|
|
752
857
|
messageSeen.delete(id);
|
|
858
|
+
state.messageNamespaces.delete(id);
|
|
753
859
|
messageConcat.delete(id);
|
|
754
860
|
messageReasoningIds.delete(id);
|
|
755
861
|
}
|
|
@@ -1085,6 +1191,7 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1085
1191
|
};
|
|
1086
1192
|
const langGraphState = {
|
|
1087
1193
|
messageSeen: /* @__PURE__ */ new Map(),
|
|
1194
|
+
messageNamespaces: /* @__PURE__ */ new Map(),
|
|
1088
1195
|
messageConcat: /* @__PURE__ */ new Map(),
|
|
1089
1196
|
emittedToolCalls: /* @__PURE__ */ new Set(),
|
|
1090
1197
|
emittedImages: /* @__PURE__ */ new Set(),
|
|
@@ -1092,6 +1199,7 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1092
1199
|
messageReasoningIds: /* @__PURE__ */ new Map(),
|
|
1093
1200
|
toolCallInfoByIndex: /* @__PURE__ */ new Map(),
|
|
1094
1201
|
currentStep: null,
|
|
1202
|
+
stepNamespace: null,
|
|
1095
1203
|
emittedToolCallsByKey: /* @__PURE__ */ new Map(),
|
|
1096
1204
|
emittedSourceIds: /* @__PURE__ */ new Set()
|
|
1097
1205
|
};
|
|
@@ -1160,7 +1268,7 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1160
1268
|
);
|
|
1161
1269
|
} else {
|
|
1162
1270
|
const eventArray = value;
|
|
1163
|
-
const [type, data] = parseLangGraphEvent(eventArray);
|
|
1271
|
+
const [, type, data] = parseLangGraphEvent(eventArray);
|
|
1164
1272
|
if (type === "values") {
|
|
1165
1273
|
lastValuesData = data;
|
|
1166
1274
|
}
|
|
@@ -1187,11 +1295,22 @@ function toUIMessageStream(stream, callbacks) {
|
|
|
1187
1295
|
controller.enqueue({ type: "finish" });
|
|
1188
1296
|
} else if (streamType === "langgraph") {
|
|
1189
1297
|
for (const [id, seen] of langGraphState.messageSeen) {
|
|
1298
|
+
const providerMetadata = getLangGraphProviderMetadata(
|
|
1299
|
+
langGraphState.messageNamespaces.get(id)
|
|
1300
|
+
);
|
|
1190
1301
|
if (seen.text) {
|
|
1191
|
-
controller.enqueue({
|
|
1302
|
+
controller.enqueue({
|
|
1303
|
+
type: "text-end",
|
|
1304
|
+
id,
|
|
1305
|
+
...providerMetadata !== void 0 && { providerMetadata }
|
|
1306
|
+
});
|
|
1192
1307
|
}
|
|
1193
1308
|
if (seen.reasoning) {
|
|
1194
|
-
controller.enqueue({
|
|
1309
|
+
controller.enqueue({
|
|
1310
|
+
type: "reasoning-end",
|
|
1311
|
+
id,
|
|
1312
|
+
...providerMetadata !== void 0 && { providerMetadata }
|
|
1313
|
+
});
|
|
1195
1314
|
}
|
|
1196
1315
|
}
|
|
1197
1316
|
if (langGraphState.currentStep !== null) {
|