@saltpub/agent-autoloop 0.1.5 → 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/CHANGELOG.md +26 -0
- package/README.en.md +1 -1
- package/README.md +1 -1
- package/SECURITY.md +1 -1
- package/dist/bin/agent-autoloop.js +67 -1
- package/dist/cli/dashboard.js +43 -2
- package/dist/cli/explain.js +2 -0
- package/dist/cli/main.js +27 -8
- package/dist/host/global-shim.js +79 -0
- package/dist/host/runtime.js +1 -1
- package/dist/host/state.js +19 -6
- package/dist/host/up.js +8 -0
- package/dist/kernel/decide.js +7 -6
- package/dist/kernel/query.js +2 -1
- package/dist/plugins/codex/agent.js +43 -6
- package/dist/plugins/delivery/project.js +3 -1
- package/dist/plugins/delivery/review.js +5 -2
- package/dist/plugins/feishu/channel.js +44 -6
- package/dist/protocol/rules.js +1 -0
- package/dist/protocol/schema.json +6 -0
- package/dist/worker/agents.js +4 -0
- package/dist/worker/contract.js +1 -0
- package/dist/worker/worker.js +107 -32
- package/docs/auto-update.md +2 -0
- package/docs/protocol.md +8 -2
- package/examples/plugins/hello-source/source.ts +4 -3
- package/package.json +1 -1
- package/plugins/codex/agent.ts +40 -6
- package/plugins/delivery/project.ts +2 -1
- package/plugins/delivery/review.ts +5 -2
- package/plugins/feishu/channel.ts +50 -6
- package/skills/agent-autoloop/SKILL.md +3 -0
|
@@ -8,6 +8,7 @@ import { setTimeout as wait } from "node:timers/promises";
|
|
|
8
8
|
import type { Card, ChannelContext } from "../../host/channels.ts";
|
|
9
9
|
import { eachLine } from "../../host/lines.ts";
|
|
10
10
|
import { commandLine } from "../../host/command.ts";
|
|
11
|
+
import { fit } from "../../worker/contract.ts";
|
|
11
12
|
|
|
12
13
|
/** Feishu sends these values as JSON in a string, which may be empty. */
|
|
13
14
|
function safeJson(text: unknown): any {
|
|
@@ -27,6 +28,35 @@ function outcomeOf(card: Card) {
|
|
|
27
28
|
return card.state === "closed" && card.result ? card.result : null;
|
|
28
29
|
}
|
|
29
30
|
|
|
31
|
+
/** What lark-cli said, on one line and short enough for a log, without cutting a character in half. */
|
|
32
|
+
const oneLine = (text: unknown) =>
|
|
33
|
+
fit(
|
|
34
|
+
String(text ?? "")
|
|
35
|
+
.replace(/\s+/g, " ")
|
|
36
|
+
.trim(),
|
|
37
|
+
300,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A failure as the reason and the next thing to do: the CLI's error body is JSON meant for a program, not the reader.
|
|
42
|
+
* The bot's permissions come from the app's settings in Feishu's developer console, and signing in again doesn't change
|
|
43
|
+
* them; only the user identity is fixed by lark-cli auth login.
|
|
44
|
+
*/
|
|
45
|
+
function explain(where: string, body: any, failure: Error | undefined, printed: string, identity?: string) {
|
|
46
|
+
const type = typeof body?.type === "string" ? body.type : "";
|
|
47
|
+
const said = typeof body?.message === "string" ? oneLine(body.message) : "";
|
|
48
|
+
const because = [type, said].filter(Boolean).join(":");
|
|
49
|
+
if (type === "authorization" || type === "authentication") {
|
|
50
|
+
const page = typeof body?.console_url === "string" ? `:${oneLine(body.console_url)}` : "";
|
|
51
|
+
const hint = typeof body?.hint === "string" ? oneLine(body.hint) : "";
|
|
52
|
+
return identity === "bot"
|
|
53
|
+
? `${where} 没通过飞书授权(${because}):在飞书开发者后台给这个应用开通所需权限${page}`
|
|
54
|
+
: `${where} 没通过飞书授权(${because}):${hint || "在这台电脑运行 lark-cli auth login 重新登录"}`;
|
|
55
|
+
}
|
|
56
|
+
if (because) return `${where} 失败:飞书说 ${because}`;
|
|
57
|
+
return `${where} 失败:${(oneLine(printed) || oneLine(failure?.message)).replace(/^Error:\s*/, "") || "没有说明原因"}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
30
60
|
/** One card, as it looks now: the options while it's open, what became of it after. */
|
|
31
61
|
export function render(card: Card) {
|
|
32
62
|
const outcome = outcomeOf(card);
|
|
@@ -104,19 +134,33 @@ export function render(card: Card) {
|
|
|
104
134
|
export default async function feishu({ hub, options, log, signal }: ChannelContext) {
|
|
105
135
|
const program = (options.lark as string | undefined) ?? "lark-cli";
|
|
106
136
|
const lark = async (args: string[], input?: string) => {
|
|
107
|
-
const
|
|
137
|
+
const ran = await new Promise<{ failure?: Error; stdout: string; stderr: string }>((resolve) => {
|
|
108
138
|
const child = execFile(
|
|
109
139
|
...commandLine([program, ...args]),
|
|
110
140
|
{ encoding: "utf8", timeout: 60_000, signal },
|
|
111
|
-
(
|
|
141
|
+
(failure, stdout, stderr) => resolve({ failure: failure ?? undefined, stdout, stderr }),
|
|
142
|
+
);
|
|
143
|
+
child.stdin!.on(
|
|
144
|
+
"error",
|
|
145
|
+
input === undefined ? () => {} : (failure) => resolve({ failure, stdout: "", stderr: "" }),
|
|
112
146
|
);
|
|
113
|
-
child.stdin!.on("error", input === undefined ? () => {} : reject);
|
|
114
147
|
child.stdin!.end(input);
|
|
115
148
|
});
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
149
|
+
const where = `lark-cli ${args.slice(0, 3).join(" ")}`;
|
|
150
|
+
// What it did is on stdout; what went wrong is on stderr, and an error body can come with either exit code.
|
|
151
|
+
const printed = ran.failure ? ran.stderr : ran.stdout;
|
|
152
|
+
let out: any;
|
|
153
|
+
try {
|
|
154
|
+
out = JSON.parse(printed);
|
|
155
|
+
} catch {
|
|
156
|
+
out = null;
|
|
119
157
|
}
|
|
158
|
+
const identity =
|
|
159
|
+
typeof out?.identity === "string" ? out.identity : args.includes("--as") ? args[args.indexOf("--as") + 1] : "";
|
|
160
|
+
if (out?.ok === false || out?.error)
|
|
161
|
+
throw new Error(explain(where, out.error ?? out, ran.failure, printed, identity));
|
|
162
|
+
if (ran.failure) throw new Error(explain(where, null, ran.failure, printed));
|
|
163
|
+
if (!out) throw new Error(`${where} 失败:返回的不是 JSON(${oneLine(printed) || "没有输出"})`);
|
|
120
164
|
return out;
|
|
121
165
|
};
|
|
122
166
|
const owner = (options.user as string | undefined) ?? (await lark(["auth", "status"], "")).identities?.user?.openId;
|
|
@@ -29,12 +29,15 @@ description: 配置 Agent 自动化中台:加规则、接新的事件插件、
|
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
- `tell` 只能是字符串 `"always"`(每轮做成都发)、`"needed"`(只在有卡片时,默认)或 `"never"`(只压住回执里的通知),不是 `true` 或 `false`;提问和中台自己的告警不受 `never` 影响。
|
|
32
|
+
- 一轮的结论只有最后一条消息的第一行:`tell: "always"` 发的那张卡正文就是这一行,第二行起的内容不进结论,只留在轮次输出文件里(结束后留 14 天)。要让人看到正文(汇总表、完整报告),在规则的 `prompt` 或手册里要求 Agent 把它写进回执的 `notice`,第一行只写一句能独立成立的话,不要写「以下是……」这种引子。
|
|
32
33
|
- `when` 只能写事件或定时其中一种。事件的 `types` 是字符串列表,写具体类型、`x.*`(前缀匹配)或 `*`(全部);定时只写 `{ "cron": "0 9 * * *" }`,不要再加 `source`、`types`。
|
|
34
|
+
- 定时规则大部分是「先看看有没有、有才动手」时,把检查拆给 `use: "command"` 的脚本规则(写上 `emits`,范本见 `examples/recipes/github-issues/`):脚本不花 Token,有发现才发事件,监听事件的 LLM 规则才动手。活本身就是 LLM 干的(写早报、做摘要)不用拆。
|
|
33
35
|
- 允许回执发事件时写 `"emits": { "source": "github", "types": ["issues.labeled"] }`;`source` 要是已登记的来源,不能是保留的 `schedule`,不写 `emits` 就不能发。
|
|
34
36
|
- 同一个对象的事件要分开处理、互不等待,就写 `parallel`,值要是正整数。一台接单程序同时跑几轮还受 `config.json` 的 `worker.capacity` 限制,默认 2,要多跑就一起调大。
|
|
35
37
|
- 对话这类要马上回的活,不想排在批量的长任务后面:把批量的规则都写进同一个 `group`,`parallel` 写得比 `worker.capacity` 小,留下的名额长任务占不了,对话来了直接领。比如 `capacity` 是 4,批量那组写 `"group": "batch", "parallel": 3`。同一组的规则 `parallel` 写成一样的。
|
|
36
38
|
- 同组一共并行三轮,但单条规则每次只跑一轮时,在规则里加 `"maxActive": 1`。每个对象仍单独排队。在 `agents` 里也可以给单个 Agent 加 `maxActive`,限制它跨规则同时跑几轮。
|
|
37
39
|
- 同一个对象的新事件要接着上一轮的会话(比如同一个对话的后续消息),就写 `"resume": true`;默认每次开新会话。上一轮被停掉、可能还在跑或者换了 Agent 时开新的。不写 `parallel` 时整条规则的事件都接同一个会话。
|
|
40
|
+
- 规则多了要分组(按项目、团队)时,给规则写 `"tags": ["shop"]`:中台不解释,只原样带进 `/v1/state` 的规则、轮次和卡片,以及这条规则每一轮的资料文件。规则放在一个 `rules.json` 里;几份分开维护的,合并成一份再换上去,先用 `agent-autoloop check` 校验。
|
|
38
41
|
- 一个事项一轮开一个会话、客户端里的会话名字都一样分不清时,给规则写 `"title": "tapd-{id}-测试"`:它单独占提示词第一行,`{subject}` 换成事件的对象,`{id}` 换成对象按 `/ # :` 分开的最后一段。要是字母或数字开头、不含空白的一个词。
|
|
39
42
|
- 要停一条规则,用 `agent-autoloop pause <规则> --reason …`(到点自动恢复加 `--until`),恢复用 `resume <规则>`,现在就跑一轮用 `run <规则>`;不用为了停规则去改 `rules.json`。暂停期间来的事件这条规则不接、也不排队;`resume` 会列出这次暂停跳过的事件,有就如实告诉用户;用户说要补,再运行它给出的 `agent-autoloop replay <规则> --since <时间>`,一次补完、每条只补一次,不用 `run` 一条条补。
|
|
40
43
|
- 改规则前可以先写成草稿,用 `agent-autoloop check <草稿>` 校验,不影响正在跑的中台。
|