@sparkleideas/shared 3.0.0-alpha.7
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 +323 -0
- package/__tests__/hooks/bash-safety.test.ts +289 -0
- package/__tests__/hooks/file-organization.test.ts +335 -0
- package/__tests__/hooks/git-commit.test.ts +336 -0
- package/__tests__/hooks/index.ts +23 -0
- package/__tests__/hooks/session-hooks.test.ts +357 -0
- package/__tests__/hooks/task-hooks.test.ts +193 -0
- package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
- package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
- package/docs/EVENTS_README.md +352 -0
- package/package.json +39 -0
- package/src/core/config/defaults.ts +207 -0
- package/src/core/config/index.ts +15 -0
- package/src/core/config/loader.ts +271 -0
- package/src/core/config/schema.ts +188 -0
- package/src/core/config/validator.ts +209 -0
- package/src/core/event-bus.ts +236 -0
- package/src/core/index.ts +22 -0
- package/src/core/interfaces/agent.interface.ts +251 -0
- package/src/core/interfaces/coordinator.interface.ts +363 -0
- package/src/core/interfaces/event.interface.ts +267 -0
- package/src/core/interfaces/index.ts +19 -0
- package/src/core/interfaces/memory.interface.ts +332 -0
- package/src/core/interfaces/task.interface.ts +223 -0
- package/src/core/orchestrator/event-coordinator.ts +122 -0
- package/src/core/orchestrator/health-monitor.ts +214 -0
- package/src/core/orchestrator/index.ts +89 -0
- package/src/core/orchestrator/lifecycle-manager.ts +263 -0
- package/src/core/orchestrator/session-manager.ts +279 -0
- package/src/core/orchestrator/task-manager.ts +317 -0
- package/src/events/domain-events.ts +584 -0
- package/src/events/event-store.test.ts +387 -0
- package/src/events/event-store.ts +588 -0
- package/src/events/example-usage.ts +293 -0
- package/src/events/index.ts +90 -0
- package/src/events/projections.ts +561 -0
- package/src/events/state-reconstructor.ts +349 -0
- package/src/events.ts +367 -0
- package/src/hooks/INTEGRATION.md +658 -0
- package/src/hooks/README.md +532 -0
- package/src/hooks/example-usage.ts +499 -0
- package/src/hooks/executor.ts +379 -0
- package/src/hooks/hooks.test.ts +421 -0
- package/src/hooks/index.ts +131 -0
- package/src/hooks/registry.ts +333 -0
- package/src/hooks/safety/bash-safety.ts +604 -0
- package/src/hooks/safety/file-organization.ts +473 -0
- package/src/hooks/safety/git-commit.ts +623 -0
- package/src/hooks/safety/index.ts +46 -0
- package/src/hooks/session-hooks.ts +559 -0
- package/src/hooks/task-hooks.ts +513 -0
- package/src/hooks/types.ts +357 -0
- package/src/hooks/verify-exports.test.ts +125 -0
- package/src/index.ts +195 -0
- package/src/mcp/connection-pool.ts +438 -0
- package/src/mcp/index.ts +183 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/session-manager.ts +428 -0
- package/src/mcp/tool-registry.ts +566 -0
- package/src/mcp/transport/http.ts +557 -0
- package/src/mcp/transport/index.ts +294 -0
- package/src/mcp/transport/stdio.ts +324 -0
- package/src/mcp/transport/websocket.ts +484 -0
- package/src/mcp/types.ts +565 -0
- package/src/plugin-interface.ts +663 -0
- package/src/plugin-loader.ts +638 -0
- package/src/plugin-registry.ts +604 -0
- package/src/plugins/index.ts +34 -0
- package/src/plugins/official/hive-mind-plugin.ts +330 -0
- package/src/plugins/official/index.ts +24 -0
- package/src/plugins/official/maestro-plugin.ts +508 -0
- package/src/plugins/types.ts +108 -0
- package/src/resilience/bulkhead.ts +277 -0
- package/src/resilience/circuit-breaker.ts +326 -0
- package/src/resilience/index.ts +26 -0
- package/src/resilience/rate-limiter.ts +420 -0
- package/src/resilience/retry.ts +224 -0
- package/src/security/index.ts +39 -0
- package/src/security/input-validation.ts +265 -0
- package/src/security/secure-random.ts +159 -0
- package/src/services/index.ts +16 -0
- package/src/services/v3-progress.service.ts +505 -0
- package/src/types/agent.types.ts +144 -0
- package/src/types/index.ts +22 -0
- package/src/types/mcp.types.ts +300 -0
- package/src/types/memory.types.ts +263 -0
- package/src/types/swarm.types.ts +255 -0
- package/src/types/task.types.ts +205 -0
- package/src/types.ts +367 -0
- package/src/utils/secure-logger.d.ts +69 -0
- package/src/utils/secure-logger.d.ts.map +1 -0
- package/src/utils/secure-logger.js +208 -0
- package/src/utils/secure-logger.js.map +1 -0
- package/src/utils/secure-logger.ts +257 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Hooks System - Tests
|
|
3
|
+
*
|
|
4
|
+
* Comprehensive tests for hook registry and executor.
|
|
5
|
+
*
|
|
6
|
+
* @module v3/shared/hooks/hooks.test
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
10
|
+
import { HookRegistry, createHookRegistry } from './registry.js';
|
|
11
|
+
import { HookExecutor, createHookExecutor } from './executor.js';
|
|
12
|
+
import { HookEvent, HookPriority, HookContext, HookResult } from './types.js';
|
|
13
|
+
import { createEventBus } from '../core/event-bus.js';
|
|
14
|
+
|
|
15
|
+
describe('HookRegistry', () => {
|
|
16
|
+
let registry: HookRegistry;
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
registry = createHookRegistry();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('should register a hook', () => {
|
|
23
|
+
const handler = vi.fn();
|
|
24
|
+
const id = registry.register(HookEvent.PreToolUse, handler);
|
|
25
|
+
|
|
26
|
+
expect(id).toBeDefined();
|
|
27
|
+
expect(registry.has(id)).toBe(true);
|
|
28
|
+
expect(registry.count()).toBe(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should unregister a hook', () => {
|
|
32
|
+
const handler = vi.fn();
|
|
33
|
+
const id = registry.register(HookEvent.PreToolUse, handler);
|
|
34
|
+
|
|
35
|
+
const result = registry.unregister(id);
|
|
36
|
+
|
|
37
|
+
expect(result).toBe(true);
|
|
38
|
+
expect(registry.has(id)).toBe(false);
|
|
39
|
+
expect(registry.count()).toBe(0);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should return false when unregistering non-existent hook', () => {
|
|
43
|
+
const result = registry.unregister('non-existent');
|
|
44
|
+
expect(result).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should get handlers sorted by priority', () => {
|
|
48
|
+
const handler1 = vi.fn();
|
|
49
|
+
const handler2 = vi.fn();
|
|
50
|
+
const handler3 = vi.fn();
|
|
51
|
+
|
|
52
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.Normal);
|
|
53
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.High);
|
|
54
|
+
registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
|
|
55
|
+
|
|
56
|
+
const handlers = registry.getHandlers(HookEvent.PreToolUse);
|
|
57
|
+
|
|
58
|
+
expect(handlers).toHaveLength(3);
|
|
59
|
+
expect(handlers[0].handler).toBe(handler2); // High priority first
|
|
60
|
+
expect(handlers[1].handler).toBe(handler1); // Normal priority second
|
|
61
|
+
expect(handlers[2].handler).toBe(handler3); // Low priority last
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should filter disabled hooks', () => {
|
|
65
|
+
const handler = vi.fn();
|
|
66
|
+
const id = registry.register(HookEvent.PreToolUse, handler, HookPriority.Normal, {
|
|
67
|
+
enabled: false,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const handlers = registry.getHandlers(HookEvent.PreToolUse);
|
|
71
|
+
const allHandlers = registry.getHandlers(HookEvent.PreToolUse, true);
|
|
72
|
+
|
|
73
|
+
expect(handlers).toHaveLength(0);
|
|
74
|
+
expect(allHandlers).toHaveLength(1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should enable and disable hooks', () => {
|
|
78
|
+
const handler = vi.fn();
|
|
79
|
+
const id = registry.register(HookEvent.PreToolUse, handler);
|
|
80
|
+
|
|
81
|
+
registry.disable(id);
|
|
82
|
+
expect(registry.getHandlers(HookEvent.PreToolUse)).toHaveLength(0);
|
|
83
|
+
|
|
84
|
+
registry.enable(id);
|
|
85
|
+
expect(registry.getHandlers(HookEvent.PreToolUse)).toHaveLength(1);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should list hooks with filters', () => {
|
|
89
|
+
registry.register(HookEvent.PreToolUse, vi.fn(), HookPriority.High);
|
|
90
|
+
registry.register(HookEvent.PostToolUse, vi.fn(), HookPriority.Normal);
|
|
91
|
+
registry.register(HookEvent.PreEdit, vi.fn(), HookPriority.Low);
|
|
92
|
+
|
|
93
|
+
const allHooks = registry.listHooks();
|
|
94
|
+
expect(allHooks).toHaveLength(3);
|
|
95
|
+
|
|
96
|
+
const preToolHooks = registry.listHooks({ event: HookEvent.PreToolUse });
|
|
97
|
+
expect(preToolHooks).toHaveLength(1);
|
|
98
|
+
|
|
99
|
+
const highPriorityHooks = registry.listHooks({ minPriority: HookPriority.Normal });
|
|
100
|
+
expect(highPriorityHooks).toHaveLength(2);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should get event types', () => {
|
|
104
|
+
registry.register(HookEvent.PreToolUse, vi.fn());
|
|
105
|
+
registry.register(HookEvent.PostToolUse, vi.fn());
|
|
106
|
+
registry.register(HookEvent.PreEdit, vi.fn());
|
|
107
|
+
|
|
108
|
+
const eventTypes = registry.getEventTypes();
|
|
109
|
+
expect(eventTypes).toContain(HookEvent.PreToolUse);
|
|
110
|
+
expect(eventTypes).toContain(HookEvent.PostToolUse);
|
|
111
|
+
expect(eventTypes).toContain(HookEvent.PreEdit);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should track statistics', () => {
|
|
115
|
+
registry.register(HookEvent.PreToolUse, vi.fn());
|
|
116
|
+
registry.register(HookEvent.PostToolUse, vi.fn());
|
|
117
|
+
|
|
118
|
+
registry.recordExecution(true, 10);
|
|
119
|
+
registry.recordExecution(true, 20);
|
|
120
|
+
registry.recordExecution(false, 5);
|
|
121
|
+
|
|
122
|
+
const stats = registry.getStats();
|
|
123
|
+
expect(stats.totalHooks).toBe(2);
|
|
124
|
+
expect(stats.totalExecutions).toBe(3);
|
|
125
|
+
expect(stats.totalFailures).toBe(1);
|
|
126
|
+
expect(stats.avgExecutionTime).toBe((10 + 20 + 5) / 3);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should reset statistics', () => {
|
|
130
|
+
registry.recordExecution(true, 10);
|
|
131
|
+
registry.resetStats();
|
|
132
|
+
|
|
133
|
+
const stats = registry.getStats();
|
|
134
|
+
expect(stats.totalExecutions).toBe(0);
|
|
135
|
+
expect(stats.totalFailures).toBe(0);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should clear all hooks', () => {
|
|
139
|
+
registry.register(HookEvent.PreToolUse, vi.fn());
|
|
140
|
+
registry.register(HookEvent.PostToolUse, vi.fn());
|
|
141
|
+
|
|
142
|
+
registry.clear();
|
|
143
|
+
|
|
144
|
+
expect(registry.count()).toBe(0);
|
|
145
|
+
expect(registry.getEventTypes()).toHaveLength(0);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
describe('HookExecutor', () => {
|
|
150
|
+
let registry: HookRegistry;
|
|
151
|
+
let executor: HookExecutor;
|
|
152
|
+
let eventBus: any;
|
|
153
|
+
|
|
154
|
+
beforeEach(() => {
|
|
155
|
+
registry = createHookRegistry();
|
|
156
|
+
eventBus = createEventBus();
|
|
157
|
+
executor = createHookExecutor(registry, eventBus);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should execute single hook successfully', async () => {
|
|
161
|
+
const handler = vi.fn(async () => ({ success: true }));
|
|
162
|
+
registry.register(HookEvent.PreToolUse, handler);
|
|
163
|
+
|
|
164
|
+
const context: HookContext = {
|
|
165
|
+
event: HookEvent.PreToolUse,
|
|
166
|
+
timestamp: new Date(),
|
|
167
|
+
tool: { name: 'Read', parameters: { path: 'file.ts' } },
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const result = await executor.execute(HookEvent.PreToolUse, context);
|
|
171
|
+
|
|
172
|
+
expect(result.success).toBe(true);
|
|
173
|
+
expect(result.hooksExecuted).toBe(1);
|
|
174
|
+
expect(result.hooksFailed).toBe(0);
|
|
175
|
+
expect(handler).toHaveBeenCalledWith(context);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should execute multiple hooks in priority order', async () => {
|
|
179
|
+
const executionOrder: number[] = [];
|
|
180
|
+
|
|
181
|
+
const handler1 = vi.fn(async () => {
|
|
182
|
+
executionOrder.push(1);
|
|
183
|
+
return { success: true };
|
|
184
|
+
});
|
|
185
|
+
const handler2 = vi.fn(async () => {
|
|
186
|
+
executionOrder.push(2);
|
|
187
|
+
return { success: true };
|
|
188
|
+
});
|
|
189
|
+
const handler3 = vi.fn(async () => {
|
|
190
|
+
executionOrder.push(3);
|
|
191
|
+
return { success: true };
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.Normal);
|
|
195
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.High);
|
|
196
|
+
registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
|
|
197
|
+
|
|
198
|
+
const context: HookContext = {
|
|
199
|
+
event: HookEvent.PreToolUse,
|
|
200
|
+
timestamp: new Date(),
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
await executor.execute(HookEvent.PreToolUse, context);
|
|
204
|
+
|
|
205
|
+
expect(executionOrder).toEqual([2, 1, 3]); // High, Normal, Low
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should handle hook errors gracefully', async () => {
|
|
209
|
+
const handler1 = vi.fn(async () => ({ success: true }));
|
|
210
|
+
const handler2 = vi.fn(async () => {
|
|
211
|
+
throw new Error('Hook failed');
|
|
212
|
+
});
|
|
213
|
+
const handler3 = vi.fn(async () => ({ success: true }));
|
|
214
|
+
|
|
215
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
|
|
216
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
|
|
217
|
+
registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
|
|
218
|
+
|
|
219
|
+
const context: HookContext = {
|
|
220
|
+
event: HookEvent.PreToolUse,
|
|
221
|
+
timestamp: new Date(),
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
const result = await executor.execute(HookEvent.PreToolUse, context, {
|
|
225
|
+
continueOnError: true,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
expect(result.hooksExecuted).toBe(3);
|
|
229
|
+
expect(result.hooksFailed).toBe(1);
|
|
230
|
+
expect(handler3).toHaveBeenCalled(); // Should continue despite error
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it('should abort on error when continueOnError is false', async () => {
|
|
234
|
+
const handler1 = vi.fn(async () => ({ success: true }));
|
|
235
|
+
const handler2 = vi.fn(async () => {
|
|
236
|
+
throw new Error('Hook failed');
|
|
237
|
+
});
|
|
238
|
+
const handler3 = vi.fn(async () => ({ success: true }));
|
|
239
|
+
|
|
240
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
|
|
241
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
|
|
242
|
+
registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
|
|
243
|
+
|
|
244
|
+
const context: HookContext = {
|
|
245
|
+
event: HookEvent.PreToolUse,
|
|
246
|
+
timestamp: new Date(),
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const result = await executor.execute(HookEvent.PreToolUse, context);
|
|
250
|
+
|
|
251
|
+
expect(result.aborted).toBe(true);
|
|
252
|
+
expect(result.hooksExecuted).toBe(2);
|
|
253
|
+
expect(handler3).not.toHaveBeenCalled(); // Should not execute after error
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should abort when hook returns abort flag', async () => {
|
|
257
|
+
const handler1 = vi.fn(async () => ({ success: true }));
|
|
258
|
+
const handler2 = vi.fn(async () => ({ success: true, abort: true }));
|
|
259
|
+
const handler3 = vi.fn(async () => ({ success: true }));
|
|
260
|
+
|
|
261
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
|
|
262
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
|
|
263
|
+
registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
|
|
264
|
+
|
|
265
|
+
const context: HookContext = {
|
|
266
|
+
event: HookEvent.PreToolUse,
|
|
267
|
+
timestamp: new Date(),
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const result = await executor.execute(HookEvent.PreToolUse, context);
|
|
271
|
+
|
|
272
|
+
expect(result.aborted).toBe(true);
|
|
273
|
+
expect(result.hooksExecuted).toBe(2);
|
|
274
|
+
expect(handler3).not.toHaveBeenCalled();
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should merge context modifications', async () => {
|
|
278
|
+
const handler1 = vi.fn(async () => ({
|
|
279
|
+
success: true,
|
|
280
|
+
data: { metadata: { modified: true } },
|
|
281
|
+
}));
|
|
282
|
+
const handler2 = vi.fn(async (context: HookContext) => {
|
|
283
|
+
expect(context.metadata?.modified).toBe(true);
|
|
284
|
+
return { success: true };
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
|
|
288
|
+
registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
|
|
289
|
+
|
|
290
|
+
const context: HookContext = {
|
|
291
|
+
event: HookEvent.PreToolUse,
|
|
292
|
+
timestamp: new Date(),
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
const result = await executor.execute(HookEvent.PreToolUse, context);
|
|
296
|
+
|
|
297
|
+
expect(result.finalContext?.metadata).toEqual({ modified: true });
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
it('should handle timeout', async () => {
|
|
301
|
+
const handler = vi.fn(async () => {
|
|
302
|
+
await new Promise(resolve => setTimeout(resolve, 200));
|
|
303
|
+
return { success: true };
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
registry.register(HookEvent.PreToolUse, handler);
|
|
307
|
+
|
|
308
|
+
const context: HookContext = {
|
|
309
|
+
event: HookEvent.PreToolUse,
|
|
310
|
+
timestamp: new Date(),
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const result = await executor.executeWithTimeout(HookEvent.PreToolUse, context, 100);
|
|
314
|
+
|
|
315
|
+
expect(result.success).toBe(false);
|
|
316
|
+
expect(result.hooksFailed).toBe(1);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('should execute hooks in parallel', async () => {
|
|
320
|
+
const handler1 = vi.fn(async () => ({ success: true }));
|
|
321
|
+
const handler2 = vi.fn(async () => ({ success: true }));
|
|
322
|
+
|
|
323
|
+
registry.register(HookEvent.PreToolUse, handler1);
|
|
324
|
+
registry.register(HookEvent.PostToolUse, handler2);
|
|
325
|
+
|
|
326
|
+
const contexts: HookContext[] = [
|
|
327
|
+
{ event: HookEvent.PreToolUse, timestamp: new Date() },
|
|
328
|
+
{ event: HookEvent.PostToolUse, timestamp: new Date() },
|
|
329
|
+
];
|
|
330
|
+
|
|
331
|
+
const results = await executor.executeParallel(
|
|
332
|
+
[HookEvent.PreToolUse, HookEvent.PostToolUse],
|
|
333
|
+
contexts
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
expect(results).toHaveLength(2);
|
|
337
|
+
expect(results[0].success).toBe(true);
|
|
338
|
+
expect(results[1].success).toBe(true);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should execute hooks sequentially with context chaining', async () => {
|
|
342
|
+
const handler1 = vi.fn(async () => ({
|
|
343
|
+
success: true,
|
|
344
|
+
data: { metadata: { step: 1 } },
|
|
345
|
+
}));
|
|
346
|
+
const handler2 = vi.fn(async () => ({
|
|
347
|
+
success: true,
|
|
348
|
+
data: { metadata: { step: 2 } },
|
|
349
|
+
}));
|
|
350
|
+
|
|
351
|
+
registry.register(HookEvent.PreToolUse, handler1);
|
|
352
|
+
registry.register(HookEvent.PostToolUse, handler2);
|
|
353
|
+
|
|
354
|
+
const initialContext: HookContext = {
|
|
355
|
+
event: HookEvent.PreToolUse,
|
|
356
|
+
timestamp: new Date(),
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const result = await executor.executeSequential(
|
|
360
|
+
[HookEvent.PreToolUse, HookEvent.PostToolUse],
|
|
361
|
+
initialContext
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
expect(result.success).toBe(true);
|
|
365
|
+
expect(result.hooksExecuted).toBe(2);
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it('should emit events to event bus', async () => {
|
|
369
|
+
const preExecuteHandler = vi.fn();
|
|
370
|
+
const postExecuteHandler = vi.fn();
|
|
371
|
+
|
|
372
|
+
eventBus.on('hooks:pre-execute', preExecuteHandler);
|
|
373
|
+
eventBus.on('hooks:post-execute', postExecuteHandler);
|
|
374
|
+
|
|
375
|
+
const handler = vi.fn(async () => ({ success: true }));
|
|
376
|
+
registry.register(HookEvent.PreToolUse, handler);
|
|
377
|
+
|
|
378
|
+
const context: HookContext = {
|
|
379
|
+
event: HookEvent.PreToolUse,
|
|
380
|
+
timestamp: new Date(),
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
await executor.execute(HookEvent.PreToolUse, context);
|
|
384
|
+
|
|
385
|
+
expect(preExecuteHandler).toHaveBeenCalled();
|
|
386
|
+
expect(postExecuteHandler).toHaveBeenCalled();
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it('should skip disabled hooks', async () => {
|
|
390
|
+
const handler = vi.fn(async () => ({ success: true }));
|
|
391
|
+
const id = registry.register(HookEvent.PreToolUse, handler, HookPriority.Normal, {
|
|
392
|
+
enabled: false,
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
const context: HookContext = {
|
|
396
|
+
event: HookEvent.PreToolUse,
|
|
397
|
+
timestamp: new Date(),
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const result = await executor.execute(HookEvent.PreToolUse, context);
|
|
401
|
+
|
|
402
|
+
expect(result.hooksExecuted).toBe(0);
|
|
403
|
+
expect(handler).not.toHaveBeenCalled();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('should record execution statistics', async () => {
|
|
407
|
+
const handler = vi.fn(async () => ({ success: true }));
|
|
408
|
+
registry.register(HookEvent.PreToolUse, handler);
|
|
409
|
+
|
|
410
|
+
const context: HookContext = {
|
|
411
|
+
event: HookEvent.PreToolUse,
|
|
412
|
+
timestamp: new Date(),
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
await executor.execute(HookEvent.PreToolUse, context);
|
|
416
|
+
|
|
417
|
+
const stats = registry.getStats();
|
|
418
|
+
expect(stats.totalExecutions).toBe(1);
|
|
419
|
+
expect(stats.totalFailures).toBe(0);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Hooks System - Main Export
|
|
3
|
+
*
|
|
4
|
+
* Provides extensible hook points for tool execution, file operations,
|
|
5
|
+
* and session lifecycle events. Integrates with event bus for coordination.
|
|
6
|
+
*
|
|
7
|
+
* Example usage:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { createHookRegistry, createHookExecutor, HookEvent, HookPriority } from '@sparkleideas/shared/hooks';
|
|
11
|
+
*
|
|
12
|
+
* const registry = createHookRegistry();
|
|
13
|
+
* const executor = createHookExecutor(registry, eventBus);
|
|
14
|
+
*
|
|
15
|
+
* // Register a hook
|
|
16
|
+
* const hookId = registry.register(
|
|
17
|
+
* HookEvent.PreToolUse,
|
|
18
|
+
* async (context) => {
|
|
19
|
+
* console.log('Before tool use:', context.tool?.name);
|
|
20
|
+
* return { success: true };
|
|
21
|
+
* },
|
|
22
|
+
* HookPriority.High
|
|
23
|
+
* );
|
|
24
|
+
*
|
|
25
|
+
* // Execute hooks
|
|
26
|
+
* const result = await executor.execute(
|
|
27
|
+
* HookEvent.PreToolUse,
|
|
28
|
+
* {
|
|
29
|
+
* event: HookEvent.PreToolUse,
|
|
30
|
+
* timestamp: new Date(),
|
|
31
|
+
* tool: { name: 'Read', parameters: { path: 'file.ts' } }
|
|
32
|
+
* }
|
|
33
|
+
* );
|
|
34
|
+
*
|
|
35
|
+
* // Unregister hook
|
|
36
|
+
* registry.unregister(hookId);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @module v3/shared/hooks
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
// Export types
|
|
43
|
+
export type {
|
|
44
|
+
HookContext,
|
|
45
|
+
HookResult,
|
|
46
|
+
HookHandler,
|
|
47
|
+
HookDefinition,
|
|
48
|
+
HookStats,
|
|
49
|
+
HookExecutionOptions,
|
|
50
|
+
ToolInfo,
|
|
51
|
+
CommandInfo,
|
|
52
|
+
FileOperationInfo,
|
|
53
|
+
SessionInfo,
|
|
54
|
+
AgentInfo,
|
|
55
|
+
TaskInfo,
|
|
56
|
+
MemoryInfo,
|
|
57
|
+
ErrorInfo,
|
|
58
|
+
} from './types.js';
|
|
59
|
+
|
|
60
|
+
export {
|
|
61
|
+
HookEvent,
|
|
62
|
+
HookPriority,
|
|
63
|
+
} from './types.js';
|
|
64
|
+
|
|
65
|
+
// Export registry
|
|
66
|
+
export {
|
|
67
|
+
HookRegistry,
|
|
68
|
+
createHookRegistry,
|
|
69
|
+
} from './registry.js';
|
|
70
|
+
|
|
71
|
+
// Export executor
|
|
72
|
+
export type {
|
|
73
|
+
AggregatedHookResult,
|
|
74
|
+
} from './executor.js';
|
|
75
|
+
|
|
76
|
+
export {
|
|
77
|
+
HookExecutor,
|
|
78
|
+
createHookExecutor,
|
|
79
|
+
} from './executor.js';
|
|
80
|
+
|
|
81
|
+
// Export task hooks
|
|
82
|
+
export {
|
|
83
|
+
TaskHooksManager,
|
|
84
|
+
createTaskHooksManager,
|
|
85
|
+
} from './task-hooks.js';
|
|
86
|
+
|
|
87
|
+
export type {
|
|
88
|
+
PreTaskHookResult,
|
|
89
|
+
PostTaskHookResult,
|
|
90
|
+
AgentSuggestion,
|
|
91
|
+
TaskPattern,
|
|
92
|
+
TaskOutcome,
|
|
93
|
+
LearningUpdate,
|
|
94
|
+
} from './task-hooks.js';
|
|
95
|
+
|
|
96
|
+
// Export session hooks
|
|
97
|
+
export {
|
|
98
|
+
SessionHooksManager,
|
|
99
|
+
createSessionHooksManager,
|
|
100
|
+
InMemorySessionStorage,
|
|
101
|
+
} from './session-hooks.js';
|
|
102
|
+
|
|
103
|
+
export type {
|
|
104
|
+
SessionState,
|
|
105
|
+
SessionEndHookResult,
|
|
106
|
+
SessionRestoreHookResult,
|
|
107
|
+
SessionSummary,
|
|
108
|
+
SessionStorage,
|
|
109
|
+
} from './session-hooks.js';
|
|
110
|
+
|
|
111
|
+
// Export safety hooks
|
|
112
|
+
export {
|
|
113
|
+
BashSafetyHook,
|
|
114
|
+
createBashSafetyHook,
|
|
115
|
+
FileOrganizationHook,
|
|
116
|
+
createFileOrganizationHook,
|
|
117
|
+
GitCommitHook,
|
|
118
|
+
createGitCommitHook,
|
|
119
|
+
} from './safety/index.js';
|
|
120
|
+
|
|
121
|
+
export type {
|
|
122
|
+
BashSafetyResult,
|
|
123
|
+
CommandRisk,
|
|
124
|
+
FileOrganizationResult,
|
|
125
|
+
FormatterRecommendation,
|
|
126
|
+
LinterRecommendation,
|
|
127
|
+
OrganizationIssue,
|
|
128
|
+
GitCommitResult,
|
|
129
|
+
CommitType,
|
|
130
|
+
CommitValidationIssue,
|
|
131
|
+
} from './safety/index.js';
|