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

hookx

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package hookx 外部 hook 拦截协议:在关键拦截点派生子进程,stdin 喂 JSON 事件载荷、stdout 收 JSON 决策,归并多 hook 结果。对标 ZCode hooks/ runner.ts + output.ts 的「跨语言 hook 事实标准」形态。

决策协议(HookOutput):

continue:false 或 decision:"block" → 阻断(PreToolUse/PermissionRequest
  事件上同时意味着 permission deny;Stop 事件上意味着「继续跑」)
decision:"approve" → 权限事件上升格放行(消费方按 permgate 语义消费——
  alwaysAsk 不可被 hook 升格,这是调用方纪律)
additionalContext / additional_context → 附加上下文累积
hookSpecificOutput.updatedInput → 输入改写(消费方必须用改写后的输入
  重走权限/许可链——hook 注入的输入不能绕过规则)

失败语义:hook 进程失败(超时/非零退出/stdout 非 JSON/事件名不符)按 「无意见」处理不炸主流程,经 OnError 可观测——扩展点故障不该阻断业务; 需要强一致的场景由调用方在 OnError 里自行收紧。子进程环境走 procx.ChildEnv 白名单(绝不继承全量密钥)。

Index

Constants

View Source
const DefaultTimeout = 60 * time.Second

DefaultTimeout 单 hook 执行预算(对标 ZCode 60s)。

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event string

Event 拦截点。

const (
	EventSessionStart       Event = "session_start"
	EventUserPromptSubmit   Event = "user_prompt_submit"
	EventPreToolUse         Event = "pre_tool_use"
	EventPermissionRequest  Event = "permission_request"
	EventPostToolUse        Event = "post_tool_use"
	EventPostToolUseFailure Event = "post_tool_use_failure"
	EventStop               Event = "stop"
)

type Hook

type Hook struct {
	// Command 子进程 argv(直传无 shell)。
	Command []string
	// Events 事件匹配:"*"(全部)、"|"分隔精确名("pre_tool_use|post_tool_use")、
	// "/正则/" 形态("/.*tool.*/")。空 = "*"。
	Events string
	// Timeout ≤0 → DefaultTimeout。
	Timeout time.Duration
	// Env 追加透传的环境变量白名单(叠加 procx 基础集)。
	Env []string
}

Hook 单个拦截器声明。

type Input

type Input struct {
	Event     Event          `json:"event"`
	Tool      string         `json:"tool,omitempty"`
	ToolInput any            `json:"input,omitempty"`
	Extra     map[string]any `json:"extra,omitempty"` // 调用方附带上下文(会话 id 等)
}

Input 派发载荷(stdin JSON)。

type Output

type Output struct {
	Continue      *bool  `json:"continue,omitempty"`
	StopReason    string `json:"stopReason,omitempty"`
	Reason        string `json:"reason,omitempty"`
	Decision      string `json:"decision,omitempty"` // "approve" | "block"
	SystemMessage string `json:"systemMessage,omitempty"`
	// 附加上下文(两种拼写都收——事实标准里两代拼写并存)。
	AdditionalContext  string    `json:"additionalContext,omitempty"`
	AdditionalContext2 string    `json:"additional_context,omitempty"`
	HookSpecificOutput *Specific `json:"hookSpecificOutput,omitempty"`
}

Output hook stdout 决策协议(未知字段忽略)。

type RunResult

type RunResult struct {
	AdditionalContexts []string
	// BlockRequested 任一 hook 请求阻断。
	BlockRequested bool
	// PreventContinuation 阻断且该事件不允许继续(PreToolUse/PermissionRequest)。
	PreventContinuation bool
	StopReason          string
	// StopShouldContinue Stop 事件上 hook 否决停止(continue:false 或
	// decision:block——block 分支沿用阻断语义)。
	StopShouldContinue bool
	// PermissionBehavior "" | "allow" | "deny"(deny 单调——保守合并)。
	PermissionBehavior string
	// UpdatedInput 最后一个改写输入(nil=未改写)。
	UpdatedInput any
}

RunResult 事件级归并结果(多个 hook 单调合并)。

type Runner

type Runner struct {
	Hooks []Hook
	// OnError hook 执行失败回调(nil=静默容错)。
	OnError func(h Hook, err error)
}

Runner hook 执行器。零值可用(无 hook 时 Run 返回空结果)。

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, event Event, payload Input) RunResult

Run 派发事件:顺序执行全部命中 hook 并归并(顺序确定性——先注册先执行)。

type Specific

type Specific struct {
	HookEventName Event `json:"hookEventName"`
	// UpdatedInput 改写后的工具输入(消费方须以改写输入重走许可链)。
	UpdatedInput any `json:"updatedInput,omitempty"`
}

Specific hook 事件专属输出。

Jump to

Keyboard shortcuts

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