work-rhythm 0.0.0-stage → 0.1.1
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 +86 -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 +35 -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 +46 -0
- package/.agents/notes/decisions/2026-12-01-repo-is-not-a-workdir-templates-hold-workdir-files.md +29 -0
- package/.agents/notes/decisions/2026-12-01-repo-stays-generic-personal-data-lives-in-workdirs.md +32 -0
- package/.agents/notes/decisions/2026-12-01-workdir-is-not-a-git-repository.md +25 -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 +278 -0
- package/AGENTS.md +19 -0
- package/README.md +69 -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 +35 -4
- package/scripts/cli.js +101 -0
- package/scripts/cmd/check.js +330 -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 +49 -0
- package/scripts/cmd/listen.js +91 -0
- package/scripts/cmd/notify.js +42 -0
- package/scripts/cmd/pack.js +104 -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 +149 -0
- package/scripts/lib/workdir.js +98 -0
- package/scripts/test/check.test.js +279 -0
- package/scripts/test/collectors.test.js +574 -0
- package/scripts/test/config.test.js +118 -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 +277 -0
- package/scripts/test/init.test.js +123 -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 +333 -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/00-config/profile.example.yaml +54 -0
- package/templates/00-config/sources.yaml +115 -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
- package/templates/AGENTS.md +31 -0
- package/templates/README.md +73 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// 待决与决定的记录。03-carryover/DECISIONS.md 一次决定一行,只记结论;KEEP_DAYS 天以前的行挪到 archive/DECISIONS-<年-月>.md
|
|
2
|
+
// work-rhythm decisions add --item <事项> --options <选项> --choice <决定> [--reason <理由>] [--by <我|AI|一起>] [--date <YYYY-MM-DD>]
|
|
3
|
+
// work-rhythm decisions answer <事项> <决定> [--reason <理由>] [--by <我|AI|一起>] 把最近一条「未答」改成我的决定
|
|
4
|
+
// work-rhythm decisions done <事项> <落实> 给最近一条事项相符、还没写落实的行补上落实
|
|
5
|
+
// work-rhythm decisions list [天数] [--today] 最近 N 天(默认 60)的决定,连归档一起查;--today 只看今天的。制表符分隔,第一行是表头
|
|
6
|
+
// work-rhythm decisions archive [天数] 把 N 天(默认 60)以前的行挪到归档文件;add 之后会自动做一遍
|
|
7
|
+
// 每个字段都有字数上限(LIMITS),超了或者含竖线、制表符、换行,按参数错误退出(2):这份记录要能一眼扫完。
|
|
8
|
+
// --date 和环境变量 DECISIONS_TODAY 是给自测用的,平时不用管。格式见 docs/formats.md 的「待决与决定」。
|
|
9
|
+
'use strict';
|
|
10
|
+
const fs = require('node:fs');
|
|
11
|
+
const path = require('node:path');
|
|
12
|
+
const { EXIT, addDays, today, writeFileAtomic } = require('../lib/run');
|
|
13
|
+
|
|
14
|
+
const USAGE = `用法: work-rhythm decisions <子命令>
|
|
15
|
+
add --item <事项> [--options <选项>] --choice <决定> [--reason <理由>] [--by <我|AI|一起>] [--date <YYYY-MM-DD>]
|
|
16
|
+
answer <事项> <决定> [--reason <理由>] [--by <我|AI|一起>]
|
|
17
|
+
done <事项> <落实>
|
|
18
|
+
list [天数] [--today]
|
|
19
|
+
archive [天数]`;
|
|
20
|
+
|
|
21
|
+
const KEEP_DAYS = 60;
|
|
22
|
+
const COLUMNS = ['日期', '事项', '选项', '决定', '理由', '谁来做', '落实'];
|
|
23
|
+
const LIMITS = { 事项: 30, 选项: 60, 决定: 12, 理由: 30, 谁来做: 6, 落实: 30 };
|
|
24
|
+
const UNANSWERED = '未答';
|
|
25
|
+
const ROW = /^\| (\d{4}-\d{2}-\d{2}) \|/;
|
|
26
|
+
const SEPARATOR = /^\|[-:|]+\|$/;
|
|
27
|
+
const ARCHIVE_NAME = /^DECISIONS-(\d{4}-\d{2})\.md$/;
|
|
28
|
+
const HEADER = '| 日期 | 事项 | 选项 | 决定 | 理由 | 谁来做 | 落实 |\n|------|------|------|------|------|--------|------|';
|
|
29
|
+
|
|
30
|
+
// 参数错误:消息打给用户,退出码 2
|
|
31
|
+
class Fail extends Error {}
|
|
32
|
+
|
|
33
|
+
const splitLines = (text) => {
|
|
34
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
35
|
+
if (lines.length && lines[lines.length - 1] === '') lines.pop();
|
|
36
|
+
return lines;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// 今天;自测可以用环境变量 DECISIONS_TODAY 指定
|
|
40
|
+
function todayOf(env) {
|
|
41
|
+
const value = env.DECISIONS_TODAY;
|
|
42
|
+
if (!value) return today();
|
|
43
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) throw new Fail(`无法识别日期「${value}」(格式应为 YYYY-MM-DD)`);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 去掉首尾空白;含竖线、制表符、换行,或超过字数上限的,拒收
|
|
48
|
+
function clean(name, value) {
|
|
49
|
+
const v = String(value ?? '').trim();
|
|
50
|
+
if (/[|\t\r\n]/.test(v)) throw new Fail(`${name}里不能有竖线、制表符或换行`);
|
|
51
|
+
const len = Array.from(v).length;
|
|
52
|
+
if (len > LIMITS[name]) throw new Fail(`${name}太长了(${len} 字,上限 ${LIMITS[name]} 字):只记结论,长话留在别处`);
|
|
53
|
+
return v;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function days(text) {
|
|
57
|
+
if (text === undefined) return KEEP_DAYS;
|
|
58
|
+
if (!/^\d+$/.test(text) || Number(text) <= 0) throw new Fail(`天数应为正整数,收到「${text}」`);
|
|
59
|
+
return Number(text);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const cellsOf = (line) => line.trim().replace(/^\|+/, '').replace(/\|+$/, '').split('|').map((cell) => cell.trim());
|
|
63
|
+
const rowOf = (cells) => `| ${cells.join(' | ')} |`;
|
|
64
|
+
|
|
65
|
+
// 把 keep_days 天以前的行挪到所属月份的归档文件,返回挪了几行
|
|
66
|
+
function archive(file, keepDays, env) {
|
|
67
|
+
if (!fs.existsSync(file)) return 0;
|
|
68
|
+
const cutoff = addDays(todayOf(env), -keepDays);
|
|
69
|
+
const keep = [];
|
|
70
|
+
const moved = [];
|
|
71
|
+
for (const line of splitLines(fs.readFileSync(file, 'utf8'))) {
|
|
72
|
+
const m = ROW.exec(line);
|
|
73
|
+
if (m && m[1] < cutoff) moved.push(line);
|
|
74
|
+
else keep.push(line);
|
|
75
|
+
}
|
|
76
|
+
const byMonth = new Map();
|
|
77
|
+
for (const line of moved) {
|
|
78
|
+
const month = ROW.exec(line)[1].slice(0, 7);
|
|
79
|
+
if (!byMonth.has(month)) byMonth.set(month, []);
|
|
80
|
+
byMonth.get(month).push(line);
|
|
81
|
+
}
|
|
82
|
+
const dir = path.join(path.dirname(file), 'archive');
|
|
83
|
+
// 先追加到归档、后改原文件:中途出错最多是归档里多一份,不会丢行
|
|
84
|
+
for (const [month, rows] of [...byMonth.entries()].sort()) {
|
|
85
|
+
const target = path.join(dir, `DECISIONS-${month}.md`);
|
|
86
|
+
if (!fs.existsSync(target)) {
|
|
87
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
88
|
+
fs.writeFileSync(target, `# 决定归档 · ${month}\n\n由 \`work-rhythm decisions archive\` 从 [DECISIONS.md](../DECISIONS.md) 挪过来的行,只追加不修改。格式见 \`docs/formats.md\` 的「待决与决定」。\n\n${HEADER}\n`);
|
|
89
|
+
}
|
|
90
|
+
fs.appendFileSync(target, `${rows.join('\n')}\n`);
|
|
91
|
+
}
|
|
92
|
+
if (moved.length) writeFileAtomic(file, `${keep.join('\n')}\n`);
|
|
93
|
+
return moved.length;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --flag value 这种选项;位置参数按顺序留下
|
|
97
|
+
function parseFlags(args, names) {
|
|
98
|
+
const opts = {};
|
|
99
|
+
const positional = [];
|
|
100
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
101
|
+
const a = args[i];
|
|
102
|
+
if (names.includes(a)) {
|
|
103
|
+
if (i + 1 >= args.length) throw new Fail(`选项 ${a} 后面要跟一个值`);
|
|
104
|
+
opts[a.slice(2)] = args[++i];
|
|
105
|
+
} else if (a.startsWith('--') && names.includes(`${a}=`)) {
|
|
106
|
+
const [k, v] = a.slice(2).split(/=(.*)/);
|
|
107
|
+
opts[k] = v ?? '';
|
|
108
|
+
} else if (a.startsWith('--') && !names.includes(a)) {
|
|
109
|
+
throw new Fail(`不认识的选项 ${a}\n${USAGE}`);
|
|
110
|
+
} else positional.push(a);
|
|
111
|
+
}
|
|
112
|
+
return { opts, positional };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = {
|
|
116
|
+
usage: USAGE,
|
|
117
|
+
archive,
|
|
118
|
+
async run(args, ctx) {
|
|
119
|
+
const { root, stdout, stderr, env } = ctx;
|
|
120
|
+
const file = path.join(root, '03-carryover', 'DECISIONS.md');
|
|
121
|
+
const [sub, ...rest] = args;
|
|
122
|
+
const needFile = () => {
|
|
123
|
+
if (!fs.existsSync(file)) throw new Fail('找不到 03-carryover/DECISIONS.md');
|
|
124
|
+
};
|
|
125
|
+
const read = () => splitLines(fs.readFileSync(file, 'utf8'));
|
|
126
|
+
try {
|
|
127
|
+
if (sub === 'add') {
|
|
128
|
+
const { opts } = parseFlags(rest, ['--item', '--options', '--choice', '--reason', '--by', '--date']);
|
|
129
|
+
if (opts.item === undefined || opts.choice === undefined) throw new Fail(`add 需要写明 --item 和 --choice\n${USAGE}`);
|
|
130
|
+
const date = opts.date ? todayOf({ DECISIONS_TODAY: opts.date }) : todayOf(env);
|
|
131
|
+
const item = clean('事项', opts.item);
|
|
132
|
+
const choice = clean('决定', opts.choice);
|
|
133
|
+
if (!item) throw new Fail('缺少 --item(事项,一句话)');
|
|
134
|
+
if (!choice) throw new Fail('缺少 --choice(决定;没回答写「未答」)');
|
|
135
|
+
needFile();
|
|
136
|
+
const cells = [date, item, clean('选项', opts.options), choice, clean('理由', opts.reason), clean('谁来做', opts.by), ''];
|
|
137
|
+
const lines = read();
|
|
138
|
+
// 新行插在表格的最后一行后面;表里还没有行,就插在表头的分隔线后面
|
|
139
|
+
let index = 0;
|
|
140
|
+
lines.forEach((line, n) => {
|
|
141
|
+
if (ROW.test(line) || SEPARATOR.test(line)) index = n;
|
|
142
|
+
});
|
|
143
|
+
lines.splice(index + 1, 0, rowOf(cells));
|
|
144
|
+
writeFileAtomic(file, `${lines.join('\n')}\n`);
|
|
145
|
+
stderr.write(`已记:${date} ${item} → ${choice}\n`);
|
|
146
|
+
const moved = archive(file, KEEP_DAYS, env);
|
|
147
|
+
if (moved) stderr.write(`归档了 ${moved} 行(挪到了 03-carryover/archive/)\n`);
|
|
148
|
+
return EXIT.OK;
|
|
149
|
+
}
|
|
150
|
+
if (sub === 'archive') {
|
|
151
|
+
if (rest.length > 1) throw new Fail(USAGE);
|
|
152
|
+
const moved = archive(file, days(rest[0]), env);
|
|
153
|
+
if (moved) stderr.write(`decisions: 归档了 ${moved} 行(挪到了 03-carryover/archive/)\n`);
|
|
154
|
+
return EXIT.OK;
|
|
155
|
+
}
|
|
156
|
+
if (sub === 'list') {
|
|
157
|
+
const onlyToday = rest.includes('--today');
|
|
158
|
+
const rest2 = rest.filter((a) => a !== '--today');
|
|
159
|
+
if (rest2.length > 1) throw new Fail(USAGE);
|
|
160
|
+
const since = onlyToday ? todayOf(env) : addDays(todayOf(env), -days(rest2[0]));
|
|
161
|
+
stdout.write(`${COLUMNS.join('\t')}\n`);
|
|
162
|
+
// 归档文件按月份命名,只看窗口起点那个月及以后的;DECISIONS.md 是现在的,总是看
|
|
163
|
+
const dir = path.join(root, '03-carryover', 'archive');
|
|
164
|
+
const files = [];
|
|
165
|
+
let names = [];
|
|
166
|
+
try {
|
|
167
|
+
names = fs.readdirSync(dir).sort();
|
|
168
|
+
} catch {
|
|
169
|
+
names = [];
|
|
170
|
+
}
|
|
171
|
+
for (const name of names) {
|
|
172
|
+
const m = ARCHIVE_NAME.exec(name);
|
|
173
|
+
if (m && m[1] >= since.slice(0, 7)) files.push(path.join(dir, name));
|
|
174
|
+
}
|
|
175
|
+
if (fs.existsSync(file)) files.push(file);
|
|
176
|
+
for (const f of files) {
|
|
177
|
+
for (const line of splitLines(fs.readFileSync(f, 'utf8'))) {
|
|
178
|
+
const m = ROW.exec(line);
|
|
179
|
+
if (!m || m[1] < since) continue;
|
|
180
|
+
const cells = cellsOf(line);
|
|
181
|
+
while (cells.length < COLUMNS.length) cells.push('');
|
|
182
|
+
stdout.write(`${cells.slice(0, COLUMNS.length).join('\t')}\n`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return EXIT.OK;
|
|
186
|
+
}
|
|
187
|
+
if (sub === 'answer' || sub === 'done') {
|
|
188
|
+
const { opts, positional } = parseFlags(rest, ['--reason', '--by']);
|
|
189
|
+
if (positional.length < 2) throw new Fail(`${sub} 要写 <事项> 和<${sub === 'answer' ? '决定' : '落实'}>\n${USAGE}`);
|
|
190
|
+
const item = clean('事项', positional[0]);
|
|
191
|
+
needFile();
|
|
192
|
+
const lines = read();
|
|
193
|
+
let hit = -1;
|
|
194
|
+
// 同一件事问过几次时,改的是最近的那一条
|
|
195
|
+
lines.forEach((line, n) => {
|
|
196
|
+
if (!ROW.test(line)) return;
|
|
197
|
+
const cells = cellsOf(line);
|
|
198
|
+
if (cells.length >= COLUMNS.length && cells[1] === item && (sub === 'answer' ? cells[3] === UNANSWERED : cells[6] === '')) hit = n;
|
|
199
|
+
});
|
|
200
|
+
if (hit < 0) {
|
|
201
|
+
throw new Fail(sub === 'answer'
|
|
202
|
+
? `没有「${item}」的未答记录;没问过、是我直接说的决定,用 add 记一条`
|
|
203
|
+
: `没找到事项为「${item}」且还没写落实的那一行`);
|
|
204
|
+
}
|
|
205
|
+
const cells = cellsOf(lines[hit]);
|
|
206
|
+
if (sub === 'answer') {
|
|
207
|
+
const choice = clean('决定', positional[1]);
|
|
208
|
+
if (!choice) throw new Fail('缺少决定');
|
|
209
|
+
if (choice === UNANSWERED) throw new Fail(`决定不能还是「${UNANSWERED}」`);
|
|
210
|
+
cells[0] = todayOf(env); // 日期记我拿主意的那天
|
|
211
|
+
cells[3] = choice;
|
|
212
|
+
const reason = clean('理由', opts.reason);
|
|
213
|
+
const by = clean('谁来做', opts.by);
|
|
214
|
+
if (reason) cells[4] = reason;
|
|
215
|
+
if (by) cells[5] = by;
|
|
216
|
+
lines[hit] = rowOf(cells);
|
|
217
|
+
writeFileAtomic(file, `${lines.join('\n')}\n`);
|
|
218
|
+
stderr.write(`已记:${cells[0]} ${item} → ${choice}\n`);
|
|
219
|
+
} else {
|
|
220
|
+
const result = clean('落实', positional[1]);
|
|
221
|
+
if (!result) throw new Fail('缺少落实结果');
|
|
222
|
+
cells[6] = result;
|
|
223
|
+
lines[hit] = rowOf(cells);
|
|
224
|
+
writeFileAtomic(file, `${lines.join('\n')}\n`);
|
|
225
|
+
stderr.write(`已记落实:${item} → ${result}\n`);
|
|
226
|
+
}
|
|
227
|
+
return EXIT.OK;
|
|
228
|
+
}
|
|
229
|
+
throw new Fail(USAGE);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (!(error instanceof Fail)) throw error;
|
|
232
|
+
stderr.write(`decisions: ${error.message}\n`);
|
|
233
|
+
return EXIT.USAGE;
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
};
|
|
@@ -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,49 @@
|
|
|
1
|
+
// 新建一个工作目录:用包里带的模板铺出来(个人信息是空白的、数据源全部关着、没有记录),然后登记为工作目录。
|
|
2
|
+
// 建好之后对 AI 说「开始引导」。工作目录是个人数据,只是用户机器上的一个目录:这里不做 git init,也不往任何仓库同步。
|
|
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 { EXIT, Fail, UsageError } = require('../lib/run');
|
|
11
|
+
const { register } = require('../lib/workdir');
|
|
12
|
+
const { writeWorkdir } = require('../lib/template');
|
|
13
|
+
|
|
14
|
+
const USAGE = '用法: work-rhythm init <目录>';
|
|
15
|
+
|
|
16
|
+
module.exports = {
|
|
17
|
+
usage: USAGE,
|
|
18
|
+
async run(args, ctx) {
|
|
19
|
+
const { stdout, stderr, cwd, env, packageRoot } = ctx;
|
|
20
|
+
if (args.length !== 1) throw new UsageError(USAGE);
|
|
21
|
+
const target = path.resolve(cwd, args[0]);
|
|
22
|
+
if (fs.existsSync(target)) throw new Fail(EXIT.USAGE, `${target} 已经存在。拿它当工作目录用 work-rhythm workdir register;要新建就换个位置`);
|
|
23
|
+
// 上级目录没有就一起建
|
|
24
|
+
fs.mkdirSync(target, { recursive: true });
|
|
25
|
+
let problems;
|
|
26
|
+
try {
|
|
27
|
+
problems = writeWorkdir(target, packageRoot);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
30
|
+
throw new Fail(EXIT.FAILED, error.message);
|
|
31
|
+
}
|
|
32
|
+
if (problems.length) {
|
|
33
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
34
|
+
throw new Fail(EXIT.FAILED, `铺出来的不是空白的工作目录,已经撤掉:${problems.join(';')}`);
|
|
35
|
+
}
|
|
36
|
+
// 有些助手只认 CLAUDE.md
|
|
37
|
+
try {
|
|
38
|
+
fs.symlinkSync('AGENTS.md', path.join(target, 'CLAUDE.md'));
|
|
39
|
+
} catch {
|
|
40
|
+
// 已经有就算了
|
|
41
|
+
}
|
|
42
|
+
const dir = fs.realpathSync(target);
|
|
43
|
+
register(dir, env);
|
|
44
|
+
stdout.write(`WORKDIR=${dir}\n`);
|
|
45
|
+
stderr.write(`init: 已新建 ${dir}:个人信息是空白的、数据源全部关着、没有记录\n`);
|
|
46
|
+
stderr.write('下一步:用能读到这个目录的 AI 打开它,对 AI 说「开始引导」。\n');
|
|
47
|
+
return EXIT.OK;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
@@ -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,104 @@
|
|
|
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
|
+
|
|
47
|
+
## 升级
|
|
48
|
+
|
|
49
|
+
工作目录里不带脚本,脚本都在 npm 包里:换新版的 tgz 重新 \`npm install -g\` 一次,再用新的 \`work-rhythm/\` 覆盖旧的 skill 目录。\`00-config/\` 和自己的记录都不用动。
|
|
50
|
+
`;
|
|
51
|
+
|
|
52
|
+
module.exports = {
|
|
53
|
+
usage: USAGE,
|
|
54
|
+
async run(args, ctx) {
|
|
55
|
+
const { stdout, stderr, packageRoot } = ctx;
|
|
56
|
+
let out = '';
|
|
57
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
58
|
+
if (args[i] === '--out' && args[i + 1]) out = path.resolve(args[++i]);
|
|
59
|
+
else throw new UsageError(USAGE);
|
|
60
|
+
}
|
|
61
|
+
const skill = path.join(packageRoot, '.agents', 'skills', 'work-rhythm', 'SKILL.md');
|
|
62
|
+
if (!fs.existsSync(skill)) throw new Fail(EXIT.NOT_CONFIGURED, `${skill} 不在,这个包不完整`);
|
|
63
|
+
if (!which('npm')) throw new Fail(EXIT.MISSING_DEP, '打包要用 npm(node 自带),PATH 上没有');
|
|
64
|
+
if (!out) out = path.join(ctx.root || process.cwd(), '01-inbox', 'outbox');
|
|
65
|
+
const name = `work-rhythm-skill-${VERSION}`;
|
|
66
|
+
const stage = fs.mkdtempSync(path.join(os.tmpdir(), 'work-rhythm-pack-'));
|
|
67
|
+
try {
|
|
68
|
+
const pkg = path.join(stage, 'work-rhythm');
|
|
69
|
+
fs.mkdirSync(pkg);
|
|
70
|
+
fs.copyFileSync(skill, path.join(pkg, 'SKILL.md'));
|
|
71
|
+
fs.writeFileSync(path.join(pkg, 'VERSION'), `${versionNote(packageRoot, ctx.env)}\n`);
|
|
72
|
+
fs.writeFileSync(path.join(pkg, 'README.md'), README(VERSION, TGZ));
|
|
73
|
+
// 包从这个包所在的目录现打;--json 的输出第一行是文件名等信息
|
|
74
|
+
const packed = spawnSync('npm', ['pack', packageRoot, '--pack-destination', pkg, '--json', '--ignore-scripts'], { cwd: stage, env: ctx.env, encoding: 'utf8', timeout: 120000 });
|
|
75
|
+
let files = [];
|
|
76
|
+
try {
|
|
77
|
+
files = JSON.parse(packed.stdout);
|
|
78
|
+
} catch {
|
|
79
|
+
files = [];
|
|
80
|
+
}
|
|
81
|
+
const tgz = files[0] && files[0].filename;
|
|
82
|
+
if (packed.status !== 0 || tgz !== TGZ || !fs.existsSync(path.join(pkg, tgz))) {
|
|
83
|
+
throw new Fail(EXIT.FAILED, `npm pack 没打出包:${tail(packed.stderr, 400)}`);
|
|
84
|
+
}
|
|
85
|
+
const count = Array.isArray(files[0].files) ? files[0].files.length : 0;
|
|
86
|
+
fs.mkdirSync(out, { recursive: true });
|
|
87
|
+
const dest = path.join(out, name);
|
|
88
|
+
fs.rmSync(dest, { recursive: true, force: true });
|
|
89
|
+
fs.renameSync(pkg, dest);
|
|
90
|
+
// 有 zip 就压一份;没有就留目录,自己压
|
|
91
|
+
if (which('zip')) {
|
|
92
|
+
const z = spawnSync('zip', ['-qr', `${dest}.zip`, name], { cwd: out, encoding: 'utf8' });
|
|
93
|
+
if (z.status === 0) stdout.write(`${dest}.zip\n`);
|
|
94
|
+
else stderr.write(`pack: zip 没成功,目录留着:${dest}\n`);
|
|
95
|
+
} else {
|
|
96
|
+
stdout.write(`${dest}\n`);
|
|
97
|
+
}
|
|
98
|
+
stderr.write(`pack: ${name}(${count} 个文件的包 ${tgz},${fs.readFileSync(path.join(dest, 'VERSION'), 'utf8').trim()})\n`);
|
|
99
|
+
} finally {
|
|
100
|
+
fs.rmSync(stage, { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
return EXIT.OK;
|
|
103
|
+
},
|
|
104
|
+
};
|