Documentation
¶
Overview ¶
Package hooks carries agents' lifecycle events from panes back to Flockdeck.
Claude Code is the case this was written for: its panes are launched with a generated --settings file registering command hooks that re-invoke this binary in `hook` mode, and those invocations POST to a loopback server Flockdeck runs, which is how a pane's status ("working", "waiting on you", "idle") is known accurately rather than being guessed from screen scraping. It is not the only case. The protocol is the event names below and nothing else, so any agent that can report its own lifecycle — including Flockdeck's own chat client, which speaks straight to a model API — reports it here, and the rest of the application never learns there was more than one kind of pane.
An agent that reports nothing is not left in the dark: its status is inferred from what the pane prints, and the briefing this server hands back on SessionStart is put in front of its opening prompt instead.
Index ¶
Constants ¶
const SessionStart = "SessionStart"
SessionStart is the lifecycle event a pane's agent fires as it starts, resumes or is compacted. It is the one event whose reply matters: the response body carries the text describing the pane the session is running in, which the hook then prints for the agent to read.
Answering it is the better of the two ways a briefing is delivered, and the reason is the compaction: a briefing put in front of an opening prompt is summarised away with everything else that was said early on, where this one is asked for again and returned afresh.
Variables ¶
This section is empty.
Functions ¶
func Emit ¶
Emit is the client half, run inside the hook subprocess. It reads Claude's hook JSON from stdin to pick up the tool name, then posts the event.
What it returns is the pane briefing, and only a SessionStart is answered with one: the caller prints it for the agent to read. An agent reporting its own lifecycle rather than being wrapped in a hook command — Flockdeck's chat client does — calls this with a nil stdin and shows what comes back the same way.
It is deliberately forgiving: a hook that fails must never block or break the session it is reporting on.
Types ¶
type Event ¶
type Event struct {
SessionID string `json:"session"`
Event string `json:"event"`
Tool string `json:"tool,omitempty"`
Cwd string `json:"cwd,omitempty"`
// Prompt is what the user asked, present on UserPromptSubmit. It is what
// lets a tab name itself after the work rather than the directory.
Prompt string `json:"prompt,omitempty"`
// Source is how a SessionStart came about: "startup", "resume", "clear",
// "compact" or "fork".
Source string `json:"source,omitempty"`
}
Event is a lifecycle notification from one pane.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server receives hook events on the loopback interface.
func Serve ¶
Serve starts a hook server on a random loopback port. Events are delivered to on, which may be called from multiple goroutines.
func (*Server) SetContextHandler ¶
SetContextHandler installs the function that describes a pane to the agent running in it. Returning an empty string leaves the session as it was.
It serves only the agents that fire hooks. An agent that fires none is briefed through its opening prompt, which is built from the same description and never reaches this server.
func (*Server) SetSpawnHandler ¶
func (s *Server) SetSpawnHandler(fn func(SpawnRequest) (SpawnResult, error))
SetSpawnHandler installs the function that starts a child agent. It returns the new pane and where it is working, or an error explaining why it could not.
type SpawnRequest ¶
type SpawnRequest struct {
Parent string `json:"parent"`
Task string `json:"task"`
Branch string `json:"branch,omitempty"`
Split bool `json:"split,omitempty"`
Shell bool `json:"shell,omitempty"`
// Agent and Model are which agent the helper should be, for an agent
// handing work to one that is better at it than itself. Both empty is what
// every earlier build sent and means the project's default, so a helper
// asked for without an opinion is started exactly as it always was.
Agent string `json:"agent,omitempty"`
Model string `json:"model,omitempty"`
Token string `json:"token"`
}
SpawnRequest is a pane asking the application to start another agent.
This is what makes a lead agent able to hand work to helpers: it runs `flockdeck spawn`, which posts here using the address and token its pane was given in its environment.
type SpawnResult ¶
type SpawnResult struct {
PaneID string `json:"paneId"`
// Cwd is where the new agent is working. It is worth saying back because
// the caller cannot work it out: --worktree asks for a branch, and which
// directory that becomes is the application's decision, not the caller's.
Cwd string `json:"cwd,omitempty"`
}
SpawnResult is what the application answers a spawn with.
func Spawn ¶
func Spawn(api, token, parent string, req SpawnRequest) (SpawnResult, error)
Spawn is the client half, used by the `spawn` subcommand inside a pane.