Turn programs in Lua
Each turn is a Lua program. The default, programs/turn.lua, sends a request to the model, runs the tools it asks for, adds the results to the context, and repeats until the model answers without calling a tool.
At the start of every turn Blackbird reads the selected program's source, records it in the session, and runs it in a fresh Lua VM. Edit the selected file and the next turn runs your edited version.
Use your own program
An installed Blackbird runs the copy of turn.lua inside its install directory. To customise it, copy it, edit the copy, and select it:
cp "$HOME/.local/lib/blackbird/current/share/blackbird/programs/turn.lua" my-turn.lua
blackbird --workflow my-turn.lua
Inside a session, /workflow FILE selects a different program from the next turn on. Blackbird saves the program's absolute path with the session; /session shows which one is selected.
/lua CODE runs a one-off snippet as a program, which is handy for trying the API:
/lua print(blackbird.json.encode(blackbird.stats()))
The default loop
Abridged from programs/turn.lua. The full file also sets default output limits for some tools.
while true do
local response = blackbird.request()
local calls = 0
for _, item in ipairs(response.output) do
if item.type == "function_call" then
calls = calls + 1
local result = blackbird.call(item.name, item.arguments, { call_id = item.call_id })
blackbird.append({
{ type = "function_call_output", call_id = item.call_id, output = result },
})
elseif item.type == "message" then
blackbird.present(item)
end
end
if blackbird.restarting() or calls == 0 then
return
end
end
The host API
Programs reach Blackbird through the blackbird table:
blackbird.request(options)sends a model request built from the live context. Options can override the model, instructions, tools and other request fields for that one call.blackbird.call(name, args)runs a tool, the same tools the model has, such asread_file,write_file,edit_fileandexec.blackbird.append(items)adds items to the context.blackbird.present(item)shows a finished assistant message.blackbird.display(text)andprintshow text.blackbird.stats()reports context sizes in bytes, not tokens.blackbird.blocks(),blackbird.read_block(id)andblackbird.compact(args)work with context; see Managing context.blackbird.json.encodeandblackbird.json.decodeconvert JSON;blackbird.array({})makes an empty array.blackbird.define_tool(definition)defines a new tool in Lua. It becomes callable once the program that defined it finishes successfully, and it stays defined when the session is reopened.
local result = blackbird.call("read_file", {
path = "README.md",
range = { mode = "lines", start = 1, ["end"] = 30 },
})
local state = blackbird.stats()
blackbird.display(blackbird.json.encode(state))
The model has a lua tool with the same API, so it can write and run programs like this during ordinary work.
What Lua can and can't do
The base, coroutine, table, string, math and utf8 libraries are available, and so is load. The io, package and debug libraries are absent, dofile and loadfile are removed, and os contains only time, clock and difftime. File and process effects go through blackbird.call, so they are recorded in the session.
This keeps effects visible. It is not a sandbox: a program can run any command through the exec tool. See What Blackbird can do on your machine.
Globals don't survive between turns, because every turn starts a new VM. Keep lasting state in the context, in files, or in services.
Hooks
Hooks run at five points: turn start, turn end, before a tool call, after a tool call, and on context compaction. A hook can observe, transform context, or, before a tool call, refuse it. Hooks are defined in the session's program configuration through the program_config tool, and changes take effect at the next turn boundary.
/hooks shows the current hooks. /hooks history shows recent hook runs, /hooks enable NAME and /hooks disable NAME toggle one, and /hooks explain JSON shows which hooks would match an event without running them.
Callable workflows
Lua workflows can also be registered as slash commands. New sessions include one, /ultracode TASK, which runs a bounded coding turn with added instructions.