logoassistant-ui
API Reference/Runtime Hooks

ComposerRuntime

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.

useComposer

Grabs the nearest composer (whether it’s the edit composer or the thread’s composer):

// Example
import { useComposer } from "@assistant-ui/react";

const composerRuntime = useComposer();

// set the text
composerRuntime.setText("Hello from the composer runtime");

// send whatever is in the composer 
composerRuntime.send();

// get the current text in the composer state
const composerState = composerRuntime.getState();
const currentText = composerState.text;

ComposerRuntime

path:

ComposerRuntimePath

type:

"edit" | "thread"

getState:

() => ComposerState

Get the current state of the composer. Includes any data that has been added to the composer.

getAttachmentAccept:

() => string

addAttachment:

(file: File) => Promise<void>

Given a standard js File object, add it to the composer. A composer can have multiple attachments.

setText:

(text: string) => void

Set the text of the composer.

setRole:

(role: "system" | "user" | "assistant") => void

Set 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) => void

Set the run config of the composer. This is used to send custom configuration data to the model. Within your backend, you can access this data using 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:

() => void

Send a message. This will send whatever text or attachments are in the composer.

cancel:

() => void

Cancel the current run. In edit mode, this will exit edit mode.

subscribe:

(callback: () => void) => Unsubscribe

Listens for changes to the composer state.

getAttachmentByIndex:

(idx: number) => AttachmentRuntime

Get an attachment by index.

useThreadComposer

Access the thread’s new message composer state:

import { useThreadComposer } from "@assistant-ui/react";

const threadComposerRuntime = useThreadComposer();

// set the text
threadComposerRuntime.setText("Hello from the thread composer runtime");

// send whatever is in the thread composer 
threadComposerRuntime.send();

// get the current text in the composer state
const threadComposerState = threadComposerRuntime.getState();
const currentText = threadComposerRuntime.text;

ThreadComposerRuntime

path:

ThreadRuntimePath & { readonly composerSource: "thread"; } & { composerSource: "thread"; }

type:

"thread"

getAttachmentAccept:

() => string

addAttachment:

(file: File) => Promise<void>

Given a standard js File object, add it to the composer. A composer can have multiple attachments.

setText:

(text: string) => void

Set the text of the composer.

setRole:

(role: "system" | "user" | "assistant") => void

Set 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) => void

Set the run config of the composer. This is used to send custom configuration data to the model. Within your backend, you can access this data using 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:

() => void

Send a message. This will send whatever text or attachments are in the composer.

cancel:

() => void

Cancel the current run. In edit mode, this will exit edit mode.

subscribe:

(callback: () => void) => Unsubscribe

Listens for changes to the composer state.

getState:

() => ThreadComposerState

getAttachmentByIndex:

(idx: number) => AttachmentRuntime & { source: "thread-composer"; }

ThreadComposerState

The state of the thread composer which is the composer the user can interact with at the bottom.

ThreadComposerState

canCancel:

boolean

isEditing:

boolean

isEmpty:

boolean

text:

string

role:

"system" | "user" | "assistant"

attachments:

readonly Attachment[] & readonly PendingAttachment[]

runConfig:

RunConfig

type:

"thread"

EditComposerState

The state of the edit composer which is the composer the user can edit messages with.

EditComposerState

canCancel:

boolean

isEditing:

boolean

isEmpty:

boolean

text:

string

role:

"system" | "user" | "assistant"

attachments:

readonly Attachment[]

runConfig:

RunConfig

type:

"edit"