@mauribadnights/clooks 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ # Creating Plugins
2
+
3
+ ## Overview
4
+
5
+ A clooks plugin is a directory containing a `clooks-plugin.yaml` manifest and any handler scripts/modules it references. Plugins let you distribute reusable hook configurations.
6
+
7
+ ## Plugin Structure
8
+
9
+ ```
10
+ my-plugin/
11
+ ├── clooks-plugin.yaml # Required: plugin manifest
12
+ ├── README.md # Recommended: usage docs
13
+ ├── handlers/
14
+ │ ├── guard.js # Handler scripts
15
+ │ └── analyzer.js
16
+ └── assets/ # Optional: supporting files
17
+ └── rules.json
18
+ ```
19
+
20
+ ## Plugin Manifest (clooks-plugin.yaml)
21
+
22
+ ```yaml
23
+ name: my-plugin # Required: unique name
24
+ version: 1.0.0 # Required: semver
25
+ description: Security hooks suite # Optional
26
+ author: Your Name # Optional
27
+
28
+ handlers:
29
+ PreToolUse:
30
+ - id: bash-guard
31
+ type: inline
32
+ module: $PLUGIN_DIR/handlers/guard.js
33
+ filter: "Bash|Execute"
34
+ timeout: 3000
35
+
36
+ - id: write-guard
37
+ type: inline
38
+ module: $PLUGIN_DIR/handlers/guard.js
39
+ filter: "Write|Edit"
40
+
41
+ PostToolUse:
42
+ - id: usage-tracker
43
+ type: script
44
+ command: "node $PLUGIN_DIR/handlers/tracker.js"
45
+ async: true
46
+
47
+ prefetch:
48
+ - git_status
49
+
50
+ extras:
51
+ skills:
52
+ - security-audit
53
+ - permission-guard
54
+ agents:
55
+ - security-reviewer
56
+ readme: README.md
57
+ ```
58
+
59
+ ## The $PLUGIN_DIR Variable
60
+
61
+ Use `$PLUGIN_DIR` in `command` and `module` paths. When the plugin is installed, clooks resolves this to the absolute installation path (e.g., `~/.clooks/plugins/my-plugin/`).
62
+
63
+ > **Note:** Always use `$PLUGIN_DIR` instead of relative paths. Plugins are copied to the plugins directory on install, so relative paths from the source won't work.
64
+
65
+ ## Required Fields
66
+
67
+ - `name` (string) — Unique plugin identifier. Used as namespace prefix for handler IDs.
68
+ - `version` (string) — Semantic version.
69
+
70
+ ## Optional Fields
71
+
72
+ - `description` (string) — Shown in `clooks plugins` output
73
+ - `author` (string) — Plugin author
74
+ - `handlers` — Same format as user manifest handlers (all 3 types supported)
75
+ - `prefetch` — Keys to pre-fetch (merged with user manifest)
76
+ - `extras` — Freeform metadata:
77
+ - `skills` (string[]) — Skill names the plugin provides
78
+ - `agents` (string[]) — Agent names the plugin registers
79
+ - `readme` (string) — Relative path to README (resolved to absolute on install)
80
+ - Any other keys (extensible)
81
+
82
+ ## Handler Namespacing
83
+
84
+ When installed, handler IDs become `plugin-name/handler-id`:
85
+
86
+ - Source: `id: bash-guard` → Installed: `my-plugin/bash-guard`
87
+ - Dependencies within the same plugin are auto-namespaced
88
+ - Cross-plugin deps use full namespaced ID: `depends: [other-plugin/handler-id]`
89
+
90
+ ## Writing Inline Handlers for Plugins
91
+
92
+ ```javascript
93
+ // handlers/guard.js
94
+ export default async function(input) {
95
+ const { tool_name, tool_input } = input;
96
+
97
+ if (tool_name === 'Bash' && tool_input?.command?.includes('rm -rf')) {
98
+ return {
99
+ decision: 'block',
100
+ reason: 'Destructive command blocked by security plugin'
101
+ };
102
+ }
103
+
104
+ return { additionalContext: 'Security check passed' };
105
+ }
106
+ ```
107
+
108
+ ## Writing Script Handlers for Plugins
109
+
110
+ ```javascript
111
+ #!/usr/bin/env node
112
+ // handlers/tracker.js
113
+ import { readFileSync } from 'fs';
114
+
115
+ const input = JSON.parse(readFileSync('/dev/stdin', 'utf8'));
116
+
117
+ // Do tracking work...
118
+ const result = { additionalContext: `Tracked ${input.hook_event_name}` };
119
+ console.log(JSON.stringify(result));
120
+ ```
121
+
122
+ ## Testing Your Plugin
123
+
124
+ Before publishing:
125
+
126
+ 1. Validate manifest: install locally with `clooks add ./my-plugin`
127
+ 2. Check handlers load: `clooks plugins` should show your plugin
128
+ 3. Test execution: trigger the relevant hook events
129
+ 4. Check metrics: `clooks stats` shows handler execution data
130
+ 5. Remove: `clooks remove my-plugin`
131
+
132
+ ## Distribution
133
+
134
+ Currently plugins are installed from local directories:
135
+
136
+ ```bash
137
+ clooks add /path/to/my-plugin
138
+ ```
139
+
140
+ Share plugins as git repos or tarballs. Users clone/extract and `clooks add` the directory.
141
+
142
+ ---
143
+
144
+ [Home](../index.md) | [Prev: Using Plugins](using-plugins.md) | [Next: CC Plugin Import](cc-plugin-import.md)
@@ -0,0 +1,63 @@
1
+ # Using Plugins
2
+
3
+ ## Overview
4
+
5
+ clooks plugins are self-contained directories that ship handler definitions in a `clooks-plugin.yaml` file. Install them locally to extend your hook pipeline without editing your manifest.
6
+
7
+ ## Installing a Plugin
8
+
9
+ ```bash
10
+ clooks add ./path/to/plugin-directory
11
+ ```
12
+
13
+ What happens:
14
+
15
+ 1. Validates `clooks-plugin.yaml` in the source directory
16
+ 2. Copies the entire directory to `~/.clooks/plugins/{name}/`
17
+ 3. Resolves `$PLUGIN_DIR` variables in handler commands to the installed path
18
+ 4. Updates plugin registry (`~/.clooks/plugins/installed.json`)
19
+ 5. Syncs settings.json with any new HTTP hook entries
20
+
21
+ ## Listing Plugins
22
+
23
+ ```bash
24
+ clooks plugins
25
+ ```
26
+
27
+ Shows: name, version, handler count, skills (if declared), agents (if declared).
28
+
29
+ ## Removing a Plugin
30
+
31
+ ```bash
32
+ clooks remove plugin-name
33
+ ```
34
+
35
+ Removes the plugin directory and registry entry. Daemon hot-reloads to drop the handlers.
36
+
37
+ ## How Plugins Merge
38
+
39
+ - Plugin handlers are namespaced: `plugin-name/handler-id` (prevents ID collisions)
40
+ - Prefetch keys are merged (union of user + all plugins)
41
+ - Settings come from user manifest only (plugins cannot override port, auth, etc.)
42
+ - Handlers from all plugins + user manifest execute together, respecting dependencies
43
+
44
+ ## Plugin Registry
45
+
46
+ Installed plugins tracked at `~/.clooks/plugins/installed.json`:
47
+
48
+ ```json
49
+ {
50
+ "plugins": [
51
+ {
52
+ "name": "security-suite",
53
+ "version": "1.0.0",
54
+ "path": "/Users/you/.clooks/plugins/security-suite",
55
+ "installedAt": "2026-03-25T10:00:00.000Z"
56
+ }
57
+ ]
58
+ }
59
+ ```
60
+
61
+ ---
62
+
63
+ [Home](../index.md) | [Prev: System Service](../guides/system-service.md) | [Next: Creating Plugins](creating-plugins.md)
@@ -0,0 +1,213 @@
1
+ # CLI Reference
2
+
3
+ clooks provides a single `clooks` command with subcommands for daemon management, configuration, plugins, observability, and system service control.
4
+
5
+ All commands read configuration from `~/.clooks/manifest.yaml` unless otherwise noted.
6
+
7
+ ---
8
+
9
+ ## Daemon
10
+
11
+ ### `clooks start`
12
+
13
+ Start the daemon process.
14
+
15
+ | Flag | Description |
16
+ |------|-------------|
17
+ | `-f, --foreground` | Run in foreground instead of detaching |
18
+ | `--no-watch` | Disable manifest file watching |
19
+
20
+ ```bash
21
+ clooks start # Background (detached)
22
+ clooks start -f # Foreground (for debugging)
23
+ clooks start --no-watch # Background, no manifest hot-reload
24
+ ```
25
+
26
+ > **Note:** When started in background mode, the daemon writes its PID to `~/.clooks/daemon.pid` and logs to `~/.clooks/daemon.log`.
27
+
28
+ ### `clooks stop`
29
+
30
+ Stop the running daemon. Tries the PID file first, then falls back to the health endpoint for orphan recovery.
31
+
32
+ ```bash
33
+ clooks stop
34
+ ```
35
+
36
+ ### `clooks status`
37
+
38
+ Show daemon status including: running/stopped, PID, port, uptime, handler count, plugin count, and service status.
39
+
40
+ ```bash
41
+ clooks status
42
+ ```
43
+
44
+ ### `clooks ensure-running`
45
+
46
+ Start the daemon if it is not already running. This is a no-op if the daemon is healthy.
47
+
48
+ Used internally by the SessionStart hook to guarantee the daemon is available before a Claude Code session begins.
49
+
50
+ ```bash
51
+ clooks ensure-running
52
+ ```
53
+
54
+ ---
55
+
56
+ ## Configuration
57
+
58
+ ### `clooks init`
59
+
60
+ Create default configuration. Generates the `~/.clooks/` directory tree, a starter `manifest.yaml`, an auth token, and installs the clooks agent and system service.
61
+
62
+ ```bash
63
+ clooks init
64
+ ```
65
+
66
+ > **Note:** Safe to run multiple times. Existing configuration is not overwritten.
67
+
68
+ ### `clooks migrate`
69
+
70
+ Migrate existing Claude Code command hooks to clooks HTTP hooks. Backs up `settings.json`, creates manifest entries from existing hooks, and rewrites the hooks to HTTP POST calls.
71
+
72
+ ```bash
73
+ clooks migrate
74
+ ```
75
+
76
+ The original settings are saved to `~/.clooks/settings.backup.json` and can be restored with `clooks restore`.
77
+
78
+ ### `clooks restore`
79
+
80
+ Restore the original `settings.json` from the backup created during `clooks migrate`.
81
+
82
+ ```bash
83
+ clooks restore
84
+ ```
85
+
86
+ ### `clooks sync`
87
+
88
+ Sync `settings.json` with the manifest. Ensures HTTP hook entries exist in Claude Code settings for every event that has handlers defined in the manifest.
89
+
90
+ ```bash
91
+ clooks sync
92
+ ```
93
+
94
+ ### `clooks rotate-token`
95
+
96
+ Generate a new auth token. Updates both `manifest.yaml` and `settings.json` with the new token.
97
+
98
+ ```bash
99
+ clooks rotate-token
100
+ ```
101
+
102
+ ### `clooks update`
103
+
104
+ Update clooks to the latest npm version. Restarts the daemon automatically if it was running.
105
+
106
+ ```bash
107
+ clooks update
108
+ ```
109
+
110
+ ---
111
+
112
+ ## Plugins
113
+
114
+ ### `clooks add <path>`
115
+
116
+ Install a plugin from a local directory. Validates the plugin manifest, copies files to the plugins directory, and syncs settings.
117
+
118
+ ```bash
119
+ clooks add ./my-plugin
120
+ clooks add ~/plugins/security-suite
121
+ ```
122
+
123
+ ### `clooks remove <name>`
124
+
125
+ Uninstall a plugin by name.
126
+
127
+ ```bash
128
+ clooks remove security-suite
129
+ ```
130
+
131
+ ### `clooks plugins`
132
+
133
+ List installed plugins with name, version, handler count, skills, and agents.
134
+
135
+ ```bash
136
+ clooks plugins
137
+ ```
138
+
139
+ ### `clooks import-plugins`
140
+
141
+ Discover and import hooks from Claude Code plugins located in `~/.claude/plugins/`.
142
+
143
+ ```bash
144
+ clooks import-plugins
145
+ ```
146
+
147
+ ---
148
+
149
+ ## Observability
150
+
151
+ ### `clooks stats`
152
+
153
+ Show hook execution metrics. Displays an interactive TUI by default.
154
+
155
+ | Flag | Description |
156
+ |------|-------------|
157
+ | `-t, --text` | Plain text output (auto-detected when piped) |
158
+
159
+ ```bash
160
+ clooks stats # Interactive TUI
161
+ clooks stats -t # Plain text output
162
+ clooks stats | head # Auto-selects text mode when piped
163
+ ```
164
+
165
+ ### `clooks costs`
166
+
167
+ Show LLM cost breakdown by model and handler.
168
+
169
+ ```bash
170
+ clooks costs
171
+ ```
172
+
173
+ ### `clooks doctor`
174
+
175
+ Run diagnostic health checks across config, manifest, daemon, port, settings, plugins, service, and agent.
176
+
177
+ ```bash
178
+ clooks doctor
179
+ ```
180
+
181
+ Each check reports `ok`, `warn`, or `error` with a human-readable message.
182
+
183
+ ---
184
+
185
+ ## System Service
186
+
187
+ ### `clooks service install`
188
+
189
+ Install clooks as a system service using the platform-native mechanism (launchd on macOS, systemd on Linux, Task Scheduler on Windows). Enables auto-start on login and auto-restart on crash.
190
+
191
+ ```bash
192
+ clooks service install
193
+ ```
194
+
195
+ ### `clooks service uninstall`
196
+
197
+ Remove the system service.
198
+
199
+ ```bash
200
+ clooks service uninstall
201
+ ```
202
+
203
+ ### `clooks service status`
204
+
205
+ Show service status: `running`, `stopped`, or `not-installed`.
206
+
207
+ ```bash
208
+ clooks service status
209
+ ```
210
+
211
+ ---
212
+
213
+ [Home](../index.md) | [Prev: CC Plugin Import](../plugins/cc-plugin-import.md) | [Next: Hook Events](hook-events.md)
@@ -0,0 +1,129 @@
1
+ # Config Files
2
+
3
+ All configuration, state, and data files used by clooks, with their locations, formats, and schemas.
4
+
5
+ ---
6
+
7
+ ## File Map
8
+
9
+ | Path | Format | Purpose |
10
+ |------|--------|---------|
11
+ | `~/.clooks/` | Directory | Configuration root |
12
+ | `~/.clooks/manifest.yaml` | YAML | Handler definitions and settings |
13
+ | `~/.clooks/daemon.pid` | Text | Running daemon PID |
14
+ | `~/.clooks/daemon.log` | Text | Daemon log output |
15
+ | `~/.clooks/metrics.jsonl` | JSONL | Hook execution metrics (max 5MB, rotated) |
16
+ | `~/.clooks/costs.jsonl` | JSONL | LLM cost tracking (max 1MB, rotated) |
17
+ | `~/.clooks/hooks/` | Directory | Built-in hook scripts |
18
+ | `~/.clooks/plugins/` | Directory | Installed plugin directories |
19
+ | `~/.clooks/plugins/installed.json` | JSON | Plugin registry |
20
+ | `~/.clooks/settings.backup.json` | JSON | Pre-migration settings backup |
21
+ | `~/.claude/settings.json` | JSON | Claude Code settings (HTTP hooks added here) |
22
+ | `~/.claude/settings.local.json` | JSON | Local settings override |
23
+ | `~/.claude/agents/clooks.md` | Markdown | clooks expert agent |
24
+
25
+ ---
26
+
27
+ ## Metrics File Format
28
+
29
+ `metrics.jsonl` stores one JSON object per line. Each entry records a single handler execution.
30
+
31
+ **Standard entry:**
32
+
33
+ ```json
34
+ {
35
+ "ts": "2026-03-25T10:00:00.000Z",
36
+ "event": "PreToolUse",
37
+ "handler": "bash-guard",
38
+ "duration_ms": 12,
39
+ "ok": true,
40
+ "filtered": false,
41
+ "session_id": "abc123",
42
+ "agent_type": "builder"
43
+ }
44
+ ```
45
+
46
+ **LLM handler entry** (includes additional fields):
47
+
48
+ ```json
49
+ {
50
+ "ts": "2026-03-25T10:00:00.000Z",
51
+ "event": "PreToolUse",
52
+ "handler": "code-review",
53
+ "duration_ms": 850,
54
+ "ok": true,
55
+ "filtered": false,
56
+ "session_id": "abc123",
57
+ "agent_type": "builder",
58
+ "usage": { "input_tokens": 150, "output_tokens": 80 },
59
+ "cost_usd": 0.00045
60
+ }
61
+ ```
62
+
63
+ ---
64
+
65
+ ## Cost File Format
66
+
67
+ `costs.jsonl` tracks LLM-specific cost data, one entry per LLM handler invocation.
68
+
69
+ ```json
70
+ {
71
+ "ts": "2026-03-25T10:00:00.000Z",
72
+ "event": "PreToolUse",
73
+ "handler": "code-review",
74
+ "model": "claude-haiku-4-5",
75
+ "usage": { "input_tokens": 200, "output_tokens": 100 },
76
+ "cost_usd": 0.00056,
77
+ "batched": true
78
+ }
79
+ ```
80
+
81
+ ---
82
+
83
+ ## Plugin Registry Format
84
+
85
+ `installed.json` tracks all plugins installed via `clooks add`.
86
+
87
+ ```json
88
+ {
89
+ "plugins": [
90
+ {
91
+ "name": "security-suite",
92
+ "version": "1.0.0",
93
+ "path": "/Users/you/.clooks/plugins/security-suite",
94
+ "installedAt": "2026-03-25T10:00:00.000Z"
95
+ }
96
+ ]
97
+ }
98
+ ```
99
+
100
+ ---
101
+
102
+ ## File Rotation
103
+
104
+ Data files are rotated when they exceed their size limit:
105
+
106
+ | File | Max Size | Rotated To |
107
+ |------|----------|------------|
108
+ | `metrics.jsonl` | 5MB | `metrics.jsonl.1` |
109
+ | `costs.jsonl` | 1MB | `costs.jsonl.1` |
110
+
111
+ Only one rotated copy is kept. When rotation occurs, the existing `.1` file is overwritten.
112
+
113
+ ---
114
+
115
+ ## Service Files
116
+
117
+ System service files are platform-specific:
118
+
119
+ | Platform | Path |
120
+ |----------|------|
121
+ | macOS | `~/Library/LaunchAgents/com.clooks.daemon.plist` |
122
+ | Linux | `~/.config/systemd/user/clooks.service` |
123
+ | Windows | Task Scheduler task named `"clooks"` |
124
+
125
+ These are managed by `clooks service install` and `clooks service uninstall`. Do not edit them manually.
126
+
127
+ ---
128
+
129
+ [Home](../index.md) | [Prev: HTTP API](http-api.md) | [Next: Types](types.md)
@@ -0,0 +1,128 @@
1
+ # Hook Events
2
+
3
+ Claude Code sends hook events to clooks via HTTP POST. Each event carries a HookInput payload with common fields plus event-specific data. There are 9 events in total.
4
+
5
+ ---
6
+
7
+ ## Common Fields
8
+
9
+ All events include these fields in the HookInput payload:
10
+
11
+ | Field | Type | Description |
12
+ |-------|------|-------------|
13
+ | `session_id` | `string` | Claude Code session identifier |
14
+ | `transcript_path` | `string` | Path to the session transcript file |
15
+ | `cwd` | `string` | Current working directory |
16
+ | `permission_mode` | `string` | Active permission level |
17
+ | `hook_event_name` | `string` | Event name (matches the route) |
18
+
19
+ ---
20
+
21
+ ## Events
22
+
23
+ ### SessionStart
24
+
25
+ Fired when a Claude Code session begins.
26
+
27
+ | Field | Type | Description |
28
+ |-------|------|-------------|
29
+ | `source` | `string` | How the session was started |
30
+ | `agent_type` | `string` | Active agent name (e.g., `"builder"`, `"coo"`) |
31
+
32
+ clooks caches `agent_type` for the duration of the session. Handlers with `sessionIsolation: true` are reset on this event.
33
+
34
+ ### UserPromptSubmit
35
+
36
+ Fired when the user submits a prompt.
37
+
38
+ | Field | Type | Description |
39
+ |-------|------|-------------|
40
+ | `prompt` | `string` | The user's prompt text |
41
+
42
+ ### PreToolUse
43
+
44
+ Fired before Claude Code executes a tool. Handlers can block tool execution by returning a deny decision.
45
+
46
+ | Field | Type | Description |
47
+ |-------|------|-------------|
48
+ | `tool_name` | `string` | Tool being called (e.g., `"Bash"`, `"Write"`, `"Read"`) |
49
+ | `tool_input` | `object` | Tool arguments |
50
+
51
+ A handler can return the following to prevent execution:
52
+
53
+ ```json
54
+ {
55
+ "decision": "block",
56
+ "reason": "Destructive command detected"
57
+ }
58
+ ```
59
+
60
+ ### PostToolUse
61
+
62
+ Fired after a tool completes.
63
+
64
+ | Field | Type | Description |
65
+ |-------|------|-------------|
66
+ | `tool_name` | `string` | Tool that was called |
67
+ | `tool_input` | `object` | Tool arguments |
68
+
69
+ > **Note:** This event is skipped if PreToolUse returned a `block` decision for the same tool invocation (short-circuit behavior).
70
+
71
+ ### Stop
72
+
73
+ Fired when a session is ending.
74
+
75
+ | Field | Type | Description |
76
+ |-------|------|-------------|
77
+ | `stop_hook_active` | `boolean` | Whether the stop hook is active |
78
+
79
+ ### SubagentStart
80
+
81
+ Fired when a subagent is spawned.
82
+
83
+ | Field | Type | Description |
84
+ |-------|------|-------------|
85
+ | `agent_type` | `string` | Subagent type |
86
+
87
+ ### SubagentStop
88
+
89
+ Fired when a subagent completes. No additional fields beyond the common set.
90
+
91
+ ### Notification
92
+
93
+ Fired when a system notification is sent. No additional fields beyond the common set.
94
+
95
+ ### ConfigChange
96
+
97
+ Fired when Claude Code configuration changes. No additional fields beyond the common set.
98
+
99
+ ---
100
+
101
+ ## Event Flow
102
+
103
+ A typical session produces events in this order:
104
+
105
+ ```
106
+ SessionStart
107
+ UserPromptSubmit
108
+ PreToolUse -> (if allowed) -> PostToolUse
109
+ PreToolUse -> (if blocked) -> [no PostToolUse]
110
+ ...
111
+ UserPromptSubmit
112
+ ...
113
+ Stop
114
+ ```
115
+
116
+ Subagent events nest within the parent session:
117
+
118
+ ```
119
+ SessionStart
120
+ SubagentStart
121
+ PreToolUse -> PostToolUse
122
+ SubagentStop
123
+ Stop
124
+ ```
125
+
126
+ ---
127
+
128
+ [Home](../index.md) | [Prev: CLI Reference](cli.md) | [Next: HTTP API](http-api.md)