work-rhythm 0.1.1 → 0.1.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 +1 -0
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +1 -0
- package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +1 -0
- package/.agents/notes/decisions/2026-12-01-scheduling-is-cli-only-and-needs-a-bound-agent.md +31 -0
- package/.agents/skills/work-rhythm/SKILL.md +23 -10
- package/README.md +2 -1
- package/collectors/local.env.example +4 -2
- package/docs/agents.md +49 -0
- package/docs/channels.md +1 -1
- package/docs/formats.md +4 -0
- package/docs/scheduled-tasks.md +2 -12
- package/package.json +1 -1
- package/scripts/cli.js +15 -4
- package/scripts/cmd/agent.js +114 -0
- package/scripts/cmd/check.js +25 -3
- package/scripts/cmd/collect.js +10 -1
- package/scripts/cmd/decisions.js +17 -6
- package/scripts/cmd/events.js +103 -0
- package/scripts/cmd/goals.js +16 -20
- package/scripts/cmd/init.js +7 -1
- package/scripts/cmd/listen.js +7 -2
- package/scripts/cmd/notify.js +9 -1
- package/scripts/cmd/pack.js +7 -1
- package/scripts/cmd/rhythm.js +7 -2
- package/scripts/cmd/selftest.js +7 -1
- package/scripts/cmd/sync.js +12 -1
- package/scripts/cmd/today.js +13 -1
- package/scripts/cmd/workdir.js +5 -2
- package/scripts/lib/agent.js +156 -17
- package/scripts/lib/bot.js +1 -1
- package/scripts/test/agent.test.js +201 -0
- package/scripts/test/check.test.js +31 -9
- package/scripts/test/config.test.js +0 -2
- package/scripts/test/events.test.js +89 -0
- package/scripts/test/harness.js +4 -1
- package/scripts/test/help.test.js +71 -0
- package/scripts/test/listen.test.js +10 -6
- package/scripts/test/rhythm.test.js +2 -2
- package/scripts/test/stubs.js +19 -1
- package/templates/00-config/profile.example.yaml +0 -1
- package/templates/AGENTS.md +1 -1
- package/templates/README.md +2 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 列出每日文件里的事件流:agent 要「基于事件流干事儿」「问问最近在忙什么」时,先从这里拿数据,不用自己翻一堆 Markdown
|
|
2
|
+
// 读的是 02-daily/YYYY-MM/YYYY-MM-DD.md 里「## 事件流」那张表(docs/formats.md),只读不改
|
|
3
|
+
'use strict';
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { EXIT, UsageError, parseDate, addDays, today, fmtDate } = require('../lib/run');
|
|
7
|
+
|
|
8
|
+
const USAGE = `work-rhythm events [选项] —— 列出每日文件「## 事件流」里的事件(什么时候、做了什么、属于哪个项目),只读
|
|
9
|
+
|
|
10
|
+
用法:
|
|
11
|
+
work-rhythm events 最近 7 天(含今天)
|
|
12
|
+
work-rhythm events --today 只看今天
|
|
13
|
+
work-rhythm events --days 14 最近 14 天
|
|
14
|
+
work-rhythm events --from 2026-11-20 --to 2026-11-30
|
|
15
|
+
work-rhythm events --type 会议,沟通 只看这些类型(词表见 docs/formats.md 的「事件类型」)
|
|
16
|
+
work-rhythm events --source calendar-yzj 只看这些来源(订阅源的 id,或「口述」)
|
|
17
|
+
work-rhythm events --project 导出 只看这些项目标签(和长期目标相关的事件,项目列是目标 id)
|
|
18
|
+
work-rhythm events --json 每条一个对象,给程序读
|
|
19
|
+
输出是 TSV(制表符分隔),第一行表头 date、time、type、item、source、project,按日期、时间排好序。
|
|
20
|
+
事件是「一行一事」的记录,不含细节;要细节看 01-inbox/ 的快照或问用户。
|
|
21
|
+
退出码: 0(没有事件也是 0,只有表头);2 选项或日期不对;3 不在工作目录里。`;
|
|
22
|
+
|
|
23
|
+
const FIELDS = ['date', 'time', 'type', 'item', 'source', 'project'];
|
|
24
|
+
const COLUMNS = { 时间: 'time', 类型: 'type', 事项: 'item', 来源: 'source', 项目: 'project' };
|
|
25
|
+
|
|
26
|
+
// 「## 事件流」那一节里的表:表头给出列名,分隔线跳过。格子里转义的竖线先换成占位符再切。后面几格缺的留空
|
|
27
|
+
function parseEvents(markdown, date) {
|
|
28
|
+
const lines = String(markdown).split('\n');
|
|
29
|
+
const start = lines.findIndex((l) => /^##\s*事件流\s*$/.test(l));
|
|
30
|
+
if (start < 0) return [];
|
|
31
|
+
const events = [];
|
|
32
|
+
let columns = null;
|
|
33
|
+
for (let i = start + 1; i < lines.length && !/^##\s/.test(lines[i]); i += 1) {
|
|
34
|
+
const line = lines[i].trim();
|
|
35
|
+
if (!line.startsWith('|')) continue;
|
|
36
|
+
const cells = line.replace(/\\\|/g, '\u0000').split('|').slice(1, -1).map((c) => c.trim().replace(/\u0000/g, '|'));
|
|
37
|
+
if (!cells.length || cells.every((c) => /^:?-+:?$/.test(c))) continue;
|
|
38
|
+
if (!columns) {
|
|
39
|
+
columns = cells.map((name) => COLUMNS[name] || '');
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
// 至少要有时间和类型两格;只有一格的(写坏的行、夹在表里的说明)不算事件
|
|
43
|
+
if (cells.length < 2) continue;
|
|
44
|
+
const event = { date, time: '', type: '', item: '', source: '', project: '' };
|
|
45
|
+
columns.forEach((field, n) => {
|
|
46
|
+
if (field) event[field] = cells[n] || '';
|
|
47
|
+
});
|
|
48
|
+
if (event.time) events.push(event);
|
|
49
|
+
}
|
|
50
|
+
return events;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function readDay(root, date) {
|
|
54
|
+
try {
|
|
55
|
+
return parseEvents(fs.readFileSync(path.join(root, '02-daily', date.slice(0, 7), `${date}.md`), 'utf8'), date);
|
|
56
|
+
} catch {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const parseList = (value) => String(value || '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
62
|
+
|
|
63
|
+
async function run(args, ctx) {
|
|
64
|
+
const { root, stdout } = ctx;
|
|
65
|
+
const opts = { from: '', to: today(), days: 7, types: [], sources: [], projects: [], json: false };
|
|
66
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
67
|
+
const a = args[i];
|
|
68
|
+
const value = (what) => {
|
|
69
|
+
const v = args[i + 1];
|
|
70
|
+
if (v === undefined || v.startsWith('--')) throw new UsageError(`${a} 后面要跟一个值(${what})`);
|
|
71
|
+
i += 1;
|
|
72
|
+
return v;
|
|
73
|
+
};
|
|
74
|
+
if (a === '--today') opts.days = 1;
|
|
75
|
+
else if (a === '--days') {
|
|
76
|
+
const n = Number(value('天数'));
|
|
77
|
+
if (!Number.isInteger(n) || n < 1) throw new UsageError('--days 要是正整数');
|
|
78
|
+
opts.days = n;
|
|
79
|
+
} else if (a === '--from') opts.from = fmtDate(parseDate(value('YYYY-MM-DD'), '起始日期'));
|
|
80
|
+
else if (a === '--to') opts.to = fmtDate(parseDate(value('YYYY-MM-DD'), '结束日期'));
|
|
81
|
+
else if (a === '--type') opts.types = parseList(value('类型,逗号分隔'));
|
|
82
|
+
else if (a === '--source') opts.sources = parseList(value('来源 id,逗号分隔'));
|
|
83
|
+
else if (a === '--project') opts.projects = parseList(value('项目标签,逗号分隔'));
|
|
84
|
+
else if (a === '--json') opts.json = true;
|
|
85
|
+
else throw new UsageError(`不认识的选项 ${a}`);
|
|
86
|
+
}
|
|
87
|
+
const from = opts.from || addDays(opts.to, -(opts.days - 1));
|
|
88
|
+
if (from > opts.to) throw new UsageError(`起始日期 ${from} 晚于结束日期 ${opts.to}`);
|
|
89
|
+
let list = [];
|
|
90
|
+
for (let d = from; d <= opts.to; d = addDays(d, 1)) list.push(...readDay(root, d));
|
|
91
|
+
const keep = (event, wanted, field) => !wanted.length || wanted.includes(event[field]);
|
|
92
|
+
list = list.filter((e) => keep(e, opts.types, 'type') && keep(e, opts.sources, 'source') && keep(e, opts.projects, 'project'));
|
|
93
|
+
list.sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : a.time.localeCompare(b.time)));
|
|
94
|
+
if (opts.json) {
|
|
95
|
+
stdout.write(`${JSON.stringify(list)}\n`);
|
|
96
|
+
return EXIT.OK;
|
|
97
|
+
}
|
|
98
|
+
stdout.write(`${FIELDS.join('\t')}\n`);
|
|
99
|
+
for (const e of list) stdout.write(`${[e.date, e.time, e.type, e.item.replace(/\t/g, ' '), e.source, e.project].join('\t')}\n`);
|
|
100
|
+
return EXIT.OK;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
module.exports = { usage: USAGE, run, parseEvents };
|
package/scripts/cmd/goals.js
CHANGED
|
@@ -7,9 +7,8 @@
|
|
|
7
7
|
'use strict';
|
|
8
8
|
const fs = require('node:fs');
|
|
9
9
|
const path = require('node:path');
|
|
10
|
-
const { EXIT, noon, today, daysBetween, readText, writeFileAtomic } = require('../lib/run');
|
|
10
|
+
const { EXIT, UsageError, noon, today, daysBetween, readText, writeFileAtomic } = require('../lib/run');
|
|
11
11
|
|
|
12
|
-
const USAGE = '用法: work-rhythm goals list | due [日期] | touch <id> [日期] | progress <id> [日期]';
|
|
13
12
|
const HEADER_LIST = 'id\tstatus\ttitle\tsince_review\tsince_progress';
|
|
14
13
|
const HEADER_DUE = 'id\ttitle\tsince_review\treview_every';
|
|
15
14
|
|
|
@@ -88,6 +87,16 @@ const goalFiles = (root) => {
|
|
|
88
87
|
return names.map((n) => path.join(dir, n));
|
|
89
88
|
};
|
|
90
89
|
|
|
90
|
+
const USAGE = `work-rhythm goals list | due [日期] | touch <id> [日期] | progress <id> [日期] —— 长期目标的小工具(目标文件在 05-goals/<id>.md)
|
|
91
|
+
|
|
92
|
+
用法:
|
|
93
|
+
work-rhythm goals list 进行中和暂停的目标,带距上次检查、距上次进展的天数
|
|
94
|
+
work-rhythm goals due [YYYY-MM-DD] 今天(或指定那天)该检查的目标:距上次检查超过 review_every 天的
|
|
95
|
+
work-rhythm goals touch <id> [YYYY-MM-DD] 检查完之后,只改 last_review
|
|
96
|
+
work-rhythm goals progress <id> [YYYY-MM-DD] 有进展之后,只改 last_progress
|
|
97
|
+
输出是 TSV(制表符分隔),第一行表头。日期默认今天。到期与否由这里算,不要手算日期。
|
|
98
|
+
退出码: 0;2 子命令、id 或日期不对;3 不在工作目录里。`;
|
|
99
|
+
|
|
91
100
|
module.exports = {
|
|
92
101
|
usage: USAGE,
|
|
93
102
|
async run(args, ctx) {
|
|
@@ -105,10 +114,7 @@ module.exports = {
|
|
|
105
114
|
}
|
|
106
115
|
if (cmd === 'due') {
|
|
107
116
|
const date = rest[0] || today();
|
|
108
|
-
if (!noon(date)) {
|
|
109
|
-
stderr.write(`goals: 无法识别日期 '${date}'\n`);
|
|
110
|
-
return EXIT.USAGE;
|
|
111
|
-
}
|
|
117
|
+
if (!noon(date)) throw new UsageError(`无法识别日期 '${date}'`);
|
|
112
118
|
stdout.write(`${HEADER_DUE}\n`);
|
|
113
119
|
for (const f of goalFiles(root)) {
|
|
114
120
|
if (field(f, 'status') !== 'active') continue;
|
|
@@ -122,24 +128,14 @@ module.exports = {
|
|
|
122
128
|
}
|
|
123
129
|
if (cmd === 'touch' || cmd === 'progress') {
|
|
124
130
|
const [id, dateArg] = rest;
|
|
125
|
-
if (!id) {
|
|
126
|
-
stderr.write(`用法: work-rhythm goals ${cmd} <id> [日期]\n`);
|
|
127
|
-
return EXIT.USAGE;
|
|
128
|
-
}
|
|
131
|
+
if (!id) throw new UsageError(`goals ${cmd} 要写 <id>:work-rhythm goals ${cmd} <id> [日期]`);
|
|
129
132
|
const date = dateArg || today();
|
|
130
133
|
const file = path.join(root, '05-goals', `${id}.md`);
|
|
131
|
-
if (!fs.existsSync(file)) {
|
|
132
|
-
|
|
133
|
-
return EXIT.USAGE;
|
|
134
|
-
}
|
|
135
|
-
if (!noon(date)) {
|
|
136
|
-
stderr.write(`goals: 无法识别日期 '${date}'\n`);
|
|
137
|
-
return EXIT.USAGE;
|
|
138
|
-
}
|
|
134
|
+
if (!fs.existsSync(file)) throw new UsageError(`找不到 05-goals/${id}.md`);
|
|
135
|
+
if (!noon(date)) throw new UsageError(`无法识别日期 '${date}'`);
|
|
139
136
|
setField(file, cmd === 'touch' ? 'last_review' : 'last_progress', date);
|
|
140
137
|
return EXIT.OK;
|
|
141
138
|
}
|
|
142
|
-
|
|
143
|
-
return EXIT.USAGE;
|
|
139
|
+
throw new UsageError(`不认识的子命令「${cmd}」,只有 list / due / touch / progress`);
|
|
144
140
|
},
|
|
145
141
|
};
|
package/scripts/cmd/init.js
CHANGED
|
@@ -11,7 +11,13 @@ const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
|
11
11
|
const { register } = require('../lib/workdir');
|
|
12
12
|
const { writeWorkdir } = require('../lib/template');
|
|
13
13
|
|
|
14
|
-
const USAGE =
|
|
14
|
+
const USAGE = `work-rhythm init <目录> —— 新建一个工作目录:用包里带的模板铺出来(个人信息空白、数据源全关、没有记录),并登记
|
|
15
|
+
|
|
16
|
+
用法:
|
|
17
|
+
work-rhythm init ~/workspace/my-work
|
|
18
|
+
work-rhythm init my-work 相对路径按当前目录算;上级目录没有就一起建
|
|
19
|
+
建好之后对 AI 说「开始引导」。工作目录只是机器上的一个目录:不 git init,不同步到任何仓库。
|
|
20
|
+
退出码: 0 建好;2 用法不对,或目录已经存在(拿它当工作目录用 work-rhythm workdir register);5 包不完整或铺出来的不是空白的(会把建了一半的目录删掉)。`;
|
|
15
21
|
|
|
16
22
|
module.exports = {
|
|
17
23
|
usage: USAGE,
|
package/scripts/cmd/listen.js
CHANGED
|
@@ -11,7 +11,9 @@ const { settings } = require('../lib/config');
|
|
|
11
11
|
const { createBot } = require('../lib/bot');
|
|
12
12
|
const launchd = require('../lib/launchd');
|
|
13
13
|
|
|
14
|
-
const USAGE =
|
|
14
|
+
const USAGE = `work-rhythm listen <子命令> —— 值守:消息渠道的收消息一半。在 IM 里说的话交给绑定的 agent 处理,回复发回来
|
|
15
|
+
|
|
16
|
+
用法: work-rhythm listen <子命令>
|
|
15
17
|
probe [--seconds N] 连上去看握手和收到的每一帧,不调 agent,到时退出(默认 60 秒)。第一次验证用
|
|
16
18
|
run 值守:一直连着,收到消息就处理
|
|
17
19
|
handle "<消息>" 不连云之家,只走一遍「交给 agent → 打印回复」,调试 agent 配置
|
|
@@ -19,7 +21,10 @@ const USAGE = `用法: work-rhythm listen <子命令>
|
|
|
19
21
|
reset 丢掉机器人的专属会话,下一句从新会话开始(在聊天里发 /new 也一样)
|
|
20
22
|
doctor 检查值守的配置和依赖,有问题逐条报出来
|
|
21
23
|
install | uninstall | status macOS launchd 常驻:开机自启、挂了拉起
|
|
22
|
-
|
|
24
|
+
前提:先绑 agent(work-rhythm agent bind),没绑就没有值守。消息一到先回「收到,正在处理」,agent 的回答随后到。
|
|
25
|
+
日志 01-inbox/_bot.log 只记时间、msgId、耗时,不记消息内容。
|
|
26
|
+
退出码: 0 完成;2 用法不对;3 没配置好(没绑 agent、钥匙串里没有地址、不是 yzj-robot 渠道);4 缺依赖;5 连不上或没处理成功。
|
|
27
|
+
步骤见 docs/channels.md「收消息:值守」。`;
|
|
23
28
|
|
|
24
29
|
const positiveInt = (value, fallback) => {
|
|
25
30
|
const n = Number(value);
|
package/scripts/cmd/notify.js
CHANGED
|
@@ -14,7 +14,15 @@ const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
|
14
14
|
const { settings } = require('../lib/config');
|
|
15
15
|
const { pushText, notifyChannel } = require('../lib/notify');
|
|
16
16
|
|
|
17
|
-
const USAGE =
|
|
17
|
+
const USAGE = `work-rhythm notify "<文字>" 或 … | work-rhythm notify - —— 把一段文字经消息渠道推给我本人(渠道写在 profile 的 preferences.notify)
|
|
18
|
+
|
|
19
|
+
用法:
|
|
20
|
+
work-rhythm notify "今天的重点三件" 文字直接给
|
|
21
|
+
… | work-rhythm notify - 文字从标准输入读(计划、复盘这类多行的用它)
|
|
22
|
+
只发纯文本,只发给我本人;云之家机器人每分钟最多 30 条。状态和信息写到标准错误,标准输出不留东西。
|
|
23
|
+
退出码: 0 已发出(渠道留空时什么都不发,也是 0);2 用法不对或渠道不认识;3 没配置好(钥匙串里没有地址、取不到我的 openId);
|
|
24
|
+
4 缺 security;5 发送失败(网络、接口拒绝)。
|
|
25
|
+
定时任务由本地服务代为推送时(指令末尾注明了「会替你推送」),不要再自己发。发消息前要先问用户。渠道怎么开通见 docs/channels.md。`;
|
|
18
26
|
|
|
19
27
|
async function readStdin(stream) {
|
|
20
28
|
const chunks = [];
|
package/scripts/cmd/pack.js
CHANGED
|
@@ -13,7 +13,13 @@ const { NAME, VERSION } = require('../cli');
|
|
|
13
13
|
// npm 给 tgz 起的名字:去掉 @、把 / 换成 -,再接版本号(work-rhythm@0.1.0 → work-rhythm-0.1.0.tgz)
|
|
14
14
|
const TGZ = `${NAME.replace(/^@/, '').replace('/', '-')}-${VERSION}.tgz`;
|
|
15
15
|
|
|
16
|
-
const USAGE =
|
|
16
|
+
const USAGE = `work-rhythm pack [--out <目录>] —— 把 skill 和这个包打成可分发的目录,给不用 npm 的人
|
|
17
|
+
|
|
18
|
+
用法:
|
|
19
|
+
work-rhythm pack 生成 01-inbox/outbox/work-rhythm-skill-<版本>/(站在工作目录里时)
|
|
20
|
+
work-rhythm pack --out <目录> 放到指定目录
|
|
21
|
+
里面是 work-rhythm/(skill)、给人看的安装说明、VERSION、这个包的 tgz;本机有 zip 再压一份 .zip。
|
|
22
|
+
退出码: 0;2 用法不对;3 这个包不完整;4 缺 npm;5 npm pack 或写入失败。`;
|
|
17
23
|
|
|
18
24
|
// VERSION 文件:包的版本,加上它来自哪个提交(工作区有没提交的改动时如实标出来)
|
|
19
25
|
function versionNote(packageRoot, env) {
|
package/scripts/cmd/rhythm.js
CHANGED
|
@@ -11,7 +11,9 @@ const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
|
11
11
|
const { settings } = require('../lib/config');
|
|
12
12
|
const { createRhythm } = require('../lib/rhythm');
|
|
13
13
|
|
|
14
|
-
const USAGE =
|
|
14
|
+
const USAGE = `work-rhythm rhythm <子命令> —— 本地服务:定时采集、定时把任务交给 agent 整理、经消息渠道推送、本机网页
|
|
15
|
+
|
|
16
|
+
用法: work-rhythm rhythm <子命令>
|
|
15
17
|
start [--tick 秒] 前台跑:到点跑采集和任务,起网页服务。要常驻用 install;--tick 是多久看一次表(默认 20,调试用)
|
|
16
18
|
stop | status 停掉;看在不在跑、下次什么时候跑什么
|
|
17
19
|
due [--at 时间] [--days 天数] 从某个时间起,列出什么时候跑什么
|
|
@@ -20,7 +22,10 @@ const USAGE = `用法: work-rhythm rhythm <子命令>
|
|
|
20
22
|
render <文件.md> 把一份 Markdown 渲染成页面,打到标准输出(自测、调试用)
|
|
21
23
|
doctor 检查配置和依赖,有问题逐条报出来
|
|
22
24
|
install | uninstall macOS launchd 常驻:开机自启、挂了拉起
|
|
23
|
-
|
|
25
|
+
定时任务只建在这个服务上,要先绑 agent(work-rhythm agent bind)。任务到点后:把 tasks/<id>.md 的指令交给 agent 跑一遍
|
|
26
|
+
(它会同步数据、整理事件流、写每日文件),agent 最后写的总结经消息渠道推给我,并附本机页面地址。
|
|
27
|
+
退出码: 0 完成;2 用法不对;3 没配置好(没绑 agent、配置有问题、已经在跑);4 缺依赖;5 运行失败;doctor 查出问题时是 1。
|
|
28
|
+
定义和用法见 docs/scheduled-tasks.md,agent 见 docs/agents.md。`;
|
|
24
29
|
|
|
25
30
|
const positiveInt = (value, fallback) => {
|
|
26
31
|
const n = Number(value);
|
package/scripts/cmd/selftest.js
CHANGED
|
@@ -6,7 +6,13 @@ const path = require('node:path');
|
|
|
6
6
|
const { spawnSync } = require('node:child_process');
|
|
7
7
|
const { UsageError } = require('../lib/run');
|
|
8
8
|
|
|
9
|
-
const USAGE =
|
|
9
|
+
const USAGE = `work-rhythm selftest [--test-name-pattern=…] —— 自测:跑 scripts/test/ 下的用例(node --test),二十秒左右
|
|
10
|
+
|
|
11
|
+
用法:
|
|
12
|
+
work-rhythm selftest 全部
|
|
13
|
+
work-rhythm selftest --test-name-pattern=notify 只跑名字匹配的
|
|
14
|
+
用假的命令行工具和本机的假 webhook,不碰真实账号、不发真消息。改完代码、发包之前跑一遍。
|
|
15
|
+
退出码就是 node --test 的:0 全过;有失败时非 0;2 用法不对。`;
|
|
10
16
|
|
|
11
17
|
module.exports = {
|
|
12
18
|
usage: USAGE,
|
package/scripts/cmd/sync.js
CHANGED
|
@@ -70,4 +70,15 @@ async function run(args, ctx, { deadlineMs } = {}) {
|
|
|
70
70
|
return fail;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
const USAGE = `work-rhythm sync [起始日期] [源 id …] —— 同步订阅源:依次跑 00-config/sources.yaml 里启用的采集器,原始数据落 01-inbox/<源id>/<今天>/
|
|
74
|
+
|
|
75
|
+
用法:
|
|
76
|
+
work-rhythm sync 从今天起,所有启用的源
|
|
77
|
+
work-rhythm sync 2026-12-01 从那天起,所有启用的源
|
|
78
|
+
work-rhythm sync 2026-12-01 im-yzj 只跑这一个源(可以写多个)
|
|
79
|
+
只跑一个源、自己指定输出目录,用 work-rhythm collect。每个源的全部输出和报错记在 01-inbox/_sync.log。
|
|
80
|
+
标准输出每个源一行:ok <源id>,或者 FAIL <源id>(详见 01-inbox/_sync.log)。
|
|
81
|
+
退出码 = 失败的源数(0 是全部成功);3 不在工作目录里。
|
|
82
|
+
采集只是取原料:把数据提炼成事件、合并进每日文件的「## 事件流」是 agent 做的(work-rhythm skill 的「同步并提炼事件」)。`;
|
|
83
|
+
|
|
84
|
+
module.exports = { usage: USAGE, run, resolveCollector, spawnCollector };
|
package/scripts/cmd/today.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
const { loadProfile } = require('../lib/config');
|
|
4
4
|
const { EXIT, UsageError, parseDate, fmtDate, today, weekdayCn, weekdayAbbr } = require('../lib/run');
|
|
5
5
|
|
|
6
|
-
const USAGE = '用法: work-rhythm today [YYYY-MM-DD]\n输出四行:DATE=、WEEKDAY=、DAILY_FILE=、IS_WORKDAY=';
|
|
7
6
|
|
|
8
7
|
// 是否工作日看 profile 的 preferences:workdays 里有没有这一天(Mon…Sun),holidays 里有没有这个日期。
|
|
9
8
|
// workdays 写成了空列表,就天天都不上班;没写这一项,就天天都上班
|
|
@@ -18,6 +17,19 @@ function resolve(root, dateArg) {
|
|
|
18
17
|
|
|
19
18
|
const output = (info) => `DATE=${info.date}\nWEEKDAY=${info.weekday}\nDAILY_FILE=${info.dailyFile}\nIS_WORKDAY=${info.isWorkday}\n`;
|
|
20
19
|
|
|
20
|
+
const USAGE = `work-rhythm today [YYYY-MM-DD] —— 今天在工作目录里的约定:日期、星期、每日文件路径、是否工作日
|
|
21
|
+
|
|
22
|
+
用法:
|
|
23
|
+
work-rhythm today 今天
|
|
24
|
+
work-rhythm today 2026-12-05 指定的一天
|
|
25
|
+
输出四行(给脚本和 agent 读的):
|
|
26
|
+
DATE=2026-12-05
|
|
27
|
+
WEEKDAY=五
|
|
28
|
+
DAILY_FILE=02-daily/2026-12/2026-12-05.md 路径相对工作目录
|
|
29
|
+
IS_WORKDAY=1 0 是休息日:不安排工作,定时任务不跑
|
|
30
|
+
是否工作日看 00-config/profile.yaml 的 preferences.workdays / holidays,不要自己算。
|
|
31
|
+
退出码: 0;2 日期写错(格式 YYYY-MM-DD);3 不在工作目录里(用 --workdir 指定)。`;
|
|
32
|
+
|
|
21
33
|
module.exports = {
|
|
22
34
|
usage: USAGE,
|
|
23
35
|
resolve,
|
package/scripts/cmd/workdir.js
CHANGED
|
@@ -14,13 +14,16 @@ const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
|
14
14
|
const { isWorkdir, configPath, register, resolveRoot, findAll } = require('../lib/workdir');
|
|
15
15
|
const { blank } = require('../lib/template');
|
|
16
16
|
|
|
17
|
-
const USAGE =
|
|
17
|
+
const USAGE = `work-rhythm workdir [子命令] —— 工作目录在哪:找、登记、扫描本机、清空白
|
|
18
|
+
|
|
19
|
+
用法: work-rhythm workdir [子命令]
|
|
18
20
|
找工作目录:打印 WORKDIR=<路径> 和 SOURCE=cwd|pointer;找不到时 WORKDIR 为空,退出码 1
|
|
19
21
|
register [目录] 把目录(默认当前目录)登记为工作目录,写进用户级配置
|
|
20
22
|
find [起点目录 …] 找本机疑似工作目录的目录(带 00-config/profile.yaml 的);制表符分隔:路径、初始化状态、称呼
|
|
21
23
|
config 打印用户级配置文件的位置
|
|
22
24
|
blank <目录> 把一个目录清成空白的工作目录
|
|
23
|
-
新建用 work-rhythm init
|
|
25
|
+
新建用 work-rhythm init <目录>。
|
|
26
|
+
退出码: 找不到时 1(WORKDIR 为空);0 完成;2 用法不对或目录不是工作目录;5 清空白时失败;3 不在工作目录里(register / blank)。`;
|
|
24
27
|
|
|
25
28
|
module.exports = {
|
|
26
29
|
usage: USAGE,
|
package/scripts/lib/agent.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
// 让本地的 agent 跑一句话、拿回它的回复。值守(bot.js)和本地服务(rhythm.js
|
|
2
|
-
// agent
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// 让本地的 agent 跑一句话、拿回它的回复。值守(bot.js)和本地服务(rhythm.js)共用,定时任务和机器人都靠它。
|
|
2
|
+
// agent 的值:zcode、dsh、qoder、claude 是预置(PRESETS,各家的无界面调用方式、续会话、权限模式都写在那里);
|
|
3
|
+
// 其余当成一条命令(按空白拆开,引号里的算一个参数;消息作为最后一个参数传给它,回复打到标准输出,退出码 0 算成功。
|
|
4
|
+
// 不支持管道、重定向这类 shell 写法——要的话包一层脚本)。
|
|
5
|
+
// 这里还管「本机装了哪些 agent」「现在是哪个 agent 在调这个 skill」(它给工具留的环境变量)、「把选定的 agent 绑进 local.env」,
|
|
6
|
+
// 给 work-rhythm agent 用。只用 node 自带的能力。找预置的命令行时返回 { problem } 而不是抛错,doctor 和真正运行时共用这一段。
|
|
5
7
|
'use strict';
|
|
6
8
|
const { spawnSync } = require('node:child_process');
|
|
7
9
|
const fs = require('node:fs');
|
|
@@ -65,6 +67,12 @@ function newestSubdir(dir, pick) {
|
|
|
65
67
|
return best;
|
|
66
68
|
}
|
|
67
69
|
|
|
70
|
+
// 在 PATH 上找一个命令。返回完整路径,找不到返回空串(不用 lib/run 的 which,免得和这里的 spawnSync 写法打架)
|
|
71
|
+
function onPath(name, env) {
|
|
72
|
+
const r = spawnSync('/usr/bin/which', [name], { encoding: 'utf8', env });
|
|
73
|
+
return r.status === 0 ? r.stdout.trim() : '';
|
|
74
|
+
}
|
|
75
|
+
|
|
68
76
|
function findZcode(env) {
|
|
69
77
|
const explicit = env.ZCODE_CLI;
|
|
70
78
|
if (explicit) {
|
|
@@ -77,6 +85,107 @@ function findZcode(env) {
|
|
|
77
85
|
return { problem: '找不到 ZCode 的命令行:装了 ZCode 桌面版的话它自带;也可以用 ZCODE_CLI 指定路径' };
|
|
78
86
|
}
|
|
79
87
|
|
|
88
|
+
// Qoder CLI(命令叫 qoder,老的 npm 包叫 qodercli):官方安装脚本 curl -fsSL https://qoder.com/install | bash,
|
|
89
|
+
// 或者 npm install -g @qoder-ai/qodercli。装了 Qoder 的 IDE 不等于装了它的命令行,找不到时提示一下
|
|
90
|
+
function findQoder(env) {
|
|
91
|
+
const explicit = env.QODER_CLI;
|
|
92
|
+
if (explicit) {
|
|
93
|
+
if (fs.existsSync(explicit)) return { command: path.resolve(explicit) };
|
|
94
|
+
return { problem: `QODER_CLI 指的文件不存在:${explicit}` };
|
|
95
|
+
}
|
|
96
|
+
for (const name of ['qoder', 'qodercli']) {
|
|
97
|
+
const found = onPath(name, env);
|
|
98
|
+
if (found) return { command: found };
|
|
99
|
+
}
|
|
100
|
+
for (const candidate of [path.join(os.homedir(), '.qoder', 'bin', 'qoder'), path.join(os.homedir(), '.local', 'bin', 'qoder')]) {
|
|
101
|
+
if (fs.existsSync(candidate)) return { command: candidate };
|
|
102
|
+
}
|
|
103
|
+
const ide = fs.existsSync('/Applications/Qoder.app') ? '本机装了 Qoder 的 IDE,但它的命令行是另装的:' : '';
|
|
104
|
+
return { problem: `找不到 Qoder 的命令行。${ide}安装:curl -fsSL https://qoder.com/install | bash(或 npm install -g @qoder-ai/qodercli),装完 qoder login;也可以用 QODER_CLI 指定路径` };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Claude Code 的命令行。ZCode 的命令行和它是同一套参数,但 ZCode 跑工具时也会留 CLAUDE_CODE_* 变量,所以认「谁在调用」时先看 ZCODE_*
|
|
108
|
+
function findClaude(env) {
|
|
109
|
+
const explicit = env.CLAUDE_CLI;
|
|
110
|
+
if (explicit) {
|
|
111
|
+
if (fs.existsSync(explicit)) return { command: path.resolve(explicit) };
|
|
112
|
+
return { problem: `CLAUDE_CLI 指的文件不存在:${explicit}` };
|
|
113
|
+
}
|
|
114
|
+
const found = onPath('claude', env);
|
|
115
|
+
if (found) return { command: found };
|
|
116
|
+
const local = path.join(os.homedir(), '.claude', 'local', 'claude');
|
|
117
|
+
if (fs.existsSync(local)) return { command: local };
|
|
118
|
+
return { problem: '找不到 Claude Code 的命令行:装了的话 claude 在 PATH 上;也可以用 CLAUDE_CLI 指定路径' };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Qoder 和 Claude Code 的 --output-format json 都是一个 JSON:回答在 result,会话在 session_id,出错时 is_error / subtype 会说。
|
|
122
|
+
// 字段名按各家文档写的;多认几个常见名字,免得版本之间改名就解析不出
|
|
123
|
+
function parseResultJson(stdout) {
|
|
124
|
+
let out;
|
|
125
|
+
try {
|
|
126
|
+
out = JSON.parse(stdout);
|
|
127
|
+
} catch {
|
|
128
|
+
return { failed: `输出不是 JSON:${tail(stdout)}` };
|
|
129
|
+
}
|
|
130
|
+
if (!out || typeof out !== 'object') return { failed: '输出不是 JSON' };
|
|
131
|
+
const reply = [out.result, out.response, out.text].find((v) => typeof v === 'string') ?? '';
|
|
132
|
+
const subtype = typeof out.subtype === 'string' ? out.subtype : '';
|
|
133
|
+
if (out.is_error === true || /error|cancel|max_turns/.test(subtype)) return { failed: `没跑完:${subtype || 'is_error'}:${tail(reply || out.error_message || '', 200)}` };
|
|
134
|
+
if (!reply.trim()) return { failed: '输出里没有回答' };
|
|
135
|
+
return { reply, sessionId: typeof out.session_id === 'string' ? out.session_id : typeof out.sessionId === 'string' ? out.sessionId : '' };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 预置。find 找命令行;plan 拼出一次无界面运行的参数;modes 是我们的权限名(plan 只读 / edit 可写工作目录)换成各家的叫法
|
|
139
|
+
const PRESETS = {
|
|
140
|
+
zcode: { label: 'ZCode 自带的命令行', modes: { plan: 'plan', edit: 'edit' }, find: findZcode },
|
|
141
|
+
dsh: { label: 'DSH(DeepSeek Harness)的 headless 模式', modes: { plan: 'read-only', edit: 'workspace-write' }, find: findDsh },
|
|
142
|
+
// Qoder:edit 用 accept_edits(改文件自动放行,shell 命令在无界面下会被拒);任务里要跑命令的话把 YZJ_BOT_AGENT_MODE 写成 auto
|
|
143
|
+
qoder: { label: 'Qoder CLI', modes: { plan: 'plan', edit: 'accept_edits' }, find: findQoder },
|
|
144
|
+
claude: { label: 'Claude Code 的命令行', modes: { plan: 'plan', edit: 'acceptEdits' }, find: findClaude },
|
|
145
|
+
};
|
|
146
|
+
const PRESET_NAMES = Object.keys(PRESETS);
|
|
147
|
+
const presetKind = (name) => (PRESET_NAMES.includes(name) ? name : 'custom');
|
|
148
|
+
|
|
149
|
+
// 现在是哪个 agent 在调这个 skill:看它给工具留的环境变量。ZCode 也留 CLAUDE_CODE_*,所以先认 ZCODE_*;
|
|
150
|
+
// Qoder 的变量名没有文档,按前缀认(QODER_CLI 是我们自己指定的路径,不算)
|
|
151
|
+
function invokingAgent(env) {
|
|
152
|
+
if (env.ZCODE_SESSION_ID || env.ZCODE_ENV || env.ZCODE_APP_VERSION) return 'zcode';
|
|
153
|
+
if (env.DSH_SESSION_ID || env.DSH_SHELL) return 'dsh';
|
|
154
|
+
if (Object.keys(env).some((k) => /^QODER_/.test(k) && k !== 'QODER_CLI')) return 'qoder';
|
|
155
|
+
if (env.CLAUDECODE === '1' || env.CLAUDE_CODE_SESSION_ID || env.CLAUDE_CODE_ENTRYPOINT) return 'claude';
|
|
156
|
+
return '';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 本机装了哪些预置。每个是 { name, label, found, command, problem, invoking }
|
|
160
|
+
function installedAgents(env) {
|
|
161
|
+
const invoking = invokingAgent(env);
|
|
162
|
+
return PRESET_NAMES.map((name) => {
|
|
163
|
+
const found = PRESETS[name].find(env);
|
|
164
|
+
return { name, label: PRESETS[name].label, found: !found.problem, command: found.command || found.cli || '', problem: found.problem || '', invoking: invoking === name };
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 把选定的 agent 写进 collectors/local.env 的 YZJ_BOT_AGENT(没有这个文件就从样例复制一份)。
|
|
169
|
+
// 先确认它在本机找得到。返回 { name, kind, file } 或 { problems }
|
|
170
|
+
function bindAgent(root, env, name) {
|
|
171
|
+
const problems = createAgent({ root, agent: name, env }).doctor();
|
|
172
|
+
if (problems.length) return { problems };
|
|
173
|
+
const dir = path.join(root, 'collectors');
|
|
174
|
+
const file = path.join(dir, 'local.env');
|
|
175
|
+
if (!fs.existsSync(file)) {
|
|
176
|
+
const example = path.join(dir, 'local.env.example');
|
|
177
|
+
if (fs.existsSync(example)) fs.copyFileSync(example, file);
|
|
178
|
+
else fs.writeFileSync(file, '');
|
|
179
|
+
}
|
|
180
|
+
const value = /[\s"'$`]/.test(name) ? `'${name.replace(/'/g, `'\\''`)}'` : `"${name}"`;
|
|
181
|
+
const lines = fs.readFileSync(file, 'utf8').split('\n');
|
|
182
|
+
const at = lines.findIndex((l) => /^#?\s*YZJ_BOT_AGENT=/.test(l));
|
|
183
|
+
if (at >= 0) lines[at] = `YZJ_BOT_AGENT=${value}`;
|
|
184
|
+
else lines.push(`YZJ_BOT_AGENT=${value}`);
|
|
185
|
+
fs.writeFileSync(file, `${lines.join('\n').replace(/\n*$/, '')}\n`);
|
|
186
|
+
return { name, kind: presetKind(name), file };
|
|
187
|
+
}
|
|
188
|
+
|
|
80
189
|
// DSH 的命令行要用它自带的 Node 跑(设 ELECTRON_RUN_AS_NODE 当普通 node 用),入口是 @deepseek-ai/dsh 的 bin.js
|
|
81
190
|
function findDsh(env) {
|
|
82
191
|
const home = env.DSH_HOME || path.join(os.homedir(), '.dsh');
|
|
@@ -158,19 +267,19 @@ function parseDsh(stdout) {
|
|
|
158
267
|
// doctor() → 配置和依赖的问题,一条一个字符串;没有就是空数组
|
|
159
268
|
function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = '', log = () => {}, env = process.env }) {
|
|
160
269
|
const name = String(agent || '').trim();
|
|
161
|
-
const kind = name
|
|
270
|
+
const kind = presetKind(name);
|
|
162
271
|
|
|
163
272
|
function doctor() {
|
|
164
|
-
if (!name) return ['
|
|
165
|
-
if (kind
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return
|
|
273
|
+
if (!name) return [`没绑 agent:定时任务和机器人值守都要一个 agent 来跑。先 work-rhythm agent detect 看本机有哪些,再 work-rhythm agent bind <${PRESET_NAMES.join('|')}|命令>(写进 collectors/local.env 的 YZJ_BOT_AGENT)`];
|
|
274
|
+
if (kind !== 'custom') {
|
|
275
|
+
const found = PRESETS[kind].find(env);
|
|
276
|
+
if (found.problem) return [found.problem];
|
|
277
|
+
// dsh 还要用桌面应用里配好的模型
|
|
278
|
+
if (kind === 'dsh') {
|
|
279
|
+
const entries = readDshPatchEntries(found.home);
|
|
280
|
+
if (entries.problem) return [entries.problem];
|
|
281
|
+
}
|
|
282
|
+
return [];
|
|
174
283
|
}
|
|
175
284
|
// 带斜杠的按工作目录里的相对路径找,其余在 PATH 上找
|
|
176
285
|
const [first] = splitCommand(name);
|
|
@@ -212,6 +321,36 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
|
|
|
212
321
|
};
|
|
213
322
|
}
|
|
214
323
|
|
|
324
|
+
// qoder:qoder --cwd <工作目录> --permission-mode <模式> [--resume <会话>] --output-format json --print <这句话>
|
|
325
|
+
// 模型和登录跟着 Qoder CLI 自己的配置走(~/.qoder)。无界面模式下它不弹确认,要确认的操作一律被拒,
|
|
326
|
+
// 所以 edit 对应 accept_edits(改文件自动放行);任务里要跑 shell 的话得用 auto(写在 YZJ_BOT_AGENT_MODE 里)
|
|
327
|
+
function planQoder(prompt) {
|
|
328
|
+
const q = findQoder(env);
|
|
329
|
+
if (q.problem) return q;
|
|
330
|
+
const permission = PRESETS.qoder.modes[mode] || mode;
|
|
331
|
+
const base = ['--cwd', root, '--permission-mode', permission];
|
|
332
|
+
return {
|
|
333
|
+
command: q.command,
|
|
334
|
+
env,
|
|
335
|
+
withSession: (sid) => [...base, ...(sid ? ['--resume', sid] : []), '--output-format', 'json', '--print', prompt],
|
|
336
|
+
parse: parseResultJson,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// claude:参数和 ZCode 那套一样,只是模式名和 JSON 的字段不同
|
|
341
|
+
function planClaude(prompt) {
|
|
342
|
+
const c = findClaude(env);
|
|
343
|
+
if (c.problem) return c;
|
|
344
|
+
const permission = PRESETS.claude.modes[mode] || mode;
|
|
345
|
+
const base = ['--cwd', root, '--permission-mode', permission];
|
|
346
|
+
return {
|
|
347
|
+
command: c.command,
|
|
348
|
+
env,
|
|
349
|
+
withSession: (sid) => [...base, ...(sid ? ['--resume', sid] : []), '--output-format', 'json', '-p', prompt],
|
|
350
|
+
parse: parseResultJson,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
215
354
|
// dsh:用 headless 模式答一句就退出。工作目录就是当前目录,所以 tsx 用绝对路径导入;回答在 --json 事件的 final 里。
|
|
216
355
|
// 模型和凭据跟着 DSH 桌面应用里的配置走。plan → read-only,edit → workspace-write,别的值原样传给它
|
|
217
356
|
function planDsh(prompt, stateDir) {
|
|
@@ -246,7 +385,7 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
|
|
|
246
385
|
return reply ? { reply } : { failed: '它没有输出任何回复' };
|
|
247
386
|
}
|
|
248
387
|
const prompt = preamble ? `${message}\n\n${preamble}` : message;
|
|
249
|
-
const plan =
|
|
388
|
+
const plan = { zcode: planZcode, dsh: planDsh, qoder: planQoder, claude: planClaude }[kind](prompt, stateDir);
|
|
250
389
|
if (plan.problem) return { failed: plan.problem, config: true }; // 配置问题:调用方按「没配置好」处理
|
|
251
390
|
let used = session;
|
|
252
391
|
let r = runProcess(plan.command, plan.withSession(used), plan.env);
|
|
@@ -267,4 +406,4 @@ function createAgent({ root, agent, mode = 'plan', timeoutS = 300, preamble = ''
|
|
|
267
406
|
return { kind, run, doctor };
|
|
268
407
|
}
|
|
269
408
|
|
|
270
|
-
module.exports = { createAgent, splitCommand };
|
|
409
|
+
module.exports = { createAgent, splitCommand, PRESETS, PRESET_NAMES, invokingAgent, installedAgents, bindAgent, findZcode, findDsh, findQoder, findClaude };
|
package/scripts/lib/bot.js
CHANGED
|
@@ -185,7 +185,7 @@ function createBot({ root, env }) {
|
|
|
185
185
|
if (found.problem) throw new Fail(found.code, found.problem);
|
|
186
186
|
if (needAgent) {
|
|
187
187
|
if (!OWNER) throw new Fail(EXIT.NOT_CONFIGURED, '取不到我的 openId:在 collectors/local.env 里写 YZJ_MY_OPEN_ID,值守只处理我本人的消息');
|
|
188
|
-
if (!AGENT) throw new Fail(EXIT.NOT_CONFIGURED, '
|
|
188
|
+
if (!AGENT) throw new Fail(EXIT.NOT_CONFIGURED, '没绑 agent,值守开不了:先 work-rhythm agent detect 看本机有哪些,再 work-rhythm agent bind <名字>(写进 collectors/local.env 的 YZJ_BOT_AGENT);也可以手写它,值是预置的名字或一条把回复打到标准输出的命令');
|
|
189
189
|
}
|
|
190
190
|
return deriveWsUrl(found.webhook);
|
|
191
191
|
}
|