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