Runtime for thread state, messages, and viewport management.
useThreadRuntime
Get the thread runtime object:
import { useThreadRuntime } from "@assistant-ui/react";
const thread = useThreadRuntime();ThreadRuntimepathrequired: ThreadRuntimePathThe selector for the thread runtime.
composerrequired: ThreadComposerRuntimeThe thread composer runtime.
getStaterequired: () => ThreadStateGets a snapshot of the thread state.
appendrequired: (message: CreateAppendMessage) => voidAppend a new message to the thread.
startRunrequired: { (parentId: string | null): void; (config: CreateStartRunConfig): void; }unstable_resumeRunrequired: (config: CreateResumeRunConfig) => voidResume a run with the given configuration.
exportExternalStaterequired: () => anyExport the thread state in the external store format. For AI SDK runtimes, this returns the AI SDK message format. For other runtimes, this may return different formats or throw an error.
importExternalStaterequired: (state: any) => voidImport thread state from the external store format. For AI SDK runtimes, this accepts AI SDK messages. For other runtimes, this may accept different formats or throw an error.
unstable_loadExternalStaterequired: (state: any) => voidLoad external state into the thread.
subscriberequired: (callback: () => void) => UnsubscribecancelRunrequired: () => voidgetModelContextrequired: () => ModelContextgetModelConfigrequired: () => ModelContextexportrequired: () => ExportedMessageRepositoryimportrequired: (repository: ExportedMessageRepository) => voidresetrequired: (initialMessages?: readonly ThreadMessageLike[] | undefined) => voidReset the thread with optional initial messages.
getMessageByIndexrequired: (idx: number) => MessageRuntimegetMessageByIdrequired: (messageId: string) => MessageRuntimestopSpeakingrequired: () => voidunstable_onrequired: (event: ThreadRuntimeEventType, callback: () => void) => UnsubscribeuseThread
Access the thread state directly:
import { useThread } from "@assistant-ui/react";
const thread = useThread();
const isRunning = useThread((m) => m.isRunning);
const isLoading = useThread((m) => m.isLoading);ThreadStatethreadIdrequired: stringThe thread ID.
metadatarequired: ThreadListItemStateThe thread metadata.
isDisabledrequired: booleanWhether the thread is disabled. Disabled threads cannot receive new messages.
isLoadingrequired: booleanWhether the thread is loading its history.
isRunningrequired: booleanWhether the thread is running. A thread is considered running when there is an active stream connection to the backend.
capabilitiesrequired: RuntimeCapabilitiesThe capabilities of the thread, such as whether the thread supports editing, branch switching, etc.
messagesrequired: readonly ThreadMessage[]The messages in the currently selected branch of the thread.
staterequired: ReadonlyJSONValueThe thread state.
suggestionsrequired: readonly ThreadSuggestion[]Follow up message suggestions to show the user.
extrasrequired: unknownCustom extra information provided by the runtime.
speechrequired: SpeechState | undefineduseThreadViewport
Manage thread viewport state (e.g., scrolling):
import { useThreadViewport } from "@assistant-ui/react";
const threadViewport = useThreadViewport();
const isAtBottom = useThreadViewport((m) => m.isAtBottom);ThreadViewportStateisAtBottomrequired: booleanWhether the thread is at the bottom.
scrollToBottomrequired: () => voidA function to scroll to the bottom.
onScrollToBottomrequired: (callback: () => void) => UnsubscribeA function to subscribe to scroll to bottom events.