Runtime for thread state, messages, and viewport management.
useAui (Thread Actions)
Access thread actions via useAui:
import { useAui } from "@assistant-ui/react";
const aui = useAui();
aui.thread().cancelRun();
aui.thread().append({ role: "user", content: [{ type: "text", text: "Hello" }] });ThreadRuntimepath: ThreadRuntimePathThe selector for the thread runtime.
composer: ThreadComposerRuntimeThe thread composer runtime.
getState: () => ThreadStateGets a snapshot of the thread state.
append: (message: CreateAppendMessage) => voidAppend a new message to the thread.
startRun: { (parentId: string | null): void; (config: CreateStartRunConfig): void; }resumeRun: (config: CreateResumeRunConfig) => voidResume a run with the given configuration.
unstable_resumeRununstable: (config: CreateResumeRunConfig) => voidexportExternalState: () => 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.
importExternalState: (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_loadExternalStateunstable: (state: any) => voidLoad external state into the thread.
subscribe: (callback: () => void) => UnsubscribecancelRun: () => voidgetModelContext: () => ModelContextgetModelConfig: () => ModelContextexport: () => ExportedMessageRepositoryimport: (repository: ExportedMessageRepository) => voidreset: (initialMessages?: readonly ThreadMessageLike[] | undefined) => voidReset the thread with optional initial messages.
getMessageByIndex: (idx: number) => MessageRuntimegetMessageById: (messageId: string) => MessageRuntimestopSpeaking: () => voidconnectVoice: () => voiddisconnectVoice: () => voidgetVoiceVolume: () => numbersubscribeVoiceVolume: (callback: () => void) => UnsubscribemuteVoice: () => voidunmuteVoice: () => voidunstable_onunstable: (event: ThreadRuntimeEventType, callback: () => void) => UnsubscribeuseAuiState (Thread State)
Access the thread state reactively:
import { useAuiState } from "@assistant-ui/react";
const isRunning = useAuiState((s) => s.thread.isRunning);
const isLoading = useAuiState((s) => s.thread.isLoading);
const messages = useAuiState((s) => s.thread.messages);ThreadStatethreadId: stringThe thread ID.
metadata: ThreadListItemStateThe thread metadata.
isDisabled: booleanWhether the thread is disabled. Disabled threads cannot receive new messages.
isLoading: booleanWhether the thread is loading its history.
isRunning: booleanWhether the thread is running. A thread is considered running when there is an active stream connection to the backend.
capabilities: RuntimeCapabilitiesThe capabilities of the thread, such as whether the thread supports editing, branch switching, etc.
messages: readonly ThreadMessage[]The messages in the currently selected branch of the thread.
state: ReadonlyJSONValueThe thread state.
suggestions: readonly ThreadSuggestion[]Follow up message suggestions to show the user.
extras: unknownCustom extra information provided by the runtime.
speech: SpeechState | undefinedvoice: VoiceSessionState | undefineduseThreadViewport
Manage thread viewport state (e.g., scrolling):
import { useThreadViewport } from "@assistant-ui/react";
const threadViewport = useThreadViewport();
const isAtBottom = useThreadViewport((m) => m.isAtBottom);ThreadViewportStateisAtBottom: booleanWhether the thread is at the bottom.
scrollToBottom: (config?: { behavior?: ScrollBehavior }) => voidA function to scroll to the bottom.
onScrollToBottom: (callback: ({ behavior }: { behavior: ScrollBehavior }) => void) => UnsubscribeA function to subscribe to scroll to bottom events.
turnAnchor: "top" | "bottom"Controls scroll anchoring: "top" anchors user messages at top, "bottom" is classic behavior.
height: { viewport: number; inset: number; userMessage: number }Raw height values from registered elements: total viewport height, total content inset height (footer, anchor message, etc.), and height of the anchor user message.
registerViewport: () => SizeHandleRegister a viewport and get a handle to update its height.
registerContentInset: () => SizeHandleRegister a content inset (footer, anchor message, etc.) and get a handle to update its height.
registerUserMessageHeight: () => SizeHandleRegister the anchor user message height.