hooks

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Interrupted = "Interrupted"

Interrupted is what a PostToolUseFailure is reported as when the tool failed because the user stopped it. Claude Code 2.1.269, read from its executable, says so in the payload's is_interrupt, and then goes back to its prompt without a Stop: the turn is over, which the failure of a tool on its own is not, so the two are told apart here, where the payload is read.

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

func Emit(stdin io.Reader, endpoint, token, sessionID, event string) (string, error)

Emit is the client half, run inside the hook subprocess. It reads Claude's hook JSON from stdin for the tool, the directory, the prompt and the source, and posts the event -- unless it is a Notification of something finished, which is not reported at all.

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"`
	// Conversation is the agent's own id for the conversation, where it says.
	// It is SessionID until the user runs /clear: Claude Code then carries on
	// under a new id, in a new transcript, and reports that SessionStart
	// (source "clear") under it -- while the pane is still known by the id it
	// was started with, which from then on names the conversation before.
	Conversation string `json:"conversation,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

func Serve(on func(Event)) (*Server, error)

Serve starts a hook server on a random loopback port. Events are delivered to on, which may be called from multiple goroutines.

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL is the address panes call back on.

func (*Server) Close

func (s *Server) Close() error

Close shuts the server down.

func (*Server) Endpoint

func (s *Server) Endpoint() string

Endpoint is the URL panes should post their events to.

func (*Server) SetContextHandler

func (s *Server) SetContextHandler(fn func(sessionID string) string)

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.

func (*Server) Token

func (s *Server) Token() string

Token is the shared secret panes must present.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL