跳到内容

自动化 Lua 脚本

Paneflow 在两个地方运行 Lua 5.4。paneflow do 在自己的进程中运行脚本,可以从你的终端或智能体的 shell 工具中调用。worker 是每个 Paneflow home 的后台进程,它会加载 init.lua,并在你于命令面板中选中某个 action,或脚本、智能体请求运行某个 action 时,运行其中声明的 action。

两者访问的都是 CLI 使用的同一个 registry。每个 method 都是一个以其名称命名的 Lua 函数:surface.list 是 paneflow.surface.list,action.invoke 是 paneflow.action.invoke。一个脚本可以调用多个 method 并返回一个小结果,因此智能体只需一次 shell 调用,而不必为每个 method 各花一次工具调用。

给智能体的 TL;DR。 运行 paneflow api list 查看每个 method,运行 paneflow api describe <method> 查看其参数。用 paneflow do -e 'return paneflow.surface.list()' 运行一段代码。用 paneflow actions 列出你可以运行的 action,用 paneflow do <name> key=value --wait 20s 运行其中一个。退出码 4 加上 running <run_id> 表示 action 仍在运行:用 paneflow do --resume <run_id> --wait 20s 继续等待。

如何运行脚本?

paneflow do 接受一个 Lua 文件、-e 之后的一段代码,或用 - 从标准输入读取脚本。以 .lua 结尾或包含路径分隔符的参数都视为文件;其他任何单词都是 action 的名称(见下文)。脚本返回的值会打印为一行 JSON。

-- panes.lua
local listed = paneflow.surface.list()
return { panes = #listed.surfaces, who = paneflow.args.who }
$ paneflow do panes.lua who=arthur
{"panes":2,"who":"arthur"}

$ paneflow do -e 'return #paneflow.surface.list().surfaces'
2

$ echo 'return paneflow.args.n * 2' | paneflow do - n=21
42

脚本之后的 key=value 对会填入 paneflow.args,并按写法确定类型: n=2 是数字,on=true 是布尔值,其他都是字符串。--args 改为接受原始 JSON,或用 @path 从文件读取,--json 会缩进输出结果。

$ paneflow do -e 'return paneflow.args' n=2 on=true env=prod
{"env":"prod","n":2,"on":true}

$ paneflow do --json -e 'return {n = paneflow.args.n}' --args '{"n": 3}'
{
  "n": 3
}

从面板启动的脚本会接收该面板所在工作区的事件,使用 --all 时则接收所有工作区的事件。--for 30s 会在这段时间后停止脚本,--memory-limit <mib> 限制 Lua 状态的内存,--deadline <seconds> 会停止连续运行这么久而没有等待的 Lua 代码。paneflow.stop(n) 以退出码 n 结束脚本,Ctrl-C 以 130 退出。

标准库就是 Lua 的标准库,但有两个例外:io.popen 和 os.execute 会被拒绝,因为 paneflow.exec 从参数列表运行程序,从不经过 shell。require 会在脚本旁边以及 Paneflow home 的 lua 文件夹中查找模块。

$ paneflow do -e 'return io.popen("ls")'
Error: (command line):1: io.popen is not available in Paneflow scripts: run programs with paneflow.exec, which takes an argument list and never starts a shell

脚本失败时会怎样?

Lua 错误会打印 Error: <message>,附带文件和行号,并以 1 退出。

-- hello.lua
return "hello " .. paneflow.args.name:upper()
$ paneflow do hello.lua name=arthur
"hello ARTHUR"

$ paneflow do hello.lua
Error: hello.lua:1: attempt to index a nil value (field 'name')

$ paneflow do -e 'error("deploy refused: tests are red")'
Error: (command line):1: deploy refused: tests are red

$ paneflow do -e 'return 1 +'
Error: (command line):1: unexpected symbol near <eof>

失败的 method 不会抛出错误。它返回 nil、错误消息和 JSON-RPC 错误码,由脚本决定如何处理:

$ paneflow do -e 'local result, message, code = paneflow.surface.read{ surface_id = 999 } return { message = message, code = code }'
{"code":-32000,"message":"Surface not found: no pane 999 on this host"}

脚本和 CLI 一样是客户端:写入面板要通过与 paneflow send 相同的 gate,详见如何安全写入?。

如何在 init.lua 中声明 action?

init.lua 位于 Paneflow home 中:~/.paneflow/init.lua,在 Windows 上为 %USERPROFILE%\.paneflow\init.lua。worker 在启动时加载它,并在你每次保存时重新加载。每个 paneflow.action{...} 声明一个 action:小写的 name、命令面板显示的 title、args 的类型,以及 run 函数。

-- ~/.paneflow/init.lua
paneflow.action{
name = "greet",
title = "Greet",
args = { name = "string" },
run = function(ctx, args)
  return { greeting = "hello " .. args.name, origin = ctx.origin }
end,
}

paneflow.action{
name = "build",
title = "Build the project",
description = "Wait for a build to finish",
agents = true,
args = { seconds = "number?" },
run = function(ctx, args)
  paneflow.sleep(args.seconds or 12)
  return "build finished"
end,
}

这里的 build 代表一个耗时较长的任务:它只是 sleep。参数类型可以是 string、 number、integer、boolean、object、array 或 any,以 ? 结尾时为可选,也可以是一个 JSON Schema 表。参数会在 run 开始前按类型检查。ctx.origin 说明是什么启动了这次运行:palette、key、cli、agent 等。

paneflow config check ~/.paneflow/init.lua
paneflow config reload
paneflow actions
$ paneflow config reload
/home/you/.paneflow/init.lua: loaded (2 actions, 0 bindings, 0 handlers, 0 timers)

$ paneflow actions
greet  Greet  [Actions]  init.lua:1
build  Build the project  [Actions]  init.lua:10

paneflow config check <file> 在自己的进程中加载文件,并打印带文件和行号的问题,因此错误永远不会进入 worker。重新加载失败时,会保留之前的配置。 paneflow actions --json 会打印每个 action 及其参数 schema、agents 标志和声明位置。

如何运行 action?

paneflow do <name> 在 worker 上运行 action,并打印 run 返回的内容。参数错误会在 run 开始前失败。

$ paneflow do greet name=arthur
{"greeting":"hello arthur","origin":"cli"}

$ paneflow do greet
Error: paneflow error -32050: missing argument "name"; the action takes name

$ paneflow do greet name=arthur extra=1
Error: paneflow error -32050: no argument named "extra"; the action takes name

$ paneflow do nope
Error: paneflow error -32602: no action named 'nope'; paneflow actions lists the actions init.lua declares

一次运行会在 30 秒后被停止。在 paneflow.ui.input 或 paneflow.ui.pick 中等待用户的时间不计入其中。

$ paneflow do build seconds=40
Error: paneflow error -32051: timeout: the action build was stopped after 30 s

要在不影响 worker 配置的情况下试用某个 action,可以在你自己的进程中从文件运行它: paneflow do --action greet ./init.lua name=arthur。

如何避免耗时较长的 action 阻塞 shell?

不带 --wait 时,paneflow do <name> 会一直等到 action 结束。智能体的 shell 工具有自己的超时,因此请传入 --wait <duration>:CLI 最多等待这么久。如果 action 先结束,会打印其结果,退出码为 0。如果它仍在运行,CLI 会打印 running <run_id> 并以 4 退出,action 则继续在 worker 上运行。

$ paneflow do build seconds=1 --wait 10s
"build finished"

$ paneflow do build --wait 5s
running b322557838e3d024cc35e884b915b67a

$ paneflow do --resume b322557838e3d024cc35e884b915b67a --wait 20s
"build finished"

--resume <run_id> 会继续等待,如果传入了 --wait,则最多等待该时长。一次运行的结果只返回一次;再次 resume 会失败:

$ paneflow do --resume b322557838e3d024cc35e884b915b67a
Error: paneflow error -32602: no action run with id b322557838e3d024cc35e884b915b67a; its result was already returned or it never existed

--wait 只适用于 action。脚本会在 paneflow do 进程中一直运行到结束,因此 paneflow do panes.lua --wait 5s 会失败并提示 --wait applies to an action of init.lua, run by its name; a script runs in this process until it ends。

智能体可以运行哪些 action?

只有声明了 agents = true 的 action 才能为智能体运行。worker 根据调用它的进程识别智能体:由 Paneflow 认识的编码智能体(例如 Claude Code 或 Codex)在面板中启动的 paneflow do,无论调用声明的 origin 是什么,都会以该智能体的身份运行。对智能体来说,没有 agents = true 的 action 并不存在:它会以与未知名称相同的消息失败。

Error: paneflow error -32602: no action named 'greet'; paneflow actions lists the actions init.lua declares

智能体一次突发最多运行 3 个 action,之后每秒 1 个。超过这个限制时, paneflow do 会以 rate limited 失败,智能体在一秒后重试。你可以运行每个 action,无论是从终端、快捷键还是命令面板。

如何从脚本或 action 向用户提问?

paneflow.ui.input 请求一行文本,paneflow.ui.pick 请求一个选择。Paneflow 会在每个能够显示问题的窗口中显示它,并注明提问的 action 或脚本,以第一个回答为准。每个函数都返回回答,或返回 nil 以及无人回答的原因:cancelled、timeout、 no_client(没有打开的窗口)、unsupported、busy,或带详细信息的 failed。

paneflow.action{
name = "new_branch",
title = "New branch",
agents = true,
run = function()
  local name, reason = paneflow.ui.input{ title = "Branch name", value = "feature/" }
  if not name then return { cancelled = reason } end
  return { branch = name }
end,
}

paneflow.action{
name = "deploy",
title = "Deploy",
run = function()
  local target, reason = paneflow.ui.pick{
    title = "Deploy to",
    items = { "staging", { id = "prod", title = "Production", icon = "server" } },
  }
  if not target then return { cancelled = reason } end
  return { deployed = target }
end,
}
$ paneflow do new_branch
{"branch":"feature/login"}

$ paneflow do deploy
{"deployed":"prod"}

没有打开的窗口时,同样的运行会立即结束:

$ paneflow do new_branch
{"cancelled":"no_client"}

pick 的选项可以是一个字符串,它同时作为自身的 id 和 title;也可以是一个带有 id、title、subtitle、icon 和 detail 的表。使用 multiple = true 时,回答是用户勾选的 id 列表,顺序与 items 相同;使用 allow_other = true 时,用户可以输入不匹配任何选项的文本,回答为 { text = ... }。问题默认等待 300 秒;可设置 timeout,最多 3600。这两个函数可以在 action、事件 handler、timer 或 paneflow do 脚本中使用,但不能在加载 init.lua 期间使用。

$ paneflow do -e 'return paneflow.ui.pick{ title = "Deploy targets", items = { "staging", "prod", "dev" }, multiple = true }'
["staging","prod"]

在智能体的 shell 中,paneflow ask 无需脚本即可提出同类问题;参见智能体如何互相发送消息并向你提问?。

如何查找脚本可以调用的 method?

paneflow api list 列出正在运行的进程提供的每个 method,以及拥有它的进程。 paneflow api describe <method> 显示其参数、结果字段和示例调用。这些名称(带点号)就是对应的 Lua 函数。

$ paneflow api list | grep '^action\.'
action.list               worker  List the actions init.lua declared, with their titles, argument schemas and sources.
action.invoke             worker  Run an action of init.lua with arguments and return what it returned. Arguments are checked against its schema first; a run is stopped after 30 seconds. With wait_ms, a run still going past it answers {status: running, run_id} for action.wait. With ctx.origin agent, only an action declared with agents = true runs.
action.wait               worker  Keep waiting on an action run that action.invoke returned as {status: running, run_id}: the run's result with status done, the same running status, or the action's error.

$ paneflow api describe action.wait
action.wait
  Keep waiting on an action run that action.invoke returned as {status: running, run_id}: the run's result with status done, the same running status, or the action's error.

  owner: worker   gate: none   reply: ready

Parameters:
  run_id   string                required  default -
  wait_ms  integer >= 0          optional  default -         Milliseconds to wait, 0 by default, at most 25000

Result fields:
  result  any
  status  "done"|"running"|"rate_limited"
  run_id  string

Example:
  paneflow api call action.wait run_id=6f1c0e9a2b7d4c3e8f5a1b2c3d4e5f60 wait_ms=20000

paneflow api list --json 和 paneflow api describe --json 会打印完整的描述符。 paneflow api call <method> key=value 无需 Lua 即可调用单个 method, paneflow api stubs 会为 Lua language server 打印定义文件,让编辑器补全每个 paneflow.* 函数。

相关