Documentation
¶
Overview ¶
Package agenthooks defines the experimental wire protocol for Coder agent lifecycle hooks. The protocol, including SchemaVersion 1, has no backward-compatibility guarantee.
Coder persists no hook dispatch state, so delivery is best-effort and may duplicate. A failed dispatch is never queued for redelivery; hooks are fail closed, so the operation that raised the event fails instead. Consumers must therefore tolerate duplicates without assuming every event arrives.
A retried HTTP attempt reuses its Meta.DispatchID, so consumers deduplicate transport retries by that ID. A repeated logical event gets a new ID, so keep side effects keyed on the event's own identifiers, such as tool_use_id, or make them safe to repeat.
Index ¶
- Constants
- func NewHTTPHandler(secret []byte, expectedAudience string, hooks Hooks, opts ...HandlerOption) http.Handler
- func SignClaims(secret []byte, claims Claims) (string, error)
- type ChatRef
- type Claims
- type EventType
- type HandlerOption
- type Hooks
- type Meta
- type Permission
- type PermissionDecision
- type PostCompactData
- type PostToolUseData
- type PreCompactData
- type PreToolUseData
- type Request
- type Response
- type SessionStartData
- type StopData
- type UserPromptSubmitData
Constants ¶
const MaxRequestBodyBytes int64 = 10 << 20 // 10 MiB
MaxRequestBodyBytes limits memory used to verify hook requests.
const MinSecretLen = 32
MinSecretLen is the minimum HS256 secret length in bytes. go-jose accepts shorter keys, so signing and verification enforce it to fail closed on missing or weak secrets.
const SchemaVersion = 1
SchemaVersion is the current lifecycle hook request schema version.
Variables ¶
This section is empty.
Functions ¶
func NewHTTPHandler ¶
func NewHTTPHandler(secret []byte, expectedAudience string, hooks Hooks, opts ...HandlerOption) http.Handler
NewHTTPHandler verifies hook POSTs, binds their claims to each request, and routes events to their configured callbacks. expectedAudience must be the URL Coder dispatches to, which is the value it signs into the aud claim. Deriving it from the request instead would let a caller replay a token minted for a different listener, because the request URL, the Host header, and any forwarding headers are all caller-controlled. A handler built with an empty audience rejects every request.
Types ¶
type ChatRef ¶
type ChatRef struct {
ChatID uuid.UUID `json:"chat_id"`
OwnerID uuid.UUID `json:"owner_id"`
WorkspaceID *uuid.UUID `json:"workspace_id,omitempty"`
TurnID *uuid.UUID `json:"turn_id,omitempty"`
ParentChatID *uuid.UUID `json:"parent_chat_id,omitempty"`
// RootChatID identifies the user-facing root of the chat tree.
RootChatID *uuid.UUID `json:"root_chat_id,omitempty"`
}
ChatRef identifies the chat a lifecycle hook event refers to.
type Claims ¶
type Claims struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Audience string `json:"aud"`
IssuedAt int64 `json:"iat"`
NotBefore int64 `json:"nbf"`
Expires int64 `json:"exp"`
JTI uuid.UUID `json:"jti"`
Type EventType `json:"type"`
BodySHA256 string `json:"body_sha256"`
}
Claims describes the JWT minted by coderd for a lifecycle hook dispatch.
type EventType ¶
type EventType string
EventType names a lifecycle event carried by a hook request.
const ( EventSessionStart EventType = "session_start" EventUserPromptSubmit EventType = "user_prompt_submit" EventPreToolUse EventType = "pre_tool_use" EventPostToolUse EventType = "post_tool_use" EventPreCompact EventType = "pre_compact" EventPostCompact EventType = "post_compact" EventStop EventType = "stop" )
type HandlerOption ¶
type HandlerOption func(*handlerOptions)
HandlerOption configures NewHTTPHandler.
func WithExpectedIssuer ¶
func WithExpectedIssuer(issuer string) HandlerOption
WithExpectedIssuer requires the verified iss claim to match issuer. If omitted, NewHTTPHandler accepts any non-empty issuer signed with the secret.
type Hooks ¶
type Hooks struct {
SessionStart func(context.Context, Meta, SessionStartData) (Response, error)
UserPromptSubmit func(context.Context, Meta, UserPromptSubmitData) (Response, error)
PreToolUse func(context.Context, Meta, PreToolUseData) (Response, error)
PostToolUse func(context.Context, Meta, PostToolUseData) (Response, error)
PreCompact func(context.Context, Meta, PreCompactData) (Response, error)
PostCompact func(context.Context, Meta, PostCompactData) (Response, error)
Stop func(context.Context, Meta, StopData) (Response, error)
}
Hooks lets a consumer implement only the lifecycle events it uses.
type Meta ¶
type Meta struct {
DispatchID uuid.UUID `json:"dispatch_id"`
SchemaVersion int `json:"schema_version"`
ChatRef
}
Meta identifies a hook dispatch and its chat.
type Permission ¶
type Permission struct {
Decision PermissionDecision `json:"decision"`
Reason string `json:"reason,omitempty"`
InputOverride json.RawMessage `json:"input_override,omitempty"`
}
Permission controls whether mutable hook input may proceed.
type PermissionDecision ¶
type PermissionDecision string
PermissionDecision is a consumer's verdict on mutable hook input.
const ( PermissionAllow PermissionDecision = "allow" PermissionDeny PermissionDecision = "deny" )
type PostCompactData ¶
type PostCompactData struct{}
PostCompactData is empty; Meta identifies the compacted chat.
type PostToolUseData ¶
type PostToolUseData struct {
ToolUseID string `json:"tool_use_id"`
ToolName string `json:"tool_name"`
ToolResponse json.RawMessage `json:"tool_response,omitempty"`
ToolError string `json:"tool_error,omitempty"`
}
PostToolUseData describes a completed tool call, carrying either ToolResponse or ToolError.
type PreCompactData ¶
type PreCompactData struct{}
PreCompactData is empty; Meta identifies the chat being compacted.
type PreToolUseData ¶
type PreToolUseData struct {
ToolUseID string `json:"tool_use_id"`
ToolName string `json:"tool_name"`
ToolInput json.RawMessage `json:"tool_input"`
}
PreToolUseData describes a tool call before execution.
type Request ¶
type Request struct {
Type EventType `json:"type"`
Meta Meta `json:"meta"`
Data json.RawMessage `json:"data"`
}
Request is the body coderd posts to the configured lifecycle hook URL.
type Response ¶
type Response struct {
Permission *Permission `json:"permission,omitempty"`
ModelContext string `json:"model_context,omitempty"`
UserMessage string `json:"user_message,omitempty"`
}
Response carries a consumer's decision and optional injected content. Permission is honored for user_prompt_submit and pre_tool_use only. user_prompt_submit folds injected content into the submitted message. A denied pre_tool_use yields a synthetic tool result carrying only the policy text and any Reason; ModelContext persists separately as model-only transcript content that never reaches clients.
type SessionStartData ¶
type SessionStartData struct {
Source string `json:"source"`
}
SessionStartData reports why a chat session started. Source is "startup", "resume", or "clear".
type UserPromptSubmitData ¶
type UserPromptSubmitData struct {
Prompt string `json:"prompt"`
Parts json.RawMessage `json:"parts,omitempty"`
}
UserPromptSubmitData includes concatenated text and persisted parts. Inspect Parts when structure matters.