@saltpub/agent-autoloop 0.1.2 → 0.1.3
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 +8 -0
- package/INSTALL.md +3 -1
- package/README.en.md +5 -1
- package/README.md +5 -1
- package/SECURITY.md +1 -0
- package/dist/cli/agents.js +14 -0
- package/dist/cli/dashboard/app.js +424 -0
- package/dist/cli/dashboard/index.html +15 -0
- package/dist/cli/dashboard/style.css +629 -0
- package/dist/cli/dashboard.js +94 -0
- package/dist/cli/main.js +209 -83
- package/dist/cli/status.js +13 -45
- package/dist/cli/ui.js +75 -0
- package/dist/cli/uninstall.js +3 -1
- package/dist/host/http.js +2 -0
- package/docs/protocol.md +1 -1
- package/package.json +3 -1
- package/skills/agent-autoloop/SKILL.md +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @saltpub/agent-autoloop
|
|
2
2
|
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 4be35b8: `init` 会检测本机装好的全部内置 Agent(Claude Code、Codex、OpenCode、pi、OMP、DeepSeek Harness、ZCode),`--agent` 也可以只接其中一个。终端输出重新排版:`help` 按用途分组、命令与说明对齐;`doctor` 分成这台电脑、Agent、通道三段,在线与「可以用」合成一行;`status` 每段上方一条分隔线,末尾给出 `why`/`runs`/`doctor`;`init` 的安装结果收在一个框里,下面直接列出下一步;`try` 等待时在终端上显示已等多少秒。
|
|
8
|
+
- 3dd1610: 新增 `agent-autoloop dashboard`:在本机开一个只读看板,一屏看规则、最近 24 小时、在跑、排队、待你的卡片和今天跑完的轮次,顶上四个数点开是明细,任意一轮能看它的来龙去脉;跟随 `/v1/stream` 实时更新。它只发 `GET /v1/state`、`GET /v1/stream`、`GET /v1/runs/<id>/why`,不进内核、不加依赖、不写日志。中台的流在还没有事实可发时也会先回响应头,页面不用等到第一条事实才知道连上了。
|
|
9
|
+
- e0c52a8: 重装后运行 `init` 会提示改用 `agent-autoloop service install` 接着用原来的规则和记录;卸载完也提示这条命令,不再只留下「已经装过了」让人以为装坏了。
|
|
10
|
+
|
|
3
11
|
## 0.1.2
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
package/INSTALL.md
CHANGED
|
@@ -37,11 +37,13 @@ npm install -g @saltpub/agent-autoloop
|
|
|
37
37
|
|
|
38
38
|
再把 `$HOME/.local/bin` 加进 PATH。
|
|
39
39
|
|
|
40
|
+
已经装过一次(`~/.agent-autoloop/config.json` 还在,比如重装、换包名、或把数据目录拷到新机器)就运行 `agent-autoloop service install`,它会接上原来的规则、密钥和记录;`init` 只在第一次装的时候用,见到这个文件会拒绝并提示这条命令。
|
|
41
|
+
|
|
40
42
|
GitHub Issue 轮询和 PR 交付等功能另需已登录的 `gh`,按对应场景说明配置;运行 Agent 也仍需该 Agent 自己的登录或授权。中台在后台启动 Agent,桌面应用里能用的浏览器和电脑操作它不一定用得上(比如 Codex 应用里的 Chrome,后台的 `codex exec` 用不到);要它操作网页,照[浏览器接入](docs/plugins.md#浏览器接入)配好,再用后台的启动方式实际跑一次。
|
|
41
43
|
|
|
42
44
|
安装和 `agent-autoloop update` 都装 npm 上的最新版本,那是通过了全部检查的版本,`main` 上还没发布的改动要等下一次发布。装好以后自动检查更新,过程、回退和关闭方法见[自动更新](docs/auto-update.md)。
|
|
43
45
|
|
|
44
|
-
`--agent
|
|
46
|
+
`--agent <名字>` 只接一个 Agent:`claude`、`codex`、`opencode`、`pi`、`omp`、`dsh`、`zcode`;只有其他 CLI 时给 `init` 加 `--command 'hermes chat -Q -q {prompt}'`,带空格的路径用引号括起来;找不到这个程序时不装。不指定时会检测本机装好的全部 Agent,第一种作为试跑对象。
|
|
45
47
|
|
|
46
48
|
程序由 npm 装进它的全局目录(`npm prefix -g`),配置和记录留在 `~/.agent-autoloop`。默认打开桌面通知;没有图形会话时安静跳过,问题仍能用 `agent-autoloop status` 查看。
|
|
47
49
|
|
package/README.en.md
CHANGED
|
@@ -16,7 +16,7 @@ Most of us use command-line agents like Claude Code or Codex by babysitting a te
|
|
|
16
16
|
|
|
17
17
|
- **One machine, one install command.** Runs on Windows, macOS, or Linux. No cloud server or inbound ports required.
|
|
18
18
|
- **Connect a new agent in one line.** No SDK, no agent source code to fork. Any CLI tool that takes a prompt and prints a final reply can plug in.
|
|
19
|
-
- **
|
|
19
|
+
- **The hub doesn't need a UI.** Cards reach you on Feishu, DingTalk, WeCom, Slack, Discord, or desktop notifications when you are needed. Everything else is one command away, or a look at the read-only `agent-autoloop dashboard`.
|
|
20
20
|
|
|
21
21
|
## Get started
|
|
22
22
|
|
|
@@ -99,6 +99,10 @@ External events work the same way once the source is connected. To hand new GitH
|
|
|
99
99
|
|
|
100
100
|
Changes take effect within seconds of saving. Run `agent-autoloop check` to validate, `agent-autoloop check <file>` to validate a draft without touching the running hub, `agent-autoloop rules explain [file]` to see the rules in effect and their next runs (a draft file too), and `agent-autoloop emit <type> --dry-run` to preview who an event would go to (it doesn't contact the hub or write to the log). If you make a mistake, or the file is deleted or can't be read, the hub keeps running the previous valid rules and sends you a card about it.
|
|
101
101
|
|
|
102
|
+
### See what's going on
|
|
103
|
+
|
|
104
|
+
`agent-autoloop dashboard` serves a read-only board on this machine (`http://127.0.0.1:3351` by default; `--port` to move it, `--no-open` to keep the browser shut): rules and the last 24 hours on the left, what's coming and what just finished on the right. The four counts on top — running, queued, waiting on you, finished today — open a detail list, and any round opens its full story. It sends only `GET /v1/state`, `GET /v1/stream`, and `GET /v1/runs/<id>/why`, follows the log live, and the hub keeps running if you close it.
|
|
105
|
+
|
|
102
106
|
### How agents talk back to the hub
|
|
103
107
|
|
|
104
108
|
The hub asks only one thing of an agent: **put your result in your final message**. When the job is done, a one-line conclusion is enough. To ask you something, the agent appends an `agent-autoloop` block:
|
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
|
|
17
17
|
- **一台电脑、一条命令就能跑。** 装在你的 Windows、Mac 或 Linux 上,不用买云服务器,也不用开放公网端口。
|
|
18
18
|
- **一行配置接新 Agent。** 不需要 SDK,也不需要改 Agent 源码,能接收提示词并打印最后一条回复的命令行工具都能接。
|
|
19
|
-
-
|
|
19
|
+
- **后台不靠界面。** 需要你拍板时把卡片推到飞书、钉钉、企微、Slack、Discord 或桌面通知;想看全局进展,敲一行命令,或者 `agent-autoloop dashboard` 在本机开一个只读看板。
|
|
20
20
|
|
|
21
21
|
## 上手
|
|
22
22
|
|
|
@@ -97,6 +97,10 @@ Agent 结束了这一轮,问了你一个问题。它现在不占进程、不
|
|
|
97
97
|
|
|
98
98
|
保存后几秒内自动生效,可以运行 `agent-autoloop check` 检查,`agent-autoloop check <文件>` 检查一份草稿,不碰正在跑的中台;`agent-autoloop rules explain [文件]` 看生效的规则和下次定时(也可以指定一份草稿),`agent-autoloop emit <类型> --dry-run` 预演一条事件会派给谁(不连接中台、不写日志)。就算改错了,或者文件被删、读不到,系统也会继续按旧规则跑,并发卡片提醒你。
|
|
99
99
|
|
|
100
|
+
### 看现在的情况
|
|
101
|
+
|
|
102
|
+
`agent-autoloop dashboard` 在这台电脑上开一个只读看板(默认 `http://127.0.0.1:3351`,`--port` 换端口,`--no-open` 不打开浏览器):左边是规则和最近 24 小时的时间轴,右边是接下去要跑的、最近跑完的;顶上四个数是“在跑”“排队”“待你”“今天跑完”,点开是明细,点任意一轮能看它的来龙去脉。它只发 `GET /v1/state`、`GET /v1/stream`、`GET /v1/runs/<id>/why` 三个请求,靠日志实时更新,关掉它不影响中台。
|
|
103
|
+
|
|
100
104
|
### Agent 怎么跟中台配合?
|
|
101
105
|
|
|
102
106
|
中台对 Agent 只有一个约定:**在最后一条回复里交代结果**。事情办完了,正常输出一句结论就行。需要向你请示时,在回复末尾附一段 `agent-autoloop` 代码块:
|
package/SECURITY.md
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
- `access` 由专用 Agent 插件映射到 CLI 权限;`command` 不替任意程序限制权限。普通 Agent 的 `read` 不等于读不到这台电脑上的密钥。要隔离密钥,用单独的系统账号或容器。
|
|
15
15
|
- 普通访问密钥只存 SHA-256,另有本机 `master.key` 推导轮次和卡片链接的 token。`master.key` 为空或不完整时中台拒绝启动,不会拿它签链接,也不会自己换一把新的。接单程序不通过提示词、资料或环境变量传递中台凭据;同一系统账号下能读哪些文件仍由 Agent 沙箱决定。回执能影响的范围见 `docs/protocol.md`。
|
|
16
16
|
- 卡片链接谁拿到谁就能回答这一张卡片,从卡片创建起 7 天内有效,过期后也不再显示卡片内容,只发给配了 `"links": true` 的通道。
|
|
17
|
+
- `agent-autoloop dashboard` 只听 127.0.0.1,只把 `/v1/state`、`/v1/stream`、`/v1/runs/<id>/why` 连同本人密钥转给中台,密钥不进页面;只认发给 `127.0.0.1:<端口>` 和 `localhost:<端口>` 的请求,别的主机名(包括被解析到本机的域名)一律拒绝,别的方法一律拒绝。它只读,不写日志、不回答问题。
|
|
17
18
|
- 本项目只启动你自己安装、自己登录的官方命令行工具,不接触、不保存、不中转它们的登录凭据。
|
|
18
19
|
- 浏览器能力由 Agent 自己的工具配置提供;专用入口只从可信规则或 Skill 取得,不从事件正文取命令。中台不自动授予浏览器权限或接管桌面会话。需本人恢复的连接或验证用 `ask` 交回,保留未验收项。
|
|
19
20
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// What `init` looks for on this machine, in the order it prefers them, and what each is called in front of a person.
|
|
2
|
+
// `command`, `acp` and `delivery` ship as plugins too but only work with a line of their own in rules.json, so they're
|
|
3
|
+
// not probed. tests/agents.test.ts keeps this list equal to the agent plugins that need nothing but being installed.
|
|
4
|
+
export const BUILTIN_AGENTS = [
|
|
5
|
+
{ use: "claude", name: "Claude Code" },
|
|
6
|
+
{ use: "codex", name: "Codex" },
|
|
7
|
+
{ use: "opencode", name: "OpenCode" },
|
|
8
|
+
{ use: "pi", name: "pi" },
|
|
9
|
+
{ use: "omp", name: "OMP" },
|
|
10
|
+
{ use: "dsh", name: "DeepSeek Harness" },
|
|
11
|
+
{ use: "zcode", name: "ZCode" },
|
|
12
|
+
];
|
|
13
|
+
export const AGENT_NAMES = BUILTIN_AGENTS.map((agent) => agent.use).join("、");
|
|
14
|
+
export const AGENT_TITLES = BUILTIN_AGENTS.map((agent) => agent.name).join("、");
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
// The owner's page: a 24-hour rail of what ran, the rules beside it, and a sheet with every item behind each count.
|
|
2
|
+
// It reads only: /api/state for the picture, /api/stream to redraw when something happens, /api/why/<轮> for one run's
|
|
3
|
+
// timeline. Answering a card still goes through a channel or `agent-autoloop answer`.
|
|
4
|
+
const STATE_WORD = { queued: "排队", claimed: "在跑", unknown: "失联", done: "跑完", nothing: "无事" };
|
|
5
|
+
const root = document.getElementById("root");
|
|
6
|
+
const sheet = document.getElementById("sheet");
|
|
7
|
+
let snapshot = null;
|
|
8
|
+
let bucket = null;
|
|
9
|
+
let failure = "";
|
|
10
|
+
let live = "starting";
|
|
11
|
+
let lastSeq = 0;
|
|
12
|
+
const timelines = new Map();
|
|
13
|
+
|
|
14
|
+
const esc = (value) =>
|
|
15
|
+
String(value ?? "").replace(
|
|
16
|
+
/[&<>"']/g,
|
|
17
|
+
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c],
|
|
18
|
+
);
|
|
19
|
+
const num = new Intl.NumberFormat("zh-CN");
|
|
20
|
+
const zone = () => snapshot?.timezone || undefined;
|
|
21
|
+
const clock = (iso) =>
|
|
22
|
+
iso
|
|
23
|
+
? new Date(iso).toLocaleTimeString("zh-CN", { timeZone: zone(), hour: "2-digit", minute: "2-digit", hourCycle: "h23" })
|
|
24
|
+
: "";
|
|
25
|
+
const monthDay = (iso) => new Date(iso).toLocaleDateString("zh-CN", { timeZone: zone(), month: "long", day: "numeric" });
|
|
26
|
+
const weekday = (iso) => new Date(iso).toLocaleDateString("zh-CN", { timeZone: zone(), weekday: "long" });
|
|
27
|
+
const stamp = (iso) => `${monthDay(iso)} ${weekday(iso)} ${clock(iso)}`;
|
|
28
|
+
const dayOf = (iso) => new Date(iso).toLocaleDateString("zh-CN", { timeZone: zone(), month: "numeric", day: "numeric" });
|
|
29
|
+
const tokens = (usage) => (usage ? (usage.input ?? 0) + (usage.output ?? 0) : 0);
|
|
30
|
+
|
|
31
|
+
function span(ms) {
|
|
32
|
+
if (!(ms >= 0)) return "";
|
|
33
|
+
const total = Math.round(ms / 1000);
|
|
34
|
+
if (total < 60) return `${total} 秒`;
|
|
35
|
+
const minutes = Math.floor(total / 60);
|
|
36
|
+
const hours = Math.floor(minutes / 60);
|
|
37
|
+
if (hours) return `${hours} 小时 ${minutes % 60} 分`;
|
|
38
|
+
return `${minutes} 分 ${String(total % 60).padStart(2, "0")} 秒`;
|
|
39
|
+
}
|
|
40
|
+
const runSpan = (run) => span(Date.parse(run.finishedAt ?? snapshot.now) - Date.parse(run.claimedAt ?? run.createdAt));
|
|
41
|
+
|
|
42
|
+
/** What needs a person: open cards first, then things that stopped working. */
|
|
43
|
+
function alertsOf(state) {
|
|
44
|
+
const list = [];
|
|
45
|
+
for (const card of state.cards) {
|
|
46
|
+
list.push({
|
|
47
|
+
kind: "card",
|
|
48
|
+
level: card.kind === "ask" ? "wait" : "note",
|
|
49
|
+
title: card.title,
|
|
50
|
+
detail: card.kind === "ask" ? `等你回答 · ${card.rule}` : `没处理的提醒 · ${card.rule}`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
for (const agent of state.agents) {
|
|
54
|
+
if (!agent.online) list.push({ kind: "agent", level: "bad", title: `${agent.name} 没连上`, detail: `接单程序 ${agent.worker}` });
|
|
55
|
+
}
|
|
56
|
+
for (const channel of state.channels) {
|
|
57
|
+
if (!channel.online) list.push({ kind: "channel", level: "bad", title: `${channel.name} 没连上`, detail: "卡片发不出去" });
|
|
58
|
+
if (channel.lastFailure) {
|
|
59
|
+
const { card, tries, error, givenUp } = channel.lastFailure;
|
|
60
|
+
list.push({
|
|
61
|
+
kind: "channel",
|
|
62
|
+
level: givenUp ? "bad" : "warn",
|
|
63
|
+
title: `${channel.name} 第 ${card} 张卡片${givenUp ? "放弃投递" : "没发出去"}`,
|
|
64
|
+
detail: `试了 ${tries} 次:${error}`,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const line of state.lines) {
|
|
69
|
+
if (line.paused) list.push({ kind: "rule", level: "bad", title: `规则「${line.name}」停着`, detail: `${line.paused.by}:${line.paused.reason}` });
|
|
70
|
+
}
|
|
71
|
+
for (const run of state.runs) {
|
|
72
|
+
if (run.state === "unknown") list.push({ kind: "run", level: "bad", title: `第 ${run.id} 轮失联`, detail: `${run.rule} · ${run.agent}` });
|
|
73
|
+
else if (run.state === "queued" && run.attempts > 0)
|
|
74
|
+
list.push({ kind: "run", level: "warn", title: `第 ${run.id} 轮在重排`, detail: `${run.rule} 失败 ${run.attempts} 次` });
|
|
75
|
+
}
|
|
76
|
+
if (state.unrouted.length)
|
|
77
|
+
list.push({
|
|
78
|
+
kind: "event",
|
|
79
|
+
level: "note",
|
|
80
|
+
title: `${state.unrouted.length} 条事件没有规则要`,
|
|
81
|
+
detail: state.unrouted.slice(0, 3).map((event) => event.type).join("、"),
|
|
82
|
+
});
|
|
83
|
+
return list;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function timetable(state) {
|
|
87
|
+
const now = Date.parse(state.now);
|
|
88
|
+
const from = now - 86400e3;
|
|
89
|
+
const windowMs = now - from;
|
|
90
|
+
const runs = state.runs
|
|
91
|
+
.filter((run) => Date.parse(run.createdAt) >= from)
|
|
92
|
+
.sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt));
|
|
93
|
+
|
|
94
|
+
const PX_PER_HOUR = 27;
|
|
95
|
+
const height = (windowMs / 3600e3) * PX_PER_HOUR;
|
|
96
|
+
const yOf = (t) => ((t - from) / windowMs) * height;
|
|
97
|
+
|
|
98
|
+
let cursor = -Infinity;
|
|
99
|
+
const marks = runs.map((run) => {
|
|
100
|
+
const y = Math.max(yOf(Date.parse(run.createdAt)), cursor + 62);
|
|
101
|
+
cursor = y;
|
|
102
|
+
return { run, y };
|
|
103
|
+
});
|
|
104
|
+
const track = Math.max(height, cursor + 40);
|
|
105
|
+
|
|
106
|
+
const ticks = [];
|
|
107
|
+
for (let t = Math.ceil(from / 3600e3) * 3600e3; t <= now; t += 3600e3) {
|
|
108
|
+
const hour = Number(clock(new Date(t).toISOString()).slice(0, 2));
|
|
109
|
+
if (hour % 3 === 0) ticks.push(`<div class="a-tick" style="top:${yOf(t)}px"></div>`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const marksHtml = marks
|
|
113
|
+
.map(({ run, y }) => {
|
|
114
|
+
const label = `${STATE_WORD[run.state] ?? run.state}${run.agent ? " · " + run.agent : ""}`;
|
|
115
|
+
const detail = run.error || run.summary || (run.events[0] ? `${run.events[0].source} · ${run.events[0].type}` : "");
|
|
116
|
+
return `<div class="a-mark ${run.state}" style="top:${y}px">
|
|
117
|
+
<span class="a-time">${clock(run.createdAt)}</span>
|
|
118
|
+
<span class="a-dot"></span>
|
|
119
|
+
<div class="a-label">
|
|
120
|
+
<b>${esc(run.rule)}</b>
|
|
121
|
+
<em>${esc(label)}${run.finishedAt ? " · " + runSpan(run) : ""}</em>
|
|
122
|
+
${detail ? `<p>${esc(detail).slice(0, 120)}</p>` : ""}
|
|
123
|
+
</div>
|
|
124
|
+
</div>`;
|
|
125
|
+
})
|
|
126
|
+
.join("");
|
|
127
|
+
|
|
128
|
+
const lines = [...state.lines].sort((a, b) => {
|
|
129
|
+
if (a.paused && !b.paused) return -1;
|
|
130
|
+
if (b.paused && !a.paused) return 1;
|
|
131
|
+
return (a.next ? Date.parse(a.next) : Infinity) - (b.next ? Date.parse(b.next) : Infinity);
|
|
132
|
+
});
|
|
133
|
+
const ruleRows = lines
|
|
134
|
+
.map((line) => {
|
|
135
|
+
const busy = line.totals.running > 0 ? "live" : line.totals.queued > 0 ? "queued" : line.paused ? "paused" : "";
|
|
136
|
+
return `<li class="a-rule ${busy}">
|
|
137
|
+
<span class="a-lamp"></span>
|
|
138
|
+
<div>
|
|
139
|
+
<b>${esc(line.name)}</b>
|
|
140
|
+
<span>${line.every ? esc(line.every) : esc(line.source) + " · " + esc(line.types.join(" "))}</span>
|
|
141
|
+
</div>
|
|
142
|
+
<span class="a-next-at">${line.paused ? "停" : line.next ? clock(line.next) : ""}</span>
|
|
143
|
+
</li>`;
|
|
144
|
+
})
|
|
145
|
+
.join("");
|
|
146
|
+
|
|
147
|
+
const upcoming = lines
|
|
148
|
+
.filter((line) => line.next && !line.paused)
|
|
149
|
+
.sort((a, b) => Date.parse(a.next) - Date.parse(b.next))
|
|
150
|
+
.slice(0, 6)
|
|
151
|
+
.map(
|
|
152
|
+
(line) => `<li><span>${dayOf(line.next)} ${clock(line.next)}</span><b>${esc(line.name)}</b><em>${esc(line.agent)}</em></li>`,
|
|
153
|
+
)
|
|
154
|
+
.join("");
|
|
155
|
+
|
|
156
|
+
const alerts = alertsOf(state);
|
|
157
|
+
const subtitle = live === "on" ? "实时" : live === "starting" ? "正在连上实时" : "实时断了,正在重连";
|
|
158
|
+
return `
|
|
159
|
+
<header class="a-top">
|
|
160
|
+
<div class="a-title">
|
|
161
|
+
<h1>中台时刻</h1>
|
|
162
|
+
<p>${stamp(state.now)} · ${esc(state.timezone)} · 最近 24 小时 · ${subtitle}</p>
|
|
163
|
+
</div>
|
|
164
|
+
<div class="a-counts">
|
|
165
|
+
<button type="button" class="a-count" data-bucket="running"><span>在跑</span><b>${state.totals.running}</b></button>
|
|
166
|
+
<button type="button" class="a-count" data-bucket="queued"><span>排队</span><b>${state.totals.queued}</b></button>
|
|
167
|
+
<button type="button" class="a-count${state.totals.attention ? " hot" : ""}" data-bucket="attention"><span>待你</span><b>${state.totals.attention}</b></button>
|
|
168
|
+
<button type="button" class="a-count" data-bucket="today"><span>今天跑完</span><b>${state.totals.doneToday}</b></button>
|
|
169
|
+
</div>
|
|
170
|
+
</header>
|
|
171
|
+
${
|
|
172
|
+
failure
|
|
173
|
+
? `<div class="a-alert"><b>读不到中台</b><span>${esc(failure)}</span></div>`
|
|
174
|
+
: alerts.length
|
|
175
|
+
? `<div class="a-alert"><b>${esc(alerts[0].title)}</b><span>${esc(alerts[0].detail)}</span>${
|
|
176
|
+
alerts.length > 1 ? `<em>另有 ${alerts.length - 1} 条</em>` : ""
|
|
177
|
+
}</div>`
|
|
178
|
+
: ""
|
|
179
|
+
}
|
|
180
|
+
<div class="a-grid">
|
|
181
|
+
<aside class="a-rules">
|
|
182
|
+
<h2>规则</h2>
|
|
183
|
+
<ul>${ruleRows}</ul>
|
|
184
|
+
</aside>
|
|
185
|
+
<section class="a-rail">
|
|
186
|
+
<h2>轨道</h2>
|
|
187
|
+
<div class="a-track" style="height:${track}px">
|
|
188
|
+
<span class="a-edge">${dayOf(new Date(from).toISOString())} ${clock(new Date(from).toISOString())}</span>
|
|
189
|
+
${ticks.join("")}
|
|
190
|
+
${marksHtml || `<p class="a-empty">过去 24 小时没有跑过活。</p>`}
|
|
191
|
+
<div class="a-now" style="top:${height}px"><span>现在</span></div>
|
|
192
|
+
</div>
|
|
193
|
+
</section>
|
|
194
|
+
<aside class="a-side">
|
|
195
|
+
<h2>接下来</h2>
|
|
196
|
+
<ul class="a-upcoming">${upcoming || `<li class="a-empty">没有排着的定时规则。</li>`}</ul>
|
|
197
|
+
<h2>最近跑完</h2>
|
|
198
|
+
<ul class="a-recent">
|
|
199
|
+
${[...state.runs]
|
|
200
|
+
.sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))
|
|
201
|
+
.slice(0, 6)
|
|
202
|
+
.map(
|
|
203
|
+
(run) =>
|
|
204
|
+
`<li><b>${clock(run.createdAt)}</b> ${esc(run.rule)}<span>${dayOf(run.createdAt)} · ${
|
|
205
|
+
STATE_WORD[run.state] ?? run.state
|
|
206
|
+
}${run.usage ? " · " + num.format(tokens(run.usage)) + " token" : ""}</span></li>`,
|
|
207
|
+
)
|
|
208
|
+
.join("")}
|
|
209
|
+
</ul>
|
|
210
|
+
</aside>
|
|
211
|
+
</div>`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/* ---------- 明细:点计数打开,看这一格里所有的活 ---------- */
|
|
215
|
+
|
|
216
|
+
const cardRow = (card) => `<article class="sheet-item">
|
|
217
|
+
<p class="si-head"><span class="si-kind ${card.kind}">${card.kind === "ask" ? "提问" : "提醒"}</span><b>${esc(card.title)}</b><em>#${card.id}</em></p>
|
|
218
|
+
<p class="si-text">${esc(card.text).slice(0, 1400)}</p>
|
|
219
|
+
${
|
|
220
|
+
card.kind === "ask" && (card.options ?? []).length
|
|
221
|
+
? `<ol class="si-options">${card.options
|
|
222
|
+
.map((option, index) => `<li${index === card.recommend ? ' class="rec"' : ""}>${esc(option)}</li>`)
|
|
223
|
+
.join("")}</ol>`
|
|
224
|
+
: ""
|
|
225
|
+
}
|
|
226
|
+
<p class="si-meta">规则「${esc(card.rule)}」${card.about ? " · 关于 " + esc(card.about) : ""} · ${dayOf(card.createdAt)} ${clock(
|
|
227
|
+
card.createdAt,
|
|
228
|
+
)} · 第 ${card.version} 版${card.ref ? " · 已发到 " + esc(card.ref) : ""}</p>
|
|
229
|
+
</article>`;
|
|
230
|
+
|
|
231
|
+
const runRow = (run) => {
|
|
232
|
+
const timeline = timelines.get(run.id);
|
|
233
|
+
return `<article class="sheet-item">
|
|
234
|
+
<p class="si-head"><span class="si-kind ${run.state}">${STATE_WORD[run.state] ?? run.state}</span><b>${esc(run.rule)}</b><em>#${run.id}</em></p>
|
|
235
|
+
<dl class="si-grid">
|
|
236
|
+
<dt>交给</dt><dd>${esc(run.agent)}${run.manual ? " · 手动起的" : ""}</dd>
|
|
237
|
+
<dt>开始</dt><dd>${dayOf(run.createdAt)} ${clock(run.createdAt)}</dd>
|
|
238
|
+
${run.claimedAt && clock(run.claimedAt) !== clock(run.createdAt) ? `<dt>领活</dt><dd>${clock(run.claimedAt)}</dd>` : ""}
|
|
239
|
+
<dt>结束</dt><dd>${
|
|
240
|
+
run.finishedAt
|
|
241
|
+
? `${clock(run.finishedAt)} · 用了 ${runSpan(run)}`
|
|
242
|
+
: run.state === "queued"
|
|
243
|
+
? `还没轮到${run.notBefore ? ",最早 " + clock(run.notBefore) : ""}`
|
|
244
|
+
: "还没结束"
|
|
245
|
+
}${run.stopping ? " · 正在停" : ""}</dd>
|
|
246
|
+
${run.attempts ? `<dt>次数</dt><dd>第 ${run.attempts + 1} 次</dd>` : ""}
|
|
247
|
+
${
|
|
248
|
+
run.usage
|
|
249
|
+
? `<dt>用量</dt><dd>${num.format(tokens(run.usage))} token${
|
|
250
|
+
run.usage.cached ? "(缓存 " + num.format(run.usage.cached) + ")" : ""
|
|
251
|
+
}${run.usage.cost ? " · $" + run.usage.cost.toFixed(3) : ""}</dd>`
|
|
252
|
+
: ""
|
|
253
|
+
}
|
|
254
|
+
${run.session ? `<dt>会话</dt><dd class="mono">${esc(run.session)}</dd>` : ""}
|
|
255
|
+
${run.link ? `<dt>链接</dt><dd class="mono">${esc(run.link)}</dd>` : ""}
|
|
256
|
+
${run.error ? `<dt>错误</dt><dd class="bad">${esc(run.error)}</dd>` : ""}
|
|
257
|
+
</dl>
|
|
258
|
+
${run.summary ? `<p class="si-text">${esc(run.summary)}</p>` : ""}
|
|
259
|
+
${
|
|
260
|
+
run.events.length
|
|
261
|
+
? `<ul class="si-events">${run.events
|
|
262
|
+
.map((event) => `<li>${esc(event.source)} · ${esc(event.type)}${event.subject ? " · " + esc(event.subject) : ""}</li>`)
|
|
263
|
+
.join("")}</ul>`
|
|
264
|
+
: ""
|
|
265
|
+
}
|
|
266
|
+
<p class="si-why-line"><button type="button" class="si-why" data-why="${run.id}">${
|
|
267
|
+
timeline ? "收起来龙去脉" : "看这一轮的来龙去脉"
|
|
268
|
+
}</button></p>
|
|
269
|
+
${timeline ? `<pre class="si-why-text">${esc(timeline)}</pre>` : ""}
|
|
270
|
+
</article>`;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const alertRow = (alert) => `<article class="sheet-item">
|
|
274
|
+
<p class="si-head"><span class="si-kind ${alert.level}">${
|
|
275
|
+
alert.level === "bad" ? "断了" : alert.level === "warn" ? "要留意" : "提醒"
|
|
276
|
+
}</span><b>${esc(alert.title)}</b></p>
|
|
277
|
+
<p class="si-text">${esc(alert.detail)}</p>
|
|
278
|
+
</article>`;
|
|
279
|
+
|
|
280
|
+
const BUCKETS = {
|
|
281
|
+
running: { label: "在跑", empty: "现在没有轮次在跑。", row: runRow, pick: (s) => s.runs.filter((run) => run.state === "claimed") },
|
|
282
|
+
queued: { label: "排队", empty: "没有排着的活。", row: runRow, pick: (s) => s.runs.filter((run) => run.state === "queued") },
|
|
283
|
+
attention: {
|
|
284
|
+
label: "待你",
|
|
285
|
+
empty: "没有等你决定的事,也没有没处理的提醒。",
|
|
286
|
+
row: cardRow,
|
|
287
|
+
pick: (s) => s.cards,
|
|
288
|
+
foot: "这里只是看:回答要走通道(飞书、小九)或者 agent-autoloop answer。",
|
|
289
|
+
},
|
|
290
|
+
today: {
|
|
291
|
+
label: "今天跑完",
|
|
292
|
+
empty: "今天还没有跑完的活。",
|
|
293
|
+
row: runRow,
|
|
294
|
+
pick: (s) => s.runs.filter((run) => run.state === "done" && run.finishedAt && dayOf(run.finishedAt) === dayOf(s.now)),
|
|
295
|
+
foot: "只列读到的那几十轮;更早的在 agent-autoloop runs --before 里往前翻。",
|
|
296
|
+
},
|
|
297
|
+
alerts: {
|
|
298
|
+
label: "需要照看",
|
|
299
|
+
empty: "没有断掉的通道、停着的规则或没人要的事件。",
|
|
300
|
+
row: alertRow,
|
|
301
|
+
pick: (s) => alertsOf(s).filter((alert) => alert.kind !== "card"),
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
function sheetHtml() {
|
|
306
|
+
if (!bucket || !snapshot) return "";
|
|
307
|
+
const view = BUCKETS[bucket];
|
|
308
|
+
const items = view ? view.pick(snapshot) : [];
|
|
309
|
+
return `<div class="sheet-backdrop" data-close="1"></div>
|
|
310
|
+
<aside class="sheet" role="dialog" aria-label="${view.label}">
|
|
311
|
+
<header><h2>${view.label}</h2><button type="button" class="sheet-close" data-close="1" aria-label="关掉">×</button></header>
|
|
312
|
+
<div class="sheet-tabs">${Object.entries(BUCKETS)
|
|
313
|
+
.map(
|
|
314
|
+
([key, item]) =>
|
|
315
|
+
`<button type="button" data-bucket="${key}"${key === bucket ? ' class="on"' : ""}>${item.label}<b>${item.pick(snapshot).length}</b></button>`,
|
|
316
|
+
)
|
|
317
|
+
.join("")}</div>
|
|
318
|
+
<div class="sheet-body">${
|
|
319
|
+
items.length ? items.map(view.row).join("") : `<p class="sheet-empty">${view.empty}</p>`
|
|
320
|
+
}</div>
|
|
321
|
+
${view.foot ? `<footer>${view.foot}</footer>` : ""}
|
|
322
|
+
</aside>`;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function render() {
|
|
326
|
+
const kept = sheet.querySelector(".sheet-body")?.scrollTop ?? 0;
|
|
327
|
+
sheet.innerHTML = sheetHtml();
|
|
328
|
+
const body = sheet.querySelector(".sheet-body");
|
|
329
|
+
if (body) body.scrollTop = kept;
|
|
330
|
+
if (!snapshot) {
|
|
331
|
+
root.innerHTML = `<p class="boot">${failure ? esc(failure) : "正在读中台状态…"}</p>`;
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const scroll = window.scrollY;
|
|
335
|
+
root.innerHTML = timetable(snapshot);
|
|
336
|
+
window.scrollTo(0, scroll);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async function load() {
|
|
340
|
+
try {
|
|
341
|
+
const response = await fetch("/api/state", { cache: "no-store" });
|
|
342
|
+
const body = await response.json();
|
|
343
|
+
if (!response.ok) throw new Error(body.error ?? `中台回了 ${response.status}`);
|
|
344
|
+
snapshot = body;
|
|
345
|
+
lastSeq = Math.max(lastSeq, body.seq);
|
|
346
|
+
failure = "";
|
|
347
|
+
} catch (error) {
|
|
348
|
+
failure = error.message;
|
|
349
|
+
}
|
|
350
|
+
render();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** One run's timeline, as the hub tells it from the log. Kept until the sheet is closed, so a refresh doesn't re-ask. */
|
|
354
|
+
async function why(id) {
|
|
355
|
+
if (timelines.has(id)) {
|
|
356
|
+
timelines.delete(id);
|
|
357
|
+
render();
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
timelines.set(id, "正在读…");
|
|
361
|
+
render();
|
|
362
|
+
try {
|
|
363
|
+
const response = await fetch(`/api/why/${id}`, { cache: "no-store" });
|
|
364
|
+
const body = await response.json();
|
|
365
|
+
timelines.set(id, response.ok ? body.text : body.error ?? `中台回了 ${response.status}`);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
timelines.set(id, `读不到:${error.message}`);
|
|
368
|
+
}
|
|
369
|
+
render();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Redraw when the hub logs a fact; the page then reads the state again rather than guessing from the fact alone. */
|
|
373
|
+
let pending = 0;
|
|
374
|
+
function schedule() {
|
|
375
|
+
clearTimeout(pending);
|
|
376
|
+
pending = setTimeout(load, 350);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function listen() {
|
|
380
|
+
const stream = new EventSource(`/api/stream?after=${lastSeq}`);
|
|
381
|
+
stream.addEventListener("open", () => {
|
|
382
|
+
live = "on";
|
|
383
|
+
load();
|
|
384
|
+
});
|
|
385
|
+
stream.addEventListener("fact", (event) => {
|
|
386
|
+
lastSeq = Math.max(lastSeq, Number(event.lastEventId) || 0);
|
|
387
|
+
schedule();
|
|
388
|
+
});
|
|
389
|
+
stream.addEventListener("error", () => {
|
|
390
|
+
live = "off";
|
|
391
|
+
stream.close();
|
|
392
|
+
render();
|
|
393
|
+
setTimeout(listen, 2000);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
document.addEventListener("click", (event) => {
|
|
398
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
399
|
+
const opener = target?.closest("[data-bucket]");
|
|
400
|
+
if (opener) {
|
|
401
|
+
bucket = opener.dataset.bucket;
|
|
402
|
+
render();
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
const asked = target?.closest("[data-why]");
|
|
406
|
+
if (asked) {
|
|
407
|
+
why(Number(asked.dataset.why));
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
if (target?.closest("[data-close]")) {
|
|
411
|
+
bucket = null;
|
|
412
|
+
render();
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
document.addEventListener("keydown", (event) => {
|
|
417
|
+
if (event.key === "Escape" && bucket) {
|
|
418
|
+
bucket = null;
|
|
419
|
+
render();
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
render();
|
|
424
|
+
load().then(listen);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<!-- The owner's page: read-only, served by `agent-autoloop dashboard` from this machine. -->
|
|
3
|
+
<html lang="zh-CN">
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="utf-8" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
7
|
+
<title>中台看板</title>
|
|
8
|
+
<link rel="stylesheet" href="/style.css" />
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<main id="root"></main>
|
|
12
|
+
<div id="sheet"></div>
|
|
13
|
+
<script type="module" src="/app.js"></script>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|