feat(schedule): 任务按会话 id 全权代办 + op=list 改名,移除微信 /tasks 命令
用户拍板三点:/tasks 对用户是认知负担删掉(能力收进 schedule 工具);list_all 改名 list; get/update/delete 支持 id 寻址跨会话代办——绝不引导用户 /switch 切会话(用户不会切,AI 代办)。 - executor toolScheduleTask:target() 解析 id(省略/'0'=本会话,兼容旧调用;否则查存在+同用户, 不静默降级防拿错目标);get/update/delete 全线作用域化,回执标注(会话 xxx); create 收窄仅本会话(外部 id 直接报错说明「定时即下班」语义);list 输出行首挂 id=task-xxx、尾部指引改/删带 id 直接办,清除所有 /switch 文案; - scheduler:getTask 补 SELECT brief(M4 起 get 显示的「简介」一直在空转的真 bug, 本次探针揪出);update/delete 报错「本会话」字样中性化为「该会话」;listUserTasks 注释同步; - definitions.json:description 重写(任务以所属会话 id 指认;list 是代办入口;create 拒 id); enum list_all→list;新增 id 参数说明; - channel.js:/tasks 命令整套拆除(CMDS/HELP/handleSend 分支/tasksReply/scheduler require), 旧用户发 /tasks 会得到「不是有效命令 + /help 引导」自然退化。 探针 12 项全过:list 带 id 无切会话引导 / get·update·delete 跨会话 / 跨用户拒 / 查无会话报错 / create 拒外部 id / create 本会话+下班标记 / 无 id 与 id=0 兼容 / list_all 退役报错。
This commit is contained in:
@@ -11,7 +11,6 @@
|
||||
// /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();
|
||||
@@ -114,21 +113,6 @@ 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 行,同样为回执占额度。
|
||||
@@ -159,7 +143,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', 'tasks', 'switch', 'rename', 'delete', 'models']);
|
||||
const CMDS = new Set(['new', 'stop', 'compact', 'clear', 'status', 'help', 'list', 'switch', 'rename', 'delete', 'models']);
|
||||
// 可带参数的命令:/new 尾随文本作首条消息;/switch /delete 吃 /list 编号(或会话 id 前缀);
|
||||
// /rename 吃新标题;/models 可带模型编号或完整 id。清单外的命令带尾巴仍按普通消息回落。
|
||||
const ARG_CMDS = new Set(['new', 'switch', 'rename', 'delete', 'models']);
|
||||
@@ -182,7 +166,6 @@ const HELP_TEXT = [
|
||||
'**可用命令**',
|
||||
'- `/new [msg]` 新建会话并切换',
|
||||
'- `/list` 会话列表',
|
||||
'- `/tasks` 定时任务全览',
|
||||
'- `/switch <编号>` 切换当前会话',
|
||||
'- `/rename <标题>` 重命名当前会话',
|
||||
'- `/delete <编号>` 删除指定会话',
|
||||
@@ -276,10 +259,6 @@ 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 };
|
||||
|
||||
@@ -75,7 +75,7 @@ function createTask(db, sessionId, cronExpr, prompt, brief) {
|
||||
// 仅当改了 cron 才重算 next_run_at。不校验 running 状态(update 天然安全,
|
||||
// delete 也不打断正在跑的回合)。
|
||||
|
||||
// 跨会话全览(schedule op=list_all 与微信 /tasks 命令同源数据):join sessions 带出标题,
|
||||
// 跨会话全览(schedule op=list 的数据源):join sessions 带出标题,
|
||||
// 读清单的人据此找到任务住在哪个会话再切过去操作;改删仍绑定各自会话(生命周期不拆)。
|
||||
function listUserTasks(db, user) {
|
||||
return db.prepare(
|
||||
@@ -86,13 +86,13 @@ function listUserTasks(db, 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 = ?'
|
||||
'SELECT cron_expr, prompt, brief, next_run_at, last_run_at, last_status, last_note FROM scheduled_tasks WHERE session_id = ?'
|
||||
).get(sessionId) || null;
|
||||
}
|
||||
|
||||
function updateTask(db, sessionId, patch) {
|
||||
const row = db.prepare('SELECT cron_expr, next_run_at FROM scheduled_tasks WHERE session_id = ?').get(sessionId);
|
||||
if (!row) throw new Error('本会话没有定时任务,先用 op=create 创建');
|
||||
if (!row) throw new Error('该会话没有定时任务,要建用 op=create(只落当前会话)');
|
||||
let cron = row.cron_expr;
|
||||
let next = row.next_run_at;
|
||||
if (patch.cronExpr && patch.cronExpr !== row.cron_expr) {
|
||||
@@ -109,7 +109,7 @@ function updateTask(db, sessionId, patch) {
|
||||
|
||||
function deleteTask(db, sessionId) {
|
||||
const row = db.prepare('SELECT cron_expr FROM scheduled_tasks WHERE session_id = ?').get(sessionId);
|
||||
if (!row) throw new Error('本会话没有定时任务,无需 op=delete');
|
||||
if (!row) throw new Error('该会话没有定时任务,无需 op=delete');
|
||||
db.prepare('DELETE FROM scheduled_tasks WHERE session_id = ?').run(sessionId);
|
||||
log.info('scheduler', sessionId + ' 删除定时任务 ' + row.cron_expr);
|
||||
return row.cron_expr;
|
||||
|
||||
@@ -292,7 +292,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=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.",
|
||||
"description": "Manage your scheduled tasks (alarms). Each task lives on exactly one session (at most one per session) and is addressed by its home session id — the same id op=list shows (e.g. 'task-20260929-003'); omit id (or '0') to act on THIS session's task. At each trigger the prompt is delivered to the task's 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 Optional id (default: this session).\nop=list No arguments. Every task of the current user across all sessions, each row starting with its id — hand that id to get/update/delete to act on any task directly; never ask the user to switch sessions.\nop=create Always targets THIS session (id is rejected). Requires cron_expr, prompt and brief. After creating, this session retires into the task's dedicated home (archived; the user's next message starts a fresh session).\nop=update Optional id. Requires at least one of cron_expr, prompt; only given fields change.\nop=delete Optional id. Cancels that task.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
@@ -303,12 +303,16 @@
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"get",
|
||||
"list",
|
||||
"create",
|
||||
"update",
|
||||
"delete",
|
||||
"list_all"
|
||||
"delete"
|
||||
],
|
||||
"description": "Operation to perform on the current session's scheduled task."
|
||||
"description": "Operation to perform. Tasks are addressed by their home session id."
|
||||
},
|
||||
"id": {
|
||||
"type": "string",
|
||||
"description": "Task handle: the session id the task lives on, exactly as listed by op=list. Omit (or '0') to act on THIS session's task. Allowed with get/update/delete; op=create rejects it (new tasks always live in this session)."
|
||||
},
|
||||
"cron_expr": {
|
||||
"type": "string",
|
||||
|
||||
@@ -893,26 +893,41 @@ async function toolMintPublicLink(ctx, args) {
|
||||
return pl.base_url + '/p/' + id;
|
||||
}
|
||||
|
||||
// schedule:当前会话定时任务的完整生命周期(op 分派,规范见 docs/tool-design.md)。
|
||||
// sessionId 取自 ctx(AI 无法指定别的会话);表主键 session_id → 每会话至多一条,
|
||||
// 资源即「本会话的任务」,无 id 参数。条件必填在代码里校验(schema 只 required op),
|
||||
// 错误回显 op 与缺失字段;delete 不落 scheduler_note(返回文本本身已进会话记录)。
|
||||
// schedule:用户全部定时任务的生命周期(op 分派,规范见 docs/tool-design.md)。
|
||||
// 任务住会话(表主键 session_id → 每会话至多一条),「哪个任务」就用所属会话 id 指认:
|
||||
// id 省略/'0' = 本会话(兼容旧调用);跨会话操作带 op=list 里看到的 id,同用户校验——
|
||||
// 用户不切会话,AI 代办一切。create 只落当前会话(定时即下班,建完本会话收口退位成专柜)。
|
||||
// 条件必填在代码里校验(schema 只 required op),错误回显 op 与缺失字段;delete 不落 scheduler_note。
|
||||
function toolScheduleTask(ctx, args) {
|
||||
const op = String(args.op || '').trim().toLowerCase();
|
||||
const cronExpr = String(args.cron_expr || '').trim();
|
||||
const prompt = String(args.prompt || '').trim();
|
||||
const db = ctx.db;
|
||||
const curUser = () => String((db.prepare('SELECT user FROM sessions WHERE id = ?').get(ctx.sessionId) || {}).user || '');
|
||||
// 目标会话解析:无 id → 本会话;有 id → 存在 + 同用户,否则报错(不静默降级,防 AI 拿错目标静默误删)。
|
||||
const target = () => {
|
||||
const raw = String(args.id == null ? '' : args.id).trim();
|
||||
if (!raw || raw === '0' || raw === ctx.sessionId) return ctx.sessionId;
|
||||
const s = db.prepare('SELECT user FROM sessions WHERE id = ?').get(raw);
|
||||
if (!s) throw new Error('id 指向的会话不存在:' + raw + '(op=list 可看各任务所属会话 id)');
|
||||
if (String(s.user || '') !== curUser()) throw new Error('无权操作其他用户的会话任务');
|
||||
return raw;
|
||||
};
|
||||
const where = sid => sid === ctx.sessionId ? '' : '(会话 ' + sid + ')';
|
||||
switch (op) {
|
||||
case 'get': {
|
||||
const t = scheduler.getTask(db, ctx.sessionId);
|
||||
if (!t) return '本会话当前没有定时任务。要创建用 op=create。';
|
||||
const sid = target();
|
||||
const t = scheduler.getTask(db, sid);
|
||||
if (!t) return '该会话' + (sid === ctx.sessionId ? '' : '(' + sid + ')') + '没有定时任务。要创建用 op=create(只落当前会话)。';
|
||||
const last = t.last_run_at
|
||||
? scheduler.fmtTime(t.last_run_at) + '(状态 ' + (t.last_status || '-') + (t.last_note ? ',' + t.last_note : '') + ')'
|
||||
: '尚未执行';
|
||||
return '本会话定时任务' + (t.brief ? '「' + t.brief + '」' : '') + ':cron=' + t.cron_expr + '(Asia/Shanghai),下次执行 ' +
|
||||
return '定时任务' + where(sid) + (t.brief ? '「' + t.brief + '」' : '') + ':cron=' + t.cron_expr + '(Asia/Shanghai),下次执行 ' +
|
||||
scheduler.fmtTime(t.next_run_at) + ',上次执行 ' + last + '。提示词:' + t.prompt;
|
||||
}
|
||||
case 'create': {
|
||||
const raw = String(args.id == null ? '' : args.id).trim();
|
||||
if (raw && raw !== '0' && raw !== ctx.sessionId) throw new Error('op=create 只作用于当前会话:新任务住进本会话并于本回合收尾后收口退位成专柜(定时即下班),无需也不能指定其它会话 id');
|
||||
if (!cronExpr) throw new Error('op=create 缺少必填参数 cron_expr');
|
||||
if (!prompt) throw new Error('op=create 缺少必填参数 prompt');
|
||||
const brief = String(args.brief || '').replace(/\s+/g, ' ').trim().slice(0, 80);
|
||||
@@ -925,35 +940,36 @@ function toolScheduleTask(ctx, args) {
|
||||
}
|
||||
case 'update': {
|
||||
if (!cronExpr && !prompt) throw new Error('op=update 需要 cron_expr 和 prompt 至少提供一个');
|
||||
const t = scheduler.updateTask(db, ctx.sessionId, {
|
||||
const sid = target();
|
||||
const t = scheduler.updateTask(db, sid, {
|
||||
cronExpr: cronExpr || null,
|
||||
prompt: prompt || null,
|
||||
});
|
||||
return '定时任务已更新:' + t.cron_expr + ',下次执行 ' + t.next_text + '(Asia/Shanghai)。';
|
||||
return '定时任务已更新' + where(sid) + ':' + 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' : '当前没有任务,可直接建') + ')。';
|
||||
case 'list': {
|
||||
// 跨会话全览:每行尾挂所属会话 id,AI 拿 id 直接代办改删——不再出现任何
|
||||
//「让用户切会话」式引导(用户不会切)。
|
||||
const rows = scheduler.listUserTasks(db, curUser());
|
||||
if (!rows.length) return '你目前没有任何定时任务。要创建用 op=create(落在本会话,建成后本会话退位成该任务专柜)。';
|
||||
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 '- id=' + t.session_id + (t.session_id === ctx.sessionId ? '(本会话)' : '')
|
||||
+ ' 「' + (t.brief || String(t.prompt).slice(0, 40)) + '」 cron=' + t.cron_expr
|
||||
+ ',下次 ' + scheduler.fmtTime(t.next_run_at) + ',上次 ' + last;
|
||||
});
|
||||
return '全部定时任务(' + rows.length + ' 个,按下次执行排序):\n' + lines.join('\n');
|
||||
return '全部定时任务(' + rows.length + ' 个,按下次执行排序):\n' + lines.join('\n')
|
||||
+ '\n改/删直接 op=update / op=delete 并带对应 id;新建闹钟用 op=create(只落本会话,定时即下班)。';
|
||||
}
|
||||
case 'delete': {
|
||||
const old = scheduler.deleteTask(db, ctx.sessionId);
|
||||
return '定时任务已删除(原定 ' + old + ')。要建新任务用 op=create。';
|
||||
const sid = target();
|
||||
const old = scheduler.deleteTask(db, sid);
|
||||
return '定时任务已删除' + where(sid) + '(原定 ' + old + ')。要建新任务用 op=create(本会话)。';
|
||||
}
|
||||
default:
|
||||
throw new Error('未知的 op:"' + op + '"。可用:get / create(需 cron_expr、prompt、brief)/ update(需 cron_expr、prompt 至少一个)/ delete / list_all(跨会话全览)');
|
||||
throw new Error('未知的 op:"' + op + '"。可用:get / list(跨会话全览,每行带 id)/ create(仅当前会话;需 cron_expr、prompt、brief)/ update(需 cron_expr、prompt 至少一个,可选 id)/ delete(可选 id)');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user