Runtime for programmatically controlling the message composer.
The composer runtime allows you to view or edit anything related to how new information is added and sent. For instance you can use the composer runtime to read the state, add attachments, update text, send a message, etc.
useAui (Composer Actions)
Grabs the nearest composer (whether it’s the edit composer or the thread’s composer) via useAui:
// Example
import { useAui } from "@assistant-ui/react";
const aui = useAui();
// set the text
aui.composer().setText("Hello from the composer");
// send whatever is in the composer
aui.composer().send();
// get the current text in the composer state
const composerState = aui.composer().getState();
const currentText = composerState.text;ComposerRuntimepath: ComposerRuntimePathtype: "thread" | "edit"getState: () => ComposerStateGet the current state of the composer. Includes any data that has been added to the composer.
addAttachment: (fileOrAttachment: File | CreateAttachment) => Promise<void>Add an attachment to the composer. Accepts either a standard File object (processed through the AttachmentAdapter) or a CreateAttachment descriptor for external-source attachments (URLs, API data, CMS references) that bypasses the adapter entirely.
setText: (text: string) => voidSet the text of the composer.
setRole: (role: "system" | "user" | "assistant") => voidSet the role of the composer. For instance, if you'd like a specific message to have the 'assistant' role, you can do so here.
setRunConfig: (runConfig: RunConfig) => voidSet the run config of the composer. This is used to send custom configuration data to the model. Within your backend, you can use the `runConfig` object. Example: ```ts composerRuntime.setRunConfig({ custom: { customField: "customValue" } }); ```
reset: () => Promise<void>Reset the composer. This will clear the entire state of the composer, including all text and attachments.
clearAttachments: () => Promise<void>Clear all attachments from the composer.
send: (options?: SendOptions | undefined) => voidSend a message. This will send whatever text or attachments are in the composer.
cancel: () => voidCancel the current run. In edit mode, this will exit edit mode.
subscribe: (callback: () => void) => UnsubscribeListens for changes to the composer state.
getAttachmentByIndex: (idx: number) => AttachmentRuntimeGet an attachment by index.
startDictation: () => voidStart dictation to convert voice to text input. Requires a DictationAdapter to be configured.
stopDictation: () => voidStop the current dictation session.
setQuote: (quote: QuoteInfo | undefined) => voidSet a quote for the next message. Pass undefined to clear.
unstable_onunstable: (event: ComposerRuntimeEventType, callback: () => void) => UnsubscribeuseAuiState (Thread Composer State)
Access the thread’s new message composer state:
import { useAuiState, useAui } from "@assistant-ui/react";
const text = useAuiState((s) => s.composer.text);
const isEmpty = useAuiState((s) => s.composer.isEmpty);
// For actions, use useAui
const aui = useAui();
// set the text
aui.composer().setText("Hello from the thread composer");
// send whatever is in the thread composer
aui.composer().send();ThreadComposerRuntimepath: ThreadRuntimePath & { readonly composerSource: "thread"; } & { composerSource: "thread"; }type: "thread"addAttachment: (fileOrAttachment: File | CreateAttachment) => Promise<void>Add an attachment to the composer. Accepts either a standard File object (processed through the AttachmentAdapter) or a CreateAttachment descriptor for external-source attachments (URLs, API data, CMS references) that bypasses the adapter entirely.
setText: (text: string) => voidSet the text of the composer.
setRole: (role: "system" | "user" | "assistant") => voidSet the role of the composer. For instance, if you'd like a specific message to have the 'assistant' role, you can do so here.
setRunConfig: (runConfig: RunConfig) => voidSet the run config of the composer. This is used to send custom configuration data to the model. Within your backend, you can use the `runConfig` object. Example: ```ts composerRuntime.setRunConfig({ custom: { customField: "customValue" } }); ```
reset: () => Promise<void>Reset the composer. This will clear the entire state of the composer, including all text and attachments.
clearAttachments: () => Promise<void>Clear all attachments from the composer.
send: (options?: SendOptions | undefined) => voidSend a message. This will send whatever text or attachments are in the composer.
cancel: () => voidCancel the current run. In edit mode, this will exit edit mode.
subscribe: (callback: () => void) => UnsubscribeListens for changes to the composer state.
startDictation: () => voidStart dictation to convert voice to text input. Requires a DictationAdapter to be configured.
stopDictation: () => voidStop the current dictation session.
setQuote: (quote: QuoteInfo | undefined) => voidSet a quote for the next message. Pass undefined to clear.
unstable_onunstable: (event: ComposerRuntimeEventType, callback: () => void) => UnsubscribegetState: () => ThreadComposerStategetAttachmentByIndex: (idx: number) => AttachmentRuntime & { source: "thread-composer"; }useAui (Edit Composer Actions)
Access edit composer actions via useAui from within a message component:
import { useAui } from "@assistant-ui/react";
const aui = useAui();
// Begin editing the current message (populates the composer with its content)
aui.message().composer().beginEdit();
// Update the text and send the edit
aui.message().composer().setText("Updated message text");
aui.message().composer().send();
// Cancel editing without saving
aui.message().composer().cancel();EditComposerRuntimepath: ThreadRuntimePath & { readonly messageSelector: { readonly type: "messageId"; readonly messageId: string; } | { readonly type: "index"; readonly index: number; }; } & { readonly composerSource: "edit"; } & { composerSource: "edit"; }type: "edit"addAttachment: (fileOrAttachment: File | CreateAttachment) => Promise<void>Add an attachment to the composer. Accepts either a standard File object (processed through the AttachmentAdapter) or a CreateAttachment descriptor for external-source attachments (URLs, API data, CMS references) that bypasses the adapter entirely.
setText: (text: string) => voidSet the text of the composer.
setRole: (role: "system" | "user" | "assistant") => voidSet the role of the composer. For instance, if you'd like a specific message to have the 'assistant' role, you can do so here.
setRunConfig: (runConfig: RunConfig) => voidSet the run config of the composer. This is used to send custom configuration data to the model. Within your backend, you can use the `runConfig` object. Example: ```ts composerRuntime.setRunConfig({ custom: { customField: "customValue" } }); ```
reset: () => Promise<void>Reset the composer. This will clear the entire state of the composer, including all text and attachments.
clearAttachments: () => Promise<void>Clear all attachments from the composer.
send: (options?: SendOptions | undefined) => voidSend a message. This will send whatever text or attachments are in the composer.
cancel: () => voidCancel the current run. In edit mode, this will exit edit mode.
subscribe: (callback: () => void) => UnsubscribeListens for changes to the composer state.
startDictation: () => voidStart dictation to convert voice to text input. Requires a DictationAdapter to be configured.
stopDictation: () => voidStop the current dictation session.
setQuote: (quote: QuoteInfo | undefined) => voidSet a quote for the next message. Pass undefined to clear.
unstable_onunstable: (event: ComposerRuntimeEventType, callback: () => void) => UnsubscribegetState: () => EditComposerStatebeginEdit: () => voidgetAttachmentByIndex: (idx: number) => AttachmentRuntime & { source: "edit-composer"; }ThreadComposerState
The state of the thread composer which is the composer the user can interact with at the bottom.
ThreadComposerStatecanCancel: booleanisEditing: booleanisEmpty: booleantext: stringrole: "system" | "user" | "assistant"attachments: readonly Attachment[]runConfig: RunConfigattachmentAccept: stringdictation: DictationState | undefinedThe current state of dictation. Undefined when dictation is not active.
quote: QuoteInfo | undefinedThe currently quoted text, if any. Undefined when no quote is set.
type: "thread"