Skip to content

Chat Component

The main chat component with history sidebar.

Import

tsx
import { Chat } from "@cognipeer/chat-ui";

Usage

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  theme="dark"
/>

Props

Required Props

PropTypeDescription
baseUrlstringBase URL for the agent server API
agentIdstringID of the agent to chat with

Optional Props

PropTypeDefaultDescription
theme"dark" | "light""dark"Theme mode
themeColorsThemeColors-Custom theme colors
authorizationstring-Authorization header
headersRecord<string, string>-Custom headers
conversationIdstring-Initial conversation ID
streamingbooleantrueEnable streaming
showHistorybooleantrueShow history sidebar
enableFileUploadbooleantrueEnable file uploads
allowedFileTypesstring[]["*"]Allowed file types
maxFileSizenumber10MBMax file size
maxFilesnumber10Max files per message
classNamestring-Container class name

Callback Props

PropTypeDescription
onMessageSent(message: Message) => voidCalled when user sends a message
onMessageReceived(message: Message) => voidCalled when response is received
onStreamText(chunk: string, fullText: string) => voidCalled on each text chunk
onToolCall(name: string, args: object) => voidCalled when tool is called
onToolResult(name: string, result: any) => voidCalled when tool returns
onConversationCreated(conv: Conversation) => voidCalled on new conversation
onConversationSelected(conv: Conversation) => voidCalled on conversation switch
onConversationDeleted(id: string) => voidCalled on conversation delete
onError(error: Error) => voidCalled on error

Render Props

PropTypeDescription
renderMessage(props: MessageProps) => ReactNodeCustom message renderer
renderMessageActions(props: ActionProps) => ReactNodeCustom message actions
renderFilePreview(props: FileProps) => ReactNodeCustom file preview
renderToolCall(props: ToolCallProps) => ReactNodeCustom tool call renderer
renderHistoryItem(props: HistoryItemProps) => ReactNodeCustom history item
renderEmpty() => ReactNodeCustom empty state

Examples

Basic

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
/>

With Auth

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  authorization="Bearer your-token"
/>

Light Theme

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  theme="light"
/>

Custom Colors

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  themeColors={{
    bgPrimary: "#1a1a2e",
    accentPrimary: "#e94560",
  }}
/>

With Callbacks

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  onMessageSent={(msg) => console.log("Sent:", msg)}
  onMessageReceived={(msg) => console.log("Received:", msg)}
  onError={(err) => console.error("Error:", err)}
/>

With Feedback Actions

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  renderMessageActions={({ message, isStreaming }) => {
    if (isStreaming || message.role !== "assistant") return null;
    
    return (
      <div className="flex gap-2">
        <button onClick={() => sendFeedback(message.id, "up")}>👍</button>
        <button onClick={() => sendFeedback(message.id, "down")}>👎</button>
      </div>
    );
  }}
/>

Images Only

tsx
<Chat
  baseUrl="/api/agents"
  agentId="vision"
  allowedFileTypes={["image/*"]}
  maxFiles={1}
/>

No History

tsx
<Chat
  baseUrl="/api/agents"
  agentId="assistant"
  showHistory={false}
/>

TypeScript

tsx
import { Chat, ChatProps } from "@cognipeer/chat-ui";

const props: ChatProps = {
  baseUrl: "/api/agents",
  agentId: "assistant",
  theme: "dark",
};

<Chat {...props} />

Sizing

The Chat component fills its container:

tsx
// Full screen
<div className="h-screen">
  <Chat ... />
</div>

// Fixed height
<div style={{ height: 600 }}>
  <Chat ... />
</div>

Released under the MIT License.