hook

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package hook is the Claude Code PreToolUse guard and its settings installer.

Index

Constants

View Source
const BinaryName = "telegram-agent-cli"

BinaryName is the program the guard watches for.

View Source
const DefaultDeadline = 3 * time.Second

DefaultDeadline bounds one check; past it the hook asks instead of waiting.

View Source
const HookArgs = "hook claude-pretooluse"

HookArgs is what Claude Code runs after the binary path.

View Source
const HookTimeout = 10

HookTimeout is the per-call limit written on the guard entries (seconds).

Variables

View Source
var Matchers = []string{"Bash|Monitor", "Read|Edit|Write|MultiEdit|NotebookEdit|Grep"}

Matchers are the PreToolUse matchers the guard is installed under.

Functions

func CheckBash

func CheckBash(cmd string, o Options) []string

CheckBash returns why a Bash command needs the owner's OK (nil = let it run).

func CheckContent

func CheckContent(tool string, ti map[string]any, o Options) string

CheckContent returns why a file tool's written content needs the owner's OK.

func CheckFile

func CheckFile(tool, p string, o Options) string

CheckFile returns why a file tool call needs the owner's OK ("" = allow).

func Evaluate

func Evaluate(in Input, o Options) string

Evaluate returns the ask reason for a tool call, or "" to stay silent.

func HookCommand

func HookCommand(binary string) string

HookCommand is the hook command line for a binary path.

func Run

func Run(r io.Reader, w io.Writer, o Options) error

Run reads one PreToolUse payload from r and writes an "ask" decision to w when the call needs the owner's OK, or when the check does not finish within the deadline. Bad input produces no output.

func SettingsPath

func SettingsPath() (string, error)

SettingsPath is $CLAUDE_CONFIG_DIR/settings.json or ~/.claude/settings.json.

func WatcherEnvsFor

func WatcherEnvsFor(dirs []string) []string

WatcherEnvsFor returns the watcher session variable names: the default plus watcher_session_env from each data dir's config.json.

Types

type Decision

type Decision struct {
	HookSpecificOutput struct {
		HookEventName            string `json:"hookEventName"`
		PermissionDecision       string `json:"permissionDecision"`
		PermissionDecisionReason string `json:"permissionDecisionReason"`
	} `json:"hookSpecificOutput"`
}

Decision is the hook's stdout when it asks.

type Input

type Input struct {
	ToolName  string         `json:"tool_name"`
	ToolInput map[string]any `json:"tool_input"`
	Cwd       string         `json:"cwd"`
}

Input is the part of the Claude Code PreToolUse payload the guard reads.

type Options

type Options struct {
	DataDirs []string                          // data dirs to protect (absolute)
	UserHome string                            // for ~ and $HOME forms
	Cwd      string                            // base for relative paths
	ReadFile func(path string) ([]byte, error) // script files named in a command
	// WatcherEnvs are the watcher session variable names (config watcher_session_env);
	// the default name is always included.
	WatcherEnvs []string
	// XDGConfigHome is $XDG_CONFIG_HOME ("" = ~/.config).
	XDGConfigHome string
	// ClaudeConfigDir is $CLAUDE_CONFIG_DIR; ~/.claude and every .claude dir are always protected.
	ClaudeConfigDir string
	Deadline        time.Duration // Run's time budget (DefaultDeadline if zero)
}

Options configures a check. Zero values fall back to the real environment.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions protects $TGAC_HOME (if set) and the default data dir.

type Result

type Result struct {
	Settings string          `json:"settings"`
	DryRun   bool            `json:"dry_run"`
	Changed  bool            `json:"changed"`
	Actions  []string        `json:"actions"`
	Backup   string          `json:"backup,omitempty"`
	Diff     string          `json:"diff"`
	Result   json.RawMessage `json:"result,omitempty"` // the new settings (dry run only)
}

Result reports an install/uninstall.

func Install

func Install(path, binary string, dryRun bool, now time.Time) (*Result, error)

Install adds TGAC_READONLY=1 and the guard hooks to the settings at path.

func Uninstall

func Uninstall(path string, dryRun bool, now time.Time) (*Result, error)

Uninstall removes the guard hooks and env.TGAC_READONLY="1".

Jump to

Keyboard shortcuts

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