Files
agent/app/server/db.js
T
2026-09-02 11:17:43 +08:00

96 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SQLite 数据层。表结构见 README 第九章;本模块同时提供常用查询。
const Database = require('better-sqlite3');
const path = require('path');
const fs = require('fs');
function initDb(dbPath) {
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
db.exec(`
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
title TEXT DEFAULT '',
status TEXT NOT NULL,
phase TEXT NOT NULL DEFAULT 'planning',
model_id TEXT,
last_model_id TEXT,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
completed_at INTEGER,
cancelled_at INTEGER,
held_at INTEGER
);
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT,
tool_calls TEXT,
tool_call_id TEXT,
name TEXT,
created_at INTEGER NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions(id)
);
CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
filename TEXT NOT NULL,
size INTEGER,
created_at INTEGER NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions(id)
);
CREATE TABLE IF NOT EXISTS model_pool (
id TEXT PRIMARY KEY,
display_name TEXT,
is_busy INTEGER DEFAULT 0,
current_session_id TEXT
);
CREATE TABLE IF NOT EXISTS wait_queue (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
queued_at INTEGER NOT NULL,
FOREIGN KEY (session_id) REFERENCES sessions(id)
);
-- 杂项持久计数/标记(如每日任务序号 task_seq:YYYYMMDD,删除会话后不回退复用)
CREATE TABLE IF NOT EXISTS meta (
key TEXT PRIMARY KEY,
value TEXT
);
-- 模型 token 用量流水(每次模型回合一条)。故意不建会话外键:
-- 删会话后累计统计仍需保留
CREATE TABLE IF NOT EXISTS model_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT NOT NULL,
model_id TEXT NOT NULL,
prompt_tokens INTEGER DEFAULT 0,
completion_tokens INTEGER DEFAULT 0,
total_tokens INTEGER DEFAULT 0,
created_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_messages_session ON messages(session_id, id);
CREATE INDEX IF NOT EXISTS idx_files_session ON files(session_id);
CREATE INDEX IF NOT EXISTS idx_model_usage_model_time ON model_usage(model_id, created_at);
`);
// 迁移:旧库补 held_at 列(held 保持租约起始时间)
const cols = db.prepare('PRAGMA table_info(sessions)').all();
if (!cols.some(c => c.name === 'held_at')) db.exec('ALTER TABLE sessions ADD COLUMN held_at INTEGER');
// 迁移:last_model_id 记住会话最近用过的模型,释放后新消息优先回绑(见 engine.dispatchQueued)
if (!cols.some(c => c.name === 'last_model_id')) {
db.exec('ALTER TABLE sessions ADD COLUMN last_model_id TEXT');
db.exec('UPDATE sessions SET last_model_id = model_id WHERE last_model_id IS NULL AND model_id IS NOT NULL');
}
return db;
}
module.exports = { initDb };