feat: 添加realtime_dialog和realtime_dialog_external_rag_test项目,更新test2项目
This commit is contained in:
@@ -2,6 +2,23 @@ const mysql = require('mysql2/promise');
|
||||
|
||||
let pool = null;
|
||||
|
||||
async function ensureColumnExists(tableName, columnName, definitionSql) {
|
||||
const [rows] = await pool.query(`SHOW COLUMNS FROM \`${tableName}\` LIKE ?`, [columnName]);
|
||||
if (rows.length === 0) {
|
||||
await pool.execute(`ALTER TABLE \`${tableName}\` ADD COLUMN ${definitionSql}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function migrateSchema() {
|
||||
await pool.execute("ALTER TABLE `sessions` MODIFY COLUMN `mode` ENUM('voice', 'chat') DEFAULT 'chat'");
|
||||
await pool.execute("ALTER TABLE `messages` MODIFY COLUMN `role` ENUM('user', 'assistant', 'tool', 'system') NOT NULL");
|
||||
await pool.execute("ALTER TABLE `messages` MODIFY COLUMN `source` ENUM('voice_asr', 'voice_bot', 'voice_tool', 'chat_user', 'chat_bot') NOT NULL");
|
||||
await ensureColumnExists('messages', 'tool_name', '`tool_name` VARCHAR(64) NULL AFTER `source`');
|
||||
await ensureColumnExists('messages', 'meta_json', '`meta_json` JSON NULL AFTER `tool_name`');
|
||||
await ensureColumnExists('messages', 'created_at', '`created_at` BIGINT NULL AFTER `tool_name`');
|
||||
await ensureColumnExists('sessions', 'updated_at', '`updated_at` BIGINT NULL AFTER `created_at`');
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 MySQL 连接池 + 自动建表
|
||||
*/
|
||||
@@ -50,12 +67,15 @@ async function initialize() {
|
||||
content TEXT NOT NULL,
|
||||
source ENUM('voice_asr', 'voice_bot', 'voice_tool', 'chat_user', 'chat_bot') NOT NULL,
|
||||
tool_name VARCHAR(64),
|
||||
meta_json JSON,
|
||||
created_at BIGINT,
|
||||
INDEX idx_session (session_id),
|
||||
INDEX idx_session_time (session_id, created_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
`);
|
||||
|
||||
await migrateSchema();
|
||||
|
||||
console.log(`[DB] MySQL connected: ${dbName}, tables ready`);
|
||||
return pool;
|
||||
}
|
||||
@@ -93,22 +113,23 @@ async function getSession(sessionId) {
|
||||
|
||||
// ==================== Messages ====================
|
||||
|
||||
async function addMessage(sessionId, role, content, source, toolName = null) {
|
||||
async function addMessage(sessionId, role, content, source, toolName = null, meta = null) {
|
||||
if (!content || content.trim() === '') return null;
|
||||
const now = Date.now();
|
||||
const metaJson = meta == null ? null : JSON.stringify(meta);
|
||||
const [result] = await pool.execute(
|
||||
'INSERT INTO messages (session_id, role, content, source, tool_name, created_at) VALUES (?, ?, ?, ?, ?, ?)',
|
||||
[sessionId, role, content, source, toolName, now]
|
||||
'INSERT INTO messages (session_id, role, content, source, tool_name, meta_json, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[sessionId, role, content, source, toolName, metaJson, now]
|
||||
);
|
||||
// 更新 session 时间
|
||||
await pool.execute('UPDATE sessions SET updated_at=? WHERE id=?', [now, sessionId]);
|
||||
return { id: result.insertId, session_id: sessionId, role, content, source, tool_name: toolName, created_at: now };
|
||||
return { id: result.insertId, session_id: sessionId, role, content, source, tool_name: toolName, meta_json: metaJson, created_at: now };
|
||||
}
|
||||
|
||||
async function getMessages(sessionId, limit = 20) {
|
||||
const safeLimit = Math.max(1, Math.min(parseInt(limit) || 20, 100));
|
||||
const [rows] = await pool.query(
|
||||
'SELECT role, content, source, tool_name, created_at FROM messages WHERE session_id=? ORDER BY created_at ASC LIMIT ?',
|
||||
'SELECT role, content, source, tool_name, meta_json, created_at FROM messages WHERE session_id=? ORDER BY created_at ASC LIMIT ?',
|
||||
[sessionId, safeLimit]
|
||||
);
|
||||
return rows;
|
||||
@@ -118,7 +139,7 @@ async function getRecentMessages(sessionId, limit = 20) {
|
||||
// 获取最近 N 条,按时间正序返回
|
||||
const safeLimit = Math.max(1, Math.min(parseInt(limit) || 20, 100));
|
||||
const [rows] = await pool.query(
|
||||
`SELECT role, content, source, tool_name, created_at FROM messages
|
||||
`SELECT role, content, source, tool_name, meta_json, created_at FROM messages
|
||||
WHERE session_id=? ORDER BY created_at DESC LIMIT ?`,
|
||||
[sessionId, safeLimit]
|
||||
);
|
||||
@@ -136,6 +157,38 @@ async function getHistoryForLLM(sessionId, limit = 20) {
|
||||
.map(m => ({ role: m.role, content: m.content }));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话列表(按更新时间倒序,带最后一条消息预览)
|
||||
*/
|
||||
async function getSessionList(userId, limit = 50) {
|
||||
const safeLimit = Math.max(1, Math.min(parseInt(limit) || 50, 200));
|
||||
let query;
|
||||
let params;
|
||||
if (userId) {
|
||||
query = `SELECT s.id, s.user_id, s.mode, s.created_at, s.updated_at,
|
||||
(SELECT content FROM messages WHERE session_id = s.id ORDER BY created_at DESC LIMIT 1) AS last_message,
|
||||
(SELECT COUNT(*) FROM messages WHERE session_id = s.id) AS message_count
|
||||
FROM sessions s WHERE s.user_id = ? ORDER BY s.updated_at DESC LIMIT ?`;
|
||||
params = [userId, safeLimit];
|
||||
} else {
|
||||
query = `SELECT s.id, s.user_id, s.mode, s.created_at, s.updated_at,
|
||||
(SELECT content FROM messages WHERE session_id = s.id ORDER BY created_at DESC LIMIT 1) AS last_message,
|
||||
(SELECT COUNT(*) FROM messages WHERE session_id = s.id) AS message_count
|
||||
FROM sessions s ORDER BY s.updated_at DESC LIMIT ?`;
|
||||
params = [safeLimit];
|
||||
}
|
||||
const [rows] = await pool.query(query, params);
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话及其所有消息
|
||||
*/
|
||||
async function deleteSession(sessionId) {
|
||||
await pool.execute('DELETE FROM messages WHERE session_id = ?', [sessionId]);
|
||||
await pool.execute('DELETE FROM sessions WHERE id = ?', [sessionId]);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initialize,
|
||||
getPool,
|
||||
@@ -146,4 +199,6 @@ module.exports = {
|
||||
getMessages,
|
||||
getRecentMessages,
|
||||
getHistoryForLLM,
|
||||
getSessionList,
|
||||
deleteSession,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user