@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
package/dist/lib/index.js
CHANGED
|
@@ -44,7 +44,7 @@ var require_package = __commonJS({
|
|
|
44
44
|
publishConfig: {
|
|
45
45
|
access: "public"
|
|
46
46
|
},
|
|
47
|
-
version: "1.
|
|
47
|
+
version: "1.4.0-pre-1-4-0.0",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -87,6 +87,7 @@ var require_package = __commonJS({
|
|
|
87
87
|
"@langchain/community": "^0.0.53",
|
|
88
88
|
"@langchain/core": "^0.3.13",
|
|
89
89
|
"@langchain/google-gauth": "^0.1.0",
|
|
90
|
+
"@langchain/langgraph-sdk": "^0.0.16",
|
|
90
91
|
"@langchain/openai": "^0.0.28",
|
|
91
92
|
"class-transformer": "^0.5.1",
|
|
92
93
|
express: "^4.19.2",
|
|
@@ -96,6 +97,7 @@ var require_package = __commonJS({
|
|
|
96
97
|
"groq-sdk": "^0.5.0",
|
|
97
98
|
langchain: "^0.3.3",
|
|
98
99
|
openai: "^4.50.0",
|
|
100
|
+
"partial-json": "^0.1.7",
|
|
99
101
|
pino: "^9.2.0",
|
|
100
102
|
"pino-pretty": "^11.2.1",
|
|
101
103
|
"reflect-metadata": "^0.2.2",
|
|
@@ -131,6 +133,7 @@ __export(lib_exports, {
|
|
|
131
133
|
UnifyAdapter: () => UnifyAdapter,
|
|
132
134
|
buildSchema: () => buildSchema,
|
|
133
135
|
config: () => config,
|
|
136
|
+
copilotKitEndpoint: () => copilotKitEndpoint,
|
|
134
137
|
copilotRuntimeNestEndpoint: () => copilotRuntimeNestEndpoint,
|
|
135
138
|
copilotRuntimeNextJSAppRouterEndpoint: () => copilotRuntimeNextJSAppRouterEndpoint,
|
|
136
139
|
copilotRuntimeNextJSPagesRouterEndpoint: () => copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -138,7 +141,8 @@ __export(lib_exports, {
|
|
|
138
141
|
copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
|
|
139
142
|
createContext: () => createContext,
|
|
140
143
|
flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
|
|
141
|
-
getCommonConfig: () => getCommonConfig
|
|
144
|
+
getCommonConfig: () => getCommonConfig,
|
|
145
|
+
langGraphCloudEndpoint: () => langGraphCloudEndpoint
|
|
142
146
|
});
|
|
143
147
|
module.exports = __toCommonJS(lib_exports);
|
|
144
148
|
|
|
@@ -1080,58 +1084,19 @@ var LangGraphEventTypes;
|
|
|
1080
1084
|
LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
|
|
1081
1085
|
LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
|
|
1082
1086
|
LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
|
|
1083
|
-
LangGraphEventTypes2["
|
|
1084
|
-
LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
|
|
1087
|
+
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
|
|
1085
1088
|
})(LangGraphEventTypes || (LangGraphEventTypes = {}));
|
|
1089
|
+
var CustomEventNames;
|
|
1090
|
+
(function(CustomEventNames2) {
|
|
1091
|
+
CustomEventNames2["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
|
|
1092
|
+
CustomEventNames2["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
|
|
1093
|
+
CustomEventNames2["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
|
|
1094
|
+
})(CustomEventNames || (CustomEventNames = {}));
|
|
1086
1095
|
|
|
1087
1096
|
// src/agents/langgraph/event-source.ts
|
|
1088
1097
|
var import_shared7 = require("@copilotkit/shared");
|
|
1089
1098
|
var RemoteLangGraphEventSource = class {
|
|
1090
1099
|
eventStream$ = new import_rxjs.ReplaySubject();
|
|
1091
|
-
async streamResponse(response) {
|
|
1092
|
-
const reader = response.body.getReader();
|
|
1093
|
-
const decoder = new TextDecoder();
|
|
1094
|
-
let buffer = [];
|
|
1095
|
-
const eventStream$ = this.eventStream$;
|
|
1096
|
-
function flushBuffer() {
|
|
1097
|
-
const currentBuffer = buffer.join("");
|
|
1098
|
-
if (currentBuffer.trim().length === 0) {
|
|
1099
|
-
return;
|
|
1100
|
-
}
|
|
1101
|
-
const parts = currentBuffer.split("\n");
|
|
1102
|
-
if (parts.length === 0) {
|
|
1103
|
-
return;
|
|
1104
|
-
}
|
|
1105
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1106
|
-
buffer = [];
|
|
1107
|
-
if (!lastPartIsComplete) {
|
|
1108
|
-
buffer.push(parts.pop());
|
|
1109
|
-
}
|
|
1110
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1111
|
-
eventStream$.next(JSON.parse(part));
|
|
1112
|
-
});
|
|
1113
|
-
}
|
|
1114
|
-
__name(flushBuffer, "flushBuffer");
|
|
1115
|
-
try {
|
|
1116
|
-
while (true) {
|
|
1117
|
-
const { done, value } = await reader.read();
|
|
1118
|
-
if (!done) {
|
|
1119
|
-
buffer.push(decoder.decode(value, {
|
|
1120
|
-
stream: true
|
|
1121
|
-
}));
|
|
1122
|
-
}
|
|
1123
|
-
flushBuffer();
|
|
1124
|
-
if (done) {
|
|
1125
|
-
break;
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
} catch (error) {
|
|
1129
|
-
console.error("Error in stream", error);
|
|
1130
|
-
eventStream$.error(error);
|
|
1131
|
-
return;
|
|
1132
|
-
}
|
|
1133
|
-
eventStream$.complete();
|
|
1134
|
-
}
|
|
1135
1100
|
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
1136
1101
|
if (typeof shouldEmitToolCalls === "boolean") {
|
|
1137
1102
|
return shouldEmitToolCalls;
|
|
@@ -1154,20 +1119,25 @@ var RemoteLangGraphEventSource = class {
|
|
|
1154
1119
|
} else {
|
|
1155
1120
|
acc.content = null;
|
|
1156
1121
|
}
|
|
1157
|
-
|
|
1122
|
+
const toolCallChunks = (
|
|
1123
|
+
// @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
|
|
1124
|
+
((_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)
|
|
1125
|
+
);
|
|
1126
|
+
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);
|
|
1127
|
+
if (toolCallChunks && toolCallChunks.length > 0) {
|
|
1158
1128
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1159
|
-
acc.toolCallMessageId =
|
|
1160
|
-
if ((
|
|
1161
|
-
acc.toolCallName =
|
|
1129
|
+
acc.toolCallMessageId = toolCallMessageId;
|
|
1130
|
+
if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
|
|
1131
|
+
acc.toolCallName = toolCallChunks[0].name;
|
|
1162
1132
|
}
|
|
1163
|
-
if ((
|
|
1164
|
-
acc.toolCallId =
|
|
1133
|
+
if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
|
|
1134
|
+
acc.toolCallId = toolCallChunks[0].id;
|
|
1165
1135
|
}
|
|
1166
1136
|
acc.prevMessageId = acc.messageId;
|
|
1167
|
-
acc.messageId =
|
|
1137
|
+
acc.messageId = toolCallMessageId;
|
|
1168
1138
|
} else if (acc.content && acc.content != "") {
|
|
1169
1139
|
acc.prevMessageId = acc.messageId;
|
|
1170
|
-
acc.messageId =
|
|
1140
|
+
acc.messageId = toolCallMessageId;
|
|
1171
1141
|
} else {
|
|
1172
1142
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1173
1143
|
acc.prevMessageId = acc.messageId;
|
|
@@ -1192,7 +1162,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1192
1162
|
prevMessageId: null,
|
|
1193
1163
|
content: null
|
|
1194
1164
|
}), (0, import_rxjs.mergeMap)((eventWithState) => {
|
|
1195
|
-
var _a, _b, _c, _d, _e;
|
|
1165
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1196
1166
|
const events = [];
|
|
1197
1167
|
let shouldEmitMessages = true;
|
|
1198
1168
|
let shouldEmitToolCalls = false;
|
|
@@ -1215,32 +1185,33 @@ var RemoteLangGraphEventSource = class {
|
|
|
1215
1185
|
});
|
|
1216
1186
|
}
|
|
1217
1187
|
switch (eventWithState.event.event) {
|
|
1218
|
-
case LangGraphEventTypes.
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1188
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
1189
|
+
if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
1190
|
+
events.push({
|
|
1191
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
1192
|
+
messageId: eventWithState.event.data.message_id
|
|
1193
|
+
});
|
|
1194
|
+
events.push({
|
|
1195
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
1196
|
+
content: eventWithState.event.data.message
|
|
1197
|
+
});
|
|
1198
|
+
events.push({
|
|
1199
|
+
type: RuntimeEventTypes.TextMessageEnd
|
|
1200
|
+
});
|
|
1201
|
+
} else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
1202
|
+
events.push({
|
|
1203
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1204
|
+
actionExecutionId: eventWithState.event.data.id,
|
|
1205
|
+
actionName: eventWithState.event.data.name
|
|
1206
|
+
});
|
|
1207
|
+
events.push({
|
|
1208
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1209
|
+
args: JSON.stringify(eventWithState.event.data.args)
|
|
1210
|
+
});
|
|
1211
|
+
events.push({
|
|
1212
|
+
type: RuntimeEventTypes.ActionExecutionEnd
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1244
1215
|
break;
|
|
1245
1216
|
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
1246
1217
|
events.push({
|
|
@@ -1275,7 +1246,8 @@ var RemoteLangGraphEventSource = class {
|
|
|
1275
1246
|
});
|
|
1276
1247
|
}
|
|
1277
1248
|
}
|
|
1278
|
-
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
|
|
1249
|
+
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
|
|
1250
|
+
((_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);
|
|
1279
1251
|
const content = eventWithState.content;
|
|
1280
1252
|
if (args) {
|
|
1281
1253
|
if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
|
|
@@ -1326,70 +1298,543 @@ var RemoteLangGraphEventSource = class {
|
|
|
1326
1298
|
};
|
|
1327
1299
|
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
1328
1300
|
|
|
1329
|
-
// src/lib/runtime/remote-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1301
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1302
|
+
var import_langgraph_sdk = require("@langchain/langgraph-sdk");
|
|
1303
|
+
var import_node_crypto = require("crypto");
|
|
1304
|
+
var import_partial_json = require("partial-json");
|
|
1305
|
+
|
|
1306
|
+
// src/graphql/types/enums.ts
|
|
1307
|
+
var import_type_graphql = require("type-graphql");
|
|
1308
|
+
var MessageRole;
|
|
1309
|
+
(function(MessageRole2) {
|
|
1310
|
+
MessageRole2["user"] = "user";
|
|
1311
|
+
MessageRole2["assistant"] = "assistant";
|
|
1312
|
+
MessageRole2["system"] = "system";
|
|
1313
|
+
})(MessageRole || (MessageRole = {}));
|
|
1314
|
+
var ActionExecutionScope;
|
|
1315
|
+
(function(ActionExecutionScope2) {
|
|
1316
|
+
ActionExecutionScope2["server"] = "server";
|
|
1317
|
+
ActionExecutionScope2["client"] = "client";
|
|
1318
|
+
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
1319
|
+
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
1320
|
+
var CopilotRequestType;
|
|
1321
|
+
(function(CopilotRequestType2) {
|
|
1322
|
+
CopilotRequestType2["Chat"] = "Chat";
|
|
1323
|
+
CopilotRequestType2["Task"] = "Task";
|
|
1324
|
+
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
1325
|
+
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
1326
|
+
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
1327
|
+
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
1328
|
+
(0, import_type_graphql.registerEnumType)(MessageRole, {
|
|
1329
|
+
name: "MessageRole",
|
|
1330
|
+
description: "The role of the message"
|
|
1331
|
+
});
|
|
1332
|
+
(0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
|
|
1333
|
+
name: "ActionExecutionScope",
|
|
1334
|
+
description: "The scope of the action"
|
|
1335
|
+
});
|
|
1336
|
+
(0, import_type_graphql.registerEnumType)(CopilotRequestType, {
|
|
1337
|
+
name: "CopilotRequestType",
|
|
1338
|
+
description: "The type of Copilot request"
|
|
1339
|
+
});
|
|
1340
|
+
|
|
1341
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1342
|
+
async function execute(args) {
|
|
1343
|
+
return new ReadableStream({
|
|
1344
|
+
async start(controller) {
|
|
1345
|
+
try {
|
|
1346
|
+
await streamEvents(controller, args);
|
|
1347
|
+
controller.close();
|
|
1348
|
+
} catch (err) {
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1335
1352
|
}
|
|
1336
|
-
__name(
|
|
1337
|
-
function
|
|
1338
|
-
const
|
|
1339
|
-
|
|
1353
|
+
__name(execute, "execute");
|
|
1354
|
+
async function streamEvents(controller, args) {
|
|
1355
|
+
const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
|
|
1356
|
+
let nodeName = initialNodeName;
|
|
1357
|
+
let state = initialState;
|
|
1358
|
+
const { name, assistantId: initialAssistantId } = agent;
|
|
1359
|
+
const client = new import_langgraph_sdk.Client({
|
|
1360
|
+
apiUrl: deploymentUrl,
|
|
1361
|
+
apiKey: langsmithApiKey
|
|
1362
|
+
});
|
|
1363
|
+
let initialThreadId = agrsInitialThreadId;
|
|
1364
|
+
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
1365
|
+
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
1366
|
+
initialThreadId = initialThreadId.substring(3);
|
|
1367
|
+
}
|
|
1368
|
+
const assistants = await client.assistants.search();
|
|
1369
|
+
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
1370
|
+
const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
|
|
1371
|
+
if (initialThreadId === threadId) {
|
|
1372
|
+
await client.threads.get(threadId);
|
|
1373
|
+
} else {
|
|
1374
|
+
await client.threads.create({
|
|
1375
|
+
threadId
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
let agentState = {
|
|
1379
|
+
values: {}
|
|
1340
1380
|
};
|
|
1341
|
-
if (
|
|
1342
|
-
|
|
1343
|
-
|
|
1381
|
+
if (wasInitiatedWithExistingThread) {
|
|
1382
|
+
agentState = await client.threads.getState(threadId);
|
|
1383
|
+
}
|
|
1384
|
+
const agentStateValues = agentState.values;
|
|
1385
|
+
state.messages = agentStateValues.messages;
|
|
1386
|
+
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
1387
|
+
state = langGraphDefaultMergeState(state, formatMessages(messages), actions, name);
|
|
1388
|
+
if (mode === "continue") {
|
|
1389
|
+
await client.threads.updateState(threadId, {
|
|
1390
|
+
values: state,
|
|
1391
|
+
asNode: nodeName
|
|
1344
1392
|
});
|
|
1345
|
-
|
|
1346
|
-
|
|
1393
|
+
}
|
|
1394
|
+
const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
|
|
1395
|
+
if (!assistantId) {
|
|
1396
|
+
console.error(`
|
|
1397
|
+
No agent found for the agent name specified in CopilotKit provider
|
|
1398
|
+
Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
|
|
1399
|
+
|
|
1400
|
+
|
|
1401
|
+
These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
|
|
1402
|
+
`);
|
|
1403
|
+
throw new Error("No agent id found");
|
|
1404
|
+
}
|
|
1405
|
+
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
1406
|
+
const streamInput = mode === "start" ? state : null;
|
|
1407
|
+
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
1408
|
+
let prevNodeName = null;
|
|
1409
|
+
let emitIntermediateStateUntilEnd = null;
|
|
1410
|
+
let shouldExit = null;
|
|
1411
|
+
let externalRunId = null;
|
|
1412
|
+
const streamResponse2 = client.runs.stream(threadId, assistantId, {
|
|
1413
|
+
input: streamInput,
|
|
1414
|
+
streamMode: [
|
|
1415
|
+
"events",
|
|
1416
|
+
"values"
|
|
1417
|
+
]
|
|
1418
|
+
});
|
|
1419
|
+
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
1420
|
+
let latestStateValues = {};
|
|
1421
|
+
for await (const chunk of streamResponse2) {
|
|
1422
|
+
if (![
|
|
1423
|
+
"events",
|
|
1424
|
+
"values",
|
|
1425
|
+
"error"
|
|
1426
|
+
].includes(chunk.event))
|
|
1427
|
+
continue;
|
|
1428
|
+
if (chunk.event === "error") {
|
|
1429
|
+
logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
|
|
1430
|
+
throw new Error(`Error event thrown: ${chunk.data.message}`);
|
|
1431
|
+
}
|
|
1432
|
+
if (chunk.event === "values") {
|
|
1433
|
+
latestStateValues = chunk.data;
|
|
1434
|
+
continue;
|
|
1435
|
+
}
|
|
1436
|
+
const event = chunk.data;
|
|
1437
|
+
const currentNodeName = event.name;
|
|
1438
|
+
const eventType = event.event;
|
|
1439
|
+
const runId = event.metadata.run_id;
|
|
1440
|
+
externalRunId = runId;
|
|
1441
|
+
const metadata = event.metadata;
|
|
1442
|
+
shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
|
|
1443
|
+
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
1444
|
+
const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
|
|
1445
|
+
const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
|
|
1446
|
+
const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
|
|
1447
|
+
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
1448
|
+
nodeName = currentNodeName;
|
|
1449
|
+
}
|
|
1450
|
+
if (!nodeName) {
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
if (forceEmitIntermediateState) {
|
|
1454
|
+
if (eventType === "on_chain_end") {
|
|
1455
|
+
state = event.data.output;
|
|
1456
|
+
emit(getStateSyncEvent({
|
|
1457
|
+
threadId,
|
|
1458
|
+
runId,
|
|
1459
|
+
agentName: agent.name,
|
|
1460
|
+
nodeName,
|
|
1461
|
+
state: event.data.output,
|
|
1462
|
+
running: true,
|
|
1463
|
+
active: true
|
|
1464
|
+
}));
|
|
1465
|
+
}
|
|
1466
|
+
continue;
|
|
1467
|
+
}
|
|
1468
|
+
if (manuallyEmitMessage) {
|
|
1469
|
+
if (eventType === "on_chain_end") {
|
|
1470
|
+
state = event.data.output;
|
|
1471
|
+
emit(JSON.stringify({
|
|
1472
|
+
event: "on_copilotkit_emit_message",
|
|
1473
|
+
message: event.data.output,
|
|
1474
|
+
messageId: (0, import_node_crypto.randomUUID)(),
|
|
1475
|
+
role: MessageRole.assistant
|
|
1476
|
+
}) + "\n");
|
|
1477
|
+
}
|
|
1478
|
+
continue;
|
|
1479
|
+
}
|
|
1480
|
+
if (manuallyEmitToolCall) {
|
|
1481
|
+
if (eventType === "on_chain_end") {
|
|
1482
|
+
state = event.data.output;
|
|
1483
|
+
emit(JSON.stringify({
|
|
1484
|
+
event: "on_copilotkit_emit_tool_call",
|
|
1485
|
+
name: event.data.output.name,
|
|
1486
|
+
args: event.data.output.args,
|
|
1487
|
+
id: event.data.output.id
|
|
1488
|
+
}) + "\n");
|
|
1489
|
+
}
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1493
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1494
|
+
}
|
|
1495
|
+
if (emitIntermediateState && eventType === "on_chat_model_start") {
|
|
1496
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1497
|
+
}
|
|
1498
|
+
let updatedState = latestStateValues;
|
|
1499
|
+
if (emitIntermediateState && eventType === "on_chat_model_stream") {
|
|
1500
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1501
|
+
}
|
|
1502
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1503
|
+
updatedState = {
|
|
1504
|
+
...updatedState,
|
|
1505
|
+
...streamingStateExtractor.extractState()
|
|
1506
|
+
};
|
|
1507
|
+
}
|
|
1508
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === "on_chain_end") {
|
|
1509
|
+
emitIntermediateStateUntilEnd = null;
|
|
1510
|
+
}
|
|
1511
|
+
const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
|
|
1512
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1513
|
+
state = updatedState;
|
|
1514
|
+
prevNodeName = nodeName;
|
|
1515
|
+
emit(getStateSyncEvent({
|
|
1516
|
+
threadId,
|
|
1517
|
+
runId,
|
|
1518
|
+
agentName: agent.name,
|
|
1519
|
+
nodeName,
|
|
1520
|
+
state,
|
|
1521
|
+
running: true,
|
|
1522
|
+
active: !exitingNode
|
|
1523
|
+
}));
|
|
1347
1524
|
}
|
|
1525
|
+
emit(JSON.stringify(event) + "\n");
|
|
1348
1526
|
}
|
|
1349
|
-
|
|
1527
|
+
state = await client.threads.getState(threadId);
|
|
1528
|
+
const isEndNode = state.next.length === 0;
|
|
1529
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1530
|
+
emit(getStateSyncEvent({
|
|
1531
|
+
threadId,
|
|
1532
|
+
runId: externalRunId,
|
|
1533
|
+
agentName: agent.name,
|
|
1534
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1535
|
+
state: state.values,
|
|
1536
|
+
running: !shouldExit,
|
|
1537
|
+
active: false
|
|
1538
|
+
}));
|
|
1539
|
+
return Promise.resolve();
|
|
1350
1540
|
}
|
|
1351
|
-
__name(
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1541
|
+
__name(streamEvents, "streamEvents");
|
|
1542
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1543
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1544
|
+
if (key !== "messages") {
|
|
1545
|
+
acc[key] = state[key];
|
|
1546
|
+
}
|
|
1547
|
+
return acc;
|
|
1548
|
+
}, {});
|
|
1549
|
+
return JSON.stringify({
|
|
1550
|
+
event: "on_copilotkit_state_sync",
|
|
1551
|
+
thread_id: threadId,
|
|
1552
|
+
run_id: runId,
|
|
1553
|
+
agent_name: agentName,
|
|
1554
|
+
node_name: nodeName,
|
|
1555
|
+
active,
|
|
1556
|
+
state: stateWithoutMessages,
|
|
1557
|
+
running,
|
|
1558
|
+
role: "assistant"
|
|
1559
|
+
}) + "\n";
|
|
1560
|
+
}
|
|
1561
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1562
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1563
|
+
emitIntermediateState;
|
|
1564
|
+
toolCallBuffer;
|
|
1565
|
+
currentToolCall;
|
|
1566
|
+
previouslyParsableState;
|
|
1567
|
+
constructor(emitIntermediateState) {
|
|
1568
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1569
|
+
this.toolCallBuffer = {};
|
|
1570
|
+
this.currentToolCall = null;
|
|
1571
|
+
this.previouslyParsableState = {};
|
|
1572
|
+
}
|
|
1573
|
+
bufferToolCalls(event) {
|
|
1574
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1575
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1576
|
+
if (chunk.name !== null) {
|
|
1577
|
+
this.currentToolCall = chunk.name;
|
|
1578
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1579
|
+
} else if (this.currentToolCall !== null) {
|
|
1580
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
getEmitStateConfig(currentToolName) {
|
|
1585
|
+
for (const config2 of this.emitIntermediateState) {
|
|
1586
|
+
const stateKey = config2["state_key"];
|
|
1587
|
+
const tool = config2["tool"];
|
|
1588
|
+
const toolArgument = config2["tool_argument"];
|
|
1589
|
+
if (currentToolName === tool) {
|
|
1590
|
+
return [
|
|
1591
|
+
toolArgument,
|
|
1592
|
+
stateKey
|
|
1593
|
+
];
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
return [
|
|
1597
|
+
null,
|
|
1598
|
+
null
|
|
1599
|
+
];
|
|
1600
|
+
}
|
|
1601
|
+
extractState() {
|
|
1602
|
+
const state = {};
|
|
1603
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1604
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1605
|
+
if (stateKey === null) {
|
|
1606
|
+
continue;
|
|
1607
|
+
}
|
|
1608
|
+
let parsedValue;
|
|
1609
|
+
try {
|
|
1610
|
+
parsedValue = (0, import_partial_json.parse)(value);
|
|
1611
|
+
} catch (error) {
|
|
1612
|
+
if (key in this.previouslyParsableState) {
|
|
1613
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1614
|
+
} else {
|
|
1615
|
+
continue;
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1618
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1619
|
+
if (!argumentName) {
|
|
1620
|
+
state[stateKey] = parsedValue;
|
|
1621
|
+
} else {
|
|
1622
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
return state;
|
|
1626
|
+
}
|
|
1627
|
+
}, "StreamingStateExtractor");
|
|
1628
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1629
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1630
|
+
messages = messages.slice(1);
|
|
1631
|
+
}
|
|
1632
|
+
const mergedMessages = state.messages || [];
|
|
1633
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1634
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1635
|
+
for (const message of mergedMessages) {
|
|
1636
|
+
if ("tool_call_id" in message) {
|
|
1637
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
for (const message of messages) {
|
|
1641
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1642
|
+
continue;
|
|
1643
|
+
}
|
|
1644
|
+
if ("name" in message && message.name === agentName) {
|
|
1645
|
+
continue;
|
|
1646
|
+
}
|
|
1647
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1648
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1649
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1650
|
+
continue;
|
|
1651
|
+
}
|
|
1652
|
+
mergedMessages.push(message);
|
|
1653
|
+
} else {
|
|
1654
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1655
|
+
if (mergedMessages[i].id === message.id) {
|
|
1656
|
+
if ("tool_calls" in message) {
|
|
1657
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1658
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1659
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
mergedMessages[i] = message;
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1668
|
+
const currentMessage = mergedMessages[i];
|
|
1669
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1670
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1671
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
const correctedMessages = [];
|
|
1675
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1676
|
+
const currentMessage = mergedMessages[i];
|
|
1677
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1678
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1679
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1680
|
+
if (!nextMessage) {
|
|
1681
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1682
|
+
continue;
|
|
1683
|
+
}
|
|
1684
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1685
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1686
|
+
if (toolMessage) {
|
|
1687
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1688
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1689
|
+
continue;
|
|
1690
|
+
} else {
|
|
1691
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1692
|
+
continue;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
correctedMessages.push(currentMessage);
|
|
1696
|
+
continue;
|
|
1697
|
+
}
|
|
1698
|
+
if ("tool_call_id" in currentMessage) {
|
|
1699
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1700
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1701
|
+
continue;
|
|
1702
|
+
}
|
|
1703
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1704
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1705
|
+
continue;
|
|
1706
|
+
}
|
|
1707
|
+
correctedMessages.push(currentMessage);
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
correctedMessages.push(currentMessage);
|
|
1711
|
+
}
|
|
1712
|
+
return deepMerge(state, {
|
|
1713
|
+
messages: correctedMessages,
|
|
1714
|
+
copilotkit: {
|
|
1715
|
+
actions
|
|
1716
|
+
}
|
|
1717
|
+
});
|
|
1718
|
+
}
|
|
1719
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1720
|
+
function deepMerge(obj1, obj2) {
|
|
1721
|
+
let result = {
|
|
1722
|
+
...obj1
|
|
1723
|
+
};
|
|
1724
|
+
for (let key in obj2) {
|
|
1725
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
1726
|
+
if (obj1[key]) {
|
|
1727
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
1728
|
+
} else {
|
|
1729
|
+
result[key] = {
|
|
1730
|
+
...obj2[key]
|
|
1731
|
+
};
|
|
1732
|
+
}
|
|
1733
|
+
} else {
|
|
1734
|
+
result[key] = obj2[key];
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
return result;
|
|
1738
|
+
}
|
|
1739
|
+
__name(deepMerge, "deepMerge");
|
|
1740
|
+
function formatMessages(messages) {
|
|
1741
|
+
return messages.map((message) => {
|
|
1742
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
1743
|
+
return message;
|
|
1744
|
+
}
|
|
1745
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
1746
|
+
return message;
|
|
1747
|
+
}
|
|
1748
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
1749
|
+
return message;
|
|
1750
|
+
}
|
|
1751
|
+
if (message.isActionExecutionMessage()) {
|
|
1752
|
+
const toolCall = {
|
|
1753
|
+
name: message.name,
|
|
1754
|
+
args: message.arguments,
|
|
1755
|
+
id: message.id
|
|
1756
|
+
};
|
|
1372
1757
|
return {
|
|
1373
|
-
|
|
1374
|
-
|
|
1758
|
+
type: message.type,
|
|
1759
|
+
content: "",
|
|
1760
|
+
tool_calls: [
|
|
1761
|
+
toolCall
|
|
1762
|
+
],
|
|
1763
|
+
role: MessageRole.assistant,
|
|
1764
|
+
id: message.id
|
|
1375
1765
|
};
|
|
1376
1766
|
}
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
};
|
|
1390
|
-
}
|
|
1767
|
+
if (message.isResultMessage()) {
|
|
1768
|
+
return {
|
|
1769
|
+
type: message.type,
|
|
1770
|
+
content: message.result,
|
|
1771
|
+
id: message.id,
|
|
1772
|
+
tool_call_id: message.actionExecutionId,
|
|
1773
|
+
name: message.actionName,
|
|
1774
|
+
role: MessageRole.assistant
|
|
1775
|
+
};
|
|
1776
|
+
}
|
|
1777
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
1778
|
+
});
|
|
1391
1779
|
}
|
|
1392
|
-
__name(
|
|
1780
|
+
__name(formatMessages, "formatMessages");
|
|
1781
|
+
|
|
1782
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
1783
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1784
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
1785
|
+
name: agent.name,
|
|
1786
|
+
description: agent.description,
|
|
1787
|
+
parameters: [],
|
|
1788
|
+
handler: async (_args) => {
|
|
1789
|
+
},
|
|
1790
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
1791
|
+
var _a;
|
|
1792
|
+
logger2.debug({
|
|
1793
|
+
actionName: agent.name
|
|
1794
|
+
}, "Executing LangGraph Cloud agent");
|
|
1795
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1796
|
+
let state = {};
|
|
1797
|
+
if (agentStates) {
|
|
1798
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1799
|
+
if (jsonState) {
|
|
1800
|
+
state = JSON.parse(jsonState);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
try {
|
|
1804
|
+
const response = await execute({
|
|
1805
|
+
logger: logger2,
|
|
1806
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
1807
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1808
|
+
agent,
|
|
1809
|
+
threadId,
|
|
1810
|
+
nodeName,
|
|
1811
|
+
messages,
|
|
1812
|
+
state,
|
|
1813
|
+
properties: graphqlContext.properties,
|
|
1814
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1815
|
+
name: action.name,
|
|
1816
|
+
description: action.description,
|
|
1817
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
1818
|
+
}))
|
|
1819
|
+
});
|
|
1820
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
1821
|
+
streamResponse(response, eventSource.eventStream$);
|
|
1822
|
+
return eventSource.processLangGraphEvents();
|
|
1823
|
+
} catch (error) {
|
|
1824
|
+
logger2.error({
|
|
1825
|
+
url: endpoint.deploymentUrl,
|
|
1826
|
+
status: 500,
|
|
1827
|
+
body: error.message
|
|
1828
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
1829
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}));
|
|
1833
|
+
return [
|
|
1834
|
+
...agents
|
|
1835
|
+
];
|
|
1836
|
+
}
|
|
1837
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1393
1838
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1394
1839
|
const actions = json["actions"].map((action) => ({
|
|
1395
1840
|
name: action.name,
|
|
@@ -1481,7 +1926,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1481
1926
|
throw new Error("Failed to execute remote agent");
|
|
1482
1927
|
}
|
|
1483
1928
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1484
|
-
|
|
1929
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1485
1930
|
return eventSource.processLangGraphEvents();
|
|
1486
1931
|
}
|
|
1487
1932
|
}));
|
|
@@ -1491,34 +1936,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1491
1936
|
];
|
|
1492
1937
|
}
|
|
1493
1938
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1494
|
-
async function
|
|
1939
|
+
async function streamResponse(response, eventStream$) {
|
|
1940
|
+
const reader = response.getReader();
|
|
1941
|
+
const decoder = new TextDecoder();
|
|
1942
|
+
let buffer = [];
|
|
1943
|
+
function flushBuffer() {
|
|
1944
|
+
const currentBuffer = buffer.join("");
|
|
1945
|
+
if (currentBuffer.trim().length === 0) {
|
|
1946
|
+
return;
|
|
1947
|
+
}
|
|
1948
|
+
const parts = currentBuffer.split("\n");
|
|
1949
|
+
if (parts.length === 0) {
|
|
1950
|
+
return;
|
|
1951
|
+
}
|
|
1952
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1953
|
+
buffer = [];
|
|
1954
|
+
if (!lastPartIsComplete) {
|
|
1955
|
+
buffer.push(parts.pop());
|
|
1956
|
+
}
|
|
1957
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1958
|
+
eventStream$.next(JSON.parse(part));
|
|
1959
|
+
});
|
|
1960
|
+
}
|
|
1961
|
+
__name(flushBuffer, "flushBuffer");
|
|
1962
|
+
try {
|
|
1963
|
+
while (true) {
|
|
1964
|
+
const { done, value } = await reader.read();
|
|
1965
|
+
if (!done) {
|
|
1966
|
+
buffer.push(decoder.decode(value, {
|
|
1967
|
+
stream: true
|
|
1968
|
+
}));
|
|
1969
|
+
}
|
|
1970
|
+
flushBuffer();
|
|
1971
|
+
if (done) {
|
|
1972
|
+
break;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
} catch (error) {
|
|
1976
|
+
console.error("Error in stream", error);
|
|
1977
|
+
eventStream$.error(error);
|
|
1978
|
+
return;
|
|
1979
|
+
}
|
|
1980
|
+
eventStream$.complete();
|
|
1981
|
+
}
|
|
1982
|
+
__name(streamResponse, "streamResponse");
|
|
1983
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1984
|
+
const headers = {
|
|
1985
|
+
"Content-Type": "application/json"
|
|
1986
|
+
};
|
|
1987
|
+
if (onBeforeRequest) {
|
|
1988
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1989
|
+
ctx: graphqlContext
|
|
1990
|
+
});
|
|
1991
|
+
if (additionalHeaders) {
|
|
1992
|
+
Object.assign(headers, additionalHeaders);
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
return headers;
|
|
1996
|
+
}
|
|
1997
|
+
__name(createHeaders, "createHeaders");
|
|
1998
|
+
|
|
1999
|
+
// src/lib/runtime/remote-actions.ts
|
|
2000
|
+
var EndpointType;
|
|
2001
|
+
(function(EndpointType2) {
|
|
2002
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
2003
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
2004
|
+
})(EndpointType || (EndpointType = {}));
|
|
2005
|
+
function isLangGraphAgentAction(action) {
|
|
2006
|
+
if (!action) {
|
|
2007
|
+
return false;
|
|
2008
|
+
}
|
|
2009
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
2010
|
+
}
|
|
2011
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
2012
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
2013
|
+
logger2.debug({
|
|
2014
|
+
url
|
|
2015
|
+
}, "Fetching actions from url");
|
|
2016
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2017
|
+
try {
|
|
2018
|
+
const response = await fetch(`${url}/info`, {
|
|
2019
|
+
method: "POST",
|
|
2020
|
+
headers,
|
|
2021
|
+
body: JSON.stringify({
|
|
2022
|
+
properties: graphqlContext.properties,
|
|
2023
|
+
frontendUrl
|
|
2024
|
+
})
|
|
2025
|
+
});
|
|
2026
|
+
if (!response.ok) {
|
|
2027
|
+
logger2.error({
|
|
2028
|
+
url,
|
|
2029
|
+
status: response.status,
|
|
2030
|
+
body: await response.text()
|
|
2031
|
+
}, "Failed to fetch actions from url");
|
|
2032
|
+
return {
|
|
2033
|
+
actions: [],
|
|
2034
|
+
agents: []
|
|
2035
|
+
};
|
|
2036
|
+
}
|
|
2037
|
+
const json = await response.json();
|
|
2038
|
+
logger2.debug({
|
|
2039
|
+
json
|
|
2040
|
+
}, "Fetched actions from url");
|
|
2041
|
+
return json;
|
|
2042
|
+
} catch (error) {
|
|
2043
|
+
logger2.error({
|
|
2044
|
+
error: error.message ? error.message : error + ""
|
|
2045
|
+
}, "Failed to fetch actions from url");
|
|
2046
|
+
return {
|
|
2047
|
+
actions: [],
|
|
2048
|
+
agents: []
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
2053
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1495
2054
|
const logger2 = graphqlContext.logger.child({
|
|
1496
2055
|
component: "remote-actions.fetchRemoteActions"
|
|
1497
2056
|
});
|
|
1498
2057
|
logger2.debug({
|
|
1499
|
-
|
|
1500
|
-
}, "Fetching remote
|
|
1501
|
-
const filtered =
|
|
1502
|
-
|
|
2058
|
+
remoteEndpointDefinitions
|
|
2059
|
+
}, "Fetching from remote endpoints");
|
|
2060
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
2061
|
+
if (value.type === "langgraph-cloud") {
|
|
2062
|
+
return value;
|
|
2063
|
+
}
|
|
2064
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
2065
|
+
});
|
|
2066
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
2067
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
2068
|
+
return constructLGCRemoteAction({
|
|
2069
|
+
endpoint,
|
|
2070
|
+
messages,
|
|
2071
|
+
graphqlContext,
|
|
2072
|
+
logger: logger2.child({
|
|
2073
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
2074
|
+
endpoint
|
|
2075
|
+
}),
|
|
2076
|
+
agentStates
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
1503
2079
|
const json = await fetchRemoteInfo({
|
|
1504
|
-
url:
|
|
1505
|
-
onBeforeRequest:
|
|
2080
|
+
url: endpoint.url,
|
|
2081
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1506
2082
|
graphqlContext,
|
|
1507
2083
|
logger: logger2.child({
|
|
1508
2084
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1509
|
-
|
|
2085
|
+
endpoint
|
|
1510
2086
|
}),
|
|
1511
2087
|
frontendUrl
|
|
1512
2088
|
});
|
|
1513
2089
|
return constructRemoteActions({
|
|
1514
2090
|
json,
|
|
1515
2091
|
messages,
|
|
1516
|
-
url:
|
|
1517
|
-
onBeforeRequest:
|
|
2092
|
+
url: endpoint.url,
|
|
2093
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1518
2094
|
graphqlContext,
|
|
1519
2095
|
logger: logger2.child({
|
|
1520
2096
|
component: "remote-actions.constructActions",
|
|
1521
|
-
|
|
2097
|
+
endpoint
|
|
1522
2098
|
}),
|
|
1523
2099
|
agentStates
|
|
1524
2100
|
});
|
|
@@ -1716,7 +2292,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
1716
2292
|
__name(executeAction, "executeAction");
|
|
1717
2293
|
|
|
1718
2294
|
// src/graphql/types/base/index.ts
|
|
1719
|
-
var
|
|
2295
|
+
var import_type_graphql2 = require("type-graphql");
|
|
1720
2296
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1721
2297
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1722
2298
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -1739,15 +2315,15 @@ var BaseMessageInput = class {
|
|
|
1739
2315
|
};
|
|
1740
2316
|
__name(BaseMessageInput, "BaseMessageInput");
|
|
1741
2317
|
_ts_decorate([
|
|
1742
|
-
(0,
|
|
2318
|
+
(0, import_type_graphql2.Field)(() => String),
|
|
1743
2319
|
_ts_metadata("design:type", String)
|
|
1744
2320
|
], BaseMessageInput.prototype, "id", void 0);
|
|
1745
2321
|
_ts_decorate([
|
|
1746
|
-
(0,
|
|
2322
|
+
(0, import_type_graphql2.Field)(() => Date),
|
|
1747
2323
|
_ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
|
|
1748
2324
|
], BaseMessageInput.prototype, "createdAt", void 0);
|
|
1749
2325
|
BaseMessageInput = _ts_decorate([
|
|
1750
|
-
(0,
|
|
2326
|
+
(0, import_type_graphql2.InputType)()
|
|
1751
2327
|
], BaseMessageInput);
|
|
1752
2328
|
|
|
1753
2329
|
// src/graphql/types/converted/index.ts
|
|
@@ -1851,7 +2427,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
|
1851
2427
|
var import_rxjs3 = require("rxjs");
|
|
1852
2428
|
var CopilotRuntime = class {
|
|
1853
2429
|
actions;
|
|
1854
|
-
|
|
2430
|
+
remoteEndpointDefinitions;
|
|
1855
2431
|
langserve = [];
|
|
1856
2432
|
onBeforeRequest;
|
|
1857
2433
|
onAfterRequest;
|
|
@@ -1862,7 +2438,7 @@ var CopilotRuntime = class {
|
|
|
1862
2438
|
const remoteChain = new RemoteChain(chain);
|
|
1863
2439
|
this.langserve.push(remoteChain.toAction());
|
|
1864
2440
|
}
|
|
1865
|
-
this.
|
|
2441
|
+
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
|
|
1866
2442
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
1867
2443
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
1868
2444
|
}
|
|
@@ -1931,7 +2507,7 @@ var CopilotRuntime = class {
|
|
|
1931
2507
|
async processAgentRequest(request) {
|
|
1932
2508
|
var _a;
|
|
1933
2509
|
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
1934
|
-
const { threadId
|
|
2510
|
+
const { threadId, agentName, nodeName } = agentSession;
|
|
1935
2511
|
const serverSideActions = await this.getServerSideActions(request);
|
|
1936
2512
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
1937
2513
|
const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
|
|
@@ -2003,8 +2579,12 @@ var CopilotRuntime = class {
|
|
|
2003
2579
|
console.error("Error loading langserve chain:", error);
|
|
2004
2580
|
}
|
|
2005
2581
|
}
|
|
2582
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
2583
|
+
...endpoint,
|
|
2584
|
+
type: this.resolveEndpointType(endpoint)
|
|
2585
|
+
}));
|
|
2006
2586
|
const remoteActions = await setupRemoteActions({
|
|
2007
|
-
|
|
2587
|
+
remoteEndpointDefinitions,
|
|
2008
2588
|
graphqlContext,
|
|
2009
2589
|
messages: inputMessages,
|
|
2010
2590
|
agentStates,
|
|
@@ -2020,6 +2600,12 @@ var CopilotRuntime = class {
|
|
|
2020
2600
|
...remoteActions
|
|
2021
2601
|
];
|
|
2022
2602
|
}
|
|
2603
|
+
resolveEndpointType(endpoint) {
|
|
2604
|
+
if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
2605
|
+
return EndpointType.LangGraphCloud;
|
|
2606
|
+
}
|
|
2607
|
+
return endpoint.type;
|
|
2608
|
+
}
|
|
2023
2609
|
};
|
|
2024
2610
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
2025
2611
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -2034,6 +2620,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
|
2034
2620
|
return allTools;
|
|
2035
2621
|
}
|
|
2036
2622
|
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
2623
|
+
function copilotKitEndpoint(config2) {
|
|
2624
|
+
return {
|
|
2625
|
+
...config2,
|
|
2626
|
+
type: EndpointType.CopilotKit
|
|
2627
|
+
};
|
|
2628
|
+
}
|
|
2629
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
2630
|
+
function langGraphCloudEndpoint(config2) {
|
|
2631
|
+
return {
|
|
2632
|
+
...config2,
|
|
2633
|
+
type: EndpointType.LangGraphCloud
|
|
2634
|
+
};
|
|
2635
|
+
}
|
|
2636
|
+
__name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
|
|
2037
2637
|
|
|
2038
2638
|
// src/lib/integrations/shared.ts
|
|
2039
2639
|
var import_type_graphql16 = require("type-graphql");
|
|
@@ -2047,43 +2647,6 @@ var import_type_graphql11 = require("type-graphql");
|
|
|
2047
2647
|
|
|
2048
2648
|
// src/graphql/inputs/message.input.ts
|
|
2049
2649
|
var import_type_graphql3 = require("type-graphql");
|
|
2050
|
-
|
|
2051
|
-
// src/graphql/types/enums.ts
|
|
2052
|
-
var import_type_graphql2 = require("type-graphql");
|
|
2053
|
-
var MessageRole;
|
|
2054
|
-
(function(MessageRole2) {
|
|
2055
|
-
MessageRole2["user"] = "user";
|
|
2056
|
-
MessageRole2["assistant"] = "assistant";
|
|
2057
|
-
MessageRole2["system"] = "system";
|
|
2058
|
-
})(MessageRole || (MessageRole = {}));
|
|
2059
|
-
var ActionExecutionScope;
|
|
2060
|
-
(function(ActionExecutionScope2) {
|
|
2061
|
-
ActionExecutionScope2["server"] = "server";
|
|
2062
|
-
ActionExecutionScope2["client"] = "client";
|
|
2063
|
-
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
2064
|
-
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
2065
|
-
var CopilotRequestType;
|
|
2066
|
-
(function(CopilotRequestType2) {
|
|
2067
|
-
CopilotRequestType2["Chat"] = "Chat";
|
|
2068
|
-
CopilotRequestType2["Task"] = "Task";
|
|
2069
|
-
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
2070
|
-
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
2071
|
-
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
2072
|
-
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
2073
|
-
(0, import_type_graphql2.registerEnumType)(MessageRole, {
|
|
2074
|
-
name: "MessageRole",
|
|
2075
|
-
description: "The role of the message"
|
|
2076
|
-
});
|
|
2077
|
-
(0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
|
|
2078
|
-
name: "ActionExecutionScope",
|
|
2079
|
-
description: "The scope of the action"
|
|
2080
|
-
});
|
|
2081
|
-
(0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
|
|
2082
|
-
name: "CopilotRequestType",
|
|
2083
|
-
description: "The type of Copilot request"
|
|
2084
|
-
});
|
|
2085
|
-
|
|
2086
|
-
// src/graphql/inputs/message.input.ts
|
|
2087
2650
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
2088
2651
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2089
2652
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -3740,6 +4303,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3740
4303
|
UnifyAdapter,
|
|
3741
4304
|
buildSchema,
|
|
3742
4305
|
config,
|
|
4306
|
+
copilotKitEndpoint,
|
|
3743
4307
|
copilotRuntimeNestEndpoint,
|
|
3744
4308
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
3745
4309
|
copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -3747,6 +4311,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3747
4311
|
copilotRuntimeNodeHttpEndpoint,
|
|
3748
4312
|
createContext,
|
|
3749
4313
|
flattenToolCallsNoDuplicates,
|
|
3750
|
-
getCommonConfig
|
|
4314
|
+
getCommonConfig,
|
|
4315
|
+
langGraphCloudEndpoint
|
|
3751
4316
|
});
|
|
3752
4317
|
//# sourceMappingURL=index.js.map
|