@copilotkit/runtime 1.9.2-next.1 → 1.9.2-next.11
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 +73 -0
- package/dist/chunk-AMUJQ6IR.mjs +50 -0
- package/dist/chunk-AMUJQ6IR.mjs.map +1 -0
- package/dist/{chunk-C3SWOFLO.mjs → chunk-B2BL6HPT.mjs} +2 -2
- package/dist/{chunk-RIPQZJB5.mjs → chunk-CBVVBPVJ.mjs} +2 -2
- package/dist/{chunk-IIXJVVTV.mjs → chunk-GS7DO47Q.mjs} +155 -78
- package/dist/chunk-GS7DO47Q.mjs.map +1 -0
- package/dist/{chunk-XGBY45FP.mjs → chunk-NI7RVCMB.mjs} +2343 -1857
- package/dist/chunk-NI7RVCMB.mjs.map +1 -0
- package/dist/{chunk-YV3YXRMR.mjs → chunk-VVXCPFVN.mjs} +19 -2
- package/dist/chunk-VVXCPFVN.mjs.map +1 -0
- package/dist/{chunk-5BIEM2UU.mjs → chunk-XWBDEXDA.mjs} +4 -3
- package/dist/{chunk-5BIEM2UU.mjs.map → chunk-XWBDEXDA.mjs.map} +1 -1
- package/dist/{chunk-KPFOAXRX.mjs → chunk-ZIEDTGZF.mjs} +2 -2
- package/dist/{groq-adapter-25a2bd35.d.ts → groq-adapter-172a2ca4.d.ts} +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3487 -2863
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -8
- package/dist/index.mjs.map +1 -1
- package/dist/lib/index.d.ts +7 -133
- package/dist/lib/index.js +3329 -2748
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -8
- package/dist/lib/integrations/index.d.ts +3 -3
- package/dist/lib/integrations/index.js +162 -98
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +7 -6
- package/dist/lib/integrations/nest/index.d.ts +2 -2
- package/dist/lib/integrations/nest/index.js +162 -98
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +5 -4
- package/dist/lib/integrations/node-express/index.d.ts +2 -2
- package/dist/lib/integrations/node-express/index.js +162 -98
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +5 -4
- package/dist/lib/integrations/node-http/index.d.ts +2 -2
- package/dist/lib/integrations/node-http/index.js +162 -98
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +4 -3
- package/dist/service-adapters/index.d.ts +6 -4
- package/dist/service-adapters/index.js +225 -107
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +6 -2
- package/dist/service-adapters/shared/index.d.ts +9 -0
- package/dist/service-adapters/shared/index.js +72 -0
- package/dist/service-adapters/shared/index.js.map +1 -0
- package/dist/service-adapters/shared/index.mjs +8 -0
- package/dist/service-adapters/shared/index.mjs.map +1 -0
- package/dist/{shared-e272b15a.d.ts → shared-4164c674.d.ts} +45 -5
- package/dist/utils/index.d.ts +17 -1
- package/dist/utils/index.js +3 -2
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +1 -1
- package/package.json +4 -4
- package/src/agents/langgraph/event-source.ts +36 -38
- package/src/agents/langgraph/events.ts +19 -1
- package/src/graphql/resolvers/copilot.resolver.ts +108 -45
- package/src/graphql/resolvers/state.resolver.ts +3 -3
- package/src/lib/error-messages.ts +200 -0
- package/src/lib/integrations/shared.ts +43 -0
- package/src/lib/runtime/__tests__/copilot-runtime-trace.test.ts +169 -0
- package/src/lib/runtime/copilot-runtime.ts +383 -83
- package/src/lib/runtime/langgraph/langgraph-agent.ts +12 -0
- package/src/lib/runtime/remote-action-constructors.ts +28 -3
- package/src/lib/runtime/remote-lg-action.ts +130 -40
- package/src/lib/streaming.ts +125 -36
- package/src/service-adapters/anthropic/anthropic-adapter.ts +67 -8
- package/src/service-adapters/anthropic/utils.ts +3 -8
- package/src/service-adapters/events.ts +37 -81
- package/src/service-adapters/groq/groq-adapter.ts +66 -56
- package/src/service-adapters/index.ts +1 -0
- package/src/service-adapters/openai/openai-adapter.ts +18 -3
- package/src/service-adapters/shared/error-utils.ts +61 -0
- package/src/service-adapters/shared/index.ts +1 -0
- package/src/utils/failed-response-status-reasons.ts +23 -1
- package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +172 -387
- package/dist/chunk-IIXJVVTV.mjs.map +0 -1
- package/dist/chunk-XGBY45FP.mjs.map +0 -1
- package/dist/chunk-YV3YXRMR.mjs.map +0 -1
- package/dist/{chunk-C3SWOFLO.mjs.map → chunk-B2BL6HPT.mjs.map} +0 -0
- package/dist/{chunk-RIPQZJB5.mjs.map → chunk-CBVVBPVJ.mjs.map} +0 -0
- package/dist/{chunk-KPFOAXRX.mjs.map → chunk-ZIEDTGZF.mjs.map} +0 -0
- package/dist/{langserve-4a5c9217.d.ts → langserve-fc5cac89.d.ts} +7 -7
|
@@ -1,22 +1,22 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ActionExecutionMessage,
|
|
3
|
+
AgentStateMessage,
|
|
4
|
+
ImageMessage,
|
|
5
|
+
ResultMessage,
|
|
6
|
+
TextMessage
|
|
7
|
+
} from "./chunk-SHBDMA63.mjs";
|
|
1
8
|
import {
|
|
2
9
|
EmptyAdapter,
|
|
3
10
|
RemoteChain,
|
|
4
11
|
streamLangChainResponse
|
|
5
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-GS7DO47Q.mjs";
|
|
6
13
|
import {
|
|
7
14
|
GuardrailsValidationFailureResponse,
|
|
8
15
|
MessageStreamInterruptedResponse,
|
|
9
16
|
ResponseStatusUnion,
|
|
10
17
|
SuccessResponseStatus,
|
|
11
18
|
UnknownErrorResponse
|
|
12
|
-
} from "./chunk-
|
|
13
|
-
import {
|
|
14
|
-
ActionExecutionMessage,
|
|
15
|
-
AgentStateMessage,
|
|
16
|
-
ImageMessage,
|
|
17
|
-
ResultMessage,
|
|
18
|
-
TextMessage
|
|
19
|
-
} from "./chunk-SHBDMA63.mjs";
|
|
19
|
+
} from "./chunk-XWBDEXDA.mjs";
|
|
20
20
|
import {
|
|
21
21
|
BaseMessageInput
|
|
22
22
|
} from "./chunk-2OZAGFV3.mjs";
|
|
@@ -40,7 +40,7 @@ var require_package = __commonJS({
|
|
|
40
40
|
publishConfig: {
|
|
41
41
|
access: "public"
|
|
42
42
|
},
|
|
43
|
-
version: "1.9.2-next.
|
|
43
|
+
version: "1.9.2-next.11",
|
|
44
44
|
sideEffects: false,
|
|
45
45
|
main: "./dist/index.js",
|
|
46
46
|
module: "./dist/index.mjs",
|
|
@@ -81,7 +81,7 @@ var require_package = __commonJS({
|
|
|
81
81
|
"@ag-ui/client": "0.0.28",
|
|
82
82
|
"@ag-ui/core": "0.0.28",
|
|
83
83
|
"@ag-ui/encoder": "0.0.28",
|
|
84
|
-
"@ag-ui/langgraph": "0.0.
|
|
84
|
+
"@ag-ui/langgraph": "0.0.4",
|
|
85
85
|
"@ag-ui/proto": "0.0.28",
|
|
86
86
|
"@anthropic-ai/sdk": "^0.27.3",
|
|
87
87
|
"@copilotkit/shared": "workspace:*",
|
|
@@ -105,7 +105,7 @@ var require_package = __commonJS({
|
|
|
105
105
|
pino: "^9.2.0",
|
|
106
106
|
"pino-pretty": "^11.2.1",
|
|
107
107
|
"reflect-metadata": "^0.2.2",
|
|
108
|
-
rxjs: "
|
|
108
|
+
rxjs: "7.8.1",
|
|
109
109
|
"type-graphql": "2.0.0-rc.1",
|
|
110
110
|
zod: "^3.23.3"
|
|
111
111
|
},
|
|
@@ -1485,20 +1485,166 @@ GenerateCopilotResponseInput = _ts_decorate15([
|
|
|
1485
1485
|
import { Repeater } from "graphql-yoga";
|
|
1486
1486
|
|
|
1487
1487
|
// src/service-adapters/events.ts
|
|
1488
|
-
import {
|
|
1489
|
-
import {
|
|
1490
|
-
|
|
1491
|
-
// src/lib/telemetry-client.ts
|
|
1492
|
-
import { TelemetryClient } from "@copilotkit/shared";
|
|
1488
|
+
import { CopilotKitError as CopilotKitError6, CopilotKitErrorCode as CopilotKitErrorCode4, CopilotKitLowLevelError as CopilotKitLowLevelError7, ensureStructuredError as ensureStructuredError3, randomId as randomId2, Severity } from "@copilotkit/shared";
|
|
1489
|
+
import { plainToInstance as plainToInstance2 } from "class-transformer";
|
|
1490
|
+
import { catchError as catchError2, concat, concatMap, EMPTY, firstValueFrom, from as from2, of, ReplaySubject as ReplaySubject2, scan as scan2 } from "rxjs";
|
|
1493
1491
|
|
|
1494
1492
|
// src/lib/runtime/remote-actions.ts
|
|
1495
|
-
import { CopilotKitErrorCode as
|
|
1493
|
+
import { CopilotKitErrorCode as CopilotKitErrorCode3 } from "@copilotkit/shared";
|
|
1496
1494
|
|
|
1497
1495
|
// src/lib/runtime/remote-action-constructors.ts
|
|
1496
|
+
import { createHash as createHash3 } from "crypto";
|
|
1497
|
+
|
|
1498
|
+
// src/lib/telemetry-client.ts
|
|
1499
|
+
import { TelemetryClient } from "@copilotkit/shared";
|
|
1498
1500
|
import { createHash as createHash2 } from "crypto";
|
|
1499
1501
|
|
|
1500
|
-
// src/
|
|
1501
|
-
import {
|
|
1502
|
+
// src/lib/runtime/copilot-runtime.ts
|
|
1503
|
+
import { actionParametersToJsonSchema, ResolvedCopilotKitError, CopilotKitApiDiscoveryError, randomId, CopilotKitError as CopilotKitError3, CopilotKitAgentDiscoveryError, CopilotKitMisuseError as CopilotKitMisuseError2, CopilotKitErrorCode as CopilotKitErrorCode2, CopilotKitLowLevelError as CopilotKitLowLevelError3, ensureStructuredError as ensureStructuredError2 } from "@copilotkit/shared";
|
|
1504
|
+
|
|
1505
|
+
// src/service-adapters/conversion.ts
|
|
1506
|
+
import { plainToInstance } from "class-transformer";
|
|
1507
|
+
import { tryMap } from "@copilotkit/shared";
|
|
1508
|
+
function convertGqlInputToMessages(inputMessages) {
|
|
1509
|
+
const messages = tryMap(inputMessages, (message) => {
|
|
1510
|
+
if (message.textMessage) {
|
|
1511
|
+
return plainToInstance(TextMessage, {
|
|
1512
|
+
id: message.id,
|
|
1513
|
+
createdAt: message.createdAt,
|
|
1514
|
+
role: message.textMessage.role,
|
|
1515
|
+
content: message.textMessage.content,
|
|
1516
|
+
parentMessageId: message.textMessage.parentMessageId
|
|
1517
|
+
});
|
|
1518
|
+
} else if (message.imageMessage) {
|
|
1519
|
+
return plainToInstance(ImageMessage, {
|
|
1520
|
+
id: message.id,
|
|
1521
|
+
createdAt: message.createdAt,
|
|
1522
|
+
role: message.imageMessage.role,
|
|
1523
|
+
bytes: message.imageMessage.bytes,
|
|
1524
|
+
format: message.imageMessage.format,
|
|
1525
|
+
parentMessageId: message.imageMessage.parentMessageId
|
|
1526
|
+
});
|
|
1527
|
+
} else if (message.actionExecutionMessage) {
|
|
1528
|
+
return plainToInstance(ActionExecutionMessage, {
|
|
1529
|
+
id: message.id,
|
|
1530
|
+
createdAt: message.createdAt,
|
|
1531
|
+
name: message.actionExecutionMessage.name,
|
|
1532
|
+
arguments: JSON.parse(message.actionExecutionMessage.arguments),
|
|
1533
|
+
parentMessageId: message.actionExecutionMessage.parentMessageId
|
|
1534
|
+
});
|
|
1535
|
+
} else if (message.resultMessage) {
|
|
1536
|
+
return plainToInstance(ResultMessage, {
|
|
1537
|
+
id: message.id,
|
|
1538
|
+
createdAt: message.createdAt,
|
|
1539
|
+
actionExecutionId: message.resultMessage.actionExecutionId,
|
|
1540
|
+
actionName: message.resultMessage.actionName,
|
|
1541
|
+
result: message.resultMessage.result
|
|
1542
|
+
});
|
|
1543
|
+
} else if (message.agentStateMessage) {
|
|
1544
|
+
return plainToInstance(AgentStateMessage, {
|
|
1545
|
+
id: message.id,
|
|
1546
|
+
threadId: message.agentStateMessage.threadId,
|
|
1547
|
+
createdAt: message.createdAt,
|
|
1548
|
+
agentName: message.agentStateMessage.agentName,
|
|
1549
|
+
nodeName: message.agentStateMessage.nodeName,
|
|
1550
|
+
runId: message.agentStateMessage.runId,
|
|
1551
|
+
active: message.agentStateMessage.active,
|
|
1552
|
+
role: message.agentStateMessage.role,
|
|
1553
|
+
state: JSON.parse(message.agentStateMessage.state),
|
|
1554
|
+
running: message.agentStateMessage.running
|
|
1555
|
+
});
|
|
1556
|
+
} else {
|
|
1557
|
+
return null;
|
|
1558
|
+
}
|
|
1559
|
+
});
|
|
1560
|
+
return messages.filter((m) => m);
|
|
1561
|
+
}
|
|
1562
|
+
__name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
1563
|
+
|
|
1564
|
+
// src/lib/runtime/copilot-runtime.ts
|
|
1565
|
+
import { from } from "rxjs";
|
|
1566
|
+
|
|
1567
|
+
// src/lib/runtime/retry-utils.ts
|
|
1568
|
+
var RETRY_CONFIG = {
|
|
1569
|
+
maxRetries: 3,
|
|
1570
|
+
baseDelayMs: 1e3,
|
|
1571
|
+
maxDelayMs: 5e3,
|
|
1572
|
+
// HTTP status codes that should be retried
|
|
1573
|
+
retryableStatusCodes: [
|
|
1574
|
+
502,
|
|
1575
|
+
503,
|
|
1576
|
+
504,
|
|
1577
|
+
408,
|
|
1578
|
+
429
|
|
1579
|
+
],
|
|
1580
|
+
// Network error patterns that should be retried
|
|
1581
|
+
retryableErrorMessages: [
|
|
1582
|
+
"fetch failed",
|
|
1583
|
+
"network error",
|
|
1584
|
+
"connection timeout",
|
|
1585
|
+
"ECONNREFUSED",
|
|
1586
|
+
"ETIMEDOUT",
|
|
1587
|
+
"ENOTFOUND",
|
|
1588
|
+
"ECONNRESET"
|
|
1589
|
+
]
|
|
1590
|
+
};
|
|
1591
|
+
function isRetryableError(error, response) {
|
|
1592
|
+
var _a, _b;
|
|
1593
|
+
if (response && RETRY_CONFIG.retryableStatusCodes.includes(response.status)) {
|
|
1594
|
+
return true;
|
|
1595
|
+
}
|
|
1596
|
+
const errorCode = ((_a = error == null ? void 0 : error.cause) == null ? void 0 : _a.code) || (error == null ? void 0 : error.code);
|
|
1597
|
+
if (errorCode && RETRY_CONFIG.retryableErrorMessages.includes(errorCode)) {
|
|
1598
|
+
return true;
|
|
1599
|
+
}
|
|
1600
|
+
const errorMessage = ((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.toLowerCase()) || "";
|
|
1601
|
+
return RETRY_CONFIG.retryableErrorMessages.some((msg) => errorMessage.includes(msg));
|
|
1602
|
+
}
|
|
1603
|
+
__name(isRetryableError, "isRetryableError");
|
|
1604
|
+
function sleep(ms) {
|
|
1605
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1606
|
+
}
|
|
1607
|
+
__name(sleep, "sleep");
|
|
1608
|
+
function calculateDelay(attempt) {
|
|
1609
|
+
const delay = RETRY_CONFIG.baseDelayMs * Math.pow(2, attempt);
|
|
1610
|
+
return Math.min(delay, RETRY_CONFIG.maxDelayMs);
|
|
1611
|
+
}
|
|
1612
|
+
__name(calculateDelay, "calculateDelay");
|
|
1613
|
+
async function fetchWithRetry(url, options, logger2) {
|
|
1614
|
+
let lastError;
|
|
1615
|
+
for (let attempt = 0; attempt <= RETRY_CONFIG.maxRetries; attempt++) {
|
|
1616
|
+
try {
|
|
1617
|
+
const response = await fetch(url, options);
|
|
1618
|
+
if (isRetryableError(null, response) && attempt < RETRY_CONFIG.maxRetries) {
|
|
1619
|
+
const delay = calculateDelay(attempt);
|
|
1620
|
+
logger2 == null ? void 0 : logger2.warn(`Request to ${url} failed with status ${response.status}. Retrying attempt ${attempt + 1}/${RETRY_CONFIG.maxRetries + 1} in ${delay}ms.`);
|
|
1621
|
+
await sleep(delay);
|
|
1622
|
+
continue;
|
|
1623
|
+
}
|
|
1624
|
+
return response;
|
|
1625
|
+
} catch (error) {
|
|
1626
|
+
lastError = error;
|
|
1627
|
+
if (isRetryableError(error) && attempt < RETRY_CONFIG.maxRetries) {
|
|
1628
|
+
const delay = calculateDelay(attempt);
|
|
1629
|
+
logger2 == null ? void 0 : logger2.warn(`Request to ${url} failed with network error. Retrying attempt ${attempt + 1}/${RETRY_CONFIG.maxRetries + 1} in ${delay}ms. Error: ${(error == null ? void 0 : error.message) || String(error)}`);
|
|
1630
|
+
await sleep(delay);
|
|
1631
|
+
continue;
|
|
1632
|
+
}
|
|
1633
|
+
break;
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
throw lastError;
|
|
1637
|
+
}
|
|
1638
|
+
__name(fetchWithRetry, "fetchWithRetry");
|
|
1639
|
+
|
|
1640
|
+
// src/lib/runtime/copilot-runtime.ts
|
|
1641
|
+
import { Client as LangGraphClient2 } from "@langchain/langgraph-sdk";
|
|
1642
|
+
|
|
1643
|
+
// src/lib/runtime/remote-lg-action.ts
|
|
1644
|
+
import { Client as LangGraphClient } from "@langchain/langgraph-sdk";
|
|
1645
|
+
import { createHash } from "crypto";
|
|
1646
|
+
import { isValidUUID, randomUUID } from "@copilotkit/shared";
|
|
1647
|
+
import { parse as parsePartialJson } from "partial-json";
|
|
1502
1648
|
|
|
1503
1649
|
// src/agents/langgraph/events.ts
|
|
1504
1650
|
var LangGraphEventTypes;
|
|
@@ -1517,6 +1663,7 @@ var LangGraphEventTypes;
|
|
|
1517
1663
|
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
|
|
1518
1664
|
LangGraphEventTypes2["OnInterrupt"] = "on_interrupt";
|
|
1519
1665
|
LangGraphEventTypes2["OnCopilotKitInterrupt"] = "on_copilotkit_interrupt";
|
|
1666
|
+
LangGraphEventTypes2["OnCopilotKitError"] = "on_copilotkit_error";
|
|
1520
1667
|
})(LangGraphEventTypes || (LangGraphEventTypes = {}));
|
|
1521
1668
|
var MetaEventNames;
|
|
1522
1669
|
(function(MetaEventNames2) {
|
|
@@ -1531,327 +1678,274 @@ var CustomEventNames;
|
|
|
1531
1678
|
CustomEventNames2["CopilotKitExit"] = "copilotkit_exit";
|
|
1532
1679
|
})(CustomEventNames || (CustomEventNames = {}));
|
|
1533
1680
|
|
|
1534
|
-
// src/
|
|
1535
|
-
import {
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1681
|
+
// src/lib/runtime/remote-lg-action.ts
|
|
1682
|
+
import { parseJson, CopilotKitMisuseError, CopilotKitLowLevelError as CopilotKitLowLevelError2, CopilotKitError as CopilotKitError2 } from "@copilotkit/shared";
|
|
1683
|
+
import { RemoveMessage } from "@langchain/core/messages";
|
|
1684
|
+
|
|
1685
|
+
// src/lib/streaming.ts
|
|
1686
|
+
import { CopilotKitLowLevelError, CopilotKitError, CopilotKitErrorCode, ensureStructuredError } from "@copilotkit/shared";
|
|
1687
|
+
|
|
1688
|
+
// src/lib/error-messages.ts
|
|
1689
|
+
var errorConfig = {
|
|
1690
|
+
errorPatterns: {
|
|
1691
|
+
ECONNREFUSED: {
|
|
1692
|
+
message: "Connection refused - the agent service is not running or not accessible at the specified address. Please check that your agent is started and listening on the correct port.",
|
|
1693
|
+
category: "network",
|
|
1694
|
+
severity: "error",
|
|
1695
|
+
actionable: true
|
|
1696
|
+
},
|
|
1697
|
+
ENOTFOUND: {
|
|
1698
|
+
message: "Host not found - the agent service URL appears to be incorrect or the service is not accessible. Please verify the agent endpoint URL.",
|
|
1699
|
+
category: "network",
|
|
1700
|
+
severity: "error",
|
|
1701
|
+
actionable: true
|
|
1702
|
+
},
|
|
1703
|
+
ETIMEDOUT: {
|
|
1704
|
+
message: "Connection timeout - the agent service is taking too long to respond. This could indicate network issues or an overloaded agent service.",
|
|
1705
|
+
category: "network",
|
|
1706
|
+
severity: "warning",
|
|
1707
|
+
actionable: true
|
|
1708
|
+
},
|
|
1709
|
+
terminated: {
|
|
1710
|
+
message: "Agent {context} was unexpectedly terminated. This often indicates an error in the agent service (e.g., authentication failures, missing environment variables, or agent crashes). Check the agent logs for the root cause.",
|
|
1711
|
+
category: "connection",
|
|
1712
|
+
severity: "error",
|
|
1713
|
+
actionable: true
|
|
1714
|
+
},
|
|
1715
|
+
UND_ERR_SOCKET: {
|
|
1716
|
+
message: "Socket connection was closed unexpectedly. This typically indicates the agent service encountered an error and shut down the connection. Check the agent logs for the underlying cause.",
|
|
1717
|
+
category: "connection",
|
|
1718
|
+
severity: "error",
|
|
1719
|
+
actionable: true
|
|
1720
|
+
},
|
|
1721
|
+
other_side_closed: {
|
|
1722
|
+
message: "The agent service closed the connection unexpectedly. This usually indicates an error in the agent service. Check the agent logs for more details.",
|
|
1723
|
+
category: "connection",
|
|
1724
|
+
severity: "error",
|
|
1725
|
+
actionable: true
|
|
1726
|
+
},
|
|
1727
|
+
fetch_failed: {
|
|
1728
|
+
message: "Failed to connect to the agent service. Please verify the agent is running and the endpoint URL is correct.",
|
|
1729
|
+
category: "network",
|
|
1730
|
+
severity: "error",
|
|
1731
|
+
actionable: true
|
|
1732
|
+
},
|
|
1733
|
+
// Authentication patterns
|
|
1734
|
+
"401": {
|
|
1735
|
+
message: "Authentication failed. Please check your API keys and ensure they are correctly configured.",
|
|
1736
|
+
category: "authentication",
|
|
1737
|
+
severity: "error",
|
|
1738
|
+
actionable: true
|
|
1739
|
+
},
|
|
1740
|
+
"api key": {
|
|
1741
|
+
message: "API key error detected. Please verify your API key is correct and has the necessary permissions.",
|
|
1742
|
+
category: "authentication",
|
|
1743
|
+
severity: "error",
|
|
1744
|
+
actionable: true
|
|
1745
|
+
},
|
|
1746
|
+
unauthorized: {
|
|
1747
|
+
message: "Unauthorized access. Please check your authentication credentials.",
|
|
1748
|
+
category: "authentication",
|
|
1749
|
+
severity: "error",
|
|
1750
|
+
actionable: true
|
|
1751
|
+
},
|
|
1752
|
+
// Python-specific error patterns
|
|
1753
|
+
AuthenticationError: {
|
|
1754
|
+
message: "OpenAI authentication failed. Please check your OPENAI_API_KEY environment variable or API key configuration.",
|
|
1755
|
+
category: "authentication",
|
|
1756
|
+
severity: "error",
|
|
1757
|
+
actionable: true
|
|
1758
|
+
},
|
|
1759
|
+
"Incorrect API key provided": {
|
|
1760
|
+
message: "OpenAI API key is invalid. Please verify your OPENAI_API_KEY is correct and active.",
|
|
1761
|
+
category: "authentication",
|
|
1762
|
+
severity: "error",
|
|
1763
|
+
actionable: true
|
|
1764
|
+
},
|
|
1765
|
+
RateLimitError: {
|
|
1766
|
+
message: "OpenAI rate limit exceeded. Please wait a moment and try again, or check your OpenAI usage limits.",
|
|
1767
|
+
category: "network",
|
|
1768
|
+
severity: "warning",
|
|
1769
|
+
actionable: true
|
|
1770
|
+
},
|
|
1771
|
+
InvalidRequestError: {
|
|
1772
|
+
message: "Invalid request to OpenAI API. Please check your request parameters and model configuration.",
|
|
1773
|
+
category: "validation",
|
|
1774
|
+
severity: "error",
|
|
1775
|
+
actionable: true
|
|
1776
|
+
},
|
|
1777
|
+
PermissionDeniedError: {
|
|
1778
|
+
message: "Permission denied for OpenAI API. Please check your API key permissions and billing status.",
|
|
1779
|
+
category: "authentication",
|
|
1780
|
+
severity: "error",
|
|
1781
|
+
actionable: true
|
|
1782
|
+
},
|
|
1783
|
+
NotFoundError: {
|
|
1784
|
+
message: "OpenAI resource not found. Please check your model name and availability.",
|
|
1785
|
+
category: "validation",
|
|
1786
|
+
severity: "error",
|
|
1787
|
+
actionable: true
|
|
1544
1788
|
}
|
|
1545
|
-
|
|
1789
|
+
},
|
|
1790
|
+
fallbacks: {
|
|
1791
|
+
network: "A network error occurred while connecting to the agent service. Please check your connection and ensure the agent service is running.",
|
|
1792
|
+
connection: "The connection to the agent service was lost unexpectedly. This may indicate an issue with the agent service.",
|
|
1793
|
+
authentication: "Authentication failed. Please check your API keys and credentials.",
|
|
1794
|
+
validation: "Invalid input or configuration. Please check your parameters and try again.",
|
|
1795
|
+
unknown: "An unexpected error occurred. Please check the logs for more details.",
|
|
1796
|
+
default: "An unexpected error occurred. Please check the logs for more details."
|
|
1797
|
+
},
|
|
1798
|
+
contextTemplates: {
|
|
1799
|
+
connection: "connection",
|
|
1800
|
+
event_streaming_connection: "event streaming connection",
|
|
1801
|
+
agent_streaming_connection: "agent streaming connection",
|
|
1802
|
+
langgraph_agent_connection: "LangGraph agent connection"
|
|
1546
1803
|
}
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1804
|
+
};
|
|
1805
|
+
function getFallbackMessage(category) {
|
|
1806
|
+
return errorConfig.fallbacks[category] || errorConfig.fallbacks.default;
|
|
1807
|
+
}
|
|
1808
|
+
__name(getFallbackMessage, "getFallbackMessage");
|
|
1809
|
+
|
|
1810
|
+
// src/lib/streaming.ts
|
|
1811
|
+
async function writeJsonLineResponseToEventStream(response, eventStream$) {
|
|
1812
|
+
const reader = response.getReader();
|
|
1813
|
+
const decoder = new TextDecoder();
|
|
1814
|
+
let buffer = [];
|
|
1815
|
+
function flushBuffer() {
|
|
1816
|
+
const currentBuffer = buffer.join("");
|
|
1817
|
+
if (currentBuffer.trim().length === 0) {
|
|
1818
|
+
return;
|
|
1557
1819
|
}
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
return content[0].text;
|
|
1820
|
+
const parts = currentBuffer.split("\n");
|
|
1821
|
+
if (parts.length === 0) {
|
|
1822
|
+
return;
|
|
1562
1823
|
}
|
|
1563
|
-
|
|
1824
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
1825
|
+
buffer = [];
|
|
1826
|
+
if (!lastPartIsComplete) {
|
|
1827
|
+
buffer.push(parts.pop());
|
|
1828
|
+
}
|
|
1829
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
1830
|
+
eventStream$.next(JSON.parse(part));
|
|
1831
|
+
});
|
|
1564
1832
|
}
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1833
|
+
__name(flushBuffer, "flushBuffer");
|
|
1834
|
+
try {
|
|
1835
|
+
while (true) {
|
|
1836
|
+
const { done, value } = await reader.read();
|
|
1837
|
+
if (!done) {
|
|
1838
|
+
buffer.push(decoder.decode(value, {
|
|
1839
|
+
stream: true
|
|
1840
|
+
}));
|
|
1841
|
+
}
|
|
1842
|
+
flushBuffer();
|
|
1843
|
+
if (done) {
|
|
1844
|
+
break;
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
} catch (error) {
|
|
1848
|
+
const structuredError = ensureStructuredError(error, convertStreamingErrorToStructured);
|
|
1849
|
+
eventStream$.error(structuredError);
|
|
1850
|
+
return;
|
|
1568
1851
|
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1852
|
+
eventStream$.complete();
|
|
1853
|
+
}
|
|
1854
|
+
__name(writeJsonLineResponseToEventStream, "writeJsonLineResponseToEventStream");
|
|
1855
|
+
function convertStreamingErrorToStructured(error) {
|
|
1856
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1857
|
+
let helpfulMessage = generateHelpfulErrorMessage(error);
|
|
1858
|
+
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("fetch failed")) || ((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.includes("ECONNREFUSED")) || ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("ENOTFOUND")) || ((_d = error == null ? void 0 : error.message) == null ? void 0 : _d.includes("ETIMEDOUT")) || ((_e = error == null ? void 0 : error.message) == null ? void 0 : _e.includes("terminated")) || ((_f = error == null ? void 0 : error.cause) == null ? void 0 : _f.code) === "UND_ERR_SOCKET" || ((_g = error == null ? void 0 : error.message) == null ? void 0 : _g.includes("other side closed")) || (error == null ? void 0 : error.code) === "UND_ERR_SOCKET") {
|
|
1859
|
+
return new CopilotKitLowLevelError({
|
|
1860
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
1861
|
+
url: "streaming connection",
|
|
1862
|
+
message: helpfulMessage
|
|
1863
|
+
});
|
|
1572
1864
|
}
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1865
|
+
return new CopilotKitError({
|
|
1866
|
+
message: helpfulMessage,
|
|
1867
|
+
code: CopilotKitErrorCode.UNKNOWN
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
__name(convertStreamingErrorToStructured, "convertStreamingErrorToStructured");
|
|
1871
|
+
function generateHelpfulErrorMessage(error, context = "connection") {
|
|
1872
|
+
var _a, _b, _c, _d;
|
|
1873
|
+
const baseMessage = (error == null ? void 0 : error.message) || String(error);
|
|
1874
|
+
const originalErrorType = (error == null ? void 0 : error.originalErrorType) || ((_a = error == null ? void 0 : error.extensions) == null ? void 0 : _a.originalErrorType);
|
|
1875
|
+
const statusCode = (error == null ? void 0 : error.statusCode) || ((_b = error == null ? void 0 : error.extensions) == null ? void 0 : _b.statusCode);
|
|
1876
|
+
const responseData = (error == null ? void 0 : error.responseData) || ((_c = error == null ? void 0 : error.extensions) == null ? void 0 : _c.responseData);
|
|
1877
|
+
if (originalErrorType) {
|
|
1878
|
+
const typeConfig = errorConfig.errorPatterns[originalErrorType];
|
|
1879
|
+
if (typeConfig) {
|
|
1880
|
+
return typeConfig.message.replace("{context}", context);
|
|
1881
|
+
}
|
|
1576
1882
|
}
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
acc.currentContent = this.getCurrentContent(event);
|
|
1583
|
-
acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
|
|
1584
|
-
const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
|
|
1585
|
-
const responseMetadata = this.getResponseMetadata(event);
|
|
1586
|
-
const toolCallCheck = toolCallChunks && toolCallChunks.length > 0;
|
|
1587
|
-
let isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
|
|
1588
|
-
acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
|
|
1589
|
-
acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
|
|
1590
|
-
let previousRoundHadToolCall = acc.isToolCall;
|
|
1591
|
-
acc.isToolCall = toolCallCheck;
|
|
1592
|
-
if (previousRoundHadToolCall && !toolCallCheck) {
|
|
1593
|
-
isToolCallEnd = true;
|
|
1594
|
-
}
|
|
1595
|
-
acc.isToolCallEnd = isToolCallEnd;
|
|
1596
|
-
acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
|
|
1597
|
-
({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
|
|
1598
|
-
name: acc.lastToolCallName,
|
|
1599
|
-
id: acc.lastToolCallId
|
|
1600
|
-
});
|
|
1601
|
-
}
|
|
1602
|
-
acc.event = event;
|
|
1603
|
-
lastEventWithState = acc;
|
|
1604
|
-
return acc;
|
|
1605
|
-
}, {
|
|
1606
|
-
event: null,
|
|
1607
|
-
isMessageStart: false,
|
|
1608
|
-
isMessageEnd: false,
|
|
1609
|
-
isToolCallStart: false,
|
|
1610
|
-
isToolCallEnd: false,
|
|
1611
|
-
isToolCall: false,
|
|
1612
|
-
lastMessageId: null,
|
|
1613
|
-
lastToolCallId: null,
|
|
1614
|
-
lastToolCallName: null,
|
|
1615
|
-
currentContent: null,
|
|
1616
|
-
processedToolCallIds: /* @__PURE__ */ new Set()
|
|
1617
|
-
}), mergeMap((acc) => {
|
|
1618
|
-
const events = [];
|
|
1619
|
-
let shouldEmitMessages = true;
|
|
1620
|
-
let shouldEmitToolCalls = true;
|
|
1621
|
-
if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
|
|
1622
|
-
if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
|
|
1623
|
-
shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
|
|
1624
|
-
}
|
|
1625
|
-
if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
|
|
1626
|
-
shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
|
|
1627
|
-
}
|
|
1628
|
-
}
|
|
1629
|
-
if (acc.event.event === LangGraphEventTypes.OnInterrupt) {
|
|
1630
|
-
events.push({
|
|
1631
|
-
type: RuntimeEventTypes.MetaEvent,
|
|
1632
|
-
name: RuntimeMetaEventName.LangGraphInterruptEvent,
|
|
1633
|
-
value: acc.event.value
|
|
1634
|
-
});
|
|
1635
|
-
}
|
|
1636
|
-
if (acc.event.event === LangGraphEventTypes.OnCopilotKitInterrupt) {
|
|
1637
|
-
events.push({
|
|
1638
|
-
type: RuntimeEventTypes.MetaEvent,
|
|
1639
|
-
name: RuntimeMetaEventName.CopilotKitLangGraphInterruptEvent,
|
|
1640
|
-
data: acc.event.data
|
|
1641
|
-
});
|
|
1642
|
-
}
|
|
1643
|
-
const responseMetadata = this.getResponseMetadata(acc.event);
|
|
1644
|
-
if (acc.isToolCallEnd && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName) && acc.lastToolCallId && !acc.processedToolCallIds.has(acc.lastToolCallId)) {
|
|
1645
|
-
acc.processedToolCallIds.add(acc.lastToolCallId);
|
|
1646
|
-
events.push({
|
|
1647
|
-
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
1648
|
-
actionExecutionId: acc.lastToolCallId
|
|
1649
|
-
});
|
|
1650
|
-
} else if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
|
|
1651
|
-
events.push({
|
|
1652
|
-
type: RuntimeEventTypes.TextMessageEnd,
|
|
1653
|
-
messageId: acc.lastMessageId
|
|
1654
|
-
});
|
|
1655
|
-
}
|
|
1656
|
-
switch (acc.event.event) {
|
|
1657
|
-
case LangGraphEventTypes.OnCustomEvent:
|
|
1658
|
-
if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
1659
|
-
events.push({
|
|
1660
|
-
type: RuntimeEventTypes.TextMessageStart,
|
|
1661
|
-
messageId: acc.event.data.message_id
|
|
1662
|
-
});
|
|
1663
|
-
events.push({
|
|
1664
|
-
type: RuntimeEventTypes.TextMessageContent,
|
|
1665
|
-
messageId: acc.event.data.message_id,
|
|
1666
|
-
content: acc.event.data.message
|
|
1667
|
-
});
|
|
1668
|
-
events.push({
|
|
1669
|
-
type: RuntimeEventTypes.TextMessageEnd,
|
|
1670
|
-
messageId: acc.event.data.message_id
|
|
1671
|
-
});
|
|
1672
|
-
} else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
1673
|
-
events.push({
|
|
1674
|
-
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1675
|
-
actionExecutionId: acc.event.data.id,
|
|
1676
|
-
actionName: acc.event.data.name,
|
|
1677
|
-
parentMessageId: acc.event.data.id
|
|
1678
|
-
});
|
|
1679
|
-
events.push({
|
|
1680
|
-
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1681
|
-
actionExecutionId: acc.event.data.id,
|
|
1682
|
-
args: JSON.stringify(acc.event.data.args)
|
|
1683
|
-
});
|
|
1684
|
-
events.push({
|
|
1685
|
-
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
1686
|
-
actionExecutionId: acc.event.data.id
|
|
1687
|
-
});
|
|
1688
|
-
}
|
|
1689
|
-
break;
|
|
1690
|
-
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
1691
|
-
events.push({
|
|
1692
|
-
type: RuntimeEventTypes.AgentStateMessage,
|
|
1693
|
-
threadId: acc.event.thread_id,
|
|
1694
|
-
role: acc.event.role,
|
|
1695
|
-
agentName: acc.event.agent_name,
|
|
1696
|
-
nodeName: acc.event.node_name,
|
|
1697
|
-
runId: acc.event.run_id,
|
|
1698
|
-
active: acc.event.active,
|
|
1699
|
-
state: JSON.stringify(acc.event.state),
|
|
1700
|
-
running: acc.event.running
|
|
1701
|
-
});
|
|
1702
|
-
break;
|
|
1703
|
-
case LangGraphEventTypes.OnChatModelStream:
|
|
1704
|
-
if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
1705
|
-
events.push({
|
|
1706
|
-
type: RuntimeEventTypes.ActionExecutionStart,
|
|
1707
|
-
actionExecutionId: acc.lastToolCallId,
|
|
1708
|
-
actionName: acc.lastToolCallName,
|
|
1709
|
-
parentMessageId: acc.lastMessageId
|
|
1710
|
-
});
|
|
1711
|
-
} else if (acc.isMessageStart && shouldEmitMessages) {
|
|
1712
|
-
acc.processedToolCallIds.clear();
|
|
1713
|
-
events.push({
|
|
1714
|
-
type: RuntimeEventTypes.TextMessageStart,
|
|
1715
|
-
messageId: acc.lastMessageId
|
|
1716
|
-
});
|
|
1717
|
-
}
|
|
1718
|
-
if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
1719
|
-
events.push({
|
|
1720
|
-
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
1721
|
-
actionExecutionId: acc.lastToolCallId,
|
|
1722
|
-
args: acc.currentContent
|
|
1723
|
-
});
|
|
1724
|
-
} else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
|
|
1725
|
-
events.push({
|
|
1726
|
-
type: RuntimeEventTypes.TextMessageContent,
|
|
1727
|
-
messageId: acc.lastMessageId,
|
|
1728
|
-
content: acc.currentContent
|
|
1729
|
-
});
|
|
1730
|
-
}
|
|
1731
|
-
break;
|
|
1732
|
-
}
|
|
1733
|
-
return events;
|
|
1734
|
-
}), catchError((error) => {
|
|
1735
|
-
console.error(error);
|
|
1736
|
-
if (error instanceof CopilotKitError || (error == null ? void 0 : error.name) && error.name.includes("CopilotKit")) {
|
|
1737
|
-
throw error;
|
|
1738
|
-
}
|
|
1739
|
-
const events = [];
|
|
1740
|
-
if ((lastEventWithState == null ? void 0 : lastEventWithState.lastMessageId) && !lastEventWithState.isToolCall) {
|
|
1741
|
-
events.push({
|
|
1742
|
-
type: RuntimeEventTypes.TextMessageEnd,
|
|
1743
|
-
messageId: lastEventWithState.lastMessageId
|
|
1744
|
-
});
|
|
1745
|
-
}
|
|
1746
|
-
if (lastEventWithState == null ? void 0 : lastEventWithState.lastToolCallId) {
|
|
1747
|
-
events.push({
|
|
1748
|
-
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
1749
|
-
actionExecutionId: lastEventWithState.lastToolCallId
|
|
1750
|
-
});
|
|
1751
|
-
}
|
|
1752
|
-
const messageId = randomId();
|
|
1753
|
-
events.push({
|
|
1754
|
-
type: RuntimeEventTypes.TextMessageStart,
|
|
1755
|
-
messageId
|
|
1756
|
-
});
|
|
1757
|
-
events.push({
|
|
1758
|
-
type: RuntimeEventTypes.TextMessageContent,
|
|
1759
|
-
messageId,
|
|
1760
|
-
content: "\u274C An error occurred. Please try again."
|
|
1761
|
-
});
|
|
1762
|
-
events.push({
|
|
1763
|
-
type: RuntimeEventTypes.TextMessageEnd,
|
|
1764
|
-
messageId
|
|
1765
|
-
});
|
|
1766
|
-
return events;
|
|
1767
|
-
}));
|
|
1883
|
+
for (const [pattern, config] of Object.entries(errorConfig.errorPatterns)) {
|
|
1884
|
+
const shouldMatch = (baseMessage == null ? void 0 : baseMessage.includes(pattern)) || ((_d = error == null ? void 0 : error.cause) == null ? void 0 : _d.code) === pattern || (error == null ? void 0 : error.code) === pattern || statusCode === parseInt(pattern) || pattern === "other_side_closed" && (baseMessage == null ? void 0 : baseMessage.includes("other side closed")) || pattern === "fetch_failed" && (baseMessage == null ? void 0 : baseMessage.includes("fetch failed")) || responseData && JSON.stringify(responseData).includes(pattern);
|
|
1885
|
+
if (shouldMatch) {
|
|
1886
|
+
return config.message.replace("{context}", context);
|
|
1887
|
+
}
|
|
1768
1888
|
}
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
// src/lib/runtime/remote-lg-action.ts
|
|
1773
|
-
import { Client as LangGraphClient } from "@langchain/langgraph-sdk";
|
|
1774
|
-
import { createHash } from "crypto";
|
|
1775
|
-
import { isValidUUID, randomUUID } from "@copilotkit/shared";
|
|
1776
|
-
import { parse as parsePartialJson } from "partial-json";
|
|
1777
|
-
import { parseJson, CopilotKitMisuseError } from "@copilotkit/shared";
|
|
1778
|
-
import { RemoveMessage } from "@langchain/core/messages";
|
|
1779
|
-
|
|
1780
|
-
// src/lib/runtime/retry-utils.ts
|
|
1781
|
-
var RETRY_CONFIG = {
|
|
1782
|
-
maxRetries: 3,
|
|
1783
|
-
baseDelayMs: 1e3,
|
|
1784
|
-
maxDelayMs: 5e3,
|
|
1785
|
-
// HTTP status codes that should be retried
|
|
1786
|
-
retryableStatusCodes: [
|
|
1787
|
-
502,
|
|
1788
|
-
503,
|
|
1789
|
-
504,
|
|
1790
|
-
408,
|
|
1791
|
-
429
|
|
1792
|
-
],
|
|
1793
|
-
// Network error patterns that should be retried
|
|
1794
|
-
retryableErrorMessages: [
|
|
1795
|
-
"fetch failed",
|
|
1796
|
-
"network error",
|
|
1797
|
-
"connection timeout",
|
|
1798
|
-
"ECONNREFUSED",
|
|
1799
|
-
"ETIMEDOUT",
|
|
1800
|
-
"ENOTFOUND",
|
|
1801
|
-
"ECONNRESET"
|
|
1802
|
-
]
|
|
1803
|
-
};
|
|
1804
|
-
function isRetryableError(error, response) {
|
|
1805
|
-
var _a, _b;
|
|
1806
|
-
if (response && RETRY_CONFIG.retryableStatusCodes.includes(response.status)) {
|
|
1807
|
-
return true;
|
|
1889
|
+
if (isNetworkError(error)) {
|
|
1890
|
+
return getFallbackMessage("network");
|
|
1808
1891
|
}
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
return true;
|
|
1892
|
+
if (isConnectionError(error)) {
|
|
1893
|
+
return getFallbackMessage("connection");
|
|
1812
1894
|
}
|
|
1813
|
-
|
|
1814
|
-
|
|
1895
|
+
if (isAuthenticationError(error)) {
|
|
1896
|
+
return getFallbackMessage("authentication");
|
|
1897
|
+
}
|
|
1898
|
+
return getFallbackMessage("default");
|
|
1815
1899
|
}
|
|
1816
|
-
__name(
|
|
1817
|
-
function
|
|
1818
|
-
|
|
1900
|
+
__name(generateHelpfulErrorMessage, "generateHelpfulErrorMessage");
|
|
1901
|
+
function isNetworkError(error) {
|
|
1902
|
+
const networkPatterns = [
|
|
1903
|
+
"ECONNREFUSED",
|
|
1904
|
+
"ENOTFOUND",
|
|
1905
|
+
"ETIMEDOUT",
|
|
1906
|
+
"fetch_failed"
|
|
1907
|
+
];
|
|
1908
|
+
return networkPatterns.some((pattern) => {
|
|
1909
|
+
var _a, _b;
|
|
1910
|
+
return ((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes(pattern)) || ((_b = error == null ? void 0 : error.cause) == null ? void 0 : _b.code) === pattern || (error == null ? void 0 : error.code) === pattern;
|
|
1911
|
+
});
|
|
1819
1912
|
}
|
|
1820
|
-
__name(
|
|
1821
|
-
function
|
|
1822
|
-
const
|
|
1823
|
-
|
|
1913
|
+
__name(isNetworkError, "isNetworkError");
|
|
1914
|
+
function isConnectionError(error) {
|
|
1915
|
+
const connectionPatterns = [
|
|
1916
|
+
"terminated",
|
|
1917
|
+
"UND_ERR_SOCKET",
|
|
1918
|
+
"other side closed"
|
|
1919
|
+
];
|
|
1920
|
+
return connectionPatterns.some((pattern) => {
|
|
1921
|
+
var _a, _b;
|
|
1922
|
+
return ((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes(pattern)) || ((_b = error == null ? void 0 : error.cause) == null ? void 0 : _b.code) === pattern || (error == null ? void 0 : error.code) === pattern;
|
|
1923
|
+
});
|
|
1824
1924
|
}
|
|
1825
|
-
__name(
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
if (isRetryableError(error) && attempt < RETRY_CONFIG.maxRetries) {
|
|
1841
|
-
const delay = calculateDelay(attempt);
|
|
1842
|
-
logger2 == null ? void 0 : logger2.warn(`Request to ${url} failed with network error. Retrying attempt ${attempt + 1}/${RETRY_CONFIG.maxRetries + 1} in ${delay}ms. Error: ${(error == null ? void 0 : error.message) || String(error)}`);
|
|
1843
|
-
await sleep(delay);
|
|
1844
|
-
continue;
|
|
1845
|
-
}
|
|
1846
|
-
break;
|
|
1847
|
-
}
|
|
1848
|
-
}
|
|
1849
|
-
throw lastError;
|
|
1925
|
+
__name(isConnectionError, "isConnectionError");
|
|
1926
|
+
function isAuthenticationError(error) {
|
|
1927
|
+
var _a, _b;
|
|
1928
|
+
const authPatterns = [
|
|
1929
|
+
"401",
|
|
1930
|
+
"api key",
|
|
1931
|
+
"unauthorized",
|
|
1932
|
+
"authentication",
|
|
1933
|
+
"AuthenticationError",
|
|
1934
|
+
"PermissionDeniedError"
|
|
1935
|
+
];
|
|
1936
|
+
const baseMessage = (error == null ? void 0 : error.message) || String(error);
|
|
1937
|
+
const originalErrorType = (error == null ? void 0 : error.originalErrorType) || ((_a = error == null ? void 0 : error.extensions) == null ? void 0 : _a.originalErrorType);
|
|
1938
|
+
const statusCode = (error == null ? void 0 : error.statusCode) || ((_b = error == null ? void 0 : error.extensions) == null ? void 0 : _b.statusCode);
|
|
1939
|
+
return authPatterns.some((pattern) => (baseMessage == null ? void 0 : baseMessage.toLowerCase().includes(pattern.toLowerCase())) || originalErrorType === pattern || statusCode === 401 || (error == null ? void 0 : error.status) === 401 || (error == null ? void 0 : error.statusCode) === 401);
|
|
1850
1940
|
}
|
|
1851
|
-
__name(
|
|
1941
|
+
__name(isAuthenticationError, "isAuthenticationError");
|
|
1852
1942
|
|
|
1853
1943
|
// src/lib/runtime/remote-lg-action.ts
|
|
1854
|
-
|
|
1944
|
+
function isUserConfigurationError(error) {
|
|
1945
|
+
var _a, _b;
|
|
1946
|
+
return (error instanceof CopilotKitError2 || error instanceof CopilotKitLowLevelError2) && (error.code === "NETWORK_ERROR" || error.code === "AUTHENTICATION_ERROR" || error.statusCode === 401 || error.statusCode === 403 || ((_a = error.message) == null ? void 0 : _a.toLowerCase().includes("authentication")) || ((_b = error.message) == null ? void 0 : _b.toLowerCase().includes("api key")));
|
|
1947
|
+
}
|
|
1948
|
+
__name(isUserConfigurationError, "isUserConfigurationError");
|
|
1855
1949
|
async function execute(args) {
|
|
1856
1950
|
return new ReadableStream({
|
|
1857
1951
|
async start(controller) {
|
|
@@ -1882,6 +1976,9 @@ async function execute(args) {
|
|
|
1882
1976
|
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
1883
1977
|
});
|
|
1884
1978
|
} else {
|
|
1979
|
+
if (lastError instanceof CopilotKitError2 || lastError instanceof CopilotKitLowLevelError2 || lastError instanceof Error && lastError.name && lastError.name.includes("CopilotKit")) {
|
|
1980
|
+
throw lastError;
|
|
1981
|
+
}
|
|
1885
1982
|
throw new CopilotKitMisuseError({
|
|
1886
1983
|
message: `
|
|
1887
1984
|
The LangGraph client threw unhandled error ${lastError}.
|
|
@@ -1894,7 +1991,7 @@ async function execute(args) {
|
|
|
1894
1991
|
}
|
|
1895
1992
|
__name(execute, "execute");
|
|
1896
1993
|
async function streamEvents(controller, args) {
|
|
1897
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1994
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
1898
1995
|
const { deploymentUrl, langsmithApiKey, threadId: argsInitialThreadId, agent, nodeName: initialNodeName, state: initialState, config: explicitConfig, messages, actions, logger: logger2, properties, metaEvents } = args;
|
|
1899
1996
|
let nodeName = initialNodeName;
|
|
1900
1997
|
let state = initialState;
|
|
@@ -1952,18 +2049,14 @@ async function streamEvents(controller, args) {
|
|
|
1952
2049
|
command: void 0
|
|
1953
2050
|
};
|
|
1954
2051
|
const lgInterruptMetaEvent = metaEvents == null ? void 0 : metaEvents.find((ev) => ev.name === MetaEventName.LangGraphInterruptEvent);
|
|
1955
|
-
if (activeInterruptEvent && !lgInterruptMetaEvent) {
|
|
1956
|
-
payload.command = {
|
|
1957
|
-
resume: state.messages
|
|
1958
|
-
};
|
|
1959
|
-
}
|
|
1960
2052
|
if (lgInterruptMetaEvent == null ? void 0 : lgInterruptMetaEvent.response) {
|
|
1961
2053
|
let response = lgInterruptMetaEvent.response;
|
|
1962
2054
|
payload.command = {
|
|
1963
2055
|
resume: parseJson(response, response)
|
|
1964
2056
|
};
|
|
1965
2057
|
}
|
|
1966
|
-
|
|
2058
|
+
const interrupts = ((_b = (_a = agentState.tasks) == null ? void 0 : _a[0]) == null ? void 0 : _b.interrupts) ?? [];
|
|
2059
|
+
if (mode === "continue" && !interrupts.length) {
|
|
1967
2060
|
await client.threads.updateState(threadId, {
|
|
1968
2061
|
values: state,
|
|
1969
2062
|
asNode: nodeName
|
|
@@ -2025,12 +2118,23 @@ async function streamEvents(controller, args) {
|
|
|
2025
2118
|
let emitIntermediateStateUntilEnd = null;
|
|
2026
2119
|
let shouldExit = false;
|
|
2027
2120
|
let externalRunId = null;
|
|
2028
|
-
const streamResponse = client.runs.stream(threadId, assistantId, payload);
|
|
2029
2121
|
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
2122
|
+
if ((interrupts == null ? void 0 : interrupts.length) && !((_c = payload.command) == null ? void 0 : _c.resume)) {
|
|
2123
|
+
if (!lgInterruptMetaEvent) {
|
|
2124
|
+
payload.command = {
|
|
2125
|
+
resume: state.messages
|
|
2126
|
+
};
|
|
2127
|
+
} else {
|
|
2128
|
+
interrupts.forEach((interrupt) => {
|
|
2129
|
+
emitInterrupt(interrupt.value, emit);
|
|
2130
|
+
});
|
|
2131
|
+
return Promise.resolve();
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
const streamResponse = client.runs.stream(threadId, assistantId, payload);
|
|
2030
2135
|
let latestStateValues = {};
|
|
2031
2136
|
let updatedState = state;
|
|
2032
2137
|
let manuallyEmittedState = null;
|
|
2033
|
-
activeInterruptEvent = false;
|
|
2034
2138
|
try {
|
|
2035
2139
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_started", {
|
|
2036
2140
|
hashedLgcKey: streamInfo.hashedLgcKey
|
|
@@ -2044,28 +2148,36 @@ async function streamEvents(controller, args) {
|
|
|
2044
2148
|
].includes(streamResponseChunk.event))
|
|
2045
2149
|
continue;
|
|
2046
2150
|
if (streamResponseChunk.event === "error") {
|
|
2047
|
-
|
|
2151
|
+
const errorData = streamResponseChunk.data;
|
|
2152
|
+
if (errorData && typeof errorData === "object" && "error_details" in errorData) {
|
|
2153
|
+
const errorDetails = errorData.error_details;
|
|
2154
|
+
const preservedError = new CopilotKitLowLevelError2({
|
|
2155
|
+
error: new Error(errorDetails.message),
|
|
2156
|
+
url: "langgraph platform agent",
|
|
2157
|
+
message: `${errorDetails.type}: ${errorDetails.message}`
|
|
2158
|
+
});
|
|
2159
|
+
if (errorDetails.status_code) {
|
|
2160
|
+
preservedError.statusCode = errorDetails.status_code;
|
|
2161
|
+
}
|
|
2162
|
+
if (errorDetails.response_data) {
|
|
2163
|
+
preservedError.responseData = errorDetails.response_data;
|
|
2164
|
+
}
|
|
2165
|
+
preservedError.agentName = errorDetails.agent_name;
|
|
2166
|
+
preservedError.originalErrorType = errorDetails.type;
|
|
2167
|
+
throw preservedError;
|
|
2168
|
+
}
|
|
2169
|
+
const helpfulMessage = generateHelpfulErrorMessage(new Error(errorData.message), "LangGraph Platform agent");
|
|
2170
|
+
throw new CopilotKitLowLevelError2({
|
|
2171
|
+
error: new Error(errorData.message),
|
|
2172
|
+
url: "langgraph platform agent",
|
|
2173
|
+
message: helpfulMessage
|
|
2174
|
+
});
|
|
2048
2175
|
}
|
|
2049
2176
|
const chunk = streamResponseChunk;
|
|
2050
2177
|
const interruptEvents = chunk.data.__interrupt__;
|
|
2051
2178
|
if (interruptEvents == null ? void 0 : interruptEvents.length) {
|
|
2052
|
-
activeInterruptEvent = true;
|
|
2053
2179
|
const interruptValue = interruptEvents == null ? void 0 : interruptEvents[0].value;
|
|
2054
|
-
|
|
2055
|
-
const evValue = interruptValue.__copilotkit_interrupt_value__;
|
|
2056
|
-
emit(JSON.stringify({
|
|
2057
|
-
event: LangGraphEventTypes.OnCopilotKitInterrupt,
|
|
2058
|
-
data: {
|
|
2059
|
-
value: typeof evValue === "string" ? evValue : JSON.stringify(evValue),
|
|
2060
|
-
messages: langchainMessagesToCopilotKit(interruptValue.__copilotkit_messages__)
|
|
2061
|
-
}
|
|
2062
|
-
}) + "\n");
|
|
2063
|
-
} else {
|
|
2064
|
-
emit(JSON.stringify({
|
|
2065
|
-
event: LangGraphEventTypes.OnInterrupt,
|
|
2066
|
-
value: typeof interruptValue === "string" ? interruptValue : JSON.stringify(interruptValue)
|
|
2067
|
-
}) + "\n");
|
|
2068
|
-
}
|
|
2180
|
+
emitInterrupt(interruptValue, emit);
|
|
2069
2181
|
continue;
|
|
2070
2182
|
}
|
|
2071
2183
|
if (streamResponseChunk.event === "updates")
|
|
@@ -2080,8 +2192,8 @@ async function streamEvents(controller, args) {
|
|
|
2080
2192
|
const runId = chunkData.metadata.run_id;
|
|
2081
2193
|
externalRunId = runId;
|
|
2082
2194
|
const metadata = chunkData.metadata;
|
|
2083
|
-
if (((
|
|
2084
|
-
streamInfo.provider = (
|
|
2195
|
+
if (((_e = (_d = chunkData.data) == null ? void 0 : _d.output) == null ? void 0 : _e.model) != null && ((_g = (_f = chunkData.data) == null ? void 0 : _f.output) == null ? void 0 : _g.model) != "") {
|
|
2196
|
+
streamInfo.provider = (_i = (_h = chunkData.data) == null ? void 0 : _h.output) == null ? void 0 : _i.model;
|
|
2085
2197
|
}
|
|
2086
2198
|
if (metadata.langgraph_host != null && metadata.langgraph_host != "") {
|
|
2087
2199
|
streamInfo.langGraphHost = metadata.langgraph_host;
|
|
@@ -2152,9 +2264,9 @@ async function streamEvents(controller, args) {
|
|
|
2152
2264
|
emit(JSON.stringify(chunkData) + "\n");
|
|
2153
2265
|
}
|
|
2154
2266
|
state = await client.threads.getState(threadId);
|
|
2155
|
-
const
|
|
2156
|
-
nodeName =
|
|
2157
|
-
const isEndNode = state.next.length === 0 && !
|
|
2267
|
+
const interrupts2 = (_k = (_j = state.tasks) == null ? void 0 : _j[0]) == null ? void 0 : _k.interrupts;
|
|
2268
|
+
nodeName = interrupts2 ? nodeName : Object.keys(state.metadata.writes)[0];
|
|
2269
|
+
const isEndNode = state.next.length === 0 && !interrupts2;
|
|
2158
2270
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_ended", streamInfo);
|
|
2159
2271
|
emit(getStateSyncEvent({
|
|
2160
2272
|
threadId,
|
|
@@ -2169,11 +2281,21 @@ async function streamEvents(controller, args) {
|
|
|
2169
2281
|
}));
|
|
2170
2282
|
return Promise.resolve();
|
|
2171
2283
|
} catch (e) {
|
|
2172
|
-
|
|
2284
|
+
if (isUserConfigurationError(e)) {
|
|
2285
|
+
logger2.debug({
|
|
2286
|
+
error: e.message,
|
|
2287
|
+
code: e.code
|
|
2288
|
+
}, "User configuration error");
|
|
2289
|
+
} else {
|
|
2290
|
+
logger2.error(e);
|
|
2291
|
+
}
|
|
2173
2292
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_errored", {
|
|
2174
2293
|
...streamInfo,
|
|
2175
2294
|
error: e.message
|
|
2176
2295
|
});
|
|
2296
|
+
if (e instanceof CopilotKitError2 || e instanceof CopilotKitLowLevelError2 || e instanceof Error && e.name && e.name.includes("CopilotKit")) {
|
|
2297
|
+
throw e;
|
|
2298
|
+
}
|
|
2177
2299
|
return Promise.resolve();
|
|
2178
2300
|
}
|
|
2179
2301
|
}
|
|
@@ -2486,195 +2608,528 @@ function filterObjectBySchemaKeys(obj, schemaKeys) {
|
|
|
2486
2608
|
return Object.fromEntries(Object.entries(obj).filter(([key]) => schemaKeys.includes(key)));
|
|
2487
2609
|
}
|
|
2488
2610
|
__name(filterObjectBySchemaKeys, "filterObjectBySchemaKeys");
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
const decoder = new TextDecoder();
|
|
2498
|
-
let buffer = [];
|
|
2499
|
-
function flushBuffer() {
|
|
2500
|
-
const currentBuffer = buffer.join("");
|
|
2501
|
-
if (currentBuffer.trim().length === 0) {
|
|
2502
|
-
return;
|
|
2503
|
-
}
|
|
2504
|
-
const parts = currentBuffer.split("\n");
|
|
2505
|
-
if (parts.length === 0) {
|
|
2506
|
-
return;
|
|
2507
|
-
}
|
|
2508
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
2509
|
-
buffer = [];
|
|
2510
|
-
if (!lastPartIsComplete) {
|
|
2511
|
-
buffer.push(parts.pop());
|
|
2512
|
-
}
|
|
2513
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
2514
|
-
eventStream$.next(JSON.parse(part));
|
|
2515
|
-
});
|
|
2516
|
-
}
|
|
2517
|
-
__name(flushBuffer, "flushBuffer");
|
|
2518
|
-
try {
|
|
2519
|
-
while (true) {
|
|
2520
|
-
const { done, value } = await reader.read();
|
|
2521
|
-
if (!done) {
|
|
2522
|
-
buffer.push(decoder.decode(value, {
|
|
2523
|
-
stream: true
|
|
2524
|
-
}));
|
|
2525
|
-
}
|
|
2526
|
-
flushBuffer();
|
|
2527
|
-
if (done) {
|
|
2528
|
-
break;
|
|
2611
|
+
function emitInterrupt(interruptValue, emit) {
|
|
2612
|
+
if (typeof interruptValue != "string" && "__copilotkit_interrupt_value__" in interruptValue) {
|
|
2613
|
+
const evValue = interruptValue.__copilotkit_interrupt_value__;
|
|
2614
|
+
emit(JSON.stringify({
|
|
2615
|
+
event: LangGraphEventTypes.OnCopilotKitInterrupt,
|
|
2616
|
+
data: {
|
|
2617
|
+
value: typeof evValue === "string" ? evValue : JSON.stringify(evValue),
|
|
2618
|
+
messages: langchainMessagesToCopilotKit(interruptValue.__copilotkit_messages__)
|
|
2529
2619
|
}
|
|
2530
|
-
}
|
|
2531
|
-
}
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2620
|
+
}) + "\n");
|
|
2621
|
+
} else {
|
|
2622
|
+
emit(JSON.stringify({
|
|
2623
|
+
event: LangGraphEventTypes.OnInterrupt,
|
|
2624
|
+
value: typeof interruptValue === "string" ? interruptValue : JSON.stringify(interruptValue)
|
|
2625
|
+
}) + "\n");
|
|
2536
2626
|
}
|
|
2537
|
-
eventStream$.complete();
|
|
2538
2627
|
}
|
|
2539
|
-
__name(
|
|
2540
|
-
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
|
|
2546
|
-
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
url: "streaming connection",
|
|
2552
|
-
message: "Network error occurred during streaming. Please check your connection and try again."
|
|
2553
|
-
});
|
|
2628
|
+
__name(emitInterrupt, "emitInterrupt");
|
|
2629
|
+
|
|
2630
|
+
// src/lib/runtime/mcp-tools-utils.ts
|
|
2631
|
+
function extractParametersFromSchema(toolOrSchema) {
|
|
2632
|
+
var _a;
|
|
2633
|
+
const parameters = [];
|
|
2634
|
+
const schema = "schema" in (toolOrSchema || {}) ? toolOrSchema.schema : toolOrSchema;
|
|
2635
|
+
const toolParameters = (schema == null ? void 0 : schema.parameters) || ((_a = schema == null ? void 0 : schema.parameters) == null ? void 0 : _a.jsonSchema);
|
|
2636
|
+
const properties = toolParameters == null ? void 0 : toolParameters.properties;
|
|
2637
|
+
const requiredParams = new Set((toolParameters == null ? void 0 : toolParameters.required) || []);
|
|
2638
|
+
if (!properties) {
|
|
2639
|
+
return parameters;
|
|
2554
2640
|
}
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2641
|
+
for (const paramName in properties) {
|
|
2642
|
+
if (Object.prototype.hasOwnProperty.call(properties, paramName)) {
|
|
2643
|
+
const paramDef = properties[paramName];
|
|
2644
|
+
parameters.push({
|
|
2645
|
+
name: paramName,
|
|
2646
|
+
// Infer type, default to string. MCP schemas might have more complex types.
|
|
2647
|
+
// This might need refinement based on common MCP schema practices.
|
|
2648
|
+
type: paramDef.type || "string",
|
|
2649
|
+
description: paramDef.description,
|
|
2650
|
+
required: requiredParams.has(paramName)
|
|
2651
|
+
});
|
|
2652
|
+
}
|
|
2560
2653
|
}
|
|
2561
|
-
return
|
|
2562
|
-
message: `Streaming error: ${(error == null ? void 0 : error.message) || String(error)}`,
|
|
2563
|
-
code: CopilotKitErrorCode.UNKNOWN
|
|
2564
|
-
});
|
|
2654
|
+
return parameters;
|
|
2565
2655
|
}
|
|
2566
|
-
__name(
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
const agents = endpoint.agents.map((agent) => ({
|
|
2573
|
-
name: agent.name,
|
|
2574
|
-
description: agent.description,
|
|
2575
|
-
parameters: [],
|
|
2576
|
-
handler: async (_args) => {
|
|
2577
|
-
},
|
|
2578
|
-
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
2579
|
-
logger2.debug({
|
|
2580
|
-
actionName: agent.name
|
|
2581
|
-
}, "Executing LangGraph Platform agent");
|
|
2582
|
-
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
2583
|
-
agentExecution: true,
|
|
2584
|
-
type: "langgraph-platform",
|
|
2585
|
-
agentsAmount: endpoint.agents.length,
|
|
2586
|
-
hashedLgcKey: endpoint.langsmithApiKey ? createHash2("sha256").update(endpoint.langsmithApiKey).digest("hex") : null
|
|
2587
|
-
});
|
|
2588
|
-
let state = {};
|
|
2589
|
-
let config = {};
|
|
2590
|
-
if (agentStates) {
|
|
2591
|
-
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
2592
|
-
if (jsonState) {
|
|
2593
|
-
state = parseJson2(jsonState.state, {});
|
|
2594
|
-
config = parseJson2(jsonState.config, {});
|
|
2595
|
-
}
|
|
2596
|
-
}
|
|
2656
|
+
__name(extractParametersFromSchema, "extractParametersFromSchema");
|
|
2657
|
+
function convertMCPToolsToActions(mcpTools, mcpEndpoint) {
|
|
2658
|
+
const actions = [];
|
|
2659
|
+
for (const [toolName, tool] of Object.entries(mcpTools)) {
|
|
2660
|
+
const parameters = extractParametersFromSchema(tool);
|
|
2661
|
+
const handler = /* @__PURE__ */ __name(async (params) => {
|
|
2597
2662
|
try {
|
|
2598
|
-
const
|
|
2599
|
-
|
|
2600
|
-
component: "remote-actions.remote-lg-action.streamEvents"
|
|
2601
|
-
}),
|
|
2602
|
-
deploymentUrl: endpoint.deploymentUrl,
|
|
2603
|
-
langsmithApiKey: endpoint.langsmithApiKey,
|
|
2604
|
-
agent,
|
|
2605
|
-
threadId,
|
|
2606
|
-
nodeName,
|
|
2607
|
-
messages: [
|
|
2608
|
-
...messages,
|
|
2609
|
-
...additionalMessages
|
|
2610
|
-
],
|
|
2611
|
-
state,
|
|
2612
|
-
config,
|
|
2613
|
-
properties: graphqlContext.properties,
|
|
2614
|
-
actions: tryMap(actionInputsWithoutAgents, (action) => ({
|
|
2615
|
-
name: action.name,
|
|
2616
|
-
description: action.description,
|
|
2617
|
-
parameters: JSON.parse(action.jsonSchema)
|
|
2618
|
-
})),
|
|
2619
|
-
metaEvents
|
|
2663
|
+
const result = await tool.execute({
|
|
2664
|
+
params
|
|
2620
2665
|
});
|
|
2621
|
-
|
|
2622
|
-
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
2623
|
-
return eventSource.processLangGraphEvents();
|
|
2666
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
2624
2667
|
} catch (error) {
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2668
|
+
console.error(`Error executing MCP tool '${toolName}' from endpoint ${mcpEndpoint}:`, error);
|
|
2669
|
+
throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`);
|
|
2670
|
+
}
|
|
2671
|
+
}, "handler");
|
|
2672
|
+
actions.push({
|
|
2673
|
+
name: toolName,
|
|
2674
|
+
description: tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,
|
|
2675
|
+
parameters,
|
|
2676
|
+
handler,
|
|
2677
|
+
// Add metadata for easier identification/debugging
|
|
2678
|
+
_isMCPTool: true,
|
|
2679
|
+
_mcpEndpoint: mcpEndpoint
|
|
2680
|
+
});
|
|
2681
|
+
}
|
|
2682
|
+
return actions;
|
|
2683
|
+
}
|
|
2684
|
+
__name(convertMCPToolsToActions, "convertMCPToolsToActions");
|
|
2685
|
+
function generateMcpToolInstructions(toolsMap) {
|
|
2686
|
+
if (!toolsMap || Object.keys(toolsMap).length === 0) {
|
|
2687
|
+
return "";
|
|
2688
|
+
}
|
|
2689
|
+
const toolEntries = Object.entries(toolsMap);
|
|
2690
|
+
const toolsDoc = toolEntries.map(([name, tool]) => {
|
|
2691
|
+
let paramsDoc = " No parameters required";
|
|
2692
|
+
try {
|
|
2693
|
+
if (tool.schema && typeof tool.schema === "object") {
|
|
2694
|
+
const schema = tool.schema;
|
|
2695
|
+
if (schema.properties) {
|
|
2696
|
+
const requiredParams = schema.required || [];
|
|
2697
|
+
const paramsList = Object.entries(schema.properties).map(([paramName, propSchema]) => {
|
|
2698
|
+
const propDetails = propSchema;
|
|
2699
|
+
const requiredMark = requiredParams.includes(paramName) ? "*" : "";
|
|
2700
|
+
const typeInfo = propDetails.type || "any";
|
|
2701
|
+
const description = propDetails.description ? ` - ${propDetails.description}` : "";
|
|
2702
|
+
return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;
|
|
2703
|
+
});
|
|
2704
|
+
if (paramsList.length > 0) {
|
|
2705
|
+
paramsDoc = paramsList.join("\n");
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2631
2708
|
}
|
|
2709
|
+
} catch (e) {
|
|
2710
|
+
console.error(`Error parsing schema for tool ${name}:`, e);
|
|
2632
2711
|
}
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2712
|
+
return `- ${name}: ${tool.description || ""}
|
|
2713
|
+
${paramsDoc}`;
|
|
2714
|
+
}).join("\n\n");
|
|
2715
|
+
return `You have access to the following external tools provided by Model Context Protocol (MCP) servers:
|
|
2716
|
+
|
|
2717
|
+
${toolsDoc}
|
|
2718
|
+
|
|
2719
|
+
When using these tools:
|
|
2720
|
+
1. Only provide valid parameters according to their type requirements
|
|
2721
|
+
2. Required parameters are marked with *
|
|
2722
|
+
3. Format API calls correctly with the expected parameter structure
|
|
2723
|
+
4. Always check tool responses to determine your next action`;
|
|
2637
2724
|
}
|
|
2638
|
-
__name(
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2725
|
+
__name(generateMcpToolInstructions, "generateMcpToolInstructions");
|
|
2726
|
+
|
|
2727
|
+
// src/lib/runtime/copilot-runtime.ts
|
|
2728
|
+
var CopilotRuntime = class {
|
|
2729
|
+
actions;
|
|
2730
|
+
agents;
|
|
2731
|
+
remoteEndpointDefinitions;
|
|
2732
|
+
langserve = [];
|
|
2733
|
+
onBeforeRequest;
|
|
2734
|
+
onAfterRequest;
|
|
2735
|
+
delegateAgentProcessingToServiceAdapter;
|
|
2736
|
+
observability;
|
|
2737
|
+
availableAgents;
|
|
2738
|
+
onTrace;
|
|
2739
|
+
hasWarnedAboutTracing = false;
|
|
2740
|
+
// +++ MCP Properties +++
|
|
2741
|
+
mcpServersConfig;
|
|
2742
|
+
mcpActionCache = /* @__PURE__ */ new Map();
|
|
2743
|
+
// --- MCP Properties ---
|
|
2744
|
+
// +++ MCP Client Factory +++
|
|
2745
|
+
createMCPClientImpl;
|
|
2746
|
+
// --- MCP Client Factory ---
|
|
2747
|
+
constructor(params) {
|
|
2748
|
+
var _a, _b, _c, _d;
|
|
2749
|
+
if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints) && (params == null ? void 0 : params.remoteEndpoints.some((e) => e.type === EndpointType.LangGraphPlatform))) {
|
|
2750
|
+
console.warn("Actions set in runtime instance will not be available for the agent");
|
|
2751
|
+
console.warn(`LangGraph Platform remote endpoints are deprecated in favor of the "agents" property`);
|
|
2752
|
+
}
|
|
2753
|
+
this.actions = (params == null ? void 0 : params.actions) || [];
|
|
2754
|
+
this.availableAgents = [];
|
|
2755
|
+
for (const chain of (params == null ? void 0 : params.langserve) || []) {
|
|
2756
|
+
const remoteChain = new RemoteChain(chain);
|
|
2757
|
+
this.langserve.push(remoteChain.toAction());
|
|
2758
|
+
}
|
|
2759
|
+
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) ?? (params == null ? void 0 : params.remoteActions) ?? [];
|
|
2760
|
+
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
2761
|
+
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
2762
|
+
this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
|
|
2763
|
+
this.observability = params == null ? void 0 : params.observability_c;
|
|
2764
|
+
this.agents = (params == null ? void 0 : params.agents) ?? {};
|
|
2765
|
+
this.onTrace = params == null ? void 0 : params.onTrace;
|
|
2766
|
+
this.mcpServersConfig = params == null ? void 0 : params.mcpServers;
|
|
2767
|
+
this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
|
|
2768
|
+
if (this.mcpServersConfig && this.mcpServersConfig.length > 0 && !this.createMCPClientImpl) {
|
|
2769
|
+
throw new CopilotKitMisuseError2({
|
|
2770
|
+
message: "MCP Integration Error: `mcpServers` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`."
|
|
2660
2771
|
});
|
|
2661
|
-
|
|
2772
|
+
}
|
|
2773
|
+
if ((params == null ? void 0 : params.actions) && (((_c = params == null ? void 0 : params.remoteEndpoints) == null ? void 0 : _c.some((e) => e.type === EndpointType.LangGraphPlatform)) || ((_d = this.mcpServersConfig) == null ? void 0 : _d.length))) {
|
|
2774
|
+
console.warn("Local 'actions' defined in CopilotRuntime might not be available to remote agents (LangGraph, MCP). Consider defining actions closer to the agent implementation if needed.");
|
|
2775
|
+
}
|
|
2776
|
+
}
|
|
2777
|
+
// +++ MCP Instruction Injection Method +++
|
|
2778
|
+
injectMCPToolInstructions(messages, currentActions) {
|
|
2779
|
+
const mcpActionsForRequest = currentActions.filter((action) => action._isMCPTool);
|
|
2780
|
+
if (!mcpActionsForRequest || mcpActionsForRequest.length === 0) {
|
|
2781
|
+
return messages;
|
|
2782
|
+
}
|
|
2783
|
+
const uniqueMcpTools = /* @__PURE__ */ new Map();
|
|
2784
|
+
mcpActionsForRequest.forEach((action) => {
|
|
2785
|
+
uniqueMcpTools.set(action.name, action);
|
|
2786
|
+
});
|
|
2787
|
+
const toolsMap = {};
|
|
2788
|
+
Array.from(uniqueMcpTools.values()).forEach((action) => {
|
|
2789
|
+
toolsMap[action.name] = {
|
|
2790
|
+
description: action.description || "",
|
|
2791
|
+
schema: action.parameters ? {
|
|
2792
|
+
parameters: {
|
|
2793
|
+
properties: action.parameters.reduce((acc, p) => ({
|
|
2794
|
+
...acc,
|
|
2795
|
+
[p.name]: {
|
|
2796
|
+
type: p.type,
|
|
2797
|
+
description: p.description
|
|
2798
|
+
}
|
|
2799
|
+
}), {}),
|
|
2800
|
+
required: action.parameters.filter((p) => p.required).map((p) => p.name)
|
|
2801
|
+
}
|
|
2802
|
+
} : {},
|
|
2803
|
+
execute: async () => ({})
|
|
2804
|
+
};
|
|
2805
|
+
});
|
|
2806
|
+
const mcpToolInstructions = generateMcpToolInstructions(toolsMap);
|
|
2807
|
+
if (!mcpToolInstructions) {
|
|
2808
|
+
return messages;
|
|
2809
|
+
}
|
|
2810
|
+
const instructions = "You have access to the following tools provided by external Model Context Protocol (MCP) servers:\n" + mcpToolInstructions + "\nUse them when appropriate to fulfill the user's request.";
|
|
2811
|
+
const systemMessageIndex = messages.findIndex((msg) => {
|
|
2812
|
+
var _a;
|
|
2813
|
+
return ((_a = msg.textMessage) == null ? void 0 : _a.role) === "system";
|
|
2814
|
+
});
|
|
2815
|
+
const newMessages = [
|
|
2816
|
+
...messages
|
|
2817
|
+
];
|
|
2818
|
+
if (systemMessageIndex !== -1) {
|
|
2819
|
+
const existingMsg = newMessages[systemMessageIndex];
|
|
2820
|
+
if (existingMsg.textMessage) {
|
|
2821
|
+
existingMsg.textMessage.content = (existingMsg.textMessage.content ? existingMsg.textMessage.content + "\n\n" : "") + instructions;
|
|
2822
|
+
}
|
|
2823
|
+
} else {
|
|
2824
|
+
newMessages.unshift({
|
|
2825
|
+
id: randomId(),
|
|
2826
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
2827
|
+
textMessage: {
|
|
2828
|
+
role: MessageRole.system,
|
|
2829
|
+
content: instructions
|
|
2830
|
+
},
|
|
2831
|
+
actionExecutionMessage: void 0,
|
|
2832
|
+
resultMessage: void 0,
|
|
2833
|
+
agentStateMessage: void 0
|
|
2834
|
+
});
|
|
2835
|
+
}
|
|
2836
|
+
return newMessages;
|
|
2837
|
+
}
|
|
2838
|
+
async processRuntimeRequest(request) {
|
|
2839
|
+
var _a, _b, _c, _d, _e;
|
|
2840
|
+
const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, url, extensions, agentSession, agentStates, publicApiKey } = request;
|
|
2841
|
+
const eventSource = new RuntimeEventSource();
|
|
2842
|
+
const requestStartTime = Date.now();
|
|
2843
|
+
const streamedChunks = [];
|
|
2844
|
+
await this.trace("request", {
|
|
2845
|
+
threadId,
|
|
2846
|
+
runId,
|
|
2847
|
+
source: "runtime",
|
|
2848
|
+
request: {
|
|
2849
|
+
operation: "processRuntimeRequest",
|
|
2850
|
+
method: "POST",
|
|
2851
|
+
url,
|
|
2852
|
+
startTime: requestStartTime
|
|
2853
|
+
},
|
|
2854
|
+
agent: agentSession ? {
|
|
2855
|
+
name: agentSession.agentName
|
|
2856
|
+
} : void 0,
|
|
2857
|
+
messages: {
|
|
2858
|
+
input: rawMessages,
|
|
2859
|
+
messageCount: rawMessages.length
|
|
2860
|
+
},
|
|
2861
|
+
technical: {
|
|
2862
|
+
environment: process.env.NODE_ENV
|
|
2863
|
+
}
|
|
2864
|
+
}, void 0, publicApiKey);
|
|
2865
|
+
try {
|
|
2866
|
+
if (Object.keys(this.agents).length && (agentSession == null ? void 0 : agentSession.agentName) && !this.delegateAgentProcessingToServiceAdapter) {
|
|
2867
|
+
this.agents = {
|
|
2868
|
+
[agentSession.agentName]: this.agents[agentSession.agentName]
|
|
2869
|
+
};
|
|
2870
|
+
}
|
|
2871
|
+
if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
|
|
2872
|
+
return await this.processAgentRequest(request);
|
|
2873
|
+
}
|
|
2874
|
+
if (serviceAdapter instanceof EmptyAdapter) {
|
|
2875
|
+
throw new CopilotKitMisuseError2({
|
|
2876
|
+
message: `Invalid adapter configuration: EmptyAdapter is only meant to be used with agent lock mode.
|
|
2877
|
+
For non-agent components like useCopilotChatSuggestions, CopilotTextarea, or CopilotTask,
|
|
2878
|
+
please use an LLM adapter instead.`
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
const serverSideActions = await this.getServerSideActions(request);
|
|
2882
|
+
const filteredRawMessages = rawMessages.filter((message) => !message.agentStateMessage);
|
|
2883
|
+
const messagesWithInjectedInstructions = this.injectMCPToolInstructions(filteredRawMessages, serverSideActions);
|
|
2884
|
+
const inputMessages = convertGqlInputToMessages(messagesWithInjectedInstructions);
|
|
2885
|
+
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
2886
|
+
try {
|
|
2887
|
+
const requestData = {
|
|
2888
|
+
threadId,
|
|
2889
|
+
runId,
|
|
2890
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
2891
|
+
messages: inputMessages,
|
|
2892
|
+
actions: clientSideActionsInput,
|
|
2893
|
+
forwardedParameters,
|
|
2894
|
+
timestamp: requestStartTime,
|
|
2895
|
+
provider: this.detectProvider(serviceAdapter)
|
|
2896
|
+
};
|
|
2897
|
+
await this.observability.hooks.handleRequest(requestData);
|
|
2898
|
+
} catch (error) {
|
|
2899
|
+
console.error("Error logging LLM request:", error);
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
const serverSideActionsInput = serverSideActions.map((action) => ({
|
|
2903
|
+
name: action.name,
|
|
2904
|
+
description: action.description,
|
|
2905
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
2906
|
+
}));
|
|
2907
|
+
const actionInputs = flattenToolCallsNoDuplicates([
|
|
2908
|
+
...serverSideActionsInput,
|
|
2909
|
+
...clientSideActionsInput.filter(
|
|
2910
|
+
// Filter remote actions from CopilotKit core loop
|
|
2911
|
+
(action) => action.available !== ActionInputAvailability.remote
|
|
2912
|
+
)
|
|
2913
|
+
]);
|
|
2914
|
+
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
2915
|
+
threadId,
|
|
2916
|
+
runId,
|
|
2917
|
+
inputMessages,
|
|
2918
|
+
properties: graphqlContext.properties,
|
|
2919
|
+
url
|
|
2920
|
+
}));
|
|
2921
|
+
const result = await serviceAdapter.process({
|
|
2922
|
+
messages: inputMessages,
|
|
2923
|
+
actions: actionInputs,
|
|
2924
|
+
threadId,
|
|
2925
|
+
runId,
|
|
2926
|
+
eventSource,
|
|
2927
|
+
forwardedParameters,
|
|
2928
|
+
extensions,
|
|
2929
|
+
agentSession,
|
|
2930
|
+
agentStates
|
|
2931
|
+
});
|
|
2932
|
+
const nonEmptyThreadId = threadId ?? result.threadId;
|
|
2933
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
2934
|
+
var _a2;
|
|
2935
|
+
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
2936
|
+
threadId: nonEmptyThreadId,
|
|
2937
|
+
runId: result.runId,
|
|
2938
|
+
inputMessages,
|
|
2939
|
+
outputMessages,
|
|
2940
|
+
properties: graphqlContext.properties,
|
|
2941
|
+
url
|
|
2942
|
+
});
|
|
2943
|
+
}).catch((_error) => {
|
|
2944
|
+
});
|
|
2945
|
+
if (((_c = this.observability) == null ? void 0 : _c.enabled) && publicApiKey) {
|
|
2946
|
+
try {
|
|
2947
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
2948
|
+
const responseData = {
|
|
2949
|
+
threadId: result.threadId,
|
|
2950
|
+
runId: result.runId,
|
|
2951
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
2952
|
+
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
2953
|
+
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
2954
|
+
latency: Date.now() - requestStartTime,
|
|
2955
|
+
timestamp: Date.now(),
|
|
2956
|
+
provider: this.detectProvider(serviceAdapter),
|
|
2957
|
+
// Indicate this is the final response
|
|
2958
|
+
isFinalResponse: true
|
|
2959
|
+
};
|
|
2960
|
+
try {
|
|
2961
|
+
this.observability.hooks.handleResponse(responseData);
|
|
2962
|
+
} catch (logError) {
|
|
2963
|
+
console.error("Error logging LLM response:", logError);
|
|
2964
|
+
}
|
|
2965
|
+
}).catch((error) => {
|
|
2966
|
+
console.error("Failed to get output messages for logging:", error);
|
|
2967
|
+
});
|
|
2968
|
+
} catch (error) {
|
|
2969
|
+
console.error("Error setting up logging for LLM response:", error);
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
if (((_d = this.observability) == null ? void 0 : _d.enabled) && this.observability.progressive && publicApiKey) {
|
|
2973
|
+
const originalStream = eventSource.stream.bind(eventSource);
|
|
2974
|
+
eventSource.stream = async (callback) => {
|
|
2975
|
+
await originalStream(async (eventStream$) => {
|
|
2976
|
+
eventStream$.subscribe({
|
|
2977
|
+
next: (event) => {
|
|
2978
|
+
if (event.type === RuntimeEventTypes.TextMessageContent) {
|
|
2979
|
+
streamedChunks.push(event.content);
|
|
2980
|
+
try {
|
|
2981
|
+
const progressiveData = {
|
|
2982
|
+
threadId: threadId || "",
|
|
2983
|
+
runId,
|
|
2984
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
2985
|
+
output: event.content,
|
|
2986
|
+
latency: Date.now() - requestStartTime,
|
|
2987
|
+
timestamp: Date.now(),
|
|
2988
|
+
provider: this.detectProvider(serviceAdapter),
|
|
2989
|
+
isProgressiveChunk: true
|
|
2990
|
+
};
|
|
2991
|
+
Promise.resolve().then(() => {
|
|
2992
|
+
this.observability.hooks.handleResponse(progressiveData);
|
|
2993
|
+
}).catch((error) => {
|
|
2994
|
+
console.error("Error in progressive logging:", error);
|
|
2995
|
+
});
|
|
2996
|
+
} catch (error) {
|
|
2997
|
+
console.error("Error preparing progressive log data:", error);
|
|
2998
|
+
}
|
|
2999
|
+
}
|
|
3000
|
+
}
|
|
3001
|
+
});
|
|
3002
|
+
await callback(eventStream$);
|
|
3003
|
+
});
|
|
3004
|
+
};
|
|
3005
|
+
}
|
|
3006
|
+
return {
|
|
3007
|
+
threadId: nonEmptyThreadId,
|
|
3008
|
+
runId: result.runId,
|
|
3009
|
+
eventSource,
|
|
3010
|
+
serverSideActions,
|
|
3011
|
+
actionInputsWithoutAgents: actionInputs.filter((action) => (
|
|
3012
|
+
// TODO-AGENTS: do not exclude ALL server side actions
|
|
3013
|
+
!serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
|
|
3014
|
+
)),
|
|
3015
|
+
extensions: result.extensions
|
|
3016
|
+
};
|
|
3017
|
+
} catch (error) {
|
|
3018
|
+
if (((_e = this.observability) == null ? void 0 : _e.enabled) && publicApiKey) {
|
|
3019
|
+
try {
|
|
3020
|
+
const errorData = {
|
|
3021
|
+
threadId,
|
|
3022
|
+
runId,
|
|
3023
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3024
|
+
error: error instanceof Error ? error : String(error),
|
|
3025
|
+
timestamp: Date.now(),
|
|
3026
|
+
latency: Date.now() - requestStartTime,
|
|
3027
|
+
provider: this.detectProvider(serviceAdapter)
|
|
3028
|
+
};
|
|
3029
|
+
await this.observability.hooks.handleError(errorData);
|
|
3030
|
+
} catch (logError) {
|
|
3031
|
+
console.error("Error logging LLM error:", logError);
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
let structuredError;
|
|
3035
|
+
if (error instanceof CopilotKitError3) {
|
|
3036
|
+
structuredError = error;
|
|
3037
|
+
} else {
|
|
3038
|
+
structuredError = ensureStructuredError2(error, (err) => this.convertStreamingErrorToStructured(err));
|
|
3039
|
+
}
|
|
3040
|
+
await this.trace("error", {
|
|
3041
|
+
threadId,
|
|
3042
|
+
runId,
|
|
3043
|
+
source: "runtime",
|
|
3044
|
+
request: {
|
|
3045
|
+
operation: "processRuntimeRequest",
|
|
3046
|
+
method: "POST",
|
|
3047
|
+
url,
|
|
3048
|
+
startTime: requestStartTime
|
|
3049
|
+
},
|
|
3050
|
+
response: {
|
|
3051
|
+
endTime: Date.now(),
|
|
3052
|
+
latency: Date.now() - requestStartTime
|
|
3053
|
+
},
|
|
3054
|
+
agent: agentSession ? {
|
|
3055
|
+
name: agentSession.agentName
|
|
3056
|
+
} : void 0,
|
|
3057
|
+
technical: {
|
|
3058
|
+
environment: process.env.NODE_ENV,
|
|
3059
|
+
stackTrace: error instanceof Error ? error.stack : void 0
|
|
3060
|
+
}
|
|
3061
|
+
}, structuredError, publicApiKey);
|
|
3062
|
+
throw structuredError;
|
|
3063
|
+
}
|
|
3064
|
+
}
|
|
3065
|
+
async getAllAgents(graphqlContext) {
|
|
3066
|
+
const agentsWithEndpoints = await this.discoverAgentsFromEndpoints(graphqlContext);
|
|
3067
|
+
const aguiAgents = this.discoverAgentsFromAgui();
|
|
3068
|
+
this.availableAgents = [
|
|
3069
|
+
...agentsWithEndpoints,
|
|
3070
|
+
...aguiAgents
|
|
3071
|
+
].map((a) => ({
|
|
3072
|
+
name: a.name,
|
|
3073
|
+
id: a.id
|
|
3074
|
+
}));
|
|
3075
|
+
return [
|
|
3076
|
+
...agentsWithEndpoints,
|
|
3077
|
+
...aguiAgents
|
|
3078
|
+
];
|
|
3079
|
+
}
|
|
3080
|
+
async discoverAgentsFromEndpoints(graphqlContext) {
|
|
3081
|
+
const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
|
|
3082
|
+
const agents2 = await acc;
|
|
3083
|
+
if (endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3084
|
+
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3085
|
+
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3086
|
+
} : null;
|
|
3087
|
+
const client = new LangGraphClient2({
|
|
3088
|
+
apiUrl: endpoint.deploymentUrl,
|
|
3089
|
+
apiKey: endpoint.langsmithApiKey,
|
|
3090
|
+
defaultHeaders: {
|
|
3091
|
+
...propertyHeaders
|
|
3092
|
+
}
|
|
3093
|
+
});
|
|
3094
|
+
let data = [];
|
|
3095
|
+
try {
|
|
3096
|
+
data = await client.assistants.search();
|
|
3097
|
+
if (data && "detail" in data && data.detail.toLowerCase() === "not found") {
|
|
3098
|
+
throw new CopilotKitAgentDiscoveryError({
|
|
3099
|
+
availableAgents: this.availableAgents
|
|
3100
|
+
});
|
|
3101
|
+
}
|
|
3102
|
+
} catch (e) {
|
|
3103
|
+
throw new CopilotKitMisuseError2({
|
|
3104
|
+
message: `
|
|
3105
|
+
Failed to find or contact remote endpoint at url ${endpoint.deploymentUrl}.
|
|
3106
|
+
Make sure the API is running and that it's indeed a LangGraph platform url.
|
|
3107
|
+
|
|
3108
|
+
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
3109
|
+
});
|
|
3110
|
+
}
|
|
3111
|
+
const endpointAgents = data.map((entry) => ({
|
|
3112
|
+
name: entry.graph_id,
|
|
3113
|
+
id: entry.assistant_id,
|
|
3114
|
+
description: "",
|
|
3115
|
+
endpoint
|
|
3116
|
+
}));
|
|
3117
|
+
return [
|
|
3118
|
+
...agents2,
|
|
3119
|
+
...endpointAgents
|
|
3120
|
+
];
|
|
3121
|
+
}
|
|
3122
|
+
const cpkEndpoint = endpoint;
|
|
3123
|
+
const fetchUrl = `${endpoint.url}/info`;
|
|
2662
3124
|
try {
|
|
2663
3125
|
const response = await fetchWithRetry(fetchUrl, {
|
|
2664
3126
|
method: "POST",
|
|
2665
|
-
headers,
|
|
3127
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
2666
3128
|
body: JSON.stringify({
|
|
2667
|
-
name: action.name,
|
|
2668
|
-
arguments: args,
|
|
2669
3129
|
properties: graphqlContext.properties
|
|
2670
3130
|
})
|
|
2671
|
-
}
|
|
3131
|
+
});
|
|
2672
3132
|
if (!response.ok) {
|
|
2673
|
-
logger2.error({
|
|
2674
|
-
url,
|
|
2675
|
-
status: response.status,
|
|
2676
|
-
body: await response.text()
|
|
2677
|
-
}, "Failed to execute remote action");
|
|
2678
3133
|
if (response.status === 404) {
|
|
2679
3134
|
throw new CopilotKitApiDiscoveryError({
|
|
2680
3135
|
url: fetchUrl
|
|
@@ -2686,1403 +3141,1391 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
2686
3141
|
isRemoteEndpoint: true
|
|
2687
3142
|
});
|
|
2688
3143
|
}
|
|
2689
|
-
const
|
|
2690
|
-
const
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
3144
|
+
const data = await response.json();
|
|
3145
|
+
const endpointAgents = ((data == null ? void 0 : data.agents) ?? []).map((agent) => ({
|
|
3146
|
+
name: agent.name,
|
|
3147
|
+
description: agent.description ?? "" ?? "",
|
|
3148
|
+
id: randomId(),
|
|
3149
|
+
endpoint
|
|
3150
|
+
}));
|
|
3151
|
+
return [
|
|
3152
|
+
...agents2,
|
|
3153
|
+
...endpointAgents
|
|
3154
|
+
];
|
|
2696
3155
|
} catch (error) {
|
|
2697
3156
|
if (error instanceof CopilotKitError3) {
|
|
2698
3157
|
throw error;
|
|
2699
3158
|
}
|
|
2700
|
-
throw new
|
|
3159
|
+
throw new CopilotKitLowLevelError3({
|
|
2701
3160
|
error,
|
|
2702
3161
|
url: fetchUrl
|
|
2703
3162
|
});
|
|
2704
3163
|
}
|
|
3164
|
+
}, Promise.resolve([]));
|
|
3165
|
+
return agents;
|
|
3166
|
+
}
|
|
3167
|
+
discoverAgentsFromAgui() {
|
|
3168
|
+
return Object.values(this.agents ?? []).map((agent) => ({
|
|
3169
|
+
name: agent.agentName,
|
|
3170
|
+
id: agent.agentId,
|
|
3171
|
+
description: ""
|
|
3172
|
+
}));
|
|
3173
|
+
}
|
|
3174
|
+
async loadAgentState(graphqlContext, threadId, agentName) {
|
|
3175
|
+
const agents = await this.getAllAgents(graphqlContext);
|
|
3176
|
+
const agent = agents.find((agent2) => agent2.name === agentName);
|
|
3177
|
+
if (!agent) {
|
|
3178
|
+
throw new Error("Agent not found");
|
|
2705
3179
|
}
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
description: agent.description,
|
|
2710
|
-
parameters: [],
|
|
2711
|
-
handler: async (_args) => {
|
|
2712
|
-
},
|
|
2713
|
-
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
2714
|
-
logger2.debug({
|
|
2715
|
-
actionName: agent.name
|
|
2716
|
-
}, "Executing remote agent");
|
|
2717
|
-
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2718
|
-
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
2719
|
-
agentExecution: true,
|
|
2720
|
-
type: "self-hosted",
|
|
2721
|
-
agentsAmount: json["agents"].length
|
|
2722
|
-
});
|
|
2723
|
-
let state = {};
|
|
2724
|
-
let config = {};
|
|
2725
|
-
if (agentStates) {
|
|
2726
|
-
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
2727
|
-
if (jsonState) {
|
|
2728
|
-
state = parseJson2(jsonState.state, {});
|
|
2729
|
-
config = parseJson2(jsonState.config, {});
|
|
2730
|
-
}
|
|
2731
|
-
}
|
|
2732
|
-
const fetchUrl = `${url}/agents/execute`;
|
|
3180
|
+
if ("endpoint" in agent && (agent.endpoint.type === EndpointType.CopilotKit || !("type" in agent.endpoint))) {
|
|
3181
|
+
const cpkEndpoint = agent.endpoint;
|
|
3182
|
+
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
2733
3183
|
try {
|
|
2734
3184
|
const response = await fetchWithRetry(fetchUrl, {
|
|
2735
3185
|
method: "POST",
|
|
2736
|
-
headers,
|
|
3186
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
2737
3187
|
body: JSON.stringify({
|
|
2738
|
-
name,
|
|
2739
|
-
threadId,
|
|
2740
|
-
nodeName,
|
|
2741
|
-
messages: [
|
|
2742
|
-
...messages,
|
|
2743
|
-
...additionalMessages
|
|
2744
|
-
],
|
|
2745
|
-
state,
|
|
2746
|
-
config,
|
|
2747
3188
|
properties: graphqlContext.properties,
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
description: action.description,
|
|
2751
|
-
parameters: JSON.parse(action.jsonSchema)
|
|
2752
|
-
})),
|
|
2753
|
-
metaEvents
|
|
3189
|
+
threadId,
|
|
3190
|
+
name: agentName
|
|
2754
3191
|
})
|
|
2755
|
-
}
|
|
3192
|
+
});
|
|
2756
3193
|
if (!response.ok) {
|
|
2757
|
-
logger2.error({
|
|
2758
|
-
url,
|
|
2759
|
-
status: response.status,
|
|
2760
|
-
body: await response.text()
|
|
2761
|
-
}, "Failed to execute remote agent");
|
|
2762
3194
|
if (response.status === 404) {
|
|
2763
3195
|
throw new CopilotKitApiDiscoveryError({
|
|
2764
3196
|
url: fetchUrl
|
|
2765
3197
|
});
|
|
2766
3198
|
}
|
|
3199
|
+
let errorMessage = `HTTP ${response.status} error`;
|
|
3200
|
+
try {
|
|
3201
|
+
const errorBody = await response.text();
|
|
3202
|
+
const parsedError = JSON.parse(errorBody);
|
|
3203
|
+
if (parsedError.error && typeof parsedError.error === "string") {
|
|
3204
|
+
errorMessage = parsedError.error;
|
|
3205
|
+
}
|
|
3206
|
+
} catch {
|
|
3207
|
+
}
|
|
2767
3208
|
throw new ResolvedCopilotKitError({
|
|
2768
3209
|
status: response.status,
|
|
2769
3210
|
url: fetchUrl,
|
|
2770
|
-
isRemoteEndpoint: true
|
|
3211
|
+
isRemoteEndpoint: true,
|
|
3212
|
+
message: errorMessage
|
|
2771
3213
|
});
|
|
2772
3214
|
}
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
2780
|
-
return eventStream$;
|
|
2781
|
-
} else {
|
|
2782
|
-
throw new Error("Unsupported agent type");
|
|
2783
|
-
}
|
|
3215
|
+
const data = await response.json();
|
|
3216
|
+
return {
|
|
3217
|
+
...data,
|
|
3218
|
+
state: JSON.stringify(data.state),
|
|
3219
|
+
messages: JSON.stringify(data.messages)
|
|
3220
|
+
};
|
|
2784
3221
|
} catch (error) {
|
|
2785
3222
|
if (error instanceof CopilotKitError3) {
|
|
2786
3223
|
throw error;
|
|
2787
3224
|
}
|
|
2788
|
-
throw new
|
|
3225
|
+
throw new CopilotKitLowLevelError3({
|
|
2789
3226
|
error,
|
|
2790
3227
|
url: fetchUrl
|
|
2791
3228
|
});
|
|
2792
3229
|
}
|
|
2793
3230
|
}
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
};
|
|
2805
|
-
if (onBeforeRequest) {
|
|
2806
|
-
const { headers: additionalHeaders } = onBeforeRequest({
|
|
2807
|
-
ctx: graphqlContext
|
|
2808
|
-
});
|
|
2809
|
-
if (additionalHeaders) {
|
|
2810
|
-
Object.assign(headers, additionalHeaders);
|
|
2811
|
-
}
|
|
2812
|
-
}
|
|
2813
|
-
return headers;
|
|
2814
|
-
}
|
|
2815
|
-
__name(createHeaders, "createHeaders");
|
|
2816
|
-
|
|
2817
|
-
// src/lib/runtime/remote-actions.ts
|
|
2818
|
-
import { CopilotKitLowLevelError as CopilotKitLowLevelError3, ResolvedCopilotKitError as ResolvedCopilotKitError2, CopilotKitError as CopilotKitError4 } from "@copilotkit/shared";
|
|
2819
|
-
|
|
2820
|
-
// src/lib/runtime/agui-action.ts
|
|
2821
|
-
import { parseJson as parseJson3 } from "@copilotkit/shared";
|
|
2822
|
-
function constructAGUIRemoteAction({ logger: logger2, messages, agentStates, agent, metaEvents }) {
|
|
2823
|
-
const action = {
|
|
2824
|
-
name: agent.agentId,
|
|
2825
|
-
description: agent.description,
|
|
2826
|
-
parameters: [],
|
|
2827
|
-
handler: async (_args) => {
|
|
2828
|
-
},
|
|
2829
|
-
remoteAgentHandler: async ({ actionInputsWithoutAgents, threadId }) => {
|
|
2830
|
-
var _a;
|
|
2831
|
-
logger2.debug({
|
|
2832
|
-
actionName: agent.agentId
|
|
2833
|
-
}, "Executing remote agent");
|
|
2834
|
-
const agentWireMessages = convertMessagesToAGUIMessage(messages);
|
|
2835
|
-
agent.messages = agentWireMessages;
|
|
2836
|
-
agent.threadId = threadId;
|
|
2837
|
-
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
2838
|
-
agentExecution: true,
|
|
2839
|
-
type: "self-hosted",
|
|
2840
|
-
agentsAmount: 1
|
|
2841
|
-
});
|
|
2842
|
-
let state = {};
|
|
2843
|
-
if (agentStates) {
|
|
2844
|
-
const jsonState = agentStates.find((state2) => state2.agentName === agent.agentId);
|
|
2845
|
-
if (jsonState) {
|
|
2846
|
-
state = parseJson3(jsonState.state, {});
|
|
2847
|
-
}
|
|
2848
|
-
}
|
|
2849
|
-
agent.state = state;
|
|
2850
|
-
const tools = actionInputsWithoutAgents.map((input) => {
|
|
2851
|
-
return {
|
|
2852
|
-
name: input.name,
|
|
2853
|
-
description: input.description,
|
|
2854
|
-
parameters: JSON.parse(input.jsonSchema)
|
|
2855
|
-
};
|
|
2856
|
-
});
|
|
2857
|
-
const forwardedProps = metaEvents.length ? {
|
|
2858
|
-
command: {
|
|
2859
|
-
resume: (_a = metaEvents[0]) == null ? void 0 : _a.response
|
|
2860
|
-
}
|
|
2861
|
-
} : void 0;
|
|
2862
|
-
return agent.legacy_to_be_removed_runAgentBridged({
|
|
2863
|
-
tools,
|
|
2864
|
-
forwardedProps
|
|
2865
|
-
});
|
|
2866
|
-
}
|
|
2867
|
-
};
|
|
2868
|
-
return [
|
|
2869
|
-
action
|
|
2870
|
-
];
|
|
2871
|
-
}
|
|
2872
|
-
__name(constructAGUIRemoteAction, "constructAGUIRemoteAction");
|
|
2873
|
-
function convertMessagesToAGUIMessage(messages) {
|
|
2874
|
-
const result = [];
|
|
2875
|
-
for (const message of messages) {
|
|
2876
|
-
if (message.isTextMessage()) {
|
|
2877
|
-
result.push({
|
|
2878
|
-
id: message.id,
|
|
2879
|
-
role: message.role,
|
|
2880
|
-
content: message.content
|
|
2881
|
-
});
|
|
2882
|
-
} else if (message.isActionExecutionMessage()) {
|
|
2883
|
-
const toolCall = {
|
|
2884
|
-
id: message.id,
|
|
2885
|
-
type: "function",
|
|
2886
|
-
function: {
|
|
2887
|
-
name: message.name,
|
|
2888
|
-
arguments: JSON.stringify(message.arguments)
|
|
2889
|
-
}
|
|
2890
|
-
};
|
|
2891
|
-
if (message.parentMessageId && result.some((m) => m.id === message.parentMessageId)) {
|
|
2892
|
-
const parentMessage = result.find((m) => m.id === message.parentMessageId);
|
|
2893
|
-
if (parentMessage.toolCalls === void 0) {
|
|
2894
|
-
parentMessage.toolCalls = [];
|
|
3231
|
+
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3232
|
+
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3233
|
+
} : null;
|
|
3234
|
+
let client;
|
|
3235
|
+
if ("endpoint" in agent && agent.endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3236
|
+
client = new LangGraphClient2({
|
|
3237
|
+
apiUrl: agent.endpoint.deploymentUrl,
|
|
3238
|
+
apiKey: agent.endpoint.langsmithApiKey,
|
|
3239
|
+
defaultHeaders: {
|
|
3240
|
+
...propertyHeaders
|
|
2895
3241
|
}
|
|
2896
|
-
parentMessage.toolCalls.push(toolCall);
|
|
2897
|
-
} else {
|
|
2898
|
-
result.push({
|
|
2899
|
-
id: message.parentMessageId ?? message.id,
|
|
2900
|
-
role: "assistant",
|
|
2901
|
-
toolCalls: [
|
|
2902
|
-
toolCall
|
|
2903
|
-
]
|
|
2904
|
-
});
|
|
2905
|
-
}
|
|
2906
|
-
} else if (message.isResultMessage()) {
|
|
2907
|
-
result.push({
|
|
2908
|
-
id: message.id,
|
|
2909
|
-
role: "tool",
|
|
2910
|
-
content: message.result,
|
|
2911
|
-
toolCallId: message.actionExecutionId
|
|
2912
3242
|
});
|
|
3243
|
+
} else {
|
|
3244
|
+
const aguiAgent = graphqlContext._copilotkit.runtime.agents[agent.name];
|
|
3245
|
+
if (!aguiAgent) {
|
|
3246
|
+
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
3247
|
+
}
|
|
3248
|
+
client = aguiAgent.client;
|
|
2913
3249
|
}
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
})(EndpointType || (EndpointType = {}));
|
|
2925
|
-
function isRemoteAgentAction(action) {
|
|
2926
|
-
if (!action) {
|
|
2927
|
-
return false;
|
|
2928
|
-
}
|
|
2929
|
-
return typeof action.remoteAgentHandler === "function";
|
|
2930
|
-
}
|
|
2931
|
-
__name(isRemoteAgentAction, "isRemoteAgentAction");
|
|
2932
|
-
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
2933
|
-
logger2.debug({
|
|
2934
|
-
url
|
|
2935
|
-
}, "Fetching actions from url");
|
|
2936
|
-
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
2937
|
-
const fetchUrl = `${url}/info`;
|
|
2938
|
-
try {
|
|
2939
|
-
const response = await fetch(fetchUrl, {
|
|
2940
|
-
method: "POST",
|
|
2941
|
-
headers,
|
|
2942
|
-
body: JSON.stringify({
|
|
2943
|
-
properties: graphqlContext.properties,
|
|
2944
|
-
frontendUrl
|
|
2945
|
-
})
|
|
2946
|
-
});
|
|
2947
|
-
if (!response.ok) {
|
|
2948
|
-
logger2.error({
|
|
2949
|
-
url,
|
|
2950
|
-
status: response.status,
|
|
2951
|
-
body: await response.text()
|
|
2952
|
-
}, "Failed to fetch actions from url");
|
|
2953
|
-
throw new ResolvedCopilotKitError2({
|
|
2954
|
-
status: response.status,
|
|
2955
|
-
url: fetchUrl,
|
|
2956
|
-
isRemoteEndpoint: true
|
|
3250
|
+
let state = {};
|
|
3251
|
+
try {
|
|
3252
|
+
state = (await client.threads.getState(threadId)).values;
|
|
3253
|
+
} catch (error) {
|
|
3254
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
3255
|
+
console.debug(`Agent '${agentName}' configuration issue: ${errorMessage}`);
|
|
3256
|
+
throw new ResolvedCopilotKitError({
|
|
3257
|
+
status: 400,
|
|
3258
|
+
message: `Agent '${agentName}' failed to execute: ${errorMessage}`,
|
|
3259
|
+
code: CopilotKitErrorCode2.CONFIGURATION_ERROR
|
|
2957
3260
|
});
|
|
2958
3261
|
}
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
3262
|
+
if (Object.keys(state).length === 0) {
|
|
3263
|
+
return {
|
|
3264
|
+
threadId: threadId || "",
|
|
3265
|
+
threadExists: false,
|
|
3266
|
+
state: JSON.stringify({}),
|
|
3267
|
+
messages: JSON.stringify([])
|
|
3268
|
+
};
|
|
3269
|
+
} else {
|
|
3270
|
+
const { messages, ...stateWithoutMessages } = state;
|
|
3271
|
+
const copilotkitMessages = langchainMessagesToCopilotKit(messages);
|
|
3272
|
+
return {
|
|
3273
|
+
threadId: threadId || "",
|
|
3274
|
+
threadExists: true,
|
|
3275
|
+
state: JSON.stringify(stateWithoutMessages),
|
|
3276
|
+
messages: JSON.stringify(copilotkitMessages)
|
|
3277
|
+
};
|
|
2967
3278
|
}
|
|
2968
|
-
throw new
|
|
2969
|
-
error,
|
|
2970
|
-
url: fetchUrl
|
|
2971
|
-
});
|
|
3279
|
+
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
2972
3280
|
}
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
3281
|
+
async processAgentRequest(request) {
|
|
3282
|
+
var _a, _b, _c, _d, _e;
|
|
3283
|
+
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession, threadId: threadIdFromRequest, metaEvents, publicApiKey, forwardedParameters } = request;
|
|
3284
|
+
const { agentName, nodeName } = agentSession;
|
|
3285
|
+
const requestStartTime = Date.now();
|
|
3286
|
+
const streamedChunks = [];
|
|
3287
|
+
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
3288
|
+
await this.trace("agent_state", {
|
|
3289
|
+
threadId,
|
|
3290
|
+
source: "agent",
|
|
3291
|
+
request: {
|
|
3292
|
+
operation: "processAgentRequest",
|
|
3293
|
+
method: "POST",
|
|
3294
|
+
startTime: requestStartTime
|
|
3295
|
+
},
|
|
3296
|
+
agent: {
|
|
3297
|
+
name: agentName,
|
|
3298
|
+
nodeName
|
|
3299
|
+
},
|
|
3300
|
+
messages: {
|
|
3301
|
+
input: rawMessages,
|
|
3302
|
+
messageCount: rawMessages.length
|
|
3303
|
+
},
|
|
3304
|
+
technical: {
|
|
3305
|
+
environment: process.env.NODE_ENV
|
|
3306
|
+
}
|
|
3307
|
+
}, void 0, publicApiKey);
|
|
3308
|
+
const serverSideActions = await this.getServerSideActions(request);
|
|
3309
|
+
const messages = convertGqlInputToMessages(rawMessages);
|
|
3310
|
+
const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
|
|
3311
|
+
if (!currentAgent) {
|
|
3312
|
+
throw new CopilotKitAgentDiscoveryError({
|
|
3313
|
+
agentName,
|
|
3314
|
+
availableAgents: this.availableAgents
|
|
2999
3315
|
});
|
|
3000
3316
|
}
|
|
3001
|
-
const
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
})
|
|
3030
|
-
|
|
3031
|
-
|
|
3317
|
+
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
3318
|
+
// Case 1: Keep all regular (non-agent) actions
|
|
3319
|
+
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
3320
|
+
isRemoteAgentAction(action) && action.name !== agentName
|
|
3321
|
+
)).map((action) => ({
|
|
3322
|
+
name: action.name,
|
|
3323
|
+
description: action.description,
|
|
3324
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3325
|
+
}));
|
|
3326
|
+
const allAvailableActions = flattenToolCallsNoDuplicates([
|
|
3327
|
+
...availableActionsForCurrentAgent,
|
|
3328
|
+
...request.actions
|
|
3329
|
+
]);
|
|
3330
|
+
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
3331
|
+
try {
|
|
3332
|
+
const requestData = {
|
|
3333
|
+
threadId,
|
|
3334
|
+
runId: void 0,
|
|
3335
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3336
|
+
messages,
|
|
3337
|
+
actions: allAvailableActions,
|
|
3338
|
+
forwardedParameters,
|
|
3339
|
+
timestamp: requestStartTime,
|
|
3340
|
+
provider: "agent",
|
|
3341
|
+
agentName,
|
|
3342
|
+
nodeName
|
|
3343
|
+
};
|
|
3344
|
+
await this.observability.hooks.handleRequest(requestData);
|
|
3345
|
+
} catch (error) {
|
|
3346
|
+
console.error("Error logging agent request:", error);
|
|
3347
|
+
}
|
|
3032
3348
|
}
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
metaEvents
|
|
3349
|
+
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
3350
|
+
threadId,
|
|
3351
|
+
runId: void 0,
|
|
3352
|
+
inputMessages: messages,
|
|
3353
|
+
properties: graphqlContext.properties
|
|
3039
3354
|
}));
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
// src/lib/runtime/copilot-runtime.ts
|
|
3049
|
-
import { actionParametersToJsonSchema, ResolvedCopilotKitError as ResolvedCopilotKitError3, CopilotKitApiDiscoveryError as CopilotKitApiDiscoveryError2, randomId as randomId2, CopilotKitError as CopilotKitError5, CopilotKitAgentDiscoveryError, CopilotKitMisuseError as CopilotKitMisuseError2, CopilotKitErrorCode as CopilotKitErrorCode3, CopilotKitLowLevelError as CopilotKitLowLevelError4 } from "@copilotkit/shared";
|
|
3050
|
-
|
|
3051
|
-
// src/service-adapters/conversion.ts
|
|
3052
|
-
import { plainToInstance } from "class-transformer";
|
|
3053
|
-
import { tryMap as tryMap2 } from "@copilotkit/shared";
|
|
3054
|
-
function convertGqlInputToMessages(inputMessages) {
|
|
3055
|
-
const messages = tryMap2(inputMessages, (message) => {
|
|
3056
|
-
if (message.textMessage) {
|
|
3057
|
-
return plainToInstance(TextMessage, {
|
|
3058
|
-
id: message.id,
|
|
3059
|
-
createdAt: message.createdAt,
|
|
3060
|
-
role: message.textMessage.role,
|
|
3061
|
-
content: message.textMessage.content,
|
|
3062
|
-
parentMessageId: message.textMessage.parentMessageId
|
|
3063
|
-
});
|
|
3064
|
-
} else if (message.imageMessage) {
|
|
3065
|
-
return plainToInstance(ImageMessage, {
|
|
3066
|
-
id: message.id,
|
|
3067
|
-
createdAt: message.createdAt,
|
|
3068
|
-
role: message.imageMessage.role,
|
|
3069
|
-
bytes: message.imageMessage.bytes,
|
|
3070
|
-
format: message.imageMessage.format,
|
|
3071
|
-
parentMessageId: message.imageMessage.parentMessageId
|
|
3072
|
-
});
|
|
3073
|
-
} else if (message.actionExecutionMessage) {
|
|
3074
|
-
return plainToInstance(ActionExecutionMessage, {
|
|
3075
|
-
id: message.id,
|
|
3076
|
-
createdAt: message.createdAt,
|
|
3077
|
-
name: message.actionExecutionMessage.name,
|
|
3078
|
-
arguments: JSON.parse(message.actionExecutionMessage.arguments),
|
|
3079
|
-
parentMessageId: message.actionExecutionMessage.parentMessageId
|
|
3080
|
-
});
|
|
3081
|
-
} else if (message.resultMessage) {
|
|
3082
|
-
return plainToInstance(ResultMessage, {
|
|
3083
|
-
id: message.id,
|
|
3084
|
-
createdAt: message.createdAt,
|
|
3085
|
-
actionExecutionId: message.resultMessage.actionExecutionId,
|
|
3086
|
-
actionName: message.resultMessage.actionName,
|
|
3087
|
-
result: message.resultMessage.result
|
|
3355
|
+
try {
|
|
3356
|
+
const eventSource = new RuntimeEventSource();
|
|
3357
|
+
const stream = await currentAgent.remoteAgentHandler({
|
|
3358
|
+
name: agentName,
|
|
3359
|
+
threadId,
|
|
3360
|
+
nodeName,
|
|
3361
|
+
metaEvents,
|
|
3362
|
+
actionInputsWithoutAgents: allAvailableActions
|
|
3088
3363
|
});
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3364
|
+
if (((_c = this.observability) == null ? void 0 : _c.enabled) && this.observability.progressive && publicApiKey) {
|
|
3365
|
+
const originalStream = eventSource.stream.bind(eventSource);
|
|
3366
|
+
eventSource.stream = async (callback) => {
|
|
3367
|
+
await originalStream(async (eventStream$) => {
|
|
3368
|
+
eventStream$.subscribe({
|
|
3369
|
+
next: (event) => {
|
|
3370
|
+
if (event.type === RuntimeEventTypes.TextMessageContent) {
|
|
3371
|
+
streamedChunks.push(event.content);
|
|
3372
|
+
try {
|
|
3373
|
+
const progressiveData = {
|
|
3374
|
+
threadId: threadId || "",
|
|
3375
|
+
runId: void 0,
|
|
3376
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3377
|
+
output: event.content,
|
|
3378
|
+
latency: Date.now() - requestStartTime,
|
|
3379
|
+
timestamp: Date.now(),
|
|
3380
|
+
provider: "agent",
|
|
3381
|
+
isProgressiveChunk: true,
|
|
3382
|
+
agentName,
|
|
3383
|
+
nodeName
|
|
3384
|
+
};
|
|
3385
|
+
Promise.resolve().then(() => {
|
|
3386
|
+
this.observability.hooks.handleResponse(progressiveData);
|
|
3387
|
+
}).catch((error) => {
|
|
3388
|
+
console.error("Error in progressive agent logging:", error);
|
|
3389
|
+
});
|
|
3390
|
+
} catch (error) {
|
|
3391
|
+
console.error("Error preparing progressive agent log data:", error);
|
|
3392
|
+
}
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
});
|
|
3396
|
+
await callback(eventStream$);
|
|
3397
|
+
});
|
|
3398
|
+
};
|
|
3399
|
+
}
|
|
3400
|
+
eventSource.stream(async (eventStream$) => {
|
|
3401
|
+
from(stream).subscribe({
|
|
3402
|
+
next: (event) => eventStream$.next(event),
|
|
3403
|
+
error: async (err) => {
|
|
3404
|
+
var _a2;
|
|
3405
|
+
if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
|
|
3406
|
+
try {
|
|
3407
|
+
const errorData = {
|
|
3408
|
+
threadId,
|
|
3409
|
+
runId: void 0,
|
|
3410
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3411
|
+
error: err instanceof Error ? err : String(err),
|
|
3412
|
+
timestamp: Date.now(),
|
|
3413
|
+
latency: Date.now() - requestStartTime,
|
|
3414
|
+
provider: "agent",
|
|
3415
|
+
agentName,
|
|
3416
|
+
nodeName
|
|
3417
|
+
};
|
|
3418
|
+
this.observability.hooks.handleError(errorData);
|
|
3419
|
+
} catch (logError) {
|
|
3420
|
+
console.error("Error logging agent error:", logError);
|
|
3421
|
+
}
|
|
3422
|
+
}
|
|
3423
|
+
const structuredError = ensureStructuredError2(err, (error) => this.convertStreamingErrorToStructured(error));
|
|
3424
|
+
await this.trace("error", {
|
|
3425
|
+
threadId,
|
|
3426
|
+
source: "agent",
|
|
3427
|
+
request: {
|
|
3428
|
+
operation: "processAgentRequest",
|
|
3429
|
+
method: "POST",
|
|
3430
|
+
startTime: requestStartTime
|
|
3431
|
+
},
|
|
3432
|
+
response: {
|
|
3433
|
+
endTime: Date.now(),
|
|
3434
|
+
latency: Date.now() - requestStartTime
|
|
3435
|
+
},
|
|
3436
|
+
agent: {
|
|
3437
|
+
name: agentName,
|
|
3438
|
+
nodeName
|
|
3439
|
+
},
|
|
3440
|
+
technical: {
|
|
3441
|
+
environment: process.env.NODE_ENV,
|
|
3442
|
+
stackTrace: err instanceof Error ? err.stack : void 0
|
|
3443
|
+
}
|
|
3444
|
+
}, structuredError, publicApiKey);
|
|
3445
|
+
eventStream$.error(structuredError);
|
|
3446
|
+
eventStream$.complete();
|
|
3447
|
+
},
|
|
3448
|
+
complete: () => eventStream$.complete()
|
|
3449
|
+
});
|
|
3101
3450
|
});
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3451
|
+
if (((_d = this.observability) == null ? void 0 : _d.enabled) && publicApiKey) {
|
|
3452
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
3453
|
+
const responseData = {
|
|
3454
|
+
threadId,
|
|
3455
|
+
runId: void 0,
|
|
3456
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3457
|
+
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
3458
|
+
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
3459
|
+
latency: Date.now() - requestStartTime,
|
|
3460
|
+
timestamp: Date.now(),
|
|
3461
|
+
provider: "agent",
|
|
3462
|
+
isFinalResponse: true,
|
|
3463
|
+
agentName,
|
|
3464
|
+
nodeName
|
|
3465
|
+
};
|
|
3466
|
+
try {
|
|
3467
|
+
this.observability.hooks.handleResponse(responseData);
|
|
3468
|
+
} catch (logError) {
|
|
3469
|
+
console.error("Error logging agent response:", logError);
|
|
3470
|
+
}
|
|
3471
|
+
}).catch((error) => {
|
|
3472
|
+
console.error("Failed to get output messages for agent logging:", error);
|
|
3473
|
+
});
|
|
3474
|
+
}
|
|
3475
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
3476
|
+
var _a2;
|
|
3477
|
+
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
3478
|
+
threadId,
|
|
3479
|
+
runId: void 0,
|
|
3480
|
+
inputMessages: messages,
|
|
3481
|
+
outputMessages,
|
|
3482
|
+
properties: graphqlContext.properties
|
|
3483
|
+
});
|
|
3484
|
+
}).catch((_error) => {
|
|
3135
3485
|
});
|
|
3486
|
+
return {
|
|
3487
|
+
threadId,
|
|
3488
|
+
runId: void 0,
|
|
3489
|
+
eventSource,
|
|
3490
|
+
serverSideActions,
|
|
3491
|
+
actionInputsWithoutAgents: allAvailableActions
|
|
3492
|
+
};
|
|
3493
|
+
} catch (error) {
|
|
3494
|
+
if (((_e = this.observability) == null ? void 0 : _e.enabled) && publicApiKey) {
|
|
3495
|
+
try {
|
|
3496
|
+
const errorData = {
|
|
3497
|
+
threadId,
|
|
3498
|
+
runId: void 0,
|
|
3499
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3500
|
+
error: error instanceof Error ? error : String(error),
|
|
3501
|
+
timestamp: Date.now(),
|
|
3502
|
+
latency: Date.now() - requestStartTime,
|
|
3503
|
+
provider: "agent",
|
|
3504
|
+
agentName,
|
|
3505
|
+
nodeName
|
|
3506
|
+
};
|
|
3507
|
+
await this.observability.hooks.handleError(errorData);
|
|
3508
|
+
} catch (logError) {
|
|
3509
|
+
console.error("Error logging agent error:", logError);
|
|
3510
|
+
}
|
|
3511
|
+
}
|
|
3512
|
+
const structuredError = ensureStructuredError2(error, (err) => this.convertStreamingErrorToStructured(err));
|
|
3513
|
+
await this.trace("error", {
|
|
3514
|
+
threadId,
|
|
3515
|
+
source: "agent",
|
|
3516
|
+
request: {
|
|
3517
|
+
operation: "processAgentRequest",
|
|
3518
|
+
method: "POST",
|
|
3519
|
+
startTime: requestStartTime
|
|
3520
|
+
},
|
|
3521
|
+
response: {
|
|
3522
|
+
endTime: Date.now(),
|
|
3523
|
+
latency: Date.now() - requestStartTime
|
|
3524
|
+
},
|
|
3525
|
+
agent: {
|
|
3526
|
+
name: agentName,
|
|
3527
|
+
nodeName
|
|
3528
|
+
},
|
|
3529
|
+
technical: {
|
|
3530
|
+
environment: process.env.NODE_ENV,
|
|
3531
|
+
stackTrace: error instanceof Error ? error.stack : void 0
|
|
3532
|
+
}
|
|
3533
|
+
}, structuredError, publicApiKey);
|
|
3534
|
+
console.error("Error getting response:", error);
|
|
3535
|
+
throw structuredError;
|
|
3136
3536
|
}
|
|
3137
3537
|
}
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
const parameters = extractParametersFromSchema(tool);
|
|
3145
|
-
const handler = /* @__PURE__ */ __name(async (params) => {
|
|
3538
|
+
async getServerSideActions(request) {
|
|
3539
|
+
var _a, _b;
|
|
3540
|
+
const { graphqlContext, messages: rawMessages, agentStates, url } = request;
|
|
3541
|
+
const inputMessages = convertGqlInputToMessages(rawMessages);
|
|
3542
|
+
const langserveFunctions = [];
|
|
3543
|
+
for (const chainPromise of this.langserve) {
|
|
3146
3544
|
try {
|
|
3147
|
-
const
|
|
3148
|
-
|
|
3149
|
-
});
|
|
3150
|
-
return typeof result === "string" ? result : JSON.stringify(result);
|
|
3545
|
+
const chain = await chainPromise;
|
|
3546
|
+
langserveFunctions.push(chain);
|
|
3151
3547
|
} catch (error) {
|
|
3152
|
-
console.error(
|
|
3153
|
-
throw new Error(`Execution failed for MCP tool '${toolName}': ${error instanceof Error ? error.message : String(error)}`);
|
|
3548
|
+
console.error("Error loading langserve chain:", error);
|
|
3154
3549
|
}
|
|
3155
|
-
}
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3550
|
+
}
|
|
3551
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
3552
|
+
...endpoint,
|
|
3553
|
+
type: resolveEndpointType(endpoint)
|
|
3554
|
+
}));
|
|
3555
|
+
const remoteActions = await setupRemoteActions({
|
|
3556
|
+
remoteEndpointDefinitions,
|
|
3557
|
+
graphqlContext,
|
|
3558
|
+
messages: inputMessages,
|
|
3559
|
+
agentStates,
|
|
3560
|
+
frontendUrl: url,
|
|
3561
|
+
agents: this.agents,
|
|
3562
|
+
metaEvents: request.metaEvents
|
|
3164
3563
|
});
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3564
|
+
const configuredActions = typeof this.actions === "function" ? this.actions({
|
|
3565
|
+
properties: graphqlContext.properties,
|
|
3566
|
+
url
|
|
3567
|
+
}) : this.actions;
|
|
3568
|
+
const requestSpecificMCPActions = [];
|
|
3569
|
+
if (this.createMCPClientImpl) {
|
|
3570
|
+
const baseEndpoints = this.mcpServersConfig || [];
|
|
3571
|
+
const requestEndpoints = ((_a = graphqlContext.properties) == null ? void 0 : _a.mcpServers) || ((_b = graphqlContext.properties) == null ? void 0 : _b.mcpEndpoints) || [];
|
|
3572
|
+
const effectiveEndpointsMap = /* @__PURE__ */ new Map();
|
|
3573
|
+
[
|
|
3574
|
+
...baseEndpoints
|
|
3575
|
+
].forEach((ep) => {
|
|
3576
|
+
if (ep && ep.endpoint) {
|
|
3577
|
+
effectiveEndpointsMap.set(ep.endpoint, ep);
|
|
3578
|
+
}
|
|
3579
|
+
});
|
|
3580
|
+
[
|
|
3581
|
+
...requestEndpoints
|
|
3582
|
+
].forEach((ep) => {
|
|
3583
|
+
if (ep && ep.endpoint) {
|
|
3584
|
+
effectiveEndpointsMap.set(ep.endpoint, ep);
|
|
3585
|
+
}
|
|
3586
|
+
});
|
|
3587
|
+
const effectiveEndpoints = Array.from(effectiveEndpointsMap.values());
|
|
3588
|
+
for (const config of effectiveEndpoints) {
|
|
3589
|
+
const endpointUrl = config.endpoint;
|
|
3590
|
+
let actionsForEndpoint = this.mcpActionCache.get(endpointUrl);
|
|
3591
|
+
if (!actionsForEndpoint) {
|
|
3592
|
+
let client = null;
|
|
3593
|
+
try {
|
|
3594
|
+
client = await this.createMCPClientImpl(config);
|
|
3595
|
+
const tools = await client.tools();
|
|
3596
|
+
actionsForEndpoint = convertMCPToolsToActions(tools, endpointUrl);
|
|
3597
|
+
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3598
|
+
} catch (error) {
|
|
3599
|
+
console.error(`MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`, error);
|
|
3600
|
+
actionsForEndpoint = [];
|
|
3601
|
+
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3190
3602
|
}
|
|
3191
3603
|
}
|
|
3604
|
+
requestSpecificMCPActions.push(...actionsForEndpoint || []);
|
|
3192
3605
|
}
|
|
3193
|
-
} catch (e) {
|
|
3194
|
-
console.error(`Error parsing schema for tool ${name}:`, e);
|
|
3195
3606
|
}
|
|
3196
|
-
return
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
createMCPClientImpl;
|
|
3228
|
-
// --- MCP Client Factory ---
|
|
3229
|
-
constructor(params) {
|
|
3230
|
-
var _a, _b, _c, _d;
|
|
3231
|
-
if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints) && (params == null ? void 0 : params.remoteEndpoints.some((e) => e.type === EndpointType.LangGraphPlatform))) {
|
|
3232
|
-
console.warn("Actions set in runtime instance will not be available for the agent");
|
|
3607
|
+
return [
|
|
3608
|
+
...configuredActions,
|
|
3609
|
+
...langserveFunctions,
|
|
3610
|
+
...remoteActions,
|
|
3611
|
+
...requestSpecificMCPActions
|
|
3612
|
+
];
|
|
3613
|
+
}
|
|
3614
|
+
// Add helper method to detect provider
|
|
3615
|
+
detectProvider(serviceAdapter) {
|
|
3616
|
+
const adapterName = serviceAdapter.constructor.name;
|
|
3617
|
+
if (adapterName.includes("OpenAI"))
|
|
3618
|
+
return "openai";
|
|
3619
|
+
if (adapterName.includes("Anthropic"))
|
|
3620
|
+
return "anthropic";
|
|
3621
|
+
if (adapterName.includes("Google"))
|
|
3622
|
+
return "google";
|
|
3623
|
+
if (adapterName.includes("Groq"))
|
|
3624
|
+
return "groq";
|
|
3625
|
+
if (adapterName.includes("LangChain"))
|
|
3626
|
+
return "langchain";
|
|
3627
|
+
return void 0;
|
|
3628
|
+
}
|
|
3629
|
+
convertStreamingErrorToStructured(error) {
|
|
3630
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
3631
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "agent streaming connection");
|
|
3632
|
+
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("fetch failed")) || ((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.includes("ECONNREFUSED")) || ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("ENOTFOUND")) || ((_d = error == null ? void 0 : error.message) == null ? void 0 : _d.includes("ETIMEDOUT")) || ((_e = error == null ? void 0 : error.message) == null ? void 0 : _e.includes("terminated")) || ((_f = error == null ? void 0 : error.cause) == null ? void 0 : _f.code) === "UND_ERR_SOCKET" || ((_g = error == null ? void 0 : error.message) == null ? void 0 : _g.includes("other side closed")) || (error == null ? void 0 : error.code) === "UND_ERR_SOCKET") {
|
|
3633
|
+
return new CopilotKitLowLevelError3({
|
|
3634
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
3635
|
+
url: "agent streaming connection",
|
|
3636
|
+
message: helpfulMessage
|
|
3637
|
+
});
|
|
3233
3638
|
}
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3639
|
+
return new CopilotKitError3({
|
|
3640
|
+
message: helpfulMessage,
|
|
3641
|
+
code: CopilotKitErrorCode2.UNKNOWN
|
|
3642
|
+
});
|
|
3643
|
+
}
|
|
3644
|
+
async trace(type, context, error, publicApiKey) {
|
|
3645
|
+
if (!this.onTrace)
|
|
3646
|
+
return;
|
|
3647
|
+
if (!publicApiKey) {
|
|
3648
|
+
if (!this.hasWarnedAboutTracing) {
|
|
3649
|
+
console.warn("CopilotKit: onTrace handler provided but requires publicApiKey to be defined for tracing to work.");
|
|
3650
|
+
this.hasWarnedAboutTracing = true;
|
|
3651
|
+
}
|
|
3652
|
+
return;
|
|
3239
3653
|
}
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3654
|
+
try {
|
|
3655
|
+
const traceEvent = {
|
|
3656
|
+
type,
|
|
3657
|
+
timestamp: Date.now(),
|
|
3658
|
+
context,
|
|
3659
|
+
...error && {
|
|
3660
|
+
error
|
|
3661
|
+
}
|
|
3662
|
+
};
|
|
3663
|
+
await this.onTrace(traceEvent);
|
|
3664
|
+
} catch (traceError) {
|
|
3665
|
+
console.error("Error in onTrace handler:", traceError);
|
|
3666
|
+
}
|
|
3667
|
+
}
|
|
3668
|
+
/**
|
|
3669
|
+
* Public method to trace GraphQL validation errors
|
|
3670
|
+
* This allows the GraphQL resolver to send validation errors through the trace system
|
|
3671
|
+
*/
|
|
3672
|
+
async traceGraphQLError(error, context) {
|
|
3673
|
+
if (!this.onTrace)
|
|
3674
|
+
return;
|
|
3675
|
+
try {
|
|
3676
|
+
await this.onTrace({
|
|
3677
|
+
type: "error",
|
|
3678
|
+
timestamp: Date.now(),
|
|
3679
|
+
context: {
|
|
3680
|
+
source: "runtime",
|
|
3681
|
+
request: {
|
|
3682
|
+
operation: context.operation,
|
|
3683
|
+
startTime: Date.now()
|
|
3684
|
+
},
|
|
3685
|
+
technical: {
|
|
3686
|
+
environment: process.env.NODE_ENV
|
|
3687
|
+
},
|
|
3688
|
+
metadata: {
|
|
3689
|
+
errorType: "GraphQLValidationError",
|
|
3690
|
+
cloudConfigPresent: context.cloudConfigPresent,
|
|
3691
|
+
guardrailsEnabled: context.guardrailsEnabled
|
|
3692
|
+
}
|
|
3693
|
+
},
|
|
3694
|
+
error
|
|
3251
3695
|
});
|
|
3696
|
+
} catch (traceError) {
|
|
3697
|
+
console.error("Error in onTrace handler:", traceError);
|
|
3252
3698
|
}
|
|
3253
|
-
|
|
3254
|
-
|
|
3699
|
+
}
|
|
3700
|
+
};
|
|
3701
|
+
__name(CopilotRuntime, "CopilotRuntime");
|
|
3702
|
+
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
3703
|
+
let allTools = [];
|
|
3704
|
+
const allToolNames = [];
|
|
3705
|
+
for (const tool of toolsByPriority) {
|
|
3706
|
+
if (!allToolNames.includes(tool.name)) {
|
|
3707
|
+
allTools.push(tool);
|
|
3708
|
+
allToolNames.push(tool.name);
|
|
3255
3709
|
}
|
|
3256
3710
|
}
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3711
|
+
return allTools;
|
|
3712
|
+
}
|
|
3713
|
+
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
3714
|
+
function copilotKitEndpoint(config) {
|
|
3715
|
+
return {
|
|
3716
|
+
...config,
|
|
3717
|
+
type: EndpointType.CopilotKit
|
|
3718
|
+
};
|
|
3719
|
+
}
|
|
3720
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
3721
|
+
function langGraphPlatformEndpoint(config) {
|
|
3722
|
+
return {
|
|
3723
|
+
...config,
|
|
3724
|
+
type: EndpointType.LangGraphPlatform
|
|
3725
|
+
};
|
|
3726
|
+
}
|
|
3727
|
+
__name(langGraphPlatformEndpoint, "langGraphPlatformEndpoint");
|
|
3728
|
+
function resolveEndpointType(endpoint) {
|
|
3729
|
+
if (!endpoint.type) {
|
|
3730
|
+
if ("deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
3731
|
+
return EndpointType.LangGraphPlatform;
|
|
3732
|
+
} else {
|
|
3733
|
+
return EndpointType.CopilotKit;
|
|
3262
3734
|
}
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3735
|
+
}
|
|
3736
|
+
return endpoint.type;
|
|
3737
|
+
}
|
|
3738
|
+
__name(resolveEndpointType, "resolveEndpointType");
|
|
3739
|
+
|
|
3740
|
+
// src/lib/telemetry-client.ts
|
|
3741
|
+
var packageJson = require_package();
|
|
3742
|
+
var telemetryClient = new TelemetryClient({
|
|
3743
|
+
packageName: packageJson.name,
|
|
3744
|
+
packageVersion: packageJson.version
|
|
3745
|
+
});
|
|
3746
|
+
function getRuntimeInstanceTelemetryInfo(options) {
|
|
3747
|
+
var _a, _b;
|
|
3748
|
+
const runtime = options.runtime;
|
|
3749
|
+
const endpointsInfo = runtime.remoteEndpointDefinitions.reduce((acc, endpoint) => {
|
|
3750
|
+
let info = {
|
|
3751
|
+
...acc
|
|
3752
|
+
};
|
|
3753
|
+
const endpointType = resolveEndpointType(endpoint);
|
|
3754
|
+
if (!info.endpointTypes.includes(endpointType)) {
|
|
3755
|
+
info = {
|
|
3756
|
+
...info,
|
|
3757
|
+
endpointTypes: [
|
|
3758
|
+
...info.endpointTypes,
|
|
3759
|
+
endpointType
|
|
3760
|
+
]
|
|
3284
3761
|
};
|
|
3285
|
-
});
|
|
3286
|
-
const mcpToolInstructions = generateMcpToolInstructions(toolsMap);
|
|
3287
|
-
if (!mcpToolInstructions) {
|
|
3288
|
-
return messages;
|
|
3289
3762
|
}
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3763
|
+
if (endpointType === EndpointType.LangGraphPlatform) {
|
|
3764
|
+
const ep = endpoint;
|
|
3765
|
+
info = {
|
|
3766
|
+
...info,
|
|
3767
|
+
agentsAmount: ep.agents.length,
|
|
3768
|
+
hashedKey: ep.langsmithApiKey ? createHash2("sha256").update(ep.langsmithApiKey).digest("hex") : null
|
|
3769
|
+
};
|
|
3770
|
+
}
|
|
3771
|
+
return info;
|
|
3772
|
+
}, {
|
|
3773
|
+
endpointTypes: [],
|
|
3774
|
+
agentsAmount: null,
|
|
3775
|
+
hashedKey: null
|
|
3776
|
+
});
|
|
3777
|
+
const publicApiKey = (_a = options.cloud) == null ? void 0 : _a.publicApiKey;
|
|
3778
|
+
const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;
|
|
3779
|
+
return {
|
|
3780
|
+
actionsAmount: runtime.actions.length,
|
|
3781
|
+
endpointsAmount: runtime.remoteEndpointDefinitions.length,
|
|
3782
|
+
endpointTypes: endpointsInfo.endpointTypes,
|
|
3783
|
+
agentsAmount: endpointsInfo.agentsAmount,
|
|
3784
|
+
hashedLgcKey: endpointsInfo.hashedKey,
|
|
3785
|
+
"cloud.api_key_provided": apiKeyProvided,
|
|
3786
|
+
...apiKeyProvided ? {
|
|
3787
|
+
"cloud.public_api_key": publicApiKey
|
|
3788
|
+
} : {},
|
|
3789
|
+
...((_b = options.cloud) == null ? void 0 : _b.baseUrl) ? {
|
|
3790
|
+
"cloud.base_url": options.cloud.baseUrl
|
|
3791
|
+
} : {}
|
|
3792
|
+
};
|
|
3793
|
+
}
|
|
3794
|
+
__name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
|
|
3795
|
+
var telemetry_client_default = telemetryClient;
|
|
3796
|
+
|
|
3797
|
+
// src/agents/langgraph/event-source.ts
|
|
3798
|
+
import { CopilotKitLowLevelError as CopilotKitLowLevelError4, isStructuredCopilotKitError } from "@copilotkit/shared";
|
|
3799
|
+
import { catchError, mergeMap, ReplaySubject, scan } from "rxjs";
|
|
3800
|
+
var RemoteLangGraphEventSource = class {
|
|
3801
|
+
eventStream$ = new ReplaySubject();
|
|
3802
|
+
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
3803
|
+
if (typeof shouldEmitToolCalls === "boolean") {
|
|
3804
|
+
return shouldEmitToolCalls;
|
|
3315
3805
|
}
|
|
3316
|
-
|
|
3806
|
+
if (Array.isArray(shouldEmitToolCalls)) {
|
|
3807
|
+
return shouldEmitToolCalls.includes(toolCallName);
|
|
3808
|
+
}
|
|
3809
|
+
return shouldEmitToolCalls === toolCallName;
|
|
3317
3810
|
}
|
|
3318
|
-
|
|
3811
|
+
getCurrentContent(event) {
|
|
3319
3812
|
var _a, _b, _c, _d, _e;
|
|
3320
|
-
const
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
[agentSession.agentName]: this.agents[agentSession.agentName]
|
|
3328
|
-
};
|
|
3329
|
-
}
|
|
3330
|
-
if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
|
|
3331
|
-
return await this.processAgentRequest(request);
|
|
3813
|
+
const content = ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.content) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.content);
|
|
3814
|
+
if (!content) {
|
|
3815
|
+
const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
|
|
3816
|
+
for (const chunk of toolCallChunks) {
|
|
3817
|
+
if (chunk.args) {
|
|
3818
|
+
return chunk.args;
|
|
3819
|
+
}
|
|
3332
3820
|
}
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3821
|
+
}
|
|
3822
|
+
if (typeof content === "string") {
|
|
3823
|
+
return content;
|
|
3824
|
+
} else if (Array.isArray(content) && content.length > 0) {
|
|
3825
|
+
return content[0].text;
|
|
3826
|
+
}
|
|
3827
|
+
return null;
|
|
3828
|
+
}
|
|
3829
|
+
getCurrentMessageId(event) {
|
|
3830
|
+
var _a, _b, _c, _d, _e;
|
|
3831
|
+
return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.id) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.id);
|
|
3832
|
+
}
|
|
3833
|
+
getCurrentToolCallChunks(event) {
|
|
3834
|
+
var _a, _b, _c, _d, _e;
|
|
3835
|
+
return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.tool_call_chunks) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.tool_call_chunks);
|
|
3836
|
+
}
|
|
3837
|
+
getResponseMetadata(event) {
|
|
3838
|
+
var _a, _b, _c, _d, _e;
|
|
3839
|
+
return ((_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.kwargs) == null ? void 0 : _c.response_metadata) ?? ((_e = (_d = event.data) == null ? void 0 : _d.chunk) == null ? void 0 : _e.response_metadata);
|
|
3840
|
+
}
|
|
3841
|
+
processLangGraphEvents() {
|
|
3842
|
+
let lastEventWithState = null;
|
|
3843
|
+
return this.eventStream$.pipe(scan((acc, event) => {
|
|
3844
|
+
if (event.event === LangGraphEventTypes.OnChatModelStream) {
|
|
3845
|
+
const prevMessageId = acc.lastMessageId;
|
|
3846
|
+
acc.currentContent = this.getCurrentContent(event);
|
|
3847
|
+
acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
|
|
3848
|
+
const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
|
|
3849
|
+
const responseMetadata = this.getResponseMetadata(event);
|
|
3850
|
+
const toolCallCheck = toolCallChunks && toolCallChunks.length > 0;
|
|
3851
|
+
let isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
|
|
3852
|
+
acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
|
|
3853
|
+
acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
|
|
3854
|
+
let previousRoundHadToolCall = acc.isToolCall;
|
|
3855
|
+
acc.isToolCall = toolCallCheck;
|
|
3856
|
+
if (previousRoundHadToolCall && !toolCallCheck) {
|
|
3857
|
+
isToolCallEnd = true;
|
|
3858
|
+
}
|
|
3859
|
+
acc.isToolCallEnd = isToolCallEnd;
|
|
3860
|
+
acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
|
|
3861
|
+
({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
|
|
3862
|
+
name: acc.lastToolCallName,
|
|
3863
|
+
id: acc.lastToolCallId
|
|
3338
3864
|
});
|
|
3339
3865
|
}
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3866
|
+
acc.event = event;
|
|
3867
|
+
lastEventWithState = acc;
|
|
3868
|
+
return acc;
|
|
3869
|
+
}, {
|
|
3870
|
+
event: null,
|
|
3871
|
+
isMessageStart: false,
|
|
3872
|
+
isMessageEnd: false,
|
|
3873
|
+
isToolCallStart: false,
|
|
3874
|
+
isToolCallEnd: false,
|
|
3875
|
+
isToolCall: false,
|
|
3876
|
+
lastMessageId: null,
|
|
3877
|
+
lastToolCallId: null,
|
|
3878
|
+
lastToolCallName: null,
|
|
3879
|
+
currentContent: null,
|
|
3880
|
+
processedToolCallIds: /* @__PURE__ */ new Set()
|
|
3881
|
+
}), mergeMap((acc) => {
|
|
3882
|
+
const events = [];
|
|
3883
|
+
let shouldEmitMessages = true;
|
|
3884
|
+
let shouldEmitToolCalls = true;
|
|
3885
|
+
if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
|
|
3886
|
+
if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
|
|
3887
|
+
shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
|
|
3359
3888
|
}
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
name: action.name,
|
|
3363
|
-
description: action.description,
|
|
3364
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3365
|
-
}));
|
|
3366
|
-
const actionInputs = flattenToolCallsNoDuplicates([
|
|
3367
|
-
...serverSideActionsInput,
|
|
3368
|
-
...clientSideActionsInput.filter(
|
|
3369
|
-
// Filter remote actions from CopilotKit core loop
|
|
3370
|
-
(action) => action.available !== ActionInputAvailability.remote
|
|
3371
|
-
)
|
|
3372
|
-
]);
|
|
3373
|
-
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
3374
|
-
threadId,
|
|
3375
|
-
runId,
|
|
3376
|
-
inputMessages,
|
|
3377
|
-
properties: graphqlContext.properties,
|
|
3378
|
-
url
|
|
3379
|
-
}));
|
|
3380
|
-
const result = await serviceAdapter.process({
|
|
3381
|
-
messages: inputMessages,
|
|
3382
|
-
actions: actionInputs,
|
|
3383
|
-
threadId,
|
|
3384
|
-
runId,
|
|
3385
|
-
eventSource,
|
|
3386
|
-
forwardedParameters,
|
|
3387
|
-
extensions,
|
|
3388
|
-
agentSession,
|
|
3389
|
-
agentStates
|
|
3390
|
-
});
|
|
3391
|
-
const nonEmptyThreadId = threadId ?? result.threadId;
|
|
3392
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3393
|
-
var _a2;
|
|
3394
|
-
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
3395
|
-
threadId: nonEmptyThreadId,
|
|
3396
|
-
runId: result.runId,
|
|
3397
|
-
inputMessages,
|
|
3398
|
-
outputMessages,
|
|
3399
|
-
properties: graphqlContext.properties,
|
|
3400
|
-
url
|
|
3401
|
-
});
|
|
3402
|
-
}).catch((_error) => {
|
|
3403
|
-
});
|
|
3404
|
-
if (((_c = this.observability) == null ? void 0 : _c.enabled) && publicApiKey) {
|
|
3405
|
-
try {
|
|
3406
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3407
|
-
const responseData = {
|
|
3408
|
-
threadId: result.threadId,
|
|
3409
|
-
runId: result.runId,
|
|
3410
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3411
|
-
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
3412
|
-
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
3413
|
-
latency: Date.now() - requestStartTime,
|
|
3414
|
-
timestamp: Date.now(),
|
|
3415
|
-
provider: this.detectProvider(serviceAdapter),
|
|
3416
|
-
// Indicate this is the final response
|
|
3417
|
-
isFinalResponse: true
|
|
3418
|
-
};
|
|
3419
|
-
try {
|
|
3420
|
-
this.observability.hooks.handleResponse(responseData);
|
|
3421
|
-
} catch (logError) {
|
|
3422
|
-
console.error("Error logging LLM response:", logError);
|
|
3423
|
-
}
|
|
3424
|
-
}).catch((error) => {
|
|
3425
|
-
console.error("Failed to get output messages for logging:", error);
|
|
3426
|
-
});
|
|
3427
|
-
} catch (error) {
|
|
3428
|
-
console.error("Error setting up logging for LLM response:", error);
|
|
3889
|
+
if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
|
|
3890
|
+
shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
|
|
3429
3891
|
}
|
|
3430
3892
|
}
|
|
3431
|
-
if (
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3436
|
-
|
|
3437
|
-
if (event.type === RuntimeEventTypes.TextMessageContent) {
|
|
3438
|
-
streamedChunks.push(event.content);
|
|
3439
|
-
try {
|
|
3440
|
-
const progressiveData = {
|
|
3441
|
-
threadId: threadId || "",
|
|
3442
|
-
runId,
|
|
3443
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3444
|
-
output: event.content,
|
|
3445
|
-
latency: Date.now() - requestStartTime,
|
|
3446
|
-
timestamp: Date.now(),
|
|
3447
|
-
provider: this.detectProvider(serviceAdapter),
|
|
3448
|
-
isProgressiveChunk: true
|
|
3449
|
-
};
|
|
3450
|
-
Promise.resolve().then(() => {
|
|
3451
|
-
this.observability.hooks.handleResponse(progressiveData);
|
|
3452
|
-
}).catch((error) => {
|
|
3453
|
-
console.error("Error in progressive logging:", error);
|
|
3454
|
-
});
|
|
3455
|
-
} catch (error) {
|
|
3456
|
-
console.error("Error preparing progressive log data:", error);
|
|
3457
|
-
}
|
|
3458
|
-
}
|
|
3459
|
-
}
|
|
3460
|
-
});
|
|
3461
|
-
await callback(eventStream$);
|
|
3462
|
-
});
|
|
3463
|
-
};
|
|
3893
|
+
if (acc.event.event === LangGraphEventTypes.OnInterrupt) {
|
|
3894
|
+
events.push({
|
|
3895
|
+
type: RuntimeEventTypes.MetaEvent,
|
|
3896
|
+
name: RuntimeMetaEventName.LangGraphInterruptEvent,
|
|
3897
|
+
value: acc.event.value
|
|
3898
|
+
});
|
|
3464
3899
|
}
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
// TODO-AGENTS: do not exclude ALL server side actions
|
|
3472
|
-
!serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
|
|
3473
|
-
)),
|
|
3474
|
-
extensions: result.extensions
|
|
3475
|
-
};
|
|
3476
|
-
} catch (error) {
|
|
3477
|
-
if (((_e = this.observability) == null ? void 0 : _e.enabled) && publicApiKey) {
|
|
3478
|
-
try {
|
|
3479
|
-
const errorData = {
|
|
3480
|
-
threadId,
|
|
3481
|
-
runId,
|
|
3482
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3483
|
-
error: error instanceof Error ? error : String(error),
|
|
3484
|
-
timestamp: Date.now(),
|
|
3485
|
-
latency: Date.now() - requestStartTime,
|
|
3486
|
-
provider: this.detectProvider(serviceAdapter)
|
|
3487
|
-
};
|
|
3488
|
-
await this.observability.hooks.handleError(errorData);
|
|
3489
|
-
} catch (logError) {
|
|
3490
|
-
console.error("Error logging LLM error:", logError);
|
|
3491
|
-
}
|
|
3900
|
+
if (acc.event.event === LangGraphEventTypes.OnCopilotKitInterrupt) {
|
|
3901
|
+
events.push({
|
|
3902
|
+
type: RuntimeEventTypes.MetaEvent,
|
|
3903
|
+
name: RuntimeMetaEventName.CopilotKitLangGraphInterruptEvent,
|
|
3904
|
+
data: acc.event.data
|
|
3905
|
+
});
|
|
3492
3906
|
}
|
|
3493
|
-
if (
|
|
3494
|
-
|
|
3907
|
+
if (acc.event.event === LangGraphEventTypes.OnCopilotKitError) {
|
|
3908
|
+
const errorData = acc.event.data.error;
|
|
3909
|
+
const preservedError = new CopilotKitLowLevelError4({
|
|
3910
|
+
error: new Error(errorData.message),
|
|
3911
|
+
url: "langgraph agent",
|
|
3912
|
+
message: `${errorData.type}: ${errorData.message}`
|
|
3913
|
+
});
|
|
3914
|
+
if (errorData.status_code) {
|
|
3915
|
+
preservedError.statusCode = errorData.status_code;
|
|
3916
|
+
}
|
|
3917
|
+
if (errorData.response_data) {
|
|
3918
|
+
preservedError.responseData = errorData.response_data;
|
|
3919
|
+
}
|
|
3920
|
+
preservedError.agentName = errorData.agent_name;
|
|
3921
|
+
preservedError.originalErrorType = errorData.type;
|
|
3922
|
+
throw preservedError;
|
|
3495
3923
|
}
|
|
3496
|
-
|
|
3497
|
-
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
3501
|
-
|
|
3502
|
-
const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
|
|
3503
|
-
const agents2 = await acc;
|
|
3504
|
-
if (endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3505
|
-
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3506
|
-
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3507
|
-
} : null;
|
|
3508
|
-
const client = new LangGraphClient2({
|
|
3509
|
-
apiUrl: endpoint.deploymentUrl,
|
|
3510
|
-
apiKey: endpoint.langsmithApiKey,
|
|
3511
|
-
defaultHeaders: {
|
|
3512
|
-
...propertyHeaders
|
|
3513
|
-
}
|
|
3924
|
+
const responseMetadata = this.getResponseMetadata(acc.event);
|
|
3925
|
+
if (acc.isToolCallEnd && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName) && acc.lastToolCallId && !acc.processedToolCallIds.has(acc.lastToolCallId)) {
|
|
3926
|
+
acc.processedToolCallIds.add(acc.lastToolCallId);
|
|
3927
|
+
events.push({
|
|
3928
|
+
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
3929
|
+
actionExecutionId: acc.lastToolCallId
|
|
3514
3930
|
});
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3931
|
+
} else if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
|
|
3932
|
+
events.push({
|
|
3933
|
+
type: RuntimeEventTypes.TextMessageEnd,
|
|
3934
|
+
messageId: acc.lastMessageId
|
|
3935
|
+
});
|
|
3936
|
+
}
|
|
3937
|
+
switch (acc.event.event) {
|
|
3938
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
3939
|
+
if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
3940
|
+
events.push({
|
|
3941
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
3942
|
+
messageId: acc.event.data.message_id
|
|
3943
|
+
});
|
|
3944
|
+
events.push({
|
|
3945
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
3946
|
+
messageId: acc.event.data.message_id,
|
|
3947
|
+
content: acc.event.data.message
|
|
3948
|
+
});
|
|
3949
|
+
events.push({
|
|
3950
|
+
type: RuntimeEventTypes.TextMessageEnd,
|
|
3951
|
+
messageId: acc.event.data.message_id
|
|
3952
|
+
});
|
|
3953
|
+
} else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
3954
|
+
events.push({
|
|
3955
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
3956
|
+
actionExecutionId: acc.event.data.id,
|
|
3957
|
+
actionName: acc.event.data.name,
|
|
3958
|
+
parentMessageId: acc.event.data.id
|
|
3959
|
+
});
|
|
3960
|
+
events.push({
|
|
3961
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
3962
|
+
actionExecutionId: acc.event.data.id,
|
|
3963
|
+
args: JSON.stringify(acc.event.data.args)
|
|
3964
|
+
});
|
|
3965
|
+
events.push({
|
|
3966
|
+
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
3967
|
+
actionExecutionId: acc.event.data.id
|
|
3521
3968
|
});
|
|
3522
3969
|
}
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3970
|
+
break;
|
|
3971
|
+
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
3972
|
+
events.push({
|
|
3973
|
+
type: RuntimeEventTypes.AgentStateMessage,
|
|
3974
|
+
threadId: acc.event.thread_id,
|
|
3975
|
+
role: acc.event.role,
|
|
3976
|
+
agentName: acc.event.agent_name,
|
|
3977
|
+
nodeName: acc.event.node_name,
|
|
3978
|
+
runId: acc.event.run_id,
|
|
3979
|
+
active: acc.event.active,
|
|
3980
|
+
state: JSON.stringify(acc.event.state),
|
|
3981
|
+
running: acc.event.running
|
|
3530
3982
|
});
|
|
3983
|
+
break;
|
|
3984
|
+
case LangGraphEventTypes.OnChatModelStream:
|
|
3985
|
+
if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
3986
|
+
events.push({
|
|
3987
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
3988
|
+
actionExecutionId: acc.lastToolCallId,
|
|
3989
|
+
actionName: acc.lastToolCallName,
|
|
3990
|
+
parentMessageId: acc.lastMessageId
|
|
3991
|
+
});
|
|
3992
|
+
} else if (acc.isMessageStart && shouldEmitMessages) {
|
|
3993
|
+
acc.processedToolCallIds.clear();
|
|
3994
|
+
events.push({
|
|
3995
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
3996
|
+
messageId: acc.lastMessageId
|
|
3997
|
+
});
|
|
3998
|
+
}
|
|
3999
|
+
if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
4000
|
+
events.push({
|
|
4001
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
4002
|
+
actionExecutionId: acc.lastToolCallId,
|
|
4003
|
+
args: acc.currentContent
|
|
4004
|
+
});
|
|
4005
|
+
} else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
|
|
4006
|
+
events.push({
|
|
4007
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
4008
|
+
messageId: acc.lastMessageId,
|
|
4009
|
+
content: acc.currentContent
|
|
4010
|
+
});
|
|
4011
|
+
}
|
|
4012
|
+
break;
|
|
4013
|
+
}
|
|
4014
|
+
return events;
|
|
4015
|
+
}), catchError((error) => {
|
|
4016
|
+
if (isStructuredCopilotKitError(error)) {
|
|
4017
|
+
throw error;
|
|
4018
|
+
}
|
|
4019
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "LangGraph agent connection");
|
|
4020
|
+
throw new CopilotKitLowLevelError4({
|
|
4021
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
4022
|
+
url: "langgraph event stream",
|
|
4023
|
+
message: helpfulMessage
|
|
4024
|
+
});
|
|
4025
|
+
}));
|
|
4026
|
+
}
|
|
4027
|
+
};
|
|
4028
|
+
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
4029
|
+
|
|
4030
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
4031
|
+
import { CopilotKitError as CopilotKitError4, CopilotKitLowLevelError as CopilotKitLowLevelError5 } from "@copilotkit/shared";
|
|
4032
|
+
import { CopilotKitApiDiscoveryError as CopilotKitApiDiscoveryError2, ResolvedCopilotKitError as ResolvedCopilotKitError2 } from "@copilotkit/shared";
|
|
4033
|
+
import { parseJson as parseJson2, tryMap as tryMap2 } from "@copilotkit/shared";
|
|
4034
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
4035
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
4036
|
+
name: agent.name,
|
|
4037
|
+
description: agent.description,
|
|
4038
|
+
parameters: [],
|
|
4039
|
+
handler: async (_args) => {
|
|
4040
|
+
},
|
|
4041
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
4042
|
+
logger2.debug({
|
|
4043
|
+
actionName: agent.name
|
|
4044
|
+
}, "Executing LangGraph Platform agent");
|
|
4045
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4046
|
+
agentExecution: true,
|
|
4047
|
+
type: "langgraph-platform",
|
|
4048
|
+
agentsAmount: endpoint.agents.length,
|
|
4049
|
+
hashedLgcKey: endpoint.langsmithApiKey ? createHash3("sha256").update(endpoint.langsmithApiKey).digest("hex") : null
|
|
4050
|
+
});
|
|
4051
|
+
let state = {};
|
|
4052
|
+
let config = {};
|
|
4053
|
+
if (agentStates) {
|
|
4054
|
+
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
4055
|
+
if (jsonState) {
|
|
4056
|
+
state = parseJson2(jsonState.state, {});
|
|
4057
|
+
config = parseJson2(jsonState.config, {});
|
|
3531
4058
|
}
|
|
3532
|
-
const endpointAgents = data.map((entry) => ({
|
|
3533
|
-
name: entry.graph_id,
|
|
3534
|
-
id: entry.assistant_id,
|
|
3535
|
-
description: "",
|
|
3536
|
-
endpoint
|
|
3537
|
-
}));
|
|
3538
|
-
return [
|
|
3539
|
-
...agents2,
|
|
3540
|
-
...endpointAgents
|
|
3541
|
-
];
|
|
3542
4059
|
}
|
|
3543
|
-
const cpkEndpoint = endpoint;
|
|
3544
|
-
const fetchUrl = `${endpoint.url}/info`;
|
|
3545
4060
|
try {
|
|
3546
|
-
const response = await
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
4061
|
+
const response = await execute({
|
|
4062
|
+
logger: logger2.child({
|
|
4063
|
+
component: "remote-actions.remote-lg-action.streamEvents"
|
|
4064
|
+
}),
|
|
4065
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
4066
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
4067
|
+
agent,
|
|
4068
|
+
threadId,
|
|
4069
|
+
nodeName,
|
|
4070
|
+
messages: [
|
|
4071
|
+
...messages,
|
|
4072
|
+
...additionalMessages
|
|
4073
|
+
],
|
|
4074
|
+
state,
|
|
4075
|
+
config,
|
|
4076
|
+
properties: graphqlContext.properties,
|
|
4077
|
+
actions: tryMap2(actionInputsWithoutAgents, (action) => ({
|
|
4078
|
+
name: action.name,
|
|
4079
|
+
description: action.description,
|
|
4080
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
4081
|
+
})),
|
|
4082
|
+
metaEvents
|
|
3552
4083
|
});
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
url: fetchUrl
|
|
3557
|
-
});
|
|
3558
|
-
}
|
|
3559
|
-
throw new ResolvedCopilotKitError3({
|
|
3560
|
-
status: response.status,
|
|
3561
|
-
url: fetchUrl,
|
|
3562
|
-
isRemoteEndpoint: true
|
|
3563
|
-
});
|
|
3564
|
-
}
|
|
3565
|
-
const data = await response.json();
|
|
3566
|
-
const endpointAgents = ((data == null ? void 0 : data.agents) ?? []).map((agent) => ({
|
|
3567
|
-
name: agent.name,
|
|
3568
|
-
description: agent.description ?? "" ?? "",
|
|
3569
|
-
id: randomId2(),
|
|
3570
|
-
endpoint
|
|
3571
|
-
}));
|
|
3572
|
-
return [
|
|
3573
|
-
...agents2,
|
|
3574
|
-
...endpointAgents
|
|
3575
|
-
];
|
|
4084
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
4085
|
+
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
4086
|
+
return eventSource.processLangGraphEvents();
|
|
3576
4087
|
} catch (error) {
|
|
3577
|
-
if (error instanceof
|
|
4088
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
4089
|
+
if (isUserConfigurationError(error)) {
|
|
4090
|
+
logger2.debug({
|
|
4091
|
+
url: endpoint.deploymentUrl,
|
|
4092
|
+
error: error.message,
|
|
4093
|
+
code: error.code
|
|
4094
|
+
}, "User configuration error in LangGraph Platform agent");
|
|
4095
|
+
} else {
|
|
4096
|
+
logger2.error({
|
|
4097
|
+
url: endpoint.deploymentUrl,
|
|
4098
|
+
error: error.message,
|
|
4099
|
+
type: error.constructor.name
|
|
4100
|
+
}, "LangGraph Platform agent error");
|
|
4101
|
+
}
|
|
3578
4102
|
throw error;
|
|
3579
4103
|
}
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
4104
|
+
logger2.error({
|
|
4105
|
+
url: endpoint.deploymentUrl,
|
|
4106
|
+
status: 500,
|
|
4107
|
+
body: error.message
|
|
4108
|
+
}, "Failed to execute LangGraph Platform agent");
|
|
4109
|
+
throw new CopilotKitLowLevelError5({
|
|
4110
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
4111
|
+
url: endpoint.deploymentUrl,
|
|
4112
|
+
message: "Failed to execute LangGraph Platform agent"
|
|
3583
4113
|
});
|
|
3584
4114
|
}
|
|
3585
|
-
}, Promise.resolve([]));
|
|
3586
|
-
this.availableAgents = (await agents ?? []).map((a) => ({
|
|
3587
|
-
name: a.name,
|
|
3588
|
-
id: a.id
|
|
3589
|
-
}));
|
|
3590
|
-
return agents;
|
|
3591
|
-
}
|
|
3592
|
-
async loadAgentState(graphqlContext, threadId, agentName) {
|
|
3593
|
-
const agentsWithEndpoints = await this.discoverAgentsFromEndpoints(graphqlContext);
|
|
3594
|
-
const agentWithEndpoint = agentsWithEndpoints.find((agent) => agent.name === agentName);
|
|
3595
|
-
if (!agentWithEndpoint) {
|
|
3596
|
-
throw new Error("Agent not found");
|
|
3597
4115
|
}
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
4116
|
+
}));
|
|
4117
|
+
return [
|
|
4118
|
+
...agents
|
|
4119
|
+
];
|
|
4120
|
+
}
|
|
4121
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
4122
|
+
var RemoteAgentType;
|
|
4123
|
+
(function(RemoteAgentType2) {
|
|
4124
|
+
RemoteAgentType2["LangGraph"] = "langgraph";
|
|
4125
|
+
RemoteAgentType2["CrewAI"] = "crewai";
|
|
4126
|
+
})(RemoteAgentType || (RemoteAgentType = {}));
|
|
4127
|
+
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
4128
|
+
const totalAgents = Array.isArray(json["agents"]) ? json["agents"].length : 0;
|
|
4129
|
+
const actions = json["actions"].map((action) => ({
|
|
4130
|
+
name: action.name,
|
|
4131
|
+
description: action.description,
|
|
4132
|
+
parameters: action.parameters,
|
|
4133
|
+
handler: async (args) => {
|
|
4134
|
+
logger2.debug({
|
|
4135
|
+
actionName: action.name,
|
|
4136
|
+
args
|
|
4137
|
+
}, "Executing remote action");
|
|
4138
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4139
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4140
|
+
agentExecution: false,
|
|
4141
|
+
type: "self-hosted",
|
|
4142
|
+
agentsAmount: totalAgents
|
|
3608
4143
|
});
|
|
3609
|
-
|
|
3610
|
-
try {
|
|
3611
|
-
state = (await client.threads.getState(threadId)).values;
|
|
3612
|
-
} catch (error) {
|
|
3613
|
-
}
|
|
3614
|
-
if (Object.keys(state).length === 0) {
|
|
3615
|
-
return {
|
|
3616
|
-
threadId: threadId || "",
|
|
3617
|
-
threadExists: false,
|
|
3618
|
-
state: JSON.stringify({}),
|
|
3619
|
-
messages: JSON.stringify([])
|
|
3620
|
-
};
|
|
3621
|
-
} else {
|
|
3622
|
-
const { messages, ...stateWithoutMessages } = state;
|
|
3623
|
-
const copilotkitMessages = langchainMessagesToCopilotKit(messages);
|
|
3624
|
-
return {
|
|
3625
|
-
threadId: threadId || "",
|
|
3626
|
-
threadExists: true,
|
|
3627
|
-
state: JSON.stringify(stateWithoutMessages),
|
|
3628
|
-
messages: JSON.stringify(copilotkitMessages)
|
|
3629
|
-
};
|
|
3630
|
-
}
|
|
3631
|
-
} else if (agentWithEndpoint.endpoint.type === EndpointType.CopilotKit || !("type" in agentWithEndpoint.endpoint)) {
|
|
3632
|
-
const cpkEndpoint = agentWithEndpoint.endpoint;
|
|
3633
|
-
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
4144
|
+
const fetchUrl = `${url}/actions/execute`;
|
|
3634
4145
|
try {
|
|
3635
4146
|
const response = await fetchWithRetry(fetchUrl, {
|
|
3636
4147
|
method: "POST",
|
|
3637
|
-
headers
|
|
4148
|
+
headers,
|
|
3638
4149
|
body: JSON.stringify({
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
4150
|
+
name: action.name,
|
|
4151
|
+
arguments: args,
|
|
4152
|
+
properties: graphqlContext.properties
|
|
3642
4153
|
})
|
|
3643
|
-
});
|
|
4154
|
+
}, logger2);
|
|
3644
4155
|
if (!response.ok) {
|
|
4156
|
+
logger2.error({
|
|
4157
|
+
url,
|
|
4158
|
+
status: response.status,
|
|
4159
|
+
body: await response.text()
|
|
4160
|
+
}, "Failed to execute remote action");
|
|
3645
4161
|
if (response.status === 404) {
|
|
3646
4162
|
throw new CopilotKitApiDiscoveryError2({
|
|
3647
4163
|
url: fetchUrl
|
|
3648
4164
|
});
|
|
3649
4165
|
}
|
|
3650
|
-
throw new
|
|
4166
|
+
throw new ResolvedCopilotKitError2({
|
|
3651
4167
|
status: response.status,
|
|
3652
4168
|
url: fetchUrl,
|
|
3653
4169
|
isRemoteEndpoint: true
|
|
3654
4170
|
});
|
|
3655
4171
|
}
|
|
3656
|
-
const
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
};
|
|
4172
|
+
const requestResult = await response.json();
|
|
4173
|
+
const result = requestResult["result"];
|
|
4174
|
+
logger2.debug({
|
|
4175
|
+
actionName: action.name,
|
|
4176
|
+
result
|
|
4177
|
+
}, "Executed remote action");
|
|
4178
|
+
return result;
|
|
3662
4179
|
} catch (error) {
|
|
3663
|
-
if (error instanceof
|
|
4180
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
3664
4181
|
throw error;
|
|
3665
4182
|
}
|
|
3666
|
-
throw new
|
|
4183
|
+
throw new CopilotKitLowLevelError5({
|
|
3667
4184
|
error,
|
|
3668
4185
|
url: fetchUrl
|
|
3669
4186
|
});
|
|
3670
4187
|
}
|
|
3671
|
-
} else {
|
|
3672
|
-
throw new Error(`Unknown endpoint type: ${agentWithEndpoint.endpoint.type}`);
|
|
3673
|
-
}
|
|
3674
|
-
}
|
|
3675
|
-
async processAgentRequest(request) {
|
|
3676
|
-
var _a, _b, _c, _d, _e;
|
|
3677
|
-
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession, threadId: threadIdFromRequest, metaEvents, publicApiKey, forwardedParameters } = request;
|
|
3678
|
-
const { agentName, nodeName } = agentSession;
|
|
3679
|
-
const requestStartTime = Date.now();
|
|
3680
|
-
const streamedChunks = [];
|
|
3681
|
-
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
3682
|
-
const serverSideActions = await this.getServerSideActions(request);
|
|
3683
|
-
const messages = convertGqlInputToMessages(rawMessages);
|
|
3684
|
-
const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
|
|
3685
|
-
if (!currentAgent) {
|
|
3686
|
-
throw new CopilotKitAgentDiscoveryError({
|
|
3687
|
-
agentName,
|
|
3688
|
-
availableAgents: this.availableAgents
|
|
3689
|
-
});
|
|
3690
|
-
}
|
|
3691
|
-
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
3692
|
-
// Case 1: Keep all regular (non-agent) actions
|
|
3693
|
-
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
3694
|
-
isRemoteAgentAction(action) && action.name !== agentName
|
|
3695
|
-
)).map((action) => ({
|
|
3696
|
-
name: action.name,
|
|
3697
|
-
description: action.description,
|
|
3698
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3699
|
-
}));
|
|
3700
|
-
const allAvailableActions = flattenToolCallsNoDuplicates([
|
|
3701
|
-
...availableActionsForCurrentAgent,
|
|
3702
|
-
...request.actions
|
|
3703
|
-
]);
|
|
3704
|
-
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
3705
|
-
try {
|
|
3706
|
-
const requestData = {
|
|
3707
|
-
threadId,
|
|
3708
|
-
runId: void 0,
|
|
3709
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3710
|
-
messages,
|
|
3711
|
-
actions: allAvailableActions,
|
|
3712
|
-
forwardedParameters,
|
|
3713
|
-
timestamp: requestStartTime,
|
|
3714
|
-
provider: "agent",
|
|
3715
|
-
agentName,
|
|
3716
|
-
nodeName
|
|
3717
|
-
};
|
|
3718
|
-
await this.observability.hooks.handleRequest(requestData);
|
|
3719
|
-
} catch (error) {
|
|
3720
|
-
console.error("Error logging agent request:", error);
|
|
3721
|
-
}
|
|
3722
4188
|
}
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
const progressiveData = {
|
|
3748
|
-
threadId: threadId || "",
|
|
3749
|
-
runId: void 0,
|
|
3750
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3751
|
-
output: event.content,
|
|
3752
|
-
latency: Date.now() - requestStartTime,
|
|
3753
|
-
timestamp: Date.now(),
|
|
3754
|
-
provider: "agent",
|
|
3755
|
-
isProgressiveChunk: true,
|
|
3756
|
-
agentName,
|
|
3757
|
-
nodeName
|
|
3758
|
-
};
|
|
3759
|
-
Promise.resolve().then(() => {
|
|
3760
|
-
this.observability.hooks.handleResponse(progressiveData);
|
|
3761
|
-
}).catch((error) => {
|
|
3762
|
-
console.error("Error in progressive agent logging:", error);
|
|
3763
|
-
});
|
|
3764
|
-
} catch (error) {
|
|
3765
|
-
console.error("Error preparing progressive agent log data:", error);
|
|
3766
|
-
}
|
|
3767
|
-
}
|
|
3768
|
-
}
|
|
3769
|
-
});
|
|
3770
|
-
await callback(eventStream$);
|
|
3771
|
-
});
|
|
3772
|
-
};
|
|
3773
|
-
}
|
|
3774
|
-
eventSource.stream(async (eventStream$) => {
|
|
3775
|
-
from(stream).subscribe({
|
|
3776
|
-
next: (event) => eventStream$.next(event),
|
|
3777
|
-
error: (err) => {
|
|
3778
|
-
var _a2;
|
|
3779
|
-
console.error("Error in stream", err);
|
|
3780
|
-
if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
|
|
3781
|
-
try {
|
|
3782
|
-
const errorData = {
|
|
3783
|
-
threadId,
|
|
3784
|
-
runId: void 0,
|
|
3785
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3786
|
-
error: err instanceof Error ? err : String(err),
|
|
3787
|
-
timestamp: Date.now(),
|
|
3788
|
-
latency: Date.now() - requestStartTime,
|
|
3789
|
-
provider: "agent",
|
|
3790
|
-
agentName,
|
|
3791
|
-
nodeName
|
|
3792
|
-
};
|
|
3793
|
-
this.observability.hooks.handleError(errorData);
|
|
3794
|
-
} catch (logError) {
|
|
3795
|
-
console.error("Error logging agent error:", logError);
|
|
3796
|
-
}
|
|
3797
|
-
}
|
|
3798
|
-
const structuredError = this.convertStreamingErrorToStructured(err);
|
|
3799
|
-
eventStream$.error(structuredError);
|
|
3800
|
-
eventStream$.complete();
|
|
3801
|
-
},
|
|
3802
|
-
complete: () => eventStream$.complete()
|
|
3803
|
-
});
|
|
3804
|
-
});
|
|
3805
|
-
if (((_d = this.observability) == null ? void 0 : _d.enabled) && publicApiKey) {
|
|
3806
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3807
|
-
const responseData = {
|
|
3808
|
-
threadId,
|
|
3809
|
-
runId: void 0,
|
|
3810
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3811
|
-
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
3812
|
-
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
3813
|
-
latency: Date.now() - requestStartTime,
|
|
3814
|
-
timestamp: Date.now(),
|
|
3815
|
-
provider: "agent",
|
|
3816
|
-
isFinalResponse: true,
|
|
3817
|
-
agentName,
|
|
3818
|
-
nodeName
|
|
3819
|
-
};
|
|
3820
|
-
try {
|
|
3821
|
-
this.observability.hooks.handleResponse(responseData);
|
|
3822
|
-
} catch (logError) {
|
|
3823
|
-
console.error("Error logging agent response:", logError);
|
|
3824
|
-
}
|
|
3825
|
-
}).catch((error) => {
|
|
3826
|
-
console.error("Failed to get output messages for agent logging:", error);
|
|
3827
|
-
});
|
|
3828
|
-
}
|
|
3829
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3830
|
-
var _a2;
|
|
3831
|
-
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
3832
|
-
threadId,
|
|
3833
|
-
runId: void 0,
|
|
3834
|
-
inputMessages: messages,
|
|
3835
|
-
outputMessages,
|
|
3836
|
-
properties: graphqlContext.properties
|
|
3837
|
-
});
|
|
3838
|
-
}).catch((_error) => {
|
|
3839
|
-
});
|
|
3840
|
-
return {
|
|
3841
|
-
threadId,
|
|
3842
|
-
runId: void 0,
|
|
3843
|
-
eventSource,
|
|
3844
|
-
serverSideActions,
|
|
3845
|
-
actionInputsWithoutAgents: allAvailableActions
|
|
3846
|
-
};
|
|
3847
|
-
} catch (error) {
|
|
3848
|
-
if (((_e = this.observability) == null ? void 0 : _e.enabled) && publicApiKey) {
|
|
3849
|
-
try {
|
|
3850
|
-
const errorData = {
|
|
3851
|
-
threadId,
|
|
3852
|
-
runId: void 0,
|
|
3853
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3854
|
-
error: error instanceof Error ? error : String(error),
|
|
3855
|
-
timestamp: Date.now(),
|
|
3856
|
-
latency: Date.now() - requestStartTime,
|
|
3857
|
-
provider: "agent",
|
|
3858
|
-
agentName,
|
|
3859
|
-
nodeName
|
|
3860
|
-
};
|
|
3861
|
-
await this.observability.hooks.handleError(errorData);
|
|
3862
|
-
} catch (logError) {
|
|
3863
|
-
console.error("Error logging agent error:", logError);
|
|
4189
|
+
}));
|
|
4190
|
+
const agents = totalAgents ? json["agents"].map((agent) => ({
|
|
4191
|
+
name: agent.name,
|
|
4192
|
+
description: agent.description,
|
|
4193
|
+
parameters: [],
|
|
4194
|
+
handler: async (_args) => {
|
|
4195
|
+
},
|
|
4196
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
4197
|
+
logger2.debug({
|
|
4198
|
+
actionName: agent.name
|
|
4199
|
+
}, "Executing remote agent");
|
|
4200
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4201
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4202
|
+
agentExecution: true,
|
|
4203
|
+
type: "self-hosted",
|
|
4204
|
+
agentsAmount: json["agents"].length
|
|
4205
|
+
});
|
|
4206
|
+
let state = {};
|
|
4207
|
+
let config = {};
|
|
4208
|
+
if (agentStates) {
|
|
4209
|
+
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
4210
|
+
if (jsonState) {
|
|
4211
|
+
state = parseJson2(jsonState.state, {});
|
|
4212
|
+
config = parseJson2(jsonState.config, {});
|
|
3864
4213
|
}
|
|
3865
4214
|
}
|
|
3866
|
-
|
|
3867
|
-
throw error;
|
|
3868
|
-
}
|
|
3869
|
-
}
|
|
3870
|
-
async getServerSideActions(request) {
|
|
3871
|
-
var _a, _b;
|
|
3872
|
-
const { graphqlContext, messages: rawMessages, agentStates, url } = request;
|
|
3873
|
-
const inputMessages = convertGqlInputToMessages(rawMessages);
|
|
3874
|
-
const langserveFunctions = [];
|
|
3875
|
-
for (const chainPromise of this.langserve) {
|
|
4215
|
+
const fetchUrl = `${url}/agents/execute`;
|
|
3876
4216
|
try {
|
|
3877
|
-
const
|
|
3878
|
-
|
|
4217
|
+
const response = await fetchWithRetry(fetchUrl, {
|
|
4218
|
+
method: "POST",
|
|
4219
|
+
headers,
|
|
4220
|
+
body: JSON.stringify({
|
|
4221
|
+
name,
|
|
4222
|
+
threadId,
|
|
4223
|
+
nodeName,
|
|
4224
|
+
messages: [
|
|
4225
|
+
...messages,
|
|
4226
|
+
...additionalMessages
|
|
4227
|
+
],
|
|
4228
|
+
state,
|
|
4229
|
+
config,
|
|
4230
|
+
properties: graphqlContext.properties,
|
|
4231
|
+
actions: tryMap2(actionInputsWithoutAgents, (action) => ({
|
|
4232
|
+
name: action.name,
|
|
4233
|
+
description: action.description,
|
|
4234
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
4235
|
+
})),
|
|
4236
|
+
metaEvents
|
|
4237
|
+
})
|
|
4238
|
+
}, logger2);
|
|
4239
|
+
if (!response.ok) {
|
|
4240
|
+
logger2.error({
|
|
4241
|
+
url,
|
|
4242
|
+
status: response.status,
|
|
4243
|
+
body: await response.text()
|
|
4244
|
+
}, "Failed to execute remote agent");
|
|
4245
|
+
if (response.status === 404) {
|
|
4246
|
+
throw new CopilotKitApiDiscoveryError2({
|
|
4247
|
+
url: fetchUrl
|
|
4248
|
+
});
|
|
4249
|
+
}
|
|
4250
|
+
throw new ResolvedCopilotKitError2({
|
|
4251
|
+
status: response.status,
|
|
4252
|
+
url: fetchUrl,
|
|
4253
|
+
isRemoteEndpoint: true
|
|
4254
|
+
});
|
|
4255
|
+
}
|
|
4256
|
+
if (agent.type === "langgraph") {
|
|
4257
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
4258
|
+
writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
|
|
4259
|
+
return eventSource.processLangGraphEvents();
|
|
4260
|
+
} else if (agent.type === "crewai") {
|
|
4261
|
+
const eventStream$ = new RuntimeEventSubject();
|
|
4262
|
+
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
4263
|
+
return eventStream$;
|
|
4264
|
+
} else {
|
|
4265
|
+
throw new Error("Unsupported agent type");
|
|
4266
|
+
}
|
|
3879
4267
|
} catch (error) {
|
|
3880
|
-
|
|
4268
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
4269
|
+
throw error;
|
|
4270
|
+
}
|
|
4271
|
+
throw new CopilotKitLowLevelError5({
|
|
4272
|
+
error,
|
|
4273
|
+
url: fetchUrl
|
|
4274
|
+
});
|
|
3881
4275
|
}
|
|
3882
4276
|
}
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
4277
|
+
})) : [];
|
|
4278
|
+
return [
|
|
4279
|
+
...actions,
|
|
4280
|
+
...agents
|
|
4281
|
+
];
|
|
4282
|
+
}
|
|
4283
|
+
__name(constructRemoteActions, "constructRemoteActions");
|
|
4284
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
4285
|
+
const headers = {
|
|
4286
|
+
"Content-Type": "application/json"
|
|
4287
|
+
};
|
|
4288
|
+
if (onBeforeRequest) {
|
|
4289
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
4290
|
+
ctx: graphqlContext
|
|
3895
4291
|
});
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
4292
|
+
if (additionalHeaders) {
|
|
4293
|
+
Object.assign(headers, additionalHeaders);
|
|
4294
|
+
}
|
|
4295
|
+
}
|
|
4296
|
+
return headers;
|
|
4297
|
+
}
|
|
4298
|
+
__name(createHeaders, "createHeaders");
|
|
4299
|
+
|
|
4300
|
+
// src/lib/runtime/remote-actions.ts
|
|
4301
|
+
import { CopilotKitLowLevelError as CopilotKitLowLevelError6, ResolvedCopilotKitError as ResolvedCopilotKitError3, CopilotKitError as CopilotKitError5 } from "@copilotkit/shared";
|
|
4302
|
+
|
|
4303
|
+
// src/lib/runtime/agui-action.ts
|
|
4304
|
+
import { parseJson as parseJson3 } from "@copilotkit/shared";
|
|
4305
|
+
function constructAGUIRemoteAction({ logger: logger2, messages, agentStates, agent, metaEvents }) {
|
|
4306
|
+
const action = {
|
|
4307
|
+
name: agent.agentId,
|
|
4308
|
+
description: agent.description,
|
|
4309
|
+
parameters: [],
|
|
4310
|
+
handler: async (_args) => {
|
|
4311
|
+
},
|
|
4312
|
+
remoteAgentHandler: async ({ actionInputsWithoutAgents, threadId }) => {
|
|
4313
|
+
var _a;
|
|
4314
|
+
logger2.debug({
|
|
4315
|
+
actionName: agent.agentId
|
|
4316
|
+
}, "Executing remote agent");
|
|
4317
|
+
const agentWireMessages = convertMessagesToAGUIMessage(messages);
|
|
4318
|
+
agent.messages = agentWireMessages;
|
|
4319
|
+
agent.threadId = threadId;
|
|
4320
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4321
|
+
agentExecution: true,
|
|
4322
|
+
type: "self-hosted",
|
|
4323
|
+
agentsAmount: 1
|
|
3911
4324
|
});
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
3915
|
-
if (
|
|
3916
|
-
|
|
4325
|
+
let state = {};
|
|
4326
|
+
if (agentStates) {
|
|
4327
|
+
const jsonState = agentStates.find((state2) => state2.agentName === agent.agentId);
|
|
4328
|
+
if (jsonState) {
|
|
4329
|
+
state = parseJson3(jsonState.state, {});
|
|
3917
4330
|
}
|
|
4331
|
+
}
|
|
4332
|
+
agent.state = state;
|
|
4333
|
+
const tools = actionInputsWithoutAgents.map((input) => {
|
|
4334
|
+
return {
|
|
4335
|
+
name: input.name,
|
|
4336
|
+
description: input.description,
|
|
4337
|
+
parameters: JSON.parse(input.jsonSchema)
|
|
4338
|
+
};
|
|
3918
4339
|
});
|
|
3919
|
-
const
|
|
3920
|
-
|
|
3921
|
-
|
|
3922
|
-
let actionsForEndpoint = this.mcpActionCache.get(endpointUrl);
|
|
3923
|
-
if (!actionsForEndpoint) {
|
|
3924
|
-
let client = null;
|
|
3925
|
-
try {
|
|
3926
|
-
client = await this.createMCPClientImpl(config);
|
|
3927
|
-
const tools = await client.tools();
|
|
3928
|
-
actionsForEndpoint = convertMCPToolsToActions(tools, endpointUrl);
|
|
3929
|
-
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3930
|
-
} catch (error) {
|
|
3931
|
-
console.error(`MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`, error);
|
|
3932
|
-
actionsForEndpoint = [];
|
|
3933
|
-
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3934
|
-
}
|
|
4340
|
+
const forwardedProps = metaEvents.length ? {
|
|
4341
|
+
command: {
|
|
4342
|
+
resume: (_a = metaEvents[0]) == null ? void 0 : _a.response
|
|
3935
4343
|
}
|
|
3936
|
-
|
|
3937
|
-
|
|
4344
|
+
} : void 0;
|
|
4345
|
+
return agent.legacy_to_be_removed_runAgentBridged({
|
|
4346
|
+
tools,
|
|
4347
|
+
forwardedProps
|
|
4348
|
+
});
|
|
3938
4349
|
}
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
3955
|
-
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
4350
|
+
};
|
|
4351
|
+
return [
|
|
4352
|
+
action
|
|
4353
|
+
];
|
|
4354
|
+
}
|
|
4355
|
+
__name(constructAGUIRemoteAction, "constructAGUIRemoteAction");
|
|
4356
|
+
function convertMessagesToAGUIMessage(messages) {
|
|
4357
|
+
const result = [];
|
|
4358
|
+
for (const message of messages) {
|
|
4359
|
+
if (message.isTextMessage()) {
|
|
4360
|
+
result.push({
|
|
4361
|
+
id: message.id,
|
|
4362
|
+
role: message.role,
|
|
4363
|
+
content: message.content
|
|
4364
|
+
});
|
|
4365
|
+
} else if (message.isActionExecutionMessage()) {
|
|
4366
|
+
const toolCall = {
|
|
4367
|
+
id: message.id,
|
|
4368
|
+
type: "function",
|
|
4369
|
+
function: {
|
|
4370
|
+
name: message.name,
|
|
4371
|
+
arguments: JSON.stringify(message.arguments)
|
|
4372
|
+
}
|
|
4373
|
+
};
|
|
4374
|
+
if (message.parentMessageId && result.some((m) => m.id === message.parentMessageId)) {
|
|
4375
|
+
const parentMessage = result.find((m) => m.id === message.parentMessageId);
|
|
4376
|
+
if (parentMessage.toolCalls === void 0) {
|
|
4377
|
+
parentMessage.toolCalls = [];
|
|
4378
|
+
}
|
|
4379
|
+
parentMessage.toolCalls.push(toolCall);
|
|
4380
|
+
} else {
|
|
4381
|
+
result.push({
|
|
4382
|
+
id: message.parentMessageId ?? message.id,
|
|
4383
|
+
role: "assistant",
|
|
4384
|
+
toolCalls: [
|
|
4385
|
+
toolCall
|
|
4386
|
+
]
|
|
4387
|
+
});
|
|
4388
|
+
}
|
|
4389
|
+
} else if (message.isResultMessage()) {
|
|
4390
|
+
result.push({
|
|
4391
|
+
id: message.id,
|
|
4392
|
+
role: "tool",
|
|
4393
|
+
content: message.result,
|
|
4394
|
+
toolCallId: message.actionExecutionId
|
|
3967
4395
|
});
|
|
3968
4396
|
}
|
|
3969
|
-
|
|
3970
|
-
|
|
3971
|
-
|
|
3972
|
-
|
|
3973
|
-
|
|
4397
|
+
}
|
|
4398
|
+
return result;
|
|
4399
|
+
}
|
|
4400
|
+
__name(convertMessagesToAGUIMessage, "convertMessagesToAGUIMessage");
|
|
4401
|
+
|
|
4402
|
+
// src/lib/runtime/remote-actions.ts
|
|
4403
|
+
var EndpointType;
|
|
4404
|
+
(function(EndpointType2) {
|
|
4405
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
4406
|
+
EndpointType2["LangGraphPlatform"] = "langgraph-platform";
|
|
4407
|
+
})(EndpointType || (EndpointType = {}));
|
|
4408
|
+
function isRemoteAgentAction(action) {
|
|
4409
|
+
if (!action) {
|
|
4410
|
+
return false;
|
|
4411
|
+
}
|
|
4412
|
+
return typeof action.remoteAgentHandler === "function";
|
|
4413
|
+
}
|
|
4414
|
+
__name(isRemoteAgentAction, "isRemoteAgentAction");
|
|
4415
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
4416
|
+
logger2.debug({
|
|
4417
|
+
url
|
|
4418
|
+
}, "Fetching actions from url");
|
|
4419
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4420
|
+
const fetchUrl = `${url}/info`;
|
|
4421
|
+
try {
|
|
4422
|
+
const response = await fetch(fetchUrl, {
|
|
4423
|
+
method: "POST",
|
|
4424
|
+
headers,
|
|
4425
|
+
body: JSON.stringify({
|
|
4426
|
+
properties: graphqlContext.properties,
|
|
4427
|
+
frontendUrl
|
|
4428
|
+
})
|
|
4429
|
+
});
|
|
4430
|
+
if (!response.ok) {
|
|
4431
|
+
logger2.error({
|
|
4432
|
+
url,
|
|
4433
|
+
status: response.status,
|
|
4434
|
+
body: await response.text()
|
|
4435
|
+
}, "Failed to fetch actions from url");
|
|
4436
|
+
throw new ResolvedCopilotKitError3({
|
|
4437
|
+
status: response.status,
|
|
4438
|
+
url: fetchUrl,
|
|
4439
|
+
isRemoteEndpoint: true
|
|
3974
4440
|
});
|
|
3975
4441
|
}
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
|
|
4442
|
+
const json = await response.json();
|
|
4443
|
+
logger2.debug({
|
|
4444
|
+
json
|
|
4445
|
+
}, "Fetched actions from url");
|
|
4446
|
+
return json;
|
|
4447
|
+
} catch (error) {
|
|
4448
|
+
if (error instanceof CopilotKitError5) {
|
|
4449
|
+
throw error;
|
|
3981
4450
|
}
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
|
|
4451
|
+
throw new CopilotKitLowLevelError6({
|
|
4452
|
+
error,
|
|
4453
|
+
url: fetchUrl
|
|
3985
4454
|
});
|
|
3986
4455
|
}
|
|
3987
|
-
};
|
|
3988
|
-
__name(CopilotRuntime, "CopilotRuntime");
|
|
3989
|
-
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
3990
|
-
let allTools = [];
|
|
3991
|
-
const allToolNames = [];
|
|
3992
|
-
for (const tool of toolsByPriority) {
|
|
3993
|
-
if (!allToolNames.includes(tool.name)) {
|
|
3994
|
-
allTools.push(tool);
|
|
3995
|
-
allToolNames.push(tool.name);
|
|
3996
|
-
}
|
|
3997
|
-
}
|
|
3998
|
-
return allTools;
|
|
3999
|
-
}
|
|
4000
|
-
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
4001
|
-
function copilotKitEndpoint(config) {
|
|
4002
|
-
return {
|
|
4003
|
-
...config,
|
|
4004
|
-
type: EndpointType.CopilotKit
|
|
4005
|
-
};
|
|
4006
|
-
}
|
|
4007
|
-
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
4008
|
-
function langGraphPlatformEndpoint(config) {
|
|
4009
|
-
return {
|
|
4010
|
-
...config,
|
|
4011
|
-
type: EndpointType.LangGraphPlatform
|
|
4012
|
-
};
|
|
4013
4456
|
}
|
|
4014
|
-
__name(
|
|
4015
|
-
function
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4457
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
4458
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl, agents, metaEvents }) {
|
|
4459
|
+
const logger2 = graphqlContext.logger.child({
|
|
4460
|
+
component: "remote-actions.fetchRemoteActions"
|
|
4461
|
+
});
|
|
4462
|
+
logger2.debug({
|
|
4463
|
+
remoteEndpointDefinitions
|
|
4464
|
+
}, "Fetching from remote endpoints");
|
|
4465
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
4466
|
+
if (value.type === "langgraph-platform") {
|
|
4467
|
+
return value;
|
|
4021
4468
|
}
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
const endpointsInfo = runtime.remoteEndpointDefinitions.reduce((acc, endpoint) => {
|
|
4037
|
-
let info = {
|
|
4038
|
-
...acc
|
|
4039
|
-
};
|
|
4040
|
-
const endpointType = resolveEndpointType(endpoint);
|
|
4041
|
-
if (!info.endpointTypes.includes(endpointType)) {
|
|
4042
|
-
info = {
|
|
4043
|
-
...info,
|
|
4044
|
-
endpointTypes: [
|
|
4045
|
-
...info.endpointTypes,
|
|
4046
|
-
endpointType
|
|
4047
|
-
]
|
|
4048
|
-
};
|
|
4469
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
4470
|
+
});
|
|
4471
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
4472
|
+
if (endpoint.type === "langgraph-platform") {
|
|
4473
|
+
return constructLGCRemoteAction({
|
|
4474
|
+
endpoint,
|
|
4475
|
+
messages,
|
|
4476
|
+
graphqlContext,
|
|
4477
|
+
logger: logger2.child({
|
|
4478
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
4479
|
+
endpoint
|
|
4480
|
+
}),
|
|
4481
|
+
agentStates
|
|
4482
|
+
});
|
|
4049
4483
|
}
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4484
|
+
const json = await fetchRemoteInfo({
|
|
4485
|
+
url: endpoint.url,
|
|
4486
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
4487
|
+
graphqlContext,
|
|
4488
|
+
logger: logger2.child({
|
|
4489
|
+
component: "remote-actions.fetchActionsFromUrl",
|
|
4490
|
+
endpoint
|
|
4491
|
+
}),
|
|
4492
|
+
frontendUrl
|
|
4493
|
+
});
|
|
4494
|
+
return constructRemoteActions({
|
|
4495
|
+
json,
|
|
4496
|
+
messages,
|
|
4497
|
+
url: endpoint.url,
|
|
4498
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
4499
|
+
graphqlContext,
|
|
4500
|
+
logger: logger2.child({
|
|
4501
|
+
component: "remote-actions.constructActions",
|
|
4502
|
+
endpoint
|
|
4503
|
+
}),
|
|
4504
|
+
agentStates
|
|
4505
|
+
});
|
|
4506
|
+
}));
|
|
4507
|
+
for (const [key, agent] of Object.entries(agents)) {
|
|
4508
|
+
if (agent.agentId !== void 0 && agent.agentId !== key) {
|
|
4509
|
+
throw new CopilotKitError5({
|
|
4510
|
+
message: `Agent ${key} has agentId ${agent.agentId} which does not match the key ${key}`,
|
|
4511
|
+
code: CopilotKitErrorCode3.UNKNOWN
|
|
4512
|
+
});
|
|
4513
|
+
} else if (agent.agentId === void 0) {
|
|
4514
|
+
agent.agentId = key;
|
|
4057
4515
|
}
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
return
|
|
4067
|
-
actionsAmount: runtime.actions.length,
|
|
4068
|
-
endpointsAmount: runtime.remoteEndpointDefinitions.length,
|
|
4069
|
-
endpointTypes: endpointsInfo.endpointTypes,
|
|
4070
|
-
agentsAmount: endpointsInfo.agentsAmount,
|
|
4071
|
-
hashedLgcKey: endpointsInfo.hashedKey,
|
|
4072
|
-
"cloud.api_key_provided": apiKeyProvided,
|
|
4073
|
-
...apiKeyProvided ? {
|
|
4074
|
-
"cloud.public_api_key": publicApiKey
|
|
4075
|
-
} : {},
|
|
4076
|
-
...((_b = options.cloud) == null ? void 0 : _b.baseUrl) ? {
|
|
4077
|
-
"cloud.base_url": options.cloud.baseUrl
|
|
4078
|
-
} : {}
|
|
4079
|
-
};
|
|
4516
|
+
result.push(constructAGUIRemoteAction({
|
|
4517
|
+
logger: logger2,
|
|
4518
|
+
messages,
|
|
4519
|
+
agentStates,
|
|
4520
|
+
agent,
|
|
4521
|
+
metaEvents
|
|
4522
|
+
}));
|
|
4523
|
+
}
|
|
4524
|
+
return result.flat();
|
|
4080
4525
|
}
|
|
4081
|
-
__name(
|
|
4082
|
-
var telemetry_client_default = telemetryClient;
|
|
4526
|
+
__name(setupRemoteActions, "setupRemoteActions");
|
|
4083
4527
|
|
|
4084
4528
|
// src/service-adapters/events.ts
|
|
4085
|
-
import { plainToInstance as plainToInstance2 } from "class-transformer";
|
|
4086
4529
|
var RuntimeEventTypes;
|
|
4087
4530
|
(function(RuntimeEventTypes2) {
|
|
4088
4531
|
RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
|
|
@@ -4205,16 +4648,15 @@ var RuntimeEventSource = class {
|
|
|
4205
4648
|
const errorMessage = `\u274C ${message}`;
|
|
4206
4649
|
if (!this.callback) {
|
|
4207
4650
|
this.stream(async (eventStream$) => {
|
|
4208
|
-
eventStream$.sendTextMessage(
|
|
4651
|
+
eventStream$.sendTextMessage(randomId2(), errorMessage);
|
|
4209
4652
|
});
|
|
4210
4653
|
} else {
|
|
4211
|
-
this.eventStream$.sendTextMessage(
|
|
4654
|
+
this.eventStream$.sendTextMessage(randomId2(), errorMessage);
|
|
4212
4655
|
}
|
|
4213
4656
|
}
|
|
4214
4657
|
processRuntimeEvents({ serverSideActions, guardrailsResult$, actionInputsWithoutAgents, threadId }) {
|
|
4215
4658
|
this.callback(this.eventStream$).catch((error) => {
|
|
4216
|
-
|
|
4217
|
-
const structuredError = convertStreamingErrorToStructured2(error);
|
|
4659
|
+
const structuredError = ensureStructuredError3(error, convertStreamingErrorToStructured2);
|
|
4218
4660
|
this.eventStream$.error(structuredError);
|
|
4219
4661
|
this.eventStream$.complete();
|
|
4220
4662
|
});
|
|
@@ -4249,12 +4691,10 @@ var RuntimeEventSource = class {
|
|
|
4249
4691
|
if (eventWithState.event.type === "ActionExecutionEnd" && eventWithState.callActionServerSide) {
|
|
4250
4692
|
const toolCallEventStream$ = new RuntimeEventSubject();
|
|
4251
4693
|
executeAction(toolCallEventStream$, guardrailsResult$ ? guardrailsResult$ : null, eventWithState.action, eventWithState.args, eventWithState.actionExecutionParentMessageId, eventWithState.actionExecutionId, actionInputsWithoutAgents, threadId).catch((error) => {
|
|
4252
|
-
console.error(error);
|
|
4253
4694
|
});
|
|
4254
4695
|
telemetry_client_default.capture("oss.runtime.server_action_executed", {});
|
|
4255
4696
|
return concat(of(eventWithState.event), toolCallEventStream$).pipe(catchError2((error) => {
|
|
4256
|
-
|
|
4257
|
-
const structuredError = convertStreamingErrorToStructured2(error);
|
|
4697
|
+
const structuredError = ensureStructuredError3(error, convertStreamingErrorToStructured2);
|
|
4258
4698
|
toolCallEventStream$.sendActionExecutionResult({
|
|
4259
4699
|
actionExecutionId: eventWithState.actionExecutionId,
|
|
4260
4700
|
actionName: eventWithState.action.name,
|
|
@@ -4334,8 +4774,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
4334
4774
|
from2(stream).subscribe({
|
|
4335
4775
|
next: (event) => eventStream$.next(event),
|
|
4336
4776
|
error: (err) => {
|
|
4337
|
-
|
|
4338
|
-
const structuredError = convertStreamingErrorToStructured2(err);
|
|
4777
|
+
const structuredError = ensureStructuredError3(err, convertStreamingErrorToStructured2);
|
|
4339
4778
|
eventStream$.sendActionExecutionResult({
|
|
4340
4779
|
actionExecutionId,
|
|
4341
4780
|
actionName: action.name,
|
|
@@ -4375,36 +4814,19 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
4375
4814
|
}
|
|
4376
4815
|
__name(executeAction, "executeAction");
|
|
4377
4816
|
function convertStreamingErrorToStructured2(error) {
|
|
4378
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
4379
|
-
|
|
4380
|
-
|
|
4381
|
-
|
|
4382
|
-
code: CopilotKitErrorCode4.NETWORK_ERROR
|
|
4383
|
-
});
|
|
4384
|
-
}
|
|
4385
|
-
if (((_d = error == null ? void 0 : error.message) == null ? void 0 : _d.includes("fetch failed")) || ((_e = error == null ? void 0 : error.message) == null ? void 0 : _e.includes("ECONNREFUSED")) || ((_f = error == null ? void 0 : error.message) == null ? void 0 : _f.includes("ENOTFOUND")) || ((_g = error == null ? void 0 : error.message) == null ? void 0 : _g.includes("ETIMEDOUT"))) {
|
|
4386
|
-
return new CopilotKitLowLevelError5({
|
|
4817
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
4818
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "event streaming connection");
|
|
4819
|
+
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("fetch failed")) || ((_b = error == null ? void 0 : error.message) == null ? void 0 : _b.includes("ECONNREFUSED")) || ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("ENOTFOUND")) || ((_d = error == null ? void 0 : error.message) == null ? void 0 : _d.includes("ETIMEDOUT")) || ((_e = error == null ? void 0 : error.message) == null ? void 0 : _e.includes("terminated")) || ((_f = error == null ? void 0 : error.cause) == null ? void 0 : _f.code) === "UND_ERR_SOCKET" || ((_g = error == null ? void 0 : error.message) == null ? void 0 : _g.includes("other side closed")) || (error == null ? void 0 : error.code) === "UND_ERR_SOCKET") {
|
|
4820
|
+
return new CopilotKitLowLevelError7({
|
|
4387
4821
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
4388
4822
|
url: "event streaming connection",
|
|
4389
|
-
message:
|
|
4390
|
-
});
|
|
4391
|
-
}
|
|
4392
|
-
if (((_h = error == null ? void 0 : error.message) == null ? void 0 : _h.includes("aborted")) || ((_i = error == null ? void 0 : error.message) == null ? void 0 : _i.includes("canceled")) || ((_j = error == null ? void 0 : error.message) == null ? void 0 : _j.includes("signal is aborted"))) {
|
|
4393
|
-
return new CopilotKitError6({
|
|
4394
|
-
message: "Request was cancelled",
|
|
4395
|
-
code: CopilotKitErrorCode4.UNKNOWN
|
|
4396
|
-
});
|
|
4397
|
-
}
|
|
4398
|
-
const errorMessage = (error == null ? void 0 : error.message) || String(error);
|
|
4399
|
-
if (errorMessage.includes("401") || errorMessage.toLowerCase().includes("api key") || errorMessage.toLowerCase().includes("unauthorized") || errorMessage.toLowerCase().includes("authentication") || errorMessage.toLowerCase().includes("incorrect api key")) {
|
|
4400
|
-
return new CopilotKitError6({
|
|
4401
|
-
message: `Event streaming error: ${errorMessage}`,
|
|
4402
|
-
code: CopilotKitErrorCode4.MISSING_PUBLIC_API_KEY_ERROR
|
|
4823
|
+
message: helpfulMessage
|
|
4403
4824
|
});
|
|
4404
4825
|
}
|
|
4405
4826
|
return new CopilotKitError6({
|
|
4406
|
-
message:
|
|
4407
|
-
code: CopilotKitErrorCode4.UNKNOWN
|
|
4827
|
+
message: helpfulMessage,
|
|
4828
|
+
code: CopilotKitErrorCode4.UNKNOWN,
|
|
4829
|
+
severity: Severity.CRITICAL
|
|
4408
4830
|
});
|
|
4409
4831
|
}
|
|
4410
4832
|
__name(convertStreamingErrorToStructured2, "convertStreamingErrorToStructured");
|
|
@@ -4413,7 +4835,7 @@ __name(convertStreamingErrorToStructured2, "convertStreamingErrorToStructured");
|
|
|
4413
4835
|
import { GraphQLJSONObject } from "graphql-scalars";
|
|
4414
4836
|
import { plainToInstance as plainToInstance3 } from "class-transformer";
|
|
4415
4837
|
import { GraphQLError } from "graphql";
|
|
4416
|
-
import { randomId as
|
|
4838
|
+
import { randomId as randomId3 } from "@copilotkit/shared";
|
|
4417
4839
|
|
|
4418
4840
|
// src/graphql/types/agents-response.type.ts
|
|
4419
4841
|
import { Field as Field16, ObjectType as ObjectType5 } from "type-graphql";
|
|
@@ -4469,7 +4891,7 @@ AgentsResponse = _ts_decorate16([
|
|
|
4469
4891
|
], AgentsResponse);
|
|
4470
4892
|
|
|
4471
4893
|
// src/graphql/resolvers/copilot.resolver.ts
|
|
4472
|
-
import { CopilotKitError as CopilotKitError7 } from "@copilotkit/shared";
|
|
4894
|
+
import { CopilotKitError as CopilotKitError7, CopilotKitLowLevelError as CopilotKitLowLevelError8, isStructuredCopilotKitError as isStructuredCopilotKitError2 } from "@copilotkit/shared";
|
|
4473
4895
|
function _ts_decorate17(decorators, target, key, desc) {
|
|
4474
4896
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4475
4897
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -4539,7 +4961,7 @@ var CopilotResolver = class {
|
|
|
4539
4961
|
};
|
|
4540
4962
|
}
|
|
4541
4963
|
async generateCopilotResponse(ctx, data, properties) {
|
|
4542
|
-
var _a, _b, _c, _d;
|
|
4964
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
4543
4965
|
telemetry_client_default.capture("oss.runtime.copilot_request_created", {
|
|
4544
4966
|
"cloud.guardrails.enabled": ((_a = data.cloud) == null ? void 0 : _a.guardrails) !== void 0,
|
|
4545
4967
|
requestType: data.metadata.requestType,
|
|
@@ -4570,23 +4992,32 @@ var CopilotResolver = class {
|
|
|
4570
4992
|
const serviceAdapter = ctx._copilotkit.serviceAdapter;
|
|
4571
4993
|
let copilotCloudPublicApiKey = null;
|
|
4572
4994
|
let copilotCloudBaseUrl;
|
|
4995
|
+
const publicApiKeyFromHeaders = ctx.request.headers.get("x-copilotcloud-public-api-key");
|
|
4996
|
+
if (publicApiKeyFromHeaders) {
|
|
4997
|
+
copilotCloudPublicApiKey = publicApiKeyFromHeaders;
|
|
4998
|
+
}
|
|
4573
4999
|
if (data.cloud) {
|
|
4574
5000
|
logger2 = logger2.child({
|
|
4575
5001
|
cloud: true
|
|
4576
5002
|
});
|
|
4577
5003
|
logger2.debug("Cloud configuration provided, checking for public API key in headers");
|
|
4578
|
-
|
|
4579
|
-
if (key) {
|
|
4580
|
-
logger2.debug("Public API key found in headers");
|
|
4581
|
-
copilotCloudPublicApiKey = key;
|
|
4582
|
-
} else {
|
|
5004
|
+
if (!copilotCloudPublicApiKey) {
|
|
4583
5005
|
logger2.error("Public API key not found in headers");
|
|
5006
|
+
await copilotRuntime.traceGraphQLError({
|
|
5007
|
+
message: "X-CopilotCloud-Public-API-Key header is required",
|
|
5008
|
+
code: "MISSING_PUBLIC_API_KEY",
|
|
5009
|
+
type: "GraphQLError"
|
|
5010
|
+
}, {
|
|
5011
|
+
operation: "generateCopilotResponse",
|
|
5012
|
+
cloudConfigPresent: Boolean(data.cloud),
|
|
5013
|
+
guardrailsEnabled: Boolean((_b = data.cloud) == null ? void 0 : _b.guardrails)
|
|
5014
|
+
});
|
|
4584
5015
|
throw new GraphQLError("X-CopilotCloud-Public-API-Key header is required");
|
|
4585
5016
|
}
|
|
4586
5017
|
if (process.env.COPILOT_CLOUD_BASE_URL) {
|
|
4587
5018
|
copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
|
|
4588
|
-
} else if ((
|
|
4589
|
-
copilotCloudBaseUrl = (
|
|
5019
|
+
} else if ((_c = ctx._copilotkit.cloud) == null ? void 0 : _c.baseUrl) {
|
|
5020
|
+
copilotCloudBaseUrl = (_d = ctx._copilotkit.cloud) == null ? void 0 : _d.baseUrl;
|
|
4590
5021
|
} else {
|
|
4591
5022
|
copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
|
|
4592
5023
|
}
|
|
@@ -4609,26 +5040,41 @@ var CopilotResolver = class {
|
|
|
4609
5040
|
ctx.properties["copilotCloudPublicApiKey"] = copilotCloudPublicApiKey;
|
|
4610
5041
|
}
|
|
4611
5042
|
logger2.debug("Processing");
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
|
|
4621
|
-
|
|
4622
|
-
|
|
4623
|
-
|
|
4624
|
-
|
|
4625
|
-
|
|
4626
|
-
|
|
4627
|
-
|
|
5043
|
+
let runtimeResponse;
|
|
5044
|
+
try {
|
|
5045
|
+
runtimeResponse = await copilotRuntime.processRuntimeRequest({
|
|
5046
|
+
serviceAdapter,
|
|
5047
|
+
messages: data.messages,
|
|
5048
|
+
actions: data.frontend.actions.filter((action) => action.available !== ActionInputAvailability.disabled),
|
|
5049
|
+
threadId: data.threadId,
|
|
5050
|
+
runId: data.runId,
|
|
5051
|
+
publicApiKey: copilotCloudPublicApiKey,
|
|
5052
|
+
outputMessagesPromise,
|
|
5053
|
+
graphqlContext: ctx,
|
|
5054
|
+
forwardedParameters: data.forwardedParameters,
|
|
5055
|
+
agentSession: data.agentSession,
|
|
5056
|
+
agentStates: data.agentStates,
|
|
5057
|
+
url: data.frontend.url,
|
|
5058
|
+
extensions: data.extensions,
|
|
5059
|
+
metaEvents: data.metaEvents
|
|
5060
|
+
});
|
|
5061
|
+
} catch (error) {
|
|
5062
|
+
if (isStructuredCopilotKitError2(error) || ((_e = error == null ? void 0 : error.extensions) == null ? void 0 : _e.visibility)) {
|
|
5063
|
+
throw new GraphQLError(error.message || "Agent error occurred", {
|
|
5064
|
+
extensions: {
|
|
5065
|
+
...error.extensions,
|
|
5066
|
+
code: error.code || ((_f = error.extensions) == null ? void 0 : _f.code) || "AGENT_ERROR",
|
|
5067
|
+
originalError: error
|
|
5068
|
+
}
|
|
5069
|
+
});
|
|
5070
|
+
}
|
|
5071
|
+
throw error;
|
|
5072
|
+
}
|
|
5073
|
+
const { eventSource, threadId = randomId3(), runId, serverSideActions, actionInputsWithoutAgents, extensions } = runtimeResponse;
|
|
4628
5074
|
logger2.debug("Event source created, creating response");
|
|
4629
5075
|
const eventStream = eventSource.processRuntimeEvents({
|
|
4630
5076
|
serverSideActions,
|
|
4631
|
-
guardrailsResult$: ((
|
|
5077
|
+
guardrailsResult$: ((_g = data.cloud) == null ? void 0 : _g.guardrails) ? guardrailsResult$ : null,
|
|
4632
5078
|
actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
|
|
4633
5079
|
// TODO-AGENTS: do not exclude ALL server side actions
|
|
4634
5080
|
(action) => !serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
|
|
@@ -4709,12 +5155,16 @@ var CopilotResolver = class {
|
|
|
4709
5155
|
}
|
|
4710
5156
|
},
|
|
4711
5157
|
error: (err) => {
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
}
|
|
5158
|
+
var _a2, _b2;
|
|
5159
|
+
if (((_a2 = err == null ? void 0 : err.name) == null ? void 0 : _a2.includes("CopilotKit")) || ((_b2 = err == null ? void 0 : err.extensions) == null ? void 0 : _b2.visibility)) {
|
|
5160
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5161
|
+
description: err.message || "Agent error occurred"
|
|
5162
|
+
}));
|
|
5163
|
+
} else {
|
|
5164
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5165
|
+
description: `An unknown error has occurred in the event stream`
|
|
5166
|
+
}));
|
|
5167
|
+
}
|
|
4718
5168
|
eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
|
|
4719
5169
|
stop();
|
|
4720
5170
|
},
|
|
@@ -4752,7 +5202,7 @@ var CopilotResolver = class {
|
|
|
4752
5202
|
});
|
|
4753
5203
|
outputMessages = [
|
|
4754
5204
|
plainToInstance3(TextMessage, {
|
|
4755
|
-
id:
|
|
5205
|
+
id: randomId3(),
|
|
4756
5206
|
createdAt: /* @__PURE__ */ new Date(),
|
|
4757
5207
|
content: result.reason,
|
|
4758
5208
|
role: MessageRole.assistant
|
|
@@ -4930,7 +5380,7 @@ var CopilotResolver = class {
|
|
|
4930
5380
|
event
|
|
4931
5381
|
}, "Agent message event received");
|
|
4932
5382
|
pushMessage({
|
|
4933
|
-
id:
|
|
5383
|
+
id: randomId3(),
|
|
4934
5384
|
status: new SuccessMessageStatus(),
|
|
4935
5385
|
threadId: event.threadId,
|
|
4936
5386
|
agentName: event.agentName,
|
|
@@ -4943,7 +5393,7 @@ var CopilotResolver = class {
|
|
|
4943
5393
|
createdAt: /* @__PURE__ */ new Date()
|
|
4944
5394
|
});
|
|
4945
5395
|
outputMessages.push(plainToInstance3(AgentStateMessage, {
|
|
4946
|
-
id:
|
|
5396
|
+
id: randomId3(),
|
|
4947
5397
|
threadId: event.threadId,
|
|
4948
5398
|
agentName: event.agentName,
|
|
4949
5399
|
nodeName: event.nodeName,
|
|
@@ -4958,13 +5408,23 @@ var CopilotResolver = class {
|
|
|
4958
5408
|
}
|
|
4959
5409
|
},
|
|
4960
5410
|
error: (err) => {
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
5411
|
+
var _a3, _b2, _c2, _d2, _e2, _f2;
|
|
5412
|
+
if (err instanceof CopilotKitError7 || err instanceof CopilotKitLowLevelError8 || err instanceof Error && err.name && err.name.includes("CopilotKit") || ((_a3 = err == null ? void 0 : err.extensions) == null ? void 0 : _a3.visibility)) {
|
|
5413
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5414
|
+
description: err.message || "Agent error occurred",
|
|
5415
|
+
// Include original error information for frontend to extract
|
|
5416
|
+
originalError: {
|
|
5417
|
+
code: err.code || ((_b2 = err.extensions) == null ? void 0 : _b2.code),
|
|
5418
|
+
statusCode: err.statusCode || ((_c2 = err.extensions) == null ? void 0 : _c2.statusCode),
|
|
5419
|
+
severity: err.severity || ((_d2 = err.extensions) == null ? void 0 : _d2.severity),
|
|
5420
|
+
visibility: err.visibility || ((_e2 = err.extensions) == null ? void 0 : _e2.visibility),
|
|
5421
|
+
originalErrorType: err.originalErrorType || ((_f2 = err.extensions) == null ? void 0 : _f2.originalErrorType),
|
|
5422
|
+
extensions: err.extensions
|
|
5423
|
+
}
|
|
5424
|
+
}));
|
|
4965
5425
|
eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
|
|
4966
5426
|
rejectOutputMessagesPromise(err);
|
|
4967
|
-
stopStreamingMessages(
|
|
5427
|
+
stopStreamingMessages();
|
|
4968
5428
|
return;
|
|
4969
5429
|
}
|
|
4970
5430
|
responseStatus$.next(new UnknownErrorResponse({
|
|
@@ -5169,9 +5629,9 @@ function _ts_param2(paramIndex, decorator) {
|
|
|
5169
5629
|
__name(_ts_param2, "_ts_param");
|
|
5170
5630
|
var StateResolver = class {
|
|
5171
5631
|
async loadAgentState(ctx, data) {
|
|
5172
|
-
const agents = await ctx._copilotkit.runtime.
|
|
5173
|
-
const
|
|
5174
|
-
if (!
|
|
5632
|
+
const agents = await ctx._copilotkit.runtime.getAllAgents(ctx);
|
|
5633
|
+
const hasAgent = agents.some((agent) => agent.name === data.agentName);
|
|
5634
|
+
if (!hasAgent) {
|
|
5175
5635
|
throw new CopilotKitAgentDiscoveryError2({
|
|
5176
5636
|
agentName: data.agentName,
|
|
5177
5637
|
availableAgents: agents.map((a) => ({
|
|
@@ -5202,6 +5662,7 @@ StateResolver = _ts_decorate20([
|
|
|
5202
5662
|
|
|
5203
5663
|
// src/lib/integrations/shared.ts
|
|
5204
5664
|
var packageJson2 = __toESM(require_package());
|
|
5665
|
+
import { CopilotKitError as CopilotKitError8, CopilotKitErrorCode as CopilotKitErrorCode5 } from "@copilotkit/shared";
|
|
5205
5666
|
var logger = createLogger();
|
|
5206
5667
|
var addCustomHeaderPlugin = {
|
|
5207
5668
|
onResponse({ response }) {
|
|
@@ -5266,6 +5727,13 @@ function getCommonConfig(options) {
|
|
|
5266
5727
|
serviceAdapter: options.serviceAdapter.constructor.name
|
|
5267
5728
|
}
|
|
5268
5729
|
});
|
|
5730
|
+
const userErrorCodes = [
|
|
5731
|
+
CopilotKitErrorCode5.AGENT_NOT_FOUND,
|
|
5732
|
+
CopilotKitErrorCode5.API_NOT_FOUND,
|
|
5733
|
+
CopilotKitErrorCode5.REMOTE_ENDPOINT_NOT_FOUND,
|
|
5734
|
+
CopilotKitErrorCode5.CONFIGURATION_ERROR,
|
|
5735
|
+
CopilotKitErrorCode5.MISSING_PUBLIC_API_KEY_ERROR
|
|
5736
|
+
];
|
|
5269
5737
|
return {
|
|
5270
5738
|
logging: createLogger({
|
|
5271
5739
|
component: "Yoga GraphQL",
|
|
@@ -5276,7 +5744,25 @@ function getCommonConfig(options) {
|
|
|
5276
5744
|
useDeferStream(),
|
|
5277
5745
|
addCustomHeaderPlugin
|
|
5278
5746
|
],
|
|
5279
|
-
context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
|
|
5747
|
+
context: (ctx) => createContext(ctx, options, contextLogger, options.properties),
|
|
5748
|
+
// Suppress logging for user configuration errors
|
|
5749
|
+
maskedErrors: {
|
|
5750
|
+
maskError: (error, message, isDev) => {
|
|
5751
|
+
const originalError = error.originalError || error;
|
|
5752
|
+
const extensions = error.extensions;
|
|
5753
|
+
const errorCode = extensions == null ? void 0 : extensions.code;
|
|
5754
|
+
if (errorCode && userErrorCodes.includes(errorCode)) {
|
|
5755
|
+
console.debug("User configuration error:", error.message);
|
|
5756
|
+
return error;
|
|
5757
|
+
}
|
|
5758
|
+
if (originalError instanceof CopilotKitError8 && userErrorCodes.includes(originalError.code)) {
|
|
5759
|
+
console.debug("User configuration error:", error.message);
|
|
5760
|
+
return error;
|
|
5761
|
+
}
|
|
5762
|
+
console.error("Application error:", error);
|
|
5763
|
+
return error;
|
|
5764
|
+
}
|
|
5765
|
+
}
|
|
5280
5766
|
};
|
|
5281
5767
|
}
|
|
5282
5768
|
__name(getCommonConfig, "getCommonConfig");
|
|
@@ -5325,4 +5811,4 @@ export {
|
|
|
5325
5811
|
getCommonConfig,
|
|
5326
5812
|
copilotRuntimeNodeHttpEndpoint
|
|
5327
5813
|
};
|
|
5328
|
-
//# sourceMappingURL=chunk-
|
|
5814
|
+
//# sourceMappingURL=chunk-NI7RVCMB.mjs.map
|