You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This RFC proposes a clean separation of message entities into three distinct types, each serving a specific purpose in the LobeChat architecture:
DB Message - Optimized for database persistence
UI Message - Optimized for frontend rendering
LLM Message - Optimized for AI provider consumption
This separation follows the principle of separation of concerns, enabling better scalability, maintainability, and flexibility for future business requirements.
🎯 Motivation
Current Problems
Previously, we used a single ChatMessage type across all layers of the application, which led to several issues:
Tight Coupling: Database schema, UI components, and LLM integration all depended on the same type, making changes risky and difficult
Bloated Types: The single type contained fields needed only in specific contexts (e.g., UI-only fields stored in DB)
Limited Flexibility: Adding new features often required changing the core message type, affecting all layers
Poor Performance: Storing UI-specific data in the database increased storage costs and query complexity
Unclear Responsibilities: It was unclear which layer "owned" certain fields and transformations
Benefits of Separation
By introducing three distinct message types, we achieve:
Clear Boundaries: Each layer has its own optimized data structure
Better Performance: Database stores only essential data; UI only processes what it needs
Easier Extension: New features can be added to specific layers without affecting others
Type Safety: TypeScript can enforce correct usage at each layer
Maintainability: Changes to one layer don't ripple through the entire system
📐 Design
1. DB Message (DBMessageItem)
Purpose: Optimized for database persistence using relational database capabilities.
Location: packages/types/src/message/db/item.ts
Key Characteristics:
Flat structure optimized for relational database storage
Uses proper database types (e.g., Date instead of number for timestamps)
Contains only fields that need to be persisted
Leverages database relationships (foreign keys, joins) for complex data
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
📋 Summary
This RFC proposes a clean separation of message entities into three distinct types, each serving a specific purpose in the LobeChat architecture:
This separation follows the principle of separation of concerns, enabling better scalability, maintainability, and flexibility for future business requirements.
🎯 Motivation
Current Problems
Previously, we used a single
ChatMessagetype across all layers of the application, which led to several issues:Benefits of Separation
By introducing three distinct message types, we achieve:
📐 Design
1. DB Message (
DBMessageItem)Purpose: Optimized for database persistence using relational database capabilities.
Location:
packages/types/src/message/db/item.tsKey Characteristics:
Dateinstead ofnumberfor timestamps)Type Definition:
Benefits:
2. UI Message (
UIChatMessage)Purpose: Optimized for frontend rendering and user interaction.
Location:
packages/types/src/message/ui/chat.tsKey Characteristics:
numberfor timestamps)Type Definition:
Benefits:
3. LLM Message
Purpose: Optimized for consumption by AI providers (OpenAI, Anthropic, etc.).
Location: Transformations in
packages/promptsandsrc/services/chatKey Characteristics:
Transformation Example:
Benefits:
🔄 Data Flow
Write Path (User sends a message)
Read Path (Load conversation)
AI Generation Path
🎁 Benefits
For Developers
For Database
For UI
For LLM Integration
Transformation Overhead
Issue: Converting between types adds runtime cost.
Mitigation:
Keeping Types in Sync
Issue: Changes to one type may need to propagate to others.
Mitigation:
packages/types/src/message/commonLearning Curve
Issue: Developers need to understand three different types.
Mitigation:
DBMessageItem,UIChatMessage,OpenAIChatMessageBeta Was this translation helpful? Give feedback.
All reactions