Help us shape the future of Go! Take the 2026 Go Developer Survey

hooks

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: EUPL-1.2 Imports: 18 Imported by: 0

Documentation

Overview

Package hooks charge, filtre et exécute les hooks utilisateur, au format compatible avec les hooks de Claude Code.

Index

Constants

View Source
const (
	// MaxHooks borne le nombre total de hooks chargés.
	MaxHooks = 200
	// MaxFileBytes borne la taille d'un fichier de hooks (1 MiB).
	MaxFileBytes = 1 << 20
)

Variables

This section is empty.

Functions

func BuildPrompt

func BuildPrompt(tmpl string, input []byte) string

BuildPrompt remplace $ARGUMENTS par input ; absent, input est ajouté après une ligne vide.

func ClaudeName

func ClaudeName(tenon string) string

ClaudeName renvoie le nom Claude Code de l'outil tenon, ou le nom inchangé.

func FromClaude

func FromClaude(tenon string, in json.RawMessage) (json.RawMessage, error)

FromClaude traduit des arguments au format Claude Code (updatedInput d'un hook) vers le format de l'outil tenon. in doit être un objet JSON.

func Load

func Load(o Options) (*Set, []Notice, error)

Load lit les fichiers de hooks utilisateur. Une erreur de fichier utilisateur est fatale (fail closed) ; les fichiers projet ne produisent que des notices.

func ParseVerdict

func ParseVerdict(text string) (ok bool, reason string, err error)

ParseVerdict lit le verdict {"ok": bool, "reason": string} du premier « { » au dernier « } ».

func ToClaude

func ToClaude(tenon string, args json.RawMessage) json.RawMessage

ToClaude traduit les arguments d'un outil tenon vers le format Claude Code. Des arguments qui ne sont pas un objet JSON sont renvoyés inchangés.

Types

type Env

type Env struct {
	SessionID, TranscriptPath, ProjectRoot string
}

Env regroupe le contexte d'exécution commun à tous les hooks.

type Evaluator

type Evaluator interface {
	Evaluate(ctx context.Context, h Hook, prompt string) (text string, err error)
}

Evaluator exécute les hooks prompt et agent : il renvoie le texte du modèle.

type Event

type Event string

Event est le nom d'un événement de hook.

const (
	PreToolUse       Event = "PreToolUse"
	PostToolUse      Event = "PostToolUse"
	UserPromptSubmit Event = "UserPromptSubmit"
	Stop             Event = "Stop"
	SubagentStop     Event = "SubagentStop"
	SessionStart     Event = "SessionStart"
	SessionEnd       Event = "SessionEnd"
	PreCompact       Event = "PreCompact"
	Notification     Event = "Notification"
)

type Hook

type Hook struct {
	ID      string // <source>:<event>:<i>.<j>
	Source  Source
	Event   Event
	Matcher string
	Type    Type
	Command string
	Prompt  string
	Model   string
	Timeout time.Duration
	// contains filtered or unexported fields
}

Hook est un hook compilé et validé.

type Notice

type Notice string

Notice est un avertissement non bloquant du chargement.

type Options

type Options struct {
	Getenv      func(string) string
	ProjectRoot string
	Config      config.Hooks
}

Options règle Load.

type Outcome

type Outcome struct {
	Results    []Result
	Block      bool
	Ask        bool
	Allow      bool
	Reason     string
	Context    string
	StopReason string
	// UpdatedInput : valeur unique demandée par les hooks ; nil en cas de conflit.
	UpdatedInput json.RawMessage
	Conflict     bool
	Stop         bool
}

Outcome est le résultat combiné d'un point de hook. Après combinaison, au plus un de Block, Ask, Allow est vrai.

func Run

func Run(ctx context.Context, hs []Hook, ev Event, fields map[string]any, env Env, eval Evaluator) Outcome

Run exécute en parallèle les hooks hs pour l'événement ev et combine les résultats. L'entrée JSON est fields plus les champs communs ; Results suit l'ordre de hs.

type Result

type Result struct {
	Hook     Hook
	Status   Status
	ExitCode int // -1 si ce n'est pas un hook command (ou lancement impossible)
	// Decision : "", "allow", "ask", "deny" ou "block".
	Decision      string
	Reason        string
	Context       string
	SystemMessage string
	StopReason    string
	Stdout        string
	Stderr        string
	Err           string
	UpdatedInput  json.RawMessage // format Claude, non traduit
	Stop          bool
	Duration      time.Duration
}

Result est le résultat d'un hook.

type Set

type Set struct {
	// contains filtered or unexported fields
}

Set est l'ensemble des hooks chargés, dans l'ordre de configuration.

func (*Set) Count

func (s *Set) Count() (total, claude int)

Count renvoie le nombre total de hooks et celui issu de Claude Code.

func (*Set) List

func (s *Set) List() []Hook

List renvoie tous les hooks chargés.

func (*Set) Match

func (s *Set) Match(ev Event, subjects ...string) []Hook

Match renvoie les hooks de l'événement dont le matcher accepte l'un des sujets, dans l'ordre de configuration. Sans sujet, le matcher est ignoré. Deux hooks command de même commande : le premier est gardé.

type Source

type Source string

Source indique d'où vient un hook.

const (
	SourceTenon  Source = "tenon"
	SourceClaude Source = "claude"
)

type Status

type Status string

Status est l'état final d'un hook.

const (
	StatusOK        Status = "ok"
	StatusBlocked   Status = "blocked"
	StatusError     Status = "error"
	StatusTimeout   Status = "timeout"
	StatusCancelled Status = "cancelled"
)

type Type

type Type string

Type est le type d'un hook.

const (
	Command Type = "command"
	Prompt  Type = "prompt"
	Agent   Type = "agent"
)

Jump to

Keyboard shortcuts

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