@copilotkit/runtime 2.0.0-rc.61 → 2.0.0-rc.62
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/agent.cjs +1 -0
- package/dist/agent.cjs.map +1 -1
- package/dist/agent.d.cts +10 -0
- package/dist/agent.d.cts.map +1 -1
- package/dist/agent.d.mts +10 -0
- package/dist/agent.d.mts.map +1 -1
- package/dist/agent.mjs +1 -0
- package/dist/agent.mjs.map +1 -1
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +4 -3
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +3 -2
- package/dist/runtime/core/channel-manager.cjs +51 -162
- package/dist/runtime/core/channel-manager.cjs.map +1 -1
- package/dist/runtime/core/channel-manager.d.cts +1 -1
- package/dist/runtime/core/channel-manager.d.cts.map +1 -1
- package/dist/runtime/core/channel-manager.d.mts +1 -1
- package/dist/runtime/core/channel-manager.d.mts.map +1 -1
- package/dist/runtime/core/channel-manager.mjs +51 -162
- package/dist/runtime/core/channel-manager.mjs.map +1 -1
- package/dist/runtime/core/fetch-cors.cjs +4 -2
- package/dist/runtime/core/fetch-cors.cjs.map +1 -1
- package/dist/runtime/core/fetch-cors.mjs +4 -2
- package/dist/runtime/core/fetch-cors.mjs.map +1 -1
- package/dist/runtime/core/package-info.cjs +1 -1
- package/dist/runtime/core/package-info.mjs +1 -1
- package/dist/runtime/core/runtime.cjs +3 -1
- package/dist/runtime/core/runtime.cjs.map +1 -1
- package/dist/runtime/core/runtime.d.cts +36 -7
- package/dist/runtime/core/runtime.d.cts.map +1 -1
- package/dist/runtime/core/runtime.d.mts +36 -7
- package/dist/runtime/core/runtime.d.mts.map +1 -1
- package/dist/runtime/core/runtime.mjs +3 -1
- package/dist/runtime/core/runtime.mjs.map +1 -1
- package/dist/runtime/core/thread-run.cjs +459 -0
- package/dist/runtime/core/thread-run.cjs.map +1 -0
- package/dist/runtime/core/thread-run.d.cts +201 -0
- package/dist/runtime/core/thread-run.d.cts.map +1 -0
- package/dist/runtime/core/thread-run.d.mts +201 -0
- package/dist/runtime/core/thread-run.d.mts.map +1 -0
- package/dist/runtime/core/thread-run.mjs +456 -0
- package/dist/runtime/core/thread-run.mjs.map +1 -0
- package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs +6 -7
- package/dist/runtime/event-transforms/open-generative-ui-middleware.cjs.map +1 -1
- package/dist/runtime/event-transforms/open-generative-ui-middleware.d.cts.map +1 -1
- package/dist/runtime/event-transforms/open-generative-ui-middleware.d.mts.map +1 -1
- package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs +6 -7
- package/dist/runtime/event-transforms/open-generative-ui-middleware.mjs.map +1 -1
- package/dist/runtime/handlers/get-runtime-info.cjs +2 -1
- package/dist/runtime/handlers/get-runtime-info.cjs.map +1 -1
- package/dist/runtime/handlers/get-runtime-info.mjs +2 -1
- package/dist/runtime/handlers/get-runtime-info.mjs.map +1 -1
- package/dist/runtime/handlers/intelligence/run.cjs +0 -1
- package/dist/runtime/handlers/intelligence/run.cjs.map +1 -1
- package/dist/runtime/handlers/intelligence/run.mjs +0 -1
- package/dist/runtime/handlers/intelligence/run.mjs.map +1 -1
- package/dist/runtime/handlers/intelligence/thread-names.cjs +43 -70
- package/dist/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
- package/dist/runtime/handlers/intelligence/thread-names.mjs +43 -70
- package/dist/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
- package/dist/runtime/index.d.cts +3 -2
- package/dist/runtime/index.d.mts +3 -2
- package/dist/runtime/intelligence-platform/client.cjs +20 -2
- package/dist/runtime/intelligence-platform/client.cjs.map +1 -1
- package/dist/runtime/intelligence-platform/client.d.cts +13 -0
- package/dist/runtime/intelligence-platform/client.d.cts.map +1 -1
- package/dist/runtime/intelligence-platform/client.d.mts +13 -0
- package/dist/runtime/intelligence-platform/client.d.mts.map +1 -1
- package/dist/runtime/intelligence-platform/client.mjs +20 -2
- package/dist/runtime/intelligence-platform/client.mjs.map +1 -1
- package/dist/runtime/runner/index.d.cts +1 -1
- package/dist/runtime/runner/index.d.mts +1 -1
- package/dist/runtime/runner/index.mjs +1 -1
- package/dist/runtime/runner/intelligence.cjs +107 -23
- package/dist/runtime/runner/intelligence.cjs.map +1 -1
- package/dist/runtime/runner/intelligence.d.cts +51 -2
- package/dist/runtime/runner/intelligence.d.cts.map +1 -1
- package/dist/runtime/runner/intelligence.d.mts +51 -2
- package/dist/runtime/runner/intelligence.d.mts.map +1 -1
- package/dist/runtime/runner/intelligence.mjs +108 -25
- package/dist/runtime/runner/intelligence.mjs.map +1 -1
- package/dist/v2/index.d.cts +18 -0
- package/dist/v2/index.d.mts +18 -0
- package/package.json +10 -10
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { IntelligenceAgentRunner } from "../runner/intelligence.mjs";
|
|
2
|
+
import { startThreadLockHeartbeat } from "../intelligence-platform/thread-lock-heartbeat.mjs";
|
|
3
|
+
import { bytesToBase64 } from "./web-crypto.mjs";
|
|
4
|
+
import { EventType } from "@ag-ui/client";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime/core/thread-run.ts
|
|
7
|
+
/** Lock TTL used when the runtime passed in does not set one. */
|
|
8
|
+
const DEFAULT_LOCK_TTL_SECONDS = 20;
|
|
9
|
+
/** Lock renewal interval used when the runtime passed in does not set one. */
|
|
10
|
+
const DEFAULT_LOCK_HEARTBEAT_INTERVAL_SECONDS = 15;
|
|
11
|
+
/**
|
|
12
|
+
* Hold a Thread's lock around one run: acquire it, renew it while the run
|
|
13
|
+
* works, stop the run if the lock is lost or `signal` aborts, and release it
|
|
14
|
+
* on every path once the runner's stream has ended.
|
|
15
|
+
*
|
|
16
|
+
* Shared by managed Channel runs and {@link runAgentInThread}.
|
|
17
|
+
*
|
|
18
|
+
* @throws The lock request's error when the lock is refused, an error thrown
|
|
19
|
+
* by `start`, the runner's stream error, or a {@link ThreadRunError} with
|
|
20
|
+
* reason `"stop_timeout"`. Lock release is attempted whenever it was
|
|
21
|
+
* acquired. If the stop deadline expires, release continues in the
|
|
22
|
+
* background for a bounded time, with the lock's TTL as the backstop.
|
|
23
|
+
*/
|
|
24
|
+
async function runWithThreadLock(args) {
|
|
25
|
+
const { runner, intelligence, lock: settings } = args;
|
|
26
|
+
const requested = {
|
|
27
|
+
threadId: args.acquire.threadId,
|
|
28
|
+
runId: args.acquire.runId
|
|
29
|
+
};
|
|
30
|
+
if (args.signal?.aborted) return {
|
|
31
|
+
...requested,
|
|
32
|
+
stopped: true
|
|
33
|
+
};
|
|
34
|
+
const lockRequestedAt = Date.now();
|
|
35
|
+
const acquiring = intelligence.ɵacquireThreadLock({
|
|
36
|
+
...args.acquire,
|
|
37
|
+
ttlSeconds: settings.ttlSeconds,
|
|
38
|
+
...settings.keyPrefix !== void 0 ? { lockKeyPrefix: settings.keyPrefix } : {}
|
|
39
|
+
});
|
|
40
|
+
const lock = await untilAborted(acquiring, args.signal);
|
|
41
|
+
if (lock === ABORTED) {
|
|
42
|
+
acquiring.then((late) => releaseLock(intelligence, {
|
|
43
|
+
threadId: late.threadId || requested.threadId,
|
|
44
|
+
runId: late.runId || requested.runId
|
|
45
|
+
}, CLEANUP_TIMEOUT_MS), () => {});
|
|
46
|
+
return {
|
|
47
|
+
...requested,
|
|
48
|
+
stopped: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const threadId = lock.threadId || requested.threadId;
|
|
52
|
+
const runId = lock.runId || requested.runId;
|
|
53
|
+
let agent;
|
|
54
|
+
let stopPromise;
|
|
55
|
+
let lockLost;
|
|
56
|
+
let stoppedBySignal = false;
|
|
57
|
+
let stopTimer;
|
|
58
|
+
let stopDeadline;
|
|
59
|
+
let failStop;
|
|
60
|
+
const stopRun = () => {
|
|
61
|
+
if (args.stopTimeoutMs !== void 0 && stopDeadline === void 0) {
|
|
62
|
+
stopDeadline = Date.now() + args.stopTimeoutMs;
|
|
63
|
+
stopTimer = setTimeout(() => {
|
|
64
|
+
failStop?.(new ThreadRunError("stop_timeout", `The run did not end within ${args.stopTimeoutMs}ms of being stopped`, {
|
|
65
|
+
threadId,
|
|
66
|
+
runId
|
|
67
|
+
}));
|
|
68
|
+
}, args.stopTimeoutMs);
|
|
69
|
+
stopTimer.unref?.();
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
agent?.abortRun();
|
|
73
|
+
} catch {}
|
|
74
|
+
stopPromise ??= Promise.resolve().then(() => runner.stop({
|
|
75
|
+
threadId,
|
|
76
|
+
runId
|
|
77
|
+
})).catch(() => false);
|
|
78
|
+
};
|
|
79
|
+
const remainingStopMs = () => stopDeadline === void 0 ? void 0 : Math.max(0, stopDeadline - Date.now());
|
|
80
|
+
const stopBySignal = () => {
|
|
81
|
+
stoppedBySignal = true;
|
|
82
|
+
stopRun();
|
|
83
|
+
};
|
|
84
|
+
const heartbeat = startThreadLockHeartbeat({
|
|
85
|
+
renew: (signal) => intelligence.ɵrenewThreadLock({
|
|
86
|
+
threadId,
|
|
87
|
+
runId,
|
|
88
|
+
ttlSeconds: settings.ttlSeconds,
|
|
89
|
+
...settings.keyPrefix !== void 0 ? { lockKeyPrefix: settings.keyPrefix } : {},
|
|
90
|
+
signal
|
|
91
|
+
}),
|
|
92
|
+
intervalMs: settings.heartbeatIntervalSeconds * 1e3,
|
|
93
|
+
fallbackTtlSeconds: settings.ttlSeconds,
|
|
94
|
+
initialTtlSeconds: lock.ttlSeconds === void 0 ? void 0 : lock.ttlSeconds - (Date.now() - lockRequestedAt) / 1e3,
|
|
95
|
+
unref: true,
|
|
96
|
+
onLost: (error) => {
|
|
97
|
+
lockLost = { error };
|
|
98
|
+
try {
|
|
99
|
+
args.onLockLost?.(error);
|
|
100
|
+
} catch {}
|
|
101
|
+
stopRun();
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
let runError;
|
|
105
|
+
const stopRanOut = new Promise((_resolve, reject) => {
|
|
106
|
+
failStop = reject;
|
|
107
|
+
});
|
|
108
|
+
stopRanOut.catch(() => {});
|
|
109
|
+
try {
|
|
110
|
+
args.signal?.addEventListener("abort", stopBySignal, { once: true });
|
|
111
|
+
if (args.signal?.aborted) stopBySignal();
|
|
112
|
+
const started = await Promise.race([args.start({
|
|
113
|
+
threadId,
|
|
114
|
+
runId
|
|
115
|
+
}), stopRanOut]);
|
|
116
|
+
agent = started.agent;
|
|
117
|
+
if (lockLost) return {
|
|
118
|
+
threadId,
|
|
119
|
+
runId,
|
|
120
|
+
lockLost,
|
|
121
|
+
stopped: false
|
|
122
|
+
};
|
|
123
|
+
if (stoppedBySignal) return {
|
|
124
|
+
threadId,
|
|
125
|
+
runId,
|
|
126
|
+
stopped: true
|
|
127
|
+
};
|
|
128
|
+
const streamed = new Promise((resolve, reject) => {
|
|
129
|
+
runner.run({
|
|
130
|
+
threadId,
|
|
131
|
+
...lock.backendThreadId === void 0 ? {} : { backendThreadId: lock.backendThreadId },
|
|
132
|
+
agent: started.agent,
|
|
133
|
+
input: {
|
|
134
|
+
...started.input,
|
|
135
|
+
threadId,
|
|
136
|
+
runId
|
|
137
|
+
},
|
|
138
|
+
...started.persistedInputMessages !== void 0 ? { persistedInputMessages: started.persistedInputMessages } : {},
|
|
139
|
+
recordStop: (signal) => stoppedBySignal ? intelligence.ɵrecordRunStop({
|
|
140
|
+
threadId,
|
|
141
|
+
runId,
|
|
142
|
+
userId: args.acquire.userId,
|
|
143
|
+
agentId: args.acquire.agentId,
|
|
144
|
+
signal
|
|
145
|
+
}) : Promise.resolve()
|
|
146
|
+
}).subscribe({
|
|
147
|
+
next: (event) => {
|
|
148
|
+
if (event.type === EventType.RUN_ERROR) runError ??= event;
|
|
149
|
+
},
|
|
150
|
+
error: (error) => lockLost ? resolve() : reject(error),
|
|
151
|
+
complete: () => resolve()
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
await Promise.race([streamed, stopRanOut]);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (!lockLost) throw error;
|
|
157
|
+
} finally {
|
|
158
|
+
args.signal?.removeEventListener("abort", stopBySignal);
|
|
159
|
+
heartbeat.stop();
|
|
160
|
+
if (stopTimer !== void 0) clearTimeout(stopTimer);
|
|
161
|
+
const remaining = remainingStopMs();
|
|
162
|
+
const releasing = releaseLock(intelligence, {
|
|
163
|
+
threadId,
|
|
164
|
+
runId
|
|
165
|
+
}, remaining !== void 0 && remaining > 0 ? remaining : CLEANUP_TIMEOUT_MS);
|
|
166
|
+
if (remaining !== 0) await releasing;
|
|
167
|
+
}
|
|
168
|
+
if (lockLost) await settled(stopPromise, remainingStopMs());
|
|
169
|
+
return {
|
|
170
|
+
threadId,
|
|
171
|
+
runId,
|
|
172
|
+
...runError ? { runError } : {},
|
|
173
|
+
...lockLost ? { lockLost } : {},
|
|
174
|
+
stopped: stoppedBySignal
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/** Longest a lock release may take before the lock is left to its TTL. */
|
|
178
|
+
const CLEANUP_TIMEOUT_MS = 1e4;
|
|
179
|
+
/** What {@link untilAborted} resolves with when the signal aborts first. */
|
|
180
|
+
const ABORTED = Symbol("aborted");
|
|
181
|
+
/** `promise`'s value, or {@link ABORTED} as soon as `signal` aborts. */
|
|
182
|
+
function untilAborted(promise, signal) {
|
|
183
|
+
if (!signal) return promise;
|
|
184
|
+
if (signal.aborted) return Promise.resolve(ABORTED);
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
const onAbort = () => resolve(ABORTED);
|
|
187
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
188
|
+
promise.then((value) => {
|
|
189
|
+
signal.removeEventListener("abort", onAbort);
|
|
190
|
+
resolve(value);
|
|
191
|
+
}, (error) => {
|
|
192
|
+
signal.removeEventListener("abort", onAbort);
|
|
193
|
+
reject(error);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Give a Thread's lock back, waiting at most `timeoutMs` for the platform to
|
|
199
|
+
* confirm it. Never rejects: the lock's TTL is the backstop for a release
|
|
200
|
+
* that cannot be confirmed.
|
|
201
|
+
*/
|
|
202
|
+
async function releaseLock(intelligence, run, timeoutMs) {
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
205
|
+
timer.unref?.();
|
|
206
|
+
try {
|
|
207
|
+
await untilAborted(intelligence.ɵcleanupThreadLock({
|
|
208
|
+
...run,
|
|
209
|
+
signal: controller.signal
|
|
210
|
+
}), controller.signal);
|
|
211
|
+
} catch {} finally {
|
|
212
|
+
clearTimeout(timer);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** Wait for `promise`, for at most `timeoutMs` when it is set. */
|
|
216
|
+
async function settled(promise, timeoutMs) {
|
|
217
|
+
if (promise === void 0) return;
|
|
218
|
+
if (timeoutMs === void 0) {
|
|
219
|
+
await promise;
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
if (timeoutMs <= 0) return;
|
|
223
|
+
let timer;
|
|
224
|
+
await Promise.race([promise, new Promise((resolve) => {
|
|
225
|
+
timer = setTimeout(resolve, timeoutMs);
|
|
226
|
+
timer.unref?.();
|
|
227
|
+
})]);
|
|
228
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* A run that {@link runAgentInThread} started and that did not finish.
|
|
232
|
+
* The runtime no longer renews its lock. If the stop deadline expired,
|
|
233
|
+
* a bounded release continues in the background, with the TTL as backstop.
|
|
234
|
+
*/
|
|
235
|
+
var ThreadRunError = class extends Error {
|
|
236
|
+
constructor(reason, message, details) {
|
|
237
|
+
super(message, details.cause === void 0 ? void 0 : { cause: details.cause });
|
|
238
|
+
this.name = "ThreadRunError";
|
|
239
|
+
this.reason = reason;
|
|
240
|
+
this.threadId = details.threadId;
|
|
241
|
+
this.runId = details.runId;
|
|
242
|
+
if (details.code !== void 0) this.code = details.code;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Run an agent in an Intelligence Thread with no client attached: a
|
|
247
|
+
* scheduled run, a hand-off from another agent, or a run that resumes once a
|
|
248
|
+
* person answers. The run is stored in the Thread as a browser's run is, so
|
|
249
|
+
* the person sees it when they open the Thread.
|
|
250
|
+
*
|
|
251
|
+
* The Thread is created if it does not exist. The run holds the Thread's
|
|
252
|
+
* lock from before its first event until the runner's stream has ended,
|
|
253
|
+
* renews it while the agent works, and releases it on every path, so a run
|
|
254
|
+
* never leaves its Thread locked. A Thread another run holds refuses the call
|
|
255
|
+
* with a `PlatformRequestError` whose status is 409, before anything runs.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* import { runAgentInThread } from "@copilotkit/runtime";
|
|
260
|
+
* import { BuiltInAgent } from "@copilotkit/runtime/agent";
|
|
261
|
+
* import { openaiText } from "@tanstack/ai-openai";
|
|
262
|
+
*
|
|
263
|
+
* const { newMessages } = await runAgentInThread({
|
|
264
|
+
* runtime,
|
|
265
|
+
* agent: new BuiltInAgent({ model: openaiText("gpt-5.4") }),
|
|
266
|
+
* agentId: "assistant",
|
|
267
|
+
* threadId,
|
|
268
|
+
* userId,
|
|
269
|
+
* messages: [
|
|
270
|
+
* { id: crypto.randomUUID(), role: "user", content: "Summarise today" },
|
|
271
|
+
* ],
|
|
272
|
+
* signal: AbortSignal.timeout(5 * 60_000),
|
|
273
|
+
* });
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @throws A `PlatformRequestError` when Intelligence refuses the Thread or
|
|
277
|
+
* its lock; a {@link ThreadRunError} when the run fails, loses its lock, or
|
|
278
|
+
* does not end within `stopTimeoutMs` of being stopped; `signal.reason`
|
|
279
|
+
* when `signal` is already aborted, before anything is sent.
|
|
280
|
+
*/
|
|
281
|
+
async function runAgentInThread(options) {
|
|
282
|
+
const { runtime, agent, agentId, userId } = options;
|
|
283
|
+
const intelligence = runtime.intelligence;
|
|
284
|
+
if (!intelligence) throw new Error("runAgentInThread requires a runtime configured with `intelligence`");
|
|
285
|
+
options.signal?.throwIfAborted();
|
|
286
|
+
const learning = options.learningContainerId !== void 0 ? { learningContainerId: options.learningContainerId } : {};
|
|
287
|
+
const requestedRunId = options.runId ?? globalThis.crypto.randomUUID();
|
|
288
|
+
if (await untilAborted(intelligence.getOrCreateThread({
|
|
289
|
+
threadId: options.threadId,
|
|
290
|
+
userId,
|
|
291
|
+
agentId,
|
|
292
|
+
...learning
|
|
293
|
+
}), options.signal) === ABORTED) return {
|
|
294
|
+
threadId: options.threadId,
|
|
295
|
+
runId: requestedRunId,
|
|
296
|
+
newMessages: [],
|
|
297
|
+
stopped: true
|
|
298
|
+
};
|
|
299
|
+
const runner = runtime.runner ?? sharedRunnerFor(intelligence);
|
|
300
|
+
/** The conversation's ids as the run was given it; unset until it was. */
|
|
301
|
+
let before;
|
|
302
|
+
const subscription = options.subscriber ? agent.subscribe(options.subscriber) : void 0;
|
|
303
|
+
const outcome = await runWithThreadLock({
|
|
304
|
+
runner,
|
|
305
|
+
intelligence,
|
|
306
|
+
lock: {
|
|
307
|
+
ttlSeconds: runtime.lockTtlSeconds ?? DEFAULT_LOCK_TTL_SECONDS,
|
|
308
|
+
heartbeatIntervalSeconds: runtime.lockHeartbeatIntervalSeconds ?? DEFAULT_LOCK_HEARTBEAT_INTERVAL_SECONDS,
|
|
309
|
+
...runtime.lockKeyPrefix !== void 0 ? { keyPrefix: runtime.lockKeyPrefix } : {}
|
|
310
|
+
},
|
|
311
|
+
acquire: {
|
|
312
|
+
supportsBackendThreadId: true,
|
|
313
|
+
threadId: options.threadId,
|
|
314
|
+
runId: requestedRunId,
|
|
315
|
+
userId,
|
|
316
|
+
agentId,
|
|
317
|
+
...learning,
|
|
318
|
+
...options.requestId !== void 0 ? { requestId: options.requestId } : {},
|
|
319
|
+
...options.analyticsContext !== void 0 ? { analyticsContext: options.analyticsContext } : {}
|
|
320
|
+
},
|
|
321
|
+
...options.signal ? { signal: options.signal } : {},
|
|
322
|
+
...options.stopTimeoutMs !== void 0 ? { stopTimeoutMs: options.stopTimeoutMs } : {},
|
|
323
|
+
...options.onLockLost ? { onLockLost: options.onLockLost } : {},
|
|
324
|
+
start: async ({ threadId, runId }) => {
|
|
325
|
+
const [stored, state] = await Promise.all([intelligence.getThreadMessages({
|
|
326
|
+
threadId,
|
|
327
|
+
userId
|
|
328
|
+
}), "state" in options ? Promise.resolve({ given: options.state }) : storedState(intelligence, {
|
|
329
|
+
threadId,
|
|
330
|
+
userId
|
|
331
|
+
})]);
|
|
332
|
+
const history = await Promise.all(stored.messages.map((message) => toAgentMessage(message, intelligence)));
|
|
333
|
+
const conversation = typeof options.messages === "function" ? await options.messages(history, {
|
|
334
|
+
threadId,
|
|
335
|
+
runId
|
|
336
|
+
}) : [...history, ...options.messages ?? []];
|
|
337
|
+
const historic = new Set(stored.messages.map((message) => message.id));
|
|
338
|
+
agent.threadId = threadId;
|
|
339
|
+
agent.setMessages(conversation);
|
|
340
|
+
if (state) agent.setState(state.given);
|
|
341
|
+
before = new Set(conversation.map((message) => message.id));
|
|
342
|
+
return {
|
|
343
|
+
agent,
|
|
344
|
+
input: {
|
|
345
|
+
threadId,
|
|
346
|
+
runId,
|
|
347
|
+
messages: conversation,
|
|
348
|
+
state: agent.state,
|
|
349
|
+
tools: options.tools ?? [],
|
|
350
|
+
context: options.context ?? [],
|
|
351
|
+
forwardedProps: options.forwardedProps,
|
|
352
|
+
...options.resume !== void 0 ? { resume: options.resume } : {}
|
|
353
|
+
},
|
|
354
|
+
persistedInputMessages: typeof options.persistedInputMessages === "function" ? options.persistedInputMessages({
|
|
355
|
+
threadId,
|
|
356
|
+
runId
|
|
357
|
+
}) : options.persistedInputMessages ?? conversation.filter((message) => !historic.has(message.id))
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
}).finally(() => subscription?.unsubscribe());
|
|
361
|
+
const { threadId, runId } = outcome;
|
|
362
|
+
if (outcome.runError) {
|
|
363
|
+
const event = outcome.runError;
|
|
364
|
+
throw new ThreadRunError("run_failed", typeof event.message === "string" && event.message.length > 0 ? event.message : "The agent run failed", {
|
|
365
|
+
threadId,
|
|
366
|
+
runId,
|
|
367
|
+
...typeof event.code === "string" && event.code.length > 0 ? { code: event.code } : {}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
if (outcome.lockLost) throw new ThreadRunError("lock_lost", "The Thread's lock could not be renewed, so the run was stopped", {
|
|
371
|
+
threadId,
|
|
372
|
+
runId,
|
|
373
|
+
cause: outcome.lockLost.error
|
|
374
|
+
});
|
|
375
|
+
return {
|
|
376
|
+
threadId,
|
|
377
|
+
runId,
|
|
378
|
+
newMessages: before ? agent.messages.filter((message) => !before?.has(message.id)) : [],
|
|
379
|
+
stopped: outcome.stopped
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
/** One runner per Intelligence client, for runs given no runner of their own. */
|
|
383
|
+
const sharedRunners = /* @__PURE__ */ new WeakMap();
|
|
384
|
+
function sharedRunnerFor(intelligence) {
|
|
385
|
+
let runner = sharedRunners.get(intelligence);
|
|
386
|
+
if (!runner) {
|
|
387
|
+
runner = new IntelligenceAgentRunner({
|
|
388
|
+
url: intelligence.ɵgetRunnerWsUrl(),
|
|
389
|
+
authToken: intelligence.ɵgetRunnerAuthToken()
|
|
390
|
+
});
|
|
391
|
+
sharedRunners.set(intelligence, runner);
|
|
392
|
+
}
|
|
393
|
+
return runner;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The state a Thread last stored, or undefined when it has none. A platform
|
|
397
|
+
* that does not serve stored state is read as having none; state that cannot
|
|
398
|
+
* be decoded fails the run rather than let it start from an empty state and
|
|
399
|
+
* store that over the real one.
|
|
400
|
+
*/
|
|
401
|
+
async function storedState(intelligence, thread) {
|
|
402
|
+
const read = await intelligence.ɵgetStoredThreadState(thread);
|
|
403
|
+
if (read?.kind === "snapshot") return { given: read.state };
|
|
404
|
+
if (read?.kind === "snapshot-decode-error") throw new Error("The Thread's stored state could not be decoded, so the run did not start");
|
|
405
|
+
}
|
|
406
|
+
/** Convert canonical Intelligence history into AG-UI messages. */
|
|
407
|
+
async function toAgentMessage(message, intelligence) {
|
|
408
|
+
const content = await hydrateManagedContent(message.content, intelligence);
|
|
409
|
+
return {
|
|
410
|
+
id: message.id,
|
|
411
|
+
role: message.role,
|
|
412
|
+
content: content ?? "",
|
|
413
|
+
...message.activityType ? { activityType: message.activityType } : {},
|
|
414
|
+
...message.toolCalls ? { toolCalls: message.toolCalls.map((call) => ({
|
|
415
|
+
id: call.id,
|
|
416
|
+
type: "function",
|
|
417
|
+
function: {
|
|
418
|
+
name: call.name,
|
|
419
|
+
arguments: call.args
|
|
420
|
+
}
|
|
421
|
+
})) } : {},
|
|
422
|
+
...message.toolCallId ? { toolCallId: message.toolCallId } : {}
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
/** Resolves managed asset references only at the authorized Runtime boundary. */
|
|
426
|
+
async function hydrateManagedContent(content, intelligence) {
|
|
427
|
+
if (Array.isArray(content)) return Promise.all(content.map(async (part) => {
|
|
428
|
+
if (typeof part !== "object" || part === null || !("source" in part) || typeof part.source !== "object" || part.source === null || !("value" in part.source) || typeof part.source.value !== "string" || !part.source.value.startsWith("cpki-asset://")) return part;
|
|
429
|
+
const assetId = part.source.value.slice(13);
|
|
430
|
+
const asset = await intelligence.ɵgetManagedChannelAsset(assetId);
|
|
431
|
+
return {
|
|
432
|
+
...part,
|
|
433
|
+
source: {
|
|
434
|
+
type: "data",
|
|
435
|
+
value: bytesToBase64(asset.bytes),
|
|
436
|
+
mimeType: asset.mimeType ?? ("mimeType" in part.source && typeof part.source.mimeType === "string" ? part.source.mimeType : "application/octet-stream")
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}));
|
|
440
|
+
if (typeof content === "object" && content !== null && "assetId" in content && typeof content.assetId === "string") {
|
|
441
|
+
const asset = await intelligence.ɵgetManagedChannelAsset(content.assetId);
|
|
442
|
+
return {
|
|
443
|
+
...content,
|
|
444
|
+
source: {
|
|
445
|
+
type: "data",
|
|
446
|
+
value: bytesToBase64(asset.bytes),
|
|
447
|
+
mimeType: asset.mimeType ?? ("mimeType" in content && typeof content.mimeType === "string" ? content.mimeType : "application/octet-stream")
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
return content;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
//#endregion
|
|
455
|
+
export { ThreadRunError, runAgentInThread, runWithThreadLock, toAgentMessage };
|
|
456
|
+
//# sourceMappingURL=thread-run.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread-run.mjs","names":[],"sources":["../../../src/runtime/core/thread-run.ts"],"sourcesContent":["import { EventType } from \"@ag-ui/client\";\nimport type {\n AbstractAgent,\n AgentSubscriber,\n BaseEvent,\n Message,\n RunAgentInput,\n} from \"@ag-ui/client\";\nimport type { CopilotKitIntelligence } from \"../intelligence-platform\";\nimport type { AcquireThreadLockRequest } from \"../intelligence-platform/client\";\nimport { startThreadLockHeartbeat } from \"../intelligence-platform/thread-lock-heartbeat\";\nimport type { AgentRunner } from \"../runner/agent-runner\";\nimport { IntelligenceAgentRunner } from \"../runner/intelligence\";\nimport { bytesToBase64 } from \"./web-crypto\";\n\n/** Lock TTL used when the runtime passed in does not set one. */\nconst DEFAULT_LOCK_TTL_SECONDS = 20;\n/** Lock renewal interval used when the runtime passed in does not set one. */\nconst DEFAULT_LOCK_HEARTBEAT_INTERVAL_SECONDS = 15;\n\n/** How a Thread lock is held: its lifetime, its renewal interval, its key. */\nexport interface ThreadLockSettings {\n ttlSeconds: number;\n heartbeatIntervalSeconds: number;\n keyPrefix?: string;\n}\n\n/** What a locked run sends, decided once the lock is held. */\nexport interface LockedRunStart {\n /** The agent the runner runs. Aborted when the run is stopped. */\n agent: AbstractAgent;\n /** The run's input. Its `threadId` and `runId` are replaced by the lock's. */\n input: RunAgentInput;\n /** The input messages Intelligence stores; the runner defaults to all of them. */\n persistedInputMessages?: Message[];\n}\n\n/** How a locked run ended, when it ended without a transport failure. */\nexport interface LockedRunOutcome {\n /** The Thread ID Intelligence issued with the lock. */\n threadId: string;\n /** The run ID Intelligence issued with the lock. */\n runId: string;\n /** The `RUN_ERROR` the run ended with, if it failed. */\n runError?: BaseEvent;\n /** The renewal failure that lost the lock, if it was lost. */\n lockLost?: { error: unknown };\n /** True when the run was stopped through `signal`. */\n stopped: boolean;\n}\n\n/** Everything {@link runWithThreadLock} needs to hold a lock around one run. */\nexport interface LockedRunArgs {\n runner: AgentRunner;\n intelligence: CopilotKitIntelligence;\n lock: ThreadLockSettings;\n /** The lock request, without the settings in `lock`. */\n acquire: Omit<AcquireThreadLockRequest, \"ttlSeconds\" | \"lockKeyPrefix\">;\n /**\n * Prepares the run once the lock is held, with the Thread and run IDs the\n * lock issued. The lock is renewed while it runs and released if it throws.\n */\n start(canonical: {\n threadId: string;\n runId: string;\n }): LockedRunStart | Promise<LockedRunStart>;\n /** Stops the run when it aborts. The stop is recorded with Intelligence. */\n signal?: AbortSignal;\n /**\n * How long to wait for the run to end once it has been asked to stop. When\n * it has not ended by then the call rejects with `\"stop_timeout\"` (or\n * `\"lock_lost\"` for a lost lock) and release continues in the background.\n * Unset, the call waits for the runner.\n */\n stopTimeoutMs?: number;\n /** Called once, as soon as the lock is lost and before the run is stopped. */\n onLockLost?: (error: unknown) => void;\n}\n\n/**\n * Hold a Thread's lock around one run: acquire it, renew it while the run\n * works, stop the run if the lock is lost or `signal` aborts, and release it\n * on every path once the runner's stream has ended.\n *\n * Shared by managed Channel runs and {@link runAgentInThread}.\n *\n * @throws The lock request's error when the lock is refused, an error thrown\n * by `start`, the runner's stream error, or a {@link ThreadRunError} with\n * reason `\"stop_timeout\"`. Lock release is attempted whenever it was\n * acquired. If the stop deadline expires, release continues in the\n * background for a bounded time, with the lock's TTL as the backstop.\n */\nexport async function runWithThreadLock(\n args: LockedRunArgs,\n): Promise<LockedRunOutcome> {\n const { runner, intelligence, lock: settings } = args;\n const requested = {\n threadId: args.acquire.threadId,\n runId: args.acquire.runId,\n };\n // Stopped before the lock was asked for: nothing is sent.\n if (args.signal?.aborted) return { ...requested, stopped: true };\n // The platform starts the lock's TTL when it handles the request, so time\n // the lifetime from the send rather than from the response.\n const lockRequestedAt = Date.now();\n const acquiring = intelligence.ɵacquireThreadLock({\n ...args.acquire,\n ttlSeconds: settings.ttlSeconds,\n ...(settings.keyPrefix !== undefined\n ? { lockKeyPrefix: settings.keyPrefix }\n : {}),\n });\n const lock = await untilAborted(acquiring, args.signal);\n if (lock === ABORTED) {\n // Stopped while the lock was being asked for. Nothing runs, and a lock\n // the platform grants after all is given back as soon as it arrives.\n void acquiring.then(\n (late) =>\n releaseLock(\n intelligence,\n {\n threadId: late.threadId || requested.threadId,\n runId: late.runId || requested.runId,\n },\n CLEANUP_TIMEOUT_MS,\n ),\n () => {\n // The lock request failed, so there is no lock to give back.\n },\n );\n return { ...requested, stopped: true };\n }\n const threadId = lock.threadId || requested.threadId;\n const runId = lock.runId || requested.runId;\n\n let agent: AbstractAgent | undefined;\n let stopPromise: Promise<boolean | undefined> | undefined;\n let lockLost: { error: unknown } | undefined;\n let stoppedBySignal = false;\n let stopTimer: ReturnType<typeof setTimeout> | undefined;\n let stopDeadline: number | undefined;\n let failStop: ((error: Error) => void) | undefined;\n\n const stopRun = (): void => {\n if (args.stopTimeoutMs !== undefined && stopDeadline === undefined) {\n stopDeadline = Date.now() + args.stopTimeoutMs;\n stopTimer = setTimeout(() => {\n failStop?.(\n new ThreadRunError(\n \"stop_timeout\",\n `The run did not end within ${args.stopTimeoutMs}ms of being stopped`,\n { threadId, runId },\n ),\n );\n }, args.stopTimeoutMs);\n stopTimer.unref?.();\n }\n try {\n agent?.abortRun();\n } catch {\n // The exact runner stop remains the authoritative cancellation path.\n }\n stopPromise ??= Promise.resolve()\n .then(() => runner.stop({ threadId, runId }))\n .catch(() => false);\n };\n const remainingStopMs = () =>\n stopDeadline === undefined\n ? undefined\n : Math.max(0, stopDeadline - Date.now());\n // A signal that aborts goes nowhere near Intelligence's own run stop, so\n // this runtime records it. A lost lock also stops the run, but that is a\n // failure rather than a cancellation, so it records nothing.\n const stopBySignal = (): void => {\n stoppedBySignal = true;\n stopRun();\n };\n\n const heartbeat = startThreadLockHeartbeat({\n renew: (signal) =>\n intelligence.ɵrenewThreadLock({\n threadId,\n runId,\n ttlSeconds: settings.ttlSeconds,\n ...(settings.keyPrefix !== undefined\n ? { lockKeyPrefix: settings.keyPrefix }\n : {}),\n signal,\n }),\n intervalMs: settings.heartbeatIntervalSeconds * 1_000,\n fallbackTtlSeconds: settings.ttlSeconds,\n initialTtlSeconds:\n lock.ttlSeconds === undefined\n ? undefined\n : lock.ttlSeconds - (Date.now() - lockRequestedAt) / 1_000,\n unref: true,\n onLost: (error) => {\n lockLost = { error };\n try {\n args.onLockLost?.(error);\n } catch {\n // A caller's observer must not keep the run from being stopped.\n }\n stopRun();\n },\n });\n\n let runError: BaseEvent | undefined;\n // Rejects when a stop has not ended the run, or its preparation, within\n // `stopTimeoutMs`.\n const stopRanOut = new Promise<never>((_resolve, reject) => {\n failStop = reject;\n });\n stopRanOut.catch(() => {\n // Read where the run is raced against it; this only keeps an unraced\n // rejection from being reported as unhandled.\n });\n try {\n // Listened for from here, so a stop while the run is being prepared is\n // heard, and bounded by `stopTimeoutMs`, as one during the run is.\n args.signal?.addEventListener(\"abort\", stopBySignal, { once: true });\n if (args.signal?.aborted) stopBySignal();\n const started = await Promise.race([\n args.start({ threadId, runId }),\n stopRanOut,\n ]);\n agent = started.agent;\n // Stopped, or the lock lost, while the run was being prepared: nothing\n // has run, so there is nothing to stop.\n if (lockLost) return { threadId, runId, lockLost, stopped: false };\n if (stoppedBySignal) return { threadId, runId, stopped: true };\n\n const streamed = new Promise<void>((resolve, reject) => {\n const stream = runner.run({\n threadId,\n ...(lock.backendThreadId === undefined\n ? {}\n : { backendThreadId: lock.backendThreadId }),\n agent: started.agent,\n input: { ...started.input, threadId, runId },\n ...(started.persistedInputMessages !== undefined\n ? { persistedInputMessages: started.persistedInputMessages }\n : {}),\n recordStop: (signal) =>\n stoppedBySignal\n ? intelligence.ɵrecordRunStop({\n threadId,\n runId,\n userId: args.acquire.userId,\n agentId: args.acquire.agentId,\n signal,\n })\n : Promise.resolve(),\n });\n stream.subscribe({\n next: (event: BaseEvent) => {\n if (event.type === EventType.RUN_ERROR) runError ??= event;\n },\n // Once the lock is lost, Intelligence refuses the run's remaining\n // events, so the stream failing is the lost lock, not a second fault.\n error: (error: unknown) => (lockLost ? resolve() : reject(error)),\n complete: () => resolve(),\n });\n });\n await Promise.race([streamed, stopRanOut]);\n } catch (error) {\n // Once the lock is lost, that is the failure to report, whatever else\n // went wrong while the run was being stopped.\n if (!lockLost) throw error;\n } finally {\n args.signal?.removeEventListener(\"abort\", stopBySignal);\n heartbeat.stop();\n if (stopTimer !== undefined) clearTimeout(stopTimer);\n // Always release the lock from the runtime side. The gateway also\n // releases it when it stores the run's terminal event; the release is a\n // compare-and-delete by run ID, so a second one changes nothing and never\n // frees a lock a newer run holds. Cleanup shares the stop's deadline.\n // Past that deadline, still try a bounded release in the background;\n // a release that cannot be confirmed leaves the lock to its TTL.\n const remaining = remainingStopMs();\n const releasing = releaseLock(\n intelligence,\n { threadId, runId },\n remaining !== undefined && remaining > 0 ? remaining : CLEANUP_TIMEOUT_MS,\n );\n if (remaining !== 0) await releasing;\n }\n\n // The stop a lost lock made is awaited before it is reported, as long as\n // the original stop deadline allows; lock release has already been attempted.\n if (lockLost) await settled(stopPromise, remainingStopMs());\n return {\n threadId,\n runId,\n ...(runError ? { runError } : {}),\n ...(lockLost ? { lockLost } : {}),\n stopped: stoppedBySignal,\n };\n}\n\n/** Longest a lock release may take before the lock is left to its TTL. */\nconst CLEANUP_TIMEOUT_MS = 10_000;\n\n/** What {@link untilAborted} resolves with when the signal aborts first. */\nconst ABORTED: unique symbol = Symbol(\"aborted\");\n\n/** `promise`'s value, or {@link ABORTED} as soon as `signal` aborts. */\nfunction untilAborted<T>(\n promise: Promise<T>,\n signal: AbortSignal | undefined,\n): Promise<T | typeof ABORTED> {\n if (!signal) return promise;\n if (signal.aborted) return Promise.resolve(ABORTED);\n return new Promise<T | typeof ABORTED>((resolve, reject) => {\n const onAbort = () => resolve(ABORTED);\n signal.addEventListener(\"abort\", onAbort, { once: true });\n promise.then(\n (value) => {\n signal.removeEventListener(\"abort\", onAbort);\n resolve(value);\n },\n (error: unknown) => {\n signal.removeEventListener(\"abort\", onAbort);\n reject(error);\n },\n );\n });\n}\n\n/**\n * Give a Thread's lock back, waiting at most `timeoutMs` for the platform to\n * confirm it. Never rejects: the lock's TTL is the backstop for a release\n * that cannot be confirmed.\n */\nasync function releaseLock(\n intelligence: CopilotKitIntelligence,\n run: { threadId: string; runId: string },\n timeoutMs: number,\n): Promise<void> {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n timer.unref?.();\n try {\n await untilAborted(\n intelligence.ɵcleanupThreadLock({ ...run, signal: controller.signal }),\n controller.signal,\n );\n } catch {\n // The TTL releases a lock this call could not.\n } finally {\n clearTimeout(timer);\n }\n}\n\n/** Wait for `promise`, for at most `timeoutMs` when it is set. */\nasync function settled(\n promise: Promise<unknown> | undefined,\n timeoutMs: number | undefined,\n): Promise<void> {\n if (promise === undefined) return;\n if (timeoutMs === undefined) {\n await promise;\n return;\n }\n if (timeoutMs <= 0) return;\n let timer: ReturnType<typeof setTimeout> | undefined;\n await Promise.race([\n promise,\n new Promise<void>((resolve) => {\n timer = setTimeout(resolve, timeoutMs);\n timer.unref?.();\n }),\n ]);\n if (timer !== undefined) clearTimeout(timer);\n}\n\n/** Why a {@link runAgentInThread} call failed after its run was started. */\nexport type ThreadRunErrorReason =\n /** The agent ended the run with a `RUN_ERROR`. */\n | \"run_failed\"\n /** The Thread's lock could not be renewed, so the run was stopped. */\n | \"lock_lost\"\n /** The run was stopped and did not end within `stopTimeoutMs`. */\n | \"stop_timeout\";\n\n/**\n * A run that {@link runAgentInThread} started and that did not finish.\n * The runtime no longer renews its lock. If the stop deadline expired,\n * a bounded release continues in the background, with the TTL as backstop.\n */\nexport class ThreadRunError extends Error {\n readonly reason: ThreadRunErrorReason;\n /** The Thread the run belongs to. */\n readonly threadId: string;\n /** The run, as Intelligence issued it with the lock. */\n readonly runId: string;\n /** The `RUN_ERROR` event's `code`, for a failed run that sent one. */\n readonly code?: string;\n\n constructor(\n reason: ThreadRunErrorReason,\n message: string,\n details: {\n threadId: string;\n runId: string;\n code?: string;\n cause?: unknown;\n },\n ) {\n super(\n message,\n details.cause === undefined ? undefined : { cause: details.cause },\n );\n this.name = \"ThreadRunError\";\n this.reason = reason;\n this.threadId = details.threadId;\n this.runId = details.runId;\n if (details.code !== undefined) this.code = details.code;\n }\n}\n\n/**\n * The runtime a {@link runAgentInThread} call runs through. A `CopilotKitRuntime`\n * configured with `intelligence` is one; so is an Intelligence client on its\n * own, `{ intelligence }`.\n */\nexport interface RunAgentInThreadRuntime {\n /** The Intelligence client that holds the Thread and issues its lock. */\n intelligence?: CopilotKitIntelligence;\n /**\n * The runner that streams the run's events to Intelligence. Omitted, the\n * run uses one Intelligence runner per client, created on first use and\n * shared by every later run, so a Thread already running in this process\n * is refused at once.\n */\n runner?: AgentRunner;\n /** Lock TTL in seconds. @default 20 */\n lockTtlSeconds?: number;\n /** Interval in seconds at which the lock is renewed. @default 15 */\n lockHeartbeatIntervalSeconds?: number;\n /** Custom key prefix for the thread lock. */\n lockKeyPrefix?: string;\n}\n\n/** Options for {@link runAgentInThread}. */\nexport interface RunAgentInThreadOptions {\n /** The runtime to run through, usually your `CopilotKitRuntime`. */\n runtime: RunAgentInThreadRuntime;\n /**\n * The agent to run. It is run in place: its messages are replaced by the\n * run's conversation and the run's output is applied to it, so pass an\n * instance no other run is using.\n */\n agent: AbstractAgent;\n /** The agent's ID, as the Thread records it. */\n agentId: string;\n /** The Thread to run in. It is created if it does not exist yet. */\n threadId: string;\n /** The user the Thread belongs to and the run acts for. */\n userId: string;\n /**\n * The run's ID. Intelligence may issue a different one with the lock; the\n * result carries the one it issued. @default a random UUID\n */\n runId?: string;\n /**\n * The run's input. An array is this run's new messages, appended to the\n * Thread's stored history. A function receives that history, converted to\n * AG-UI messages, and the Thread and run IDs the lock issued, and returns\n * the whole conversation the agent sees. Unless `persistedInputMessages`\n * says otherwise, only messages whose IDs the Thread does not already hold\n * are stored.\n */\n messages?:\n | Message[]\n | ((\n history: Message[],\n run: { threadId: string; runId: string },\n ) => Message[] | Promise<Message[]>);\n /**\n * The input messages the Thread stores for this run, when that is not the\n * new part of the conversation: for example a short request on record\n * while the agent is sent a longer brief. A function receives the Thread\n * and run IDs the lock issued.\n */\n persistedInputMessages?:\n | Message[]\n | ((run: { threadId: string; runId: string }) => Message[]);\n /**\n * The state the run starts from. Omitted, the run starts from the state\n * the Thread last stored, so a fresh agent instance carries on where the\n * Thread's previous run left off; a Thread with no stored state keeps the\n * agent's own.\n */\n state?: RunAgentInput[\"state\"];\n /** Tools offered to the agent for this run. */\n tools?: RunAgentInput[\"tools\"];\n /** Context passed to the agent for this run. */\n context?: RunAgentInput[\"context\"];\n /** Properties forwarded to the agent with the run. */\n forwardedProps?: RunAgentInput[\"forwardedProps\"];\n /** Answers to the interrupts the Thread's previous run left open. */\n resume?: RunAgentInput[\"resume\"];\n /** Learning Container to assign the Thread to when it is created and locked. */\n learningContainerId?: string;\n /** The app's analytics context, which Intelligence records with the run. */\n analyticsContext?: Record<string, unknown>;\n /**\n * The request the run belongs to. Runs of one request (a follow-up, a\n * resume) send the same ID so the platform counts the request once.\n */\n requestId?: string;\n /**\n * Receives the agent's events while the run works, for example to show a\n * reply as it is written.\n */\n subscriber?: AgentSubscriber;\n /**\n * Stops the run when it aborts. The stop is recorded with Intelligence, so\n * the run reads as stopped rather than finished, and the call resolves with\n * `stopped: true` once the runner has ended the run. Use\n * `AbortSignal.timeout()` for a deadline.\n */\n signal?: AbortSignal;\n /**\n * How long to wait after `signal` or a lost lock stops the run. The stream,\n * lock release and final runner stop wait share one deadline. Past it,\n * release continues in the background for a bounded time, with the lock's\n * TTL as backstop. A run still active at the deadline rejects with\n * `\"stop_timeout\"`; a lost lock remains `\"lock_lost\"`.\n * Unset, the call waits for the runner.\n */\n stopTimeoutMs?: number;\n /**\n * Called once, as soon as the Thread's lock is lost and before the run is\n * stopped, so a caller can tell this stop from one somebody else made. The\n * call still rejects with a {@link ThreadRunError} whose reason is\n * `\"lock_lost\"` once the run has ended.\n */\n onLockLost?: (error: unknown) => void;\n}\n\n/** What a finished {@link runAgentInThread} call returns. */\nexport interface RunAgentInThreadResult {\n /** The Thread the run belongs to. */\n threadId: string;\n /** The run, as Intelligence issued it with the lock. */\n runId: string;\n /** The messages the run added to the conversation, in order. */\n newMessages: Message[];\n /**\n * True when `signal` stopped the run before it finished on its own. A run\n * stopped before it started (while the Thread was being created, its lock\n * taken, or the run prepared) added no messages.\n */\n stopped: boolean;\n}\n\n/**\n * Run an agent in an Intelligence Thread with no client attached: a\n * scheduled run, a hand-off from another agent, or a run that resumes once a\n * person answers. The run is stored in the Thread as a browser's run is, so\n * the person sees it when they open the Thread.\n *\n * The Thread is created if it does not exist. The run holds the Thread's\n * lock from before its first event until the runner's stream has ended,\n * renews it while the agent works, and releases it on every path, so a run\n * never leaves its Thread locked. A Thread another run holds refuses the call\n * with a `PlatformRequestError` whose status is 409, before anything runs.\n *\n * @example\n * ```ts\n * import { runAgentInThread } from \"@copilotkit/runtime\";\n * import { BuiltInAgent } from \"@copilotkit/runtime/agent\";\n * import { openaiText } from \"@tanstack/ai-openai\";\n *\n * const { newMessages } = await runAgentInThread({\n * runtime,\n * agent: new BuiltInAgent({ model: openaiText(\"gpt-5.4\") }),\n * agentId: \"assistant\",\n * threadId,\n * userId,\n * messages: [\n * { id: crypto.randomUUID(), role: \"user\", content: \"Summarise today\" },\n * ],\n * signal: AbortSignal.timeout(5 * 60_000),\n * });\n * ```\n *\n * @throws A `PlatformRequestError` when Intelligence refuses the Thread or\n * its lock; a {@link ThreadRunError} when the run fails, loses its lock, or\n * does not end within `stopTimeoutMs` of being stopped; `signal.reason`\n * when `signal` is already aborted, before anything is sent.\n */\nexport async function runAgentInThread(\n options: RunAgentInThreadOptions,\n): Promise<RunAgentInThreadResult> {\n const { runtime, agent, agentId, userId } = options;\n const intelligence = runtime.intelligence;\n if (!intelligence) {\n throw new Error(\n \"runAgentInThread requires a runtime configured with `intelligence`\",\n );\n }\n options.signal?.throwIfAborted();\n const learning =\n options.learningContainerId !== undefined\n ? { learningContainerId: options.learningContainerId }\n : {};\n\n const requestedRunId = options.runId ?? globalThis.crypto.randomUUID();\n\n // No client created the Thread, so the run creates it.\n const created = await untilAborted(\n intelligence.getOrCreateThread({\n threadId: options.threadId,\n userId,\n agentId,\n ...learning,\n }),\n options.signal,\n );\n if (created === ABORTED)\n return {\n threadId: options.threadId,\n runId: requestedRunId,\n newMessages: [],\n stopped: true,\n };\n\n const runner = runtime.runner ?? sharedRunnerFor(intelligence);\n /** The conversation's ids as the run was given it; unset until it was. */\n let before: Set<string> | undefined;\n const subscription = options.subscriber\n ? agent.subscribe(options.subscriber)\n : undefined;\n const outcome = await runWithThreadLock({\n runner,\n intelligence,\n lock: {\n ttlSeconds: runtime.lockTtlSeconds ?? DEFAULT_LOCK_TTL_SECONDS,\n heartbeatIntervalSeconds:\n runtime.lockHeartbeatIntervalSeconds ??\n DEFAULT_LOCK_HEARTBEAT_INTERVAL_SECONDS,\n ...(runtime.lockKeyPrefix !== undefined\n ? { keyPrefix: runtime.lockKeyPrefix }\n : {}),\n },\n acquire: {\n supportsBackendThreadId: true,\n threadId: options.threadId,\n runId: requestedRunId,\n userId,\n agentId,\n ...learning,\n ...(options.requestId !== undefined\n ? { requestId: options.requestId }\n : {}),\n ...(options.analyticsContext !== undefined\n ? { analyticsContext: options.analyticsContext }\n : {}),\n },\n ...(options.signal ? { signal: options.signal } : {}),\n ...(options.stopTimeoutMs !== undefined\n ? { stopTimeoutMs: options.stopTimeoutMs }\n : {}),\n ...(options.onLockLost ? { onLockLost: options.onLockLost } : {}),\n start: async ({ threadId, runId }) => {\n // Read once the lock is held, so no other run can add to the Thread\n // between this read and the run.\n const [stored, state] = await Promise.all([\n intelligence.getThreadMessages({ threadId, userId }),\n \"state\" in options\n ? Promise.resolve({ given: options.state })\n : storedState(intelligence, { threadId, userId }),\n ]);\n const history = await Promise.all(\n stored.messages.map((message) => toAgentMessage(message, intelligence)),\n );\n const conversation =\n typeof options.messages === \"function\"\n ? await options.messages(history, { threadId, runId })\n : [...history, ...(options.messages ?? [])];\n const historic = new Set(stored.messages.map((message) => message.id));\n // AG-UI agents build a run's input from their own Thread ID, messages\n // and state, so the conversation goes on the agent.\n agent.threadId = threadId;\n agent.setMessages(conversation);\n if (state) agent.setState(state.given);\n before = new Set(conversation.map((message) => message.id));\n return {\n agent,\n input: {\n threadId,\n runId,\n messages: conversation,\n state: agent.state,\n tools: options.tools ?? [],\n context: options.context ?? [],\n forwardedProps: options.forwardedProps,\n ...(options.resume !== undefined ? { resume: options.resume } : {}),\n },\n persistedInputMessages:\n typeof options.persistedInputMessages === \"function\"\n ? options.persistedInputMessages({ threadId, runId })\n : (options.persistedInputMessages ??\n conversation.filter((message) => !historic.has(message.id))),\n };\n },\n }).finally(() => subscription?.unsubscribe());\n\n const { threadId, runId } = outcome;\n if (outcome.runError) {\n const event = outcome.runError as BaseEvent & {\n message?: unknown;\n code?: unknown;\n };\n throw new ThreadRunError(\n \"run_failed\",\n typeof event.message === \"string\" && event.message.length > 0\n ? event.message\n : \"The agent run failed\",\n {\n threadId,\n runId,\n ...(typeof event.code === \"string\" && event.code.length > 0\n ? { code: event.code }\n : {}),\n },\n );\n }\n if (outcome.lockLost) {\n throw new ThreadRunError(\n \"lock_lost\",\n \"The Thread's lock could not be renewed, so the run was stopped\",\n { threadId, runId, cause: outcome.lockLost.error },\n );\n }\n return {\n threadId,\n runId,\n newMessages: before\n ? agent.messages.filter((message) => !before?.has(message.id))\n : [],\n stopped: outcome.stopped,\n };\n}\n\n/** One runner per Intelligence client, for runs given no runner of their own. */\nconst sharedRunners = new WeakMap<CopilotKitIntelligence, AgentRunner>();\n\nfunction sharedRunnerFor(intelligence: CopilotKitIntelligence): AgentRunner {\n let runner = sharedRunners.get(intelligence);\n if (!runner) {\n runner = new IntelligenceAgentRunner({\n url: intelligence.ɵgetRunnerWsUrl(),\n authToken: intelligence.ɵgetRunnerAuthToken(),\n });\n sharedRunners.set(intelligence, runner);\n }\n return runner;\n}\n\n/**\n * The state a Thread last stored, or undefined when it has none. A platform\n * that does not serve stored state is read as having none; state that cannot\n * be decoded fails the run rather than let it start from an empty state and\n * store that over the real one.\n */\nasync function storedState(\n intelligence: CopilotKitIntelligence,\n thread: { threadId: string; userId: string },\n): Promise<{ given: unknown } | undefined> {\n const read = await intelligence.ɵgetStoredThreadState(thread);\n if (read?.kind === \"snapshot\") return { given: read.state };\n if (read?.kind === \"snapshot-decode-error\") {\n throw new Error(\n \"The Thread's stored state could not be decoded, so the run did not start\",\n );\n }\n return undefined;\n}\n\n/** Convert canonical Intelligence history into AG-UI messages. */\nexport async function toAgentMessage(\n message: {\n id: string;\n role: string;\n activityType?: string;\n content?: unknown;\n toolCalls?: Array<{ id: string; name: string; args: string }>;\n toolCallId?: string;\n },\n intelligence: CopilotKitIntelligence,\n): Promise<Message> {\n const content = await hydrateManagedContent(message.content, intelligence);\n return {\n id: message.id,\n role: message.role as Message[\"role\"],\n content: content ?? \"\",\n ...(message.activityType ? { activityType: message.activityType } : {}),\n ...(message.toolCalls\n ? {\n toolCalls: message.toolCalls.map((call) => ({\n id: call.id,\n type: \"function\" as const,\n function: { name: call.name, arguments: call.args },\n })),\n }\n : {}),\n ...(message.toolCallId ? { toolCallId: message.toolCallId } : {}),\n } as Message;\n}\n\n/** Resolves managed asset references only at the authorized Runtime boundary. */\nasync function hydrateManagedContent(\n content: unknown,\n intelligence: CopilotKitIntelligence,\n): Promise<unknown> {\n if (Array.isArray(content)) {\n return Promise.all(\n content.map(async (part) => {\n if (\n typeof part !== \"object\" ||\n part === null ||\n !(\"source\" in part) ||\n typeof part.source !== \"object\" ||\n part.source === null ||\n !(\"value\" in part.source) ||\n typeof part.source.value !== \"string\" ||\n !part.source.value.startsWith(\"cpki-asset://\")\n ) {\n return part;\n }\n const assetId = part.source.value.slice(\"cpki-asset://\".length);\n const asset = await intelligence.ɵgetManagedChannelAsset(assetId);\n return {\n ...part,\n source: {\n type: \"data\",\n value: bytesToBase64(asset.bytes),\n mimeType:\n asset.mimeType ??\n (\"mimeType\" in part.source &&\n typeof part.source.mimeType === \"string\"\n ? part.source.mimeType\n : \"application/octet-stream\"),\n },\n };\n }),\n );\n }\n\n if (\n typeof content === \"object\" &&\n content !== null &&\n \"assetId\" in content &&\n typeof content.assetId === \"string\"\n ) {\n const asset = await intelligence.ɵgetManagedChannelAsset(content.assetId);\n return {\n ...content,\n source: {\n type: \"data\",\n value: bytesToBase64(asset.bytes),\n mimeType:\n asset.mimeType ??\n (\"mimeType\" in content && typeof content.mimeType === \"string\"\n ? content.mimeType\n : \"application/octet-stream\"),\n },\n };\n }\n\n return content;\n}\n"],"mappings":";;;;;;;AAgBA,MAAM,2BAA2B;;AAEjC,MAAM,0CAA0C;;;;;;;;;;;;;;AA0EhD,eAAsB,kBACpB,MAC2B;CAC3B,MAAM,EAAE,QAAQ,cAAc,MAAM,aAAa;CACjD,MAAM,YAAY;EAChB,UAAU,KAAK,QAAQ;EACvB,OAAO,KAAK,QAAQ;EACrB;AAED,KAAI,KAAK,QAAQ,QAAS,QAAO;EAAE,GAAG;EAAW,SAAS;EAAM;CAGhE,MAAM,kBAAkB,KAAK,KAAK;CAClC,MAAM,YAAY,aAAa,mBAAmB;EAChD,GAAG,KAAK;EACR,YAAY,SAAS;EACrB,GAAI,SAAS,cAAc,SACvB,EAAE,eAAe,SAAS,WAAW,GACrC,EAAE;EACP,CAAC;CACF,MAAM,OAAO,MAAM,aAAa,WAAW,KAAK,OAAO;AACvD,KAAI,SAA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QACZ,aAAa,WACb,OAAO,QAAQ,YAAY,UAC3B;EACA,MAAM,QAAQ,MAAM,aAAa,wBAAwB,QAAQ,QAAQ;AACzE,SAAO;GACL,GAAG;GACH,QAAQ;IACN,MAAM;IACN,OAAO,cAAc,MAAM,MAAM;IACjC,UACE,MAAM,aACL,cAAc,WAAW,OAAO,QAAQ,aAAa,WAClD,QAAQ,WACR;IACP;GACF;;AAGH,QAAO"}
|
|
@@ -174,7 +174,7 @@ var ArgsParser = class {
|
|
|
174
174
|
};
|
|
175
175
|
var OpenGenerativeUIMiddleware = class extends _ag_ui_client.Middleware {
|
|
176
176
|
run(input, next) {
|
|
177
|
-
return this.processStream(this.
|
|
177
|
+
return this.processStream(this.runNext(input, next));
|
|
178
178
|
}
|
|
179
179
|
processStream(source) {
|
|
180
180
|
return new rxjs.Observable((subscriber) => {
|
|
@@ -192,14 +192,13 @@ var OpenGenerativeUIMiddleware = class extends _ag_ui_client.Middleware {
|
|
|
192
192
|
}
|
|
193
193
|
};
|
|
194
194
|
const subscription = source.subscribe({
|
|
195
|
-
next: (
|
|
196
|
-
const event = eventWithState.event;
|
|
195
|
+
next: (event) => {
|
|
197
196
|
if (heldRunFinished) {
|
|
198
|
-
subscriber.next(heldRunFinished
|
|
197
|
+
subscriber.next(heldRunFinished);
|
|
199
198
|
heldRunFinished = null;
|
|
200
199
|
}
|
|
201
200
|
if (event.type === _ag_ui_client.EventType.RUN_FINISHED) {
|
|
202
|
-
heldRunFinished =
|
|
201
|
+
heldRunFinished = event;
|
|
203
202
|
return;
|
|
204
203
|
}
|
|
205
204
|
if (event.type === _ag_ui_client.EventType.TOOL_CALL_START) {
|
|
@@ -249,7 +248,7 @@ var OpenGenerativeUIMiddleware = class extends _ag_ui_client.Middleware {
|
|
|
249
248
|
for (const [, events] of heldToolCallEvents) for (const event of events) subscriber.next(event);
|
|
250
249
|
heldToolCallEvents.clear();
|
|
251
250
|
if (heldRunFinished) {
|
|
252
|
-
subscriber.next(heldRunFinished
|
|
251
|
+
subscriber.next(heldRunFinished);
|
|
253
252
|
heldRunFinished = null;
|
|
254
253
|
}
|
|
255
254
|
subscriber.error(err);
|
|
@@ -259,7 +258,7 @@ var OpenGenerativeUIMiddleware = class extends _ag_ui_client.Middleware {
|
|
|
259
258
|
flushHeldEvents(toolCallId);
|
|
260
259
|
});
|
|
261
260
|
if (heldRunFinished) {
|
|
262
|
-
subscriber.next(heldRunFinished
|
|
261
|
+
subscriber.next(heldRunFinished);
|
|
263
262
|
heldRunFinished = null;
|
|
264
263
|
}
|
|
265
264
|
activeParsers.clear();
|