MongoDB Data Models
Relevant source files
The following files were used as context for generating this wiki page:
The AI Assistant platform utilizes MongoDB as its primary persistence layer to manage user identities, complex conversation histories, and a long-term memory system. These models are defined using the Mongoose ODM (Object Data Modeling) library, which provides schema validation and relationship mapping between documents.
The data architecture is designed to support a multi-user environment where each user's data is isolated. Key functionalities such as JWT-based authentication, vector-based memory retrieval, and multi-model AI switching (Phi-3 and Gemini) rely directly on the structure of these models.
Sources: README.md, Ai-Assistant-v2/auth-service/server.js:47-51
Entity Relationships
The data model follows a relational pattern within a NoSQL structure, primarily centered around the User entity. Both Conversation and Memory documents maintain a reference to the User via an ObjectId.
This diagram illustrates the ownership and source relationships between users, their chat histories, and extracted long-term memories.
Sources: Ai-Assistant-v2/auth-service/models/User.js, Ai-Assistant-v2/auth-service/models/Conversation.js:4-9, Ai-Assistant-v2/auth-service/models/Memory.js:3-8
Detailed Schema Definitions
User Model
The User model handles authentication credentials and global application preferences. It includes a nested settings object that dictates the behavior of the AI interface for that specific user.
| Field | Type | Description | Constraints |
|---|---|---|---|
username | String | Unique identifier for login | Required, Unique |
email | String | User's email address | Unique, Sparse |
password | String | Hashed user password | Required |
settings.defaultModel | String | Preferred AI engine | Enum: ['phi3', 'gemini'], Default: 'phi3' |
settings.theme | String | UI appearance | Enum: ['dark', 'light'], Default: 'dark' |
settings.memoryEnabled | Boolean | Toggle for memory retrieval | Default: true |
Logic & Middleware:
- Password Hashing: Uses a
pre('save')hook to hash passwords withbcryptbefore persistence. - Verification: Includes a
comparePasswordinstance method for authentication checks.
Sources: Ai-Assistant-v2/auth-service/server.js:29-51, Ai-Assistant-v2/auth-service/models/User.js:4-21
Conversation Model
The Conversation model stores the full transcript of interactions. Rather than storing messages in a separate collection, they are managed as an array of sub-documents to maintain chronological integrity and simplify retrieval for context-aware AI prompts.
| Field | Type | Description |
|---|---|---|
userId | ObjectId | Reference to the owner |
messages | Array | List of message objects (role, text, timestamp, intent, sentiment) |
model | String | The AI model used for this specific thread |
updatedAt | Date | Timestamp of the last interaction |
Indexing:
The model uses a compound index on { userId: 1, updatedAt: -1 } to efficiently fetch a user's most recent chat threads.
Sources: Ai-Assistant-v2/auth-service/models/Conversation.js:3-48, Ai-Assistant-v2/auth-service/routes/ai.js:49-52
Memory Model
The Memory model is the core of the platform's "Intelligent Memory System." It stores distilled facts extracted from conversations, enhanced with vector embeddings for similarity searches.
const MemorySchema = new mongoose.Schema({
userId: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true, index: true },
fact: { type: String, required: true },
sourceConversationId: { type: mongoose.Schema.Types.ObjectId, ref: 'Conversation' },
importanceScore: { type: Number, min: 0, max: 1, default: 0.5 },
tags: [{ type: String }],
embedding: { type: [Number], default: null },
createdAt: { type: Date, default: Date.now }
});
Sources: Ai-Assistant-v2/auth-service/models/Memory.js:3-31
Data Flow: Chat and Memory Persistence
When a user sends a message, the system orchestrates data across multiple models to provide a context-aware response.
The workflow demonstrates how the Conversation and Memory models interact during a standard chat cycle.
Sources: Ai-Assistant-v2/auth-service/routes/ai.js:15-89, Ai-Assistant-v2/auth-service/server.js:192-230
Memory Management Logic
Memories are not merely stored; they are ranked and retrieved based on relevance.
- Extraction: During chat, if the Python AI service identifies "memory candidates," the system filters them based on an
importanceScorethreshold (default0.6). - Indexing: The
Memoryschema utilizes multiple indexes to optimize performance:userId: 1, importanceScore: -1: For fetching the most significant facts.userId: 1, embedding: 1: Facilitates vector-based similarity lookups.
- Training: A dedicated endpoint
POST /train-memoryallows the system to retroactively scan the last 5 conversations, extract facts, and populate theMemorycollection while avoiding duplicates via regex-based existing-fact checks.
Sources: Ai-Assistant-v2/auth-service/models/Memory.js:34-36, Ai-Assistant-v2/auth-service/routes/ai.js:155-200, Ai-Assistant-v2/auth-service/services/aiService.js:52-60
Summary
The MongoDB data layer provides the structural foundation for the AI Assistant's advanced features. By leveraging Mongoose schemas for Users, Conversations, and Memories, the system maintains a clear separation of concerns while enabling complex interactions like vector-based context retrieval and automated memory extraction. This architecture ensures that user preferences and long-term knowledge persist across sessions, significantly enhancing the conversational intelligence of the platform.