work-rhythm 0.2.3 → 0.2.12
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 +5 -0
- package/.agents/notes/decisions/2026-11-30-pending-decisions-asked-in-a-chat-and-logged-briefly.md +1 -0
- package/.agents/notes/decisions/2026-12-01-agent-runs-the-login-for-the-user.md +30 -0
- package/.agents/notes/decisions/2026-12-01-question-cards-via-cli-tool-on-todays-page-answers-return-to-router.md +40 -0
- package/.agents/notes/decisions/2026-12-01-router-and-worker-pass-content-in-handoff-docs.md +1 -0
- package/.agents/notes/decisions/2026-12-01-router-runs-on-a-flash-model-with-low-effort.md +33 -0
- package/.agents/notes/decisions/2026-12-01-sync-feeds-refine-then-events-to-the-router.md +38 -0
- package/.agents/notes/pitfalls/2026-12-02-rhythm-start-in-the-foreground-displaces-the-launchd-service.md +27 -0
- package/.agents/skills/work-rhythm/SKILL.md +9 -3
- package/AGENTS.md +1 -1
- package/README.md +25 -0
- package/collectors/local.env.example +2 -2
- package/docs/agents.md +28 -5
- package/docs/formats.md +1 -1
- package/docs/router-worker.md +30 -8
- package/docs/scheduled-tasks.md +2 -2
- package/docs/sources.md +20 -0
- package/package.json +1 -1
- package/scripts/cli.js +1 -0
- package/scripts/cmd/agent.js +117 -16
- package/scripts/cmd/ask.js +104 -0
- package/scripts/cmd/decisions.js +4 -1
- package/scripts/cmd/sync.js +11 -3
- package/scripts/lib/agent.js +110 -5
- package/scripts/lib/asks.js +156 -0
- package/scripts/lib/engine.js +192 -8
- package/scripts/lib/handoff.js +2 -0
- package/scripts/lib/markdown.js +48 -19
- package/scripts/lib/rhythm.js +238 -38
- package/scripts/lib/router.js +60 -13
- package/scripts/lib/view.js +182 -29
- package/scripts/lib/worker.js +3 -2
- package/scripts/test/agent.test.js +109 -0
- package/scripts/test/decisions.test.js +18 -0
- package/scripts/test/harness.js +2 -0
- package/scripts/test/rhythm.test.js +84 -3
- package/scripts/test/router-helper.js +33 -3
- package/scripts/test/router.test.js +335 -31
- package/scripts/test/stubs.js +33 -2
- package/tasks/daily-plan.md +1 -2
- package/templates/00-config/profile.example.yaml +3 -1
- package/templates/03-carryover/DECISIONS.md +1 -1
package/scripts/lib/markdown.js
CHANGED
|
@@ -207,7 +207,7 @@ const BASE_CSS = `
|
|
|
207
207
|
--shadow:0 1px 2px rgba(20,18,14,.04),0 4px 16px rgba(20,18,14,.03);
|
|
208
208
|
--radius:14px;
|
|
209
209
|
--radius-sm:9px;
|
|
210
|
-
--max-w:
|
|
210
|
+
--max-w:1160px; /* 整个站就这一个宽度:换页签时版心不跳 */
|
|
211
211
|
--font:-apple-system,BlinkMacSystemFont,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","Helvetica Neue",Arial,sans-serif;
|
|
212
212
|
--mono:ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,monospace;
|
|
213
213
|
}
|
|
@@ -252,7 +252,6 @@ const BASE_CSS = `
|
|
|
252
252
|
*,*::before,*::after{box-sizing:border-box}
|
|
253
253
|
html{-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}
|
|
254
254
|
body{margin:0;padding:0 0 56px;background:var(--bg);color:var(--ink);font:15px/1.68 var(--font);font-feature-settings:"tnum"}
|
|
255
|
-
body.is-wide{--max-w:1160px}
|
|
256
255
|
a{color:var(--accent-ink);text-decoration:none;text-underline-offset:3px}
|
|
257
256
|
a:hover{text-decoration:underline}
|
|
258
257
|
|
|
@@ -270,13 +269,11 @@ a:hover{text-decoration:underline}
|
|
|
270
269
|
|
|
271
270
|
/* 主容器 */
|
|
272
271
|
.wrap{max-width:var(--max-w);margin:24px auto 0;padding:0 24px}
|
|
273
|
-
main.sheet{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:32px 36px;box-shadow:var(--shadow)}
|
|
274
272
|
main.stack{display:flex;flex-direction:column;gap:16px}
|
|
275
273
|
|
|
276
274
|
/* 通用排版 */
|
|
277
275
|
h1{font-size:25px;line-height:1.3;font-weight:700;letter-spacing:-.02em;margin:0 0 16px;color:var(--ink)}
|
|
278
276
|
h2{font-size:16.5px;line-height:1.4;font-weight:650;letter-spacing:-.01em;margin:28px 0 12px;padding-bottom:8px;border-bottom:1px solid var(--border-soft);color:var(--ink)}
|
|
279
|
-
main.sheet>h2:first-child{margin-top:0}
|
|
280
277
|
h3{font-size:14.5px;font-weight:650;margin:20px 0 8px;color:var(--ink)}
|
|
281
278
|
p{margin:0 0 11px;color:var(--ink-2)}
|
|
282
279
|
p:last-child{margin-bottom:0}
|
|
@@ -303,6 +300,8 @@ tbody tr:hover{background:var(--surface-2)}
|
|
|
303
300
|
|
|
304
301
|
/* 卡片与通用组件 */
|
|
305
302
|
.card{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:22px 24px;box-shadow:var(--shadow)}
|
|
303
|
+
.card>*:first-child{margin-top:0}
|
|
304
|
+
.card>*:last-child{margin-bottom:0}
|
|
306
305
|
.card-head{display:flex;align-items:center;justify-content:space-between;gap:12px;margin:0 0 14px;padding-bottom:11px;border-bottom:1px solid var(--border-soft)}
|
|
307
306
|
.card-head h2{margin:0;padding:0;border:0;font-size:16px;display:inline-flex;align-items:center;gap:8px}
|
|
308
307
|
.card.is-empty{padding:14px 22px;background:var(--surface-2);border-style:dashed;box-shadow:none;display:flex;align-items:center;justify-content:space-between;gap:12px}
|
|
@@ -320,7 +319,6 @@ tbody tr:hover{background:var(--surface-2)}
|
|
|
320
319
|
.dot.is-rose{background:#dc2626}
|
|
321
320
|
|
|
322
321
|
/* 每日页头部与日期切换 */
|
|
323
|
-
.day-hero{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:18px 22px 14px;box-shadow:var(--shadow)}
|
|
324
322
|
.day-hero-top{display:flex;align-items:center;justify-content:space-between;gap:16px;flex-wrap:wrap}
|
|
325
323
|
.day-eyebrow{font-size:12.5px;color:var(--muted);display:flex;align-items:center;gap:8px;margin-bottom:3px}
|
|
326
324
|
.day-title{display:flex;align-items:baseline;gap:10px;flex-wrap:wrap}
|
|
@@ -439,18 +437,50 @@ tbody tr:hover{background:var(--surface-2)}
|
|
|
439
437
|
.report-list{display:flex;flex-direction:column;gap:8px}
|
|
440
438
|
.report-item{display:flex;align-items:center;justify-content:space-between;padding:12px 16px;border-radius:10px;background:var(--surface-2);border:1px solid var(--border-soft);color:var(--ink);font-weight:540;transition:all .15s}
|
|
441
439
|
.report-item:hover{background:var(--surface-3);border-color:var(--border);text-decoration:none}
|
|
442
|
-
.mon-
|
|
443
|
-
.mon-bar label{display:inline-flex;align-items:center;gap:6px;cursor:pointer}
|
|
440
|
+
.mon-check{display:inline-flex;align-items:center;gap:6px;cursor:pointer;font-size:13px;color:var(--muted);white-space:nowrap}
|
|
444
441
|
.mon-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:14px}
|
|
445
|
-
.mon-
|
|
446
|
-
.mon-card h2{margin:0 0 10px;padding:0 0 8px;font-size:14.5px;border-bottom:1px solid var(--border-soft);display:flex;align-items:center;justify-content:space-between}
|
|
447
|
-
.mon-card p{font-size:13.5px;line-height:1.7}
|
|
442
|
+
.mon-grid p{font-size:13.5px;line-height:1.7}
|
|
448
443
|
.mon-kv{display:flex;flex-wrap:wrap;gap:6px;margin-top:4px}
|
|
449
444
|
.mon-log pre{max-height:360px;font-size:12px}
|
|
445
|
+
.mon-todos{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:10px}
|
|
446
|
+
.mon-todo{display:flex;gap:10px;align-items:flex-start;padding:12px 14px;border-radius:10px;background:var(--surface-2);border:1px solid var(--border-soft);font-size:13.5px;line-height:1.6}
|
|
447
|
+
.mon-todo .pill{flex-shrink:0;margin-top:2px}
|
|
448
|
+
.mon-todo-body{min-width:0;flex:1}
|
|
449
|
+
.todo-opts{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px}
|
|
450
|
+
.todo-opt{padding:7px 13px;border-radius:9px;font:inherit;font-size:13.5px;color:var(--ink);background:var(--surface);border:1px solid var(--border);cursor:pointer}
|
|
451
|
+
.todo-opt:hover:not(:disabled){border-color:var(--accent);color:var(--accent-ink);background:var(--accent-bg)}
|
|
452
|
+
.todo-opt.is-picked{border-color:var(--accent);background:var(--accent-bg);color:var(--accent-ink);font-weight:600}
|
|
453
|
+
.todo-opt:disabled{opacity:.55;cursor:default}
|
|
454
|
+
.mon-todo.is-done .todo-opt{opacity:.4}
|
|
455
|
+
.mon-todo.is-done .todo-opt.is-picked{opacity:1}
|
|
456
|
+
.todo-input{flex:1 1 200px;padding:7px 11px;border-radius:9px;font:inherit;font-size:13.5px;color:var(--ink);border:1px solid var(--border);background:var(--surface)}
|
|
457
|
+
.todo-after{margin-top:8px;font-size:13px;color:var(--green-ink,#137333)}
|
|
458
|
+
.ask-list{display:flex;flex-direction:column;gap:12px}
|
|
459
|
+
.ask-card{padding:14px 16px;border-radius:11px;background:var(--surface-2);border:1px solid var(--border);transition:border-color .15s}
|
|
460
|
+
.ask-card.is-done{opacity:.72;border-color:var(--green-bd)}
|
|
461
|
+
.ask-head{display:flex;align-items:flex-start;justify-content:space-between;gap:10px}
|
|
462
|
+
.ask-q{font-size:15px;font-weight:600;color:var(--ink);line-height:1.5}
|
|
463
|
+
.ask-ctx{margin-top:6px;font-size:13.5px;color:var(--ink-2);line-height:1.6}
|
|
464
|
+
.ask-meta{margin-top:6px;font-size:12px;color:var(--muted)}
|
|
465
|
+
.ask-opts{display:flex;flex-wrap:wrap;gap:8px;margin-top:10px}
|
|
466
|
+
.ask-opt{display:inline-flex;align-items:center;gap:6px;padding:7px 13px;border-radius:9px;font:inherit;font-size:13.5px;color:var(--ink);background:var(--surface);border:1px solid var(--border);cursor:pointer;text-align:left}
|
|
467
|
+
.ask-opt:hover:not(:disabled){border-color:var(--accent);color:var(--accent-ink);background:var(--accent-bg)}
|
|
468
|
+
.ask-opt.is-suggested{border-color:var(--amber-bd);background:var(--amber-bg)}
|
|
469
|
+
.ask-opt.is-picked{border-color:var(--accent);background:var(--accent-bg);color:var(--accent-ink);font-weight:600}
|
|
470
|
+
.ask-opt:disabled{opacity:.55;cursor:default}
|
|
471
|
+
.ask-opt-detail{font-size:12px;color:var(--muted);font-weight:400}
|
|
472
|
+
.ask-form{display:flex;gap:8px;margin-top:10px;flex-wrap:wrap}
|
|
473
|
+
.ask-after{margin-top:8px;font-size:13px;color:var(--green-ink)}
|
|
474
|
+
.ask-after.is-err{color:var(--rose-ink)}
|
|
475
|
+
.page-head,.day-hero{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:18px 22px;box-shadow:var(--shadow)}
|
|
476
|
+
.day-hero{padding-bottom:14px}
|
|
477
|
+
.page-head{display:flex;align-items:baseline;justify-content:space-between;gap:8px 16px;flex-wrap:wrap}
|
|
478
|
+
.page-head h1{margin:0;font-size:22px;letter-spacing:-.02em}
|
|
479
|
+
.page-head-meta{display:flex;align-items:baseline;gap:12px;flex-wrap:wrap;margin-left:auto}
|
|
480
|
+
.page-head .muted{font-size:13.5px}
|
|
450
481
|
|
|
451
482
|
@media (max-width:780px){
|
|
452
483
|
.topbar-inner,.wrap{padding-left:14px;padding-right:14px}
|
|
453
|
-
main.sheet{padding:22px 18px}
|
|
454
484
|
.card,.day-hero{padding:18px 16px}
|
|
455
485
|
.kpi-grid{grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}
|
|
456
486
|
.tl-row{grid-template-columns:48px 58px 1fr;gap:8px}
|
|
@@ -459,18 +489,17 @@ tbody tr:hover{background:var(--surface-2)}
|
|
|
459
489
|
}
|
|
460
490
|
@media print{
|
|
461
491
|
body{background:#fff;padding:0}
|
|
462
|
-
.topbar,.day-arrows,.day-strip,.kpi-grid,.filter-bar,.pending-hint-link{display:none}
|
|
492
|
+
.topbar,.day-arrows,.day-strip,.kpi-grid,.filter-bar,.pending-hint-link,.ask-form,.ask-opts{display:none}
|
|
463
493
|
.day-panel{display:block !important}
|
|
464
494
|
.wrap{margin:0;padding:0;max-width:none}
|
|
465
|
-
.card
|
|
495
|
+
.card,.day-hero,.page-head{box-shadow:none;border-color:#ddd}
|
|
466
496
|
}
|
|
467
497
|
`;
|
|
468
498
|
|
|
469
499
|
// 整页:样式内联,不引外部资源(docs/formats.md 的「视图」)
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const bodyClass = [wide.trim(), opts.bodyClass || ''].filter(Boolean).join(' ');
|
|
500
|
+
// 一张完整的页面:顶栏(nav 是站内导航的 HTML)+ main.stack 里放 body。
|
|
501
|
+
// 宽度只有 --max-w 一个,壳只有 stack 一种(页头卡片 + 正文卡片,见 view.js 的 renderDocPage)——换页签时版心不跳
|
|
502
|
+
function page(title, body, nav = '') {
|
|
474
503
|
const header = nav
|
|
475
504
|
? `<header class="topbar"><div class="topbar-inner"><a class="brand" href="/"><span class="brand-mark" aria-hidden="true"></span><span>work-rhythm</span></a><nav aria-label="主导航">${nav}</nav></div></header>`
|
|
476
505
|
: '';
|
|
@@ -483,10 +512,10 @@ function page(title, body, nav = '', opts = {}) {
|
|
|
483
512
|
<title>${escapeHtml(title)} · work-rhythm</title>
|
|
484
513
|
<style>${BASE_CSS}</style>
|
|
485
514
|
</head>
|
|
486
|
-
<body
|
|
515
|
+
<body>
|
|
487
516
|
${header}
|
|
488
517
|
<div class="wrap">
|
|
489
|
-
<main class="
|
|
518
|
+
<main class="stack">
|
|
490
519
|
${body}
|
|
491
520
|
</main>
|
|
492
521
|
</div>
|
package/scripts/lib/rhythm.js
CHANGED
|
@@ -12,18 +12,21 @@ const fs = require('node:fs');
|
|
|
12
12
|
const http = require('node:http');
|
|
13
13
|
const path = require('node:path');
|
|
14
14
|
const { EXIT, Fail, fmtDate, fmtStamp, pad, tail, readText, writeFileAtomic, seconds } = require('./run');
|
|
15
|
-
const {
|
|
15
|
+
const { createHash } = require('node:crypto');
|
|
16
|
+
const { loadProfile, loadSources } = require('./config');
|
|
16
17
|
const { logger } = require('./log');
|
|
17
|
-
const { createAgent, roles } = require('./agent');
|
|
18
|
+
const { createAgent, roles, routerProblems } = require('./agent');
|
|
18
19
|
const { createBot } = require('./bot');
|
|
19
20
|
const { notifyChannel, pushText } = require('./notify');
|
|
20
|
-
const { createEngine, appendHistory, pruneState, historyStats, uid } = require('./engine');
|
|
21
|
+
const { createEngine, appendHistory, pruneState, historyStats, uid, pendingCards } = require('./engine');
|
|
21
22
|
const handoff = require('./handoff');
|
|
22
23
|
const md = require('./markdown');
|
|
23
24
|
const view = require('./view');
|
|
24
25
|
const launchd = require('./launchd');
|
|
25
26
|
const sync = require('../cmd/sync');
|
|
26
27
|
const today = require('../cmd/today');
|
|
28
|
+
const decisions = require('../cmd/decisions');
|
|
29
|
+
const asks = require('./asks');
|
|
27
30
|
|
|
28
31
|
const TICK_MS = 20000;
|
|
29
32
|
const CATCH_UP_MS = 5 * 60 * 1000;
|
|
@@ -38,11 +41,13 @@ const LINKS = {
|
|
|
38
41
|
'evening-review': { path: (d) => `/day/${d}`, label: '今天的文件' },
|
|
39
42
|
'weekly-rhythm': { path: () => '/rhythm', label: '工作规律' },
|
|
40
43
|
'alerts-morning': { path: (d) => `/reports/${d}-alerts.html`, label: '告警晨报' },
|
|
44
|
+
// 一批新事件之后的「今日重点与待办」:地址给「决定」页(待办卡片在那里点),手机打不开,要在自己电脑上开
|
|
45
|
+
'todos': { path: () => '/#pending-decisions', label: '今日页「待你定」(在自己电脑的浏览器里打开)' },
|
|
41
46
|
};
|
|
42
47
|
const NAV = [['/', '今日'], ['/carryover', '遗留'], ['/decisions', '决定'], ['/rhythm', '规律'], ['/goals', '目标'], ['/reports', '报告'], ['/monitor', '监控']];
|
|
43
48
|
const NEW_KEYS = [['workers', 'rhythm.workers'], ['router-timeout', 'rhythm.router-timeout'], ['handoff-keep', 'rhythm.handoff-keep'], ['history-keep', 'rhythm.history-keep']];
|
|
44
49
|
// 状态文件的骨架:done(跑过的任务)、runs(各任务最近一次)、seq(触发短号的序号)、triggers(近两天的触发)、history(运行记录)、rounds(路由的轮数)
|
|
45
|
-
const EMPTY_STATE = () => ({ done: {}, runs: {}, seq: 0, triggers: {}, history: [], rounds: [] });
|
|
50
|
+
const EMPTY_STATE = () => ({ done: {}, runs: {}, seq: 0, triggers: {}, history: [], rounds: [], sources: {}, refinedAt: '', syncDigest: { date: '', digest: '' } });
|
|
46
51
|
|
|
47
52
|
const positiveInt = (value, fallback) => {
|
|
48
53
|
const n = Number(value);
|
|
@@ -77,7 +82,10 @@ function loadConfig(root) {
|
|
|
77
82
|
// 路由和执行者(docs/router-worker.md)。router 没写就是直通(老做法)
|
|
78
83
|
router: rhythm.router === true,
|
|
79
84
|
routerTimeout: positiveInt(rhythm['router-timeout'], 120),
|
|
80
|
-
workers: positiveInt(rhythm.workers,
|
|
85
|
+
workers: positiveInt(rhythm.workers, 3),
|
|
86
|
+
// 定时采集之后:有新数据就起一轮提炼(把 01-inbox 的新数据炼成事件、新增的打包给路由)。两轮提炼之间至少隔 refine-interval 分钟
|
|
87
|
+
refine: rhythm.refine !== false,
|
|
88
|
+
refineInterval: positiveInt(rhythm['refine-interval'], 120),
|
|
81
89
|
handoffKeep: positiveInt(rhythm['handoff-keep'], 14),
|
|
82
90
|
historyKeep: positiveInt(rhythm['history-keep'], 90),
|
|
83
91
|
raw: rhythm,
|
|
@@ -264,15 +272,79 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
264
272
|
|
|
265
273
|
// ---------------------------------------------------------------- 采集、任务、推送
|
|
266
274
|
// 跑一遍 sync(在同一个进程里),最多等 cfg.syncTimeout 秒
|
|
275
|
+
const todayStr = () => fmtDate(new Date());
|
|
276
|
+
|
|
277
|
+
// 今天 01-inbox/<源>/<日期>/ 下所有文件的名字和内容搓成的指纹:采集之后跟上次比,不一样就是有新数据(采集器每次都重写文件,所以不按修改时间算)
|
|
278
|
+
function inboxDigest(date) {
|
|
279
|
+
const base = path.join(root, '01-inbox');
|
|
280
|
+
const h = createHash('sha1');
|
|
281
|
+
let names;
|
|
282
|
+
try {
|
|
283
|
+
names = fs.readdirSync(base).sort();
|
|
284
|
+
} catch {
|
|
285
|
+
return '';
|
|
286
|
+
}
|
|
287
|
+
for (const id of names) {
|
|
288
|
+
const dir = path.join(base, id, date);
|
|
289
|
+
let files;
|
|
290
|
+
try {
|
|
291
|
+
if (!fs.statSync(dir).isDirectory()) continue;
|
|
292
|
+
files = fs.readdirSync(dir).sort();
|
|
293
|
+
} catch {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
for (const name of files) {
|
|
297
|
+
const file = path.join(dir, name);
|
|
298
|
+
if (!fs.statSync(file).isFile()) continue;
|
|
299
|
+
h.update(`${id}/${name}\n`);
|
|
300
|
+
h.update(fs.readFileSync(file));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return h.digest('hex');
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 每个源的运行状态记进 state.json:最近一次成败、用时、原因,连着失败几次,上次成功是什么时候
|
|
307
|
+
function recordSources(results) {
|
|
308
|
+
for (const r of results) {
|
|
309
|
+
const prev = store.data.sources[r.id] || {};
|
|
310
|
+
store.data.sources[r.id] = { at: fmtStamp(), ok: Boolean(r.ok), code: r.code || 0, seconds: r.seconds || 0, detail: tail(r.detail || '', 200), fails: r.ok ? 0 : (prev.fails || 0) + 1, lastOk: r.ok ? fmtStamp() : prev.lastOk || '' };
|
|
311
|
+
}
|
|
312
|
+
if (results.length) saveState(store.data);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 启用的源 + 它们的运行状态(监控中心、doctor 用)。缺脚本的也标出来
|
|
316
|
+
function sourceStatus() {
|
|
317
|
+
return loadSources(root).map((src) => {
|
|
318
|
+
const collector = src.collector === 'none' ? '' : src.collector;
|
|
319
|
+
const missing = Boolean(src.enabled && collector && !sync.resolveCollector(root, packageRoot, collector));
|
|
320
|
+
return { id: src.id, name: src.name || '', signal: src.signal || '', enabled: src.enabled, collector: src.collector || '', missing, last: store.data.sources[src.id] || null };
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
267
324
|
async function runSync() {
|
|
268
325
|
const t0 = Date.now();
|
|
269
326
|
const chunks = [];
|
|
270
327
|
const capture = { write: (s) => (chunks.push(String(s)), true) };
|
|
271
|
-
const
|
|
328
|
+
const results = [];
|
|
329
|
+
const fail = await sync.run([], { root, cwd: root, env, stdout: capture, stderr: capture, packageRoot }, { deadlineMs: t0 + cfg.syncTimeout * 1000, results });
|
|
272
330
|
const seconds = Math.round((Date.now() - t0) / 1000);
|
|
331
|
+
recordSources(results);
|
|
332
|
+
// 有没有新数据:今天的指纹跟上一次比。没有文件也算一种指纹(空串),和上次不一样才是有新数据
|
|
333
|
+
const digest = inboxDigest(todayStr());
|
|
334
|
+
const prev = store.data.syncDigest || {};
|
|
335
|
+
const changed = Boolean(digest) && digest !== prev.digest;
|
|
336
|
+
store.data.syncDigest = { date: todayStr(), digest, at: fmtStamp() };
|
|
337
|
+
saveState(store.data);
|
|
338
|
+
// 留给服务的那个进程:它起提炼、把新事件打包给路由
|
|
339
|
+
try {
|
|
340
|
+
fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
341
|
+
fs.writeFileSync(path.join(STATE_DIR, 'last-sync.json'), `${JSON.stringify({ at: fmtStamp(), ok: !fail, seconds, results, changed }, null, 1)}\n`);
|
|
342
|
+
} catch {
|
|
343
|
+
// 写不下就算了:服务那边没有它就不提炼
|
|
344
|
+
}
|
|
273
345
|
const out = chunks.join('');
|
|
274
|
-
if (fail) return { ok: false, failed: `退出码 ${fail}:${tail(out, 300)}`, seconds };
|
|
275
|
-
return { ok: true, seconds, detail: tail(chunks.join(''), 300).replace(/\n/g, ' · ') };
|
|
346
|
+
if (fail) return { ok: false, failed: `退出码 ${fail}:${tail(out, 300)}`, seconds, results, changed };
|
|
347
|
+
return { ok: true, seconds, detail: tail(chunks.join(''), 300).replace(/\n/g, ' · '), results, changed };
|
|
276
348
|
}
|
|
277
349
|
|
|
278
350
|
// 经消息渠道发一段文字。渠道留空时什么都不发(不算失败);发不出去记日志
|
|
@@ -361,7 +433,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
361
433
|
res.end(body);
|
|
362
434
|
}
|
|
363
435
|
|
|
364
|
-
const notFound = (res, what) => send(res, 404, md.page('没有这个页面',
|
|
436
|
+
const notFound = (res, what) => send(res, 404, md.page('没有这个页面', view.renderDocPage('没有这个页面', '', { empty: what || '这个地址没有对应的页面。' }), navHtml()));
|
|
365
437
|
|
|
366
438
|
function listFiles(dir, filter) {
|
|
367
439
|
try {
|
|
@@ -392,16 +464,18 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
392
464
|
return Number.isNaN(d.getTime()) ? '' : WEEKDAY_CN[d.getDay()];
|
|
393
465
|
}
|
|
394
466
|
|
|
395
|
-
// 读到就渲染,读不到就回 404
|
|
467
|
+
// 读到就渲染,读不到就回 404(所有业务页统一走 plain + stack + card 骨架)
|
|
396
468
|
function showMarkdown(res, rel, title, active = '') {
|
|
397
469
|
const text = readText(path.join(root, rel));
|
|
398
470
|
if (!text) notFound(res, `${rel} 还没有`);
|
|
399
|
-
else send(res, 200, md.page(title,
|
|
471
|
+
else send(res, 200, md.page(title, view.renderDocPage(title, text, { subtitle: rel }), navHtml(active)));
|
|
400
472
|
}
|
|
401
473
|
|
|
402
474
|
function route(req, res) {
|
|
475
|
+
// 唯一的写接口:本机今日页「待你定」卡片的回答
|
|
476
|
+
if (req.method === 'POST' && /^\/api\/asks\/answer$/.test(String(req.url || '').split('?')[0])) return answerOnPage(req, res);
|
|
403
477
|
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
404
|
-
return send(res, 405, md.page('只支持打开页面', '
|
|
478
|
+
return send(res, 405, md.page('只支持打开页面', view.renderDocPage('只支持打开页面', '', { empty: '这个服务只看记录;唯一能写的是今日页「待你定」卡片的回答。' }), navHtml()));
|
|
405
479
|
}
|
|
406
480
|
let pathname;
|
|
407
481
|
try {
|
|
@@ -413,8 +487,17 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
413
487
|
if (pathname === '/' || pathname === `/day/${now}`) {
|
|
414
488
|
const dates = dailyDates();
|
|
415
489
|
const text = readText(path.join(root, '02-daily', now.slice(0, 7), `${now}.md`));
|
|
416
|
-
const
|
|
417
|
-
|
|
490
|
+
const cards = asks.cardsForPage(root, pendingCards);
|
|
491
|
+
const body = view.renderDaily({ date: now, weekday: WEEKDAY_CN[new Date().getDay()], isToday: true, markdown: text, dates, asks: cards, token: webToken() });
|
|
492
|
+
return send(res, 200, md.page(`今日 · ${now}`, body, navHtml('/')));
|
|
493
|
+
}
|
|
494
|
+
if (pathname === '/asks.html') {
|
|
495
|
+
const cards = asks.cardsForPage(root, pendingCards);
|
|
496
|
+
return send(res, 200, view.renderAsksFragment(cards), 'text/html; charset=utf-8');
|
|
497
|
+
}
|
|
498
|
+
if (pathname === '/asks.json') {
|
|
499
|
+
const cards = asks.cardsForPage(root, pendingCards);
|
|
500
|
+
return send(res, 200, `${JSON.stringify(cards, null, 1)}\n`, 'application/json; charset=utf-8');
|
|
418
501
|
}
|
|
419
502
|
let m = /^\/day\/(\d{4}-\d{2}-\d{2})$/.exec(pathname);
|
|
420
503
|
if (m) {
|
|
@@ -422,7 +505,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
422
505
|
const text = readText(path.join(root, '02-daily', date.slice(0, 7), `${date}.md`));
|
|
423
506
|
if (!text) return notFound(res, `02-daily/${date.slice(0, 7)}/${date}.md 还没有`);
|
|
424
507
|
const body = view.renderDaily({ date, weekday: weekdayOf(date), isToday: false, markdown: text, dates: dailyDates() });
|
|
425
|
-
return send(res, 200, md.page(date, body, navHtml('/')
|
|
508
|
+
return send(res, 200, md.page(date, body, navHtml('/')));
|
|
426
509
|
}
|
|
427
510
|
if (pathname === '/carryover') return showMarkdown(res, '03-carryover/CARRYOVER.md', '遗留待处理', '/carryover');
|
|
428
511
|
if (pathname === '/decisions') return showMarkdown(res, '03-carryover/DECISIONS.md', '待决与决定', '/decisions');
|
|
@@ -431,11 +514,11 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
431
514
|
const files = listFiles(path.join(root, '05-goals'), (n) => /^[\w-]+\.md$/.test(n));
|
|
432
515
|
if (!files.length) return notFound(res, '还没有进行中的目标');
|
|
433
516
|
const items = files.map((name) => ({ name, text: readText(path.join(root, '05-goals', name)) }));
|
|
434
|
-
return send(res, 200, md.page('长期目标', view.renderGoals(items), navHtml('/goals')
|
|
517
|
+
return send(res, 200, md.page('长期目标', view.renderGoals(items), navHtml('/goals')));
|
|
435
518
|
}
|
|
436
519
|
if (pathname === '/reports') {
|
|
437
520
|
const files = listFiles(path.join(root, '01-inbox', 'reports'), (n) => /^[\w.-]+\.html$/.test(n));
|
|
438
|
-
return send(res, 200, md.page('报告', view.renderReports(files), navHtml('/reports')
|
|
521
|
+
return send(res, 200, md.page('报告', view.renderReports(files), navHtml('/reports')));
|
|
439
522
|
}
|
|
440
523
|
m = /^\/reports\/([\w.-]+\.html)$/.exec(pathname);
|
|
441
524
|
if (m) {
|
|
@@ -450,7 +533,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
450
533
|
if (m) {
|
|
451
534
|
const file = path.join(handoff.dir(root), m[1]);
|
|
452
535
|
if (!handoff.NAME_RE.test(m[1]) || !fs.existsSync(file)) return notFound(res, pathname);
|
|
453
|
-
return send(res, 200, md.page(m[1],
|
|
536
|
+
return send(res, 200, md.page(m[1], view.renderDocPage(m[1].endsWith('.result.md') ? '回执' : '任务书', readText(file), { subtitle: `${handoff.DIR}/${m[1]}`, meta: '<a class="day-btn" href="/monitor">← 监控中心</a>' }), navHtml('/monitor')));
|
|
454
537
|
}
|
|
455
538
|
return notFound(res, pathname);
|
|
456
539
|
}
|
|
@@ -470,13 +553,76 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
470
553
|
// 单个请求出错不能把服务带崩
|
|
471
554
|
log('页面出错', `${req.url} ${error.message}`);
|
|
472
555
|
try {
|
|
473
|
-
send(res, 500, md.page('页面出错了',
|
|
556
|
+
send(res, 500, md.page('页面出错了', view.renderDocPage('页面出错了', '', { empty: String(error.message) }), navHtml()));
|
|
474
557
|
} catch {
|
|
475
558
|
// 连响应都发不出去就算了
|
|
476
559
|
}
|
|
477
560
|
}
|
|
478
561
|
}
|
|
479
562
|
|
|
563
|
+
// ---------------------------------------------------------------- 「决定」页上的待办卡片:能点的
|
|
564
|
+
// 网页服务开一个写接口(点选项 = 我的决定),靠两道闸防别的网页乱点:Host 头必须是本机(防 DNS 重绑定,上面),
|
|
565
|
+
// 外加一个只在本机页面里的令牌:随机串存在 01-inbox/_rhythm/web-token,页面脚本带着它,接口只认带对的。
|
|
566
|
+
// 别的网站的页面读不到 127.0.0.1 上的这个页面(同源策略),拿不到令牌,就用图片、表单点不动
|
|
567
|
+
function webToken() {
|
|
568
|
+
const file = path.join(STATE_DIR, 'web-token');
|
|
569
|
+
try {
|
|
570
|
+
const found = readText(file).trim();
|
|
571
|
+
if (/^[0-9a-f]{32}$/.test(found)) return found;
|
|
572
|
+
} catch {
|
|
573
|
+
// 还没有,下面建一个
|
|
574
|
+
}
|
|
575
|
+
fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
576
|
+
const token = createHash('sha256').update(`${Date.now()}:${Math.random()}:${process.pid}`).digest('hex').slice(0, 32);
|
|
577
|
+
fs.writeFileSync(file, `${token}\n`, { mode: 0o600 });
|
|
578
|
+
return token;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// 今日页「待你定」卡片的回答:记下答案、更新 DECISIONS.md,并作为一条 answer 触发交给路由
|
|
582
|
+
async function answerOnPage(req, res) {
|
|
583
|
+
if (String(req.headers['x-wrj-token'] || '') !== webToken()) return send(res, 403, JSON.stringify({ error: '令牌不对:这个接口只给本机页面用' }), 'application/json; charset=utf-8');
|
|
584
|
+
const body = await new Promise((resolve) => {
|
|
585
|
+
let data = '';
|
|
586
|
+
req.on('data', (c) => {
|
|
587
|
+
data += c;
|
|
588
|
+
if (data.length > 64 * 1024) req.destroy();
|
|
589
|
+
});
|
|
590
|
+
req.on('end', () => resolve(data));
|
|
591
|
+
req.on('error', () => resolve(''));
|
|
592
|
+
});
|
|
593
|
+
let parsed;
|
|
594
|
+
try {
|
|
595
|
+
parsed = JSON.parse(body || '{}');
|
|
596
|
+
} catch {
|
|
597
|
+
return send(res, 400, JSON.stringify({ error: '请求体不是 JSON' }), 'application/json; charset=utf-8');
|
|
598
|
+
}
|
|
599
|
+
const id = String(parsed.id || '').trim();
|
|
600
|
+
const item = String(parsed.item || '').trim();
|
|
601
|
+
const choice = String(parsed.choice || '').trim();
|
|
602
|
+
const note = String(parsed.note || '').trim();
|
|
603
|
+
if (!choice || (!id && !item)) return send(res, 400, JSON.stringify({ error: '要写回答和卡片标识' }), 'application/json; charset=utf-8');
|
|
604
|
+
let ask;
|
|
605
|
+
if (id) {
|
|
606
|
+
try {
|
|
607
|
+
ask = await asks.answerAsk(root, id, { choice, note, by: 'page' }, { env, packageRoot, decisions });
|
|
608
|
+
} catch (e) {
|
|
609
|
+
return send(res, 409, JSON.stringify({ error: e.message || String(e) }), 'application/json; charset=utf-8');
|
|
610
|
+
}
|
|
611
|
+
} else {
|
|
612
|
+
const err = [];
|
|
613
|
+
const code = await decisions.run(['answer', item, choice], { root, cwd: root, env, packageRoot, stdout: { write() {} }, stderr: { write: (x) => err.push(String(x)) } });
|
|
614
|
+
if (code !== 0) return send(res, 409, JSON.stringify({ error: tail(err.join(''), 300) || `decisions answer 退出码 ${code}` }), 'application/json; charset=utf-8');
|
|
615
|
+
ask = { id: '', item, question: item, context: '', options: [], record: '03-carryover/DECISIONS.md', from: {}, answer: { choice, note, at: fmtStamp(), by: 'page' } };
|
|
616
|
+
}
|
|
617
|
+
let dispatched = false;
|
|
618
|
+
const eng = engine || makeEngine();
|
|
619
|
+
if (eng) {
|
|
620
|
+
eng.answer(ask);
|
|
621
|
+
dispatched = true;
|
|
622
|
+
}
|
|
623
|
+
return send(res, 200, JSON.stringify({ ok: true, id: ask.id, item: ask.item, choice, dispatched, message: dispatched ? '已记下,已交给路由继续处理。' : '已记下(路由未开启,仅记录)。' }), 'application/json; charset=utf-8');
|
|
624
|
+
}
|
|
625
|
+
|
|
480
626
|
// 只绑 127.0.0.1:页面带着工作记录,不给别的机器看
|
|
481
627
|
const listen = (port) => new Promise((resolve, reject) => {
|
|
482
628
|
const server = http.createServer(handleRequest);
|
|
@@ -488,7 +634,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
488
634
|
function render(file) {
|
|
489
635
|
const full = path.resolve(root, file);
|
|
490
636
|
if (!fs.existsSync(full)) throw new Fail(EXIT.USAGE, `没有这个文件:${file}`);
|
|
491
|
-
return md.page(path.basename(full),
|
|
637
|
+
return md.page(path.basename(full), view.renderDocPage(path.basename(full), fs.readFileSync(full, 'utf8')), navHtml());
|
|
492
638
|
}
|
|
493
639
|
|
|
494
640
|
// ---------------------------------------------------------------- 监控中心
|
|
@@ -515,7 +661,9 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
515
661
|
agents: { router: { agent: r.router.agent, model: r.router.model, effort: r.router.effort }, worker: workerSpec() },
|
|
516
662
|
tasks: jobs().filter((j) => j.kind === 'task').map((j) => ({ id: j.id, spec: cfg.schedule[j.id], hasPrompt: j.hasPrompt, problem: j.spec && j.spec.problem ? j.spec.problem : '', next: j.spec && j.spec.problem ? '' : nextRun(j.spec), ...historyStats(store.data, j.id) })),
|
|
517
663
|
triggers: Object.values(store.data.triggers || {}).sort((a, b) => b.at.localeCompare(a.at) || b.id.localeCompare(a.id)).slice(0, 100),
|
|
518
|
-
|
|
664
|
+
sources: sourceStatus(),
|
|
665
|
+
refinedAt: store.data.refinedAt || '',
|
|
666
|
+
config: { router: cfg.router, routerTimeout: cfg.routerTimeout, workers: cfg.workers, refine: cfg.refine, refineInterval: cfg.refineInterval, taskTimeout: cfg.taskTimeout, syncTimeout: cfg.syncTimeout, agentMode: cfg.agentMode, handoffKeep: cfg.handoffKeep, historyKeep: cfg.historyKeep, push: cfg.push, quiet: cfg.quiet, notifyChannel: cfg.notifyChannel },
|
|
519
667
|
};
|
|
520
668
|
}
|
|
521
669
|
|
|
@@ -535,6 +683,36 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
535
683
|
return '';
|
|
536
684
|
}
|
|
537
685
|
|
|
686
|
+
// 监控中心的「订阅源」一块:每个源的状态、最近一次、连着失败几次
|
|
687
|
+
function sourcesCard(d) {
|
|
688
|
+
const enabled = d.sources.filter((x) => x.enabled).length;
|
|
689
|
+
const rows = d.sources.map((x) => {
|
|
690
|
+
const l = x.last;
|
|
691
|
+
let state;
|
|
692
|
+
if (!x.enabled) state = '<span class="pill">关着</span>';
|
|
693
|
+
else if (x.missing) state = '<span class="pill is-rose">采集脚本不存在</span>';
|
|
694
|
+
else if (!l) state = '<span class="pill">还没跑过</span>';
|
|
695
|
+
else if (l.ok) state = '<span class="pill is-green"><span class="dot is-green"></span>正常</span>';
|
|
696
|
+
else state = `<span class="pill is-rose">失败</span>${l.detail ? `<br><span class="muted">${esc(l.detail)}</span>` : ''}`;
|
|
697
|
+
return [
|
|
698
|
+
`<code>${esc(x.id)}</code>${x.collector ? `<br><span class="muted">${esc(x.collector)}</span>` : ''}`,
|
|
699
|
+
esc(x.signal || x.name || ''),
|
|
700
|
+
state,
|
|
701
|
+
l ? esc(l.at.slice(5, 16)) : '<span class="muted">—</span>',
|
|
702
|
+
l ? `${l.seconds} 秒` : '<span class="muted">—</span>',
|
|
703
|
+
l && l.fails ? `<span class="pill is-rose">${l.fails} 次</span>` : '<span class="muted">0</span>',
|
|
704
|
+
l && l.lastOk ? esc(l.lastOk.slice(5, 16)) : '<span class="muted">—</span>',
|
|
705
|
+
];
|
|
706
|
+
});
|
|
707
|
+
const refine = d.config.router ? ` · 有新数据就提炼${d.config.refine ? '' : '(关着)'},间隔 ${d.config.refineInterval} 分钟` : '';
|
|
708
|
+
const last = d.refinedAt ? ` · 上次提炼 ${esc(d.refinedAt.slice(5, 16))}` : '';
|
|
709
|
+
return `<section class="card">
|
|
710
|
+
<div class="card-head"><h2>订阅源 <span class="muted">采集 ${esc(d.service.sync || '不定时')}${refine}${last}</span></h2><span class="pill">${enabled} 个启用</span></div>
|
|
711
|
+
${table(['源', '反映什么', '状态', '最近一次', '用时', '连续失败', '上次成功'], rows)}
|
|
712
|
+
</section>`;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// 监控中心的「待办卡片」一块:等我拿主意的事(DECISIONS.md 里「未答」的行)
|
|
538
716
|
function monitorPage() {
|
|
539
717
|
const d = monitorData();
|
|
540
718
|
const statusPill = (t) => {
|
|
@@ -565,34 +743,28 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
565
743
|
const svcBadge = d.service.running ? `<span class="pill is-green"><span class="dot is-green"></span>在跑 · pid ${d.service.pid}</span>` : '<span class="pill">没在跑</span>';
|
|
566
744
|
const routerBadge = rs.enabled ? (rs.busy ? '<span class="pill is-accent"><span class="dot is-accent"></span>正在想</span>' : '<span class="pill is-green">开着</span>') : '<span class="pill">关着</span>';
|
|
567
745
|
const workerBadge = d.workers.running.length ? `<span class="pill is-accent">在跑 ${d.workers.running.length} / ${d.workers.limit}</span>` : `<span class="pill">空闲 0 / ${d.workers.limit}</span>`;
|
|
568
|
-
const body =
|
|
569
|
-
<div class="mon-bar">
|
|
570
|
-
<span>更新于 <code>${esc(d.at)}</code></span>
|
|
571
|
-
<div style="display:flex;align-items:center;gap:14px;flex-wrap:wrap">
|
|
572
|
-
<label><input type="checkbox" id="refresh" checked> 每 10 秒自动刷新(前台时)</label>
|
|
573
|
-
<a class="day-btn" href="/status.json">status.json</a>
|
|
574
|
-
</div>
|
|
575
|
-
</div>
|
|
746
|
+
const body = `${view.pageHead('监控中心', { subtitle: `更新于 ${d.at}`, meta: `<label class="mon-check"><input type="checkbox" id="refresh" checked> 每 10 秒自动刷新(前台时)</label><a class="day-btn" href="/status.json">status.json</a>` })}
|
|
576
747
|
<script>(function(){var b=document.getElementById('refresh');setInterval(function(){if(b.checked&&document.visibilityState==='visible')location.reload()},10000)})();</script>
|
|
577
748
|
<div class="mon-grid">
|
|
578
|
-
<section class="
|
|
579
|
-
<h2>服务</h2>
|
|
749
|
+
<section class="card">
|
|
750
|
+
<div class="card-head"><h2>服务</h2></div>
|
|
580
751
|
<p>${svcBadge}${d.service.since ? `<br><span class="muted">起于 ${esc(d.service.since)} · 已 ${esc(fmtDur(d.service.uptimeS))}</span>` : ''}</p>
|
|
581
752
|
<p>值守 ${d.service.listen ? '开着' : '关着'} · 采集 ${esc(d.service.sync || '不定时')}<br>
|
|
582
753
|
推送 ${d.service.push ? `开着(${esc(d.service.notifyChannel || '没配渠道')})` : '关着'} · 端口 <code>${d.service.port}</code></p>
|
|
583
754
|
</section>
|
|
584
|
-
<section class="
|
|
585
|
-
<h2>路由</h2>
|
|
755
|
+
<section class="card">
|
|
756
|
+
<div class="card-head"><h2>路由</h2></div>
|
|
586
757
|
<p>${routerBadge}${rs.enabled ? `<br>${session}<br>排队 ${rs.queue} 条 · 今天 ${rs.roundsToday.total} 轮${rs.roundsToday.failed ? `(失败 ${rs.roundsToday.failed})` : ''}${rs.last ? `<br><span class="muted">最近一轮 ${esc(rs.last.at.slice(11))} · ${rs.last.seconds} 秒${rs.last.ok ? '' : ',没出决定'}</span>` : ''}` : '<br><span class="muted">profile 的 rhythm.router 没写:到点直接跑任务,IM 里的话直接答</span>'}</p>
|
|
587
758
|
<p class="muted">agent <code>${esc(d.agents.router.agent || '没绑')}</code>${d.agents.router.model ? ` · ${esc(d.agents.router.model)}` : ''}${d.agents.router.effort ? ` · ${esc(d.agents.router.effort)}` : ''} · plan</p>
|
|
588
759
|
</section>
|
|
589
|
-
<section class="
|
|
590
|
-
<h2>执行者</h2>
|
|
760
|
+
<section class="card">
|
|
761
|
+
<div class="card-head"><h2>执行者</h2></div>
|
|
591
762
|
<p>${workerBadge}${d.workers.queued.length ? `<br>排队 ${d.workers.queued.map(short).join(' ')}` : ''}</p>
|
|
592
763
|
${running ? `<ul style="margin:6px 0 8px;padding-left:18px;font-size:13px">${running}</ul>` : ''}
|
|
593
764
|
<p class="muted">agent <code>${esc(d.agents.worker.agent || '没绑')}</code>${d.agents.worker.model ? ` · ${esc(d.agents.worker.model)}` : ''}${d.agents.worker.effort ? ` · ${esc(d.agents.worker.effort)}` : ''}</p>
|
|
594
765
|
</section>
|
|
595
766
|
</div>
|
|
767
|
+
${sourcesCard(d)}
|
|
596
768
|
<section class="card">
|
|
597
769
|
<div class="card-head"><h2>定时任务 <span class="muted">近 ${d.config.historyKeep} 天</span></h2><span class="pill">${d.tasks.length} 个</span></div>
|
|
598
770
|
${table(['任务', '时间', '下一次', '今天', '成功 / 失败', '最近一次', '平均用时'], d.tasks.map((t) => [
|
|
@@ -623,6 +795,8 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
623
795
|
<span class="pill">router: ${d.config.router}</span>
|
|
624
796
|
<span class="pill">router-timeout: ${d.config.routerTimeout}s</span>
|
|
625
797
|
<span class="pill">workers: ${d.config.workers}</span>
|
|
798
|
+
<span class="pill">refine: ${d.config.refine}</span>
|
|
799
|
+
<span class="pill">refine-interval: ${d.config.refineInterval}m</span>
|
|
626
800
|
<span class="pill">task-timeout: ${d.config.taskTimeout}s</span>
|
|
627
801
|
<span class="pill">agent-mode: ${esc(d.config.agentMode)}</span>
|
|
628
802
|
<span class="pill">handoff-keep: ${d.config.handoffKeep}d</span>
|
|
@@ -638,7 +812,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
638
812
|
<h3>01-inbox/_bot.log 末 30 行</h3>
|
|
639
813
|
<pre><code>${esc(readText(path.join(root, '01-inbox', '_bot.log')).trim().split('\n').slice(-30).join('\n') || '(暂无)')}</code></pre>
|
|
640
814
|
</section>`;
|
|
641
|
-
return md.page('监控中心', body, navHtml('/monitor')
|
|
815
|
+
return md.page('监控中心', body, navHtml('/monitor'));
|
|
642
816
|
}
|
|
643
817
|
const triggersById = (data) => Object.fromEntries(Object.values(data.triggers || {}).map((t) => [t.id, t]));
|
|
644
818
|
|
|
@@ -674,13 +848,16 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
674
848
|
}
|
|
675
849
|
// 路由开着时它的 agent 也要配得对(值守开着时 bot 那边查的是同一个)
|
|
676
850
|
if (cfg.router) problems.push(...routerDoctorProblems());
|
|
851
|
+
// 启用着的源,采集脚本得在
|
|
852
|
+
for (const x of sourceStatus()) if (x.missing) problems.push(`订阅源 ${x.id} 的采集脚本不存在:${x.collector}(sources.yaml 的 collector 写错了,或者脚本没放进 collectors/)`);
|
|
677
853
|
return problems;
|
|
678
854
|
}
|
|
679
855
|
|
|
680
856
|
// 路由用的 agent 有没有配对
|
|
857
|
+
// 路由的 agent 找得到、模型和强度它认,再加上路由的硬规矩:模型必须是 flash 这类快模型、强度固定 low
|
|
681
858
|
const routerDoctorProblems = () => {
|
|
682
859
|
const r = roles(env).router;
|
|
683
|
-
return createAgent({ root, agent: r.agent, mode: 'plan', env, model: r.model, effort: r.effort }).doctor();
|
|
860
|
+
return [...createAgent({ root, agent: r.agent, mode: 'plan', env, model: r.model, effort: r.effort }).doctor(), ...routerProblems({ agent: r.agent, model: r.model, effort: r.effort, env })];
|
|
684
861
|
};
|
|
685
862
|
|
|
686
863
|
// 命令行里走一遍全流程(work-rhythm listen handle,路由开着时):在命令行的进程里建引擎,这句话变成一条触发,
|
|
@@ -700,8 +877,9 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
700
877
|
function doctorNotes({ mentionOff = true } = {}) {
|
|
701
878
|
if (!cfg.router) return mentionOff ? ['路由没开(profile 的 rhythm.router 没写):到点直接跑任务,IM 里的话直接答;要走「触发 → 路由 → 执行者」写上 rhythm.router: true(docs/router-worker.md)'] : [];
|
|
702
879
|
const r = roles(env).router;
|
|
703
|
-
const notes = createAgent({ root, agent: r.agent, mode: 'plan', env, model: r.model, effort: r.effort }).notes();
|
|
880
|
+
const notes = cfg.router ? createAgent({ root, agent: r.agent, mode: 'plan', env, model: r.model, effort: r.effort }).notes() : [];
|
|
704
881
|
if (cfg.router && !cfg.notifyChannel && cfg.push) notes.push('推送关不掉又没配渠道:路由写给用户的话发不出去,只记日志');
|
|
882
|
+
for (const x of sourceStatus()) if (x.enabled && x.last && x.last.fails >= 3) notes.push(`订阅源 ${x.id} 已经连续失败 ${x.last.fails} 次(最近 ${x.last.at}):看 01-inbox/_sync.log,或者 work-rhythm sync ${x.id}`);
|
|
705
883
|
return notes;
|
|
706
884
|
}
|
|
707
885
|
|
|
@@ -760,6 +938,19 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
760
938
|
log('跑完', `${job.id} rc=${code}`);
|
|
761
939
|
running = null;
|
|
762
940
|
startNext();
|
|
941
|
+
// 采集是有新数据才提炼:子进程把每个源的结果和「有没有新数据」留在 last-sync.json,这里交给引擎
|
|
942
|
+
if (job.kind === 'sync' && eng) {
|
|
943
|
+
try {
|
|
944
|
+
const file = path.join(STATE_DIR, 'last-sync.json');
|
|
945
|
+
const st = fs.statSync(file);
|
|
946
|
+
if (Date.now() - st.mtimeMs < 10 * 60 * 1000) {
|
|
947
|
+
const last = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
948
|
+
eng.afterSync({ results: last.results || [], changed: Boolean(last.changed) });
|
|
949
|
+
}
|
|
950
|
+
} catch (error) {
|
|
951
|
+
log('采集结果读不了', error && error.message ? error.message : String(error));
|
|
952
|
+
}
|
|
953
|
+
}
|
|
763
954
|
});
|
|
764
955
|
};
|
|
765
956
|
const tick = () => {
|
|
@@ -851,6 +1042,14 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
851
1042
|
const rounds = (store.data.rounds || []).filter((x) => x.at.startsWith(fmtDate(new Date()))).length;
|
|
852
1043
|
lines.push(`路由:${cfg.router ? `开着(${r.agent || '没绑'}${r.model ? `,模型 ${r.model}` : ''}${r.effort ? `,强度 ${r.effort}` : ''}),今天 ${rounds} 轮;实时的排队和在跑看 ${baseUrl()}/monitor` : '关着(到点直接跑任务,IM 里的话直接答;要开在 profile 的 rhythm 写 router: true)'}`);
|
|
853
1044
|
lines.push(`执行者:${w.agent || '没绑'}${w.model ? `,模型 ${w.model}` : ''}${w.effort ? `,强度 ${w.effort}` : ''},同时最多 ${cfg.workers} 个`);
|
|
1045
|
+
// 订阅源:几个正常、谁在连着失败、谁还没跑过
|
|
1046
|
+
const srcs = sourceStatus().filter((x) => x.enabled);
|
|
1047
|
+
if (srcs.length) {
|
|
1048
|
+
const okN = srcs.filter((x) => !x.missing && x.last && x.last.ok).length;
|
|
1049
|
+
const bad = srcs.filter((x) => x.missing || (x.last && !x.last.ok)).map((x) => (x.missing ? `${x.id} 缺采集脚本` : `${x.id} 失败 ${x.last.fails} 次(${x.last.at.slice(5, 16)})`));
|
|
1050
|
+
const never = srcs.filter((x) => !x.last).map((x) => x.id);
|
|
1051
|
+
lines.push(`订阅源:${okN} 个正常${bad.length ? `,${bad.join(';')}` : ''}${never.length ? `,${never.join('、')} 还没跑过` : ''};看 ${baseUrl()}/monitor`);
|
|
1052
|
+
}
|
|
854
1053
|
const triggers = Object.values(store.data.triggers || {}).filter((t) => t.worker && ['queued', 'running'].includes(t.worker.status));
|
|
855
1054
|
if (triggers.length) lines.push(`没跑完的:${triggers.map((t) => t.id).join(' ')}(下次启动时补失败回执)`);
|
|
856
1055
|
const runs = Object.entries(loadState().runs).sort((a, b) => a[1].at.localeCompare(b[1].at)).slice(-5);
|
|
@@ -870,6 +1069,7 @@ function createRhythm({ root, env, packageRoot }) {
|
|
|
870
1069
|
monitorData,
|
|
871
1070
|
monitorPage,
|
|
872
1071
|
doctorNotes,
|
|
1072
|
+
recordForTest: (id, r) => recordSources([{ id, ...r }]),
|
|
873
1073
|
handle,
|
|
874
1074
|
makeEngine,
|
|
875
1075
|
engine: () => engine,
|