Documentation Index
Fetch the complete documentation index at: https://algolia.com/llms.txt
Use this file to discover all available pages before exploring further.
This widget is and is subject to change in minor versions.
Signature
Import
About this widget
Use thechat widget to display a chat interface that interacts with a generative AI assistant.
See also: Agent Studio
Examples
JavaScript
Options
The CSS Selector or
HTMLElement to insert the widget into.The unique identifier of the agent to connect to. You can find the
agentId in the Agent Studio dashboard.JavaScript
Whether to enable feedback (thumbs up/down) on assistant messages. Only available when using
agentId.JavaScript
A custom transport object to handle the communication between the chat widget and the agent. The API endpoint must be compatible with Vercel AI SDK 5.
JavaScript
A function to return the URL of the main search page with the
nextUiState. This is used to navigate to the main search page when the user clicks on “View all” in the search tool.JavaScript
An object that defines the client-side tools the agent can use to interact with your app.
The object keys must match the tool names you defined in the Agent Studio dashboard.The widget has built-in renderers for the following tool types. Import and use the exported constants as keys to customize the default rendering:Each tool is an object with the following properties:
SearchIndexToolType('algolia_search_index'). Displays search results from an Algolia index in a carousel.RecommendToolType('algolia_recommend'). Displays recommendations in a carousel.DisplayResultsToolType('algolia_display_results'). Displays search results streamed directly from the agent in a carousel.
JavaScript
templates. An object containing template functions for the tool.layout. A tagged template function for rendering the tool call in the chat. It receives:message. The tool call message. It containsinput(parameters from the agent) andoutput(the result you provide withaddToolResult). For more information on the message structure, see the Vercel AI SDK documentation.indexUiState. The current InstantSearch UI state.setIndexUiState. Updates the InstantSearch UI state (for example, to refine filters or update the query based on the tool call).applyFilters. Applies filters to the InstantSearch UI state from the tool call.addToolResult. Sends the tool’s output back to the agent. You must call this at least once before the next message. For more information, see the Vercel AI SDK documentation.onClose. Dismisses the tool’s UI in the chat.sendEvent. Sendsclickorconversionevents related to the tool call. For more information, see theinsightsmiddleware documentation.
onToolCall. Optional handler invoked when the agent calls the tool. Receives a parameter object with:input. The parameters the agent passed to the tool.addToolResult. Sends the tool’s output back to the agent. For more information, see the Vercel AI SDK documentation.toolCallId. The unique identifier of the tool call.toolName. The name of the tool being invoked.dynamic. Whether the tool is dynamically registered.
streamInput. Optional boolean. Whentrue, the default loader is suppressed as the tool’s input is streamed from the agent. Use the partialinputin yourlayouttemplate to render the streaming state as input chunks arrive.
JavaScript
Extra context to send with each user message (for example, the current page or selected locale).
The widget sends this context with every message, but doesn’t show it in the chat UI.
context can be a static object or a function that returns an object at send time.
The widget serializes the context as JSON and adds it to the user message as a hidden text part of the form <context>{"key":"value"}</context>.Messages to pre-populate the chat with when it’s initialized. These messages are added without triggering an AI response.
initialMessages only applies when the chat has no existing messages. When resume is enabled, initialMessages is ignored.JavaScript
A message to send automatically when the chat is initialized.
initialUserMessage is only sent when the chat has no existing messages. It’s sent after initialMessages are applied. When resume is enabled, this message isn’t sent.JavaScript
Whether to resume an ongoing chat generation stream when the widget mounts. Use this when restoring a chat session after a page reload to continue receiving an in-flight assistant response.
JavaScript
A callback called when the assistant response has finished streaming, including when the stream is aborted, disconnected, or fails.The callback receives an object with:
message: the final assistant message.messages: the full message list including the new message.isAbort:trueif the stream was stopped withstop().isDisconnect:trueif the connection was lost.isError:trueif the stream finished with an error.
JavaScript
The CSS classes you can override:
root. The root element of the widget.container. The container element.header. The header section of the widget.root. The root element.clear. The clear button.close. The close button.maximize. The maximize button.title. The title element.titleIcon. The title icon element.
messages. The messages section of the widget.root. The root element.content. The scrollable content.scroll. The scroll container.scrollToBottom. The scroll to bottom button.scrollToBottomHidden. The hidden state of the scroll to bottom button.
message. The message in the messages section.root. The root element.container. The message container.leading. The leading element (e.g., avatar).content. The content element.message. The message text element.actions. The action buttons container.footer. The footer element.
prompt. The prompt section of the widget.root. The root element.actions. The actions container.body. The body element.footer. The footer element.header. The header element.submit. The submit button.textarea. The textarea element.
toggleButton. The toggle button of the widget.root. The root element.
JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html function is available in InstantSearch.js version 4.46.0 or later.A template to customize the overall layout of the chat widget. Use
instantsearch.templates.chatInlineLayout() for an inline (non-overlay) layout, or provide a custom function.JavaScript
The template to use for each result. This template receives an object containing a single record. You can use Algolia’s highlighting feature with the
highlight function, directly from the template system.JavaScript
Templates to use for the header section of the widget.
clearLabelText. Accessible label for the clear button.closeIcon. The close icon template.closeLabel. Accessible label for the close button.maximizeIcon. The maximize icon template. Receives a parameter containing{ maximized: boolean }for conditional rendering.maximizeLabelText. Accessible label for the maximize button.minimizeIcon. The minimize icon template.minimizeLabelText. Accessible label for the minimize button.titleIcon. The title icon template (defaults to sparkles).titleText. The title text to display.
JavaScript
Templates to use for the messages section of the widget.
copyToClipboardLabelText. Accessible label for the copy to clipboard action.error. Custom template when there is an error loading messages.regenerateLabelText. Accessible label for the regenerate action.scrollToBottomLabelText. Accessible label for the scroll to bottom button.
JavaScript
A template to customize the loader shown while waiting for an assistant response.
JavaScript
Text to display in the default loader.
JavaScript
Templates to use for an individual message in the messages section of the widget.
messageLabelText. Accessible label for the message.actionsLabelText. Accessible label for the actions container.
JavaScript
Templates to use for messages that come from the chat assistant.
leading. The leading element (e.g., avatar).footer. The footer element of the message.
JavaScript
Templates to use for messages that come from the user.
leading. The leading element (e.g., avatar).footer. The footer element of the message.
JavaScript
Templates to use for the prompt section of the widget.
disclaimerText. Disclaimer text shown in the prompt footer.emptyMessageTooltipText. Tooltip for the submit button when message is empty.footer. Custom footer template.header. Custom header template.sendMessageTooltipText. Tooltip for the send button.stopResponseTooltipText. Tooltip for the stop button.textareaLabelText. Accessible label for the textarea.textareaPlaceholderText. Placeholder text for the textarea.
JavaScript
The template to use for prompt suggestions. This template receives an object containing the list of suggestions and a
onSuggestionClick function to call when the suggestion is clicked.JavaScript
Templates to use for the toggle button of the widget.
icon. Custom icon template. Receives a parameter containing{ isOpen: boolean }for conditional rendering.
JavaScript
HTML output
HTML
Streaming and resumption
Assistant responses stream over Server-Sent Events. The chat widget exposes the streaming lifecycle through its connector render state if you want to drive a custom UI.Status values
The render state exposesstatus, which can be one of:
'ready'. The chat is idle and ready to accept a new message.'submitted'. A user message was submitted and the assistant hasn’t started responding yet.'streaming'. The assistant is streaming a response.'error'. The last response finished with an error. CallclearError()before sending a new message.
state of 'streaming' or 'done' while the response streams.
Stop and resume
The render state exposes two methods:stop(). Aborts the current streaming response. TheonFinishcallback runs withisAbort: true.resumeStream(). Reconnects to an in-flight stream. Call this on mount, or passresume: trueto do it automatically.
connectChat, read status and stop from the render options:
JavaScript
resume: true on the widget, or call resumeStream() from a custom render function.