EN 中文

Hook Kinds

Hook 类型

Two categories, separated at the type level — prefix-cache safety is a compile-time property:

两类 hooks,在类型层面区分,前缀缓存安全性在编译时保证:

Decorators (can mutate the request)

装饰器(可修改请求)

Hook When 触发时机 Purpose 作用
PrePromptHook Before user message is appended to history 用户消息追加到历史之前 Inject memory, expand slash commands, rewrite user text 注入记忆、展开斜杠命令、改写用户文本
PreToolUseHook Before tool call execution 工具调用执行前 Modify tool arguments, inject context 修改工具参数、注入上下文
⚠️ Output MUST be deterministic — DeepSeek's prefix cache keys on the exact byte sequence of the prompt history. 输出必须确定——DeepSeek 前缀缓存 key 是 prompt 历史的精确字节序列。

Observers (read-only)

观察器(只读)

Hook When 触发时机
PreTurnBefore each turn每轮开始前
PostTurnAfter each turn每轮结束后
PostToolUseAfter each tool call每个工具执行后
SessionStartSession begins会话开始时
SessionEndSession ends会话结束时

Built-in Hooks

内置 Hook

CLI: seek hooks

CLI:seek hooks

$ seek hooks list     # List trusted hook scripts
$ seek hooks check --event <event> [--tool <tool>]    # Dry-run: which hooks fire for this event/tool?
$ seek hooks trust [--reset[=<path>]]    # List trusted projects; --reset removes trust
$ seek hooks audit    # Audit hook call records

Writing Custom Hooks (Go)

编写自定义 Hook

A single struct may implement multiple hook interfaces. Registry.Register dispatches it into every slot it satisfies.

一个结构体可以实现多个 hook 接口。Registry.Register 自动分发:

// A struct implementing both PrePrompt + PostTurn
type myHook struct{}

func (h *myHook) OnPrePrompt(ctx context.Context, in hooks.PrePromptIn) (hooks.PrePromptOut, error) {
    return hooks.PrePromptOut{UserText: in.UserText + "\n[injected context]"}, nil
}

func (h *myHook) OnPostTurn(ctx context.Context, ev hooks.PostTurnEvent) {
    // analyse, distil, persist...
}

// Register
registry := hooks.NewRegistry()
registry.Register(&myHook{})

Design Notes

设计要点