forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-threads.tsx
More file actions
325 lines (304 loc) · 9.9 KB
/
Copy pathuse-threads.tsx
File metadata and controls
325 lines (304 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { useCopilotKit } from "../context";
import {
CopilotKitCoreRuntimeConnectionStatus,
ɵcreateThreadStore,
ɵselectThreads,
ɵselectThreadsError,
ɵselectThreadsIsLoading,
ɵselectHasNextPage,
ɵselectIsFetchingNextPage,
type ɵThreadRuntimeContext,
type ɵThreadStore,
} from "@copilotkit/core";
import {
useCallback,
useEffect,
useMemo,
useState,
useSyncExternalStore,
} from "react";
/**
* A conversation thread managed by the Intelligence platform.
*
* Each thread has a unique `id`, an optional human-readable `name`, and
* timestamp fields tracking creation and update times.
*/
export interface Thread {
id: string;
agentId: string;
name: string | null;
archived: boolean;
createdAt: string;
updatedAt: string;
/**
* ISO-8601 timestamp of the most recent agent run on this thread. Absent
* when the thread has never been run. Prefer this over `updatedAt` for
* user-facing "last activity" displays — it is not bumped by metadata-only
* actions like rename or archive.
*/
lastRunAt?: string;
}
/**
* Configuration for the {@link useThreads} hook.
*
* Thread operations are scoped to the runtime-authenticated user and the
* provided agent on the Intelligence platform.
*/
export interface UseThreadsInput {
/** The ID of the agent whose threads to list and manage. */
agentId: string;
/** When `true`, archived threads are included in the list. Defaults to `false`. */
includeArchived?: boolean;
/** Maximum number of threads to fetch per page. When set, enables cursor-based pagination. */
limit?: number;
}
/**
* Return value of the {@link useThreads} hook.
*
* The `threads` array is kept in sync with the platform via a realtime
* WebSocket subscription (when available) and is sorted most-recently-updated
* first. Mutations reject with an `Error` if the platform request fails.
*/
export interface UseThreadsResult {
/**
* Threads for the current user/agent pair, sorted by most recently
* updated first. Updated in realtime when the platform pushes metadata
* events. Includes archived threads only when `includeArchived` is set.
*/
threads: Thread[];
/**
* `true` while the initial thread list is being fetched from the platform.
* Subsequent realtime updates do not re-enter the loading state.
*/
isLoading: boolean;
/**
* The most recent error from fetching threads or executing a mutation,
* or `null` when there is no error. Reset to `null` on the next
* successful fetch.
*/
error: Error | null;
/**
* `true` when there are more threads available to fetch via
* {@link fetchMoreThreads}. Only meaningful when `limit` is set.
*/
hasMoreThreads: boolean;
/**
* `true` while a subsequent page of threads is being fetched.
*/
isFetchingMoreThreads: boolean;
/**
* Fetch the next page of threads. No-op when {@link hasMoreThreads} is
* `false` or a fetch is already in progress.
*/
fetchMoreThreads: () => void;
/**
* Rename a thread on the platform.
* Resolves when the server confirms the update; rejects on failure.
*/
renameThread: (threadId: string, name: string) => Promise<void>;
/**
* Archive a thread on the platform.
* Archived threads are excluded from subsequent list results.
* Resolves when the server confirms the update; rejects on failure.
*/
archiveThread: (threadId: string) => Promise<void>;
/**
* Permanently delete a thread from the platform.
* This is irreversible. Resolves when the server confirms deletion;
* rejects on failure.
*/
deleteThread: (threadId: string) => Promise<void>;
}
function useThreadStoreSelector<T>(
store: ɵThreadStore,
selector: (state: ReturnType<ɵThreadStore["getState"]>) => T,
): T {
return useSyncExternalStore(
useCallback(
(onStoreChange) => {
const subscription = store.select(selector).subscribe(onStoreChange);
return () => subscription.unsubscribe();
},
[store, selector],
),
() => selector(store.getState()),
);
}
/**
* React hook for listing and managing Intelligence platform threads.
*
* On mount the hook fetches the thread list for the runtime-authenticated user
* and the given `agentId`. When the Intelligence platform exposes a WebSocket
* URL, it also opens a realtime subscription so the `threads` array stays
* current without polling — thread creates, renames, archives, and deletes
* from any client are reflected immediately.
*
* Mutation methods (`renameThread`, `archiveThread`, `deleteThread`) return
* promises that resolve once the platform confirms the operation and reject
* with an `Error` on failure.
*
* @param input - Agent identifier and optional list controls.
* @returns Thread list state and stable mutation callbacks.
*
* @example
* ```tsx
* import { useThreads } from "@copilotkit/react-core";
*
* function ThreadList() {
* const { threads, isLoading, renameThread, deleteThread } = useThreads({
* agentId: "agent-1",
* });
*
* if (isLoading) return <p>Loading…</p>;
*
* return (
* <ul>
* {threads.map((t) => (
* <li key={t.id}>
* {t.name ?? "Untitled"}
* <button onClick={() => renameThread(t.id, "New name")}>Rename</button>
* <button onClick={() => deleteThread(t.id)}>Delete</button>
* </li>
* ))}
* </ul>
* );
* }
* ```
*/
export function useThreads({
agentId,
includeArchived,
limit,
}: UseThreadsInput): UseThreadsResult {
const { copilotkit } = useCopilotKit();
const [store] = useState(() =>
ɵcreateThreadStore({
fetch: globalThis.fetch,
}),
);
const coreThreads = useThreadStoreSelector(store, ɵselectThreads);
const threads: Thread[] = useMemo(
() =>
coreThreads.map(
({ id, agentId, name, archived, createdAt, updatedAt, lastRunAt }) => ({
id,
agentId,
name,
archived,
createdAt,
updatedAt,
...(lastRunAt !== undefined ? { lastRunAt } : {}),
}),
),
[coreThreads],
);
const storeIsLoading = useThreadStoreSelector(store, ɵselectThreadsIsLoading);
const storeError = useThreadStoreSelector(store, ɵselectThreadsError);
const hasMoreThreads = useThreadStoreSelector(store, ɵselectHasNextPage);
const isFetchingMoreThreads = useThreadStoreSelector(
store,
ɵselectIsFetchingNextPage,
);
const headersKey = useMemo(() => {
return JSON.stringify(
Object.entries(copilotkit.headers ?? {}).sort(([left], [right]) =>
left.localeCompare(right),
),
);
}, [copilotkit.headers]);
const runtimeError = useMemo(() => {
if (copilotkit.runtimeUrl) {
return null;
}
return new Error("Runtime URL is not configured");
}, [copilotkit.runtimeUrl]);
// Tracks whether we've dispatched the first real context to the store.
// The store itself starts with `isLoading: false`, so before we dispatch
// consumers would otherwise see an empty, non-loading state (empty-list
// flash). While runtimeUrl is set and we haven't dispatched yet, we
// synthesize `isLoading: true` so the UI keeps its loading indicator until
// the first fetch is in flight (at which point the store's own
// isLoading takes over).
const [hasDispatchedContext, setHasDispatchedContext] = useState(false);
const preConnectLoading = !!copilotkit.runtimeUrl && !hasDispatchedContext;
const isLoading = runtimeError ? false : preConnectLoading || storeIsLoading;
const error = runtimeError ?? storeError;
useEffect(() => {
store.start();
return () => {
store.stop();
};
}, [store]);
// Defer setting the context until the runtime reports Connected. Before
// `/info` resolves we don't know `intelligence.wsUrl`, so dispatching the
// context early would issue a list fetch with `wsUrl: undefined`, then a
// second list fetch (and a `/threads/subscribe`) once the flag lands.
// Waiting lets the hook issue just one `/threads?…` + one `/threads/subscribe`.
//
// When `runtimeUrl` is absent we dispatch `null` to clear the store. For
// transient states (Disconnected/Connecting/Error with a URL still set) we
// leave the previously-dispatched context in place — any in-flight
// realtime subscription or cached thread list stays usable while the
// runtime recovers, and we don't re-trigger a fetch storm on transitions.
const runtimeStatus = copilotkit.runtimeConnectionStatus;
useEffect(() => {
copilotkit.registerThreadStore(agentId, store);
return () => {
copilotkit.unregisterThreadStore(agentId);
};
}, [copilotkit, agentId, store]);
useEffect(() => {
if (!copilotkit.runtimeUrl) {
store.setContext(null);
return;
}
// Wait for /info to land so we can include `wsUrl` in the initial
// context and avoid a redundant second list fetch.
if (runtimeStatus !== CopilotKitCoreRuntimeConnectionStatus.Connected) {
return;
}
const context: ɵThreadRuntimeContext = {
runtimeUrl: copilotkit.runtimeUrl,
headers: { ...copilotkit.headers },
wsUrl: copilotkit.intelligence?.wsUrl,
agentId,
includeArchived,
limit,
};
store.setContext(context);
setHasDispatchedContext(true);
}, [
store,
copilotkit.runtimeUrl,
runtimeStatus,
headersKey,
copilotkit.intelligence?.wsUrl,
agentId,
includeArchived,
limit,
]);
const renameThread = useCallback(
(threadId: string, name: string) => store.renameThread(threadId, name),
[store],
);
const archiveThread = useCallback(
(threadId: string) => store.archiveThread(threadId),
[store],
);
const deleteThread = useCallback(
(threadId: string) => store.deleteThread(threadId),
[store],
);
const fetchMoreThreads = useCallback(() => store.fetchNextPage(), [store]);
return {
threads,
isLoading,
error,
hasMoreThreads,
isFetchingMoreThreads,
fetchMoreThreads,
renameThread,
archiveThread,
deleteThread,
};
}