@copilotkit/runtime 1.3.16-mme-reset-chat.2 → 1.4.0-feat-langgraph-cloud.0
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 +4 -17
- package/__snapshots__/schema/schema.graphql +1 -0
- package/dist/{chunk-DX45O7PM.mjs → chunk-AEUECXAW.mjs} +675 -151
- package/dist/chunk-AEUECXAW.mjs.map +1 -0
- package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
- package/dist/chunk-B74M7FXG.mjs.map +1 -0
- package/dist/{chunk-KFO5XSPV.mjs → chunk-BLB2XDIL.mjs} +2 -2
- package/dist/{chunk-STQIVH2M.mjs → chunk-DP7YGMSZ.mjs} +37 -10
- package/dist/chunk-DP7YGMSZ.mjs.map +1 -0
- package/dist/{chunk-7LIN2NIO.mjs → chunk-ITCPKBTK.mjs} +2 -2
- package/dist/{chunk-SMVZ2NQP.mjs → chunk-VGYN7TGZ.mjs} +19 -29
- package/dist/{chunk-SMVZ2NQP.mjs.map → chunk-VGYN7TGZ.mjs.map} +1 -1
- package/dist/{chunk-AXHCROTL.mjs → chunk-WQFBGT3B.mjs} +2 -2
- package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-543a59ae.d.ts} +29 -7
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -1
- package/dist/{groq-adapter-b122e71f.d.ts → groq-adapter-7aa25931.d.ts} +4 -2
- package/dist/{index-cff31380.d.ts → index-83ee522f.d.ts} +3 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +765 -224
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -9
- package/dist/index.mjs.map +1 -1
- package/dist/{langserve-a14a6849.d.ts → langserve-f00629d2.d.ts} +1 -1
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +765 -224
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +13 -9
- package/dist/lib/integrations/index.d.ts +4 -4
- package/dist/lib/integrations/index.js +4 -1
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +5 -5
- package/dist/lib/integrations/nest/index.d.ts +3 -3
- package/dist/lib/integrations/nest/index.js +4 -1
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +3 -3
- package/dist/lib/integrations/node-express/index.d.ts +3 -3
- package/dist/lib/integrations/node-express/index.js +4 -1
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +3 -3
- package/dist/lib/integrations/node-http/index.d.ts +3 -3
- package/dist/lib/integrations/node-http/index.js +4 -1
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +2 -2
- package/dist/service-adapters/index.d.ts +4 -4
- package/dist/service-adapters/index.js +18 -28
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +1 -1
- package/package.json +6 -4
- package/src/agents/langgraph/event-source.ts +62 -94
- package/src/agents/langgraph/events.ts +27 -17
- package/src/graphql/types/converted/index.ts +5 -1
- package/src/graphql/types/enums.ts +1 -0
- package/src/lib/runtime/copilot-runtime.ts +58 -11
- package/src/lib/runtime/remote-action-constructors.ts +284 -0
- package/src/lib/runtime/remote-actions.ts +65 -159
- package/src/lib/runtime/remote-lg-cloud-action.ts +613 -0
- package/src/service-adapters/google/google-genai-adapter.ts +2 -2
- package/src/service-adapters/langchain/langchain-adapter.ts +19 -23
- package/dist/chunk-DX45O7PM.mjs.map +0 -1
- package/dist/chunk-MYZHUCL6.mjs.map +0 -1
- package/dist/chunk-STQIVH2M.mjs.map +0 -1
- /package/dist/{chunk-KFO5XSPV.mjs.map → chunk-BLB2XDIL.mjs.map} +0 -0
- /package/dist/{chunk-7LIN2NIO.mjs.map → chunk-ITCPKBTK.mjs.map} +0 -0
- /package/dist/{chunk-AXHCROTL.mjs.map → chunk-WQFBGT3B.mjs.map} +0 -0
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
AgentStateMessage,
|
|
14
14
|
ResultMessage,
|
|
15
15
|
TextMessage
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-B74M7FXG.mjs";
|
|
17
17
|
import {
|
|
18
18
|
BaseMessageInput
|
|
19
19
|
} from "./chunk-D2WLFQS6.mjs";
|
|
@@ -36,7 +36,7 @@ var require_package = __commonJS({
|
|
|
36
36
|
publishConfig: {
|
|
37
37
|
access: "public"
|
|
38
38
|
},
|
|
39
|
-
version: "1.
|
|
39
|
+
version: "1.4.0-feat-langgraph-cloud.0",
|
|
40
40
|
sideEffects: false,
|
|
41
41
|
main: "./dist/index.js",
|
|
42
42
|
module: "./dist/index.mjs",
|
|
@@ -79,6 +79,7 @@ var require_package = __commonJS({
|
|
|
79
79
|
"@langchain/community": "^0.0.53",
|
|
80
80
|
"@langchain/core": "^0.3.13",
|
|
81
81
|
"@langchain/google-gauth": "^0.1.0",
|
|
82
|
+
"@langchain/langgraph-sdk": "^0.0.16",
|
|
82
83
|
"@langchain/openai": "^0.0.28",
|
|
83
84
|
"class-transformer": "^0.5.1",
|
|
84
85
|
express: "^4.19.2",
|
|
@@ -88,6 +89,7 @@ var require_package = __commonJS({
|
|
|
88
89
|
"groq-sdk": "^0.5.0",
|
|
89
90
|
langchain: "^0.3.3",
|
|
90
91
|
openai: "^4.50.0",
|
|
92
|
+
"partial-json": "^0.1.7",
|
|
91
93
|
pino: "^9.2.0",
|
|
92
94
|
"pino-pretty": "^11.2.1",
|
|
93
95
|
"reflect-metadata": "^0.2.2",
|
|
@@ -134,6 +136,7 @@ var MessageRole;
|
|
|
134
136
|
MessageRole2["user"] = "user";
|
|
135
137
|
MessageRole2["assistant"] = "assistant";
|
|
136
138
|
MessageRole2["system"] = "system";
|
|
139
|
+
MessageRole2["tool"] = "tool";
|
|
137
140
|
})(MessageRole || (MessageRole = {}));
|
|
138
141
|
var ActionExecutionScope;
|
|
139
142
|
(function(ActionExecutionScope2) {
|
|
@@ -1046,58 +1049,20 @@ var LangGraphEventTypes;
|
|
|
1046
1049
|
LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
|
|
1047
1050
|
LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
|
|
1048
1051
|
LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
|
|
1049
|
-
LangGraphEventTypes2["
|
|
1050
|
-
LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
|
|
1052
|
+
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
|
|
1051
1053
|
})(LangGraphEventTypes || (LangGraphEventTypes = {}));
|
|
1054
|
+
var CustomEventNames;
|
|
1055
|
+
(function(CustomEventNames2) {
|
|
1056
|
+
CustomEventNames2["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
|
|
1057
|
+
CustomEventNames2["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
|
|
1058
|
+
CustomEventNames2["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
|
|
1059
|
+
CustomEventNames2["CopilotKitExit"] = "copilotkit_exit";
|
|
1060
|
+
})(CustomEventNames || (CustomEventNames = {}));
|
|
1052
1061
|
|
|
1053
1062
|
// src/agents/langgraph/event-source.ts
|
|
1054
1063
|
import { randomId } from "@copilotkit/shared";
|
|
1055
1064
|
var RemoteLangGraphEventSource = class {
|
|
1056
1065
|
eventStream$ = new ReplaySubject();
|
|
1057
|
-
async streamResponse(response) {
|
|
1058
|
-
const reader = response.body.getReader();
|
|
1059
|
-
const decoder = new TextDecoder();
|
|
1060
|
-
let buffer = [];
|
|
1061
|
-
const eventStream$ = this.eventStream$;
|
|
1062
|
-
function flushBuffer() {
|
|
1063
|
-
const currentBuffer = buffer.join("");
|
|
1064
|
-
if (currentBuffer.trim().length === 0) {
|
|
1065
|
-
return;
|
|
1066
|
-
}
|
|
1067
|
-
const parts = currentBuffer.split("\n");
|
|
1068
|
-
if (parts.length === 0) {
|
|
1069
|
-
return;
|
|
1070
|
-
}
|
|
1071
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1072
|
-
buffer = [];
|
|
1073
|
-
if (!lastPartIsComplete) {
|
|
1074
|
-
buffer.push(parts.pop());
|
|
1075
|
-
}
|
|
1076
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1077
|
-
eventStream$.next(JSON.parse(part));
|
|
1078
|
-
});
|
|
1079
|
-
}
|
|
1080
|
-
__name(flushBuffer, "flushBuffer");
|
|
1081
|
-
try {
|
|
1082
|
-
while (true) {
|
|
1083
|
-
const { done, value } = await reader.read();
|
|
1084
|
-
if (!done) {
|
|
1085
|
-
buffer.push(decoder.decode(value, {
|
|
1086
|
-
stream: true
|
|
1087
|
-
}));
|
|
1088
|
-
}
|
|
1089
|
-
flushBuffer();
|
|
1090
|
-
if (done) {
|
|
1091
|
-
break;
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
} catch (error) {
|
|
1095
|
-
console.error("Error in stream", error);
|
|
1096
|
-
eventStream$.error(error);
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
eventStream$.complete();
|
|
1100
|
-
}
|
|
1101
1066
|
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
1102
1067
|
if (typeof shouldEmitToolCalls === "boolean") {
|
|
1103
1068
|
return shouldEmitToolCalls;
|
|
@@ -1120,20 +1085,25 @@ var RemoteLangGraphEventSource = class {
|
|
|
1120
1085
|
} else {
|
|
1121
1086
|
acc.content = null;
|
|
1122
1087
|
}
|
|
1123
|
-
|
|
1088
|
+
const toolCallChunks = (
|
|
1089
|
+
// @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
|
|
1090
|
+
((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) ?? ((_j = (_i = event.data) == null ? void 0 : _i.chunk) == null ? void 0 : _j.tool_call_chunks)
|
|
1091
|
+
);
|
|
1092
|
+
const toolCallMessageId = ((_m = (_l = (_k = event.data) == null ? void 0 : _k.chunk) == null ? void 0 : _l.kwargs) == null ? void 0 : _m.id) ?? ((_o = (_n = event.data) == null ? void 0 : _n.chunk) == null ? void 0 : _o.id);
|
|
1093
|
+
if (toolCallChunks && toolCallChunks.length > 0) {
|
|
1124
1094
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1125
|
-
acc.toolCallMessageId =
|
|
1126
|
-
if ((
|
|
1127
|
-
acc.toolCallName =
|
|
1095
|
+
acc.toolCallMessageId = toolCallMessageId;
|
|
1096
|
+
if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
|
|
1097
|
+
acc.toolCallName = toolCallChunks[0].name;
|
|
1128
1098
|
}
|
|
1129
|
-
if ((
|
|
1130
|
-
acc.toolCallId =
|
|
1099
|
+
if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
|
|
1100
|
+
acc.toolCallId = toolCallChunks[0].id;
|
|
1131
1101
|
}
|
|
1132
1102
|
acc.prevMessageId = acc.messageId;
|
|
1133
|
-
acc.messageId =
|
|
1103
|
+
acc.messageId = toolCallMessageId;
|
|
1134
1104
|
} else if (acc.content && acc.content != "") {
|
|
1135
1105
|
acc.prevMessageId = acc.messageId;
|
|
1136
|
-
acc.messageId =
|
|
1106
|
+
acc.messageId = toolCallMessageId;
|
|
1137
1107
|
} else {
|
|
1138
1108
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1139
1109
|
acc.prevMessageId = acc.messageId;
|
|
@@ -1158,7 +1128,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1158
1128
|
prevMessageId: null,
|
|
1159
1129
|
content: null
|
|
1160
1130
|
}), mergeMap((eventWithState) => {
|
|
1161
|
-
var _a, _b, _c, _d, _e;
|
|
1131
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1162
1132
|
const events = [];
|
|
1163
1133
|
let shouldEmitMessages = true;
|
|
1164
1134
|
let shouldEmitToolCalls = false;
|
|
@@ -1181,32 +1151,33 @@ var RemoteLangGraphEventSource = class {
|
|
|
1181
1151
|
});
|
|
1182
1152
|
}
|
|
1183
1153
|
switch (eventWithState.event.event) {
|
|
1184
|
-
case LangGraphEventTypes.
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1154
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
1155
|
+
if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
1156
|
+
events.push({
|
|
1157
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
1158
|
+
messageId: eventWithState.event.data.message_id
|
|
1159
|
+
});
|
|
1160
|
+
events.push({
|
|
1161
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
1162
|
+
content: eventWithState.event.data.message
|
|
1163
|
+
});
|
|
1164
|
+
events.push({
|
|
1165
|
+
type: RuntimeEventTypes.TextMessageEnd
|
|
1166
|
+
});
|
|
1167
|
+
} else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
1168
|
+
events.push({
|
|
1169
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1170
|
+
actionExecutionId: eventWithState.event.data.id,
|
|
1171
|
+
actionName: eventWithState.event.data.name
|
|
1172
|
+
});
|
|
1173
|
+
events.push({
|
|
1174
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1175
|
+
args: JSON.stringify(eventWithState.event.data.args)
|
|
1176
|
+
});
|
|
1177
|
+
events.push({
|
|
1178
|
+
type: RuntimeEventTypes.ActionExecutionEnd
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1210
1181
|
break;
|
|
1211
1182
|
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
1212
1183
|
events.push({
|
|
@@ -1241,7 +1212,8 @@ var RemoteLangGraphEventSource = class {
|
|
|
1241
1212
|
});
|
|
1242
1213
|
}
|
|
1243
1214
|
}
|
|
1244
|
-
const args = (_e = (_d = (_c = (_b = (_a = eventWithState.event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) == null ? void 0 : _d[0]) == null ? void 0 : _e.args
|
|
1215
|
+
const args = ((_e = (_d = (_c = (_b = (_a = eventWithState.event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) == null ? void 0 : _d[0]) == null ? void 0 : _e.args) ?? // @ts-expect-error -- sdf
|
|
1216
|
+
((_i = (_h = (_g = (_f = eventWithState.event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.tool_call_chunks) == null ? void 0 : _h[0]) == null ? void 0 : _i.args);
|
|
1245
1217
|
const content = eventWithState.content;
|
|
1246
1218
|
if (args) {
|
|
1247
1219
|
if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
|
|
@@ -1292,70 +1264,490 @@ var RemoteLangGraphEventSource = class {
|
|
|
1292
1264
|
};
|
|
1293
1265
|
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
1294
1266
|
|
|
1295
|
-
// src/lib/runtime/remote-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
return
|
|
1267
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1268
|
+
import { Client } from "@langchain/langgraph-sdk";
|
|
1269
|
+
import { randomUUID } from "crypto";
|
|
1270
|
+
import { parse as parsePartialJson } from "partial-json";
|
|
1271
|
+
async function execute(args) {
|
|
1272
|
+
return new ReadableStream({
|
|
1273
|
+
async start(controller) {
|
|
1274
|
+
try {
|
|
1275
|
+
await streamEvents(controller, args);
|
|
1276
|
+
controller.close();
|
|
1277
|
+
} catch (err) {
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
});
|
|
1301
1281
|
}
|
|
1302
|
-
__name(
|
|
1303
|
-
function
|
|
1304
|
-
const
|
|
1305
|
-
|
|
1282
|
+
__name(execute, "execute");
|
|
1283
|
+
async function streamEvents(controller, args) {
|
|
1284
|
+
const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
|
|
1285
|
+
let nodeName = initialNodeName;
|
|
1286
|
+
let state = initialState;
|
|
1287
|
+
const { name, assistantId: initialAssistantId } = agent;
|
|
1288
|
+
const client = new Client({
|
|
1289
|
+
apiUrl: deploymentUrl,
|
|
1290
|
+
apiKey: langsmithApiKey
|
|
1291
|
+
});
|
|
1292
|
+
let initialThreadId = agrsInitialThreadId;
|
|
1293
|
+
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
1294
|
+
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
1295
|
+
initialThreadId = initialThreadId.substring(3);
|
|
1296
|
+
}
|
|
1297
|
+
const assistants = await client.assistants.search();
|
|
1298
|
+
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
1299
|
+
const threadId = initialThreadId ?? randomUUID();
|
|
1300
|
+
if (initialThreadId === threadId) {
|
|
1301
|
+
await client.threads.get(threadId);
|
|
1302
|
+
} else {
|
|
1303
|
+
await client.threads.create({
|
|
1304
|
+
threadId
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
let agentState = {
|
|
1308
|
+
values: {}
|
|
1306
1309
|
};
|
|
1307
|
-
if (
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
+
if (wasInitiatedWithExistingThread) {
|
|
1311
|
+
agentState = await client.threads.getState(threadId);
|
|
1312
|
+
}
|
|
1313
|
+
const agentStateValues = agentState.values;
|
|
1314
|
+
state.messages = agentStateValues.messages;
|
|
1315
|
+
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
1316
|
+
let formattedMessages = [];
|
|
1317
|
+
try {
|
|
1318
|
+
formattedMessages = formatMessages(messages);
|
|
1319
|
+
} catch (e) {
|
|
1320
|
+
logger2.error(e, `Error event thrown: ${e.message}`);
|
|
1321
|
+
}
|
|
1322
|
+
state = langGraphDefaultMergeState(state, formattedMessages, actions, name);
|
|
1323
|
+
if (mode === "continue") {
|
|
1324
|
+
await client.threads.updateState(threadId, {
|
|
1325
|
+
values: state,
|
|
1326
|
+
asNode: nodeName
|
|
1310
1327
|
});
|
|
1311
|
-
|
|
1312
|
-
|
|
1328
|
+
}
|
|
1329
|
+
const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
|
|
1330
|
+
if (!assistantId) {
|
|
1331
|
+
console.error(`
|
|
1332
|
+
No agent found for the agent name specified in CopilotKit provider
|
|
1333
|
+
Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
|
|
1337
|
+
`);
|
|
1338
|
+
throw new Error("No agent id found");
|
|
1339
|
+
}
|
|
1340
|
+
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
1341
|
+
const streamInput = mode === "start" ? state : null;
|
|
1342
|
+
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
1343
|
+
let prevNodeName = null;
|
|
1344
|
+
let emitIntermediateStateUntilEnd = null;
|
|
1345
|
+
let shouldExit = null;
|
|
1346
|
+
let externalRunId = null;
|
|
1347
|
+
const streamResponse2 = client.runs.stream(threadId, assistantId, {
|
|
1348
|
+
input: streamInput,
|
|
1349
|
+
streamMode: [
|
|
1350
|
+
"events",
|
|
1351
|
+
"values"
|
|
1352
|
+
]
|
|
1353
|
+
});
|
|
1354
|
+
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
1355
|
+
let latestStateValues = {};
|
|
1356
|
+
try {
|
|
1357
|
+
for await (const chunk of streamResponse2) {
|
|
1358
|
+
if (![
|
|
1359
|
+
"events",
|
|
1360
|
+
"values",
|
|
1361
|
+
"error"
|
|
1362
|
+
].includes(chunk.event))
|
|
1363
|
+
continue;
|
|
1364
|
+
if (chunk.event === "error") {
|
|
1365
|
+
logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
|
|
1366
|
+
throw new Error(`Error event thrown: ${chunk.data.message}`);
|
|
1367
|
+
}
|
|
1368
|
+
if (chunk.event === "values") {
|
|
1369
|
+
latestStateValues = chunk.data;
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
const event = chunk.data;
|
|
1373
|
+
const currentNodeName = event.name;
|
|
1374
|
+
const eventType = event.event;
|
|
1375
|
+
const runId = event.metadata.run_id;
|
|
1376
|
+
externalRunId = runId;
|
|
1377
|
+
const metadata = event.metadata;
|
|
1378
|
+
shouldExit = shouldExit != null ? shouldExit : eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitExit;
|
|
1379
|
+
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
1380
|
+
const manuallyEmitIntermediateState = eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState;
|
|
1381
|
+
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
1382
|
+
nodeName = currentNodeName;
|
|
1383
|
+
}
|
|
1384
|
+
if (!nodeName) {
|
|
1385
|
+
continue;
|
|
1386
|
+
}
|
|
1387
|
+
if (manuallyEmitIntermediateState) {
|
|
1388
|
+
if (eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1389
|
+
state = event.data.output;
|
|
1390
|
+
emit(getStateSyncEvent({
|
|
1391
|
+
threadId,
|
|
1392
|
+
runId,
|
|
1393
|
+
agentName: agent.name,
|
|
1394
|
+
nodeName,
|
|
1395
|
+
state: event.data.output,
|
|
1396
|
+
running: true,
|
|
1397
|
+
active: true
|
|
1398
|
+
}));
|
|
1399
|
+
}
|
|
1400
|
+
continue;
|
|
1401
|
+
}
|
|
1402
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1403
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1404
|
+
}
|
|
1405
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
|
|
1406
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1407
|
+
}
|
|
1408
|
+
let updatedState = latestStateValues;
|
|
1409
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
|
|
1410
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1411
|
+
}
|
|
1412
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1413
|
+
updatedState = {
|
|
1414
|
+
...updatedState,
|
|
1415
|
+
...streamingStateExtractor.extractState()
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1419
|
+
emitIntermediateStateUntilEnd = null;
|
|
1420
|
+
}
|
|
1421
|
+
const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
|
|
1422
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1423
|
+
state = updatedState;
|
|
1424
|
+
prevNodeName = nodeName;
|
|
1425
|
+
emit(getStateSyncEvent({
|
|
1426
|
+
threadId,
|
|
1427
|
+
runId,
|
|
1428
|
+
agentName: agent.name,
|
|
1429
|
+
nodeName,
|
|
1430
|
+
state,
|
|
1431
|
+
running: true,
|
|
1432
|
+
active: !exitingNode
|
|
1433
|
+
}));
|
|
1434
|
+
}
|
|
1435
|
+
emit(JSON.stringify(event) + "\n");
|
|
1313
1436
|
}
|
|
1437
|
+
state = await client.threads.getState(threadId);
|
|
1438
|
+
const isEndNode = state.next.length === 0;
|
|
1439
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1440
|
+
emit(getStateSyncEvent({
|
|
1441
|
+
threadId,
|
|
1442
|
+
runId: externalRunId,
|
|
1443
|
+
agentName: agent.name,
|
|
1444
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1445
|
+
state: state.values,
|
|
1446
|
+
running: !shouldExit,
|
|
1447
|
+
active: false
|
|
1448
|
+
}));
|
|
1449
|
+
return Promise.resolve();
|
|
1450
|
+
} catch (e) {
|
|
1451
|
+
return Promise.resolve();
|
|
1314
1452
|
}
|
|
1315
|
-
return headers;
|
|
1316
1453
|
}
|
|
1317
|
-
__name(
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1454
|
+
__name(streamEvents, "streamEvents");
|
|
1455
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1456
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1457
|
+
if (key !== "messages") {
|
|
1458
|
+
acc[key] = state[key];
|
|
1459
|
+
}
|
|
1460
|
+
return acc;
|
|
1461
|
+
}, {});
|
|
1462
|
+
return JSON.stringify({
|
|
1463
|
+
event: LangGraphEventTypes.OnCopilotKitStateSync,
|
|
1464
|
+
thread_id: threadId,
|
|
1465
|
+
run_id: runId,
|
|
1466
|
+
agent_name: agentName,
|
|
1467
|
+
node_name: nodeName,
|
|
1468
|
+
active,
|
|
1469
|
+
state: stateWithoutMessages,
|
|
1470
|
+
running,
|
|
1471
|
+
role: "assistant"
|
|
1472
|
+
}) + "\n";
|
|
1473
|
+
}
|
|
1474
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1475
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1476
|
+
emitIntermediateState;
|
|
1477
|
+
toolCallBuffer;
|
|
1478
|
+
currentToolCall;
|
|
1479
|
+
previouslyParsableState;
|
|
1480
|
+
constructor(emitIntermediateState) {
|
|
1481
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1482
|
+
this.toolCallBuffer = {};
|
|
1483
|
+
this.currentToolCall = null;
|
|
1484
|
+
this.previouslyParsableState = {};
|
|
1485
|
+
}
|
|
1486
|
+
bufferToolCalls(event) {
|
|
1487
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1488
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1489
|
+
if (chunk.name !== null) {
|
|
1490
|
+
this.currentToolCall = chunk.name;
|
|
1491
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1492
|
+
} else if (this.currentToolCall !== null) {
|
|
1493
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
getEmitStateConfig(currentToolName) {
|
|
1498
|
+
for (const config of this.emitIntermediateState) {
|
|
1499
|
+
const stateKey = config["state_key"];
|
|
1500
|
+
const tool = config["tool"];
|
|
1501
|
+
const toolArgument = config["tool_argument"];
|
|
1502
|
+
if (currentToolName === tool) {
|
|
1503
|
+
return [
|
|
1504
|
+
toolArgument,
|
|
1505
|
+
stateKey
|
|
1506
|
+
];
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return [
|
|
1510
|
+
null,
|
|
1511
|
+
null
|
|
1512
|
+
];
|
|
1513
|
+
}
|
|
1514
|
+
extractState() {
|
|
1515
|
+
const state = {};
|
|
1516
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1517
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1518
|
+
if (stateKey === null) {
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
let parsedValue;
|
|
1522
|
+
try {
|
|
1523
|
+
parsedValue = parsePartialJson(value);
|
|
1524
|
+
} catch (error) {
|
|
1525
|
+
if (key in this.previouslyParsableState) {
|
|
1526
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1527
|
+
} else {
|
|
1528
|
+
continue;
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1532
|
+
if (!argumentName) {
|
|
1533
|
+
state[stateKey] = parsedValue;
|
|
1534
|
+
} else {
|
|
1535
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
return state;
|
|
1539
|
+
}
|
|
1540
|
+
}, "StreamingStateExtractor");
|
|
1541
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1542
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1543
|
+
messages = messages.slice(1);
|
|
1544
|
+
}
|
|
1545
|
+
const mergedMessages = state.messages || [];
|
|
1546
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1547
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1548
|
+
for (const message of mergedMessages) {
|
|
1549
|
+
if ("tool_call_id" in message) {
|
|
1550
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
for (const message of messages) {
|
|
1554
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1555
|
+
continue;
|
|
1556
|
+
}
|
|
1557
|
+
if ("name" in message && message.name === agentName) {
|
|
1558
|
+
continue;
|
|
1559
|
+
}
|
|
1560
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1561
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1562
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1563
|
+
continue;
|
|
1564
|
+
}
|
|
1565
|
+
mergedMessages.push(message);
|
|
1566
|
+
} else {
|
|
1567
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1568
|
+
if (mergedMessages[i].id === message.id) {
|
|
1569
|
+
if ("tool_calls" in message) {
|
|
1570
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1571
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1572
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
mergedMessages[i] = message;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1581
|
+
const currentMessage = mergedMessages[i];
|
|
1582
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1583
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1584
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
const correctedMessages = [];
|
|
1588
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1589
|
+
const currentMessage = mergedMessages[i];
|
|
1590
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1591
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1592
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1593
|
+
if (!nextMessage) {
|
|
1594
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1598
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1599
|
+
if (toolMessage) {
|
|
1600
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1601
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1602
|
+
continue;
|
|
1603
|
+
} else {
|
|
1604
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1605
|
+
continue;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
correctedMessages.push(currentMessage);
|
|
1609
|
+
continue;
|
|
1610
|
+
}
|
|
1611
|
+
if ("tool_call_id" in currentMessage) {
|
|
1612
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1613
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1614
|
+
continue;
|
|
1615
|
+
}
|
|
1616
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1617
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1618
|
+
continue;
|
|
1619
|
+
}
|
|
1620
|
+
correctedMessages.push(currentMessage);
|
|
1621
|
+
continue;
|
|
1622
|
+
}
|
|
1623
|
+
correctedMessages.push(currentMessage);
|
|
1624
|
+
}
|
|
1625
|
+
return deepMerge(state, {
|
|
1626
|
+
messages: correctedMessages,
|
|
1627
|
+
copilotkit: {
|
|
1628
|
+
actions
|
|
1629
|
+
}
|
|
1630
|
+
});
|
|
1631
|
+
}
|
|
1632
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1633
|
+
function deepMerge(obj1, obj2) {
|
|
1634
|
+
let result = {
|
|
1635
|
+
...obj1
|
|
1636
|
+
};
|
|
1637
|
+
for (let key in obj2) {
|
|
1638
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
1639
|
+
if (obj1[key]) {
|
|
1640
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
1641
|
+
} else {
|
|
1642
|
+
result[key] = {
|
|
1643
|
+
...obj2[key]
|
|
1644
|
+
};
|
|
1645
|
+
}
|
|
1646
|
+
} else {
|
|
1647
|
+
result[key] = obj2[key];
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
return result;
|
|
1651
|
+
}
|
|
1652
|
+
__name(deepMerge, "deepMerge");
|
|
1653
|
+
function formatMessages(messages) {
|
|
1654
|
+
return messages.map((message) => {
|
|
1655
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
1656
|
+
return message;
|
|
1657
|
+
}
|
|
1658
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
1659
|
+
return message;
|
|
1660
|
+
}
|
|
1661
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
1662
|
+
return message;
|
|
1663
|
+
}
|
|
1664
|
+
if (message.isActionExecutionMessage()) {
|
|
1665
|
+
const toolCall = {
|
|
1666
|
+
name: message.name,
|
|
1667
|
+
args: message.arguments,
|
|
1668
|
+
id: message.id
|
|
1669
|
+
};
|
|
1338
1670
|
return {
|
|
1339
|
-
|
|
1340
|
-
|
|
1671
|
+
type: message.type,
|
|
1672
|
+
content: "",
|
|
1673
|
+
tool_calls: [
|
|
1674
|
+
toolCall
|
|
1675
|
+
],
|
|
1676
|
+
role: MessageRole.assistant,
|
|
1677
|
+
id: message.id
|
|
1341
1678
|
};
|
|
1342
1679
|
}
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
};
|
|
1356
|
-
}
|
|
1680
|
+
if (message.isResultMessage()) {
|
|
1681
|
+
return {
|
|
1682
|
+
type: message.type,
|
|
1683
|
+
content: message.result,
|
|
1684
|
+
id: message.id,
|
|
1685
|
+
tool_call_id: message.actionExecutionId,
|
|
1686
|
+
name: message.actionName,
|
|
1687
|
+
role: MessageRole.tool
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1690
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
1691
|
+
});
|
|
1357
1692
|
}
|
|
1358
|
-
__name(
|
|
1693
|
+
__name(formatMessages, "formatMessages");
|
|
1694
|
+
|
|
1695
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
1696
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1697
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
1698
|
+
name: agent.name,
|
|
1699
|
+
description: agent.description,
|
|
1700
|
+
parameters: [],
|
|
1701
|
+
handler: async (_args) => {
|
|
1702
|
+
},
|
|
1703
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
1704
|
+
var _a;
|
|
1705
|
+
logger2.debug({
|
|
1706
|
+
actionName: agent.name
|
|
1707
|
+
}, "Executing LangGraph Cloud agent");
|
|
1708
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1709
|
+
let state = {};
|
|
1710
|
+
if (agentStates) {
|
|
1711
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1712
|
+
if (jsonState) {
|
|
1713
|
+
state = JSON.parse(jsonState);
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
try {
|
|
1717
|
+
const response = await execute({
|
|
1718
|
+
logger: logger2,
|
|
1719
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
1720
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1721
|
+
agent,
|
|
1722
|
+
threadId,
|
|
1723
|
+
nodeName,
|
|
1724
|
+
messages,
|
|
1725
|
+
state,
|
|
1726
|
+
properties: graphqlContext.properties,
|
|
1727
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1728
|
+
name: action.name,
|
|
1729
|
+
description: action.description,
|
|
1730
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
1731
|
+
}))
|
|
1732
|
+
});
|
|
1733
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
1734
|
+
streamResponse(response, eventSource.eventStream$);
|
|
1735
|
+
return eventSource.processLangGraphEvents();
|
|
1736
|
+
} catch (error) {
|
|
1737
|
+
logger2.error({
|
|
1738
|
+
url: endpoint.deploymentUrl,
|
|
1739
|
+
status: 500,
|
|
1740
|
+
body: error.message
|
|
1741
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
1742
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
}));
|
|
1746
|
+
return [
|
|
1747
|
+
...agents
|
|
1748
|
+
];
|
|
1749
|
+
}
|
|
1750
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1359
1751
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1360
1752
|
const actions = json["actions"].map((action) => ({
|
|
1361
1753
|
name: action.name,
|
|
@@ -1447,7 +1839,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1447
1839
|
throw new Error("Failed to execute remote agent");
|
|
1448
1840
|
}
|
|
1449
1841
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1450
|
-
|
|
1842
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1451
1843
|
return eventSource.processLangGraphEvents();
|
|
1452
1844
|
}
|
|
1453
1845
|
}));
|
|
@@ -1457,34 +1849,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1457
1849
|
];
|
|
1458
1850
|
}
|
|
1459
1851
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1460
|
-
async function
|
|
1852
|
+
async function streamResponse(response, eventStream$) {
|
|
1853
|
+
const reader = response.getReader();
|
|
1854
|
+
const decoder = new TextDecoder();
|
|
1855
|
+
let buffer = [];
|
|
1856
|
+
function flushBuffer() {
|
|
1857
|
+
const currentBuffer = buffer.join("");
|
|
1858
|
+
if (currentBuffer.trim().length === 0) {
|
|
1859
|
+
return;
|
|
1860
|
+
}
|
|
1861
|
+
const parts = currentBuffer.split("\n");
|
|
1862
|
+
if (parts.length === 0) {
|
|
1863
|
+
return;
|
|
1864
|
+
}
|
|
1865
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1866
|
+
buffer = [];
|
|
1867
|
+
if (!lastPartIsComplete) {
|
|
1868
|
+
buffer.push(parts.pop());
|
|
1869
|
+
}
|
|
1870
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1871
|
+
eventStream$.next(JSON.parse(part));
|
|
1872
|
+
});
|
|
1873
|
+
}
|
|
1874
|
+
__name(flushBuffer, "flushBuffer");
|
|
1875
|
+
try {
|
|
1876
|
+
while (true) {
|
|
1877
|
+
const { done, value } = await reader.read();
|
|
1878
|
+
if (!done) {
|
|
1879
|
+
buffer.push(decoder.decode(value, {
|
|
1880
|
+
stream: true
|
|
1881
|
+
}));
|
|
1882
|
+
}
|
|
1883
|
+
flushBuffer();
|
|
1884
|
+
if (done) {
|
|
1885
|
+
break;
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
} catch (error) {
|
|
1889
|
+
console.error("Error in stream", error);
|
|
1890
|
+
eventStream$.error(error);
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
eventStream$.complete();
|
|
1894
|
+
}
|
|
1895
|
+
__name(streamResponse, "streamResponse");
|
|
1896
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1897
|
+
const headers = {
|
|
1898
|
+
"Content-Type": "application/json"
|
|
1899
|
+
};
|
|
1900
|
+
if (onBeforeRequest) {
|
|
1901
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1902
|
+
ctx: graphqlContext
|
|
1903
|
+
});
|
|
1904
|
+
if (additionalHeaders) {
|
|
1905
|
+
Object.assign(headers, additionalHeaders);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
return headers;
|
|
1909
|
+
}
|
|
1910
|
+
__name(createHeaders, "createHeaders");
|
|
1911
|
+
|
|
1912
|
+
// src/lib/runtime/remote-actions.ts
|
|
1913
|
+
var EndpointType;
|
|
1914
|
+
(function(EndpointType2) {
|
|
1915
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
1916
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
1917
|
+
})(EndpointType || (EndpointType = {}));
|
|
1918
|
+
function isLangGraphAgentAction(action) {
|
|
1919
|
+
if (!action) {
|
|
1920
|
+
return false;
|
|
1921
|
+
}
|
|
1922
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
1923
|
+
}
|
|
1924
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1925
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1926
|
+
logger2.debug({
|
|
1927
|
+
url
|
|
1928
|
+
}, "Fetching actions from url");
|
|
1929
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
1930
|
+
try {
|
|
1931
|
+
const response = await fetch(`${url}/info`, {
|
|
1932
|
+
method: "POST",
|
|
1933
|
+
headers,
|
|
1934
|
+
body: JSON.stringify({
|
|
1935
|
+
properties: graphqlContext.properties,
|
|
1936
|
+
frontendUrl
|
|
1937
|
+
})
|
|
1938
|
+
});
|
|
1939
|
+
if (!response.ok) {
|
|
1940
|
+
logger2.error({
|
|
1941
|
+
url,
|
|
1942
|
+
status: response.status,
|
|
1943
|
+
body: await response.text()
|
|
1944
|
+
}, "Failed to fetch actions from url");
|
|
1945
|
+
return {
|
|
1946
|
+
actions: [],
|
|
1947
|
+
agents: []
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1950
|
+
const json = await response.json();
|
|
1951
|
+
logger2.debug({
|
|
1952
|
+
json
|
|
1953
|
+
}, "Fetched actions from url");
|
|
1954
|
+
return json;
|
|
1955
|
+
} catch (error) {
|
|
1956
|
+
logger2.error({
|
|
1957
|
+
error: error.message ? error.message : error + ""
|
|
1958
|
+
}, "Failed to fetch actions from url");
|
|
1959
|
+
return {
|
|
1960
|
+
actions: [],
|
|
1961
|
+
agents: []
|
|
1962
|
+
};
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
1966
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1461
1967
|
const logger2 = graphqlContext.logger.child({
|
|
1462
1968
|
component: "remote-actions.fetchRemoteActions"
|
|
1463
1969
|
});
|
|
1464
1970
|
logger2.debug({
|
|
1465
|
-
|
|
1466
|
-
}, "Fetching remote
|
|
1467
|
-
const filtered =
|
|
1468
|
-
|
|
1971
|
+
remoteEndpointDefinitions
|
|
1972
|
+
}, "Fetching from remote endpoints");
|
|
1973
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
1974
|
+
if (value.type === "langgraph-cloud") {
|
|
1975
|
+
return value;
|
|
1976
|
+
}
|
|
1977
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
1978
|
+
});
|
|
1979
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
1980
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
1981
|
+
return constructLGCRemoteAction({
|
|
1982
|
+
endpoint,
|
|
1983
|
+
messages,
|
|
1984
|
+
graphqlContext,
|
|
1985
|
+
logger: logger2.child({
|
|
1986
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
1987
|
+
endpoint
|
|
1988
|
+
}),
|
|
1989
|
+
agentStates
|
|
1990
|
+
});
|
|
1991
|
+
}
|
|
1469
1992
|
const json = await fetchRemoteInfo({
|
|
1470
|
-
url:
|
|
1471
|
-
onBeforeRequest:
|
|
1993
|
+
url: endpoint.url,
|
|
1994
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1472
1995
|
graphqlContext,
|
|
1473
1996
|
logger: logger2.child({
|
|
1474
1997
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1475
|
-
|
|
1998
|
+
endpoint
|
|
1476
1999
|
}),
|
|
1477
2000
|
frontendUrl
|
|
1478
2001
|
});
|
|
1479
2002
|
return constructRemoteActions({
|
|
1480
2003
|
json,
|
|
1481
2004
|
messages,
|
|
1482
|
-
url:
|
|
1483
|
-
onBeforeRequest:
|
|
2005
|
+
url: endpoint.url,
|
|
2006
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1484
2007
|
graphqlContext,
|
|
1485
2008
|
logger: logger2.child({
|
|
1486
2009
|
component: "remote-actions.constructActions",
|
|
1487
|
-
|
|
2010
|
+
endpoint
|
|
1488
2011
|
}),
|
|
1489
2012
|
agentStates
|
|
1490
2013
|
});
|
|
@@ -2243,6 +2766,7 @@ __name(copilotRuntimeNodeHttpEndpoint, "copilotRuntimeNodeHttpEndpoint");
|
|
|
2243
2766
|
|
|
2244
2767
|
export {
|
|
2245
2768
|
telemetry_client_default,
|
|
2769
|
+
EndpointType,
|
|
2246
2770
|
isLangGraphAgentAction,
|
|
2247
2771
|
setupRemoteActions,
|
|
2248
2772
|
RuntimeEventSource,
|
|
@@ -2251,4 +2775,4 @@ export {
|
|
|
2251
2775
|
getCommonConfig,
|
|
2252
2776
|
copilotRuntimeNodeHttpEndpoint
|
|
2253
2777
|
};
|
|
2254
|
-
//# sourceMappingURL=chunk-
|
|
2778
|
+
//# sourceMappingURL=chunk-AEUECXAW.mjs.map
|