@copilotkit/runtime 1.9.2-next.8 → 1.9.2-next.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/dist/{chunk-IIXJVVTV.mjs → chunk-4JBKY7XT.mjs} +109 -75
- package/dist/chunk-4JBKY7XT.mjs.map +1 -0
- package/dist/{chunk-JWPSIGSA.mjs → chunk-5YGKE5SN.mjs} +2 -2
- package/dist/{chunk-5SG4WWXH.mjs → chunk-ALZ5H3VD.mjs} +2 -2
- package/dist/chunk-AMUJQ6IR.mjs +50 -0
- package/dist/chunk-AMUJQ6IR.mjs.map +1 -0
- package/dist/{chunk-KYCDL2KX.mjs → chunk-SMDVD4VG.mjs} +2 -2
- package/dist/{chunk-4TLMVLU4.mjs → chunk-UUXRYAB4.mjs} +2 -2
- 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-WIXS6EG7.mjs → chunk-Z5GYTKMD.mjs} +2304 -2034
- package/dist/chunk-Z5GYTKMD.mjs.map +1 -0
- 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 +3280 -2932
- 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 +3 -3
- package/dist/lib/index.js +2828 -2480
- 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 +141 -86
- 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 +141 -86
- 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 +141 -86
- 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 +141 -86
- 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 +5 -4
- package/dist/service-adapters/index.js +179 -104
- 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-941d59dc.d.ts → shared-bd953ebf.d.ts} +8 -4
- 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 +2 -2
- package/src/agents/langgraph/event-source.ts +36 -38
- package/src/agents/langgraph/events.ts +19 -1
- package/src/graphql/resolvers/copilot.resolver.ts +84 -40
- package/src/lib/error-messages.ts +200 -0
- package/src/lib/integrations/shared.ts +43 -0
- package/src/lib/runtime/copilot-runtime.ts +56 -48
- package/src/lib/runtime/remote-action-constructors.ts +28 -3
- package/src/lib/runtime/remote-lg-action.ts +85 -3
- package/src/lib/streaming.ts +125 -36
- package/src/service-adapters/anthropic/anthropic-adapter.ts +3 -4
- 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/dist/chunk-IIXJVVTV.mjs.map +0 -1
- package/dist/chunk-WIXS6EG7.mjs.map +0 -1
- package/dist/{chunk-JWPSIGSA.mjs.map → chunk-5YGKE5SN.mjs.map} +0 -0
- package/dist/{chunk-5SG4WWXH.mjs.map → chunk-ALZ5H3VD.mjs.map} +0 -0
- package/dist/{chunk-KYCDL2KX.mjs.map → chunk-SMDVD4VG.mjs.map} +0 -0
- package/dist/{chunk-4TLMVLU4.mjs.map → chunk-UUXRYAB4.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-4JBKY7XT.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.9",
|
|
44
44
|
sideEffects: false,
|
|
45
45
|
main: "./dist/index.js",
|
|
46
46
|
module: "./dist/index.mjs",
|
|
@@ -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,326 +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
|
|
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");
|
|
1854
1949
|
async function execute(args) {
|
|
1855
1950
|
return new ReadableStream({
|
|
1856
1951
|
async start(controller) {
|
|
@@ -1881,6 +1976,9 @@ async function execute(args) {
|
|
|
1881
1976
|
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
1882
1977
|
});
|
|
1883
1978
|
} else {
|
|
1979
|
+
if (lastError instanceof CopilotKitError2 || lastError instanceof CopilotKitLowLevelError2 || lastError instanceof Error && lastError.name && lastError.name.includes("CopilotKit")) {
|
|
1980
|
+
throw lastError;
|
|
1981
|
+
}
|
|
1884
1982
|
throw new CopilotKitMisuseError({
|
|
1885
1983
|
message: `
|
|
1886
1984
|
The LangGraph client threw unhandled error ${lastError}.
|
|
@@ -2050,7 +2148,30 @@ async function streamEvents(controller, args) {
|
|
|
2050
2148
|
].includes(streamResponseChunk.event))
|
|
2051
2149
|
continue;
|
|
2052
2150
|
if (streamResponseChunk.event === "error") {
|
|
2053
|
-
|
|
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
|
+
});
|
|
2054
2175
|
}
|
|
2055
2176
|
const chunk = streamResponseChunk;
|
|
2056
2177
|
const interruptEvents = chunk.data.__interrupt__;
|
|
@@ -2160,11 +2281,21 @@ async function streamEvents(controller, args) {
|
|
|
2160
2281
|
}));
|
|
2161
2282
|
return Promise.resolve();
|
|
2162
2283
|
} catch (e) {
|
|
2163
|
-
|
|
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
|
+
}
|
|
2164
2292
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_errored", {
|
|
2165
2293
|
...streamInfo,
|
|
2166
2294
|
error: e.message
|
|
2167
2295
|
});
|
|
2296
|
+
if (e instanceof CopilotKitError2 || e instanceof CopilotKitLowLevelError2 || e instanceof Error && e.name && e.name.includes("CopilotKit")) {
|
|
2297
|
+
throw e;
|
|
2298
|
+
}
|
|
2168
2299
|
return Promise.resolve();
|
|
2169
2300
|
}
|
|
2170
2301
|
}
|
|
@@ -2496,857 +2627,704 @@ function emitInterrupt(interruptValue, emit) {
|
|
|
2496
2627
|
}
|
|
2497
2628
|
__name(emitInterrupt, "emitInterrupt");
|
|
2498
2629
|
|
|
2499
|
-
// src/lib/runtime/
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
const
|
|
2506
|
-
const
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
const currentBuffer = buffer.join("");
|
|
2510
|
-
if (currentBuffer.trim().length === 0) {
|
|
2511
|
-
return;
|
|
2512
|
-
}
|
|
2513
|
-
const parts = currentBuffer.split("\n");
|
|
2514
|
-
if (parts.length === 0) {
|
|
2515
|
-
return;
|
|
2516
|
-
}
|
|
2517
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
2518
|
-
buffer = [];
|
|
2519
|
-
if (!lastPartIsComplete) {
|
|
2520
|
-
buffer.push(parts.pop());
|
|
2521
|
-
}
|
|
2522
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
2523
|
-
eventStream$.next(JSON.parse(part));
|
|
2524
|
-
});
|
|
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;
|
|
2525
2640
|
}
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
break;
|
|
2538
|
-
}
|
|
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
|
+
});
|
|
2539
2652
|
}
|
|
2540
|
-
} catch (error) {
|
|
2541
|
-
console.error("Error in stream", error);
|
|
2542
|
-
const structuredError = convertStreamingErrorToStructured(error);
|
|
2543
|
-
eventStream$.error(structuredError);
|
|
2544
|
-
return;
|
|
2545
2653
|
}
|
|
2546
|
-
|
|
2654
|
+
return parameters;
|
|
2547
2655
|
}
|
|
2548
|
-
__name(
|
|
2549
|
-
function
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
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) => {
|
|
2662
|
+
try {
|
|
2663
|
+
const result = await tool.execute({
|
|
2664
|
+
params
|
|
2665
|
+
});
|
|
2666
|
+
return typeof result === "string" ? result : JSON.stringify(result);
|
|
2667
|
+
} catch (error) {
|
|
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
|
|
2568
2680
|
});
|
|
2569
2681
|
}
|
|
2570
|
-
return
|
|
2571
|
-
message: `Streaming error: ${(error == null ? void 0 : error.message) || String(error)}`,
|
|
2572
|
-
code: CopilotKitErrorCode.UNKNOWN
|
|
2573
|
-
});
|
|
2682
|
+
return actions;
|
|
2574
2683
|
}
|
|
2575
|
-
__name(
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
const
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
let config = {};
|
|
2599
|
-
if (agentStates) {
|
|
2600
|
-
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
2601
|
-
if (jsonState) {
|
|
2602
|
-
state = parseJson2(jsonState.state, {});
|
|
2603
|
-
config = parseJson2(jsonState.config, {});
|
|
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
|
+
}
|
|
2604
2707
|
}
|
|
2605
2708
|
}
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
logger: logger2.child({
|
|
2609
|
-
component: "remote-actions.remote-lg-action.streamEvents"
|
|
2610
|
-
}),
|
|
2611
|
-
deploymentUrl: endpoint.deploymentUrl,
|
|
2612
|
-
langsmithApiKey: endpoint.langsmithApiKey,
|
|
2613
|
-
agent,
|
|
2614
|
-
threadId,
|
|
2615
|
-
nodeName,
|
|
2616
|
-
messages: [
|
|
2617
|
-
...messages,
|
|
2618
|
-
...additionalMessages
|
|
2619
|
-
],
|
|
2620
|
-
state,
|
|
2621
|
-
config,
|
|
2622
|
-
properties: graphqlContext.properties,
|
|
2623
|
-
actions: tryMap(actionInputsWithoutAgents, (action) => ({
|
|
2624
|
-
name: action.name,
|
|
2625
|
-
description: action.description,
|
|
2626
|
-
parameters: JSON.parse(action.jsonSchema)
|
|
2627
|
-
})),
|
|
2628
|
-
metaEvents
|
|
2629
|
-
});
|
|
2630
|
-
const eventSource = new RemoteLangGraphEventSource();
|
|
2631
|
-
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
2632
|
-
return eventSource.processLangGraphEvents();
|
|
2633
|
-
} catch (error) {
|
|
2634
|
-
logger2.error({
|
|
2635
|
-
url: endpoint.deploymentUrl,
|
|
2636
|
-
status: 500,
|
|
2637
|
-
body: error.message
|
|
2638
|
-
}, "Failed to execute LangGraph Platform agent");
|
|
2639
|
-
throw new Error("Failed to execute LangGraph Platform agent");
|
|
2640
|
-
}
|
|
2709
|
+
} catch (e) {
|
|
2710
|
+
console.error(`Error parsing schema for tool ${name}:`, e);
|
|
2641
2711
|
}
|
|
2642
|
-
|
|
2643
|
-
|
|
2644
|
-
|
|
2645
|
-
|
|
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`;
|
|
2646
2724
|
}
|
|
2647
|
-
__name(
|
|
2648
|
-
|
|
2649
|
-
|
|
2650
|
-
|
|
2651
|
-
|
|
2652
|
-
|
|
2653
|
-
|
|
2654
|
-
|
|
2655
|
-
|
|
2656
|
-
|
|
2657
|
-
|
|
2658
|
-
|
|
2659
|
-
|
|
2660
|
-
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
2671
|
-
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
headers,
|
|
2675
|
-
body: JSON.stringify({
|
|
2676
|
-
name: action.name,
|
|
2677
|
-
arguments: args,
|
|
2678
|
-
properties: graphqlContext.properties
|
|
2679
|
-
})
|
|
2680
|
-
}, logger2);
|
|
2681
|
-
if (!response.ok) {
|
|
2682
|
-
logger2.error({
|
|
2683
|
-
url,
|
|
2684
|
-
status: response.status,
|
|
2685
|
-
body: await response.text()
|
|
2686
|
-
}, "Failed to execute remote action");
|
|
2687
|
-
if (response.status === 404) {
|
|
2688
|
-
throw new CopilotKitApiDiscoveryError({
|
|
2689
|
-
url: fetchUrl
|
|
2690
|
-
});
|
|
2691
|
-
}
|
|
2692
|
-
throw new ResolvedCopilotKitError({
|
|
2693
|
-
status: response.status,
|
|
2694
|
-
url: fetchUrl,
|
|
2695
|
-
isRemoteEndpoint: true
|
|
2696
|
-
});
|
|
2697
|
-
}
|
|
2698
|
-
const requestResult = await response.json();
|
|
2699
|
-
const result = requestResult["result"];
|
|
2700
|
-
logger2.debug({
|
|
2701
|
-
actionName: action.name,
|
|
2702
|
-
result
|
|
2703
|
-
}, "Executed remote action");
|
|
2704
|
-
return result;
|
|
2705
|
-
} catch (error) {
|
|
2706
|
-
if (error instanceof CopilotKitError3) {
|
|
2707
|
-
throw error;
|
|
2708
|
-
}
|
|
2709
|
-
throw new CopilotKitLowLevelError2({
|
|
2710
|
-
error,
|
|
2711
|
-
url: fetchUrl
|
|
2712
|
-
});
|
|
2713
|
-
}
|
|
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`);
|
|
2714
2752
|
}
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
2722
|
-
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2726
|
-
|
|
2727
|
-
|
|
2728
|
-
|
|
2729
|
-
|
|
2730
|
-
|
|
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`."
|
|
2731
2771
|
});
|
|
2732
|
-
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2752
|
-
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
})),
|
|
2762
|
-
metaEvents
|
|
2763
|
-
})
|
|
2764
|
-
}, logger2);
|
|
2765
|
-
if (!response.ok) {
|
|
2766
|
-
logger2.error({
|
|
2767
|
-
url,
|
|
2768
|
-
status: response.status,
|
|
2769
|
-
body: await response.text()
|
|
2770
|
-
}, "Failed to execute remote agent");
|
|
2771
|
-
if (response.status === 404) {
|
|
2772
|
-
throw new CopilotKitApiDiscoveryError({
|
|
2773
|
-
url: fetchUrl
|
|
2774
|
-
});
|
|
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)
|
|
2775
2801
|
}
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
|
|
2783
|
-
const eventSource = new RemoteLangGraphEventSource();
|
|
2784
|
-
writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
|
|
2785
|
-
return eventSource.processLangGraphEvents();
|
|
2786
|
-
} else if (agent.type === "crewai") {
|
|
2787
|
-
const eventStream$ = new RuntimeEventSubject();
|
|
2788
|
-
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
2789
|
-
return eventStream$;
|
|
2790
|
-
} else {
|
|
2791
|
-
throw new Error("Unsupported agent type");
|
|
2792
|
-
}
|
|
2793
|
-
} catch (error) {
|
|
2794
|
-
if (error instanceof CopilotKitError3) {
|
|
2795
|
-
throw error;
|
|
2796
|
-
}
|
|
2797
|
-
throw new CopilotKitLowLevelError2({
|
|
2798
|
-
error,
|
|
2799
|
-
url: fetchUrl
|
|
2800
|
-
});
|
|
2801
|
-
}
|
|
2802
|
+
} : {},
|
|
2803
|
+
execute: async () => ({})
|
|
2804
|
+
};
|
|
2805
|
+
});
|
|
2806
|
+
const mcpToolInstructions = generateMcpToolInstructions(toolsMap);
|
|
2807
|
+
if (!mcpToolInstructions) {
|
|
2808
|
+
return messages;
|
|
2802
2809
|
}
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
];
|
|
2808
|
-
}
|
|
2809
|
-
__name(constructRemoteActions, "constructRemoteActions");
|
|
2810
|
-
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
2811
|
-
const headers = {
|
|
2812
|
-
"Content-Type": "application/json"
|
|
2813
|
-
};
|
|
2814
|
-
if (onBeforeRequest) {
|
|
2815
|
-
const { headers: additionalHeaders } = onBeforeRequest({
|
|
2816
|
-
ctx: graphqlContext
|
|
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";
|
|
2817
2814
|
});
|
|
2818
|
-
|
|
2819
|
-
|
|
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
|
+
});
|
|
2820
2835
|
}
|
|
2836
|
+
return newMessages;
|
|
2821
2837
|
}
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
|
|
2825
|
-
|
|
2826
|
-
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
agentExecution: true,
|
|
2848
|
-
type: "self-hosted",
|
|
2849
|
-
agentsAmount: 1
|
|
2850
|
-
});
|
|
2851
|
-
let state = {};
|
|
2852
|
-
if (agentStates) {
|
|
2853
|
-
const jsonState = agentStates.find((state2) => state2.agentName === agent.agentId);
|
|
2854
|
-
if (jsonState) {
|
|
2855
|
-
state = parseJson3(jsonState.state, {});
|
|
2856
|
-
}
|
|
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
|
|
2857
2863
|
}
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
parameters: JSON.parse(input.jsonSchema)
|
|
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]
|
|
2864
2869
|
};
|
|
2865
|
-
}
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
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);
|
|
2869
2900
|
}
|
|
2870
|
-
}
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
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
|
|
2874
2931
|
});
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
id: message.id,
|
|
2888
|
-
role: message.role,
|
|
2889
|
-
content: message.content
|
|
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) => {
|
|
2890
2944
|
});
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
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);
|
|
2904
2970
|
}
|
|
2905
|
-
parentMessage.toolCalls.push(toolCall);
|
|
2906
|
-
} else {
|
|
2907
|
-
result.push({
|
|
2908
|
-
id: message.parentMessageId ?? message.id,
|
|
2909
|
-
role: "assistant",
|
|
2910
|
-
toolCalls: [
|
|
2911
|
-
toolCall
|
|
2912
|
-
]
|
|
2913
|
-
});
|
|
2914
2971
|
}
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
(
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
}
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
}
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
|
|
2950
|
-
|
|
2951
|
-
|
|
2952
|
-
|
|
2953
|
-
|
|
2954
|
-
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
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;
|
|
2976
3063
|
}
|
|
2977
|
-
throw new CopilotKitLowLevelError3({
|
|
2978
|
-
error,
|
|
2979
|
-
url: fetchUrl
|
|
2980
|
-
});
|
|
2981
3064
|
}
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
return value;
|
|
2994
|
-
}
|
|
2995
|
-
return index === self.findIndex((t) => t.url === value.url);
|
|
2996
|
-
});
|
|
2997
|
-
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
2998
|
-
if (endpoint.type === "langgraph-platform") {
|
|
2999
|
-
return constructLGCRemoteAction({
|
|
3000
|
-
endpoint,
|
|
3001
|
-
messages,
|
|
3002
|
-
graphqlContext,
|
|
3003
|
-
logger: logger2.child({
|
|
3004
|
-
component: "remote-actions.constructLGCRemoteAction",
|
|
3005
|
-
endpoint
|
|
3006
|
-
}),
|
|
3007
|
-
agentStates
|
|
3008
|
-
});
|
|
3009
|
-
}
|
|
3010
|
-
const json = await fetchRemoteInfo({
|
|
3011
|
-
url: endpoint.url,
|
|
3012
|
-
onBeforeRequest: endpoint.onBeforeRequest,
|
|
3013
|
-
graphqlContext,
|
|
3014
|
-
logger: logger2.child({
|
|
3015
|
-
component: "remote-actions.fetchActionsFromUrl",
|
|
3016
|
-
endpoint
|
|
3017
|
-
}),
|
|
3018
|
-
frontendUrl
|
|
3019
|
-
});
|
|
3020
|
-
return constructRemoteActions({
|
|
3021
|
-
json,
|
|
3022
|
-
messages,
|
|
3023
|
-
url: endpoint.url,
|
|
3024
|
-
onBeforeRequest: endpoint.onBeforeRequest,
|
|
3025
|
-
graphqlContext,
|
|
3026
|
-
logger: logger2.child({
|
|
3027
|
-
component: "remote-actions.constructActions",
|
|
3028
|
-
endpoint
|
|
3029
|
-
}),
|
|
3030
|
-
agentStates
|
|
3031
|
-
});
|
|
3032
|
-
}));
|
|
3033
|
-
for (const [key, agent] of Object.entries(agents)) {
|
|
3034
|
-
if (agent.agentId !== void 0 && agent.agentId !== key) {
|
|
3035
|
-
throw new CopilotKitError4({
|
|
3036
|
-
message: `Agent ${key} has agentId ${agent.agentId} which does not match the key ${key}`,
|
|
3037
|
-
code: CopilotKitErrorCode2.UNKNOWN
|
|
3038
|
-
});
|
|
3039
|
-
} else if (agent.agentId === void 0) {
|
|
3040
|
-
agent.agentId = key;
|
|
3041
|
-
}
|
|
3042
|
-
result.push(constructAGUIRemoteAction({
|
|
3043
|
-
logger: logger2,
|
|
3044
|
-
messages,
|
|
3045
|
-
agentStates,
|
|
3046
|
-
agent,
|
|
3047
|
-
metaEvents
|
|
3065
|
+
async getAllAgents(graphqlContext) {
|
|
3066
|
+
const [agentsWithEndpoints, aguiAgents] = await Promise.all([
|
|
3067
|
+
this.discoverAgentsFromEndpoints(graphqlContext),
|
|
3068
|
+
this.discoverAgentsFromAgui()
|
|
3069
|
+
]);
|
|
3070
|
+
this.availableAgents = [
|
|
3071
|
+
...agentsWithEndpoints,
|
|
3072
|
+
...aguiAgents
|
|
3073
|
+
].map((a) => ({
|
|
3074
|
+
name: a.name,
|
|
3075
|
+
id: a.id
|
|
3048
3076
|
}));
|
|
3077
|
+
return [
|
|
3078
|
+
...agentsWithEndpoints,
|
|
3079
|
+
...aguiAgents
|
|
3080
|
+
];
|
|
3049
3081
|
}
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
actionExecutionId: message.resultMessage.actionExecutionId,
|
|
3095
|
-
actionName: message.resultMessage.actionName,
|
|
3096
|
-
result: message.resultMessage.result
|
|
3097
|
-
});
|
|
3098
|
-
} else if (message.agentStateMessage) {
|
|
3099
|
-
return plainToInstance(AgentStateMessage, {
|
|
3100
|
-
id: message.id,
|
|
3101
|
-
threadId: message.agentStateMessage.threadId,
|
|
3102
|
-
createdAt: message.createdAt,
|
|
3103
|
-
agentName: message.agentStateMessage.agentName,
|
|
3104
|
-
nodeName: message.agentStateMessage.nodeName,
|
|
3105
|
-
runId: message.agentStateMessage.runId,
|
|
3106
|
-
active: message.agentStateMessage.active,
|
|
3107
|
-
role: message.agentStateMessage.role,
|
|
3108
|
-
state: JSON.parse(message.agentStateMessage.state),
|
|
3109
|
-
running: message.agentStateMessage.running
|
|
3110
|
-
});
|
|
3111
|
-
} else {
|
|
3112
|
-
return null;
|
|
3113
|
-
}
|
|
3114
|
-
});
|
|
3115
|
-
return messages.filter((m) => m);
|
|
3116
|
-
}
|
|
3117
|
-
__name(convertGqlInputToMessages, "convertGqlInputToMessages");
|
|
3118
|
-
|
|
3119
|
-
// src/lib/runtime/copilot-runtime.ts
|
|
3120
|
-
import { from } from "rxjs";
|
|
3121
|
-
import { Client as LangGraphClient2 } from "@langchain/langgraph-sdk";
|
|
3122
|
-
|
|
3123
|
-
// src/lib/runtime/mcp-tools-utils.ts
|
|
3124
|
-
function extractParametersFromSchema(toolOrSchema) {
|
|
3125
|
-
var _a;
|
|
3126
|
-
const parameters = [];
|
|
3127
|
-
const schema = "schema" in (toolOrSchema || {}) ? toolOrSchema.schema : toolOrSchema;
|
|
3128
|
-
const toolParameters = (schema == null ? void 0 : schema.parameters) || ((_a = schema == null ? void 0 : schema.parameters) == null ? void 0 : _a.jsonSchema);
|
|
3129
|
-
const properties = toolParameters == null ? void 0 : toolParameters.properties;
|
|
3130
|
-
const requiredParams = new Set((toolParameters == null ? void 0 : toolParameters.required) || []);
|
|
3131
|
-
if (!properties) {
|
|
3132
|
-
return parameters;
|
|
3133
|
-
}
|
|
3134
|
-
for (const paramName in properties) {
|
|
3135
|
-
if (Object.prototype.hasOwnProperty.call(properties, paramName)) {
|
|
3136
|
-
const paramDef = properties[paramName];
|
|
3137
|
-
parameters.push({
|
|
3138
|
-
name: paramName,
|
|
3139
|
-
// Infer type, default to string. MCP schemas might have more complex types.
|
|
3140
|
-
// This might need refinement based on common MCP schema practices.
|
|
3141
|
-
type: paramDef.type || "string",
|
|
3142
|
-
description: paramDef.description,
|
|
3143
|
-
required: requiredParams.has(paramName)
|
|
3144
|
-
});
|
|
3145
|
-
}
|
|
3146
|
-
}
|
|
3147
|
-
return parameters;
|
|
3148
|
-
}
|
|
3149
|
-
__name(extractParametersFromSchema, "extractParametersFromSchema");
|
|
3150
|
-
function convertMCPToolsToActions(mcpTools, mcpEndpoint) {
|
|
3151
|
-
const actions = [];
|
|
3152
|
-
for (const [toolName, tool] of Object.entries(mcpTools)) {
|
|
3153
|
-
const parameters = extractParametersFromSchema(tool);
|
|
3154
|
-
const handler = /* @__PURE__ */ __name(async (params) => {
|
|
3082
|
+
async discoverAgentsFromEndpoints(graphqlContext) {
|
|
3083
|
+
const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
|
|
3084
|
+
const agents2 = await acc;
|
|
3085
|
+
if (endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3086
|
+
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3087
|
+
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3088
|
+
} : null;
|
|
3089
|
+
const client = new LangGraphClient2({
|
|
3090
|
+
apiUrl: endpoint.deploymentUrl,
|
|
3091
|
+
apiKey: endpoint.langsmithApiKey,
|
|
3092
|
+
defaultHeaders: {
|
|
3093
|
+
...propertyHeaders
|
|
3094
|
+
}
|
|
3095
|
+
});
|
|
3096
|
+
let data = [];
|
|
3097
|
+
try {
|
|
3098
|
+
data = await client.assistants.search();
|
|
3099
|
+
if (data && "detail" in data && data.detail.toLowerCase() === "not found") {
|
|
3100
|
+
throw new CopilotKitAgentDiscoveryError({
|
|
3101
|
+
availableAgents: this.availableAgents
|
|
3102
|
+
});
|
|
3103
|
+
}
|
|
3104
|
+
} catch (e) {
|
|
3105
|
+
throw new CopilotKitMisuseError2({
|
|
3106
|
+
message: `
|
|
3107
|
+
Failed to find or contact remote endpoint at url ${endpoint.deploymentUrl}.
|
|
3108
|
+
Make sure the API is running and that it's indeed a LangGraph platform url.
|
|
3109
|
+
|
|
3110
|
+
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
3111
|
+
});
|
|
3112
|
+
}
|
|
3113
|
+
const endpointAgents = data.map((entry) => ({
|
|
3114
|
+
name: entry.graph_id,
|
|
3115
|
+
id: entry.assistant_id,
|
|
3116
|
+
description: "",
|
|
3117
|
+
endpoint
|
|
3118
|
+
}));
|
|
3119
|
+
return [
|
|
3120
|
+
...agents2,
|
|
3121
|
+
...endpointAgents
|
|
3122
|
+
];
|
|
3123
|
+
}
|
|
3124
|
+
const cpkEndpoint = endpoint;
|
|
3125
|
+
const fetchUrl = `${endpoint.url}/info`;
|
|
3155
3126
|
try {
|
|
3156
|
-
const
|
|
3157
|
-
|
|
3127
|
+
const response = await fetchWithRetry(fetchUrl, {
|
|
3128
|
+
method: "POST",
|
|
3129
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
3130
|
+
body: JSON.stringify({
|
|
3131
|
+
properties: graphqlContext.properties
|
|
3132
|
+
})
|
|
3158
3133
|
});
|
|
3159
|
-
|
|
3134
|
+
if (!response.ok) {
|
|
3135
|
+
if (response.status === 404) {
|
|
3136
|
+
throw new CopilotKitApiDiscoveryError({
|
|
3137
|
+
url: fetchUrl
|
|
3138
|
+
});
|
|
3139
|
+
}
|
|
3140
|
+
throw new ResolvedCopilotKitError({
|
|
3141
|
+
status: response.status,
|
|
3142
|
+
url: fetchUrl,
|
|
3143
|
+
isRemoteEndpoint: true
|
|
3144
|
+
});
|
|
3145
|
+
}
|
|
3146
|
+
const data = await response.json();
|
|
3147
|
+
const endpointAgents = ((data == null ? void 0 : data.agents) ?? []).map((agent) => ({
|
|
3148
|
+
name: agent.name,
|
|
3149
|
+
description: agent.description ?? "" ?? "",
|
|
3150
|
+
id: randomId(),
|
|
3151
|
+
endpoint
|
|
3152
|
+
}));
|
|
3153
|
+
return [
|
|
3154
|
+
...agents2,
|
|
3155
|
+
...endpointAgents
|
|
3156
|
+
];
|
|
3160
3157
|
} catch (error) {
|
|
3161
|
-
|
|
3162
|
-
|
|
3158
|
+
if (error instanceof CopilotKitError3) {
|
|
3159
|
+
throw error;
|
|
3160
|
+
}
|
|
3161
|
+
throw new CopilotKitLowLevelError3({
|
|
3162
|
+
error,
|
|
3163
|
+
url: fetchUrl
|
|
3164
|
+
});
|
|
3163
3165
|
}
|
|
3164
|
-
},
|
|
3165
|
-
|
|
3166
|
-
name: toolName,
|
|
3167
|
-
description: tool.description || `MCP tool: ${toolName} (from ${mcpEndpoint})`,
|
|
3168
|
-
parameters,
|
|
3169
|
-
handler,
|
|
3170
|
-
// Add metadata for easier identification/debugging
|
|
3171
|
-
_isMCPTool: true,
|
|
3172
|
-
_mcpEndpoint: mcpEndpoint
|
|
3173
|
-
});
|
|
3174
|
-
}
|
|
3175
|
-
return actions;
|
|
3176
|
-
}
|
|
3177
|
-
__name(convertMCPToolsToActions, "convertMCPToolsToActions");
|
|
3178
|
-
function generateMcpToolInstructions(toolsMap) {
|
|
3179
|
-
if (!toolsMap || Object.keys(toolsMap).length === 0) {
|
|
3180
|
-
return "";
|
|
3166
|
+
}, Promise.resolve([]));
|
|
3167
|
+
return agents;
|
|
3181
3168
|
}
|
|
3182
|
-
|
|
3183
|
-
|
|
3184
|
-
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
const requiredMark = requiredParams.includes(paramName) ? "*" : "";
|
|
3193
|
-
const typeInfo = propDetails.type || "any";
|
|
3194
|
-
const description = propDetails.description ? ` - ${propDetails.description}` : "";
|
|
3195
|
-
return ` - ${paramName}${requiredMark} (${typeInfo})${description}`;
|
|
3169
|
+
async discoverAgentsFromAgui() {
|
|
3170
|
+
const agents = Object.values(this.agents ?? []).reduce(async (acc, agent) => {
|
|
3171
|
+
const agents2 = await acc;
|
|
3172
|
+
const client = agent.client;
|
|
3173
|
+
let data = [];
|
|
3174
|
+
try {
|
|
3175
|
+
data = await client.assistants.search();
|
|
3176
|
+
if (data && "detail" in data && data.detail.toLowerCase() === "not found") {
|
|
3177
|
+
throw new CopilotKitAgentDiscoveryError({
|
|
3178
|
+
availableAgents: this.availableAgents
|
|
3196
3179
|
});
|
|
3197
|
-
if (paramsList.length > 0) {
|
|
3198
|
-
paramsDoc = paramsList.join("\n");
|
|
3199
|
-
}
|
|
3200
3180
|
}
|
|
3181
|
+
} catch (e) {
|
|
3182
|
+
throw new CopilotKitMisuseError2({
|
|
3183
|
+
message: `
|
|
3184
|
+
Failed to find or contact agent ${agent.graphId}.
|
|
3185
|
+
Make sure the LangGraph API is running and the agent is defined in langgraph.json
|
|
3186
|
+
|
|
3187
|
+
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
3188
|
+
});
|
|
3201
3189
|
}
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
1. Only provide valid parameters according to their type requirements
|
|
3214
|
-
2. Required parameters are marked with *
|
|
3215
|
-
3. Format API calls correctly with the expected parameter structure
|
|
3216
|
-
4. Always check tool responses to determine your next action`;
|
|
3217
|
-
}
|
|
3218
|
-
__name(generateMcpToolInstructions, "generateMcpToolInstructions");
|
|
3219
|
-
|
|
3220
|
-
// src/lib/runtime/copilot-runtime.ts
|
|
3221
|
-
var CopilotRuntime = class {
|
|
3222
|
-
actions;
|
|
3223
|
-
agents;
|
|
3224
|
-
remoteEndpointDefinitions;
|
|
3225
|
-
langserve = [];
|
|
3226
|
-
onBeforeRequest;
|
|
3227
|
-
onAfterRequest;
|
|
3228
|
-
delegateAgentProcessingToServiceAdapter;
|
|
3229
|
-
observability;
|
|
3230
|
-
availableAgents;
|
|
3231
|
-
onTrace;
|
|
3232
|
-
hasWarnedAboutTracing = false;
|
|
3233
|
-
// +++ MCP Properties +++
|
|
3234
|
-
mcpServersConfig;
|
|
3235
|
-
mcpActionCache = /* @__PURE__ */ new Map();
|
|
3236
|
-
// --- MCP Properties ---
|
|
3237
|
-
// +++ MCP Client Factory +++
|
|
3238
|
-
createMCPClientImpl;
|
|
3239
|
-
// --- MCP Client Factory ---
|
|
3240
|
-
constructor(params) {
|
|
3241
|
-
var _a, _b, _c, _d;
|
|
3242
|
-
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))) {
|
|
3243
|
-
console.warn("Actions set in runtime instance will not be available for the agent");
|
|
3244
|
-
console.warn(`LangGraph Platform remote endpoints are deprecated in favor of the "agents" property`);
|
|
3245
|
-
}
|
|
3246
|
-
this.actions = (params == null ? void 0 : params.actions) || [];
|
|
3247
|
-
this.availableAgents = [];
|
|
3248
|
-
for (const chain of (params == null ? void 0 : params.langserve) || []) {
|
|
3249
|
-
const remoteChain = new RemoteChain(chain);
|
|
3250
|
-
this.langserve.push(remoteChain.toAction());
|
|
3251
|
-
}
|
|
3252
|
-
this.remoteEndpointDefinitions = (params == null ? void 0 : params.remoteEndpoints) ?? (params == null ? void 0 : params.remoteActions) ?? [];
|
|
3253
|
-
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
3254
|
-
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
3255
|
-
this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
|
|
3256
|
-
this.observability = params == null ? void 0 : params.observability_c;
|
|
3257
|
-
this.agents = (params == null ? void 0 : params.agents) ?? {};
|
|
3258
|
-
this.onTrace = params == null ? void 0 : params.onTrace;
|
|
3259
|
-
this.mcpServersConfig = params == null ? void 0 : params.mcpServers;
|
|
3260
|
-
this.createMCPClientImpl = params == null ? void 0 : params.createMCPClient;
|
|
3261
|
-
if (this.mcpServersConfig && this.mcpServersConfig.length > 0 && !this.createMCPClientImpl) {
|
|
3262
|
-
throw new CopilotKitMisuseError2({
|
|
3263
|
-
message: "MCP Integration Error: `mcpServers` were provided, but the `createMCPClient` function was not passed to the CopilotRuntime constructor. Please provide an implementation for `createMCPClient`."
|
|
3264
|
-
});
|
|
3265
|
-
}
|
|
3266
|
-
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))) {
|
|
3267
|
-
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.");
|
|
3268
|
-
}
|
|
3190
|
+
const endpointAgents = data.map((entry) => ({
|
|
3191
|
+
name: entry.graph_id,
|
|
3192
|
+
id: entry.assistant_id,
|
|
3193
|
+
description: ""
|
|
3194
|
+
}));
|
|
3195
|
+
return [
|
|
3196
|
+
...agents2,
|
|
3197
|
+
...endpointAgents
|
|
3198
|
+
];
|
|
3199
|
+
}, Promise.resolve([]));
|
|
3200
|
+
return agents;
|
|
3269
3201
|
}
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
const
|
|
3273
|
-
if (!
|
|
3274
|
-
|
|
3202
|
+
async loadAgentState(graphqlContext, threadId, agentName) {
|
|
3203
|
+
const agents = await this.getAllAgents(graphqlContext);
|
|
3204
|
+
const agent = agents.find((agent2) => agent2.name === agentName);
|
|
3205
|
+
if (!agent) {
|
|
3206
|
+
throw new Error("Agent not found");
|
|
3275
3207
|
}
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
|
|
3208
|
+
if ("endpoint" in agent && (agent.endpoint.type === EndpointType.CopilotKit || !("type" in agent.endpoint))) {
|
|
3209
|
+
const cpkEndpoint = agent.endpoint;
|
|
3210
|
+
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
3211
|
+
try {
|
|
3212
|
+
const response = await fetchWithRetry(fetchUrl, {
|
|
3213
|
+
method: "POST",
|
|
3214
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
3215
|
+
body: JSON.stringify({
|
|
3216
|
+
properties: graphqlContext.properties,
|
|
3217
|
+
threadId,
|
|
3218
|
+
name: agentName
|
|
3219
|
+
})
|
|
3220
|
+
});
|
|
3221
|
+
if (!response.ok) {
|
|
3222
|
+
if (response.status === 404) {
|
|
3223
|
+
throw new CopilotKitApiDiscoveryError({
|
|
3224
|
+
url: fetchUrl
|
|
3225
|
+
});
|
|
3294
3226
|
}
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
3311
|
-
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3227
|
+
let errorMessage = `HTTP ${response.status} error`;
|
|
3228
|
+
try {
|
|
3229
|
+
const errorBody = await response.text();
|
|
3230
|
+
const parsedError = JSON.parse(errorBody);
|
|
3231
|
+
if (parsedError.error && typeof parsedError.error === "string") {
|
|
3232
|
+
errorMessage = parsedError.error;
|
|
3233
|
+
}
|
|
3234
|
+
} catch {
|
|
3235
|
+
}
|
|
3236
|
+
throw new ResolvedCopilotKitError({
|
|
3237
|
+
status: response.status,
|
|
3238
|
+
url: fetchUrl,
|
|
3239
|
+
isRemoteEndpoint: true,
|
|
3240
|
+
message: errorMessage
|
|
3241
|
+
});
|
|
3242
|
+
}
|
|
3243
|
+
const data = await response.json();
|
|
3244
|
+
return {
|
|
3245
|
+
...data,
|
|
3246
|
+
state: JSON.stringify(data.state),
|
|
3247
|
+
messages: JSON.stringify(data.messages)
|
|
3248
|
+
};
|
|
3249
|
+
} catch (error) {
|
|
3250
|
+
if (error instanceof CopilotKitError3) {
|
|
3251
|
+
throw error;
|
|
3252
|
+
}
|
|
3253
|
+
throw new CopilotKitLowLevelError3({
|
|
3254
|
+
error,
|
|
3255
|
+
url: fetchUrl
|
|
3256
|
+
});
|
|
3315
3257
|
}
|
|
3258
|
+
}
|
|
3259
|
+
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3260
|
+
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3261
|
+
} : null;
|
|
3262
|
+
let client;
|
|
3263
|
+
if ("endpoint" in agent && agent.endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3264
|
+
client = new LangGraphClient2({
|
|
3265
|
+
apiUrl: agent.endpoint.deploymentUrl,
|
|
3266
|
+
apiKey: agent.endpoint.langsmithApiKey,
|
|
3267
|
+
defaultHeaders: {
|
|
3268
|
+
...propertyHeaders
|
|
3269
|
+
}
|
|
3270
|
+
});
|
|
3316
3271
|
} else {
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3272
|
+
const aguiAgent = graphqlContext._copilotkit.runtime.agents[agent.name];
|
|
3273
|
+
if (!aguiAgent) {
|
|
3274
|
+
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
3275
|
+
}
|
|
3276
|
+
client = aguiAgent.client;
|
|
3277
|
+
}
|
|
3278
|
+
let state = {};
|
|
3279
|
+
try {
|
|
3280
|
+
state = (await client.threads.getState(threadId)).values;
|
|
3281
|
+
} catch (error) {
|
|
3282
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
3283
|
+
console.debug(`Agent '${agentName}' configuration issue: ${errorMessage}`);
|
|
3284
|
+
throw new ResolvedCopilotKitError({
|
|
3285
|
+
status: 400,
|
|
3286
|
+
message: `Agent '${agentName}' failed to execute: ${errorMessage}`,
|
|
3287
|
+
code: CopilotKitErrorCode2.CONFIGURATION_ERROR
|
|
3327
3288
|
});
|
|
3328
3289
|
}
|
|
3329
|
-
|
|
3290
|
+
if (Object.keys(state).length === 0) {
|
|
3291
|
+
return {
|
|
3292
|
+
threadId: threadId || "",
|
|
3293
|
+
threadExists: false,
|
|
3294
|
+
state: JSON.stringify({}),
|
|
3295
|
+
messages: JSON.stringify([])
|
|
3296
|
+
};
|
|
3297
|
+
} else {
|
|
3298
|
+
const { messages, ...stateWithoutMessages } = state;
|
|
3299
|
+
const copilotkitMessages = langchainMessagesToCopilotKit(messages);
|
|
3300
|
+
return {
|
|
3301
|
+
threadId: threadId || "",
|
|
3302
|
+
threadExists: true,
|
|
3303
|
+
state: JSON.stringify(stateWithoutMessages),
|
|
3304
|
+
messages: JSON.stringify(copilotkitMessages)
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
3330
3308
|
}
|
|
3331
|
-
async
|
|
3309
|
+
async processAgentRequest(request) {
|
|
3332
3310
|
var _a, _b, _c, _d, _e;
|
|
3333
|
-
const {
|
|
3334
|
-
const
|
|
3311
|
+
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession, threadId: threadIdFromRequest, metaEvents, publicApiKey, forwardedParameters } = request;
|
|
3312
|
+
const { agentName, nodeName } = agentSession;
|
|
3335
3313
|
const requestStartTime = Date.now();
|
|
3336
3314
|
const streamedChunks = [];
|
|
3337
|
-
|
|
3315
|
+
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
3316
|
+
await this.trace("agent_state", {
|
|
3338
3317
|
threadId,
|
|
3339
|
-
|
|
3340
|
-
source: "runtime",
|
|
3318
|
+
source: "agent",
|
|
3341
3319
|
request: {
|
|
3342
|
-
operation: "
|
|
3320
|
+
operation: "processAgentRequest",
|
|
3343
3321
|
method: "POST",
|
|
3344
|
-
url,
|
|
3345
3322
|
startTime: requestStartTime
|
|
3346
3323
|
},
|
|
3347
|
-
agent:
|
|
3348
|
-
name:
|
|
3349
|
-
|
|
3324
|
+
agent: {
|
|
3325
|
+
name: agentName,
|
|
3326
|
+
nodeName
|
|
3327
|
+
},
|
|
3350
3328
|
messages: {
|
|
3351
3329
|
input: rawMessages,
|
|
3352
3330
|
messageCount: rawMessages.length
|
|
@@ -3355,114 +3333,63 @@ var CopilotRuntime = class {
|
|
|
3355
3333
|
environment: process.env.NODE_ENV
|
|
3356
3334
|
}
|
|
3357
3335
|
}, void 0, publicApiKey);
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3336
|
+
const serverSideActions = await this.getServerSideActions(request);
|
|
3337
|
+
const messages = convertGqlInputToMessages(rawMessages);
|
|
3338
|
+
const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
|
|
3339
|
+
if (!currentAgent) {
|
|
3340
|
+
throw new CopilotKitAgentDiscoveryError({
|
|
3341
|
+
agentName,
|
|
3342
|
+
availableAgents: this.availableAgents
|
|
3343
|
+
});
|
|
3344
|
+
}
|
|
3345
|
+
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
3346
|
+
// Case 1: Keep all regular (non-agent) actions
|
|
3347
|
+
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
3348
|
+
isRemoteAgentAction(action) && action.name !== agentName
|
|
3349
|
+
)).map((action) => ({
|
|
3350
|
+
name: action.name,
|
|
3351
|
+
description: action.description,
|
|
3352
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3353
|
+
}));
|
|
3354
|
+
const allAvailableActions = flattenToolCallsNoDuplicates([
|
|
3355
|
+
...availableActionsForCurrentAgent,
|
|
3356
|
+
...request.actions
|
|
3357
|
+
]);
|
|
3358
|
+
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
3359
|
+
try {
|
|
3360
|
+
const requestData = {
|
|
3361
|
+
threadId,
|
|
3362
|
+
runId: void 0,
|
|
3363
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3364
|
+
messages,
|
|
3365
|
+
actions: allAvailableActions,
|
|
3366
|
+
forwardedParameters,
|
|
3367
|
+
timestamp: requestStartTime,
|
|
3368
|
+
provider: "agent",
|
|
3369
|
+
agentName,
|
|
3370
|
+
nodeName
|
|
3362
3371
|
};
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
}
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
const
|
|
3376
|
-
const
|
|
3377
|
-
|
|
3378
|
-
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
3379
|
-
try {
|
|
3380
|
-
const requestData = {
|
|
3381
|
-
threadId,
|
|
3382
|
-
runId,
|
|
3383
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3384
|
-
messages: inputMessages,
|
|
3385
|
-
actions: clientSideActionsInput,
|
|
3386
|
-
forwardedParameters,
|
|
3387
|
-
timestamp: requestStartTime,
|
|
3388
|
-
provider: this.detectProvider(serviceAdapter)
|
|
3389
|
-
};
|
|
3390
|
-
await this.observability.hooks.handleRequest(requestData);
|
|
3391
|
-
} catch (error) {
|
|
3392
|
-
console.error("Error logging LLM request:", error);
|
|
3393
|
-
}
|
|
3394
|
-
}
|
|
3395
|
-
const serverSideActionsInput = serverSideActions.map((action) => ({
|
|
3396
|
-
name: action.name,
|
|
3397
|
-
description: action.description,
|
|
3398
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3399
|
-
}));
|
|
3400
|
-
const actionInputs = flattenToolCallsNoDuplicates([
|
|
3401
|
-
...serverSideActionsInput,
|
|
3402
|
-
...clientSideActionsInput.filter(
|
|
3403
|
-
// Filter remote actions from CopilotKit core loop
|
|
3404
|
-
(action) => action.available !== ActionInputAvailability.remote
|
|
3405
|
-
)
|
|
3406
|
-
]);
|
|
3407
|
-
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
3408
|
-
threadId,
|
|
3409
|
-
runId,
|
|
3410
|
-
inputMessages,
|
|
3411
|
-
properties: graphqlContext.properties,
|
|
3412
|
-
url
|
|
3413
|
-
}));
|
|
3414
|
-
const result = await serviceAdapter.process({
|
|
3415
|
-
messages: inputMessages,
|
|
3416
|
-
actions: actionInputs,
|
|
3372
|
+
await this.observability.hooks.handleRequest(requestData);
|
|
3373
|
+
} catch (error) {
|
|
3374
|
+
console.error("Error logging agent request:", error);
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
3378
|
+
threadId,
|
|
3379
|
+
runId: void 0,
|
|
3380
|
+
inputMessages: messages,
|
|
3381
|
+
properties: graphqlContext.properties
|
|
3382
|
+
}));
|
|
3383
|
+
try {
|
|
3384
|
+
const eventSource = new RuntimeEventSource();
|
|
3385
|
+
const stream = await currentAgent.remoteAgentHandler({
|
|
3386
|
+
name: agentName,
|
|
3417
3387
|
threadId,
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
extensions,
|
|
3422
|
-
agentSession,
|
|
3423
|
-
agentStates
|
|
3424
|
-
});
|
|
3425
|
-
const nonEmptyThreadId = threadId ?? result.threadId;
|
|
3426
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3427
|
-
var _a2;
|
|
3428
|
-
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
3429
|
-
threadId: nonEmptyThreadId,
|
|
3430
|
-
runId: result.runId,
|
|
3431
|
-
inputMessages,
|
|
3432
|
-
outputMessages,
|
|
3433
|
-
properties: graphqlContext.properties,
|
|
3434
|
-
url
|
|
3435
|
-
});
|
|
3436
|
-
}).catch((_error) => {
|
|
3388
|
+
nodeName,
|
|
3389
|
+
metaEvents,
|
|
3390
|
+
actionInputsWithoutAgents: allAvailableActions
|
|
3437
3391
|
});
|
|
3438
|
-
if (((_c = this.observability) == null ? void 0 : _c.enabled) && publicApiKey) {
|
|
3439
|
-
try {
|
|
3440
|
-
outputMessagesPromise.then((outputMessages) => {
|
|
3441
|
-
const responseData = {
|
|
3442
|
-
threadId: result.threadId,
|
|
3443
|
-
runId: result.runId,
|
|
3444
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3445
|
-
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
3446
|
-
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
3447
|
-
latency: Date.now() - requestStartTime,
|
|
3448
|
-
timestamp: Date.now(),
|
|
3449
|
-
provider: this.detectProvider(serviceAdapter),
|
|
3450
|
-
// Indicate this is the final response
|
|
3451
|
-
isFinalResponse: true
|
|
3452
|
-
};
|
|
3453
|
-
try {
|
|
3454
|
-
this.observability.hooks.handleResponse(responseData);
|
|
3455
|
-
} catch (logError) {
|
|
3456
|
-
console.error("Error logging LLM response:", logError);
|
|
3457
|
-
}
|
|
3458
|
-
}).catch((error) => {
|
|
3459
|
-
console.error("Failed to get output messages for logging:", error);
|
|
3460
|
-
});
|
|
3461
|
-
} catch (error) {
|
|
3462
|
-
console.error("Error setting up logging for LLM response:", error);
|
|
3463
|
-
}
|
|
3464
|
-
}
|
|
3465
|
-
if (((_d = this.observability) == null ? void 0 : _d.enabled) && this.observability.progressive && publicApiKey) {
|
|
3392
|
+
if (((_c = this.observability) == null ? void 0 : _c.enabled) && this.observability.progressive && publicApiKey) {
|
|
3466
3393
|
const originalStream = eventSource.stream.bind(eventSource);
|
|
3467
3394
|
eventSource.stream = async (callback) => {
|
|
3468
3395
|
await originalStream(async (eventStream$) => {
|
|
@@ -3473,21 +3400,23 @@ please use an LLM adapter instead.`
|
|
|
3473
3400
|
try {
|
|
3474
3401
|
const progressiveData = {
|
|
3475
3402
|
threadId: threadId || "",
|
|
3476
|
-
runId,
|
|
3403
|
+
runId: void 0,
|
|
3477
3404
|
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3478
3405
|
output: event.content,
|
|
3479
3406
|
latency: Date.now() - requestStartTime,
|
|
3480
3407
|
timestamp: Date.now(),
|
|
3481
|
-
provider:
|
|
3482
|
-
isProgressiveChunk: true
|
|
3408
|
+
provider: "agent",
|
|
3409
|
+
isProgressiveChunk: true,
|
|
3410
|
+
agentName,
|
|
3411
|
+
nodeName
|
|
3483
3412
|
};
|
|
3484
3413
|
Promise.resolve().then(() => {
|
|
3485
3414
|
this.observability.hooks.handleResponse(progressiveData);
|
|
3486
3415
|
}).catch((error) => {
|
|
3487
|
-
console.error("Error in progressive logging:", error);
|
|
3416
|
+
console.error("Error in progressive agent logging:", error);
|
|
3488
3417
|
});
|
|
3489
3418
|
} catch (error) {
|
|
3490
|
-
console.error("Error preparing progressive log data:", error);
|
|
3419
|
+
console.error("Error preparing progressive agent log data:", error);
|
|
3491
3420
|
}
|
|
3492
3421
|
}
|
|
3493
3422
|
}
|
|
@@ -3496,828 +3425,1135 @@ please use an LLM adapter instead.`
|
|
|
3496
3425
|
});
|
|
3497
3426
|
};
|
|
3498
3427
|
}
|
|
3428
|
+
eventSource.stream(async (eventStream$) => {
|
|
3429
|
+
from(stream).subscribe({
|
|
3430
|
+
next: (event) => eventStream$.next(event),
|
|
3431
|
+
error: async (err) => {
|
|
3432
|
+
var _a2;
|
|
3433
|
+
if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
|
|
3434
|
+
try {
|
|
3435
|
+
const errorData = {
|
|
3436
|
+
threadId,
|
|
3437
|
+
runId: void 0,
|
|
3438
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3439
|
+
error: err instanceof Error ? err : String(err),
|
|
3440
|
+
timestamp: Date.now(),
|
|
3441
|
+
latency: Date.now() - requestStartTime,
|
|
3442
|
+
provider: "agent",
|
|
3443
|
+
agentName,
|
|
3444
|
+
nodeName
|
|
3445
|
+
};
|
|
3446
|
+
this.observability.hooks.handleError(errorData);
|
|
3447
|
+
} catch (logError) {
|
|
3448
|
+
console.error("Error logging agent error:", logError);
|
|
3449
|
+
}
|
|
3450
|
+
}
|
|
3451
|
+
const structuredError = ensureStructuredError2(err, (error) => this.convertStreamingErrorToStructured(error));
|
|
3452
|
+
await this.trace("error", {
|
|
3453
|
+
threadId,
|
|
3454
|
+
source: "agent",
|
|
3455
|
+
request: {
|
|
3456
|
+
operation: "processAgentRequest",
|
|
3457
|
+
method: "POST",
|
|
3458
|
+
startTime: requestStartTime
|
|
3459
|
+
},
|
|
3460
|
+
response: {
|
|
3461
|
+
endTime: Date.now(),
|
|
3462
|
+
latency: Date.now() - requestStartTime
|
|
3463
|
+
},
|
|
3464
|
+
agent: {
|
|
3465
|
+
name: agentName,
|
|
3466
|
+
nodeName
|
|
3467
|
+
},
|
|
3468
|
+
technical: {
|
|
3469
|
+
environment: process.env.NODE_ENV,
|
|
3470
|
+
stackTrace: err instanceof Error ? err.stack : void 0
|
|
3471
|
+
}
|
|
3472
|
+
}, structuredError, publicApiKey);
|
|
3473
|
+
eventStream$.error(structuredError);
|
|
3474
|
+
eventStream$.complete();
|
|
3475
|
+
},
|
|
3476
|
+
complete: () => eventStream$.complete()
|
|
3477
|
+
});
|
|
3478
|
+
});
|
|
3479
|
+
if (((_d = this.observability) == null ? void 0 : _d.enabled) && publicApiKey) {
|
|
3480
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
3481
|
+
const responseData = {
|
|
3482
|
+
threadId,
|
|
3483
|
+
runId: void 0,
|
|
3484
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3485
|
+
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
3486
|
+
output: this.observability.progressive ? streamedChunks : outputMessages,
|
|
3487
|
+
latency: Date.now() - requestStartTime,
|
|
3488
|
+
timestamp: Date.now(),
|
|
3489
|
+
provider: "agent",
|
|
3490
|
+
isFinalResponse: true,
|
|
3491
|
+
agentName,
|
|
3492
|
+
nodeName
|
|
3493
|
+
};
|
|
3494
|
+
try {
|
|
3495
|
+
this.observability.hooks.handleResponse(responseData);
|
|
3496
|
+
} catch (logError) {
|
|
3497
|
+
console.error("Error logging agent response:", logError);
|
|
3498
|
+
}
|
|
3499
|
+
}).catch((error) => {
|
|
3500
|
+
console.error("Failed to get output messages for agent logging:", error);
|
|
3501
|
+
});
|
|
3502
|
+
}
|
|
3503
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
3504
|
+
var _a2;
|
|
3505
|
+
(_a2 = this.onAfterRequest) == null ? void 0 : _a2.call(this, {
|
|
3506
|
+
threadId,
|
|
3507
|
+
runId: void 0,
|
|
3508
|
+
inputMessages: messages,
|
|
3509
|
+
outputMessages,
|
|
3510
|
+
properties: graphqlContext.properties
|
|
3511
|
+
});
|
|
3512
|
+
}).catch((_error) => {
|
|
3513
|
+
});
|
|
3499
3514
|
return {
|
|
3500
|
-
threadId
|
|
3501
|
-
runId:
|
|
3515
|
+
threadId,
|
|
3516
|
+
runId: void 0,
|
|
3502
3517
|
eventSource,
|
|
3503
3518
|
serverSideActions,
|
|
3504
|
-
actionInputsWithoutAgents:
|
|
3505
|
-
// TODO-AGENTS: do not exclude ALL server side actions
|
|
3506
|
-
!serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
|
|
3507
|
-
)),
|
|
3508
|
-
extensions: result.extensions
|
|
3519
|
+
actionInputsWithoutAgents: allAvailableActions
|
|
3509
3520
|
};
|
|
3510
3521
|
} catch (error) {
|
|
3511
3522
|
if (((_e = this.observability) == null ? void 0 : _e.enabled) && publicApiKey) {
|
|
3512
3523
|
try {
|
|
3513
3524
|
const errorData = {
|
|
3514
3525
|
threadId,
|
|
3515
|
-
runId,
|
|
3526
|
+
runId: void 0,
|
|
3516
3527
|
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3517
3528
|
error: error instanceof Error ? error : String(error),
|
|
3518
3529
|
timestamp: Date.now(),
|
|
3519
3530
|
latency: Date.now() - requestStartTime,
|
|
3520
|
-
provider:
|
|
3531
|
+
provider: "agent",
|
|
3532
|
+
agentName,
|
|
3533
|
+
nodeName
|
|
3521
3534
|
};
|
|
3522
3535
|
await this.observability.hooks.handleError(errorData);
|
|
3523
3536
|
} catch (logError) {
|
|
3524
|
-
console.error("Error logging
|
|
3537
|
+
console.error("Error logging agent error:", logError);
|
|
3525
3538
|
}
|
|
3526
3539
|
}
|
|
3527
|
-
|
|
3528
|
-
if (error instanceof CopilotKitError5) {
|
|
3529
|
-
structuredError = error;
|
|
3530
|
-
} else {
|
|
3531
|
-
console.error("Error getting response:", error);
|
|
3532
|
-
structuredError = this.convertStreamingErrorToStructured(error);
|
|
3533
|
-
}
|
|
3540
|
+
const structuredError = ensureStructuredError2(error, (err) => this.convertStreamingErrorToStructured(err));
|
|
3534
3541
|
await this.trace("error", {
|
|
3535
3542
|
threadId,
|
|
3536
|
-
|
|
3537
|
-
source: "runtime",
|
|
3543
|
+
source: "agent",
|
|
3538
3544
|
request: {
|
|
3539
|
-
operation: "
|
|
3545
|
+
operation: "processAgentRequest",
|
|
3540
3546
|
method: "POST",
|
|
3541
|
-
url,
|
|
3542
3547
|
startTime: requestStartTime
|
|
3543
3548
|
},
|
|
3544
3549
|
response: {
|
|
3545
3550
|
endTime: Date.now(),
|
|
3546
3551
|
latency: Date.now() - requestStartTime
|
|
3547
3552
|
},
|
|
3548
|
-
agent:
|
|
3549
|
-
name:
|
|
3550
|
-
|
|
3553
|
+
agent: {
|
|
3554
|
+
name: agentName,
|
|
3555
|
+
nodeName
|
|
3556
|
+
},
|
|
3551
3557
|
technical: {
|
|
3552
3558
|
environment: process.env.NODE_ENV,
|
|
3553
3559
|
stackTrace: error instanceof Error ? error.stack : void 0
|
|
3554
3560
|
}
|
|
3555
|
-
}, structuredError, publicApiKey);
|
|
3556
|
-
|
|
3561
|
+
}, structuredError, publicApiKey);
|
|
3562
|
+
console.error("Error getting response:", error);
|
|
3563
|
+
throw structuredError;
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
async getServerSideActions(request) {
|
|
3567
|
+
var _a, _b;
|
|
3568
|
+
const { graphqlContext, messages: rawMessages, agentStates, url } = request;
|
|
3569
|
+
const inputMessages = convertGqlInputToMessages(rawMessages);
|
|
3570
|
+
const langserveFunctions = [];
|
|
3571
|
+
for (const chainPromise of this.langserve) {
|
|
3572
|
+
try {
|
|
3573
|
+
const chain = await chainPromise;
|
|
3574
|
+
langserveFunctions.push(chain);
|
|
3575
|
+
} catch (error) {
|
|
3576
|
+
console.error("Error loading langserve chain:", error);
|
|
3577
|
+
}
|
|
3578
|
+
}
|
|
3579
|
+
const remoteEndpointDefinitions = this.remoteEndpointDefinitions.map((endpoint) => ({
|
|
3580
|
+
...endpoint,
|
|
3581
|
+
type: resolveEndpointType(endpoint)
|
|
3582
|
+
}));
|
|
3583
|
+
const remoteActions = await setupRemoteActions({
|
|
3584
|
+
remoteEndpointDefinitions,
|
|
3585
|
+
graphqlContext,
|
|
3586
|
+
messages: inputMessages,
|
|
3587
|
+
agentStates,
|
|
3588
|
+
frontendUrl: url,
|
|
3589
|
+
agents: this.agents,
|
|
3590
|
+
metaEvents: request.metaEvents
|
|
3591
|
+
});
|
|
3592
|
+
const configuredActions = typeof this.actions === "function" ? this.actions({
|
|
3593
|
+
properties: graphqlContext.properties,
|
|
3594
|
+
url
|
|
3595
|
+
}) : this.actions;
|
|
3596
|
+
const requestSpecificMCPActions = [];
|
|
3597
|
+
if (this.createMCPClientImpl) {
|
|
3598
|
+
const baseEndpoints = this.mcpServersConfig || [];
|
|
3599
|
+
const requestEndpoints = ((_a = graphqlContext.properties) == null ? void 0 : _a.mcpServers) || ((_b = graphqlContext.properties) == null ? void 0 : _b.mcpEndpoints) || [];
|
|
3600
|
+
const effectiveEndpointsMap = /* @__PURE__ */ new Map();
|
|
3601
|
+
[
|
|
3602
|
+
...baseEndpoints
|
|
3603
|
+
].forEach((ep) => {
|
|
3604
|
+
if (ep && ep.endpoint) {
|
|
3605
|
+
effectiveEndpointsMap.set(ep.endpoint, ep);
|
|
3606
|
+
}
|
|
3607
|
+
});
|
|
3608
|
+
[
|
|
3609
|
+
...requestEndpoints
|
|
3610
|
+
].forEach((ep) => {
|
|
3611
|
+
if (ep && ep.endpoint) {
|
|
3612
|
+
effectiveEndpointsMap.set(ep.endpoint, ep);
|
|
3613
|
+
}
|
|
3614
|
+
});
|
|
3615
|
+
const effectiveEndpoints = Array.from(effectiveEndpointsMap.values());
|
|
3616
|
+
for (const config of effectiveEndpoints) {
|
|
3617
|
+
const endpointUrl = config.endpoint;
|
|
3618
|
+
let actionsForEndpoint = this.mcpActionCache.get(endpointUrl);
|
|
3619
|
+
if (!actionsForEndpoint) {
|
|
3620
|
+
let client = null;
|
|
3621
|
+
try {
|
|
3622
|
+
client = await this.createMCPClientImpl(config);
|
|
3623
|
+
const tools = await client.tools();
|
|
3624
|
+
actionsForEndpoint = convertMCPToolsToActions(tools, endpointUrl);
|
|
3625
|
+
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3626
|
+
} catch (error) {
|
|
3627
|
+
console.error(`MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`, error);
|
|
3628
|
+
actionsForEndpoint = [];
|
|
3629
|
+
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
3630
|
+
}
|
|
3631
|
+
}
|
|
3632
|
+
requestSpecificMCPActions.push(...actionsForEndpoint || []);
|
|
3633
|
+
}
|
|
3557
3634
|
}
|
|
3558
|
-
}
|
|
3559
|
-
async getAllAgents(graphqlContext) {
|
|
3560
|
-
const [agentsWithEndpoints, aguiAgents] = await Promise.all([
|
|
3561
|
-
this.discoverAgentsFromEndpoints(graphqlContext),
|
|
3562
|
-
this.discoverAgentsFromAgui()
|
|
3563
|
-
]);
|
|
3564
|
-
this.availableAgents = [
|
|
3565
|
-
...agentsWithEndpoints,
|
|
3566
|
-
...aguiAgents
|
|
3567
|
-
].map((a) => ({
|
|
3568
|
-
name: a.name,
|
|
3569
|
-
id: a.id
|
|
3570
|
-
}));
|
|
3571
3635
|
return [
|
|
3572
|
-
...
|
|
3573
|
-
...
|
|
3636
|
+
...configuredActions,
|
|
3637
|
+
...langserveFunctions,
|
|
3638
|
+
...remoteActions,
|
|
3639
|
+
...requestSpecificMCPActions
|
|
3574
3640
|
];
|
|
3575
3641
|
}
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3642
|
+
// Add helper method to detect provider
|
|
3643
|
+
detectProvider(serviceAdapter) {
|
|
3644
|
+
const adapterName = serviceAdapter.constructor.name;
|
|
3645
|
+
if (adapterName.includes("OpenAI"))
|
|
3646
|
+
return "openai";
|
|
3647
|
+
if (adapterName.includes("Anthropic"))
|
|
3648
|
+
return "anthropic";
|
|
3649
|
+
if (adapterName.includes("Google"))
|
|
3650
|
+
return "google";
|
|
3651
|
+
if (adapterName.includes("Groq"))
|
|
3652
|
+
return "groq";
|
|
3653
|
+
if (adapterName.includes("LangChain"))
|
|
3654
|
+
return "langchain";
|
|
3655
|
+
return void 0;
|
|
3656
|
+
}
|
|
3657
|
+
convertStreamingErrorToStructured(error) {
|
|
3658
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
3659
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "agent streaming connection");
|
|
3660
|
+
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") {
|
|
3661
|
+
return new CopilotKitLowLevelError3({
|
|
3662
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
3663
|
+
url: "agent streaming connection",
|
|
3664
|
+
message: helpfulMessage
|
|
3665
|
+
});
|
|
3666
|
+
}
|
|
3667
|
+
return new CopilotKitError3({
|
|
3668
|
+
message: helpfulMessage,
|
|
3669
|
+
code: CopilotKitErrorCode2.UNKNOWN
|
|
3670
|
+
});
|
|
3671
|
+
}
|
|
3672
|
+
async trace(type, context, error, publicApiKey) {
|
|
3673
|
+
if (!this.onTrace)
|
|
3674
|
+
return;
|
|
3675
|
+
if (!publicApiKey) {
|
|
3676
|
+
if (!this.hasWarnedAboutTracing) {
|
|
3677
|
+
console.warn("CopilotKit: onTrace handler provided but requires publicApiKey to be defined for tracing to work.");
|
|
3678
|
+
this.hasWarnedAboutTracing = true;
|
|
3679
|
+
}
|
|
3680
|
+
return;
|
|
3681
|
+
}
|
|
3682
|
+
try {
|
|
3683
|
+
const traceEvent = {
|
|
3684
|
+
type,
|
|
3685
|
+
timestamp: Date.now(),
|
|
3686
|
+
context,
|
|
3687
|
+
...error && {
|
|
3688
|
+
error
|
|
3689
|
+
}
|
|
3690
|
+
};
|
|
3691
|
+
await this.onTrace(traceEvent);
|
|
3692
|
+
} catch (traceError) {
|
|
3693
|
+
console.error("Error in onTrace handler:", traceError);
|
|
3694
|
+
}
|
|
3695
|
+
}
|
|
3696
|
+
/**
|
|
3697
|
+
* Public method to trace GraphQL validation errors
|
|
3698
|
+
* This allows the GraphQL resolver to send validation errors through the trace system
|
|
3699
|
+
*/
|
|
3700
|
+
async traceGraphQLError(error, context) {
|
|
3701
|
+
if (!this.onTrace)
|
|
3702
|
+
return;
|
|
3703
|
+
try {
|
|
3704
|
+
await this.onTrace({
|
|
3705
|
+
type: "error",
|
|
3706
|
+
timestamp: Date.now(),
|
|
3707
|
+
context: {
|
|
3708
|
+
source: "runtime",
|
|
3709
|
+
request: {
|
|
3710
|
+
operation: context.operation,
|
|
3711
|
+
startTime: Date.now()
|
|
3712
|
+
},
|
|
3713
|
+
technical: {
|
|
3714
|
+
environment: process.env.NODE_ENV
|
|
3715
|
+
},
|
|
3716
|
+
metadata: {
|
|
3717
|
+
errorType: "GraphQLValidationError",
|
|
3718
|
+
cloudConfigPresent: context.cloudConfigPresent,
|
|
3719
|
+
guardrailsEnabled: context.guardrailsEnabled
|
|
3597
3720
|
}
|
|
3598
|
-
}
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
|
|
3604
|
-
|
|
3605
|
-
|
|
3721
|
+
},
|
|
3722
|
+
error
|
|
3723
|
+
});
|
|
3724
|
+
} catch (traceError) {
|
|
3725
|
+
console.error("Error in onTrace handler:", traceError);
|
|
3726
|
+
}
|
|
3727
|
+
}
|
|
3728
|
+
};
|
|
3729
|
+
__name(CopilotRuntime, "CopilotRuntime");
|
|
3730
|
+
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
3731
|
+
let allTools = [];
|
|
3732
|
+
const allToolNames = [];
|
|
3733
|
+
for (const tool of toolsByPriority) {
|
|
3734
|
+
if (!allToolNames.includes(tool.name)) {
|
|
3735
|
+
allTools.push(tool);
|
|
3736
|
+
allToolNames.push(tool.name);
|
|
3737
|
+
}
|
|
3738
|
+
}
|
|
3739
|
+
return allTools;
|
|
3740
|
+
}
|
|
3741
|
+
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
3742
|
+
function copilotKitEndpoint(config) {
|
|
3743
|
+
return {
|
|
3744
|
+
...config,
|
|
3745
|
+
type: EndpointType.CopilotKit
|
|
3746
|
+
};
|
|
3747
|
+
}
|
|
3748
|
+
__name(copilotKitEndpoint, "copilotKitEndpoint");
|
|
3749
|
+
function langGraphPlatformEndpoint(config) {
|
|
3750
|
+
return {
|
|
3751
|
+
...config,
|
|
3752
|
+
type: EndpointType.LangGraphPlatform
|
|
3753
|
+
};
|
|
3754
|
+
}
|
|
3755
|
+
__name(langGraphPlatformEndpoint, "langGraphPlatformEndpoint");
|
|
3756
|
+
function resolveEndpointType(endpoint) {
|
|
3757
|
+
if (!endpoint.type) {
|
|
3758
|
+
if ("deploymentUrl" in endpoint && "agents" in endpoint) {
|
|
3759
|
+
return EndpointType.LangGraphPlatform;
|
|
3760
|
+
} else {
|
|
3761
|
+
return EndpointType.CopilotKit;
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
return endpoint.type;
|
|
3765
|
+
}
|
|
3766
|
+
__name(resolveEndpointType, "resolveEndpointType");
|
|
3767
|
+
|
|
3768
|
+
// src/lib/telemetry-client.ts
|
|
3769
|
+
var packageJson = require_package();
|
|
3770
|
+
var telemetryClient = new TelemetryClient({
|
|
3771
|
+
packageName: packageJson.name,
|
|
3772
|
+
packageVersion: packageJson.version
|
|
3773
|
+
});
|
|
3774
|
+
function getRuntimeInstanceTelemetryInfo(options) {
|
|
3775
|
+
var _a, _b;
|
|
3776
|
+
const runtime = options.runtime;
|
|
3777
|
+
const endpointsInfo = runtime.remoteEndpointDefinitions.reduce((acc, endpoint) => {
|
|
3778
|
+
let info = {
|
|
3779
|
+
...acc
|
|
3780
|
+
};
|
|
3781
|
+
const endpointType = resolveEndpointType(endpoint);
|
|
3782
|
+
if (!info.endpointTypes.includes(endpointType)) {
|
|
3783
|
+
info = {
|
|
3784
|
+
...info,
|
|
3785
|
+
endpointTypes: [
|
|
3786
|
+
...info.endpointTypes,
|
|
3787
|
+
endpointType
|
|
3788
|
+
]
|
|
3789
|
+
};
|
|
3790
|
+
}
|
|
3791
|
+
if (endpointType === EndpointType.LangGraphPlatform) {
|
|
3792
|
+
const ep = endpoint;
|
|
3793
|
+
info = {
|
|
3794
|
+
...info,
|
|
3795
|
+
agentsAmount: ep.agents.length,
|
|
3796
|
+
hashedKey: ep.langsmithApiKey ? createHash2("sha256").update(ep.langsmithApiKey).digest("hex") : null
|
|
3797
|
+
};
|
|
3798
|
+
}
|
|
3799
|
+
return info;
|
|
3800
|
+
}, {
|
|
3801
|
+
endpointTypes: [],
|
|
3802
|
+
agentsAmount: null,
|
|
3803
|
+
hashedKey: null
|
|
3804
|
+
});
|
|
3805
|
+
const publicApiKey = (_a = options.cloud) == null ? void 0 : _a.publicApiKey;
|
|
3806
|
+
const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;
|
|
3807
|
+
return {
|
|
3808
|
+
actionsAmount: runtime.actions.length,
|
|
3809
|
+
endpointsAmount: runtime.remoteEndpointDefinitions.length,
|
|
3810
|
+
endpointTypes: endpointsInfo.endpointTypes,
|
|
3811
|
+
agentsAmount: endpointsInfo.agentsAmount,
|
|
3812
|
+
hashedLgcKey: endpointsInfo.hashedKey,
|
|
3813
|
+
"cloud.api_key_provided": apiKeyProvided,
|
|
3814
|
+
...apiKeyProvided ? {
|
|
3815
|
+
"cloud.public_api_key": publicApiKey
|
|
3816
|
+
} : {},
|
|
3817
|
+
...((_b = options.cloud) == null ? void 0 : _b.baseUrl) ? {
|
|
3818
|
+
"cloud.base_url": options.cloud.baseUrl
|
|
3819
|
+
} : {}
|
|
3820
|
+
};
|
|
3821
|
+
}
|
|
3822
|
+
__name(getRuntimeInstanceTelemetryInfo, "getRuntimeInstanceTelemetryInfo");
|
|
3823
|
+
var telemetry_client_default = telemetryClient;
|
|
3824
|
+
|
|
3825
|
+
// src/agents/langgraph/event-source.ts
|
|
3826
|
+
import { CopilotKitLowLevelError as CopilotKitLowLevelError4, isStructuredCopilotKitError } from "@copilotkit/shared";
|
|
3827
|
+
import { catchError, mergeMap, ReplaySubject, scan } from "rxjs";
|
|
3828
|
+
var RemoteLangGraphEventSource = class {
|
|
3829
|
+
eventStream$ = new ReplaySubject();
|
|
3830
|
+
shouldEmitToolCall(shouldEmitToolCalls, toolCallName) {
|
|
3831
|
+
if (typeof shouldEmitToolCalls === "boolean") {
|
|
3832
|
+
return shouldEmitToolCalls;
|
|
3833
|
+
}
|
|
3834
|
+
if (Array.isArray(shouldEmitToolCalls)) {
|
|
3835
|
+
return shouldEmitToolCalls.includes(toolCallName);
|
|
3836
|
+
}
|
|
3837
|
+
return shouldEmitToolCalls === toolCallName;
|
|
3838
|
+
}
|
|
3839
|
+
getCurrentContent(event) {
|
|
3840
|
+
var _a, _b, _c, _d, _e;
|
|
3841
|
+
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);
|
|
3842
|
+
if (!content) {
|
|
3843
|
+
const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
|
|
3844
|
+
for (const chunk of toolCallChunks) {
|
|
3845
|
+
if (chunk.args) {
|
|
3846
|
+
return chunk.args;
|
|
3606
3847
|
}
|
|
3607
|
-
const endpointAgents = data.map((entry) => ({
|
|
3608
|
-
name: entry.graph_id,
|
|
3609
|
-
id: entry.assistant_id,
|
|
3610
|
-
description: "",
|
|
3611
|
-
endpoint
|
|
3612
|
-
}));
|
|
3613
|
-
return [
|
|
3614
|
-
...agents2,
|
|
3615
|
-
...endpointAgents
|
|
3616
|
-
];
|
|
3617
3848
|
}
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3849
|
+
}
|
|
3850
|
+
if (typeof content === "string") {
|
|
3851
|
+
return content;
|
|
3852
|
+
} else if (Array.isArray(content) && content.length > 0) {
|
|
3853
|
+
return content[0].text;
|
|
3854
|
+
}
|
|
3855
|
+
return null;
|
|
3856
|
+
}
|
|
3857
|
+
getCurrentMessageId(event) {
|
|
3858
|
+
var _a, _b, _c, _d, _e;
|
|
3859
|
+
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);
|
|
3860
|
+
}
|
|
3861
|
+
getCurrentToolCallChunks(event) {
|
|
3862
|
+
var _a, _b, _c, _d, _e;
|
|
3863
|
+
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);
|
|
3864
|
+
}
|
|
3865
|
+
getResponseMetadata(event) {
|
|
3866
|
+
var _a, _b, _c, _d, _e;
|
|
3867
|
+
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);
|
|
3868
|
+
}
|
|
3869
|
+
processLangGraphEvents() {
|
|
3870
|
+
let lastEventWithState = null;
|
|
3871
|
+
return this.eventStream$.pipe(scan((acc, event) => {
|
|
3872
|
+
if (event.event === LangGraphEventTypes.OnChatModelStream) {
|
|
3873
|
+
const prevMessageId = acc.lastMessageId;
|
|
3874
|
+
acc.currentContent = this.getCurrentContent(event);
|
|
3875
|
+
acc.lastMessageId = this.getCurrentMessageId(event) ?? acc.lastMessageId;
|
|
3876
|
+
const toolCallChunks = this.getCurrentToolCallChunks(event) ?? [];
|
|
3877
|
+
const responseMetadata = this.getResponseMetadata(event);
|
|
3878
|
+
const toolCallCheck = toolCallChunks && toolCallChunks.length > 0;
|
|
3879
|
+
let isToolCallEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "tool_calls";
|
|
3880
|
+
acc.isToolCallStart = toolCallChunks.some((chunk) => chunk.name && chunk.id);
|
|
3881
|
+
acc.isMessageStart = prevMessageId !== acc.lastMessageId && !acc.isToolCallStart;
|
|
3882
|
+
let previousRoundHadToolCall = acc.isToolCall;
|
|
3883
|
+
acc.isToolCall = toolCallCheck;
|
|
3884
|
+
if (previousRoundHadToolCall && !toolCallCheck) {
|
|
3885
|
+
isToolCallEnd = true;
|
|
3886
|
+
}
|
|
3887
|
+
acc.isToolCallEnd = isToolCallEnd;
|
|
3888
|
+
acc.isMessageEnd = (responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop";
|
|
3889
|
+
({ name: acc.lastToolCallName, id: acc.lastToolCallId } = toolCallChunks.find((chunk) => chunk.name && chunk.id) ?? {
|
|
3890
|
+
name: acc.lastToolCallName,
|
|
3891
|
+
id: acc.lastToolCallId
|
|
3627
3892
|
});
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3893
|
+
}
|
|
3894
|
+
acc.event = event;
|
|
3895
|
+
lastEventWithState = acc;
|
|
3896
|
+
return acc;
|
|
3897
|
+
}, {
|
|
3898
|
+
event: null,
|
|
3899
|
+
isMessageStart: false,
|
|
3900
|
+
isMessageEnd: false,
|
|
3901
|
+
isToolCallStart: false,
|
|
3902
|
+
isToolCallEnd: false,
|
|
3903
|
+
isToolCall: false,
|
|
3904
|
+
lastMessageId: null,
|
|
3905
|
+
lastToolCallId: null,
|
|
3906
|
+
lastToolCallName: null,
|
|
3907
|
+
currentContent: null,
|
|
3908
|
+
processedToolCallIds: /* @__PURE__ */ new Set()
|
|
3909
|
+
}), mergeMap((acc) => {
|
|
3910
|
+
const events = [];
|
|
3911
|
+
let shouldEmitMessages = true;
|
|
3912
|
+
let shouldEmitToolCalls = true;
|
|
3913
|
+
if (acc.event.event == LangGraphEventTypes.OnChatModelStream) {
|
|
3914
|
+
if ("copilotkit:emit-tool-calls" in (acc.event.metadata || {})) {
|
|
3915
|
+
shouldEmitToolCalls = acc.event.metadata["copilotkit:emit-tool-calls"];
|
|
3639
3916
|
}
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
name: agent.name,
|
|
3643
|
-
description: agent.description ?? "" ?? "",
|
|
3644
|
-
id: randomId2(),
|
|
3645
|
-
endpoint
|
|
3646
|
-
}));
|
|
3647
|
-
return [
|
|
3648
|
-
...agents2,
|
|
3649
|
-
...endpointAgents
|
|
3650
|
-
];
|
|
3651
|
-
} catch (error) {
|
|
3652
|
-
if (error instanceof CopilotKitError5) {
|
|
3653
|
-
throw error;
|
|
3917
|
+
if ("copilotkit:emit-messages" in (acc.event.metadata || {})) {
|
|
3918
|
+
shouldEmitMessages = acc.event.metadata["copilotkit:emit-messages"];
|
|
3654
3919
|
}
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3920
|
+
}
|
|
3921
|
+
if (acc.event.event === LangGraphEventTypes.OnInterrupt) {
|
|
3922
|
+
events.push({
|
|
3923
|
+
type: RuntimeEventTypes.MetaEvent,
|
|
3924
|
+
name: RuntimeMetaEventName.LangGraphInterruptEvent,
|
|
3925
|
+
value: acc.event.value
|
|
3658
3926
|
});
|
|
3659
3927
|
}
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
const agents2 = await acc;
|
|
3666
|
-
const client = agent.client;
|
|
3667
|
-
let data = [];
|
|
3668
|
-
try {
|
|
3669
|
-
data = await client.assistants.search();
|
|
3670
|
-
if (data && "detail" in data && data.detail.toLowerCase() === "not found") {
|
|
3671
|
-
throw new CopilotKitAgentDiscoveryError({
|
|
3672
|
-
availableAgents: this.availableAgents
|
|
3673
|
-
});
|
|
3674
|
-
}
|
|
3675
|
-
} catch (e) {
|
|
3676
|
-
throw new CopilotKitMisuseError2({
|
|
3677
|
-
message: `
|
|
3678
|
-
Failed to find or contact agent ${agent.graphId}.
|
|
3679
|
-
Make sure the LangGraph API is running and the agent is defined in langgraph.json
|
|
3680
|
-
|
|
3681
|
-
See more: https://docs.copilotkit.ai/troubleshooting/common-issues`
|
|
3928
|
+
if (acc.event.event === LangGraphEventTypes.OnCopilotKitInterrupt) {
|
|
3929
|
+
events.push({
|
|
3930
|
+
type: RuntimeEventTypes.MetaEvent,
|
|
3931
|
+
name: RuntimeMetaEventName.CopilotKitLangGraphInterruptEvent,
|
|
3932
|
+
data: acc.event.data
|
|
3682
3933
|
});
|
|
3683
3934
|
}
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
...agents2,
|
|
3691
|
-
...endpointAgents
|
|
3692
|
-
];
|
|
3693
|
-
}, Promise.resolve([]));
|
|
3694
|
-
return agents;
|
|
3695
|
-
}
|
|
3696
|
-
async loadAgentState(graphqlContext, threadId, agentName) {
|
|
3697
|
-
const agents = await this.getAllAgents(graphqlContext);
|
|
3698
|
-
const agent = agents.find((agent2) => agent2.name === agentName);
|
|
3699
|
-
if (!agent) {
|
|
3700
|
-
throw new Error("Agent not found");
|
|
3701
|
-
}
|
|
3702
|
-
if ("endpoint" in agent && (agent.endpoint.type === EndpointType.CopilotKit || !("type" in agent.endpoint))) {
|
|
3703
|
-
const cpkEndpoint = agent.endpoint;
|
|
3704
|
-
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
3705
|
-
try {
|
|
3706
|
-
const response = await fetchWithRetry(fetchUrl, {
|
|
3707
|
-
method: "POST",
|
|
3708
|
-
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
3709
|
-
body: JSON.stringify({
|
|
3710
|
-
properties: graphqlContext.properties,
|
|
3711
|
-
threadId,
|
|
3712
|
-
name: agentName
|
|
3713
|
-
})
|
|
3935
|
+
if (acc.event.event === LangGraphEventTypes.OnCopilotKitError) {
|
|
3936
|
+
const errorData = acc.event.data.error;
|
|
3937
|
+
const preservedError = new CopilotKitLowLevelError4({
|
|
3938
|
+
error: new Error(errorData.message),
|
|
3939
|
+
url: "langgraph agent",
|
|
3940
|
+
message: `${errorData.type}: ${errorData.message}`
|
|
3714
3941
|
});
|
|
3715
|
-
if (
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3942
|
+
if (errorData.status_code) {
|
|
3943
|
+
preservedError.statusCode = errorData.status_code;
|
|
3944
|
+
}
|
|
3945
|
+
if (errorData.response_data) {
|
|
3946
|
+
preservedError.responseData = errorData.response_data;
|
|
3947
|
+
}
|
|
3948
|
+
preservedError.agentName = errorData.agent_name;
|
|
3949
|
+
preservedError.originalErrorType = errorData.type;
|
|
3950
|
+
throw preservedError;
|
|
3951
|
+
}
|
|
3952
|
+
const responseMetadata = this.getResponseMetadata(acc.event);
|
|
3953
|
+
if (acc.isToolCallEnd && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName) && acc.lastToolCallId && !acc.processedToolCallIds.has(acc.lastToolCallId)) {
|
|
3954
|
+
acc.processedToolCallIds.add(acc.lastToolCallId);
|
|
3955
|
+
events.push({
|
|
3956
|
+
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
3957
|
+
actionExecutionId: acc.lastToolCallId
|
|
3958
|
+
});
|
|
3959
|
+
} else if ((responseMetadata == null ? void 0 : responseMetadata.finish_reason) === "stop" && shouldEmitMessages) {
|
|
3960
|
+
events.push({
|
|
3961
|
+
type: RuntimeEventTypes.TextMessageEnd,
|
|
3962
|
+
messageId: acc.lastMessageId
|
|
3963
|
+
});
|
|
3964
|
+
}
|
|
3965
|
+
switch (acc.event.event) {
|
|
3966
|
+
case LangGraphEventTypes.OnCustomEvent:
|
|
3967
|
+
if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
|
|
3968
|
+
events.push({
|
|
3969
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
3970
|
+
messageId: acc.event.data.message_id
|
|
3971
|
+
});
|
|
3972
|
+
events.push({
|
|
3973
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
3974
|
+
messageId: acc.event.data.message_id,
|
|
3975
|
+
content: acc.event.data.message
|
|
3976
|
+
});
|
|
3977
|
+
events.push({
|
|
3978
|
+
type: RuntimeEventTypes.TextMessageEnd,
|
|
3979
|
+
messageId: acc.event.data.message_id
|
|
3980
|
+
});
|
|
3981
|
+
} else if (acc.event.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
|
|
3982
|
+
events.push({
|
|
3983
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
3984
|
+
actionExecutionId: acc.event.data.id,
|
|
3985
|
+
actionName: acc.event.data.name,
|
|
3986
|
+
parentMessageId: acc.event.data.id
|
|
3987
|
+
});
|
|
3988
|
+
events.push({
|
|
3989
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
3990
|
+
actionExecutionId: acc.event.data.id,
|
|
3991
|
+
args: JSON.stringify(acc.event.data.args)
|
|
3992
|
+
});
|
|
3993
|
+
events.push({
|
|
3994
|
+
type: RuntimeEventTypes.ActionExecutionEnd,
|
|
3995
|
+
actionExecutionId: acc.event.data.id
|
|
3996
|
+
});
|
|
3997
|
+
}
|
|
3998
|
+
break;
|
|
3999
|
+
case LangGraphEventTypes.OnCopilotKitStateSync:
|
|
4000
|
+
events.push({
|
|
4001
|
+
type: RuntimeEventTypes.AgentStateMessage,
|
|
4002
|
+
threadId: acc.event.thread_id,
|
|
4003
|
+
role: acc.event.role,
|
|
4004
|
+
agentName: acc.event.agent_name,
|
|
4005
|
+
nodeName: acc.event.node_name,
|
|
4006
|
+
runId: acc.event.run_id,
|
|
4007
|
+
active: acc.event.active,
|
|
4008
|
+
state: JSON.stringify(acc.event.state),
|
|
4009
|
+
running: acc.event.running
|
|
4010
|
+
});
|
|
4011
|
+
break;
|
|
4012
|
+
case LangGraphEventTypes.OnChatModelStream:
|
|
4013
|
+
if (acc.isToolCallStart && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
4014
|
+
events.push({
|
|
4015
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
4016
|
+
actionExecutionId: acc.lastToolCallId,
|
|
4017
|
+
actionName: acc.lastToolCallName,
|
|
4018
|
+
parentMessageId: acc.lastMessageId
|
|
4019
|
+
});
|
|
4020
|
+
} else if (acc.isMessageStart && shouldEmitMessages) {
|
|
4021
|
+
acc.processedToolCallIds.clear();
|
|
4022
|
+
events.push({
|
|
4023
|
+
type: RuntimeEventTypes.TextMessageStart,
|
|
4024
|
+
messageId: acc.lastMessageId
|
|
3719
4025
|
});
|
|
3720
4026
|
}
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
throw error;
|
|
3736
|
-
}
|
|
3737
|
-
throw new CopilotKitLowLevelError4({
|
|
3738
|
-
error,
|
|
3739
|
-
url: fetchUrl
|
|
3740
|
-
});
|
|
3741
|
-
}
|
|
3742
|
-
}
|
|
3743
|
-
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
3744
|
-
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
3745
|
-
} : null;
|
|
3746
|
-
let client;
|
|
3747
|
-
if ("endpoint" in agent && agent.endpoint.type === EndpointType.LangGraphPlatform) {
|
|
3748
|
-
client = new LangGraphClient2({
|
|
3749
|
-
apiUrl: agent.endpoint.deploymentUrl,
|
|
3750
|
-
apiKey: agent.endpoint.langsmithApiKey,
|
|
3751
|
-
defaultHeaders: {
|
|
3752
|
-
...propertyHeaders
|
|
3753
|
-
}
|
|
3754
|
-
});
|
|
3755
|
-
} else {
|
|
3756
|
-
const aguiAgent = graphqlContext._copilotkit.runtime.agents[agent.name];
|
|
3757
|
-
if (!aguiAgent) {
|
|
3758
|
-
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
4027
|
+
if (acc.isToolCall && acc.currentContent && this.shouldEmitToolCall(shouldEmitToolCalls, acc.lastToolCallName)) {
|
|
4028
|
+
events.push({
|
|
4029
|
+
type: RuntimeEventTypes.ActionExecutionArgs,
|
|
4030
|
+
actionExecutionId: acc.lastToolCallId,
|
|
4031
|
+
args: acc.currentContent
|
|
4032
|
+
});
|
|
4033
|
+
} else if (!acc.isToolCall && acc.currentContent && shouldEmitMessages) {
|
|
4034
|
+
events.push({
|
|
4035
|
+
type: RuntimeEventTypes.TextMessageContent,
|
|
4036
|
+
messageId: acc.lastMessageId,
|
|
4037
|
+
content: acc.currentContent
|
|
4038
|
+
});
|
|
4039
|
+
}
|
|
4040
|
+
break;
|
|
3759
4041
|
}
|
|
3760
|
-
|
|
3761
|
-
}
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
state = (await client.threads.getState(threadId)).values;
|
|
3765
|
-
} catch (error) {
|
|
3766
|
-
}
|
|
3767
|
-
if (Object.keys(state).length === 0) {
|
|
3768
|
-
return {
|
|
3769
|
-
threadId: threadId || "",
|
|
3770
|
-
threadExists: false,
|
|
3771
|
-
state: JSON.stringify({}),
|
|
3772
|
-
messages: JSON.stringify([])
|
|
3773
|
-
};
|
|
3774
|
-
} else {
|
|
3775
|
-
const { messages, ...stateWithoutMessages } = state;
|
|
3776
|
-
const copilotkitMessages = langchainMessagesToCopilotKit(messages);
|
|
3777
|
-
return {
|
|
3778
|
-
threadId: threadId || "",
|
|
3779
|
-
threadExists: true,
|
|
3780
|
-
state: JSON.stringify(stateWithoutMessages),
|
|
3781
|
-
messages: JSON.stringify(copilotkitMessages)
|
|
3782
|
-
};
|
|
3783
|
-
}
|
|
3784
|
-
throw new Error(`Agent: ${agent.name} could not be resolved`);
|
|
3785
|
-
}
|
|
3786
|
-
async processAgentRequest(request) {
|
|
3787
|
-
var _a, _b, _c, _d, _e;
|
|
3788
|
-
const { messages: rawMessages, outputMessagesPromise, graphqlContext, agentSession, threadId: threadIdFromRequest, metaEvents, publicApiKey, forwardedParameters } = request;
|
|
3789
|
-
const { agentName, nodeName } = agentSession;
|
|
3790
|
-
const requestStartTime = Date.now();
|
|
3791
|
-
const streamedChunks = [];
|
|
3792
|
-
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
3793
|
-
await this.trace("agent_state", {
|
|
3794
|
-
threadId,
|
|
3795
|
-
source: "agent",
|
|
3796
|
-
request: {
|
|
3797
|
-
operation: "processAgentRequest",
|
|
3798
|
-
method: "POST",
|
|
3799
|
-
startTime: requestStartTime
|
|
3800
|
-
},
|
|
3801
|
-
agent: {
|
|
3802
|
-
name: agentName,
|
|
3803
|
-
nodeName
|
|
3804
|
-
},
|
|
3805
|
-
messages: {
|
|
3806
|
-
input: rawMessages,
|
|
3807
|
-
messageCount: rawMessages.length
|
|
3808
|
-
},
|
|
3809
|
-
technical: {
|
|
3810
|
-
environment: process.env.NODE_ENV
|
|
4042
|
+
return events;
|
|
4043
|
+
}), catchError((error) => {
|
|
4044
|
+
if (isStructuredCopilotKitError(error)) {
|
|
4045
|
+
throw error;
|
|
3811
4046
|
}
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
throw new CopilotKitAgentDiscoveryError({
|
|
3818
|
-
agentName,
|
|
3819
|
-
availableAgents: this.availableAgents
|
|
4047
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "LangGraph agent connection");
|
|
4048
|
+
throw new CopilotKitLowLevelError4({
|
|
4049
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
4050
|
+
url: "langgraph event stream",
|
|
4051
|
+
message: helpfulMessage
|
|
3820
4052
|
});
|
|
3821
|
-
}
|
|
3822
|
-
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
3823
|
-
// Case 1: Keep all regular (non-agent) actions
|
|
3824
|
-
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
3825
|
-
isRemoteAgentAction(action) && action.name !== agentName
|
|
3826
|
-
)).map((action) => ({
|
|
3827
|
-
name: action.name,
|
|
3828
|
-
description: action.description,
|
|
3829
|
-
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters))
|
|
3830
|
-
}));
|
|
3831
|
-
const allAvailableActions = flattenToolCallsNoDuplicates([
|
|
3832
|
-
...availableActionsForCurrentAgent,
|
|
3833
|
-
...request.actions
|
|
3834
|
-
]);
|
|
3835
|
-
if (((_a = this.observability) == null ? void 0 : _a.enabled) && publicApiKey) {
|
|
3836
|
-
try {
|
|
3837
|
-
const requestData = {
|
|
3838
|
-
threadId,
|
|
3839
|
-
runId: void 0,
|
|
3840
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3841
|
-
messages,
|
|
3842
|
-
actions: allAvailableActions,
|
|
3843
|
-
forwardedParameters,
|
|
3844
|
-
timestamp: requestStartTime,
|
|
3845
|
-
provider: "agent",
|
|
3846
|
-
agentName,
|
|
3847
|
-
nodeName
|
|
3848
|
-
};
|
|
3849
|
-
await this.observability.hooks.handleRequest(requestData);
|
|
3850
|
-
} catch (error) {
|
|
3851
|
-
console.error("Error logging agent request:", error);
|
|
3852
|
-
}
|
|
3853
|
-
}
|
|
3854
|
-
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
3855
|
-
threadId,
|
|
3856
|
-
runId: void 0,
|
|
3857
|
-
inputMessages: messages,
|
|
3858
|
-
properties: graphqlContext.properties
|
|
3859
4053
|
}));
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
|
|
3869
|
-
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
|
|
3874
|
-
|
|
3875
|
-
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
timestamp: Date.now(),
|
|
3885
|
-
provider: "agent",
|
|
3886
|
-
isProgressiveChunk: true,
|
|
3887
|
-
agentName,
|
|
3888
|
-
nodeName
|
|
3889
|
-
};
|
|
3890
|
-
Promise.resolve().then(() => {
|
|
3891
|
-
this.observability.hooks.handleResponse(progressiveData);
|
|
3892
|
-
}).catch((error) => {
|
|
3893
|
-
console.error("Error in progressive agent logging:", error);
|
|
3894
|
-
});
|
|
3895
|
-
} catch (error) {
|
|
3896
|
-
console.error("Error preparing progressive agent log data:", error);
|
|
3897
|
-
}
|
|
3898
|
-
}
|
|
3899
|
-
}
|
|
3900
|
-
});
|
|
3901
|
-
await callback(eventStream$);
|
|
3902
|
-
});
|
|
3903
|
-
};
|
|
3904
|
-
}
|
|
3905
|
-
eventSource.stream(async (eventStream$) => {
|
|
3906
|
-
from(stream).subscribe({
|
|
3907
|
-
next: (event) => eventStream$.next(event),
|
|
3908
|
-
error: async (err) => {
|
|
3909
|
-
var _a2;
|
|
3910
|
-
console.error("Error in stream", err);
|
|
3911
|
-
if (((_a2 = this.observability) == null ? void 0 : _a2.enabled) && publicApiKey) {
|
|
3912
|
-
try {
|
|
3913
|
-
const errorData = {
|
|
3914
|
-
threadId,
|
|
3915
|
-
runId: void 0,
|
|
3916
|
-
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
3917
|
-
error: err instanceof Error ? err : String(err),
|
|
3918
|
-
timestamp: Date.now(),
|
|
3919
|
-
latency: Date.now() - requestStartTime,
|
|
3920
|
-
provider: "agent",
|
|
3921
|
-
agentName,
|
|
3922
|
-
nodeName
|
|
3923
|
-
};
|
|
3924
|
-
this.observability.hooks.handleError(errorData);
|
|
3925
|
-
} catch (logError) {
|
|
3926
|
-
console.error("Error logging agent error:", logError);
|
|
3927
|
-
}
|
|
3928
|
-
}
|
|
3929
|
-
const structuredError = this.convertStreamingErrorToStructured(err);
|
|
3930
|
-
await this.trace("error", {
|
|
3931
|
-
threadId,
|
|
3932
|
-
source: "agent",
|
|
3933
|
-
request: {
|
|
3934
|
-
operation: "processAgentRequest",
|
|
3935
|
-
method: "POST",
|
|
3936
|
-
startTime: requestStartTime
|
|
3937
|
-
},
|
|
3938
|
-
response: {
|
|
3939
|
-
endTime: Date.now(),
|
|
3940
|
-
latency: Date.now() - requestStartTime
|
|
3941
|
-
},
|
|
3942
|
-
agent: {
|
|
3943
|
-
name: agentName,
|
|
3944
|
-
nodeName
|
|
3945
|
-
},
|
|
3946
|
-
technical: {
|
|
3947
|
-
environment: process.env.NODE_ENV,
|
|
3948
|
-
stackTrace: err instanceof Error ? err.stack : void 0
|
|
3949
|
-
}
|
|
3950
|
-
}, structuredError, publicApiKey);
|
|
3951
|
-
eventStream$.error(structuredError);
|
|
3952
|
-
eventStream$.complete();
|
|
3953
|
-
},
|
|
3954
|
-
complete: () => eventStream$.complete()
|
|
3955
|
-
});
|
|
4054
|
+
}
|
|
4055
|
+
};
|
|
4056
|
+
__name(RemoteLangGraphEventSource, "RemoteLangGraphEventSource");
|
|
4057
|
+
|
|
4058
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
4059
|
+
import { CopilotKitError as CopilotKitError4, CopilotKitLowLevelError as CopilotKitLowLevelError5 } from "@copilotkit/shared";
|
|
4060
|
+
import { CopilotKitApiDiscoveryError as CopilotKitApiDiscoveryError2, ResolvedCopilotKitError as ResolvedCopilotKitError2 } from "@copilotkit/shared";
|
|
4061
|
+
import { parseJson as parseJson2, tryMap as tryMap2 } from "@copilotkit/shared";
|
|
4062
|
+
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
4063
|
+
const agents = endpoint.agents.map((agent) => ({
|
|
4064
|
+
name: agent.name,
|
|
4065
|
+
description: agent.description,
|
|
4066
|
+
parameters: [],
|
|
4067
|
+
handler: async (_args) => {
|
|
4068
|
+
},
|
|
4069
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
4070
|
+
logger2.debug({
|
|
4071
|
+
actionName: agent.name
|
|
4072
|
+
}, "Executing LangGraph Platform agent");
|
|
4073
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4074
|
+
agentExecution: true,
|
|
4075
|
+
type: "langgraph-platform",
|
|
4076
|
+
agentsAmount: endpoint.agents.length,
|
|
4077
|
+
hashedLgcKey: endpoint.langsmithApiKey ? createHash3("sha256").update(endpoint.langsmithApiKey).digest("hex") : null
|
|
3956
4078
|
});
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
latency: Date.now() - requestStartTime,
|
|
3966
|
-
timestamp: Date.now(),
|
|
3967
|
-
provider: "agent",
|
|
3968
|
-
isFinalResponse: true,
|
|
3969
|
-
agentName,
|
|
3970
|
-
nodeName
|
|
3971
|
-
};
|
|
3972
|
-
try {
|
|
3973
|
-
this.observability.hooks.handleResponse(responseData);
|
|
3974
|
-
} catch (logError) {
|
|
3975
|
-
console.error("Error logging agent response:", logError);
|
|
3976
|
-
}
|
|
3977
|
-
}).catch((error) => {
|
|
3978
|
-
console.error("Failed to get output messages for agent logging:", error);
|
|
3979
|
-
});
|
|
4079
|
+
let state = {};
|
|
4080
|
+
let config = {};
|
|
4081
|
+
if (agentStates) {
|
|
4082
|
+
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
4083
|
+
if (jsonState) {
|
|
4084
|
+
state = parseJson2(jsonState.state, {});
|
|
4085
|
+
config = parseJson2(jsonState.config, {});
|
|
4086
|
+
}
|
|
3980
4087
|
}
|
|
3981
|
-
|
|
3982
|
-
|
|
3983
|
-
|
|
4088
|
+
try {
|
|
4089
|
+
const response = await execute({
|
|
4090
|
+
logger: logger2.child({
|
|
4091
|
+
component: "remote-actions.remote-lg-action.streamEvents"
|
|
4092
|
+
}),
|
|
4093
|
+
deploymentUrl: endpoint.deploymentUrl,
|
|
4094
|
+
langsmithApiKey: endpoint.langsmithApiKey,
|
|
4095
|
+
agent,
|
|
3984
4096
|
threadId,
|
|
3985
|
-
|
|
3986
|
-
|
|
3987
|
-
|
|
3988
|
-
|
|
4097
|
+
nodeName,
|
|
4098
|
+
messages: [
|
|
4099
|
+
...messages,
|
|
4100
|
+
...additionalMessages
|
|
4101
|
+
],
|
|
4102
|
+
state,
|
|
4103
|
+
config,
|
|
4104
|
+
properties: graphqlContext.properties,
|
|
4105
|
+
actions: tryMap2(actionInputsWithoutAgents, (action) => ({
|
|
4106
|
+
name: action.name,
|
|
4107
|
+
description: action.description,
|
|
4108
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
4109
|
+
})),
|
|
4110
|
+
metaEvents
|
|
3989
4111
|
});
|
|
3990
|
-
|
|
4112
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
4113
|
+
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
4114
|
+
return eventSource.processLangGraphEvents();
|
|
4115
|
+
} catch (error) {
|
|
4116
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
4117
|
+
if (isUserConfigurationError(error)) {
|
|
4118
|
+
logger2.debug({
|
|
4119
|
+
url: endpoint.deploymentUrl,
|
|
4120
|
+
error: error.message,
|
|
4121
|
+
code: error.code
|
|
4122
|
+
}, "User configuration error in LangGraph Platform agent");
|
|
4123
|
+
} else {
|
|
4124
|
+
logger2.error({
|
|
4125
|
+
url: endpoint.deploymentUrl,
|
|
4126
|
+
error: error.message,
|
|
4127
|
+
type: error.constructor.name
|
|
4128
|
+
}, "LangGraph Platform agent error");
|
|
4129
|
+
}
|
|
4130
|
+
throw error;
|
|
4131
|
+
}
|
|
4132
|
+
logger2.error({
|
|
4133
|
+
url: endpoint.deploymentUrl,
|
|
4134
|
+
status: 500,
|
|
4135
|
+
body: error.message
|
|
4136
|
+
}, "Failed to execute LangGraph Platform agent");
|
|
4137
|
+
throw new CopilotKitLowLevelError5({
|
|
4138
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
4139
|
+
url: endpoint.deploymentUrl,
|
|
4140
|
+
message: "Failed to execute LangGraph Platform agent"
|
|
4141
|
+
});
|
|
4142
|
+
}
|
|
4143
|
+
}
|
|
4144
|
+
}));
|
|
4145
|
+
return [
|
|
4146
|
+
...agents
|
|
4147
|
+
];
|
|
4148
|
+
}
|
|
4149
|
+
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
4150
|
+
var RemoteAgentType;
|
|
4151
|
+
(function(RemoteAgentType2) {
|
|
4152
|
+
RemoteAgentType2["LangGraph"] = "langgraph";
|
|
4153
|
+
RemoteAgentType2["CrewAI"] = "crewai";
|
|
4154
|
+
})(RemoteAgentType || (RemoteAgentType = {}));
|
|
4155
|
+
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
4156
|
+
const totalAgents = Array.isArray(json["agents"]) ? json["agents"].length : 0;
|
|
4157
|
+
const actions = json["actions"].map((action) => ({
|
|
4158
|
+
name: action.name,
|
|
4159
|
+
description: action.description,
|
|
4160
|
+
parameters: action.parameters,
|
|
4161
|
+
handler: async (args) => {
|
|
4162
|
+
logger2.debug({
|
|
4163
|
+
actionName: action.name,
|
|
4164
|
+
args
|
|
4165
|
+
}, "Executing remote action");
|
|
4166
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4167
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4168
|
+
agentExecution: false,
|
|
4169
|
+
type: "self-hosted",
|
|
4170
|
+
agentsAmount: totalAgents
|
|
3991
4171
|
});
|
|
3992
|
-
|
|
3993
|
-
|
|
3994
|
-
|
|
3995
|
-
|
|
3996
|
-
|
|
3997
|
-
|
|
3998
|
-
|
|
3999
|
-
|
|
4000
|
-
|
|
4001
|
-
|
|
4002
|
-
|
|
4003
|
-
|
|
4004
|
-
|
|
4005
|
-
|
|
4006
|
-
|
|
4007
|
-
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4172
|
+
const fetchUrl = `${url}/actions/execute`;
|
|
4173
|
+
try {
|
|
4174
|
+
const response = await fetchWithRetry(fetchUrl, {
|
|
4175
|
+
method: "POST",
|
|
4176
|
+
headers,
|
|
4177
|
+
body: JSON.stringify({
|
|
4178
|
+
name: action.name,
|
|
4179
|
+
arguments: args,
|
|
4180
|
+
properties: graphqlContext.properties
|
|
4181
|
+
})
|
|
4182
|
+
}, logger2);
|
|
4183
|
+
if (!response.ok) {
|
|
4184
|
+
logger2.error({
|
|
4185
|
+
url,
|
|
4186
|
+
status: response.status,
|
|
4187
|
+
body: await response.text()
|
|
4188
|
+
}, "Failed to execute remote action");
|
|
4189
|
+
if (response.status === 404) {
|
|
4190
|
+
throw new CopilotKitApiDiscoveryError2({
|
|
4191
|
+
url: fetchUrl
|
|
4192
|
+
});
|
|
4193
|
+
}
|
|
4194
|
+
throw new ResolvedCopilotKitError2({
|
|
4195
|
+
status: response.status,
|
|
4196
|
+
url: fetchUrl,
|
|
4197
|
+
isRemoteEndpoint: true
|
|
4198
|
+
});
|
|
4199
|
+
}
|
|
4200
|
+
const requestResult = await response.json();
|
|
4201
|
+
const result = requestResult["result"];
|
|
4202
|
+
logger2.debug({
|
|
4203
|
+
actionName: action.name,
|
|
4204
|
+
result
|
|
4205
|
+
}, "Executed remote action");
|
|
4206
|
+
return result;
|
|
4207
|
+
} catch (error) {
|
|
4208
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
4209
|
+
throw error;
|
|
4016
4210
|
}
|
|
4211
|
+
throw new CopilotKitLowLevelError5({
|
|
4212
|
+
error,
|
|
4213
|
+
url: fetchUrl
|
|
4214
|
+
});
|
|
4017
4215
|
}
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4216
|
+
}
|
|
4217
|
+
}));
|
|
4218
|
+
const agents = totalAgents ? json["agents"].map((agent) => ({
|
|
4219
|
+
name: agent.name,
|
|
4220
|
+
description: agent.description,
|
|
4221
|
+
parameters: [],
|
|
4222
|
+
handler: async (_args) => {
|
|
4223
|
+
},
|
|
4224
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
4225
|
+
logger2.debug({
|
|
4226
|
+
actionName: agent.name
|
|
4227
|
+
}, "Executing remote agent");
|
|
4228
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4229
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4230
|
+
agentExecution: true,
|
|
4231
|
+
type: "self-hosted",
|
|
4232
|
+
agentsAmount: json["agents"].length
|
|
4233
|
+
});
|
|
4234
|
+
let state = {};
|
|
4235
|
+
let config = {};
|
|
4236
|
+
if (agentStates) {
|
|
4237
|
+
const jsonState = agentStates.find((state2) => state2.agentName === name);
|
|
4238
|
+
if (jsonState) {
|
|
4239
|
+
state = parseJson2(jsonState.state, {});
|
|
4240
|
+
config = parseJson2(jsonState.config, {});
|
|
4241
|
+
}
|
|
4023
4242
|
}
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
request: {
|
|
4028
|
-
operation: "processAgentRequest",
|
|
4243
|
+
const fetchUrl = `${url}/agents/execute`;
|
|
4244
|
+
try {
|
|
4245
|
+
const response = await fetchWithRetry(fetchUrl, {
|
|
4029
4246
|
method: "POST",
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4247
|
+
headers,
|
|
4248
|
+
body: JSON.stringify({
|
|
4249
|
+
name,
|
|
4250
|
+
threadId,
|
|
4251
|
+
nodeName,
|
|
4252
|
+
messages: [
|
|
4253
|
+
...messages,
|
|
4254
|
+
...additionalMessages
|
|
4255
|
+
],
|
|
4256
|
+
state,
|
|
4257
|
+
config,
|
|
4258
|
+
properties: graphqlContext.properties,
|
|
4259
|
+
actions: tryMap2(actionInputsWithoutAgents, (action) => ({
|
|
4260
|
+
name: action.name,
|
|
4261
|
+
description: action.description,
|
|
4262
|
+
parameters: JSON.parse(action.jsonSchema)
|
|
4263
|
+
})),
|
|
4264
|
+
metaEvents
|
|
4265
|
+
})
|
|
4266
|
+
}, logger2);
|
|
4267
|
+
if (!response.ok) {
|
|
4268
|
+
logger2.error({
|
|
4269
|
+
url,
|
|
4270
|
+
status: response.status,
|
|
4271
|
+
body: await response.text()
|
|
4272
|
+
}, "Failed to execute remote agent");
|
|
4273
|
+
if (response.status === 404) {
|
|
4274
|
+
throw new CopilotKitApiDiscoveryError2({
|
|
4275
|
+
url: fetchUrl
|
|
4276
|
+
});
|
|
4277
|
+
}
|
|
4278
|
+
throw new ResolvedCopilotKitError2({
|
|
4279
|
+
status: response.status,
|
|
4280
|
+
url: fetchUrl,
|
|
4281
|
+
isRemoteEndpoint: true
|
|
4282
|
+
});
|
|
4283
|
+
}
|
|
4284
|
+
if (agent.type === "langgraph") {
|
|
4285
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
4286
|
+
writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
|
|
4287
|
+
return eventSource.processLangGraphEvents();
|
|
4288
|
+
} else if (agent.type === "crewai") {
|
|
4289
|
+
const eventStream$ = new RuntimeEventSubject();
|
|
4290
|
+
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
4291
|
+
return eventStream$;
|
|
4292
|
+
} else {
|
|
4293
|
+
throw new Error("Unsupported agent type");
|
|
4043
4294
|
}
|
|
4044
|
-
}, structuredError, publicApiKey);
|
|
4045
|
-
console.error("Error getting response:", error);
|
|
4046
|
-
throw structuredError;
|
|
4047
|
-
}
|
|
4048
|
-
}
|
|
4049
|
-
async getServerSideActions(request) {
|
|
4050
|
-
var _a, _b;
|
|
4051
|
-
const { graphqlContext, messages: rawMessages, agentStates, url } = request;
|
|
4052
|
-
const inputMessages = convertGqlInputToMessages(rawMessages);
|
|
4053
|
-
const langserveFunctions = [];
|
|
4054
|
-
for (const chainPromise of this.langserve) {
|
|
4055
|
-
try {
|
|
4056
|
-
const chain = await chainPromise;
|
|
4057
|
-
langserveFunctions.push(chain);
|
|
4058
4295
|
} catch (error) {
|
|
4059
|
-
|
|
4296
|
+
if (error instanceof CopilotKitError4 || error instanceof CopilotKitLowLevelError5) {
|
|
4297
|
+
throw error;
|
|
4298
|
+
}
|
|
4299
|
+
throw new CopilotKitLowLevelError5({
|
|
4300
|
+
error,
|
|
4301
|
+
url: fetchUrl
|
|
4302
|
+
});
|
|
4060
4303
|
}
|
|
4061
4304
|
}
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4305
|
+
})) : [];
|
|
4306
|
+
return [
|
|
4307
|
+
...actions,
|
|
4308
|
+
...agents
|
|
4309
|
+
];
|
|
4310
|
+
}
|
|
4311
|
+
__name(constructRemoteActions, "constructRemoteActions");
|
|
4312
|
+
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
4313
|
+
const headers = {
|
|
4314
|
+
"Content-Type": "application/json"
|
|
4315
|
+
};
|
|
4316
|
+
if (onBeforeRequest) {
|
|
4317
|
+
const { headers: additionalHeaders } = onBeforeRequest({
|
|
4318
|
+
ctx: graphqlContext
|
|
4074
4319
|
});
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4320
|
+
if (additionalHeaders) {
|
|
4321
|
+
Object.assign(headers, additionalHeaders);
|
|
4322
|
+
}
|
|
4323
|
+
}
|
|
4324
|
+
return headers;
|
|
4325
|
+
}
|
|
4326
|
+
__name(createHeaders, "createHeaders");
|
|
4327
|
+
|
|
4328
|
+
// src/lib/runtime/remote-actions.ts
|
|
4329
|
+
import { CopilotKitLowLevelError as CopilotKitLowLevelError6, ResolvedCopilotKitError as ResolvedCopilotKitError3, CopilotKitError as CopilotKitError5 } from "@copilotkit/shared";
|
|
4330
|
+
|
|
4331
|
+
// src/lib/runtime/agui-action.ts
|
|
4332
|
+
import { parseJson as parseJson3 } from "@copilotkit/shared";
|
|
4333
|
+
function constructAGUIRemoteAction({ logger: logger2, messages, agentStates, agent, metaEvents }) {
|
|
4334
|
+
const action = {
|
|
4335
|
+
name: agent.agentId,
|
|
4336
|
+
description: agent.description,
|
|
4337
|
+
parameters: [],
|
|
4338
|
+
handler: async (_args) => {
|
|
4339
|
+
},
|
|
4340
|
+
remoteAgentHandler: async ({ actionInputsWithoutAgents, threadId }) => {
|
|
4341
|
+
var _a;
|
|
4342
|
+
logger2.debug({
|
|
4343
|
+
actionName: agent.agentId
|
|
4344
|
+
}, "Executing remote agent");
|
|
4345
|
+
const agentWireMessages = convertMessagesToAGUIMessage(messages);
|
|
4346
|
+
agent.messages = agentWireMessages;
|
|
4347
|
+
agent.threadId = threadId;
|
|
4348
|
+
telemetry_client_default.capture("oss.runtime.remote_action_executed", {
|
|
4349
|
+
agentExecution: true,
|
|
4350
|
+
type: "self-hosted",
|
|
4351
|
+
agentsAmount: 1
|
|
4097
4352
|
});
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
const
|
|
4101
|
-
|
|
4102
|
-
|
|
4103
|
-
let client = null;
|
|
4104
|
-
try {
|
|
4105
|
-
client = await this.createMCPClientImpl(config);
|
|
4106
|
-
const tools = await client.tools();
|
|
4107
|
-
actionsForEndpoint = convertMCPToolsToActions(tools, endpointUrl);
|
|
4108
|
-
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
4109
|
-
} catch (error) {
|
|
4110
|
-
console.error(`MCP: Failed to fetch tools from endpoint ${endpointUrl}. Skipping. Error:`, error);
|
|
4111
|
-
actionsForEndpoint = [];
|
|
4112
|
-
this.mcpActionCache.set(endpointUrl, actionsForEndpoint);
|
|
4113
|
-
}
|
|
4353
|
+
let state = {};
|
|
4354
|
+
if (agentStates) {
|
|
4355
|
+
const jsonState = agentStates.find((state2) => state2.agentName === agent.agentId);
|
|
4356
|
+
if (jsonState) {
|
|
4357
|
+
state = parseJson3(jsonState.state, {});
|
|
4114
4358
|
}
|
|
4115
|
-
requestSpecificMCPActions.push(...actionsForEndpoint || []);
|
|
4116
4359
|
}
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4123
|
-
|
|
4124
|
-
}
|
|
4125
|
-
// Add helper method to detect provider
|
|
4126
|
-
detectProvider(serviceAdapter) {
|
|
4127
|
-
const adapterName = serviceAdapter.constructor.name;
|
|
4128
|
-
if (adapterName.includes("OpenAI"))
|
|
4129
|
-
return "openai";
|
|
4130
|
-
if (adapterName.includes("Anthropic"))
|
|
4131
|
-
return "anthropic";
|
|
4132
|
-
if (adapterName.includes("Google"))
|
|
4133
|
-
return "google";
|
|
4134
|
-
if (adapterName.includes("Groq"))
|
|
4135
|
-
return "groq";
|
|
4136
|
-
if (adapterName.includes("LangChain"))
|
|
4137
|
-
return "langchain";
|
|
4138
|
-
return void 0;
|
|
4139
|
-
}
|
|
4140
|
-
convertStreamingErrorToStructured(error) {
|
|
4141
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
4142
|
-
if (((_a = error == null ? void 0 : error.message) == null ? void 0 : _a.includes("terminated")) || ((_b = error == null ? void 0 : error.cause) == null ? void 0 : _b.code) === "UND_ERR_SOCKET" || ((_c = error == null ? void 0 : error.message) == null ? void 0 : _c.includes("other side closed")) || (error == null ? void 0 : error.code) === "UND_ERR_SOCKET") {
|
|
4143
|
-
return new CopilotKitError5({
|
|
4144
|
-
message: "Connection to agent was unexpectedly terminated. This may be due to the agent service being restarted or network issues. Please try again.",
|
|
4145
|
-
code: CopilotKitErrorCode3.NETWORK_ERROR
|
|
4360
|
+
agent.state = state;
|
|
4361
|
+
const tools = actionInputsWithoutAgents.map((input) => {
|
|
4362
|
+
return {
|
|
4363
|
+
name: input.name,
|
|
4364
|
+
description: input.description,
|
|
4365
|
+
parameters: JSON.parse(input.jsonSchema)
|
|
4366
|
+
};
|
|
4146
4367
|
});
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4368
|
+
const forwardedProps = metaEvents.length ? {
|
|
4369
|
+
command: {
|
|
4370
|
+
resume: (_a = metaEvents[0]) == null ? void 0 : _a.response
|
|
4371
|
+
}
|
|
4372
|
+
} : void 0;
|
|
4373
|
+
return agent.legacy_to_be_removed_runAgentBridged({
|
|
4374
|
+
tools,
|
|
4375
|
+
forwardedProps
|
|
4153
4376
|
});
|
|
4154
4377
|
}
|
|
4155
|
-
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4378
|
+
};
|
|
4379
|
+
return [
|
|
4380
|
+
action
|
|
4381
|
+
];
|
|
4382
|
+
}
|
|
4383
|
+
__name(constructAGUIRemoteAction, "constructAGUIRemoteAction");
|
|
4384
|
+
function convertMessagesToAGUIMessage(messages) {
|
|
4385
|
+
const result = [];
|
|
4386
|
+
for (const message of messages) {
|
|
4387
|
+
if (message.isTextMessage()) {
|
|
4388
|
+
result.push({
|
|
4389
|
+
id: message.id,
|
|
4390
|
+
role: message.role,
|
|
4391
|
+
content: message.content
|
|
4159
4392
|
});
|
|
4160
|
-
}
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
if (!this.onTrace)
|
|
4168
|
-
return;
|
|
4169
|
-
if (!publicApiKey) {
|
|
4170
|
-
if (!this.hasWarnedAboutTracing) {
|
|
4171
|
-
console.warn("CopilotKit: onTrace handler provided but requires publicApiKey for tracing to work. This is a CopilotKit Cloud feature. See: https://docs.copilotkit.ai/cloud");
|
|
4172
|
-
this.hasWarnedAboutTracing = true;
|
|
4173
|
-
}
|
|
4174
|
-
return;
|
|
4175
|
-
}
|
|
4176
|
-
try {
|
|
4177
|
-
const traceEvent = {
|
|
4178
|
-
type,
|
|
4179
|
-
timestamp: Date.now(),
|
|
4180
|
-
context,
|
|
4181
|
-
...error && {
|
|
4182
|
-
error
|
|
4393
|
+
} else if (message.isActionExecutionMessage()) {
|
|
4394
|
+
const toolCall = {
|
|
4395
|
+
id: message.id,
|
|
4396
|
+
type: "function",
|
|
4397
|
+
function: {
|
|
4398
|
+
name: message.name,
|
|
4399
|
+
arguments: JSON.stringify(message.arguments)
|
|
4183
4400
|
}
|
|
4184
4401
|
};
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
|
|
4190
|
-
|
|
4191
|
-
|
|
4192
|
-
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4201
|
-
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
},
|
|
4207
|
-
technical: {
|
|
4208
|
-
environment: process.env.NODE_ENV
|
|
4209
|
-
},
|
|
4210
|
-
metadata: {
|
|
4211
|
-
errorType: "GraphQLValidationError",
|
|
4212
|
-
cloudConfigPresent: context.cloudConfigPresent,
|
|
4213
|
-
guardrailsEnabled: context.guardrailsEnabled
|
|
4214
|
-
}
|
|
4215
|
-
},
|
|
4216
|
-
error
|
|
4402
|
+
if (message.parentMessageId && result.some((m) => m.id === message.parentMessageId)) {
|
|
4403
|
+
const parentMessage = result.find((m) => m.id === message.parentMessageId);
|
|
4404
|
+
if (parentMessage.toolCalls === void 0) {
|
|
4405
|
+
parentMessage.toolCalls = [];
|
|
4406
|
+
}
|
|
4407
|
+
parentMessage.toolCalls.push(toolCall);
|
|
4408
|
+
} else {
|
|
4409
|
+
result.push({
|
|
4410
|
+
id: message.parentMessageId ?? message.id,
|
|
4411
|
+
role: "assistant",
|
|
4412
|
+
toolCalls: [
|
|
4413
|
+
toolCall
|
|
4414
|
+
]
|
|
4415
|
+
});
|
|
4416
|
+
}
|
|
4417
|
+
} else if (message.isResultMessage()) {
|
|
4418
|
+
result.push({
|
|
4419
|
+
id: message.id,
|
|
4420
|
+
role: "tool",
|
|
4421
|
+
content: message.result,
|
|
4422
|
+
toolCallId: message.actionExecutionId
|
|
4217
4423
|
});
|
|
4218
|
-
} catch (traceError) {
|
|
4219
|
-
console.error("Error in onTrace handler:", traceError);
|
|
4220
|
-
}
|
|
4221
|
-
}
|
|
4222
|
-
};
|
|
4223
|
-
__name(CopilotRuntime, "CopilotRuntime");
|
|
4224
|
-
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
4225
|
-
let allTools = [];
|
|
4226
|
-
const allToolNames = [];
|
|
4227
|
-
for (const tool of toolsByPriority) {
|
|
4228
|
-
if (!allToolNames.includes(tool.name)) {
|
|
4229
|
-
allTools.push(tool);
|
|
4230
|
-
allToolNames.push(tool.name);
|
|
4231
4424
|
}
|
|
4232
4425
|
}
|
|
4233
|
-
return
|
|
4234
|
-
}
|
|
4235
|
-
__name(flattenToolCallsNoDuplicates, "flattenToolCallsNoDuplicates");
|
|
4236
|
-
function copilotKitEndpoint(config) {
|
|
4237
|
-
return {
|
|
4238
|
-
...config,
|
|
4239
|
-
type: EndpointType.CopilotKit
|
|
4240
|
-
};
|
|
4426
|
+
return result;
|
|
4241
4427
|
}
|
|
4242
|
-
__name(
|
|
4243
|
-
|
|
4244
|
-
|
|
4245
|
-
|
|
4246
|
-
|
|
4247
|
-
|
|
4428
|
+
__name(convertMessagesToAGUIMessage, "convertMessagesToAGUIMessage");
|
|
4429
|
+
|
|
4430
|
+
// src/lib/runtime/remote-actions.ts
|
|
4431
|
+
var EndpointType;
|
|
4432
|
+
(function(EndpointType2) {
|
|
4433
|
+
EndpointType2["CopilotKit"] = "copilotKit";
|
|
4434
|
+
EndpointType2["LangGraphPlatform"] = "langgraph-platform";
|
|
4435
|
+
})(EndpointType || (EndpointType = {}));
|
|
4436
|
+
function isRemoteAgentAction(action) {
|
|
4437
|
+
if (!action) {
|
|
4438
|
+
return false;
|
|
4439
|
+
}
|
|
4440
|
+
return typeof action.remoteAgentHandler === "function";
|
|
4248
4441
|
}
|
|
4249
|
-
__name(
|
|
4250
|
-
function
|
|
4251
|
-
|
|
4252
|
-
|
|
4253
|
-
|
|
4254
|
-
|
|
4255
|
-
|
|
4442
|
+
__name(isRemoteAgentAction, "isRemoteAgentAction");
|
|
4443
|
+
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
4444
|
+
logger2.debug({
|
|
4445
|
+
url
|
|
4446
|
+
}, "Fetching actions from url");
|
|
4447
|
+
const headers = createHeaders(onBeforeRequest, graphqlContext);
|
|
4448
|
+
const fetchUrl = `${url}/info`;
|
|
4449
|
+
try {
|
|
4450
|
+
const response = await fetch(fetchUrl, {
|
|
4451
|
+
method: "POST",
|
|
4452
|
+
headers,
|
|
4453
|
+
body: JSON.stringify({
|
|
4454
|
+
properties: graphqlContext.properties,
|
|
4455
|
+
frontendUrl
|
|
4456
|
+
})
|
|
4457
|
+
});
|
|
4458
|
+
if (!response.ok) {
|
|
4459
|
+
logger2.error({
|
|
4460
|
+
url,
|
|
4461
|
+
status: response.status,
|
|
4462
|
+
body: await response.text()
|
|
4463
|
+
}, "Failed to fetch actions from url");
|
|
4464
|
+
throw new ResolvedCopilotKitError3({
|
|
4465
|
+
status: response.status,
|
|
4466
|
+
url: fetchUrl,
|
|
4467
|
+
isRemoteEndpoint: true
|
|
4468
|
+
});
|
|
4469
|
+
}
|
|
4470
|
+
const json = await response.json();
|
|
4471
|
+
logger2.debug({
|
|
4472
|
+
json
|
|
4473
|
+
}, "Fetched actions from url");
|
|
4474
|
+
return json;
|
|
4475
|
+
} catch (error) {
|
|
4476
|
+
if (error instanceof CopilotKitError5) {
|
|
4477
|
+
throw error;
|
|
4256
4478
|
}
|
|
4479
|
+
throw new CopilotKitLowLevelError6({
|
|
4480
|
+
error,
|
|
4481
|
+
url: fetchUrl
|
|
4482
|
+
});
|
|
4257
4483
|
}
|
|
4258
|
-
return endpoint.type;
|
|
4259
4484
|
}
|
|
4260
|
-
__name(
|
|
4261
|
-
|
|
4262
|
-
|
|
4263
|
-
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
|
|
4267
|
-
});
|
|
4268
|
-
|
|
4269
|
-
|
|
4270
|
-
|
|
4271
|
-
const endpointsInfo = runtime.remoteEndpointDefinitions.reduce((acc, endpoint) => {
|
|
4272
|
-
let info = {
|
|
4273
|
-
...acc
|
|
4274
|
-
};
|
|
4275
|
-
const endpointType = resolveEndpointType(endpoint);
|
|
4276
|
-
if (!info.endpointTypes.includes(endpointType)) {
|
|
4277
|
-
info = {
|
|
4278
|
-
...info,
|
|
4279
|
-
endpointTypes: [
|
|
4280
|
-
...info.endpointTypes,
|
|
4281
|
-
endpointType
|
|
4282
|
-
]
|
|
4283
|
-
};
|
|
4284
|
-
}
|
|
4285
|
-
if (endpointType === EndpointType.LangGraphPlatform) {
|
|
4286
|
-
const ep = endpoint;
|
|
4287
|
-
info = {
|
|
4288
|
-
...info,
|
|
4289
|
-
agentsAmount: ep.agents.length,
|
|
4290
|
-
hashedKey: ep.langsmithApiKey ? createHash3("sha256").update(ep.langsmithApiKey).digest("hex") : null
|
|
4291
|
-
};
|
|
4485
|
+
__name(fetchRemoteInfo, "fetchRemoteInfo");
|
|
4486
|
+
async function setupRemoteActions({ remoteEndpointDefinitions, graphqlContext, messages, agentStates, frontendUrl, agents, metaEvents }) {
|
|
4487
|
+
const logger2 = graphqlContext.logger.child({
|
|
4488
|
+
component: "remote-actions.fetchRemoteActions"
|
|
4489
|
+
});
|
|
4490
|
+
logger2.debug({
|
|
4491
|
+
remoteEndpointDefinitions
|
|
4492
|
+
}, "Fetching from remote endpoints");
|
|
4493
|
+
const filtered = remoteEndpointDefinitions.filter((value, index, self) => {
|
|
4494
|
+
if (value.type === "langgraph-platform") {
|
|
4495
|
+
return value;
|
|
4292
4496
|
}
|
|
4293
|
-
return
|
|
4294
|
-
}, {
|
|
4295
|
-
endpointTypes: [],
|
|
4296
|
-
agentsAmount: null,
|
|
4297
|
-
hashedKey: null
|
|
4497
|
+
return index === self.findIndex((t) => t.url === value.url);
|
|
4298
4498
|
});
|
|
4299
|
-
const
|
|
4300
|
-
|
|
4301
|
-
|
|
4302
|
-
|
|
4303
|
-
|
|
4304
|
-
|
|
4305
|
-
|
|
4306
|
-
|
|
4307
|
-
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
|
|
4311
|
-
|
|
4312
|
-
|
|
4313
|
-
|
|
4314
|
-
|
|
4499
|
+
const result = await Promise.all(filtered.map(async (endpoint) => {
|
|
4500
|
+
if (endpoint.type === "langgraph-platform") {
|
|
4501
|
+
return constructLGCRemoteAction({
|
|
4502
|
+
endpoint,
|
|
4503
|
+
messages,
|
|
4504
|
+
graphqlContext,
|
|
4505
|
+
logger: logger2.child({
|
|
4506
|
+
component: "remote-actions.constructLGCRemoteAction",
|
|
4507
|
+
endpoint
|
|
4508
|
+
}),
|
|
4509
|
+
agentStates
|
|
4510
|
+
});
|
|
4511
|
+
}
|
|
4512
|
+
const json = await fetchRemoteInfo({
|
|
4513
|
+
url: endpoint.url,
|
|
4514
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
4515
|
+
graphqlContext,
|
|
4516
|
+
logger: logger2.child({
|
|
4517
|
+
component: "remote-actions.fetchActionsFromUrl",
|
|
4518
|
+
endpoint
|
|
4519
|
+
}),
|
|
4520
|
+
frontendUrl
|
|
4521
|
+
});
|
|
4522
|
+
return constructRemoteActions({
|
|
4523
|
+
json,
|
|
4524
|
+
messages,
|
|
4525
|
+
url: endpoint.url,
|
|
4526
|
+
onBeforeRequest: endpoint.onBeforeRequest,
|
|
4527
|
+
graphqlContext,
|
|
4528
|
+
logger: logger2.child({
|
|
4529
|
+
component: "remote-actions.constructActions",
|
|
4530
|
+
endpoint
|
|
4531
|
+
}),
|
|
4532
|
+
agentStates
|
|
4533
|
+
});
|
|
4534
|
+
}));
|
|
4535
|
+
for (const [key, agent] of Object.entries(agents)) {
|
|
4536
|
+
if (agent.agentId !== void 0 && agent.agentId !== key) {
|
|
4537
|
+
throw new CopilotKitError5({
|
|
4538
|
+
message: `Agent ${key} has agentId ${agent.agentId} which does not match the key ${key}`,
|
|
4539
|
+
code: CopilotKitErrorCode3.UNKNOWN
|
|
4540
|
+
});
|
|
4541
|
+
} else if (agent.agentId === void 0) {
|
|
4542
|
+
agent.agentId = key;
|
|
4543
|
+
}
|
|
4544
|
+
result.push(constructAGUIRemoteAction({
|
|
4545
|
+
logger: logger2,
|
|
4546
|
+
messages,
|
|
4547
|
+
agentStates,
|
|
4548
|
+
agent,
|
|
4549
|
+
metaEvents
|
|
4550
|
+
}));
|
|
4551
|
+
}
|
|
4552
|
+
return result.flat();
|
|
4315
4553
|
}
|
|
4316
|
-
__name(
|
|
4317
|
-
var telemetry_client_default = telemetryClient;
|
|
4554
|
+
__name(setupRemoteActions, "setupRemoteActions");
|
|
4318
4555
|
|
|
4319
4556
|
// src/service-adapters/events.ts
|
|
4320
|
-
import { plainToInstance as plainToInstance2 } from "class-transformer";
|
|
4321
4557
|
var RuntimeEventTypes;
|
|
4322
4558
|
(function(RuntimeEventTypes2) {
|
|
4323
4559
|
RuntimeEventTypes2["TextMessageStart"] = "TextMessageStart";
|
|
@@ -4440,16 +4676,15 @@ var RuntimeEventSource = class {
|
|
|
4440
4676
|
const errorMessage = `\u274C ${message}`;
|
|
4441
4677
|
if (!this.callback) {
|
|
4442
4678
|
this.stream(async (eventStream$) => {
|
|
4443
|
-
eventStream$.sendTextMessage(
|
|
4679
|
+
eventStream$.sendTextMessage(randomId2(), errorMessage);
|
|
4444
4680
|
});
|
|
4445
4681
|
} else {
|
|
4446
|
-
this.eventStream$.sendTextMessage(
|
|
4682
|
+
this.eventStream$.sendTextMessage(randomId2(), errorMessage);
|
|
4447
4683
|
}
|
|
4448
4684
|
}
|
|
4449
4685
|
processRuntimeEvents({ serverSideActions, guardrailsResult$, actionInputsWithoutAgents, threadId }) {
|
|
4450
4686
|
this.callback(this.eventStream$).catch((error) => {
|
|
4451
|
-
|
|
4452
|
-
const structuredError = convertStreamingErrorToStructured2(error);
|
|
4687
|
+
const structuredError = ensureStructuredError3(error, convertStreamingErrorToStructured2);
|
|
4453
4688
|
this.eventStream$.error(structuredError);
|
|
4454
4689
|
this.eventStream$.complete();
|
|
4455
4690
|
});
|
|
@@ -4484,12 +4719,10 @@ var RuntimeEventSource = class {
|
|
|
4484
4719
|
if (eventWithState.event.type === "ActionExecutionEnd" && eventWithState.callActionServerSide) {
|
|
4485
4720
|
const toolCallEventStream$ = new RuntimeEventSubject();
|
|
4486
4721
|
executeAction(toolCallEventStream$, guardrailsResult$ ? guardrailsResult$ : null, eventWithState.action, eventWithState.args, eventWithState.actionExecutionParentMessageId, eventWithState.actionExecutionId, actionInputsWithoutAgents, threadId).catch((error) => {
|
|
4487
|
-
console.error(error);
|
|
4488
4722
|
});
|
|
4489
4723
|
telemetry_client_default.capture("oss.runtime.server_action_executed", {});
|
|
4490
4724
|
return concat(of(eventWithState.event), toolCallEventStream$).pipe(catchError2((error) => {
|
|
4491
|
-
|
|
4492
|
-
const structuredError = convertStreamingErrorToStructured2(error);
|
|
4725
|
+
const structuredError = ensureStructuredError3(error, convertStreamingErrorToStructured2);
|
|
4493
4726
|
toolCallEventStream$.sendActionExecutionResult({
|
|
4494
4727
|
actionExecutionId: eventWithState.actionExecutionId,
|
|
4495
4728
|
actionName: eventWithState.action.name,
|
|
@@ -4569,8 +4802,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
4569
4802
|
from2(stream).subscribe({
|
|
4570
4803
|
next: (event) => eventStream$.next(event),
|
|
4571
4804
|
error: (err) => {
|
|
4572
|
-
|
|
4573
|
-
const structuredError = convertStreamingErrorToStructured2(err);
|
|
4805
|
+
const structuredError = ensureStructuredError3(err, convertStreamingErrorToStructured2);
|
|
4574
4806
|
eventStream$.sendActionExecutionResult({
|
|
4575
4807
|
actionExecutionId,
|
|
4576
4808
|
actionName: action.name,
|
|
@@ -4610,36 +4842,19 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
4610
4842
|
}
|
|
4611
4843
|
__name(executeAction, "executeAction");
|
|
4612
4844
|
function convertStreamingErrorToStructured2(error) {
|
|
4613
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
4614
|
-
|
|
4615
|
-
|
|
4616
|
-
|
|
4617
|
-
code: CopilotKitErrorCode4.NETWORK_ERROR
|
|
4618
|
-
});
|
|
4619
|
-
}
|
|
4620
|
-
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"))) {
|
|
4621
|
-
return new CopilotKitLowLevelError5({
|
|
4845
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
4846
|
+
let helpfulMessage = generateHelpfulErrorMessage(error, "event streaming connection");
|
|
4847
|
+
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") {
|
|
4848
|
+
return new CopilotKitLowLevelError7({
|
|
4622
4849
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
4623
4850
|
url: "event streaming connection",
|
|
4624
|
-
message:
|
|
4625
|
-
});
|
|
4626
|
-
}
|
|
4627
|
-
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"))) {
|
|
4628
|
-
return new CopilotKitError6({
|
|
4629
|
-
message: "Request was cancelled",
|
|
4630
|
-
code: CopilotKitErrorCode4.UNKNOWN
|
|
4631
|
-
});
|
|
4632
|
-
}
|
|
4633
|
-
const errorMessage = (error == null ? void 0 : error.message) || String(error);
|
|
4634
|
-
if (errorMessage.includes("401") || errorMessage.toLowerCase().includes("api key") || errorMessage.toLowerCase().includes("unauthorized") || errorMessage.toLowerCase().includes("authentication") || errorMessage.toLowerCase().includes("incorrect api key")) {
|
|
4635
|
-
return new CopilotKitError6({
|
|
4636
|
-
message: `Event streaming error: ${errorMessage}`,
|
|
4637
|
-
code: CopilotKitErrorCode4.MISSING_PUBLIC_API_KEY_ERROR
|
|
4851
|
+
message: helpfulMessage
|
|
4638
4852
|
});
|
|
4639
4853
|
}
|
|
4640
4854
|
return new CopilotKitError6({
|
|
4641
|
-
message:
|
|
4642
|
-
code: CopilotKitErrorCode4.UNKNOWN
|
|
4855
|
+
message: helpfulMessage,
|
|
4856
|
+
code: CopilotKitErrorCode4.UNKNOWN,
|
|
4857
|
+
severity: Severity.CRITICAL
|
|
4643
4858
|
});
|
|
4644
4859
|
}
|
|
4645
4860
|
__name(convertStreamingErrorToStructured2, "convertStreamingErrorToStructured");
|
|
@@ -4648,7 +4863,7 @@ __name(convertStreamingErrorToStructured2, "convertStreamingErrorToStructured");
|
|
|
4648
4863
|
import { GraphQLJSONObject } from "graphql-scalars";
|
|
4649
4864
|
import { plainToInstance as plainToInstance3 } from "class-transformer";
|
|
4650
4865
|
import { GraphQLError } from "graphql";
|
|
4651
|
-
import { randomId as
|
|
4866
|
+
import { randomId as randomId3 } from "@copilotkit/shared";
|
|
4652
4867
|
|
|
4653
4868
|
// src/graphql/types/agents-response.type.ts
|
|
4654
4869
|
import { Field as Field16, ObjectType as ObjectType5 } from "type-graphql";
|
|
@@ -4704,7 +4919,7 @@ AgentsResponse = _ts_decorate16([
|
|
|
4704
4919
|
], AgentsResponse);
|
|
4705
4920
|
|
|
4706
4921
|
// src/graphql/resolvers/copilot.resolver.ts
|
|
4707
|
-
import { CopilotKitError as CopilotKitError7 } from "@copilotkit/shared";
|
|
4922
|
+
import { CopilotKitError as CopilotKitError7, CopilotKitLowLevelError as CopilotKitLowLevelError8, isStructuredCopilotKitError as isStructuredCopilotKitError2 } from "@copilotkit/shared";
|
|
4708
4923
|
function _ts_decorate17(decorators, target, key, desc) {
|
|
4709
4924
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4710
4925
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -4774,7 +4989,7 @@ var CopilotResolver = class {
|
|
|
4774
4989
|
};
|
|
4775
4990
|
}
|
|
4776
4991
|
async generateCopilotResponse(ctx, data, properties) {
|
|
4777
|
-
var _a, _b, _c, _d, _e;
|
|
4992
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
4778
4993
|
telemetry_client_default.capture("oss.runtime.copilot_request_created", {
|
|
4779
4994
|
"cloud.guardrails.enabled": ((_a = data.cloud) == null ? void 0 : _a.guardrails) !== void 0,
|
|
4780
4995
|
requestType: data.metadata.requestType,
|
|
@@ -4853,26 +5068,41 @@ var CopilotResolver = class {
|
|
|
4853
5068
|
ctx.properties["copilotCloudPublicApiKey"] = copilotCloudPublicApiKey;
|
|
4854
5069
|
}
|
|
4855
5070
|
logger2.debug("Processing");
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
5071
|
+
let runtimeResponse;
|
|
5072
|
+
try {
|
|
5073
|
+
runtimeResponse = await copilotRuntime.processRuntimeRequest({
|
|
5074
|
+
serviceAdapter,
|
|
5075
|
+
messages: data.messages,
|
|
5076
|
+
actions: data.frontend.actions.filter((action) => action.available !== ActionInputAvailability.disabled),
|
|
5077
|
+
threadId: data.threadId,
|
|
5078
|
+
runId: data.runId,
|
|
5079
|
+
publicApiKey: copilotCloudPublicApiKey,
|
|
5080
|
+
outputMessagesPromise,
|
|
5081
|
+
graphqlContext: ctx,
|
|
5082
|
+
forwardedParameters: data.forwardedParameters,
|
|
5083
|
+
agentSession: data.agentSession,
|
|
5084
|
+
agentStates: data.agentStates,
|
|
5085
|
+
url: data.frontend.url,
|
|
5086
|
+
extensions: data.extensions,
|
|
5087
|
+
metaEvents: data.metaEvents
|
|
5088
|
+
});
|
|
5089
|
+
} catch (error) {
|
|
5090
|
+
if (isStructuredCopilotKitError2(error) || ((_e = error == null ? void 0 : error.extensions) == null ? void 0 : _e.visibility)) {
|
|
5091
|
+
throw new GraphQLError(error.message || "Agent error occurred", {
|
|
5092
|
+
extensions: {
|
|
5093
|
+
...error.extensions,
|
|
5094
|
+
code: error.code || ((_f = error.extensions) == null ? void 0 : _f.code) || "AGENT_ERROR",
|
|
5095
|
+
originalError: error
|
|
5096
|
+
}
|
|
5097
|
+
});
|
|
5098
|
+
}
|
|
5099
|
+
throw error;
|
|
5100
|
+
}
|
|
5101
|
+
const { eventSource, threadId = randomId3(), runId, serverSideActions, actionInputsWithoutAgents, extensions } = runtimeResponse;
|
|
4872
5102
|
logger2.debug("Event source created, creating response");
|
|
4873
5103
|
const eventStream = eventSource.processRuntimeEvents({
|
|
4874
5104
|
serverSideActions,
|
|
4875
|
-
guardrailsResult$: ((
|
|
5105
|
+
guardrailsResult$: ((_g = data.cloud) == null ? void 0 : _g.guardrails) ? guardrailsResult$ : null,
|
|
4876
5106
|
actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
|
|
4877
5107
|
// TODO-AGENTS: do not exclude ALL server side actions
|
|
4878
5108
|
(action) => !serverSideActions.find((serverSideAction) => serverSideAction.name == action.name)
|
|
@@ -4953,12 +5183,16 @@ var CopilotResolver = class {
|
|
|
4953
5183
|
}
|
|
4954
5184
|
},
|
|
4955
5185
|
error: (err) => {
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
}
|
|
5186
|
+
var _a2, _b2;
|
|
5187
|
+
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)) {
|
|
5188
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5189
|
+
description: err.message || "Agent error occurred"
|
|
5190
|
+
}));
|
|
5191
|
+
} else {
|
|
5192
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5193
|
+
description: `An unknown error has occurred in the event stream`
|
|
5194
|
+
}));
|
|
5195
|
+
}
|
|
4962
5196
|
eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
|
|
4963
5197
|
stop();
|
|
4964
5198
|
},
|
|
@@ -4996,7 +5230,7 @@ var CopilotResolver = class {
|
|
|
4996
5230
|
});
|
|
4997
5231
|
outputMessages = [
|
|
4998
5232
|
plainToInstance3(TextMessage, {
|
|
4999
|
-
id:
|
|
5233
|
+
id: randomId3(),
|
|
5000
5234
|
createdAt: /* @__PURE__ */ new Date(),
|
|
5001
5235
|
content: result.reason,
|
|
5002
5236
|
role: MessageRole.assistant
|
|
@@ -5174,7 +5408,7 @@ var CopilotResolver = class {
|
|
|
5174
5408
|
event
|
|
5175
5409
|
}, "Agent message event received");
|
|
5176
5410
|
pushMessage({
|
|
5177
|
-
id:
|
|
5411
|
+
id: randomId3(),
|
|
5178
5412
|
status: new SuccessMessageStatus(),
|
|
5179
5413
|
threadId: event.threadId,
|
|
5180
5414
|
agentName: event.agentName,
|
|
@@ -5187,7 +5421,7 @@ var CopilotResolver = class {
|
|
|
5187
5421
|
createdAt: /* @__PURE__ */ new Date()
|
|
5188
5422
|
});
|
|
5189
5423
|
outputMessages.push(plainToInstance3(AgentStateMessage, {
|
|
5190
|
-
id:
|
|
5424
|
+
id: randomId3(),
|
|
5191
5425
|
threadId: event.threadId,
|
|
5192
5426
|
agentName: event.agentName,
|
|
5193
5427
|
nodeName: event.nodeName,
|
|
@@ -5202,13 +5436,23 @@ var CopilotResolver = class {
|
|
|
5202
5436
|
}
|
|
5203
5437
|
},
|
|
5204
5438
|
error: (err) => {
|
|
5205
|
-
|
|
5206
|
-
|
|
5207
|
-
|
|
5208
|
-
|
|
5439
|
+
var _a3, _b2, _c2, _d2, _e2, _f2;
|
|
5440
|
+
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)) {
|
|
5441
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
5442
|
+
description: err.message || "Agent error occurred",
|
|
5443
|
+
// Include original error information for frontend to extract
|
|
5444
|
+
originalError: {
|
|
5445
|
+
code: err.code || ((_b2 = err.extensions) == null ? void 0 : _b2.code),
|
|
5446
|
+
statusCode: err.statusCode || ((_c2 = err.extensions) == null ? void 0 : _c2.statusCode),
|
|
5447
|
+
severity: err.severity || ((_d2 = err.extensions) == null ? void 0 : _d2.severity),
|
|
5448
|
+
visibility: err.visibility || ((_e2 = err.extensions) == null ? void 0 : _e2.visibility),
|
|
5449
|
+
originalErrorType: err.originalErrorType || ((_f2 = err.extensions) == null ? void 0 : _f2.originalErrorType),
|
|
5450
|
+
extensions: err.extensions
|
|
5451
|
+
}
|
|
5452
|
+
}));
|
|
5209
5453
|
eventStreamSubscription == null ? void 0 : eventStreamSubscription.unsubscribe();
|
|
5210
5454
|
rejectOutputMessagesPromise(err);
|
|
5211
|
-
stopStreamingMessages(
|
|
5455
|
+
stopStreamingMessages();
|
|
5212
5456
|
return;
|
|
5213
5457
|
}
|
|
5214
5458
|
responseStatus$.next(new UnknownErrorResponse({
|
|
@@ -5446,6 +5690,7 @@ StateResolver = _ts_decorate20([
|
|
|
5446
5690
|
|
|
5447
5691
|
// src/lib/integrations/shared.ts
|
|
5448
5692
|
var packageJson2 = __toESM(require_package());
|
|
5693
|
+
import { CopilotKitError as CopilotKitError8, CopilotKitErrorCode as CopilotKitErrorCode5 } from "@copilotkit/shared";
|
|
5449
5694
|
var logger = createLogger();
|
|
5450
5695
|
var addCustomHeaderPlugin = {
|
|
5451
5696
|
onResponse({ response }) {
|
|
@@ -5510,6 +5755,13 @@ function getCommonConfig(options) {
|
|
|
5510
5755
|
serviceAdapter: options.serviceAdapter.constructor.name
|
|
5511
5756
|
}
|
|
5512
5757
|
});
|
|
5758
|
+
const userErrorCodes = [
|
|
5759
|
+
CopilotKitErrorCode5.AGENT_NOT_FOUND,
|
|
5760
|
+
CopilotKitErrorCode5.API_NOT_FOUND,
|
|
5761
|
+
CopilotKitErrorCode5.REMOTE_ENDPOINT_NOT_FOUND,
|
|
5762
|
+
CopilotKitErrorCode5.CONFIGURATION_ERROR,
|
|
5763
|
+
CopilotKitErrorCode5.MISSING_PUBLIC_API_KEY_ERROR
|
|
5764
|
+
];
|
|
5513
5765
|
return {
|
|
5514
5766
|
logging: createLogger({
|
|
5515
5767
|
component: "Yoga GraphQL",
|
|
@@ -5520,7 +5772,25 @@ function getCommonConfig(options) {
|
|
|
5520
5772
|
useDeferStream(),
|
|
5521
5773
|
addCustomHeaderPlugin
|
|
5522
5774
|
],
|
|
5523
|
-
context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
|
|
5775
|
+
context: (ctx) => createContext(ctx, options, contextLogger, options.properties),
|
|
5776
|
+
// Suppress logging for user configuration errors
|
|
5777
|
+
maskedErrors: {
|
|
5778
|
+
maskError: (error, message, isDev) => {
|
|
5779
|
+
const originalError = error.originalError || error;
|
|
5780
|
+
const extensions = error.extensions;
|
|
5781
|
+
const errorCode = extensions == null ? void 0 : extensions.code;
|
|
5782
|
+
if (errorCode && userErrorCodes.includes(errorCode)) {
|
|
5783
|
+
console.debug("User configuration error:", error.message);
|
|
5784
|
+
return error;
|
|
5785
|
+
}
|
|
5786
|
+
if (originalError instanceof CopilotKitError8 && userErrorCodes.includes(originalError.code)) {
|
|
5787
|
+
console.debug("User configuration error:", error.message);
|
|
5788
|
+
return error;
|
|
5789
|
+
}
|
|
5790
|
+
console.error("Application error:", error);
|
|
5791
|
+
return error;
|
|
5792
|
+
}
|
|
5793
|
+
}
|
|
5524
5794
|
};
|
|
5525
5795
|
}
|
|
5526
5796
|
__name(getCommonConfig, "getCommonConfig");
|
|
@@ -5569,4 +5839,4 @@ export {
|
|
|
5569
5839
|
getCommonConfig,
|
|
5570
5840
|
copilotRuntimeNodeHttpEndpoint
|
|
5571
5841
|
};
|
|
5572
|
-
//# sourceMappingURL=chunk-
|
|
5842
|
+
//# sourceMappingURL=chunk-Z5GYTKMD.mjs.map
|