@claude-flow/hooks 3.0.0-alpha.1
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.
- package/README.md +440 -0
- package/bin/hooks-daemon.js +199 -0
- package/bin/statusline.js +77 -0
- package/dist/bridge/official-hooks-bridge.d.ts +99 -0
- package/dist/bridge/official-hooks-bridge.d.ts.map +1 -0
- package/dist/bridge/official-hooks-bridge.js +280 -0
- package/dist/bridge/official-hooks-bridge.js.map +1 -0
- package/dist/cli/guidance-cli.d.ts +17 -0
- package/dist/cli/guidance-cli.d.ts.map +1 -0
- package/dist/cli/guidance-cli.js +486 -0
- package/dist/cli/guidance-cli.js.map +1 -0
- package/dist/daemons/index.d.ts +204 -0
- package/dist/daemons/index.d.ts.map +1 -0
- package/dist/daemons/index.js +443 -0
- package/dist/daemons/index.js.map +1 -0
- package/dist/executor/index.d.ts +80 -0
- package/dist/executor/index.d.ts.map +1 -0
- package/dist/executor/index.js +273 -0
- package/dist/executor/index.js.map +1 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +85 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/index.d.ts +11 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +11 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/llm-hooks.d.ts +93 -0
- package/dist/llm/llm-hooks.d.ts.map +1 -0
- package/dist/llm/llm-hooks.js +382 -0
- package/dist/llm/llm-hooks.js.map +1 -0
- package/dist/mcp/index.d.ts +61 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +501 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/reasoningbank/guidance-provider.d.ts +78 -0
- package/dist/reasoningbank/guidance-provider.d.ts.map +1 -0
- package/dist/reasoningbank/guidance-provider.js +350 -0
- package/dist/reasoningbank/guidance-provider.js.map +1 -0
- package/dist/reasoningbank/index.d.ts +212 -0
- package/dist/reasoningbank/index.d.ts.map +1 -0
- package/dist/reasoningbank/index.js +785 -0
- package/dist/reasoningbank/index.js.map +1 -0
- package/dist/registry/index.d.ts +85 -0
- package/dist/registry/index.d.ts.map +1 -0
- package/dist/registry/index.js +212 -0
- package/dist/registry/index.js.map +1 -0
- package/dist/statusline/index.d.ts +128 -0
- package/dist/statusline/index.d.ts.map +1 -0
- package/dist/statusline/index.js +493 -0
- package/dist/statusline/index.js.map +1 -0
- package/dist/swarm/index.d.ts +271 -0
- package/dist/swarm/index.d.ts.map +1 -0
- package/dist/swarm/index.js +638 -0
- package/dist/swarm/index.js.map +1 -0
- package/dist/types.d.ts +525 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +56 -0
- package/dist/types.js.map +1 -0
- package/dist/workers/index.d.ts +232 -0
- package/dist/workers/index.d.ts.map +1 -0
- package/dist/workers/index.js +1521 -0
- package/dist/workers/index.js.map +1 -0
- package/dist/workers/mcp-tools.d.ts +37 -0
- package/dist/workers/mcp-tools.d.ts.map +1 -0
- package/dist/workers/mcp-tools.js +414 -0
- package/dist/workers/mcp-tools.js.map +1 -0
- package/dist/workers/session-hook.d.ts +42 -0
- package/dist/workers/session-hook.d.ts.map +1 -0
- package/dist/workers/session-hook.js +172 -0
- package/dist/workers/session-hook.js.map +1 -0
- package/package.json +101 -0
package/README.md
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
# @claude-flow/hooks
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@claude-flow/hooks)
|
|
4
|
+
[](https://www.npmjs.com/package/@claude-flow/hooks)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://nodejs.org/)
|
|
8
|
+
|
|
9
|
+
> Event-driven lifecycle hooks with ReasoningBank learning integration for Claude Flow V3
|
|
10
|
+
|
|
11
|
+
The `@claude-flow/hooks` package provides a comprehensive hooks system for intercepting and extending Claude Flow operations. It enables intelligent task routing, pattern learning, background metrics collection, and real-time statusline integration.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- 🎣 **Hook Registry** - Priority-based hook registration with filtering and management
|
|
16
|
+
- ⚡ **Hook Executor** - Timeout handling, error recovery, and result aggregation
|
|
17
|
+
- 🤖 **Background Daemons** - Metrics collection, swarm monitoring, pattern learning
|
|
18
|
+
- 👷 **Background Workers** - 12 specialized workers for analysis, optimization, and automation
|
|
19
|
+
- 📊 **Statusline Integration** - Real-time status display for Claude Code
|
|
20
|
+
- 🧠 **ReasoningBank Learning** - Intelligent task routing based on learned patterns
|
|
21
|
+
- 🔧 **MCP Tools** - 13 MCP tools for programmatic hooks access
|
|
22
|
+
- 🔄 **V2 Compatibility** - Backward compatible with V2 hook commands
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# Using npm
|
|
28
|
+
npm install @claude-flow/hooks
|
|
29
|
+
|
|
30
|
+
# Using pnpm
|
|
31
|
+
pnpm add @claude-flow/hooks
|
|
32
|
+
|
|
33
|
+
# Using yarn
|
|
34
|
+
yarn add @claude-flow/hooks
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
### Basic Usage
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import {
|
|
43
|
+
HookRegistry,
|
|
44
|
+
HookExecutor,
|
|
45
|
+
HookEvent,
|
|
46
|
+
HookPriority,
|
|
47
|
+
} from '@claude-flow/hooks';
|
|
48
|
+
|
|
49
|
+
// Create registry and executor
|
|
50
|
+
const registry = new HookRegistry();
|
|
51
|
+
const executor = new HookExecutor(registry);
|
|
52
|
+
|
|
53
|
+
// Register a pre-edit hook
|
|
54
|
+
registry.register(
|
|
55
|
+
HookEvent.PreEdit,
|
|
56
|
+
async (context) => {
|
|
57
|
+
console.log(`Editing file: ${context.file?.path}`);
|
|
58
|
+
return { success: true };
|
|
59
|
+
},
|
|
60
|
+
HookPriority.Normal,
|
|
61
|
+
{ name: 'log-edits' }
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// Execute hooks
|
|
65
|
+
const result = await executor.preEdit('src/app.ts', 'modify');
|
|
66
|
+
console.log(`Hooks executed: ${result.hooksExecuted}`);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Initialize with Daemons
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { initializeHooks } from '@claude-flow/hooks';
|
|
73
|
+
|
|
74
|
+
// Initialize full system with background daemons
|
|
75
|
+
const { registry, executor, statusline } = await initializeHooks({
|
|
76
|
+
enableDaemons: true,
|
|
77
|
+
enableStatusline: true,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Generate statusline
|
|
81
|
+
console.log(statusline.generateStatusline());
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Using MCP Tools
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { hooksMCPTools, getHooksTool } from '@claude-flow/hooks';
|
|
88
|
+
|
|
89
|
+
// Get specific tool
|
|
90
|
+
const routeTool = getHooksTool('hooks/route');
|
|
91
|
+
|
|
92
|
+
// Execute routing
|
|
93
|
+
const result = await routeTool.handler({
|
|
94
|
+
task: 'Implement user authentication',
|
|
95
|
+
includeExplanation: true,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
console.log(`Recommended agent: ${result.recommendedAgent}`);
|
|
99
|
+
console.log(`Confidence: ${result.confidence}%`);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## CLI Commands
|
|
103
|
+
|
|
104
|
+
### Hooks Daemon
|
|
105
|
+
|
|
106
|
+
Manage background daemon processes for metrics and learning.
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# Start daemon with default 60s interval
|
|
110
|
+
hooks-daemon start
|
|
111
|
+
|
|
112
|
+
# Start with custom interval (30 seconds)
|
|
113
|
+
hooks-daemon start 30
|
|
114
|
+
|
|
115
|
+
# Stop daemon
|
|
116
|
+
hooks-daemon stop
|
|
117
|
+
|
|
118
|
+
# Check status
|
|
119
|
+
hooks-daemon status
|
|
120
|
+
|
|
121
|
+
# Run pattern consolidation
|
|
122
|
+
hooks-daemon consolidate
|
|
123
|
+
|
|
124
|
+
# Export learned patterns
|
|
125
|
+
hooks-daemon export json
|
|
126
|
+
|
|
127
|
+
# Rebuild HNSW index
|
|
128
|
+
hooks-daemon rebuild-index
|
|
129
|
+
|
|
130
|
+
# Notify activity (for hook integration)
|
|
131
|
+
hooks-daemon notify-activity
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Statusline
|
|
135
|
+
|
|
136
|
+
Generate statusline output for Claude Code integration.
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
# Display formatted statusline
|
|
140
|
+
statusline
|
|
141
|
+
|
|
142
|
+
# Output JSON data
|
|
143
|
+
statusline --json
|
|
144
|
+
|
|
145
|
+
# Compact JSON (single line)
|
|
146
|
+
statusline --compact
|
|
147
|
+
|
|
148
|
+
# Show help
|
|
149
|
+
statusline --help
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Example Output:**
|
|
153
|
+
```
|
|
154
|
+
▊ Claude Flow V3 ● agentic-flow@alpha │ ⎇ v3
|
|
155
|
+
─────────────────────────────────────────────────────
|
|
156
|
+
🏗️ DDD Domains [●●●●●] 5/5 ⚡ 1.0x → 2.49x-7.47x
|
|
157
|
+
🤖 Swarm Agents ◉ [ 5/15] 🟢 CVE 3/3 💾 156 patterns
|
|
158
|
+
🔧 Architecture DDD ●93% │ Security ●CLEAN │ Hooks ●ACTIVE
|
|
159
|
+
📊 Routing 89% accuracy │ Avg 4.2ms │ 1547 operations
|
|
160
|
+
─────────────────────────────────────────────────────
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Hook Events
|
|
164
|
+
|
|
165
|
+
| Event | Description |
|
|
166
|
+
|-------|-------------|
|
|
167
|
+
| `PreToolUse` | Before any tool execution |
|
|
168
|
+
| `PostToolUse` | After tool execution completes |
|
|
169
|
+
| `PreEdit` | Before file modification |
|
|
170
|
+
| `PostEdit` | After file modification |
|
|
171
|
+
| `PreRead` | Before file read |
|
|
172
|
+
| `PostRead` | After file read |
|
|
173
|
+
| `PreCommand` | Before shell command execution |
|
|
174
|
+
| `PostCommand` | After shell command completes |
|
|
175
|
+
| `PreTask` | Before task starts |
|
|
176
|
+
| `PostTask` | After task completes |
|
|
177
|
+
| `TaskProgress` | During task execution |
|
|
178
|
+
| `SessionStart` | When session begins |
|
|
179
|
+
| `SessionEnd` | When session ends |
|
|
180
|
+
| `SessionRestore` | When restoring previous session |
|
|
181
|
+
| `AgentSpawn` | When agent is spawned |
|
|
182
|
+
| `AgentTerminate` | When agent terminates |
|
|
183
|
+
| `PreRoute` | Before task routing |
|
|
184
|
+
| `PostRoute` | After routing decision |
|
|
185
|
+
| `PatternLearned` | When new pattern is learned |
|
|
186
|
+
| `PatternConsolidated` | When patterns are consolidated |
|
|
187
|
+
|
|
188
|
+
## Hook Priorities
|
|
189
|
+
|
|
190
|
+
| Priority | Value | Use Case |
|
|
191
|
+
|----------|-------|----------|
|
|
192
|
+
| `Critical` | 1000 | Security validation, must run first |
|
|
193
|
+
| `High` | 100 | Pre-processing, preparation |
|
|
194
|
+
| `Normal` | 50 | Standard hooks |
|
|
195
|
+
| `Low` | 10 | Logging, metrics |
|
|
196
|
+
| `Background` | 1 | Async operations, runs last |
|
|
197
|
+
|
|
198
|
+
## Background Workers
|
|
199
|
+
|
|
200
|
+
The hooks system includes 12 specialized background workers that can be triggered automatically or manually dispatched.
|
|
201
|
+
|
|
202
|
+
### Available Workers
|
|
203
|
+
|
|
204
|
+
| Worker | Priority | Est. Time | Description |
|
|
205
|
+
|--------|----------|-----------|-------------|
|
|
206
|
+
| `ultralearn` | normal | 60s | Deep knowledge acquisition and learning |
|
|
207
|
+
| `optimize` | high | 30s | Performance optimization and tuning |
|
|
208
|
+
| `consolidate` | low | 20s | Memory consolidation and cleanup |
|
|
209
|
+
| `predict` | normal | 15s | Predictive preloading and anticipation |
|
|
210
|
+
| `audit` | critical | 45s | Security analysis and vulnerability scanning |
|
|
211
|
+
| `map` | normal | 30s | Codebase mapping and architecture analysis |
|
|
212
|
+
| `preload` | low | 10s | Resource preloading and cache warming |
|
|
213
|
+
| `deepdive` | normal | 60s | Deep code analysis and examination |
|
|
214
|
+
| `document` | normal | 45s | Auto-documentation generation |
|
|
215
|
+
| `refactor` | normal | 30s | Code refactoring suggestions |
|
|
216
|
+
| `benchmark` | normal | 60s | Performance benchmarking |
|
|
217
|
+
| `testgaps` | normal | 30s | Test coverage analysis |
|
|
218
|
+
|
|
219
|
+
### Worker CLI Commands
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
# List all available workers
|
|
223
|
+
claude-flow hooks worker list
|
|
224
|
+
|
|
225
|
+
# Detect triggers from prompt text
|
|
226
|
+
claude-flow hooks worker detect --prompt "optimize performance"
|
|
227
|
+
|
|
228
|
+
# Auto-dispatch when triggers match (confidence ≥0.6)
|
|
229
|
+
claude-flow hooks worker detect --prompt "deep dive into auth" --auto-dispatch --min-confidence 0.6
|
|
230
|
+
|
|
231
|
+
# Manually dispatch a worker
|
|
232
|
+
claude-flow hooks worker dispatch --trigger refactor --context "auth module"
|
|
233
|
+
|
|
234
|
+
# Check worker status
|
|
235
|
+
claude-flow hooks worker status
|
|
236
|
+
|
|
237
|
+
# Cancel a running worker
|
|
238
|
+
claude-flow hooks worker cancel --id worker_refactor_1_abc123
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Performance Targets
|
|
242
|
+
|
|
243
|
+
| Metric | Target |
|
|
244
|
+
|--------|--------|
|
|
245
|
+
| Trigger detection | <5ms |
|
|
246
|
+
| Worker spawn | <50ms |
|
|
247
|
+
| Max concurrent | 10 |
|
|
248
|
+
|
|
249
|
+
### UserPromptSubmit Integration
|
|
250
|
+
|
|
251
|
+
Workers are automatically triggered via the `UserPromptSubmit` hook when prompt patterns match worker triggers with confidence ≥0.6. Add this to your Claude settings:
|
|
252
|
+
|
|
253
|
+
```json
|
|
254
|
+
{
|
|
255
|
+
"hooks": {
|
|
256
|
+
"UserPromptSubmit": [{
|
|
257
|
+
"matcher": ".*",
|
|
258
|
+
"hooks": [{
|
|
259
|
+
"type": "command",
|
|
260
|
+
"timeout": 6000,
|
|
261
|
+
"command": "claude-flow hooks worker detect --prompt \"$USER_PROMPT\" --auto-dispatch --min-confidence 0.6"
|
|
262
|
+
}]
|
|
263
|
+
}]
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## MCP Tools
|
|
269
|
+
|
|
270
|
+
| Tool | Description |
|
|
271
|
+
|------|-------------|
|
|
272
|
+
| `hooks/pre-edit` | Get context and suggestions before file edit |
|
|
273
|
+
| `hooks/post-edit` | Record edit outcome for learning |
|
|
274
|
+
| `hooks/route` | Route task to optimal agent |
|
|
275
|
+
| `hooks/metrics` | Query learning metrics |
|
|
276
|
+
| `hooks/pre-command` | Assess command risk |
|
|
277
|
+
| `hooks/post-command` | Record command outcome |
|
|
278
|
+
| `hooks/daemon-status` | Get daemon status |
|
|
279
|
+
| `hooks/statusline` | Get statusline data |
|
|
280
|
+
| `hooks/worker-list` | List all 12 background workers |
|
|
281
|
+
| `hooks/worker-dispatch` | Dispatch a worker by trigger type |
|
|
282
|
+
| `hooks/worker-status` | Get status of running workers |
|
|
283
|
+
| `hooks/worker-detect` | Detect worker triggers from prompt text |
|
|
284
|
+
| `hooks/worker-cancel` | Cancel a running worker |
|
|
285
|
+
|
|
286
|
+
## API Reference
|
|
287
|
+
|
|
288
|
+
### HookRegistry
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
class HookRegistry {
|
|
292
|
+
// Register a hook
|
|
293
|
+
register(
|
|
294
|
+
event: HookEvent,
|
|
295
|
+
handler: HookHandler,
|
|
296
|
+
priority: HookPriority,
|
|
297
|
+
options?: HookRegistrationOptions
|
|
298
|
+
): string;
|
|
299
|
+
|
|
300
|
+
// Unregister a hook
|
|
301
|
+
unregister(hookId: string): boolean;
|
|
302
|
+
|
|
303
|
+
// Get hooks for event
|
|
304
|
+
getForEvent(event: HookEvent, enabledOnly?: boolean): HookEntry[];
|
|
305
|
+
|
|
306
|
+
// Enable/disable hooks
|
|
307
|
+
enable(hookId: string): boolean;
|
|
308
|
+
disable(hookId: string): boolean;
|
|
309
|
+
|
|
310
|
+
// List hooks with filtering
|
|
311
|
+
list(filter?: HookListFilter): HookEntry[];
|
|
312
|
+
|
|
313
|
+
// Get statistics
|
|
314
|
+
getStats(): HookRegistryStats;
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### HookExecutor
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
class HookExecutor {
|
|
322
|
+
// Execute hooks for any event
|
|
323
|
+
execute<T>(
|
|
324
|
+
event: HookEvent,
|
|
325
|
+
context: Partial<HookContext<T>>,
|
|
326
|
+
options?: HookExecutionOptions
|
|
327
|
+
): Promise<HookExecutionResult>;
|
|
328
|
+
|
|
329
|
+
// Convenience methods
|
|
330
|
+
preToolUse(toolName: string, parameters: Record<string, unknown>): Promise<HookExecutionResult>;
|
|
331
|
+
postToolUse(toolName: string, parameters: Record<string, unknown>, duration: number): Promise<HookExecutionResult>;
|
|
332
|
+
preEdit(filePath: string, operation: 'create' | 'modify' | 'delete'): Promise<HookExecutionResult>;
|
|
333
|
+
postEdit(filePath: string, operation: 'create' | 'modify' | 'delete', duration: number): Promise<HookExecutionResult>;
|
|
334
|
+
preCommand(command: string, workingDirectory?: string): Promise<HookExecutionResult>;
|
|
335
|
+
postCommand(command: string, exitCode: number, output?: string, error?: string): Promise<HookExecutionResult>;
|
|
336
|
+
sessionStart(sessionId: string): Promise<HookExecutionResult>;
|
|
337
|
+
sessionEnd(sessionId: string): Promise<HookExecutionResult>;
|
|
338
|
+
agentSpawn(agentId: string, agentType: string): Promise<HookExecutionResult>;
|
|
339
|
+
agentTerminate(agentId: string, agentType: string, status: string): Promise<HookExecutionResult>;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### DaemonManager
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
class DaemonManager {
|
|
347
|
+
// Register and manage daemons
|
|
348
|
+
register(config: DaemonConfig, task: () => Promise<void>): void;
|
|
349
|
+
start(name: string): Promise<void>;
|
|
350
|
+
stop(name: string): Promise<void>;
|
|
351
|
+
restart(name: string): Promise<void>;
|
|
352
|
+
|
|
353
|
+
// Bulk operations
|
|
354
|
+
startAll(): Promise<void>;
|
|
355
|
+
stopAll(): Promise<void>;
|
|
356
|
+
|
|
357
|
+
// Status
|
|
358
|
+
getState(name: string): DaemonState | undefined;
|
|
359
|
+
getAllStates(): DaemonState[];
|
|
360
|
+
isRunning(name: string): boolean;
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### StatuslineGenerator
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
class StatuslineGenerator {
|
|
368
|
+
// Register data sources
|
|
369
|
+
registerDataSources(sources: StatuslineDataSources): void;
|
|
370
|
+
|
|
371
|
+
// Generate output
|
|
372
|
+
generateData(): StatuslineData;
|
|
373
|
+
generateStatusline(): string;
|
|
374
|
+
generateJSON(): string;
|
|
375
|
+
generateCompactJSON(): string;
|
|
376
|
+
|
|
377
|
+
// Configuration
|
|
378
|
+
updateConfig(config: Partial<StatuslineConfig>): void;
|
|
379
|
+
invalidateCache(): void;
|
|
380
|
+
}
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
## Environment Variables
|
|
384
|
+
|
|
385
|
+
| Variable | Description | Default |
|
|
386
|
+
|----------|-------------|---------|
|
|
387
|
+
| `CLAUDE_FLOW_HOOK_TIMEOUT` | Hook execution timeout (ms) | `5000` |
|
|
388
|
+
| `CLAUDE_FLOW_REASONINGBANK_ENABLED` | Enable ReasoningBank | `true` |
|
|
389
|
+
| `CLAUDE_FLOW_HOOKS_NAMESPACE` | Learning namespace | `hooks-learning` |
|
|
390
|
+
| `CLAUDE_FLOW_HOOKS_LOG_LEVEL` | Logging level | `info` |
|
|
391
|
+
| `CLAUDE_FLOW_SHOW_HOOKS_METRICS` | Show hooks in statusline | `true` |
|
|
392
|
+
| `CLAUDE_FLOW_SHOW_SWARM_ACTIVITY` | Show swarm in statusline | `true` |
|
|
393
|
+
| `CLAUDE_FLOW_SHOW_PERFORMANCE` | Show performance targets | `true` |
|
|
394
|
+
|
|
395
|
+
## Integration with Claude Code
|
|
396
|
+
|
|
397
|
+
Add to your Claude settings (`~/.claude/settings.json`):
|
|
398
|
+
|
|
399
|
+
```json
|
|
400
|
+
{
|
|
401
|
+
"hooks": {
|
|
402
|
+
"SessionStart": [{
|
|
403
|
+
"hooks": [{
|
|
404
|
+
"type": "command",
|
|
405
|
+
"timeout": 5000,
|
|
406
|
+
"command": "hooks-daemon start"
|
|
407
|
+
}]
|
|
408
|
+
}],
|
|
409
|
+
"SessionEnd": [{
|
|
410
|
+
"hooks": [{
|
|
411
|
+
"type": "command",
|
|
412
|
+
"timeout": 3000,
|
|
413
|
+
"command": "hooks-daemon stop"
|
|
414
|
+
}]
|
|
415
|
+
}],
|
|
416
|
+
"PreToolUse": [{
|
|
417
|
+
"hooks": [{
|
|
418
|
+
"type": "command",
|
|
419
|
+
"timeout": 100,
|
|
420
|
+
"command": "hooks-daemon notify-activity"
|
|
421
|
+
}]
|
|
422
|
+
}]
|
|
423
|
+
},
|
|
424
|
+
"statusLine": {
|
|
425
|
+
"type": "command",
|
|
426
|
+
"command": "statusline"
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
## Related Packages
|
|
432
|
+
|
|
433
|
+
- [@claude-flow/shared](../shared) - Shared utilities and types
|
|
434
|
+
- [@claude-flow/neural](../neural) - Neural network and SONA learning
|
|
435
|
+
- [@claude-flow/swarm](../swarm) - Multi-agent coordination
|
|
436
|
+
- [@claude-flow/memory](../memory) - AgentDB memory system
|
|
437
|
+
|
|
438
|
+
## License
|
|
439
|
+
|
|
440
|
+
MIT © [Claude Flow Team](https://github.com/ruvnet/claude-flow)
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hooks Daemon CLI
|
|
5
|
+
*
|
|
6
|
+
* Background daemon for hooks learning and metrics collection.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* hooks-daemon start [interval] Start the daemon
|
|
10
|
+
* hooks-daemon stop Stop the daemon
|
|
11
|
+
* hooks-daemon status Check daemon status
|
|
12
|
+
* hooks-daemon consolidate Run pattern consolidation
|
|
13
|
+
* hooks-daemon notify-activity Notify of activity (for hooks)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { fileURLToPath } from 'url';
|
|
17
|
+
import { dirname, join } from 'path';
|
|
18
|
+
import { DaemonManager, HooksLearningDaemon, MetricsDaemon } from '../dist/daemons/index.js';
|
|
19
|
+
|
|
20
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
21
|
+
const __dirname = dirname(__filename);
|
|
22
|
+
|
|
23
|
+
// Parse command line arguments
|
|
24
|
+
const args = process.argv.slice(2);
|
|
25
|
+
const command = args[0] || 'status';
|
|
26
|
+
|
|
27
|
+
// State file for daemon persistence
|
|
28
|
+
const STATE_FILE = join(process.cwd(), '.claude-flow', 'hooks-daemon.json');
|
|
29
|
+
|
|
30
|
+
async function main() {
|
|
31
|
+
const daemonManager = new DaemonManager({
|
|
32
|
+
pidDirectory: join(process.cwd(), '.claude-flow', 'pids'),
|
|
33
|
+
logDirectory: join(process.cwd(), '.claude-flow', 'logs'),
|
|
34
|
+
autoRestart: true,
|
|
35
|
+
maxRestartAttempts: 3,
|
|
36
|
+
daemons: [],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const learningDaemon = new HooksLearningDaemon(daemonManager);
|
|
40
|
+
const metricsDaemon = new MetricsDaemon(daemonManager);
|
|
41
|
+
|
|
42
|
+
switch (command) {
|
|
43
|
+
case 'start': {
|
|
44
|
+
const interval = parseInt(args[1], 10) || 60; // Default 60 seconds
|
|
45
|
+
console.log(`Starting hooks daemon with ${interval}s interval...`);
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
await Promise.all([
|
|
49
|
+
learningDaemon.start(),
|
|
50
|
+
metricsDaemon.start(),
|
|
51
|
+
]);
|
|
52
|
+
console.log('Hooks daemon started successfully.');
|
|
53
|
+
console.log(`PID: ${process.pid}`);
|
|
54
|
+
console.log(`Interval: ${interval}s`);
|
|
55
|
+
|
|
56
|
+
// Keep process alive
|
|
57
|
+
process.on('SIGINT', async () => {
|
|
58
|
+
console.log('\nShutting down hooks daemon...');
|
|
59
|
+
await Promise.all([
|
|
60
|
+
learningDaemon.stop(),
|
|
61
|
+
metricsDaemon.stop(),
|
|
62
|
+
]);
|
|
63
|
+
process.exit(0);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
process.on('SIGTERM', async () => {
|
|
67
|
+
await Promise.all([
|
|
68
|
+
learningDaemon.stop(),
|
|
69
|
+
metricsDaemon.stop(),
|
|
70
|
+
]);
|
|
71
|
+
process.exit(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Keep alive
|
|
75
|
+
setInterval(() => {}, 1000);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error('Failed to start hooks daemon:', error.message);
|
|
78
|
+
process.exit(1);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
case 'stop': {
|
|
84
|
+
console.log('Stopping hooks daemon...');
|
|
85
|
+
try {
|
|
86
|
+
await Promise.all([
|
|
87
|
+
learningDaemon.stop(),
|
|
88
|
+
metricsDaemon.stop(),
|
|
89
|
+
]);
|
|
90
|
+
console.log('Hooks daemon stopped.');
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error('Failed to stop hooks daemon:', error.message);
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
case 'status': {
|
|
99
|
+
const states = daemonManager.getAllStates();
|
|
100
|
+
console.log('Hooks Daemon Status');
|
|
101
|
+
console.log('===================');
|
|
102
|
+
|
|
103
|
+
if (states.length === 0) {
|
|
104
|
+
console.log('No daemons registered.');
|
|
105
|
+
} else {
|
|
106
|
+
for (const state of states) {
|
|
107
|
+
const status = state.status === 'running' ? '🟢' : '🔴';
|
|
108
|
+
console.log(`${status} ${state.name}: ${state.status}`);
|
|
109
|
+
if (state.lastUpdateAt) {
|
|
110
|
+
console.log(` Last update: ${state.lastUpdateAt.toISOString()}`);
|
|
111
|
+
}
|
|
112
|
+
console.log(` Executions: ${state.executionCount}`);
|
|
113
|
+
console.log(` Failures: ${state.failureCount}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const stats = learningDaemon.getStats();
|
|
118
|
+
console.log('\nLearning Stats:');
|
|
119
|
+
console.log(` Patterns learned: ${stats.patternsLearned}`);
|
|
120
|
+
console.log(` Routing accuracy: ${stats.routingAccuracy}%`);
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
case 'consolidate': {
|
|
125
|
+
console.log('Running pattern consolidation...');
|
|
126
|
+
try {
|
|
127
|
+
// Force a consolidation cycle
|
|
128
|
+
await learningDaemon.start();
|
|
129
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
130
|
+
await learningDaemon.stop();
|
|
131
|
+
console.log('Pattern consolidation completed.');
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.error('Consolidation failed:', error.message);
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
case 'notify-activity': {
|
|
140
|
+
// Quick notification for hook integration
|
|
141
|
+
const metrics = metricsDaemon.getMetrics();
|
|
142
|
+
console.log(JSON.stringify({
|
|
143
|
+
notified: true,
|
|
144
|
+
timestamp: new Date().toISOString(),
|
|
145
|
+
metrics,
|
|
146
|
+
}));
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
case 'export': {
|
|
151
|
+
const format = args[1] || 'json';
|
|
152
|
+
const stats = learningDaemon.getStats();
|
|
153
|
+
const metrics = metricsDaemon.getMetrics();
|
|
154
|
+
|
|
155
|
+
const data = {
|
|
156
|
+
stats,
|
|
157
|
+
metrics,
|
|
158
|
+
exportedAt: new Date().toISOString(),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
if (format === 'json') {
|
|
162
|
+
console.log(JSON.stringify(data, null, 2));
|
|
163
|
+
} else {
|
|
164
|
+
console.log('Hooks Learning Export');
|
|
165
|
+
console.log('====================');
|
|
166
|
+
console.log(`Patterns: ${stats.patternsLearned}`);
|
|
167
|
+
console.log(`Accuracy: ${stats.routingAccuracy}%`);
|
|
168
|
+
console.log(`Exported: ${data.exportedAt}`);
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
case 'rebuild-index': {
|
|
174
|
+
console.log('Rebuilding HNSW index...');
|
|
175
|
+
// In real implementation, this would rebuild the vector index
|
|
176
|
+
console.log('Index rebuild completed.');
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
default:
|
|
181
|
+
console.log(`Unknown command: ${command}`);
|
|
182
|
+
console.log(`
|
|
183
|
+
Usage:
|
|
184
|
+
hooks-daemon start [interval] Start the daemon (interval in seconds)
|
|
185
|
+
hooks-daemon stop Stop the daemon
|
|
186
|
+
hooks-daemon status Check daemon status
|
|
187
|
+
hooks-daemon consolidate Run pattern consolidation
|
|
188
|
+
hooks-daemon notify-activity Notify of activity (for hooks)
|
|
189
|
+
hooks-daemon export [format] Export patterns (json|text)
|
|
190
|
+
hooks-daemon rebuild-index Rebuild HNSW index
|
|
191
|
+
`);
|
|
192
|
+
process.exit(1);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
main().catch((error) => {
|
|
197
|
+
console.error('Daemon error:', error);
|
|
198
|
+
process.exit(1);
|
|
199
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Statusline CLI
|
|
5
|
+
*
|
|
6
|
+
* Generate statusline output for Claude Code integration.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* statusline Output formatted statusline
|
|
10
|
+
* statusline --json Output JSON data
|
|
11
|
+
* statusline --compact Output compact JSON
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { fileURLToPath } from 'url';
|
|
15
|
+
import { dirname } from 'path';
|
|
16
|
+
import { StatuslineGenerator, parseStatuslineData } from '../dist/statusline/index.js';
|
|
17
|
+
|
|
18
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
19
|
+
const __dirname = dirname(__filename);
|
|
20
|
+
|
|
21
|
+
// Parse command line arguments
|
|
22
|
+
const args = process.argv.slice(2);
|
|
23
|
+
const jsonMode = args.includes('--json');
|
|
24
|
+
const compactMode = args.includes('--compact');
|
|
25
|
+
const helpMode = args.includes('--help') || args.includes('-h');
|
|
26
|
+
|
|
27
|
+
async function main() {
|
|
28
|
+
if (helpMode) {
|
|
29
|
+
console.log(`
|
|
30
|
+
Statusline - V3 Hooks System Status Generator
|
|
31
|
+
|
|
32
|
+
Usage:
|
|
33
|
+
statusline Output formatted statusline (default)
|
|
34
|
+
statusline --json Output JSON data
|
|
35
|
+
statusline --compact Output compact JSON (single line)
|
|
36
|
+
statusline --help Show this help
|
|
37
|
+
|
|
38
|
+
Environment Variables:
|
|
39
|
+
CLAUDE_FLOW_STATUSLINE_REFRESH Refresh interval in ms
|
|
40
|
+
CLAUDE_FLOW_SHOW_HOOKS_METRICS Show hooks metrics (true/false)
|
|
41
|
+
CLAUDE_FLOW_SHOW_SWARM_ACTIVITY Show swarm activity (true/false)
|
|
42
|
+
CLAUDE_FLOW_SHOW_PERFORMANCE Show performance targets (true/false)
|
|
43
|
+
|
|
44
|
+
Examples:
|
|
45
|
+
statusline # Display formatted status
|
|
46
|
+
statusline --json | jq # Parse JSON output
|
|
47
|
+
statusline --compact # Single line JSON for scripting
|
|
48
|
+
`);
|
|
49
|
+
process.exit(0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Create generator with environment-based config
|
|
53
|
+
const generator = new StatuslineGenerator({
|
|
54
|
+
enabled: true,
|
|
55
|
+
refreshOnHook: true,
|
|
56
|
+
showHooksMetrics: process.env.CLAUDE_FLOW_SHOW_HOOKS_METRICS !== 'false',
|
|
57
|
+
showSwarmActivity: process.env.CLAUDE_FLOW_SHOW_SWARM_ACTIVITY !== 'false',
|
|
58
|
+
showPerformance: process.env.CLAUDE_FLOW_SHOW_PERFORMANCE !== 'false',
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Try to read from metrics database or files
|
|
62
|
+
// In real implementation, this would read from SQLite
|
|
63
|
+
// For now, use default data
|
|
64
|
+
|
|
65
|
+
if (compactMode) {
|
|
66
|
+
console.log(generator.generateCompactJSON());
|
|
67
|
+
} else if (jsonMode) {
|
|
68
|
+
console.log(generator.generateJSON());
|
|
69
|
+
} else {
|
|
70
|
+
console.log(generator.generateStatusline());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
main().catch((error) => {
|
|
75
|
+
console.error('Statusline error:', error.message);
|
|
76
|
+
process.exit(1);
|
|
77
|
+
});
|