hooks

package
v0.3.10 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 17 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 LaunchEnv = "FLOCKDECK_LAUNCH"

LaunchEnv is the variable a pane's process is started with naming that start, which the hook reads back and sends as Event.Launch. Claude Code passes its environment on to the hooks it runs, whichever form they are written in, as it does FLOCKDECK_TOKEN.

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.

func Report added in v0.3.0

func Report(endpoint, token string, r spend.Report, timeout time.Duration) error

Report is the client half: it posts one report and gives up after timeout.

It is sent from inside an agent's own loop -- after an API call, or on every tick of Claude Code's status line -- so it must never hold that loop up. A Flockdeck that is not there loses the figure, and nothing else.

Types

type AskOption added in v0.3.9

type AskOption struct {
	Label       string `json:"label"`
	Description string `json:"description,omitempty"`
}

type AskQuestion added in v0.3.9

type AskQuestion struct {
	Header      string      `json:"header,omitempty"`
	Question    string      `json:"question"`
	MultiSelect bool        `json:"multiSelect,omitempty"`
	Options     []AskOption `json:"options"`
}

AskQuestion and AskOption are one question of an AskUserQuestion call, read straight off its tool_input -- Claude Code's own field names already match these tags, so there is nothing to translate.

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"`
	// Launch names the start of the pane's process the event comes from: the
	// pane's LaunchEnv, which is new each time the pane is started. A restarted
	// pane keeps its id, and a hook the process before it ran a moment too
	// late -- which on Windows can outlive the process that ran it -- is
	// otherwise an event about the new one. Empty from a hook that predates it.
	Launch string `json:"launch,omitempty"`
	// ToolInput is what a PreToolUse call's own tool_input says, kept to the
	// parts worth carrying past this process and each capped -- a Write's
	// whole file, say, never travels beyond what buildToolInput keeps of it --
	// then re-marshalled as compact JSON. Empty for every event but
	// PreToolUse, and for a PreToolUse whose input said nothing this cares
	// about. It is opaque to everything between here and the phone-facing
	// view that finally reads it (internal/server's waitingViews): Session
	// only stores and carries it forward the way it already does Tool.
	ToolInput string `json:"toolInput,omitempty"`
	// NotificationType is what a Notification is about -- Claude Code's
	// notification_type, carried through so the receiving end can tell an idle
	// nudge ("idle_prompt") apart from a real ask ("permission_prompt" and the
	// rest); see session.IsIdleReminder. Empty for every other event, and for a
	// Notification from a Claude Code too old to say.
	NotificationType string `json:"notificationType,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) SetUsageHandler added in v0.3.0

func (s *Server) SetUsageHandler(fn func(spend.Report))

SetUsageHandler installs the function a pane's spending is delivered to.

func (*Server) Token

func (s *Server) Token() string

Token is the shared secret panes must present.

func (*Server) UsageEndpoint added in v0.3.0

func (s *Server) UsageEndpoint() string

UsageEndpoint is the URL panes post their spending to.

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.

type UsageReport added in v0.3.0

type UsageReport struct {
	spend.Report
	Token string `json:"token"`
}

UsageReport is what a pane posts to say what its agent has spent: one call's tokens from Flockdeck's own chat client, or the running totals and the subscription's windows that Claude Code hands its status line.

It goes to a route of its own rather than riding a lifecycle event, because it is not one: it changes nothing about the pane's status, and the events are Claude Code's own names, which this is not.

Jump to

Keyboard shortcuts

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