Base Message Type
All messages in Cedar extend theBaseMessage interface from @messages/types:
Requirements for Custom Messages
To use the custom message rendering system, your message objects must have atype field. This field determines which renderer will be used to display the message.
Message Renderer Interface
Cedar uses a strongly typedMessageRenderer interface that ensures type safety throughout the rendering pipeline:
- The
renderfunction receives your narrowly typed custom message (e.g.,AlertMessage) - The
validateMessagefunction receives the broadly typedMessagefor runtime validation
Custom Message Type Definition
Cedar exports aCustomMessage<T, P> type that allows you to create type-safe custom messages:
Message Renderer Factory Function
Cedar exports acreateMessageRenderer factory function to create type-safe renderers:
How Message Rendering Works Internally
The chat system uses the following process to render messages:- Message Reception: When a message is added to the chat, the system examines its
typefield - Renderer Lookup: The system searches for a registered renderer that matches the message type
- Validation: If a renderer is found and has a validation function, it checks if the message structure is valid
- Rendering: If validation passes (or no validation exists), the renderer’s
renderfunction is called - Fallback: If no matching renderer is found or validation fails, the default text renderer is used
Full Implementation Examples
Here’s an example of creating a custom message renderer for an AlertMessage.Default Message Types and Renderers
Cedar automatically handles these message types with default behavior. Each type has a corresponding default renderer:"message" Type
- Behavior: Standard text messages with role-based styling
- Properties:
content: string,role: 'user' | 'assistant' | 'bot' - Renderer: Built-in text message renderer with role-based styling
- Warning: Overriding this type will disable default text message rendering
"setState" Type
- Behavior: Executes state actions and displays completion status
- Properties:
stateKey: string,setterKey: string,args?: unknown - Default Renderer:
SetStateRenderershows setState completion with shimmer effects
"frontendTool" Type
- Behavior: Displays frontend tool execution results with status indicators
- Properties:
toolName: string,args?: unknown,result?: unknown,error?: string,status: 'success' | 'error' - Default Renderer:
FrontendToolRenderershows tool execution status with result display
"progress_update" Type
- Behavior: Shows progress indicators with shimmer effects
- Properties:
content: string,metadata.state?: 'in_progress' | 'complete' | 'error' - Default Renderer:
ProgressUpdateRendererwith animated shimmer text
Additional Built-in Renderers
Cedar also includes renderers for streaming events:Mastra Event Renderer
Cedar automatically receives and renders all Mastra streamed events. Any of these can be customized or overridden with your own renderers: Supported Mastra Event Types:'start'- Agent run started'step-start'- Agent step started'tool-call'- Tool being called'tool-result'- Tool call result'step-finish'- Agent step completed'tool-output'- Tool output received'step-result'- Step result available'step-output'- Step output received'finish'- Agent run completed
type. For example, to customize just the 'tool-call' event:
Recommended File Structure
Cedar encourages organizing custom renderers in dedicated files for easier maintenance and debugging:cedar-os directory, making it easier to maintain, debug, and ensure consistency across your application.
