@copilotkit/runtime 2.0.0-rc.60 → 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 +9 -9
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { AgentRunner } from "../runner/agent-runner.cjs";
|
|
2
|
+
import "../intelligence-platform/index.cjs";
|
|
3
|
+
import { CopilotKitIntelligence } from "../intelligence-platform/client.cjs";
|
|
4
|
+
import { AbstractAgent, AgentSubscriber, BaseEvent, Message, RunAgentInput } from "@ag-ui/client";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime/core/thread-run.d.ts
|
|
7
|
+
/** Why a {@link runAgentInThread} call failed after its run was started. */
|
|
8
|
+
type ThreadRunErrorReason = /** The agent ended the run with a `RUN_ERROR`. */"run_failed" /** The Thread's lock could not be renewed, so the run was stopped. */ | "lock_lost" /** The run was stopped and did not end within `stopTimeoutMs`. */ | "stop_timeout";
|
|
9
|
+
/**
|
|
10
|
+
* A run that {@link runAgentInThread} started and that did not finish.
|
|
11
|
+
* The runtime no longer renews its lock. If the stop deadline expired,
|
|
12
|
+
* a bounded release continues in the background, with the TTL as backstop.
|
|
13
|
+
*/
|
|
14
|
+
declare class ThreadRunError extends Error {
|
|
15
|
+
readonly reason: ThreadRunErrorReason;
|
|
16
|
+
/** The Thread the run belongs to. */
|
|
17
|
+
readonly threadId: string;
|
|
18
|
+
/** The run, as Intelligence issued it with the lock. */
|
|
19
|
+
readonly runId: string;
|
|
20
|
+
/** The `RUN_ERROR` event's `code`, for a failed run that sent one. */
|
|
21
|
+
readonly code?: string;
|
|
22
|
+
constructor(reason: ThreadRunErrorReason, message: string, details: {
|
|
23
|
+
threadId: string;
|
|
24
|
+
runId: string;
|
|
25
|
+
code?: string;
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The runtime a {@link runAgentInThread} call runs through. A `CopilotKitRuntime`
|
|
31
|
+
* configured with `intelligence` is one; so is an Intelligence client on its
|
|
32
|
+
* own, `{ intelligence }`.
|
|
33
|
+
*/
|
|
34
|
+
interface RunAgentInThreadRuntime {
|
|
35
|
+
/** The Intelligence client that holds the Thread and issues its lock. */
|
|
36
|
+
intelligence?: CopilotKitIntelligence;
|
|
37
|
+
/**
|
|
38
|
+
* The runner that streams the run's events to Intelligence. Omitted, the
|
|
39
|
+
* run uses one Intelligence runner per client, created on first use and
|
|
40
|
+
* shared by every later run, so a Thread already running in this process
|
|
41
|
+
* is refused at once.
|
|
42
|
+
*/
|
|
43
|
+
runner?: AgentRunner;
|
|
44
|
+
/** Lock TTL in seconds. @default 20 */
|
|
45
|
+
lockTtlSeconds?: number;
|
|
46
|
+
/** Interval in seconds at which the lock is renewed. @default 15 */
|
|
47
|
+
lockHeartbeatIntervalSeconds?: number;
|
|
48
|
+
/** Custom key prefix for the thread lock. */
|
|
49
|
+
lockKeyPrefix?: string;
|
|
50
|
+
}
|
|
51
|
+
/** Options for {@link runAgentInThread}. */
|
|
52
|
+
interface RunAgentInThreadOptions {
|
|
53
|
+
/** The runtime to run through, usually your `CopilotKitRuntime`. */
|
|
54
|
+
runtime: RunAgentInThreadRuntime;
|
|
55
|
+
/**
|
|
56
|
+
* The agent to run. It is run in place: its messages are replaced by the
|
|
57
|
+
* run's conversation and the run's output is applied to it, so pass an
|
|
58
|
+
* instance no other run is using.
|
|
59
|
+
*/
|
|
60
|
+
agent: AbstractAgent;
|
|
61
|
+
/** The agent's ID, as the Thread records it. */
|
|
62
|
+
agentId: string;
|
|
63
|
+
/** The Thread to run in. It is created if it does not exist yet. */
|
|
64
|
+
threadId: string;
|
|
65
|
+
/** The user the Thread belongs to and the run acts for. */
|
|
66
|
+
userId: string;
|
|
67
|
+
/**
|
|
68
|
+
* The run's ID. Intelligence may issue a different one with the lock; the
|
|
69
|
+
* result carries the one it issued. @default a random UUID
|
|
70
|
+
*/
|
|
71
|
+
runId?: string;
|
|
72
|
+
/**
|
|
73
|
+
* The run's input. An array is this run's new messages, appended to the
|
|
74
|
+
* Thread's stored history. A function receives that history, converted to
|
|
75
|
+
* AG-UI messages, and the Thread and run IDs the lock issued, and returns
|
|
76
|
+
* the whole conversation the agent sees. Unless `persistedInputMessages`
|
|
77
|
+
* says otherwise, only messages whose IDs the Thread does not already hold
|
|
78
|
+
* are stored.
|
|
79
|
+
*/
|
|
80
|
+
messages?: Message[] | ((history: Message[], run: {
|
|
81
|
+
threadId: string;
|
|
82
|
+
runId: string;
|
|
83
|
+
}) => Message[] | Promise<Message[]>);
|
|
84
|
+
/**
|
|
85
|
+
* The input messages the Thread stores for this run, when that is not the
|
|
86
|
+
* new part of the conversation: for example a short request on record
|
|
87
|
+
* while the agent is sent a longer brief. A function receives the Thread
|
|
88
|
+
* and run IDs the lock issued.
|
|
89
|
+
*/
|
|
90
|
+
persistedInputMessages?: Message[] | ((run: {
|
|
91
|
+
threadId: string;
|
|
92
|
+
runId: string;
|
|
93
|
+
}) => Message[]);
|
|
94
|
+
/**
|
|
95
|
+
* The state the run starts from. Omitted, the run starts from the state
|
|
96
|
+
* the Thread last stored, so a fresh agent instance carries on where the
|
|
97
|
+
* Thread's previous run left off; a Thread with no stored state keeps the
|
|
98
|
+
* agent's own.
|
|
99
|
+
*/
|
|
100
|
+
state?: RunAgentInput["state"];
|
|
101
|
+
/** Tools offered to the agent for this run. */
|
|
102
|
+
tools?: RunAgentInput["tools"];
|
|
103
|
+
/** Context passed to the agent for this run. */
|
|
104
|
+
context?: RunAgentInput["context"];
|
|
105
|
+
/** Properties forwarded to the agent with the run. */
|
|
106
|
+
forwardedProps?: RunAgentInput["forwardedProps"];
|
|
107
|
+
/** Answers to the interrupts the Thread's previous run left open. */
|
|
108
|
+
resume?: RunAgentInput["resume"];
|
|
109
|
+
/** Learning Container to assign the Thread to when it is created and locked. */
|
|
110
|
+
learningContainerId?: string;
|
|
111
|
+
/** The app's analytics context, which Intelligence records with the run. */
|
|
112
|
+
analyticsContext?: Record<string, unknown>;
|
|
113
|
+
/**
|
|
114
|
+
* The request the run belongs to. Runs of one request (a follow-up, a
|
|
115
|
+
* resume) send the same ID so the platform counts the request once.
|
|
116
|
+
*/
|
|
117
|
+
requestId?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Receives the agent's events while the run works, for example to show a
|
|
120
|
+
* reply as it is written.
|
|
121
|
+
*/
|
|
122
|
+
subscriber?: AgentSubscriber;
|
|
123
|
+
/**
|
|
124
|
+
* Stops the run when it aborts. The stop is recorded with Intelligence, so
|
|
125
|
+
* the run reads as stopped rather than finished, and the call resolves with
|
|
126
|
+
* `stopped: true` once the runner has ended the run. Use
|
|
127
|
+
* `AbortSignal.timeout()` for a deadline.
|
|
128
|
+
*/
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
/**
|
|
131
|
+
* How long to wait after `signal` or a lost lock stops the run. The stream,
|
|
132
|
+
* lock release and final runner stop wait share one deadline. Past it,
|
|
133
|
+
* release continues in the background for a bounded time, with the lock's
|
|
134
|
+
* TTL as backstop. A run still active at the deadline rejects with
|
|
135
|
+
* `"stop_timeout"`; a lost lock remains `"lock_lost"`.
|
|
136
|
+
* Unset, the call waits for the runner.
|
|
137
|
+
*/
|
|
138
|
+
stopTimeoutMs?: number;
|
|
139
|
+
/**
|
|
140
|
+
* Called once, as soon as the Thread's lock is lost and before the run is
|
|
141
|
+
* stopped, so a caller can tell this stop from one somebody else made. The
|
|
142
|
+
* call still rejects with a {@link ThreadRunError} whose reason is
|
|
143
|
+
* `"lock_lost"` once the run has ended.
|
|
144
|
+
*/
|
|
145
|
+
onLockLost?: (error: unknown) => void;
|
|
146
|
+
}
|
|
147
|
+
/** What a finished {@link runAgentInThread} call returns. */
|
|
148
|
+
interface RunAgentInThreadResult {
|
|
149
|
+
/** The Thread the run belongs to. */
|
|
150
|
+
threadId: string;
|
|
151
|
+
/** The run, as Intelligence issued it with the lock. */
|
|
152
|
+
runId: string;
|
|
153
|
+
/** The messages the run added to the conversation, in order. */
|
|
154
|
+
newMessages: Message[];
|
|
155
|
+
/**
|
|
156
|
+
* True when `signal` stopped the run before it finished on its own. A run
|
|
157
|
+
* stopped before it started (while the Thread was being created, its lock
|
|
158
|
+
* taken, or the run prepared) added no messages.
|
|
159
|
+
*/
|
|
160
|
+
stopped: boolean;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Run an agent in an Intelligence Thread with no client attached: a
|
|
164
|
+
* scheduled run, a hand-off from another agent, or a run that resumes once a
|
|
165
|
+
* person answers. The run is stored in the Thread as a browser's run is, so
|
|
166
|
+
* the person sees it when they open the Thread.
|
|
167
|
+
*
|
|
168
|
+
* The Thread is created if it does not exist. The run holds the Thread's
|
|
169
|
+
* lock from before its first event until the runner's stream has ended,
|
|
170
|
+
* renews it while the agent works, and releases it on every path, so a run
|
|
171
|
+
* never leaves its Thread locked. A Thread another run holds refuses the call
|
|
172
|
+
* with a `PlatformRequestError` whose status is 409, before anything runs.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { runAgentInThread } from "@copilotkit/runtime";
|
|
177
|
+
* import { BuiltInAgent } from "@copilotkit/runtime/agent";
|
|
178
|
+
* import { openaiText } from "@tanstack/ai-openai";
|
|
179
|
+
*
|
|
180
|
+
* const { newMessages } = await runAgentInThread({
|
|
181
|
+
* runtime,
|
|
182
|
+
* agent: new BuiltInAgent({ model: openaiText("gpt-5.4") }),
|
|
183
|
+
* agentId: "assistant",
|
|
184
|
+
* threadId,
|
|
185
|
+
* userId,
|
|
186
|
+
* messages: [
|
|
187
|
+
* { id: crypto.randomUUID(), role: "user", content: "Summarise today" },
|
|
188
|
+
* ],
|
|
189
|
+
* signal: AbortSignal.timeout(5 * 60_000),
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @throws A `PlatformRequestError` when Intelligence refuses the Thread or
|
|
194
|
+
* its lock; a {@link ThreadRunError} when the run fails, loses its lock, or
|
|
195
|
+
* does not end within `stopTimeoutMs` of being stopped; `signal.reason`
|
|
196
|
+
* when `signal` is already aborted, before anything is sent.
|
|
197
|
+
*/
|
|
198
|
+
declare function runAgentInThread(options: RunAgentInThreadOptions): Promise<RunAgentInThreadResult>;
|
|
199
|
+
//#endregion
|
|
200
|
+
export { RunAgentInThreadOptions, RunAgentInThreadResult, RunAgentInThreadRuntime, ThreadRunError, ThreadRunErrorReason, runAgentInThread };
|
|
201
|
+
//# sourceMappingURL=thread-run.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread-run.d.cts","names":[],"sources":["../../../src/runtime/core/thread-run.ts"],"mappings":";;;;;;;KAyXY,oBAAA;;;;;;cAaC,cAAA,SAAuB,KAAA;EAAA,SACzB,MAAA,EAAQ,oBAAA;EA+Fb;EAAA,SA7FK,QAAA;EA8FsB;EAAA,SA5FtB,KAAA;EA4F0C;EAAA,SA1F1C,IAAA;cAGP,MAAA,EAAQ,oBAAA,EACR,OAAA,UACA,OAAA;IACE,QAAA;IACA,KAAA;IACA,IAAA;IACA,KAAA;EAAA;AAAA;;;;;;UAoBW,uBAAA;EA0Ff;EAxFA,YAAA,GAAe,sBAAA;EA+Ff;;;;;;EAxFA,MAAA,GAAS,WAAA;EA4GM;EA1Gf,cAAA;;EAEA,4BAAA;EA0GA;EAxGA,aAAA;AAAA;;UAIe,uBAAA;EA8GR;EA5GP,OAAA,EAAS,uBAAA;EAmJW;;;;;EA7IpB,KAAA,EAAO,aAAA;EA+IC;EA7IR,OAAA;EA4IS;EA1IT,QAAA;EA2IC;EAzID,MAAA;EAyI+B;;;;EApI/B,KAAA;;;;;;;;;EASA,QAAA,GACI,OAAA,OAEE,OAAA,EAAS,OAAA,IACT,GAAA;IAAO,QAAA;IAAkB,KAAA;EAAA,MACtB,OAAA,KAAY,OAAA,CAAQ,OAAA;;;;;;;EAO7B,sBAAA,GACI,OAAA,OACE,GAAA;IAAO,QAAA;IAAkB,KAAA;EAAA,MAAoB,OAAA;;;;;;;EAOnD,KAAA,GAAQ,aAAA;;EAER,KAAA,GAAQ,aAAA;;EAER,OAAA,GAAU,aAAA;;EAEV,cAAA,GAAiB,aAAA;;EAEjB,MAAA,GAAS,aAAA;;EAET,mBAAA;;EAEA,gBAAA,GAAmB,MAAA;;;;;EAKnB,SAAA;;;;;EAKA,UAAA,GAAa,eAAA;;;;;;;EAOb,MAAA,GAAS,WAAA;;;;;;;;;EAST,aAAA;;;;;;;EAOA,UAAA,IAAc,KAAA;AAAA;;UAIC,sBAAA;;EAEf,QAAA;;EAEA,KAAA;;EAEA,WAAA,EAAa,OAAA;;;;;;EAMb,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuCoB,gBAAA,CACpB,OAAA,EAAS,uBAAA,GACR,OAAA,CAAQ,sBAAA"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { AgentRunner } from "../runner/agent-runner.mjs";
|
|
2
|
+
import "../intelligence-platform/index.mjs";
|
|
3
|
+
import { CopilotKitIntelligence } from "../intelligence-platform/client.mjs";
|
|
4
|
+
import { AbstractAgent, AgentSubscriber, BaseEvent, Message, RunAgentInput } from "@ag-ui/client";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime/core/thread-run.d.ts
|
|
7
|
+
/** Why a {@link runAgentInThread} call failed after its run was started. */
|
|
8
|
+
type ThreadRunErrorReason = /** The agent ended the run with a `RUN_ERROR`. */"run_failed" /** The Thread's lock could not be renewed, so the run was stopped. */ | "lock_lost" /** The run was stopped and did not end within `stopTimeoutMs`. */ | "stop_timeout";
|
|
9
|
+
/**
|
|
10
|
+
* A run that {@link runAgentInThread} started and that did not finish.
|
|
11
|
+
* The runtime no longer renews its lock. If the stop deadline expired,
|
|
12
|
+
* a bounded release continues in the background, with the TTL as backstop.
|
|
13
|
+
*/
|
|
14
|
+
declare class ThreadRunError extends Error {
|
|
15
|
+
readonly reason: ThreadRunErrorReason;
|
|
16
|
+
/** The Thread the run belongs to. */
|
|
17
|
+
readonly threadId: string;
|
|
18
|
+
/** The run, as Intelligence issued it with the lock. */
|
|
19
|
+
readonly runId: string;
|
|
20
|
+
/** The `RUN_ERROR` event's `code`, for a failed run that sent one. */
|
|
21
|
+
readonly code?: string;
|
|
22
|
+
constructor(reason: ThreadRunErrorReason, message: string, details: {
|
|
23
|
+
threadId: string;
|
|
24
|
+
runId: string;
|
|
25
|
+
code?: string;
|
|
26
|
+
cause?: unknown;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The runtime a {@link runAgentInThread} call runs through. A `CopilotKitRuntime`
|
|
31
|
+
* configured with `intelligence` is one; so is an Intelligence client on its
|
|
32
|
+
* own, `{ intelligence }`.
|
|
33
|
+
*/
|
|
34
|
+
interface RunAgentInThreadRuntime {
|
|
35
|
+
/** The Intelligence client that holds the Thread and issues its lock. */
|
|
36
|
+
intelligence?: CopilotKitIntelligence;
|
|
37
|
+
/**
|
|
38
|
+
* The runner that streams the run's events to Intelligence. Omitted, the
|
|
39
|
+
* run uses one Intelligence runner per client, created on first use and
|
|
40
|
+
* shared by every later run, so a Thread already running in this process
|
|
41
|
+
* is refused at once.
|
|
42
|
+
*/
|
|
43
|
+
runner?: AgentRunner;
|
|
44
|
+
/** Lock TTL in seconds. @default 20 */
|
|
45
|
+
lockTtlSeconds?: number;
|
|
46
|
+
/** Interval in seconds at which the lock is renewed. @default 15 */
|
|
47
|
+
lockHeartbeatIntervalSeconds?: number;
|
|
48
|
+
/** Custom key prefix for the thread lock. */
|
|
49
|
+
lockKeyPrefix?: string;
|
|
50
|
+
}
|
|
51
|
+
/** Options for {@link runAgentInThread}. */
|
|
52
|
+
interface RunAgentInThreadOptions {
|
|
53
|
+
/** The runtime to run through, usually your `CopilotKitRuntime`. */
|
|
54
|
+
runtime: RunAgentInThreadRuntime;
|
|
55
|
+
/**
|
|
56
|
+
* The agent to run. It is run in place: its messages are replaced by the
|
|
57
|
+
* run's conversation and the run's output is applied to it, so pass an
|
|
58
|
+
* instance no other run is using.
|
|
59
|
+
*/
|
|
60
|
+
agent: AbstractAgent;
|
|
61
|
+
/** The agent's ID, as the Thread records it. */
|
|
62
|
+
agentId: string;
|
|
63
|
+
/** The Thread to run in. It is created if it does not exist yet. */
|
|
64
|
+
threadId: string;
|
|
65
|
+
/** The user the Thread belongs to and the run acts for. */
|
|
66
|
+
userId: string;
|
|
67
|
+
/**
|
|
68
|
+
* The run's ID. Intelligence may issue a different one with the lock; the
|
|
69
|
+
* result carries the one it issued. @default a random UUID
|
|
70
|
+
*/
|
|
71
|
+
runId?: string;
|
|
72
|
+
/**
|
|
73
|
+
* The run's input. An array is this run's new messages, appended to the
|
|
74
|
+
* Thread's stored history. A function receives that history, converted to
|
|
75
|
+
* AG-UI messages, and the Thread and run IDs the lock issued, and returns
|
|
76
|
+
* the whole conversation the agent sees. Unless `persistedInputMessages`
|
|
77
|
+
* says otherwise, only messages whose IDs the Thread does not already hold
|
|
78
|
+
* are stored.
|
|
79
|
+
*/
|
|
80
|
+
messages?: Message[] | ((history: Message[], run: {
|
|
81
|
+
threadId: string;
|
|
82
|
+
runId: string;
|
|
83
|
+
}) => Message[] | Promise<Message[]>);
|
|
84
|
+
/**
|
|
85
|
+
* The input messages the Thread stores for this run, when that is not the
|
|
86
|
+
* new part of the conversation: for example a short request on record
|
|
87
|
+
* while the agent is sent a longer brief. A function receives the Thread
|
|
88
|
+
* and run IDs the lock issued.
|
|
89
|
+
*/
|
|
90
|
+
persistedInputMessages?: Message[] | ((run: {
|
|
91
|
+
threadId: string;
|
|
92
|
+
runId: string;
|
|
93
|
+
}) => Message[]);
|
|
94
|
+
/**
|
|
95
|
+
* The state the run starts from. Omitted, the run starts from the state
|
|
96
|
+
* the Thread last stored, so a fresh agent instance carries on where the
|
|
97
|
+
* Thread's previous run left off; a Thread with no stored state keeps the
|
|
98
|
+
* agent's own.
|
|
99
|
+
*/
|
|
100
|
+
state?: RunAgentInput["state"];
|
|
101
|
+
/** Tools offered to the agent for this run. */
|
|
102
|
+
tools?: RunAgentInput["tools"];
|
|
103
|
+
/** Context passed to the agent for this run. */
|
|
104
|
+
context?: RunAgentInput["context"];
|
|
105
|
+
/** Properties forwarded to the agent with the run. */
|
|
106
|
+
forwardedProps?: RunAgentInput["forwardedProps"];
|
|
107
|
+
/** Answers to the interrupts the Thread's previous run left open. */
|
|
108
|
+
resume?: RunAgentInput["resume"];
|
|
109
|
+
/** Learning Container to assign the Thread to when it is created and locked. */
|
|
110
|
+
learningContainerId?: string;
|
|
111
|
+
/** The app's analytics context, which Intelligence records with the run. */
|
|
112
|
+
analyticsContext?: Record<string, unknown>;
|
|
113
|
+
/**
|
|
114
|
+
* The request the run belongs to. Runs of one request (a follow-up, a
|
|
115
|
+
* resume) send the same ID so the platform counts the request once.
|
|
116
|
+
*/
|
|
117
|
+
requestId?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Receives the agent's events while the run works, for example to show a
|
|
120
|
+
* reply as it is written.
|
|
121
|
+
*/
|
|
122
|
+
subscriber?: AgentSubscriber;
|
|
123
|
+
/**
|
|
124
|
+
* Stops the run when it aborts. The stop is recorded with Intelligence, so
|
|
125
|
+
* the run reads as stopped rather than finished, and the call resolves with
|
|
126
|
+
* `stopped: true` once the runner has ended the run. Use
|
|
127
|
+
* `AbortSignal.timeout()` for a deadline.
|
|
128
|
+
*/
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
/**
|
|
131
|
+
* How long to wait after `signal` or a lost lock stops the run. The stream,
|
|
132
|
+
* lock release and final runner stop wait share one deadline. Past it,
|
|
133
|
+
* release continues in the background for a bounded time, with the lock's
|
|
134
|
+
* TTL as backstop. A run still active at the deadline rejects with
|
|
135
|
+
* `"stop_timeout"`; a lost lock remains `"lock_lost"`.
|
|
136
|
+
* Unset, the call waits for the runner.
|
|
137
|
+
*/
|
|
138
|
+
stopTimeoutMs?: number;
|
|
139
|
+
/**
|
|
140
|
+
* Called once, as soon as the Thread's lock is lost and before the run is
|
|
141
|
+
* stopped, so a caller can tell this stop from one somebody else made. The
|
|
142
|
+
* call still rejects with a {@link ThreadRunError} whose reason is
|
|
143
|
+
* `"lock_lost"` once the run has ended.
|
|
144
|
+
*/
|
|
145
|
+
onLockLost?: (error: unknown) => void;
|
|
146
|
+
}
|
|
147
|
+
/** What a finished {@link runAgentInThread} call returns. */
|
|
148
|
+
interface RunAgentInThreadResult {
|
|
149
|
+
/** The Thread the run belongs to. */
|
|
150
|
+
threadId: string;
|
|
151
|
+
/** The run, as Intelligence issued it with the lock. */
|
|
152
|
+
runId: string;
|
|
153
|
+
/** The messages the run added to the conversation, in order. */
|
|
154
|
+
newMessages: Message[];
|
|
155
|
+
/**
|
|
156
|
+
* True when `signal` stopped the run before it finished on its own. A run
|
|
157
|
+
* stopped before it started (while the Thread was being created, its lock
|
|
158
|
+
* taken, or the run prepared) added no messages.
|
|
159
|
+
*/
|
|
160
|
+
stopped: boolean;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Run an agent in an Intelligence Thread with no client attached: a
|
|
164
|
+
* scheduled run, a hand-off from another agent, or a run that resumes once a
|
|
165
|
+
* person answers. The run is stored in the Thread as a browser's run is, so
|
|
166
|
+
* the person sees it when they open the Thread.
|
|
167
|
+
*
|
|
168
|
+
* The Thread is created if it does not exist. The run holds the Thread's
|
|
169
|
+
* lock from before its first event until the runner's stream has ended,
|
|
170
|
+
* renews it while the agent works, and releases it on every path, so a run
|
|
171
|
+
* never leaves its Thread locked. A Thread another run holds refuses the call
|
|
172
|
+
* with a `PlatformRequestError` whose status is 409, before anything runs.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* import { runAgentInThread } from "@copilotkit/runtime";
|
|
177
|
+
* import { BuiltInAgent } from "@copilotkit/runtime/agent";
|
|
178
|
+
* import { openaiText } from "@tanstack/ai-openai";
|
|
179
|
+
*
|
|
180
|
+
* const { newMessages } = await runAgentInThread({
|
|
181
|
+
* runtime,
|
|
182
|
+
* agent: new BuiltInAgent({ model: openaiText("gpt-5.4") }),
|
|
183
|
+
* agentId: "assistant",
|
|
184
|
+
* threadId,
|
|
185
|
+
* userId,
|
|
186
|
+
* messages: [
|
|
187
|
+
* { id: crypto.randomUUID(), role: "user", content: "Summarise today" },
|
|
188
|
+
* ],
|
|
189
|
+
* signal: AbortSignal.timeout(5 * 60_000),
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @throws A `PlatformRequestError` when Intelligence refuses the Thread or
|
|
194
|
+
* its lock; a {@link ThreadRunError} when the run fails, loses its lock, or
|
|
195
|
+
* does not end within `stopTimeoutMs` of being stopped; `signal.reason`
|
|
196
|
+
* when `signal` is already aborted, before anything is sent.
|
|
197
|
+
*/
|
|
198
|
+
declare function runAgentInThread(options: RunAgentInThreadOptions): Promise<RunAgentInThreadResult>;
|
|
199
|
+
//#endregion
|
|
200
|
+
export { RunAgentInThreadOptions, RunAgentInThreadResult, RunAgentInThreadRuntime, ThreadRunError, ThreadRunErrorReason, runAgentInThread };
|
|
201
|
+
//# sourceMappingURL=thread-run.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread-run.d.mts","names":[],"sources":["../../../src/runtime/core/thread-run.ts"],"mappings":";;;;;;;KAyXY,oBAAA;;;;;;cAaC,cAAA,SAAuB,KAAA;EAAA,SACzB,MAAA,EAAQ,oBAAA;EA+Fb;EAAA,SA7FK,QAAA;EA8FsB;EAAA,SA5FtB,KAAA;EA4F0C;EAAA,SA1F1C,IAAA;cAGP,MAAA,EAAQ,oBAAA,EACR,OAAA,UACA,OAAA;IACE,QAAA;IACA,KAAA;IACA,IAAA;IACA,KAAA;EAAA;AAAA;;;;;;UAoBW,uBAAA;EA0Ff;EAxFA,YAAA,GAAe,sBAAA;EA+Ff;;;;;;EAxFA,MAAA,GAAS,WAAA;EA4GM;EA1Gf,cAAA;;EAEA,4BAAA;EA0GA;EAxGA,aAAA;AAAA;;UAIe,uBAAA;EA8GR;EA5GP,OAAA,EAAS,uBAAA;EAmJW;;;;;EA7IpB,KAAA,EAAO,aAAA;EA+IC;EA7IR,OAAA;EA4IS;EA1IT,QAAA;EA2IC;EAzID,MAAA;EAyI+B;;;;EApI/B,KAAA;;;;;;;;;EASA,QAAA,GACI,OAAA,OAEE,OAAA,EAAS,OAAA,IACT,GAAA;IAAO,QAAA;IAAkB,KAAA;EAAA,MACtB,OAAA,KAAY,OAAA,CAAQ,OAAA;;;;;;;EAO7B,sBAAA,GACI,OAAA,OACE,GAAA;IAAO,QAAA;IAAkB,KAAA;EAAA,MAAoB,OAAA;;;;;;;EAOnD,KAAA,GAAQ,aAAA;;EAER,KAAA,GAAQ,aAAA;;EAER,OAAA,GAAU,aAAA;;EAEV,cAAA,GAAiB,aAAA;;EAEjB,MAAA,GAAS,aAAA;;EAET,mBAAA;;EAEA,gBAAA,GAAmB,MAAA;;;;;EAKnB,SAAA;;;;;EAKA,UAAA,GAAa,eAAA;;;;;;;EAOb,MAAA,GAAS,WAAA;;;;;;;;;EAST,aAAA;;;;;;;EAOA,UAAA,IAAc,KAAA;AAAA;;UAIC,sBAAA;;EAEf,QAAA;;EAEA,KAAA;;EAEA,WAAA,EAAa,OAAA;;;;;;EAMb,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuCoB,gBAAA,CACpB,OAAA,EAAS,uBAAA,GACR,OAAA,CAAQ,sBAAA"}
|