| title | useThreads |
|---|---|
| description | React hook for listing, managing, and syncing conversation threads with useThreads. Rename, archive, delete, and paginate threads with realtime updates via WebSocket. |
useThreads is a React hook for managing conversation threads on the Enterprise Intelligence Platform. It fetches the thread list for a given agent, keeps it synchronized in realtime via WebSocket, and exposes mutation methods for renaming, archiving, and deleting threads.
Threads are sorted by most recently updated first. The hook supports cursor-based pagination when a limit is provided.
import { useThreads } from "@copilotkit/react-native";
function useThreads(input: UseThreadsInput): UseThreadsResultimport { useThreads } from "@copilotkit/react-native"; // [!code highlight]
import { FlatList, Text, TouchableOpacity, View } from "react-native";
function ThreadList() {
const {
threads,
isLoading,
renameThread,
archiveThread,
deleteThread,
} = useThreads({ agentId: "my-agent" }); // [!code highlight]
if (isLoading) {
return (
<View>
<Text>Loading threads...</Text>
</View>
);
}
return (
<FlatList
data={threads}
keyExtractor={(thread) => thread.id}
renderItem={({ item: thread }) => (
<View>
<Text>{thread.name ?? "Untitled"}</Text>
<TouchableOpacity onPress={() => renameThread(thread.id, "New name")}>
<Text>Rename</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => archiveThread(thread.id)}>
<Text>Archive</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => deleteThread(thread.id)}>
<Text>Delete</Text>
</TouchableOpacity>
</View>
)}
/>
);
}- On mount, fetches the thread list and establishes a realtime WebSocket subscription.
- Thread creates, renames, archives, and deletes from any client are reflected immediately without polling.
- All mutation methods use pessimistic updates: the UI updates only after the server confirms the operation via WebSocket, not immediately on dispatch. Promises resolve on confirmation (15-second timeout) and reject on failure.
- The
errorstate updates with the most recent error from any operation. - The
threadsarray stays sorted byupdatedAtdescending (most recent first). - New threads are automatically named by the LLM after their first run (a short title of 2 to 5 words). This is configurable via
generateThreadNameson the runtime.
- Thread architecture: Threads & Persistence Architecture covers the event replay model and WebSocket sync
- Step-by-step guide: Threads walks you through setting up thread management in your app
- React (V2) reference:
useThreads, the webuseThreadsthis hook mirrors