@saltpub/agent-autoloop 0.0.0-stage → 0.1.2
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/CHANGELOG.md +19 -0
- package/INSTALL.md +118 -0
- package/LICENSE +21 -0
- package/README.en.md +264 -0
- package/README.md +260 -2
- package/SECURITY.md +48 -0
- package/dist/bin/agent-autoloop.js +2 -0
- package/dist/cli/backup.js +144 -0
- package/dist/cli/explain.js +84 -0
- package/dist/cli/main.js +1016 -0
- package/dist/cli/mcp.js +151 -0
- package/dist/cli/runs.js +40 -0
- package/dist/cli/spend.js +21 -0
- package/dist/cli/status.js +304 -0
- package/dist/cli/try.js +6 -0
- package/dist/cli/uninstall.js +84 -0
- package/dist/cli/update.js +65 -0
- package/dist/cli/why.js +115 -0
- package/dist/host/background-launcher.js +12 -0
- package/dist/host/background.js +24 -0
- package/dist/host/channels.js +4 -0
- package/dist/host/command.js +98 -0
- package/dist/host/config.js +37 -0
- package/dist/host/files.js +22 -0
- package/dist/host/home.js +5 -0
- package/dist/host/http.js +43 -0
- package/dist/host/launcher.js +188 -0
- package/dist/host/lines.js +15 -0
- package/dist/host/node-path.js +33 -0
- package/dist/host/npm.js +28 -0
- package/dist/host/plugins.js +26 -0
- package/dist/host/program.js +14 -0
- package/dist/host/runtime.js +179 -0
- package/dist/host/server.js +159 -0
- package/dist/host/service.js +325 -0
- package/dist/host/state.js +203 -0
- package/dist/host/up.js +162 -0
- package/dist/host/update.js +286 -0
- package/dist/host/version.js +81 -0
- package/dist/kernel/card-page.js +54 -0
- package/dist/kernel/credentials.js +16 -0
- package/dist/kernel/cron.js +135 -0
- package/dist/kernel/decide.js +826 -0
- package/dist/kernel/due.js +73 -0
- package/dist/kernel/errors.js +9 -0
- package/dist/kernel/evolve.js +264 -0
- package/dist/kernel/http.js +357 -0
- package/dist/kernel/query.js +202 -0
- package/dist/kernel/rules.js +20 -0
- package/dist/kernel/store.js +52 -0
- package/dist/plugins/acp/agent.js +32 -0
- package/dist/plugins/acp/bridge.js +204 -0
- package/dist/plugins/claude/agent.js +113 -0
- package/dist/plugins/codex/agent.js +105 -0
- package/dist/plugins/command/agent.js +25 -0
- package/dist/plugins/delivery/action.js +25 -0
- package/dist/plugins/delivery/agent.js +13 -0
- package/dist/plugins/delivery/delivery.js +388 -0
- package/dist/plugins/delivery/github.js +201 -0
- package/dist/plugins/delivery/handoff.js +50 -0
- package/dist/plugins/delivery/input.js +47 -0
- package/dist/plugins/delivery/poll.js +72 -0
- package/dist/plugins/delivery/project.js +144 -0
- package/dist/plugins/delivery/review-ledger.js +82 -0
- package/dist/plugins/delivery/review-policy.js +99 -0
- package/dist/plugins/delivery/review.js +280 -0
- package/dist/plugins/delivery/run.js +204 -0
- package/dist/plugins/desktop/channel.js +94 -0
- package/dist/plugins/dsh/agent.js +116 -0
- package/dist/plugins/feishu/channel.js +245 -0
- package/dist/plugins/github/source.js +30 -0
- package/dist/plugins/gitlab/source.js +30 -0
- package/dist/plugins/omp/agent.js +3 -0
- package/dist/plugins/opencode/agent.js +88 -0
- package/dist/plugins/pi/agent.js +92 -0
- package/dist/plugins/webhook/channel.js +70 -0
- package/dist/plugins/webhook/templates.js +34 -0
- package/dist/plugins/xiaojiu/channel.js +410 -0
- package/dist/plugins/zcode/agent.js +102 -0
- package/dist/protocol/client.js +56 -0
- package/dist/protocol/conformance.js +184 -0
- package/dist/protocol/job.js +1 -0
- package/dist/protocol/rules.js +230 -0
- package/dist/protocol/schema.js +70 -0
- package/dist/protocol/schema.json +985 -0
- package/dist/protocol/validate.js +150 -0
- package/dist/worker/agents.js +47 -0
- package/dist/worker/contract.js +178 -0
- package/dist/worker/run.js +52 -0
- package/dist/worker/usage.js +45 -0
- package/dist/worker/windows.ps1 +48 -0
- package/dist/worker/worker.js +595 -0
- package/docs/architecture.en.svg +178 -0
- package/docs/architecture.svg +178 -0
- package/docs/auto-update.md +53 -0
- package/docs/plugins.md +139 -0
- package/docs/protocol.md +582 -0
- package/docs/state-machines.md +77 -0
- package/examples/channel.sh +15 -0
- package/examples/plugins/hello-agent/agent.ts +40 -0
- package/examples/plugins/hello-channel/channel.ts +54 -0
- package/examples/plugins/hello-source/source.ts +19 -0
- package/examples/recipes/browser-handoff/README.md +44 -0
- package/examples/recipes/browser-handoff/SKILL.md +22 -0
- package/examples/recipes/browser-handoff/browser.json +10 -0
- package/examples/recipes/browser-handoff/handoff.ts +44 -0
- package/examples/recipes/browser-handoff/rules.json +19 -0
- package/examples/recipes/feedback/README.md +34 -0
- package/examples/recipes/feedback/SKILL.md +22 -0
- package/examples/recipes/feedback/event.json +8 -0
- package/examples/recipes/feedback/rules.json +37 -0
- package/examples/recipes/feishu-brief/README.md +28 -0
- package/examples/recipes/feishu-brief/SKILL.md +16 -0
- package/examples/recipes/feishu-brief/collect.ts +164 -0
- package/examples/recipes/feishu-brief/rules.json +57 -0
- package/examples/recipes/feishu-brief/sources.json +13 -0
- package/examples/recipes/github-issues/README.md +17 -0
- package/examples/recipes/github-issues/SKILL.md +18 -0
- package/examples/recipes/github-issues/rules.json +30 -0
- package/examples/recipes/inbox-triage/README.md +36 -0
- package/examples/recipes/inbox-triage/SKILL.md +19 -0
- package/examples/recipes/inbox-triage/collect.ts +191 -0
- package/examples/recipes/inbox-triage/rules.json +57 -0
- package/examples/recipes/inbox-triage/sources.json +8 -0
- package/examples/recipes/morning-brief/README.md +17 -0
- package/examples/recipes/morning-brief/SKILL.md +17 -0
- package/examples/recipes/morning-brief/rules.json +16 -0
- package/examples/recipes/ops-alerts/README.md +18 -0
- package/examples/recipes/ops-alerts/SKILL.md +34 -0
- package/examples/recipes/ops-alerts/restart.sh +5 -0
- package/examples/recipes/ops-alerts/rules.json +29 -0
- package/examples/recipes/website-watch/README.md +26 -0
- package/examples/recipes/website-watch/SKILL.md +19 -0
- package/examples/recipes/website-watch/check.ts +86 -0
- package/examples/recipes/website-watch/rules.json +56 -0
- package/examples/recipes/website-watch/site.json +6 -0
- package/examples/skills/weekly-sweep/SKILL.md +17 -0
- package/examples/watch-github-issues.sh +23 -0
- package/examples/worker.sh +18 -0
- package/package.json +55 -4
- package/plugins/acp/agent.ts +31 -0
- package/plugins/acp/bridge.ts +190 -0
- package/plugins/claude/agent.ts +121 -0
- package/plugins/codex/agent.ts +103 -0
- package/plugins/command/agent.ts +25 -0
- package/plugins/delivery/action.ts +29 -0
- package/plugins/delivery/action.yml +28 -0
- package/plugins/delivery/agent.ts +15 -0
- package/plugins/delivery/delivery.ts +408 -0
- package/plugins/delivery/github.ts +207 -0
- package/plugins/delivery/handoff.ts +72 -0
- package/plugins/delivery/input.ts +55 -0
- package/plugins/delivery/poll.ts +65 -0
- package/plugins/delivery/project.ts +161 -0
- package/plugins/delivery/review-ledger.ts +90 -0
- package/plugins/delivery/review-policy.ts +126 -0
- package/plugins/delivery/review.ts +324 -0
- package/plugins/delivery/run.ts +219 -0
- package/plugins/desktop/channel.ts +100 -0
- package/plugins/dsh/agent.ts +100 -0
- package/plugins/feishu/channel.ts +243 -0
- package/plugins/github/source.ts +33 -0
- package/plugins/gitlab/source.ts +32 -0
- package/plugins/omp/agent.ts +4 -0
- package/plugins/opencode/agent.ts +78 -0
- package/plugins/pi/agent.ts +80 -0
- package/plugins/webhook/channel.ts +74 -0
- package/plugins/webhook/templates.ts +32 -0
- package/plugins/xiaojiu/channel.ts +426 -0
- package/plugins/zcode/agent.ts +96 -0
- package/skills/agent-autoloop/SKILL.md +260 -0
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,1016 @@
|
|
|
1
|
+
// The command line: what a person, or their Agent, types.
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { hostname } from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { createInterface } from "node:readline/promises";
|
|
8
|
+
import { hubClient } from "../protocol/client.js";
|
|
9
|
+
import { parseRules } from "../protocol/rules.js";
|
|
10
|
+
import { CONFIG_FILE, readConfig, writeConfig } from "../host/config.js";
|
|
11
|
+
import { HOME, RULES_FILE } from "../host/home.js";
|
|
12
|
+
import { findOnPath, psQuote } from "../host/command.js";
|
|
13
|
+
import { privateDirectory } from "../host/files.js";
|
|
14
|
+
import { aboveDesktop, installService, removeService, restartService, runningUp, serviceLogs, serviceRunning, startupInstead, systemUnit, untilAnswering, } from "../host/service.js";
|
|
15
|
+
import { versionLine } from "../host/version.js";
|
|
16
|
+
import { ENTRY as PROGRAM, ROOT } from "../host/program.js";
|
|
17
|
+
import { pointSkill } from "../host/update.js";
|
|
18
|
+
import { clockFor, colorFor, render, replayCommand, skippedLine } from "./status.js";
|
|
19
|
+
import { attemptsOf, newFailure, waitingRetry } from "./try.js";
|
|
20
|
+
const HELP = `Agent 自动化中台
|
|
21
|
+
|
|
22
|
+
agent-autoloop init [--agent claude|codex] [--command '某个命令 {prompt}'] [--port 3350] [--no-service]
|
|
23
|
+
在这台电脑上装好:一个人一台电脑就够
|
|
24
|
+
agent-autoloop try 用你的 Agent 跑一遍完整流程
|
|
25
|
+
agent-autoloop why <第几轮> [--json] 查看时间线;--json 输出原始事实
|
|
26
|
+
agent-autoloop conformance <地址> --config <测试凭据文件>
|
|
27
|
+
在测试中台上检查协议
|
|
28
|
+
agent-autoloop status [--json] 现在什么情况:谁在线、等你什么、在做什么、做过什么、花了多少
|
|
29
|
+
--json 输出 /v1/state 的原始结构,给脚本用
|
|
30
|
+
agent-autoloop runs [规则] [--before <第几轮>] [--json]
|
|
31
|
+
列出做完的轮次,可以只看一条规则、往前翻;细节再用 why
|
|
32
|
+
agent-autoloop answer <卡片> [序号] [--text 别的想法]
|
|
33
|
+
回答一张卡片;通知不用写序号
|
|
34
|
+
agent-autoloop resolve <卡片> <结果> 这件事不用决定了,写一句结果
|
|
35
|
+
agent-autoloop doctor 查哪里没配好
|
|
36
|
+
agent-autoloop check [文件] 检查规则文件,不碰中台;不写文件就查 rules.json
|
|
37
|
+
agent-autoloop rules explain [文件] 按本机 rules.json(或指定草稿)说明生效的规则、Agent、通道和下次定时
|
|
38
|
+
agent-autoloop pause <规则> [--reason 原因] [--until 时间]
|
|
39
|
+
暂停一条规则:到点不跑、来事件不接也不排队,直到恢复
|
|
40
|
+
agent-autoloop resume <规则>
|
|
41
|
+
恢复,列出暂停期间跳过的事件;错过的时段不补跑
|
|
42
|
+
agent-autoloop replay <规则> --since <时间>
|
|
43
|
+
补跑这条规则从这个时间起暂停时跳过的事件,每条只补一次
|
|
44
|
+
agent-autoloop run <规则> [--subject 对象] [--reason 说明]
|
|
45
|
+
立即运行一轮,照常排队派活
|
|
46
|
+
agent-autoloop emit <类型> [--dry-run] [--subject x] [--summary x] [--detail x] [--link x] [--id x] [--key 来源密钥]
|
|
47
|
+
发一条事件;--dry-run 只预演会派给谁,不连接中台
|
|
48
|
+
agent-autoloop tell <编号> [--task 定时任务名]
|
|
49
|
+
直接告诉你一件事,内容从标准输入读
|
|
50
|
+
agent-autoloop ask --task <定时任务名>
|
|
51
|
+
定时任务问你一件事:标准输入传回执那样的 ask;你答了,它的 Agent 在 cwd 接着做
|
|
52
|
+
agent-autoloop stop <第几轮> 停掉一轮
|
|
53
|
+
agent-autoloop key new <source|worker|channel|owner> <名字>
|
|
54
|
+
新建一把密钥(只显示这一次)
|
|
55
|
+
agent-autoloop key list 有哪些密钥
|
|
56
|
+
agent-autoloop key remove <种类> <名字> 立即收回一把密钥
|
|
57
|
+
agent-autoloop add <git 地址> 安装插件,不自动更新
|
|
58
|
+
agent-autoloop mcp 用标准输入输出连接 MCP 客户端
|
|
59
|
+
agent-autoloop join <中台地址> --worker <名字> --key <密钥> [--no-service]
|
|
60
|
+
另一台电脑只接活时用
|
|
61
|
+
agent-autoloop backup [目录] 备份数据库、主密钥、配置、规则和 secrets.env;不填目录就放在 ~/.agent-autoloop/backups
|
|
62
|
+
agent-autoloop restore <目录> [--yes] 停掉中台后,用一次备份恢复本机数据
|
|
63
|
+
agent-autoloop update 更新到 npm 上的最新版本
|
|
64
|
+
agent-autoloop uninstall [--yes] 卸载程序并保留数据;--yes 跳过确认
|
|
65
|
+
agent-autoloop --version 版本(源码检出还会有提交)
|
|
66
|
+
agent-autoloop help 这份说明
|
|
67
|
+
agent-autoloop up 启动(平时由系统服务调用)
|
|
68
|
+
agent-autoloop service install|remove 系统服务
|
|
69
|
+
|
|
70
|
+
配置和数据在 ${HOME}。规则在 ${RULES_FILE},改了自动生效。`;
|
|
71
|
+
const INIT_USAGE = `用法:agent-autoloop init [--agent claude|codex] [--command '某个命令 {prompt}'] [--port 3350] [--no-service]
|
|
72
|
+
|
|
73
|
+
--agent 只接这个 Agent:claude 或 codex。不写就接本机装好的全部
|
|
74
|
+
--command 改用别的命令行 Agent;{prompt} 会作为一个独立参数传进去,带空格的路径用引号括起来;
|
|
75
|
+
这台电脑上找不到这个程序就不装
|
|
76
|
+
--port 中台监听的端口,默认 3350
|
|
77
|
+
--no-service 先不装系统服务,之后手动运行 agent-autoloop up`;
|
|
78
|
+
const TRY_USAGE = `用法:agent-autoloop try
|
|
79
|
+
|
|
80
|
+
用本机接好的 Agent 跑一遍完整流程:提问,进程退出等你回复,你拍板,它接着干完。`;
|
|
81
|
+
const STATUS_USAGE = `用法:agent-autoloop status [--json]
|
|
82
|
+
|
|
83
|
+
现在什么情况:谁在线、等你什么、在做什么、做过什么、花了多少。
|
|
84
|
+
--json 输出 /v1/state 的原始结构,给脚本和看板用`;
|
|
85
|
+
const RUNS_USAGE = `用法:agent-autoloop runs [规则] [--before <第几轮>] [--json]
|
|
86
|
+
|
|
87
|
+
列出做完的轮次,新的在前,一次 20 轮:编号、结束时间、怎么样了、规则和结果。
|
|
88
|
+
规则 只看这一条规则的
|
|
89
|
+
--before 从这一轮往前列,接着上一次列到的地方
|
|
90
|
+
--json 输出这些轮次的原始结构和下一页的 before,给脚本用`;
|
|
91
|
+
const UPDATE_USAGE = `用法:agent-autoloop update
|
|
92
|
+
|
|
93
|
+
更新到最新发布的版本。没有参数。`;
|
|
94
|
+
const UNINSTALL_USAGE = `用法:agent-autoloop uninstall [--yes]
|
|
95
|
+
|
|
96
|
+
卸载程序并保留规则、密钥和记录。
|
|
97
|
+
--yes 跳过确认`;
|
|
98
|
+
const RESTORE_USAGE = `用法:agent-autoloop restore <目录> [--yes]
|
|
99
|
+
|
|
100
|
+
恢复会替换本机的数据库、主密钥、配置、规则和 secrets.env;先停掉中台,--yes 跳过确认。`;
|
|
101
|
+
/** What each command reads besides its own words, and how to use it. */
|
|
102
|
+
const TAKES = {
|
|
103
|
+
add: { flags: [], usage: "用法:agent-autoloop add <git 地址>" },
|
|
104
|
+
answer: { flags: ["text"], usage: "用法:agent-autoloop answer <卡片> [序号] [--text 别的想法]" },
|
|
105
|
+
ask: {
|
|
106
|
+
flags: ["task"],
|
|
107
|
+
usage: `用法:agent-autoloop ask --task <定时任务名>,问题从标准输入读:
|
|
108
|
+
{ "title": "…", "text": "…", "options": ["…", "…"], "recommend": 0, "about": "留给接着做的", "key": "这件事的固定名字" }
|
|
109
|
+
rules.json 里这个定时任务要写 cwd;你答了,中台叫醒它的 Agent 在 cwd 照 skill、prompt 接着做。`,
|
|
110
|
+
},
|
|
111
|
+
backup: { flags: [], usage: "用法:agent-autoloop backup [目录]" },
|
|
112
|
+
check: { flags: [], usage: "用法:agent-autoloop check [文件]" },
|
|
113
|
+
conformance: { flags: ["config"], usage: "用法:agent-autoloop conformance <测试中台地址> --config <测试凭据文件>" },
|
|
114
|
+
doctor: { flags: [], usage: "用法:agent-autoloop doctor" },
|
|
115
|
+
emit: {
|
|
116
|
+
flags: ["dry-run", "subject", "summary", "detail", "link", "id", "key"],
|
|
117
|
+
usage: "用法:agent-autoloop emit <类型> [--dry-run] [--subject x] [--summary x] [--detail x] [--link x] [--id x] [--key 来源密钥]",
|
|
118
|
+
},
|
|
119
|
+
init: { flags: ["agent", "command", "port", "no-service"], usage: INIT_USAGE },
|
|
120
|
+
join: {
|
|
121
|
+
flags: ["worker", "key", "no-service"],
|
|
122
|
+
usage: "用法:agent-autoloop join <中台地址> --worker <名字> --key <密钥> [--no-service]",
|
|
123
|
+
},
|
|
124
|
+
key: {
|
|
125
|
+
flags: [],
|
|
126
|
+
usage: `用法:agent-autoloop key new <source|worker|channel|owner> <名字>
|
|
127
|
+
agent-autoloop key list
|
|
128
|
+
agent-autoloop key remove <种类> <名字>`,
|
|
129
|
+
},
|
|
130
|
+
mcp: { flags: [], usage: "用法:agent-autoloop mcp" },
|
|
131
|
+
pause: {
|
|
132
|
+
flags: ["reason", "until"],
|
|
133
|
+
usage: "用法:agent-autoloop pause <规则> [--reason 原因] [--until 时间]",
|
|
134
|
+
},
|
|
135
|
+
// Only its first word is read as anything: the rest is a result in someone's own words, dashes and all.
|
|
136
|
+
resolve: { flags: [], usage: "用法:agent-autoloop resolve <卡片> <结果>" },
|
|
137
|
+
restore: { flags: ["yes"], usage: RESTORE_USAGE },
|
|
138
|
+
resume: { flags: [], usage: "用法:agent-autoloop resume <规则>" },
|
|
139
|
+
replay: {
|
|
140
|
+
flags: ["since"],
|
|
141
|
+
usage: "用法:agent-autoloop replay <规则> --since <时间>(恢复时打印的就是这条命令)",
|
|
142
|
+
},
|
|
143
|
+
rules: { flags: [], usage: "用法:agent-autoloop rules explain [文件]" },
|
|
144
|
+
runs: { flags: ["before", "json"], usage: RUNS_USAGE },
|
|
145
|
+
run: { flags: ["subject", "reason"], usage: "用法:agent-autoloop run <规则> [--subject 对象] [--reason 说明]" },
|
|
146
|
+
service: { flags: [], usage: "用法:agent-autoloop service install|remove" },
|
|
147
|
+
status: { flags: ["json"], usage: STATUS_USAGE },
|
|
148
|
+
stop: { flags: [], usage: "用法:agent-autoloop stop <第几轮>" },
|
|
149
|
+
tell: { flags: ["task"], usage: "用法:agent-autoloop tell <编号> [--task 定时任务名],内容从标准输入读" },
|
|
150
|
+
try: { flags: [], usage: TRY_USAGE },
|
|
151
|
+
uninstall: { flags: ["yes"], usage: UNINSTALL_USAGE },
|
|
152
|
+
up: { flags: [], usage: "用法:agent-autoloop up" },
|
|
153
|
+
update: { flags: [], usage: UPDATE_USAGE },
|
|
154
|
+
why: { flags: ["json"], usage: "用法:agent-autoloop why <第几轮> [--json]" },
|
|
155
|
+
};
|
|
156
|
+
function flags(args) {
|
|
157
|
+
const named = {};
|
|
158
|
+
const rest = [];
|
|
159
|
+
for (let i = 0; i < args.length; i++) {
|
|
160
|
+
if (!args[i].startsWith("--")) {
|
|
161
|
+
rest.push(args[i]);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const name = args[i].slice(2);
|
|
165
|
+
if (args[i + 1] !== undefined && !args[i + 1].startsWith("--"))
|
|
166
|
+
named[name] = args[++i];
|
|
167
|
+
else
|
|
168
|
+
named[name] = true;
|
|
169
|
+
}
|
|
170
|
+
return { named, rest };
|
|
171
|
+
}
|
|
172
|
+
const text = (value) => (typeof value === "string" ? value : undefined);
|
|
173
|
+
const fail = (words) => {
|
|
174
|
+
console.error(words);
|
|
175
|
+
process.exit(1);
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Stops at a flag the command doesn't read. A misspelt --port would otherwise install with the default in silence, and
|
|
179
|
+
* --help would install at all.
|
|
180
|
+
*/
|
|
181
|
+
function refuseUnknownFlags(args, { flags: known, usage }) {
|
|
182
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
183
|
+
console.log(usage);
|
|
184
|
+
process.exit(0);
|
|
185
|
+
}
|
|
186
|
+
const unknown = Object.keys(flags(args).named).filter((name) => !known.includes(name));
|
|
187
|
+
if (unknown.length)
|
|
188
|
+
fail(`不认识的参数:${unknown.map((name) => `--${name}`).join("、")}\n\n${usage}`);
|
|
189
|
+
}
|
|
190
|
+
const client = (key, config = readConfig()) => hubClient(config.url, key);
|
|
191
|
+
const ownerClient = (config = readConfig()) => client(config.owner ?? fail("这台电脑上没有本人的密钥(只接活的电脑看不了)"), config);
|
|
192
|
+
/** The first rules of a new install: its agents and one rule to try the whole loop with. */
|
|
193
|
+
function starterRules(agents, first) {
|
|
194
|
+
return {
|
|
195
|
+
sources: { local: { name: "本机" } },
|
|
196
|
+
agents,
|
|
197
|
+
channels: { desktop: { name: "桌面通知" } },
|
|
198
|
+
rules: [
|
|
199
|
+
{
|
|
200
|
+
name: "试一下",
|
|
201
|
+
when: { source: "local", types: ["try.*"] },
|
|
202
|
+
to: first,
|
|
203
|
+
cwd: path.join(HOME, "try"),
|
|
204
|
+
prompt: "这是 agent-autoloop try 的试跑,不要改任何文件,不要做别的事。读资料文件。" +
|
|
205
|
+
"如果 answers 是空的,就问一个问题:title 写“试跑:选一个颜色”,text 写“这是一次试跑,选哪个都行。”," +
|
|
206
|
+
'options 写 ["蓝色","绿色"],key 写 try-color,第一行结论写“问了一个问题”。' +
|
|
207
|
+
"如果 answers 不是空的,第一行结论写“你选了某某,试跑通过”,把某某换成 answers 里的 chosen(所选那项的文字),不附回执。",
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
elsewhere: [],
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
/** Shows the user's Agents how to set it up: one link in the shared skills folder every Agent reads, and the manual's
|
|
214
|
+
* relative paths lead to the documents and plugin sources of the version that's running. */
|
|
215
|
+
function linkSkill() {
|
|
216
|
+
pointSkill();
|
|
217
|
+
}
|
|
218
|
+
/** What to type to run this program: its name if that's what a shell finds, otherwise where it is. */
|
|
219
|
+
function howToRun() {
|
|
220
|
+
if (process.platform === "win32") {
|
|
221
|
+
const found = findOnPath("agent-autoloop");
|
|
222
|
+
// npm's shim names this package rather than sitting inside its folder.
|
|
223
|
+
let ours = false;
|
|
224
|
+
try {
|
|
225
|
+
ours =
|
|
226
|
+
found !== null &&
|
|
227
|
+
readFileSync(found, "utf8").replaceAll("\\", "/").includes("node_modules/@saltpub/agent-autoloop/");
|
|
228
|
+
}
|
|
229
|
+
catch { }
|
|
230
|
+
if (ours)
|
|
231
|
+
return "agent-autoloop";
|
|
232
|
+
return `& ${psQuote(process.execPath)} ${psQuote(PROGRAM)}`;
|
|
233
|
+
}
|
|
234
|
+
const found = spawnSync("/bin/sh", ["-c", "command -v agent-autoloop"], { encoding: "utf8" }).stdout.trim();
|
|
235
|
+
if (found && realpathSync(found) === realpathSync(PROGRAM))
|
|
236
|
+
return "agent-autoloop";
|
|
237
|
+
return /\s/.test(PROGRAM) ? `'${PROGRAM}'` : PROGRAM;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* A command line split the way a shell would: spaces part words, quotes keep one together and are dropped. No escapes, as
|
|
241
|
+
* a Windows path is all backslashes. Null when a quote is left open.
|
|
242
|
+
*/
|
|
243
|
+
function commandWords(line) {
|
|
244
|
+
const words = [];
|
|
245
|
+
let word = null;
|
|
246
|
+
let quote = null;
|
|
247
|
+
for (const char of line) {
|
|
248
|
+
if (quote) {
|
|
249
|
+
if (char === quote)
|
|
250
|
+
quote = null;
|
|
251
|
+
else
|
|
252
|
+
word += char;
|
|
253
|
+
}
|
|
254
|
+
else if (char === '"' || char === "'") {
|
|
255
|
+
quote = char;
|
|
256
|
+
word ??= "";
|
|
257
|
+
}
|
|
258
|
+
else if (/\s/.test(char)) {
|
|
259
|
+
if (word !== null)
|
|
260
|
+
words.push(word);
|
|
261
|
+
word = null;
|
|
262
|
+
}
|
|
263
|
+
else
|
|
264
|
+
word = (word ?? "") + char;
|
|
265
|
+
}
|
|
266
|
+
if (quote)
|
|
267
|
+
return null;
|
|
268
|
+
if (word !== null)
|
|
269
|
+
words.push(word);
|
|
270
|
+
return words;
|
|
271
|
+
}
|
|
272
|
+
/** What was asked for on the command line, or whichever of the usual Agents is installed. */
|
|
273
|
+
async function agentsToUse(named, worker) {
|
|
274
|
+
const agents = {};
|
|
275
|
+
const { adapterFor, findProgram } = await import("../worker/agents.js");
|
|
276
|
+
const command = text(named.command);
|
|
277
|
+
if (command) {
|
|
278
|
+
const words = commandWords(command);
|
|
279
|
+
if (!words?.length)
|
|
280
|
+
return fail(`--command 里有没配对的引号:${command}`);
|
|
281
|
+
// Found now, or every run of every rule fails later with the install long said to be done.
|
|
282
|
+
const [first] = words;
|
|
283
|
+
const there = /[\\/]/.test(first)
|
|
284
|
+
? existsSync(path.resolve(first))
|
|
285
|
+
: /^[\w.+@-]+$/.test(first) &&
|
|
286
|
+
(() => {
|
|
287
|
+
try {
|
|
288
|
+
return Boolean(findProgram(first));
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
})();
|
|
294
|
+
if (!there)
|
|
295
|
+
return fail(`找不到 ${first}:先装好它,或者在 --command 里写它的完整路径。`);
|
|
296
|
+
agents.agent = { name: "Agent", worker, use: "command", command: words };
|
|
297
|
+
return agents;
|
|
298
|
+
}
|
|
299
|
+
const wanted = text(named.agent);
|
|
300
|
+
const emptyTurn = {
|
|
301
|
+
prompt: "",
|
|
302
|
+
session: "",
|
|
303
|
+
resume: null,
|
|
304
|
+
cwd: "",
|
|
305
|
+
access: "edit",
|
|
306
|
+
contextFile: "",
|
|
307
|
+
folder: "",
|
|
308
|
+
};
|
|
309
|
+
for (const name of ["claude", "codex"]) {
|
|
310
|
+
if (wanted && wanted !== name)
|
|
311
|
+
continue;
|
|
312
|
+
const adapter = await adapterFor(name);
|
|
313
|
+
try {
|
|
314
|
+
// An adapter finds its program to build the command line, and throws if it's not there.
|
|
315
|
+
adapter.command(emptyTurn);
|
|
316
|
+
agents[name] = { name: name === "claude" ? "Claude Code" : "Codex", worker, use: name };
|
|
317
|
+
}
|
|
318
|
+
catch { }
|
|
319
|
+
}
|
|
320
|
+
return agents;
|
|
321
|
+
}
|
|
322
|
+
/** An npm install gets the managed layout before its service points at it; a checkout keeps running itself. */
|
|
323
|
+
async function prepareManagedInstall() {
|
|
324
|
+
try {
|
|
325
|
+
const { prepareManaged } = await import("../host/update.js");
|
|
326
|
+
const pointer = prepareManaged(ROOT);
|
|
327
|
+
if (pointer)
|
|
328
|
+
console.log(`程序副本:${pointer.dir}`);
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
console.log(`没能准备自动更新:${error.message}。服务会直接运行当前程序。`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async function init(args) {
|
|
335
|
+
const { named, rest } = flags(args);
|
|
336
|
+
// `init -p 4000` would otherwise install on the default port in silence.
|
|
337
|
+
if (rest.length)
|
|
338
|
+
return fail(`不认识的参数:${rest.join(" ")}\n\n${INIT_USAGE}`);
|
|
339
|
+
if (existsSync(CONFIG_FILE))
|
|
340
|
+
return fail(`已经装过了:${CONFIG_FILE}`);
|
|
341
|
+
privateDirectory(HOME);
|
|
342
|
+
mkdirSync(path.join(HOME, "try"), { recursive: true });
|
|
343
|
+
const hostName = hostname()
|
|
344
|
+
.toLowerCase()
|
|
345
|
+
.replace(/\.local$/, "");
|
|
346
|
+
const worker = hostName.replace(/[^a-z0-9-]/g, "-").replace(/^[^a-z]+/, "") || "this-machine";
|
|
347
|
+
const agents = await agentsToUse(named, worker);
|
|
348
|
+
if (!Object.keys(agents).length) {
|
|
349
|
+
return fail("这台电脑上没找到 Claude Code 或 Codex。先装好并登录," +
|
|
350
|
+
"或者用 --command 指定别的命令行 Agent,比如 --command 'hermes chat -Q -q {prompt}'");
|
|
351
|
+
}
|
|
352
|
+
const port = text(named.port) ?? "3350";
|
|
353
|
+
const url = `http://127.0.0.1:${port}`;
|
|
354
|
+
const ownRules = !existsSync(RULES_FILE);
|
|
355
|
+
if (ownRules) {
|
|
356
|
+
writeFileSync(RULES_FILE, `${JSON.stringify(starterRules(agents, Object.keys(agents)[0]), null, 2)}\n`);
|
|
357
|
+
}
|
|
358
|
+
const { addKey } = await import("../host/runtime.js");
|
|
359
|
+
const config = {
|
|
360
|
+
url,
|
|
361
|
+
hub: { listen: `127.0.0.1:${port}`, publicUrl: url },
|
|
362
|
+
worker: { name: worker, key: addKey("worker", worker) },
|
|
363
|
+
source: { name: "local", key: addKey("source", "local") },
|
|
364
|
+
owner: addKey("owner", "owner"),
|
|
365
|
+
// A channel only rules.json knows about, or only this file, would do nothing: it's on only if both are ours to write.
|
|
366
|
+
...(ownRules && { channels: { desktop: { key: addKey("channel", "desktop") } } }),
|
|
367
|
+
};
|
|
368
|
+
writeConfig(config);
|
|
369
|
+
await prepareManagedInstall();
|
|
370
|
+
linkSkill();
|
|
371
|
+
if (!named["no-service"]) {
|
|
372
|
+
installService();
|
|
373
|
+
await waitForHub();
|
|
374
|
+
}
|
|
375
|
+
const names = Object.values(agents).map((agent) => agent.name);
|
|
376
|
+
const reach = ownRules ? " 通道:桌面通知\n" : "";
|
|
377
|
+
const run = howToRun();
|
|
378
|
+
const next = named["no-service"]
|
|
379
|
+
? `先运行 ${run} up 启动中台,再开一个终端运行 ${run} try 跑一遍完整流程。`
|
|
380
|
+
: `运行 ${run} try 跑一遍完整流程。`;
|
|
381
|
+
console.log(`装好了。\n Agent:${names.join("、")}\n${reach} 规则:${RULES_FILE}\n\n${next}`);
|
|
382
|
+
}
|
|
383
|
+
async function join(args) {
|
|
384
|
+
const { named, rest } = flags(args);
|
|
385
|
+
const [url] = rest;
|
|
386
|
+
const name = text(named.worker);
|
|
387
|
+
const key = text(named.key);
|
|
388
|
+
if (!url || !name || !key)
|
|
389
|
+
return fail(TAKES.join.usage);
|
|
390
|
+
// Joining another hub must not silently replace this machine's existing hub, worker or owner keys.
|
|
391
|
+
if (existsSync(CONFIG_FILE)) {
|
|
392
|
+
return fail(`已经配置过了:${CONFIG_FILE}。为防止覆盖现有设置,请先备份并手动修改配置。`);
|
|
393
|
+
}
|
|
394
|
+
writeConfig({ url, worker: { name, key } });
|
|
395
|
+
if (!named["no-service"])
|
|
396
|
+
installService();
|
|
397
|
+
const next = named["no-service"] ? `运行 ${howToRun()} up 开始。` : "";
|
|
398
|
+
console.log(`这台电脑以 ${name} 的身份接活了。${next}`);
|
|
399
|
+
}
|
|
400
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
401
|
+
function showCard(card) {
|
|
402
|
+
console.log(`\n【${card.kind === "ask" ? "等你决定" : "要你留意"}】#${card.id} ${card.title}`);
|
|
403
|
+
if (card.text)
|
|
404
|
+
console.log(card.text);
|
|
405
|
+
if (card.kind !== "ask")
|
|
406
|
+
return;
|
|
407
|
+
card.options.forEach((option, index) => {
|
|
408
|
+
console.log(` ${index + 1}. ${option}${index === card.recommend ? "(推荐)" : ""}`);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
const howToAnswer = (card) => card.kind === "ask" ? `回答:agent-autoloop answer ${card.id} <序号>` : `处理完了:agent-autoloop answer ${card.id}`;
|
|
412
|
+
async function chooseAt(terminal, card) {
|
|
413
|
+
for (;;) {
|
|
414
|
+
const choice = Number(await terminal.question("\n选几?"));
|
|
415
|
+
if (Number.isInteger(choice) && choice >= 1 && choice <= card.options.length)
|
|
416
|
+
return choice - 1;
|
|
417
|
+
console.log(`输入 1 到 ${card.options.length}。`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
async function tryIt() {
|
|
421
|
+
const config = readConfig();
|
|
422
|
+
if (!config.source)
|
|
423
|
+
return fail("这台电脑上没有来源的密钥。");
|
|
424
|
+
const owner = ownerClient(config);
|
|
425
|
+
const stamp = Date.now();
|
|
426
|
+
// A hub that can't be reached is said just below, when the event doesn't go through.
|
|
427
|
+
const before = await owner.state().catch(() => ({ runs: [] }));
|
|
428
|
+
const attempts = attemptsOf(before.runs);
|
|
429
|
+
await client(config.source.key, config)
|
|
430
|
+
.event({ id: `try-${stamp}`, type: "try.start", subject: `#${stamp}` })
|
|
431
|
+
.catch((error) => fail(`连不上中台(${error.message}):先 agent-autoloop up 或 agent-autoloop service install`));
|
|
432
|
+
const retrying = waitingRetry(before.runs);
|
|
433
|
+
if (retrying) {
|
|
434
|
+
const clock = { timeZone: before.timezone, hour: "2-digit", minute: "2-digit", hourCycle: "h23" };
|
|
435
|
+
const time = new Date(retrying.notBefore).toLocaleString("zh-CN", clock);
|
|
436
|
+
console.log(`上一次没跑成(${retrying.error}),${time} 会自动再试,这次的事件会一起处理。`);
|
|
437
|
+
}
|
|
438
|
+
else
|
|
439
|
+
console.log("事件发出去了,Agent 开始干活……");
|
|
440
|
+
let asked = null;
|
|
441
|
+
for (let i = 0; i < 300; i++) {
|
|
442
|
+
await sleep(1000);
|
|
443
|
+
const state = await owner.state();
|
|
444
|
+
const failed = newFailure(state.runs, attempts);
|
|
445
|
+
if (failed)
|
|
446
|
+
return fail(`Agent 没跑成:${failed.error}\n运行 agent-autoloop doctor 看看。`);
|
|
447
|
+
if (asked === null) {
|
|
448
|
+
// An agent asking the same thing again lands on the card already open, so this may be from before: it's the one.
|
|
449
|
+
const card = state.cards.find((open) => open.kind === "ask" && open.title.startsWith("试跑"));
|
|
450
|
+
if (!card)
|
|
451
|
+
continue;
|
|
452
|
+
asked = card.id;
|
|
453
|
+
console.log("\nAgent 结束了这一轮,问了你一个问题。它现在不占进程、不花 Token。");
|
|
454
|
+
showCard(card);
|
|
455
|
+
if (!process.stdin.isTTY) {
|
|
456
|
+
console.log(`${howToAnswer(card)}\n答完运行 agent-autoloop status,能看到它接着把活干完。`);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const terminal = createInterface({ input: process.stdin, output: process.stdout });
|
|
460
|
+
const choice = await chooseAt(terminal, card);
|
|
461
|
+
terminal.close();
|
|
462
|
+
await owner.answer(card.id, { choice });
|
|
463
|
+
console.log("收到,Agent 接着干……");
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const done = state.recentCards.find((card) => card.id === asked && card.state === "done");
|
|
467
|
+
if (done) {
|
|
468
|
+
console.log(`它把活干完了:${done.result}\n\n通了。接下来把 ${RULES_FILE} 改成你自己的规则,或者跟你的 Agent 说一句。`);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
fail("五分钟没等到结果。运行 agent-autoloop doctor 看看。");
|
|
473
|
+
}
|
|
474
|
+
async function status(named) {
|
|
475
|
+
const state = await ownerClient().state();
|
|
476
|
+
if (named.json !== undefined)
|
|
477
|
+
return console.log(JSON.stringify(state, null, 2));
|
|
478
|
+
console.log(render(state, { color: colorFor(process.stdout, process.env), columns: process.stdout.columns }));
|
|
479
|
+
}
|
|
480
|
+
async function runs(args) {
|
|
481
|
+
const { named, rest } = flags(args);
|
|
482
|
+
if (rest.length > 1 || named.before === true)
|
|
483
|
+
return fail(RUNS_USAGE);
|
|
484
|
+
const before = named.before === undefined ? undefined : runOf(named.before, RUNS_USAGE);
|
|
485
|
+
const owner = ownerClient();
|
|
486
|
+
const { findRuns, runsText } = await import("./runs.js");
|
|
487
|
+
const found = await findRuns((at) => owner.state(at), { rule: rest[0], before });
|
|
488
|
+
if (named.json !== undefined)
|
|
489
|
+
return console.log(JSON.stringify({ runs: found.runs, before: found.more }, null, 2));
|
|
490
|
+
const color = colorFor(process.stdout, process.env);
|
|
491
|
+
console.log(runsText(found, { rule: rest[0], color, columns: process.stdout.columns }));
|
|
492
|
+
}
|
|
493
|
+
async function answer(args) {
|
|
494
|
+
const { named, rest } = flags(args);
|
|
495
|
+
const card = Number(rest[0]);
|
|
496
|
+
if (!Number.isInteger(card) || card < 1)
|
|
497
|
+
return fail(TAKES.answer.usage);
|
|
498
|
+
const words = text(named.text);
|
|
499
|
+
const done = await ownerClient().answer(card, {
|
|
500
|
+
...(rest[1] !== undefined && { choice: Number(rest[1]) - 1 }),
|
|
501
|
+
...(words && { text: words }),
|
|
502
|
+
});
|
|
503
|
+
console.log(!done.changed
|
|
504
|
+
? "这张卡片已经处理过了,这次没有改动。"
|
|
505
|
+
: done.state === "closed"
|
|
506
|
+
? "结案了。"
|
|
507
|
+
: "收到,Agent 接着干。");
|
|
508
|
+
}
|
|
509
|
+
async function resolve(args) {
|
|
510
|
+
const [id, ...words] = args;
|
|
511
|
+
const card = Number(id);
|
|
512
|
+
if (!Number.isInteger(card) || card < 1 || !words.length)
|
|
513
|
+
return fail(TAKES.resolve.usage);
|
|
514
|
+
const { changed } = await ownerClient().resolve(card, words.join(" "));
|
|
515
|
+
console.log(changed ? "结案了。" : "这张卡片已经处理过了,这次没有改动。");
|
|
516
|
+
}
|
|
517
|
+
async function pause(args) {
|
|
518
|
+
const { named, rest } = flags(args);
|
|
519
|
+
const body = {
|
|
520
|
+
...(text(named.reason) && { reason: text(named.reason) }),
|
|
521
|
+
...(text(named.until) && { until: text(named.until) }),
|
|
522
|
+
};
|
|
523
|
+
if (!rest[0])
|
|
524
|
+
return fail(TAKES.pause.usage);
|
|
525
|
+
const done = await ownerClient().pause(rest[0], body);
|
|
526
|
+
const { since, until, skipped } = done.paused;
|
|
527
|
+
const ends = until ? `,${localClock().when(until)} 自动恢复` : "";
|
|
528
|
+
const lines = [done.changed ? `暂停了「${done.rule}」${ends}。` : `「${done.rule}」本来就暂停着${ends}。`];
|
|
529
|
+
// A schedule lets its moments pass and keeps no events.
|
|
530
|
+
if (skipped === null)
|
|
531
|
+
lines.push("暂停期间到点不跑,恢复后也不补跑错过的时段。");
|
|
532
|
+
else if (!until)
|
|
533
|
+
lines.push("暂停期间来的事件这条规则不接,也不排队等恢复;恢复时会列出这些事件,可以补跑。");
|
|
534
|
+
// Nobody reads anything when a pause ends by itself, so the way to replay is told now.
|
|
535
|
+
else
|
|
536
|
+
lines.push("暂停期间来的事件这条规则不接,也不排队;到时不会列出,之后补跑:", ` ${replayCommand(done.rule, since)}`);
|
|
537
|
+
if (skipped?.count)
|
|
538
|
+
lines.push(`到现在跳过了 ${skipped.count} 条事件。`);
|
|
539
|
+
console.log(lines.join("\n"));
|
|
540
|
+
}
|
|
541
|
+
/** Times from the hub as they read here, for what answers a command rather than the state. */
|
|
542
|
+
const localClock = () => clockFor({ now: new Date().toISOString(), timezone: Intl.DateTimeFormat().resolvedOptions().timeZone });
|
|
543
|
+
async function resume(args) {
|
|
544
|
+
const { rest } = flags(args);
|
|
545
|
+
if (!rest[0])
|
|
546
|
+
return fail(TAKES.resume.usage);
|
|
547
|
+
const done = await ownerClient().resume(rest[0]);
|
|
548
|
+
if (!done.changed)
|
|
549
|
+
return console.log(`「${done.rule}」本来就没暂停。`);
|
|
550
|
+
const { since, skipped } = done.paused ?? {};
|
|
551
|
+
const { when } = localClock();
|
|
552
|
+
const lines = [`恢复了「${done.rule}」。`];
|
|
553
|
+
if (skipped === null)
|
|
554
|
+
lines[0] += "错过的时段不补跑,从下一个时刻照常跑。";
|
|
555
|
+
else if (skipped?.count === 0)
|
|
556
|
+
lines[0] += "暂停期间没有跳过事件。";
|
|
557
|
+
else if (skipped) {
|
|
558
|
+
lines[0] += `暂停期间(${when(since)} 起)跳过了 ${skipped.count} 条事件,没有补跑:`;
|
|
559
|
+
const room = Math.min(process.stdout.columns || 100, 120) - 2;
|
|
560
|
+
for (const event of skipped.events)
|
|
561
|
+
lines.push(` ${skippedLine(event, room, when)}`);
|
|
562
|
+
if (skipped.count > skipped.events.length) {
|
|
563
|
+
lines.push(` 共 ${skipped.count} 条,这里列出最近 ${skipped.events.length} 条`);
|
|
564
|
+
}
|
|
565
|
+
lines.push(`补跑这 ${skipped.count} 条:${replayCommand(done.rule, since)}`);
|
|
566
|
+
}
|
|
567
|
+
console.log(lines.join("\n"));
|
|
568
|
+
}
|
|
569
|
+
async function replay(args) {
|
|
570
|
+
const { named, rest } = flags(args);
|
|
571
|
+
const since = text(named.since);
|
|
572
|
+
if (!rest[0] || !since)
|
|
573
|
+
return fail(TAKES.replay.usage);
|
|
574
|
+
const done = await ownerClient().replay(rest[0], { since });
|
|
575
|
+
if (!done.events.length)
|
|
576
|
+
return console.log("没有要补跑的事件:这段时间没有跳过的,或者都已经补跑过了。");
|
|
577
|
+
console.log(`补跑了 ${done.events.length} 条,排上了第 ${done.runs.join("、")} 轮。`);
|
|
578
|
+
}
|
|
579
|
+
async function runNow(args) {
|
|
580
|
+
const { named, rest } = flags(args);
|
|
581
|
+
if (!rest[0])
|
|
582
|
+
return fail(TAKES.run.usage);
|
|
583
|
+
const body = {
|
|
584
|
+
...(text(named.subject) && { subject: text(named.subject) }),
|
|
585
|
+
...(text(named.reason) && { reason: text(named.reason) }),
|
|
586
|
+
};
|
|
587
|
+
const done = await ownerClient().run(rest[0], body);
|
|
588
|
+
console.log(`排上了:第 ${done.run} 轮。`);
|
|
589
|
+
}
|
|
590
|
+
async function doctor() {
|
|
591
|
+
let bad = 0;
|
|
592
|
+
const say = (ok, words) => {
|
|
593
|
+
console.log(`${ok ? "●" : "○"} ${words}`);
|
|
594
|
+
if (!ok)
|
|
595
|
+
bad += 1;
|
|
596
|
+
};
|
|
597
|
+
say(Number(process.versions.node.split(".")[0]) >= 24, `Node ${process.versions.node}`);
|
|
598
|
+
if (!existsSync(CONFIG_FILE))
|
|
599
|
+
return say(false, "还没装好:运行 agent-autoloop init");
|
|
600
|
+
const config = readConfig();
|
|
601
|
+
if (config.hub) {
|
|
602
|
+
try {
|
|
603
|
+
const { channels } = checkRulesFile(RULES_FILE);
|
|
604
|
+
say(true, `规则 ${RULES_FILE}`);
|
|
605
|
+
for (const [id, channel] of Object.entries(channels)) {
|
|
606
|
+
// Nobody answers from a desktop notification, and a link goes only to whoever it's sent to.
|
|
607
|
+
if (id !== "desktop" && !channel.links && !channel.answerers) {
|
|
608
|
+
console.log(`○ ${channel.name}没写 answerers:这个通道里谁都能回答卡片。`);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
catch (error) {
|
|
613
|
+
say(false, error.message);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (serviceRunning())
|
|
617
|
+
say(true, "系统服务在跑");
|
|
618
|
+
else if (systemUnit())
|
|
619
|
+
say(true, "系统级服务在跑");
|
|
620
|
+
else
|
|
621
|
+
say(false, "系统服务没在跑:agent-autoloop service install");
|
|
622
|
+
try {
|
|
623
|
+
const health = await (await fetch(`${config.url}/healthz`, { signal: AbortSignal.timeout(5000) })).json();
|
|
624
|
+
say(health.ok === true, `中台 ${config.url}(${String(health.commit).slice(0, 12)})`);
|
|
625
|
+
}
|
|
626
|
+
catch (error) {
|
|
627
|
+
say(false, `连不上中台 ${config.url}:${error.cause?.code ?? error.message}`);
|
|
628
|
+
}
|
|
629
|
+
if (config.owner) {
|
|
630
|
+
try {
|
|
631
|
+
const state = await ownerClient(config).state();
|
|
632
|
+
for (const agent of state.agents) {
|
|
633
|
+
say(agent.online, `${agent.name}(${agent.worker})${agent.online ? "在线" : "不在线"}`);
|
|
634
|
+
if (agent.worker === config.worker?.name) {
|
|
635
|
+
const { adapterFor } = await import("../worker/agents.js");
|
|
636
|
+
const problem = await adapterFor(agent.use).then((adapter) => adapter.check?.(agent.program) ?? null, (error) => String(error.message));
|
|
637
|
+
say(!problem, problem ?? `${agent.name} 可以用`);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
for (const channel of state.channels) {
|
|
641
|
+
say(channel.online, `${channel.name}${channel.online ? "在线" : "不在线"}`);
|
|
642
|
+
const failure = channel.lastFailure;
|
|
643
|
+
if (!failure)
|
|
644
|
+
continue;
|
|
645
|
+
// Not "still retrying": a card closed before it got through is never tried again, though its failure stays the last.
|
|
646
|
+
const tried = `试了 ${failure.tries} 次${failure.givenUp ? ",已经不再重试" : ""}`;
|
|
647
|
+
say(false, `${channel.name}最近一张卡片(#${failure.card})没送到:${failure.error}(${tried})`);
|
|
648
|
+
}
|
|
649
|
+
const manual = path.join(ROOT, "skills", "agent-autoloop", "SKILL.md");
|
|
650
|
+
if (!state.channels.length) {
|
|
651
|
+
console.log(`○ 还没接通道:Agent 问你时不会主动告诉你,要自己运行 agent-autoloop status 看。接法见 ${manual}`);
|
|
652
|
+
}
|
|
653
|
+
else if (state.channels.length === 1 && state.channels[0].id === "desktop") {
|
|
654
|
+
console.log("○ 只接了桌面通知:Agent 问你时只有这台电脑会弹通知,人不在电脑前、或者电脑没有图形界面就看不到。" +
|
|
655
|
+
`要发到飞书、钉钉这些地方,接法见 ${manual}`);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
catch (error) {
|
|
659
|
+
say(false, `读不到中台状态:${error.message}`);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (bad)
|
|
663
|
+
process.exit(1);
|
|
664
|
+
}
|
|
665
|
+
/** Only reads the file: checking a draft must not open the hub's database or load the draft into it. */
|
|
666
|
+
function checkRulesFile(file) {
|
|
667
|
+
let json;
|
|
668
|
+
try {
|
|
669
|
+
json = readFileSync(file, "utf8");
|
|
670
|
+
}
|
|
671
|
+
catch (error) {
|
|
672
|
+
throw new Error(`读不到 ${file}:${error.code ?? error.message}`);
|
|
673
|
+
}
|
|
674
|
+
return parseRules(json, Date.now(), Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
675
|
+
}
|
|
676
|
+
function check(args) {
|
|
677
|
+
const file = path.resolve(args[0] ?? RULES_FILE);
|
|
678
|
+
checkRulesFile(file);
|
|
679
|
+
console.log(`规则没问题:${file}`);
|
|
680
|
+
}
|
|
681
|
+
async function key(args) {
|
|
682
|
+
const [action, kind, name] = args;
|
|
683
|
+
if (action === "remove") {
|
|
684
|
+
if (!kind || !name)
|
|
685
|
+
return fail("用法:agent-autoloop key remove <种类> <名字>");
|
|
686
|
+
const { changed } = await ownerClient().removeKey(kind, name);
|
|
687
|
+
console.log(changed ? "密钥已失效。" : "没有这把密钥,可能已经删过了。");
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const { addKey, listKeys } = await import("../host/runtime.js");
|
|
691
|
+
if (action === "list") {
|
|
692
|
+
for (const row of listKeys())
|
|
693
|
+
console.log(`${row.kind} ${row.name} ${row.created_at}`);
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
if (action !== "new" || !["source", "worker", "channel", "owner"].includes(kind) || !name) {
|
|
697
|
+
return fail("用法:agent-autoloop key new <source|worker|channel|owner> <名字>");
|
|
698
|
+
}
|
|
699
|
+
console.log(addKey(kind, name));
|
|
700
|
+
}
|
|
701
|
+
async function emit(args) {
|
|
702
|
+
const { named, rest } = flags(args);
|
|
703
|
+
const config = readConfig();
|
|
704
|
+
const type = rest[0] ?? fail(TAKES.emit.usage);
|
|
705
|
+
const event = {
|
|
706
|
+
id: text(named.id) ?? randomUUID(),
|
|
707
|
+
type,
|
|
708
|
+
...(text(named.subject) && { subject: text(named.subject) }),
|
|
709
|
+
...(text(named.summary) && { summary: text(named.summary) }),
|
|
710
|
+
...(text(named.detail) && { detail: text(named.detail) }),
|
|
711
|
+
...(text(named.link) && { link: text(named.link) }),
|
|
712
|
+
};
|
|
713
|
+
if (named["dry-run"] !== undefined) {
|
|
714
|
+
// The hub tells a source by its key, and only the key's own source is known here.
|
|
715
|
+
if (text(named.key))
|
|
716
|
+
return fail("--dry-run 按这台电脑的来源预演,不能和 --key 一起用");
|
|
717
|
+
if (!config.source)
|
|
718
|
+
return fail("这台电脑上没有来源的密钥:--dry-run 需要知道来源名");
|
|
719
|
+
const { previewEvent } = await import("./explain.js");
|
|
720
|
+
console.log("预演(按本机 rules.json,不连接中台、不写日志):");
|
|
721
|
+
for (const line of previewEvent(readFileSync(RULES_FILE, "utf8"), config.source.name, event))
|
|
722
|
+
console.log(line);
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
const secret = text(named.key) ?? config.source?.key ?? fail("这台电脑上没有来源的密钥:用 --key");
|
|
726
|
+
const answer = await client(secret, config).event(event);
|
|
727
|
+
const paused = answer.paused?.length ? `暂停中没接:${answer.paused.join("、")}` : "";
|
|
728
|
+
if (answer.routed.length)
|
|
729
|
+
console.log(`交给了:${answer.routed.join("、")}${paused && `;${paused}`}`);
|
|
730
|
+
else
|
|
731
|
+
console.log(paused ? `记下了,${paused}。` : "记下了,没有规则接它。");
|
|
732
|
+
}
|
|
733
|
+
async function backupCommand(args) {
|
|
734
|
+
const { rest } = flags(args);
|
|
735
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
|
|
736
|
+
const target = path.resolve(rest[0] ?? path.join(HOME, "backups", stamp));
|
|
737
|
+
const { backup } = await import("./backup.js");
|
|
738
|
+
const { files } = await backup(HOME, target);
|
|
739
|
+
console.log(`备份好了:${target}\n ${files.join("、")}\n里面有密钥,把它放到安全的地方;恢复要中台停下来。`);
|
|
740
|
+
}
|
|
741
|
+
async function restoreCommand(args) {
|
|
742
|
+
const { named, rest } = flags(args);
|
|
743
|
+
if (!rest[0])
|
|
744
|
+
return fail(RESTORE_USAGE);
|
|
745
|
+
if (named.yes === undefined) {
|
|
746
|
+
if (!process.stdin.isTTY)
|
|
747
|
+
return fail("恢复会替换本机数据;确认后运行 agent-autoloop restore <目录> --yes");
|
|
748
|
+
const answer = await askAtTerminal("恢复会替换本机的数据库、主密钥、配置、规则和 secrets.env,继续?[y/N] ");
|
|
749
|
+
if (!/^y(es)?$/i.test(answer.trim())) {
|
|
750
|
+
console.log("没有恢复。");
|
|
751
|
+
return;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
const { restore } = await import("./backup.js");
|
|
755
|
+
const { safety } = await restore(HOME, path.resolve(rest[0]));
|
|
756
|
+
console.log(`恢复好了。原来的文件在 ${safety}。\n启动:agent-autoloop up 或 agent-autoloop service install`);
|
|
757
|
+
}
|
|
758
|
+
async function rulesCommand(args) {
|
|
759
|
+
const { rest } = flags(args);
|
|
760
|
+
if (rest[0] !== "explain" || rest.length > 2)
|
|
761
|
+
return fail(TAKES.rules.usage);
|
|
762
|
+
const file = rest[1] ? path.resolve(rest[1]) : RULES_FILE;
|
|
763
|
+
let text;
|
|
764
|
+
try {
|
|
765
|
+
text = readFileSync(file, "utf8");
|
|
766
|
+
}
|
|
767
|
+
catch (error) {
|
|
768
|
+
return fail(`读不到 ${file}:${error.message}`);
|
|
769
|
+
}
|
|
770
|
+
const { explainRules } = await import("./explain.js");
|
|
771
|
+
for (const line of explainRules(text, Date.now()))
|
|
772
|
+
console.log(line);
|
|
773
|
+
}
|
|
774
|
+
async function tell(args) {
|
|
775
|
+
const { named, rest } = flags(args);
|
|
776
|
+
const config = readConfig();
|
|
777
|
+
const matter = rest[0] ?? fail(TAKES.tell.usage);
|
|
778
|
+
const words = readFileSync(0, "utf8").trim();
|
|
779
|
+
if (!words)
|
|
780
|
+
return fail("要说的话从标准输入传,不能为空。");
|
|
781
|
+
const task = text(named.task);
|
|
782
|
+
const secret = task ? config.worker?.key : config.source?.key;
|
|
783
|
+
if (!secret)
|
|
784
|
+
return fail(task ? "这台电脑不是接单程序" : "这台电脑上没有来源的密钥");
|
|
785
|
+
const answer = await client(secret, config).tell({ key: matter, text: words, ...(task && { task }) });
|
|
786
|
+
console.log(answer.duplicate ? "这件事已经说过了,这次没再发。" : "说了。");
|
|
787
|
+
}
|
|
788
|
+
async function ask(args) {
|
|
789
|
+
const { named, rest } = flags(args);
|
|
790
|
+
const task = text(named.task);
|
|
791
|
+
if (!task || rest.length)
|
|
792
|
+
return fail(TAKES.ask.usage);
|
|
793
|
+
let question;
|
|
794
|
+
try {
|
|
795
|
+
question = JSON.parse(readFileSync(0, "utf8"));
|
|
796
|
+
}
|
|
797
|
+
catch {
|
|
798
|
+
return fail("问题从标准输入传,是一段 JSON:和回执里的 ask 一样");
|
|
799
|
+
}
|
|
800
|
+
const config = readConfig();
|
|
801
|
+
if (!config.worker)
|
|
802
|
+
return fail("这台电脑不是接单程序");
|
|
803
|
+
const answer = await client(config.worker.key, config).tell({ task, ask: question });
|
|
804
|
+
console.log(answer.duplicate ? "同一件事还在等你回答,这次没再问。" : "问了。");
|
|
805
|
+
}
|
|
806
|
+
/** A run is named by its number; anything else would reach the hub as /v1/runs/NaN and come back as "没有这个地址". */
|
|
807
|
+
function runOf(word, usage) {
|
|
808
|
+
const id = Number(word);
|
|
809
|
+
if (word === undefined || !/^[1-9][0-9]*$/.test(word) || !Number.isSafeInteger(id))
|
|
810
|
+
return fail(usage);
|
|
811
|
+
return id;
|
|
812
|
+
}
|
|
813
|
+
async function why(args) {
|
|
814
|
+
const id = runOf(args.find((arg) => !arg.startsWith("--")), "用法:agent-autoloop why <第几轮> [--json]");
|
|
815
|
+
const facts = await ownerClient().why(id);
|
|
816
|
+
if (args.includes("--json"))
|
|
817
|
+
return console.log(JSON.stringify(facts, null, 2));
|
|
818
|
+
const { formatWhy } = await import("./why.js");
|
|
819
|
+
console.log(formatWhy(id, facts));
|
|
820
|
+
}
|
|
821
|
+
async function checkConformance(args) {
|
|
822
|
+
const { named, rest } = flags(args);
|
|
823
|
+
const file = text(named.config) ?? process.env.AGENT_AUTOLOOP_CONFORMANCE;
|
|
824
|
+
if (!rest[0] || !file)
|
|
825
|
+
return fail(TAKES.conformance.usage);
|
|
826
|
+
const { conformance } = await import("../protocol/conformance.js");
|
|
827
|
+
console.log(JSON.stringify(await conformance(rest[0], JSON.parse(readFileSync(file, "utf8"))), null, 2));
|
|
828
|
+
}
|
|
829
|
+
async function stop(args) {
|
|
830
|
+
const id = runOf(args[0], "用法:agent-autoloop stop <第几轮>");
|
|
831
|
+
await ownerClient().cancel(id);
|
|
832
|
+
console.log("通知它停了。");
|
|
833
|
+
}
|
|
834
|
+
/** A command run right after starting the service would find no hub yet: this machine's is waited for, if it has one. */
|
|
835
|
+
async function waitForHub() {
|
|
836
|
+
const config = readConfig();
|
|
837
|
+
if (!config.hub)
|
|
838
|
+
return;
|
|
839
|
+
const answering = await untilAnswering(config.url);
|
|
840
|
+
if (answering === false) {
|
|
841
|
+
const log = serviceLogs();
|
|
842
|
+
const last = existsSync(log) ? readFileSync(log, "utf8").trimEnd().split("\n").slice(-5).join("\n") : "";
|
|
843
|
+
console.error(`系统服务起了,但中台 20 秒内没有应答。看看日志:${log}${last ? `\n\n${last}` : ""}`);
|
|
844
|
+
}
|
|
845
|
+
// A login task asks for the user's limited token, and the built-in Administrator gets the full one all the same.
|
|
846
|
+
const hub = answering && process.platform === "win32" ? runningUp() : null;
|
|
847
|
+
if (hub !== null && aboveDesktop(hub)) {
|
|
848
|
+
removeService();
|
|
849
|
+
fail("登录任务拉起的中台拿到了比你的桌面高的完整管理员权限,它派的每个 Agent 也会带着,所以没有装这个任务" +
|
|
850
|
+
`(Windows 自带的 Administrator 账号会这样)。${startupInstead()}`);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
/** fetch says only "fetch failed" when no hub answers; which hub, why and what to do are what a person can act on. */
|
|
854
|
+
function plainly(error) {
|
|
855
|
+
if (!(error instanceof TypeError && error.message === "fetch failed"))
|
|
856
|
+
return error?.message ?? String(error);
|
|
857
|
+
let url = "";
|
|
858
|
+
try {
|
|
859
|
+
url = readConfig().url;
|
|
860
|
+
}
|
|
861
|
+
catch { }
|
|
862
|
+
const cause = error.cause;
|
|
863
|
+
const why = cause?.code ?? cause?.message;
|
|
864
|
+
return (`连不上中台 ${url}${why ? `(${why})` : ""}:中台没在运行,或者刚启动还没好。` +
|
|
865
|
+
"在中台那台电脑上运行 agent-autoloop service install 启动它;刚装好、刚更新的等几秒再试。");
|
|
866
|
+
}
|
|
867
|
+
async function service(args) {
|
|
868
|
+
if (args[0] === "install") {
|
|
869
|
+
// Installing over kept data comes here and not to init, and uninstalling took the links away.
|
|
870
|
+
await prepareManagedInstall();
|
|
871
|
+
linkSkill();
|
|
872
|
+
installService();
|
|
873
|
+
return waitForHub();
|
|
874
|
+
}
|
|
875
|
+
if (args[0] === "remove")
|
|
876
|
+
return removeService();
|
|
877
|
+
fail(TAKES.service.usage);
|
|
878
|
+
}
|
|
879
|
+
/** A plugin is a folder of its own under this install, named after its repository. */
|
|
880
|
+
function addPlugin(args) {
|
|
881
|
+
const url = args[0] ?? fail(TAKES.add.usage);
|
|
882
|
+
const name = url
|
|
883
|
+
.replace(/\/$/, "")
|
|
884
|
+
.split(/[/:]/)
|
|
885
|
+
.at(-1)
|
|
886
|
+
.replace(/\.git$/, "")
|
|
887
|
+
.toLowerCase();
|
|
888
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name))
|
|
889
|
+
fail(`插件名字「${name}」不能用:只能用小写字母、数字和 -,规则里的 use 也写这个名字`);
|
|
890
|
+
mkdirSync(path.join(HOME, "plugins"), { recursive: true });
|
|
891
|
+
const clone = spawnSync("git", ["clone", "--", url, path.join(HOME, "plugins", name)], { stdio: "inherit" });
|
|
892
|
+
if (clone.status !== 0)
|
|
893
|
+
fail("安装失败。");
|
|
894
|
+
console.log(`已安装 ${name}。`);
|
|
895
|
+
}
|
|
896
|
+
/** What the hub is doing right now; a hub that isn't answering has nothing to cut short. */
|
|
897
|
+
async function activeRunCount() {
|
|
898
|
+
try {
|
|
899
|
+
const state = await ownerClient().state();
|
|
900
|
+
return state.runs.filter((run) => ["queued", "claimed", "unknown"].includes(run.state)).length;
|
|
901
|
+
}
|
|
902
|
+
catch {
|
|
903
|
+
return 0;
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
async function update() {
|
|
907
|
+
const { managedAt, prepareManaged, stageLatest } = await import("../host/update.js");
|
|
908
|
+
// Installed before versions were unpacked: update the npm package, then put the new files under the launcher.
|
|
909
|
+
if (!managedAt(HOME)) {
|
|
910
|
+
const { updateCode } = await import("./update.js");
|
|
911
|
+
const { changed, to } = updateCode(ROOT);
|
|
912
|
+
if (changed) {
|
|
913
|
+
let managed = false;
|
|
914
|
+
try {
|
|
915
|
+
managed = prepareManaged(ROOT) !== null;
|
|
916
|
+
}
|
|
917
|
+
catch (error) {
|
|
918
|
+
console.log(`自动更新没准备好:${error.message}`);
|
|
919
|
+
}
|
|
920
|
+
if (managed) {
|
|
921
|
+
installService();
|
|
922
|
+
await waitForHub();
|
|
923
|
+
return console.log(`更新好了(${to}),服务已从新副本启动。`);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
const version = to ? `(${to})` : "";
|
|
927
|
+
if (!changed)
|
|
928
|
+
return console.log(`已经是最新的${version}。`);
|
|
929
|
+
const told = restartService();
|
|
930
|
+
if (told === undefined)
|
|
931
|
+
await waitForHub();
|
|
932
|
+
console.log(`更新好了${version}。${told ?? ""}`);
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const running = await activeRunCount();
|
|
936
|
+
if (running > 0)
|
|
937
|
+
return fail(`还有 ${running} 轮在跑,等它们做完再更新。`);
|
|
938
|
+
const result = stageLatest();
|
|
939
|
+
if (result.status === "current")
|
|
940
|
+
return console.log(`已经是最新的(v${result.version})。`);
|
|
941
|
+
if (result.status === "pending")
|
|
942
|
+
return console.log(`v${result.version} 已经准备好,中台重启时换上。`);
|
|
943
|
+
if (result.status === "bad")
|
|
944
|
+
return fail(`v${result.version} 之前启动失败过,先不装它;修好后删掉 ${path.join(HOME, "update", "bad.json")} 再试。`);
|
|
945
|
+
if (result.status === "busy")
|
|
946
|
+
return fail("另一个更新正在准备,过一会儿再看。");
|
|
947
|
+
if (result.status === "unmanaged")
|
|
948
|
+
return fail("这个副本不在受管理的位置里,没法按版本更新。");
|
|
949
|
+
const told = restartService();
|
|
950
|
+
const service = serviceRunning();
|
|
951
|
+
if (told === undefined && service)
|
|
952
|
+
await waitForHub();
|
|
953
|
+
console.log(`更新好了(v${result.version})。${told ?? (service ? "" : "下次启动中台时换上。")}`);
|
|
954
|
+
}
|
|
955
|
+
async function askAtTerminal(question) {
|
|
956
|
+
const terminal = createInterface({ input: process.stdin, output: process.stdout });
|
|
957
|
+
const answer = await terminal.question(question);
|
|
958
|
+
terminal.close();
|
|
959
|
+
return answer;
|
|
960
|
+
}
|
|
961
|
+
const [command, ...args] = process.argv.slice(2);
|
|
962
|
+
const commands = {
|
|
963
|
+
mcp: async () => (await import("./mcp.js")).mcp(),
|
|
964
|
+
add: () => addPlugin(args),
|
|
965
|
+
init: () => init(args),
|
|
966
|
+
join: () => join(args),
|
|
967
|
+
up: async () => (await import("../host/up.js")).up(),
|
|
968
|
+
try: () => tryIt(),
|
|
969
|
+
status: () => status(flags(args).named),
|
|
970
|
+
why: () => why(args),
|
|
971
|
+
runs: () => runs(args),
|
|
972
|
+
conformance: () => checkConformance(args),
|
|
973
|
+
answer: () => answer(args),
|
|
974
|
+
resolve: () => resolve(args),
|
|
975
|
+
doctor,
|
|
976
|
+
check: () => check(args),
|
|
977
|
+
rules: () => rulesCommand(args),
|
|
978
|
+
pause: () => pause(args),
|
|
979
|
+
resume: () => resume(args),
|
|
980
|
+
replay: () => replay(args),
|
|
981
|
+
run: () => runNow(args),
|
|
982
|
+
key: () => key(args),
|
|
983
|
+
emit: () => emit(args),
|
|
984
|
+
tell: () => tell(args),
|
|
985
|
+
ask: () => ask(args),
|
|
986
|
+
stop: () => stop(args),
|
|
987
|
+
backup: () => backupCommand(args),
|
|
988
|
+
restore: () => restoreCommand(args),
|
|
989
|
+
update,
|
|
990
|
+
"stage-update": async () => {
|
|
991
|
+
const { stageLatest } = await import("../host/update.js");
|
|
992
|
+
const result = stageLatest();
|
|
993
|
+
console.log(`${new Date().toISOString()} ${JSON.stringify(result)}`);
|
|
994
|
+
},
|
|
995
|
+
uninstall: async () => {
|
|
996
|
+
const { uninstall } = await import("./uninstall.js");
|
|
997
|
+
await uninstall(flags(args).named.yes !== undefined, process.stdin.isTTY ? askAtTerminal : null);
|
|
998
|
+
},
|
|
999
|
+
service: () => service(args),
|
|
1000
|
+
help: () => console.log(HELP),
|
|
1001
|
+
"--help": () => console.log(HELP),
|
|
1002
|
+
"-h": () => console.log(HELP),
|
|
1003
|
+
"--version": () => console.log(versionLine()),
|
|
1004
|
+
};
|
|
1005
|
+
if (!command || !commands[command]) {
|
|
1006
|
+
console.log(HELP);
|
|
1007
|
+
if (command)
|
|
1008
|
+
process.exit(1);
|
|
1009
|
+
}
|
|
1010
|
+
else {
|
|
1011
|
+
if (TAKES[command])
|
|
1012
|
+
refuseUnknownFlags(command === "resolve" ? args.slice(0, 1) : args, TAKES[command]);
|
|
1013
|
+
Promise.resolve()
|
|
1014
|
+
.then(commands[command])
|
|
1015
|
+
.catch((error) => fail(plainly(error)));
|
|
1016
|
+
}
|