Spaces:
Sleeping
Sleeping
Download models/ChatHistory.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 3.88 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/models/ChatHistory.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/models/ChatHistory.js
-
curl -L -o ChatHistory.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/models/ChatHistory.js
3.88 kB
| const mongoose = require('mongoose'); | |
| const chatHistorySchema = new mongoose.Schema({ | |
| userId: { | |
| type: mongoose.Schema.Types.ObjectId, | |
| ref: 'User', | |
| required: true, | |
| index: true | |
| }, | |
| sessionId: { | |
| type: String, | |
| required: true, | |
| index: true | |
| }, | |
| // Chat metadata | |
| title: { | |
| type: String, | |
| default: 'New Chat', | |
| maxlength: 100 | |
| }, | |
| tags: [String], // e.g., ['homework', 'schedule', 'uga-info'] | |
| // Messages array | |
| messages: [{ | |
| role: { | |
| type: String, | |
| enum: ['user', 'assistant', 'system'], | |
| required: true | |
| }, | |
| content: { | |
| type: String, | |
| required: true | |
| }, | |
| timestamp: { | |
| type: Date, | |
| default: Date.now | |
| }, | |
| metadata: { | |
| model: String, // which AI model was used | |
| tokens: Number, | |
| responseTime: Number, // ms | |
| hasWebSearch: Boolean, | |
| searchQueries: [String], | |
| confidence: Number | |
| } | |
| }], | |
| // Chat session info | |
| startTime: { | |
| type: Date, | |
| default: Date.now | |
| }, | |
| lastMessage: { | |
| type: Date, | |
| default: Date.now | |
| }, | |
| messageCount: { | |
| type: Number, | |
| default: 0 | |
| }, | |
| // AI interaction tracking | |
| aiModel: { | |
| type: String, | |
| default: 'llama3' | |
| }, | |
| totalTokens: { | |
| type: Number, | |
| default: 0 | |
| }, | |
| webSearchUsed: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| // Status and organization | |
| isArchived: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| isFavorite: { | |
| type: Boolean, | |
| default: false | |
| }, | |
| category: { | |
| type: String, | |
| enum: ['academic', 'personal', 'research', 'help', 'other'], | |
| default: 'other' | |
| } | |
| }, { | |
| timestamps: true | |
| }); | |
| // Compound indexes for efficient querying | |
| chatHistorySchema.index({ userId: 1, lastMessage: -1 }); | |
| chatHistorySchema.index({ userId: 1, category: 1, lastMessage: -1 }); | |
| chatHistorySchema.index({ userId: 1, tags: 1 }); | |
| // Virtual for formatted duration | |
| chatHistorySchema.virtual('duration').get(function() { | |
| if (this.lastMessage && this.startTime) { | |
| return this.lastMessage - this.startTime; | |
| } | |
| return 0; | |
| }); | |
| // Method to add a message | |
| chatHistorySchema.methods.addMessage = function(role, content, metadata = {}) { | |
| this.messages.push({ | |
| role, | |
| content, | |
| metadata | |
| }); | |
| this.lastMessage = new Date(); | |
| this.messageCount = this.messages.length; | |
| // Auto-generate title from first user message | |
| if (!this.title || this.title === 'New Chat') { | |
| if (role === 'user' && content.length > 0) { | |
| this.title = content.substring(0, 50) + (content.length > 50 ? '...' : ''); | |
| } | |
| } | |
| // Update token count | |
| if (metadata.tokens) { | |
| this.totalTokens += metadata.tokens; | |
| } | |
| // Track web search usage | |
| if (metadata.hasWebSearch) { | |
| this.webSearchUsed = true; | |
| } | |
| return this.save(); | |
| }; | |
| // Static method to get user's recent chats | |
| chatHistorySchema.statics.getRecentChats = function(userId, limit = 10) { | |
| return this.find({ userId, isArchived: false }) | |
| .sort({ lastMessage: -1 }) | |
| .limit(limit) | |
| .select('title lastMessage messageCount category tags isFavorite'); | |
| }; | |
| // Static method to search chats | |
| chatHistorySchema.statics.searchChats = function(userId, query, options = {}) { | |
| const searchCriteria = { | |
| userId, | |
| $or: [ | |
| { title: { $regex: query, $options: 'i' } }, | |
| { tags: { $regex: query, $options: 'i' } }, | |
| { 'messages.content': { $regex: query, $options: 'i' } } | |
| ] | |
| }; | |
| if (options.category) { | |
| searchCriteria.category = options.category; | |
| } | |
| if (options.startDate || options.endDate) { | |
| searchCriteria.lastMessage = {}; | |
| if (options.startDate) searchCriteria.lastMessage.$gte = options.startDate; | |
| if (options.endDate) searchCriteria.lastMessage.$lte = options.endDate; | |
| } | |
| return this.find(searchCriteria) | |
| .sort({ lastMessage: -1 }) | |
| .limit(options.limit || 20); | |
| }; | |
| module.exports = mongoose.model('ChatHistory', chatHistorySchema); |