@agiflowai/hooks-adapter 0.0.0

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,319 @@
1
+ //#region src/constants/index.d.ts
2
+ /**
3
+ * @agiflowai/hooks-adapter - Constants
4
+ *
5
+ * DESIGN PATTERNS:
6
+ * - Strongly-typed constant exports for compile-time safety
7
+ * - Immutable by default (as const assertions)
8
+ *
9
+ * CODING STANDARDS:
10
+ * - Primitive constants: UPPER_SNAKE_CASE
11
+ * - Always include JSDoc with purpose and usage
12
+ *
13
+ * AVOID:
14
+ * - Mutable exports (let, var)
15
+ * - Magic strings without explanation
16
+ */
17
+ /**
18
+ * Hook type identifiers for different hook events
19
+ */
20
+ declare const PRE_TOOL_USE = "PreToolUse";
21
+ declare const POST_TOOL_USE = "PostToolUse";
22
+ /**
23
+ * Union type of all supported hook types
24
+ */
25
+ type HookType = typeof PRE_TOOL_USE | typeof POST_TOOL_USE;
26
+ //#endregion
27
+ //#region src/types/index.d.ts
28
+ /**
29
+ * @agiflowai/hooks-adapter - Type Definitions
30
+ *
31
+ * DESIGN PATTERNS:
32
+ * - Interface segregation: Keep interfaces focused and minimal
33
+ * - Type composition: Build complex types from simple primitives
34
+ * - Generics: Use type parameters for reusable, type-safe abstractions
35
+ *
36
+ * CODING STANDARDS:
37
+ * - Use PascalCase for type/interface names
38
+ * - Prefix interfaces with 'I' only for abstract contracts
39
+ * - Document complex types with JSDoc comments
40
+ * - Export all public types
41
+ *
42
+ * AVOID:
43
+ * - Any type unless absolutely necessary
44
+ * - Overly complex type gymnastics
45
+ * - Coupling types to implementation details
46
+ */
47
+ /**
48
+ * Normalized context passed to hook callback function
49
+ */
50
+ interface HookContext {
51
+ /** Name of the tool being invoked (e.g., "Read", "Write", "Edit") */
52
+ toolName: string;
53
+ /** Input parameters passed to the tool */
54
+ toolInput: Record<string, any>;
55
+ /** File path if this is a file operation */
56
+ filePath?: string;
57
+ /** Type of file operation */
58
+ operation?: 'read' | 'write' | 'edit';
59
+ /** Current working directory */
60
+ cwd: string;
61
+ /** Unique session identifier */
62
+ sessionId: string;
63
+ /** Optional LLM tool to use for processing (e.g., "claude-code", "gemini") */
64
+ llmTool?: string;
65
+ }
66
+ /**
67
+ * Normalized response from hook callback function
68
+ */
69
+ interface HookResponse {
70
+ /** Permission decision for the tool execution */
71
+ decision: 'allow' | 'deny' | 'ask' | 'skip';
72
+ /** Message shown to the LLM (e.g., design patterns, warnings) */
73
+ message: string;
74
+ /** Optional message shown only to the user (not the LLM) */
75
+ userMessage?: string;
76
+ /** Optional updated input parameters for the tool */
77
+ updatedInput?: Record<string, any>;
78
+ }
79
+ /**
80
+ * Hook callback function signature
81
+ * Takes normalized context and returns normalized response
82
+ */
83
+ type HookCallback = (context: HookContext) => Promise<HookResponse>;
84
+ //#endregion
85
+ //#region src/adapters/BaseAdapter.d.ts
86
+
87
+ /**
88
+ * Abstract base adapter for normalizing AI agent hook formats
89
+ */
90
+ declare abstract class BaseAdapter {
91
+ /**
92
+ * Parse stdin from AI agent into normalized HookContext
93
+ * @param stdin - Raw stdin string from AI agent
94
+ * @returns Normalized hook context
95
+ */
96
+ abstract parseInput(stdin: string): HookContext;
97
+ /**
98
+ * Format normalized HookResponse into AI agent-specific output
99
+ * @param response - Normalized hook response
100
+ * @returns Formatted output string for AI agent
101
+ */
102
+ abstract formatOutput(response: HookResponse): string;
103
+ /**
104
+ * Execute hook callback with normalized context
105
+ * Template method that orchestrates the hook execution flow
106
+ *
107
+ * @param callback - Hook callback function to execute
108
+ */
109
+ execute(callback: HookCallback): Promise<void>;
110
+ /**
111
+ * Read stdin from AI agent
112
+ * @returns Promise resolving to stdin content
113
+ */
114
+ private readStdin;
115
+ /**
116
+ * Handle errors with fail-open behavior
117
+ * Allows operation to proceed with warning message
118
+ *
119
+ * @param error - Error that occurred during hook execution
120
+ */
121
+ private handleError;
122
+ }
123
+ //#endregion
124
+ //#region src/adapters/ClaudeCodeAdapter.d.ts
125
+ /**
126
+ * Adapter for Claude Code hook format
127
+ */
128
+ declare class ClaudeCodeAdapter extends BaseAdapter {
129
+ /**
130
+ * Parse Claude Code stdin into normalized HookContext
131
+ *
132
+ * @param stdin - Raw JSON string from Claude Code
133
+ * @returns Normalized hook context
134
+ */
135
+ parseInput(stdin: string): HookContext;
136
+ /**
137
+ * Format normalized HookResponse into Claude Code output
138
+ *
139
+ * @param response - Normalized hook response
140
+ * @returns JSON string for Claude Code
141
+ */
142
+ formatOutput(response: HookResponse): string;
143
+ /**
144
+ * Extract file path from tool input
145
+ *
146
+ * @param toolName - Name of the tool
147
+ * @param toolInput - Tool input parameters
148
+ * @returns File path if this is a file operation
149
+ */
150
+ private extractFilePath;
151
+ /**
152
+ * Extract operation type from tool name
153
+ *
154
+ * @param toolName - Name of the tool
155
+ * @returns Operation type if this is a file operation
156
+ */
157
+ private extractOperation;
158
+ }
159
+ //#endregion
160
+ //#region src/adapters/ClaudeCodePostToolUseAdapter.d.ts
161
+ /**
162
+ * Adapter for Claude Code PostToolUse hook format
163
+ */
164
+ declare class ClaudeCodePostToolUseAdapter extends BaseAdapter {
165
+ /**
166
+ * Parse Claude Code PostToolUse stdin into normalized HookContext
167
+ *
168
+ * @param stdin - Raw JSON string from Claude Code
169
+ * @returns Normalized hook context
170
+ */
171
+ parseInput(stdin: string): HookContext;
172
+ /**
173
+ * Format normalized HookResponse into Claude Code PostToolUse output
174
+ *
175
+ * @param response - Normalized hook response
176
+ * @returns JSON string for Claude Code
177
+ */
178
+ formatOutput(response: HookResponse): string;
179
+ /**
180
+ * Extract file path from tool input or response
181
+ *
182
+ * @param toolName - Name of the tool
183
+ * @param toolInput - Tool input parameters
184
+ * @param toolResponse - Tool response data
185
+ * @returns File path if this is a file operation
186
+ */
187
+ private extractFilePath;
188
+ /**
189
+ * Extract operation type from tool name
190
+ *
191
+ * @param toolName - Name of the tool
192
+ * @returns Operation type if this is a file operation
193
+ */
194
+ private extractOperation;
195
+ }
196
+ //#endregion
197
+ //#region src/services/ExecutionLogService.d.ts
198
+ /**
199
+ * ExecutionLogService - Tracks hook executions to prevent duplicate actions
200
+ *
201
+ * DESIGN PATTERNS:
202
+ * - Repository pattern: Abstracts data access to execution log
203
+ * - Query pattern: Provides efficient lookups for hook execution history
204
+ * - Singleton cache: In-memory cache for performance
205
+ *
206
+ * CODING STANDARDS:
207
+ * - Use static methods for stateless operations
208
+ * - Handle file system errors gracefully
209
+ * - Optimize for performance with efficient data structures
210
+ *
211
+ * AVOID:
212
+ * - Loading entire log file into memory
213
+ * - Blocking I/O operations
214
+ * - Complex parsing logic (keep it simple)
215
+ */
216
+ /**
217
+ * Input parameters for logging a hook execution
218
+ */
219
+ interface LogExecutionParams {
220
+ sessionId: string;
221
+ filePath: string;
222
+ operation: string;
223
+ decision: string;
224
+ filePattern?: string;
225
+ /** File modification timestamp (mtime) at time of execution */
226
+ fileMtime?: number;
227
+ /** MD5 checksum of file content at time of execution */
228
+ fileChecksum?: string;
229
+ }
230
+ /**
231
+ * Service for tracking hook executions using an append-only log
232
+ * Prevents duplicate hook actions (e.g., showing design patterns twice for same file)
233
+ */
234
+ declare class ExecutionLogService {
235
+ /** Log file path - stored in system temp directory */
236
+ private static readonly LOG_FILE;
237
+ /** In-memory cache of recent executions (last 1000 entries) */
238
+ private static cache;
239
+ /** Max cache size to prevent memory bloat */
240
+ private static readonly MAX_CACHE_SIZE;
241
+ /**
242
+ * Check if a specific action was already taken for this file in this session
243
+ *
244
+ * @param sessionId - Session identifier
245
+ * @param filePath - File path to check
246
+ * @param decision - Decision to check for (e.g., 'deny' means we already showed patterns)
247
+ * @returns true if the action was already taken
248
+ */
249
+ static hasExecuted(sessionId: string, filePath: string, decision: string): Promise<boolean>;
250
+ /**
251
+ * Log a hook execution
252
+ *
253
+ * @param params - Log execution parameters
254
+ */
255
+ static logExecution(params: LogExecutionParams): Promise<void>;
256
+ /**
257
+ * Load execution log from file
258
+ * Uses in-memory cache for performance
259
+ */
260
+ private static loadLog;
261
+ /**
262
+ * Clear the execution log (for testing)
263
+ */
264
+ static clearLog(): Promise<void>;
265
+ /**
266
+ * Get log statistics (for debugging)
267
+ */
268
+ static getStats(): Promise<{
269
+ totalEntries: number;
270
+ uniqueSessions: number;
271
+ uniqueFiles: number;
272
+ }>;
273
+ /**
274
+ * Get file metadata (mtime and checksum) for a file
275
+ *
276
+ * @param filePath - Path to the file
277
+ * @returns File metadata or null if file doesn't exist
278
+ */
279
+ static getFileMetadata(filePath: string): Promise<{
280
+ mtime: number;
281
+ checksum: string;
282
+ } | null>;
283
+ /**
284
+ * Check if a file has changed since the last execution for this session
285
+ * Returns true if the file should be reviewed (new file or content changed)
286
+ *
287
+ * @param sessionId - Session identifier
288
+ * @param filePath - File path to check
289
+ * @param decision - Decision type to check for
290
+ * @returns true if file has changed or no previous execution found
291
+ */
292
+ static hasFileChanged(sessionId: string, filePath: string, decision: string): Promise<boolean>;
293
+ }
294
+ //#endregion
295
+ //#region src/services/AdapterProxyService.d.ts
296
+ /**
297
+ * Proxy service for routing hook execution
298
+ * Eliminates duplication across commands by centralizing hook routing logic
299
+ */
300
+ declare class AdapterProxyService {
301
+ /**
302
+ * Execute hook with the appropriate adapter for the agent
303
+ *
304
+ * @param agentName - Agent identifier (e.g., "claude-code")
305
+ * @param hookType - Type of hook ("PreToolUse" or "PostToolUse")
306
+ * @param callback - Hook callback function to execute
307
+ */
308
+ static execute(agentName: string, hookType: HookType, callback: HookCallback): Promise<void>;
309
+ /**
310
+ * Get adapter instance for agent and hook type
311
+ *
312
+ * @param agentName - Name of the AI agent (e.g., "claude-code")
313
+ * @param hookType - Type of hook ("PreToolUse" or "PostToolUse")
314
+ * @returns Adapter instance
315
+ */
316
+ private static getAdapter;
317
+ }
318
+ //#endregion
319
+ export { AdapterProxyService, BaseAdapter, ClaudeCodeAdapter, ClaudeCodePostToolUseAdapter, ExecutionLogService, HookCallback, HookContext, HookResponse, HookType, LogExecutionParams, POST_TOOL_USE, PRE_TOOL_USE };
@@ -0,0 +1,319 @@
1
+ //#region src/constants/index.d.ts
2
+ /**
3
+ * @agiflowai/hooks-adapter - Constants
4
+ *
5
+ * DESIGN PATTERNS:
6
+ * - Strongly-typed constant exports for compile-time safety
7
+ * - Immutable by default (as const assertions)
8
+ *
9
+ * CODING STANDARDS:
10
+ * - Primitive constants: UPPER_SNAKE_CASE
11
+ * - Always include JSDoc with purpose and usage
12
+ *
13
+ * AVOID:
14
+ * - Mutable exports (let, var)
15
+ * - Magic strings without explanation
16
+ */
17
+ /**
18
+ * Hook type identifiers for different hook events
19
+ */
20
+ declare const PRE_TOOL_USE = "PreToolUse";
21
+ declare const POST_TOOL_USE = "PostToolUse";
22
+ /**
23
+ * Union type of all supported hook types
24
+ */
25
+ type HookType = typeof PRE_TOOL_USE | typeof POST_TOOL_USE;
26
+ //#endregion
27
+ //#region src/types/index.d.ts
28
+ /**
29
+ * @agiflowai/hooks-adapter - Type Definitions
30
+ *
31
+ * DESIGN PATTERNS:
32
+ * - Interface segregation: Keep interfaces focused and minimal
33
+ * - Type composition: Build complex types from simple primitives
34
+ * - Generics: Use type parameters for reusable, type-safe abstractions
35
+ *
36
+ * CODING STANDARDS:
37
+ * - Use PascalCase for type/interface names
38
+ * - Prefix interfaces with 'I' only for abstract contracts
39
+ * - Document complex types with JSDoc comments
40
+ * - Export all public types
41
+ *
42
+ * AVOID:
43
+ * - Any type unless absolutely necessary
44
+ * - Overly complex type gymnastics
45
+ * - Coupling types to implementation details
46
+ */
47
+ /**
48
+ * Normalized context passed to hook callback function
49
+ */
50
+ interface HookContext {
51
+ /** Name of the tool being invoked (e.g., "Read", "Write", "Edit") */
52
+ toolName: string;
53
+ /** Input parameters passed to the tool */
54
+ toolInput: Record<string, any>;
55
+ /** File path if this is a file operation */
56
+ filePath?: string;
57
+ /** Type of file operation */
58
+ operation?: 'read' | 'write' | 'edit';
59
+ /** Current working directory */
60
+ cwd: string;
61
+ /** Unique session identifier */
62
+ sessionId: string;
63
+ /** Optional LLM tool to use for processing (e.g., "claude-code", "gemini") */
64
+ llmTool?: string;
65
+ }
66
+ /**
67
+ * Normalized response from hook callback function
68
+ */
69
+ interface HookResponse {
70
+ /** Permission decision for the tool execution */
71
+ decision: 'allow' | 'deny' | 'ask' | 'skip';
72
+ /** Message shown to the LLM (e.g., design patterns, warnings) */
73
+ message: string;
74
+ /** Optional message shown only to the user (not the LLM) */
75
+ userMessage?: string;
76
+ /** Optional updated input parameters for the tool */
77
+ updatedInput?: Record<string, any>;
78
+ }
79
+ /**
80
+ * Hook callback function signature
81
+ * Takes normalized context and returns normalized response
82
+ */
83
+ type HookCallback = (context: HookContext) => Promise<HookResponse>;
84
+ //#endregion
85
+ //#region src/adapters/BaseAdapter.d.ts
86
+
87
+ /**
88
+ * Abstract base adapter for normalizing AI agent hook formats
89
+ */
90
+ declare abstract class BaseAdapter {
91
+ /**
92
+ * Parse stdin from AI agent into normalized HookContext
93
+ * @param stdin - Raw stdin string from AI agent
94
+ * @returns Normalized hook context
95
+ */
96
+ abstract parseInput(stdin: string): HookContext;
97
+ /**
98
+ * Format normalized HookResponse into AI agent-specific output
99
+ * @param response - Normalized hook response
100
+ * @returns Formatted output string for AI agent
101
+ */
102
+ abstract formatOutput(response: HookResponse): string;
103
+ /**
104
+ * Execute hook callback with normalized context
105
+ * Template method that orchestrates the hook execution flow
106
+ *
107
+ * @param callback - Hook callback function to execute
108
+ */
109
+ execute(callback: HookCallback): Promise<void>;
110
+ /**
111
+ * Read stdin from AI agent
112
+ * @returns Promise resolving to stdin content
113
+ */
114
+ private readStdin;
115
+ /**
116
+ * Handle errors with fail-open behavior
117
+ * Allows operation to proceed with warning message
118
+ *
119
+ * @param error - Error that occurred during hook execution
120
+ */
121
+ private handleError;
122
+ }
123
+ //#endregion
124
+ //#region src/adapters/ClaudeCodeAdapter.d.ts
125
+ /**
126
+ * Adapter for Claude Code hook format
127
+ */
128
+ declare class ClaudeCodeAdapter extends BaseAdapter {
129
+ /**
130
+ * Parse Claude Code stdin into normalized HookContext
131
+ *
132
+ * @param stdin - Raw JSON string from Claude Code
133
+ * @returns Normalized hook context
134
+ */
135
+ parseInput(stdin: string): HookContext;
136
+ /**
137
+ * Format normalized HookResponse into Claude Code output
138
+ *
139
+ * @param response - Normalized hook response
140
+ * @returns JSON string for Claude Code
141
+ */
142
+ formatOutput(response: HookResponse): string;
143
+ /**
144
+ * Extract file path from tool input
145
+ *
146
+ * @param toolName - Name of the tool
147
+ * @param toolInput - Tool input parameters
148
+ * @returns File path if this is a file operation
149
+ */
150
+ private extractFilePath;
151
+ /**
152
+ * Extract operation type from tool name
153
+ *
154
+ * @param toolName - Name of the tool
155
+ * @returns Operation type if this is a file operation
156
+ */
157
+ private extractOperation;
158
+ }
159
+ //#endregion
160
+ //#region src/adapters/ClaudeCodePostToolUseAdapter.d.ts
161
+ /**
162
+ * Adapter for Claude Code PostToolUse hook format
163
+ */
164
+ declare class ClaudeCodePostToolUseAdapter extends BaseAdapter {
165
+ /**
166
+ * Parse Claude Code PostToolUse stdin into normalized HookContext
167
+ *
168
+ * @param stdin - Raw JSON string from Claude Code
169
+ * @returns Normalized hook context
170
+ */
171
+ parseInput(stdin: string): HookContext;
172
+ /**
173
+ * Format normalized HookResponse into Claude Code PostToolUse output
174
+ *
175
+ * @param response - Normalized hook response
176
+ * @returns JSON string for Claude Code
177
+ */
178
+ formatOutput(response: HookResponse): string;
179
+ /**
180
+ * Extract file path from tool input or response
181
+ *
182
+ * @param toolName - Name of the tool
183
+ * @param toolInput - Tool input parameters
184
+ * @param toolResponse - Tool response data
185
+ * @returns File path if this is a file operation
186
+ */
187
+ private extractFilePath;
188
+ /**
189
+ * Extract operation type from tool name
190
+ *
191
+ * @param toolName - Name of the tool
192
+ * @returns Operation type if this is a file operation
193
+ */
194
+ private extractOperation;
195
+ }
196
+ //#endregion
197
+ //#region src/services/ExecutionLogService.d.ts
198
+ /**
199
+ * ExecutionLogService - Tracks hook executions to prevent duplicate actions
200
+ *
201
+ * DESIGN PATTERNS:
202
+ * - Repository pattern: Abstracts data access to execution log
203
+ * - Query pattern: Provides efficient lookups for hook execution history
204
+ * - Singleton cache: In-memory cache for performance
205
+ *
206
+ * CODING STANDARDS:
207
+ * - Use static methods for stateless operations
208
+ * - Handle file system errors gracefully
209
+ * - Optimize for performance with efficient data structures
210
+ *
211
+ * AVOID:
212
+ * - Loading entire log file into memory
213
+ * - Blocking I/O operations
214
+ * - Complex parsing logic (keep it simple)
215
+ */
216
+ /**
217
+ * Input parameters for logging a hook execution
218
+ */
219
+ interface LogExecutionParams {
220
+ sessionId: string;
221
+ filePath: string;
222
+ operation: string;
223
+ decision: string;
224
+ filePattern?: string;
225
+ /** File modification timestamp (mtime) at time of execution */
226
+ fileMtime?: number;
227
+ /** MD5 checksum of file content at time of execution */
228
+ fileChecksum?: string;
229
+ }
230
+ /**
231
+ * Service for tracking hook executions using an append-only log
232
+ * Prevents duplicate hook actions (e.g., showing design patterns twice for same file)
233
+ */
234
+ declare class ExecutionLogService {
235
+ /** Log file path - stored in system temp directory */
236
+ private static readonly LOG_FILE;
237
+ /** In-memory cache of recent executions (last 1000 entries) */
238
+ private static cache;
239
+ /** Max cache size to prevent memory bloat */
240
+ private static readonly MAX_CACHE_SIZE;
241
+ /**
242
+ * Check if a specific action was already taken for this file in this session
243
+ *
244
+ * @param sessionId - Session identifier
245
+ * @param filePath - File path to check
246
+ * @param decision - Decision to check for (e.g., 'deny' means we already showed patterns)
247
+ * @returns true if the action was already taken
248
+ */
249
+ static hasExecuted(sessionId: string, filePath: string, decision: string): Promise<boolean>;
250
+ /**
251
+ * Log a hook execution
252
+ *
253
+ * @param params - Log execution parameters
254
+ */
255
+ static logExecution(params: LogExecutionParams): Promise<void>;
256
+ /**
257
+ * Load execution log from file
258
+ * Uses in-memory cache for performance
259
+ */
260
+ private static loadLog;
261
+ /**
262
+ * Clear the execution log (for testing)
263
+ */
264
+ static clearLog(): Promise<void>;
265
+ /**
266
+ * Get log statistics (for debugging)
267
+ */
268
+ static getStats(): Promise<{
269
+ totalEntries: number;
270
+ uniqueSessions: number;
271
+ uniqueFiles: number;
272
+ }>;
273
+ /**
274
+ * Get file metadata (mtime and checksum) for a file
275
+ *
276
+ * @param filePath - Path to the file
277
+ * @returns File metadata or null if file doesn't exist
278
+ */
279
+ static getFileMetadata(filePath: string): Promise<{
280
+ mtime: number;
281
+ checksum: string;
282
+ } | null>;
283
+ /**
284
+ * Check if a file has changed since the last execution for this session
285
+ * Returns true if the file should be reviewed (new file or content changed)
286
+ *
287
+ * @param sessionId - Session identifier
288
+ * @param filePath - File path to check
289
+ * @param decision - Decision type to check for
290
+ * @returns true if file has changed or no previous execution found
291
+ */
292
+ static hasFileChanged(sessionId: string, filePath: string, decision: string): Promise<boolean>;
293
+ }
294
+ //#endregion
295
+ //#region src/services/AdapterProxyService.d.ts
296
+ /**
297
+ * Proxy service for routing hook execution
298
+ * Eliminates duplication across commands by centralizing hook routing logic
299
+ */
300
+ declare class AdapterProxyService {
301
+ /**
302
+ * Execute hook with the appropriate adapter for the agent
303
+ *
304
+ * @param agentName - Agent identifier (e.g., "claude-code")
305
+ * @param hookType - Type of hook ("PreToolUse" or "PostToolUse")
306
+ * @param callback - Hook callback function to execute
307
+ */
308
+ static execute(agentName: string, hookType: HookType, callback: HookCallback): Promise<void>;
309
+ /**
310
+ * Get adapter instance for agent and hook type
311
+ *
312
+ * @param agentName - Name of the AI agent (e.g., "claude-code")
313
+ * @param hookType - Type of hook ("PreToolUse" or "PostToolUse")
314
+ * @returns Adapter instance
315
+ */
316
+ private static getAdapter;
317
+ }
318
+ //#endregion
319
+ export { AdapterProxyService, BaseAdapter, ClaudeCodeAdapter, ClaudeCodePostToolUseAdapter, ExecutionLogService, HookCallback, HookContext, HookResponse, HookType, LogExecutionParams, POST_TOOL_USE, PRE_TOOL_USE };