llm-chat/backend/models/Conversation.js

167 lines
4.1 KiB
JavaScript

const database = require('./database');
class Conversation {
constructor(data = {}) {
this.id = data.id;
this.title = data.title;
this.created_at = data.created_at;
this.updated_at = data.updated_at;
}
/**
* 创建新对话
*/
static async create(title = '新对话') {
try {
const sql = `
INSERT INTO conversations (title, created_at, updated_at)
VALUES (?, datetime('now'), datetime('now'))
`;
const result = await database.run(sql, [title]);
// 返回新创建的对话
return await Conversation.findById(result.id);
} catch (error) {
console.error('创建对话失败:', error);
throw error;
}
}
/**
* 根据ID查找对话
*/
static async findById(id) {
try {
const sql = 'SELECT * FROM conversations WHERE id = ?';
const row = await database.get(sql, [id]);
return row ? new Conversation(row) : null;
} catch (error) {
console.error('查找对话失败:', error);
throw error;
}
}
/**
* 获取所有对话列表(按更新时间倒序)
*/
static async findAll(limit = 50) {
try {
const sql = `
SELECT c.*,
m.content as last_message,
m.timestamp as last_message_time
FROM conversations c
LEFT JOIN messages m ON c.id = m.conversation_id
WHERE m.id = (
SELECT MAX(id) FROM messages WHERE conversation_id = c.id
) OR m.id IS NULL
ORDER BY c.updated_at DESC
LIMIT ?
`;
const rows = await database.all(sql, [limit]);
return rows.map(row => ({
...new Conversation(row),
last_message: row.last_message,
last_message_time: row.last_message_time
}));
} catch (error) {
console.error('获取对话列表失败:', error);
throw error;
}
}
/**
* 更新对话标题
*/
static async updateTitle(id, title) {
try {
const sql = `
UPDATE conversations
SET title = ?, updated_at = datetime('now')
WHERE id = ?
`;
const result = await database.run(sql, [title, id]);
if (result.changes === 0) {
throw new Error('对话不存在');
}
return await Conversation.findById(id);
} catch (error) {
console.error('更新对话标题失败:', error);
throw error;
}
}
/**
* 更新对话的最后活动时间
*/
static async updateLastActivity(id) {
try {
const sql = `
UPDATE conversations
SET updated_at = datetime('now')
WHERE id = ?
`;
await database.run(sql, [id]);
} catch (error) {
console.error('更新对话活动时间失败:', error);
throw error;
}
}
/**
* 删除对话(级联删除所有相关消息)
*/
static async delete(id) {
try {
const sql = 'DELETE FROM conversations WHERE id = ?';
const result = await database.run(sql, [id]);
if (result.changes === 0) {
throw new Error('对话不存在');
}
return true;
} catch (error) {
console.error('删除对话失败:', error);
throw error;
}
}
/**
* 获取对话的消息数量
*/
static async getMessageCount(conversationId) {
try {
const sql = 'SELECT COUNT(*) as count FROM messages WHERE conversation_id = ?';
const result = await database.get(sql, [conversationId]);
return result.count;
} catch (error) {
console.error('获取消息数量失败:', error);
throw error;
}
}
/**
* 批量删除对话
*/
static async deleteMultiple(ids) {
try {
if (!Array.isArray(ids) || ids.length === 0) {
return 0;
}
const placeholders = ids.map(() => '?').join(',');
const sql = `DELETE FROM conversations WHERE id IN (${placeholders})`;
const result = await database.run(sql, ids);
return result.changes;
} catch (error) {
console.error('批量删除对话失败:', error);
throw error;
}
}
}
module.exports = Conversation;