@copilotkit/core 1.79.1 → 1.80.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +443 -75
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +47 -12
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +47 -12
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +444 -76
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +442 -82
- package/dist/index.umd.js.map +1 -1
- package/package.json +3 -3
package/dist/index.umd.js
CHANGED
|
@@ -908,6 +908,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
908
908
|
}
|
|
909
909
|
const repaired = [];
|
|
910
910
|
const emitted = /* @__PURE__ */ new Set();
|
|
911
|
+
const expandedMessages = /* @__PURE__ */ new Set();
|
|
911
912
|
const emit = (message) => {
|
|
912
913
|
if (emitted.has(message.id)) return;
|
|
913
914
|
emitted.add(message.id);
|
|
@@ -916,6 +917,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
916
917
|
/** Emit an assistant turn together with every result it is owed. */
|
|
917
918
|
const emitWithResults = (message) => {
|
|
918
919
|
emit(message);
|
|
920
|
+
if (expandedMessages.has(message)) return;
|
|
921
|
+
expandedMessages.add(message);
|
|
919
922
|
for (const callId of toolCallIdsOf(message)) {
|
|
920
923
|
const result = callIdToResult.get(callId);
|
|
921
924
|
if (result) emit(result);
|
|
@@ -936,6 +939,199 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
936
939
|
}
|
|
937
940
|
return repaired;
|
|
938
941
|
}
|
|
942
|
+
/**
|
|
943
|
+
* Move each detached tool result to sit behind the assistant call that issued
|
|
944
|
+
* it — usually back to an earlier call, and forward only when no earlier call
|
|
945
|
+
* has its id and exactly one call in the list does — and change nothing else.
|
|
946
|
+
* Used for unfiltered sends, and on a configured filter's output only when
|
|
947
|
+
* {@link ɵrepairToolCallPairs}, which filtered sends go through otherwise,
|
|
948
|
+
* throws.
|
|
949
|
+
*
|
|
950
|
+
* The client store can detach a tool result from its call on its own: when a
|
|
951
|
+
* backend's `MESSAGES_SNAPSHOT` carries a tool message under a different id
|
|
952
|
+
* than the streamed `TOOL_CALL_RESULT`, the `@ag-ui/client` merge drops the
|
|
953
|
+
* copy placed after its call and appends the snapshot's copy at the end,
|
|
954
|
+
* leaving `assistant(tool_call), assistant(text), tool(result)`. A backend that
|
|
955
|
+
* rebuilds its state from that payload discards every result it cannot pair.
|
|
956
|
+
*
|
|
957
|
+
* The pairing rule mirrors the `@ag-ui/client` snapshot pass
|
|
958
|
+
* (ag-ui-protocol/ag-ui#3114):
|
|
959
|
+
*
|
|
960
|
+
* - Everything is by list position. The same object or id appearing twice is
|
|
961
|
+
* two independent slots, including an assistant call the merge duplicated,
|
|
962
|
+
* so a duplicated call can be left unanswered. Nothing is dropped,
|
|
963
|
+
* deduplicated, or added, and only tool-role messages ever change position.
|
|
964
|
+
* - Transparent entries are `activity` and `reasoning` messages and anything
|
|
965
|
+
* that is not a plain object (`null`, arrays, primitives). They are never
|
|
966
|
+
* the anchor, never separate a call from its result, and stay in place.
|
|
967
|
+
* Reasoning stored between a call and its result is the next step's: in a
|
|
968
|
+
* multi-step turn whose calls share one assistant message, Mastra leaves
|
|
969
|
+
* `assistant(c1, c2), tool(c1), reasoning, tool(c2)`. Moving the result in
|
|
970
|
+
* front of it leaves the reasoning trailing, and Mastra drops it.
|
|
971
|
+
* - The anchor is the last non-transparent message so far that is not `tool`.
|
|
972
|
+
* Each tool call an assistant issues opens a slot for its call id, and a
|
|
973
|
+
* result pairs with slots for its `toolCallId` alone. `subagentRunId` is
|
|
974
|
+
* ignored: results CopilotKit creates itself (frontend tool results,
|
|
975
|
+
* interrupt responses) do not carry one, and on a result it names the run
|
|
976
|
+
* that executed the tool, which may differ from the run that issued the
|
|
977
|
+
* call. Known limitation: a parent call and a subagent call that reuse one
|
|
978
|
+
* id are indistinguishable, so their drifted results can be swapped.
|
|
979
|
+
* - Pass 1, left to right: a result with an open slot issued at the anchor is
|
|
980
|
+
* in place; it takes that slot and stays put. A result with an open slot
|
|
981
|
+
* issued earlier is detached. A result with no slot for its id issued
|
|
982
|
+
* earlier at all is early. Any other result — every earlier slot for its id
|
|
983
|
+
* already answered — stays put. Detached results take no slot in this
|
|
984
|
+
* pass: they could only ever take a slot issued before the current anchor,
|
|
985
|
+
* which no later in-place result can want.
|
|
986
|
+
* - Pass 2, backward, per id: the detached results, from the newest back,
|
|
987
|
+
* each take the newest still-open slot issued before them; one that finds
|
|
988
|
+
* none stays put. So when several results for a reused id (providers reuse
|
|
989
|
+
* `call_0` every turn) are all detached, they keep their stored order
|
|
990
|
+
* across the calls, and a later turn's lone result goes to the later call
|
|
991
|
+
* rather than to an earlier one that was abandoned.
|
|
992
|
+
* - Pass 3, forward, per id: an early result moves forward only when its id
|
|
993
|
+
* is unambiguous — exactly one call slot in the whole list has it, that
|
|
994
|
+
* slot is issued after the result, and it is still open after passes 1-2.
|
|
995
|
+
* The first early result for the id, left to right, takes it; any other
|
|
996
|
+
* early result for that id stays put. This repairs a result whose call
|
|
997
|
+
* was re-issued after it (a snapshot that re-ids the assistant message but
|
|
998
|
+
* keeps the result's id). With a reused id (providers reuse `call_0` every
|
|
999
|
+
* turn) nothing moves forward: in `[t0, t1, a1(c), user, a2(c)]` nothing
|
|
1000
|
+
* says t1 answers a2, and a2 may be a pending call (a frontend tool, an
|
|
1001
|
+
* interrupt, a human-in-the-loop call waiting on the user). Only early
|
|
1002
|
+
* results move forward: an extra result for a call that is already
|
|
1003
|
+
* answered never does.
|
|
1004
|
+
* - A moved result goes behind its issuer, after the tool and transparent
|
|
1005
|
+
* entries already following it, but in front of any reasoning among those
|
|
1006
|
+
* transparent entries: reasoning belongs to the message after it, so a
|
|
1007
|
+
* moved result never lands between a reasoning message and its owner.
|
|
1008
|
+
* Results moved behind one issuer keep their stored order. Any other
|
|
1009
|
+
* message between a call and its result separates them, so a
|
|
1010
|
+
* backward-moved result moves before it and a forward-moved result moves
|
|
1011
|
+
* past it.
|
|
1012
|
+
* - The rule cannot tell a duplicate result from a detached one. A second
|
|
1013
|
+
* result for an already-answered call moves back to an older still-open
|
|
1014
|
+
* slot for its id if there is one, even across user turns; it never moves
|
|
1015
|
+
* forward.
|
|
1016
|
+
* - Malformed entries are ignored rather than thrown on. Non-array
|
|
1017
|
+
* `toolCalls`, call entries without a string `id`, and results without a
|
|
1018
|
+
* string `toolCallId` open or take no slot.
|
|
1019
|
+
*
|
|
1020
|
+
* Returns `messages` itself when nothing moved.
|
|
1021
|
+
*/
|
|
1022
|
+
function ɵkeepToolResultsWithCalls(messages) {
|
|
1023
|
+
const slots = /* @__PURE__ */ new Map();
|
|
1024
|
+
const openCount = /* @__PURE__ */ new Map();
|
|
1025
|
+
const close = (callId, slot) => {
|
|
1026
|
+
slot.open = false;
|
|
1027
|
+
openCount.set(callId, openCount.get(callId) - 1);
|
|
1028
|
+
};
|
|
1029
|
+
const detached = /* @__PURE__ */ new Map();
|
|
1030
|
+
const early = /* @__PURE__ */ new Map();
|
|
1031
|
+
let anchor = -1;
|
|
1032
|
+
for (let index = 0; index < messages.length; index++) {
|
|
1033
|
+
const message = messages[index];
|
|
1034
|
+
if (isTransparent(message)) continue;
|
|
1035
|
+
const role = roleOf(message);
|
|
1036
|
+
if (role !== "tool") {
|
|
1037
|
+
anchor = index;
|
|
1038
|
+
const calls = role === "assistant" ? message.toolCalls : void 0;
|
|
1039
|
+
if (!Array.isArray(calls)) continue;
|
|
1040
|
+
for (const call of calls) {
|
|
1041
|
+
var _openCount$get;
|
|
1042
|
+
if (typeof call !== "object" || call === null) continue;
|
|
1043
|
+
const callId = call.id;
|
|
1044
|
+
if (typeof callId !== "string") continue;
|
|
1045
|
+
push(slots, callId, {
|
|
1046
|
+
issuer: index,
|
|
1047
|
+
open: true
|
|
1048
|
+
});
|
|
1049
|
+
openCount.set(callId, ((_openCount$get = openCount.get(callId)) !== null && _openCount$get !== void 0 ? _openCount$get : 0) + 1);
|
|
1050
|
+
}
|
|
1051
|
+
continue;
|
|
1052
|
+
}
|
|
1053
|
+
const callId = message.toolCallId;
|
|
1054
|
+
if (typeof callId !== "string") continue;
|
|
1055
|
+
const forId = slots.get(callId);
|
|
1056
|
+
if (!forId) {
|
|
1057
|
+
push(early, callId, index);
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
let atAnchor;
|
|
1061
|
+
for (let s = forId.length - 1; s >= 0; s--) {
|
|
1062
|
+
const slot = forId[s];
|
|
1063
|
+
if (slot.issuer !== anchor) break;
|
|
1064
|
+
if (slot.open) atAnchor = slot;
|
|
1065
|
+
}
|
|
1066
|
+
if (atAnchor) {
|
|
1067
|
+
close(callId, atAnchor);
|
|
1068
|
+
continue;
|
|
1069
|
+
}
|
|
1070
|
+
if (openCount.get(callId) > 0) push(detached, callId, index);
|
|
1071
|
+
}
|
|
1072
|
+
const movedBehind = /* @__PURE__ */ new Map();
|
|
1073
|
+
const moved = /* @__PURE__ */ new Set();
|
|
1074
|
+
const take = (callId, slot, index) => {
|
|
1075
|
+
close(callId, slot);
|
|
1076
|
+
moved.add(index);
|
|
1077
|
+
push(movedBehind, slot.issuer, index);
|
|
1078
|
+
};
|
|
1079
|
+
for (const [callId, results] of detached) {
|
|
1080
|
+
const forId = slots.get(callId);
|
|
1081
|
+
let s = forId.length - 1;
|
|
1082
|
+
for (let r = results.length - 1; r >= 0; r--) {
|
|
1083
|
+
const index = results[r];
|
|
1084
|
+
while (s >= 0 && (!forId[s].open || forId[s].issuer >= index)) s--;
|
|
1085
|
+
if (s < 0) break;
|
|
1086
|
+
take(callId, forId[s], index);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
for (const [callId, results] of early) {
|
|
1090
|
+
const forId = slots.get(callId);
|
|
1091
|
+
if ((forId === null || forId === void 0 ? void 0 : forId.length) !== 1 || !forId[0].open) continue;
|
|
1092
|
+
take(callId, forId[0], results[0]);
|
|
1093
|
+
}
|
|
1094
|
+
if (moved.size === 0) return messages;
|
|
1095
|
+
const result = [];
|
|
1096
|
+
let openIssuer = -1;
|
|
1097
|
+
const flush = () => {
|
|
1098
|
+
const behind = movedBehind.get(openIssuer);
|
|
1099
|
+
if (!behind) return;
|
|
1100
|
+
let at = result.length;
|
|
1101
|
+
for (let tail = result.length - 1; tail >= 0; tail--) {
|
|
1102
|
+
if (!isTransparent(result[tail])) break;
|
|
1103
|
+
if (roleOf(result[tail]) === "reasoning") at = tail;
|
|
1104
|
+
}
|
|
1105
|
+
result.splice(at, 0, ...[...behind].sort((a, b) => a - b).map((index) => messages[index]));
|
|
1106
|
+
};
|
|
1107
|
+
for (let index = 0; index < messages.length; index++) {
|
|
1108
|
+
if (moved.has(index)) continue;
|
|
1109
|
+
const message = messages[index];
|
|
1110
|
+
if (!isTransparent(message) && roleOf(message) !== "tool") {
|
|
1111
|
+
flush();
|
|
1112
|
+
openIssuer = index;
|
|
1113
|
+
}
|
|
1114
|
+
result.push(message);
|
|
1115
|
+
}
|
|
1116
|
+
flush();
|
|
1117
|
+
return result;
|
|
1118
|
+
}
|
|
1119
|
+
function push(map, key, value) {
|
|
1120
|
+
const list = map.get(key);
|
|
1121
|
+
if (list) list.push(value);
|
|
1122
|
+
else map.set(key, [value]);
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* Activity and reasoning messages and anything that is not a plain object
|
|
1126
|
+
* (`null`, arrays, primitives) never anchor or separate.
|
|
1127
|
+
*/
|
|
1128
|
+
function isTransparent(message) {
|
|
1129
|
+
return typeof message !== "object" || message === null || Array.isArray(message) || message.role === "activity" || message.role === "reasoning";
|
|
1130
|
+
}
|
|
1131
|
+
function roleOf(message) {
|
|
1132
|
+
if (typeof message !== "object" || message === null) return void 0;
|
|
1133
|
+
return message.role;
|
|
1134
|
+
}
|
|
939
1135
|
|
|
940
1136
|
//#endregion
|
|
941
1137
|
//#region \0@oxc-project+runtime@0.112.0/helpers/checkPrivateRedeclaration.js
|
|
@@ -1470,7 +1666,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1470
1666
|
};
|
|
1471
1667
|
_ProxiedCopilotRuntimeAgent = ProxiedCopilotRuntimeAgent;
|
|
1472
1668
|
/**
|
|
1473
|
-
*
|
|
1669
|
+
* Build the outbound payload, keeping every tool result with the call that
|
|
1670
|
+
* issued it.
|
|
1671
|
+
*
|
|
1672
|
+
* - **No filter** (or a filter whose output is unusable, which falls back to
|
|
1673
|
+
* the full thread): `ɵkeepToolResultsWithCalls` re-places tool results
|
|
1674
|
+
* that drifted from their call and changes nothing else. The client store
|
|
1675
|
+
* can detach them on its own when a `MESSAGES_SNAPSHOT` re-ids a tool
|
|
1676
|
+
* message, and a backend that rebuilds state from such a payload drops
|
|
1677
|
+
* every result it cannot pair.
|
|
1678
|
+
* - **Filter**: `ɵrepairToolCallPairs(kept, full)` restores the call or the
|
|
1679
|
+
* results a filter trimmed. Its known limitations are tracked in
|
|
1680
|
+
* https://github.com/CopilotKit/CopilotKit/issues/7891.
|
|
1681
|
+
* - **Filter repair throws**: the filter's output goes through
|
|
1682
|
+
* `ɵkeepToolResultsWithCalls` instead, keeping its trimming and
|
|
1683
|
+
* redaction; if that throws too, the output is sent as is.
|
|
1474
1684
|
*
|
|
1475
1685
|
* **Called from the HTTP paths only, and that placement is the Intelligence
|
|
1476
1686
|
* exemption.** An earlier revision applied the filter in
|
|
@@ -1493,35 +1703,91 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1493
1703
|
* cannot reach the messages the UI renders no matter what it does with the
|
|
1494
1704
|
* array it is handed. The filter gets a *second* deep clone on top of that,
|
|
1495
1705
|
* because `input.messages` is also the repair baseline and the untrimmed
|
|
1496
|
-
* fallback: sharing the message objects let a filter that
|
|
1497
|
-
*
|
|
1498
|
-
* then went out
|
|
1499
|
-
* exactly the failure this whole mechanism
|
|
1706
|
+
* fallback: sharing the message objects let a filter that rewrote the tool
|
|
1707
|
+
* call id of an assistant message it dropped hide that issuer from the
|
|
1708
|
+
* repair, and the kept tool result then went out orphaned — a payload the
|
|
1709
|
+
* provider rejects, which is exactly the failure this whole mechanism
|
|
1710
|
+
* exists to prevent.
|
|
1500
1711
|
*/
|
|
1501
|
-
function
|
|
1502
|
-
|
|
1503
|
-
|
|
1712
|
+
function _prepareOutboundMessages(input) {
|
|
1713
|
+
var _this$agentId;
|
|
1714
|
+
const full = input.messages;
|
|
1715
|
+
const kept = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _applyMessageFilter).call(this, full);
|
|
1716
|
+
const agentId = (_this$agentId = this.agentId) !== null && _this$agentId !== void 0 ? _this$agentId : "";
|
|
1504
1717
|
try {
|
|
1505
|
-
|
|
1506
|
-
const kept = filter((0, _ag_ui_client.structuredClone_)(input.messages), { agentId: (_this$agentId = this.agentId) !== null && _this$agentId !== void 0 ? _this$agentId : "" });
|
|
1507
|
-
if (!Array.isArray(kept)) {
|
|
1508
|
-
console.warn("ProxiedCopilotRuntimeAgent: messageFilter returned a non-array value; sending the full message history instead.");
|
|
1509
|
-
return input;
|
|
1510
|
-
}
|
|
1718
|
+
const messages = kept === void 0 ? ɵkeepToolResultsWithCalls(full) : ɵrepairToolCallPairs(kept, full);
|
|
1511
1719
|
return {
|
|
1512
1720
|
...input,
|
|
1513
|
-
messages
|
|
1721
|
+
messages
|
|
1514
1722
|
};
|
|
1723
|
+
} catch (error) {
|
|
1724
|
+
if (kept === void 0) {
|
|
1725
|
+
console.warn(`ProxiedCopilotRuntimeAgent: tool-call repair failed for agent "${agentId}"; sending the message history unrepaired instead.`, error);
|
|
1726
|
+
return input;
|
|
1727
|
+
}
|
|
1728
|
+
try {
|
|
1729
|
+
const messages = ɵkeepToolResultsWithCalls(kept);
|
|
1730
|
+
console.warn(`ProxiedCopilotRuntimeAgent: tool-call repair failed for agent "${agentId}"; sending the filtered message history with only drifted tool results moved instead.`, error);
|
|
1731
|
+
return {
|
|
1732
|
+
...input,
|
|
1733
|
+
messages
|
|
1734
|
+
};
|
|
1735
|
+
} catch (moveError) {
|
|
1736
|
+
console.warn(`ProxiedCopilotRuntimeAgent: tool-call repair failed for agent "${agentId}"; sending the filtered message history unrepaired instead.`, error, moveError);
|
|
1737
|
+
return {
|
|
1738
|
+
...input,
|
|
1739
|
+
messages: kept
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Run the configured filter. Returns `undefined` when there is no filter,
|
|
1746
|
+
* copying the thread for it fails, or the filter's output cannot be used,
|
|
1747
|
+
* meaning "send the full thread".
|
|
1748
|
+
*
|
|
1749
|
+
* A filter that throws, returns a non-array, or returns an array with an
|
|
1750
|
+
* entry that is not a message object (a hole or a nested array included)
|
|
1751
|
+
* falls back to the untrimmed thread rather than failing the run. Trimming
|
|
1752
|
+
* is an optimization, and taking the user's message down with it would be
|
|
1753
|
+
* the worse outcome; the warning is what surfaces the bug.
|
|
1754
|
+
*/
|
|
1755
|
+
function _applyMessageFilter(full) {
|
|
1756
|
+
const filter = this._messageFilter;
|
|
1757
|
+
if (!filter) return void 0;
|
|
1758
|
+
let copy;
|
|
1759
|
+
try {
|
|
1760
|
+
copy = (0, _ag_ui_client.structuredClone_)(full);
|
|
1761
|
+
} catch (error) {
|
|
1762
|
+
console.warn("ProxiedCopilotRuntimeAgent: could not copy the message history for messageFilter; sending the full message history instead.", error);
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
let kept;
|
|
1766
|
+
try {
|
|
1767
|
+
var _this$agentId2;
|
|
1768
|
+
kept = filter(copy, { agentId: (_this$agentId2 = this.agentId) !== null && _this$agentId2 !== void 0 ? _this$agentId2 : "" });
|
|
1515
1769
|
} catch (error) {
|
|
1516
1770
|
console.warn("ProxiedCopilotRuntimeAgent: messageFilter failed; sending the full message history instead.", error);
|
|
1517
|
-
return
|
|
1771
|
+
return;
|
|
1518
1772
|
}
|
|
1773
|
+
if (!Array.isArray(kept)) {
|
|
1774
|
+
console.warn("ProxiedCopilotRuntimeAgent: messageFilter returned a non-array value; sending the full message history instead.");
|
|
1775
|
+
return;
|
|
1776
|
+
}
|
|
1777
|
+
for (let index = 0; index < kept.length; index++) {
|
|
1778
|
+
const entry = kept[index];
|
|
1779
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
1780
|
+
console.warn("ProxiedCopilotRuntimeAgent: messageFilter returned an entry that is not a message; sending the full message history instead.");
|
|
1781
|
+
return;
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
return kept;
|
|
1519
1785
|
}
|
|
1520
1786
|
function _connectViaDelegate(input, lifecycle) {
|
|
1521
1787
|
return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.connect(input, lifecycle))));
|
|
1522
1788
|
}
|
|
1523
1789
|
function _connectViaHttp(unfiltered) {
|
|
1524
|
-
const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this,
|
|
1790
|
+
const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _prepareOutboundMessages).call(this, unfiltered);
|
|
1525
1791
|
this.activeRun = void 0;
|
|
1526
1792
|
const routedId = this.routedAgentId();
|
|
1527
1793
|
let httpEvents;
|
|
@@ -1554,7 +1820,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1554
1820
|
return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.run(input))));
|
|
1555
1821
|
}
|
|
1556
1822
|
function _runViaHttp(unfiltered) {
|
|
1557
|
-
const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this,
|
|
1823
|
+
const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _prepareOutboundMessages).call(this, unfiltered);
|
|
1558
1824
|
const activeRun = {
|
|
1559
1825
|
threadId: input.threadId,
|
|
1560
1826
|
runId: input.runId
|
|
@@ -3607,6 +3873,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3607
3873
|
* takes no reason and whose signals never expose one.
|
|
3608
3874
|
*/
|
|
3609
3875
|
const liveHITLStops = /* @__PURE__ */ new WeakMap();
|
|
3876
|
+
/**
|
|
3877
|
+
* The message a failed tool call reports. An error thrown with an empty
|
|
3878
|
+
* message still means the call failed, so it gets a fallback rather than an
|
|
3879
|
+
* empty string, which everything downstream reads as success.
|
|
3880
|
+
*/
|
|
3881
|
+
function toolFailureMessage(error) {
|
|
3882
|
+
return error.message || "Tool call failed";
|
|
3883
|
+
}
|
|
3610
3884
|
function discardAbortedResult(signal, discardOnAbort, errorMessage) {
|
|
3611
3885
|
var _liveHITLStops$get;
|
|
3612
3886
|
return !!(discardOnAbort && (signal === null || signal === void 0 ? void 0 : signal.aborted) && !(((_liveHITLStops$get = liveHITLStops.get(signal)) === null || _liveHITLStops$get === void 0 ? void 0 : _liveHITLStops$get()) && errorMessage));
|
|
@@ -3644,8 +3918,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3644
3918
|
_defineProperty(this, "_disabledCatalogComponents", /* @__PURE__ */ new Set());
|
|
3645
3919
|
_defineProperty(this, "_disabledToolKeys", /* @__PURE__ */ new Set());
|
|
3646
3920
|
_defineProperty(this, "_warnedMissingToolParameters", /* @__PURE__ */ new Set());
|
|
3647
|
-
_defineProperty(this, "
|
|
3648
|
-
_defineProperty(this, "
|
|
3921
|
+
_defineProperty(this, "_runAbortControllers", /* @__PURE__ */ new WeakMap());
|
|
3922
|
+
_defineProperty(this, "_runDepths", /* @__PURE__ */ new WeakMap());
|
|
3923
|
+
_defineProperty(this, "_originalAbortRuns", /* @__PURE__ */ new WeakMap());
|
|
3649
3924
|
_defineProperty(this, "_webmcpRegistry", new WebMCPRegistry());
|
|
3650
3925
|
_defineProperty(this, "_lastConnectedThreadIdsByAgent", /* @__PURE__ */ new Map());
|
|
3651
3926
|
_defineProperty(this, "_lastActiveThreadIdsByAgent", /* @__PURE__ */ new Map());
|
|
@@ -3661,8 +3936,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3661
3936
|
* not start a follow-up run.
|
|
3662
3937
|
*/
|
|
3663
3938
|
abortCurrentRun(agent) {
|
|
3664
|
-
|
|
3665
|
-
(_this$_runAbortContro = this._runAbortController) === null || _this$_runAbortContro === void 0 || _this$_runAbortContro.abort();
|
|
3939
|
+
this.abortAgentRuns(agent);
|
|
3666
3940
|
const replay = this._interactionAbortControllers.get(agent);
|
|
3667
3941
|
if (replay) {
|
|
3668
3942
|
for (const controller of replay.controllers) controller.abort();
|
|
@@ -3670,6 +3944,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3670
3944
|
this._executingToolCalls.delete(agent);
|
|
3671
3945
|
}
|
|
3672
3946
|
}
|
|
3947
|
+
abortAgentRuns(agent) {
|
|
3948
|
+
var _this$_runAbortContro;
|
|
3949
|
+
for (const controller of (_this$_runAbortContro = this._runAbortControllers.get(agent)) !== null && _this$_runAbortContro !== void 0 ? _this$_runAbortContro : []) controller.abort();
|
|
3950
|
+
}
|
|
3951
|
+
/** Signal of the agent's most recently started in-flight run, if any. */
|
|
3952
|
+
latestRunSignal(agent) {
|
|
3953
|
+
var _this$_runAbortContro2;
|
|
3954
|
+
let latest;
|
|
3955
|
+
for (const controller of (_this$_runAbortContro2 = this._runAbortControllers.get(agent)) !== null && _this$_runAbortContro2 !== void 0 ? _this$_runAbortContro2 : []) latest = controller;
|
|
3956
|
+
return latest === null || latest === void 0 ? void 0 : latest.signal;
|
|
3957
|
+
}
|
|
3673
3958
|
/**
|
|
3674
3959
|
* Typed access to CopilotKitCore's internal ("friend") methods.
|
|
3675
3960
|
* Centralises the single unavoidable cast so call-sites stay clean.
|
|
@@ -3922,7 +4207,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3922
4207
|
/**
|
|
3923
4208
|
* Run an agent
|
|
3924
4209
|
*/
|
|
3925
|
-
async runAgent({ agent, forwardedProps, resume, runId }, continuationHandoff) {
|
|
4210
|
+
async runAgent({ agent, forwardedProps, resume, runId }, continuationHandoff, followUpOf) {
|
|
4211
|
+
var _this$_runDepths$get;
|
|
3926
4212
|
if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
|
|
3927
4213
|
try {
|
|
3928
4214
|
const headersApplied = this._internal.prepareAgentHeadersForRun(agent);
|
|
@@ -3946,28 +4232,34 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3946
4232
|
forwardedProps
|
|
3947
4233
|
});
|
|
3948
4234
|
const logicalHandoff = continuationHandoff !== null && continuationHandoff !== void 0 ? continuationHandoff : resumedRunId ? this._internal.stateManager.markNextRunAsContinuation(agent, resumedRunId) : void 0;
|
|
3949
|
-
const
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3953
|
-
|
|
3954
|
-
|
|
4235
|
+
const isFollowUp = followUpOf !== void 0;
|
|
4236
|
+
const controller = followUpOf !== null && followUpOf !== void 0 ? followUpOf : new AbortController();
|
|
4237
|
+
const depth = (_this$_runDepths$get = this._runDepths.get(agent)) !== null && _this$_runDepths$get !== void 0 ? _this$_runDepths$get : 0;
|
|
4238
|
+
this._runDepths.set(agent, depth + 1);
|
|
4239
|
+
if (!isFollowUp) {
|
|
4240
|
+
var _this$_runAbortContro3;
|
|
4241
|
+
const controllers = (_this$_runAbortContro3 = this._runAbortControllers.get(agent)) !== null && _this$_runAbortContro3 !== void 0 ? _this$_runAbortContro3 : /* @__PURE__ */ new Set();
|
|
4242
|
+
controllers.add(controller);
|
|
4243
|
+
this._runAbortControllers.set(agent, controllers);
|
|
4244
|
+
}
|
|
4245
|
+
if (depth === 0) {
|
|
4246
|
+
const originalAbortRun = agent.abortRun;
|
|
4247
|
+
this._originalAbortRuns.set(agent, originalAbortRun);
|
|
3955
4248
|
agent.abortRun = () => {
|
|
3956
|
-
|
|
3957
|
-
originalAbortRun();
|
|
4249
|
+
this.abortAgentRuns(agent);
|
|
4250
|
+
originalAbortRun.call(agent);
|
|
3958
4251
|
};
|
|
3959
|
-
|
|
4252
|
+
}
|
|
4253
|
+
try {
|
|
4254
|
+
if (depth === 0) await this._internal.notifySubscribers((subscriber) => {
|
|
3960
4255
|
var _subscriber$onAgentRu2;
|
|
3961
4256
|
return (_subscriber$onAgentRu2 = subscriber.onAgentRunStarted) === null || _subscriber$onAgentRu2 === void 0 ? void 0 : _subscriber$onAgentRu2.call(subscriber, {
|
|
3962
4257
|
copilotkit: this.core,
|
|
3963
4258
|
agent
|
|
3964
4259
|
});
|
|
3965
4260
|
}, "Subscriber onAgentRunStarted error:");
|
|
3966
|
-
}
|
|
3967
|
-
this._runDepth++;
|
|
3968
|
-
try {
|
|
3969
4261
|
let logicalRunId = resumedRunId !== null && resumedRunId !== void 0 ? resumedRunId : runId;
|
|
3970
|
-
const agentSubscriber = this.createAgentErrorSubscriber(agent);
|
|
4262
|
+
const agentSubscriber = this.createAgentErrorSubscriber(agent, controller.signal);
|
|
3971
4263
|
let started = false;
|
|
3972
4264
|
const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
|
|
3973
4265
|
const onRunInitialized = agentSubscriber.onRunInitialized;
|
|
@@ -3997,7 +4289,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3997
4289
|
return await this.processAgentResult({
|
|
3998
4290
|
runAgentResult,
|
|
3999
4291
|
agent,
|
|
4000
|
-
runId: logicalRunId
|
|
4292
|
+
runId: logicalRunId,
|
|
4293
|
+
runController: controller
|
|
4001
4294
|
});
|
|
4002
4295
|
} catch (error) {
|
|
4003
4296
|
logicalHandoff === null || logicalHandoff === void 0 || logicalHandoff.cancel();
|
|
@@ -4014,17 +4307,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4014
4307
|
newMessages: []
|
|
4015
4308
|
};
|
|
4016
4309
|
} finally {
|
|
4017
|
-
|
|
4018
|
-
|
|
4310
|
+
var _this$_runDepths$get2;
|
|
4311
|
+
const remainingDepth = ((_this$_runDepths$get2 = this._runDepths.get(agent)) !== null && _this$_runDepths$get2 !== void 0 ? _this$_runDepths$get2 : 1) - 1;
|
|
4312
|
+
this._runDepths.set(agent, remainingDepth);
|
|
4313
|
+
if (!isFollowUp) {
|
|
4314
|
+
const controllers = this._runAbortControllers.get(agent);
|
|
4315
|
+
controllers === null || controllers === void 0 || controllers.delete(controller);
|
|
4316
|
+
if ((controllers === null || controllers === void 0 ? void 0 : controllers.size) === 0) this._runAbortControllers.delete(agent);
|
|
4317
|
+
}
|
|
4318
|
+
if (remainingDepth === 0) {
|
|
4319
|
+
const originalAbortRun = this._originalAbortRuns.get(agent);
|
|
4320
|
+
if (originalAbortRun) {
|
|
4321
|
+
agent.abortRun = originalAbortRun;
|
|
4322
|
+
this._originalAbortRuns.delete(agent);
|
|
4323
|
+
}
|
|
4324
|
+
}
|
|
4019
4325
|
}
|
|
4020
4326
|
}
|
|
4021
4327
|
/**
|
|
4022
4328
|
* Process agent result and execute tools
|
|
4023
4329
|
*/
|
|
4024
|
-
async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", signal =
|
|
4025
|
-
var _this$_runAbortContro2;
|
|
4026
|
-
return (_this$_runAbortContro2 = this._runAbortController) === null || _this$_runAbortContro2 === void 0 ? void 0 : _this$_runAbortContro2.signal;
|
|
4027
|
-
})() }) {
|
|
4330
|
+
async processAgentResult({ runAgentResult, agent, runId, toolExecutionMode = "all", runController, signal = runController === null || runController === void 0 ? void 0 : runController.signal }) {
|
|
4028
4331
|
var _this$_executingToolC;
|
|
4029
4332
|
const { newMessages } = runAgentResult;
|
|
4030
4333
|
const agentId = agent.agentId;
|
|
@@ -4032,11 +4335,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4032
4335
|
const threadId = agent.threadId;
|
|
4033
4336
|
const executing = (_this$_executingToolC = this._executingToolCalls.get(agent)) !== null && _this$_executingToolC !== void 0 ? _this$_executingToolC : /* @__PURE__ */ new Set();
|
|
4034
4337
|
this._executingToolCalls.set(agent, executing);
|
|
4035
|
-
|
|
4338
|
+
const messagesToProcess = toolExecutionMode === "restore" ? agent.messages.filter((message) => message.role === "assistant" && this.isInLatestUserTurn(agent.messages, message.id)) : newMessages;
|
|
4339
|
+
if (toolExecutionMode === "restore" && messagesToProcess.some((message) => {
|
|
4036
4340
|
var _message$toolCalls;
|
|
4037
4341
|
return message.role === "assistant" && ((_message$toolCalls = message.toolCalls) === null || _message$toolCalls === void 0 ? void 0 : _message$toolCalls.some((call) => executing.has(JSON.stringify([threadId, call.id])) && !agent.messages.some((result) => result.role === "tool" && result.toolCallId === call.id && !this.isFrontendPlaceholderResult(result))));
|
|
4038
4342
|
})) return runAgentResult;
|
|
4039
|
-
const messagesToProcess = toolExecutionMode === "restore" ? [...agent.messages] : newMessages;
|
|
4040
4343
|
for (const message of messagesToProcess) if (message.role === "assistant") for (const toolCall of message.toolCalls || []) {
|
|
4041
4344
|
var _agent$messages$find;
|
|
4042
4345
|
const tool = this.getTool({
|
|
@@ -4053,12 +4356,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4053
4356
|
return wildcardTool;
|
|
4054
4357
|
};
|
|
4055
4358
|
const executableTool = tool !== null && tool !== void 0 ? tool : getWildcardTool();
|
|
4056
|
-
if (toolExecutionMode === "restore" && (!
|
|
4359
|
+
if (toolExecutionMode === "restore" && (!executableTool || executableTool.handler && executableTool.type !== "human-in-the-loop" && executableTool.reconnectBehavior !== "resume-pending")) continue;
|
|
4057
4360
|
const executionKey = JSON.stringify([threadId, toolCall.id]);
|
|
4058
|
-
if ((signal === null || signal === void 0 ? void 0 : signal.aborted) || agent.threadId !== threadId || executing.has(executionKey)) continue;
|
|
4361
|
+
if ((signal === null || signal === void 0 ? void 0 : signal.aborted) || agent.threadId !== threadId || toolExecutionMode === "restore" && !this.isInLatestUserTurn(agent.messages, message.id) || executing.has(executionKey)) continue;
|
|
4059
4362
|
const existingResultIndex = newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
|
|
4060
4363
|
let existingResult = (_agent$messages$find = agent.messages.find((m) => m.role === "tool" && m.toolCallId === toolCall.id)) !== null && _agent$messages$find !== void 0 ? _agent$messages$find : newMessages[existingResultIndex];
|
|
4061
|
-
if (existingResult && (executableTool === null || executableTool === void 0 ? void 0 : executableTool.handler)
|
|
4364
|
+
if (existingResult && this.isFrontendPlaceholderResult(existingResult) && (toolExecutionMode === "restore" || (executableTool === null || executableTool === void 0 ? void 0 : executableTool.handler))) {
|
|
4062
4365
|
if (existingResultIndex !== -1) newMessages.splice(existingResultIndex, 1);
|
|
4063
4366
|
existingResult = void 0;
|
|
4064
4367
|
const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
|
|
@@ -4106,18 +4409,47 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4106
4409
|
if (interactionController === null || interactionController === void 0 ? void 0 : interactionController.signal.aborted) return runAgentResult;
|
|
4107
4410
|
}
|
|
4108
4411
|
}
|
|
4109
|
-
if (needsFollowUp && agent.threadId === threadId && !(signal === null || signal === void 0 ? void 0 : signal.aborted))
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
agent,
|
|
4115
|
-
|
|
4116
|
-
|
|
4412
|
+
if (needsFollowUp && agent.threadId === threadId && !(signal === null || signal === void 0 ? void 0 : signal.aborted)) {
|
|
4413
|
+
var _this$_runDepths$get3;
|
|
4414
|
+
if (((_this$_runDepths$get3 = this._runDepths.get(agent)) !== null && _this$_runDepths$get3 !== void 0 ? _this$_runDepths$get3 : 0) >= MAX_FOLLOW_UP_DEPTH) _copilotkit_shared.logger.warn(`[CopilotKit] Follow-up depth limit (${MAX_FOLLOW_UP_DEPTH}) reached for agent "${agentId}". Stopping recursive follow-up runs to prevent an infinite loop. This usually indicates a tool that keeps requesting a follow-up (e.g. the LLM repeatedly calling the same tool). Consider setting "followUp: false" on the offending tool.`);
|
|
4415
|
+
else {
|
|
4416
|
+
await this._internal.waitForPendingFrameworkUpdates();
|
|
4417
|
+
if (toolExecutionMode === "restore" && !messagesToProcess.some((message) => this.isInLatestUserTurn(agent.messages, message.id))) return runAgentResult;
|
|
4418
|
+
const continuationHandoff = this._internal.stateManager.markNextRunAsContinuation(agent, runId);
|
|
4419
|
+
return await this.runAgent({
|
|
4420
|
+
agent,
|
|
4421
|
+
...runId !== void 0 ? { runId } : {}
|
|
4422
|
+
}, continuationHandoff, runController);
|
|
4423
|
+
}
|
|
4117
4424
|
}
|
|
4118
4425
|
this._internal.suggestionEngine.reloadSuggestions(agentId);
|
|
4119
4426
|
return runAgentResult;
|
|
4120
4427
|
}
|
|
4428
|
+
/** A later user message supersedes pending work from an older turn. */
|
|
4429
|
+
isInLatestUserTurn(messages, messageId) {
|
|
4430
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
4431
|
+
const message = messages[index];
|
|
4432
|
+
if ((message === null || message === void 0 ? void 0 : message.role) === "user") return false;
|
|
4433
|
+
if ((message === null || message === void 0 ? void 0 : message.id) === messageId) return true;
|
|
4434
|
+
}
|
|
4435
|
+
return false;
|
|
4436
|
+
}
|
|
4437
|
+
/** Locate a missing result before saved results for later calls in its batch. */
|
|
4438
|
+
toolResultInsertionIndex(messages, messageIndex, toolCallId) {
|
|
4439
|
+
var _parent$toolCalls;
|
|
4440
|
+
const parent = messages[messageIndex];
|
|
4441
|
+
const calls = (parent === null || parent === void 0 ? void 0 : parent.role) === "assistant" ? (_parent$toolCalls = parent.toolCalls) !== null && _parent$toolCalls !== void 0 ? _parent$toolCalls : [] : [];
|
|
4442
|
+
const callIndex = calls.findIndex((call) => call.id === toolCallId);
|
|
4443
|
+
let insertAt = messageIndex + 1;
|
|
4444
|
+
while (insertAt < messages.length) {
|
|
4445
|
+
const result = messages[insertAt];
|
|
4446
|
+
if ((result === null || result === void 0 ? void 0 : result.role) !== "tool") break;
|
|
4447
|
+
if (calls.findIndex((call) => call.id === result.toolCallId) > callIndex) break;
|
|
4448
|
+
insertAt++;
|
|
4449
|
+
}
|
|
4450
|
+
return insertAt;
|
|
4451
|
+
}
|
|
4452
|
+
/** Identify transport placeholders that must not count as completed answers. */
|
|
4121
4453
|
isFrontendPlaceholderResult(message) {
|
|
4122
4454
|
return message.role === "tool" && isForwardedToClientPlaceholder(message.content);
|
|
4123
4455
|
}
|
|
@@ -4126,10 +4458,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4126
4458
|
* Handles arg parsing, subscriber notifications, handler invocation, result stringification,
|
|
4127
4459
|
* and error handling.
|
|
4128
4460
|
*/
|
|
4129
|
-
async executeToolHandler({ tool, toolCall, agent, agentId, handlerArgs, toolType, messageId, signal = (
|
|
4130
|
-
var _this$_runAbortContro3;
|
|
4131
|
-
return (_this$_runAbortContro3 = this._runAbortController) === null || _this$_runAbortContro3 === void 0 ? void 0 : _this$_runAbortContro3.signal;
|
|
4132
|
-
})(), discardOnAbort = false, isReplay = false }) {
|
|
4461
|
+
async executeToolHandler({ tool, toolCall, agent, agentId, handlerArgs, toolType, messageId, signal = this.latestRunSignal(agent), discardOnAbort = false, isReplay = false }) {
|
|
4133
4462
|
let toolCallResult = "";
|
|
4134
4463
|
let toolCallText = "";
|
|
4135
4464
|
let errorMessage;
|
|
@@ -4139,7 +4468,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4139
4468
|
parsedArgs = parseToolArguments(handlerArgs, toolCall.function.name);
|
|
4140
4469
|
} catch (error) {
|
|
4141
4470
|
const parseError = error instanceof Error ? error : new Error(String(error));
|
|
4142
|
-
errorMessage = parseError
|
|
4471
|
+
errorMessage = toolFailureMessage(parseError);
|
|
4143
4472
|
isArgumentError = true;
|
|
4144
4473
|
await this._internal.emitError({
|
|
4145
4474
|
error: parseError,
|
|
@@ -4174,7 +4503,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4174
4503
|
({content: toolCallResult, text: toolCallText} = toToolResultContent(result));
|
|
4175
4504
|
} catch (error) {
|
|
4176
4505
|
const handlerError = error instanceof Error ? error : new Error(String(error));
|
|
4177
|
-
errorMessage = handlerError
|
|
4506
|
+
errorMessage = toolFailureMessage(handlerError);
|
|
4178
4507
|
if (!discardAbortedResult(signal, discardOnAbort, errorMessage)) await this._internal.emitError({
|
|
4179
4508
|
error: handlerError,
|
|
4180
4509
|
code: CopilotKitCoreErrorCode.TOOL_HANDLER_FAILED,
|
|
@@ -4232,16 +4561,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4232
4561
|
isReplay
|
|
4233
4562
|
});
|
|
4234
4563
|
{
|
|
4235
|
-
var _agent$messages$inser;
|
|
4236
4564
|
const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
|
|
4237
|
-
if (discardAbortedResult(signal, discardOnAbort, handlerResult.error) || agent.threadId !== threadId || messageIndex === -1 || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id && !this.isFrontendPlaceholderResult(m))) return false;
|
|
4238
|
-
|
|
4239
|
-
while (insertAt < agent.messages.length && ((_agent$messages$inser = agent.messages[insertAt]) === null || _agent$messages$inser === void 0 ? void 0 : _agent$messages$inser.role) === "tool") insertAt++;
|
|
4565
|
+
if (discardAbortedResult(signal, discardOnAbort, handlerResult.error) || agent.threadId !== threadId || messageIndex === -1 || isReplay && !this.isInLatestUserTurn(agent.messages, message.id) || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id && !this.isFrontendPlaceholderResult(m))) return false;
|
|
4566
|
+
const insertAt = this.toolResultInsertionIndex(agent.messages, messageIndex, toolCall.id);
|
|
4240
4567
|
const toolMessage = {
|
|
4241
4568
|
id: (0, _copilotkit_shared.randomUUID)(),
|
|
4242
4569
|
role: "tool",
|
|
4243
4570
|
toolCallId: toolCall.id,
|
|
4244
|
-
content: handlerResult.content
|
|
4571
|
+
content: handlerResult.content,
|
|
4572
|
+
...handlerResult.error ? { error: handlerResult.error } : {}
|
|
4245
4573
|
};
|
|
4246
4574
|
agent.messages.splice(insertAt, 0, toolMessage);
|
|
4247
4575
|
if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
|
|
@@ -4266,7 +4594,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4266
4594
|
parsedArgs = parseToolArguments(toolCall.function.arguments, toolCall.function.name);
|
|
4267
4595
|
} catch (error) {
|
|
4268
4596
|
const parseError = error instanceof Error ? error : new Error(String(error));
|
|
4269
|
-
errorMessage = parseError
|
|
4597
|
+
errorMessage = toolFailureMessage(parseError);
|
|
4270
4598
|
await this._internal.emitError({
|
|
4271
4599
|
error: parseError,
|
|
4272
4600
|
code: CopilotKitCoreErrorCode.TOOL_ARGUMENT_PARSE_FAILED,
|
|
@@ -4304,7 +4632,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4304
4632
|
({content: toolCallResult, text: toolCallText} = toToolResultContent(result));
|
|
4305
4633
|
} catch (error) {
|
|
4306
4634
|
const handlerError = error instanceof Error ? error : new Error(String(error));
|
|
4307
|
-
errorMessage = handlerError
|
|
4635
|
+
errorMessage = toolFailureMessage(handlerError);
|
|
4308
4636
|
if (!discardAbortedResult(signal, discardOnAbort, errorMessage)) await this._internal.emitError({
|
|
4309
4637
|
error: handlerError,
|
|
4310
4638
|
code: CopilotKitCoreErrorCode.TOOL_HANDLER_FAILED,
|
|
@@ -4332,16 +4660,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4332
4660
|
}, "Subscriber onToolExecutionEnd error:");
|
|
4333
4661
|
}
|
|
4334
4662
|
{
|
|
4335
|
-
var _agent$messages$inser2;
|
|
4336
4663
|
const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
|
|
4337
|
-
if (discardAbortedResult(signal, discardOnAbort, errorMessage) || agent.threadId !== threadId || messageIndex === -1 || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id && !this.isFrontendPlaceholderResult(m))) return false;
|
|
4338
|
-
|
|
4339
|
-
while (insertAt < agent.messages.length && ((_agent$messages$inser2 = agent.messages[insertAt]) === null || _agent$messages$inser2 === void 0 ? void 0 : _agent$messages$inser2.role) === "tool") insertAt++;
|
|
4664
|
+
if (discardAbortedResult(signal, discardOnAbort, errorMessage) || agent.threadId !== threadId || messageIndex === -1 || isReplay && !this.isInLatestUserTurn(agent.messages, message.id) || agent.messages.some((m) => m.role === "tool" && m.toolCallId === toolCall.id && !this.isFrontendPlaceholderResult(m))) return false;
|
|
4665
|
+
const insertAt = this.toolResultInsertionIndex(agent.messages, messageIndex, toolCall.id);
|
|
4340
4666
|
const toolMessage = {
|
|
4341
4667
|
id: (0, _copilotkit_shared.randomUUID)(),
|
|
4342
4668
|
role: "tool",
|
|
4343
4669
|
toolCallId: toolCall.id,
|
|
4344
|
-
content: toolCallResult
|
|
4670
|
+
content: toolCallResult,
|
|
4671
|
+
...errorMessage ? { error: errorMessage } : {}
|
|
4345
4672
|
};
|
|
4346
4673
|
agent.messages.splice(insertAt, 0, toolMessage);
|
|
4347
4674
|
if (!errorMessage && (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.followUp) !== false) return true;
|
|
@@ -4416,7 +4743,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4416
4743
|
id: (0, _copilotkit_shared.randomUUID)(),
|
|
4417
4744
|
role: "tool",
|
|
4418
4745
|
toolCallId,
|
|
4419
|
-
content: handlerResult.content
|
|
4746
|
+
content: handlerResult.content,
|
|
4747
|
+
...handlerResult.error ? { error: handlerResult.error } : {}
|
|
4420
4748
|
};
|
|
4421
4749
|
const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
|
|
4422
4750
|
if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
|
|
@@ -4501,7 +4829,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4501
4829
|
/**
|
|
4502
4830
|
* Create an agent error subscriber
|
|
4503
4831
|
*/
|
|
4504
|
-
createAgentErrorSubscriber(agent) {
|
|
4832
|
+
createAgentErrorSubscriber(agent, runSignal) {
|
|
4505
4833
|
const emitAgentError = async (error, code, extraContext = {}) => {
|
|
4506
4834
|
const context = { ...extraContext };
|
|
4507
4835
|
if (agent.agentId) context.agentId = agent.agentId;
|
|
@@ -4516,8 +4844,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4516
4844
|
await emitAgentError(error, error instanceof AgentThreadLockedError ? CopilotKitCoreErrorCode.AGENT_THREAD_LOCKED : CopilotKitCoreErrorCode.AGENT_RUN_FAILED_EVENT, { source: "onRunFailed" });
|
|
4517
4845
|
},
|
|
4518
4846
|
onRunErrorEvent: async ({ event }) => {
|
|
4519
|
-
var
|
|
4520
|
-
if (((
|
|
4847
|
+
var _runSignal$aborted, _event$rawEvent, _event$rawEvent2, _event$message;
|
|
4848
|
+
if (((_runSignal$aborted = runSignal === null || runSignal === void 0 ? void 0 : runSignal.aborted) !== null && _runSignal$aborted !== void 0 ? _runSignal$aborted : false) || (event === null || event === void 0 ? void 0 : event.code) === "abort") return;
|
|
4849
|
+
if ((event === null || event === void 0 ? void 0 : event.code) === _copilotkit_shared.THREAD_ALREADY_RUNNING_ERROR_CODE) {
|
|
4850
|
+
await emitAgentError(new AgentThreadLockedError(agent.threadId), CopilotKitCoreErrorCode.AGENT_THREAD_LOCKED, {
|
|
4851
|
+
source: "onRunErrorEvent",
|
|
4852
|
+
event
|
|
4853
|
+
});
|
|
4854
|
+
return;
|
|
4855
|
+
}
|
|
4521
4856
|
const eventError = (event === null || event === void 0 ? void 0 : event.rawEvent) instanceof Error ? event.rawEvent : (event === null || event === void 0 || (_event$rawEvent = event.rawEvent) === null || _event$rawEvent === void 0 ? void 0 : _event$rawEvent.error) instanceof Error ? event.rawEvent.error : void 0;
|
|
4522
4857
|
const errorMessage = typeof (event === null || event === void 0 || (_event$rawEvent2 = event.rawEvent) === null || _event$rawEvent2 === void 0 ? void 0 : _event$rawEvent2.error) === "string" ? event.rawEvent.error : (_event$message = event === null || event === void 0 ? void 0 : event.message) !== null && _event$message !== void 0 ? _event$message : "Agent run error";
|
|
4523
4858
|
const rawError = eventError !== null && eventError !== void 0 ? eventError : new Error(errorMessage);
|
|
@@ -5598,6 +5933,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5598
5933
|
get isConfigured() {
|
|
5599
5934
|
return this.config !== void 0;
|
|
5600
5935
|
}
|
|
5936
|
+
/**
|
|
5937
|
+
* Whether events may carry agent message text: not when the capture's config
|
|
5938
|
+
* sets `capture.agentText` to false. A running custom-sink capture keeps the
|
|
5939
|
+
* choice it started with; otherwise the current config decides.
|
|
5940
|
+
*/
|
|
5941
|
+
get capturesAgentText() {
|
|
5942
|
+
var _this$activeConfig;
|
|
5943
|
+
const config = (_this$activeConfig = this.activeConfig) !== null && _this$activeConfig !== void 0 ? _this$activeConfig : this.config;
|
|
5944
|
+
if (config === void 0) return false;
|
|
5945
|
+
const capture = config.capture;
|
|
5946
|
+
return !(typeof capture === "object" && capture !== null && capture.agentText === false);
|
|
5947
|
+
}
|
|
5601
5948
|
get isLegacyCaptureActive() {
|
|
5602
5949
|
return this.collector !== null && this.collector.trajectoryId !== null;
|
|
5603
5950
|
}
|
|
@@ -5707,13 +6054,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5707
6054
|
this.toolCalls.clear();
|
|
5708
6055
|
this.executingTools.clear();
|
|
5709
6056
|
}
|
|
5710
|
-
/**
|
|
6057
|
+
/**
|
|
6058
|
+
* A developer event. Only an explicit legacy sink adds open-Thread context;
|
|
6059
|
+
* an event that names its own Thread gets no ambiguity next to it.
|
|
6060
|
+
*/
|
|
5711
6061
|
emit(name, value) {
|
|
5712
6062
|
this.connection.emit(name, value);
|
|
5713
6063
|
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
5714
6064
|
var _this$collector4;
|
|
6065
|
+
const context = typeof value["threadId"] === "string" ? {} : this.openThreadContext();
|
|
5715
6066
|
(_this$collector4 = this.collector) === null || _this$collector4 === void 0 || _this$collector4.emit(name, {
|
|
5716
|
-
...
|
|
6067
|
+
...context,
|
|
5717
6068
|
...value
|
|
5718
6069
|
});
|
|
5719
6070
|
}
|
|
@@ -5799,9 +6150,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5799
6150
|
});
|
|
5800
6151
|
}
|
|
5801
6152
|
emitMessage(agent, message) {
|
|
5802
|
-
var _this$
|
|
6153
|
+
var _this$collector7;
|
|
5803
6154
|
const content = typeof message.content === "string" ? message.content : "";
|
|
5804
|
-
const includeText =
|
|
6155
|
+
const includeText = this.capturesAgentText && typeof message.content === "string";
|
|
5805
6156
|
(_this$collector7 = this.collector) === null || _this$collector7 === void 0 || _this$collector7.ɵemit("agent.message", {
|
|
5806
6157
|
messageId: message.id,
|
|
5807
6158
|
runId: this.runIdForMessage(agent, message.id),
|
|
@@ -7434,6 +7785,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
7434
7785
|
get ɵlearningConfigured() {
|
|
7435
7786
|
return this.learningBridge.isConfigured;
|
|
7436
7787
|
}
|
|
7788
|
+
/**
|
|
7789
|
+
* @internal Whether captured events may carry agent message text: false
|
|
7790
|
+
* without a `learning` config, or when a custom sink sets
|
|
7791
|
+
* `capture.agentText` to false. A running custom-sink capture keeps the
|
|
7792
|
+
* choice it started with.
|
|
7793
|
+
*/
|
|
7794
|
+
get ɵlearningCapturesAgentText() {
|
|
7795
|
+
return this.learningBridge.capturesAgentText;
|
|
7796
|
+
}
|
|
7437
7797
|
/** @internal Calls `listener` when {@link ɵlearningConfigured} changes. */
|
|
7438
7798
|
ɵsubscribeToLearningConfigured(listener) {
|
|
7439
7799
|
this.learningConfiguredListeners.add(listener);
|