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