Composer
AIMessageComposer is where the user writes to your agent. It's a form with a text input that grows with its lines, file attachments with previews, dictation through the browser's speech recognition, a menu of chat options such as "Think harder", and a model picker. You put it together from its parts, and it hands you what the user wrote when they send it:
import { AIMessageComposer } from "stream-chat-react/ai-components";
export const Composer = () => (
<AIMessageComposer onSubmit={handleSubmit}>
<AIMessageComposer.AttachmentPreview />
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.FileInput name="attachments" />
<AIMessageComposer.SpeechToTextButton />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>
);AIMessageComposer is part of the stream-chat-react/ai-components entry point. See the Quickstart to install it and load its stylesheet.
The parts are static properties of AIMessageComposer, not separate exports, and each reads the composer's state from it, so render them inside it. Lay them out with your own elements around them as you like. The form stacks what's inside it, with the text field growing to fill it, and AIMessageComposer.Actions puts the buttons in a row of their own, as Lay out the parts shows. The form also grows to fill a flex container it's placed in, so put it in an element of its own when it sits under a conversation. It brings the SDK's theme scope, the str-chat class, so it takes the SDK's fonts and colors without a wrapper.
Lay out the parts
From version 15.0.1 of stream-chat-react, the composer lays out its controls without CSS of your own. AIMessageComposer.Actions is the row of buttons under the text, lined up at its end. Before 15.0.1, put the buttons in a div of your own with display: flex, justify-content: flex-end and a small gap:
<AIMessageComposer onSubmit={handleSubmit}>
<AIMessageComposer.AttachmentPreview />
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.FileInput name="attachments" />
<AIMessageComposer.SpeechToTextButton />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>A composer whose only control is the send button fits on one line with layout="inline". The text and the controls sit side by side, the controls level with the text's last line as it grows, and attachment previews keep a full-width row above them. The default, layout="stacked", puts one part under another, as above.
<AIMessageComposer layout="inline" onSubmit={handleSubmit}>
<AIMessageComposer.TextInput />
<AIMessageComposer.SubmitButton />
</AIMessageComposer>Send what the user writes
AIMessageComposer renders a form, and takes every prop of one. It keeps the text and the picked files in a store of its own, and these props on top:
| Prop | Description | Default |
|---|---|---|
conversation |
An AIConversationController the composer sends to. See With a conversation. |
- |
onSubmit |
Called when the user sends: with the submit button, or Enter in the text input. | - |
onChange |
Called on every change in the form, before the composer reads it. | - |
disabled |
Disables every part of the composer. | - |
layout |
How the parts are arranged: "stacked", one under another, or "inline", the text and the controls on one line. See Lay out the parts. From version 15.0.1. |
"stacked" |
nameMapping |
The names of the inputs the composer reads the text and the files from. | { message: "message", attachments: "attachments" } |
resetAttachmentsOnSelect |
Clears the file input after each selection, so the same file can be picked again. The picked files stay in the composer's store. | true |
The text input is named message already. FileInput has no name of its own, so give it name="attachments" for the composer to pick up its files, or another name that nameMapping maps.
Since the file input is cleared after each selection, the form's data on submit holds the text, but not the files. Keep the files as the user picks them, in onChange, and show them in AttachmentPreview:
import { type FormEvent, useState } from "react";
import { AIMessageComposer } from "stream-chat-react/ai-components";
type Picked = { file: File; id: string };
export const Composer = ({
onSend,
}: {
onSend: (text: string, files: File[]) => void;
}) => {
const [picked, setPicked] = useState<Picked[]>([]);
const handleChange = (event: FormEvent<HTMLFormElement>) => {
const input = event.target;
if (input instanceof HTMLInputElement && input.name === "attachments") {
const files = Array.from(input.files ?? []);
setPicked((current) => [
...current,
...files.map((file) => ({ file, id: crypto.randomUUID() })),
]);
}
};
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const text = new FormData(event.currentTarget).get("message");
onSend(
typeof text === "string" ? text : "",
picked.map(({ file }) => file),
);
event.currentTarget.reset();
setPicked([]);
};
return (
<AIMessageComposer onChange={handleChange} onSubmit={handleSubmit}>
<AIMessageComposer.AttachmentPreview>
{picked.map(({ file, id }) => (
<AIMessageComposer.AttachmentPreview.Item
file={file}
key={id}
onDelete={() =>
setPicked((current) => current.filter((item) => item.id !== id))
}
/>
))}
</AIMessageComposer.AttachmentPreview>
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.FileInput name="attachments" />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>
);
};Resetting the form clears the composer's text and files, and keeps it disabled if disabled says so.
To send through a Stream Chat channel, with its uploads and its stop button, render the composer as the channel's composer, as SDK integration shows.
With a conversation
Pass an AIConversationController as conversation, and the composer sends to it, with nothing to handle yourself:
import {
AIMessageComposer,
type AIChatOption,
type AIConversationController,
} from "stream-chat-react/ai-components";
const thinkHarder: AIChatOption = {
id: "think_harder",
title: "Think harder",
};
export const Composer = ({
conversation,
}: {
conversation: AIConversationController;
}) => (
<AIMessageComposer conversation={conversation}>
<AIMessageComposer.AttachmentPreview />
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.FileInput name="attachments" />
<AIMessageComposer.ChatOptions options={[thinkHarder]} />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>
);With a conversation, the composer:
- Shows the controller's composer store,
conversation.composer, in place of a store of its own. The hooks in Control it from your code read it inside the composer, and your code can read and change it anywhere throughconversation.composer. - Sends what the user wrote. Submitting calls
conversation.send({ text, attachments, chatOption, custom })with the text, the picked files, the picked chat option and the store'scustom, then empties the text and the files. The chat option stays picked. Nothing is sent while the text is empty and no files are picked. - Turns the send button into a stop button. While the agent answers,
SubmitButtonshows aStopButtonin its place, which stops the reply, and Enter in the text input doesn't send. - Shows that the user is editing a message. While the user edits one of their messages, the composer shows an "Editing" chip above the text, with a button that cancels the edit, and the next message they send replaces the one they edited. The store's
isEditingistruemeanwhile.
onSubmit is still called first. To send in a way of your own, such as for a command, call event.preventDefault() in it, and the composer sends nothing:
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
const { text } = conversation.composer.getLatestValue();
if (text.trim() === "/clear") {
event.preventDefault();
conversation.composer.partialNext({ text: "" });
}
};
<AIMessageComposer conversation={conversation} onSubmit={handleSubmit}>
<AIMessageComposer.TextInput />
<AIMessageComposer.SubmitButton />
</AIMessageComposer>;To fill the store yourself, such as with custom data that goes with every message, make it with createAIMessageComposerStore() and pass it to the controller as composer:
import {
AIConversationController,
createAIMessageComposerStore,
} from "stream-chat-react/ai-components";
// Your transport, as in Conversation state.
import { transport } from "./transport";
const composer = createAIMessageComposerStore();
composer.partialNext({ custom: { locale: navigator.language } });
const controller = new AIConversationController({ composer, transport });Make it where you make the controller, inside the effect, as Make a conversation shows.
Control it from your code
Inside the composer, these hooks read and change its state:
| Hook | Returns |
|---|---|
useText() |
text, and setText(text) to replace it. |
useAttachments() |
attachments, each a { id, file, meta? }, with removeAttachment(idOrFile) and updateAttachments(ids, update). |
useIsDisabled() |
disabled, and setDisabled(disabled). |
useAIMessageComposerContext() |
The composer's store, an AIMessageComposerStore in a StateStore. |
They're exported from stream-chat-react/ai-components. Outside a composer, they read a store no composer uses. For example, a button that fills in a prompt:
import { Button } from "stream-chat-react";
import { AIMessageComposer, useText } from "stream-chat-react/ai-components";
const PromptButton = ({ prompt }: { prompt: string }) => {
const { setText } = useText();
return (
<Button
appearance="outline"
onClick={() => setText(prompt)}
size="sm"
type="button"
variant="secondary"
>
{prompt}
</Button>
);
};
<AIMessageComposer onSubmit={handleSubmit}>
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<PromptButton prompt="Summarize this thread" />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>;Chat options
AIMessageComposer.ChatOptions adds a menu of ways to ask, such as Think harder, and shows the one picked as a chip the user can remove. With a name, the picked option's id goes with the form's data:
import {
AIMessageComposer,
type AIChatOption,
} from "stream-chat-react/ai-components";
const thinkHarder: AIChatOption = {
id: "think_harder",
title: "Think harder",
description: "Takes longer, for harder questions",
shortTitle: "Think",
};
export const Composer = () => (
<AIMessageComposer onSubmit={handleSubmit}>
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.FileInput name="attachments" />
<AIMessageComposer.ChatOptions name="option" options={[thinkHarder]} />
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>
);On submit, new FormData(event.currentTarget).get("option") is "think_harder", or "" when nothing is picked.
| Prop | Description | Default |
|---|---|---|
options |
The options: an id and a title, and an optional description, icon, shortTitle for the chip and customData. |
- |
name |
Submits the picked option's id with the form under this name. |
- |
onSelectedChange |
Called with the option the user picks, or null when they remove it. |
- |
selectedId |
The picked option's id, when you control it. |
- |
defaultSelectedId |
The option picked at first, when you don't. | null |
icon |
The menu button's icon. | A plus |
disabled |
Disables the menu and the chip. | The composer's |
The picked option is kept in the composer's store as chatOption, so it stays picked after a message is sent, and a conversation sends it with the message. Picking the option that's already picked removes it. ChatOptionsList and SelectedChatOptionChip, the menu's items and the chip, are exported too, to build a menu of your own.
Stop the reply
With a conversation, SubmitButton turns into a stop button while the agent answers, and stops the reply through the controller.
Without one, show AIMessageComposer.StopButton, from version 15.0.1, in place of the send button while the agent answers, and stop the reply in its onClick. It's type="button", so it never submits the form, and it stays enabled when the composer is disabled, which it often is while a reply runs. Its children replace its stop icon.
{
isAnswering ? (
<AIMessageComposer.StopButton onClick={stop} />
) : (
<AIMessageComposer.SubmitButton />
);
}With a Stream Chat channel, read the channel's AI state with useAIState, and stop the reply with channel.stopAIResponse(), which sends ai_indicator.stop to the channel. A button of your own looks like this:
import { AIStates, type AIState } from "stream-chat";
import { useAIState, useChannel } from "stream-chat-react";
import { AIMessageComposer } from "stream-chat-react/ai-components";
const activeStates: AIState[] = [
AIStates.Thinking,
AIStates.ExternalSources,
AIStates.Generating,
];
export const StopReplyButton = () => {
const channel = useChannel();
const { aiState } = useAIState(channel);
if (!activeStates.includes(aiState)) return null;
return (
<AIMessageComposer.StopButton onClick={() => channel.stopAIResponse()} />
);
};Render it in the composer's row of buttons, in place of the send button while it shows, and the composer inside Channel. Screen readers name it "Stop AI Generation". Before version 15.0.1, render a Button of your own with the same onClick and type="button". The button doesn't hide itself on click: it goes once ai_indicator.stop, or the backend's ai_indicator.clear, reaches the channel and moves aiState out of the active states.
The default MessageComposer of stream-chat-react has a stop button of its own, StopAIGenerationButton. It shows only while aiState is AI_STATE_THINKING or AI_STATE_GENERATING, not during AI_STATE_EXTERNAL_SOURCES, so the send button comes back while a tool runs. Replacing the composer's UI with AIMessageComposer removes it. SDK integration shows how to restyle or remove it.
Stop the reply covers what the backend does with the event.
Dictation
AIMessageComposer.SpeechToTextButton turns speech into text with the browser's Web Speech API, and writes the transcript into the composer's text input. Its options go to the speech recognizer:
<AIMessageComposer onSubmit={handleSubmit}>
<AIMessageComposer.TextInput />
<AIMessageComposer.Actions>
<AIMessageComposer.SpeechToTextButton
options={{ lang: "en-US", continuous: false, interimResults: true }}
/>
<AIMessageComposer.SubmitButton />
</AIMessageComposer.Actions>
</AIMessageComposer>| Option | Description | Default |
|---|---|---|
lang |
The language to recognize. | "en-US" |
interimResults |
Writes the words so far while the user speaks. | true |
continuous |
Keeps listening after the user stops speaking. | false |
maxAlternatives |
The most alternatives the browser returns for each result. The first one is used. | 1 |
onTranscript |
Called with the transcript, besides writing it into the input. | - |
onError |
Called with an English description of what went wrong, in place of logging it. | Logs to console.error |
The button takes the props of a button too. When clicked, the browser asks for the microphone, and the button shows as pressed (aria-pressed) while it listens. Clicking it again stops it.
The button renders nothing in browsers without the Web Speech API, such as Firefox. Support is only known in the browser, so with server rendering the button appears after hydration.
To dictate into an input of your own, use the useSpeechToText hook the button is built on. It takes the same options and returns isSupported, isListening, startListening and stopListening.
To let the user talk to your agent instead, rather than dictate, see the Voice agent use case.
Customize the composer
Each part takes the props of the element it renders, so you can name it, style it or handle its events:
| Part | Renders |
|---|---|
AIMessageComposer.TextInput |
A textarea named message that grows with its lines up to 200 px, then scrolls. Enter sends, and Shift+Enter starts a new line. |
AIMessageComposer.FileInput |
A round button that picks one or more files. The native file input stays focusable for keyboard users, and labelProps go to the round button. |
AIMessageComposer.SpeechToTextButton |
The dictation button. |
AIMessageComposer.ChatOptions |
The chat options menu and chip. |
AIMessageComposer.ModelSelect |
A select of models. Pass your own <option> elements as options. |
AIMessageComposer.SubmitButton |
The send button. active highlights it, for example while files are picked. With a conversation, a StopButton takes its place while the agent answers. |
AIMessageComposer.StopButton |
A round button that stops the reply. With a conversation, it stops the conversation's reply, unless its onClick calls event.preventDefault(). |
AIMessageComposer.Actions |
A row for the buttons, lined up at its end. See Lay out the parts. From version 15.0.1. |
AIMessageComposer.AttachmentPreview |
The picked files. Without children, it shows the composer's own files, each with a delete button, and nothing while there are none. With children, it shows them instead. |
AIMessageComposer.AttachmentPreview.Item |
One file: a thumbnail for an image, a card with the name and size for any other file, and a delete button. |
ModelSelect offers two OpenAI models by default, with values such as openai|gpt-4o-mini. Give it your own list and a name, so the pick goes with the form's data, and check on your backend that it's a model you offer:
<AIMessageComposer.ModelSelect
name="model"
options={
<>
<option value="fast">Fast</option>
<option value="smart">Smart</option>
</>
}
/>An AttachmentPreview.Item takes the file, an onDelete, and optionally a title in place of the file's name, an imagePreviewSource for an image, and an upload state. With state="failed", it shows a button that calls onRetry. The children of AttachmentPreview can also be a function of what useAttachments() returns.
Styling and texts
The composer follows the SDK's theme in light and dark mode, and its colors come from the --str-chat__ai-* variables described in Theming: border, surface-hover and surface-pressed for the composer, and button-bg, scrollbar-track and scrollbar-thumb for the previews. Its class names start with str-chat__ai-message-composer, str-chat__ai-attachment-preview and str-chat__ai-chat-options.
It shows the SDK's icons, which you replace through WithComponents like any other icon. Its texts go through the SDK's translations, such as aiComponents.messageComposer.textInput.placeholder ("Ask a question..."), aiComponents.messageComposer.speechToText.ariaLabel, aiComponents.chatOptions.chatOptions.ariaLabel, aiComponents.messageComposer.editing.text ("Editing") and aiComponents.messageComposer.cancelEditing.ariaLabel ("Cancel editing"), which also name the buttons for screen readers. English is built in. To change or translate them, register your own dictionary, as described in Localization.