work-rhythm 0.0.0-stage → 0.1.0
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 +84 -0
- package/.agents/notes/decisions/2026-11-28-long-term-goals-ride-existing-tasks.md +36 -0
- package/.agents/notes/decisions/2026-11-29-building-a-workdir-means-cloning-the-template.md +25 -0
- package/.agents/notes/decisions/2026-11-29-find-workdir-cwd-first-then-registered.md +32 -0
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +48 -0
- package/.agents/notes/decisions/2026-11-30-cost-entry-writes-into-empty-cells-directly.md +25 -0
- package/.agents/notes/decisions/2026-11-30-dependencies-are-allowed-user-installs-missing-ones.md +30 -0
- package/.agents/notes/decisions/2026-11-30-markdown-is-the-record-html-is-only-a-view.md +25 -0
- package/.agents/notes/decisions/2026-11-30-message-channels-are-preset-in-the-skill.md +34 -0
- package/.agents/notes/decisions/2026-11-30-no-workdir-offers-to-create-one-by-default.md +34 -0
- package/.agents/notes/decisions/2026-11-30-paging-requires-coverage-only-when-enabled.md +27 -0
- package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +39 -0
- package/.agents/notes/decisions/2026-11-30-push-via-personal-robot-webhook.md +33 -0
- package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +37 -0
- package/.agents/notes/decisions/2026-11-30-skill-ships-with-its-template-in-a-zip.md +30 -0
- package/.agents/notes/decisions/2026-11-30-skill-stays-generic-user-provides-cli-or-mcp.md +25 -0
- package/.agents/notes/decisions/2026-12-01-everything-is-js-behind-work-rhythm.md +45 -0
- package/.agents/notes/pitfalls/2026-11-29-do-not-hand-roll-git-log.md +22 -0
- package/.agents/notes/pitfalls/2026-11-29-empty-or-partial-collector-output.md +23 -0
- package/.agents/notes/pitfalls/2026-11-29-skill-description-is-truncated.md +21 -0
- package/.agents/notes/pitfalls/2026-11-29-tool-shell-is-zsh.md +24 -0
- package/.agents/notes/pitfalls/2026-11-30-compare-timestamps-as-digits.md +22 -0
- package/.agents/notes/pitfalls/2026-11-30-message-search-lags-after-sending.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-rewriting-a-file-with-empty-output-after-tool-failure.md +20 -0
- package/.agents/notes/pitfalls/2026-11-30-secret-scan-false-positive-on-hyphenated-file-names.md +18 -0
- package/.agents/notes/pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md +29 -0
- package/.agents/notes/pitfalls/2026-11-30-yzj-cli-list-limit-is-20.md +25 -0
- package/.agents/notes/pitfalls/2026-11-30-zsh-no-word-splitting-special-names.md +23 -0
- package/.agents/notes/pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md +22 -0
- package/.agents/notes/pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md +18 -0
- package/.agents/skills/work-rhythm/SKILL.md +277 -0
- package/00-config/profile.example.yaml +54 -0
- package/00-config/sources.yaml +115 -0
- package/AGENTS.md +31 -0
- package/README.md +105 -2
- package/collectors/approval-yzj.js +114 -0
- package/collectors/calendar-yzj.js +48 -0
- package/collectors/file-activity.js +131 -0
- package/collectors/git-activity.js +105 -0
- package/collectors/im-yzj.js +151 -0
- package/collectors/linky-issues.js +88 -0
- package/collectors/local.env.example +52 -0
- package/docs/channels.md +95 -0
- package/docs/formats.md +285 -0
- package/docs/im-adapters.md +79 -0
- package/docs/scheduled-tasks.md +115 -0
- package/docs/sources.md +104 -0
- package/package.json +37 -4
- package/scripts/cli.js +98 -0
- package/scripts/cmd/check.js +297 -0
- package/scripts/cmd/collect.js +25 -0
- package/scripts/cmd/decisions.js +236 -0
- package/scripts/cmd/goals.js +145 -0
- package/scripts/cmd/init.js +53 -0
- package/scripts/cmd/listen.js +91 -0
- package/scripts/cmd/notify.js +42 -0
- package/scripts/cmd/pack.js +105 -0
- package/scripts/cmd/rhythm.js +102 -0
- package/scripts/cmd/selftest.js +23 -0
- package/scripts/cmd/sync.js +73 -0
- package/scripts/cmd/today.js +29 -0
- package/scripts/cmd/workdir.js +81 -0
- package/scripts/lib/agent.js +270 -0
- package/scripts/lib/bot.js +376 -0
- package/scripts/lib/collector.js +74 -0
- package/scripts/lib/config.js +250 -0
- package/scripts/lib/launchd.js +82 -0
- package/scripts/lib/log.js +22 -0
- package/scripts/lib/markdown.js +208 -0
- package/scripts/lib/notify.js +43 -0
- package/scripts/lib/rhythm.js +508 -0
- package/scripts/lib/robot.js +64 -0
- package/scripts/lib/run.js +151 -0
- package/scripts/lib/template.js +144 -0
- package/scripts/lib/workdir.js +89 -0
- package/scripts/test/check.test.js +217 -0
- package/scripts/test/collectors.test.js +574 -0
- package/scripts/test/config.test.js +119 -0
- package/scripts/test/decisions.test.js +129 -0
- package/scripts/test/fake-webhook.js +31 -0
- package/scripts/test/goals.test.js +109 -0
- package/scripts/test/harness.js +279 -0
- package/scripts/test/init.test.js +125 -0
- package/scripts/test/listen.test.js +362 -0
- package/scripts/test/notify.test.js +176 -0
- package/scripts/test/pack.test.js +141 -0
- package/scripts/test/rhythm.test.js +324 -0
- package/scripts/test/stubs.js +140 -0
- package/scripts/test/today.test.js +73 -0
- package/scripts/test/workdir.test.js +152 -0
- package/scripts/work-rhythm.js +18 -0
- package/tasks/alerts-morning.md +24 -0
- package/tasks/carryover.md +16 -0
- package/tasks/daily-plan.md +29 -0
- package/tasks/evening-review.md +27 -0
- package/tasks/linky-cost-check.md +13 -0
- package/tasks/linky-cost-fill.md +20 -0
- package/tasks/noon-checkin.md +15 -0
- package/tasks/weekly-rhythm.md +18 -0
- package/templates/03-carryover/CARRYOVER.md +14 -0
- package/templates/03-carryover/DECISIONS.md +10 -0
- package/templates/04-patterns/WORK_RHYTHM.md +16 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// 长期目标(05-goals/<id>.md)的小工具。目标的元数据在文件开头的 frontmatter 里,每个字段一行。
|
|
2
|
+
// 输出都是制表符分隔,第一行是表头
|
|
3
|
+
// work-rhythm goals list 列出 active 和 paused 的目标
|
|
4
|
+
// work-rhythm goals due [日期] 列出到期该检查的 active 目标(默认今天)
|
|
5
|
+
// work-rhythm goals touch <id> [日期] 把 last_review 更新为某天(默认今天),检查完用
|
|
6
|
+
// work-rhythm goals progress <id> [日期] 把 last_progress 更新为某天(默认今天),有进展时用
|
|
7
|
+
'use strict';
|
|
8
|
+
const fs = require('node:fs');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { EXIT, noon, today, daysBetween, readText, writeFileAtomic } = require('../lib/run');
|
|
11
|
+
|
|
12
|
+
const USAGE = '用法: work-rhythm goals list | due [日期] | touch <id> [日期] | progress <id> [日期]';
|
|
13
|
+
const HEADER_LIST = 'id\tstatus\ttitle\tsince_review\tsince_progress';
|
|
14
|
+
const HEADER_DUE = 'id\ttitle\tsince_review\treview_every';
|
|
15
|
+
|
|
16
|
+
const splitLines = (text) => {
|
|
17
|
+
const lines = text.split('\n');
|
|
18
|
+
if (lines.length && lines[lines.length - 1] === '') lines.pop();
|
|
19
|
+
return lines;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// frontmatter 里某个字段的值:去掉首尾空白、行内注释和引号;同一字段出现几次,取最后一个
|
|
23
|
+
function field(file, key) {
|
|
24
|
+
const lines = splitLines(readText(file));
|
|
25
|
+
if (lines[0] !== '---') return '';
|
|
26
|
+
let v = '';
|
|
27
|
+
for (const line of lines.slice(1)) {
|
|
28
|
+
if (line === '---') break;
|
|
29
|
+
const i = line.indexOf(':');
|
|
30
|
+
if (i > 0 && line.slice(0, i) === key) v = line.slice(i + 1);
|
|
31
|
+
}
|
|
32
|
+
v = v.trim();
|
|
33
|
+
const q = v[0];
|
|
34
|
+
if (q === '"' || q === "'") {
|
|
35
|
+
const end = v.indexOf(q, 1);
|
|
36
|
+
return end > 0 ? v.slice(1, end) : '';
|
|
37
|
+
}
|
|
38
|
+
const cut = v.indexOf(' #');
|
|
39
|
+
return (cut >= 0 ? v.slice(0, cut) : v).trim();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 只改 frontmatter 里的这一行并保住行内注释;字段不存在就补在结尾的 --- 之前
|
|
43
|
+
function setField(file, key, value) {
|
|
44
|
+
const lines = splitLines(readText(file));
|
|
45
|
+
const out = [];
|
|
46
|
+
let closed = false;
|
|
47
|
+
let hit = false;
|
|
48
|
+
lines.forEach((line, n) => {
|
|
49
|
+
if (n === 0) {
|
|
50
|
+
out.push(line);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!closed && line === '---') {
|
|
54
|
+
if (!hit) out.push(`${key}: ${value}`);
|
|
55
|
+
out.push(line);
|
|
56
|
+
closed = true;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!closed) {
|
|
60
|
+
const i = line.indexOf(':');
|
|
61
|
+
if (i > 0 && line.slice(0, i) === key) {
|
|
62
|
+
const j = line.indexOf(' #');
|
|
63
|
+
out.push(`${key}: ${value}${j > 0 ? ` ${line.slice(j)}` : ''}`);
|
|
64
|
+
hit = true;
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
out.push(line);
|
|
69
|
+
});
|
|
70
|
+
writeFileAtomic(file, out.join('\n') + '\n');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 那个字段对应的日子距截止日期几天。没有日期、或日期读不懂,输出 ?
|
|
74
|
+
function age(file, key, fallback, until) {
|
|
75
|
+
let d = field(file, key);
|
|
76
|
+
if (!d) d = field(file, fallback);
|
|
77
|
+
return d && noon(d) && noon(until) ? daysBetween(d, until) : '?';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const goalFiles = (root) => {
|
|
81
|
+
const dir = path.join(root, '05-goals');
|
|
82
|
+
let names = [];
|
|
83
|
+
try {
|
|
84
|
+
names = fs.readdirSync(dir).filter((n) => n.endsWith('.md')).sort();
|
|
85
|
+
} catch {
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
return names.map((n) => path.join(dir, n));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
module.exports = {
|
|
92
|
+
usage: USAGE,
|
|
93
|
+
async run(args, ctx) {
|
|
94
|
+
const { root, stderr, stdout } = ctx;
|
|
95
|
+
const [cmd = 'list', ...rest] = args;
|
|
96
|
+
if (cmd === 'list') {
|
|
97
|
+
const date = today();
|
|
98
|
+
stdout.write(`${HEADER_LIST}\n`);
|
|
99
|
+
for (const f of goalFiles(root)) {
|
|
100
|
+
const status = field(f, 'status');
|
|
101
|
+
if (status !== 'active' && status !== 'paused') continue;
|
|
102
|
+
stdout.write([field(f, 'id'), status, field(f, 'title'), age(f, 'last_review', 'started', date), age(f, 'last_progress', 'started', date)].join('\t') + '\n');
|
|
103
|
+
}
|
|
104
|
+
return EXIT.OK;
|
|
105
|
+
}
|
|
106
|
+
if (cmd === 'due') {
|
|
107
|
+
const date = rest[0] || today();
|
|
108
|
+
if (!noon(date)) {
|
|
109
|
+
stderr.write(`goals: 无法识别日期 '${date}'\n`);
|
|
110
|
+
return EXIT.USAGE;
|
|
111
|
+
}
|
|
112
|
+
stdout.write(`${HEADER_DUE}\n`);
|
|
113
|
+
for (const f of goalFiles(root)) {
|
|
114
|
+
if (field(f, 'status') !== 'active') continue;
|
|
115
|
+
const everyRaw = field(f, 'review_every');
|
|
116
|
+
const every = /^\d+$/.test(everyRaw) ? Number(everyRaw) : 14;
|
|
117
|
+
const sinceRaw = age(f, 'last_review', 'started', date);
|
|
118
|
+
const since = /^\d+$/.test(String(sinceRaw)) ? Number(sinceRaw) : every;
|
|
119
|
+
if (since >= every) stdout.write([field(f, 'id'), field(f, 'title'), since, every].join('\t') + '\n');
|
|
120
|
+
}
|
|
121
|
+
return EXIT.OK;
|
|
122
|
+
}
|
|
123
|
+
if (cmd === 'touch' || cmd === 'progress') {
|
|
124
|
+
const [id, dateArg] = rest;
|
|
125
|
+
if (!id) {
|
|
126
|
+
stderr.write(`用法: work-rhythm goals ${cmd} <id> [日期]\n`);
|
|
127
|
+
return EXIT.USAGE;
|
|
128
|
+
}
|
|
129
|
+
const date = dateArg || today();
|
|
130
|
+
const file = path.join(root, '05-goals', `${id}.md`);
|
|
131
|
+
if (!fs.existsSync(file)) {
|
|
132
|
+
stderr.write(`goals: 找不到 05-goals/${id}.md\n`);
|
|
133
|
+
return EXIT.USAGE;
|
|
134
|
+
}
|
|
135
|
+
if (!noon(date)) {
|
|
136
|
+
stderr.write(`goals: 无法识别日期 '${date}'\n`);
|
|
137
|
+
return EXIT.USAGE;
|
|
138
|
+
}
|
|
139
|
+
setField(file, cmd === 'touch' ? 'last_review' : 'last_progress', date);
|
|
140
|
+
return EXIT.OK;
|
|
141
|
+
}
|
|
142
|
+
stderr.write(`${USAGE}\n`);
|
|
143
|
+
return EXIT.USAGE;
|
|
144
|
+
},
|
|
145
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// 新建一个工作目录:用包里带的模板铺出来(个人信息是空白的、数据源全部关着、没有记录),
|
|
2
|
+
// git init 一个不连任何远程的本地仓库(本机有 git 的话),然后登记为工作目录。建好之后对 AI 说「开始引导」。
|
|
3
|
+
// 用法: work-rhythm init <目录>
|
|
4
|
+
// 退出码: 0 建好;2 用法不对,或目录已经存在;5 包不完整、铺出来的不是空白的(会把建了一半的目录删掉)。
|
|
5
|
+
// 目录可以写相对路径,按当前目录算(work-rhythm init my-work → ./my-work)。
|
|
6
|
+
// 脚本和内置采集器不进工作目录,它们在这个包里;sources.yaml 用 id 引用,自己写的采集器放 collectors/ 下写路径
|
|
7
|
+
'use strict';
|
|
8
|
+
const fs = require('node:fs');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { spawnSync } = require('node:child_process');
|
|
11
|
+
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
12
|
+
const { register } = require('../lib/workdir');
|
|
13
|
+
const { writeWorkdir } = require('../lib/template');
|
|
14
|
+
|
|
15
|
+
const USAGE = '用法: work-rhythm init <目录>';
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
usage: USAGE,
|
|
19
|
+
async run(args, ctx) {
|
|
20
|
+
const { stdout, stderr, cwd, env, packageRoot } = ctx;
|
|
21
|
+
if (args.length !== 1) throw new UsageError(USAGE);
|
|
22
|
+
const target = path.resolve(cwd, args[0]);
|
|
23
|
+
if (fs.existsSync(target)) throw new Fail(EXIT.USAGE, `${target} 已经存在。拿它当工作目录用 work-rhythm workdir register;要新建就换个位置`);
|
|
24
|
+
// 上级目录没有就一起建
|
|
25
|
+
fs.mkdirSync(target, { recursive: true });
|
|
26
|
+
let problems;
|
|
27
|
+
try {
|
|
28
|
+
problems = writeWorkdir(target, packageRoot);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
31
|
+
throw new Fail(EXIT.FAILED, error.message);
|
|
32
|
+
}
|
|
33
|
+
if (problems.length) {
|
|
34
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
35
|
+
throw new Fail(EXIT.FAILED, `铺出来的不是空白的工作目录,已经撤掉:${problems.join(';')}`);
|
|
36
|
+
}
|
|
37
|
+
// 一个不连任何远程的本地仓库:记录有版本可回溯,又不会推到别人的仓库去。没有 git 就跳过
|
|
38
|
+
const git = spawnSync('git', ['init', '-q'], { cwd: target, env, encoding: 'utf8' });
|
|
39
|
+
if (git.status !== 0) stderr.write(`init: 本机没有 git,${target} 没有初始化成 git 仓库\n`);
|
|
40
|
+
// 有些助手只认 CLAUDE.md
|
|
41
|
+
try {
|
|
42
|
+
fs.symlinkSync('AGENTS.md', path.join(target, 'CLAUDE.md'));
|
|
43
|
+
} catch {
|
|
44
|
+
// 已经有就算了
|
|
45
|
+
}
|
|
46
|
+
const dir = fs.realpathSync(target);
|
|
47
|
+
register(dir, env);
|
|
48
|
+
stdout.write(`WORKDIR=${dir}\n`);
|
|
49
|
+
stderr.write(`init: 已新建 ${dir}:个人信息是空白的、数据源全部关着、没有记录、不连任何远程\n`);
|
|
50
|
+
stderr.write('下一步:用能读到这个目录的 AI 打开它,对 AI 说「开始引导」。\n');
|
|
51
|
+
return EXIT.OK;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// 值守:消息渠道的收消息一半。实现在 lib/bot.js,这里只管子命令、输出和退出码。
|
|
2
|
+
// 配置在 collectors/local.env(说明见 local.env.example):YZJ_BOT_AGENT 让哪个 agent 回复——预置 zcode(ZCode 自带的命令行,
|
|
3
|
+
// 模型是它自己的默认选择)、dsh(DSH 的 headless 模式,模型和凭据跟着 DSH 桌面应用里的配置走),或一条自己的命令。
|
|
4
|
+
// YZJ_MY_OPEN_ID 只认我本人。机器人的 Webhook 地址和推送共用,取自系统钥匙串。值守属于 yzj-robot 渠道,preferences.notify
|
|
5
|
+
// 不是它时拒绝运行。消息一到先回「收到,正在处理」(YZJ_BOT_ACK=0 关掉)。
|
|
6
|
+
// 日志写 01-inbox/_bot.log,只记时间、msgId、发消息人、耗时、退出码,不记消息内容。步骤见 docs/channels.md「收消息:值守」。
|
|
7
|
+
'use strict';
|
|
8
|
+
const path = require('node:path');
|
|
9
|
+
const { EXIT, UsageError } = require('../lib/run');
|
|
10
|
+
const { settings } = require('../lib/config');
|
|
11
|
+
const { createBot } = require('../lib/bot');
|
|
12
|
+
const launchd = require('../lib/launchd');
|
|
13
|
+
|
|
14
|
+
const USAGE = `用法: work-rhythm listen <子命令>
|
|
15
|
+
probe [--seconds N] 连上去看握手和收到的每一帧,不调 agent,到时退出(默认 60 秒)。第一次验证用
|
|
16
|
+
run 值守:一直连着,收到消息就处理
|
|
17
|
+
handle "<消息>" 不连云之家,只走一遍「交给 agent → 打印回复」,调试 agent 配置
|
|
18
|
+
handle-frame '<JSON>' 把一帧当作刚收到的处理一遍,回复照发(自测用)
|
|
19
|
+
reset 丢掉机器人的专属会话,下一句从新会话开始(在聊天里发 /new 也一样)
|
|
20
|
+
doctor 检查值守的配置和依赖,有问题逐条报出来
|
|
21
|
+
install | uninstall | status macOS launchd 常驻:开机自启、挂了拉起
|
|
22
|
+
退出码: 0 完成;2 用法不对;3 没配置好;4 缺依赖;5 连不上或没处理成功。详见 lib/bot.js 的文件头和 docs/channels.md`;
|
|
23
|
+
|
|
24
|
+
const positiveInt = (value, fallback) => {
|
|
25
|
+
const n = Number(value);
|
|
26
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
module.exports = {
|
|
30
|
+
usage: USAGE,
|
|
31
|
+
async run(args, ctx) {
|
|
32
|
+
const { root, stdout, env, packageRoot } = ctx;
|
|
33
|
+
const [cmd, ...rest] = args;
|
|
34
|
+
const bot = createBot({ root, env: settings(root, env) });
|
|
35
|
+
const entry = path.join(packageRoot, 'scripts', 'work-rhythm.js');
|
|
36
|
+
const logFile = path.join(root, '01-inbox', '_bot.launchd.log');
|
|
37
|
+
switch (cmd) {
|
|
38
|
+
case 'probe': {
|
|
39
|
+
let seconds = 60;
|
|
40
|
+
if (rest[0] === '--seconds') seconds = positiveInt(rest[1], 60);
|
|
41
|
+
else if (rest.length) throw new UsageError(USAGE);
|
|
42
|
+
return bot.probe(seconds);
|
|
43
|
+
}
|
|
44
|
+
case 'run':
|
|
45
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
46
|
+
return bot.run();
|
|
47
|
+
case 'handle':
|
|
48
|
+
if (!rest.length) throw new UsageError(USAGE);
|
|
49
|
+
stdout.write(`${bot.handle(rest.join(' '))}\n`);
|
|
50
|
+
return EXIT.OK;
|
|
51
|
+
case 'handle-frame': {
|
|
52
|
+
if (!rest.length) throw new UsageError(USAGE);
|
|
53
|
+
const r = await bot.processFrame(rest.join(' '), true);
|
|
54
|
+
stdout.write(`${r.result}\n`);
|
|
55
|
+
return r.code;
|
|
56
|
+
}
|
|
57
|
+
case 'reset':
|
|
58
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
59
|
+
bot.clearSession();
|
|
60
|
+
stdout.write('已丢掉机器人的专属会话,下一句从新会话开始。\n');
|
|
61
|
+
return EXIT.OK;
|
|
62
|
+
case 'doctor': {
|
|
63
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
64
|
+
const problems = bot.doctorProblems();
|
|
65
|
+
if (problems.length) stdout.write(`${problems.join('\n')}\n`);
|
|
66
|
+
return problems.length ? 1 : EXIT.OK;
|
|
67
|
+
}
|
|
68
|
+
case 'install': {
|
|
69
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
70
|
+
// 先把配置查一遍,装一个跑不起来的常驻没有意义
|
|
71
|
+
bot.requireConfig(true);
|
|
72
|
+
const plist = launchd.install({ kind: 'bot', root, entry, args: ['listen', 'run', '--workdir', root], logFile });
|
|
73
|
+
stdout.write(`已装成常驻:${plist}\n日志:${logFile} 和 ${path.join(root, '01-inbox', '_bot.log')}\n改了配置要重启一次:launchctl kickstart -k gui/${process.getuid()}/${launchd.label('bot', root)}\n`);
|
|
74
|
+
return EXIT.OK;
|
|
75
|
+
}
|
|
76
|
+
case 'uninstall':
|
|
77
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
78
|
+
launchd.uninstall('bot', root);
|
|
79
|
+
stdout.write('已卸载常驻。\n');
|
|
80
|
+
return EXIT.OK;
|
|
81
|
+
case 'status': {
|
|
82
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
83
|
+
const s = launchd.status('bot', root);
|
|
84
|
+
stdout.write(s.running ? `在跑${s.pid ? `,pid ${s.pid}` : ''}。日志:${path.join(root, '01-inbox', '_bot.log')}\n` : '没在跑。\n');
|
|
85
|
+
return s.running ? EXIT.OK : 1;
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
throw new UsageError(USAGE);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// 把一段文字推给我本人,这是「消息渠道」的出口(docs/channels.md)
|
|
2
|
+
// 用法: work-rhythm notify "<文字>" 或 … | work-rhythm notify - (文字从标准输入读)
|
|
3
|
+
// 退出码: 0 = 已发出(preferences.notify 留空时什么都不发,也是 0);2 = 用法不对,或渠道不认识;
|
|
4
|
+
// 3 = 没配置好(钥匙串里没有地址、取不到我的 openId);4 = 缺依赖;5 = 发送失败。
|
|
5
|
+
// 状态和信息都写到标准错误,标准输出不留东西。凭据不打印:接口报错时带回的 token 会打码。
|
|
6
|
+
// 渠道(写在 preferences.notify 里)。给别的 IM 新增一个渠道的做法见 docs/channels.md:
|
|
7
|
+
// yzj-robot 云之家个人机器人私聊给我,消息带「有人@你」的提醒。只能发纯文本;每个机器人每分钟 30 条。
|
|
8
|
+
// 不经过 yzj-cli,登录过期不影响推送。Webhook 地址就是凭据,存在系统钥匙串里
|
|
9
|
+
// (服务名 YZJ_ROBOT_KEYCHAIN_SERVICE,默认 work-rhythm-yzj-robot-webhook),不进任何文件。
|
|
10
|
+
// 收件人 openId 取 collectors/local.env 的 YZJ_MY_OPEN_ID,没配就用 yzj-cli whoami 现取。
|
|
11
|
+
// 建机器人、把地址存进钥匙串的步骤见 docs/channels.md「云之家」。
|
|
12
|
+
'use strict';
|
|
13
|
+
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
14
|
+
const { settings } = require('../lib/config');
|
|
15
|
+
const { pushText, notifyChannel } = require('../lib/notify');
|
|
16
|
+
|
|
17
|
+
const USAGE = '用法: work-rhythm notify "<文字>" 或 … | work-rhythm notify -';
|
|
18
|
+
|
|
19
|
+
async function readStdin(stream) {
|
|
20
|
+
const chunks = [];
|
|
21
|
+
for await (const chunk of stream) chunks.push(chunk);
|
|
22
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = {
|
|
26
|
+
usage: USAGE,
|
|
27
|
+
async run(args, ctx) {
|
|
28
|
+
const { root, stderr, env } = ctx;
|
|
29
|
+
// 先看参数再看标准输入:没给文字就直接报用法,不等输入
|
|
30
|
+
if (args.length !== 1) throw new UsageError(USAGE);
|
|
31
|
+
const text = args[0] === '-' ? await readStdin(ctx.stdin) : args[0];
|
|
32
|
+
if (!text.replace(/\s/g, '')) throw new UsageError('没有给要发的文字');
|
|
33
|
+
if (!notifyChannel(root)) {
|
|
34
|
+
stderr.write('notify: preferences.notify 留空,不推送(只写文件)\n');
|
|
35
|
+
return EXIT.OK;
|
|
36
|
+
}
|
|
37
|
+
const r = await pushText(root, text, { env: settings(root, env) });
|
|
38
|
+
if (!r.ok) throw new Fail(r.code, r.reason);
|
|
39
|
+
stderr.write(`notify: 已通过云之家机器人发给我(msgId ${r.msgId},会话 ${r.groupId})\n`);
|
|
40
|
+
return EXIT.OK;
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// 把 skill 和 npm 包打成一个可分发的目录(本机有 zip 就顺手压一下):别人不需要这个仓库的访问权也能装上、开始引导
|
|
2
|
+
// 用法: work-rhythm pack [--out <目录>]
|
|
3
|
+
// 生成 <out>/work-rhythm-skill-<版本>/:SKILL.md、README.md(安装说明)、VERSION、这个包的 tgz(npm pack 打出来的,名字是 npm 定的)。
|
|
4
|
+
// 不带 --out 时放在当前工作目录的 01-inbox/outbox/(不入库)。
|
|
5
|
+
// 模板是 init 现铺的,包里只有空白的记录(templates/),个人信息和记录一律不带
|
|
6
|
+
'use strict';
|
|
7
|
+
const fs = require('node:fs');
|
|
8
|
+
const os = require('node:os');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
const { spawnSync } = require('node:child_process');
|
|
11
|
+
const { EXIT, Fail, UsageError, which, tail } = require('../lib/run');
|
|
12
|
+
const { NAME, VERSION } = require('../cli');
|
|
13
|
+
// npm 给 tgz 起的名字:去掉 @、把 / 换成 -,再接版本号(work-rhythm@0.1.0 → work-rhythm-0.1.0.tgz)
|
|
14
|
+
const TGZ = `${NAME.replace(/^@/, '').replace('/', '-')}-${VERSION}.tgz`;
|
|
15
|
+
|
|
16
|
+
const USAGE = '用法: work-rhythm pack [--out <目录>]';
|
|
17
|
+
|
|
18
|
+
// VERSION 文件:包的版本,加上它来自哪个提交(工作区有没提交的改动时如实标出来)
|
|
19
|
+
function versionNote(packageRoot, env) {
|
|
20
|
+
const git = (args) => spawnSync('git', args, { cwd: packageRoot, env, encoding: 'utf8' });
|
|
21
|
+
const head = git(['rev-parse', '--short', 'HEAD']);
|
|
22
|
+
if (head.status !== 0) return `${VERSION}(不在 git 仓库里)`;
|
|
23
|
+
const dirty = (git(['status', '--porcelain']).stdout || '').trim();
|
|
24
|
+
return `${VERSION}(提交 ${head.stdout.trim()}${dirty ? ',含没提交的改动' : ''})`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const README = (version, tgz) => `# work-rhythm skill:安装和开始
|
|
28
|
+
|
|
29
|
+
给 AI 助手用的 skill:个人工作目录的搭建引导与日常运转。它自带 npm 包(${tgz}),不需要任何别的仓库的访问权,也不联网拉别的东西。
|
|
30
|
+
|
|
31
|
+
## 安装
|
|
32
|
+
|
|
33
|
+
1. 装 npm 包(要 node 22 以上)。在这个目录里运行:
|
|
34
|
+
\`npm install -g ./${tgz}\`
|
|
35
|
+
装好后 \`work-rhythm --help\` 能跑。
|
|
36
|
+
2. 把整个 \`work-rhythm/\` 目录放进 AI 助手的 skills 目录:
|
|
37
|
+
- 只在某个项目里用:放到那个项目的 \`.agents/skills/\` 下。
|
|
38
|
+
- 在任何目录里都能用:放到用户级目录 \`~/.agents/skills/\` 下。
|
|
39
|
+
|
|
40
|
+
用会自动发现这个目录的 AI 助手(ZCode、DeepSeek Harness 等)打开任意项目。
|
|
41
|
+
|
|
42
|
+
## 开始
|
|
43
|
+
|
|
44
|
+
对 AI 说「开始引导」。它会问你想把工作目录放在哪(默认 \`~/workspace/my-work\`),用 \`work-rhythm init\` 建好、登记,再一步步问你:用哪个 IM、接哪些数据源、作息和定时任务。
|
|
45
|
+
|
|
46
|
+
建出来的工作目录是一个不连任何远程的本地 git 仓库(本机有 git 的话)。想备份到自己的私有仓库,进去 \`git remote add origin <地址>\` 再推。
|
|
47
|
+
|
|
48
|
+
## 升级
|
|
49
|
+
|
|
50
|
+
工作目录里不带脚本,脚本都在 npm 包里:换新版的 tgz 重新 \`npm install -g\` 一次,再用新的 \`work-rhythm/\` 覆盖旧的 skill 目录。\`00-config/\` 和自己的记录都不用动。
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
usage: USAGE,
|
|
55
|
+
async run(args, ctx) {
|
|
56
|
+
const { stdout, stderr, packageRoot } = ctx;
|
|
57
|
+
let out = '';
|
|
58
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
59
|
+
if (args[i] === '--out' && args[i + 1]) out = path.resolve(args[++i]);
|
|
60
|
+
else throw new UsageError(USAGE);
|
|
61
|
+
}
|
|
62
|
+
const skill = path.join(packageRoot, '.agents', 'skills', 'work-rhythm', 'SKILL.md');
|
|
63
|
+
if (!fs.existsSync(skill)) throw new Fail(EXIT.NOT_CONFIGURED, `${skill} 不在,这个包不完整`);
|
|
64
|
+
if (!which('npm')) throw new Fail(EXIT.MISSING_DEP, '打包要用 npm(node 自带),PATH 上没有');
|
|
65
|
+
if (!out) out = path.join(ctx.root || process.cwd(), '01-inbox', 'outbox');
|
|
66
|
+
const name = `work-rhythm-skill-${VERSION}`;
|
|
67
|
+
const stage = fs.mkdtempSync(path.join(os.tmpdir(), 'work-rhythm-pack-'));
|
|
68
|
+
try {
|
|
69
|
+
const pkg = path.join(stage, 'work-rhythm');
|
|
70
|
+
fs.mkdirSync(pkg);
|
|
71
|
+
fs.copyFileSync(skill, path.join(pkg, 'SKILL.md'));
|
|
72
|
+
fs.writeFileSync(path.join(pkg, 'VERSION'), `${versionNote(packageRoot, ctx.env)}\n`);
|
|
73
|
+
fs.writeFileSync(path.join(pkg, 'README.md'), README(VERSION, TGZ));
|
|
74
|
+
// 包从这个包所在的目录现打;--json 的输出第一行是文件名等信息
|
|
75
|
+
const packed = spawnSync('npm', ['pack', packageRoot, '--pack-destination', pkg, '--json', '--ignore-scripts'], { cwd: stage, env: ctx.env, encoding: 'utf8', timeout: 120000 });
|
|
76
|
+
let files = [];
|
|
77
|
+
try {
|
|
78
|
+
files = JSON.parse(packed.stdout);
|
|
79
|
+
} catch {
|
|
80
|
+
files = [];
|
|
81
|
+
}
|
|
82
|
+
const tgz = files[0] && files[0].filename;
|
|
83
|
+
if (packed.status !== 0 || tgz !== TGZ || !fs.existsSync(path.join(pkg, tgz))) {
|
|
84
|
+
throw new Fail(EXIT.FAILED, `npm pack 没打出包:${tail(packed.stderr, 400)}`);
|
|
85
|
+
}
|
|
86
|
+
const count = Array.isArray(files[0].files) ? files[0].files.length : 0;
|
|
87
|
+
fs.mkdirSync(out, { recursive: true });
|
|
88
|
+
const dest = path.join(out, name);
|
|
89
|
+
fs.rmSync(dest, { recursive: true, force: true });
|
|
90
|
+
fs.renameSync(pkg, dest);
|
|
91
|
+
// 有 zip 就压一份;没有就留目录,自己压
|
|
92
|
+
if (which('zip')) {
|
|
93
|
+
const z = spawnSync('zip', ['-qr', `${dest}.zip`, name], { cwd: out, encoding: 'utf8' });
|
|
94
|
+
if (z.status === 0) stdout.write(`${dest}.zip\n`);
|
|
95
|
+
else stderr.write(`pack: zip 没成功,目录留着:${dest}\n`);
|
|
96
|
+
} else {
|
|
97
|
+
stdout.write(`${dest}\n`);
|
|
98
|
+
}
|
|
99
|
+
stderr.write(`pack: ${name}(${count} 个文件的包 ${tgz},${fs.readFileSync(path.join(dest, 'VERSION'), 'utf8').trim()})\n`);
|
|
100
|
+
} finally {
|
|
101
|
+
fs.rmSync(stage, { recursive: true, force: true });
|
|
102
|
+
}
|
|
103
|
+
return EXIT.OK;
|
|
104
|
+
},
|
|
105
|
+
};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// work-rhythm 本地服务:定时采集、定时整理、推送、看页面,都由这一个常驻进程管,不再依赖 agent 运行时的定时器。
|
|
2
|
+
// 实现在 lib/rhythm.js,这里只管子命令、输出和退出码。
|
|
3
|
+
// 配置:
|
|
4
|
+
// profile.yaml 的 schedule 各任务的时间(已有的那一节);任务的指令在 tasks/<id>.md,一个任务一个文件
|
|
5
|
+
// profile.yaml 的 rhythm 采集的时间 sync、网页端口 port、推送开关 push、不推送的任务 quiet
|
|
6
|
+
// collectors/local.env YZJ_BOT_AGENT 让哪个 agent 整理(同值守:dsh、zcode,或一条命令)
|
|
7
|
+
// 推送走消息渠道(work-rhythm notify),所以 preferences.notify 要配好。任务要写文件,agent 以可写工作目录的权限跑。
|
|
8
|
+
// 说明见 docs/scheduled-tasks.md。
|
|
9
|
+
'use strict';
|
|
10
|
+
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
11
|
+
const { settings } = require('../lib/config');
|
|
12
|
+
const { createRhythm } = require('../lib/rhythm');
|
|
13
|
+
|
|
14
|
+
const USAGE = `用法: work-rhythm rhythm <子命令>
|
|
15
|
+
start [--tick 秒] 前台跑:到点跑采集和任务,起网页服务。要常驻用 install;--tick 是多久看一次表(默认 20,调试用)
|
|
16
|
+
stop | status 停掉;看在不在跑、下次什么时候跑什么
|
|
17
|
+
due [--at 时间] [--days 天数] 从某个时间起,列出什么时候跑什么
|
|
18
|
+
run <任务id|sync> [--push] 立刻跑一次,不等时间;带 --push 才推送
|
|
19
|
+
serve [--port 端口] 只起网页服务
|
|
20
|
+
render <文件.md> 把一份 Markdown 渲染成页面,打到标准输出(自测、调试用)
|
|
21
|
+
doctor 检查配置和依赖,有问题逐条报出来
|
|
22
|
+
install | uninstall macOS launchd 常驻:开机自启、挂了拉起
|
|
23
|
+
退出码: 0 完成;2 用法不对;3 没配置好;4 缺依赖;5 运行失败。详见 lib/rhythm.js 的文件头和 docs/scheduled-tasks.md`;
|
|
24
|
+
|
|
25
|
+
const positiveInt = (value, fallback) => {
|
|
26
|
+
const n = Number(value);
|
|
27
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
usage: USAGE,
|
|
32
|
+
async run(args, ctx) {
|
|
33
|
+
const { root, stdout, env, packageRoot } = ctx;
|
|
34
|
+
const [cmd, ...rest] = args;
|
|
35
|
+
const service = createRhythm({ root, env: settings(root, env), packageRoot });
|
|
36
|
+
switch (cmd) {
|
|
37
|
+
case 'start': {
|
|
38
|
+
const tick = rest.length === 2 && rest[0] === '--tick' ? positiveInt(rest[1], 0) : rest.length === 0 ? 20 : 0;
|
|
39
|
+
if (!tick) throw new UsageError(USAGE);
|
|
40
|
+
return service.start({ tickMs: tick * 1000 });
|
|
41
|
+
}
|
|
42
|
+
case 'serve':
|
|
43
|
+
if (!(rest.length === 0 || (rest.length === 2 && rest[0] === '--port'))) throw new UsageError(USAGE);
|
|
44
|
+
return service.serve(rest.length ? Number(rest[1]) : service.cfg.port);
|
|
45
|
+
case 'stop':
|
|
46
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
47
|
+
stdout.write(`${service.stop()}\n`);
|
|
48
|
+
return EXIT.OK;
|
|
49
|
+
case 'status':
|
|
50
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
51
|
+
stdout.write(`${service.statusText()}\n`);
|
|
52
|
+
return EXIT.OK;
|
|
53
|
+
case 'due': {
|
|
54
|
+
let at = new Date();
|
|
55
|
+
let days = 1;
|
|
56
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
57
|
+
if (rest[i] === '--at' && rest[i + 1]) {
|
|
58
|
+
const d = new Date(rest[i + 1].replace(' ', 'T'));
|
|
59
|
+
if (Number.isNaN(d.getTime())) throw new UsageError('--at 后面写「YYYY-MM-DD HH:MM」');
|
|
60
|
+
at = d;
|
|
61
|
+
i += 1;
|
|
62
|
+
} else if (rest[i] === '--days' && rest[i + 1]) {
|
|
63
|
+
days = positiveInt(rest[i + 1], 1);
|
|
64
|
+
i += 1;
|
|
65
|
+
} else throw new UsageError(USAGE);
|
|
66
|
+
}
|
|
67
|
+
stdout.write(`${service.due(at, days)}\n`);
|
|
68
|
+
return EXIT.OK;
|
|
69
|
+
}
|
|
70
|
+
case 'run': {
|
|
71
|
+
const id = rest[0];
|
|
72
|
+
if (!id || rest.slice(1).some((a) => a !== '--push')) throw new UsageError(USAGE);
|
|
73
|
+
return service.runJob(id, rest.includes('--push'));
|
|
74
|
+
}
|
|
75
|
+
case 'render':
|
|
76
|
+
if (rest.length !== 1) throw new UsageError(USAGE);
|
|
77
|
+
stdout.write(service.render(rest[0]));
|
|
78
|
+
return EXIT.OK;
|
|
79
|
+
case 'doctor': {
|
|
80
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
81
|
+
const problems = service.doctorProblems();
|
|
82
|
+
if (problems.length) stdout.write(`${problems.join('\n')}\n`);
|
|
83
|
+
return problems.length ? 1 : EXIT.OK;
|
|
84
|
+
}
|
|
85
|
+
case 'install': {
|
|
86
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
87
|
+
const problems = service.doctorProblems();
|
|
88
|
+
if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `配置有问题,先解决:\n${problems.join('\n')}`);
|
|
89
|
+
const plist = service.launchd.install();
|
|
90
|
+
stdout.write(`已装成常驻:${plist}\n页面:${service.baseUrl()} 日志:${root}/01-inbox/_rhythm.log\n`);
|
|
91
|
+
return EXIT.OK;
|
|
92
|
+
}
|
|
93
|
+
case 'uninstall':
|
|
94
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
95
|
+
service.launchd.uninstall();
|
|
96
|
+
stdout.write('已卸载常驻。\n');
|
|
97
|
+
return EXIT.OK;
|
|
98
|
+
default:
|
|
99
|
+
throw new UsageError(USAGE);
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// 自测:跑 scripts/test/ 下的用例(node --test),二十秒左右。改完脚本、发包之前跑一遍。
|
|
2
|
+
// 用法: work-rhythm selftest [传给 node --test 的参数,例如 --test-name-pattern=notify]
|
|
3
|
+
// 退出码就是 node --test 的:0 全过
|
|
4
|
+
'use strict';
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
const { spawnSync } = require('node:child_process');
|
|
7
|
+
const { UsageError } = require('../lib/run');
|
|
8
|
+
|
|
9
|
+
const USAGE = '用法: work-rhythm selftest [传给 node --test 的参数]';
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
usage: USAGE,
|
|
13
|
+
async run(args, ctx) {
|
|
14
|
+
for (const a of args) {
|
|
15
|
+
if (!a.startsWith('-')) throw new UsageError(USAGE);
|
|
16
|
+
}
|
|
17
|
+
// 自己就是在 node --test 里被调起来的时候(用例里跑自测),要抹掉 node 留下的标记,否则它会当成递归调用、一个用例都不跑
|
|
18
|
+
const env = { ...ctx.env };
|
|
19
|
+
delete env.NODE_TEST_CONTEXT;
|
|
20
|
+
const r = spawnSync(process.execPath, ['--test', '--test-reporter=spec', ...args, path.join(ctx.packageRoot, 'scripts', 'test')], { cwd: ctx.packageRoot, env, stdio: 'inherit' });
|
|
21
|
+
return r.status === null ? 5 : r.status;
|
|
22
|
+
},
|
|
23
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// 同步订阅源:依次运行 00-config/sources.yaml 里已启用源的采集器,原始数据落 01-inbox/<源>/<今天>/
|
|
2
|
+
// 用法: work-rhythm sync [起始日期 YYYY-MM-DD,默认今天] [源 id …(默认全部已启用的源)]
|
|
3
|
+
// 日志:01-inbox/_sync.log。退出码等于失败的源数
|
|
4
|
+
'use strict';
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
const { EXIT, run: spawn, today, fmtStamp, isExecutable } = require('../lib/run');
|
|
8
|
+
const { loadSources } = require('../lib/config');
|
|
9
|
+
|
|
10
|
+
// collector 字段的两种写法:内置采集器写 id(如 im-yzj),自己写的写工作目录里的路径。
|
|
11
|
+
// 返回能直接运行的文件路径;找不到返回空串
|
|
12
|
+
function resolveCollector(root, packageRoot, collector) {
|
|
13
|
+
const spec = String(collector || '').trim();
|
|
14
|
+
if (!spec) return '';
|
|
15
|
+
const isPath = /[\\/]/.test(spec) || /\.(sh|py|bash|zsh|js|mjs|cjs)$/.test(spec);
|
|
16
|
+
const file = isPath ? path.resolve(root, spec) : path.join(packageRoot, 'collectors', `${spec}.js`);
|
|
17
|
+
return fs.existsSync(file) ? file : '';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 有可执行权限的直接运行(采集器可以用任何语言写);.js 用当前这个 node 跑;其余按 bash 脚本运行。
|
|
21
|
+
// timeout 是最多等几秒,到了就杀掉
|
|
22
|
+
function spawnCollector(file, args, { cwd, env, input, timeout }) {
|
|
23
|
+
if (/\.(js|mjs|cjs)$/.test(file)) return spawn(process.execPath, [file, ...args], { cwd, env, input, timeout });
|
|
24
|
+
if (isExecutable(file)) return spawn(file, args, { cwd, env, input, timeout });
|
|
25
|
+
return spawn('bash', [file, ...args], { cwd, env, input, timeout });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// deadlineMs 给了的话,整轮采集到这个时间戳为止:每个采集器最多等剩下的秒数,到了还没完就算它失败
|
|
29
|
+
async function run(args, ctx, { deadlineMs } = {}) {
|
|
30
|
+
const { root, stdout, stderr, env, packageRoot } = ctx;
|
|
31
|
+
const sourcesFile = path.join(root, '00-config', 'sources.yaml');
|
|
32
|
+
if (!fs.existsSync(sourcesFile)) {
|
|
33
|
+
stderr.write('sync: 找不到 00-config/sources.yaml\n');
|
|
34
|
+
return EXIT.USAGE;
|
|
35
|
+
}
|
|
36
|
+
const date = today();
|
|
37
|
+
const since = args[0] || date;
|
|
38
|
+
const wanted = args.length ? args.slice(1) : [];
|
|
39
|
+
const log = path.join(root, '01-inbox', '_sync.log');
|
|
40
|
+
fs.mkdirSync(path.dirname(log), { recursive: true });
|
|
41
|
+
fs.appendFileSync(log, `${fmtStamp()} sync 开始 since=${since}\n`);
|
|
42
|
+
const sources = loadSources(root).filter((s) => s.enabled && s.collector !== 'none');
|
|
43
|
+
let fail = 0;
|
|
44
|
+
let ran = 0;
|
|
45
|
+
for (const src of sources) {
|
|
46
|
+
if (wanted.length && !wanted.includes(src.id)) continue;
|
|
47
|
+
const collector = resolveCollector(root, packageRoot, src.collector);
|
|
48
|
+
if (!collector) {
|
|
49
|
+
fs.appendFileSync(log, `${fmtStamp()} ${src.id} 的采集脚本不存在: ${src.collector}\n`);
|
|
50
|
+
stdout.write(`FAIL ${src.id}(采集脚本不存在: ${src.collector})\n`);
|
|
51
|
+
fail += 1;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
ran += 1;
|
|
55
|
+
// 采集器的输出目录和筛选都用源 id,不用脚本的文件名:几个源可以共用一个采集脚本
|
|
56
|
+
const budget = deadlineMs ? Math.max(1, Math.ceil((deadlineMs - Date.now()) / 1000)) : undefined;
|
|
57
|
+
const r = spawnCollector(collector, [path.join('01-inbox', src.id, date), since], { cwd: root, env: { ...env, WORK_RHYTHM_ROOT: root }, timeout: budget });
|
|
58
|
+
fs.appendFileSync(log, `${r.stdout}${r.stdout && !r.stdout.endsWith('\n') ? '\n' : ''}${r.stderr}`);
|
|
59
|
+
if (r.timedOut) fs.appendFileSync(log, `${fmtStamp()} ${src.id} 超过 ${budget} 秒没跑完\n`);
|
|
60
|
+
if (r.code === 0) {
|
|
61
|
+
fs.appendFileSync(log, `${fmtStamp()} ${src.id} ok\n`);
|
|
62
|
+
stdout.write(`ok ${src.id}\n`);
|
|
63
|
+
} else {
|
|
64
|
+
fs.appendFileSync(log, `${fmtStamp()} ${src.id} 失败\n`);
|
|
65
|
+
stdout.write(`FAIL ${src.id}(详见 01-inbox/_sync.log)\n`);
|
|
66
|
+
fail += 1;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!ran) stdout.write('sync: 没有可运行的源(看 00-config/sources.yaml 里的 enabled)\n');
|
|
70
|
+
return fail;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { usage: '用法: work-rhythm sync [起始日期] [源 id …]', run, resolveCollector, spawnCollector };
|