From faa1f9e9f014cd87e6722baa3d1e5995912165b8 Mon Sep 17 00:00:00 2001 From: kicer Date: Fri, 4 Sep 2026 07:13:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E4=BC=9A=E8=AF=9D=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E8=80=85=E5=AD=97=E6=AE=B5=EF=BC=9A=E5=BB=BA=E6=A1=A3?= =?UTF-8?q?=E6=97=B6=E8=AE=B0=E5=BD=95=20req.user=EF=BC=8C=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8C=89=E5=BD=93=E5=89=8D=E7=94=A8=E6=88=B7=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=EF=BC=88=E6=9C=AA=E5=90=AF=E7=94=A8=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E4=B8=8D=E8=BF=87=E6=BB=A4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/db.js | 6 ++++++ app/server/engine.js | 6 +++--- app/server/routes/sessions.js | 7 ++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/server/db.js b/app/server/db.js index e7ffdb1..bffd0d7 100644 --- a/app/server/db.js +++ b/app/server/db.js @@ -18,6 +18,7 @@ function initDb(dbPath) { model_id TEXT, last_model_id TEXT, assistant_type TEXT DEFAULT 'secretary', + user TEXT DEFAULT '', created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, completed_at INTEGER, @@ -100,6 +101,11 @@ function initDb(dbPath) { if (!cols.some(c => c.name === 'assistant_type')) { db.exec("ALTER TABLE sessions ADD COLUMN assistant_type TEXT DEFAULT 'secretary'"); } + // 迁移:user 记录建档时的登录用户,列表接口据此按用户隔离; + // 存量老会话与未启用认证时建的会话为空串(见 routes/sessions.js 列表过滤) + if (!cols.some(c => c.name === 'user')) { + db.exec("ALTER TABLE sessions ADD COLUMN user TEXT DEFAULT ''"); + } return db; } diff --git a/app/server/engine.js b/app/server/engine.js index 06b7320..656598e 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -681,13 +681,13 @@ function loadHistoryForModel(sessionId) { .concat(rows.filter(r => r.name !== SESSION_CONTEXT_NAME)); } -function createSession(title, preferModelId, assistantType) { +function createSession(title, preferModelId, assistantType, user) { const id = genTaskId(); const dir = taskDirOf(id); fs.mkdirSync(dir, { recursive: true }); db.prepare( - "INSERT INTO sessions (id, title, status, phase, model_id, assistant_type, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?,?)" - ).run(id, title || '', assistantType || 'secretary', now(), now()); + "INSERT INTO sessions (id, title, status, phase, model_id, assistant_type, user, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?,?,?)" + ).run(id, title || '', assistantType || 'secretary', user || '', now(), now()); // 指定了模型且它恰好空闲 → 直接绑定,跳过排队;否则回退到 FIFO 队列 const prefer = preferModelId ? db.prepare('SELECT * FROM model_pool WHERE id = ? AND is_busy = 0').get(preferModelId) diff --git a/app/server/routes/sessions.js b/app/server/routes/sessions.js index 7b6c1eb..1e545a8 100644 --- a/app/server/routes/sessions.js +++ b/app/server/routes/sessions.js @@ -51,7 +51,9 @@ router.post('/', wrap((req, res) => { const modelId = (req.body && req.body.model_id || '').trim(); // 助手类型建档时锁定,后续不可变更;缺省按文秘助理 const assistantType = String((req.body && req.body.assistant_type) || 'secretary').trim().slice(0, 32) || 'secretary'; - const session = engine.createSession(title, modelId, assistantType); + // 创建者取认证中间件的 req.user(不信客户端传参);未启用认证时为空串 + const user = String(req.user || '').trim().slice(0, 64); + const session = engine.createSession(title, modelId, assistantType, user); log.info('[task] ' + (req.user || '-') + ' 新建任务 ' + session.id + '「' + (session.title || '未命名') + '」model=' + (modelId || '自动') + ' type=' + (session.assistant_type || 'secretary')); res.json(session); })); @@ -72,6 +74,9 @@ router.get('/', wrap((req, res) => { const where = []; const args = []; + // 按创建者隔离:已登录只列自己的会话;未启用认证(req.user 空)不过滤,保持自用场景全可见 + const me = String(req.user || '').trim(); + if (me) { where.push('user = ?'); args.push(me); } if (status === 'active') where.push("status IN ('queued','active','held')"); else if (status === 'done') where.push("status IN ('completed','cancelled')"); if (q) {