Context API
assistant-ui components and APIs use React Context to store information and interact with the runtime.
The context is split into four hierarchies:
- Assistant Context: Wraps your entire app. An app can contain multiple threads.
- Thread Context: Provides access to the thread state. A thread can contain multiple messages.
- Message Context: Provides access to the message state. A message can contain multiple content parts.
- Content Part Context: Provides access to the content part state.
Assistant Context
useAssistantActions
AssistantActionsState
switchToThread:
Switch to a new thread.
getRuntime:
Get the current runtime.
useToolUIs
AssistantToolUIsState
getToolUI:
Gets the current tool UI for a given tool name.
ToolCallContentPartProps<TArgs, TResult>
part?:
The tool call content part.
status?:
The tool call status.
addResult?:
Adds a result to the tool call.
setToolUI:
Sets the tool UI.
Thread Context
useThread
ThreadState
isRunning:
Whether the thread is running.
isDisabled:
Whether the thread is disabled.
useThreadMessages
ThreadMessagesState
messages:
The messages in the thread.
useThreadActions
ThreadActionsState
getBranches:
A function to get the branches for a message.
switchToBranch:
A function to switch to a branch.
append:
A function to append a message to the thread.
startRun:
A function to start a run.
cancelRun:
A function to cancel a run.
addToolResult:
A function to add a tool result.
useThreadRuntime
useThreadComposer
ComposerState
text:
The current text of the composer.
setText:
A function to set the text of the composer.
attachments:
The current attachments of the composer.
addAttachment:
A function to add an attachment to the composer.
removeAttachment:
A function to remove an attachment from the composer.
reset:
A function to reset the composer.
canCancel:
Whether the composer can be canceled.
isEditing:
Whether the composer is in edit mode.
send:
A function to send the message.
cancel:
A function to cancel the run.
focus:
A function to focus the composer.
onFocus:
A function to subscribe to focus events.
useThreadViewport
ThreadViewportState
isAtBottom:
Whether the thread is at the bottom.
scrollToBottom:
A function to scroll to the bottom.
onScrollToBottom:
A function to subscribe to scroll to bottom events.
Message Context
useMessage
MessageState
message:
The current message.
parentId:
The parent message id.
branches:
The branches for the message.
isLast:
Whether the message is the last in the thread.
useMessageUtils
MessageUtilsState
isCopied:
Whether the message is copied.
setIsCopied:
A function to set the is copied.
isHovering:
Whether the message is being hovered.
setIsHovering:
A function to set the is hovering.
isSpeaking:
Whether the message is currently being spoken.
stopSpeaking:
A function to stop the message from being spoken.
addUtterance:
A function to add a speech utterance.
useEditComposer
EditComposerState
text:
The current text of the composer.
setText:
A function to set the text of the composer.
attachments:
The current attachments of the composer.
addAttachment:
A function to add an attachment to the composer.
removeAttachment:
A function to remove an attachment from the composer.
reset:
A function to reset the composer.
canCancel:
Whether the composer can be canceled.
isEditing:
Whether the composer is in edit mode.
edit:
A function to enter edit mode.
send:
A function to send the message.
cancel:
A function to exit the edit mode.
Content Part Context
useContentPart
ContentPartState
part:
The current content part.
status:
The current content part status.
MessageStatus
type:
The status.
finish-reason?:
The finish reason if the status is 'incomplete'.
error?:
The error object if the status is 'error'.
Composer Context
Grabs the nearest composer state (either the edit composer or the thread's new message composer).
useComposer
Attachment Context
Grabs the attachment state (either the composer or message attachment).
useAttachment
useComposerAttachment
(Composer)
ComposerAttachmentState
attachment:
The current composer attachment.
useMessageAttachment
(Message)
MessageAttachmentState
attachment:
The current message attachment.