@copilotkit/runtime 1.3.16-mme-azure-openai.0 → 1.3.16-mme-sdk-js.2
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 +19 -3
- package/__snapshots__/schema/schema.graphql +1 -0
- package/dist/{chunk-MVG266E4.mjs → chunk-3N5PRHNJ.mjs} +37 -10
- package/dist/chunk-3N5PRHNJ.mjs.map +1 -0
- package/dist/{chunk-S5SYJ6JC.mjs → chunk-5IF2J3NL.mjs} +2 -2
- package/dist/{chunk-YVZORQSA.mjs → chunk-6UQXCIKF.mjs} +673 -151
- package/dist/chunk-6UQXCIKF.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-MKZBH3Y6.mjs → chunk-HFTU4B7Q.mjs} +2 -2
- package/dist/{chunk-CRSCH25P.mjs → chunk-PA6U4BDE.mjs} +2 -2
- package/dist/{chunk-BHSRGDL6.mjs → chunk-XE3SYKK4.mjs} +1 -4
- package/dist/chunk-XE3SYKK4.mjs.map +1 -0
- 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-798aff23.d.ts → groq-adapter-7aa25931.d.ts} +1 -34
- package/dist/{index-cff31380.d.ts → index-83ee522f.d.ts} +3 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +745 -199
- 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 +745 -199
- 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 +0 -3
- 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 +612 -0
- package/src/service-adapters/openai/openai-adapter.ts +0 -37
- package/src/service-adapters/openai/utils.ts +13 -9
- package/dist/chunk-BHSRGDL6.mjs.map +0 -1
- package/dist/chunk-MVG266E4.mjs.map +0 -1
- package/dist/chunk-MYZHUCL6.mjs.map +0 -1
- package/dist/chunk-YVZORQSA.mjs.map +0 -1
- /package/dist/{chunk-S5SYJ6JC.mjs.map → chunk-5IF2J3NL.mjs.map} +0 -0
- /package/dist/{chunk-MKZBH3Y6.mjs.map → chunk-HFTU4B7Q.mjs.map} +0 -0
- /package/dist/{chunk-CRSCH25P.mjs.map → chunk-PA6U4BDE.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.3.16-mme-
|
|
39
|
+
version: "1.3.16-mme-sdk-js.2",
|
|
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,488 @@ 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
|
+
state = event.data;
|
|
1389
|
+
emit(getStateSyncEvent({
|
|
1390
|
+
threadId,
|
|
1391
|
+
runId,
|
|
1392
|
+
agentName: agent.name,
|
|
1393
|
+
nodeName,
|
|
1394
|
+
state,
|
|
1395
|
+
running: true,
|
|
1396
|
+
active: true
|
|
1397
|
+
}));
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1401
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1402
|
+
}
|
|
1403
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
|
|
1404
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1405
|
+
}
|
|
1406
|
+
let updatedState = latestStateValues;
|
|
1407
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
|
|
1408
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1409
|
+
}
|
|
1410
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1411
|
+
updatedState = {
|
|
1412
|
+
...updatedState,
|
|
1413
|
+
...streamingStateExtractor.extractState()
|
|
1414
|
+
};
|
|
1415
|
+
}
|
|
1416
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1417
|
+
emitIntermediateStateUntilEnd = null;
|
|
1418
|
+
}
|
|
1419
|
+
const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
|
|
1420
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1421
|
+
state = updatedState;
|
|
1422
|
+
prevNodeName = nodeName;
|
|
1423
|
+
emit(getStateSyncEvent({
|
|
1424
|
+
threadId,
|
|
1425
|
+
runId,
|
|
1426
|
+
agentName: agent.name,
|
|
1427
|
+
nodeName,
|
|
1428
|
+
state,
|
|
1429
|
+
running: true,
|
|
1430
|
+
active: !exitingNode
|
|
1431
|
+
}));
|
|
1432
|
+
}
|
|
1433
|
+
emit(JSON.stringify(event) + "\n");
|
|
1313
1434
|
}
|
|
1435
|
+
state = await client.threads.getState(threadId);
|
|
1436
|
+
const isEndNode = state.next.length === 0;
|
|
1437
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1438
|
+
emit(getStateSyncEvent({
|
|
1439
|
+
threadId,
|
|
1440
|
+
runId: externalRunId,
|
|
1441
|
+
agentName: agent.name,
|
|
1442
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1443
|
+
state: state.values,
|
|
1444
|
+
running: !shouldExit,
|
|
1445
|
+
active: false
|
|
1446
|
+
}));
|
|
1447
|
+
return Promise.resolve();
|
|
1448
|
+
} catch (e) {
|
|
1449
|
+
return Promise.resolve();
|
|
1314
1450
|
}
|
|
1315
|
-
return headers;
|
|
1316
1451
|
}
|
|
1317
|
-
__name(
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1452
|
+
__name(streamEvents, "streamEvents");
|
|
1453
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1454
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1455
|
+
if (key !== "messages") {
|
|
1456
|
+
acc[key] = state[key];
|
|
1457
|
+
}
|
|
1458
|
+
return acc;
|
|
1459
|
+
}, {});
|
|
1460
|
+
return JSON.stringify({
|
|
1461
|
+
event: LangGraphEventTypes.OnCopilotKitStateSync,
|
|
1462
|
+
thread_id: threadId,
|
|
1463
|
+
run_id: runId,
|
|
1464
|
+
agent_name: agentName,
|
|
1465
|
+
node_name: nodeName,
|
|
1466
|
+
active,
|
|
1467
|
+
state: stateWithoutMessages,
|
|
1468
|
+
running,
|
|
1469
|
+
role: "assistant"
|
|
1470
|
+
}) + "\n";
|
|
1471
|
+
}
|
|
1472
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1473
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1474
|
+
emitIntermediateState;
|
|
1475
|
+
toolCallBuffer;
|
|
1476
|
+
currentToolCall;
|
|
1477
|
+
previouslyParsableState;
|
|
1478
|
+
constructor(emitIntermediateState) {
|
|
1479
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1480
|
+
this.toolCallBuffer = {};
|
|
1481
|
+
this.currentToolCall = null;
|
|
1482
|
+
this.previouslyParsableState = {};
|
|
1483
|
+
}
|
|
1484
|
+
bufferToolCalls(event) {
|
|
1485
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1486
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1487
|
+
if (chunk.name !== null && chunk.name !== void 0) {
|
|
1488
|
+
this.currentToolCall = chunk.name;
|
|
1489
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1490
|
+
} else if (this.currentToolCall !== null && this.currentToolCall !== void 0) {
|
|
1491
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
getEmitStateConfig(currentToolName) {
|
|
1496
|
+
for (const config of this.emitIntermediateState) {
|
|
1497
|
+
const stateKey = config["state_key"];
|
|
1498
|
+
const tool = config["tool"];
|
|
1499
|
+
const toolArgument = config["tool_argument"];
|
|
1500
|
+
if (currentToolName === tool) {
|
|
1501
|
+
return [
|
|
1502
|
+
toolArgument,
|
|
1503
|
+
stateKey
|
|
1504
|
+
];
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
return [
|
|
1508
|
+
null,
|
|
1509
|
+
null
|
|
1510
|
+
];
|
|
1511
|
+
}
|
|
1512
|
+
extractState() {
|
|
1513
|
+
const state = {};
|
|
1514
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1515
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1516
|
+
if (stateKey === null) {
|
|
1517
|
+
continue;
|
|
1518
|
+
}
|
|
1519
|
+
let parsedValue;
|
|
1520
|
+
try {
|
|
1521
|
+
parsedValue = parsePartialJson(value);
|
|
1522
|
+
} catch (error) {
|
|
1523
|
+
if (key in this.previouslyParsableState) {
|
|
1524
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1525
|
+
} else {
|
|
1526
|
+
continue;
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1530
|
+
if (!argumentName) {
|
|
1531
|
+
state[stateKey] = parsedValue;
|
|
1532
|
+
} else {
|
|
1533
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
return state;
|
|
1537
|
+
}
|
|
1538
|
+
}, "StreamingStateExtractor");
|
|
1539
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1540
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1541
|
+
messages = messages.slice(1);
|
|
1542
|
+
}
|
|
1543
|
+
const mergedMessages = state.messages || [];
|
|
1544
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1545
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1546
|
+
for (const message of mergedMessages) {
|
|
1547
|
+
if ("tool_call_id" in message) {
|
|
1548
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
for (const message of messages) {
|
|
1552
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
if ("name" in message && message.name === agentName) {
|
|
1556
|
+
continue;
|
|
1557
|
+
}
|
|
1558
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1559
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1560
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1561
|
+
continue;
|
|
1562
|
+
}
|
|
1563
|
+
mergedMessages.push(message);
|
|
1564
|
+
} else {
|
|
1565
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1566
|
+
if (mergedMessages[i].id === message.id) {
|
|
1567
|
+
if ("tool_calls" in message) {
|
|
1568
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1569
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1570
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
mergedMessages[i] = message;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1579
|
+
const currentMessage = mergedMessages[i];
|
|
1580
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1581
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1582
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
const correctedMessages = [];
|
|
1586
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1587
|
+
const currentMessage = mergedMessages[i];
|
|
1588
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1589
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1590
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1591
|
+
if (!nextMessage) {
|
|
1592
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1593
|
+
continue;
|
|
1594
|
+
}
|
|
1595
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1596
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1597
|
+
if (toolMessage) {
|
|
1598
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1599
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1600
|
+
continue;
|
|
1601
|
+
} else {
|
|
1602
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1603
|
+
continue;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
correctedMessages.push(currentMessage);
|
|
1607
|
+
continue;
|
|
1608
|
+
}
|
|
1609
|
+
if ("tool_call_id" in currentMessage) {
|
|
1610
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1611
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1612
|
+
continue;
|
|
1613
|
+
}
|
|
1614
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1615
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
correctedMessages.push(currentMessage);
|
|
1619
|
+
continue;
|
|
1620
|
+
}
|
|
1621
|
+
correctedMessages.push(currentMessage);
|
|
1622
|
+
}
|
|
1623
|
+
return deepMerge(state, {
|
|
1624
|
+
messages: correctedMessages,
|
|
1625
|
+
copilotkit: {
|
|
1626
|
+
actions
|
|
1627
|
+
}
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1631
|
+
function deepMerge(obj1, obj2) {
|
|
1632
|
+
let result = {
|
|
1633
|
+
...obj1
|
|
1634
|
+
};
|
|
1635
|
+
for (let key in obj2) {
|
|
1636
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
1637
|
+
if (obj1[key]) {
|
|
1638
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
1639
|
+
} else {
|
|
1640
|
+
result[key] = {
|
|
1641
|
+
...obj2[key]
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
} else {
|
|
1645
|
+
result[key] = obj2[key];
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
return result;
|
|
1649
|
+
}
|
|
1650
|
+
__name(deepMerge, "deepMerge");
|
|
1651
|
+
function formatMessages(messages) {
|
|
1652
|
+
return messages.map((message) => {
|
|
1653
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
1654
|
+
return message;
|
|
1655
|
+
}
|
|
1656
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
1657
|
+
return message;
|
|
1658
|
+
}
|
|
1659
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
1660
|
+
return message;
|
|
1661
|
+
}
|
|
1662
|
+
if (message.isActionExecutionMessage()) {
|
|
1663
|
+
const toolCall = {
|
|
1664
|
+
name: message.name,
|
|
1665
|
+
args: message.arguments,
|
|
1666
|
+
id: message.id
|
|
1667
|
+
};
|
|
1338
1668
|
return {
|
|
1339
|
-
|
|
1340
|
-
|
|
1669
|
+
type: message.type,
|
|
1670
|
+
content: "",
|
|
1671
|
+
tool_calls: [
|
|
1672
|
+
toolCall
|
|
1673
|
+
],
|
|
1674
|
+
role: MessageRole.assistant,
|
|
1675
|
+
id: message.id
|
|
1341
1676
|
};
|
|
1342
1677
|
}
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
};
|
|
1356
|
-
}
|
|
1678
|
+
if (message.isResultMessage()) {
|
|
1679
|
+
return {
|
|
1680
|
+
type: message.type,
|
|
1681
|
+
content: message.result,
|
|
1682
|
+
id: message.id,
|
|
1683
|
+
tool_call_id: message.actionExecutionId,
|
|
1684
|
+
name: message.actionName,
|
|
1685
|
+
role: MessageRole.tool
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
1689
|
+
});
|
|
1357
1690
|
}
|
|
1358
|
-
__name(
|
|
1691
|
+
__name(formatMessages, "formatMessages");
|
|
1692
|
+
|
|
1693
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
1694
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1695
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
1696
|
+
name: agent.name,
|
|
1697
|
+
description: agent.description,
|
|
1698
|
+
parameters: [],
|
|
1699
|
+
handler: async (_args) => {
|
|
1700
|
+
},
|
|
1701
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
1702
|
+
var _a;
|
|
1703
|
+
logger2.debug({
|
|
1704
|
+
actionName: agent.name
|
|
1705
|
+
}, "Executing LangGraph Cloud agent");
|
|
1706
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1707
|
+
let state = {};
|
|
1708
|
+
if (agentStates) {
|
|
1709
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1710
|
+
if (jsonState) {
|
|
1711
|
+
state = JSON.parse(jsonState);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
try {
|
|
1715
|
+
const response = await execute({
|
|
1716
|
+
logger: logger2,
|
|
1717
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
1718
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1719
|
+
agent,
|
|
1720
|
+
threadId,
|
|
1721
|
+
nodeName,
|
|
1722
|
+
messages,
|
|
1723
|
+
state,
|
|
1724
|
+
properties: graphqlContext.properties,
|
|
1725
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1726
|
+
name: action.name,
|
|
1727
|
+
description: action.description,
|
|
1728
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
1729
|
+
}))
|
|
1730
|
+
});
|
|
1731
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
1732
|
+
streamResponse(response, eventSource.eventStream$);
|
|
1733
|
+
return eventSource.processLangGraphEvents();
|
|
1734
|
+
} catch (error) {
|
|
1735
|
+
logger2.error({
|
|
1736
|
+
url: endpoint.deploymentUrl,
|
|
1737
|
+
status: 500,
|
|
1738
|
+
body: error.message
|
|
1739
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
1740
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
}));
|
|
1744
|
+
return [
|
|
1745
|
+
...agents
|
|
1746
|
+
];
|
|
1747
|
+
}
|
|
1748
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1359
1749
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1360
1750
|
const actions = json["actions"].map((action) => ({
|
|
1361
1751
|
name: action.name,
|
|
@@ -1447,7 +1837,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1447
1837
|
throw new Error("Failed to execute remote agent");
|
|
1448
1838
|
}
|
|
1449
1839
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1450
|
-
|
|
1840
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1451
1841
|
return eventSource.processLangGraphEvents();
|
|
1452
1842
|
}
|
|
1453
1843
|
}));
|
|
@@ -1457,34 +1847,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1457
1847
|
];
|
|
1458
1848
|
}
|
|
1459
1849
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1460
|
-
async function
|
|
1850
|
+
async function streamResponse(response, eventStream$) {
|
|
1851
|
+
const reader = response.getReader();
|
|
1852
|
+
const decoder = new TextDecoder();
|
|
1853
|
+
let buffer = [];
|
|
1854
|
+
function flushBuffer() {
|
|
1855
|
+
const currentBuffer = buffer.join("");
|
|
1856
|
+
if (currentBuffer.trim().length === 0) {
|
|
1857
|
+
return;
|
|
1858
|
+
}
|
|
1859
|
+
const parts = currentBuffer.split("\n");
|
|
1860
|
+
if (parts.length === 0) {
|
|
1861
|
+
return;
|
|
1862
|
+
}
|
|
1863
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1864
|
+
buffer = [];
|
|
1865
|
+
if (!lastPartIsComplete) {
|
|
1866
|
+
buffer.push(parts.pop());
|
|
1867
|
+
}
|
|
1868
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1869
|
+
eventStream$.next(JSON.parse(part));
|
|
1870
|
+
});
|
|
1871
|
+
}
|
|
1872
|
+
__name(flushBuffer, "flushBuffer");
|
|
1873
|
+
try {
|
|
1874
|
+
while (true) {
|
|
1875
|
+
const { done, value } = await reader.read();
|
|
1876
|
+
if (!done) {
|
|
1877
|
+
buffer.push(decoder.decode(value, {
|
|
1878
|
+
stream: true
|
|
1879
|
+
}));
|
|
1880
|
+
}
|
|
1881
|
+
flushBuffer();
|
|
1882
|
+
if (done) {
|
|
1883
|
+
break;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
} catch (error) {
|
|
1887
|
+
console.error("Error in stream", error);
|
|
1888
|
+
eventStream$.error(error);
|
|
1889
|
+
return;
|
|
1890
|
+
}
|
|
1891
|
+
eventStream$.complete();
|
|
1892
|
+
}
|
|
1893
|
+
__name(streamResponse, "streamResponse");
|
|
1894
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1895
|
+
const headers = {
|
|
1896
|
+
"Content-Type": "application/json"
|
|
1897
|
+
};
|
|
1898
|
+
if (onBeforeRequest) {
|
|
1899
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1900
|
+
ctx: graphqlContext
|
|
1901
|
+
});
|
|
1902
|
+
if (additionalHeaders) {
|
|
1903
|
+
Object.assign(headers, additionalHeaders);
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
return headers;
|
|
1907
|
+
}
|
|
1908
|
+
__name(createHeaders, "createHeaders");
|
|
1909
|
+
|
|
1910
|
+
// src/lib/runtime/remote-actions.ts
|
|
1911
|
+
var EndpointType;
|
|
1912
|
+
(function(EndpointType2) {
|
|
1913
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
1914
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
1915
|
+
})(EndpointType || (EndpointType = {}));
|
|
1916
|
+
function isLangGraphAgentAction(action) {
|
|
1917
|
+
if (!action) {
|
|
1918
|
+
return false;
|
|
1919
|
+
}
|
|
1920
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
1921
|
+
}
|
|
1922
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1923
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1924
|
+
logger2.debug({
|
|
1925
|
+
url
|
|
1926
|
+
}, "Fetching actions from url");
|
|
1927
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
1928
|
+
try {
|
|
1929
|
+
const response = await fetch(`${url}/info`, {
|
|
1930
|
+
method: "POST",
|
|
1931
|
+
headers,
|
|
1932
|
+
body: JSON.stringify({
|
|
1933
|
+
properties: graphqlContext.properties,
|
|
1934
|
+
frontendUrl
|
|
1935
|
+
})
|
|
1936
|
+
});
|
|
1937
|
+
if (!response.ok) {
|
|
1938
|
+
logger2.error({
|
|
1939
|
+
url,
|
|
1940
|
+
status: response.status,
|
|
1941
|
+
body: await response.text()
|
|
1942
|
+
}, "Failed to fetch actions from url");
|
|
1943
|
+
return {
|
|
1944
|
+
actions: [],
|
|
1945
|
+
agents: []
|
|
1946
|
+
};
|
|
1947
|
+
}
|
|
1948
|
+
const json = await response.json();
|
|
1949
|
+
logger2.debug({
|
|
1950
|
+
json
|
|
1951
|
+
}, "Fetched actions from url");
|
|
1952
|
+
return json;
|
|
1953
|
+
} catch (error) {
|
|
1954
|
+
logger2.error({
|
|
1955
|
+
error: error.message ? error.message : error + ""
|
|
1956
|
+
}, "Failed to fetch actions from url");
|
|
1957
|
+
return {
|
|
1958
|
+
actions: [],
|
|
1959
|
+
agents: []
|
|
1960
|
+
};
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
1964
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1461
1965
|
const logger2 = graphqlContext.logger.child({
|
|
1462
1966
|
component: "remote-actions.fetchRemoteActions"
|
|
1463
1967
|
});
|
|
1464
1968
|
logger2.debug({
|
|
1465
|
-
|
|
1466
|
-
}, "Fetching remote
|
|
1467
|
-
const filtered =
|
|
1468
|
-
|
|
1969
|
+
remoteEndpointDefinitions
|
|
1970
|
+
}, "Fetching from remote endpoints");
|
|
1971
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
1972
|
+
if (value.type === "langgraph-cloud") {
|
|
1973
|
+
return value;
|
|
1974
|
+
}
|
|
1975
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
1976
|
+
});
|
|
1977
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
1978
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
1979
|
+
return constructLGCRemoteAction({
|
|
1980
|
+
endpoint,
|
|
1981
|
+
messages,
|
|
1982
|
+
graphqlContext,
|
|
1983
|
+
logger: logger2.child({
|
|
1984
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
1985
|
+
endpoint
|
|
1986
|
+
}),
|
|
1987
|
+
agentStates
|
|
1988
|
+
});
|
|
1989
|
+
}
|
|
1469
1990
|
const json = await fetchRemoteInfo({
|
|
1470
|
-
url:
|
|
1471
|
-
onBeforeRequest:
|
|
1991
|
+
url: endpoint.url,
|
|
1992
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1472
1993
|
graphqlContext,
|
|
1473
1994
|
logger: logger2.child({
|
|
1474
1995
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1475
|
-
|
|
1996
|
+
endpoint
|
|
1476
1997
|
}),
|
|
1477
1998
|
frontendUrl
|
|
1478
1999
|
});
|
|
1479
2000
|
return constructRemoteActions({
|
|
1480
2001
|
json,
|
|
1481
2002
|
messages,
|
|
1482
|
-
url:
|
|
1483
|
-
onBeforeRequest:
|
|
2003
|
+
url: endpoint.url,
|
|
2004
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1484
2005
|
graphqlContext,
|
|
1485
2006
|
logger: logger2.child({
|
|
1486
2007
|
component: "remote-actions.constructActions",
|
|
1487
|
-
|
|
2008
|
+
endpoint
|
|
1488
2009
|
}),
|
|
1489
2010
|
agentStates
|
|
1490
2011
|
});
|
|
@@ -2243,6 +2764,7 @@ __name(copilotRuntimeNodeHttpEndpoint, "copilotRuntimeNodeHttpEndpoint");
|
|
|
2243
2764
|
|
|
2244
2765
|
export {
|
|
2245
2766
|
telemetry_client_default,
|
|
2767
|
+
EndpointType,
|
|
2246
2768
|
isLangGraphAgentAction,
|
|
2247
2769
|
setupRemoteActions,
|
|
2248
2770
|
RuntimeEventSource,
|
|
@@ -2251,4 +2773,4 @@ export {
|
|
|
2251
2773
|
getCommonConfig,
|
|
2252
2774
|
copilotRuntimeNodeHttpEndpoint
|
|
2253
2775
|
};
|
|
2254
|
-
//# sourceMappingURL=chunk-
|
|
2776
|
+
//# sourceMappingURL=chunk-6UQXCIKF.mjs.map
|