From c184537c2209102e29367673b7d6c68f0f2d1539 Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 29 Sep 2026 12:37:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(schedule):=20M4=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E9=85=8D=E5=A5=97=E2=80=94=E2=80=94=E8=B7=A8?= =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E5=85=A8=E8=A7=88=20op=3Dlist=5Fall=20+=20?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=20/tasks=20=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户第 6 点(在当前会话读到他所有定时任务并管理)的两种触达: - scheduler.listUserTasks:join sessions 按 user 全览(下次执行排序),两条链路同源数据。 - schedule 工具加 op=list_all:用户问「我都有哪些闹钟」AI 自取;只读全览不拆生命周期—— update/delete 仍绑各自会话,清单每行带会话标题+id,异会话任务引导用户切过去操作 (网页打开会话 / 微信 /switch <会话id>)。default 提示行同步。 - 微信 /tasks 命令(与 /list 同族、不依赖绑定会话):markdown 全览 brief/cron/下次/上次, 尾部引导「改删先 /switch 过去;删会话连带删任务」;/help 收录。 探针 7 项全过(list_all 隔离/标题id/本会话标记、/tasks 隔离/引导/空态、/help 收录)。 定时即下班(上一笔)后每任务天然独占一会话,create 撞主键仅剩手动 delete 重建路径。 --- app/server/routes/channel.js | 23 ++++++++++++++++++++++- app/server/scheduler.js | 11 ++++++++++- app/server/tools/definitions.json | 5 +++-- app/server/tools/executor.js | 19 ++++++++++++++++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/app/server/routes/channel.js b/app/server/routes/channel.js index 3dc1ecb..45c9db4 100644 --- a/app/server/routes/channel.js +++ b/app/server/routes/channel.js @@ -11,6 +11,7 @@ // /new 尾随文本与普通消息是真实内容,照常入库。 const express = require('express'); const engine = require('../engine'); +const scheduler = require('../scheduler'); // 只依赖 log/cron-parser,无环;/tasks 与 schedule op=list_all 同源 const log = require('../log'); const router = express.Router(); @@ -113,6 +114,21 @@ function listReply(db, user, curId) { return ['**会话列表**'].concat(lines).join('\n'); } +// 定时任务全览(/tasks):跨会话列出本用户的周期任务(schedule op=list_all 同源数据)。 +// 生命周期绑会话不拆:每行尾部标出会话 id,改删先 /switch 过去再让 AI 操作(与 /list 编号同源)。 +function tasksReply(db, user, curId) { + const rows = scheduler.listUserTasks(db, user); + if (!rows.length) return '**定时任务**\n还没有任何定时任务(聊天里让 AI 设个闹钟即可)'; + const lines = rows.map(t => { + const last = t.last_run_at ? fmtTime(t.last_run_at) + ' ' + (t.last_status || '-') : '尚未执行'; + return '- `' + (t.brief || String(t.prompt).slice(0, 24) + '…') + '` · cron `' + t.cron_expr + '` · 下次 ' + fmtTime(t.next_run_at) + + ' · 上次 ' + last + + (t.session_id === curId ? '(本会话)' : '|会话 `' + t.session_id + '`'); + }); + lines.push('修改/取消:先 /switch 到对应会话,再让 AI 处理;删会话会连带删它的任务'); + return ['**定时任务**'].concat(lines).join('\n'); +} + // ---------- 模型清单(/models) ---------- // cfg.models 按 key 展开(同平台多 key 每个模型重复多份),按 id 去重保配置序; // enable:false 不列(bind 会被 engine 拒,列出来只会诱导白点)。上限 15 行,同样为回执占额度。 @@ -143,7 +159,7 @@ function modelsReply(ms, curModelId) { // 严格匹配:整条消息恰好是命令词(ARG_CMDS 内的可带参数)才算命令。 // 首词形如 /纯ASCII词 但不在清单 → 回「未知命令」提示、不转发模型(防弱模型就着 /hlep 演一段); // 其余一律普通消息:/home/x、/a.b 这类路径代码,以及 /clear abc 这种裸命令带尾巴的(用户约定:强检查不匹配)。 -const CMDS = new Set(['new', 'stop', 'compact', 'clear', 'status', 'help', 'list', 'switch', 'rename', 'delete', 'models']); +const CMDS = new Set(['new', 'stop', 'compact', 'clear', 'status', 'help', 'list', 'tasks', 'switch', 'rename', 'delete', 'models']); // 可带参数的命令:/new 尾随文本作首条消息;/switch /delete 吃 /list 编号(或会话 id 前缀); // /rename 吃新标题;/models 可带模型编号或完整 id。清单外的命令带尾巴仍按普通消息回落。 const ARG_CMDS = new Set(['new', 'switch', 'rename', 'delete', 'models']); @@ -166,6 +182,7 @@ const HELP_TEXT = [ '**可用命令**', '- `/new [msg]` 新建会话并切换', '- `/list` 会话列表', + '- `/tasks` 定时任务全览', '- `/switch <编号>` 切换当前会话', '- `/rename <标题>` 重命名当前会话', '- `/delete <编号>` 删除指定会话', @@ -259,6 +276,10 @@ function handleSend(db, opts) { return { kind: 'command', reply: listReply(db, user, s ? s.id : null), session_id: s ? s.id : null }; } + if (cmd === 'tasks') { + return { kind: 'command', reply: tasksReply(db, user, s ? s.id : null), session_id: s ? s.id : null }; + } + if (cmd === 'switch') { if (!parsed.rest) { return { kind: 'command', reply: '用法:/list 查看编号,再 /switch <编号>', session_id: s ? s.id : null }; diff --git a/app/server/scheduler.js b/app/server/scheduler.js index bb6b01f..c914c76 100644 --- a/app/server/scheduler.js +++ b/app/server/scheduler.js @@ -75,6 +75,15 @@ function createTask(db, sessionId, cronExpr, prompt, brief) { // 仅当改了 cron 才重算 next_run_at。不校验 running 状态(update 天然安全, // delete 也不打断正在跑的回合)。 +// 跨会话全览(schedule op=list_all 与微信 /tasks 命令同源数据):join sessions 带出标题, +// 读清单的人据此找到任务住在哪个会话再切过去操作;改删仍绑定各自会话(生命周期不拆)。 +function listUserTasks(db, user) { + return db.prepare( + 'SELECT t.session_id, t.cron_expr, t.brief, t.prompt, t.next_run_at, t.last_run_at, t.last_status, t.last_note, s.title' + + ' FROM scheduled_tasks t JOIN sessions s ON s.id = t.session_id WHERE s.user = ? ORDER BY t.next_run_at ASC' + ).all(String(user || '')); +} + function getTask(db, sessionId) { return db.prepare( 'SELECT cron_expr, prompt, next_run_at, last_run_at, last_status, last_note FROM scheduled_tasks WHERE session_id = ?' @@ -193,4 +202,4 @@ function start(engine) { if (timer.unref) timer.unref(); // 不阻止进程退出 } -module.exports = { start, createTask, getTask, updateTask, deleteTask, computeNext, validateCron, fmtTime }; +module.exports = { start, createTask, getTask, listUserTasks, updateTask, deleteTask, computeNext, validateCron, fmtTime }; diff --git a/app/server/tools/definitions.json b/app/server/tools/definitions.json index 842b6d6..16164b6 100644 --- a/app/server/tools/definitions.json +++ b/app/server/tools/definitions.json @@ -275,7 +275,7 @@ "type": "function", "function": { "name": "schedule", - "description": "Manage the recurring scheduled task bound to the CURRENT session. At most one task per session; it is deleted together with the session. At each trigger, the prompt is sent to this session as a new user message. Schedule times are interpreted in Asia/Shanghai (UTC+8). Call this ONLY when the user explicitly asks for something to run periodically or at a specific time.\n\nop=get No arguments.\nop=create Requires cron_expr, prompt and brief.\nop=update Requires at least one of cron_expr, prompt; only the given fields change.\nop=delete No arguments.", + "description": "Manage the recurring scheduled task bound to the CURRENT session. At most one task per session; it is deleted together with the session. At each trigger, the prompt is sent to this session as a new user message. Schedule times are interpreted in Asia/Shanghai (UTC+8). Call this ONLY when the user explicitly asks for something to run periodically or at a specific time.\n\nop=get No arguments.\nop=list_all No arguments. Lists every scheduled task of the current user across all sessions (update/delete still act on the CURRENT session only; other rows name their session id so the user can switch there).\nop=create Requires cron_expr, prompt and brief.\nop=update Requires at least one of cron_expr, prompt; only the given fields change.\nop=delete No arguments.", "parameters": { "type": "object", "required": [ @@ -288,7 +288,8 @@ "get", "create", "update", - "delete" + "delete", + "list_all" ], "description": "Operation to perform on the current session's scheduled task." }, diff --git a/app/server/tools/executor.js b/app/server/tools/executor.js index bfd5f94..d6c6629 100644 --- a/app/server/tools/executor.js +++ b/app/server/tools/executor.js @@ -799,12 +799,29 @@ function toolScheduleTask(ctx, args) { }); return '定时任务已更新:' + t.cron_expr + ',下次执行 ' + t.next_text + '(Asia/Shanghai)。'; } + case 'list_all': { + // 跨会话全览(用户问「我都有哪些定时任务」时的唯一入口):只读不拆生命周期—— + // update/delete 仍只作用本会话,其余任务靠清单里的会话 id 引导用户切过去操作。 + const me = db.prepare('SELECT user FROM sessions WHERE id = ?').get(ctx.sessionId); + const rows = scheduler.listUserTasks(db, (me && me.user) || ''); + if (!rows.length) return '你目前没有任何定时任务。要创建用 op=create(本会话' + (scheduler.getTask(db, ctx.sessionId) ? '已有任务,需先 delete' : '当前没有任务,可直接建') + ')。'; + const lines = rows.map(t => { + const last = t.last_run_at + ? scheduler.fmtTime(t.last_run_at) + '(' + (t.last_status || '-') + (t.last_note ? ',' + t.last_note : '') + ')' + : '尚未执行'; + return '- 「' + (t.brief || String(t.prompt).slice(0, 40)) + '」 cron=' + t.cron_expr + + ',下次 ' + scheduler.fmtTime(t.next_run_at) + ',上次 ' + last + + ',会话「' + (t.title || t.session_id) + '」' + t.session_id + + (t.session_id === ctx.sessionId ? '(本会话,可直接 op=update/delete)' : '(要改删请让用户先切到该会话:网页打开它,微信发 /switch ' + t.session_id + ')'); + }); + return '全部定时任务(' + rows.length + ' 个,按下次执行排序):\n' + lines.join('\n'); + } case 'delete': { const old = scheduler.deleteTask(db, ctx.sessionId); return '定时任务已删除(原定 ' + old + ')。要建新任务用 op=create。'; } default: - throw new Error('未知的 op:"' + op + '"。可用:get / create(需 cron_expr、prompt、brief)/ update(需 cron_expr、prompt 至少一个)/ delete'); + throw new Error('未知的 op:"' + op + '"。可用:get / create(需 cron_expr、prompt、brief)/ update(需 cron_expr、prompt 至少一个)/ delete / list_all(跨会话全览)'); } }