CopilotChatMessageView
Component for rendering a list of chat messages
Overview
CopilotChatMessageView renders a list of chat messages and, optionally, a typing cursor while the agent is running. It delegates rendering of individual messages to slot components (assistantMessage and userMessage) and supports a render-prop children function for full control over layout.
Import
import { CopilotChatMessageView } from "@copilotkit/react-core/v2";
import "@copilotkit/react-core/v2/styles.css";Props
Prop
Type
Prop
Type
Prop
Type
Slots
All slot props follow the CopilotKit slot system: each accepts a replacement React component, a className string that is merged into the default component's classes, or a partial props object that extends the default component.
Prop
Type
Prop
Type
Prop
Type
Usage
Basic Message List
function MessageList() {
const { agent } = useAgent();
return (
<CopilotChatMessageView
messages={agent.messages}
isRunning={agent.isRunning}
/>
);
}Customizing Message Slots
function CustomMessages() {
const { agent } = useAgent();
return (
<CopilotChatMessageView
messages={agent.messages}
isRunning={agent.isRunning}
assistantMessage="bg-blue-50 p-4 rounded-xl"
userMessage="bg-gray-100 p-4 rounded-xl"
cursor={() => (
<div className="animate-pulse text-gray-400">Thinking...</div>
)}
/>
);
}Render-Prop Children for Custom Layout
function CustomLayout() {
const { agent } = useAgent();
return (
<CopilotChatMessageView
messages={agent.messages}
isRunning={agent.isRunning}
>
{({ isRunning, messages, messageElements }) => (
<div className="flex flex-col gap-4 p-6">
<div className="text-sm text-gray-500">
{messages.length} message{messages.length !== 1 ? "s" : ""}
</div>
{messageElements}
{isRunning && (
<div className="text-sm text-blue-500 animate-pulse">
Agent is responding...
</div>
)}
</div>
)}
</CopilotChatMessageView>
);
}Hiding the Toolbar on Assistant Messages
function MinimalChat() {
const { agent } = useAgent();
return (
<CopilotChatMessageView
messages={agent.messages}
isRunning={agent.isRunning}
assistantMessage={{ toolbarVisible: false }}
/>
);
}Behavior
- Message Rendering: Each message in the
messagesarray is rendered using the corresponding slot component based on itsrole. Assistant messages use theassistantMessageslot; user messages use theuserMessageslot. - Cursor Display: The
cursorslot is only rendered whenisRunningistrue. It appears after the last message in the list. - Render-Prop Override: When
childrenis provided as a function, the component delegates all layout to that function. ThemessageElementsarray is pre-built from the slot components, so custom layouts still benefit from slot customization. - Slot System: Each slot prop accepts three forms -- a replacement component, a className string merged into the default, or a partial props object that extends the default component's props.
Related
CopilotChatAssistantMessage-- Default assistant message slot componentCopilotChatUserMessage-- Default user message slot componentCopilotChatView-- Higher-level chat view that composes this component