Database Schema Definitions
Relevant source files
The following files were used as context for generating this wiki page:
The Database Schema for OpenChat is built on Convex, providing a real-time, serverless data layer. The schema is modularized to support complex chat interactions, multi-modal message content, AI agent scheduling, and service integrations. It defines the structure of tables, indexes for efficient querying, and relationships between core entities like users, chats, and messages.
OpenChat utilizes a modular schema structure located in convex/schema/, where individual entities are defined in dedicated files and aggregated in a central convex/schema.ts file. This architecture supports advanced features such as message threading, rich message parts (text, images, reasoning), and automated background tasks.
Sources: convex/schema/README.md:1-26, convex/schema.ts:1-41, agent_rules/convex_rules.md:135-144
Core Data Model Architecture
The application's data model is centered around the relationship between Users, Chats, and Messages. It follows a chat-based paradigm rather than a thread-based one, though it maintains support for message threading via parent identifiers.
Entity Relationship Overview
The following ER diagram illustrates the primary relationships between the core database tables:
The data model uses Id<TableName> for type-safe cross-table references.
Sources: convex/schema.ts:13-40, convex/schema/README.md:25-26, convex/_generated/dataModel.d.ts:35-47
Primary Table Definitions
The schema defines several tables to manage the application state. Key tables and their indexed fields are summarized below:
| Table Name | Description | Key Indexes |
|---|---|---|
users | User profiles, preferences, and authentication data. | email |
chats | Conversation sessions belonging to a user. | by_user |
messages | Individual entries within a chat, supporting rich content parts. | by_chat_and_created, by_user, by_user_content (Search) |
chat_attachments | Metadata for files and images stored in object storage (R2). | by_chatId, by_userId, by_key |
scheduled_tasks | Definitions for background AI agents and automated workflows. | by_user, by_next_execution |
connectors | Configuration for third-party service integrations (Gmail, GitHub, etc.). | by_user, by_user_and_type |
Sources: convex/schema.ts:13-41, convex/schema/README.md:5-20
Message Parts and Rich Content
A significant evolution in the OpenChat schema is the transition to a "Message Parts" system. While maintaining a legacy content field for backward compatibility, messages now utilize a structured parts system to handle multi-modal data.
Message Content Types
Messages are structured into specific parts defined in parts.ts:
- TextPart: Standard plain text content.
- ImagePart: References to image attachments.
- ReasoningPart: Captures AI thinking steps or "hidden" reasoning.
- FilePart: General file attachments.
- ToolInvocationUIPart: Data related to tool/function calling interfaces.
Sources: convex/schema/README.md:9-16, convex/schema/message.ts
Message Search Logic
The messages table includes a specialized search index for full-text queries:
// convex/schema.ts
messages: defineTable(Message)
.index("by_chat_and_created", ["chatId", "createdAt"])
.index("by_user", ["userId"])
.searchIndex("by_user_content", {
searchField: "content",
filterFields: ["userId", "chatId"],
}),
Sources: convex/schema.ts:16-22
Automation and Task Management
The schema supports background automation through scheduled_tasks and task_history tables. This system tracks the lifecycle of AI agents.
Task Execution State Machine
The scheduled_tasks table is optimized for the worker loop using the by_next_execution index, which filters by status and nextExecution time.
Sources: convex/schema.ts:33-40, convex/schema/README.md:21-23
System Fields and Constraints
Every document in the Convex database automatically includes system fields:
_id: A uniquev.id(tableName)._creationTime: Av.number()representing the Unix timestamp of creation.
Schema Validation Rules
The project follows strict Convex validation guidelines:
- IDs: Always use
v.id(tableName)for references to ensure referential integrity at the database level. - Indexes: Index names must include the field names (e.g.,
by_user_and_type). - Immutability: While Convex allows
patchandreplace, system fields like_idcannot be modified.
Sources: agent_rules/convex_rules.md:135-147, convex/_generated/dataModel.d.ts:35-47
Summary
The database schema of OpenChat provides a robust foundation for a multi-modal AI assistant. By modularizing entity definitions and leveraging Convex's indexing and search capabilities, the system efficiently handles real-time chat, complex background automations, and rich media attachments while maintaining strict type safety through generated data models.