work-rhythm 0.1.7 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/notes/AGENTS.md +8 -1
- package/.agents/notes/decisions/2026-12-01-interjections-batch-into-one-answer-and-interrupt-reruns.md +33 -0
- package/.agents/notes/decisions/2026-12-01-long-runs-are-bounded-by-silence-not-wall-clock.md +35 -0
- package/.agents/notes/decisions/2026-12-01-router-and-worker-pass-content-in-handoff-docs.md +41 -0
- package/.agents/notes/pitfalls/2026-12-01-launchd-install-bakes-invoking-node.md +28 -0
- package/.agents/notes/pitfalls/2026-12-01-replace-string-dollar-quote-expands.md +16 -0
- package/.agents/skills/work-rhythm/SKILL.md +14 -12
- package/README.md +7 -4
- package/collectors/local.env.example +10 -1
- package/docs/agents.md +28 -11
- package/docs/channels.md +9 -6
- package/docs/router-worker.md +241 -0
- package/docs/scheduled-tasks.md +4 -4
- package/package.json +1 -1
- package/scripts/cli.js +4 -3
- package/scripts/cmd/agent.js +78 -40
- package/scripts/cmd/check.js +4 -0
- package/scripts/cmd/listen.js +20 -4
- package/scripts/cmd/rhythm.js +9 -2
- package/scripts/cmd/trigger.js +143 -0
- package/scripts/lib/agent.js +377 -30
- package/scripts/lib/bot.js +140 -35
- package/scripts/lib/engine.js +523 -0
- package/scripts/lib/handoff.js +165 -0
- package/scripts/lib/launchd.js +49 -3
- package/scripts/lib/markdown.js +328 -38
- package/scripts/lib/rhythm.js +377 -33
- package/scripts/lib/router.js +161 -0
- package/scripts/lib/run.js +43 -0
- package/scripts/lib/view.js +583 -0
- package/scripts/lib/worker.js +97 -0
- package/scripts/test/agent.test.js +17 -9
- package/scripts/test/check.test.js +7 -2
- package/scripts/test/harness.js +13 -1
- package/scripts/test/listen.test.js +3 -3
- package/scripts/test/rhythm.test.js +42 -8
- package/scripts/test/router-helper.js +80 -0
- package/scripts/test/router.test.js +509 -0
- package/scripts/test/stubs.js +82 -12
- package/scripts/test/timeout.test.js +99 -0
- package/templates/00-config/profile.example.yaml +7 -0
- package/templates/AGENTS.md +3 -1
- package/templates/README.md +4 -1
package/scripts/cmd/listen.js
CHANGED
|
@@ -20,7 +20,8 @@ const USAGE = `work-rhythm listen <子命令> —— 值守:消息渠道的收
|
|
|
20
20
|
用法: work-rhythm listen <子命令>
|
|
21
21
|
probe [--seconds N] 连上去看握手和收到的每一帧,不调 agent,到时退出(默认 60 秒)。第一次验证用
|
|
22
22
|
run 值守:一直连着,收到消息就处理
|
|
23
|
-
handle "<消息>"
|
|
23
|
+
handle "<消息>" [--push] 不连云之家,走一遍「交给 agent → 打印回复」,调试 agent 配置。路由开着时走全流程
|
|
24
|
+
(路由 → 执行者 → 路由),把要发给用户的话打到屏幕上;带 --push 才真发
|
|
24
25
|
handle-frame '<JSON>' 把一帧当作刚收到的处理一遍,回复照发(自测用)
|
|
25
26
|
reset 丢掉机器人的专属会话,下一句从新会话开始(在聊天里发 /new 也一样)
|
|
26
27
|
doctor 检查值守的配置和依赖,有问题逐条报出来
|
|
@@ -66,10 +67,21 @@ module.exports = {
|
|
|
66
67
|
if (rest.length) throw new UsageError(USAGE);
|
|
67
68
|
await bot.run();
|
|
68
69
|
return EXIT.OK; // 循环退出后可能还挂着重连的定时器,命令到这里就结束
|
|
69
|
-
case 'handle':
|
|
70
|
+
case 'handle': {
|
|
70
71
|
if (!rest.length) throw new UsageError(USAGE);
|
|
71
|
-
|
|
72
|
+
const really = rest.includes('--push');
|
|
73
|
+
const text = rest.filter((a) => a !== '--push').join(' ');
|
|
74
|
+
// 路由开着:在命令行的进程里走一遍全流程(路由 → 执行者 → 路由),要发的话打到屏幕上
|
|
75
|
+
const r = await service.handle(text, { push: really });
|
|
76
|
+
if (r) {
|
|
77
|
+
stdout.write(`触发 ${r.id}:路由 → 执行者 → 路由走完了。${really ? '要发的话已经发了。' : '要发给用户的话(没发;带 --push 才真发):'}\n`);
|
|
78
|
+
for (const t of r.texts) stdout.write(`\n${t}\n`);
|
|
79
|
+
if (!r.texts.length) stdout.write(`${really ? '' : '\n(没有:路由没有要发给用户的话。work-rhythm trigger show 看任务书和回执)'}\n`);
|
|
80
|
+
return EXIT.OK;
|
|
81
|
+
}
|
|
82
|
+
stdout.write(`${bot.handle(text)}\n`);
|
|
72
83
|
return EXIT.OK;
|
|
84
|
+
}
|
|
73
85
|
case 'handle-frame': {
|
|
74
86
|
if (!rest.length) throw new UsageError(USAGE);
|
|
75
87
|
const r = await bot.processFrame(rest.join(' '), true);
|
|
@@ -83,8 +95,10 @@ module.exports = {
|
|
|
83
95
|
return EXIT.OK;
|
|
84
96
|
case 'doctor': {
|
|
85
97
|
if (rest.length) throw new UsageError(USAGE);
|
|
86
|
-
const problems = bot.doctorProblems();
|
|
98
|
+
const problems = [...bot.doctorProblems(), ...(service.cfg.router ? service.doctorProblems({ strict: false, bot: false }).filter((p) => !p.startsWith('任务 ')) : [])];
|
|
99
|
+
const notes = service.doctorNotes({ mentionOff: false });
|
|
87
100
|
if (problems.length) stdout.write(`${problems.join('\n')}\n`);
|
|
101
|
+
for (const n of notes) stdout.write(`注意:${n}\n`);
|
|
88
102
|
return problems.length ? 1 : EXIT.OK;
|
|
89
103
|
}
|
|
90
104
|
case 'install': {
|
|
@@ -100,6 +114,8 @@ module.exports = {
|
|
|
100
114
|
const problems = service.doctorProblems();
|
|
101
115
|
if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `值守的开关已打开,但本地服务装不成常驻,先解决:\n${problems.join('\n')}`);
|
|
102
116
|
service.launchd.install();
|
|
117
|
+
const problem = launchd.startupProblem('service', root, path.join(root, '01-inbox', '_rhythm.launchd.log'));
|
|
118
|
+
if (problem) throw new Fail(EXIT.FAILED, problem);
|
|
103
119
|
how = '本地服务已装成常驻(定时任务、推送、网页和值守都在这一个进程里)。';
|
|
104
120
|
}
|
|
105
121
|
stdout.write(`值守已打开(${path.relative(root, file)} 的 YZJ_BOT_LISTEN=1),跑在本地服务里。${how}\n日志:${path.join(root, '01-inbox', '_bot.log')}\n现在可以在云之家给机器人发一句试试。\n`);
|
package/scripts/cmd/rhythm.js
CHANGED
|
@@ -18,13 +18,15 @@ const USAGE = `work-rhythm rhythm <子命令> —— 本地服务:定时采集
|
|
|
18
18
|
start [--tick 秒] 前台跑:到点跑采集和任务,起网页服务。要常驻用 install;--tick 是多久看一次表(默认 20,调试用)
|
|
19
19
|
stop | status | restart 停掉;看在不在跑、下次什么时候跑什么、值守开着没;重启常驻(改了配置用)
|
|
20
20
|
due [--at 时间] [--days 天数] 从某个时间起,列出什么时候跑什么
|
|
21
|
-
run <任务id|sync> [--push] 立刻跑一次,不等时间;带 --push
|
|
21
|
+
run <任务id|sync> [--push] 立刻跑一次,不等时间;带 --push 才推送(不经路由,调试和兜底)
|
|
22
22
|
serve [--port 端口] 只起网页服务
|
|
23
23
|
render <文件.md> 把一份 Markdown 渲染成页面,打到标准输出(自测、调试用)
|
|
24
|
-
doctor
|
|
24
|
+
doctor 检查配置和依赖,有问题逐条报出来;不算问题的注意事项另起一行(路由没开、路由没有只读档)
|
|
25
25
|
install | uninstall macOS launchd 常驻:开机自启、挂了拉起
|
|
26
26
|
定时任务只建在这个服务上,要先绑 agent(work-rhythm agent bind)。任务到点后:把 tasks/<id>.md 的指令交给 agent 跑一遍
|
|
27
27
|
(它会同步数据、整理事件流、写每日文件),agent 最后写的总结经消息渠道推给我,并附本机页面地址。
|
|
28
|
+
profile 的 rhythm.router 为 true 时多一步:到点的任务和 IM 里的话先变成触发交给路由判断,再派给执行者(docs/router-worker.md);
|
|
29
|
+
没写就是上面的直通做法。网页多一页监控中心 /monitor(服务、路由、执行者、定时任务、触发、配置、日志),/status.json 是同一份数据。
|
|
28
30
|
退出码: 0 完成;2 用法不对;3 没配置好(没绑 agent、配置有问题、已经在跑);4 缺依赖;5 运行失败;doctor 查出问题时是 1。
|
|
29
31
|
定义和用法见 docs/scheduled-tasks.md,agent 见 docs/agents.md。`;
|
|
30
32
|
|
|
@@ -92,7 +94,9 @@ module.exports = {
|
|
|
92
94
|
case 'doctor': {
|
|
93
95
|
if (rest.length) throw new UsageError(USAGE);
|
|
94
96
|
const problems = service.doctorProblems();
|
|
97
|
+
const notes = service.doctorNotes();
|
|
95
98
|
if (problems.length) stdout.write(`${problems.join('\n')}\n`);
|
|
99
|
+
for (const n of notes) stdout.write(`注意:${n}\n`);
|
|
96
100
|
return problems.length ? 1 : EXIT.OK;
|
|
97
101
|
}
|
|
98
102
|
case 'install': {
|
|
@@ -102,7 +106,10 @@ module.exports = {
|
|
|
102
106
|
const plist = service.launchd.install();
|
|
103
107
|
// 值守现在跑在服务里;以前单独装的常驻收掉,别两个都开着
|
|
104
108
|
launchd.uninstall('bot', root);
|
|
109
|
+
// 装完看一眼真的起来了:launchd 拉起失败时 bootstrap 也是成功的
|
|
110
|
+
const problem = launchd.startupProblem('service', root, `${root}/01-inbox/_rhythm.launchd.log`);
|
|
105
111
|
stdout.write(`已装成常驻:${plist}\n页面:${service.baseUrl()} 日志:${root}/01-inbox/_rhythm.log\n值守跟着服务一起跑(collectors/local.env 的 YZJ_BOT_LISTEN=1 才开;work-rhythm listen install 打开)。\n`);
|
|
112
|
+
if (problem) throw new Fail(EXIT.FAILED, problem);
|
|
106
113
|
return EXIT.OK;
|
|
107
114
|
}
|
|
108
115
|
case 'uninstall':
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// work-rhythm trigger:看处理过的触发,手动塞一条进去。路由开着时才有这条线(profile 的 rhythm.router),
|
|
2
|
+
// 实现(排队、决定、执行者、汇报)在 lib/engine.js,交接文档在 lib/handoff.js。说明见 docs/router-worker.md。
|
|
3
|
+
'use strict';
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
7
|
+
const { settings } = require('../lib/config');
|
|
8
|
+
const { createRhythm } = require('../lib/rhythm');
|
|
9
|
+
const { fire, queueDir } = require('../lib/engine');
|
|
10
|
+
const handoff = require('../lib/handoff');
|
|
11
|
+
|
|
12
|
+
const USAGE = `work-rhythm trigger <子命令> —— 触发:到点的任务、IM 里的话、执行者的汇报都从这条路走(profile 的 rhythm.router 开着时)
|
|
13
|
+
|
|
14
|
+
用法: work-rhythm trigger <子命令>
|
|
15
|
+
list [--days 1] [--json] 最近处理过的触发,新的在前:时间、短号、来源、决定、执行者的状态和用时、发没发、任务书和回执在不在
|
|
16
|
+
show <短号> 打印这条的任务书和回执(工作目录的 .agents/handoff/ 下)
|
|
17
|
+
fire "<一句话>" [--mode plan|edit] [--wait [--timeout 秒]]
|
|
18
|
+
手动塞一条触发进队列:本地服务在跑就交给路由,由它判断再派给执行者。--mode 是执行者的权限
|
|
19
|
+
(默认 plan)。--wait 等它做完,把路由的决定和执行者的回执打到屏幕上
|
|
20
|
+
路由先判一下再派人:这句话是闲聊它可能不回,是一句话能答的它直接答,要动手的才派执行者。
|
|
21
|
+
退出码: 0;2 用法不对;3 路由没开、服务没在跑、没有这条触发,或 --wait 没等到;5 运行失败。
|
|
22
|
+
详见 docs/router-worker.md。`;
|
|
23
|
+
|
|
24
|
+
const STATUS_CN = { queued: '排队中', running: '在跑', ok: '成功', failed: '失败', stopped: '叫停' };
|
|
25
|
+
const FIELDS = ['at', 'id', 'kind', 'task', 'of', 'decision', 'worker', 'seconds', 'agent', 'mode', 'notified', 'docs'];
|
|
26
|
+
|
|
27
|
+
const positiveInt = (value, fallback) => {
|
|
28
|
+
const n = Number(value);
|
|
29
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const nonEmpty = (file) => {
|
|
33
|
+
try {
|
|
34
|
+
return fs.statSync(file).size > 0;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// 一条触发的任务书、回执在不在
|
|
41
|
+
const docsOf = (root, t) => ({ task: nonEmpty(handoff.taskPath(root, t.id)), result: nonEmpty(handoff.resultPath(root, t.id)) });
|
|
42
|
+
|
|
43
|
+
module.exports = {
|
|
44
|
+
usage: USAGE,
|
|
45
|
+
async run(args, ctx) {
|
|
46
|
+
const { root, stdout, env, packageRoot } = ctx;
|
|
47
|
+
const [cmd, ...rest] = args;
|
|
48
|
+
const settingsEnv = settings(root, env);
|
|
49
|
+
const service = createRhythm({ root, env: settingsEnv, packageRoot });
|
|
50
|
+
switch (cmd) {
|
|
51
|
+
case 'list': {
|
|
52
|
+
let days = 1;
|
|
53
|
+
let json = false;
|
|
54
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
55
|
+
if (rest[i] === '--json') json = true;
|
|
56
|
+
else if (rest[i] === '--days') {
|
|
57
|
+
days = positiveInt(rest[i + 1], 0);
|
|
58
|
+
if (!days) throw new UsageError('--days 要是正整数');
|
|
59
|
+
i += 1;
|
|
60
|
+
} else throw new UsageError(USAGE);
|
|
61
|
+
}
|
|
62
|
+
const d = new Date(Date.now() - days * 24 * 3600 * 1000);
|
|
63
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
64
|
+
const since = `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
65
|
+
const list = Object.values(service.store.data.triggers || {}).filter((t) => t.at >= since).sort((a, b) => b.at.localeCompare(a.at) || b.id.localeCompare(a.id));
|
|
66
|
+
if (json) {
|
|
67
|
+
stdout.write(`${JSON.stringify(list.map((t) => ({ ...t, docs: docsOf(root, t) })), null, 1)}\n`);
|
|
68
|
+
return EXIT.OK;
|
|
69
|
+
}
|
|
70
|
+
stdout.write(`${FIELDS.join('\t')}\n`);
|
|
71
|
+
for (const t of list) {
|
|
72
|
+
const w = t.worker || {};
|
|
73
|
+
const docs = docsOf(root, t);
|
|
74
|
+
const cells = {
|
|
75
|
+
at: t.at,
|
|
76
|
+
id: t.id,
|
|
77
|
+
kind: t.kind,
|
|
78
|
+
task: t.task || '',
|
|
79
|
+
of: t.of || '',
|
|
80
|
+
decision: t.decision || '',
|
|
81
|
+
worker: w.status ? `${STATUS_CN[w.status] || w.status}${w.status === 'failed' && w.reason ? `(${w.reason})` : ''}` : '',
|
|
82
|
+
seconds: w.seconds === undefined ? '' : String(w.seconds),
|
|
83
|
+
agent: w.agent || '',
|
|
84
|
+
mode: w.mode || '',
|
|
85
|
+
notified: t.notified ? 'yes' : '',
|
|
86
|
+
docs: [docs.task ? '任务书' : '', docs.result ? '回执' : ''].filter(Boolean).join('+'),
|
|
87
|
+
};
|
|
88
|
+
stdout.write(`${FIELDS.map((f) => String(cells[f]).replace(/\t/g, ' ')).join('\t')}\n`);
|
|
89
|
+
}
|
|
90
|
+
if (!list.length) stdout.write(`(最近 ${days} 天没有触发${service.cfg.router ? '' : ';路由没开:profile 的 rhythm 写上 router: true'})\n`);
|
|
91
|
+
return EXIT.OK;
|
|
92
|
+
}
|
|
93
|
+
case 'show': {
|
|
94
|
+
if (rest.length !== 1 || !/^t-\d+$/.test(rest[0])) throw new UsageError(USAGE);
|
|
95
|
+
const id = rest[0];
|
|
96
|
+
const task = handoff.readText(handoff.taskPath(root, id));
|
|
97
|
+
const result = handoff.readText(handoff.resultPath(root, id));
|
|
98
|
+
if (!task && !result) throw new Fail(EXIT.NOT_CONFIGURED, `没有 ${id} 这条的交接文档(.agents/handoff/):可能过期清掉了,work-rhythm trigger list 看还有哪些`);
|
|
99
|
+
if (task) stdout.write(`${task.trimEnd()}\n`);
|
|
100
|
+
if (result) stdout.write(`\n${'─'.repeat(40)}\n\n${result.trimEnd()}\n`);
|
|
101
|
+
return EXIT.OK;
|
|
102
|
+
}
|
|
103
|
+
case 'fire': {
|
|
104
|
+
let text = '';
|
|
105
|
+
let mode = 'plan';
|
|
106
|
+
let wait = false;
|
|
107
|
+
let timeout = 900;
|
|
108
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
109
|
+
if (rest[i] === '--mode' && rest[i + 1]) mode = rest[++i];
|
|
110
|
+
else if (rest[i] === '--wait') wait = true;
|
|
111
|
+
else if (rest[i] === '--timeout' && rest[i + 1]) timeout = positiveInt(rest[++i], 900);
|
|
112
|
+
else if (rest[i].startsWith('--')) throw new UsageError(USAGE);
|
|
113
|
+
else if (!text) text = rest[i];
|
|
114
|
+
else throw new UsageError(USAGE);
|
|
115
|
+
}
|
|
116
|
+
if (!text) throw new UsageError(USAGE);
|
|
117
|
+
if (!['plan', 'edit'].includes(mode)) throw new UsageError('--mode 只能是 plan 或 edit');
|
|
118
|
+
if (!service.cfg.router) throw new Fail(EXIT.NOT_CONFIGURED, '路由没开(profile 的 rhythm 没写 router):触发塞进去没人取。要走「触发 → 路由 → 执行者」在 00-config/profile.yaml 的 rhythm 一节写上 router: true,再 work-rhythm rhythm restart');
|
|
119
|
+
if (!service.serviceAlive()) throw new Fail(EXIT.NOT_CONFIGURED, '本地服务没在跑:先 work-rhythm rhythm start(或 install 装成常驻),再 fire');
|
|
120
|
+
const key = fire(root, { text, mode });
|
|
121
|
+
stdout.write(`已塞进队列(${path.relative(root, queueDir(root))}),服务下一次看表(最多 20 秒)取走交给路由。\n`);
|
|
122
|
+
if (!wait) return EXIT.OK;
|
|
123
|
+
// 等它做完:先等服务给它发短号,再等执行者结束(或者路由决定不动手)
|
|
124
|
+
const until = Date.now() + timeout * 1000;
|
|
125
|
+
const find = () => Object.values(createRhythm({ root, env: settingsEnv, packageRoot }).store.data.triggers || {}).find((x) => x.key === `fired-${key}`) || null;
|
|
126
|
+
let t = null;
|
|
127
|
+
while (Date.now() < until) {
|
|
128
|
+
t = find();
|
|
129
|
+
if (t && t.decision && (!t.worker || !['queued', 'running'].includes(t.worker.status))) break;
|
|
130
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
131
|
+
}
|
|
132
|
+
if (!t) throw new Fail(EXIT.NOT_CONFIGURED, `等了 ${timeout} 秒服务还没取走它:work-rhythm rhythm status 看服务在不在,01-inbox/_rhythm.log 看出了什么`);
|
|
133
|
+
stdout.write(`${t.id} 路由的决定:${t.decision}${t.reason ? `(${t.reason})` : ''}\n`);
|
|
134
|
+
const result = handoff.readText(handoff.resultPath(root, t.id));
|
|
135
|
+
if (result) stdout.write(`\n${result.trimEnd()}\n`);
|
|
136
|
+
else stdout.write('没有执行者回执:路由没有派人(直接回了一句、或者觉得不用处理)。要发给我的话只在 IM 里。\n');
|
|
137
|
+
return EXIT.OK;
|
|
138
|
+
}
|
|
139
|
+
default:
|
|
140
|
+
throw new UsageError(USAGE);
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
};
|