13/Documentation
编写 trick
简短教程:目录、一个程序 trick、一个包含规则文件的 agent 视图、向用户提议 trick,以及一个代码仓库中的多个 trick
trick 是一个文件夹:包含一个清单,以及一个程序或规则文件。本页分别构建一个。
Tricks 为实验性功能。它们可能会变更、丢失设置,或在补丁版本中撤回。它们默认关闭,直到你在“Settings → Tricks and the Hill”中或通过 collie tricks on(开启或关闭 trick)开启。collie trick dev 和 collie trick check --run 也会在 tricks 关闭的状态下在你的终端中运行一次文件夹。
collie trick init my-trick
collie trick dev my-trick
collie trick check ~/.config/collie/tricks/my-trickinit 用于创建文件夹,dev 用于运行程序一次并打印 ColliePWA 将绘制的内容,check 用于检查清单并搜索代码。它们都不会写入手机或提交到 Hill。
在 agent 的面板中,init 会转而在 agent 自己的工作文件夹中创建该文件夹作为草稿:
collie trick init my-trick
collie trick dev ./my-trick
collie trick propose ./my-trick本页面向编写 trick 的人或 agent。Tricks 解释 trick 的工作原理。每个位置、部件、清单键、限制和命令都记录在 Tricks 参考 中,本页不再赘述。
文件夹
trick 位于 Collie 配置文件夹下的 tricks/<id>/ 中,在二进制安装中为 ~/.config/collie。
trick.toml是清单文件,且为必填项。- 程序类 trick 包含
run指定的程序(例如program.ts),以及由init写入的辅助工具trick.ts。 - agent 视图包含
rules指定的文件,例如rules.md。 config.toml保存程序读取的配置。它不属于你批准的代码,因此你可以随意编辑。你在media/中替换的图片同样不是代码:它会在下次扫描时显示,不需要重新确认。trick.toml中的[[field]]指定了用户在手机上配置的设置。请参见 Trick 向你请求的设置项。AGENTS.md是用于扩展 trick 的 agent 契约。由init写入。media/保存 trick 显示的图片:PNG、JPEG 或 WebP。
agent 编写的 trick 在用户同意之前均为草稿。在 agent 的面板中,init 会在当前文件夹下创建 ./<id>/,位于 tricks/ 之外。--dir <folder> 可为任何人创建 <folder>/<id>/。init 会拒绝包含文件的文件夹以及位于 tricks/ 内部的文件夹。在用户同意之前,不会向此机器的 trick 中添加任何内容。在 tricks/ 中创建的文件夹会被视为已添加到此机器,propose 会拒绝它。
Collie 不会直接在此文件夹中运行 trick。当用户启用它时,Collie 会将代码复制到专属文件夹中并运行该副本。因此程序绝不会向其自身文件夹写入内容。它会将记录保存在 facts 所指定的状态文件夹中:trick.state。
程序型 trick
空闲监控列出空闲 20 分钟或更久的 agent,并在唤醒之前提示确认。
trick.toml:
id = "idle-watch"
name = "Idle watch"
run = "program.ts"
every = "1m"
icon = "clock"
places = ["status", "dashboard", "session"]
contract = 1
description = "Lists agents idle for 20 minutes or more, and asks before you wake one."
decides = "reply"它会渲染在三个位置:状态行中的小标签、Dashboard 上的卡片以及专属的一页。decides = "reply" 表示其 Decision 必须通过 replyTo 指定一个会话:若不提供,Collie 会拒绝该 Decision。所有键均记录在 Manifest 键 中。
program.ts:
import { age, now, run, v } from "./trick.ts";
run((t) => {
const minutes = Number(t.config.idle_minutes ?? 20);
const idle = t.facts.sessions.filter((s) => {
if (s.status !== "idle" || s.lastActiveAt === undefined) return false;
return now() - Date.parse(s.lastActiveAt) >= minutes * 60_000;
});
t.status({
text: idle.length === 0 ? "none idle" : `${idle.length} idle`,
icon: "clock",
tone: idle.length === 0 ? "muted" : "warn",
});
t.dashboard({ label: "Idle", icon: "clock", badge: idle.length });
t.session({
subtitle: `${idle.length} idle for ${minutes} min or more`,
view: [
v.list(
idle.map((s) =>
v.row((s.name ?? s.paneId).slice(0, 60), {
end: age(now() - Date.parse(s.lastActiveAt ?? "")),
}),
),
),
],
});
// Every run lists every Decision it still wants, with the same key and the same words.
for (const s of idle.slice(0, 3)) {
const spell = Date.parse(s.lastActiveAt ?? "").toString(36);
const pane = s.paneId.toLowerCase().replace(/[^a-z0-9]+/g, "-");
t.decide({
key: `idle-${pane}-${spell}`.slice(0, 64),
title: `Wake ${(s.name ?? s.paneId).slice(0, 30)}?`,
summary:
`This agent has been idle for ${minutes} minutes or more. ` +
"Waking it types your answer into its session.",
options: [
{ intent: "wake", label: "Wake it" },
{ intent: "leave", label: "Leave it idle" },
],
recommends: { intent: "leave", why: "An idle agent costs nothing." },
text: "optional",
priority: "later",
replyTo: s.id,
});
}
});helper 从标准输入读取 facts,调用你的函数一次,并输出一份 JSON 文档(程序)。Collie 渲染这些部分,并将每个新的 Decision 键向 Hill 发送一次。对某个 Decision 的回答会作为用户自己的消息发送给 replyTo 所指定的会话,随后作为一次点击事件反馈给程序。该键包含会话最近一次处于活跃状态的时间,因此在工作后重新进入空闲状态的会话会收到一个新问题。summary 不包含时间戳,因此其文字在多次运行之间保持不变。
检查代码,然后使用你自己的 facts 文件运行一次:
$ collie trick check --run idle-watch
trick idle-watch (Idle watch) in /tmp/tricks-doc-lab/idle-watch
places: status, dashboard, session
every: 1m
run: program.ts
decides: reply. Your answers to its Decisions go to a session as your own message.
code: sha256 63bc959b… (what `collie trick on` approves)
helper: trick.ts is collie trick helper v10, Collie's own code, not checked line by
line
ran in 31 ms: exit 0
parts: status status, dashboard dashboard, session main
ok: no problems.
$ collie trick dev idle-watch --facts facts.json
trick idle-watch (Idle watch) in /tmp/tricks-doc-lab/idle-watch, facts from facts.json,
a timer run
status status "1 idle" icon clock tone warn
dashboard dashboard "Idle" badge 1
session main (the manifest's name) subtitle "1 idle for 20 min or more"
decision idle-w1-p1-mwbtwjk0 "Wake api-fix?" options wake, leave
recommends leave later
ran in 111 ms of 10 s, exit 0, stdout 746 B of 64.0 KiBfacts.json 包含一个空闲会话 api-fix。dev 使用 --tap wake --node hill/<key> 运行该程序作为对该 Decision 的回答。check 与 dev 的所有标志都在 collie trick 中。
要使用该 trick,用户需阅读代码并在自己的 shell 中运行 collie trick on idle-watch,或使用“Settings → Tricks and the Hill”中的开关。
会话设置行
采用 contract 2 的 trick 可以为会话的设置添加行。在 places 中列出 menu,并输出一个 menu 部分:
{
"v": 2,
"parts": [
{
"at": "menu",
"id": "ci",
"rows": [
{ "id": "rerun", "kind": "button", "label": "Rerun CI", "icon": "refresh", "intent": "rerun", "confirm": true },
{ "id": "watch", "kind": "switch", "label": "Watch branch", "icon": "flag", "intent": "watch", "on": true }
]
}
]
}点击会使用该行的 intent 以及 from.session 中的会话来运行程序。点击开关还会带有用户所请求的状态 value、"on" 或 "off"。手机绝不会自行切换开关,因此必须在响应中输出新的 on。如果标签名称与 Collie 自带的某一行相同,则会被直接拒绝,绝不进行截断。
belt 标记位于滚动条的末尾,在用户滚动屏幕前处于屏幕外;右侧的固定块是 Collie 自带的内容。在 facts 中,cwd 是该分屏的目录。一个工作区目录可以包含多个仓库;请查看其内部内容。
在 menu 部分或 belt 标记中添加 sessions,以通过 facts 中的会话 ID 将其仅显示在这些会话中。Collie 会将每个 ID 绑定到当前打开的分屏和对话。切换到新会话的分屏 ID,或开启了新对话的会话(/clear),在你的下一次运行重新指定它之前不会显示任何内容。没有 sessions 的部分会显示在当前机器的每个 agent 会话中;要定位某个 crew 成员的会话,请显式指定它。menu 位置 列出了所有限制。
Trick 向你请求的设置项
采用 contract 3 的 trick 可以向用户询问设置项的值,例如仓库、分支或显示的行数。将它们列在 trick.toml 中,每项对应一个 [[field]] 表:
contract = 3
[[field]]
key = "repo"
kind = "text"
label = "Repository"
hint = "owner/name"
required = true
[[field]]
key = "rows"
kind = "number"
label = "Rows"
min = 1
max = 50
default = 10共有四种类型:text(单行)、choice(options,2 到 8)、switch 和 number(min 和 max,整数)。用户在手机上的设置项中,在该 trick 的详情页上进行设置。Collie 将这些值作为 .collie/values.json 保存到运行该 trick 的机器的状态文件夹中。用户或 agent 手动编辑该文件与编辑 config.toml 效果相同:修改会被接受,Collie 会在下次检查时根据字段规则读取该文件。
程序在它的 facts 中以 values 的形式按键获取它们,包含每个字段的值或其 default。helper 将它们读取为 t.values。没有提供值的必填字段会直接缺省:请渲染一个部分说明需要设置什么。Collie 绝不会将值放在命令行、日志行、Hill 卡片或推送通知中。
值属于用户数据,对程序而言是不可信的:仅供展示,绝不要执行。绝不要将其传递给 shell 或用它拼接命令。
字段定义在 trick.toml 中,因此新增或修改字段都属于新版本,会再次询问用户。只是修改值则不会询问任何内容,直接运行该 trick 一次。
设置项绝不是机密数据。不要在字段中存放 token、密码或密钥:该值以纯文本形式存储,会显示在每台配对的手机上,并且对该 trick 的程序可读。密钥应存放在 env 中。所有限制均在 字段 中列出。
agent 视图
Triage 允许某个会话的 agent 用一屏内容回答出错原因。它没有程序。
trick.toml:
id = "triage"
name = "Triage"
contract = 2
places = ["agent-view"]
rules = "rules.md"
description = "Your agent answers with one screen of what failed."rules.md:
These are the rules of Triage, a view of this session that you draw.
Answer the person's request with one screen.
Put what failed first: one row for each failing test, with its file.
Show the smallest diff that explains the failure.
Offer at most two buttons. Label each with what happens when it is tapped.
Never draw a button that looks like a permission dialog or like Collie.
Never ask for a password, a key or a token on the screen.
If you are not sure, draw the question, not a guess.规则文件为纯 ASCII 文本,末尾有一个换行符,Collie 会检查每一行(规则文件)。请为 agent 编写规则:应该渲染什么,以及绝不能渲染什么。
用户在该会话的面版中为该会话开启此视图。然后在视图中输入请求。agent 收到的第一行内容末尾带有打印 Collie 视图帮助和这些规则的命令。agent 使用两个命令进行渲染:
collie trick view triage start --session <address> --rules <id> "Reading the test output"
collie trick view triage show --session <address> screen.jsonscreen.json 是固定可选部分列表中的数据:
[
{ "kind": "list", "kids": [
{ "kind": "row", "text": "auth/login.test.ts",
"attrs": { "end": "1 fail", "tone": "danger" } },
{ "kind": "row", "text": "auth/token.test.ts",
"attrs": { "end": "1 fail", "tone": "danger" } }
] },
{ "kind": "bar", "kids": [
{ "kind": "button",
"attrs": { "label": "Fix both", "intent": "fix", "tone": "accent" } },
{ "kind": "button", "attrs": { "label": "Show the first one", "intent": "show" } }
] }
]点击“Fix both”会将 [view:triage <address>] from operator: tapped "Fix both" 输入到会话中。按钮仅发送其标签,不发送其他任何内容。collie trick view 的所有动词都在 参考 中列出。
选项的勾选标记由 Collie 负责渲染,绝不是由 agent 渲染。按钮不接受 pressed,决策块不接受 picked:否则写入会被拒绝。当下一步需要相同的问题时,保留其数据块,Collie 就会保留其上的勾选标记。链接只能指向网页地址,手机会在链接后显示其主机名。agent 输出的文本中不得包含 Collie 自带的字样,例如“From Collie”或“Tap again to send”。agent view 位置 列出了全部此类字样。
向用户提议使用 trick
编写了程序 trick 的 agent 会将其交给用户。Collie 没有为 agent 提供启用 trick 的途径。以你的身份运行的程序仍然可以执行此操作,因此 agent 会移交该 trick 并停止。
collie trick propose ~/work/idle-watchpropose 从 agent 自己的工作文件夹中提取草稿,例如由 init 生成的 ./<id>/。它不会向此机器的 tricks 添加任何内容。propose 运行与 collie trick check 相同的检查,然后将文件作为卡片发送给用户:“Switch on the trick idle-watch?”。该卡片会显示在手机上该 agent 自己的会话中、在“Settings, Tricks and the Hill”的“Waiting for your yes”下,以及在 the Hill 处于开启状态时的 the Hill 上。点击一次“Switch it on”即可精确批准该卡片显示的代码并开启该 trick。点击“Not now”会在会话中关闭该卡片,且该 trick 仍会在“Settings, Tricks and the Hill”中等待。它会携带 media/ 和 captures/ 的 PNG、JPEG 及 WebP 图片(最多 50 张,每张 2 MB),卡片会列出每张图片的名称。它会留下点文件、node_modules/、日志、inputs/、config.toml 以及 media/ 或 captures/ 中的任何其他文件,并列出留在后面的这两个文件夹中的每个文件。它会打印卡片的 id。使用 collie hill status <id> 读取答复,或以消息形式等待答复。消息会说明发生的情况:trick 已安装并开启,或者用户拒绝且未安装任何内容。
trick 必须在没有 config.toml 的情况下也能运行。当目录中存在该文件时,propose 首先会在缺少它的情况下运行一次该 trick,正如 bridge 在获得确认后运行它的方式一样。如果此时 trick 未绘制任何内容、失败或输出了 Collie 拒绝的部件,propose 会输出警告并仍会发送它,卡片会告知用户在添加配置文件之前可能什么都不显示。此时应绘制一条关于缺少配置的简短消息。状态文本最多 12 个字符,因此 "not set up" 放得下,而 "no folder set" 放不下。
卡片会显示代码的每一个文件,且不提供任何推荐意见:由用户自行阅读进行核验。辅助文件 trick.ts 以及由 init 编写的 AGENTS.md 只显示名称而不展示内容,前提是它们与 Collie 自带的内容逐字节完全一致。修改过的 AGENTS.md 仅显示更改内容。
文本搜索会读取 run 行作为代码指定的每个文件,以及此类文件通过相对路径导入的每个文件,无论其名称或目录为何:测试、fixture 或 .txt 文件也包括在内。如果 run 行向 shell 或解释器传递了搜索未读取的内容,例如二进制文件、trick 之外的路径或该行本身包含的代码,则会收到对应的警告。在用户同意之前,任何内容都不会运行。包含 rules 或 agent view 的 trick 无法被提议:"A trick that gives agents instructions cannot be proposed by an agent. Ask your person to add it on the machine."。限制条件见 来自 agent 的提议。
一个仓库中的多个 trick
一个 https git 仓库可以容纳多个 trick,每个 trick 各占一个目录。
collie trick add https://github.com/owner/tricks//idle-watch
collie trick add https://github.com/owner/tricks//triage#v1// 后面的部分指定目录名称,# 指定分支、标签或完整的 commit。每个目录都是一个完整的 trick,带有自己的 trick.toml。程序仅导入其自身目录下的文件,因此两个 trick 之间不共享任何代码:需要将辅助文件分别复制到每个目录中。collie trick update <id> 会重新拉取同一个目录,新代码等待用户确认。
在仓库顶层的 collie-tricks.json 文件中列出这些 trick,以便用户无需知道目录名称即可查看其中包含的内容:
{
"contract": 1,
"tricks": [
{ "folder": "idle-watch", "name": "Idle Watch", "about": "Marks a session that waits for you." },
{ "folder": "triage", "name": "Triage", "about": "Sorts the open issues by label." }
]
}该文件仅为一个列表,无其他内容。folder 写为 trick id 并在顶层命名一个目录;name 和 about 各占一行。Collie 会拒绝其不认识的键、超过 50 个 trick、超过 32 KiB 的文件以及其无法读取的 contract。其他限制在 参考 中说明。
collie trick add https://github.com/owner/tricks 随后打印列表且不添加任何内容。在手机上,“Settings → Tricks and the Hill → Add a trick from a repository”会显示该列表,并且每次点击仅从 github.com、gitlab.com 和 codeberg.org 添加一个 trick。该列表不授予任何权限:每个 trick 仍会被获取、检查并在关闭状态下添加,其本身的 trick.toml 优先于列表的名称和行。仓库列表绝不会使 trick 成为 Official。
为仓库添加 GitHub topic colliepwa-tricks,以便用户找到它。官方 trick 也以相同方式保存在 https://github.com/AltanS/collie-tricks 中。