返回 CodeWhale
docs-hooks.ts
根目录 / web / lib / i18n / dictionaries / en / docs-hooks.ts
1 import type { DocsHooksDict } from "../types";
2
3 /**
4 * English reference dictionary for `app/[locale]/docs/hooks/page.tsx`
5 * ("Run commands on events"). Checked against docs/HOOKS.md — the
6 * authoritative event-by-event contract (15 events, steering events,
7 * conditions, environment variables, project-hook approval).
8 */
9 export const docsHooks: DocsHooksDict = {
10 metaTitle: "Run commands on events · Codewhale Docs",
11 metaDescription:
12 "Run your own scripts when a session starts, before a tool call, when a turn ends, or when Codewhale is waiting for you — to add context, enforce a rule, or get notified.",
13 bodyClassName: "text-ink-soft leading-relaxed",
14 title: "Run commands on events",
15 lede:
16 "Hooks run a command of yours at set moments in a Codewhale session. Use them to block a risky command, add context to a message, log what happened, or get told when Codewhale is waiting for you.",
17 sections: [
18 {
19 id: "first-hook",
20 title: "Add your first hook",
21 blocks: [
22 { p: "Hooks live in `~/.codewhale/config.toml`. This one prints a line whenever a session starts:" },
23 {
24 code: `[hooks]
25 enabled = true
26
27 [[hooks.hooks]]
28 name = "announce"
29 event = "session_start"
30 command = "echo 'Codewhale session started'"`,
31 lang: "config.toml",
32 },
33 {
34 p: "Start a session, then run `/hooks` to see every configured hook, whether hooks are on, and any entry that was rejected. `/hooks events` lists the event names.",
35 },
36 ],
37 },
38 {
39 id: "gate",
40 title: "Block a command before it runs",
41 blocks: [
42 {
43 p: "A `tool_call_before` hook sees each tool call before it executes and can allow it, deny it, or force an approval prompt. This POSIX-shell example denies every call named `bash`, rather than guessing what a shell command string will do.",
44 },
45 {
46 code: `#!/bin/sh
47 printf '%s\\n' '{"decision":"deny","reason":"Shell calls are blocked by this hook."}'`,
48 lang: "~/.codewhale/hooks/deny-shell.sh",
49 },
50 { p: "Create `~/.codewhale/hooks/`, save the script there as `deny-shell.sh`, and make it executable on macOS or Linux:" },
51 { code: "chmod +x ~/.codewhale/hooks/deny-shell.sh", lang: "Terminal" },
52 {
53 code: `[[hooks.hooks]]
54 name = "deny-shell"
55 event = "tool_call_before"
56 command = "~/.codewhale/hooks/deny-shell.sh"
57 condition = { type = "tool_name", name = "bash" }
58 continue_on_error = false`,
59 lang: "config.toml",
60 },
61 {
62 p: "The hook reads the call from environment variables and answers with JSON on standard output: `allow`, `deny`, or `ask`, plus an optional `reason`, a rewritten input (`updatedInput`), or extra context for the model (`additionalContext`). Exit code 2 always denies. When several hooks answer, deny beats ask, and ask beats allow.",
63 },
64 { note: "This example covers only the exact `bash` tool; it does not block other tools, external clients or commands you run yourself. A substring match for force-push flags is not a shell security boundary; use the execution policy and OS sandbox for broader restrictions." },
65 {
66 note: "`ask` forces a prompt wherever the active permission posture asks or reviews. Full Access does not add approval prompts, so `ask` does not add one there.",
67 },
68 ],
69 },
70 {
71 id: "events",
72 title: "Choose the moment",
73 blocks: [
74 {
75 p: "Three events can change what happens next. The rest only observe; their output is ignored and a failure is a warning.",
76 },
77 {
78 rows: [
79 ["message_submit", "Before your message reaches the model. Can replace the text or block it."],
80 ["tool_call_before", "Before each tool call. Can allow, deny, ask, rewrite the input, or add context."],
81 ["shell_env", "Before each shell command. Can add environment variables."],
82 ["session_start / session_end", "When a session opens or closes cleanly."],
83 ["turn_end", "After a turn finishes, with its status, duration, and token usage."],
84 ["tool_call_after", "After each tool result, with its exit code when there is one."],
85 ["waiting_for_user", "When Codewhale starts waiting for an approval, an answer, or a paused goal."],
86 ["session_idle / session_busy", "When the session settles or starts working again."],
87 ["session_error / on_error", "When a turn fails for good, or on any error or failed tool."],
88 ["mode_change", "When you change the mode."],
89 ["subagent_spawn / subagent_complete", "When a sub-agent starts or finishes."],
90 ],
91 codeTerms: true,
92 },
93 {
94 p: "A `condition` narrows when a hook fires: by tool name (with `*` globs), tool category, mode, or exit code, and combinations with `all` and `any`. A condition that can never match its event is rejected when the config loads, so a gate you think is armed never sits silently inert.",
95 },
96 ],
97 },
98 {
99 id: "options",
100 title: "Set timeouts and failure behavior",
101 blocks: [
102 {
103 rows: [
104 ["timeout_secs", "How long the hook may run. Default 30."],
105 ["continue_on_error", "`true` (default): a failing hook only warns. `false`: the failure blocks."],
106 ["background", "`true` runs the hook as an observer only; it cannot block or rewrite."],
107 ["working_dir", "Under `[hooks]`: where hooks run. Default: the session's workspace."],
108 ],
109 codeTerms: true,
110 },
111 {
112 note: "`[hooks] default_timeout_secs` replaces every hook's own `timeout_secs`, not just the missing ones. Leave it unset if you want per-hook timeouts.",
113 },
114 ],
115 },
116 {
117 id: "project",
118 title: "Use hooks a repository ships",
119 blocks: [
120 {
121 p: "A repository can include hooks in `.codewhale/hooks.toml`. Because they run commands on your machine, they load only after you trust the workspace and approve that exact file:",
122 },
123 { code: "/hooks review\n/hooks approve <digest>\n/hooks revoke", lang: "Codewhale" },
124 {
125 p: "`/hooks review` shows the commands and a digest of the file; approving that digest enables those exact bytes from the next session. Any change to the file needs a new approval. Review the scripts the commands call, too.",
126 },
127 ],
128 },
129 {
130 id: "headless",
131 title: "Use hooks in scripts and CI",
132 blocks: [
133 {
134 p: "Hooks run in the interactive session. `codewhale exec` fires none by default; add `--hooks` to fire `tool_call_before` and `shell_env`. With no one to answer, an `ask` becomes a deny.",
135 },
136 { code: 'codewhale exec --auto --sandbox workspace-write --hooks "run the test suite and fix the first failure"', lang: "Terminal" },
137 ],
138 },
139 ],
140 next: [
141 {
142 href: "/docs/modes",
143 label: "Set modes and approvals",
144 note: "See how hook decisions combine with your approval setting.",
145 },
146 {
147 href: "/docs/mcp",
148 label: "Connect tools with MCP",
149 note: "Gate MCP tools with the same `tool_call_before` hooks.",
150 },
151 {
152 href: "/docs/configuration",
153 label: "Change settings",
154 note: "Where `config.toml` lives and what a project may override.",
155 },
156 ],
157 sourceNote:
158 "Source documents: docs/HOOKS.md (authoritative), docs/CONFIGURATION.md · Update docs-map.ts when changing.",
159 };
160
160 lines TYPESCRIPT