@copilotkit/runtime 1.3.16-mme-azure-openai.0 → 1.3.16-mme-sdk-js.1
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 -3
- package/__snapshots__/schema/schema.graphql +1 -0
- package/dist/{chunk-MKZBH3Y6.mjs → chunk-2LR3IZ2Q.mjs} +2 -2
- package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
- package/dist/chunk-B74M7FXG.mjs.map +1 -0
- package/dist/{chunk-MVG266E4.mjs → chunk-BTAHXIKM.mjs} +37 -10
- package/dist/chunk-BTAHXIKM.mjs.map +1 -0
- package/dist/{chunk-CRSCH25P.mjs → chunk-CVLU3KWZ.mjs} +2 -2
- package/dist/{chunk-S5SYJ6JC.mjs → chunk-L23AVL2R.mjs} +2 -2
- package/dist/{chunk-YVZORQSA.mjs → chunk-VQXA3N5D.mjs} +675 -151
- package/dist/chunk-VQXA3N5D.mjs.map +1 -0
- 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 +747 -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 +747 -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 +614 -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-MKZBH3Y6.mjs.map → chunk-2LR3IZ2Q.mjs.map} +0 -0
- /package/dist/{chunk-CRSCH25P.mjs.map → chunk-CVLU3KWZ.mjs.map} +0 -0
- /package/dist/{chunk-S5SYJ6JC.mjs.map → chunk-L23AVL2R.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.3.16-mme-
|
|
47
|
+
version: "1.3.16-mme-sdk-js.1",
|
|
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
|
|
|
@@ -420,9 +424,6 @@ var OpenAIAdapter = class {
|
|
|
420
424
|
var _a, _b;
|
|
421
425
|
let mode = null;
|
|
422
426
|
for await (const chunk of stream) {
|
|
423
|
-
if (chunk.choices.length === 0) {
|
|
424
|
-
continue;
|
|
425
|
-
}
|
|
426
427
|
const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
|
|
427
428
|
const content = chunk.choices[0].delta.content;
|
|
428
429
|
if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
|
|
@@ -1083,58 +1084,20 @@ var LangGraphEventTypes;
|
|
|
1083
1084
|
LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
|
|
1084
1085
|
LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
|
|
1085
1086
|
LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
|
|
1086
|
-
LangGraphEventTypes2["
|
|
1087
|
-
LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
|
|
1087
|
+
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
|
|
1088
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
|
+
CustomEventNames2["CopilotKitExit"] = "copilotkit_exit";
|
|
1095
|
+
})(CustomEventNames || (CustomEventNames = {}));
|
|
1089
1096
|
|
|
1090
1097
|
// src/agents/langgraph/event-source.ts
|
|
1091
1098
|
var import_shared7 = require("@copilotkit/shared");
|
|
1092
1099
|
var RemoteLangGraphEventSource = class {
|
|
1093
1100
|
eventStream$ = new import_rxjs.ReplaySubject();
|
|
1094
|
-
async streamResponse(response) {
|
|
1095
|
-
const reader = response.body.getReader();
|
|
1096
|
-
const decoder = new TextDecoder();
|
|
1097
|
-
let buffer = [];
|
|
1098
|
-
const eventStream$ = this.eventStream$;
|
|
1099
|
-
function flushBuffer() {
|
|
1100
|
-
const currentBuffer = buffer.join("");
|
|
1101
|
-
if (currentBuffer.trim().length === 0) {
|
|
1102
|
-
return;
|
|
1103
|
-
}
|
|
1104
|
-
const parts = currentBuffer.split("\n");
|
|
1105
|
-
if (parts.length === 0) {
|
|
1106
|
-
return;
|
|
1107
|
-
}
|
|
1108
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1109
|
-
buffer = [];
|
|
1110
|
-
if (!lastPartIsComplete) {
|
|
1111
|
-
buffer.push(parts.pop());
|
|
1112
|
-
}
|
|
1113
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1114
|
-
eventStream$.next(JSON.parse(part));
|
|
1115
|
-
});
|
|
1116
|
-
}
|
|
1117
|
-
__name(flushBuffer, "flushBuffer");
|
|
1118
|
-
try {
|
|
1119
|
-
while (true) {
|
|
1120
|
-
const { done, value } = await reader.read();
|
|
1121
|
-
if (!done) {
|
|
1122
|
-
buffer.push(decoder.decode(value, {
|
|
1123
|
-
stream: true
|
|
1124
|
-
}));
|
|
1125
|
-
}
|
|
1126
|
-
flushBuffer();
|
|
1127
|
-
if (done) {
|
|
1128
|
-
break;
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
} catch (error) {
|
|
1132
|
-
console.error("Error in stream", error);
|
|
1133
|
-
eventStream$.error(error);
|
|
1134
|
-
return;
|
|
1135
|
-
}
|
|
1136
|
-
eventStream$.complete();
|
|
1137
|
-
}
|
|
1138
1101
|
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
1139
1102
|
if (typeof shouldEmitToolCalls === "boolean") {
|
|
1140
1103
|
return shouldEmitToolCalls;
|
|
@@ -1157,20 +1120,25 @@ var RemoteLangGraphEventSource = class {
|
|
|
1157
1120
|
} else {
|
|
1158
1121
|
acc.content = null;
|
|
1159
1122
|
}
|
|
1160
|
-
|
|
1123
|
+
const toolCallChunks = (
|
|
1124
|
+
// @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
|
|
1125
|
+
((_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)
|
|
1126
|
+
);
|
|
1127
|
+
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);
|
|
1128
|
+
if (toolCallChunks && toolCallChunks.length > 0) {
|
|
1161
1129
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1162
|
-
acc.toolCallMessageId =
|
|
1163
|
-
if ((
|
|
1164
|
-
acc.toolCallName =
|
|
1130
|
+
acc.toolCallMessageId = toolCallMessageId;
|
|
1131
|
+
if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
|
|
1132
|
+
acc.toolCallName = toolCallChunks[0].name;
|
|
1165
1133
|
}
|
|
1166
|
-
if ((
|
|
1167
|
-
acc.toolCallId =
|
|
1134
|
+
if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
|
|
1135
|
+
acc.toolCallId = toolCallChunks[0].id;
|
|
1168
1136
|
}
|
|
1169
1137
|
acc.prevMessageId = acc.messageId;
|
|
1170
|
-
acc.messageId =
|
|
1138
|
+
acc.messageId = toolCallMessageId;
|
|
1171
1139
|
} else if (acc.content && acc.content != "") {
|
|
1172
1140
|
acc.prevMessageId = acc.messageId;
|
|
1173
|
-
acc.messageId =
|
|
1141
|
+
acc.messageId = toolCallMessageId;
|
|
1174
1142
|
} else {
|
|
1175
1143
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1176
1144
|
acc.prevMessageId = acc.messageId;
|
|
@@ -1195,7 +1163,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1195
1163
|
prevMessageId: null,
|
|
1196
1164
|
content: null
|
|
1197
1165
|
}), (0, import_rxjs.mergeMap)((eventWithState) => {
|
|
1198
|
-
var _a, _b, _c, _d, _e;
|
|
1166
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1199
1167
|
const events = [];
|
|
1200
1168
|
let shouldEmitMessages = true;
|
|
1201
1169
|
let shouldEmitToolCalls = false;
|
|
@@ -1218,32 +1186,33 @@ var RemoteLangGraphEventSource = class {
|
|
|
1218
1186
|
});
|
|
1219
1187
|
}
|
|
1220
1188
|
switch (eventWithState.event.event) {
|
|
1221
|
-
case LangGraphEventTypes.
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1189
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
1190
|
+
if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
1191
|
+
events.push({
|
|
1192
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
1193
|
+
messageId: eventWithState.event.data.message_id
|
|
1194
|
+
});
|
|
1195
|
+
events.push({
|
|
1196
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
1197
|
+
content: eventWithState.event.data.message
|
|
1198
|
+
});
|
|
1199
|
+
events.push({
|
|
1200
|
+
type: RuntimeEventTypes.TextMessageEnd
|
|
1201
|
+
});
|
|
1202
|
+
} else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
1203
|
+
events.push({
|
|
1204
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1205
|
+
actionExecutionId: eventWithState.event.data.id,
|
|
1206
|
+
actionName: eventWithState.event.data.name
|
|
1207
|
+
});
|
|
1208
|
+
events.push({
|
|
1209
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1210
|
+
args: JSON.stringify(eventWithState.event.data.args)
|
|
1211
|
+
});
|
|
1212
|
+
events.push({
|
|
1213
|
+
type: RuntimeEventTypes.ActionExecutionEnd
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1247
1216
|
break;
|
|
1248
1217
|
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
1249
1218
|
events.push({
|
|
@@ -1278,7 +1247,8 @@ var RemoteLangGraphEventSource = class {
|
|
|
1278
1247
|
});
|
|
1279
1248
|
}
|
|
1280
1249
|
}
|
|
1281
|
-
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
|
|
1250
|
+
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
|
|
1251
|
+
((_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);
|
|
1282
1252
|
const content = eventWithState.content;
|
|
1283
1253
|
if (args) {
|
|
1284
1254
|
if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
|
|
@@ -1329,70 +1299,528 @@ var RemoteLangGraphEventSource = class {
|
|
|
1329
1299
|
};
|
|
1330
1300
|
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
1331
1301
|
|
|
1332
|
-
// src/lib/runtime/remote-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1302
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1303
|
+
var import_langgraph_sdk = require("@langchain/langgraph-sdk");
|
|
1304
|
+
var import_node_crypto = require("crypto");
|
|
1305
|
+
var import_partial_json = require("partial-json");
|
|
1306
|
+
|
|
1307
|
+
// src/graphql/types/enums.ts
|
|
1308
|
+
var import_type_graphql = require("type-graphql");
|
|
1309
|
+
var MessageRole;
|
|
1310
|
+
(function(MessageRole2) {
|
|
1311
|
+
MessageRole2["user"] = "user";
|
|
1312
|
+
MessageRole2["assistant"] = "assistant";
|
|
1313
|
+
MessageRole2["system"] = "system";
|
|
1314
|
+
MessageRole2["tool"] = "tool";
|
|
1315
|
+
})(MessageRole || (MessageRole = {}));
|
|
1316
|
+
var ActionExecutionScope;
|
|
1317
|
+
(function(ActionExecutionScope2) {
|
|
1318
|
+
ActionExecutionScope2["server"] = "server";
|
|
1319
|
+
ActionExecutionScope2["client"] = "client";
|
|
1320
|
+
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
1321
|
+
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
1322
|
+
var CopilotRequestType;
|
|
1323
|
+
(function(CopilotRequestType2) {
|
|
1324
|
+
CopilotRequestType2["Chat"] = "Chat";
|
|
1325
|
+
CopilotRequestType2["Task"] = "Task";
|
|
1326
|
+
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
1327
|
+
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
1328
|
+
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
1329
|
+
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
1330
|
+
(0, import_type_graphql.registerEnumType)(MessageRole, {
|
|
1331
|
+
name: "MessageRole",
|
|
1332
|
+
description: "The role of the message"
|
|
1333
|
+
});
|
|
1334
|
+
(0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
|
|
1335
|
+
name: "ActionExecutionScope",
|
|
1336
|
+
description: "The scope of the action"
|
|
1337
|
+
});
|
|
1338
|
+
(0, import_type_graphql.registerEnumType)(CopilotRequestType, {
|
|
1339
|
+
name: "CopilotRequestType",
|
|
1340
|
+
description: "The type of Copilot request"
|
|
1341
|
+
});
|
|
1342
|
+
|
|
1343
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1344
|
+
async function execute(args) {
|
|
1345
|
+
return new ReadableStream({
|
|
1346
|
+
async start(controller) {
|
|
1347
|
+
try {
|
|
1348
|
+
await streamEvents(controller, args);
|
|
1349
|
+
controller.close();
|
|
1350
|
+
} catch (err) {
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
});
|
|
1338
1354
|
}
|
|
1339
|
-
__name(
|
|
1340
|
-
function
|
|
1341
|
-
const
|
|
1342
|
-
|
|
1355
|
+
__name(execute, "execute");
|
|
1356
|
+
async function streamEvents(controller, args) {
|
|
1357
|
+
const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
|
|
1358
|
+
let nodeName = initialNodeName;
|
|
1359
|
+
let state = initialState;
|
|
1360
|
+
const { name, assistantId: initialAssistantId } = agent;
|
|
1361
|
+
const client = new import_langgraph_sdk.Client({
|
|
1362
|
+
apiUrl: deploymentUrl,
|
|
1363
|
+
apiKey: langsmithApiKey
|
|
1364
|
+
});
|
|
1365
|
+
let initialThreadId = agrsInitialThreadId;
|
|
1366
|
+
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
1367
|
+
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
1368
|
+
initialThreadId = initialThreadId.substring(3);
|
|
1369
|
+
}
|
|
1370
|
+
const assistants = await client.assistants.search();
|
|
1371
|
+
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
1372
|
+
const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
|
|
1373
|
+
if (initialThreadId === threadId) {
|
|
1374
|
+
await client.threads.get(threadId);
|
|
1375
|
+
} else {
|
|
1376
|
+
await client.threads.create({
|
|
1377
|
+
threadId
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
let agentState = {
|
|
1381
|
+
values: {}
|
|
1343
1382
|
};
|
|
1344
|
-
if (
|
|
1345
|
-
|
|
1346
|
-
|
|
1383
|
+
if (wasInitiatedWithExistingThread) {
|
|
1384
|
+
agentState = await client.threads.getState(threadId);
|
|
1385
|
+
}
|
|
1386
|
+
const agentStateValues = agentState.values;
|
|
1387
|
+
state.messages = agentStateValues.messages;
|
|
1388
|
+
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
1389
|
+
let formattedMessages = [];
|
|
1390
|
+
try {
|
|
1391
|
+
formattedMessages = formatMessages(messages);
|
|
1392
|
+
} catch (e) {
|
|
1393
|
+
logger2.error(e, `Error event thrown: ${e.message}`);
|
|
1394
|
+
}
|
|
1395
|
+
state = langGraphDefaultMergeState(state, formattedMessages, actions, name);
|
|
1396
|
+
if (mode === "continue") {
|
|
1397
|
+
await client.threads.updateState(threadId, {
|
|
1398
|
+
values: state,
|
|
1399
|
+
asNode: nodeName
|
|
1347
1400
|
});
|
|
1348
|
-
|
|
1349
|
-
|
|
1401
|
+
}
|
|
1402
|
+
const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
|
|
1403
|
+
if (!assistantId) {
|
|
1404
|
+
console.error(`
|
|
1405
|
+
No agent found for the agent name specified in CopilotKit provider
|
|
1406
|
+
Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
|
|
1407
|
+
|
|
1408
|
+
|
|
1409
|
+
These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
|
|
1410
|
+
`);
|
|
1411
|
+
throw new Error("No agent id found");
|
|
1412
|
+
}
|
|
1413
|
+
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
1414
|
+
const streamInput = mode === "start" ? state : null;
|
|
1415
|
+
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
1416
|
+
let prevNodeName = null;
|
|
1417
|
+
let emitIntermediateStateUntilEnd = null;
|
|
1418
|
+
let shouldExit = null;
|
|
1419
|
+
let externalRunId = null;
|
|
1420
|
+
const streamResponse2 = client.runs.stream(threadId, assistantId, {
|
|
1421
|
+
input: streamInput,
|
|
1422
|
+
streamMode: [
|
|
1423
|
+
"events",
|
|
1424
|
+
"values"
|
|
1425
|
+
]
|
|
1426
|
+
});
|
|
1427
|
+
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
1428
|
+
let latestStateValues = {};
|
|
1429
|
+
try {
|
|
1430
|
+
for await (const chunk of streamResponse2) {
|
|
1431
|
+
if (![
|
|
1432
|
+
"events",
|
|
1433
|
+
"values",
|
|
1434
|
+
"error"
|
|
1435
|
+
].includes(chunk.event))
|
|
1436
|
+
continue;
|
|
1437
|
+
if (chunk.event === "error") {
|
|
1438
|
+
logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
|
|
1439
|
+
throw new Error(`Error event thrown: ${chunk.data.message}`);
|
|
1440
|
+
}
|
|
1441
|
+
if (chunk.event === "values") {
|
|
1442
|
+
latestStateValues = chunk.data;
|
|
1443
|
+
continue;
|
|
1444
|
+
}
|
|
1445
|
+
const event = chunk.data;
|
|
1446
|
+
const currentNodeName = event.name;
|
|
1447
|
+
const eventType = event.event;
|
|
1448
|
+
const runId = event.metadata.run_id;
|
|
1449
|
+
externalRunId = runId;
|
|
1450
|
+
const metadata = event.metadata;
|
|
1451
|
+
shouldExit = shouldExit != null ? shouldExit : eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitExit;
|
|
1452
|
+
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
1453
|
+
const manuallyEmitIntermediateState = eventType === LangGraphEventTypes.OnCustomEvent && event.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState;
|
|
1454
|
+
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
1455
|
+
nodeName = currentNodeName;
|
|
1456
|
+
}
|
|
1457
|
+
if (!nodeName) {
|
|
1458
|
+
continue;
|
|
1459
|
+
}
|
|
1460
|
+
if (manuallyEmitIntermediateState) {
|
|
1461
|
+
if (eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1462
|
+
state = event.data.output;
|
|
1463
|
+
emit(getStateSyncEvent({
|
|
1464
|
+
threadId,
|
|
1465
|
+
runId,
|
|
1466
|
+
agentName: agent.name,
|
|
1467
|
+
nodeName,
|
|
1468
|
+
state: event.data.output,
|
|
1469
|
+
running: true,
|
|
1470
|
+
active: true
|
|
1471
|
+
}));
|
|
1472
|
+
}
|
|
1473
|
+
continue;
|
|
1474
|
+
}
|
|
1475
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1476
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1477
|
+
}
|
|
1478
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
|
|
1479
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1480
|
+
}
|
|
1481
|
+
let updatedState = latestStateValues;
|
|
1482
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
|
|
1483
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1484
|
+
}
|
|
1485
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1486
|
+
updatedState = {
|
|
1487
|
+
...updatedState,
|
|
1488
|
+
...streamingStateExtractor.extractState()
|
|
1489
|
+
};
|
|
1490
|
+
}
|
|
1491
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1492
|
+
emitIntermediateStateUntilEnd = null;
|
|
1493
|
+
}
|
|
1494
|
+
const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
|
|
1495
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1496
|
+
state = updatedState;
|
|
1497
|
+
prevNodeName = nodeName;
|
|
1498
|
+
emit(getStateSyncEvent({
|
|
1499
|
+
threadId,
|
|
1500
|
+
runId,
|
|
1501
|
+
agentName: agent.name,
|
|
1502
|
+
nodeName,
|
|
1503
|
+
state,
|
|
1504
|
+
running: true,
|
|
1505
|
+
active: !exitingNode
|
|
1506
|
+
}));
|
|
1507
|
+
}
|
|
1508
|
+
emit(JSON.stringify(event) + "\n");
|
|
1350
1509
|
}
|
|
1510
|
+
state = await client.threads.getState(threadId);
|
|
1511
|
+
const isEndNode = state.next.length === 0;
|
|
1512
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1513
|
+
emit(getStateSyncEvent({
|
|
1514
|
+
threadId,
|
|
1515
|
+
runId: externalRunId,
|
|
1516
|
+
agentName: agent.name,
|
|
1517
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1518
|
+
state: state.values,
|
|
1519
|
+
running: !shouldExit,
|
|
1520
|
+
active: false
|
|
1521
|
+
}));
|
|
1522
|
+
return Promise.resolve();
|
|
1523
|
+
} catch (e) {
|
|
1524
|
+
return Promise.resolve();
|
|
1351
1525
|
}
|
|
1352
|
-
return headers;
|
|
1353
1526
|
}
|
|
1354
|
-
__name(
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1527
|
+
__name(streamEvents, "streamEvents");
|
|
1528
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1529
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1530
|
+
if (key !== "messages") {
|
|
1531
|
+
acc[key] = state[key];
|
|
1532
|
+
}
|
|
1533
|
+
return acc;
|
|
1534
|
+
}, {});
|
|
1535
|
+
return JSON.stringify({
|
|
1536
|
+
event: LangGraphEventTypes.OnCopilotKitStateSync,
|
|
1537
|
+
thread_id: threadId,
|
|
1538
|
+
run_id: runId,
|
|
1539
|
+
agent_name: agentName,
|
|
1540
|
+
node_name: nodeName,
|
|
1541
|
+
active,
|
|
1542
|
+
state: stateWithoutMessages,
|
|
1543
|
+
running,
|
|
1544
|
+
role: "assistant"
|
|
1545
|
+
}) + "\n";
|
|
1546
|
+
}
|
|
1547
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1548
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1549
|
+
emitIntermediateState;
|
|
1550
|
+
toolCallBuffer;
|
|
1551
|
+
currentToolCall;
|
|
1552
|
+
previouslyParsableState;
|
|
1553
|
+
constructor(emitIntermediateState) {
|
|
1554
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1555
|
+
this.toolCallBuffer = {};
|
|
1556
|
+
this.currentToolCall = null;
|
|
1557
|
+
this.previouslyParsableState = {};
|
|
1558
|
+
}
|
|
1559
|
+
bufferToolCalls(event) {
|
|
1560
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1561
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1562
|
+
if (chunk.name !== null) {
|
|
1563
|
+
this.currentToolCall = chunk.name;
|
|
1564
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1565
|
+
} else if (this.currentToolCall !== null) {
|
|
1566
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
getEmitStateConfig(currentToolName) {
|
|
1571
|
+
for (const config2 of this.emitIntermediateState) {
|
|
1572
|
+
const stateKey = config2["state_key"];
|
|
1573
|
+
const tool = config2["tool"];
|
|
1574
|
+
const toolArgument = config2["tool_argument"];
|
|
1575
|
+
if (currentToolName === tool) {
|
|
1576
|
+
return [
|
|
1577
|
+
toolArgument,
|
|
1578
|
+
stateKey
|
|
1579
|
+
];
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
return [
|
|
1583
|
+
null,
|
|
1584
|
+
null
|
|
1585
|
+
];
|
|
1586
|
+
}
|
|
1587
|
+
extractState() {
|
|
1588
|
+
const state = {};
|
|
1589
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1590
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1591
|
+
if (stateKey === null) {
|
|
1592
|
+
continue;
|
|
1593
|
+
}
|
|
1594
|
+
let parsedValue;
|
|
1595
|
+
try {
|
|
1596
|
+
parsedValue = (0, import_partial_json.parse)(value);
|
|
1597
|
+
} catch (error) {
|
|
1598
|
+
if (key in this.previouslyParsableState) {
|
|
1599
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1600
|
+
} else {
|
|
1601
|
+
continue;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1605
|
+
if (!argumentName) {
|
|
1606
|
+
state[stateKey] = parsedValue;
|
|
1607
|
+
} else {
|
|
1608
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
return state;
|
|
1612
|
+
}
|
|
1613
|
+
}, "StreamingStateExtractor");
|
|
1614
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1615
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1616
|
+
messages = messages.slice(1);
|
|
1617
|
+
}
|
|
1618
|
+
const mergedMessages = state.messages || [];
|
|
1619
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1620
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1621
|
+
for (const message of mergedMessages) {
|
|
1622
|
+
if ("tool_call_id" in message) {
|
|
1623
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
for (const message of messages) {
|
|
1627
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1628
|
+
continue;
|
|
1629
|
+
}
|
|
1630
|
+
if ("name" in message && message.name === agentName) {
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1634
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1635
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
mergedMessages.push(message);
|
|
1639
|
+
} else {
|
|
1640
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1641
|
+
if (mergedMessages[i].id === message.id) {
|
|
1642
|
+
if ("tool_calls" in message) {
|
|
1643
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1644
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1645
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
mergedMessages[i] = message;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1654
|
+
const currentMessage = mergedMessages[i];
|
|
1655
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1656
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1657
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
const correctedMessages = [];
|
|
1661
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1662
|
+
const currentMessage = mergedMessages[i];
|
|
1663
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1664
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1665
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1666
|
+
if (!nextMessage) {
|
|
1667
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1668
|
+
continue;
|
|
1669
|
+
}
|
|
1670
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1671
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1672
|
+
if (toolMessage) {
|
|
1673
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1674
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1675
|
+
continue;
|
|
1676
|
+
} else {
|
|
1677
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
correctedMessages.push(currentMessage);
|
|
1682
|
+
continue;
|
|
1683
|
+
}
|
|
1684
|
+
if ("tool_call_id" in currentMessage) {
|
|
1685
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1686
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1687
|
+
continue;
|
|
1688
|
+
}
|
|
1689
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1690
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1691
|
+
continue;
|
|
1692
|
+
}
|
|
1693
|
+
correctedMessages.push(currentMessage);
|
|
1694
|
+
continue;
|
|
1695
|
+
}
|
|
1696
|
+
correctedMessages.push(currentMessage);
|
|
1697
|
+
}
|
|
1698
|
+
return deepMerge(state, {
|
|
1699
|
+
messages: correctedMessages,
|
|
1700
|
+
copilotkit: {
|
|
1701
|
+
actions
|
|
1702
|
+
}
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1706
|
+
function deepMerge(obj1, obj2) {
|
|
1707
|
+
let result = {
|
|
1708
|
+
...obj1
|
|
1709
|
+
};
|
|
1710
|
+
for (let key in obj2) {
|
|
1711
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
1712
|
+
if (obj1[key]) {
|
|
1713
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
1714
|
+
} else {
|
|
1715
|
+
result[key] = {
|
|
1716
|
+
...obj2[key]
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
} else {
|
|
1720
|
+
result[key] = obj2[key];
|
|
1721
|
+
}
|
|
1722
|
+
}
|
|
1723
|
+
return result;
|
|
1724
|
+
}
|
|
1725
|
+
__name(deepMerge, "deepMerge");
|
|
1726
|
+
function formatMessages(messages) {
|
|
1727
|
+
return messages.map((message) => {
|
|
1728
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
1729
|
+
return message;
|
|
1730
|
+
}
|
|
1731
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
1732
|
+
return message;
|
|
1733
|
+
}
|
|
1734
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
1735
|
+
return message;
|
|
1736
|
+
}
|
|
1737
|
+
if (message.isActionExecutionMessage()) {
|
|
1738
|
+
const toolCall = {
|
|
1739
|
+
name: message.name,
|
|
1740
|
+
args: message.arguments,
|
|
1741
|
+
id: message.id
|
|
1742
|
+
};
|
|
1375
1743
|
return {
|
|
1376
|
-
|
|
1377
|
-
|
|
1744
|
+
type: message.type,
|
|
1745
|
+
content: "",
|
|
1746
|
+
tool_calls: [
|
|
1747
|
+
toolCall
|
|
1748
|
+
],
|
|
1749
|
+
role: MessageRole.assistant,
|
|
1750
|
+
id: message.id
|
|
1378
1751
|
};
|
|
1379
1752
|
}
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
};
|
|
1393
|
-
}
|
|
1753
|
+
if (message.isResultMessage()) {
|
|
1754
|
+
return {
|
|
1755
|
+
type: message.type,
|
|
1756
|
+
content: message.result,
|
|
1757
|
+
id: message.id,
|
|
1758
|
+
tool_call_id: message.actionExecutionId,
|
|
1759
|
+
name: message.actionName,
|
|
1760
|
+
role: MessageRole.tool
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
1764
|
+
});
|
|
1394
1765
|
}
|
|
1395
|
-
__name(
|
|
1766
|
+
__name(formatMessages, "formatMessages");
|
|
1767
|
+
|
|
1768
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
1769
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1770
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
1771
|
+
name: agent.name,
|
|
1772
|
+
description: agent.description,
|
|
1773
|
+
parameters: [],
|
|
1774
|
+
handler: async (_args) => {
|
|
1775
|
+
},
|
|
1776
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
1777
|
+
var _a;
|
|
1778
|
+
logger2.debug({
|
|
1779
|
+
actionName: agent.name
|
|
1780
|
+
}, "Executing LangGraph Cloud agent");
|
|
1781
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1782
|
+
let state = {};
|
|
1783
|
+
if (agentStates) {
|
|
1784
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1785
|
+
if (jsonState) {
|
|
1786
|
+
state = JSON.parse(jsonState);
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
try {
|
|
1790
|
+
const response = await execute({
|
|
1791
|
+
logger: logger2,
|
|
1792
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
1793
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1794
|
+
agent,
|
|
1795
|
+
threadId,
|
|
1796
|
+
nodeName,
|
|
1797
|
+
messages,
|
|
1798
|
+
state,
|
|
1799
|
+
properties: graphqlContext.properties,
|
|
1800
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1801
|
+
name: action.name,
|
|
1802
|
+
description: action.description,
|
|
1803
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
1804
|
+
}))
|
|
1805
|
+
});
|
|
1806
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
1807
|
+
streamResponse(response, eventSource.eventStream$);
|
|
1808
|
+
return eventSource.processLangGraphEvents();
|
|
1809
|
+
} catch (error) {
|
|
1810
|
+
logger2.error({
|
|
1811
|
+
url: endpoint.deploymentUrl,
|
|
1812
|
+
status: 500,
|
|
1813
|
+
body: error.message
|
|
1814
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
1815
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
}));
|
|
1819
|
+
return [
|
|
1820
|
+
...agents
|
|
1821
|
+
];
|
|
1822
|
+
}
|
|
1823
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1396
1824
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1397
1825
|
const actions = json["actions"].map((action) => ({
|
|
1398
1826
|
name: action.name,
|
|
@@ -1484,7 +1912,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1484
1912
|
throw new Error("Failed to execute remote agent");
|
|
1485
1913
|
}
|
|
1486
1914
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1487
|
-
|
|
1915
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1488
1916
|
return eventSource.processLangGraphEvents();
|
|
1489
1917
|
}
|
|
1490
1918
|
}));
|
|
@@ -1494,34 +1922,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1494
1922
|
];
|
|
1495
1923
|
}
|
|
1496
1924
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1497
|
-
async function
|
|
1925
|
+
async function streamResponse(response, eventStream$) {
|
|
1926
|
+
const reader = response.getReader();
|
|
1927
|
+
const decoder = new TextDecoder();
|
|
1928
|
+
let buffer = [];
|
|
1929
|
+
function flushBuffer() {
|
|
1930
|
+
const currentBuffer = buffer.join("");
|
|
1931
|
+
if (currentBuffer.trim().length === 0) {
|
|
1932
|
+
return;
|
|
1933
|
+
}
|
|
1934
|
+
const parts = currentBuffer.split("\n");
|
|
1935
|
+
if (parts.length === 0) {
|
|
1936
|
+
return;
|
|
1937
|
+
}
|
|
1938
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1939
|
+
buffer = [];
|
|
1940
|
+
if (!lastPartIsComplete) {
|
|
1941
|
+
buffer.push(parts.pop());
|
|
1942
|
+
}
|
|
1943
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1944
|
+
eventStream$.next(JSON.parse(part));
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
__name(flushBuffer, "flushBuffer");
|
|
1948
|
+
try {
|
|
1949
|
+
while (true) {
|
|
1950
|
+
const { done, value } = await reader.read();
|
|
1951
|
+
if (!done) {
|
|
1952
|
+
buffer.push(decoder.decode(value, {
|
|
1953
|
+
stream: true
|
|
1954
|
+
}));
|
|
1955
|
+
}
|
|
1956
|
+
flushBuffer();
|
|
1957
|
+
if (done) {
|
|
1958
|
+
break;
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
} catch (error) {
|
|
1962
|
+
console.error("Error in stream", error);
|
|
1963
|
+
eventStream$.error(error);
|
|
1964
|
+
return;
|
|
1965
|
+
}
|
|
1966
|
+
eventStream$.complete();
|
|
1967
|
+
}
|
|
1968
|
+
__name(streamResponse, "streamResponse");
|
|
1969
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1970
|
+
const headers = {
|
|
1971
|
+
"Content-Type": "application/json"
|
|
1972
|
+
};
|
|
1973
|
+
if (onBeforeRequest) {
|
|
1974
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1975
|
+
ctx: graphqlContext
|
|
1976
|
+
});
|
|
1977
|
+
if (additionalHeaders) {
|
|
1978
|
+
Object.assign(headers, additionalHeaders);
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
return headers;
|
|
1982
|
+
}
|
|
1983
|
+
__name(createHeaders, "createHeaders");
|
|
1984
|
+
|
|
1985
|
+
// src/lib/runtime/remote-actions.ts
|
|
1986
|
+
var EndpointType;
|
|
1987
|
+
(function(EndpointType2) {
|
|
1988
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
1989
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
1990
|
+
})(EndpointType || (EndpointType = {}));
|
|
1991
|
+
function isLangGraphAgentAction(action) {
|
|
1992
|
+
if (!action) {
|
|
1993
|
+
return false;
|
|
1994
|
+
}
|
|
1995
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
1996
|
+
}
|
|
1997
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1998
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1999
|
+
logger2.debug({
|
|
2000
|
+
url
|
|
2001
|
+
}, "Fetching actions from url");
|
|
2002
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2003
|
+
try {
|
|
2004
|
+
const response = await fetch(`${url}/info`, {
|
|
2005
|
+
method: "POST",
|
|
2006
|
+
headers,
|
|
2007
|
+
body: JSON.stringify({
|
|
2008
|
+
properties: graphqlContext.properties,
|
|
2009
|
+
frontendUrl
|
|
2010
|
+
})
|
|
2011
|
+
});
|
|
2012
|
+
if (!response.ok) {
|
|
2013
|
+
logger2.error({
|
|
2014
|
+
url,
|
|
2015
|
+
status: response.status,
|
|
2016
|
+
body: await response.text()
|
|
2017
|
+
}, "Failed to fetch actions from url");
|
|
2018
|
+
return {
|
|
2019
|
+
actions: [],
|
|
2020
|
+
agents: []
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
const json = await response.json();
|
|
2024
|
+
logger2.debug({
|
|
2025
|
+
json
|
|
2026
|
+
}, "Fetched actions from url");
|
|
2027
|
+
return json;
|
|
2028
|
+
} catch (error) {
|
|
2029
|
+
logger2.error({
|
|
2030
|
+
error: error.message ? error.message : error + ""
|
|
2031
|
+
}, "Failed to fetch actions from url");
|
|
2032
|
+
return {
|
|
2033
|
+
actions: [],
|
|
2034
|
+
agents: []
|
|
2035
|
+
};
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
2039
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1498
2040
|
const logger2 = graphqlContext.logger.child({
|
|
1499
2041
|
component: "remote-actions.fetchRemoteActions"
|
|
1500
2042
|
});
|
|
1501
2043
|
logger2.debug({
|
|
1502
|
-
|
|
1503
|
-
}, "Fetching remote
|
|
1504
|
-
const filtered =
|
|
1505
|
-
|
|
2044
|
+
remoteEndpointDefinitions
|
|
2045
|
+
}, "Fetching from remote endpoints");
|
|
2046
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
2047
|
+
if (value.type === "langgraph-cloud") {
|
|
2048
|
+
return value;
|
|
2049
|
+
}
|
|
2050
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
2051
|
+
});
|
|
2052
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
2053
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
2054
|
+
return constructLGCRemoteAction({
|
|
2055
|
+
endpoint,
|
|
2056
|
+
messages,
|
|
2057
|
+
graphqlContext,
|
|
2058
|
+
logger: logger2.child({
|
|
2059
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
2060
|
+
endpoint
|
|
2061
|
+
}),
|
|
2062
|
+
agentStates
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
1506
2065
|
const json = await fetchRemoteInfo({
|
|
1507
|
-
url:
|
|
1508
|
-
onBeforeRequest:
|
|
2066
|
+
url: endpoint.url,
|
|
2067
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1509
2068
|
graphqlContext,
|
|
1510
2069
|
logger: logger2.child({
|
|
1511
2070
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1512
|
-
|
|
2071
|
+
endpoint
|
|
1513
2072
|
}),
|
|
1514
2073
|
frontendUrl
|
|
1515
2074
|
});
|
|
1516
2075
|
return constructRemoteActions({
|
|
1517
2076
|
json,
|
|
1518
2077
|
messages,
|
|
1519
|
-
url:
|
|
1520
|
-
onBeforeRequest:
|
|
2078
|
+
url: endpoint.url,
|
|
2079
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1521
2080
|
graphqlContext,
|
|
1522
2081
|
logger: logger2.child({
|
|
1523
2082
|
component: "remote-actions.constructActions",
|
|
1524
|
-
|
|
2083
|
+
endpoint
|
|
1525
2084
|
}),
|
|
1526
2085
|
agentStates
|
|
1527
2086
|
});
|
|
@@ -1719,7 +2278,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
1719
2278
|
__name(executeAction, "executeAction");
|
|
1720
2279
|
|
|
1721
2280
|
// src/graphql/types/base/index.ts
|
|
1722
|
-
var
|
|
2281
|
+
var import_type_graphql2 = require("type-graphql");
|
|
1723
2282
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1724
2283
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1725
2284
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -1742,15 +2301,15 @@ var BaseMessageInput = class {
|
|
|
1742
2301
|
};
|
|
1743
2302
|
__name(BaseMessageInput, "BaseMessageInput");
|
|
1744
2303
|
_ts_decorate([
|
|
1745
|
-
(0,
|
|
2304
|
+
(0, import_type_graphql2.Field)(() => String),
|
|
1746
2305
|
_ts_metadata("design:type", String)
|
|
1747
2306
|
], BaseMessageInput.prototype, "id", void 0);
|
|
1748
2307
|
_ts_decorate([
|
|
1749
|
-
(0,
|
|
2308
|
+
(0, import_type_graphql2.Field)(() => Date),
|
|
1750
2309
|
_ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
|
|
1751
2310
|
], BaseMessageInput.prototype, "createdAt", void 0);
|
|
1752
2311
|
BaseMessageInput = _ts_decorate([
|
|
1753
|
-
(0,
|
|
2312
|
+
(0, import_type_graphql2.InputType)()
|
|
1754
2313
|
], BaseMessageInput);
|
|
1755
2314
|
|
|
1756
2315
|
// src/graphql/types/converted/index.ts
|
|
@@ -1854,7 +2413,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
|
1854
2413
|
var import_rxjs3 = require("rxjs");
|
|
1855
2414
|
var CopilotRuntime = class {
|
|
1856
2415
|
actions;
|
|
1857
|
-
|
|
2416
|
+
remoteEndpointDefinitions;
|
|
1858
2417
|
langserve = [];
|
|
1859
2418
|
onBeforeRequest;
|
|
1860
2419
|
onAfterRequest;
|
|
@@ -1865,7 +2424,7 @@ var CopilotRuntime = class {
|
|
|
1865
2424
|
const remoteChain = new RemoteChain(chain);
|
|
1866
2425
|
this.langserve.push(remoteChain.toAction());
|
|
1867
2426
|
}
|
|
1868
|
-
this.
|
|
2427
|
+
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
|
|
1869
2428
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
1870
2429
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
1871
2430
|
}
|
|
@@ -1934,7 +2493,7 @@ var CopilotRuntime = class {
|
|
|
1934
2493
|
async processAgentRequest(request) {
|
|
1935
2494
|
var _a;
|
|
1936
2495
|
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
1937
|
-
const { threadId
|
|
2496
|
+
const { threadId, agentName, nodeName } = agentSession;
|
|
1938
2497
|
const serverSideActions = await this.getServerSideActions(request);
|
|
1939
2498
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
1940
2499
|
const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
|
|
@@ -2006,8 +2565,12 @@ var CopilotRuntime = class {
|
|
|
2006
2565
|
console.error("Error loading langserve chain:", error);
|
|
2007
2566
|
}
|
|
2008
2567
|
}
|
|
2568
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
2569
|
+
...endpoint,
|
|
2570
|
+
type: this.resolveEndpointType(endpoint)
|
|
2571
|
+
}));
|
|
2009
2572
|
const remoteActions = await setupRemoteActions({
|
|
2010
|
-
|
|
2573
|
+
remoteEndpointDefinitions,
|
|
2011
2574
|
graphqlContext,
|
|
2012
2575
|
messages: inputMessages,
|
|
2013
2576
|
agentStates,
|
|
@@ -2023,6 +2586,12 @@ var CopilotRuntime = class {
|
|
|
2023
2586
|
...remoteActions
|
|
2024
2587
|
];
|
|
2025
2588
|
}
|
|
2589
|
+
resolveEndpointType(endpoint) {
|
|
2590
|
+
if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
2591
|
+
return EndpointType.LangGraphCloud;
|
|
2592
|
+
}
|
|
2593
|
+
return endpoint.type;
|
|
2594
|
+
}
|
|
2026
2595
|
};
|
|
2027
2596
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
2028
2597
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -2037,6 +2606,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
|
2037
2606
|
return allTools;
|
|
2038
2607
|
}
|
|
2039
2608
|
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
2609
|
+
function copilotKitEndpoint(config2) {
|
|
2610
|
+
return {
|
|
2611
|
+
...config2,
|
|
2612
|
+
type: EndpointType.CopilotKit
|
|
2613
|
+
};
|
|
2614
|
+
}
|
|
2615
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
2616
|
+
function langGraphCloudEndpoint(config2) {
|
|
2617
|
+
return {
|
|
2618
|
+
...config2,
|
|
2619
|
+
type: EndpointType.LangGraphCloud
|
|
2620
|
+
};
|
|
2621
|
+
}
|
|
2622
|
+
__name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
|
|
2040
2623
|
|
|
2041
2624
|
// src/lib/integrations/shared.ts
|
|
2042
2625
|
var import_type_graphql16 = require("type-graphql");
|
|
@@ -2050,43 +2633,6 @@ var import_type_graphql11 = require("type-graphql");
|
|
|
2050
2633
|
|
|
2051
2634
|
// src/graphql/inputs/message.input.ts
|
|
2052
2635
|
var import_type_graphql3 = require("type-graphql");
|
|
2053
|
-
|
|
2054
|
-
// src/graphql/types/enums.ts
|
|
2055
|
-
var import_type_graphql2 = require("type-graphql");
|
|
2056
|
-
var MessageRole;
|
|
2057
|
-
(function(MessageRole2) {
|
|
2058
|
-
MessageRole2["user"] = "user";
|
|
2059
|
-
MessageRole2["assistant"] = "assistant";
|
|
2060
|
-
MessageRole2["system"] = "system";
|
|
2061
|
-
})(MessageRole || (MessageRole = {}));
|
|
2062
|
-
var ActionExecutionScope;
|
|
2063
|
-
(function(ActionExecutionScope2) {
|
|
2064
|
-
ActionExecutionScope2["server"] = "server";
|
|
2065
|
-
ActionExecutionScope2["client"] = "client";
|
|
2066
|
-
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
2067
|
-
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
2068
|
-
var CopilotRequestType;
|
|
2069
|
-
(function(CopilotRequestType2) {
|
|
2070
|
-
CopilotRequestType2["Chat"] = "Chat";
|
|
2071
|
-
CopilotRequestType2["Task"] = "Task";
|
|
2072
|
-
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
2073
|
-
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
2074
|
-
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
2075
|
-
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
2076
|
-
(0, import_type_graphql2.registerEnumType)(MessageRole, {
|
|
2077
|
-
name: "MessageRole",
|
|
2078
|
-
description: "The role of the message"
|
|
2079
|
-
});
|
|
2080
|
-
(0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
|
|
2081
|
-
name: "ActionExecutionScope",
|
|
2082
|
-
description: "The scope of the action"
|
|
2083
|
-
});
|
|
2084
|
-
(0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
|
|
2085
|
-
name: "CopilotRequestType",
|
|
2086
|
-
description: "The type of Copilot request"
|
|
2087
|
-
});
|
|
2088
|
-
|
|
2089
|
-
// src/graphql/inputs/message.input.ts
|
|
2090
2636
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
2091
2637
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2092
2638
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -3743,6 +4289,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3743
4289
|
UnifyAdapter,
|
|
3744
4290
|
buildSchema,
|
|
3745
4291
|
config,
|
|
4292
|
+
copilotKitEndpoint,
|
|
3746
4293
|
copilotRuntimeNestEndpoint,
|
|
3747
4294
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
3748
4295
|
copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -3750,6 +4297,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3750
4297
|
copilotRuntimeNodeHttpEndpoint,
|
|
3751
4298
|
createContext,
|
|
3752
4299
|
flattenToolCallsNoDuplicates,
|
|
3753
|
-
getCommonConfig
|
|
4300
|
+
getCommonConfig,
|
|
4301
|
+
langGraphCloudEndpoint
|
|
3754
4302
|
});
|
|
3755
4303
|
//# sourceMappingURL=index.js.map
|