Source

models/Chat.model.js

import { Intents } from "@typez/intent/enums";
import { ObjectId } from "mongodb";
import mongoose, { Schema } from "mongoose";
/**
 * Embedded schema representing a user entry in the chat history.
 * @category Models
 */
const UserSchema = new Schema({
    id: { type: ObjectId, unique: false },
    content: { type: String },
    createdAt: { type: Date, default: Date.now },
}, { _id: false });
/**
 * Embedded schema representing assistant responses in the chat history.
 * @category Models
 */
const AssistantSchema = new Schema({
    content: { type: String, required: true },
    content_for_llm: { type: String },
    metadata: { type: String },
    createdAt: { type: Date, default: Date.now },
    lessonWithoutInvestigation: { type: String, default: null },
}, { _id: false });
/**
 * Chat history entry schema combining user and assistant messages.
 * @category Models
 */
const ChatHistorySchema = new Schema({
    user: { type: UserSchema, required: true },
    assistant: { type: AssistantSchema, required: true },
    intent: { type: String, enum: Object.values(Intents) },
    hidden: { type: Boolean, default: false }, // If true, this message should not be visible in chat UI
}, { _id: false });
/**
 * Root schema representing a chat session and associated investigation reference.
 * @category Models
 */
const ChatSchema = new Schema({
    history: { type: [ChatHistorySchema], required: true },
    investigationId: { type: ObjectId, required: false, default: null },
});
/* Indexes for better performance */
ChatSchema.index({ investigationId: 1 });
/**
 * Chat model used to persist conversation history.
 * @category Models
 */
export const Chat = mongoose.model("Chat", ChatSchema);