Zum Inhalt springen

Automatisieren Lua-Scripting

Paneflow führt Lua 5.4 an zwei Stellen aus. paneflow do führt ein Skript in einem eigenen Prozess aus, aus deinem Terminal oder aus dem Shell-Tool eines Agenten. Der Worker, der Hintergrundprozess jedes Paneflow-Homes, lädt init.lua und führt die dort deklarierten Aktionen aus, wenn du sie in der Befehlspalette wählst oder ein Skript oder ein Agent eine davon anfordert.

Beide erreichen dasselbe Register, das die CLI nutzt. Jede Methode ist eine Lua-Funktion mit ihrem Namen: surface.list ist paneflow.surface.list, und action.invoke ist paneflow.action.invoke. Ein Skript kann viele Methoden aufrufen und ein kleines Ergebnis zurückgeben, sodass ein Agent einen einzigen Shell-Aufruf braucht, wo er sonst einen Tool-Aufruf pro Methode bräuchte.

TL;DR für Agenten. Führe paneflow api list aus, um jede Methode zu sehen, und paneflow api describe <method> für ihre Parameter. Führe einen Chunk mit paneflow do -e 'return paneflow.surface.list()' aus. Liste die Aktionen, die du ausführen darfst, mit paneflow actions auf, und führe eine mit paneflow do <name> key=value --wait 20s aus. Exit-Code 4 mit running <run_id> bedeutet, dass die Aktion noch läuft: Mach mit paneflow do --resume <run_id> --wait 20s weiter.

Wie führe ich ein Skript aus?

paneflow do nimmt eine Lua-Datei, einen Chunk nach -e oder -, um das Skript von der Standardeingabe zu lesen. Als Datei gilt alles, was auf .lua endet oder ein Pfadtrennzeichen enthält; jedes andere Wort ist der Name einer Aktion (siehe unten). Der Wert, den das Skript zurückgibt, wird als eine JSON-Zeile ausgegeben.

-- 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

Die key=value-Paare nach dem Skript füllen paneflow.args, typisiert nach ihrer Schreibweise: n=2 ist eine Zahl, on=true ein Boolean, alles andere ein String. --args nimmt stattdessen rohes JSON, oder @path, um es aus einer Datei zu lesen, und --json rückt das Ergebnis ein.

$ 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
}

Ein aus einem Pane gestartetes Skript empfängt die Events des Workspace dieses Panes, oder mit --all die Events jedes Workspace. --for 30s stoppt es nach dieser Dauer, --memory-limit <mib> begrenzt den Speicher des Lua-Zustands, und --deadline <seconds> stoppt Lua-Code, der so lange läuft, ohne zu warten. paneflow.stop(n) beendet das Skript mit Exit-Code n, und Ctrl-C beendet es mit 130.

Die Standardbibliothek ist die von Lua, mit zwei Ausnahmen: io.popen und os.execute werden verweigert, weil paneflow.exec ein Programm aus einer Argumentliste startet, nie über eine Shell. require findet Module neben dem Skript und im Ordner lua des Paneflow-Homes.

$ 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

Was passiert, wenn ein Skript fehlschlägt?

Ein Lua-Fehler gibt Error: <message> mit Datei und Zeile aus und endet mit 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>

Eine fehlschlagende Methode löst keinen Fehler aus. Sie gibt nil, die Meldung und den JSON-RPC-Fehlercode zurück, sodass das Skript entscheidet, was es tut:

$ 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"}

Ein Skript ist ein Client wie die CLI: Ein Schreibzugriff in ein Pane durchläuft dieselben Gates wie paneflow send, beschrieben unter Wie schreibe ich sicher?.

Wie deklariere ich Aktionen in init.lua?

init.lua liegt im Paneflow-Home: ~/.paneflow/init.lua, oder %USERPROFILE%\.paneflow\init.lua unter Windows. Der Worker lädt die Datei beim Start und erneut bei jedem Speichern. Jedes paneflow.action{...} deklariert eine Aktion: einen kleingeschriebenen name, einen title, den die Befehlspalette zeigt, die Typen ihrer args und die Funktion 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 steht hier für einen langen Job: Es schläft nur. Ein Argumenttyp ist string, number, integer, boolean, object, array oder any, optional, wenn er auf ? endet, oder eine JSON-Schema-Tabelle. Die Argumente werden gegen die Typen geprüft, bevor run startet. ctx.origin sagt, was den Durchlauf gestartet hat: palette, key, cli, agent und so weiter.

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> lädt eine Datei in einem eigenen Prozess und gibt ihre Probleme mit Datei und Zeile aus, sodass ein Fehler nie den Worker erreicht. Ein fehlgeschlagenes Neuladen behält die vorherige Konfiguration. paneflow actions --json gibt jede Aktion mit ihrem Argumentschema, ihrem agents-Flag und dem Ort ihrer Deklaration aus.

Wie führe ich eine Aktion aus?

paneflow do <name> führt die Aktion auf dem Worker aus und gibt aus, was run zurückgegeben hat. Ein falsches Argument schlägt fehl, bevor run startet.

$ 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

Ein Durchlauf wird nach 30 Sekunden gestoppt. Die Zeit, in der paneflow.ui.input oder paneflow.ui.pick auf den Nutzer wartet, zählt nicht.

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

Um eine Aktion auszuprobieren, ohne die Konfiguration des Workers anzufassen, führe sie aus der Datei in deinem eigenen Prozess aus: paneflow do --action greet ./init.lua name=arthur.

Wie verhindere ich, dass eine lange Aktion meine Shell blockiert?

Ohne --wait wartet paneflow do <name>, bis die Aktion endet. Das Shell-Tool eines Agenten hat ein eigenes Timeout, also übergib --wait <duration>: Die CLI wartet höchstens so lange. Endet die Aktion vorher, wird ihr Ergebnis ausgegeben und der Exit-Code ist 0. Läuft sie noch, gibt die CLI running <run_id> aus und endet mit 4, und die Aktion läuft auf dem Worker weiter.

$ 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> wartet weiter, höchstens so lange wie sein eigenes --wait, wenn du eines übergibst. Das Ergebnis eines Durchlaufs wird einmal zurückgegeben; ein erneutes Fortsetzen schlägt fehl:

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

--wait gilt nur für Aktionen. Ein Skript läuft im Prozess von paneflow do, bis es endet, daher schlägt paneflow do panes.lua --wait 5s fehl mit --wait applies to an action of init.lua, run by its name; a script runs in this process until it ends.

Welche Aktionen kann ein Agent ausführen?

Eine Aktion läuft für einen Agenten nur, wenn sie agents = true deklariert. Der Worker erkennt einen Agenten am aufrufenden Prozess: Ein paneflow do, das ein Paneflow bekannter Coding-Agent wie Claude Code oder Codex in einem Pane startet, läuft als dieser Agent, welchen Ursprung der Aufruf auch angibt. Für einen Agenten existiert eine Aktion ohne agents = true nicht: Sie schlägt mit derselben Meldung fehl wie ein unbekannter Name.

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

Ein Agent führt höchstens 3 Aktionen in einem Burst aus, danach 1 pro Sekunde. Darüber hinaus schlägt paneflow do mit rate limited fehl, und der Agent versucht es eine Sekunde später erneut. Du führst jede Aktion aus, aus deinem Terminal, über eine Taste oder die Befehlspalette.

Wie frage ich den Nutzer aus einem Skript oder einer Aktion?

paneflow.ui.input fragt nach einer Textzeile und paneflow.ui.pick nach einer Auswahl. Paneflow zeigt die Frage in jedem Fenster, das sie zeigen kann, nennt die fragende Aktion oder das fragende Skript, und die erste Antwort gewinnt. Beide geben die Antwort zurück, oder nil und den Grund, warum niemand geantwortet hat: cancelled, timeout, no_client (kein Fenster ist offen), unsupported, busy oder failed mit einem Detail.

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"}

Ohne offenes Fenster enden dieselben Durchläufe sofort:

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

Ein Auswahleintrag ist ein String, der zugleich seine ID und sein Titel ist, oder eine Tabelle mit id, title, subtitle, icon und detail. Mit multiple = true ist die Antwort die Liste der IDs, die der Nutzer angehakt hat, in der Reihenfolge von items; mit allow_other = true darf der Nutzer Text eingeben, der zu keinem Eintrag passt, und die Antwort ist { text = ... }. Eine Frage wartet standardmäßig 300 Sekunden; setze timeout, höchstens 3600. Beide Funktionen arbeiten in einer Aktion, einem Event-Handler, einem Timer oder einem paneflow do-Skript, aber nicht, während init.lua lädt.

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

Aus der Shell eines Agenten stellt paneflow ask dieselbe Art von Frage ohne Skript; siehe Wie schreiben Agenten einander und stellen dir Fragen?.

Wie finde ich die Methoden, die ein Skript aufrufen kann?

paneflow api list listet jede Methode, die die laufenden Prozesse bereitstellen, mit dem Prozess, dem sie gehört. paneflow api describe <method> zeigt ihre Parameter, ihre Ergebnisfelder und einen Beispielaufruf. Dieselben Namen, mit einem Punkt, sind die Lua-Funktionen.

$ 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 und paneflow api describe --json geben die vollständigen Deskriptoren aus. paneflow api call <method> key=value ruft eine Methode ohne Lua auf, und paneflow api stubs gibt eine Definitionsdatei für einen Lua-Language-Server aus, sodass dein Editor jede paneflow.*-Funktion vervollständigt.

Verwandte Seiten