work-rhythm 0.1.6 → 0.1.8
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 +3 -0
- package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md +1 -0
- package/.agents/notes/decisions/2026-11-30-scheduling-moves-to-a-local-service.md +1 -0
- package/.agents/notes/decisions/2026-12-01-binding-carries-the-current-session.md +29 -0
- package/.agents/notes/decisions/2026-12-01-bot-lives-in-the-service-process.md +28 -0
- package/.agents/notes/pitfalls/2026-12-01-launchd-install-bakes-invoking-node.md +28 -0
- package/.agents/skills/work-rhythm/SKILL.md +1 -1
- package/collectors/local.env.example +6 -1
- package/docs/agents.md +10 -1
- package/docs/channels.md +5 -2
- package/docs/scheduled-tasks.md +1 -1
- package/package.json +1 -1
- package/scripts/cmd/agent.js +66 -5
- package/scripts/cmd/listen.js +48 -14
- package/scripts/cmd/rhythm.js +15 -2
- package/scripts/lib/agent.js +33 -3
- package/scripts/lib/bot.js +61 -7
- package/scripts/lib/config.js +22 -1
- package/scripts/lib/launchd.js +56 -3
- package/scripts/lib/rhythm.js +40 -3
- package/scripts/test/agent.test.js +66 -1
- package/scripts/test/harness.js +1 -0
- package/scripts/test/listen.test.js +97 -0
- package/scripts/test/rhythm.test.js +77 -0
- package/scripts/test/stubs.js +11 -1
package/.agents/notes/AGENTS.md
CHANGED
|
@@ -25,6 +25,9 @@
|
|
|
25
25
|
| 验证一条推送有没有送到,或想把机器人会话加进订阅 | [刚发的消息搜不到](pitfalls/2026-11-30-message-search-lags-after-sending.md) |
|
|
26
26
|
| 改模型 / 思考强度的配置、`agent models`,或想知道 zcode 为什么配不了模型 | [绑定时问模型和思考强度](decisions/2026-12-01-agent-binding-asks-for-model-and-effort.md) |
|
|
27
27
|
| 改引导第 6 步、`work-rhythm agent`、预置的 agent(`scripts/lib/agent.js`),或想知道为什么不往运行时的调度器建任务 | "+NEW+" |
|
|
28
|
+
| 改 launchd 的安装(`scripts/lib/launchd.js`)、plist 里绑哪个 node,或从桌面 agent 里装完服务起不来 | [装常驻服务要绑真正的 node](pitfalls/2026-12-01-launchd-install-bakes-invoking-node.md) |
|
|
29
|
+
| 改值守跑在哪(服务进程还是单独进程)、`listen install` / `rhythm restart`,或想知道为什么 launchd 里只有一项 | [值守并进本地服务,本机只留一个后台进程](decisions/2026-12-01-bot-lives-in-the-service-process.md) |
|
|
30
|
+
| 改会话绑定(`YZJ_BOT_SESSION`、`agent session` / `sessions`),或想知道机器人为什么不接上下文 | [绑 agent 时把当前会话也绑上](decisions/2026-12-01-binding-carries-the-current-session.md) |
|
|
28
31
|
| 改值守(`scripts/lib/bot.js`、`scripts/lib/agent.js`),想换一个回复的 agent,或要改值守的权限默认值 | [值守用长连接收消息](decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md) |
|
|
29
32
|
| 改本地服务(`scripts/lib/rhythm.js`)、`tasks/*.md`、`profile` 的 `rhythm:` 一节,或想知道定时任务为什么不绑在运行时的调度器上 | [运转的中心挪到本机服务](decisions/2026-11-30-scheduling-moves-to-a-local-service.md) |
|
|
30
33
|
| 写或改自测(`scripts/test/`),或自测在真实使用过的目录里变红 | [自测不能带上本机的个人配置](pitfalls/2026-11-30-selftest-must-not-inherit-local-config.md)、[在用例里再起 node --test 会一个用例都不跑](pitfalls/2026-12-01-spawning-node-test-inside-a-test-runs-nothing.md)、[钩子里登记不了收尾](pitfalls/2026-12-01-node-test-hooks-cannot-register-after-callbacks.md) |
|
package/.agents/notes/decisions/2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md
CHANGED
|
@@ -47,3 +47,4 @@ Decided: 2026-11-30
|
|
|
47
47
|
- 重新考虑的情况:云之家的长连接协议变了;ZCode 的命令行有了更轻的无界面入口;要在群里用。
|
|
48
48
|
- 2026-12-01 后续:bash 入口没有了,值守整个是 node,命令是 `work-rhythm listen`;agent 自定义命令不再经过 bash 展开。见[全改成 JS、收成一个 work-rhythm 命令](2026-12-01-everything-is-js-behind-work-rhythm.md)。
|
|
49
49
|
- 2026-12-01 后续:值守的 agent 改由 `work-rhythm agent bind` 绑(预置加了 qoder、claude),没绑就不跑。见[定时任务只建在 CLI 上,先绑一个 agent](2026-12-01-scheduling-is-cli-only-and-needs-a-bound-agent.md)。
|
|
50
|
+
- 2026-12-01 后续:「机器人有自己的专属会话、不碰用户正在用的会话」改了:绑 agent 时默认把当前会话绑上,机器人的话从它接着走。见[绑 agent 时把当前会话也绑上](2026-12-01-binding-carries-the-current-session.md);值守也不再单独常驻,见[值守并进本地服务,本机只留一个后台进程](2026-12-01-bot-lives-in-the-service-process.md)。
|
|
@@ -36,3 +36,4 @@ Decided: 2026-11-30
|
|
|
36
36
|
- 重新考虑的情况:运行时的调度器能不占会话、能跨目录建任务了;或者本机不让跑常驻进程。
|
|
37
37
|
- 2026-12-01 后续:bash 入口没有了,服务是 `work-rhythm rhythm`;判断工作日直接调 today 的函数,采集直接在同一个进程里跑 sync,不再起 bash 子进程。见[全改成 JS、收成一个 work-rhythm 命令](2026-12-01-everything-is-js-behind-work-rhythm.md)。
|
|
38
38
|
- 2026-12-01 后续:「备选:绑到运行时的调度器」去掉了,定时任务只建在本地服务上,而且要先绑一个 agent(`work-rhythm agent`)。见[定时任务只建在 CLI 上,先绑一个 agent](2026-12-01-scheduling-is-cli-only-and-needs-a-bound-agent.md)。
|
|
39
|
+
- 2026-12-01 再后续:值守并进了这个服务的进程,本机只留一个后台。见[值守并进本地服务,本机只留一个后台进程](2026-12-01-bot-lives-in-the-service-process.md)。
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# 绑 agent 时把当前这个会话也绑上:机器人在 IM 里说的话接着那个会话走
|
|
2
|
+
|
|
3
|
+
Decided: 2026-12-01
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
[值守用长连接收消息](2026-11-30-bot-inbound-uses-websocket-and-a-responder-command.md)时特意让机器人用自己的专属会话,「不碰用户正在用的会话」。用户在 my-work 里从云之家发了一句,收到的是新会话的回答——它不知道桌面上刚聊过的引导内容。用户要求:agent 关联的时候,会话要是同一个;而且发给 agent 的工作目录得是工作目录。
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
- `work-rhythm agent bind` 绑的正好是正在调这个 skill 的 agent 时,顺手把它当前这个会话绑上,写进 `collectors/local.env` 的 `YZJ_BOT_SESSION`。当前会话 id 从 agent 给工具留的环境变量拿:`DSH_SESSION_ID`、`ZCODE_SESSION_ID`、`CLAUDE_CODE_SESSION_ID`、`QODER_SESSION_ID`(Qoder 这个名字来自它的设置参考文档,没在真机上验)。换了别的 agent 就把原来绑的清掉。
|
|
12
|
+
- 值守每句话先从绑定的会话走;接不上(被删、过期)退回自己的专属会话,「接不上过」记在 `01-inbox/_bot/bound-session-lost`,换绑或 `/new` 时清掉。
|
|
13
|
+
- `work-rhythm agent session [id|--clear]` 看 / 换 / 清,`agent sessions` 列出这个工作区里 dsh 的会话(它的会话按工作区存:`~/.dsh/sessions/<路径转成的目录>/session-…`)。
|
|
14
|
+
- 工作目录不变:dsh 以工作目录为进程 cwd,zcode / qoder / claude 传 `--cwd`。dsh 的会话本来就属于一个工作区,绑的会话必须是这个工作区里的。
|
|
15
|
+
- 定时任务不受影响,每次还是新会话。
|
|
16
|
+
|
|
17
|
+
## Alternatives considered
|
|
18
|
+
|
|
19
|
+
**继续只用专属会话。** 被否:用户要的就是上下文连着。
|
|
20
|
+
|
|
21
|
+
**让机器人和桌面同时写一个会话。** 没做任何锁定:dsh 的会话目录里有 `session.lock`,实测桌面开着时 headless 仍能 `--session-id` 续上并写回;两边同时发消息会交错追加,这是用户自己能看见、能避免的事。
|
|
22
|
+
|
|
23
|
+
**给 zcode 也列会话。** 推迟:ZCode 的会话存储没查到公开的布局;绑当前会话靠 `ZCODE_SESSION_ID` 一样生效。
|
|
24
|
+
|
|
25
|
+
## Consequences
|
|
26
|
+
|
|
27
|
+
- 真机验证:dsh 用 `--session-id` 续桌面里的会话,问「我们这个会话之前在聊什么」,它答出了那次每日计划的内容。
|
|
28
|
+
- `agent test` 还是一次性的新会话,不动绑定的那个。
|
|
29
|
+
- 机器人的 `/new` 语义变了:绑了会话时,它是「丢掉专属会话、下一句从绑的那个开始」。
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 值守并进本地服务的那个进程:本机只留一个后台,launchd 里只有一项
|
|
2
|
+
|
|
3
|
+
Decided: 2026-12-01
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
定时任务在 `work-rhythm rhythm`(launchd 常驻 `work-rhythm.service.<目录>`),机器人的值守在 `work-rhythm listen run`(另一个常驻 `work-rhythm.bot.<目录>`)。用户看到后台起了好几个,提出 listen 和消息通知应该是一个。[运转的中心挪到本机服务](2026-11-30-scheduling-moves-to-a-local-service.md)当时没并:「一个是定时出活,一个是实时应答,节奏不同」。实测下来两边的配置完全重叠——同一个 agent、同一条消息渠道、同一个钥匙串、同一个工作目录——用户要装两次、改配置要重启两次,`listen install` 还得单独查一遍配置。
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
- 值守跑在 `rhythm start` 那个进程里,由 `collectors/local.env` 的 `YZJ_BOT_LISTEN=1` 打开。值守的配置不齐不拦着服务起,只记一笔「值守没开」;`rhythm doctor` 会报出来,`rhythm status` 里有「值守:开着 / 关着」和最近一条值守日志。
|
|
12
|
+
- `work-rhythm listen install` 的意思变成「打开值守」:写开关、收掉以前单独装的 `work-rhythm.bot.*` 常驻、服务没装就装、装了就 `kickstart` 重启一次;`uninstall` 只关值守。`rhythm install` 也顺手收掉老的单独常驻。新增 `rhythm restart`(launchd 管的才重启,手动起的提示停了再起)。
|
|
13
|
+
- `listen run`(前台单独跑)和 `listen probe` / `handle` 留着调试用;`bot.run({ signals: false })` 让值守能嵌在别的进程里,由外面调 `stop()`。
|
|
14
|
+
- 值守和定时任务仍是两段逻辑,只是同一个进程:定时任务一个跑完再跑下一个,值守一条一条处理,互不等待。
|
|
15
|
+
|
|
16
|
+
## Alternatives considered
|
|
17
|
+
|
|
18
|
+
**继续两个常驻。** 被否:用户要的就是一个后台;配置完全重叠,分开没有收益。
|
|
19
|
+
|
|
20
|
+
**把值守做成服务的一个「任务」。** 被否:任务是「到点跑一次 agent 然后退出」的模型,值守是长连接常驻,塞进任务队列会让它和定时任务互相排队。
|
|
21
|
+
|
|
22
|
+
**默认就开着值守。** 被否:每条消息是一次完整的 agent 运行,有费用;默认关、用户明确开了才开,这条不变。
|
|
23
|
+
|
|
24
|
+
## Consequences
|
|
25
|
+
|
|
26
|
+
- 以前按 `listen install` 装过单独常驻的机器,升级后第一次 `listen install` 或 `rhythm install` 会把老的收掉。
|
|
27
|
+
- 值守连不上云之家时在服务进程里按退避重连,不影响定时任务和网页;日志还是 `01-inbox/_bot.log`。
|
|
28
|
+
- 服务停的时候先停值守(最多等 2 秒),再关网页。
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 装常驻服务要绑真正的 node,不能绑 agent 运行时那个 Electron 二进制
|
|
2
|
+
|
|
3
|
+
Verified: 2026-12-01 · 在 DSH 里跑 `work-rhythm rhythm install`,再 `launchctl list | grep work-rhythm` 看到 PID 是 `-`、状态是 `5`,`01-inbox/_rhythm.launchd.log` 里是 `Unable to find helper app`
|
|
4
|
+
Origin: 2026-12-01 · 个人工作目录 `/Users/kingdee/workspace/my-work` 的引导第 6 步,从 DSH(DeepSeek Harness)里装的 · 实际出错(装完 `rhythm status` 立刻报「没在跑」)
|
|
5
|
+
|
|
6
|
+
## 现象
|
|
7
|
+
|
|
8
|
+
`rhythm install` 报「已装成常驻」并给出页面地址,但紧接着 `rhythm status` 是「没在跑」。`launchctl list` 里那一行是 `- -5 work-rhythm.service.my-work`(PID 空、上次退出码 5),启动日志只有一行:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
[1201/121626.592752:FATAL:electron/shell/app/electron_main_delegate_mac.mm:66] Unable to find helper app
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
plist 的 `ProgramArguments` 第一个参数是 `/Users/kingdee/.dsh-desktop/sidecar-node/<hash>/DSH Node.app/Contents/MacOS/DSH Node`。同一个二进制在终端里手动跑 `rhythm start` 是好的。
|
|
15
|
+
|
|
16
|
+
## 原因
|
|
17
|
+
|
|
18
|
+
`scripts/lib/launchd.js` 把 `process.execPath` 原样写进 plist——谁执行 `install`,服务就绑谁那个 node。从 DSH、ZCode 这类桌面 agent 里跑时,`process.execPath` 是它们自带的 Electron 包(形如 `…/DSH Node.app/Contents/MacOS/DSH Node`)。那个二进制在终端里有父进程和 app bundle 上下文,找得到 `DSH Node Helper.app`;被 launchd 直接拉起时找不到 helper,启动即退出,退出码 5。
|
|
19
|
+
|
|
20
|
+
`rhythm doctor` 不校验 plist 里的可执行文件,`rhythm status` 只报「在跑 / 没在跑」,所以安装命令本身是「成功」的,问题要等看 launchctl 和 launchd 日志才暴露。
|
|
21
|
+
|
|
22
|
+
## 怎么做
|
|
23
|
+
|
|
24
|
+
`scripts/lib/launchd.js` 的 `nodeForLaunchd()`:`process.execPath` 看着像 Electron(路径里有 `.app/`,或者设了 `ELECTRON_RUN_AS_NODE`)就另找一个真正的 node(PATH 上的,再 homebrew / usr/local 的常见位置)。装完 `install` 会等几秒核对 launchd 真的把它拉起来了,没有就带着 `launchd.log` 的最后几行报错;`rhythm doctor` 也会报「plist 里绑的不是真正的 node」。
|
|
25
|
+
|
|
26
|
+
装之前被 DSH 的工具环境影响过的话,`install` 一样能装对;已经装坏的,`work-rhythm rhythm uninstall` 再 `install` 一次。装完核对:`work-rhythm rhythm status` 显示 `在跑(pid …)`,plist 的 `ProgramArguments` 第一项是 `/opt/homebrew/Cellar/node/…/bin/node` 这类真 node。
|
|
27
|
+
|
|
28
|
+
兜底:`rhythm install` 的启动核对,`rhythm doctor` 对 plist 的检查,`scripts/test/rhythm.test.js` 的「装的是服务那一个常驻」用例。原始日志在 `01-inbox/_rhythm.launchd.log`。
|
|
@@ -228,7 +228,7 @@ IM 是工作密度最高的信号,也决定了后面能不能自动化,所
|
|
|
228
228
|
3. **没有预置的,先问用户那个 IM 有没有机器人这类能力**(Webhook 机器人、应用消息)。有就按 `docs/channels.md` 的「新增一个渠道」接成内置渠道;都没有就如实说做不了,`notify` 留空,原因记进 `setup.skipped`。不要退而用用户的账号给他自己发消息:推送会跟着登录一起失效。
|
|
229
229
|
4. **核对。** `work-rhythm check` 通过后,把要发的测试内容给用户看一眼,同意了用 `work-rhythm notify "<内容>"` 发一条,请他确认收得到、客户端有提醒。
|
|
230
230
|
|
|
231
|
-
渠道还能反过来收:用户在 IM 里说的话交给本地的 agent
|
|
231
|
+
渠道还能反过来收:用户在 IM 里说的话交给本地的 agent 处理,回复再发回来,这叫「值守」。每条消息跑一次 agent,所以默认不开;没绑 agent 就开不了。它跑在本地服务那一个进程里(`work-rhythm listen install` 打开,`uninstall` 关掉,不单独常驻)。用户要开,或问能不能在 IM 里直接对话时,按 `docs/channels.md` 的「收消息:值守」走:先讲清代价,以及权限默认只读,再确认绑了 agent(第 6 步的 `work-rhythm agent bind`,没绑就先绑),`listen handle` 试一句,`listen install` 开。绑 agent 时如果绑的正好是正在调 skill 的这个,当前这个会话会一起绑上——用户在 IM 里说的话接着这个会话走。
|
|
232
232
|
|
|
233
233
|
## 手动执行定时任务
|
|
234
234
|
|
|
@@ -53,7 +53,12 @@
|
|
|
53
53
|
# YZJ_BOT_TIMEOUT 每条消息最多等多少秒(默认 300);YZJ_BOT_REPLY_MAX 回复最多多少字(默认 2000,超出截断并注明)
|
|
54
54
|
# 消息一到机器人先回「收到,正在处理,稍等。」,YZJ_BOT_ACK=0 关掉。
|
|
55
55
|
# 每句话后面会带给 agent 一句提示(回复要以纯文本私聊发回,直接答、简短答),YZJ_BOT_PREAMBLE="" 关掉,或换成自己的
|
|
56
|
-
#
|
|
56
|
+
# 会话:绑的正好是正在调 skill 的那个 agent 时,work-rhythm agent bind 会把它当前这个会话绑上(YZJ_BOT_SESSION),
|
|
57
|
+
# 机器人在 IM 里说的话从这个会话接着走。换绑 work-rhythm agent session <id>(agent sessions 列出 dsh 在这个工作区的会话),--clear 清掉
|
|
58
|
+
# YZJ_BOT_SESSION=""
|
|
59
|
+
# 值守跑在本地服务(work-rhythm rhythm)那一个进程里,不单独常驻。work-rhythm listen install 打开它(就是写下面这个开关并重启 / 安装服务),uninstall 关掉
|
|
60
|
+
# YZJ_BOT_LISTEN="1"
|
|
61
|
+
# 配完跑 work-rhythm agent test 让它答一句;开值守后 work-rhythm listen status 看它在不在听
|
|
57
62
|
# YZJ_BOT_AGENT="dsh"
|
|
58
63
|
# YZJ_BOT_AGENT_MODE="plan"
|
|
59
64
|
|
package/docs/agents.md
CHANGED
|
@@ -11,7 +11,7 @@ work-rhythm agent test ["一句话"] # 让绑定的 agent 只读答一句(
|
|
|
11
11
|
work-rhythm agent doctor # 绑定的 agent、模型、强度有没有问题
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
绑哪个要**问**用户,不要只推一个(绑的正好是正在调 skill 的那个时,当前会话会一起绑上,见下面「会话」):把四个预置列成一张表——名字、装没装、没装的怎么装、能不能配模型——让他选,标出建议(正在调这个 skill 的那个,登录现成;或者 dsh)。没装的点了就带他装、登录(在他自己的终端里做,凭据不经过对话),`work-rhythm agent --json` 里 `agents[].install` 就是每个的安装步骤;本机装着 Qoder 的 IDE 却没装 Qoder CLI 时 `ideOnly` 会列出 `qoder`,提醒别把这项漏了(装了 IDE 不等于装了命令行)。一个都不装,如实说定时任务和值守现在做不了,`schedule` 留空,原因记进 `setup.skipped`。
|
|
15
15
|
|
|
16
16
|
## 预置
|
|
17
17
|
|
|
@@ -46,6 +46,15 @@ work-rhythm agent doctor # 绑定的 agent、模型、强度有没
|
|
|
46
46
|
|
|
47
47
|
定时任务可以用和值守不一样的一套:profile 的 `rhythm` 一节写 `agent-model` / `agent-effort`(可选项),不配就沿用上面两个。
|
|
48
48
|
|
|
49
|
+
## 会话:绑定的时候顺手把当前这个会话绑上
|
|
50
|
+
|
|
51
|
+
绑 agent 时如果绑的正好是正在调这个 skill 的那个 agent,`work-rhythm agent bind` 会把它**当前这个会话**也绑上,写进 `collectors/local.env` 的 `YZJ_BOT_SESSION`(从它给工具留的 `DSH_SESSION_ID` / `ZCODE_SESSION_ID` / `CLAUDE_CODE_SESSION_ID` / `QODER_SESSION_ID` 拿;Qoder 这个变量名来自它的设置参考文档)。之后机器人在 IM 里说的话从这个会话接着走,上下文连着。换绑 `agent session <id>`,清掉 `agent session --clear`,`--no-session` 不绑;换了别的 agent 会把原来绑的清掉(一个 agent 的会话 id 别的接不上)。
|
|
52
|
+
|
|
53
|
+
- dsh 的会话按工作区存(`~/.dsh/sessions/<工作区路径转成的目录>/session-…`),`agent sessions` 列得出这个工作区里的,新的在前,标出绑着的和值守自己的。别的 agent 的存储没查到公开的布局,列不出,但绑当前会话一样生效。
|
|
54
|
+
- 绑的会话接不上时退回值守自己的专属会话,这件事记在 `01-inbox/_bot/bound-session-lost`,换绑或 `/new` 时清掉。
|
|
55
|
+
- 2026-12-01 实测:dsh 用 `--session-id` 续桌面里的会话,问「我们这个会话之前在聊什么」,它答出了那次每日计划的内容。
|
|
56
|
+
- 定时任务不受影响:每次都是新会话,一次性运行。
|
|
57
|
+
|
|
49
58
|
## 权限
|
|
50
59
|
|
|
51
60
|
`YZJ_BOT_AGENT_MODE` 两个常用值,各家换成自己的叫法:
|
package/docs/channels.md
CHANGED
|
@@ -68,13 +68,16 @@
|
|
|
68
68
|
写完跑 `work-rhythm listen doctor`,配置和依赖有问题它会逐条报出来。
|
|
69
69
|
2. **先单跑一句**:`work-rhythm listen handle "今天怎么安排"`。它不连云之家,只看 agent 的回复对不对。
|
|
70
70
|
3. **真连着看一眼**:`work-rhythm listen probe --seconds 120`,然后在云之家里给机器人发一句话,终端里应出现这一帧。
|
|
71
|
-
4.
|
|
71
|
+
4. **开值守**:`work-rhythm listen install`。它不再单独常驻——值守跑在本地服务(`work-rhythm rhythm`)那**一个进程**里,和定时任务、推送、网页在一起,launchd 里只有一项。`install` 做三件事:打开开关(`collectors/local.env` 的 `YZJ_BOT_LISTEN=1`)、收掉以前单独装的值守常驻、本地服务没装就装上(装了就重启一次让改动生效)。`listen uninstall` 只把关掉,服务照跑;`listen status` 看服务在不在跑、值守开着没。只想在终端里看一眼用 `work-rhythm listen run`(这个是单独的进程,停了就没了)。改了配置(含值守的开关、会话)用 `work-rhythm rhythm restart` 让服务重新读。
|
|
72
72
|
|
|
73
73
|
- **只认本人**:入站消息里的发消息人(`operatorOpenid`)等于 `YZJ_MY_OPEN_ID` 才处理,别人的丢掉并记一行日志。回复一律私聊发给我,走 `work-rhythm notify`。群里的消息不处理。
|
|
74
74
|
- **先回一句收到**:消息一到,机器人先回「收到,正在处理,稍等。」,agent 的回答随后到,不用干等。`YZJ_BOT_ACK=0` 关掉。
|
|
75
75
|
- **带给 agent 的提示**:我的每句话后面会加一句「这句来自云之家,回复会以纯文本私聊发回,直接答、简短答」,免得它拿聊天当命令行,长篇大论或翻一堆文件。`YZJ_BOT_PREAMBLE=""` 关掉,或换成自己的。它只对两个预置生效;用自定义命令的话,提示写在你的脚本里。
|
|
76
76
|
- **权限**:`YZJ_BOT_AGENT_MODE` 默认 `plan`,agent 只读,能查、能答、能出计划,不改文件(zcode 是它的 plan 模式,dsh 是 `read-only`)。要在 IM 里「记一下」得改成 `edit`,那等于我在聊天里说什么它就做什么(dsh 是 `workspace-write`)。zcode 的 `build` 会停下来等确认,无界面下只能等到超时,不要用。
|
|
77
|
-
-
|
|
77
|
+
- **会话**:默认绑的就是你绑 agent 时所在的那个会话——`work-rhythm agent bind` 发现绑的正好是正在调 skill 的 agent(按它给工具留的 `DSH_SESSION_ID` / `ZCODE_SESSION_ID` / `CLAUDE_CODE_SESSION_ID` / `QODER_SESSION_ID`),就把它当前这个会话写进 `YZJ_BOT_SESSION`。之后你在 IM 里说的话从这个会话接着走,上下文是连着的:桌面上聊到一半,回云之家继续问,它知道前面说了什么。换绑用 `work-rhythm agent session <id>`(`agent sessions` 列出这个工作区里 dsh 的会话,新的在前),`agent session --clear` 清掉。
|
|
78
|
+
没绑会话、或者绑的那个接不上(被删、过期)时,机器人用自己的专属会话(ID 存在 `01-inbox/_bot/session-id`),第二轮起续它。在聊天里发 `/new` 或 `work-rhythm listen reset` 丢掉专属会话:绑了会话的,下一句又从绑的那个开始。
|
|
79
|
+
agent 永远在工作目录里跑(dsh 以工作目录为 cwd,别的传 `--cwd`);dsh 的会话本来就是按工作区存的,所以绑的会话得是这个工作区里的。
|
|
80
|
+
- **一个后台进程**:值守不单独常驻,`work-rhythm rhythm` 那个服务管全部——定时采集、定时整理、推送、网页、值守。`collectors/local.env` 的 `YZJ_BOT_LISTEN=1` 才开值守;配置不齐(没绑 agent、没写 openId、钥匙串里没地址)服务照常起,只记一笔「值守没开」。
|
|
78
81
|
- **代价**:每条消息是一次完整的 agent 运行,实测一句话 6–18 秒、约 8 万输入 token(大部分走缓存)。
|
|
79
82
|
- **用哪个模型**:`dsh` 跟着 DSH 桌面应用里的默认模型和凭据。值守只把那份配置里模型的两条摘出来、再打开它的凭据存储,写成一份覆盖文件交给 headless 模式,密钥始终在 DSH 自己的存储里,不经过值守。2026-11-30 实测是 OpenCode Go 的 `deepseek-v4.1-flash`,问「你能帮我做什么」28 秒回了一段 170 字的纯文本。
|
|
80
83
|
`zcode` 则只认 ZCode 命令行自己的「默认模型选择」,和窗口里每个会话选的无关:同一天实测窗口里是 GLM-5.4,值守起的会话是 `minimax-m3.1-flash`,一句宽泛的话它翻了 5 分钟没答完;命令行没有 `--model` 参数,提示词开头写 `/model` 不生效,续接窗口里建的会话报 `Select a model before sending a message`。所以要挑回答质量,用 `dsh`。
|
package/docs/scheduled-tasks.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 定时任务
|
|
2
2
|
|
|
3
|
-
运转的中心是本地服务 `work-rhythm rhythm`:到点采集,到点把任务交给 agent 跑一遍,跑完经消息渠道把总结推给我,再在本机起一个网页服务给我看页面。定时任务只建在这个服务上,不建在 agent 运行时自带的调度器上;换运行时也不用重建。服务要先绑一个 agent(`work-rhythm agent bind`,见 [agents.md](agents.md)
|
|
3
|
+
运转的中心是本地服务 `work-rhythm rhythm`:到点采集,到点把任务交给 agent 跑一遍,跑完经消息渠道把总结推给我,再在本机起一个网页服务给我看页面。定时任务只建在这个服务上,不建在 agent 运行时自带的调度器上;换运行时也不用重建。服务要先绑一个 agent(`work-rhythm agent bind`,见 [agents.md](agents.md)),没绑就不起。机器人值守也跑在这同一个进程里([channels.md](channels.md) 的「收消息:值守」),本机只有这一个后台。改了配置用 `work-rhythm rhythm restart`。Markdown 仍是唯一的记录,任务写的东西都在每日文件和那几个固定文件里;页面只是同一份内容的视图。
|
|
4
4
|
|
|
5
5
|
长期目标不单独占任务:每日工作计划负责提醒到期的检查、安排当天的一个最小动作,晚间工作复盘负责收集进展。目标文件的格式见 [formats.md](formats.md) 的「长期目标」。
|
|
6
6
|
|
package/package.json
CHANGED
package/scripts/cmd/agent.js
CHANGED
|
@@ -16,7 +16,9 @@ const fs = require('node:fs');
|
|
|
16
16
|
const path = require('node:path');
|
|
17
17
|
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
18
18
|
const { settings } = require('../lib/config');
|
|
19
|
-
const {
|
|
19
|
+
const { createBot } = require('../lib/bot');
|
|
20
|
+
const read = (file) => { try { return fs.readFileSync(file, 'utf8'); } catch { return ''; } };
|
|
21
|
+
const { PRESETS, PRESET_NAMES, MODEL_SUPPORT, EFFORTS, invokingAgent, invokingSession, installedAgents, ideOnly, bindAgent, createAgent, listModels, listSessions } = require('../lib/agent');
|
|
20
22
|
|
|
21
23
|
const USAGE = `work-rhythm agent [子命令] —— 定时任务和机器人值守用哪个 agent、哪个模型、思考强度多少:检测本机的、绑定、试一句
|
|
22
24
|
|
|
@@ -26,6 +28,10 @@ const USAGE = `work-rhythm agent [子命令] —— 定时任务和机器人值
|
|
|
26
28
|
bind [名字|命令] 绑定,写进 collectors/local.env 的 YZJ_BOT_AGENT;不给名字就用正在调的这个,没有就装了的第一个
|
|
27
29
|
[--model <模型>] 一起写模型:dsh 写「provider/模型id」或模型id;qoder、claude 写它们认的模型名。不写不动原来的
|
|
28
30
|
[--effort <强度>] 一起写思考强度:none / low / medium / high / max。不写不动原来的
|
|
31
|
+
[--session <id>] 一起绑会话:机器人在 IM 里说的话从这个会话接着走(上下文连着)。不写:绑的正好是正在调这个 skill 的
|
|
32
|
+
agent 时,自动绑它当前这个会话;--no-session 不绑,机器人用自己的专属会话
|
|
33
|
+
session [id|--clear] 看 / 换 / 清掉绑定的会话(YZJ_BOT_SESSION)
|
|
34
|
+
sessions 这个工作区里绑定的 agent 的会话,新的在前(现在只有 dsh 列得出)
|
|
29
35
|
model <模型id> 只换模型;model --clear 清掉,用 agent 自己的默认
|
|
30
36
|
effort <强度> 只换思考强度(none / low / medium / high / max);effort --clear 清掉
|
|
31
37
|
test ["一句话"] 让绑定的 agent 答一句(默认请它自报模型),只读
|
|
@@ -38,7 +44,13 @@ const USAGE = `work-rhythm agent [子命令] —— 定时任务和机器人值
|
|
|
38
44
|
|
|
39
45
|
function binding(root, env) {
|
|
40
46
|
const s = settings(root, env);
|
|
41
|
-
return { agent: (s.YZJ_BOT_AGENT || '').trim(), model: (s.YZJ_BOT_MODEL || '').trim(), effort: (s.YZJ_BOT_EFFORT || '').trim() };
|
|
47
|
+
return { agent: (s.YZJ_BOT_AGENT || '').trim(), model: (s.YZJ_BOT_MODEL || '').trim(), effort: (s.YZJ_BOT_EFFORT || '').trim(), session: (s.YZJ_BOT_SESSION || '').trim() };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 绑会话的一句话说明
|
|
51
|
+
function describeSession(session, kind) {
|
|
52
|
+
if (kind === 'custom') return '';
|
|
53
|
+
return session ? `会话:绑着 ${session},机器人的话从这个会话接着走` : '会话:没绑,机器人用自己的专属会话';
|
|
42
54
|
}
|
|
43
55
|
|
|
44
56
|
const kindOf = (name) => (PRESET_NAMES.includes(name) ? name : name ? 'custom' : '');
|
|
@@ -81,13 +93,15 @@ function showModels(kind, env, stdout) {
|
|
|
81
93
|
if (!r.models.length) stdout.write(' (没列出来:看它的文档或 --list-models)\n');
|
|
82
94
|
}
|
|
83
95
|
|
|
84
|
-
// bind 的参数:<名字> 可选(最多一个),--model X / --effort X 可选,顺序随意
|
|
96
|
+
// bind 的参数:<名字> 可选(最多一个),--model X / --effort X / --session X 可选,顺序随意
|
|
85
97
|
function parseBind(params) {
|
|
86
98
|
const names = [];
|
|
87
99
|
const opts = {};
|
|
88
100
|
for (let i = 0; i < params.length; i += 1) {
|
|
89
101
|
const a = params[i];
|
|
90
|
-
if (a === '--
|
|
102
|
+
if (a === '--no-session') {
|
|
103
|
+
opts.session = '';
|
|
104
|
+
} else if (a === '--model' || a === '--effort' || a === '--session') {
|
|
91
105
|
const v = params[i + 1];
|
|
92
106
|
if (v === undefined || v.startsWith('--')) throw new UsageError(`${a} 后面要跟一个值\n${USAGE}`);
|
|
93
107
|
opts[a.slice(2)] = v;
|
|
@@ -124,6 +138,7 @@ module.exports = {
|
|
|
124
138
|
boundValue: bound.agent,
|
|
125
139
|
model: bound.model,
|
|
126
140
|
effort: bound.effort,
|
|
141
|
+
session: bound.session,
|
|
127
142
|
modelSupported: support ? support.model : null,
|
|
128
143
|
effortSupported: support ? support.effort : null,
|
|
129
144
|
suggest: pick,
|
|
@@ -144,6 +159,8 @@ module.exports = {
|
|
|
144
159
|
if (bound.agent) {
|
|
145
160
|
const described = describeModel(boundKind, bound.model, bound.effort);
|
|
146
161
|
if (described) stdout.write(` ${described}\n`);
|
|
162
|
+
const sessionLine = describeSession(bound.session, boundKind);
|
|
163
|
+
if (sessionLine) stdout.write(` ${sessionLine}\n`);
|
|
147
164
|
}
|
|
148
165
|
if (!bound.agent) stdout.write(`没绑 agent 就没有定时任务,也不支持机器人响应。${pick ? `建议:work-rhythm agent bind ${pick}` : '本机一个预置的 agent 都没装,先装一个(docs/agents.md)'}\n`);
|
|
149
166
|
return EXIT.OK;
|
|
@@ -161,19 +178,63 @@ module.exports = {
|
|
|
161
178
|
const { name: given, opts } = parseBind(params);
|
|
162
179
|
const name = given || suggest(list);
|
|
163
180
|
if (!name) throw new Fail(EXIT.NOT_CONFIGURED, '本机没找到能用的 agent,也没指定要绑的命令:装一个预置的(docs/agents.md),或者 work-rhythm agent bind "<一条命令>"');
|
|
164
|
-
|
|
181
|
+
// 会话:没指定时,绑的正好是正在调这个 skill 的 agent 就把它当前这个会话绑上(它给工具留了会话 id);
|
|
182
|
+
// 换了别的 agent 就把原来绑的清掉——一个 agent 的会话 id 别的 agent 接不上
|
|
183
|
+
const bindingKind = kindOf(name);
|
|
184
|
+
if (opts.session === undefined) {
|
|
185
|
+
opts.session = bindingKind === invokingAgent(env) ? invokingSession(bindingKind, env) : '';
|
|
186
|
+
}
|
|
187
|
+
const r = bindAgent(root, env, name, { model: opts.model, effort: opts.effort, session: opts.session });
|
|
165
188
|
if (r.problems) throw new Fail(EXIT.NOT_CONFIGURED, `绑不了 ${name}:${r.problems.join(';')}`);
|
|
189
|
+
createBot({ root, env: settings(root, env) }).forgetBoundLost(); // 换了会话,之前「接不上」的记录不算了
|
|
166
190
|
const kind = kindOf(name);
|
|
167
191
|
const support = kind !== 'custom' ? MODEL_SUPPORT[kind] : null;
|
|
168
192
|
const after = binding(root, env);
|
|
169
193
|
stdout.write(`已绑定 ${name}${kind === 'custom' ? '(一条自定义命令)' : `(${PRESETS[kind].label})`},写进了 ${path.relative(root, r.file)} 的 YZJ_BOT_AGENT。\n`);
|
|
170
194
|
stdout.write(` ${describeModel(kind, after.model, after.effort)}\n`);
|
|
195
|
+
if (kind !== 'custom') stdout.write(` ${describeSession(after.session, kind)}${opts.session ? '(刚绑上)' : ''}\n`);
|
|
171
196
|
if (support && !after.model && !after.effort) {
|
|
172
197
|
stdout.write(` 模型和思考强度还没配:work-rhythm agent models ${name} 看能用哪些,work-rhythm agent model <模型> / agent effort <强度> 配上;不配就用它自己的默认。\n`);
|
|
173
198
|
}
|
|
174
199
|
stdout.write('下一步:work-rhythm agent test 让它答一句(会自报模型);定时任务用 work-rhythm rhythm doctor 查配置,机器人值守用 work-rhythm listen doctor。\n');
|
|
175
200
|
return EXIT.OK;
|
|
176
201
|
}
|
|
202
|
+
case 'sessions': {
|
|
203
|
+
if (params.length) throw new UsageError(USAGE);
|
|
204
|
+
if (!bound.agent) throw new Fail(EXIT.NOT_CONFIGURED, '还没绑 agent:work-rhythm agent bind <名字>');
|
|
205
|
+
const r = listSessions(boundKind, env, root);
|
|
206
|
+
if (r.problem) throw new Fail(EXIT.NOT_CONFIGURED, r.problem);
|
|
207
|
+
if (!r.sessions.length) {
|
|
208
|
+
stdout.write(`${boundKind} 在这个工作区里还没有会话${boundKind === 'dsh' ? '' : '(这个 agent 的会话存储没查到公开的布局,列不出来)'}。\n`);
|
|
209
|
+
return EXIT.OK;
|
|
210
|
+
}
|
|
211
|
+
stdout.write(`${boundKind} 在这个工作区的会话,新的在前:\n`);
|
|
212
|
+
for (const s of r.sessions) {
|
|
213
|
+
const when = new Date(s.mtime).toLocaleString('zh-CN', { hour12: false });
|
|
214
|
+
const marks = [s.id === bound.session ? '绑着的' : '', s.id === read(`${root}/01-inbox/_bot/session-id`).trim() ? '值守自己的' : ''].filter(Boolean);
|
|
215
|
+
stdout.write(` ${s.id} ${when}${marks.length ? ` ${marks.join(' · ')}` : ''}\n`);
|
|
216
|
+
}
|
|
217
|
+
stdout.write('绑哪个:work-rhythm agent session <id>;机器人说的话就从那个会话接着走。\n');
|
|
218
|
+
return EXIT.OK;
|
|
219
|
+
}
|
|
220
|
+
case 'session': {
|
|
221
|
+
if (!bound.agent) throw new Fail(EXIT.NOT_CONFIGURED, '还没绑 agent:work-rhythm agent bind <名字>');
|
|
222
|
+
if (boundKind === 'custom') throw new Fail(EXIT.NOT_CONFIGURED, '自定义命令的会话由你的命令自己管,这里不绑');
|
|
223
|
+
let session;
|
|
224
|
+
if (params.length === 1 && params[0] === '--clear') session = '';
|
|
225
|
+
else if (params.length === 1 && !params[0].startsWith('--')) session = params[0];
|
|
226
|
+
else if (params.length === 0) session = undefined;
|
|
227
|
+
else throw new UsageError(USAGE);
|
|
228
|
+
if (session === undefined) {
|
|
229
|
+
stdout.write(`${describeSession(bound.session, boundKind)}。当前这个会话(如果是从 ${boundKind} 里跑的):${invokingSession(boundKind, env) || '认不出来'}\n`);
|
|
230
|
+
return EXIT.OK;
|
|
231
|
+
}
|
|
232
|
+
const r = bindAgent(root, env, bound.agent, { session });
|
|
233
|
+
if (r.problems) throw new Fail(EXIT.NOT_CONFIGURED, `写不了:${r.problems.join(';')}`);
|
|
234
|
+
createBot({ root, env: settings(root, env) }).forgetBoundLost();
|
|
235
|
+
stdout.write(session ? `会话已绑为 ${session},写进了 ${path.relative(root, r.file)}。机器人下一句从这个会话接着走。\n` : `会话已清掉,机器人用自己的专属会话。\n`);
|
|
236
|
+
return EXIT.OK;
|
|
237
|
+
}
|
|
177
238
|
case 'model':
|
|
178
239
|
case 'effort': {
|
|
179
240
|
const isModel = verb === 'model';
|
package/scripts/cmd/listen.js
CHANGED
|
@@ -6,12 +6,16 @@
|
|
|
6
6
|
// 日志写 01-inbox/_bot.log,只记时间、msgId、发消息人、耗时、退出码,不记消息内容。步骤见 docs/channels.md「收消息:值守」。
|
|
7
7
|
'use strict';
|
|
8
8
|
const path = require('node:path');
|
|
9
|
-
const
|
|
9
|
+
const fs = require('node:fs');
|
|
10
|
+
const { EXIT, UsageError, readText } = require('../lib/run');
|
|
10
11
|
const { settings } = require('../lib/config');
|
|
11
12
|
const { createBot } = require('../lib/bot');
|
|
13
|
+
const { createRhythm } = require('../lib/rhythm');
|
|
14
|
+
const { writeLocalEnv } = require('../lib/config');
|
|
12
15
|
const launchd = require('../lib/launchd');
|
|
13
16
|
|
|
14
|
-
const USAGE = `work-rhythm listen <子命令> —— 值守:消息渠道的收消息一半。在 IM 里说的话交给绑定的 agent
|
|
17
|
+
const USAGE = `work-rhythm listen <子命令> —— 值守:消息渠道的收消息一半。在 IM 里说的话交给绑定的 agent 处理,回复发回来。
|
|
18
|
+
它跑在本地服务那一个进程里(work-rhythm rhythm),不再单独常驻
|
|
15
19
|
|
|
16
20
|
用法: work-rhythm listen <子命令>
|
|
17
21
|
probe [--seconds N] 连上去看握手和收到的每一帧,不调 agent,到时退出(默认 60 秒)。第一次验证用
|
|
@@ -20,7 +24,9 @@ const USAGE = `work-rhythm listen <子命令> —— 值守:消息渠道的收
|
|
|
20
24
|
handle-frame '<JSON>' 把一帧当作刚收到的处理一遍,回复照发(自测用)
|
|
21
25
|
reset 丢掉机器人的专属会话,下一句从新会话开始(在聊天里发 /new 也一样)
|
|
22
26
|
doctor 检查值守的配置和依赖,有问题逐条报出来
|
|
23
|
-
install | uninstall
|
|
27
|
+
install | uninstall 开 / 关值守的常驻:它跑在本地服务(work-rhythm rhythm)那个进程里,和定时任务、推送、网页是同一个后台;
|
|
28
|
+
没装服务的话顺手装上,装了就重启一次让改动生效
|
|
29
|
+
status 本地服务在不在跑、值守开着没、最近一条值守日志
|
|
24
30
|
前提:先绑 agent(work-rhythm agent bind),没绑就没有值守。消息一到先回「收到,正在处理」,agent 的回答随后到。
|
|
25
31
|
日志 01-inbox/_bot.log 只记时间、msgId、耗时,不记消息内容。
|
|
26
32
|
退出码: 0 完成;2 用法不对;3 没配置好(没绑 agent、钥匙串里没有地址、不是 yzj-robot 渠道);4 缺依赖;5 连不上或没处理成功。
|
|
@@ -36,9 +42,19 @@ module.exports = {
|
|
|
36
42
|
async run(args, ctx) {
|
|
37
43
|
const { root, stdout, env, packageRoot } = ctx;
|
|
38
44
|
const [cmd, ...rest] = args;
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
45
|
+
const settingsEnv = settings(root, env);
|
|
46
|
+
const bot = createBot({ root, env: settingsEnv });
|
|
47
|
+
const service = createRhythm({ root, env: settingsEnv, packageRoot });
|
|
48
|
+
const lastBotLog = () => {
|
|
49
|
+
const lines = readText(path.join(root, '01-inbox', '_bot.log')).trim().split('\n');
|
|
50
|
+
return lines[lines.length - 1] || '';
|
|
51
|
+
};
|
|
52
|
+
// 值守开着没(跑在服务里的那个开关);改了之后让服务重新读:launchd 管的重启,手动起的提示一下
|
|
53
|
+
const listenWanted = () => (settingsEnv.YZJ_BOT_LISTEN || '').trim() === '1';
|
|
54
|
+
const applyToService = () => {
|
|
55
|
+
if (!service.serviceAlive()) return '';
|
|
56
|
+
return launchd.kickstart('service', root) ? '本地服务已重启,值守的改动生效。' : '本地服务在跑但不是 launchd 管的(在终端里起的):停了再 start 才生效。';
|
|
57
|
+
};
|
|
42
58
|
switch (cmd) {
|
|
43
59
|
case 'probe': {
|
|
44
60
|
let seconds = 60;
|
|
@@ -48,7 +64,8 @@ module.exports = {
|
|
|
48
64
|
}
|
|
49
65
|
case 'run':
|
|
50
66
|
if (rest.length) throw new UsageError(USAGE);
|
|
51
|
-
|
|
67
|
+
await bot.run();
|
|
68
|
+
return EXIT.OK; // 循环退出后可能还挂着重连的定时器,命令到这里就结束
|
|
52
69
|
case 'handle':
|
|
53
70
|
if (!rest.length) throw new UsageError(USAGE);
|
|
54
71
|
stdout.write(`${bot.handle(rest.join(' '))}\n`);
|
|
@@ -71,23 +88,40 @@ module.exports = {
|
|
|
71
88
|
return problems.length ? 1 : EXIT.OK;
|
|
72
89
|
}
|
|
73
90
|
case 'install': {
|
|
91
|
+
// 开值守:先把配置查一遍(agent、钥匙串、我的 openId、渠道),再打开开关;值守跑在本地服务那个进程里
|
|
74
92
|
if (rest.length) throw new UsageError(USAGE);
|
|
75
|
-
// 先把配置查一遍,装一个跑不起来的常驻没有意义
|
|
76
93
|
bot.requireConfig(true);
|
|
77
|
-
const
|
|
78
|
-
|
|
94
|
+
const file = writeLocalEnv(root, { YZJ_BOT_LISTEN: '1' });
|
|
95
|
+
// 以前值守是单独一个常驻,把它收掉,只留服务那一个
|
|
96
|
+
launchd.uninstall('bot', root);
|
|
97
|
+
let how;
|
|
98
|
+
if (service.serviceAlive()) how = applyToService();
|
|
99
|
+
else {
|
|
100
|
+
const problems = service.doctorProblems();
|
|
101
|
+
if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `值守的开关已打开,但本地服务装不成常驻,先解决:\n${problems.join('\n')}`);
|
|
102
|
+
service.launchd.install();
|
|
103
|
+
const problem = launchd.startupProblem('service', root, path.join(root, '01-inbox', '_rhythm.launchd.log'));
|
|
104
|
+
if (problem) throw new Fail(EXIT.FAILED, problem);
|
|
105
|
+
how = '本地服务已装成常驻(定时任务、推送、网页和值守都在这一个进程里)。';
|
|
106
|
+
}
|
|
107
|
+
stdout.write(`值守已打开(${path.relative(root, file)} 的 YZJ_BOT_LISTEN=1),跑在本地服务里。${how}\n日志:${path.join(root, '01-inbox', '_bot.log')}\n现在可以在云之家给机器人发一句试试。\n`);
|
|
79
108
|
return EXIT.OK;
|
|
80
109
|
}
|
|
81
110
|
case 'uninstall':
|
|
82
111
|
if (rest.length) throw new UsageError(USAGE);
|
|
112
|
+
writeLocalEnv(root, { YZJ_BOT_LISTEN: '0' });
|
|
83
113
|
launchd.uninstall('bot', root);
|
|
84
|
-
stdout.write(
|
|
114
|
+
stdout.write(`值守已关掉(YZJ_BOT_LISTEN=0)。${applyToService()}本地服务不受影响,定时任务照跑。\n`);
|
|
85
115
|
return EXIT.OK;
|
|
86
116
|
case 'status': {
|
|
87
117
|
if (rest.length) throw new UsageError(USAGE);
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
118
|
+
const running = service.serviceAlive();
|
|
119
|
+
const legacy = fs.existsSync(launchd.plistPath('bot', root));
|
|
120
|
+
stdout.write(`本地服务${running ? '在跑' : '没在跑'}(work-rhythm rhythm status 看详情);值守${listenWanted() ? '开着' : '关着'}${listenWanted() && !running ? ',但服务没在跑,所以没在听' : ''}。\n`);
|
|
121
|
+
const last = lastBotLog();
|
|
122
|
+
if (last) stdout.write(`最近一条值守日志:${last}\n`);
|
|
123
|
+
if (legacy) stdout.write(`提醒:还有一个老的单独常驻 ${launchd.plistPath('bot', root)},work-rhythm listen install 会把它收掉。\n`);
|
|
124
|
+
return running && listenWanted() ? EXIT.OK : 1;
|
|
91
125
|
}
|
|
92
126
|
default:
|
|
93
127
|
throw new UsageError(USAGE);
|
package/scripts/cmd/rhythm.js
CHANGED
|
@@ -10,12 +10,13 @@
|
|
|
10
10
|
const { EXIT, Fail, UsageError } = require('../lib/run');
|
|
11
11
|
const { settings } = require('../lib/config');
|
|
12
12
|
const { createRhythm } = require('../lib/rhythm');
|
|
13
|
+
const launchd = require('../lib/launchd');
|
|
13
14
|
|
|
14
15
|
const USAGE = `work-rhythm rhythm <子命令> —— 本地服务:定时采集、定时把任务交给 agent 整理、经消息渠道推送、本机网页
|
|
15
16
|
|
|
16
17
|
用法: work-rhythm rhythm <子命令>
|
|
17
18
|
start [--tick 秒] 前台跑:到点跑采集和任务,起网页服务。要常驻用 install;--tick 是多久看一次表(默认 20,调试用)
|
|
18
|
-
stop | status
|
|
19
|
+
stop | status | restart 停掉;看在不在跑、下次什么时候跑什么、值守开着没;重启常驻(改了配置用)
|
|
19
20
|
due [--at 时间] [--days 天数] 从某个时间起,列出什么时候跑什么
|
|
20
21
|
run <任务id|sync> [--push] 立刻跑一次,不等时间;带 --push 才推送
|
|
21
22
|
serve [--port 端口] 只起网页服务
|
|
@@ -47,6 +48,13 @@ module.exports = {
|
|
|
47
48
|
case 'serve':
|
|
48
49
|
if (!(rest.length === 0 || (rest.length === 2 && rest[0] === '--port'))) throw new UsageError(USAGE);
|
|
49
50
|
return service.serve(rest.length ? Number(rest[1]) : service.cfg.port);
|
|
51
|
+
case 'restart': {
|
|
52
|
+
if (rest.length) throw new UsageError(USAGE);
|
|
53
|
+
if (!launchd.status('service', root).running) throw new Fail(EXIT.NOT_CONFIGURED, '服务不是 launchd 管的常驻(没装,或者在终端里手动起的):手动起的停了再 start,没装的用 install');
|
|
54
|
+
if (!launchd.kickstart('service', root)) throw new Fail(EXIT.FAILED, 'launchctl kickstart 没成功');
|
|
55
|
+
stdout.write('已重启常驻,改过的配置(含值守的开关)生效。\n');
|
|
56
|
+
return EXIT.OK;
|
|
57
|
+
}
|
|
50
58
|
case 'stop':
|
|
51
59
|
if (rest.length) throw new UsageError(USAGE);
|
|
52
60
|
stdout.write(`${service.stop()}\n`);
|
|
@@ -92,7 +100,12 @@ module.exports = {
|
|
|
92
100
|
const problems = service.doctorProblems();
|
|
93
101
|
if (problems.length) throw new Fail(EXIT.NOT_CONFIGURED, `配置有问题,先解决:\n${problems.join('\n')}`);
|
|
94
102
|
const plist = service.launchd.install();
|
|
95
|
-
|
|
103
|
+
// 值守现在跑在服务里;以前单独装的常驻收掉,别两个都开着
|
|
104
|
+
launchd.uninstall('bot', root);
|
|
105
|
+
// 装完看一眼真的起来了:launchd 拉起失败时 bootstrap 也是成功的
|
|
106
|
+
const problem = launchd.startupProblem('service', root, `${root}/01-inbox/_rhythm.launchd.log`);
|
|
107
|
+
stdout.write(`已装成常驻:${plist}\n页面:${service.baseUrl()} 日志:${root}/01-inbox/_rhythm.log\n值守跟着服务一起跑(collectors/local.env 的 YZJ_BOT_LISTEN=1 才开;work-rhythm listen install 打开)。\n`);
|
|
108
|
+
if (problem) throw new Fail(EXIT.FAILED, problem);
|
|
96
109
|
return EXIT.OK;
|
|
97
110
|
}
|
|
98
111
|
case 'uninstall':
|
package/scripts/lib/agent.js
CHANGED
|
@@ -174,9 +174,10 @@ function ideOnly(env) {
|
|
|
174
174
|
return out;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
|
-
// 把选定的 agent 写进 collectors/local.env 的 YZJ_BOT_AGENT
|
|
177
|
+
// 把选定的 agent 写进 collectors/local.env 的 YZJ_BOT_AGENT(没有这个文件就从样例复制一份),顺带可以写模型、思考强度和会话。
|
|
178
178
|
// 先确认它在本机找得到、模型和强度它认。返回 { name, kind, file } 或 { problems }
|
|
179
|
-
function bindAgent(root, env, name, { model, effort } = {}) {
|
|
179
|
+
function bindAgent(root, env, name, { model, effort, session } = {}) {
|
|
180
|
+
session = session === undefined ? undefined : String(session).trim();
|
|
180
181
|
// undefined = 不动原来的;空串 = 清掉
|
|
181
182
|
model = model === undefined ? undefined : String(model).trim();
|
|
182
183
|
effort = effort === undefined ? undefined : String(effort).trim();
|
|
@@ -195,6 +196,7 @@ function bindAgent(root, env, name, { model, effort } = {}) {
|
|
|
195
196
|
// 模型和思考强度:给了字符串就写(空串是清掉),没给(undefined)不动原来的
|
|
196
197
|
if (model !== undefined) lines = setLocalEnvKey(lines, 'YZJ_BOT_MODEL', quote(model));
|
|
197
198
|
if (effort !== undefined) lines = setLocalEnvKey(lines, 'YZJ_BOT_EFFORT', quote(effort));
|
|
199
|
+
if (session !== undefined) lines = setLocalEnvKey(lines, 'YZJ_BOT_SESSION', quote(session));
|
|
198
200
|
fs.writeFileSync(file, `${lines.join('\n').replace(/\n*$/, '')}\n`);
|
|
199
201
|
return { name, kind: presetKind(name), file, model, effort };
|
|
200
202
|
}
|
|
@@ -300,6 +302,34 @@ function dshProviderOf(text, modelId) {
|
|
|
300
302
|
// 思考强度的词表(我们的叫法):各家换成自己的,zcode 没有入口
|
|
301
303
|
const EFFORTS = ['none', 'low', 'medium', 'high', 'max'];
|
|
302
304
|
|
|
305
|
+
// 各家给工具留的「当前会话 id」:绑定时如果绑的就是正在调这个 skill 的 agent,把这个会话也绑上,IM 里的话从它接着走
|
|
306
|
+
const SESSION_ENV = { dsh: 'DSH_SESSION_ID', zcode: 'ZCODE_SESSION_ID', claude: 'CLAUDE_CODE_SESSION_ID', qoder: 'QODER_SESSION_ID' };
|
|
307
|
+
|
|
308
|
+
function invokingSession(kind, env) {
|
|
309
|
+
const key = SESSION_ENV[kind];
|
|
310
|
+
return key ? String(env[key] || '').trim() : '';
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// DSH 的会话按工作区存:<DSH_HOME>/sessions/<把路径里非字母数字的字符都换成 ->/session-<uuid>/。
|
|
314
|
+
// 列出这个工作区里的会话,新的在前:[{ id, mtime }]。别的 agent 的存储没查到公开的布局,不列
|
|
315
|
+
function dshSessions(home, root) {
|
|
316
|
+
const dir = path.join(home || os.homedir(), 'sessions', `-${`${path.resolve(root)}/`.replace(/[^A-Za-z0-9]/g, '-')}-`);
|
|
317
|
+
let names;
|
|
318
|
+
try {
|
|
319
|
+
names = fs.readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory() && d.name.startsWith('session-')).map((d) => d.name);
|
|
320
|
+
} catch {
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
return names.map((id) => ({ id, mtime: fs.statSync(path.join(dir, id)).mtimeMs })).sort((a, b) => b.mtime - a.mtime);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function listSessions(kind, env, root) {
|
|
327
|
+
if (kind !== 'dsh') return { sessions: [] };
|
|
328
|
+
const found = findDsh(env);
|
|
329
|
+
if (found.problem) return { problem: found.problem };
|
|
330
|
+
return { sessions: dshSessions(found.home, root) };
|
|
331
|
+
}
|
|
332
|
+
|
|
303
333
|
// 一个预置能不能指定模型和思考强度:modelArgs(model, effort) 返回要加的参数(没有就返回空数组)
|
|
304
334
|
const MODEL_SUPPORT = {
|
|
305
335
|
zcode: { model: false, effort: false },
|
|
@@ -528,4 +558,4 @@ function listModels(kind, env) {
|
|
|
528
558
|
return { models: [] };
|
|
529
559
|
}
|
|
530
560
|
|
|
531
|
-
module.exports = { createAgent, splitCommand, PRESETS, PRESET_NAMES, MODEL_SUPPORT, EFFORTS, invokingAgent, installedAgents, ideOnly, bindAgent, listModels, findZcode, findDsh, findQoder, findClaude };
|
|
561
|
+
module.exports = { createAgent, splitCommand, PRESETS, PRESET_NAMES, MODEL_SUPPORT, EFFORTS, SESSION_ENV, invokingAgent, invokingSession, installedAgents, ideOnly, bindAgent, listModels, listSessions, dshSessions, findZcode, findDsh, findQoder, findClaude };
|