@copilotkit/runtime 1.3.13 → 1.3.14-feat-langgraph-cloud.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/{chunk-MCUYYSCJ.mjs → chunk-44JZSRY7.mjs} +2 -2
- package/dist/{chunk-SPZSO3T3.mjs → chunk-7MKHQGLE.mjs} +2 -2
- package/dist/{chunk-MYZHUCL6.mjs → chunk-B74M7FXG.mjs} +1 -1
- package/dist/chunk-B74M7FXG.mjs.map +1 -0
- package/dist/{chunk-DEZQR4EM.mjs → chunk-PY76HMMJ.mjs} +2 -2
- package/dist/{chunk-3LFBIWWK.mjs → chunk-UF6TJDZ5.mjs} +36 -9
- package/dist/chunk-UF6TJDZ5.mjs.map +1 -0
- package/dist/{chunk-76K222WC.mjs → chunk-ZIHDDSMJ.mjs} +689 -151
- package/dist/chunk-ZIHDDSMJ.mjs.map +1 -0
- package/dist/{copilot-runtime-df3527ad.d.ts → copilot-runtime-68acb673.d.ts} +29 -7
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -1
- package/dist/{groq-adapter-b122e71f.d.ts → groq-adapter-c2e65c95.d.ts} +1 -1
- package/dist/{index-cff31380.d.ts → index-439dfa2a.d.ts} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +761 -196
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -8
- package/dist/index.mjs.map +1 -1
- package/dist/{langserve-a14a6849.d.ts → langserve-0d844922.d.ts} +1 -1
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +761 -196
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +12 -8
- package/dist/lib/integrations/index.d.ts +4 -4
- package/dist/lib/integrations/index.js +3 -1
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +5 -5
- package/dist/lib/integrations/nest/index.d.ts +3 -3
- package/dist/lib/integrations/nest/index.js +3 -1
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +3 -3
- package/dist/lib/integrations/node-express/index.d.ts +3 -3
- package/dist/lib/integrations/node-express/index.js +3 -1
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +3 -3
- package/dist/lib/integrations/node-http/index.d.ts +3 -3
- package/dist/lib/integrations/node-http/index.js +3 -1
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +2 -2
- package/dist/service-adapters/index.d.ts +4 -4
- package/package.json +6 -4
- package/src/agents/langgraph/event-source.ts +62 -94
- package/src/agents/langgraph/events.ts +26 -17
- package/src/graphql/types/converted/index.ts +5 -1
- package/src/lib/runtime/copilot-runtime.ts +58 -11
- package/src/lib/runtime/remote-action-constructors.ts +284 -0
- package/src/lib/runtime/remote-actions.ts +65 -159
- package/src/lib/runtime/remote-lg-cloud-action.ts +624 -0
- package/dist/chunk-3LFBIWWK.mjs.map +0 -1
- package/dist/chunk-76K222WC.mjs.map +0 -1
- package/dist/chunk-MYZHUCL6.mjs.map +0 -1
- /package/dist/{chunk-MCUYYSCJ.mjs.map → chunk-44JZSRY7.mjs.map} +0 -0
- /package/dist/{chunk-SPZSO3T3.mjs.map → chunk-7MKHQGLE.mjs.map} +0 -0
- /package/dist/{chunk-DEZQR4EM.mjs.map → chunk-PY76HMMJ.mjs.map} +0 -0
package/dist/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.14-feat-langgraph-cloud.0",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -87,6 +87,7 @@ var require_package = __commonJS({
|
|
|
87
87
|
"@langchain/community": "^0.0.53",
|
|
88
88
|
"@langchain/core": "^0.3.13",
|
|
89
89
|
"@langchain/google-gauth": "^0.1.0",
|
|
90
|
+
"@langchain/langgraph-sdk": "^0.0.16",
|
|
90
91
|
"@langchain/openai": "^0.0.28",
|
|
91
92
|
"class-transformer": "^0.5.1",
|
|
92
93
|
express: "^4.19.2",
|
|
@@ -96,6 +97,7 @@ var require_package = __commonJS({
|
|
|
96
97
|
"groq-sdk": "^0.5.0",
|
|
97
98
|
langchain: "^0.3.3",
|
|
98
99
|
openai: "^4.50.0",
|
|
100
|
+
"partial-json": "^0.1.7",
|
|
99
101
|
pino: "^9.2.0",
|
|
100
102
|
"pino-pretty": "^11.2.1",
|
|
101
103
|
"reflect-metadata": "^0.2.2",
|
|
@@ -136,6 +138,7 @@ __export(src_exports, {
|
|
|
136
138
|
UnknownErrorResponse: () => UnknownErrorResponse,
|
|
137
139
|
buildSchema: () => buildSchema,
|
|
138
140
|
config: () => config,
|
|
141
|
+
copilotKitEndpoint: () => copilotKitEndpoint,
|
|
139
142
|
copilotRuntimeNestEndpoint: () => copilotRuntimeNestEndpoint,
|
|
140
143
|
copilotRuntimeNextJSAppRouterEndpoint: () => copilotRuntimeNextJSAppRouterEndpoint,
|
|
141
144
|
copilotRuntimeNextJSPagesRouterEndpoint: () => copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -143,7 +146,8 @@ __export(src_exports, {
|
|
|
143
146
|
copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
|
|
144
147
|
createContext: () => createContext,
|
|
145
148
|
flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
|
|
146
|
-
getCommonConfig: () => getCommonConfig
|
|
149
|
+
getCommonConfig: () => getCommonConfig,
|
|
150
|
+
langGraphCloudEndpoint: () => langGraphCloudEndpoint
|
|
147
151
|
});
|
|
148
152
|
module.exports = __toCommonJS(src_exports);
|
|
149
153
|
var import_reflect_metadata = require("reflect-metadata");
|
|
@@ -1354,58 +1358,19 @@ var LangGraphEventTypes;
|
|
|
1354
1358
|
LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
|
|
1355
1359
|
LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
|
|
1356
1360
|
LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
|
|
1357
|
-
LangGraphEventTypes2["
|
|
1358
|
-
LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
|
|
1361
|
+
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
|
|
1359
1362
|
})(LangGraphEventTypes || (LangGraphEventTypes = {}));
|
|
1363
|
+
var CustomEventNames;
|
|
1364
|
+
(function(CustomEventNames2) {
|
|
1365
|
+
CustomEventNames2["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
|
|
1366
|
+
CustomEventNames2["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
|
|
1367
|
+
CustomEventNames2["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
|
|
1368
|
+
})(CustomEventNames || (CustomEventNames = {}));
|
|
1360
1369
|
|
|
1361
1370
|
// src/agents/langgraph/event-source.ts
|
|
1362
1371
|
var import_shared8 = require("@copilotkit/shared");
|
|
1363
1372
|
var RemoteLangGraphEventSource = class {
|
|
1364
1373
|
eventStream$ = new import_rxjs.ReplaySubject();
|
|
1365
|
-
async streamResponse(response) {
|
|
1366
|
-
const reader = response.body.getReader();
|
|
1367
|
-
const decoder = new TextDecoder();
|
|
1368
|
-
let buffer = [];
|
|
1369
|
-
const eventStream$ = this.eventStream$;
|
|
1370
|
-
function flushBuffer() {
|
|
1371
|
-
const currentBuffer = buffer.join("");
|
|
1372
|
-
if (currentBuffer.trim().length === 0) {
|
|
1373
|
-
return;
|
|
1374
|
-
}
|
|
1375
|
-
const parts = currentBuffer.split("\n");
|
|
1376
|
-
if (parts.length === 0) {
|
|
1377
|
-
return;
|
|
1378
|
-
}
|
|
1379
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1380
|
-
buffer = [];
|
|
1381
|
-
if (!lastPartIsComplete) {
|
|
1382
|
-
buffer.push(parts.pop());
|
|
1383
|
-
}
|
|
1384
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1385
|
-
eventStream$.next(JSON.parse(part));
|
|
1386
|
-
});
|
|
1387
|
-
}
|
|
1388
|
-
__name(flushBuffer, "flushBuffer");
|
|
1389
|
-
try {
|
|
1390
|
-
while (true) {
|
|
1391
|
-
const { done, value } = await reader.read();
|
|
1392
|
-
if (!done) {
|
|
1393
|
-
buffer.push(decoder.decode(value, {
|
|
1394
|
-
stream: true
|
|
1395
|
-
}));
|
|
1396
|
-
}
|
|
1397
|
-
flushBuffer();
|
|
1398
|
-
if (done) {
|
|
1399
|
-
break;
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
} catch (error) {
|
|
1403
|
-
console.error("Error in stream", error);
|
|
1404
|
-
eventStream$.error(error);
|
|
1405
|
-
return;
|
|
1406
|
-
}
|
|
1407
|
-
eventStream$.complete();
|
|
1408
|
-
}
|
|
1409
1374
|
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
1410
1375
|
if (typeof shouldEmitToolCalls === "boolean") {
|
|
1411
1376
|
return shouldEmitToolCalls;
|
|
@@ -1428,20 +1393,25 @@ var RemoteLangGraphEventSource = class {
|
|
|
1428
1393
|
} else {
|
|
1429
1394
|
acc.content = null;
|
|
1430
1395
|
}
|
|
1431
|
-
|
|
1396
|
+
const toolCallChunks = (
|
|
1397
|
+
// @ts-expect-error -- LangGraph Cloud implementation stores data outside of kwargs
|
|
1398
|
+
((_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)
|
|
1399
|
+
);
|
|
1400
|
+
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);
|
|
1401
|
+
if (toolCallChunks && toolCallChunks.length > 0) {
|
|
1432
1402
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1433
|
-
acc.toolCallMessageId =
|
|
1434
|
-
if ((
|
|
1435
|
-
acc.toolCallName =
|
|
1403
|
+
acc.toolCallMessageId = toolCallMessageId;
|
|
1404
|
+
if ((_p = toolCallChunks[0]) == null ? void 0 : _p.name) {
|
|
1405
|
+
acc.toolCallName = toolCallChunks[0].name;
|
|
1436
1406
|
}
|
|
1437
|
-
if ((
|
|
1438
|
-
acc.toolCallId =
|
|
1407
|
+
if ((_q = toolCallChunks[0]) == null ? void 0 : _q.id) {
|
|
1408
|
+
acc.toolCallId = toolCallChunks[0].id;
|
|
1439
1409
|
}
|
|
1440
1410
|
acc.prevMessageId = acc.messageId;
|
|
1441
|
-
acc.messageId =
|
|
1411
|
+
acc.messageId = toolCallMessageId;
|
|
1442
1412
|
} else if (acc.content && acc.content != "") {
|
|
1443
1413
|
acc.prevMessageId = acc.messageId;
|
|
1444
|
-
acc.messageId =
|
|
1414
|
+
acc.messageId = toolCallMessageId;
|
|
1445
1415
|
} else {
|
|
1446
1416
|
acc.prevToolCallMessageId = acc.toolCallMessageId;
|
|
1447
1417
|
acc.prevMessageId = acc.messageId;
|
|
@@ -1466,7 +1436,7 @@ var RemoteLangGraphEventSource = class {
|
|
|
1466
1436
|
prevMessageId: null,
|
|
1467
1437
|
content: null
|
|
1468
1438
|
}), (0, import_rxjs.mergeMap)((eventWithState) => {
|
|
1469
|
-
var _a, _b, _c, _d, _e;
|
|
1439
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
1470
1440
|
const events = [];
|
|
1471
1441
|
let shouldEmitMessages = true;
|
|
1472
1442
|
let shouldEmitToolCalls = false;
|
|
@@ -1489,32 +1459,33 @@ var RemoteLangGraphEventSource = class {
|
|
|
1489
1459
|
});
|
|
1490
1460
|
}
|
|
1491
1461
|
switch (eventWithState.event.event) {
|
|
1492
|
-
case LangGraphEventTypes.
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1462
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
1463
|
+
if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
1464
|
+
events.push({
|
|
1465
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
1466
|
+
messageId: eventWithState.event.data.message_id
|
|
1467
|
+
});
|
|
1468
|
+
events.push({
|
|
1469
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
1470
|
+
content: eventWithState.event.data.message
|
|
1471
|
+
});
|
|
1472
|
+
events.push({
|
|
1473
|
+
type: RuntimeEventTypes.TextMessageEnd
|
|
1474
|
+
});
|
|
1475
|
+
} else if (eventWithState.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
1476
|
+
events.push({
|
|
1477
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1478
|
+
actionExecutionId: eventWithState.event.data.id,
|
|
1479
|
+
actionName: eventWithState.event.data.name
|
|
1480
|
+
});
|
|
1481
|
+
events.push({
|
|
1482
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1483
|
+
args: JSON.stringify(eventWithState.event.data.args)
|
|
1484
|
+
});
|
|
1485
|
+
events.push({
|
|
1486
|
+
type: RuntimeEventTypes.ActionExecutionEnd
|
|
1487
|
+
});
|
|
1488
|
+
}
|
|
1518
1489
|
break;
|
|
1519
1490
|
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
1520
1491
|
events.push({
|
|
@@ -1549,7 +1520,8 @@ var RemoteLangGraphEventSource = class {
|
|
|
1549
1520
|
});
|
|
1550
1521
|
}
|
|
1551
1522
|
}
|
|
1552
|
-
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
|
|
1523
|
+
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
|
|
1524
|
+
((_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);
|
|
1553
1525
|
const content = eventWithState.content;
|
|
1554
1526
|
if (args) {
|
|
1555
1527
|
if (this.shouldEmitToolCall(shouldEmitToolCalls, eventWithState.toolCallName)) {
|
|
@@ -1600,70 +1572,543 @@ var RemoteLangGraphEventSource = class {
|
|
|
1600
1572
|
};
|
|
1601
1573
|
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
1602
1574
|
|
|
1603
|
-
// src/lib/runtime/remote-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1575
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1576
|
+
var import_langgraph_sdk = require("@langchain/langgraph-sdk");
|
|
1577
|
+
var import_node_crypto = require("crypto");
|
|
1578
|
+
var import_partial_json = require("partial-json");
|
|
1579
|
+
|
|
1580
|
+
// src/graphql/types/enums.ts
|
|
1581
|
+
var import_type_graphql = require("type-graphql");
|
|
1582
|
+
var MessageRole;
|
|
1583
|
+
(function(MessageRole2) {
|
|
1584
|
+
MessageRole2["user"] = "user";
|
|
1585
|
+
MessageRole2["assistant"] = "assistant";
|
|
1586
|
+
MessageRole2["system"] = "system";
|
|
1587
|
+
})(MessageRole || (MessageRole = {}));
|
|
1588
|
+
var ActionExecutionScope;
|
|
1589
|
+
(function(ActionExecutionScope2) {
|
|
1590
|
+
ActionExecutionScope2["server"] = "server";
|
|
1591
|
+
ActionExecutionScope2["client"] = "client";
|
|
1592
|
+
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
1593
|
+
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
1594
|
+
var CopilotRequestType;
|
|
1595
|
+
(function(CopilotRequestType2) {
|
|
1596
|
+
CopilotRequestType2["Chat"] = "Chat";
|
|
1597
|
+
CopilotRequestType2["Task"] = "Task";
|
|
1598
|
+
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
1599
|
+
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
1600
|
+
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
1601
|
+
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
1602
|
+
(0, import_type_graphql.registerEnumType)(MessageRole, {
|
|
1603
|
+
name: "MessageRole",
|
|
1604
|
+
description: "The role of the message"
|
|
1605
|
+
});
|
|
1606
|
+
(0, import_type_graphql.registerEnumType)(ActionExecutionScope, {
|
|
1607
|
+
name: "ActionExecutionScope",
|
|
1608
|
+
description: "The scope of the action"
|
|
1609
|
+
});
|
|
1610
|
+
(0, import_type_graphql.registerEnumType)(CopilotRequestType, {
|
|
1611
|
+
name: "CopilotRequestType",
|
|
1612
|
+
description: "The type of Copilot request"
|
|
1613
|
+
});
|
|
1614
|
+
|
|
1615
|
+
// src/lib/runtime/remote-lg-cloud-action.ts
|
|
1616
|
+
async function execute(args) {
|
|
1617
|
+
return new ReadableStream({
|
|
1618
|
+
async start(controller) {
|
|
1619
|
+
try {
|
|
1620
|
+
await streamEvents(controller, args);
|
|
1621
|
+
controller.close();
|
|
1622
|
+
} catch (err) {
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
});
|
|
1609
1626
|
}
|
|
1610
|
-
__name(
|
|
1611
|
-
function
|
|
1612
|
-
const
|
|
1613
|
-
|
|
1627
|
+
__name(execute, "execute");
|
|
1628
|
+
async function streamEvents(controller, args) {
|
|
1629
|
+
const { deploymentUrl, langsmithApiKey, threadId: agrsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, messages, actions, logger: logger2 } = args;
|
|
1630
|
+
let nodeName = initialNodeName;
|
|
1631
|
+
let state = initialState;
|
|
1632
|
+
const { name, assistantId: initialAssistantId } = agent;
|
|
1633
|
+
const client = new import_langgraph_sdk.Client({
|
|
1634
|
+
apiUrl: deploymentUrl,
|
|
1635
|
+
apiKey: langsmithApiKey
|
|
1636
|
+
});
|
|
1637
|
+
let initialThreadId = agrsInitialThreadId;
|
|
1638
|
+
const wasInitiatedWithExistingThread = !!initialThreadId;
|
|
1639
|
+
if (initialThreadId && initialThreadId.startsWith("ck-")) {
|
|
1640
|
+
initialThreadId = initialThreadId.substring(3);
|
|
1641
|
+
}
|
|
1642
|
+
const assistants = await client.assistants.search();
|
|
1643
|
+
const retrievedAssistant = assistants.find((a) => a.name === name);
|
|
1644
|
+
const threadId = initialThreadId ?? (0, import_node_crypto.randomUUID)();
|
|
1645
|
+
if (initialThreadId === threadId) {
|
|
1646
|
+
await client.threads.get(threadId);
|
|
1647
|
+
} else {
|
|
1648
|
+
await client.threads.create({
|
|
1649
|
+
threadId
|
|
1650
|
+
});
|
|
1651
|
+
}
|
|
1652
|
+
let agentState = {
|
|
1653
|
+
values: {}
|
|
1614
1654
|
};
|
|
1615
|
-
if (
|
|
1616
|
-
|
|
1617
|
-
|
|
1655
|
+
if (wasInitiatedWithExistingThread) {
|
|
1656
|
+
agentState = await client.threads.getState(threadId);
|
|
1657
|
+
}
|
|
1658
|
+
const agentStateValues = agentState.values;
|
|
1659
|
+
state.messages = agentStateValues.messages;
|
|
1660
|
+
const mode = wasInitiatedWithExistingThread && nodeName != "__end__" ? "continue" : "start";
|
|
1661
|
+
state = langGraphDefaultMergeState(state, formatMessages(messages), actions, name);
|
|
1662
|
+
if (mode === "continue") {
|
|
1663
|
+
await client.threads.updateState(threadId, {
|
|
1664
|
+
values: state,
|
|
1665
|
+
asNode: nodeName
|
|
1618
1666
|
});
|
|
1619
|
-
|
|
1620
|
-
|
|
1667
|
+
}
|
|
1668
|
+
const assistantId = initialAssistantId ?? (retrievedAssistant == null ? void 0 : retrievedAssistant.assistant_id);
|
|
1669
|
+
if (!assistantId) {
|
|
1670
|
+
console.error(`
|
|
1671
|
+
No agent found for the agent name specified in CopilotKit provider
|
|
1672
|
+
Please check your available agents or provide an agent ID in the LangGraph Cloud endpoint definition.
|
|
1673
|
+
|
|
1674
|
+
|
|
1675
|
+
These are the available agents: [${assistants.map((a) => `${a.name} (ID: ${a.assistant_id})`).join(", ")}]
|
|
1676
|
+
`);
|
|
1677
|
+
throw new Error("No agent id found");
|
|
1678
|
+
}
|
|
1679
|
+
const graphInfo = await client.assistants.getGraph(assistantId);
|
|
1680
|
+
const streamInput = mode === "start" ? state : null;
|
|
1681
|
+
let streamingStateExtractor = new StreamingStateExtractor([]);
|
|
1682
|
+
let prevNodeName = null;
|
|
1683
|
+
let emitIntermediateStateUntilEnd = null;
|
|
1684
|
+
let shouldExit = null;
|
|
1685
|
+
let externalRunId = null;
|
|
1686
|
+
const streamResponse2 = client.runs.stream(threadId, assistantId, {
|
|
1687
|
+
input: streamInput,
|
|
1688
|
+
streamMode: [
|
|
1689
|
+
"events",
|
|
1690
|
+
"values"
|
|
1691
|
+
]
|
|
1692
|
+
});
|
|
1693
|
+
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
1694
|
+
let latestStateValues = {};
|
|
1695
|
+
for await (const chunk of streamResponse2) {
|
|
1696
|
+
if (![
|
|
1697
|
+
"events",
|
|
1698
|
+
"values",
|
|
1699
|
+
"error"
|
|
1700
|
+
].includes(chunk.event))
|
|
1701
|
+
continue;
|
|
1702
|
+
if (chunk.event === "error") {
|
|
1703
|
+
logger2.error(chunk, `Error event thrown: ${chunk.data.message}`);
|
|
1704
|
+
throw new Error(`Error event thrown: ${chunk.data.message}`);
|
|
1705
|
+
}
|
|
1706
|
+
if (chunk.event === "values") {
|
|
1707
|
+
latestStateValues = chunk.data;
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
const event = chunk.data;
|
|
1711
|
+
const currentNodeName = event.name;
|
|
1712
|
+
const eventType = event.event;
|
|
1713
|
+
const runId = event.metadata.run_id;
|
|
1714
|
+
externalRunId = runId;
|
|
1715
|
+
const metadata = event.metadata;
|
|
1716
|
+
shouldExit = shouldExit != null ? shouldExit : metadata["copilotkit:exit"];
|
|
1717
|
+
const emitIntermediateState = metadata["copilotkit:emit-intermediate-state"];
|
|
1718
|
+
const forceEmitIntermediateState = metadata["copilotkit:force-emit-intermediate-state"];
|
|
1719
|
+
const manuallyEmitMessage = metadata["copilotkit:manually-emit-messages"];
|
|
1720
|
+
const manuallyEmitToolCall = metadata["copilotkit:manually-emit-tool-calls"];
|
|
1721
|
+
if (graphInfo["nodes"].some((node) => node.id === currentNodeName)) {
|
|
1722
|
+
nodeName = currentNodeName;
|
|
1723
|
+
}
|
|
1724
|
+
if (!nodeName) {
|
|
1725
|
+
continue;
|
|
1726
|
+
}
|
|
1727
|
+
if (forceEmitIntermediateState) {
|
|
1728
|
+
if (eventType === "on_chain_end") {
|
|
1729
|
+
state = event.data.output;
|
|
1730
|
+
emit(getStateSyncEvent({
|
|
1731
|
+
threadId,
|
|
1732
|
+
runId,
|
|
1733
|
+
agentName: agent.name,
|
|
1734
|
+
nodeName,
|
|
1735
|
+
state: event.data.output,
|
|
1736
|
+
running: true,
|
|
1737
|
+
active: true
|
|
1738
|
+
}));
|
|
1739
|
+
}
|
|
1740
|
+
continue;
|
|
1741
|
+
}
|
|
1742
|
+
if (manuallyEmitMessage) {
|
|
1743
|
+
if (eventType === "on_chain_end") {
|
|
1744
|
+
state = event.data.output;
|
|
1745
|
+
emit(JSON.stringify({
|
|
1746
|
+
event: "on_copilotkit_emit_message",
|
|
1747
|
+
message: event.data.output,
|
|
1748
|
+
messageId: (0, import_node_crypto.randomUUID)(),
|
|
1749
|
+
role: MessageRole.assistant
|
|
1750
|
+
}) + "\n");
|
|
1751
|
+
}
|
|
1752
|
+
continue;
|
|
1753
|
+
}
|
|
1754
|
+
if (manuallyEmitToolCall) {
|
|
1755
|
+
if (eventType === "on_chain_end") {
|
|
1756
|
+
state = event.data.output;
|
|
1757
|
+
emit(JSON.stringify({
|
|
1758
|
+
event: "on_copilotkit_emit_tool_call",
|
|
1759
|
+
name: event.data.output.name,
|
|
1760
|
+
args: event.data.output.args,
|
|
1761
|
+
id: event.data.output.id
|
|
1762
|
+
}) + "\n");
|
|
1763
|
+
}
|
|
1764
|
+
continue;
|
|
1765
|
+
}
|
|
1766
|
+
if (emitIntermediateState && emitIntermediateStateUntilEnd == null) {
|
|
1767
|
+
emitIntermediateStateUntilEnd = nodeName;
|
|
1768
|
+
}
|
|
1769
|
+
if (emitIntermediateState && eventType === "on_chat_model_start") {
|
|
1770
|
+
streamingStateExtractor = new StreamingStateExtractor(emitIntermediateState);
|
|
1771
|
+
}
|
|
1772
|
+
let updatedState = latestStateValues;
|
|
1773
|
+
if (emitIntermediateState && eventType === "on_chat_model_stream") {
|
|
1774
|
+
streamingStateExtractor.bufferToolCalls(event);
|
|
1775
|
+
}
|
|
1776
|
+
if (emitIntermediateStateUntilEnd !== null) {
|
|
1777
|
+
updatedState = {
|
|
1778
|
+
...updatedState,
|
|
1779
|
+
...streamingStateExtractor.extractState()
|
|
1780
|
+
};
|
|
1781
|
+
}
|
|
1782
|
+
if (!emitIntermediateState && currentNodeName === emitIntermediateStateUntilEnd && eventType === "on_chain_end") {
|
|
1783
|
+
emitIntermediateStateUntilEnd = null;
|
|
1784
|
+
}
|
|
1785
|
+
const exitingNode = nodeName === currentNodeName && eventType === "on_chain_end";
|
|
1786
|
+
if (JSON.stringify(updatedState) !== JSON.stringify(state) || prevNodeName != nodeName || exitingNode) {
|
|
1787
|
+
state = updatedState;
|
|
1788
|
+
prevNodeName = nodeName;
|
|
1789
|
+
emit(getStateSyncEvent({
|
|
1790
|
+
threadId,
|
|
1791
|
+
runId,
|
|
1792
|
+
agentName: agent.name,
|
|
1793
|
+
nodeName,
|
|
1794
|
+
state,
|
|
1795
|
+
running: true,
|
|
1796
|
+
active: !exitingNode
|
|
1797
|
+
}));
|
|
1621
1798
|
}
|
|
1799
|
+
emit(JSON.stringify(event) + "\n");
|
|
1622
1800
|
}
|
|
1623
|
-
|
|
1801
|
+
state = await client.threads.getState(threadId);
|
|
1802
|
+
const isEndNode = state.next.length === 0;
|
|
1803
|
+
nodeName = Object.keys(state.metadata.writes)[0];
|
|
1804
|
+
emit(getStateSyncEvent({
|
|
1805
|
+
threadId,
|
|
1806
|
+
runId: externalRunId,
|
|
1807
|
+
agentName: agent.name,
|
|
1808
|
+
nodeName: isEndNode ? "__end__" : nodeName,
|
|
1809
|
+
state: state.values,
|
|
1810
|
+
running: !shouldExit,
|
|
1811
|
+
active: false
|
|
1812
|
+
}));
|
|
1813
|
+
return Promise.resolve();
|
|
1624
1814
|
}
|
|
1625
|
-
__name(
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1815
|
+
__name(streamEvents, "streamEvents");
|
|
1816
|
+
function getStateSyncEvent({ threadId, runId, agentName, nodeName, state, running, active }) {
|
|
1817
|
+
const stateWithoutMessages = Object.keys(state).reduce((acc, key) => {
|
|
1818
|
+
if (key !== "messages") {
|
|
1819
|
+
acc[key] = state[key];
|
|
1820
|
+
}
|
|
1821
|
+
return acc;
|
|
1822
|
+
}, {});
|
|
1823
|
+
return JSON.stringify({
|
|
1824
|
+
event: "on_copilotkit_state_sync",
|
|
1825
|
+
thread_id: threadId,
|
|
1826
|
+
run_id: runId,
|
|
1827
|
+
agent_name: agentName,
|
|
1828
|
+
node_name: nodeName,
|
|
1829
|
+
active,
|
|
1830
|
+
state: stateWithoutMessages,
|
|
1831
|
+
running,
|
|
1832
|
+
role: "assistant"
|
|
1833
|
+
}) + "\n";
|
|
1834
|
+
}
|
|
1835
|
+
__name(getStateSyncEvent, "getStateSyncEvent");
|
|
1836
|
+
var StreamingStateExtractor = /* @__PURE__ */ __name(class StreamingStateExtractor2 {
|
|
1837
|
+
emitIntermediateState;
|
|
1838
|
+
toolCallBuffer;
|
|
1839
|
+
currentToolCall;
|
|
1840
|
+
previouslyParsableState;
|
|
1841
|
+
constructor(emitIntermediateState) {
|
|
1842
|
+
this.emitIntermediateState = emitIntermediateState;
|
|
1843
|
+
this.toolCallBuffer = {};
|
|
1844
|
+
this.currentToolCall = null;
|
|
1845
|
+
this.previouslyParsableState = {};
|
|
1846
|
+
}
|
|
1847
|
+
bufferToolCalls(event) {
|
|
1848
|
+
if (event.data.chunk.tool_call_chunks.length > 0) {
|
|
1849
|
+
const chunk = event.data.chunk.tool_call_chunks[0];
|
|
1850
|
+
if (chunk.name !== null) {
|
|
1851
|
+
this.currentToolCall = chunk.name;
|
|
1852
|
+
this.toolCallBuffer[this.currentToolCall] = chunk.args;
|
|
1853
|
+
} else if (this.currentToolCall !== null) {
|
|
1854
|
+
this.toolCallBuffer[this.currentToolCall] += chunk.args;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
getEmitStateConfig(currentToolName) {
|
|
1859
|
+
for (const config2 of this.emitIntermediateState) {
|
|
1860
|
+
const stateKey = config2["state_key"];
|
|
1861
|
+
const tool = config2["tool"];
|
|
1862
|
+
const toolArgument = config2["tool_argument"];
|
|
1863
|
+
if (currentToolName === tool) {
|
|
1864
|
+
return [
|
|
1865
|
+
toolArgument,
|
|
1866
|
+
stateKey
|
|
1867
|
+
];
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
return [
|
|
1871
|
+
null,
|
|
1872
|
+
null
|
|
1873
|
+
];
|
|
1874
|
+
}
|
|
1875
|
+
extractState() {
|
|
1876
|
+
const state = {};
|
|
1877
|
+
for (const [key, value] of Object.entries(this.toolCallBuffer)) {
|
|
1878
|
+
const [argumentName, stateKey] = this.getEmitStateConfig(key);
|
|
1879
|
+
if (stateKey === null) {
|
|
1880
|
+
continue;
|
|
1881
|
+
}
|
|
1882
|
+
let parsedValue;
|
|
1883
|
+
try {
|
|
1884
|
+
parsedValue = (0, import_partial_json.parse)(value);
|
|
1885
|
+
} catch (error) {
|
|
1886
|
+
if (key in this.previouslyParsableState) {
|
|
1887
|
+
parsedValue = this.previouslyParsableState[key];
|
|
1888
|
+
} else {
|
|
1889
|
+
continue;
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
this.previouslyParsableState[key] = parsedValue;
|
|
1893
|
+
if (!argumentName) {
|
|
1894
|
+
state[stateKey] = parsedValue;
|
|
1895
|
+
} else {
|
|
1896
|
+
state[stateKey] = parsedValue[argumentName];
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
return state;
|
|
1900
|
+
}
|
|
1901
|
+
}, "StreamingStateExtractor");
|
|
1902
|
+
function langGraphDefaultMergeState(state, messages, actions, agentName) {
|
|
1903
|
+
if (messages.length > 0 && "role" in messages[0] && messages[0].role === "system") {
|
|
1904
|
+
messages = messages.slice(1);
|
|
1905
|
+
}
|
|
1906
|
+
const mergedMessages = state.messages || [];
|
|
1907
|
+
const existingMessageIds = new Set(mergedMessages.map((message) => message.id));
|
|
1908
|
+
const existingToolCallResults = /* @__PURE__ */ new Set();
|
|
1909
|
+
for (const message of mergedMessages) {
|
|
1910
|
+
if ("tool_call_id" in message) {
|
|
1911
|
+
existingToolCallResults.add(message.tool_call_id);
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
for (const message of messages) {
|
|
1915
|
+
if ("tool_calls" in message && message.tool_calls.length > 0 && message.tool_calls[0].name === agentName) {
|
|
1916
|
+
continue;
|
|
1917
|
+
}
|
|
1918
|
+
if ("name" in message && message.name === agentName) {
|
|
1919
|
+
continue;
|
|
1920
|
+
}
|
|
1921
|
+
if (!existingMessageIds.has(message.id)) {
|
|
1922
|
+
if ("tool_call_id" in message && existingToolCallResults.has(message.tool_call_id)) {
|
|
1923
|
+
console.warn("Warning: Duplicate tool call result, skipping:", message.tool_call_id);
|
|
1924
|
+
continue;
|
|
1925
|
+
}
|
|
1926
|
+
mergedMessages.push(message);
|
|
1927
|
+
} else {
|
|
1928
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1929
|
+
if (mergedMessages[i].id === message.id) {
|
|
1930
|
+
if ("tool_calls" in message) {
|
|
1931
|
+
if (("tool_calls" in mergedMessages[i] || "additional_kwargs" in mergedMessages[i]) && mergedMessages[i].content) {
|
|
1932
|
+
message.tool_calls = mergedMessages[i]["tool_calls"];
|
|
1933
|
+
message.additional_kwargs = mergedMessages[i].additional_kwargs;
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
mergedMessages[i] = message;
|
|
1937
|
+
}
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
for (let i = 0; i < mergedMessages.length - 1; i++) {
|
|
1942
|
+
const currentMessage = mergedMessages[i];
|
|
1943
|
+
const nextMessage = mergedMessages[i + 1];
|
|
1944
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0 && "tool_call_id" in nextMessage) {
|
|
1945
|
+
nextMessage.tool_call_id = currentMessage.tool_calls[0].id;
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
const correctedMessages = [];
|
|
1949
|
+
for (let i = 0; i < mergedMessages.length; i++) {
|
|
1950
|
+
const currentMessage = mergedMessages[i];
|
|
1951
|
+
const nextMessage = mergedMessages[i + 1] || null;
|
|
1952
|
+
const prevMessage = mergedMessages[i - 1] || null;
|
|
1953
|
+
if ("tool_calls" in currentMessage && currentMessage.tool_calls.length > 0) {
|
|
1954
|
+
if (!nextMessage) {
|
|
1955
|
+
console.warn("No next message to auto-correct tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1956
|
+
continue;
|
|
1957
|
+
}
|
|
1958
|
+
if (!("tool_call_id" in nextMessage) || nextMessage.tool_call_id !== currentMessage.tool_calls[0].id) {
|
|
1959
|
+
const toolMessage = mergedMessages.find((m) => "tool_call_id" in m && m.tool_call_id === currentMessage.tool_calls[0].id);
|
|
1960
|
+
if (toolMessage) {
|
|
1961
|
+
console.warn("Auto-corrected tool call alignment issue:", currentMessage.tool_calls[0].id);
|
|
1962
|
+
correctedMessages.push(currentMessage, toolMessage);
|
|
1963
|
+
continue;
|
|
1964
|
+
} else {
|
|
1965
|
+
console.warn("No corresponding tool call result found for tool call, skipping:", currentMessage.tool_calls[0].id);
|
|
1966
|
+
continue;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
correctedMessages.push(currentMessage);
|
|
1970
|
+
continue;
|
|
1971
|
+
}
|
|
1972
|
+
if ("tool_call_id" in currentMessage) {
|
|
1973
|
+
if (!prevMessage || !("tool_calls" in prevMessage)) {
|
|
1974
|
+
console.warn("No previous tool call, skipping tool call result:", currentMessage.id);
|
|
1975
|
+
continue;
|
|
1976
|
+
}
|
|
1977
|
+
if (prevMessage.tool_calls && prevMessage.tool_calls[0].id !== currentMessage.tool_call_id) {
|
|
1978
|
+
console.warn("Tool call id is incorrect, skipping tool call result:", currentMessage.id);
|
|
1979
|
+
continue;
|
|
1980
|
+
}
|
|
1981
|
+
correctedMessages.push(currentMessage);
|
|
1982
|
+
continue;
|
|
1983
|
+
}
|
|
1984
|
+
correctedMessages.push(currentMessage);
|
|
1985
|
+
}
|
|
1986
|
+
return deepMerge(state, {
|
|
1987
|
+
messages: correctedMessages,
|
|
1988
|
+
copilotkit: {
|
|
1989
|
+
actions
|
|
1990
|
+
}
|
|
1991
|
+
});
|
|
1992
|
+
}
|
|
1993
|
+
__name(langGraphDefaultMergeState, "langGraphDefaultMergeState");
|
|
1994
|
+
function deepMerge(obj1, obj2) {
|
|
1995
|
+
let result = {
|
|
1996
|
+
...obj1
|
|
1997
|
+
};
|
|
1998
|
+
for (let key in obj2) {
|
|
1999
|
+
if (typeof obj2[key] === "object" && !Array.isArray(obj2[key])) {
|
|
2000
|
+
if (obj1[key]) {
|
|
2001
|
+
result[key] = deepMerge(obj1[key], obj2[key]);
|
|
2002
|
+
} else {
|
|
2003
|
+
result[key] = {
|
|
2004
|
+
...obj2[key]
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
} else {
|
|
2008
|
+
result[key] = obj2[key];
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
return result;
|
|
2012
|
+
}
|
|
2013
|
+
__name(deepMerge, "deepMerge");
|
|
2014
|
+
function formatMessages(messages) {
|
|
2015
|
+
return messages.map((message) => {
|
|
2016
|
+
if (message.isTextMessage() && message.role === "assistant") {
|
|
2017
|
+
return message;
|
|
2018
|
+
}
|
|
2019
|
+
if (message.isTextMessage() && message.role === "system") {
|
|
2020
|
+
return message;
|
|
2021
|
+
}
|
|
2022
|
+
if (message.isTextMessage() && message.role === "user") {
|
|
2023
|
+
return message;
|
|
2024
|
+
}
|
|
2025
|
+
if (message.isActionExecutionMessage()) {
|
|
2026
|
+
const toolCall = {
|
|
2027
|
+
name: message.name,
|
|
2028
|
+
args: message.arguments,
|
|
2029
|
+
id: message.id
|
|
2030
|
+
};
|
|
1646
2031
|
return {
|
|
1647
|
-
|
|
1648
|
-
|
|
2032
|
+
type: message.type,
|
|
2033
|
+
content: "",
|
|
2034
|
+
tool_calls: [
|
|
2035
|
+
toolCall
|
|
2036
|
+
],
|
|
2037
|
+
role: MessageRole.assistant,
|
|
2038
|
+
id: message.id
|
|
1649
2039
|
};
|
|
1650
2040
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
};
|
|
1664
|
-
}
|
|
2041
|
+
if (message.isResultMessage()) {
|
|
2042
|
+
return {
|
|
2043
|
+
type: message.type,
|
|
2044
|
+
content: message.result,
|
|
2045
|
+
id: message.id,
|
|
2046
|
+
tool_call_id: message.actionExecutionId,
|
|
2047
|
+
name: message.actionName,
|
|
2048
|
+
role: MessageRole.assistant
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
throw new Error(`Unknown message type ${message.type}`);
|
|
2052
|
+
});
|
|
1665
2053
|
}
|
|
1666
|
-
__name(
|
|
2054
|
+
__name(formatMessages, "formatMessages");
|
|
2055
|
+
|
|
2056
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
2057
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
2058
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
2059
|
+
name: agent.name,
|
|
2060
|
+
description: agent.description,
|
|
2061
|
+
parameters: [],
|
|
2062
|
+
handler: async (_args) => {
|
|
2063
|
+
},
|
|
2064
|
+
langGraphAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName }) => {
|
|
2065
|
+
var _a;
|
|
2066
|
+
logger2.debug({
|
|
2067
|
+
actionName: agent.name
|
|
2068
|
+
}, "Executing LangGraph Cloud agent");
|
|
2069
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {});
|
|
2070
|
+
let state = {};
|
|
2071
|
+
if (agentStates) {
|
|
2072
|
+
const jsonState = (_a = agentStates.find((state2) => state2.agentName === name)) == null ? void 0 : _a.state;
|
|
2073
|
+
if (jsonState) {
|
|
2074
|
+
state = JSON.parse(jsonState);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
try {
|
|
2078
|
+
const response = await execute({
|
|
2079
|
+
logger: logger2,
|
|
2080
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
2081
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
2082
|
+
agent,
|
|
2083
|
+
threadId,
|
|
2084
|
+
nodeName,
|
|
2085
|
+
messages,
|
|
2086
|
+
state,
|
|
2087
|
+
properties: graphqlContext.properties,
|
|
2088
|
+
actions: actionInputsWithoutAgents.map((action) => ({
|
|
2089
|
+
name: action.name,
|
|
2090
|
+
description: action.description,
|
|
2091
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
2092
|
+
}))
|
|
2093
|
+
});
|
|
2094
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
2095
|
+
streamResponse(response, eventSource.eventStream$);
|
|
2096
|
+
return eventSource.processLangGraphEvents();
|
|
2097
|
+
} catch (error) {
|
|
2098
|
+
logger2.error({
|
|
2099
|
+
url: endpoint.deploymentUrl,
|
|
2100
|
+
status: 500,
|
|
2101
|
+
body: error.message
|
|
2102
|
+
}, "Failed to execute LangGraph Cloud agent");
|
|
2103
|
+
throw new Error("Failed to execute LangGraph Cloud agent");
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
}));
|
|
2107
|
+
return [
|
|
2108
|
+
...agents
|
|
2109
|
+
];
|
|
2110
|
+
}
|
|
2111
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
1667
2112
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
1668
2113
|
const actions = json["actions"].map((action) => ({
|
|
1669
2114
|
name: action.name,
|
|
@@ -1755,7 +2200,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1755
2200
|
throw new Error("Failed to execute remote agent");
|
|
1756
2201
|
}
|
|
1757
2202
|
const eventSource = new RemoteLangGraphEventSource();
|
|
1758
|
-
|
|
2203
|
+
streamResponse(response.body, eventSource.eventStream$);
|
|
1759
2204
|
return eventSource.processLangGraphEvents();
|
|
1760
2205
|
}
|
|
1761
2206
|
}));
|
|
@@ -1765,34 +2210,165 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
1765
2210
|
];
|
|
1766
2211
|
}
|
|
1767
2212
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
1768
|
-
async function
|
|
2213
|
+
async function streamResponse(response, eventStream$) {
|
|
2214
|
+
const reader = response.getReader();
|
|
2215
|
+
const decoder = new TextDecoder();
|
|
2216
|
+
let buffer = [];
|
|
2217
|
+
function flushBuffer() {
|
|
2218
|
+
const currentBuffer = buffer.join("");
|
|
2219
|
+
if (currentBuffer.trim().length === 0) {
|
|
2220
|
+
return;
|
|
2221
|
+
}
|
|
2222
|
+
const parts = currentBuffer.split("\n");
|
|
2223
|
+
if (parts.length === 0) {
|
|
2224
|
+
return;
|
|
2225
|
+
}
|
|
2226
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
2227
|
+
buffer = [];
|
|
2228
|
+
if (!lastPartIsComplete) {
|
|
2229
|
+
buffer.push(parts.pop());
|
|
2230
|
+
}
|
|
2231
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
2232
|
+
eventStream$.next(JSON.parse(part));
|
|
2233
|
+
});
|
|
2234
|
+
}
|
|
2235
|
+
__name(flushBuffer, "flushBuffer");
|
|
2236
|
+
try {
|
|
2237
|
+
while (true) {
|
|
2238
|
+
const { done, value } = await reader.read();
|
|
2239
|
+
if (!done) {
|
|
2240
|
+
buffer.push(decoder.decode(value, {
|
|
2241
|
+
stream: true
|
|
2242
|
+
}));
|
|
2243
|
+
}
|
|
2244
|
+
flushBuffer();
|
|
2245
|
+
if (done) {
|
|
2246
|
+
break;
|
|
2247
|
+
}
|
|
2248
|
+
}
|
|
2249
|
+
} catch (error) {
|
|
2250
|
+
console.error("Error in stream", error);
|
|
2251
|
+
eventStream$.error(error);
|
|
2252
|
+
return;
|
|
2253
|
+
}
|
|
2254
|
+
eventStream$.complete();
|
|
2255
|
+
}
|
|
2256
|
+
__name(streamResponse, "streamResponse");
|
|
2257
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
2258
|
+
const headers = {
|
|
2259
|
+
"Content-Type": "application/json"
|
|
2260
|
+
};
|
|
2261
|
+
if (onBeforeRequest) {
|
|
2262
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
2263
|
+
ctx: graphqlContext
|
|
2264
|
+
});
|
|
2265
|
+
if (additionalHeaders) {
|
|
2266
|
+
Object.assign(headers, additionalHeaders);
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
return headers;
|
|
2270
|
+
}
|
|
2271
|
+
__name(createHeaders, "createHeaders");
|
|
2272
|
+
|
|
2273
|
+
// src/lib/runtime/remote-actions.ts
|
|
2274
|
+
var EndpointType;
|
|
2275
|
+
(function(EndpointType2) {
|
|
2276
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
2277
|
+
EndpointType2["LangGraphCloud"] = "langgraph-cloud";
|
|
2278
|
+
})(EndpointType || (EndpointType = {}));
|
|
2279
|
+
function isLangGraphAgentAction(action) {
|
|
2280
|
+
if (!action) {
|
|
2281
|
+
return false;
|
|
2282
|
+
}
|
|
2283
|
+
return typeof action.langGraphAgentHandler === "function";
|
|
2284
|
+
}
|
|
2285
|
+
__name(isLangGraphAgentAction, "isLangGraphAgentAction");
|
|
2286
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
2287
|
+
logger2.debug({
|
|
2288
|
+
url
|
|
2289
|
+
}, "Fetching actions from url");
|
|
2290
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2291
|
+
try {
|
|
2292
|
+
const response = await fetch(`${url}/info`, {
|
|
2293
|
+
method: "POST",
|
|
2294
|
+
headers,
|
|
2295
|
+
body: JSON.stringify({
|
|
2296
|
+
properties: graphqlContext.properties,
|
|
2297
|
+
frontendUrl
|
|
2298
|
+
})
|
|
2299
|
+
});
|
|
2300
|
+
if (!response.ok) {
|
|
2301
|
+
logger2.error({
|
|
2302
|
+
url,
|
|
2303
|
+
status: response.status,
|
|
2304
|
+
body: await response.text()
|
|
2305
|
+
}, "Failed to fetch actions from url");
|
|
2306
|
+
return {
|
|
2307
|
+
actions: [],
|
|
2308
|
+
agents: []
|
|
2309
|
+
};
|
|
2310
|
+
}
|
|
2311
|
+
const json = await response.json();
|
|
2312
|
+
logger2.debug({
|
|
2313
|
+
json
|
|
2314
|
+
}, "Fetched actions from url");
|
|
2315
|
+
return json;
|
|
2316
|
+
} catch (error) {
|
|
2317
|
+
logger2.error({
|
|
2318
|
+
error: error.message ? error.message : error + ""
|
|
2319
|
+
}, "Failed to fetch actions from url");
|
|
2320
|
+
return {
|
|
2321
|
+
actions: [],
|
|
2322
|
+
agents: []
|
|
2323
|
+
};
|
|
2324
|
+
}
|
|
2325
|
+
}
|
|
2326
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
2327
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl }) {
|
|
1769
2328
|
const logger2 = graphqlContext.logger.child({
|
|
1770
2329
|
component: "remote-actions.fetchRemoteActions"
|
|
1771
2330
|
});
|
|
1772
2331
|
logger2.debug({
|
|
1773
|
-
|
|
1774
|
-
}, "Fetching remote
|
|
1775
|
-
const filtered =
|
|
1776
|
-
|
|
2332
|
+
remoteEndpointDefinitions
|
|
2333
|
+
}, "Fetching from remote endpoints");
|
|
2334
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
2335
|
+
if (value.type === "langgraph-cloud") {
|
|
2336
|
+
return value;
|
|
2337
|
+
}
|
|
2338
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
2339
|
+
});
|
|
2340
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
2341
|
+
if (endpoint.type === "langgraph-cloud") {
|
|
2342
|
+
return constructLGCRemoteAction({
|
|
2343
|
+
endpoint,
|
|
2344
|
+
messages,
|
|
2345
|
+
graphqlContext,
|
|
2346
|
+
logger: logger2.child({
|
|
2347
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
2348
|
+
endpoint
|
|
2349
|
+
}),
|
|
2350
|
+
agentStates
|
|
2351
|
+
});
|
|
2352
|
+
}
|
|
1777
2353
|
const json = await fetchRemoteInfo({
|
|
1778
|
-
url:
|
|
1779
|
-
onBeforeRequest:
|
|
2354
|
+
url: endpoint.url,
|
|
2355
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1780
2356
|
graphqlContext,
|
|
1781
2357
|
logger: logger2.child({
|
|
1782
2358
|
component: "remote-actions.fetchActionsFromUrl",
|
|
1783
|
-
|
|
2359
|
+
endpoint
|
|
1784
2360
|
}),
|
|
1785
2361
|
frontendUrl
|
|
1786
2362
|
});
|
|
1787
2363
|
return constructRemoteActions({
|
|
1788
2364
|
json,
|
|
1789
2365
|
messages,
|
|
1790
|
-
url:
|
|
1791
|
-
onBeforeRequest:
|
|
2366
|
+
url: endpoint.url,
|
|
2367
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
1792
2368
|
graphqlContext,
|
|
1793
2369
|
logger: logger2.child({
|
|
1794
2370
|
component: "remote-actions.constructActions",
|
|
1795
|
-
|
|
2371
|
+
endpoint
|
|
1796
2372
|
}),
|
|
1797
2373
|
agentStates
|
|
1798
2374
|
});
|
|
@@ -1990,7 +2566,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
1990
2566
|
__name(executeAction, "executeAction");
|
|
1991
2567
|
|
|
1992
2568
|
// src/graphql/types/base/index.ts
|
|
1993
|
-
var
|
|
2569
|
+
var import_type_graphql2 = require("type-graphql");
|
|
1994
2570
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1995
2571
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1996
2572
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -2013,15 +2589,15 @@ var BaseMessageInput = class {
|
|
|
2013
2589
|
};
|
|
2014
2590
|
__name(BaseMessageInput, "BaseMessageInput");
|
|
2015
2591
|
_ts_decorate([
|
|
2016
|
-
(0,
|
|
2592
|
+
(0, import_type_graphql2.Field)(() => String),
|
|
2017
2593
|
_ts_metadata("design:type", String)
|
|
2018
2594
|
], BaseMessageInput.prototype, "id", void 0);
|
|
2019
2595
|
_ts_decorate([
|
|
2020
|
-
(0,
|
|
2596
|
+
(0, import_type_graphql2.Field)(() => Date),
|
|
2021
2597
|
_ts_metadata("design:type", typeof Date === "undefined" ? Object : Date)
|
|
2022
2598
|
], BaseMessageInput.prototype, "createdAt", void 0);
|
|
2023
2599
|
BaseMessageInput = _ts_decorate([
|
|
2024
|
-
(0,
|
|
2600
|
+
(0, import_type_graphql2.InputType)()
|
|
2025
2601
|
], BaseMessageInput);
|
|
2026
2602
|
|
|
2027
2603
|
// src/graphql/types/converted/index.ts
|
|
@@ -2125,7 +2701,7 @@ __name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
|
2125
2701
|
var import_rxjs3 = require("rxjs");
|
|
2126
2702
|
var CopilotRuntime = class {
|
|
2127
2703
|
actions;
|
|
2128
|
-
|
|
2704
|
+
remoteEndpointDefinitions;
|
|
2129
2705
|
langserve = [];
|
|
2130
2706
|
onBeforeRequest;
|
|
2131
2707
|
onAfterRequest;
|
|
@@ -2136,7 +2712,7 @@ var CopilotRuntime = class {
|
|
|
2136
2712
|
const remoteChain = new RemoteChain(chain);
|
|
2137
2713
|
this.langserve.push(remoteChain.toAction());
|
|
2138
2714
|
}
|
|
2139
|
-
this.
|
|
2715
|
+
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) || [];
|
|
2140
2716
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
2141
2717
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
2142
2718
|
}
|
|
@@ -2205,7 +2781,7 @@ var CopilotRuntime = class {
|
|
|
2205
2781
|
async processAgentRequest(request) {
|
|
2206
2782
|
var _a;
|
|
2207
2783
|
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession } = request;
|
|
2208
|
-
const { threadId
|
|
2784
|
+
const { threadId, agentName, nodeName } = agentSession;
|
|
2209
2785
|
const serverSideActions = await this.getServerSideActions(request);
|
|
2210
2786
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
2211
2787
|
const agent = serverSideActions.find((action) => action.name === agentName && isLangGraphAgentAction(action));
|
|
@@ -2277,8 +2853,12 @@ var CopilotRuntime = class {
|
|
|
2277
2853
|
console.error("Error loading langserve chain:", error);
|
|
2278
2854
|
}
|
|
2279
2855
|
}
|
|
2856
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
2857
|
+
...endpoint,
|
|
2858
|
+
type: this.resolveEndpointType(endpoint)
|
|
2859
|
+
}));
|
|
2280
2860
|
const remoteActions = await setupRemoteActions({
|
|
2281
|
-
|
|
2861
|
+
remoteEndpointDefinitions,
|
|
2282
2862
|
graphqlContext,
|
|
2283
2863
|
messages: inputMessages,
|
|
2284
2864
|
agentStates,
|
|
@@ -2294,6 +2874,12 @@ var CopilotRuntime = class {
|
|
|
2294
2874
|
...remoteActions
|
|
2295
2875
|
];
|
|
2296
2876
|
}
|
|
2877
|
+
resolveEndpointType(endpoint) {
|
|
2878
|
+
if (!endpoint.type && "langsmithApiKey" in endpoint && "deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
2879
|
+
return EndpointType.LangGraphCloud;
|
|
2880
|
+
}
|
|
2881
|
+
return endpoint.type;
|
|
2882
|
+
}
|
|
2297
2883
|
};
|
|
2298
2884
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
2299
2885
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -2308,6 +2894,20 @@ function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
|
2308
2894
|
return allTools;
|
|
2309
2895
|
}
|
|
2310
2896
|
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
2897
|
+
function copilotKitEndpoint(config2) {
|
|
2898
|
+
return {
|
|
2899
|
+
...config2,
|
|
2900
|
+
type: EndpointType.CopilotKit
|
|
2901
|
+
};
|
|
2902
|
+
}
|
|
2903
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
2904
|
+
function langGraphCloudEndpoint(config2) {
|
|
2905
|
+
return {
|
|
2906
|
+
...config2,
|
|
2907
|
+
type: EndpointType.LangGraphCloud
|
|
2908
|
+
};
|
|
2909
|
+
}
|
|
2910
|
+
__name(langGraphCloudEndpoint, "langGraphCloudEndpoint");
|
|
2311
2911
|
|
|
2312
2912
|
// src/lib/integrations/shared.ts
|
|
2313
2913
|
var import_type_graphql16 = require("type-graphql");
|
|
@@ -2321,43 +2921,6 @@ var import_type_graphql11 = require("type-graphql");
|
|
|
2321
2921
|
|
|
2322
2922
|
// src/graphql/inputs/message.input.ts
|
|
2323
2923
|
var import_type_graphql3 = require("type-graphql");
|
|
2324
|
-
|
|
2325
|
-
// src/graphql/types/enums.ts
|
|
2326
|
-
var import_type_graphql2 = require("type-graphql");
|
|
2327
|
-
var MessageRole;
|
|
2328
|
-
(function(MessageRole2) {
|
|
2329
|
-
MessageRole2["user"] = "user";
|
|
2330
|
-
MessageRole2["assistant"] = "assistant";
|
|
2331
|
-
MessageRole2["system"] = "system";
|
|
2332
|
-
})(MessageRole || (MessageRole = {}));
|
|
2333
|
-
var ActionExecutionScope;
|
|
2334
|
-
(function(ActionExecutionScope2) {
|
|
2335
|
-
ActionExecutionScope2["server"] = "server";
|
|
2336
|
-
ActionExecutionScope2["client"] = "client";
|
|
2337
|
-
ActionExecutionScope2["passThrough"] = "passThrough";
|
|
2338
|
-
})(ActionExecutionScope || (ActionExecutionScope = {}));
|
|
2339
|
-
var CopilotRequestType;
|
|
2340
|
-
(function(CopilotRequestType2) {
|
|
2341
|
-
CopilotRequestType2["Chat"] = "Chat";
|
|
2342
|
-
CopilotRequestType2["Task"] = "Task";
|
|
2343
|
-
CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion";
|
|
2344
|
-
CopilotRequestType2["TextareaPopover"] = "TextareaPopover";
|
|
2345
|
-
CopilotRequestType2["Suggestion"] = "Suggestion";
|
|
2346
|
-
})(CopilotRequestType || (CopilotRequestType = {}));
|
|
2347
|
-
(0, import_type_graphql2.registerEnumType)(MessageRole, {
|
|
2348
|
-
name: "MessageRole",
|
|
2349
|
-
description: "The role of the message"
|
|
2350
|
-
});
|
|
2351
|
-
(0, import_type_graphql2.registerEnumType)(ActionExecutionScope, {
|
|
2352
|
-
name: "ActionExecutionScope",
|
|
2353
|
-
description: "The scope of the action"
|
|
2354
|
-
});
|
|
2355
|
-
(0, import_type_graphql2.registerEnumType)(CopilotRequestType, {
|
|
2356
|
-
name: "CopilotRequestType",
|
|
2357
|
-
description: "The type of Copilot request"
|
|
2358
|
-
});
|
|
2359
|
-
|
|
2360
|
-
// src/graphql/inputs/message.input.ts
|
|
2361
2924
|
function _ts_decorate2(decorators, target, key, desc) {
|
|
2362
2925
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2363
2926
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -4019,6 +4582,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
4019
4582
|
UnknownErrorResponse,
|
|
4020
4583
|
buildSchema,
|
|
4021
4584
|
config,
|
|
4585
|
+
copilotKitEndpoint,
|
|
4022
4586
|
copilotRuntimeNestEndpoint,
|
|
4023
4587
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
4024
4588
|
copilotRuntimeNextJSPagesRouterEndpoint,
|
|
@@ -4026,6 +4590,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
4026
4590
|
copilotRuntimeNodeHttpEndpoint,
|
|
4027
4591
|
createContext,
|
|
4028
4592
|
flattenToolCallsNoDuplicates,
|
|
4029
|
-
getCommonConfig
|
|
4593
|
+
getCommonConfig,
|
|
4594
|
+
langGraphCloudEndpoint
|
|
4030
4595
|
});
|
|
4031
4596
|
//# sourceMappingURL=index.js.map
|