diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index f6de75f..963b79a 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -69,6 +69,32 @@ body { .badge.cancelled { background: #fdecea; color: #b3382c; } .session-del { color: #c33; background: none; border: none; font-size: 12px; padding: 6px; } +/* ---- 任务搜索 + 归档折叠(2.0) ---- */ +.search-bar { display: flex; gap: 8px; align-items: center; padding: 10px 12px 0; } +.search-bar input { + flex: 1; border: 1px solid #dde1ea; border-radius: 18px; padding: 8px 14px; + font-size: 14px; background: #fff; outline: none; color: #222; appearance: none; +} +.search-bar input:focus { border-color: #2a5299; } +.search-cancel { background: none; border: none; color: #2a5299; font-size: 14px; padding: 6px 4px; cursor: pointer; flex: none; } +.search-tip { color: #8891a5; font-size: 12px; padding: 2px 4px 8px; } +.done-head { + display: flex; align-items: center; justify-content: center; gap: 4px; + color: #77818f; font-size: 13px; padding: 10px 0 12px; cursor: pointer; user-select: none; +} +.done-head:active { color: #445; } +.done-head .chev { + width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; + stroke-linecap: round; stroke-linejoin: round; transition: transform 0.2s; +} +.done-head.open .chev { transform: rotate(180deg); } +.load-more { + width: 100%; background: none; border: 1px dashed #ccd2dd; border-radius: 10px; + color: #667; font-size: 13px; padding: 9px; margin-bottom: 10px; cursor: pointer; +} +.load-more:active { background: #eceef3; } +.load-more:disabled { opacity: 0.5; cursor: default; } + /* ---- 任务状态栏 ---- */ .phase-bar { display: flex; align-items: center; diff --git a/app/server/public/index.html b/app/server/public/index.html index dab9cf5..23c87c1 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -18,11 +18,18 @@

Agent 任务台

+
+
@@ -277,10 +284,10 @@ - - - - - + + + + + diff --git a/app/server/public/js/settings.js b/app/server/public/js/settings.js index e8b0bbe..1d342f9 100644 --- a/app/server/public/js/settings.js +++ b/app/server/public/js/settings.js @@ -60,7 +60,7 @@ function kvPlain(label, v) { function svcCard(st) { let html = '

服务状态' + (pendingKeys.size ? ' 待重启生效' : '') + '

'; - html += kvPlain('版本 / Node', st.version + (st.version_date ? '(' + st.version_date + ')' : '') + ' / ' + st.node); + html += kvPlain('版本 / Node', st.version + ' / ' + st.node); html += kvPlain('已运行', fmtUptime(st.uptime_ms) + '(启动于 ' + new Date(st.started_at).toLocaleString() + ')'); html += kvPlain('端口', st.port); html += kvPlain('配置文件', st.config_path); diff --git a/app/server/public/js/tasks.js b/app/server/public/js/tasks.js index 79e93b3..5b4f351 100644 --- a/app/server/public/js/tasks.js +++ b/app/server/public/js/tasks.js @@ -54,28 +54,107 @@ function showList() { }, { passive: true }); })(); -// ---------- 会话列表 ---------- +// ---------- 会话列表(活跃 + 归档折叠 + 搜索) ---------- +const DONE_PAGE = 20; // 归档每页条数 +let activeRows = []; // 当前活跃会话(含排队/保持) +let searchMode = false; // 搜索模式:列表区显示搜索结果 +let doneExpanded = false; // 归档区是否展开 +let doneRows = []; // 已加载的归档行 +let doneTotal = 0; // 归档总数 +let doneCursor = null; // 下一页游标 {updated_at, id} +let doneLoading = false; + +// 单张会话卡片(活跃/归档/搜索结果共用) +function sessionCard(s) { + const st = { queued: '排队中', active: '活跃中', held: '保持中', completed: '已完成', cancelled: '已终止' }[s.status] || s.status; + const badge = '' + st + (s.status === 'queued' && s.queue_pos ? ' #' + s.queue_pos : '') + ''; + return '
' + + '
' + + '
' + esc(s.title || s.id) + '
' + + '
' + esc(s.id) + '' + badge + + (s.model_id ? '' + esc(s.model_id) + '' : '') + '
' + + '
' + + '' + + '
'; +} + +function renderSessionList() { + const el = $('session-list'); + let html = activeRows.map(sessionCard).join(''); + if (doneTotal) { + html += '
' + + '已完成/已终止 (' + doneTotal + ')' + + '
'; + if (doneExpanded) { + html += doneRows.map(sessionCard).join(''); + if (doneRows.length < doneTotal) { + html += ''; + } + } + } else if (!activeRows.length) { + html += '
还没有任务,点右上角 + 新建
'; + } + el.innerHTML = html; +} + async function loadSessions() { + if (searchMode) return doSearch(); try { - const rows = await req('GET', '/api/sessions'); + const lists = await Promise.all([ + req('GET', '/api/sessions?status=active&limit=100'), + req('GET', '/api/sessions?status=done&limit=' + DONE_PAGE + '&with_total=1'), + ]); + activeRows = lists[0]; + doneRows = lists[1].rows; + doneTotal = lists[1].total; + const last = doneRows[doneRows.length - 1]; + doneCursor = last ? { updated_at: last.updated_at, id: last.id } : null; + renderSessionList(); + } catch (e) { toast('加载会话失败:' + e.message); } +} + +async function loadMoreDone() { + if (doneLoading || !doneCursor) return; + doneLoading = true; + renderSessionList(); // 按钮置灰 + try { + const r = await req('GET', '/api/sessions?status=done&limit=' + DONE_PAGE + '&with_total=1' + + '&before=' + encodeURIComponent(doneCursor.updated_at) + '&before_id=' + encodeURIComponent(doneCursor.id)); + doneRows = doneRows.concat(r.rows); + doneTotal = r.total; + const last = r.rows[r.rows.length - 1]; + if (last) doneCursor = { updated_at: last.updated_at, id: last.id }; + else doneTotal = doneRows.length; // 防御:数据变少导致翻页空转,直接隐藏按钮 + } catch (e) { toast('加载更多失败:' + e.message); } + doneLoading = false; + renderSessionList(); +} + +function toggleSearch() { + searchMode = !searchMode; + $('search-bar').classList.toggle('hidden', !searchMode); + const input = $('search-input'); + if (searchMode) { + input.focus(); + if (input.value.trim()) doSearch(); + } else { + input.value = ''; + loadSessions(); + } +} + +async function doSearch() { + const q = $('search-input').value.trim(); + if (!q) return loadSessions(); + try { + const r = await req('GET', '/api/sessions?q=' + encodeURIComponent(q) + '&limit=50&with_total=1'); const el = $('session-list'); - if (!rows.length) { - el.innerHTML = '
还没有任务,点右上角 + 新建
'; + if (!r.rows.length) { + el.innerHTML = '
没有找到包含「' + esc(q) + '」的任务
'; return; } - el.innerHTML = rows.map(s => { - const st = { queued: '排队中', active: '活跃中', held: '保持中', completed: '已完成', cancelled: '已终止' }[s.status] || s.status; - const badge = '' + st + (s.status === 'queued' && s.queue_pos ? ' #' + s.queue_pos : '') + ''; - return '
' + - '
' + - '
' + esc(s.title || s.id) + '
' + - '
' + esc(s.id) + '' + badge + - (s.model_id ? '' + esc(s.model_id) + '' : '') + '
' + - '
' + - '' + - '
'; - }).join(''); - } catch (e) { toast('加载会话失败:' + e.message); } + el.innerHTML = '
找到 ' + r.total + ' 个相关任务
' + r.rows.map(sessionCard).join(''); + } catch (e) { toast('搜索失败:' + e.message); } } // ---------- 消息渲染 ---------- @@ -459,6 +538,10 @@ const ui = { } catch (e) { toast('删除失败:' + e.message); } }, logout() { location.href = '/logout'; }, + toggleSearch() { toggleSearch(); }, + doSearch() { doSearch(); }, + toggleDone() { doneExpanded = !doneExpanded; renderSessionList(); }, + loadMoreDone() { return loadMoreDone(); }, }; // 文件面板展开时,点击面板以外的区域自动关闭 @@ -486,6 +569,17 @@ document.addEventListener('keydown', e => { } }); +// 搜索框:回车触发搜索;清空后退回普通列表 +document.addEventListener('keydown', e => { + if (e.target.id === 'search-input' && e.key === 'Enter' && !e.isComposing) { + e.preventDefault(); + doSearch(); + } +}); +document.addEventListener('input', e => { + if (e.target.id === 'search-input' && !e.target.value.trim()) loadSessions(); +}); + // ---------- Tab 注册 ---------- window.tabModules = window.tabModules || {}; tabModules.tasks = { diff --git a/app/server/routes/sessions.js b/app/server/routes/sessions.js index c030c94..0f38f4b 100644 --- a/app/server/routes/sessions.js +++ b/app/server/routes/sessions.js @@ -52,9 +52,41 @@ router.post('/', wrap((req, res) => { res.json(session); })); +// 列表查询参数(2.0): +// status=active|done|all 归档分组:active=排队/活跃/保持,done=已完成/已终止 +// q=关键词 标题或消息内容 LIKE 搜索 +// before + before_id 游标分页(上一页末行的 updated_at,id),避免 offset 翻页在 +// updated_at 变动时重复/漏行 +// with_total=1 返回 {rows,total}(归档折叠区需要总数);默认仍返回纯数组 router.get('/', wrap((req, res) => { const db = engine.getDb(); - const rows = db.prepare('SELECT * FROM sessions ORDER BY updated_at DESC LIMIT 50').all(); + const status = String(req.query.status || 'all'); + const q = String(req.query.q || '').trim(); + let limit = parseInt(req.query.limit, 10); + if (!Number.isFinite(limit) || limit < 1) limit = 50; + if (limit > 200) limit = 200; + + const where = []; + const args = []; + if (status === 'active') where.push("status IN ('queued','active','held')"); + else if (status === 'done') where.push("status IN ('completed','cancelled')"); + if (q) { + where.push('(title LIKE ? OR EXISTS(SELECT 1 FROM messages m WHERE m.session_id = sessions.id AND m.content LIKE ?))'); + args.push('%' + q + '%', '%' + q + '%'); + } + const before = req.query.before; + const beforeId = req.query.before_id; + if (before != null && before !== '' && beforeId) { + where.push('(updated_at < ? OR (updated_at = ? AND id < ?))'); + args.push(String(before), String(before), String(beforeId)); + } + const whereSql = where.length ? ' WHERE ' + where.join(' AND ') : ''; + + const stmt = db.prepare( + 'SELECT * FROM sessions' + whereSql + ' ORDER BY updated_at DESC, id DESC LIMIT ?' + ); + const rows = stmt.all.apply(stmt, args.concat([limit])); + const qpos = db.prepare( 'SELECT session_id, ROW_NUMBER() OVER (ORDER BY queued_at, id) AS pos FROM wait_queue' ).all(); @@ -63,6 +95,11 @@ router.get('/', wrap((req, res) => { for (const row of rows) { row.queue_pos = row.status === 'queued' ? (posMap[row.id] || null) : null; } + if (req.query.with_total === '1') { + const cnt = db.prepare('SELECT COUNT(*) AS n FROM sessions' + whereSql); + const total = cnt.all.apply(cnt, args)[0].n; + return res.json({ rows: rows, total: total }); + } res.json(rows); })); diff --git a/app/server/routes/system.js b/app/server/routes/system.js index 398d7c5..e631081 100644 --- a/app/server/routes/system.js +++ b/app/server/routes/system.js @@ -12,8 +12,8 @@ const router = express.Router(); const STARTED_AT = Date.now(); let VERSION = '2.0.0'; try { VERSION = require('../../package.json').version || VERSION; } catch (_) {} -// 附带 git 提交号(post-commit 钩子自动生成),免去手动改版本号 -if (versionInfo.commit) VERSION += '+' + versionInfo.commit; +// git 提交号单独返回(提交时 post-commit 钩子自动生成),前端只显示简洁版本号 +const VERSION_COMMIT = versionInfo.commit || ''; function sendErr(res, err) { const code = err && err.status_code || 500; @@ -31,6 +31,7 @@ router.get('/status', wrap((req, res) => { res.json({ ok: true, version: VERSION, + version_commit: VERSION_COMMIT, version_date: versionInfo.date, pid: process.pid, node: process.version, diff --git a/app/test/smoke.js b/app/test/smoke.js index ccbbb70..4b803b1 100644 --- a/app/test/smoke.js +++ b/app/test/smoke.js @@ -220,8 +220,33 @@ async function main() { const restored = await api('PUT', '/api/config', { text: origText }); ok(restored.ok && fs.readFileSync(cfgPath, 'utf8') === origText, 'PUT 整文件恢复原配置'); - // ---- 9. 技能管理(阶段 3)---- - console.log('\n[9] 技能管理'); + // ---- 9. 会话列表过滤/游标分页/搜索(2.0)---- + console.log('\n[9] 会话列表过滤与搜索'); + const uniqTitle = '冒烟-搜索专用-' + Date.now(); + const fs9 = await api('POST', '/api/sessions', { title: uniqTitle }); + ok(fs9 && fs9.id, '创建搜索专用会话'); + await api('POST', '/api/sessions/' + fs9.id + '/messages', { content: 'zzq-unique 检索内容 12345' }); + await waitSession(fs9.id, x => x.messages.some(m => m.content.indexOf('zzq-unique') >= 0), 8000, '消息落库'); + const st9 = (await api('GET', '/api/sessions/' + fs9.id)).session.status; + const actSet = await api('GET', '/api/sessions?status=active&limit=200'); + const doneSet = await api('GET', '/api/sessions?status=done&limit=200'); + ok(((st9 === 'completed' || st9 === 'cancelled') ? doneSet : actSet).some(x => x.id === fs9.id), '新会话按状态归入对应过滤', st9); + ok(!actSet.some(x => x.status === 'completed' || x.status === 'cancelled'), 'active 过滤不含归档会话'); + const withTot = await api('GET', '/api/sessions?status=done&limit=1&with_total=1'); + ok(Array.isArray(withTot.rows) && typeof withTot.total === 'number' && withTot.total >= withTot.rows.length, 'with_total 返回 {rows,total}'); + const pg1 = await api('GET', '/api/sessions?limit=1'); + ok(Array.isArray(pg1) && pg1.length === 1, '默认数组响应兼容旧格式(limit=1)'); + const pg2 = await api('GET', '/api/sessions?limit=1&before=' + encodeURIComponent(pg1[0].updated_at) + '&before_id=' + pg1[0].id); + ok(Array.isArray(pg2) && pg2.length === 1 && pg2[0].id !== pg1[0].id, '游标翻页取到不同的下一页'); + const qt = await api('GET', '/api/sessions?q=' + encodeURIComponent(uniqTitle)); + ok(qt.some(x => x.id === fs9.id), 'q= 按标题命中'); + const qc = await api('GET', '/api/sessions?q=zzq-unique'); + ok(qc.some(x => x.id === fs9.id), 'q= 按消息内容命中'); + const qnone = await api('GET', '/api/sessions?q=绝无此内容xyzzy-9x7'); + ok(Array.isArray(qnone) && qnone.length === 0, 'q= 无命中返回空'); + + // ---- 10. 技能管理(阶段 3)---- + console.log('\n[10] 技能管理'); ok((await api('POST', '/api/skills', { name: 'demo-skill', description: '演示技能' })).ok, '创建技能'); let slist = await api('GET', '/api/skills'); const demo = slist.find(x => x.name === 'demo-skill'); @@ -250,8 +275,8 @@ async function main() { slist = await api('GET', '/api/skills'); ok(!slist.some(x => x.name.indexOf('demo') === 0), '删除后列表干净'); - // ---- 10. 系统状态与优雅重启(阶段 2;放在最后,服务进程将退出收尾)---- - console.log('\n[10] 优雅重启'); + // ---- 11. 系统状态与优雅重启(阶段 2;放在最后,服务进程将退出收尾)---- + console.log('\n[11] 优雅重启'); const st = await api('GET', '/api/system/status'); ok(st.ok && st.started_at && st.port === 8099, 'GET /api/system/status'); s = await api('POST', '/api/sessions', { title: '冒烟-重启保护' });