@copilotkit/core 1.75.2 → 1.77.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 +1085 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +110 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +110 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1088 -62
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1126 -66
- package/dist/index.umd.js.map +1 -1
- package/package.json +5 -3
package/dist/index.umd.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
(function(global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/pacer'), require('@ag-ui/client'), require('@copilotkit/shared'), require('rxjs'), require('rxjs/operators'), require('phoenix'), require('zod-to-json-schema')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/pacer', '@ag-ui/client', '@copilotkit/shared', 'rxjs', 'rxjs/operators', 'phoenix', 'zod-to-json-schema'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.CopilotKitCore = {}), global._tanstack_pacer,global.AgUIClient,global.CopilotKitShared,global.rxjs,global.rxjs.operators,global.phoenix,global.zodToJsonSchema));
|
|
5
|
-
})(this, function(exports, _tanstack_pacer, _ag_ui_client, _copilotkit_shared, rxjs, rxjs_operators, phoenix, zod_to_json_schema) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/pacer'), require('@ag-ui/client'), require('@copilotkit/shared'), require('rxjs'), require('rxjs/operators'), require('phoenix'), require('zod-to-json-schema'), require('@copilotkit/learning')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/pacer', '@ag-ui/client', '@copilotkit/shared', 'rxjs', 'rxjs/operators', 'phoenix', 'zod-to-json-schema', '@copilotkit/learning'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.CopilotKitCore = {}), global._tanstack_pacer,global.AgUIClient,global.CopilotKitShared,global.rxjs,global.rxjs.operators,global.phoenix,global.zodToJsonSchema,global._copilotkit_learning));
|
|
5
|
+
})(this, function(exports, _tanstack_pacer, _ag_ui_client, _copilotkit_shared, rxjs, rxjs_operators, phoenix, zod_to_json_schema, _copilotkit_learning) {
|
|
6
6
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
7
7
|
|
|
8
|
+
//#region src/utils/http-agent.ts
|
|
9
|
+
/**
|
|
10
|
+
* Whether an agent is an `HttpAgent`, including one built from another copy of
|
|
11
|
+
* `@ag-ui/client` (an app that pins its own 0.x version next to CopilotKit's
|
|
12
|
+
* 1.0, for example). `instanceof` is false across package copies, which made
|
|
13
|
+
* CopilotKit silently skip headers and abort wiring for those agents. The
|
|
14
|
+
* fallback checks the public fields CopilotKit uses, which every published
|
|
15
|
+
* `HttpAgent` has.
|
|
16
|
+
*/
|
|
17
|
+
function ɵisHttpAgent(agent) {
|
|
18
|
+
if (agent instanceof _ag_ui_client.HttpAgent) return true;
|
|
19
|
+
if (typeof agent !== "object" || agent === null) return false;
|
|
20
|
+
const candidate = agent;
|
|
21
|
+
return typeof candidate.url === "string" && typeof candidate.headers === "object" && candidate.headers !== null && candidate.abortController instanceof AbortController && typeof candidate.runAgent === "function";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
8
25
|
//#region src/utils/connect-replay.ts
|
|
9
26
|
/**
|
|
10
27
|
* Runs an agent's `connect()` stream through the AbstractAgent apply pipeline
|
|
@@ -21,18 +38,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
21
38
|
* Cannot send event type 'RUN_STARTED': The run has already errored with
|
|
22
39
|
* 'RUN_ERROR'. No further events can be sent.
|
|
23
40
|
*
|
|
24
|
-
*
|
|
41
|
+
* The AG-UI 1.0 compatibility boundary still runs first, so stored 0.x history
|
|
42
|
+
* is translated (THINKING_* events, `binary` parts, legacy nulls) exactly as in
|
|
43
|
+
* the base pipeline. `enforceEvents` is omitted too: a replay is stored
|
|
44
|
+
* history, and one event that the 1.0 schema rejects (the Intelligence gateway
|
|
45
|
+
* replays RUN_STARTED without `runId`, for example) must not stop the whole
|
|
46
|
+
* thread from hydrating. That keeps the leniency this path had before 1.0.
|
|
25
47
|
*
|
|
26
48
|
* Connection-local replay hooks track the phase before applying events. An
|
|
27
49
|
* explicitly live RUN_ERROR clears busy without closing the connection;
|
|
28
50
|
* historical errors remain data. Connections without hooks retain the existing
|
|
29
|
-
* completion behavior.
|
|
51
|
+
* completion behavior.
|
|
52
|
+
*
|
|
53
|
+
* Apart from the above, this mirrors the base `AbstractAgent.connectAgent`
|
|
54
|
+
* implementation, so callers keep the same subscriber notifications, detach
|
|
55
|
+
* semantics, and `{ result, newMessages }` return shape.
|
|
30
56
|
*
|
|
31
57
|
* TODO: Remove this in favour of the base implementation once AG-UI's
|
|
32
58
|
* AbstractAgent supports opting out of `verifyEvents` for transports whose
|
|
33
59
|
* connection life-cycle isn't a single run AND preserves the connection-local
|
|
34
60
|
* replay lifecycle and running-state behavior below. Skipping verification alone
|
|
35
|
-
* is insufficient. As of `@ag-ui/client@
|
|
61
|
+
* is insufficient. As of `@ag-ui/client@1.0.1`
|
|
36
62
|
* `connectAgent(parameters?, subscriber?)` takes no such option.
|
|
37
63
|
*
|
|
38
64
|
* @param agent - The agent whose `connect()` stream should be consumed.
|
|
@@ -41,6 +67,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
41
67
|
*/
|
|
42
68
|
async function ɵconnectWithoutEventVerification(agent, parameters, subscriber, connect) {
|
|
43
69
|
const self = agent;
|
|
70
|
+
let finalizeRun = () => {
|
|
71
|
+
agent.isRunning = false;
|
|
72
|
+
};
|
|
44
73
|
try {
|
|
45
74
|
var _agent$agentId;
|
|
46
75
|
agent.isRunning = true;
|
|
@@ -65,12 +94,35 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
65
94
|
...agent.subscribers,
|
|
66
95
|
subscriber !== null && subscriber !== void 0 ? subscriber : {}
|
|
67
96
|
];
|
|
68
|
-
|
|
69
|
-
self.activeRunDetach$ =
|
|
97
|
+
const activeRunDetach$ = new rxjs.Subject();
|
|
98
|
+
self.activeRunDetach$ = activeRunDetach$;
|
|
70
99
|
let resolveCompletion;
|
|
71
100
|
self.activeRunCompletionPromise = new Promise((resolve) => {
|
|
72
101
|
resolveCompletion = resolve;
|
|
73
102
|
});
|
|
103
|
+
let detached = false;
|
|
104
|
+
let finalized = false;
|
|
105
|
+
const detachSubscription = activeRunDetach$.subscribe(() => {
|
|
106
|
+
detached = true;
|
|
107
|
+
});
|
|
108
|
+
finalizeRun = () => {
|
|
109
|
+
if (finalized) return;
|
|
110
|
+
finalized = true;
|
|
111
|
+
detachSubscription.unsubscribe();
|
|
112
|
+
if (self.activeRunDetach$ === activeRunDetach$) {
|
|
113
|
+
agent.isRunning = false;
|
|
114
|
+
self.activeRunCompletionPromise = void 0;
|
|
115
|
+
self.activeRunDetach$ = void 0;
|
|
116
|
+
self.onFinalize(input, subscribers);
|
|
117
|
+
}
|
|
118
|
+
resolveCompletion === null || resolveCompletion === void 0 || resolveCompletion();
|
|
119
|
+
resolveCompletion = void 0;
|
|
120
|
+
};
|
|
121
|
+
await self.onInitialize(input, subscribers);
|
|
122
|
+
if (detached) return {
|
|
123
|
+
result: void 0,
|
|
124
|
+
newMessages: []
|
|
125
|
+
};
|
|
74
126
|
const lifecycle = {
|
|
75
127
|
onReplayStarted: () => {
|
|
76
128
|
isReplaying = true;
|
|
@@ -80,29 +132,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
80
132
|
isReplaying = false;
|
|
81
133
|
}
|
|
82
134
|
};
|
|
83
|
-
const source$ = (0, rxjs.defer)(() => connect ? connect(input, lifecycle) : self.connect(input)).pipe((0, rxjs_operators.tap)((event) => {
|
|
135
|
+
const source$ = (0, rxjs.defer)(() => new _ag_ui_client.CompatibilityBoundary().run(input, { run: () => connect ? connect(input, lifecycle) : self.connect(input) })).pipe((0, rxjs_operators.tap)((event) => {
|
|
84
136
|
if (!isReplaying && event.type === _ag_ui_client.EventType.RUN_ERROR) liveErrors.add(event);
|
|
85
|
-
}), (0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(
|
|
137
|
+
}), (0, _ag_ui_client.transformChunks)(self.debugLogger), (0, rxjs_operators.takeUntil)(activeRunDetach$));
|
|
86
138
|
const applied$ = self.apply(input, source$, subscribers);
|
|
87
139
|
await (0, rxjs.lastValueFrom)(self.processApplyEvents(input, applied$, subscribers).pipe((0, rxjs_operators.catchError)((error) => {
|
|
140
|
+
if (self.activeRunDetach$ !== activeRunDetach$) return rxjs.EMPTY;
|
|
88
141
|
agent.isRunning = false;
|
|
89
142
|
if (error instanceof _ag_ui_client.AGUIConnectNotImplementedError) return rxjs.EMPTY;
|
|
90
143
|
return self.onError(input, error, subscribers);
|
|
91
|
-
}), (0, rxjs_operators.finalize)(() => {
|
|
92
|
-
agent.isRunning = false;
|
|
93
|
-
self.onFinalize(input, subscribers);
|
|
94
|
-
resolveCompletion === null || resolveCompletion === void 0 || resolveCompletion();
|
|
95
|
-
resolveCompletion = void 0;
|
|
96
|
-
self.activeRunCompletionPromise = void 0;
|
|
97
|
-
self.activeRunDetach$ = void 0;
|
|
98
|
-
})), { defaultValue: void 0 });
|
|
144
|
+
}), (0, rxjs_operators.finalize)(() => finalizeRun())), { defaultValue: void 0 });
|
|
99
145
|
const newMessages = (0, _ag_ui_client.structuredClone_)(agent.messages).filter((m) => !previousMessageIds.has(m.id));
|
|
100
146
|
return {
|
|
101
147
|
result,
|
|
102
148
|
newMessages
|
|
103
149
|
};
|
|
104
150
|
} finally {
|
|
105
|
-
|
|
151
|
+
finalizeRun();
|
|
106
152
|
}
|
|
107
153
|
}
|
|
108
154
|
|
|
@@ -635,7 +681,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
635
681
|
observeThreadEvents$(threadId, channel$, options) {
|
|
636
682
|
return channel$.pipe((0, rxjs_operators.switchMap)(({ channel }) => this.observeChannelEvent$(channel, CLIENT_AG_UI_EVENT)), (0, rxjs_operators.tap)((payload) => {
|
|
637
683
|
this.updateLastSeenEventId(threadId, payload);
|
|
638
|
-
}), (0, rxjs_operators.mergeMap)((payload) => this.createThreadNotifications(payload, {
|
|
684
|
+
}), (0, rxjs_operators.map)((payload) => options.streamMode === "run" ? (0, _copilotkit_shared.stripIntelligenceRoutingFields)(payload) : payload), (0, rxjs_operators.mergeMap)((payload) => this.createThreadNotifications(payload, {
|
|
639
685
|
completeOnRunError: options.completeOnRunError,
|
|
640
686
|
completeOnRunFinished: options.streamMode === "run",
|
|
641
687
|
errorOnRunError: options.streamMode === "run"
|
|
@@ -957,6 +1003,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
957
1003
|
_defineProperty(this, "_capabilities", void 0);
|
|
958
1004
|
_defineProperty(this, "delegate", void 0);
|
|
959
1005
|
_defineProperty(this, "runtimeInfoPromise", void 0);
|
|
1006
|
+
_defineProperty(this, "connectionAttempt", void 0);
|
|
960
1007
|
_defineProperty(this, "_messageFilter", void 0);
|
|
961
1008
|
_defineProperty(this, "activeRun", void 0);
|
|
962
1009
|
this.runtimeUrl = normalizedRuntimeUrl !== null && normalizedRuntimeUrl !== void 0 ? normalizedRuntimeUrl : config.runtimeUrl;
|
|
@@ -1000,13 +1047,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1000
1047
|
this.runtimeMode = runtimeMode;
|
|
1001
1048
|
this.intelligence = intelligence;
|
|
1002
1049
|
if (unchanged) return;
|
|
1050
|
+
this.connectionAttempt = void 0;
|
|
1003
1051
|
this.runtimeInfoPromise = void 0;
|
|
1004
1052
|
const staleDelegate = this.delegate;
|
|
1005
1053
|
this.delegate = void 0;
|
|
1006
|
-
if (staleDelegate)
|
|
1007
|
-
|
|
1008
|
-
this.detachActiveRun();
|
|
1009
|
-
}
|
|
1054
|
+
if (staleDelegate) staleDelegate.abortRun();
|
|
1055
|
+
this.detachActiveRun();
|
|
1010
1056
|
}
|
|
1011
1057
|
/**
|
|
1012
1058
|
* The agent id used for outbound runtime requests — `runtimeAgentId` when
|
|
@@ -1052,8 +1098,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1052
1098
|
return (_this$_capabilities = this._capabilities) !== null && _this$_capabilities !== void 0 ? _this$_capabilities : {};
|
|
1053
1099
|
}
|
|
1054
1100
|
async detachActiveRun() {
|
|
1055
|
-
|
|
1056
|
-
|
|
1101
|
+
var _this$delegate;
|
|
1102
|
+
this.connectionAttempt = void 0;
|
|
1103
|
+
this.isRunning = false;
|
|
1104
|
+
const delegateDetach = (_this$delegate = this.delegate) === null || _this$delegate === void 0 ? void 0 : _this$delegate.detachActiveRun();
|
|
1105
|
+
const proxyDetach = super.detachActiveRun();
|
|
1106
|
+
await Promise.all([delegateDetach, proxyDetach]);
|
|
1057
1107
|
}
|
|
1058
1108
|
abortRun() {
|
|
1059
1109
|
var _this$activeRun;
|
|
@@ -1108,41 +1158,74 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1108
1158
|
});
|
|
1109
1159
|
}
|
|
1110
1160
|
async connectAgent(parameters, subscriber) {
|
|
1161
|
+
const detached = this.detachActiveRun();
|
|
1162
|
+
const connection = {};
|
|
1163
|
+
const threadId = this.threadId;
|
|
1164
|
+
this.connectionAttempt = connection;
|
|
1165
|
+
const isCurrentConnection = () => this.connectionAttempt === connection && this.threadId === threadId;
|
|
1166
|
+
await detached;
|
|
1167
|
+
if (!isCurrentConnection()) return {
|
|
1168
|
+
result: void 0,
|
|
1169
|
+
newMessages: []
|
|
1170
|
+
};
|
|
1171
|
+
await this.ensureRuntimeConfiguration();
|
|
1172
|
+
if (!isCurrentConnection()) return {
|
|
1173
|
+
result: void 0,
|
|
1174
|
+
newMessages: []
|
|
1175
|
+
};
|
|
1111
1176
|
if (this.runtimeMode !== _copilotkit_shared.RUNTIME_MODE_INTELLIGENCE) return ɵconnectWithoutEventVerification(this, parameters, subscriber, (input, lifecycle) => this.connect(input, lifecycle));
|
|
1112
|
-
if (this.delegate) await this.delegate.detachActiveRun();
|
|
1113
1177
|
await this.resolveDelegate();
|
|
1178
|
+
if (!isCurrentConnection()) return {
|
|
1179
|
+
result: void 0,
|
|
1180
|
+
newMessages: []
|
|
1181
|
+
};
|
|
1114
1182
|
const delegate = this.delegate;
|
|
1115
1183
|
const bridgeSub = delegate.subscribe({
|
|
1116
1184
|
onMessagesChanged: () => {
|
|
1185
|
+
if (!isCurrentConnection()) return;
|
|
1117
1186
|
this.setMessages([...delegate.messages]);
|
|
1118
1187
|
},
|
|
1119
1188
|
onStateChanged: () => {
|
|
1189
|
+
if (!isCurrentConnection()) return;
|
|
1120
1190
|
this.setState({ ...delegate.state });
|
|
1121
1191
|
},
|
|
1122
1192
|
onRunInitialized: () => {
|
|
1193
|
+
if (!isCurrentConnection()) return;
|
|
1123
1194
|
this.isRunning = true;
|
|
1124
1195
|
},
|
|
1125
1196
|
onRunFinalized: () => {
|
|
1197
|
+
if (!isCurrentConnection()) return;
|
|
1198
|
+
this.pendingInterrupts = (0, _ag_ui_client.structuredClone_)(delegate.pendingInterrupts);
|
|
1126
1199
|
this.isRunning = false;
|
|
1127
1200
|
},
|
|
1128
1201
|
onRunFailed: () => {
|
|
1202
|
+
if (!isCurrentConnection()) return;
|
|
1129
1203
|
this.isRunning = false;
|
|
1130
1204
|
},
|
|
1131
1205
|
onRunErrorEvent: () => {
|
|
1206
|
+
if (!isCurrentConnection()) return;
|
|
1132
1207
|
this.isRunning = delegate.isRunning;
|
|
1133
1208
|
},
|
|
1134
1209
|
onRunStartedEvent: () => {
|
|
1210
|
+
if (!isCurrentConnection()) return;
|
|
1135
1211
|
this.isRunning = delegate.isRunning;
|
|
1136
1212
|
}
|
|
1137
1213
|
});
|
|
1138
|
-
const forwardedSubs = this.subscribers.map((
|
|
1214
|
+
const forwardedSubs = [...this.subscribers, ...subscriber ? [subscriber] : []].map((subscriber) => delegate.subscribe(new Proxy(subscriber, { get(target, property, receiver) {
|
|
1215
|
+
const callback = Reflect.get(target, property, receiver);
|
|
1216
|
+
if (typeof callback !== "function") return callback;
|
|
1217
|
+
return (...args) => isCurrentConnection() ? Reflect.apply(callback, target, args) : void 0;
|
|
1218
|
+
} })));
|
|
1139
1219
|
try {
|
|
1140
|
-
const result = await delegate.connectAgent(parameters
|
|
1141
|
-
|
|
1142
|
-
|
|
1220
|
+
const result = await delegate.connectAgent(parameters);
|
|
1221
|
+
if (isCurrentConnection()) {
|
|
1222
|
+
this.setMessages([...delegate.messages]);
|
|
1223
|
+
this.setState({ ...delegate.state });
|
|
1224
|
+
this.pendingInterrupts = (0, _ag_ui_client.structuredClone_)(delegate.pendingInterrupts);
|
|
1225
|
+
}
|
|
1143
1226
|
return result;
|
|
1144
1227
|
} finally {
|
|
1145
|
-
this.isRunning = false;
|
|
1228
|
+
if (isCurrentConnection()) this.isRunning = false;
|
|
1146
1229
|
bridgeSub.unsubscribe();
|
|
1147
1230
|
for (const sub of forwardedSubs) sub.unsubscribe();
|
|
1148
1231
|
}
|
|
@@ -1177,6 +1260,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1177
1260
|
cloned.threadId = this.threadId;
|
|
1178
1261
|
cloned.setState(this.state);
|
|
1179
1262
|
cloned.setMessages(this.messages);
|
|
1263
|
+
cloned.pendingInterrupts = (0, _ag_ui_client.structuredClone_)(this.pendingInterrupts);
|
|
1180
1264
|
if (this.delegate) {
|
|
1181
1265
|
const clonedDelegate = this.delegate.clone();
|
|
1182
1266
|
cloned.delegate = clonedDelegate;
|
|
@@ -1218,6 +1302,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1218
1302
|
if (!this.runtimeUrl) throw new Error("Runtime URL is not set");
|
|
1219
1303
|
const runtimeInfoPromise = (_this$runtimeInfoProm = this.runtimeInfoPromise) !== null && _this$runtimeInfoProm !== void 0 ? _this$runtimeInfoProm : this.fetchRuntimeInfo().then((runtimeInfo) => {
|
|
1220
1304
|
var _runtimeInfo$mode;
|
|
1305
|
+
if (this.runtimeInfoPromise !== runtimeInfoPromise) return;
|
|
1221
1306
|
this.runtimeMode = (_runtimeInfo$mode = runtimeInfo.mode) !== null && _runtimeInfo$mode !== void 0 ? _runtimeInfo$mode : _copilotkit_shared.RUNTIME_MODE_SSE;
|
|
1222
1307
|
this.intelligence = runtimeInfo.intelligence;
|
|
1223
1308
|
});
|
|
@@ -1325,6 +1410,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1325
1410
|
delegate.debugLogger = this.debugLogger;
|
|
1326
1411
|
delegate.setMessages(this.messages);
|
|
1327
1412
|
delegate.setState(this.state);
|
|
1413
|
+
delegate.pendingInterrupts = (0, _ag_ui_client.structuredClone_)(this.pendingInterrupts);
|
|
1328
1414
|
if (hasHeaders(delegate)) delegate.headers = { ...this.headers };
|
|
1329
1415
|
if (hasCredentials(delegate)) delegate.credentials = this.credentials;
|
|
1330
1416
|
}
|
|
@@ -1385,14 +1471,31 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1385
1471
|
const input = _assertClassBrand(_ProxiedCopilotRuntimeAgent_brand, this, _applyMessageFilter).call(this, unfiltered);
|
|
1386
1472
|
this.activeRun = void 0;
|
|
1387
1473
|
const routedId = this.routedAgentId();
|
|
1474
|
+
let httpEvents;
|
|
1388
1475
|
if (this.transport === "single") {
|
|
1389
1476
|
if (!this.singleEndpointUrl) throw new Error("Single endpoint transport requires a runtimeUrl");
|
|
1390
1477
|
const requestInit = this.createSingleRouteRequestInit(input, "agent/connect", { agentId: routedId });
|
|
1391
|
-
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1478
|
+
httpEvents = (0, _ag_ui_client.runHttpRequest)(() => this.fetch(this.singleEndpointUrl, requestInit));
|
|
1479
|
+
} else {
|
|
1480
|
+
const connectUrl = `${this.runtimeUrl}/agent/${routedId}/connect`;
|
|
1481
|
+
const connectRequestInit = this.requestInit(input);
|
|
1482
|
+
httpEvents = (0, _ag_ui_client.runHttpRequest)(() => this.fetch(connectUrl, connectRequestInit));
|
|
1483
|
+
}
|
|
1484
|
+
return withAbortErrorHandling((0, rxjs.defer)(() => {
|
|
1485
|
+
let replayStarted = false;
|
|
1486
|
+
const emptyHistory = [{
|
|
1487
|
+
type: _ag_ui_client.EventType.MESSAGES_SNAPSHOT,
|
|
1488
|
+
messages: []
|
|
1489
|
+
}, {
|
|
1490
|
+
type: _ag_ui_client.EventType.STATE_SNAPSHOT,
|
|
1491
|
+
snapshot: {}
|
|
1492
|
+
}];
|
|
1493
|
+
return (0, _ag_ui_client.transformHttpEventStream)(httpEvents).pipe((0, rxjs_operators.concatMap)((event) => {
|
|
1494
|
+
if (replayStarted) return (0, rxjs.of)(event);
|
|
1495
|
+
replayStarted = true;
|
|
1496
|
+
return (0, rxjs.from)([...emptyHistory, event]);
|
|
1497
|
+
}), (0, rxjs_operators.concatWith)((0, rxjs.defer)(() => replayStarted ? rxjs.EMPTY : (0, rxjs.from)(emptyHistory))));
|
|
1498
|
+
}));
|
|
1396
1499
|
}
|
|
1397
1500
|
function _runViaDelegate(input) {
|
|
1398
1501
|
return (0, rxjs.defer)(() => (0, rxjs.from)(this.resolveDelegate())).pipe((0, rxjs_operators.switchMap)((delegate) => withAbortErrorHandling(delegate.run(input))));
|
|
@@ -1840,7 +1943,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1840
1943
|
* because only `HttpAgent` carries a `headers` field. See #5635.
|
|
1841
1944
|
*/
|
|
1842
1945
|
applyHeadersToAgent(agent) {
|
|
1843
|
-
if (agent
|
|
1946
|
+
if (ɵisHttpAgent(agent)) {
|
|
1844
1947
|
if (!this.agentOwnHeaders.has(agent)) this.agentOwnHeaders.set(agent, { ...agent.headers });
|
|
1845
1948
|
agent.headers = {
|
|
1846
1949
|
...this.agentOwnHeaders.get(agent),
|
|
@@ -1912,6 +2015,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
1912
2015
|
this.applyMessageFilterToAgent(agent);
|
|
1913
2016
|
});
|
|
1914
2017
|
}
|
|
2018
|
+
/** Preserve the exact configured URL when posting to a single endpoint. */
|
|
2019
|
+
get runtimeEndpointUrl() {
|
|
2020
|
+
return this._runtimeEndpointUrl;
|
|
2021
|
+
}
|
|
1915
2022
|
createRuntimeFetch() {
|
|
1916
2023
|
if (!this.runtimeFetch) this.runtimeFetch = (async (input, init) => {
|
|
1917
2024
|
const meta = () => runtimeRequestMeta(init);
|
|
@@ -2977,6 +3084,68 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
2977
3084
|
function isForwardedToClientPlaceholder(content) {
|
|
2978
3085
|
return normalizeToolResultContent(content) === FORWARDED_TO_CLIENT;
|
|
2979
3086
|
}
|
|
3087
|
+
const MEDIA_PART_TYPES = new Set([
|
|
3088
|
+
"image",
|
|
3089
|
+
"audio",
|
|
3090
|
+
"video",
|
|
3091
|
+
"document"
|
|
3092
|
+
]);
|
|
3093
|
+
function isOptionalString(value) {
|
|
3094
|
+
return value === void 0 || typeof value === "string";
|
|
3095
|
+
}
|
|
3096
|
+
function isPartSource(value) {
|
|
3097
|
+
if (!value || typeof value !== "object") return false;
|
|
3098
|
+
const source = value;
|
|
3099
|
+
if (typeof source.value !== "string") return false;
|
|
3100
|
+
switch (source.type) {
|
|
3101
|
+
case "data": return typeof source.mimeType === "string";
|
|
3102
|
+
case "url": return isOptionalString(source.mimeType);
|
|
3103
|
+
case "file": return isOptionalString(source.provider) && isOptionalString(source.mimeType);
|
|
3104
|
+
default: return false;
|
|
3105
|
+
}
|
|
3106
|
+
}
|
|
3107
|
+
/**
|
|
3108
|
+
* Mirrors `ContentPartSchema` from `@ag-ui/core/schemas`. The runtime parses
|
|
3109
|
+
* the request with that schema, and one invalid part there fails the whole run.
|
|
3110
|
+
* Core does not import the schema because the UMD build has no global for it.
|
|
3111
|
+
* A test checks this function against the schema.
|
|
3112
|
+
*/
|
|
3113
|
+
function isContentPart(value) {
|
|
3114
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
3115
|
+
const part = value;
|
|
3116
|
+
if (!isOptionalString(part.id) || part.metadata === null) return false;
|
|
3117
|
+
if (part.type === "text") return typeof part.text === "string";
|
|
3118
|
+
return typeof part.type === "string" && MEDIA_PART_TYPES.has(part.type) && isPartSource(part.source);
|
|
3119
|
+
}
|
|
3120
|
+
/**
|
|
3121
|
+
* Converts what a frontend tool handler returned into tool message content.
|
|
3122
|
+
* A non-empty array of AG-UI content parts passes through unchanged, so an
|
|
3123
|
+
* image reaches the model as media. Everything else becomes a string, as before.
|
|
3124
|
+
*
|
|
3125
|
+
* `text` is the string form, for surfaces typed as `result: string`
|
|
3126
|
+
* (`runTool`, `onToolExecutionEnd`). For content parts it is the JSON text they
|
|
3127
|
+
* received before. It is computed here, so a part that cannot be serialized
|
|
3128
|
+
* throws inside the caller's handler error boundary.
|
|
3129
|
+
*/
|
|
3130
|
+
function toToolResultContent(result) {
|
|
3131
|
+
if (result === void 0 || result === null) return {
|
|
3132
|
+
content: "",
|
|
3133
|
+
text: ""
|
|
3134
|
+
};
|
|
3135
|
+
if (typeof result === "string") return {
|
|
3136
|
+
content: result,
|
|
3137
|
+
text: result
|
|
3138
|
+
};
|
|
3139
|
+
const text = JSON.stringify(result);
|
|
3140
|
+
if (Array.isArray(result) && result.length > 0 && result.every(isContentPart)) return {
|
|
3141
|
+
content: result,
|
|
3142
|
+
text
|
|
3143
|
+
};
|
|
3144
|
+
return {
|
|
3145
|
+
content: text,
|
|
3146
|
+
text
|
|
3147
|
+
};
|
|
3148
|
+
}
|
|
2980
3149
|
|
|
2981
3150
|
//#endregion
|
|
2982
3151
|
//#region src/core/tool-schema.ts
|
|
@@ -3166,6 +3335,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3166
3335
|
_defineProperty(this, "_runDepth", 0);
|
|
3167
3336
|
_defineProperty(this, "_webmcpRegistry", new WebMCPRegistry());
|
|
3168
3337
|
_defineProperty(this, "_lastConnectedThreadIdsByAgent", /* @__PURE__ */ new Map());
|
|
3338
|
+
_defineProperty(this, "_lastActiveThreadIdsByAgent", /* @__PURE__ */ new Map());
|
|
3169
3339
|
_defineProperty(this, "_anonymousAgentIds", /* @__PURE__ */ new WeakMap());
|
|
3170
3340
|
_defineProperty(this, "_nextAnonymousAgentId", 0);
|
|
3171
3341
|
_defineProperty(this, "_executingToolCalls", /* @__PURE__ */ new WeakMap());
|
|
@@ -3342,7 +3512,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3342
3512
|
*/
|
|
3343
3513
|
async connectAgent({ agent }) {
|
|
3344
3514
|
var _agent$threadId;
|
|
3345
|
-
|
|
3515
|
+
const connection = {};
|
|
3516
|
+
this._connectionGenerations.set(agent, connection);
|
|
3346
3517
|
const incomingThreadId = (_agent$threadId = agent.threadId) !== null && _agent$threadId !== void 0 ? _agent$threadId : null;
|
|
3347
3518
|
const previousReplay = this._interactionAbortControllers.get(agent);
|
|
3348
3519
|
if (previousReplay && previousReplay.threadId !== incomingThreadId) {
|
|
@@ -3351,14 +3522,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3351
3522
|
this._executingToolCalls.delete(agent);
|
|
3352
3523
|
}
|
|
3353
3524
|
try {
|
|
3354
|
-
var
|
|
3525
|
+
var _ref, _agent$threadId2, _agent$threadId3, _agent$threadId4, _this$_interactionAbo;
|
|
3355
3526
|
const restoreKey = this.getConnectRestoreKey(agent);
|
|
3356
|
-
const
|
|
3357
|
-
this._lastConnectedThreadIdsByAgent.
|
|
3527
|
+
const previousThreadId = this._lastActiveThreadIdsByAgent.get(restoreKey);
|
|
3528
|
+
const isFreshRestore = incomingThreadId !== ((_ref = previousThreadId !== null && previousThreadId !== void 0 ? previousThreadId : this._lastConnectedThreadIdsByAgent.get(restoreKey)) !== null && _ref !== void 0 ? _ref : null);
|
|
3358
3529
|
await agent.detachActiveRun();
|
|
3530
|
+
if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId2 = agent.threadId) !== null && _agent$threadId2 !== void 0 ? _agent$threadId2 : null) !== incomingThreadId) return {
|
|
3531
|
+
result: void 0,
|
|
3532
|
+
newMessages: []
|
|
3533
|
+
};
|
|
3534
|
+
this._lastConnectedThreadIdsByAgent.set(restoreKey, incomingThreadId);
|
|
3535
|
+
this._lastActiveThreadIdsByAgent.set(restoreKey, incomingThreadId);
|
|
3359
3536
|
if (isFreshRestore) {
|
|
3360
3537
|
agent.setMessages([]);
|
|
3361
3538
|
agent.setState({});
|
|
3539
|
+
if (previousThreadId !== void 0 && previousThreadId !== incomingThreadId) agent.pendingInterrupts = [];
|
|
3362
3540
|
const cursorAware = agent;
|
|
3363
3541
|
if (incomingThreadId) {
|
|
3364
3542
|
if (typeof cursorAware.clearReplayCursor === "function") cursorAware.clearReplayCursor(incomingThreadId);
|
|
@@ -3373,12 +3551,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3373
3551
|
agent
|
|
3374
3552
|
});
|
|
3375
3553
|
}, "Subscriber onAgentRunStarted error:");
|
|
3554
|
+
if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId3 = agent.threadId) !== null && _agent$threadId3 !== void 0 ? _agent$threadId3 : null) !== incomingThreadId) return {
|
|
3555
|
+
result: void 0,
|
|
3556
|
+
newMessages: []
|
|
3557
|
+
};
|
|
3376
3558
|
const runAgentResult = await agent.connectAgent({
|
|
3377
3559
|
forwardedProps: this._internal.properties,
|
|
3378
3560
|
tools: this.buildFrontendTools(agent.agentId),
|
|
3379
3561
|
context: this._internal.getContextForAgent(agent.agentId)
|
|
3380
3562
|
}, this.createAgentErrorSubscriber(agent));
|
|
3381
|
-
if (((_agent$
|
|
3563
|
+
if (this._connectionGenerations.get(agent) !== connection || ((_agent$threadId4 = agent.threadId) !== null && _agent$threadId4 !== void 0 ? _agent$threadId4 : null) !== incomingThreadId) return runAgentResult;
|
|
3382
3564
|
const controller = new AbortController();
|
|
3383
3565
|
const replay = (_this$_interactionAbo = this._interactionAbortControllers.get(agent)) !== null && _this$_interactionAbo !== void 0 ? _this$_interactionAbo : {
|
|
3384
3566
|
threadId: incomingThreadId,
|
|
@@ -3435,6 +3617,12 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3435
3617
|
continuationHandoff === null || continuationHandoff === void 0 || continuationHandoff.cancel();
|
|
3436
3618
|
throw error;
|
|
3437
3619
|
}
|
|
3620
|
+
const resumedRunId = this._internal.stateManager.getContinuationRunId(agent, {
|
|
3621
|
+
threadId: agent.threadId,
|
|
3622
|
+
resume,
|
|
3623
|
+
forwardedProps
|
|
3624
|
+
});
|
|
3625
|
+
const logicalHandoff = continuationHandoff !== null && continuationHandoff !== void 0 ? continuationHandoff : resumedRunId ? this._internal.stateManager.markNextRunAsContinuation(agent, resumedRunId) : void 0;
|
|
3438
3626
|
const isTopLevel = this._runDepth === 0;
|
|
3439
3627
|
let originalAbortRun;
|
|
3440
3628
|
if (isTopLevel) {
|
|
@@ -3455,18 +3643,19 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3455
3643
|
}
|
|
3456
3644
|
this._runDepth++;
|
|
3457
3645
|
try {
|
|
3458
|
-
let logicalRunId = runId;
|
|
3646
|
+
let logicalRunId = resumedRunId !== null && resumedRunId !== void 0 ? resumedRunId : runId;
|
|
3459
3647
|
const agentSubscriber = this.createAgentErrorSubscriber(agent);
|
|
3460
3648
|
let started = false;
|
|
3461
3649
|
const onRunStartedEvent = agentSubscriber.onRunStartedEvent;
|
|
3462
3650
|
const onRunInitialized = agentSubscriber.onRunInitialized;
|
|
3463
3651
|
agentSubscriber.onRunInitialized = async (params) => {
|
|
3464
|
-
|
|
3652
|
+
this._lastActiveThreadIdsByAgent.set(this.getConnectRestoreKey(agent), params.input.threadId);
|
|
3653
|
+
logicalHandoff === null || logicalHandoff === void 0 || logicalHandoff.bind(params.input);
|
|
3465
3654
|
return onRunInitialized === null || onRunInitialized === void 0 ? void 0 : onRunInitialized(params);
|
|
3466
3655
|
};
|
|
3467
3656
|
agentSubscriber.onRunStartedEvent = async (params) => {
|
|
3468
3657
|
started = true;
|
|
3469
|
-
if (!
|
|
3658
|
+
if (!logicalHandoff) logicalRunId = params.input.runId;
|
|
3470
3659
|
return onRunStartedEvent === null || onRunStartedEvent === void 0 ? void 0 : onRunStartedEvent(params);
|
|
3471
3660
|
};
|
|
3472
3661
|
const pinRunIdOnWire = runId !== void 0 && !continuationHandoff;
|
|
@@ -3481,14 +3670,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3481
3670
|
context: this._internal.getContextForAgent(agent.agentId)
|
|
3482
3671
|
};
|
|
3483
3672
|
const runAgentResult = await agent.runAgent(agentRunInput, agentSubscriber);
|
|
3484
|
-
if (!started)
|
|
3673
|
+
if (!started) logicalHandoff === null || logicalHandoff === void 0 || logicalHandoff.cancel();
|
|
3485
3674
|
return await this.processAgentResult({
|
|
3486
3675
|
runAgentResult,
|
|
3487
3676
|
agent,
|
|
3488
3677
|
runId: logicalRunId
|
|
3489
3678
|
});
|
|
3490
3679
|
} catch (error) {
|
|
3491
|
-
|
|
3680
|
+
logicalHandoff === null || logicalHandoff === void 0 || logicalHandoff.cancel();
|
|
3492
3681
|
const runError = error instanceof Error ? error : new Error(String(error));
|
|
3493
3682
|
const context = {};
|
|
3494
3683
|
if (agent.agentId) context.agentId = agent.agentId;
|
|
@@ -3619,6 +3808,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3619
3808
|
return (_this$_runAbortContro3 = this._runAbortController) === null || _this$_runAbortContro3 === void 0 ? void 0 : _this$_runAbortContro3.signal;
|
|
3620
3809
|
})(), discardOnAbort = false }) {
|
|
3621
3810
|
let toolCallResult = "";
|
|
3811
|
+
let toolCallText = "";
|
|
3622
3812
|
let errorMessage;
|
|
3623
3813
|
let isArgumentError = false;
|
|
3624
3814
|
let parsedArgs;
|
|
@@ -3657,9 +3847,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3657
3847
|
agent,
|
|
3658
3848
|
signal
|
|
3659
3849
|
});
|
|
3660
|
-
|
|
3661
|
-
else if (typeof result === "string") toolCallResult = result;
|
|
3662
|
-
else toolCallResult = JSON.stringify(result);
|
|
3850
|
+
({content: toolCallResult, text: toolCallText} = toToolResultContent(result));
|
|
3663
3851
|
} catch (error) {
|
|
3664
3852
|
const handlerError = error instanceof Error ? error : new Error(String(error));
|
|
3665
3853
|
errorMessage = handlerError.message;
|
|
@@ -3676,7 +3864,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3676
3864
|
}
|
|
3677
3865
|
});
|
|
3678
3866
|
}
|
|
3679
|
-
if (errorMessage) toolCallResult = `Error: ${errorMessage}`;
|
|
3867
|
+
if (errorMessage) toolCallResult = toolCallText = `Error: ${errorMessage}`;
|
|
3680
3868
|
await this._internal.notifySubscribers((subscriber) => {
|
|
3681
3869
|
var _subscriber$onToolExe2;
|
|
3682
3870
|
return (_subscriber$onToolExe2 = subscriber.onToolExecutionEnd) === null || _subscriber$onToolExe2 === void 0 ? void 0 : _subscriber$onToolExe2.call(subscriber, {
|
|
@@ -3684,12 +3872,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3684
3872
|
toolCallId: toolCall.id,
|
|
3685
3873
|
agentId,
|
|
3686
3874
|
toolName: toolCall.function.name,
|
|
3687
|
-
result: errorMessage ? "" :
|
|
3875
|
+
result: errorMessage ? "" : toolCallText,
|
|
3688
3876
|
error: errorMessage
|
|
3689
3877
|
});
|
|
3690
3878
|
}, "Subscriber onToolExecutionEnd error:");
|
|
3691
3879
|
return {
|
|
3692
|
-
|
|
3880
|
+
content: toolCallResult,
|
|
3881
|
+
result: toolCallText,
|
|
3693
3882
|
error: errorMessage,
|
|
3694
3883
|
isArgumentError
|
|
3695
3884
|
};
|
|
@@ -3701,6 +3890,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3701
3890
|
const threadId = agent.threadId;
|
|
3702
3891
|
if ((tool === null || tool === void 0 ? void 0 : tool.agentId) && tool.agentId !== agent.agentId) return false;
|
|
3703
3892
|
let handlerResult = {
|
|
3893
|
+
content: "",
|
|
3704
3894
|
result: "",
|
|
3705
3895
|
error: void 0,
|
|
3706
3896
|
isArgumentError: false
|
|
@@ -3726,7 +3916,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3726
3916
|
id: (0, _copilotkit_shared.randomUUID)(),
|
|
3727
3917
|
role: "tool",
|
|
3728
3918
|
toolCallId: toolCall.id,
|
|
3729
|
-
content: handlerResult.
|
|
3919
|
+
content: handlerResult.content
|
|
3730
3920
|
};
|
|
3731
3921
|
agent.messages.splice(insertAt, 0, toolMessage);
|
|
3732
3922
|
if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
|
|
@@ -3743,6 +3933,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3743
3933
|
const threadId = agent.threadId;
|
|
3744
3934
|
if ((wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.agentId) && wildcardTool.agentId !== agent.agentId) return false;
|
|
3745
3935
|
let toolCallResult = "";
|
|
3936
|
+
let toolCallText = "";
|
|
3746
3937
|
let errorMessage;
|
|
3747
3938
|
if (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.handler) {
|
|
3748
3939
|
let parsedArgs;
|
|
@@ -3784,9 +3975,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3784
3975
|
agent,
|
|
3785
3976
|
signal
|
|
3786
3977
|
});
|
|
3787
|
-
|
|
3788
|
-
else if (typeof result === "string") toolCallResult = result;
|
|
3789
|
-
else toolCallResult = JSON.stringify(result);
|
|
3978
|
+
({content: toolCallResult, text: toolCallText} = toToolResultContent(result));
|
|
3790
3979
|
} catch (error) {
|
|
3791
3980
|
const handlerError = error instanceof Error ? error : new Error(String(error));
|
|
3792
3981
|
errorMessage = handlerError.message;
|
|
@@ -3803,7 +3992,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3803
3992
|
}
|
|
3804
3993
|
});
|
|
3805
3994
|
}
|
|
3806
|
-
if (errorMessage) toolCallResult = `Error: ${errorMessage}`;
|
|
3995
|
+
if (errorMessage) toolCallResult = toolCallText = `Error: ${errorMessage}`;
|
|
3807
3996
|
await this._internal.notifySubscribers((subscriber) => {
|
|
3808
3997
|
var _subscriber$onToolExe4;
|
|
3809
3998
|
return (_subscriber$onToolExe4 = subscriber.onToolExecutionEnd) === null || _subscriber$onToolExe4 === void 0 ? void 0 : _subscriber$onToolExe4.call(subscriber, {
|
|
@@ -3811,7 +4000,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3811
4000
|
toolCallId: toolCall.id,
|
|
3812
4001
|
agentId,
|
|
3813
4002
|
toolName: toolCall.function.name,
|
|
3814
|
-
result: errorMessage ? "" :
|
|
4003
|
+
result: errorMessage ? "" : toolCallText,
|
|
3815
4004
|
error: errorMessage
|
|
3816
4005
|
});
|
|
3817
4006
|
}, "Subscriber onToolExecutionEnd error:");
|
|
@@ -3884,6 +4073,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3884
4073
|
};
|
|
3885
4074
|
agent.messages.push(assistantMessage);
|
|
3886
4075
|
let handlerResult = {
|
|
4076
|
+
content: "",
|
|
3887
4077
|
result: "",
|
|
3888
4078
|
error: void 0,
|
|
3889
4079
|
isArgumentError: false
|
|
@@ -3900,7 +4090,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
3900
4090
|
id: (0, _copilotkit_shared.randomUUID)(),
|
|
3901
4091
|
role: "tool",
|
|
3902
4092
|
toolCallId,
|
|
3903
|
-
content: handlerResult.
|
|
4093
|
+
content: handlerResult.content
|
|
3904
4094
|
};
|
|
3905
4095
|
const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
|
|
3906
4096
|
if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
|
|
@@ -4057,6 +4247,15 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4057
4247
|
* Tracks agent state snapshots and message-to-run associations.
|
|
4058
4248
|
*/
|
|
4059
4249
|
var StateManager = class {
|
|
4250
|
+
/** Find the interrupted logical run without consuming its pending answers. */
|
|
4251
|
+
getContinuationRunId(agent, input) {
|
|
4252
|
+
var _this$interruptedRuns, _input$resume, _input$resume2;
|
|
4253
|
+
const pending = (_this$interruptedRuns = this.interruptedRuns.get(agent)) === null || _this$interruptedRuns === void 0 ? void 0 : _this$interruptedRuns.get(input.threadId);
|
|
4254
|
+
if (!pending) return void 0;
|
|
4255
|
+
if (pending.interruptIds === null) return input.resume === void 0 && isContinuation(input) ? pending.runId : void 0;
|
|
4256
|
+
const submitted = new Set((_input$resume = input.resume) === null || _input$resume === void 0 ? void 0 : _input$resume.map((entry) => entry.interruptId));
|
|
4257
|
+
return submitted.size > 0 && submitted.size === ((_input$resume2 = input.resume) === null || _input$resume2 === void 0 ? void 0 : _input$resume2.length) && submitted.size === pending.interruptIds.length && pending.interruptIds.every((id) => submitted.has(id)) ? pending.runId : void 0;
|
|
4258
|
+
}
|
|
4060
4259
|
constructor(core) {
|
|
4061
4260
|
this.core = core;
|
|
4062
4261
|
_defineProperty(this, "stateByRun", /* @__PURE__ */ new Map());
|
|
@@ -4065,6 +4264,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4065
4264
|
_defineProperty(this, "activeRun", /* @__PURE__ */ new Map());
|
|
4066
4265
|
_defineProperty(this, "agentSubscriptions", /* @__PURE__ */ new Map());
|
|
4067
4266
|
_defineProperty(this, "pendingContinuations", /* @__PURE__ */ new WeakMap());
|
|
4267
|
+
_defineProperty(this, "interruptedRuns", /* @__PURE__ */ new WeakMap());
|
|
4068
4268
|
}
|
|
4069
4269
|
/**
|
|
4070
4270
|
* Initialize state tracking for an agent
|
|
@@ -4106,6 +4306,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4106
4306
|
}
|
|
4107
4307
|
let revoked = false;
|
|
4108
4308
|
let subRunId;
|
|
4309
|
+
let legacyInterrupted = false;
|
|
4109
4310
|
let runFinished = false;
|
|
4110
4311
|
const pendingResults = /* @__PURE__ */ new WeakMap();
|
|
4111
4312
|
const reconcilePendingResults = (historyMessages, input) => {
|
|
@@ -4184,27 +4385,52 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4184
4385
|
};
|
|
4185
4386
|
const { unsubscribe } = agent.subscribe({
|
|
4186
4387
|
onRunStartedEvent: ({ event, input, state }) => {
|
|
4388
|
+
var _event$input, _this$interruptedRuns2;
|
|
4187
4389
|
if (revoked) return;
|
|
4188
4390
|
const pendingForAgent = this.pendingContinuations.get(agent);
|
|
4189
4391
|
const internalContinuation = [...pendingForAgent !== null && pendingForAgent !== void 0 ? pendingForAgent : []].find((pending) => pending.expectedInput === input);
|
|
4190
4392
|
internalContinuation === null || internalContinuation === void 0 || internalContinuation.cancel();
|
|
4393
|
+
const startedInput = (_event$input = event.input) !== null && _event$input !== void 0 ? _event$input : {
|
|
4394
|
+
...input,
|
|
4395
|
+
threadId: event.threadId
|
|
4396
|
+
};
|
|
4397
|
+
const resumedRunId = this.getContinuationRunId(agent, startedInput);
|
|
4398
|
+
(_this$interruptedRuns2 = this.interruptedRuns.get(agent)) === null || _this$interruptedRuns2 === void 0 || _this$interruptedRuns2.delete(startedInput.threadId);
|
|
4399
|
+
legacyInterrupted = false;
|
|
4191
4400
|
if (internalContinuation) {
|
|
4192
4401
|
var _ref, _internalContinuation;
|
|
4193
4402
|
subRunId = (_ref = (_internalContinuation = internalContinuation.expectedRunId) !== null && _internalContinuation !== void 0 ? _internalContinuation : event.runId) !== null && _ref !== void 0 ? _ref : input.runId;
|
|
4194
|
-
} else if (
|
|
4403
|
+
} else if (resumedRunId) subRunId = resumedRunId;
|
|
4404
|
+
else if (runFinished && input.runId === subRunId && !isContinuation(input) && (event.runId == null || event.runId === subRunId)) subRunId = (0, _ag_ui_client.randomUUID)();
|
|
4195
4405
|
else subRunId = event.runId || input.runId;
|
|
4196
4406
|
runFinished = false;
|
|
4197
4407
|
this.handleRunStarted(agent, effectiveInput(input), state);
|
|
4198
4408
|
},
|
|
4199
|
-
onRunFinishedEvent: ({ input, state, messages }) => {
|
|
4409
|
+
onRunFinishedEvent: ({ event, input, state, messages }) => {
|
|
4410
|
+
var _event$outcome;
|
|
4200
4411
|
if (revoked) return;
|
|
4201
4412
|
runFinished = true;
|
|
4202
4413
|
const effective = effectiveInput(input);
|
|
4414
|
+
let interrupted = this.interruptedRuns.get(agent);
|
|
4415
|
+
if (((_event$outcome = event.outcome) === null || _event$outcome === void 0 ? void 0 : _event$outcome.type) === "interrupt" || legacyInterrupted) {
|
|
4416
|
+
var _event$outcome2;
|
|
4417
|
+
if (!interrupted) {
|
|
4418
|
+
interrupted = /* @__PURE__ */ new Map();
|
|
4419
|
+
this.interruptedRuns.set(agent, interrupted);
|
|
4420
|
+
}
|
|
4421
|
+
interrupted.set(event.threadId, {
|
|
4422
|
+
runId: effective.runId,
|
|
4423
|
+
interruptIds: ((_event$outcome2 = event.outcome) === null || _event$outcome2 === void 0 ? void 0 : _event$outcome2.type) === "interrupt" ? event.outcome.interrupts.map((interrupt) => interrupt.id) : null
|
|
4424
|
+
});
|
|
4425
|
+
} else interrupted === null || interrupted === void 0 || interrupted.delete(event.threadId);
|
|
4203
4426
|
const mutation = reconcilePendingResults(messages, input);
|
|
4204
4427
|
clearPendingResults(input);
|
|
4205
4428
|
this.handleRunFinished(agent, effective, state);
|
|
4206
4429
|
return mutation;
|
|
4207
4430
|
},
|
|
4431
|
+
onCustomEvent: ({ event }) => {
|
|
4432
|
+
if (!revoked && event.name === "on_interrupt") legacyInterrupted = true;
|
|
4433
|
+
},
|
|
4208
4434
|
onRunErrorEvent: ({ input, state, messages }) => {
|
|
4209
4435
|
if (revoked) return;
|
|
4210
4436
|
runFinished = true;
|
|
@@ -4261,6 +4487,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4261
4487
|
unsubscribe: () => {
|
|
4262
4488
|
revoked = true;
|
|
4263
4489
|
this.pendingContinuations.delete(agent);
|
|
4490
|
+
this.interruptedRuns.delete(agent);
|
|
4264
4491
|
unsubscribe();
|
|
4265
4492
|
}
|
|
4266
4493
|
});
|
|
@@ -4435,6 +4662,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4435
4662
|
* Clear all state for an agent
|
|
4436
4663
|
*/
|
|
4437
4664
|
clearAgentState(agentId) {
|
|
4665
|
+
var _this$agentSubscripti;
|
|
4666
|
+
const agent = (_this$agentSubscripti = this.agentSubscriptions.get(agentId)) === null || _this$agentSubscripti === void 0 ? void 0 : _this$agentSubscripti.agent;
|
|
4667
|
+
if (agent) this.interruptedRuns.delete(agent);
|
|
4438
4668
|
this.stateByRun.delete(agentId);
|
|
4439
4669
|
this.messageToRun.delete(agentId);
|
|
4440
4670
|
this.rawEventByMessage.delete(agentId);
|
|
@@ -4443,7 +4673,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4443
4673
|
* Clear all state for a thread
|
|
4444
4674
|
*/
|
|
4445
4675
|
clearThreadState(agentId, threadId) {
|
|
4446
|
-
var _this$stateByRun$get4, _this$messageToRun$ge2, _this$rawEventByMessa8;
|
|
4676
|
+
var _this$agentSubscripti2, _this$interruptedRuns3, _this$stateByRun$get4, _this$messageToRun$ge2, _this$rawEventByMessa8;
|
|
4677
|
+
const agent = (_this$agentSubscripti2 = this.agentSubscriptions.get(agentId)) === null || _this$agentSubscripti2 === void 0 ? void 0 : _this$agentSubscripti2.agent;
|
|
4678
|
+
if (agent) (_this$interruptedRuns3 = this.interruptedRuns.get(agent)) === null || _this$interruptedRuns3 === void 0 || _this$interruptedRuns3.delete(threadId);
|
|
4447
4679
|
(_this$stateByRun$get4 = this.stateByRun.get(agentId)) === null || _this$stateByRun$get4 === void 0 || _this$stateByRun$get4.delete(threadId);
|
|
4448
4680
|
(_this$messageToRun$ge2 = this.messageToRun.get(agentId)) === null || _this$messageToRun$ge2 === void 0 || _this$messageToRun$ge2.delete(threadId);
|
|
4449
4681
|
(_this$rawEventByMessa8 = this.rawEventByMessage.get(agentId)) === null || _this$rawEventByMessa8 === void 0 || _this$rawEventByMessa8.delete(threadId);
|
|
@@ -4511,6 +4743,745 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
4511
4743
|
}
|
|
4512
4744
|
};
|
|
4513
4745
|
|
|
4746
|
+
//#endregion
|
|
4747
|
+
//#region src/core/trajectory-connection.ts
|
|
4748
|
+
const TIMEOUT_MS = 1e4;
|
|
4749
|
+
const BATCH_INTERVAL_MS = 2e3;
|
|
4750
|
+
const MAX_BATCH_EVENTS = 50;
|
|
4751
|
+
const MAX_BATCH_BYTES = 64 * 1024;
|
|
4752
|
+
const MAX_EVENT_BYTES = 16 * 1024;
|
|
4753
|
+
const byteLength = (value) => new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
4754
|
+
const reconnectDelay = (0, _copilotkit_shared.phoenixExponentialBackoff)(1e3, 1e4);
|
|
4755
|
+
function isObject(value) {
|
|
4756
|
+
return typeof value === "object" && value !== null;
|
|
4757
|
+
}
|
|
4758
|
+
function isNonNegativeInteger(value) {
|
|
4759
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
4760
|
+
}
|
|
4761
|
+
function isBatchAcknowledgement(value, eventCount) {
|
|
4762
|
+
return isObject(value) && isNonNegativeInteger(value.accepted) && isNonNegativeInteger(value.rejected) && value.accepted + value.rejected === eventCount && (value.highestSeq === null || isNonNegativeInteger(value.highestSeq)) && (value.accepted === 0 || value.highestSeq !== null);
|
|
4763
|
+
}
|
|
4764
|
+
function isGrant(value) {
|
|
4765
|
+
return isObject(value) && typeof value.joinToken === "string" && value.joinToken.length > 0 && isObject(value.realtime) && typeof value.realtime.clientUrl === "string" && value.realtime.clientUrl.length > 0 && typeof value.realtime.topic === "string" && value.realtime.topic.length > 0;
|
|
4766
|
+
}
|
|
4767
|
+
function errorCode(value, fallback) {
|
|
4768
|
+
return isObject(value) && typeof value.code === "string" && /^[A-Z][A-Z0-9_]{0,63}$/.test(value.code) ? value.code : fallback;
|
|
4769
|
+
}
|
|
4770
|
+
/** Owns bounded, connected-only capture batches. Failed batches are never resent. */
|
|
4771
|
+
var TrajectoryConnection = class {
|
|
4772
|
+
constructor(core, onChange) {
|
|
4773
|
+
this.core = core;
|
|
4774
|
+
this.onChange = onChange;
|
|
4775
|
+
_defineProperty(this, "session", void 0);
|
|
4776
|
+
_defineProperty(this, "nextSeq", Math.floor(Date.now()) * 1e3);
|
|
4777
|
+
}
|
|
4778
|
+
get trajectoryId() {
|
|
4779
|
+
var _this$session;
|
|
4780
|
+
return ((_this$session = this.session) === null || _this$session === void 0 ? void 0 : _this$session.ready) ? this.session.trajectoryId : null;
|
|
4781
|
+
}
|
|
4782
|
+
start(trajectoryId, config) {
|
|
4783
|
+
if (this.session && (trajectoryId === void 0 || trajectoryId === this.session.trajectoryId)) return this.session.promise;
|
|
4784
|
+
this.stop();
|
|
4785
|
+
const id = trajectoryId !== null && trajectoryId !== void 0 ? trajectoryId : crypto.randomUUID();
|
|
4786
|
+
let resolveStart;
|
|
4787
|
+
const promise = new Promise((resolve) => {
|
|
4788
|
+
resolveStart = resolve;
|
|
4789
|
+
});
|
|
4790
|
+
const session = {
|
|
4791
|
+
trajectoryId: id,
|
|
4792
|
+
config,
|
|
4793
|
+
promise,
|
|
4794
|
+
resolve: resolveStart,
|
|
4795
|
+
retries: 0,
|
|
4796
|
+
queue: [],
|
|
4797
|
+
dropped: 0,
|
|
4798
|
+
ready: false,
|
|
4799
|
+
started: false,
|
|
4800
|
+
offline: typeof navigator !== "undefined" && navigator.onLine === false,
|
|
4801
|
+
onOffline: () => {
|
|
4802
|
+
if (this.session !== session) return;
|
|
4803
|
+
session.offline = true;
|
|
4804
|
+
if (session.connection) this.fail(session, session.connection, "CONNECTION_LOST");
|
|
4805
|
+
clearTimeout(session.retry);
|
|
4806
|
+
session.retry = void 0;
|
|
4807
|
+
},
|
|
4808
|
+
onOnline: () => {
|
|
4809
|
+
if (this.session !== session) return;
|
|
4810
|
+
session.offline = false;
|
|
4811
|
+
if (!session.connection) {
|
|
4812
|
+
clearTimeout(session.retry);
|
|
4813
|
+
session.retry = void 0;
|
|
4814
|
+
this.connect(session);
|
|
4815
|
+
}
|
|
4816
|
+
}
|
|
4817
|
+
};
|
|
4818
|
+
this.session = session;
|
|
4819
|
+
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
4820
|
+
window.addEventListener("offline", session.onOffline);
|
|
4821
|
+
window.addEventListener("online", session.onOnline);
|
|
4822
|
+
}
|
|
4823
|
+
if (session.offline) {
|
|
4824
|
+
this.report(session, "CONNECTION_LOST");
|
|
4825
|
+
this.end(session, {
|
|
4826
|
+
status: "error",
|
|
4827
|
+
code: "CONNECTION_LOST"
|
|
4828
|
+
});
|
|
4829
|
+
} else this.connect(session);
|
|
4830
|
+
return promise;
|
|
4831
|
+
}
|
|
4832
|
+
stop() {
|
|
4833
|
+
const session = this.session;
|
|
4834
|
+
if (!session) return;
|
|
4835
|
+
if (session.ready && session.connection) this.flush(session, session.connection);
|
|
4836
|
+
this.end(session, {
|
|
4837
|
+
status: "error",
|
|
4838
|
+
code: "CANCELLED"
|
|
4839
|
+
});
|
|
4840
|
+
}
|
|
4841
|
+
emit(name, value) {
|
|
4842
|
+
var _session$collector;
|
|
4843
|
+
const session = this.session;
|
|
4844
|
+
if (session === null || session === void 0 ? void 0 : session.ready) (_session$collector = session.collector) === null || _session$collector === void 0 || _session$collector.emit(name, value);
|
|
4845
|
+
else if (session === null || session === void 0 ? void 0 : session.started) this.addDropped(session, 1, false);
|
|
4846
|
+
}
|
|
4847
|
+
report(session, code) {
|
|
4848
|
+
try {
|
|
4849
|
+
var _session$config$onErr, _session$config;
|
|
4850
|
+
(_session$config$onErr = (_session$config = session.config).onError) === null || _session$config$onErr === void 0 || _session$config$onErr.call(_session$config, {
|
|
4851
|
+
code,
|
|
4852
|
+
message: `Trajectory capture: ${code}.`
|
|
4853
|
+
});
|
|
4854
|
+
} catch (_unused) {}
|
|
4855
|
+
}
|
|
4856
|
+
current(session, connection) {
|
|
4857
|
+
return this.session === session && session.connection === connection;
|
|
4858
|
+
}
|
|
4859
|
+
end(session, result) {
|
|
4860
|
+
var _session$collector2;
|
|
4861
|
+
if (this.session !== session) return;
|
|
4862
|
+
this.session = void 0;
|
|
4863
|
+
clearTimeout(session.retry);
|
|
4864
|
+
(_session$collector2 = session.collector) === null || _session$collector2 === void 0 || _session$collector2.stop();
|
|
4865
|
+
session.ready = false;
|
|
4866
|
+
if (session.queue.length > 0) this.report(session, "EVENTS_DROPPED");
|
|
4867
|
+
this.discardQueue(session);
|
|
4868
|
+
if (session.connection) {
|
|
4869
|
+
for (const pending of session.connection.pending) {
|
|
4870
|
+
pending.finish("PERSISTENCE_UNKNOWN");
|
|
4871
|
+
this.report(session, "PERSISTENCE_UNKNOWN");
|
|
4872
|
+
}
|
|
4873
|
+
this.cleanup(session, session.connection);
|
|
4874
|
+
}
|
|
4875
|
+
if (typeof window !== "undefined" && typeof window.removeEventListener === "function") {
|
|
4876
|
+
window.removeEventListener("offline", session.onOffline);
|
|
4877
|
+
window.removeEventListener("online", session.onOnline);
|
|
4878
|
+
}
|
|
4879
|
+
session.resolve(result);
|
|
4880
|
+
this.onChange();
|
|
4881
|
+
}
|
|
4882
|
+
cleanup(session, connection) {
|
|
4883
|
+
session.connection = void 0;
|
|
4884
|
+
clearTimeout(connection.timer);
|
|
4885
|
+
connection.abort.abort();
|
|
4886
|
+
for (const pending of connection.pending) clearTimeout(pending.timer);
|
|
4887
|
+
connection.pending.clear();
|
|
4888
|
+
const { socket, channel } = connection;
|
|
4889
|
+
try {
|
|
4890
|
+
if (channel) {
|
|
4891
|
+
for (const [event, ref] of connection.channelRefs) channel.off(event, ref);
|
|
4892
|
+
channel.leave();
|
|
4893
|
+
}
|
|
4894
|
+
} finally {
|
|
4895
|
+
if (socket) {
|
|
4896
|
+
socket.off(connection.socketRefs);
|
|
4897
|
+
socket.disconnect();
|
|
4898
|
+
}
|
|
4899
|
+
}
|
|
4900
|
+
}
|
|
4901
|
+
fail(session, connection, code, retryable = true) {
|
|
4902
|
+
var _session$collector3;
|
|
4903
|
+
if (!this.current(session, connection)) return;
|
|
4904
|
+
(_session$collector3 = session.collector) === null || _session$collector3 === void 0 || _session$collector3.stop();
|
|
4905
|
+
session.collector = void 0;
|
|
4906
|
+
if (session.ready && session.started) session.promise = new Promise((resolve) => {
|
|
4907
|
+
session.resolve = resolve;
|
|
4908
|
+
});
|
|
4909
|
+
session.ready = false;
|
|
4910
|
+
this.discardQueue(session);
|
|
4911
|
+
for (const pending of connection.pending) pending.finish("PERSISTENCE_UNKNOWN");
|
|
4912
|
+
if (!this.current(session, connection)) return;
|
|
4913
|
+
this.cleanup(session, connection);
|
|
4914
|
+
this.onChange();
|
|
4915
|
+
this.report(session, code);
|
|
4916
|
+
if (this.session !== session) return;
|
|
4917
|
+
if (!session.started || !retryable) {
|
|
4918
|
+
this.end(session, {
|
|
4919
|
+
status: "error",
|
|
4920
|
+
code
|
|
4921
|
+
});
|
|
4922
|
+
return;
|
|
4923
|
+
}
|
|
4924
|
+
if (session.offline || this.session !== session) return;
|
|
4925
|
+
session.retry = setTimeout(() => {
|
|
4926
|
+
session.retry = void 0;
|
|
4927
|
+
if (this.session === session && !session.offline) this.connect(session);
|
|
4928
|
+
}, reconnectDelay(++session.retries));
|
|
4929
|
+
}
|
|
4930
|
+
async connect(session) {
|
|
4931
|
+
if (this.session !== session || session.offline || session.connection) return;
|
|
4932
|
+
const connection = {
|
|
4933
|
+
abort: new AbortController(),
|
|
4934
|
+
socketRefs: [],
|
|
4935
|
+
channelRefs: [],
|
|
4936
|
+
pending: /* @__PURE__ */ new Set()
|
|
4937
|
+
};
|
|
4938
|
+
session.connection = connection;
|
|
4939
|
+
connection.timer = setTimeout(() => this.fail(session, connection, "CONNECTION_TIMEOUT"), TIMEOUT_MS);
|
|
4940
|
+
try {
|
|
4941
|
+
var _this$core$ɵruntimeE;
|
|
4942
|
+
const runtimeUrl = this.core.runtimeUrl;
|
|
4943
|
+
if (!runtimeUrl) {
|
|
4944
|
+
this.fail(session, connection, "RUNTIME_REQUIRED", false);
|
|
4945
|
+
return;
|
|
4946
|
+
}
|
|
4947
|
+
let transport = this.core.runtimeTransport;
|
|
4948
|
+
if (transport === "auto") {
|
|
4949
|
+
transport = await this.detectedTransport(connection.abort.signal);
|
|
4950
|
+
if (!this.current(session, connection)) return;
|
|
4951
|
+
}
|
|
4952
|
+
const rest = transport === "rest";
|
|
4953
|
+
const response = await fetch(rest ? `${runtimeUrl}/trajectory/${encodeURIComponent(session.trajectoryId)}/connect` : (_this$core$ɵruntimeE = this.core.ɵruntimeEndpointUrl) !== null && _this$core$ɵruntimeE !== void 0 ? _this$core$ɵruntimeE : runtimeUrl, {
|
|
4954
|
+
method: "POST",
|
|
4955
|
+
redirect: "error",
|
|
4956
|
+
signal: connection.abort.signal,
|
|
4957
|
+
headers: {
|
|
4958
|
+
...this.core.headers,
|
|
4959
|
+
"Content-Type": "application/json"
|
|
4960
|
+
},
|
|
4961
|
+
credentials: this.core.credentials,
|
|
4962
|
+
body: JSON.stringify(rest ? {} : {
|
|
4963
|
+
method: "trajectory/connect",
|
|
4964
|
+
params: { trajectoryId: session.trajectoryId },
|
|
4965
|
+
body: {}
|
|
4966
|
+
})
|
|
4967
|
+
});
|
|
4968
|
+
if (!this.current(session, connection)) return;
|
|
4969
|
+
const payload = await response.json().catch(() => void 0);
|
|
4970
|
+
if (!this.current(session, connection)) return;
|
|
4971
|
+
if (!response.ok) {
|
|
4972
|
+
this.fail(session, connection, errorCode(payload, "CONNECTION_FAILED"), response.status >= 500 || response.status === 408 || response.status === 429);
|
|
4973
|
+
return;
|
|
4974
|
+
}
|
|
4975
|
+
if (!isGrant(payload)) {
|
|
4976
|
+
this.fail(session, connection, "INVALID_GRANT", false);
|
|
4977
|
+
return;
|
|
4978
|
+
}
|
|
4979
|
+
const socket = new phoenix.Socket(payload.realtime.clientUrl, {
|
|
4980
|
+
params: { join_token: payload.joinToken },
|
|
4981
|
+
timeout: TIMEOUT_MS
|
|
4982
|
+
});
|
|
4983
|
+
connection.socket = socket;
|
|
4984
|
+
const lost = () => this.fail(session, connection, "CONNECTION_LOST");
|
|
4985
|
+
connection.socketRefs.push(socket.onError(lost), socket.onClose(lost));
|
|
4986
|
+
const channel = socket.channel(payload.realtime.topic, {});
|
|
4987
|
+
connection.channel = channel;
|
|
4988
|
+
connection.channelRefs.push(["phx_error", channel.onError(lost)], ["phx_close", channel.onClose(lost)]);
|
|
4989
|
+
socket.connect();
|
|
4990
|
+
if (!this.current(session, connection)) return;
|
|
4991
|
+
channel.join(TIMEOUT_MS).receive("ok", () => {
|
|
4992
|
+
if (!this.current(session, connection)) return;
|
|
4993
|
+
try {
|
|
4994
|
+
var _session$config$ignor;
|
|
4995
|
+
clearTimeout(connection.timer);
|
|
4996
|
+
session.ready = true;
|
|
4997
|
+
session.retries = 0;
|
|
4998
|
+
session.collector = (0, _copilotkit_learning.createTrajectoryCollector)({
|
|
4999
|
+
...session.config,
|
|
5000
|
+
ignoreUrls: [
|
|
5001
|
+
...(_session$config$ignor = session.config.ignoreUrls) !== null && _session$config$ignor !== void 0 ? _session$config$ignor : [],
|
|
5002
|
+
...this.core.runtimeUrl ? [this.core.runtimeUrl] : [],
|
|
5003
|
+
...this.core.ɵruntimeEndpointUrl ? [this.core.ɵruntimeEndpointUrl] : [],
|
|
5004
|
+
"https://telemetry.copilotkit.ai/",
|
|
5005
|
+
"https://cdn.copilotkit.ai/"
|
|
5006
|
+
],
|
|
5007
|
+
onError: (error) => {
|
|
5008
|
+
if (error.code === "CAPTURE_FAILED") this.fail(session, connection, error.code, false);
|
|
5009
|
+
else {
|
|
5010
|
+
var _session$config$onErr2, _session$config2;
|
|
5011
|
+
if (error.code === "INVALID_EVENT" || error.code === "EVENT_TOO_LARGE") this.addDropped(session, 1);
|
|
5012
|
+
(_session$config$onErr2 = (_session$config2 = session.config).onError) === null || _session$config$onErr2 === void 0 || _session$config$onErr2.call(_session$config2, error);
|
|
5013
|
+
}
|
|
5014
|
+
},
|
|
5015
|
+
send: (event) => this.send(session, connection, event)
|
|
5016
|
+
});
|
|
5017
|
+
session.collector.start();
|
|
5018
|
+
if (!this.current(session, connection)) return;
|
|
5019
|
+
if (session.dropped > 0) this.scheduleFlush(session, connection);
|
|
5020
|
+
session.started = true;
|
|
5021
|
+
session.resolve({
|
|
5022
|
+
status: "started",
|
|
5023
|
+
trajectoryId: session.trajectoryId
|
|
5024
|
+
});
|
|
5025
|
+
this.onChange();
|
|
5026
|
+
} catch (_unused2) {
|
|
5027
|
+
this.fail(session, connection, "CAPTURE_FAILED", false);
|
|
5028
|
+
}
|
|
5029
|
+
}).receive("error", (joinError) => this.fail(session, connection, errorCode(joinError, "JOIN_FAILED"))).receive("timeout", () => this.fail(session, connection, "CONNECTION_TIMEOUT"));
|
|
5030
|
+
} catch (_unused3) {
|
|
5031
|
+
this.fail(session, connection, "CONNECTION_FAILED");
|
|
5032
|
+
}
|
|
5033
|
+
}
|
|
5034
|
+
/** Resolves once auto-detection picks a transport, or with "auto" when the attempt is aborted. */
|
|
5035
|
+
detectedTransport(signal) {
|
|
5036
|
+
return new Promise((resolve) => {
|
|
5037
|
+
const finish = () => {
|
|
5038
|
+
subscription.unsubscribe();
|
|
5039
|
+
signal.removeEventListener("abort", finish);
|
|
5040
|
+
resolve(this.core.runtimeTransport);
|
|
5041
|
+
};
|
|
5042
|
+
const subscription = this.core.subscribe({ onRuntimeConnectionStatusChanged: () => {
|
|
5043
|
+
if (this.core.runtimeTransport !== "auto") finish();
|
|
5044
|
+
} });
|
|
5045
|
+
signal.addEventListener("abort", finish, { once: true });
|
|
5046
|
+
});
|
|
5047
|
+
}
|
|
5048
|
+
addDropped(session, count, schedule = true) {
|
|
5049
|
+
session.dropped = Math.min(Number.MAX_SAFE_INTEGER, session.dropped + count);
|
|
5050
|
+
if (schedule && count > 0 && session.ready && session.connection) this.scheduleFlush(session, session.connection);
|
|
5051
|
+
}
|
|
5052
|
+
scheduleFlush(session, connection) {
|
|
5053
|
+
if (session.flushTimer !== void 0 || !this.current(session, connection)) return;
|
|
5054
|
+
session.flushTimer = setTimeout(() => {
|
|
5055
|
+
session.flushTimer = void 0;
|
|
5056
|
+
this.flush(session, connection);
|
|
5057
|
+
}, BATCH_INTERVAL_MS);
|
|
5058
|
+
}
|
|
5059
|
+
discardQueue(session) {
|
|
5060
|
+
clearTimeout(session.flushTimer);
|
|
5061
|
+
session.flushTimer = void 0;
|
|
5062
|
+
this.addDropped(session, session.queue.length, false);
|
|
5063
|
+
session.queue = [];
|
|
5064
|
+
}
|
|
5065
|
+
connected(connection) {
|
|
5066
|
+
var _connection$socket, _connection$channel;
|
|
5067
|
+
return ((_connection$socket = connection.socket) === null || _connection$socket === void 0 ? void 0 : _connection$socket.isConnected()) === true && ((_connection$channel = connection.channel) === null || _connection$channel === void 0 ? void 0 : _connection$channel.state) === "joined";
|
|
5068
|
+
}
|
|
5069
|
+
send(session, connection, event) {
|
|
5070
|
+
if (!this.current(session, connection) || !session.ready) return;
|
|
5071
|
+
if (!this.connected(connection)) {
|
|
5072
|
+
this.addDropped(session, 1);
|
|
5073
|
+
this.fail(session, connection, "CONNECTION_LOST");
|
|
5074
|
+
return;
|
|
5075
|
+
}
|
|
5076
|
+
if (!Number.isSafeInteger(this.nextSeq)) {
|
|
5077
|
+
this.addDropped(session, 1);
|
|
5078
|
+
this.report(session, "SEQUENCE_EXHAUSTED");
|
|
5079
|
+
return;
|
|
5080
|
+
}
|
|
5081
|
+
const original = event.value;
|
|
5082
|
+
const fields = isObject(original) && !Array.isArray(original) && !Object.hasOwn(original, "seq") ? original : { data: original };
|
|
5083
|
+
const wire = {
|
|
5084
|
+
...event,
|
|
5085
|
+
value: {
|
|
5086
|
+
...fields,
|
|
5087
|
+
seq: this.nextSeq++
|
|
5088
|
+
}
|
|
5089
|
+
};
|
|
5090
|
+
if (byteLength(wire) > MAX_EVENT_BYTES) {
|
|
5091
|
+
this.addDropped(session, 1);
|
|
5092
|
+
this.report(session, "EVENT_TOO_LARGE");
|
|
5093
|
+
return;
|
|
5094
|
+
}
|
|
5095
|
+
const fits = () => session.queue.length < MAX_BATCH_EVENTS && byteLength({
|
|
5096
|
+
events: [...session.queue, wire],
|
|
5097
|
+
dropped: Number.MAX_SAFE_INTEGER
|
|
5098
|
+
}) <= MAX_BATCH_BYTES;
|
|
5099
|
+
if (!fits()) this.flush(session, connection);
|
|
5100
|
+
if (!this.current(session, connection) || !session.ready) return;
|
|
5101
|
+
if (!fits()) {
|
|
5102
|
+
this.addDropped(session, 1);
|
|
5103
|
+
this.report(session, "EVENTS_DROPPED");
|
|
5104
|
+
return;
|
|
5105
|
+
}
|
|
5106
|
+
session.queue.push(wire);
|
|
5107
|
+
if (session.queue.length === MAX_BATCH_EVENTS) this.flush(session, connection);
|
|
5108
|
+
else this.scheduleFlush(session, connection);
|
|
5109
|
+
}
|
|
5110
|
+
flush(session, connection) {
|
|
5111
|
+
if (!this.current(session, connection) || !session.ready || session.queue.length === 0 && session.dropped === 0) return;
|
|
5112
|
+
if (!this.connected(connection)) {
|
|
5113
|
+
this.fail(session, connection, "CONNECTION_LOST");
|
|
5114
|
+
return;
|
|
5115
|
+
}
|
|
5116
|
+
if (connection.pending.size > 0) return;
|
|
5117
|
+
clearTimeout(session.flushTimer);
|
|
5118
|
+
session.flushTimer = void 0;
|
|
5119
|
+
const batch = {
|
|
5120
|
+
events: session.queue,
|
|
5121
|
+
dropped: session.dropped
|
|
5122
|
+
};
|
|
5123
|
+
session.queue = [];
|
|
5124
|
+
session.dropped = 0;
|
|
5125
|
+
let finished = false;
|
|
5126
|
+
const pending = {
|
|
5127
|
+
timer: setTimeout(() => pending.finish("PERSISTENCE_UNKNOWN"), TIMEOUT_MS),
|
|
5128
|
+
finish: (code, knownRollback = false, flushNext = true) => {
|
|
5129
|
+
if (finished) return;
|
|
5130
|
+
finished = true;
|
|
5131
|
+
clearTimeout(pending.timer);
|
|
5132
|
+
connection.pending.delete(pending);
|
|
5133
|
+
if (knownRollback) this.addDropped(session, batch.events.length + batch.dropped, false);
|
|
5134
|
+
if (code && this.current(session, connection)) this.report(session, code);
|
|
5135
|
+
if (flushNext && this.current(session, connection) && session.ready && (session.queue.length > 0 || !knownRollback && session.dropped > 0)) this.flush(session, connection);
|
|
5136
|
+
}
|
|
5137
|
+
};
|
|
5138
|
+
connection.pending.add(pending);
|
|
5139
|
+
try {
|
|
5140
|
+
connection.channel.push("events", batch, TIMEOUT_MS).receive("ok", (payload) => {
|
|
5141
|
+
pending.finish(!isBatchAcknowledgement(payload, batch.events.length) ? "PERSISTENCE_UNKNOWN" : payload.rejected > 0 ? "EVENTS_REJECTED" : void 0);
|
|
5142
|
+
}).receive("error", (payload) => {
|
|
5143
|
+
if (!this.current(session, connection)) return;
|
|
5144
|
+
const reason = isObject(payload) ? payload.reason : void 0;
|
|
5145
|
+
if (reason === "unauthorized" || reason === "trajectory_mismatch") {
|
|
5146
|
+
pending.finish(void 0, true, false);
|
|
5147
|
+
this.fail(session, connection, reason.toUpperCase(), false);
|
|
5148
|
+
return;
|
|
5149
|
+
}
|
|
5150
|
+
const rollback = typeof reason === "string" && [
|
|
5151
|
+
"invalid_batch",
|
|
5152
|
+
"batch_too_large",
|
|
5153
|
+
"batch_too_many_events",
|
|
5154
|
+
"trajectory_over_share",
|
|
5155
|
+
"trajectory_outbox_full",
|
|
5156
|
+
"unsupported_event"
|
|
5157
|
+
].includes(reason);
|
|
5158
|
+
pending.finish(rollback ? reason.toUpperCase() : "PERSISTENCE_UNKNOWN", rollback);
|
|
5159
|
+
}).receive("timeout", () => pending.finish("PERSISTENCE_UNKNOWN"));
|
|
5160
|
+
} catch (_unused4) {
|
|
5161
|
+
pending.finish("PERSISTENCE_UNKNOWN");
|
|
5162
|
+
}
|
|
5163
|
+
}
|
|
5164
|
+
};
|
|
5165
|
+
|
|
5166
|
+
//#endregion
|
|
5167
|
+
//#region src/core/learning-bridge.ts
|
|
5168
|
+
const COPILOTKIT_OWN_URLS = ["https://telemetry.copilotkit.ai/", "https://cdn.copilotkit.ai/"];
|
|
5169
|
+
/**
|
|
5170
|
+
* Wires `@copilotkit/learning` into Core. Default capture uses Runtime and Phoenix;
|
|
5171
|
+
* explicit legacy sinks also use agent events and open-Thread attribution.
|
|
5172
|
+
*/
|
|
5173
|
+
var LearningBridge = class {
|
|
5174
|
+
constructor(core, config, onChange = () => {}) {
|
|
5175
|
+
this.core = core;
|
|
5176
|
+
this.config = config;
|
|
5177
|
+
_defineProperty(this, "collector", null);
|
|
5178
|
+
_defineProperty(this, "agents", /* @__PURE__ */ new Map());
|
|
5179
|
+
_defineProperty(this, "openThreads", /* @__PURE__ */ new Set());
|
|
5180
|
+
_defineProperty(this, "toolCalls", /* @__PURE__ */ new Map());
|
|
5181
|
+
_defineProperty(this, "executingTools", /* @__PURE__ */ new Map());
|
|
5182
|
+
_defineProperty(this, "lastRunIds", /* @__PURE__ */ new WeakMap());
|
|
5183
|
+
_defineProperty(this, "lastLinkedThreadId", null);
|
|
5184
|
+
_defineProperty(this, "coreSubscription", null);
|
|
5185
|
+
_defineProperty(this, "seq", 0);
|
|
5186
|
+
_defineProperty(this, "activeConfig", void 0);
|
|
5187
|
+
_defineProperty(this, "connection", void 0);
|
|
5188
|
+
this.connection = new TrajectoryConnection(core, onChange);
|
|
5189
|
+
this.setConfig(config);
|
|
5190
|
+
}
|
|
5191
|
+
setConfig(config) {
|
|
5192
|
+
this.config = config;
|
|
5193
|
+
if (config === void 0) this.stop();
|
|
5194
|
+
}
|
|
5195
|
+
get isConfigured() {
|
|
5196
|
+
return this.config !== void 0;
|
|
5197
|
+
}
|
|
5198
|
+
get isLegacyCaptureActive() {
|
|
5199
|
+
return this.collector !== null && this.collector.trajectoryId !== null;
|
|
5200
|
+
}
|
|
5201
|
+
startTracking() {
|
|
5202
|
+
if (this.coreSubscription !== null || !this.isLegacyCaptureActive) return;
|
|
5203
|
+
this.coreSubscription = this.core.subscribe({
|
|
5204
|
+
onAgentsChanged: ({ agents }) => {
|
|
5205
|
+
for (const agent of Object.values(agents)) this.track(agent);
|
|
5206
|
+
},
|
|
5207
|
+
onAgentRunStarted: ({ agent }) => this.track(agent),
|
|
5208
|
+
onToolExecutionStart: ({ toolCallId, agentId, toolName }) => {
|
|
5209
|
+
if (!this.isLegacyCaptureActive) return;
|
|
5210
|
+
this.executingTools.set(toolCallId, Date.now());
|
|
5211
|
+
this.emitToolCall("executing", toolCallId, agentId, toolName, {});
|
|
5212
|
+
},
|
|
5213
|
+
onToolExecutionEnd: ({ toolCallId, agentId, toolName, error }) => {
|
|
5214
|
+
if (!this.isLegacyCaptureActive) return;
|
|
5215
|
+
const startedAt = this.executingTools.get(toolCallId);
|
|
5216
|
+
this.executingTools.delete(toolCallId);
|
|
5217
|
+
this.emitToolCall("completed", toolCallId, agentId, toolName, {
|
|
5218
|
+
durationMs: startedAt === void 0 ? void 0 : Date.now() - startedAt,
|
|
5219
|
+
outcome: error === void 0 ? "ok" : "error"
|
|
5220
|
+
});
|
|
5221
|
+
this.toolCalls.delete(toolCallId);
|
|
5222
|
+
}
|
|
5223
|
+
});
|
|
5224
|
+
for (const agent of Object.values(this.core.agents)) this.track(agent);
|
|
5225
|
+
}
|
|
5226
|
+
get trajectoryId() {
|
|
5227
|
+
var _ref, _this$connection$traj, _this$collector;
|
|
5228
|
+
return (_ref = (_this$connection$traj = this.connection.trajectoryId) !== null && _this$connection$traj !== void 0 ? _this$connection$traj : (_this$collector = this.collector) === null || _this$collector === void 0 ? void 0 : _this$collector.trajectoryId) !== null && _ref !== void 0 ? _ref : null;
|
|
5229
|
+
}
|
|
5230
|
+
start(options = {}) {
|
|
5231
|
+
var _ref2, _options$trajectoryId, _this$collector2;
|
|
5232
|
+
const config = this.config;
|
|
5233
|
+
if (config === void 0) {
|
|
5234
|
+
console.warn("[CopilotKit] startTrajectory() needs the `learning` option on CopilotKitCore (or the CopilotKitProvider `learning` prop).");
|
|
5235
|
+
return Promise.resolve({
|
|
5236
|
+
status: "error",
|
|
5237
|
+
code: "LEARNING_DISABLED"
|
|
5238
|
+
});
|
|
5239
|
+
}
|
|
5240
|
+
if (config.sink === void 0) {
|
|
5241
|
+
if (options.learningContainerIds !== void 0) console.warn("[CopilotKit] learningContainerIds is supported only with a custom sink. Authenticated Trajectory capture does not assign Learning Containers; remove learningContainerIds from the capture options.");
|
|
5242
|
+
this.stopLegacy();
|
|
5243
|
+
return this.connection.start(options.trajectoryId, config);
|
|
5244
|
+
}
|
|
5245
|
+
this.connection.stop();
|
|
5246
|
+
const trajectoryId = (_ref2 = (_options$trajectoryId = options.trajectoryId) !== null && _options$trajectoryId !== void 0 ? _options$trajectoryId : (_this$collector2 = this.collector) === null || _this$collector2 === void 0 ? void 0 : _this$collector2.trajectoryId) !== null && _ref2 !== void 0 ? _ref2 : crypto.randomUUID();
|
|
5247
|
+
if (this.collector === null) {
|
|
5248
|
+
var _config$ignoreUrls;
|
|
5249
|
+
this.activeConfig = config;
|
|
5250
|
+
const runtimeUrl = this.core.runtimeUrl;
|
|
5251
|
+
this.collector = (0, _copilotkit_learning.createCollector)({
|
|
5252
|
+
...config,
|
|
5253
|
+
beforeSend: (event) => {
|
|
5254
|
+
const sequenced = {
|
|
5255
|
+
...event,
|
|
5256
|
+
value: {
|
|
5257
|
+
...event.value,
|
|
5258
|
+
seq: ++this.seq
|
|
5259
|
+
}
|
|
5260
|
+
};
|
|
5261
|
+
return config.beforeSend === void 0 ? sequenced : config.beforeSend(sequenced);
|
|
5262
|
+
},
|
|
5263
|
+
ignoreUrls: [
|
|
5264
|
+
...(_config$ignoreUrls = config.ignoreUrls) !== null && _config$ignoreUrls !== void 0 ? _config$ignoreUrls : [],
|
|
5265
|
+
...COPILOTKIT_OWN_URLS,
|
|
5266
|
+
...runtimeUrl === void 0 ? [] : [runtimeUrl]
|
|
5267
|
+
],
|
|
5268
|
+
enrich: (target) => this.enrich(target)
|
|
5269
|
+
});
|
|
5270
|
+
}
|
|
5271
|
+
const wasActive = this.collector.trajectoryId !== null;
|
|
5272
|
+
this.collector.start({
|
|
5273
|
+
...options,
|
|
5274
|
+
trajectoryId
|
|
5275
|
+
});
|
|
5276
|
+
if (wasActive) return Promise.resolve(this.collector.trajectoryId === trajectoryId ? {
|
|
5277
|
+
status: "started",
|
|
5278
|
+
trajectoryId
|
|
5279
|
+
} : {
|
|
5280
|
+
status: "error",
|
|
5281
|
+
code: "TRAJECTORY_ACTIVE"
|
|
5282
|
+
});
|
|
5283
|
+
this.startTracking();
|
|
5284
|
+
this.lastLinkedThreadId = null;
|
|
5285
|
+
for (const threadId of this.distinctOpenThreadIds()) this.emitThreadLinked(threadId, "start");
|
|
5286
|
+
return Promise.resolve({
|
|
5287
|
+
status: "started",
|
|
5288
|
+
trajectoryId
|
|
5289
|
+
});
|
|
5290
|
+
}
|
|
5291
|
+
stop() {
|
|
5292
|
+
this.connection.stop();
|
|
5293
|
+
this.stopLegacy();
|
|
5294
|
+
}
|
|
5295
|
+
stopLegacy() {
|
|
5296
|
+
var _this$collector3, _this$coreSubscriptio;
|
|
5297
|
+
(_this$collector3 = this.collector) === null || _this$collector3 === void 0 || _this$collector3.stop();
|
|
5298
|
+
this.collector = null;
|
|
5299
|
+
this.activeConfig = void 0;
|
|
5300
|
+
(_this$coreSubscriptio = this.coreSubscription) === null || _this$coreSubscriptio === void 0 || _this$coreSubscriptio.unsubscribe();
|
|
5301
|
+
this.coreSubscription = null;
|
|
5302
|
+
for (const subscription of this.agents.values()) subscription.unsubscribe();
|
|
5303
|
+
this.agents.clear();
|
|
5304
|
+
this.toolCalls.clear();
|
|
5305
|
+
this.executingTools.clear();
|
|
5306
|
+
}
|
|
5307
|
+
/** A developer event. Only an explicit legacy sink adds open-Thread context. */
|
|
5308
|
+
emit(name, value) {
|
|
5309
|
+
this.connection.emit(name, value);
|
|
5310
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
5311
|
+
var _this$collector4;
|
|
5312
|
+
(_this$collector4 = this.collector) === null || _this$collector4 === void 0 || _this$collector4.emit(name, {
|
|
5313
|
+
...this.openThreadContext(),
|
|
5314
|
+
...value
|
|
5315
|
+
});
|
|
5316
|
+
}
|
|
5317
|
+
}
|
|
5318
|
+
registerOpenThread(params) {
|
|
5319
|
+
const entry = { ...params };
|
|
5320
|
+
const isNew = !this.distinctOpenThreadIds().includes(entry.threadId);
|
|
5321
|
+
this.openThreads.add(entry);
|
|
5322
|
+
if (isNew) this.emitThreadLinked(entry.threadId, "open");
|
|
5323
|
+
return {
|
|
5324
|
+
update: (threadId) => {
|
|
5325
|
+
if (threadId === entry.threadId) return;
|
|
5326
|
+
entry.threadId = threadId;
|
|
5327
|
+
if (![...this.openThreads].filter((other) => other !== entry).some((other) => other.threadId === threadId)) this.emitThreadLinked(threadId, "switch");
|
|
5328
|
+
},
|
|
5329
|
+
unregister: () => {
|
|
5330
|
+
this.openThreads.delete(entry);
|
|
5331
|
+
}
|
|
5332
|
+
};
|
|
5333
|
+
}
|
|
5334
|
+
distinctOpenThreadIds() {
|
|
5335
|
+
return [...new Set([...this.openThreads].map((open) => open.threadId))];
|
|
5336
|
+
}
|
|
5337
|
+
emitThreadLinked(threadId, reason) {
|
|
5338
|
+
var _this$collector5, _ref3, _owner$agentId, _agent$messages$lengt;
|
|
5339
|
+
if (!(this.collector !== null && this.collector.trajectoryId !== null) || threadId === this.lastLinkedThreadId) return;
|
|
5340
|
+
this.lastLinkedThreadId = threadId;
|
|
5341
|
+
const owner = [...this.openThreads].find((open) => open.threadId === threadId);
|
|
5342
|
+
const agent = this.findAgentForThread(threadId);
|
|
5343
|
+
(_this$collector5 = this.collector) === null || _this$collector5 === void 0 || _this$collector5.ɵemit("thread.linked", {
|
|
5344
|
+
threadId,
|
|
5345
|
+
agentId: (_ref3 = (_owner$agentId = owner === null || owner === void 0 ? void 0 : owner.agentId) !== null && _owner$agentId !== void 0 ? _owner$agentId : agent === null || agent === void 0 ? void 0 : agent.agentId) !== null && _ref3 !== void 0 ? _ref3 : null,
|
|
5346
|
+
hasMessages: ((_agent$messages$lengt = agent === null || agent === void 0 ? void 0 : agent.messages.length) !== null && _agent$messages$lengt !== void 0 ? _agent$messages$lengt : 0) > 0,
|
|
5347
|
+
reason
|
|
5348
|
+
});
|
|
5349
|
+
}
|
|
5350
|
+
findAgentForThread(threadId) {
|
|
5351
|
+
return [...this.agents.keys()].find((agent) => agent.threadId === threadId);
|
|
5352
|
+
}
|
|
5353
|
+
track(agent) {
|
|
5354
|
+
const agentId = agent.agentId;
|
|
5355
|
+
if (!this.isLegacyCaptureActive || this.agents.has(agent) || agentId === void 0) return;
|
|
5356
|
+
this.agents.set(agent, agent.subscribe(this.agentSubscriber(agent, agentId)));
|
|
5357
|
+
}
|
|
5358
|
+
agentSubscriber(agent, agentId) {
|
|
5359
|
+
const runIdFor = () => this.lastRunIds.get(agent);
|
|
5360
|
+
return {
|
|
5361
|
+
onRunStartedEvent: ({ event }) => {
|
|
5362
|
+
this.lastRunIds.set(agent, event.runId);
|
|
5363
|
+
this.emitRun("started", agent, event.threadId, event.runId);
|
|
5364
|
+
},
|
|
5365
|
+
onRunFinishedEvent: ({ event }) => {
|
|
5366
|
+
this.emitRun("finished", agent, event.threadId, event.runId);
|
|
5367
|
+
},
|
|
5368
|
+
onRunErrorEvent: () => {
|
|
5369
|
+
var _runIdFor;
|
|
5370
|
+
this.emitRun("error", agent, agent.threadId, (_runIdFor = runIdFor()) !== null && _runIdFor !== void 0 ? _runIdFor : null);
|
|
5371
|
+
},
|
|
5372
|
+
onToolCallStartEvent: ({ event }) => {
|
|
5373
|
+
if (!this.isLegacyCaptureActive) return;
|
|
5374
|
+
const origin = {
|
|
5375
|
+
agentId,
|
|
5376
|
+
threadId: agent.threadId,
|
|
5377
|
+
messageId: event.parentMessageId,
|
|
5378
|
+
runId: runIdFor()
|
|
5379
|
+
};
|
|
5380
|
+
this.toolCalls.set(event.toolCallId, origin);
|
|
5381
|
+
this.emitToolCall("started", event.toolCallId, origin.agentId, event.toolCallName, {});
|
|
5382
|
+
},
|
|
5383
|
+
onToolCallResultEvent: ({ event }) => {
|
|
5384
|
+
if (!this.executingTools.has(event.toolCallId)) this.toolCalls.delete(event.toolCallId);
|
|
5385
|
+
},
|
|
5386
|
+
onNewMessage: ({ message }) => this.emitMessage(agent, message)
|
|
5387
|
+
};
|
|
5388
|
+
}
|
|
5389
|
+
emitRun(phase, agent, threadId, runId) {
|
|
5390
|
+
var _this$collector6;
|
|
5391
|
+
(_this$collector6 = this.collector) === null || _this$collector6 === void 0 || _this$collector6.ɵemit("agent.run", {
|
|
5392
|
+
phase,
|
|
5393
|
+
runId,
|
|
5394
|
+
threadId,
|
|
5395
|
+
agentId: agent.agentId
|
|
5396
|
+
});
|
|
5397
|
+
}
|
|
5398
|
+
emitMessage(agent, message) {
|
|
5399
|
+
var _this$activeConfig, _this$collector7;
|
|
5400
|
+
const content = typeof message.content === "string" ? message.content : "";
|
|
5401
|
+
const includeText = ((_this$activeConfig = this.activeConfig) === null || _this$activeConfig === void 0 || (_this$activeConfig = _this$activeConfig.capture) === null || _this$activeConfig === void 0 ? void 0 : _this$activeConfig.agentText) !== false && typeof message.content === "string";
|
|
5402
|
+
(_this$collector7 = this.collector) === null || _this$collector7 === void 0 || _this$collector7.ɵemit("agent.message", {
|
|
5403
|
+
messageId: message.id,
|
|
5404
|
+
runId: this.runIdForMessage(agent, message.id),
|
|
5405
|
+
threadId: agent.threadId,
|
|
5406
|
+
agentId: agent.agentId,
|
|
5407
|
+
role: message.role,
|
|
5408
|
+
textLength: content.length,
|
|
5409
|
+
...includeText ? { text: content } : {}
|
|
5410
|
+
});
|
|
5411
|
+
}
|
|
5412
|
+
emitToolCall(phase, toolCallId, agentId, toolName, extra) {
|
|
5413
|
+
var _this$collector8, _origin$threadId;
|
|
5414
|
+
const origin = this.toolCalls.get(toolCallId);
|
|
5415
|
+
(_this$collector8 = this.collector) === null || _this$collector8 === void 0 || _this$collector8.ɵemit("tool.call", {
|
|
5416
|
+
phase,
|
|
5417
|
+
toolCallId,
|
|
5418
|
+
toolName,
|
|
5419
|
+
agentId,
|
|
5420
|
+
threadId: (_origin$threadId = origin === null || origin === void 0 ? void 0 : origin.threadId) !== null && _origin$threadId !== void 0 ? _origin$threadId : null,
|
|
5421
|
+
messageId: origin === null || origin === void 0 ? void 0 : origin.messageId,
|
|
5422
|
+
runId: origin === null || origin === void 0 ? void 0 : origin.runId,
|
|
5423
|
+
...extra
|
|
5424
|
+
});
|
|
5425
|
+
}
|
|
5426
|
+
runIdForMessage(agent, messageId) {
|
|
5427
|
+
var _ref4, _this$core$getRunIdFo;
|
|
5428
|
+
const agentId = agent.agentId;
|
|
5429
|
+
if (agentId === void 0) return null;
|
|
5430
|
+
return (_ref4 = (_this$core$getRunIdFo = this.core.getRunIdForMessage(agentId, agent.threadId, messageId)) !== null && _this$core$getRunIdFo !== void 0 ? _this$core$getRunIdFo : this.lastRunIds.get(agent)) !== null && _ref4 !== void 0 ? _ref4 : null;
|
|
5431
|
+
}
|
|
5432
|
+
findMessage(messageId) {
|
|
5433
|
+
for (const agent of this.agents.keys()) {
|
|
5434
|
+
const message = agent.messages.find((candidate) => candidate.id === messageId);
|
|
5435
|
+
if (message !== void 0) return {
|
|
5436
|
+
agent,
|
|
5437
|
+
message
|
|
5438
|
+
};
|
|
5439
|
+
}
|
|
5440
|
+
return null;
|
|
5441
|
+
}
|
|
5442
|
+
toolStatus(agent, toolCallId) {
|
|
5443
|
+
if (this.executingTools.has(toolCallId)) return "executing";
|
|
5444
|
+
return agent.messages.some((message) => message.role === "tool" && message.toolCallId === toolCallId) ? "complete" : "inProgress";
|
|
5445
|
+
}
|
|
5446
|
+
/** Adds agent context to a click: exact message first, then the single open Thread. */
|
|
5447
|
+
enrich(target) {
|
|
5448
|
+
var _target$closest$getAt, _target$closest, _target$closest$getAt2, _target$closest2;
|
|
5449
|
+
const messageId = (_target$closest$getAt = (_target$closest = target.closest("[data-message-id]")) === null || _target$closest === void 0 ? void 0 : _target$closest.getAttribute("data-message-id")) !== null && _target$closest$getAt !== void 0 ? _target$closest$getAt : null;
|
|
5450
|
+
const toolCallId = (_target$closest$getAt2 = (_target$closest2 = target.closest("[data-tool-call-id]")) === null || _target$closest2 === void 0 ? void 0 : _target$closest2.getAttribute("data-tool-call-id")) !== null && _target$closest$getAt2 !== void 0 ? _target$closest$getAt2 : null;
|
|
5451
|
+
const found = messageId === null ? null : this.findMessage(messageId);
|
|
5452
|
+
if (found !== null) {
|
|
5453
|
+
var _message$toolCalls;
|
|
5454
|
+
const { agent, message } = found;
|
|
5455
|
+
const toolCall = toolCallId !== null && message.role === "assistant" ? (_message$toolCalls = message.toolCalls) === null || _message$toolCalls === void 0 ? void 0 : _message$toolCalls.find((call) => call.id === toolCallId) : void 0;
|
|
5456
|
+
return {
|
|
5457
|
+
threadId: agent.threadId,
|
|
5458
|
+
agentId: agent.agentId,
|
|
5459
|
+
messageId: message.id,
|
|
5460
|
+
runId: this.runIdForMessage(agent, message.id),
|
|
5461
|
+
...toolCall === void 0 ? {} : {
|
|
5462
|
+
toolCallId: toolCall.id,
|
|
5463
|
+
toolName: toolCall.function.name,
|
|
5464
|
+
toolStatus: this.toolStatus(agent, toolCall.id)
|
|
5465
|
+
}
|
|
5466
|
+
};
|
|
5467
|
+
}
|
|
5468
|
+
return this.openThreadContext();
|
|
5469
|
+
}
|
|
5470
|
+
/** The single open Thread, or `null` with the candidates when several are open. */
|
|
5471
|
+
openThreadContext() {
|
|
5472
|
+
var _openIds$;
|
|
5473
|
+
const openIds = this.distinctOpenThreadIds();
|
|
5474
|
+
if (openIds.length > 1) return {
|
|
5475
|
+
threadId: null,
|
|
5476
|
+
threadAmbiguity: {
|
|
5477
|
+
reason: "multiple-open",
|
|
5478
|
+
candidates: openIds
|
|
5479
|
+
}
|
|
5480
|
+
};
|
|
5481
|
+
return { threadId: (_openIds$ = openIds[0]) !== null && _openIds$ !== void 0 ? _openIds$ : null };
|
|
5482
|
+
}
|
|
5483
|
+
};
|
|
5484
|
+
|
|
4514
5485
|
//#endregion
|
|
4515
5486
|
//#region src/utils/micro-redux.ts
|
|
4516
5487
|
const INTERNAL_ACTION_TYPES = {
|
|
@@ -5655,7 +6626,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5655
6626
|
return Object.fromEntries(Object.entries(headers).filter((entry) => entry[1] != null));
|
|
5656
6627
|
}
|
|
5657
6628
|
var CopilotKitCore = class {
|
|
5658
|
-
constructor({ runtimeUrl, runtimeTransport = "auto", deferInitialConnection = false, headers = {}, credentials, messageFilter, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug }) {
|
|
6629
|
+
constructor({ runtimeUrl, runtimeTransport = "auto", deferInitialConnection = false, headers = {}, credentials, messageFilter, properties = {}, agents__unsafe_dev_only = {}, tools = [], suggestionsConfig = [], debug, learning }) {
|
|
5659
6630
|
_defineProperty(this, "_headers", void 0);
|
|
5660
6631
|
_defineProperty(this, "_credentials", void 0);
|
|
5661
6632
|
_defineProperty(this, "_messageFilter", void 0);
|
|
@@ -5669,6 +6640,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5669
6640
|
_defineProperty(this, "runHandler", void 0);
|
|
5670
6641
|
_defineProperty(this, "stateManager", void 0);
|
|
5671
6642
|
_defineProperty(this, "threadStoreRegistry", void 0);
|
|
6643
|
+
_defineProperty(this, "learningBridge", void 0);
|
|
6644
|
+
_defineProperty(this, "notifiedTrajectoryId", null);
|
|
6645
|
+
_defineProperty(this, "learningConfiguredListeners", /* @__PURE__ */ new Set());
|
|
5672
6646
|
_defineProperty(this, "_memoryStore", void 0);
|
|
5673
6647
|
_defineProperty(this, "previousAgentIds", /* @__PURE__ */ new Set());
|
|
5674
6648
|
this._headers = normalizeHeaders(headers);
|
|
@@ -5686,6 +6660,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5686
6660
|
this.runHandler.initialize(tools);
|
|
5687
6661
|
this.suggestionEngine.initialize(suggestionsConfig);
|
|
5688
6662
|
this.stateManager.initialize();
|
|
6663
|
+
this.learningBridge = new LearningBridge(this, learning, () => this.notifyTrajectoryChanged());
|
|
5689
6664
|
this.agentRegistry.setRuntimeTransport(runtimeTransport);
|
|
5690
6665
|
this.agentRegistry.setRuntimeUrl(runtimeUrl, { deferConnection: deferInitialConnection });
|
|
5691
6666
|
this.previousAgentIds = new Set(Object.keys(agents__unsafe_dev_only));
|
|
@@ -5840,6 +6815,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
5840
6815
|
get runtimeConnectionStatus() {
|
|
5841
6816
|
return this.agentRegistry.runtimeConnectionStatus;
|
|
5842
6817
|
}
|
|
6818
|
+
/** @internal The verbatim single-endpoint URL, including any trailing slash. */
|
|
6819
|
+
get ɵruntimeEndpointUrl() {
|
|
6820
|
+
return this.agentRegistry.runtimeEndpointUrl;
|
|
6821
|
+
}
|
|
5843
6822
|
get ɵruntimeFetch() {
|
|
5844
6823
|
return this.agentRegistry.createRuntimeFetch();
|
|
5845
6824
|
}
|
|
@@ -6005,6 +6984,86 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
6005
6984
|
getAgent(id) {
|
|
6006
6985
|
return this.agentRegistry.getAgent(id);
|
|
6007
6986
|
}
|
|
6987
|
+
/** Updates settings for the next capture. Removing the config stops capture. */
|
|
6988
|
+
setLearningConfig(config) {
|
|
6989
|
+
const wasConfigured = this.ɵlearningConfigured;
|
|
6990
|
+
this.learningBridge.setConfig(config);
|
|
6991
|
+
this.notifyTrajectoryChanged();
|
|
6992
|
+
if (this.ɵlearningConfigured === wasConfigured) return;
|
|
6993
|
+
for (const listener of this.learningConfiguredListeners) try {
|
|
6994
|
+
listener();
|
|
6995
|
+
} catch (error) {
|
|
6996
|
+
console.error("Learning configured listener error:", error);
|
|
6997
|
+
}
|
|
6998
|
+
}
|
|
6999
|
+
/**
|
|
7000
|
+
* @internal Whether a `learning` config is set. Unlike {@link trajectoryId},
|
|
7001
|
+
* it does not change with capture start, connection failures or reconnects.
|
|
7002
|
+
*/
|
|
7003
|
+
get ɵlearningConfigured() {
|
|
7004
|
+
return this.learningBridge.isConfigured;
|
|
7005
|
+
}
|
|
7006
|
+
/** @internal Calls `listener` when {@link ɵlearningConfigured} changes. */
|
|
7007
|
+
ɵsubscribeToLearningConfigured(listener) {
|
|
7008
|
+
this.learningConfiguredListeners.add(listener);
|
|
7009
|
+
return () => {
|
|
7010
|
+
this.learningConfiguredListeners.delete(listener);
|
|
7011
|
+
};
|
|
7012
|
+
}
|
|
7013
|
+
/** The active capture's Trajectory ID, or null while capture is stopped. */
|
|
7014
|
+
get trajectoryId() {
|
|
7015
|
+
return this.learningBridge.trajectoryId;
|
|
7016
|
+
}
|
|
7017
|
+
notifyTrajectoryChanged() {
|
|
7018
|
+
const trajectoryId = this.trajectoryId;
|
|
7019
|
+
if (trajectoryId === this.notifiedTrajectoryId) return;
|
|
7020
|
+
this.notifiedTrajectoryId = trajectoryId;
|
|
7021
|
+
this.notifySubscribers((subscriber) => {
|
|
7022
|
+
var _subscriber$onTraject;
|
|
7023
|
+
return (_subscriber$onTraject = subscriber.onTrajectoryChanged) === null || _subscriber$onTraject === void 0 ? void 0 : _subscriber$onTraject.call(subscriber, {
|
|
7024
|
+
copilotkit: this,
|
|
7025
|
+
trajectoryId
|
|
7026
|
+
});
|
|
7027
|
+
}, "Subscriber onTrajectoryChanged error:");
|
|
7028
|
+
}
|
|
7029
|
+
/**
|
|
7030
|
+
* Starts one Trajectory after Runtime authorization and the Gateway join.
|
|
7031
|
+
* Omit the ID to create one. Repeated starts await the active connection;
|
|
7032
|
+
* a different ID stops the previous capture. Explicit sinks retain the
|
|
7033
|
+
* prototype's synchronous capture and require a stop before changing IDs.
|
|
7034
|
+
*
|
|
7035
|
+
* @example copilotkit.startTrajectory({ trajectoryId: crypto.randomUUID() })
|
|
7036
|
+
*/
|
|
7037
|
+
startTrajectory(options = {}) {
|
|
7038
|
+
const result = this.learningBridge.start(options);
|
|
7039
|
+
this.notifyTrajectoryChanged();
|
|
7040
|
+
return result;
|
|
7041
|
+
}
|
|
7042
|
+
/** Cancels pending work and stops capture, attempting one final batch without waiting for its ACK. */
|
|
7043
|
+
stopTrajectory() {
|
|
7044
|
+
this.learningBridge.stop();
|
|
7045
|
+
this.notifyTrajectoryChanged();
|
|
7046
|
+
}
|
|
7047
|
+
/**
|
|
7048
|
+
* Records an outcome that clicks cannot show, such as a saved report or an
|
|
7049
|
+
* approved deal. The wire adds `value.seq`; arrays, primitives, and objects
|
|
7050
|
+
* already containing `seq` are preserved under `value.data`. Explicit legacy
|
|
7051
|
+
* sinks retain object-only events with open-Thread enrichment.
|
|
7052
|
+
* No-op while no Trajectory runs. Events emitted during connection recovery
|
|
7053
|
+
* count as dropped. Built-in names such as `click` are rejected.
|
|
7054
|
+
*
|
|
7055
|
+
* @example copilotkit.emitTrajectoryEvent("deal.approved", { dealId: "deal-1" })
|
|
7056
|
+
*/
|
|
7057
|
+
emitTrajectoryEvent(name, value = {}) {
|
|
7058
|
+
this.learningBridge.emit(name, value);
|
|
7059
|
+
}
|
|
7060
|
+
/**
|
|
7061
|
+
* Tells Core that a view shows this Thread, so captured interactions can link to it.
|
|
7062
|
+
* Framework bindings call this; call it yourself only without a CopilotKit provider.
|
|
7063
|
+
*/
|
|
7064
|
+
registerOpenThread(params) {
|
|
7065
|
+
return this.learningBridge.registerOpenThread(params);
|
|
7066
|
+
}
|
|
6008
7067
|
/**
|
|
6009
7068
|
* Re-apply the current headers to a single agent (delegated to
|
|
6010
7069
|
* AgentRegistry). Core headers are merged on top of the agent's own
|
|
@@ -7621,6 +8680,7 @@ exports.ɵcreateMemoryStore = createMemoryStore;
|
|
|
7621
8680
|
exports.ɵcreateThreadSelectors = ɵcreateThreadSelectors;
|
|
7622
8681
|
exports.ɵcreateThreadStore = createThreadStore;
|
|
7623
8682
|
exports.ɵisCopilotKitFeature = ɵisCopilotKitFeature;
|
|
8683
|
+
exports.ɵisHttpAgent = ɵisHttpAgent;
|
|
7624
8684
|
exports.ɵisRetryableMemoryStatus = isRetryableStatus;
|
|
7625
8685
|
exports.ɵjoinPhoenixChannel$ = ɵjoinPhoenixChannel$;
|
|
7626
8686
|
exports.ɵmapMemoryMetadataEvent = ɵmapMemoryMetadataEvent;
|