@tecet/ollm 0.1.4 → 0.1.5
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/dist/cli.js +20 -14
- package/dist/cli.js.map +3 -3
- package/dist/services/documentService.d.ts.map +1 -1
- package/dist/services/documentService.js +12 -2
- package/dist/services/documentService.js.map +1 -1
- package/dist/ui/components/docs/DocsPanel.d.ts.map +1 -1
- package/dist/ui/components/docs/DocsPanel.js +1 -1
- package/dist/ui/components/docs/DocsPanel.js.map +1 -1
- package/dist/ui/components/launch/VersionBanner.js +1 -1
- package/dist/ui/components/launch/VersionBanner.js.map +1 -1
- package/dist/ui/components/layout/KeybindsLegend.d.ts.map +1 -1
- package/dist/ui/components/layout/KeybindsLegend.js +1 -1
- package/dist/ui/components/layout/KeybindsLegend.js.map +1 -1
- package/dist/ui/components/tabs/BugReportTab.js +1 -1
- package/dist/ui/components/tabs/BugReportTab.js.map +1 -1
- package/dist/ui/services/docsService.d.ts +12 -27
- package/dist/ui/services/docsService.d.ts.map +1 -1
- package/dist/ui/services/docsService.js +40 -67
- package/dist/ui/services/docsService.js.map +1 -1
- package/docs/README.md +3 -410
- package/package.json +10 -7
- package/scripts/copy-docs-to-user.cjs +34 -0
- package/docs/Context/CheckpointFlowDiagram.md +0 -673
- package/docs/Context/ContextArchitecture.md +0 -898
- package/docs/Context/ContextCompression.md +0 -1102
- package/docs/Context/ContextManagment.md +0 -750
- package/docs/Context/Index.md +0 -209
- package/docs/Context/README.md +0 -390
- package/docs/DevelopmentRoadmap/Index.md +0 -238
- package/docs/DevelopmentRoadmap/OLLM-CLI_Releases.md +0 -419
- package/docs/DevelopmentRoadmap/PlanedFeatures.md +0 -448
- package/docs/DevelopmentRoadmap/README.md +0 -174
- package/docs/DevelopmentRoadmap/Roadmap.md +0 -572
- package/docs/DevelopmentRoadmap/RoadmapVisual.md +0 -372
- package/docs/Hooks/Architecture.md +0 -885
- package/docs/Hooks/Index.md +0 -244
- package/docs/Hooks/KeyboardShortcuts.md +0 -248
- package/docs/Hooks/Protocol.md +0 -817
- package/docs/Hooks/README.md +0 -403
- package/docs/Hooks/UserGuide.md +0 -1483
- package/docs/Hooks/VisualGuide.md +0 -598
- package/docs/Index.md +0 -506
- package/docs/Installation.md +0 -586
- package/docs/Introduction.md +0 -367
- package/docs/LLM Models/Index.md +0 -239
- package/docs/LLM Models/LLM_GettingStarted.md +0 -748
- package/docs/LLM Models/LLM_Index.md +0 -701
- package/docs/LLM Models/LLM_MemorySystem.md +0 -337
- package/docs/LLM Models/LLM_ModelCompatibility.md +0 -499
- package/docs/LLM Models/LLM_ModelsArchitecture.md +0 -933
- package/docs/LLM Models/LLM_ModelsCommands.md +0 -839
- package/docs/LLM Models/LLM_ModelsConfiguration.md +0 -1094
- package/docs/LLM Models/LLM_ModelsList.md +0 -1071
- package/docs/LLM Models/LLM_ModelsList.md.backup +0 -400
- package/docs/LLM Models/README.md +0 -355
- package/docs/MCP/MCP_Architecture.md +0 -1086
- package/docs/MCP/MCP_Commands.md +0 -1111
- package/docs/MCP/MCP_GettingStarted.md +0 -590
- package/docs/MCP/MCP_Index.md +0 -524
- package/docs/MCP/MCP_Integration.md +0 -866
- package/docs/MCP/MCP_Marketplace.md +0 -160
- package/docs/MCP/README.md +0 -415
- package/docs/Prompts System/Architecture.md +0 -760
- package/docs/Prompts System/Index.md +0 -223
- package/docs/Prompts System/PromptsRouting.md +0 -1047
- package/docs/Prompts System/PromptsTemplates.md +0 -1102
- package/docs/Prompts System/README.md +0 -389
- package/docs/Prompts System/SystemPrompts.md +0 -856
- package/docs/Quickstart.md +0 -535
- package/docs/Tools/Architecture.md +0 -884
- package/docs/Tools/GettingStarted.md +0 -624
- package/docs/Tools/Index.md +0 -216
- package/docs/Tools/ManifestReference.md +0 -141
- package/docs/Tools/README.md +0 -440
- package/docs/Tools/UserGuide.md +0 -773
- package/docs/Troubleshooting.md +0 -1265
- package/docs/UI&Settings/Architecture.md +0 -729
- package/docs/UI&Settings/ColorASCII.md +0 -34
- package/docs/UI&Settings/Commands.md +0 -755
- package/docs/UI&Settings/Configuration.md +0 -872
- package/docs/UI&Settings/Index.md +0 -293
- package/docs/UI&Settings/Keybinds.md +0 -372
- package/docs/UI&Settings/README.md +0 -278
- package/docs/UI&Settings/Terminal.md +0 -637
- package/docs/UI&Settings/Themes.md +0 -604
- package/docs/UI&Settings/UIGuide.md +0 -550
|
@@ -1,885 +0,0 @@
|
|
|
1
|
-
# Hook System Architecture
|
|
2
|
-
|
|
3
|
-
**Technical Architecture and Design of the Hook System**
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## Table of Contents
|
|
8
|
-
|
|
9
|
-
1. [Overview](#overview)
|
|
10
|
-
2. [System Architecture](#system-architecture)
|
|
11
|
-
3. [Core Components](#core-components)
|
|
12
|
-
4. [Data Flow](#data-flow)
|
|
13
|
-
5. [Integration Points](#integration-points)
|
|
14
|
-
6. [Design Decisions](#design-decisions)
|
|
15
|
-
7. [Security Model](#security-model)
|
|
16
|
-
|
|
17
|
-
**See Also:**
|
|
18
|
-
|
|
19
|
-
- [Hook User Guide](UserGuide.md) - Using hooks
|
|
20
|
-
- [Hook Protocol](Protocol.md) - Technical protocol specification
|
|
21
|
-
- [MCP Architecture](../MCP/MCP_Architecture.md) - System architecture
|
|
22
|
-
- [MCP Commands](../MCP/MCP_Commands.md) - Hook command reference
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
## Overview
|
|
27
|
-
|
|
28
|
-
The Hook System provides event-driven automation and safety gates for OLLM CLI. Hooks execute custom scripts in response to lifecycle events, enabling workflow automation, validation, and integration with external systems.
|
|
29
|
-
|
|
30
|
-
### Key Features
|
|
31
|
-
|
|
32
|
-
- ✅ 12 event types for comprehensive lifecycle coverage
|
|
33
|
-
- ✅ JSON-based stdin/stdout protocol
|
|
34
|
-
- ✅ Trust model with approval workflow
|
|
35
|
-
- ✅ Rate limiting and execution planning
|
|
36
|
-
- ✅ Parallel and sequential execution strategies
|
|
37
|
-
- ✅ Command whitelist for security
|
|
38
|
-
- ✅ Integration with MCP and extensions
|
|
39
|
-
|
|
40
|
-
---
|
|
41
|
-
|
|
42
|
-
## System Architecture
|
|
43
|
-
|
|
44
|
-
### High-Level Architecture
|
|
45
|
-
|
|
46
|
-
```mermaid
|
|
47
|
-
graph TB
|
|
48
|
-
subgraph Events["Event Sources"]
|
|
49
|
-
FileEvent["File Events"]
|
|
50
|
-
AgentEvent["Agent Events"]
|
|
51
|
-
ModelEvent["Model Events"]
|
|
52
|
-
ToolEvent["Tool Events"]
|
|
53
|
-
UserEvent["User Events"]
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
subgraph HookSystem["Hook System"]
|
|
57
|
-
EventHandler["Hook Event Handler"]
|
|
58
|
-
Registry["Hook Registry"]
|
|
59
|
-
Planner["Hook Planner"]
|
|
60
|
-
Runner["Hook Runner"]
|
|
61
|
-
Translator["Hook Translator"]
|
|
62
|
-
TrustModel["Trust Model"]
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
subgraph Execution["Execution"]
|
|
66
|
-
AskAgent["Ask Agent"]
|
|
67
|
-
RunCommand["Run Command"]
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
Events --> EventHandler
|
|
71
|
-
EventHandler --> Registry
|
|
72
|
-
Registry --> Planner
|
|
73
|
-
Planner --> TrustModel
|
|
74
|
-
TrustModel --> Runner
|
|
75
|
-
Runner --> Translator
|
|
76
|
-
Translator --> AskAgent
|
|
77
|
-
Translator --> RunCommand
|
|
78
|
-
|
|
79
|
-
style HookSystem fill:#f9f,stroke:#333,stroke-width:2px
|
|
80
|
-
style Events fill:#bbf,stroke:#333,stroke-width:2px
|
|
81
|
-
style Execution fill:#bfb,stroke:#333,stroke-width:2px
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Component Interaction Flow
|
|
85
|
-
|
|
86
|
-
```mermaid
|
|
87
|
-
flowchart LR
|
|
88
|
-
Event[Event Occurs] --> Handler[Event Handler]
|
|
89
|
-
Handler --> Registry[Hook Registry]
|
|
90
|
-
Registry --> Planner[Hook Planner]
|
|
91
|
-
Planner --> Trust[Trust Model]
|
|
92
|
-
Trust --> Runner[Hook Runner]
|
|
93
|
-
Runner --> Exec[Execute Hook]
|
|
94
|
-
Exec --> Result[Process Result]
|
|
95
|
-
|
|
96
|
-
style Event fill:#e1f5ff
|
|
97
|
-
style Trust fill:#ffe1e1
|
|
98
|
-
style Exec fill:#e8f5e9
|
|
99
|
-
style Result fill:#f3e5f5
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
---
|
|
103
|
-
|
|
104
|
-
## Core Components
|
|
105
|
-
|
|
106
|
-
### 1. Hook Event Handler
|
|
107
|
-
|
|
108
|
-
**Location:** `packages/core/src/hooks/hookEventHandler.ts`
|
|
109
|
-
|
|
110
|
-
**Responsibilities:**
|
|
111
|
-
|
|
112
|
-
- Emit events when lifecycle points are reached
|
|
113
|
-
- Provide event data to hook system
|
|
114
|
-
- Manage event subscriptions
|
|
115
|
-
|
|
116
|
-
**Key Methods:**
|
|
117
|
-
|
|
118
|
-
```typescript
|
|
119
|
-
interface HookEventHandler {
|
|
120
|
-
emit(eventType: string, eventData: any): Promise<void>;
|
|
121
|
-
on(eventType: string, handler: Function): void;
|
|
122
|
-
off(eventType: string, handler: Function): void;
|
|
123
|
-
}
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
**Event Types (12 total):**
|
|
127
|
-
|
|
128
|
-
1. `session_start` - Session initialization
|
|
129
|
-
2. `session_end` - Session cleanup
|
|
130
|
-
3. `before_agent` - Before agent processes request
|
|
131
|
-
4. `after_agent` - After agent response
|
|
132
|
-
5. `before_model` - Before model invocation
|
|
133
|
-
6. `after_model` - After model response
|
|
134
|
-
7. `before_tool_selection` - Before tool selection
|
|
135
|
-
8. `before_tool` - Before tool execution
|
|
136
|
-
9. `after_tool` - After tool execution
|
|
137
|
-
10. `pre_compress` - Before context compression
|
|
138
|
-
11. `post_compress` - After context compression
|
|
139
|
-
12. `notification` - System notifications
|
|
140
|
-
|
|
141
|
-
### 2. Hook Registry
|
|
142
|
-
|
|
143
|
-
**Location:** `packages/core/src/hooks/hookRegistry.ts`
|
|
144
|
-
|
|
145
|
-
**Responsibilities:**
|
|
146
|
-
|
|
147
|
-
- Store and retrieve hook definitions
|
|
148
|
-
- Filter hooks by event type
|
|
149
|
-
- Manage hook lifecycle (register, unregister)
|
|
150
|
-
- Track hook metadata
|
|
151
|
-
|
|
152
|
-
**Key Methods:**
|
|
153
|
-
|
|
154
|
-
```typescript
|
|
155
|
-
interface HookRegistry {
|
|
156
|
-
registerHook(hook: HookDefinition): void;
|
|
157
|
-
unregisterHook(hookId: string): void;
|
|
158
|
-
getHooksForEvent(eventType: string): HookDefinition[];
|
|
159
|
-
getAllHooks(): HookDefinition[];
|
|
160
|
-
getHook(hookId: string): HookDefinition | undefined;
|
|
161
|
-
}
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
**Hook Definition:**
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
interface HookDefinition {
|
|
168
|
-
id: string;
|
|
169
|
-
name: string;
|
|
170
|
-
eventType: string;
|
|
171
|
-
filePatterns?: string[];
|
|
172
|
-
action: {
|
|
173
|
-
type: 'askAgent' | 'runCommand';
|
|
174
|
-
prompt?: string;
|
|
175
|
-
command?: string;
|
|
176
|
-
};
|
|
177
|
-
trusted: boolean;
|
|
178
|
-
enabled: boolean;
|
|
179
|
-
source: 'builtin' | 'user' | 'workspace' | 'extension';
|
|
180
|
-
}
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### 3. Hook Planner
|
|
184
|
-
|
|
185
|
-
**Location:** `packages/core/src/hooks/hookPlanner.ts`
|
|
186
|
-
|
|
187
|
-
**Responsibilities:**
|
|
188
|
-
|
|
189
|
-
- Determine execution strategy (sequential, parallel, optimized)
|
|
190
|
-
- Check execution conditions
|
|
191
|
-
- Evaluate priorities
|
|
192
|
-
- Apply rate limiting
|
|
193
|
-
|
|
194
|
-
**Execution Strategies:**
|
|
195
|
-
|
|
196
|
-
```mermaid
|
|
197
|
-
graph TD
|
|
198
|
-
Start[Hook List] --> Analyze[Analyze Dependencies]
|
|
199
|
-
Analyze --> HasDeps{Has Dependencies?}
|
|
200
|
-
|
|
201
|
-
HasDeps -->|Yes| Sequential[Sequential Execution]
|
|
202
|
-
HasDeps -->|No| CheckParallel{Can Parallelize?}
|
|
203
|
-
|
|
204
|
-
CheckParallel -->|Yes| Parallel[Parallel Execution]
|
|
205
|
-
CheckParallel -->|No| Sequential
|
|
206
|
-
|
|
207
|
-
Sequential --> Execute1[Execute Hook 1]
|
|
208
|
-
Execute1 --> Execute2[Execute Hook 2]
|
|
209
|
-
Execute2 --> Execute3[Execute Hook 3]
|
|
210
|
-
|
|
211
|
-
Parallel --> ParallelExec[Execute All Hooks]
|
|
212
|
-
ParallelExec --> Wait[Wait for All]
|
|
213
|
-
|
|
214
|
-
Execute3 --> Done[Complete]
|
|
215
|
-
Wait --> Done
|
|
216
|
-
|
|
217
|
-
style Start fill:#e1f5ff
|
|
218
|
-
style Sequential fill:#fff4e1
|
|
219
|
-
style Parallel fill:#e8f5e9
|
|
220
|
-
style Done fill:#f3e5f5
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
**Rate Limiting:**
|
|
224
|
-
|
|
225
|
-
- Per-hook rate limits
|
|
226
|
-
- Global rate limits
|
|
227
|
-
- Cooldown periods
|
|
228
|
-
- Execution count tracking
|
|
229
|
-
|
|
230
|
-
### 4. Hook Runner
|
|
231
|
-
|
|
232
|
-
**Location:** `packages/core/src/hooks/hookRunner.ts`
|
|
233
|
-
|
|
234
|
-
**Responsibilities:**
|
|
235
|
-
|
|
236
|
-
- Execute hook processes
|
|
237
|
-
- Manage stdin/stdout communication
|
|
238
|
-
- Handle timeouts
|
|
239
|
-
- Capture and format results
|
|
240
|
-
- Enforce command whitelist
|
|
241
|
-
|
|
242
|
-
**Command Whitelist (Line 128):**
|
|
243
|
-
|
|
244
|
-
```typescript
|
|
245
|
-
const WHITELISTED_COMMANDS = ['node', 'python', 'python3', 'bash', 'sh', 'npx', 'uvx'];
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
**Execution Flow:**
|
|
249
|
-
|
|
250
|
-
```mermaid
|
|
251
|
-
flowchart TD
|
|
252
|
-
Start[Start Execution] --> Validate[Validate Command]
|
|
253
|
-
Validate --> Whitelist{In Whitelist?}
|
|
254
|
-
|
|
255
|
-
Whitelist -->|No| Block[Block Execution]
|
|
256
|
-
Whitelist -->|Yes| Spawn[Spawn Process]
|
|
257
|
-
|
|
258
|
-
Spawn --> WriteStdin[Write Event to stdin]
|
|
259
|
-
WriteStdin --> Wait[Wait for Response]
|
|
260
|
-
Wait --> Timeout{Timeout?}
|
|
261
|
-
|
|
262
|
-
Timeout -->|Yes| Kill[Kill Process]
|
|
263
|
-
Timeout -->|No| ReadStdout[Read from stdout]
|
|
264
|
-
|
|
265
|
-
ReadStdout --> Parse[Parse JSON]
|
|
266
|
-
Parse --> Valid{Valid JSON?}
|
|
267
|
-
|
|
268
|
-
Valid -->|No| Error[Return Error]
|
|
269
|
-
Valid -->|Yes| Return[Return Result]
|
|
270
|
-
|
|
271
|
-
Kill --> Error
|
|
272
|
-
Block --> Error
|
|
273
|
-
|
|
274
|
-
style Start fill:#e1f5ff
|
|
275
|
-
style Whitelist fill:#fff4e1
|
|
276
|
-
style Block fill:#ffe1e1
|
|
277
|
-
style Return fill:#e8f5e9
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
### 5. Hook Translator
|
|
281
|
-
|
|
282
|
-
**Location:** `packages/core/src/hooks/hookTranslator.ts`
|
|
283
|
-
|
|
284
|
-
**Responsibilities:**
|
|
285
|
-
|
|
286
|
-
- Translate legacy hook format to modern format
|
|
287
|
-
- Convert between JSON and TypeScript types
|
|
288
|
-
- Format hook input/output
|
|
289
|
-
- Handle protocol versioning
|
|
290
|
-
|
|
291
|
-
**Translation Flow:**
|
|
292
|
-
|
|
293
|
-
```typescript
|
|
294
|
-
// Legacy Format (JSON)
|
|
295
|
-
{
|
|
296
|
-
"name": "Lint on Save",
|
|
297
|
-
"when": { "type": "fileEdited", "patterns": ["*.ts"] },
|
|
298
|
-
"then": { "type": "askAgent", "prompt": "Run linting" }
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Modern Format (TypeScript)
|
|
302
|
-
{
|
|
303
|
-
id: "lint-on-save",
|
|
304
|
-
name: "Lint on Save",
|
|
305
|
-
eventType: "fileEdited",
|
|
306
|
-
filePatterns: ["*.ts"],
|
|
307
|
-
action: {
|
|
308
|
-
type: "askAgent",
|
|
309
|
-
prompt: "Run linting on {{file.path}}"
|
|
310
|
-
},
|
|
311
|
-
trusted: false,
|
|
312
|
-
enabled: true
|
|
313
|
-
}
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
### 6. Trust Model
|
|
317
|
-
|
|
318
|
-
**Location:** `packages/core/src/hooks/trustedHooks.ts`
|
|
319
|
-
|
|
320
|
-
**Responsibilities:**
|
|
321
|
-
|
|
322
|
-
- Verify hook trust level
|
|
323
|
-
- Request user approval
|
|
324
|
-
- Store approval decisions
|
|
325
|
-
- Compute and verify hook hashes
|
|
326
|
-
|
|
327
|
-
**Trust Levels:**
|
|
328
|
-
|
|
329
|
-
```mermaid
|
|
330
|
-
graph TD
|
|
331
|
-
Hook[Hook] --> CheckSource{Check Source}
|
|
332
|
-
|
|
333
|
-
CheckSource -->|Builtin| Trusted[Always Trusted]
|
|
334
|
-
CheckSource -->|User| CheckHash{Check Hash}
|
|
335
|
-
CheckSource -->|Workspace| CheckApproval{Check Approval}
|
|
336
|
-
CheckSource -->|Extension| CheckApproval
|
|
337
|
-
|
|
338
|
-
CheckHash -->|Match| Trusted
|
|
339
|
-
CheckHash -->|Mismatch| RequestApproval[Request Approval]
|
|
340
|
-
|
|
341
|
-
CheckApproval -->|Approved| Trusted
|
|
342
|
-
CheckApproval -->|Not Approved| RequestApproval
|
|
343
|
-
|
|
344
|
-
RequestApproval --> UserDecision{User Approves?}
|
|
345
|
-
UserDecision -->|Yes| StoreApproval[Store Approval]
|
|
346
|
-
UserDecision -->|No| Block[Block Execution]
|
|
347
|
-
|
|
348
|
-
StoreApproval --> Trusted
|
|
349
|
-
Trusted --> Execute[Execute Hook]
|
|
350
|
-
|
|
351
|
-
style Trusted fill:#e8f5e9
|
|
352
|
-
style Block fill:#ffe1e1
|
|
353
|
-
style Execute fill:#e1f5ff
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
### 7. Message Bus
|
|
357
|
-
|
|
358
|
-
**Location:** `packages/core/src/hooks/messageBus.ts`
|
|
359
|
-
|
|
360
|
-
**Responsibilities:**
|
|
361
|
-
|
|
362
|
-
- Event-driven architecture
|
|
363
|
-
- Decouple components
|
|
364
|
-
- Enable parallel execution
|
|
365
|
-
- Support complex workflows
|
|
366
|
-
|
|
367
|
-
**Event Flow:**
|
|
368
|
-
|
|
369
|
-
```mermaid
|
|
370
|
-
sequenceDiagram
|
|
371
|
-
participant Source as Event Source
|
|
372
|
-
participant Bus as Message Bus
|
|
373
|
-
participant Handler as Event Handler
|
|
374
|
-
participant Registry as Hook Registry
|
|
375
|
-
participant Runner as Hook Runner
|
|
376
|
-
|
|
377
|
-
Source->>Bus: emit(event, data)
|
|
378
|
-
Bus->>Handler: notify(event, data)
|
|
379
|
-
Handler->>Registry: getHooksForEvent(event)
|
|
380
|
-
Registry-->>Handler: hooks[]
|
|
381
|
-
|
|
382
|
-
loop For each hook
|
|
383
|
-
Handler->>Runner: execute(hook, data)
|
|
384
|
-
Runner-->>Handler: result
|
|
385
|
-
end
|
|
386
|
-
|
|
387
|
-
Handler-->>Bus: complete
|
|
388
|
-
Bus-->>Source: done
|
|
389
|
-
```
|
|
390
|
-
|
|
391
|
-
---
|
|
392
|
-
|
|
393
|
-
## Data Flow
|
|
394
|
-
|
|
395
|
-
### Hook Execution Flow
|
|
396
|
-
|
|
397
|
-
```mermaid
|
|
398
|
-
flowchart TD
|
|
399
|
-
Event[Event Occurs] --> Emit[MessageBus Emits]
|
|
400
|
-
Emit --> Handler[Event Handler]
|
|
401
|
-
Handler --> Registry[Get Hooks from Registry]
|
|
402
|
-
Registry --> Filter[Filter by Event Type]
|
|
403
|
-
Filter --> Planner[Hook Planner]
|
|
404
|
-
|
|
405
|
-
Planner --> Strategy[Determine Strategy]
|
|
406
|
-
Strategy --> RateLimit[Check Rate Limits]
|
|
407
|
-
RateLimit --> Trust[Check Trust]
|
|
408
|
-
|
|
409
|
-
Trust --> Approval{Needs Approval?}
|
|
410
|
-
Approval -->|Yes| RequestApproval[Request User Approval]
|
|
411
|
-
Approval -->|No| Execute[Execute Hook]
|
|
412
|
-
|
|
413
|
-
RequestApproval --> UserApproves{User Approves?}
|
|
414
|
-
UserApproves -->|Yes| Execute
|
|
415
|
-
UserApproves -->|No| Skip[Skip Hook]
|
|
416
|
-
|
|
417
|
-
Execute --> Runner[Hook Runner]
|
|
418
|
-
Runner --> Spawn[Spawn Process]
|
|
419
|
-
Spawn --> WriteStdin[Write Event Data]
|
|
420
|
-
WriteStdin --> ReadStdout[Read Response]
|
|
421
|
-
ReadStdout --> Parse[Parse JSON]
|
|
422
|
-
Parse --> Process[Process Result]
|
|
423
|
-
|
|
424
|
-
Process --> Continue{Continue Flag?}
|
|
425
|
-
Continue -->|Yes| NextHook[Next Hook]
|
|
426
|
-
Continue -->|No| Stop[Stop Execution]
|
|
427
|
-
|
|
428
|
-
NextHook --> Registry
|
|
429
|
-
Skip --> NextHook
|
|
430
|
-
Stop --> Done[Complete]
|
|
431
|
-
|
|
432
|
-
style Event fill:#e1f5ff
|
|
433
|
-
style Trust fill:#fff4e1
|
|
434
|
-
style Execute fill:#e8f5e9
|
|
435
|
-
style Done fill:#f3e5f5
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
### Hook Input/Output Flow
|
|
439
|
-
|
|
440
|
-
```mermaid
|
|
441
|
-
flowchart LR
|
|
442
|
-
EventData[Event Data] --> Format[Format Input]
|
|
443
|
-
Format --> JSON[JSON Object]
|
|
444
|
-
JSON --> Stdin[Write to stdin]
|
|
445
|
-
Stdin --> Hook[Hook Process]
|
|
446
|
-
Hook --> Stdout[Read from stdout]
|
|
447
|
-
Stdout --> ParseJSON[Parse JSON]
|
|
448
|
-
ParseJSON --> Validate[Validate Response]
|
|
449
|
-
Validate --> Result[Process Result]
|
|
450
|
-
|
|
451
|
-
style EventData fill:#e1f5ff
|
|
452
|
-
style Hook fill:#e8f5e9
|
|
453
|
-
style Result fill:#f3e5f5
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
---
|
|
457
|
-
|
|
458
|
-
## Integration Points
|
|
459
|
-
|
|
460
|
-
### 1. Tool System Integration
|
|
461
|
-
|
|
462
|
-
Hooks can trigger tool execution through `askAgent` action:
|
|
463
|
-
|
|
464
|
-
```typescript
|
|
465
|
-
// Hook triggers agent to use tool
|
|
466
|
-
{
|
|
467
|
-
action: {
|
|
468
|
-
type: 'askAgent',
|
|
469
|
-
prompt: 'Run linting on {{file.path}} using the shell tool'
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
```
|
|
473
|
-
|
|
474
|
-
**Flow:**
|
|
475
|
-
|
|
476
|
-
```mermaid
|
|
477
|
-
sequenceDiagram
|
|
478
|
-
participant Hook as Hook
|
|
479
|
-
participant Agent as Agent
|
|
480
|
-
participant Tool as Tool Registry
|
|
481
|
-
participant Shell as Shell Tool
|
|
482
|
-
|
|
483
|
-
Hook->>Agent: askAgent("Run linting...")
|
|
484
|
-
Agent->>Tool: selectTool("shell")
|
|
485
|
-
Tool-->>Agent: shell tool
|
|
486
|
-
Agent->>Shell: execute("eslint --fix file.ts")
|
|
487
|
-
Shell-->>Agent: result
|
|
488
|
-
Agent-->>Hook: complete
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
### 2. MCP Integration
|
|
492
|
-
|
|
493
|
-
**Important:** MCP OAuth must be completed BEFORE hook execution.
|
|
494
|
-
|
|
495
|
-
**Pre-Authentication Flow:**
|
|
496
|
-
|
|
497
|
-
```mermaid
|
|
498
|
-
flowchart TD
|
|
499
|
-
User[User] --> Panel[MCP Panel UI]
|
|
500
|
-
Panel --> OAuth[OAuth Flow]
|
|
501
|
-
OAuth --> Store[Store Tokens]
|
|
502
|
-
Store --> Ready[Ready for Hooks]
|
|
503
|
-
|
|
504
|
-
Ready --> HookTrigger[Hook Triggers]
|
|
505
|
-
HookTrigger --> UseToken[Use Existing Token]
|
|
506
|
-
UseToken --> MCPCall[MCP Tool Call]
|
|
507
|
-
|
|
508
|
-
style OAuth fill:#fff4e1
|
|
509
|
-
style Store fill:#e8f5e9
|
|
510
|
-
style MCPCall fill:#e1f5ff
|
|
511
|
-
```
|
|
512
|
-
|
|
513
|
-
**Why:** Hooks run automatically and cannot prompt for user authentication.
|
|
514
|
-
|
|
515
|
-
**See:** `dev_MCPIntegration.md` for MCP OAuth details
|
|
516
|
-
|
|
517
|
-
### 3. Extension System Integration
|
|
518
|
-
|
|
519
|
-
Extensions can register hooks via manifest:
|
|
520
|
-
|
|
521
|
-
```json
|
|
522
|
-
{
|
|
523
|
-
"hooks": {
|
|
524
|
-
"before_model": [
|
|
525
|
-
{
|
|
526
|
-
"name": "validate-input",
|
|
527
|
-
"command": "node",
|
|
528
|
-
"args": ["hooks/validate.js"]
|
|
529
|
-
}
|
|
530
|
-
]
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
```
|
|
534
|
-
|
|
535
|
-
**Registration Flow:**
|
|
536
|
-
|
|
537
|
-
```mermaid
|
|
538
|
-
flowchart LR
|
|
539
|
-
Extension[Extension] --> Manager[Extension Manager]
|
|
540
|
-
Manager --> Parse[Parse Manifest]
|
|
541
|
-
Parse --> Register[Register Hooks]
|
|
542
|
-
Register --> Registry[Hook Registry]
|
|
543
|
-
Registry --> Available[Available to System]
|
|
544
|
-
|
|
545
|
-
style Extension fill:#e1f5ff
|
|
546
|
-
style Register fill:#e8f5e9
|
|
547
|
-
style Available fill:#f3e5f5
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
---
|
|
551
|
-
|
|
552
|
-
## Design Decisions
|
|
553
|
-
|
|
554
|
-
### 1. JSON stdin/stdout Protocol
|
|
555
|
-
|
|
556
|
-
**Decision:** Use JSON over stdin/stdout for hook communication
|
|
557
|
-
|
|
558
|
-
**Rationale:**
|
|
559
|
-
|
|
560
|
-
- Language-agnostic (works with any language)
|
|
561
|
-
- Simple to implement
|
|
562
|
-
- Standard input/output (no special libraries needed)
|
|
563
|
-
- Easy to debug and test
|
|
564
|
-
|
|
565
|
-
**Trade-offs:**
|
|
566
|
-
|
|
567
|
-
- ✅ Universal compatibility
|
|
568
|
-
- ✅ Simple implementation
|
|
569
|
-
- ❌ No streaming support
|
|
570
|
-
- ❌ Limited to text data
|
|
571
|
-
|
|
572
|
-
### 2. Command Whitelist
|
|
573
|
-
|
|
574
|
-
**Decision:** Only allow whitelisted commands in `runCommand` hooks
|
|
575
|
-
|
|
576
|
-
**Rationale:**
|
|
577
|
-
|
|
578
|
-
- Security first approach
|
|
579
|
-
- Prevent arbitrary command execution
|
|
580
|
-
- Limit attack surface
|
|
581
|
-
- User control over allowed commands
|
|
582
|
-
|
|
583
|
-
**Whitelisted Commands:**
|
|
584
|
-
|
|
585
|
-
- `node` - Node.js runtime
|
|
586
|
-
- `python`, `python3` - Python runtime
|
|
587
|
-
- `bash`, `sh` - Shell scripts
|
|
588
|
-
- `npx` - npm package runner
|
|
589
|
-
- `uvx` - uv package runner
|
|
590
|
-
|
|
591
|
-
**Trade-offs:**
|
|
592
|
-
|
|
593
|
-
- ✅ Enhanced security
|
|
594
|
-
- ✅ Predictable behavior
|
|
595
|
-
- ❌ Limited flexibility
|
|
596
|
-
- ❌ May need updates for new tools
|
|
597
|
-
|
|
598
|
-
### 3. Trust Model with Approval
|
|
599
|
-
|
|
600
|
-
**Decision:** Require approval for untrusted hooks
|
|
601
|
-
|
|
602
|
-
**Rationale:**
|
|
603
|
-
|
|
604
|
-
- Security and safety
|
|
605
|
-
- User control
|
|
606
|
-
- Prevent malicious code execution
|
|
607
|
-
- Hash verification for changes
|
|
608
|
-
|
|
609
|
-
**Trust Levels:**
|
|
610
|
-
|
|
611
|
-
1. **Builtin** - Always trusted
|
|
612
|
-
2. **User** - Trusted by default
|
|
613
|
-
3. **Workspace** - Requires approval
|
|
614
|
-
4. **Downloaded** - Requires approval
|
|
615
|
-
|
|
616
|
-
**Trade-offs:**
|
|
617
|
-
|
|
618
|
-
- ✅ Enhanced security
|
|
619
|
-
- ✅ User awareness
|
|
620
|
-
- ❌ Additional friction
|
|
621
|
-
- ❌ Approval fatigue risk
|
|
622
|
-
|
|
623
|
-
### 4. Event-Driven Architecture
|
|
624
|
-
|
|
625
|
-
**Decision:** Use MessageBus for event-driven architecture
|
|
626
|
-
|
|
627
|
-
**Rationale:**
|
|
628
|
-
|
|
629
|
-
- Decouples components
|
|
630
|
-
- Enables parallel execution
|
|
631
|
-
- Supports complex workflows
|
|
632
|
-
- Easy to extend
|
|
633
|
-
|
|
634
|
-
**Benefits:**
|
|
635
|
-
|
|
636
|
-
- ✅ Loose coupling
|
|
637
|
-
- ✅ Testability
|
|
638
|
-
- ✅ Extensibility
|
|
639
|
-
- ✅ Performance (parallel execution)
|
|
640
|
-
|
|
641
|
-
### 5. Rate Limiting
|
|
642
|
-
|
|
643
|
-
**Decision:** Implement per-hook and global rate limits
|
|
644
|
-
|
|
645
|
-
**Rationale:**
|
|
646
|
-
|
|
647
|
-
- Prevent hook spam
|
|
648
|
-
- Protect system resources
|
|
649
|
-
- Avoid infinite loops
|
|
650
|
-
- Better user experience
|
|
651
|
-
|
|
652
|
-
**Configuration:**
|
|
653
|
-
|
|
654
|
-
- Per-hook max executions per minute
|
|
655
|
-
- Global max executions per minute
|
|
656
|
-
- Cooldown period between runs
|
|
657
|
-
- Execution count tracking
|
|
658
|
-
|
|
659
|
-
---
|
|
660
|
-
|
|
661
|
-
## Security Model
|
|
662
|
-
|
|
663
|
-
### 1. Command Injection Prevention
|
|
664
|
-
|
|
665
|
-
**Threat:** Malicious hooks executing arbitrary commands
|
|
666
|
-
|
|
667
|
-
**Mitigation:**
|
|
668
|
-
|
|
669
|
-
- Command whitelist enforcement
|
|
670
|
-
- Argument sanitization
|
|
671
|
-
- No shell expansion
|
|
672
|
-
- Safe process spawning
|
|
673
|
-
|
|
674
|
-
**Implementation:**
|
|
675
|
-
|
|
676
|
-
```typescript
|
|
677
|
-
// Check command is whitelisted
|
|
678
|
-
if (!WHITELISTED_COMMANDS.includes(command)) {
|
|
679
|
-
throw new Error(`Command not allowed: ${command}`);
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
// Spawn process safely (no shell)
|
|
683
|
-
const process = spawn(command, args, {
|
|
684
|
-
shell: false, // Prevent shell injection
|
|
685
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
686
|
-
});
|
|
687
|
-
```
|
|
688
|
-
|
|
689
|
-
### 2. Input Validation
|
|
690
|
-
|
|
691
|
-
**Threat:** Malformed input causing crashes or exploits
|
|
692
|
-
|
|
693
|
-
**Mitigation:**
|
|
694
|
-
|
|
695
|
-
- JSON schema validation
|
|
696
|
-
- Type checking
|
|
697
|
-
- Required field verification
|
|
698
|
-
- Value range validation
|
|
699
|
-
|
|
700
|
-
**Implementation:**
|
|
701
|
-
|
|
702
|
-
```typescript
|
|
703
|
-
function validateHookInput(input: any): void {
|
|
704
|
-
if (!input.event) {
|
|
705
|
-
throw new Error('Missing required field: event');
|
|
706
|
-
}
|
|
707
|
-
if (!input.context) {
|
|
708
|
-
throw new Error('Missing required field: context');
|
|
709
|
-
}
|
|
710
|
-
// More validation...
|
|
711
|
-
}
|
|
712
|
-
```
|
|
713
|
-
|
|
714
|
-
### 3. Output Sanitization
|
|
715
|
-
|
|
716
|
-
**Threat:** Malicious output affecting system
|
|
717
|
-
|
|
718
|
-
**Mitigation:**
|
|
719
|
-
|
|
720
|
-
- JSON parsing with error handling
|
|
721
|
-
- Output size limits
|
|
722
|
-
- Special character escaping
|
|
723
|
-
- Sensitive data redaction
|
|
724
|
-
|
|
725
|
-
**Implementation:**
|
|
726
|
-
|
|
727
|
-
```typescript
|
|
728
|
-
function sanitizeHookOutput(output: string): any {
|
|
729
|
-
// Limit output size
|
|
730
|
-
if (output.length > MAX_OUTPUT_SIZE) {
|
|
731
|
-
output = output.substring(0, MAX_OUTPUT_SIZE);
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
// Parse JSON safely
|
|
735
|
-
try {
|
|
736
|
-
return JSON.parse(output);
|
|
737
|
-
} catch (error) {
|
|
738
|
-
throw new Error('Invalid JSON output');
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
```
|
|
742
|
-
|
|
743
|
-
### 4. Resource Limits
|
|
744
|
-
|
|
745
|
-
**Threat:** Hooks consuming excessive resources
|
|
746
|
-
|
|
747
|
-
**Mitigation:**
|
|
748
|
-
|
|
749
|
-
- Execution timeout (default: 5 seconds)
|
|
750
|
-
- Memory limits
|
|
751
|
-
- Process isolation
|
|
752
|
-
- Rate limiting
|
|
753
|
-
|
|
754
|
-
**Implementation:**
|
|
755
|
-
|
|
756
|
-
```typescript
|
|
757
|
-
// Set timeout
|
|
758
|
-
const timeout = setTimeout(() => {
|
|
759
|
-
process.kill();
|
|
760
|
-
reject(new Error('Hook execution timeout'));
|
|
761
|
-
}, HOOK_TIMEOUT);
|
|
762
|
-
|
|
763
|
-
// Clear timeout on completion
|
|
764
|
-
process.on('exit', () => {
|
|
765
|
-
clearTimeout(timeout);
|
|
766
|
-
});
|
|
767
|
-
```
|
|
768
|
-
|
|
769
|
-
### 5. Privilege Separation
|
|
770
|
-
|
|
771
|
-
**Threat:** Hooks accessing sensitive resources
|
|
772
|
-
|
|
773
|
-
**Mitigation:**
|
|
774
|
-
|
|
775
|
-
- Run with minimal privileges
|
|
776
|
-
- File system access restrictions
|
|
777
|
-
- Network access restrictions
|
|
778
|
-
- Environment variable filtering
|
|
779
|
-
|
|
780
|
-
---
|
|
781
|
-
|
|
782
|
-
## Performance Considerations
|
|
783
|
-
|
|
784
|
-
### 1. Parallel Execution
|
|
785
|
-
|
|
786
|
-
**Optimization:** Execute independent hooks in parallel
|
|
787
|
-
|
|
788
|
-
```mermaid
|
|
789
|
-
graph LR
|
|
790
|
-
Start[Start] --> Analyze[Analyze Dependencies]
|
|
791
|
-
Analyze --> Independent{Independent?}
|
|
792
|
-
|
|
793
|
-
Independent -->|Yes| Parallel[Execute in Parallel]
|
|
794
|
-
Independent -->|No| Sequential[Execute Sequentially]
|
|
795
|
-
|
|
796
|
-
Parallel --> Hook1[Hook 1]
|
|
797
|
-
Parallel --> Hook2[Hook 2]
|
|
798
|
-
Parallel --> Hook3[Hook 3]
|
|
799
|
-
|
|
800
|
-
Hook1 --> Wait[Wait for All]
|
|
801
|
-
Hook2 --> Wait
|
|
802
|
-
Hook3 --> Wait
|
|
803
|
-
|
|
804
|
-
Sequential --> Hook1Seq[Hook 1]
|
|
805
|
-
Hook1Seq --> Hook2Seq[Hook 2]
|
|
806
|
-
Hook2Seq --> Hook3Seq[Hook 3]
|
|
807
|
-
|
|
808
|
-
Wait --> Done[Complete]
|
|
809
|
-
Hook3Seq --> Done
|
|
810
|
-
|
|
811
|
-
style Parallel fill:#e8f5e9
|
|
812
|
-
style Sequential fill:#fff4e1
|
|
813
|
-
```
|
|
814
|
-
|
|
815
|
-
### 2. Process Pooling
|
|
816
|
-
|
|
817
|
-
**Optimization:** Reuse hook processes for multiple executions
|
|
818
|
-
|
|
819
|
-
**Implementation:**
|
|
820
|
-
|
|
821
|
-
- Keep processes alive
|
|
822
|
-
- Reuse for multiple events
|
|
823
|
-
- Close on idle timeout
|
|
824
|
-
- Restart on failure
|
|
825
|
-
|
|
826
|
-
### 3. Lazy Loading
|
|
827
|
-
|
|
828
|
-
**Optimization:** Load hooks only when needed
|
|
829
|
-
|
|
830
|
-
**Implementation:**
|
|
831
|
-
|
|
832
|
-
- Load hook definitions on startup
|
|
833
|
-
- Spawn processes only on execution
|
|
834
|
-
- Cache hook metadata
|
|
835
|
-
- Unload unused hooks
|
|
836
|
-
|
|
837
|
-
---
|
|
838
|
-
|
|
839
|
-
## File Locations
|
|
840
|
-
|
|
841
|
-
| File | Purpose |
|
|
842
|
-
| ---------------------------------------------- | ------------------------------------- |
|
|
843
|
-
| `packages/core/src/hooks/hookRegistry.ts` | Hook storage and retrieval |
|
|
844
|
-
| `packages/core/src/hooks/hookEventHandler.ts` | Event emission |
|
|
845
|
-
| `packages/core/src/hooks/hookPlanner.ts` | Execution planning |
|
|
846
|
-
| `packages/core/src/hooks/hookRunner.ts` | Hook execution (line 128: whitelist) |
|
|
847
|
-
| `packages/core/src/hooks/types.ts` | Type definitions (line 30-42: events) |
|
|
848
|
-
| `packages/core/src/hooks/hookDebugger.ts` | Debugging and logging |
|
|
849
|
-
| `packages/core/src/hooks/hookTranslator.ts` | Legacy format translation |
|
|
850
|
-
| `packages/core/src/hooks/trustedHooks.ts` | Trust management |
|
|
851
|
-
| `packages/core/src/hooks/config.ts` | Hook configuration |
|
|
852
|
-
| `packages/core/src/hooks/messageBus.ts` | Event bus |
|
|
853
|
-
| `packages/cli/src/commands/hookCommands.ts` | Hook management commands |
|
|
854
|
-
| `packages/core/src/services/hookService.ts` | Hook service integration |
|
|
855
|
-
| `packages/cli/src/services/hookLoader.ts` | Hook loading |
|
|
856
|
-
| `packages/cli/src/services/hookFileService.ts` | Hook file operations |
|
|
857
|
-
|
|
858
|
-
---
|
|
859
|
-
|
|
860
|
-
## Further Reading
|
|
861
|
-
|
|
862
|
-
### Documentation
|
|
863
|
-
|
|
864
|
-
- [Hook User Guide](UserGuide.md) - Using hooks
|
|
865
|
-
- [Hook Protocol](Protocol.md) - Technical protocol specification
|
|
866
|
-
- [Keyboard Shortcuts](KeyboardShortcuts.md) - Hooks Panel UI shortcuts
|
|
867
|
-
- [Visual Guide](VisualGuide.md) - Hooks Panel UI visual guide
|
|
868
|
-
|
|
869
|
-
### Related Systems
|
|
870
|
-
|
|
871
|
-
- [MCP Architecture](../MCP/MCP_Architecture.md) - MCP system architecture
|
|
872
|
-
- [Tool Execution](../Tools/Architecture.md) - Tool system architecture
|
|
873
|
-
- [Extension System](../Tools/GettingStarted.md) - Extension system
|
|
874
|
-
|
|
875
|
-
### External References
|
|
876
|
-
|
|
877
|
-
- JSON Specification (https://www.json.org/)
|
|
878
|
-
- Process Management (https://nodejs.org/api/child_process.html)
|
|
879
|
-
- Event-Driven Architecture (https://en.wikipedia.org/wiki/Event-driven_architecture)
|
|
880
|
-
|
|
881
|
-
---
|
|
882
|
-
|
|
883
|
-
**Document Version:** 1.0
|
|
884
|
-
**Last Updated:** 2026-01-26
|
|
885
|
-
**Status:** ✅ Complete
|