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