@copilotkit/runtime 1.3.16-mme-azure-openai.0 → 1.3.16-mme-sdk-js.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -3
- package/__snapshots__/schema/schema.graphql +1 -0
- package/dist/{chunk-MVG266E4.mjs → chunk-3N5PRHNJ.mjs} +37 -10
- package/dist/chunk-3N5PRHNJ.mjs.map +1 -0
- package/dist/{chunk-S5SYJ6JC.mjs → chunk-5IF2J3NL.mjs} +2 -2
- package/dist/{chunk-YVZORQSA.mjs → chunk-6UQXCIKF.mjs} +673 -151
- package/dist/chunk-6UQXCIKF.mjs.map +1 -0
- package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
- package/dist/chunk-B74M7FXG.mjs.map +1 -0
- package/dist/{chunk-MKZBH3Y6.mjs → chunk-HFTU4B7Q.mjs} +2 -2
- package/dist/{chunk-CRSCH25P.mjs → chunk-PA6U4BDE.mjs} +2 -2
- package/dist/{chunk-BHSRGDL6.mjs → chunk-XE3SYKK4.mjs} +1 -4
- package/dist/chunk-XE3SYKK4.mjs.map +1 -0
- package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-543a59ae.d.ts} +29 -7
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -1
- package/dist/{groq-adapter-798aff23.d.ts → groq-adapter-7aa25931.d.ts} +1 -34
- package/dist/{index-cff31380.d.ts → index-83ee522f.d.ts} +3 -2
- package/dist/index.d.ts +4 -4
- package/dist/index.js +745 -199
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -9
- package/dist/index.mjs.map +1 -1
- package/dist/{langserve-a14a6849.d.ts → langserve-f00629d2.d.ts} +1 -1
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +745 -199
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +13 -9
- package/dist/lib/integrations/index.d.ts +4 -4
- package/dist/lib/integrations/index.js +4 -1
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +5 -5
- package/dist/lib/integrations/nest/index.d.ts +3 -3
- package/dist/lib/integrations/nest/index.js +4 -1
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +3 -3
- package/dist/lib/integrations/node-express/index.d.ts +3 -3
- package/dist/lib/integrations/node-express/index.js +4 -1
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +3 -3
- package/dist/lib/integrations/node-http/index.d.ts +3 -3
- package/dist/lib/integrations/node-http/index.js +4 -1
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +2 -2
- package/dist/service-adapters/index.d.ts +4 -4
- package/dist/service-adapters/index.js +0 -3
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +1 -1
- package/package.json +6 -4
- package/src/agents/langgraph/event-source.ts +62 -94
- package/src/agents/langgraph/events.ts +27 -17
- package/src/graphql/types/converted/index.ts +5 -1
- package/src/graphql/types/enums.ts +1 -0
- package/src/lib/runtime/copilot-runtime.ts +58 -11
- package/src/lib/runtime/remote-action-constructors.ts +284 -0
- package/src/lib/runtime/remote-actions.ts +65 -159
- package/src/lib/runtime/remote-lg-cloud-action.ts +612 -0
- package/src/service-adapters/openai/openai-adapter.ts +0 -37
- package/src/service-adapters/openai/utils.ts +13 -9
- package/dist/chunk-BHSRGDL6.mjs.map +0 -1
- package/dist/chunk-MVG266E4.mjs.map +0 -1
- package/dist/chunk-MYZHUCL6.mjs.map +0 -1
- package/dist/chunk-YVZORQSA.mjs.map +0 -1
- /package/dist/{chunk-S5SYJ6JC.mjs.map → chunk-5IF2J3NL.mjs.map} +0 -0
- /package/dist/{chunk-MKZBH3Y6.mjs.map → chunk-HFTU4B7Q.mjs.map} +0 -0
- /package/dist/{chunk-CRSCH25P.mjs.map → chunk-PA6U4BDE.mjs.map} +0 -0
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.2",
|
|
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,526 @@ 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
|
+
state = event.data;
|
|
1462
|
+
emit(getStateSyncEvent({
|
|
1463
|
+
threadId,
|
|
1464
|
+
runId,
|
|
1465
|
+
agentName: agent.name,
|
|
1466
|
+
nodeName,
|
|
1467
|
+
state,
|
|
1468
|
+
running: true,
|
|
1469
|
+
active: true
|
|
1470
|
+
}));
|
|
1471
|
+
continue;
|
|
1472
|
+
}
|
|
1473
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1474
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1475
|
+
}
|
|
1476
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStart) {
|
|
1477
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1478
|
+
}
|
|
1479
|
+
let updatedState = latestStateValues;
|
|
1480
|
+
if (emitIntermediateState && eventType === LangGraphEventTypes.OnChatModelStream) {
|
|
1481
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1482
|
+
}
|
|
1483
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1484
|
+
updatedState = {
|
|
1485
|
+
...updatedState,
|
|
1486
|
+
...streamingStateExtractor.extractState()
|
|
1487
|
+
};
|
|
1488
|
+
}
|
|
1489
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === LangGraphEventTypes.OnChainEnd) {
|
|
1490
|
+
emitIntermediateStateUntilEnd = null;
|
|
1491
|
+
}
|
|
1492
|
+
const exitingNode = nodeName === currentNodeName && eventType === LangGraphEventTypes.OnChainEnd;
|
|
1493
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1494
|
+
state = updatedState;
|
|
1495
|
+
prevNodeName = nodeName;
|
|
1496
|
+
emit(getStateSyncEvent({
|
|
1497
|
+
threadId,
|
|
1498
|
+
runId,
|
|
1499
|
+
agentName: agent.name,
|
|
1500
|
+
nodeName,
|
|
1501
|
+
state,
|
|
1502
|
+
running: true,
|
|
1503
|
+
active: !exitingNode
|
|
1504
|
+
}));
|
|
1505
|
+
}
|
|
1506
|
+
emit(JSON.stringify(event) + "\n");
|
|
1350
1507
|
}
|
|
1508
|
+
state = await client.threads.getState(threadId);
|
|
1509
|
+
const isEndNode = state.next.length === 0;
|
|
1510
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1511
|
+
emit(getStateSyncEvent({
|
|
1512
|
+
threadId,
|
|
1513
|
+
runId: externalRunId,
|
|
1514
|
+
agentName: agent.name,
|
|
1515
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1516
|
+
state: state.values,
|
|
1517
|
+
running: !shouldExit,
|
|
1518
|
+
active: false
|
|
1519
|
+
}));
|
|
1520
|
+
return Promise.resolve();
|
|
1521
|
+
} catch (e) {
|
|
1522
|
+
return Promise.resolve();
|
|
1351
1523
|
}
|
|
1352
|
-
return headers;
|
|
1353
1524
|
}
|
|
1354
|
-
__name(
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1525
|
+
__name(streamEvents, "streamEvents");
|
|
1526
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1527
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1528
|
+
if (key !== "messages") {
|
|
1529
|
+
acc[key] = state[key];
|
|
1530
|
+
}
|
|
1531
|
+
return acc;
|
|
1532
|
+
}, {});
|
|
1533
|
+
return JSON.stringify({
|
|
1534
|
+
event: LangGraphEventTypes.OnCopilotKitStateSync,
|
|
1535
|
+
thread_id: threadId,
|
|
1536
|
+
run_id: runId,
|
|
1537
|
+
agent_name: agentName,
|
|
1538
|
+
node_name: nodeName,
|
|
1539
|
+
active,
|
|
1540
|
+
state: stateWithoutMessages,
|
|
1541
|
+
running,
|
|
1542
|
+
role: "assistant"
|
|
1543
|
+
}) + "\n";
|
|
1544
|
+
}
|
|
1545
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1546
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1547
|
+
emitIntermediateState;
|
|
1548
|
+
toolCallBuffer;
|
|
1549
|
+
currentToolCall;
|
|
1550
|
+
previouslyParsableState;
|
|
1551
|
+
constructor(emitIntermediateState) {
|
|
1552
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1553
|
+
this.toolCallBuffer = {};
|
|
1554
|
+
this.currentToolCall = null;
|
|
1555
|
+
this.previouslyParsableState = {};
|
|
1556
|
+
}
|
|
1557
|
+
bufferToolCalls(event) {
|
|
1558
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1559
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1560
|
+
if (chunk.name !== null && chunk.name !== void 0) {
|
|
1561
|
+
this.currentToolCall = chunk.name;
|
|
1562
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1563
|
+
} else if (this.currentToolCall !== null && this.currentToolCall !== void 0) {
|
|
1564
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
getEmitStateConfig(currentToolName) {
|
|
1569
|
+
for (const config2 of this.emitIntermediateState) {
|
|
1570
|
+
const stateKey = config2["state_key"];
|
|
1571
|
+
const tool = config2["tool"];
|
|
1572
|
+
const toolArgument = config2["tool_argument"];
|
|
1573
|
+
if (currentToolName === tool) {
|
|
1574
|
+
return [
|
|
1575
|
+
toolArgument,
|
|
1576
|
+
stateKey
|
|
1577
|
+
];
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
return [
|
|
1581
|
+
null,
|
|
1582
|
+
null
|
|
1583
|
+
];
|
|
1584
|
+
}
|
|
1585
|
+
extractState() {
|
|
1586
|
+
const state = {};
|
|
1587
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1588
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1589
|
+
if (stateKey === null) {
|
|
1590
|
+
continue;
|
|
1591
|
+
}
|
|
1592
|
+
let parsedValue;
|
|
1593
|
+
try {
|
|
1594
|
+
parsedValue = (0, import_partial_json.parse)(value);
|
|
1595
|
+
} catch (error) {
|
|
1596
|
+
if (key in this.previouslyParsableState) {
|
|
1597
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1598
|
+
} else {
|
|
1599
|
+
continue;
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1603
|
+
if (!argumentName) {
|
|
1604
|
+
state[stateKey] = parsedValue;
|
|
1605
|
+
} else {
|
|
1606
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
return state;
|
|
1610
|
+
}
|
|
1611
|
+
}, "StreamingStateExtractor");
|
|
1612
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1613
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1614
|
+
messages = messages.slice(1);
|
|
1615
|
+
}
|
|
1616
|
+
const mergedMessages = state.messages || [];
|
|
1617
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1618
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1619
|
+
for (const message of mergedMessages) {
|
|
1620
|
+
if ("tool_call_id" in message) {
|
|
1621
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
for (const message of messages) {
|
|
1625
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1626
|
+
continue;
|
|
1627
|
+
}
|
|
1628
|
+
if ("name" in message && message.name === agentName) {
|
|
1629
|
+
continue;
|
|
1630
|
+
}
|
|
1631
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1632
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1633
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1634
|
+
continue;
|
|
1635
|
+
}
|
|
1636
|
+
mergedMessages.push(message);
|
|
1637
|
+
} else {
|
|
1638
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1639
|
+
if (mergedMessages[i].id === message.id) {
|
|
1640
|
+
if ("tool_calls" in message) {
|
|
1641
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1642
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1643
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
mergedMessages[i] = message;
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1652
|
+
const currentMessage = mergedMessages[i];
|
|
1653
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1654
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1655
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
const correctedMessages = [];
|
|
1659
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1660
|
+
const currentMessage = mergedMessages[i];
|
|
1661
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1662
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1663
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1664
|
+
if (!nextMessage) {
|
|
1665
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1666
|
+
continue;
|
|
1667
|
+
}
|
|
1668
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1669
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1670
|
+
if (toolMessage) {
|
|
1671
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1672
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1673
|
+
continue;
|
|
1674
|
+
} else {
|
|
1675
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1676
|
+
continue;
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
correctedMessages.push(currentMessage);
|
|
1680
|
+
continue;
|
|
1681
|
+
}
|
|
1682
|
+
if ("tool_call_id" in currentMessage) {
|
|
1683
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1684
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1688
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1689
|
+
continue;
|
|
1690
|
+
}
|
|
1691
|
+
correctedMessages.push(currentMessage);
|
|
1692
|
+
continue;
|
|
1693
|
+
}
|
|
1694
|
+
correctedMessages.push(currentMessage);
|
|
1695
|
+
}
|
|
1696
|
+
return deepMerge(state, {
|
|
1697
|
+
messages: correctedMessages,
|
|
1698
|
+
copilotkit: {
|
|
1699
|
+
actions
|
|
1700
|
+
}
|
|
1701
|
+
});
|
|
1702
|
+
}
|
|
1703
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1704
|
+
function deepMerge(obj1, obj2) {
|
|
1705
|
+
let result = {
|
|
1706
|
+
...obj1
|
|
1707
|
+
};
|
|
1708
|
+
for (let key in obj2) {
|
|
1709
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
1710
|
+
if (obj1[key]) {
|
|
1711
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
1712
|
+
} else {
|
|
1713
|
+
result[key] = {
|
|
1714
|
+
...obj2[key]
|
|
1715
|
+
};
|
|
1716
|
+
}
|
|
1717
|
+
} else {
|
|
1718
|
+
result[key] = obj2[key];
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
return result;
|
|
1722
|
+
}
|
|
1723
|
+
__name(deepMerge, "deepMerge");
|
|
1724
|
+
function formatMessages(messages) {
|
|
1725
|
+
return messages.map((message) => {
|
|
1726
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
1727
|
+
return message;
|
|
1728
|
+
}
|
|
1729
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
1730
|
+
return message;
|
|
1731
|
+
}
|
|
1732
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
1733
|
+
return message;
|
|
1734
|
+
}
|
|
1735
|
+
if (message.isActionExecutionMessage()) {
|
|
1736
|
+
const toolCall = {
|
|
1737
|
+
name: message.name,
|
|
1738
|
+
args: message.arguments,
|
|
1739
|
+
id: message.id
|
|
1740
|
+
};
|
|
1375
1741
|
return {
|
|
1376
|
-
|
|
1377
|
-
|
|
1742
|
+
type: message.type,
|
|
1743
|
+
content: "",
|
|
1744
|
+
tool_calls: [
|
|
1745
|
+
toolCall
|
|
1746
|
+
],
|
|
1747
|
+
role: MessageRole.assistant,
|
|
1748
|
+
id: message.id
|
|
1378
1749
|
};
|
|
1379
1750
|
}
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
};
|
|
1393
|
-
}
|
|
1751
|
+
if (message.isResultMessage()) {
|
|
1752
|
+
return {
|
|
1753
|
+
type: message.type,
|
|
1754
|
+
content: message.result,
|
|
1755
|
+
id: message.id,
|
|
1756
|
+
tool_call_id: message.actionExecutionId,
|
|
1757
|
+
name: message.actionName,
|
|
1758
|
+
role: MessageRole.tool
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
1762
|
+
});
|
|
1394
1763
|
}
|
|
1395
|
-
__name(
|
|
1764
|
+
__name(formatMessages, "formatMessages");
|
|
1765
|
+
|
|
1766
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
1767
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1768
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
1769
|
+
name: agent.name,
|
|
1770
|
+
description: agent.description,
|
|
1771
|
+
parameters: [],
|
|
1772
|
+
handler: async (_args) => {
|
|
1773
|
+
},
|
|
1774
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
1775
|
+
var _a;
|
|
1776
|
+
logger2.debug({
|
|
1777
|
+
actionName: agent.name
|
|
1778
|
+
}, "Executing LangGraph Cloud agent");
|
|
1779
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1780
|
+
let state = {};
|
|
1781
|
+
if (agentStates) {
|
|
1782
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1783
|
+
if (jsonState) {
|
|
1784
|
+
state = JSON.parse(jsonState);
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
try {
|
|
1788
|
+
const response = await execute({
|
|
1789
|
+
logger: logger2,
|
|
1790
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
1791
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1792
|
+
agent,
|
|
1793
|
+
threadId,
|
|
1794
|
+
nodeName,
|
|
1795
|
+
messages,
|
|
1796
|
+
state,
|
|
1797
|
+
properties: graphqlContext.properties,
|
|
1798
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1799
|
+
name: action.name,
|
|
1800
|
+
description: action.description,
|
|
1801
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
1802
|
+
}))
|
|
1803
|
+
});
|
|
1804
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
1805
|
+
streamResponse(response, eventSource.eventStream$);
|
|
1806
|
+
return eventSource.processLangGraphEvents();
|
|
1807
|
+
} catch (error) {
|
|
1808
|
+
logger2.error({
|
|
1809
|
+
url: endpoint.deploymentUrl,
|
|
1810
|
+
status: 500,
|
|
1811
|
+
body: error.message
|
|
1812
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
1813
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1814
|
+
}
|
|
1815
|
+
}
|
|
1816
|
+
}));
|
|
1817
|
+
return [
|
|
1818
|
+
...agents
|
|
1819
|
+
];
|
|
1820
|
+
}
|
|
1821
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1396
1822
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1397
1823
|
const actions = json["actions"].map((action) => ({
|
|
1398
1824
|
name: action.name,
|
|
@@ -1484,7 +1910,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1484
1910
|
throw new Error("Failed to execute remote agent");
|
|
1485
1911
|
}
|
|
1486
1912
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1487
|
-
|
|
1913
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1488
1914
|
return eventSource.processLangGraphEvents();
|
|
1489
1915
|
}
|
|
1490
1916
|
}));
|
|
@@ -1494,34 +1920,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1494
1920
|
];
|
|
1495
1921
|
}
|
|
1496
1922
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1497
|
-
async function
|
|
1923
|
+
async function streamResponse(response, eventStream$) {
|
|
1924
|
+
const reader = response.getReader();
|
|
1925
|
+
const decoder = new TextDecoder();
|
|
1926
|
+
let buffer = [];
|
|
1927
|
+
function flushBuffer() {
|
|
1928
|
+
const currentBuffer = buffer.join("");
|
|
1929
|
+
if (currentBuffer.trim().length === 0) {
|
|
1930
|
+
return;
|
|
1931
|
+
}
|
|
1932
|
+
const parts = currentBuffer.split("\n");
|
|
1933
|
+
if (parts.length === 0) {
|
|
1934
|
+
return;
|
|
1935
|
+
}
|
|
1936
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1937
|
+
buffer = [];
|
|
1938
|
+
if (!lastPartIsComplete) {
|
|
1939
|
+
buffer.push(parts.pop());
|
|
1940
|
+
}
|
|
1941
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1942
|
+
eventStream$.next(JSON.parse(part));
|
|
1943
|
+
});
|
|
1944
|
+
}
|
|
1945
|
+
__name(flushBuffer, "flushBuffer");
|
|
1946
|
+
try {
|
|
1947
|
+
while (true) {
|
|
1948
|
+
const { done, value } = await reader.read();
|
|
1949
|
+
if (!done) {
|
|
1950
|
+
buffer.push(decoder.decode(value, {
|
|
1951
|
+
stream: true
|
|
1952
|
+
}));
|
|
1953
|
+
}
|
|
1954
|
+
flushBuffer();
|
|
1955
|
+
if (done) {
|
|
1956
|
+
break;
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
} catch (error) {
|
|
1960
|
+
console.error("Error in stream", error);
|
|
1961
|
+
eventStream$.error(error);
|
|
1962
|
+
return;
|
|
1963
|
+
}
|
|
1964
|
+
eventStream$.complete();
|
|
1965
|
+
}
|
|
1966
|
+
__name(streamResponse, "streamResponse");
|
|
1967
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1968
|
+
const headers = {
|
|
1969
|
+
"Content-Type": "application/json"
|
|
1970
|
+
};
|
|
1971
|
+
if (onBeforeRequest) {
|
|
1972
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1973
|
+
ctx: graphqlContext
|
|
1974
|
+
});
|
|
1975
|
+
if (additionalHeaders) {
|
|
1976
|
+
Object.assign(headers, additionalHeaders);
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
return headers;
|
|
1980
|
+
}
|
|
1981
|
+
__name(createHeaders, "createHeaders");
|
|
1982
|
+
|
|
1983
|
+
// src/lib/runtime/remote-actions.ts
|
|
1984
|
+
var EndpointType;
|
|
1985
|
+
(function(EndpointType2) {
|
|
1986
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
1987
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
1988
|
+
})(EndpointType || (EndpointType = {}));
|
|
1989
|
+
function isLangGraphAgentAction(action) {
|
|
1990
|
+
if (!action) {
|
|
1991
|
+
return false;
|
|
1992
|
+
}
|
|
1993
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
1994
|
+
}
|
|
1995
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1996
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1997
|
+
logger2.debug({
|
|
1998
|
+
url
|
|
1999
|
+
}, "Fetching actions from url");
|
|
2000
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2001
|
+
try {
|
|
2002
|
+
const response = await fetch(`${url}/info`, {
|
|
2003
|
+
method: "POST",
|
|
2004
|
+
headers,
|
|
2005
|
+
body: JSON.stringify({
|
|
2006
|
+
properties: graphqlContext.properties,
|
|
2007
|
+
frontendUrl
|
|
2008
|
+
})
|
|
2009
|
+
});
|
|
2010
|
+
if (!response.ok) {
|
|
2011
|
+
logger2.error({
|
|
2012
|
+
url,
|
|
2013
|
+
status: response.status,
|
|
2014
|
+
body: await response.text()
|
|
2015
|
+
}, "Failed to fetch actions from url");
|
|
2016
|
+
return {
|
|
2017
|
+
actions: [],
|
|
2018
|
+
agents: []
|
|
2019
|
+
};
|
|
2020
|
+
}
|
|
2021
|
+
const json = await response.json();
|
|
2022
|
+
logger2.debug({
|
|
2023
|
+
json
|
|
2024
|
+
}, "Fetched actions from url");
|
|
2025
|
+
return json;
|
|
2026
|
+
} catch (error) {
|
|
2027
|
+
logger2.error({
|
|
2028
|
+
error: error.message ? error.message : error + ""
|
|
2029
|
+
}, "Failed to fetch actions from url");
|
|
2030
|
+
return {
|
|
2031
|
+
actions: [],
|
|
2032
|
+
agents: []
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
2037
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1498
2038
|
const logger2 = graphqlContext.logger.child({
|
|
1499
2039
|
component: "remote-actions.fetchRemoteActions"
|
|
1500
2040
|
});
|
|
1501
2041
|
logger2.debug({
|
|
1502
|
-
|
|
1503
|
-
}, "Fetching remote
|
|
1504
|
-
const filtered =
|
|
1505
|
-
|
|
2042
|
+
remoteEndpointDefinitions
|
|
2043
|
+
}, "Fetching from remote endpoints");
|
|
2044
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
2045
|
+
if (value.type === "langgraph-cloud") {
|
|
2046
|
+
return value;
|
|
2047
|
+
}
|
|
2048
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
2049
|
+
});
|
|
2050
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
2051
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
2052
|
+
return constructLGCRemoteAction({
|
|
2053
|
+
endpoint,
|
|
2054
|
+
messages,
|
|
2055
|
+
graphqlContext,
|
|
2056
|
+
logger: logger2.child({
|
|
2057
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
2058
|
+
endpoint
|
|
2059
|
+
}),
|
|
2060
|
+
agentStates
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
1506
2063
|
const json = await fetchRemoteInfo({
|
|
1507
|
-
url:
|
|
1508
|
-
onBeforeRequest:
|
|
2064
|
+
url: endpoint.url,
|
|
2065
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1509
2066
|
graphqlContext,
|
|
1510
2067
|
logger: logger2.child({
|
|
1511
2068
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1512
|
-
|
|
2069
|
+
endpoint
|
|
1513
2070
|
}),
|
|
1514
2071
|
frontendUrl
|
|
1515
2072
|
});
|
|
1516
2073
|
return constructRemoteActions({
|
|
1517
2074
|
json,
|
|
1518
2075
|
messages,
|
|
1519
|
-
url:
|
|
1520
|
-
onBeforeRequest:
|
|
2076
|
+
url: endpoint.url,
|
|
2077
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1521
2078
|
graphqlContext,
|
|
1522
2079
|
logger: logger2.child({
|
|
1523
2080
|
component: "remote-actions.constructActions",
|
|
1524
|
-
|
|
2081
|
+
endpoint
|
|
1525
2082
|
}),
|
|
1526
2083
|
agentStates
|
|
1527
2084
|
});
|
|
@@ -1719,7 +2276,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
1719
2276
|
__name(executeAction, "executeAction");
|
|
1720
2277
|
|
|
1721
2278
|
// src/graphql/types/base/index.ts
|
|
1722
|
-
var
|
|
2279
|
+
var import_type_graphql2 = require("type-graphql");
|
|
1723
2280
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1724
2281
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1725
2282
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -1742,15 +2299,15 @@ var BaseMessageInput = class {
|
|
|
1742
2299
|
};
|
|
1743
2300
|
__name(BaseMessageInput, "BaseMessageInput");
|
|
1744
2301
|
_ts_decorate([
|
|
1745
|
-
(0,
|
|
2302
|
+
(0, import_type_graphql2.Field)(() => String),
|
|
1746
2303
|
_ts_metadata("design:type", String)
|
|
1747
2304
|
], BaseMessageInput.prototype, "id", void 0);
|
|
1748
2305
|
_ts_decorate([
|
|
1749
|
-
(0,
|
|
2306
|
+
(0, import_type_graphql2.Field)(() => Date),
|
|
1750
2307
|
_ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
|
|
1751
2308
|
], BaseMessageInput.prototype, "createdAt", void 0);
|
|
1752
2309
|
BaseMessageInput = _ts_decorate([
|
|
1753
|
-
(0,
|
|
2310
|
+
(0, import_type_graphql2.InputType)()
|
|
1754
2311
|
], BaseMessageInput);
|
|
1755
2312
|
|
|
1756
2313
|
// src/graphql/types/converted/index.ts
|
|
@@ -1854,7 +2411,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
|
1854
2411
|
var import_rxjs3 = require("rxjs");
|
|
1855
2412
|
var CopilotRuntime = class {
|
|
1856
2413
|
actions;
|
|
1857
|
-
|
|
2414
|
+
remoteEndpointDefinitions;
|
|
1858
2415
|
langserve = [];
|
|
1859
2416
|
onBeforeRequest;
|
|
1860
2417
|
onAfterRequest;
|
|
@@ -1865,7 +2422,7 @@ var CopilotRuntime = class {
|
|
|
1865
2422
|
const remoteChain = new RemoteChain(chain);
|
|
1866
2423
|
this.langserve.push(remoteChain.toAction());
|
|
1867
2424
|
}
|
|
1868
|
-
this.
|
|
2425
|
+
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
|
|
1869
2426
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
1870
2427
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
1871
2428
|
}
|
|
@@ -1934,7 +2491,7 @@ var CopilotRuntime = class {
|
|
|
1934
2491
|
async processAgentRequest(request) {
|
|
1935
2492
|
var _a;
|
|
1936
2493
|
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
1937
|
-
const { threadId
|
|
2494
|
+
const { threadId, agentName, nodeName } = agentSession;
|
|
1938
2495
|
const serverSideActions = await this.getServerSideActions(request);
|
|
1939
2496
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
1940
2497
|
const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
|
|
@@ -2006,8 +2563,12 @@ var CopilotRuntime = class {
|
|
|
2006
2563
|
console.error("Error loading langserve chain:", error);
|
|
2007
2564
|
}
|
|
2008
2565
|
}
|
|
2566
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
2567
|
+
...endpoint,
|
|
2568
|
+
type: this.resolveEndpointType(endpoint)
|
|
2569
|
+
}));
|
|
2009
2570
|
const remoteActions = await setupRemoteActions({
|
|
2010
|
-
|
|
2571
|
+
remoteEndpointDefinitions,
|
|
2011
2572
|
graphqlContext,
|
|
2012
2573
|
messages: inputMessages,
|
|
2013
2574
|
agentStates,
|
|
@@ -2023,6 +2584,12 @@ var CopilotRuntime = class {
|
|
|
2023
2584
|
...remoteActions
|
|
2024
2585
|
];
|
|
2025
2586
|
}
|
|
2587
|
+
resolveEndpointType(endpoint) {
|
|
2588
|
+
if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
2589
|
+
return EndpointType.LangGraphCloud;
|
|
2590
|
+
}
|
|
2591
|
+
return endpoint.type;
|
|
2592
|
+
}
|
|
2026
2593
|
};
|
|
2027
2594
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
2028
2595
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -2037,6 +2604,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
|
2037
2604
|
return allTools;
|
|
2038
2605
|
}
|
|
2039
2606
|
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
2607
|
+
function copilotKitEndpoint(config2) {
|
|
2608
|
+
return {
|
|
2609
|
+
...config2,
|
|
2610
|
+
type: EndpointType.CopilotKit
|
|
2611
|
+
};
|
|
2612
|
+
}
|
|
2613
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
2614
|
+
function langGraphCloudEndpoint(config2) {
|
|
2615
|
+
return {
|
|
2616
|
+
...config2,
|
|
2617
|
+
type: EndpointType.LangGraphCloud
|
|
2618
|
+
};
|
|
2619
|
+
}
|
|
2620
|
+
__name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
|
|
2040
2621
|
|
|
2041
2622
|
// src/lib/integrations/shared.ts
|
|
2042
2623
|
var import_type_graphql16 = require("type-graphql");
|
|
@@ -2050,43 +2631,6 @@ var import_type_graphql11 = require("type-graphql");
|
|
|
2050
2631
|
|
|
2051
2632
|
// src/graphql/inputs/message.input.ts
|
|
2052
2633
|
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
2634
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
2091
2635
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2092
2636
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -3743,6 +4287,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3743
4287
|
UnifyAdapter,
|
|
3744
4288
|
buildSchema,
|
|
3745
4289
|
config,
|
|
4290
|
+
copilotKitEndpoint,
|
|
3746
4291
|
copilotRuntimeNestEndpoint,
|
|
3747
4292
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
3748
4293
|
copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -3750,6 +4295,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
3750
4295
|
copilotRuntimeNodeHttpEndpoint,
|
|
3751
4296
|
createContext,
|
|
3752
4297
|
flattenToolCallsNoDuplicates,
|
|
3753
|
-
getCommonConfig
|
|
4298
|
+
getCommonConfig,
|
|
4299
|
+
langGraphCloudEndpoint
|
|
3754
4300
|
});
|
|
3755
4301
|
//# sourceMappingURL=index.js.map
|