@copilotkit/runtime 1.3.12-lgc-alpha-1.0 → 1.3.13
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 +12 -3
- package/dist/{chunk-6HXQC7IT.mjs → chunk-3LFBIWWK.mjs} +9 -36
- package/dist/chunk-3LFBIWWK.mjs.map +1 -0
- package/dist/{chunk-E6ZFCM3B.mjs → chunk-76K222WC.mjs} +133 -561
- package/dist/chunk-76K222WC.mjs.map +1 -0
- package/dist/{chunk-V7SK6QZN.mjs → chunk-CPAHDRLS.mjs} +36 -9
- package/dist/chunk-CPAHDRLS.mjs.map +1 -0
- package/dist/{chunk-TM7ZRU3M.mjs → chunk-DEZQR4EM.mjs} +2 -2
- package/dist/{chunk-XMDH5MKI.mjs → chunk-MCUYYSCJ.mjs} +2 -2
- package/dist/{chunk-6B3NPPSR.mjs → chunk-SPZSO3T3.mjs} +2 -2
- package/dist/{chunk-7MQDBRXJ.mjs → chunk-Y6RQG5HF.mjs} +2 -2
- package/dist/{copilot-runtime-aba7d4b4.d.ts → copilot-runtime-df3527ad.d.ts} +5 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.js +213 -641
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -13
- package/dist/index.mjs.map +1 -1
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +213 -641
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -13
- package/dist/lib/integrations/index.d.ts +2 -2
- package/dist/lib/integrations/index.js +4 -5
- 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 +1 -1
- package/dist/lib/integrations/nest/index.js +4 -5
- 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 +1 -1
- package/dist/lib/integrations/node-express/index.js +4 -5
- 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 +1 -1
- package/dist/lib/integrations/node-http/index.js +4 -5
- 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.js +35 -8
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +2 -2
- package/package.json +6 -7
- package/src/agents/langgraph/event-source.ts +67 -22
- package/src/lib/runtime/copilot-runtime.ts +11 -58
- package/src/lib/runtime/remote-actions.ts +159 -65
- package/src/service-adapters/events.ts +5 -1
- package/src/service-adapters/langchain/utils.test.ts +169 -0
- package/src/service-adapters/langchain/utils.ts +41 -8
- package/dist/chunk-6HXQC7IT.mjs.map +0 -1
- package/dist/chunk-E6ZFCM3B.mjs.map +0 -1
- package/dist/chunk-V7SK6QZN.mjs.map +0 -1
- package/src/lib/runtime/remote-action-constructors.ts +0 -283
- package/src/lib/runtime/remote-lg-cloud-action.ts +0 -441
- /package/dist/{chunk-TM7ZRU3M.mjs.map → chunk-DEZQR4EM.mjs.map} +0 -0
- /package/dist/{chunk-XMDH5MKI.mjs.map → chunk-MCUYYSCJ.mjs.map} +0 -0
- /package/dist/{chunk-6B3NPPSR.mjs.map → chunk-SPZSO3T3.mjs.map} +0 -0
- /package/dist/{chunk-7MQDBRXJ.mjs.map → chunk-Y6RQG5HF.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.
|
|
47
|
+
version: "1.3.13",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -77,16 +77,16 @@ var require_package = __commonJS({
|
|
|
77
77
|
"ts-node": "^10.9.2",
|
|
78
78
|
tsconfig: "workspace:*",
|
|
79
79
|
tsup: "^6.7.0",
|
|
80
|
-
typescript: "^5.2.3"
|
|
80
|
+
typescript: "^5.2.3",
|
|
81
|
+
"zod-to-json-schema": "^3.23.5"
|
|
81
82
|
},
|
|
82
83
|
dependencies: {
|
|
83
84
|
"@anthropic-ai/sdk": "^0.27.3",
|
|
84
85
|
"@copilotkit/shared": "workspace:*",
|
|
85
|
-
"@langchain/google-gauth": "^0.1.0",
|
|
86
86
|
"@graphql-yoga/plugin-defer-stream": "^3.3.1",
|
|
87
87
|
"@langchain/community": "^0.0.53",
|
|
88
88
|
"@langchain/core": "^0.3.13",
|
|
89
|
-
"@langchain/
|
|
89
|
+
"@langchain/google-gauth": "^0.1.0",
|
|
90
90
|
"@langchain/openai": "^0.0.28",
|
|
91
91
|
"class-transformer": "^0.5.1",
|
|
92
92
|
express: "^4.19.2",
|
|
@@ -96,7 +96,6 @@ var require_package = __commonJS({
|
|
|
96
96
|
"groq-sdk": "^0.5.0",
|
|
97
97
|
langchain: "^0.3.3",
|
|
98
98
|
openai: "^4.50.0",
|
|
99
|
-
"partial-json": "^0.1.7",
|
|
100
99
|
pino: "^9.2.0",
|
|
101
100
|
"pino-pretty": "^11.2.1",
|
|
102
101
|
"reflect-metadata": "^0.2.2",
|
|
@@ -132,7 +131,6 @@ __export(lib_exports, {
|
|
|
132
131
|
UnifyAdapter: () => UnifyAdapter,
|
|
133
132
|
buildSchema: () => buildSchema,
|
|
134
133
|
config: () => config,
|
|
135
|
-
copilotKitEndpoint: () => copilotKitEndpoint,
|
|
136
134
|
copilotRuntimeNestEndpoint: () => copilotRuntimeNestEndpoint,
|
|
137
135
|
copilotRuntimeNextJSAppRouterEndpoint: () => copilotRuntimeNextJSAppRouterEndpoint,
|
|
138
136
|
copilotRuntimeNextJSPagesRouterEndpoint: () => copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -140,8 +138,7 @@ __export(lib_exports, {
|
|
|
140
138
|
copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
|
|
141
139
|
createContext: () => createContext,
|
|
142
140
|
flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
|
|
143
|
-
getCommonConfig: () => getCommonConfig
|
|
144
|
-
langGraphCloudEndpoint: () => langGraphCloudEndpoint
|
|
141
|
+
getCommonConfig: () => getCommonConfig
|
|
145
142
|
});
|
|
146
143
|
module.exports = __toCommonJS(lib_exports);
|
|
147
144
|
|
|
@@ -497,24 +494,27 @@ __name(convertMessageToLangChainMessage, "convertMessageToLangChainMessage");
|
|
|
497
494
|
function convertJsonSchemaToZodSchema(jsonSchema, required) {
|
|
498
495
|
if (jsonSchema.type === "object") {
|
|
499
496
|
const spec = {};
|
|
497
|
+
if (!jsonSchema.properties || !Object.keys(jsonSchema.properties).length) {
|
|
498
|
+
return !required ? import_zod.z.object(spec).optional() : import_zod.z.object(spec);
|
|
499
|
+
}
|
|
500
500
|
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
501
501
|
spec[key] = convertJsonSchemaToZodSchema(value, jsonSchema.required ? jsonSchema.required.includes(key) : false);
|
|
502
502
|
}
|
|
503
503
|
let schema = import_zod.z.object(spec);
|
|
504
|
-
return
|
|
504
|
+
return required ? schema : schema.optional();
|
|
505
505
|
} else if (jsonSchema.type === "string") {
|
|
506
506
|
let schema = import_zod.z.string().describe(jsonSchema.description);
|
|
507
|
-
return
|
|
507
|
+
return required ? schema : schema.optional();
|
|
508
508
|
} else if (jsonSchema.type === "number") {
|
|
509
509
|
let schema = import_zod.z.number().describe(jsonSchema.description);
|
|
510
|
-
return
|
|
510
|
+
return required ? schema : schema.optional();
|
|
511
511
|
} else if (jsonSchema.type === "boolean") {
|
|
512
512
|
let schema = import_zod.z.boolean().describe(jsonSchema.description);
|
|
513
|
-
return
|
|
513
|
+
return required ? schema : schema.optional();
|
|
514
514
|
} else if (jsonSchema.type === "array") {
|
|
515
515
|
let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, true);
|
|
516
516
|
let schema = import_zod.z.array(itemSchema);
|
|
517
|
-
return
|
|
517
|
+
return required ? schema : schema.optional();
|
|
518
518
|
}
|
|
519
519
|
}
|
|
520
520
|
__name(convertJsonSchemaToZodSchema, "convertJsonSchemaToZodSchema");
|
|
@@ -577,20 +577,39 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
577
577
|
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
578
578
|
let reader = result.getReader();
|
|
579
579
|
let mode = null;
|
|
580
|
+
const toolCallDetails = {
|
|
581
|
+
name: null,
|
|
582
|
+
id: null,
|
|
583
|
+
index: null,
|
|
584
|
+
prevIndex: null
|
|
585
|
+
};
|
|
580
586
|
while (true) {
|
|
581
587
|
try {
|
|
582
588
|
const { done, value } = await reader.read();
|
|
583
589
|
let toolCallName = void 0;
|
|
584
590
|
let toolCallId = void 0;
|
|
585
591
|
let toolCallArgs = void 0;
|
|
592
|
+
let toolCallIndex = void 0;
|
|
593
|
+
let toolCallPrevIndex = void 0;
|
|
586
594
|
let hasToolCall = false;
|
|
587
595
|
let content = value == null ? void 0 : value.content;
|
|
588
596
|
if (isAIMessageChunk(value)) {
|
|
589
597
|
let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
|
|
590
|
-
toolCallName = chunk == null ? void 0 : chunk.name;
|
|
591
|
-
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
592
598
|
toolCallArgs = chunk == null ? void 0 : chunk.args;
|
|
593
599
|
hasToolCall = chunk != void 0;
|
|
600
|
+
if (chunk == null ? void 0 : chunk.name)
|
|
601
|
+
toolCallDetails.name = chunk.name;
|
|
602
|
+
if ((chunk == null ? void 0 : chunk.index) != null) {
|
|
603
|
+
toolCallDetails.index = chunk.index;
|
|
604
|
+
if (toolCallDetails.prevIndex == null)
|
|
605
|
+
toolCallDetails.prevIndex = chunk.index;
|
|
606
|
+
}
|
|
607
|
+
if (chunk == null ? void 0 : chunk.id)
|
|
608
|
+
toolCallDetails.id = chunk.index != null ? `${chunk.id}-idx-${chunk.index}` : chunk.id;
|
|
609
|
+
toolCallName = toolCallDetails.name;
|
|
610
|
+
toolCallId = toolCallDetails.id;
|
|
611
|
+
toolCallIndex = toolCallDetails.index;
|
|
612
|
+
toolCallPrevIndex = toolCallDetails.prevIndex;
|
|
594
613
|
} else if (isBaseMessageChunk(value)) {
|
|
595
614
|
let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
|
|
596
615
|
toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
|
|
@@ -609,7 +628,7 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
609
628
|
break;
|
|
610
629
|
}
|
|
611
630
|
if (mode === null) {
|
|
612
|
-
if (hasToolCall) {
|
|
631
|
+
if (hasToolCall && toolCallId && toolCallName) {
|
|
613
632
|
mode = "function";
|
|
614
633
|
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
615
634
|
} else if (content) {
|
|
@@ -620,6 +639,11 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
620
639
|
if (mode === "message" && content) {
|
|
621
640
|
eventStream$.sendTextMessageContent(Array.isArray(content) ? ((_i = content[0]) == null ? void 0 : _i.text) ?? "" : content);
|
|
622
641
|
} else if (mode === "function" && toolCallArgs) {
|
|
642
|
+
if (toolCallIndex !== toolCallPrevIndex) {
|
|
643
|
+
eventStream$.sendActionExecutionEnd();
|
|
644
|
+
eventStream$.sendActionExecutionStart(toolCallId, toolCallName);
|
|
645
|
+
toolCallDetails.prevIndex = toolCallDetails.index;
|
|
646
|
+
}
|
|
623
647
|
eventStream$.sendActionExecutionArgs(toolCallArgs);
|
|
624
648
|
}
|
|
625
649
|
} catch (error) {
|
|
@@ -1068,6 +1092,50 @@ var LangGraphEventTypes;
|
|
|
1068
1092
|
var import_shared7 = require("@copilotkit/shared");
|
|
1069
1093
|
var RemoteLangGraphEventSource = class {
|
|
1070
1094
|
eventStream$ = new import_rxjs.ReplaySubject();
|
|
1095
|
+
async streamResponse(response) {
|
|
1096
|
+
const reader = response.body.getReader();
|
|
1097
|
+
const decoder = new TextDecoder();
|
|
1098
|
+
let buffer = [];
|
|
1099
|
+
const eventStream$ = this.eventStream$;
|
|
1100
|
+
function flushBuffer() {
|
|
1101
|
+
const currentBuffer = buffer.join("");
|
|
1102
|
+
if (currentBuffer.trim().length === 0) {
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
const parts = currentBuffer.split("\n");
|
|
1106
|
+
if (parts.length === 0) {
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1110
|
+
buffer = [];
|
|
1111
|
+
if (!lastPartIsComplete) {
|
|
1112
|
+
buffer.push(parts.pop());
|
|
1113
|
+
}
|
|
1114
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1115
|
+
eventStream$.next(JSON.parse(part));
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
__name(flushBuffer, "flushBuffer");
|
|
1119
|
+
try {
|
|
1120
|
+
while (true) {
|
|
1121
|
+
const { done, value } = await reader.read();
|
|
1122
|
+
if (!done) {
|
|
1123
|
+
buffer.push(decoder.decode(value, {
|
|
1124
|
+
stream: true
|
|
1125
|
+
}));
|
|
1126
|
+
}
|
|
1127
|
+
flushBuffer();
|
|
1128
|
+
if (done) {
|
|
1129
|
+
break;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
} catch (error) {
|
|
1133
|
+
console.error("Error in stream", error);
|
|
1134
|
+
eventStream$.error(error);
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
eventStream$.complete();
|
|
1138
|
+
}
|
|
1071
1139
|
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
1072
1140
|
if (typeof shouldEmitToolCalls === "boolean") {
|
|
1073
1141
|
return shouldEmitToolCalls;
|
|
@@ -1090,25 +1158,20 @@ var RemoteLangGraphEventSource = class {
|
|
|
1090
1158
|
} else {
|
|
1091
1159
|
acc.content = null;
|
|
1092
1160
|
}
|
|
1093
|
-
|
|
1094
|
-
// @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
|
|
1095
|
-
((_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)
|
|
1096
|
-
);
|
|
1097
|
-
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);
|
|
1098
|
-
if (toolCallChunks && toolCallChunks.length > 0) {
|
|
1161
|
+
if ((_h = (_g = (_f = event.data) == null ? void 0 : _f.chunk) == null ? void 0 : _g.kwargs) == null ? void 0 : _h.tool_call_chunks) {
|
|
1099
1162
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1100
|
-
acc.toolCallMessageId =
|
|
1101
|
-
if ((
|
|
1102
|
-
acc.toolCallName =
|
|
1163
|
+
acc.toolCallMessageId = (_i = event.data.chunk.kwargs) == null ? void 0 : _i.id;
|
|
1164
|
+
if ((_j = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _j.name) {
|
|
1165
|
+
acc.toolCallName = event.data.chunk.kwargs.tool_call_chunks[0].name;
|
|
1103
1166
|
}
|
|
1104
|
-
if ((
|
|
1105
|
-
acc.toolCallId =
|
|
1167
|
+
if ((_k = event.data.chunk.kwargs.tool_call_chunks[0]) == null ? void 0 : _k.id) {
|
|
1168
|
+
acc.toolCallId = event.data.chunk.kwargs.tool_call_chunks[0].id;
|
|
1106
1169
|
}
|
|
1107
1170
|
acc.prevMessageId = acc.messageId;
|
|
1108
|
-
acc.messageId =
|
|
1171
|
+
acc.messageId = (_n = (_m = (_l = event.data) == null ? void 0 : _l.chunk) == null ? void 0 : _m.kwargs) == null ? void 0 : _n.id;
|
|
1109
1172
|
} else if (acc.content && acc.content != "") {
|
|
1110
1173
|
acc.prevMessageId = acc.messageId;
|
|
1111
|
-
acc.messageId =
|
|
1174
|
+
acc.messageId = (_q = (_p = (_o = event.data) == null ? void 0 : _o.chunk) == null ? void 0 : _p.kwargs) == null ? void 0 : _q.id;
|
|
1112
1175
|
} else {
|
|
1113
1176
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1114
1177
|
acc.prevMessageId = acc.messageId;
|
|
@@ -1133,7 +1196,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1133
1196
|
prevMessageId: null,
|
|
1134
1197
|
content: null
|
|
1135
1198
|
}), (0, import_rxjs.mergeMap)((eventWithState) => {
|
|
1136
|
-
var _a, _b, _c, _d, _e
|
|
1199
|
+
var _a, _b, _c, _d, _e;
|
|
1137
1200
|
const events = [];
|
|
1138
1201
|
let shouldEmitMessages = true;
|
|
1139
1202
|
let shouldEmitToolCalls = false;
|
|
@@ -1216,8 +1279,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1216
1279
|
});
|
|
1217
1280
|
}
|
|
1218
1281
|
}
|
|
1219
|
-
const args = (
|
|
1220
|
-
((_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
|
+
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;
|
|
1221
1283
|
const content = eventWithState.content;
|
|
1222
1284
|
if (args) {
|
|
1223
1285
|
if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
|
|
@@ -1268,446 +1330,70 @@ var RemoteLangGraphEventSource = class {
|
|
|
1268
1330
|
};
|
|
1269
1331
|
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
1270
1332
|
|
|
1271
|
-
// src/lib/runtime/remote-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
// src/graphql/types/enums.ts
|
|
1277
|
-
var import_type_graphql = require("type-graphql");
|
|
1278
|
-
var MessageRole;
|
|
1279
|
-
(function(MessageRole2) {
|
|
1280
|
-
MessageRole2["user"] = "user";
|
|
1281
|
-
MessageRole2["assistant"] = "assistant";
|
|
1282
|
-
MessageRole2["system"] = "system";
|
|
1283
|
-
})(MessageRole || (MessageRole = {}));
|
|
1284
|
-
var ActionExecutionScope;
|
|
1285
|
-
(function(ActionExecutionScope2) {
|
|
1286
|
-
ActionExecutionScope2["server"] = "server";
|
|
1287
|
-
ActionExecutionScope2["client"] = "client";
|
|
1288
|
-
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
1289
|
-
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
1290
|
-
var CopilotRequestType;
|
|
1291
|
-
(function(CopilotRequestType2) {
|
|
1292
|
-
CopilotRequestType2["Chat"] = "Chat";
|
|
1293
|
-
CopilotRequestType2["Task"] = "Task";
|
|
1294
|
-
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
1295
|
-
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
1296
|
-
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
1297
|
-
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
1298
|
-
(0, import_type_graphql.registerEnumType)(MessageRole, {
|
|
1299
|
-
name: "MessageRole",
|
|
1300
|
-
description: "The role of the message"
|
|
1301
|
-
});
|
|
1302
|
-
(0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
|
|
1303
|
-
name: "ActionExecutionScope",
|
|
1304
|
-
description: "The scope of the action"
|
|
1305
|
-
});
|
|
1306
|
-
(0, import_type_graphql.registerEnumType)(CopilotRequestType, {
|
|
1307
|
-
name: "CopilotRequestType",
|
|
1308
|
-
description: "The type of Copilot request"
|
|
1309
|
-
});
|
|
1310
|
-
|
|
1311
|
-
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1312
|
-
async function execute(args) {
|
|
1313
|
-
return new ReadableStream({
|
|
1314
|
-
async start(controller) {
|
|
1315
|
-
try {
|
|
1316
|
-
await streamEvents(controller, args);
|
|
1317
|
-
controller.close();
|
|
1318
|
-
} catch (err) {
|
|
1319
|
-
}
|
|
1320
|
-
}
|
|
1321
|
-
});
|
|
1322
|
-
}
|
|
1323
|
-
__name(execute, "execute");
|
|
1324
|
-
async function streamEvents(controller, args) {
|
|
1325
|
-
const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions } = args;
|
|
1326
|
-
let nodeName = initialNodeName;
|
|
1327
|
-
let state = initialState;
|
|
1328
|
-
const { name, assistantId: initialAssistantId } = agent;
|
|
1329
|
-
const client = new import_langgraph_sdk.Client({
|
|
1330
|
-
apiUrl: deploymentUrl,
|
|
1331
|
-
apiKey: langsmithApiKey
|
|
1332
|
-
});
|
|
1333
|
-
let initialThreadId = agrsInitialThreadId;
|
|
1334
|
-
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
1335
|
-
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
1336
|
-
initialThreadId = initialThreadId.substring(3);
|
|
1337
|
-
}
|
|
1338
|
-
const assistants = await client.assistants.search();
|
|
1339
|
-
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
1340
|
-
const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
|
|
1341
|
-
if (initialThreadId === threadId) {
|
|
1342
|
-
await client.threads.get(threadId);
|
|
1343
|
-
} else {
|
|
1344
|
-
await client.threads.create({
|
|
1345
|
-
threadId
|
|
1346
|
-
});
|
|
1347
|
-
}
|
|
1348
|
-
let agentState = {
|
|
1349
|
-
values: {}
|
|
1350
|
-
};
|
|
1351
|
-
if (wasInitiatedWithExistingThread) {
|
|
1352
|
-
agentState = await client.threads.getState(threadId);
|
|
1353
|
-
}
|
|
1354
|
-
const agentStateValues = agentState.values;
|
|
1355
|
-
state.messages = agentStateValues.messages;
|
|
1356
|
-
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
1357
|
-
state = langGraphDefaultMergeState(state, formatMessages(messages), actions);
|
|
1358
|
-
if (mode === "continue") {
|
|
1359
|
-
await client.threads.updateState(threadId, {
|
|
1360
|
-
values: state,
|
|
1361
|
-
asNode: nodeName
|
|
1362
|
-
});
|
|
1363
|
-
}
|
|
1364
|
-
const assistantId = initialAssistantId ?? retrievedAssistant.assistant_id;
|
|
1365
|
-
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
1366
|
-
const streamInput = mode === "start" ? state : null;
|
|
1367
|
-
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
1368
|
-
let prevNodeName = null;
|
|
1369
|
-
let emitIntermediateStateUntilEnd = null;
|
|
1370
|
-
let shouldExit = null;
|
|
1371
|
-
let externalRunId = null;
|
|
1372
|
-
const streamResponse2 = client.runs.stream(threadId, assistantId, {
|
|
1373
|
-
input: streamInput,
|
|
1374
|
-
streamMode: [
|
|
1375
|
-
"events",
|
|
1376
|
-
"values"
|
|
1377
|
-
]
|
|
1378
|
-
});
|
|
1379
|
-
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
1380
|
-
let latestStateValues = {};
|
|
1381
|
-
for await (const chunk of streamResponse2) {
|
|
1382
|
-
if (![
|
|
1383
|
-
"events",
|
|
1384
|
-
"values"
|
|
1385
|
-
].includes(chunk.event))
|
|
1386
|
-
continue;
|
|
1387
|
-
if (chunk.event === "values") {
|
|
1388
|
-
latestStateValues = chunk.data;
|
|
1389
|
-
continue;
|
|
1390
|
-
}
|
|
1391
|
-
const event = chunk.data;
|
|
1392
|
-
const currentNodeName = event.name;
|
|
1393
|
-
const eventType = event.event;
|
|
1394
|
-
const runId = event.metadata.run_id;
|
|
1395
|
-
externalRunId = runId;
|
|
1396
|
-
const metadata = event.metadata;
|
|
1397
|
-
shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
|
|
1398
|
-
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
1399
|
-
const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
|
|
1400
|
-
const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
|
|
1401
|
-
const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
|
|
1402
|
-
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
1403
|
-
nodeName = currentNodeName;
|
|
1404
|
-
}
|
|
1405
|
-
if (!nodeName) {
|
|
1406
|
-
continue;
|
|
1407
|
-
}
|
|
1408
|
-
if (forceEmitIntermediateState) {
|
|
1409
|
-
if (eventType === "on_chain_end") {
|
|
1410
|
-
state = event.data.output;
|
|
1411
|
-
emit(getStateSyncEvent({
|
|
1412
|
-
threadId,
|
|
1413
|
-
runId,
|
|
1414
|
-
agentName: agent.name,
|
|
1415
|
-
nodeName,
|
|
1416
|
-
state: event.data.output,
|
|
1417
|
-
running: true,
|
|
1418
|
-
active: true
|
|
1419
|
-
}));
|
|
1420
|
-
}
|
|
1421
|
-
continue;
|
|
1422
|
-
}
|
|
1423
|
-
if (manuallyEmitMessage) {
|
|
1424
|
-
if (eventType === "on_chain_end") {
|
|
1425
|
-
state = event.data.output;
|
|
1426
|
-
emit(JSON.stringify({
|
|
1427
|
-
event: "on_copilotkit_emit_message",
|
|
1428
|
-
message: event.data.output,
|
|
1429
|
-
messageId: (0, import_node_crypto.randomUUID)(),
|
|
1430
|
-
role: MessageRole.assistant
|
|
1431
|
-
}) + "\n");
|
|
1432
|
-
}
|
|
1433
|
-
continue;
|
|
1434
|
-
}
|
|
1435
|
-
if (manuallyEmitToolCall) {
|
|
1436
|
-
if (eventType === "on_chain_end") {
|
|
1437
|
-
state = event.data.output;
|
|
1438
|
-
emit(JSON.stringify({
|
|
1439
|
-
event: "on_copilotkit_emit_tool_call",
|
|
1440
|
-
name: event.data.output.name,
|
|
1441
|
-
args: event.data.output.args,
|
|
1442
|
-
id: event.data.output.id
|
|
1443
|
-
}) + "\n");
|
|
1444
|
-
}
|
|
1445
|
-
continue;
|
|
1446
|
-
}
|
|
1447
|
-
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1448
|
-
emitIntermediateStateUntilEnd = nodeName;
|
|
1449
|
-
}
|
|
1450
|
-
if (emitIntermediateState && eventType === "on_chat_model_start") {
|
|
1451
|
-
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1452
|
-
}
|
|
1453
|
-
let updatedState = latestStateValues;
|
|
1454
|
-
if (emitIntermediateState && eventType === "on_chat_model_stream") {
|
|
1455
|
-
streamingStateExtractor.bufferToolCalls(event);
|
|
1456
|
-
}
|
|
1457
|
-
if (emitIntermediateStateUntilEnd !== null) {
|
|
1458
|
-
updatedState = {
|
|
1459
|
-
...updatedState,
|
|
1460
|
-
...streamingStateExtractor.extractState()
|
|
1461
|
-
};
|
|
1462
|
-
}
|
|
1463
|
-
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === "on_chain_end") {
|
|
1464
|
-
emitIntermediateStateUntilEnd = null;
|
|
1465
|
-
}
|
|
1466
|
-
const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
|
|
1467
|
-
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1468
|
-
state = updatedState;
|
|
1469
|
-
prevNodeName = nodeName;
|
|
1470
|
-
emit(getStateSyncEvent({
|
|
1471
|
-
threadId,
|
|
1472
|
-
runId,
|
|
1473
|
-
agentName: agent.name,
|
|
1474
|
-
nodeName,
|
|
1475
|
-
state,
|
|
1476
|
-
running: true,
|
|
1477
|
-
active: !exitingNode
|
|
1478
|
-
}));
|
|
1479
|
-
}
|
|
1480
|
-
emit(JSON.stringify(event) + "\n");
|
|
1481
|
-
}
|
|
1482
|
-
state = await client.threads.getState(threadId);
|
|
1483
|
-
const isEndNode = state.next.length === 0;
|
|
1484
|
-
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1485
|
-
emit(getStateSyncEvent({
|
|
1486
|
-
threadId,
|
|
1487
|
-
runId: externalRunId,
|
|
1488
|
-
agentName: agent.name,
|
|
1489
|
-
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1490
|
-
state: state.values,
|
|
1491
|
-
running: !shouldExit,
|
|
1492
|
-
active: false
|
|
1493
|
-
}));
|
|
1494
|
-
return Promise.resolve();
|
|
1495
|
-
}
|
|
1496
|
-
__name(streamEvents, "streamEvents");
|
|
1497
|
-
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1498
|
-
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1499
|
-
if (key !== "messages") {
|
|
1500
|
-
acc[key] = state[key];
|
|
1501
|
-
}
|
|
1502
|
-
return acc;
|
|
1503
|
-
}, {});
|
|
1504
|
-
return JSON.stringify({
|
|
1505
|
-
event: "on_copilotkit_state_sync",
|
|
1506
|
-
thread_id: threadId,
|
|
1507
|
-
run_id: runId,
|
|
1508
|
-
agent_name: agentName,
|
|
1509
|
-
node_name: nodeName,
|
|
1510
|
-
active,
|
|
1511
|
-
state: stateWithoutMessages,
|
|
1512
|
-
running,
|
|
1513
|
-
role: "assistant"
|
|
1514
|
-
}) + "\n";
|
|
1515
|
-
}
|
|
1516
|
-
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1517
|
-
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1518
|
-
emitIntermediateState;
|
|
1519
|
-
toolCallBuffer;
|
|
1520
|
-
currentToolCall;
|
|
1521
|
-
previouslyParsableState;
|
|
1522
|
-
constructor(emitIntermediateState) {
|
|
1523
|
-
this.emitIntermediateState = emitIntermediateState;
|
|
1524
|
-
this.toolCallBuffer = {};
|
|
1525
|
-
this.currentToolCall = null;
|
|
1526
|
-
this.previouslyParsableState = {};
|
|
1527
|
-
}
|
|
1528
|
-
bufferToolCalls(event) {
|
|
1529
|
-
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1530
|
-
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1531
|
-
if (chunk.name !== null) {
|
|
1532
|
-
this.currentToolCall = chunk.name;
|
|
1533
|
-
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1534
|
-
} else if (this.currentToolCall !== null) {
|
|
1535
|
-
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
getEmitStateConfig(currentToolName) {
|
|
1540
|
-
for (const config2 of this.emitIntermediateState) {
|
|
1541
|
-
const stateKey = config2["state_key"];
|
|
1542
|
-
const tool = config2["tool"];
|
|
1543
|
-
const toolArgument = config2["tool_argument"];
|
|
1544
|
-
if (currentToolName === tool) {
|
|
1545
|
-
return [
|
|
1546
|
-
toolArgument,
|
|
1547
|
-
stateKey
|
|
1548
|
-
];
|
|
1549
|
-
}
|
|
1550
|
-
}
|
|
1551
|
-
return [
|
|
1552
|
-
null,
|
|
1553
|
-
null
|
|
1554
|
-
];
|
|
1555
|
-
}
|
|
1556
|
-
extractState() {
|
|
1557
|
-
const state = {};
|
|
1558
|
-
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1559
|
-
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1560
|
-
if (stateKey === null) {
|
|
1561
|
-
continue;
|
|
1562
|
-
}
|
|
1563
|
-
let parsedValue;
|
|
1564
|
-
try {
|
|
1565
|
-
parsedValue = (0, import_partial_json.parse)(value);
|
|
1566
|
-
} catch (error) {
|
|
1567
|
-
if (key in this.previouslyParsableState) {
|
|
1568
|
-
parsedValue = this.previouslyParsableState[key];
|
|
1569
|
-
} else {
|
|
1570
|
-
continue;
|
|
1571
|
-
}
|
|
1572
|
-
}
|
|
1573
|
-
this.previouslyParsableState[key] = parsedValue;
|
|
1574
|
-
if (!argumentName) {
|
|
1575
|
-
state[stateKey] = parsedValue;
|
|
1576
|
-
} else {
|
|
1577
|
-
state[stateKey] = parsedValue[argumentName];
|
|
1578
|
-
}
|
|
1579
|
-
}
|
|
1580
|
-
return state;
|
|
1581
|
-
}
|
|
1582
|
-
}, "StreamingStateExtractor");
|
|
1583
|
-
function langGraphDefaultMergeState(state, messages, actions) {
|
|
1584
|
-
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1585
|
-
messages = messages.slice(1);
|
|
1586
|
-
}
|
|
1587
|
-
const mergedMessages = state.messages || [];
|
|
1588
|
-
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1589
|
-
for (const message of messages) {
|
|
1590
|
-
if (!existingMessageIds.has(message.id)) {
|
|
1591
|
-
mergedMessages.push(message);
|
|
1592
|
-
}
|
|
1333
|
+
// src/lib/runtime/remote-actions.ts
|
|
1334
|
+
function isLangGraphAgentAction(action) {
|
|
1335
|
+
if (!action) {
|
|
1336
|
+
return false;
|
|
1593
1337
|
}
|
|
1594
|
-
return
|
|
1595
|
-
messages: mergedMessages,
|
|
1596
|
-
copilotkit: {
|
|
1597
|
-
actions
|
|
1598
|
-
}
|
|
1599
|
-
});
|
|
1338
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
1600
1339
|
}
|
|
1601
|
-
__name(
|
|
1602
|
-
function
|
|
1603
|
-
|
|
1604
|
-
|
|
1340
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1341
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1342
|
+
const headers = {
|
|
1343
|
+
"Content-Type": "application/json"
|
|
1605
1344
|
};
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
...obj2[key]
|
|
1613
|
-
};
|
|
1614
|
-
}
|
|
1615
|
-
} else {
|
|
1616
|
-
result[key] = obj2[key];
|
|
1345
|
+
if (onBeforeRequest) {
|
|
1346
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1347
|
+
ctx: graphqlContext
|
|
1348
|
+
});
|
|
1349
|
+
if (additionalHeaders) {
|
|
1350
|
+
Object.assign(headers, additionalHeaders);
|
|
1617
1351
|
}
|
|
1618
1352
|
}
|
|
1619
|
-
return
|
|
1353
|
+
return headers;
|
|
1620
1354
|
}
|
|
1621
|
-
__name(
|
|
1622
|
-
function
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
if ("actionExecutionId" in message) {
|
|
1355
|
+
__name(createHeaders, "createHeaders");
|
|
1356
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1357
|
+
logger2.debug({
|
|
1358
|
+
url
|
|
1359
|
+
}, "Fetching actions from url");
|
|
1360
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
1361
|
+
try {
|
|
1362
|
+
const response = await fetch(`${url}/info`, {
|
|
1363
|
+
method: "POST",
|
|
1364
|
+
headers,
|
|
1365
|
+
body: JSON.stringify({
|
|
1366
|
+
properties: graphqlContext.properties,
|
|
1367
|
+
frontendUrl
|
|
1368
|
+
})
|
|
1369
|
+
});
|
|
1370
|
+
if (!response.ok) {
|
|
1371
|
+
logger2.error({
|
|
1372
|
+
url,
|
|
1373
|
+
status: response.status,
|
|
1374
|
+
body: await response.text()
|
|
1375
|
+
}, "Failed to fetch actions from url");
|
|
1643
1376
|
return {
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
name: message["actionName"],
|
|
1647
|
-
tool_call_id: message["actionExecutionId"],
|
|
1648
|
-
role: message["role"] ?? MessageRole.user
|
|
1377
|
+
actions: [],
|
|
1378
|
+
agents: []
|
|
1649
1379
|
};
|
|
1650
1380
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
}
|
|
1664
|
-
|
|
1665
|
-
var _a;
|
|
1666
|
-
logger2.debug({
|
|
1667
|
-
actionName: agent.name
|
|
1668
|
-
}, "Executing LangGraph Cloud agent");
|
|
1669
|
-
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
1670
|
-
let state = {};
|
|
1671
|
-
if (agentStates) {
|
|
1672
|
-
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
1673
|
-
if (jsonState) {
|
|
1674
|
-
state = JSON.parse(jsonState);
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
|
-
try {
|
|
1678
|
-
const response = await execute({
|
|
1679
|
-
deploymentUrl: endpoint.deploymentUrl,
|
|
1680
|
-
langsmithApiKey: endpoint.langsmithApiKey,
|
|
1681
|
-
agent,
|
|
1682
|
-
threadId,
|
|
1683
|
-
nodeName,
|
|
1684
|
-
messages,
|
|
1685
|
-
state,
|
|
1686
|
-
properties: graphqlContext.properties,
|
|
1687
|
-
actions: actionInputsWithoutAgents.map((action) => ({
|
|
1688
|
-
name: action.name,
|
|
1689
|
-
description: action.description,
|
|
1690
|
-
parameters: JSON.parse(action.jsonSchema)
|
|
1691
|
-
}))
|
|
1692
|
-
});
|
|
1693
|
-
const eventSource = new RemoteLangGraphEventSource();
|
|
1694
|
-
streamResponse(response, eventSource.eventStream$);
|
|
1695
|
-
return eventSource.processLangGraphEvents();
|
|
1696
|
-
} catch (error) {
|
|
1697
|
-
logger2.error({
|
|
1698
|
-
url: endpoint.deploymentUrl,
|
|
1699
|
-
status: 500,
|
|
1700
|
-
body: error.message
|
|
1701
|
-
}, "Failed to execute LangGraph Cloud agent");
|
|
1702
|
-
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
}));
|
|
1706
|
-
return [
|
|
1707
|
-
...agents
|
|
1708
|
-
];
|
|
1381
|
+
const json = await response.json();
|
|
1382
|
+
logger2.debug({
|
|
1383
|
+
json
|
|
1384
|
+
}, "Fetched actions from url");
|
|
1385
|
+
return json;
|
|
1386
|
+
} catch (error) {
|
|
1387
|
+
logger2.error({
|
|
1388
|
+
error: error.message ? error.message : error + ""
|
|
1389
|
+
}, "Failed to fetch actions from url");
|
|
1390
|
+
return {
|
|
1391
|
+
actions: [],
|
|
1392
|
+
agents: []
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1709
1395
|
}
|
|
1710
|
-
__name(
|
|
1396
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
1711
1397
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1712
1398
|
const actions = json["actions"].map((action) => ({
|
|
1713
1399
|
name: action.name,
|
|
@@ -1799,7 +1485,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1799
1485
|
throw new Error("Failed to execute remote agent");
|
|
1800
1486
|
}
|
|
1801
1487
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1802
|
-
streamResponse(response
|
|
1488
|
+
eventSource.streamResponse(response);
|
|
1803
1489
|
return eventSource.processLangGraphEvents();
|
|
1804
1490
|
}
|
|
1805
1491
|
}));
|
|
@@ -1809,165 +1495,34 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1809
1495
|
];
|
|
1810
1496
|
}
|
|
1811
1497
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1812
|
-
async function
|
|
1813
|
-
const reader = response.getReader();
|
|
1814
|
-
const decoder = new TextDecoder();
|
|
1815
|
-
let buffer = [];
|
|
1816
|
-
function flushBuffer() {
|
|
1817
|
-
const currentBuffer = buffer.join("");
|
|
1818
|
-
if (currentBuffer.trim().length === 0) {
|
|
1819
|
-
return;
|
|
1820
|
-
}
|
|
1821
|
-
const parts = currentBuffer.split("\n");
|
|
1822
|
-
if (parts.length === 0) {
|
|
1823
|
-
return;
|
|
1824
|
-
}
|
|
1825
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1826
|
-
buffer = [];
|
|
1827
|
-
if (!lastPartIsComplete) {
|
|
1828
|
-
buffer.push(parts.pop());
|
|
1829
|
-
}
|
|
1830
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1831
|
-
eventStream$.next(JSON.parse(part));
|
|
1832
|
-
});
|
|
1833
|
-
}
|
|
1834
|
-
__name(flushBuffer, "flushBuffer");
|
|
1835
|
-
try {
|
|
1836
|
-
while (true) {
|
|
1837
|
-
const { done, value } = await reader.read();
|
|
1838
|
-
if (!done) {
|
|
1839
|
-
buffer.push(decoder.decode(value, {
|
|
1840
|
-
stream: true
|
|
1841
|
-
}));
|
|
1842
|
-
}
|
|
1843
|
-
flushBuffer();
|
|
1844
|
-
if (done) {
|
|
1845
|
-
break;
|
|
1846
|
-
}
|
|
1847
|
-
}
|
|
1848
|
-
} catch (error) {
|
|
1849
|
-
console.error("Error in stream", error);
|
|
1850
|
-
eventStream$.error(error);
|
|
1851
|
-
return;
|
|
1852
|
-
}
|
|
1853
|
-
eventStream$.complete();
|
|
1854
|
-
}
|
|
1855
|
-
__name(streamResponse, "streamResponse");
|
|
1856
|
-
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
1857
|
-
const headers = {
|
|
1858
|
-
"Content-Type": "application/json"
|
|
1859
|
-
};
|
|
1860
|
-
if (onBeforeRequest) {
|
|
1861
|
-
const { headers: additionalHeaders } = onBeforeRequest({
|
|
1862
|
-
ctx: graphqlContext
|
|
1863
|
-
});
|
|
1864
|
-
if (additionalHeaders) {
|
|
1865
|
-
Object.assign(headers, additionalHeaders);
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
return headers;
|
|
1869
|
-
}
|
|
1870
|
-
__name(createHeaders, "createHeaders");
|
|
1871
|
-
|
|
1872
|
-
// src/lib/runtime/remote-actions.ts
|
|
1873
|
-
var EndpointType;
|
|
1874
|
-
(function(EndpointType2) {
|
|
1875
|
-
EndpointType2["CopilotKit"] = "copilotKit";
|
|
1876
|
-
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
1877
|
-
})(EndpointType || (EndpointType = {}));
|
|
1878
|
-
function isLangGraphAgentAction(action) {
|
|
1879
|
-
if (!action) {
|
|
1880
|
-
return false;
|
|
1881
|
-
}
|
|
1882
|
-
return typeof action.langGraphAgentHandler === "function";
|
|
1883
|
-
}
|
|
1884
|
-
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
1885
|
-
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
1886
|
-
logger2.debug({
|
|
1887
|
-
url
|
|
1888
|
-
}, "Fetching actions from url");
|
|
1889
|
-
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
1890
|
-
try {
|
|
1891
|
-
const response = await fetch(`${url}/info`, {
|
|
1892
|
-
method: "POST",
|
|
1893
|
-
headers,
|
|
1894
|
-
body: JSON.stringify({
|
|
1895
|
-
properties: graphqlContext.properties,
|
|
1896
|
-
frontendUrl
|
|
1897
|
-
})
|
|
1898
|
-
});
|
|
1899
|
-
if (!response.ok) {
|
|
1900
|
-
logger2.error({
|
|
1901
|
-
url,
|
|
1902
|
-
status: response.status,
|
|
1903
|
-
body: await response.text()
|
|
1904
|
-
}, "Failed to fetch actions from url");
|
|
1905
|
-
return {
|
|
1906
|
-
actions: [],
|
|
1907
|
-
agents: []
|
|
1908
|
-
};
|
|
1909
|
-
}
|
|
1910
|
-
const json = await response.json();
|
|
1911
|
-
logger2.debug({
|
|
1912
|
-
json
|
|
1913
|
-
}, "Fetched actions from url");
|
|
1914
|
-
return json;
|
|
1915
|
-
} catch (error) {
|
|
1916
|
-
logger2.error({
|
|
1917
|
-
error: error.message ? error.message : error + ""
|
|
1918
|
-
}, "Failed to fetch actions from url");
|
|
1919
|
-
return {
|
|
1920
|
-
actions: [],
|
|
1921
|
-
agents: []
|
|
1922
|
-
};
|
|
1923
|
-
}
|
|
1924
|
-
}
|
|
1925
|
-
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
1926
|
-
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1498
|
+
async function setupRemoteActions({ remoteActionDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1927
1499
|
const logger2 = graphqlContext.logger.child({
|
|
1928
1500
|
component: "remote-actions.fetchRemoteActions"
|
|
1929
1501
|
});
|
|
1930
1502
|
logger2.debug({
|
|
1931
|
-
|
|
1932
|
-
}, "Fetching
|
|
1933
|
-
const filtered =
|
|
1934
|
-
|
|
1935
|
-
return value;
|
|
1936
|
-
}
|
|
1937
|
-
return index === self.findIndex((t) => t.url === value.url);
|
|
1938
|
-
});
|
|
1939
|
-
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
1940
|
-
if (endpoint.type === "langgraph-cloud") {
|
|
1941
|
-
return constructLGCRemoteAction({
|
|
1942
|
-
endpoint,
|
|
1943
|
-
messages,
|
|
1944
|
-
graphqlContext,
|
|
1945
|
-
logger: logger2.child({
|
|
1946
|
-
component: "remote-actions.constructLGCRemoteAction",
|
|
1947
|
-
endpoint
|
|
1948
|
-
}),
|
|
1949
|
-
agentStates
|
|
1950
|
-
});
|
|
1951
|
-
}
|
|
1503
|
+
remoteActionDefinitions
|
|
1504
|
+
}, "Fetching remote actions");
|
|
1505
|
+
const filtered = remoteActionDefinitions.filter((value, index, self) => index === self.findIndex((t) => t.url === value.url));
|
|
1506
|
+
const result = await Promise.all(filtered.map(async (actionDefinition) => {
|
|
1952
1507
|
const json = await fetchRemoteInfo({
|
|
1953
|
-
url:
|
|
1954
|
-
onBeforeRequest:
|
|
1508
|
+
url: actionDefinition.url,
|
|
1509
|
+
onBeforeRequest: actionDefinition.onBeforeRequest,
|
|
1955
1510
|
graphqlContext,
|
|
1956
1511
|
logger: logger2.child({
|
|
1957
1512
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1958
|
-
|
|
1513
|
+
actionDefinition
|
|
1959
1514
|
}),
|
|
1960
1515
|
frontendUrl
|
|
1961
1516
|
});
|
|
1962
1517
|
return constructRemoteActions({
|
|
1963
1518
|
json,
|
|
1964
1519
|
messages,
|
|
1965
|
-
url:
|
|
1966
|
-
onBeforeRequest:
|
|
1520
|
+
url: actionDefinition.url,
|
|
1521
|
+
onBeforeRequest: actionDefinition.onBeforeRequest,
|
|
1967
1522
|
graphqlContext,
|
|
1968
1523
|
logger: logger2.child({
|
|
1969
1524
|
component: "remote-actions.constructActions",
|
|
1970
|
-
|
|
1525
|
+
actionDefinition
|
|
1971
1526
|
}),
|
|
1972
1527
|
agentStates
|
|
1973
1528
|
});
|
|
@@ -2131,7 +1686,13 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
2131
1686
|
}
|
|
2132
1687
|
let args = [];
|
|
2133
1688
|
if (actionArguments) {
|
|
2134
|
-
|
|
1689
|
+
try {
|
|
1690
|
+
args = JSON.parse(actionArguments);
|
|
1691
|
+
} catch (e) {
|
|
1692
|
+
console.warn("Action argument unparsable", {
|
|
1693
|
+
actionArguments
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
2135
1696
|
}
|
|
2136
1697
|
if (isLangGraphAgentAction(action)) {
|
|
2137
1698
|
eventStream$.sendActionExecutionResult(actionExecutionId, action.name, `${action.name} agent started`);
|
|
@@ -2159,7 +1720,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
2159
1720
|
__name(executeAction, "executeAction");
|
|
2160
1721
|
|
|
2161
1722
|
// src/graphql/types/base/index.ts
|
|
2162
|
-
var
|
|
1723
|
+
var import_type_graphql = require("type-graphql");
|
|
2163
1724
|
function _ts_decorate(decorators, target, key, desc) {
|
|
2164
1725
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2165
1726
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -2182,15 +1743,15 @@ var BaseMessageInput = class {
|
|
|
2182
1743
|
};
|
|
2183
1744
|
__name(BaseMessageInput, "BaseMessageInput");
|
|
2184
1745
|
_ts_decorate([
|
|
2185
|
-
(0,
|
|
1746
|
+
(0, import_type_graphql.Field)(() => String),
|
|
2186
1747
|
_ts_metadata("design:type", String)
|
|
2187
1748
|
], BaseMessageInput.prototype, "id", void 0);
|
|
2188
1749
|
_ts_decorate([
|
|
2189
|
-
(0,
|
|
1750
|
+
(0, import_type_graphql.Field)(() => Date),
|
|
2190
1751
|
_ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
|
|
2191
1752
|
], BaseMessageInput.prototype, "createdAt", void 0);
|
|
2192
1753
|
BaseMessageInput = _ts_decorate([
|
|
2193
|
-
(0,
|
|
1754
|
+
(0, import_type_graphql.InputType)()
|
|
2194
1755
|
], BaseMessageInput);
|
|
2195
1756
|
|
|
2196
1757
|
// src/graphql/types/converted/index.ts
|
|
@@ -2294,7 +1855,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
|
2294
1855
|
var import_rxjs3 = require("rxjs");
|
|
2295
1856
|
var CopilotRuntime = class {
|
|
2296
1857
|
actions;
|
|
2297
|
-
|
|
1858
|
+
remoteActionDefinitions;
|
|
2298
1859
|
langserve = [];
|
|
2299
1860
|
onBeforeRequest;
|
|
2300
1861
|
onAfterRequest;
|
|
@@ -2305,7 +1866,7 @@ var CopilotRuntime = class {
|
|
|
2305
1866
|
const remoteChain = new RemoteChain(chain);
|
|
2306
1867
|
this.langserve.push(remoteChain.toAction());
|
|
2307
1868
|
}
|
|
2308
|
-
this.
|
|
1869
|
+
this.remoteActionDefinitions = (params == null ? void 0 : params.remoteActions) || [];
|
|
2309
1870
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
2310
1871
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
2311
1872
|
}
|
|
@@ -2374,7 +1935,7 @@ var CopilotRuntime = class {
|
|
|
2374
1935
|
async processAgentRequest(request) {
|
|
2375
1936
|
var _a;
|
|
2376
1937
|
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
2377
|
-
const { threadId, agentName, nodeName } = agentSession;
|
|
1938
|
+
const { threadId = (0, import_shared8.randomId)(), agentName, nodeName } = agentSession;
|
|
2378
1939
|
const serverSideActions = await this.getServerSideActions(request);
|
|
2379
1940
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
2380
1941
|
const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
|
|
@@ -2446,12 +2007,8 @@ var CopilotRuntime = class {
|
|
|
2446
2007
|
console.error("Error loading langserve chain:", error);
|
|
2447
2008
|
}
|
|
2448
2009
|
}
|
|
2449
|
-
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
2450
|
-
...endpoint,
|
|
2451
|
-
type: this.resolveEndpointType(endpoint)
|
|
2452
|
-
}));
|
|
2453
2010
|
const remoteActions = await setupRemoteActions({
|
|
2454
|
-
|
|
2011
|
+
remoteActionDefinitions: this.remoteActionDefinitions,
|
|
2455
2012
|
graphqlContext,
|
|
2456
2013
|
messages: inputMessages,
|
|
2457
2014
|
agentStates,
|
|
@@ -2467,12 +2024,6 @@ var CopilotRuntime = class {
|
|
|
2467
2024
|
...remoteActions
|
|
2468
2025
|
];
|
|
2469
2026
|
}
|
|
2470
|
-
resolveEndpointType(endpoint) {
|
|
2471
|
-
if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
2472
|
-
return EndpointType.LangGraphCloud;
|
|
2473
|
-
}
|
|
2474
|
-
return endpoint.type;
|
|
2475
|
-
}
|
|
2476
2027
|
};
|
|
2477
2028
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
2478
2029
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -2487,20 +2038,6 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
|
2487
2038
|
return allTools;
|
|
2488
2039
|
}
|
|
2489
2040
|
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
2490
|
-
function copilotKitEndpoint(config2) {
|
|
2491
|
-
return {
|
|
2492
|
-
...config2,
|
|
2493
|
-
type: EndpointType.CopilotKit
|
|
2494
|
-
};
|
|
2495
|
-
}
|
|
2496
|
-
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
2497
|
-
function langGraphCloudEndpoint(config2) {
|
|
2498
|
-
return {
|
|
2499
|
-
...config2,
|
|
2500
|
-
type: EndpointType.LangGraphCloud
|
|
2501
|
-
};
|
|
2502
|
-
}
|
|
2503
|
-
__name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
|
|
2504
2041
|
|
|
2505
2042
|
// src/lib/integrations/shared.ts
|
|
2506
2043
|
var import_type_graphql16 = require("type-graphql");
|
|
@@ -2514,6 +2051,43 @@ var import_type_graphql11 = require("type-graphql");
|
|
|
2514
2051
|
|
|
2515
2052
|
// src/graphql/inputs/message.input.ts
|
|
2516
2053
|
var import_type_graphql3 = require("type-graphql");
|
|
2054
|
+
|
|
2055
|
+
// src/graphql/types/enums.ts
|
|
2056
|
+
var import_type_graphql2 = require("type-graphql");
|
|
2057
|
+
var MessageRole;
|
|
2058
|
+
(function(MessageRole2) {
|
|
2059
|
+
MessageRole2["user"] = "user";
|
|
2060
|
+
MessageRole2["assistant"] = "assistant";
|
|
2061
|
+
MessageRole2["system"] = "system";
|
|
2062
|
+
})(MessageRole || (MessageRole = {}));
|
|
2063
|
+
var ActionExecutionScope;
|
|
2064
|
+
(function(ActionExecutionScope2) {
|
|
2065
|
+
ActionExecutionScope2["server"] = "server";
|
|
2066
|
+
ActionExecutionScope2["client"] = "client";
|
|
2067
|
+
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
2068
|
+
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
2069
|
+
var CopilotRequestType;
|
|
2070
|
+
(function(CopilotRequestType2) {
|
|
2071
|
+
CopilotRequestType2["Chat"] = "Chat";
|
|
2072
|
+
CopilotRequestType2["Task"] = "Task";
|
|
2073
|
+
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
2074
|
+
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
2075
|
+
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
2076
|
+
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
2077
|
+
(0, import_type_graphql2.registerEnumType)(MessageRole, {
|
|
2078
|
+
name: "MessageRole",
|
|
2079
|
+
description: "The role of the message"
|
|
2080
|
+
});
|
|
2081
|
+
(0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
|
|
2082
|
+
name: "ActionExecutionScope",
|
|
2083
|
+
description: "The scope of the action"
|
|
2084
|
+
});
|
|
2085
|
+
(0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
|
|
2086
|
+
name: "CopilotRequestType",
|
|
2087
|
+
description: "The type of Copilot request"
|
|
2088
|
+
});
|
|
2089
|
+
|
|
2090
|
+
// src/graphql/inputs/message.input.ts
|
|
2517
2091
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
2518
2092
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2519
2093
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -4170,7 +3744,6 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
4170
3744
|
UnifyAdapter,
|
|
4171
3745
|
buildSchema,
|
|
4172
3746
|
config,
|
|
4173
|
-
copilotKitEndpoint,
|
|
4174
3747
|
copilotRuntimeNestEndpoint,
|
|
4175
3748
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
4176
3749
|
copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -4178,7 +3751,6 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
4178
3751
|
copilotRuntimeNodeHttpEndpoint,
|
|
4179
3752
|
createContext,
|
|
4180
3753
|
flattenToolCallsNoDuplicates,
|
|
4181
|
-
getCommonConfig
|
|
4182
|
-
langGraphCloudEndpoint
|
|
3754
|
+
getCommonConfig
|
|
4183
3755
|
});
|
|
4184
3756
|
//# sourceMappingURL=index.js.map
|