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