wave-agent-sdk 0.0.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 +32 -0
- package/dist/agent.d.ts +96 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +286 -0
- package/dist/hooks/executor.d.ts +56 -0
- package/dist/hooks/executor.d.ts.map +1 -0
- package/dist/hooks/executor.js +312 -0
- package/dist/hooks/index.d.ts +17 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +14 -0
- package/dist/hooks/manager.d.ts +90 -0
- package/dist/hooks/manager.d.ts.map +1 -0
- package/dist/hooks/manager.js +395 -0
- package/dist/hooks/matcher.d.ts +49 -0
- package/dist/hooks/matcher.d.ts.map +1 -0
- package/dist/hooks/matcher.js +147 -0
- package/dist/hooks/settings.d.ts +46 -0
- package/dist/hooks/settings.d.ts.map +1 -0
- package/dist/hooks/settings.js +100 -0
- package/dist/hooks/types.d.ts +80 -0
- package/dist/hooks/types.d.ts.map +1 -0
- package/dist/hooks/types.js +59 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -0
- package/dist/managers/aiManager.d.ts +61 -0
- package/dist/managers/aiManager.d.ts.map +1 -0
- package/dist/managers/aiManager.js +415 -0
- package/dist/managers/backgroundBashManager.d.ts +27 -0
- package/dist/managers/backgroundBashManager.d.ts.map +1 -0
- package/dist/managers/backgroundBashManager.js +166 -0
- package/dist/managers/bashManager.d.ts +20 -0
- package/dist/managers/bashManager.d.ts.map +1 -0
- package/dist/managers/bashManager.js +66 -0
- package/dist/managers/mcpManager.d.ts +63 -0
- package/dist/managers/mcpManager.d.ts.map +1 -0
- package/dist/managers/mcpManager.js +378 -0
- package/dist/managers/messageManager.d.ts +85 -0
- package/dist/managers/messageManager.d.ts.map +1 -0
- package/dist/managers/messageManager.js +265 -0
- package/dist/managers/skillManager.d.ts +59 -0
- package/dist/managers/skillManager.d.ts.map +1 -0
- package/dist/managers/skillManager.js +317 -0
- package/dist/managers/slashCommandManager.d.ts +77 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -0
- package/dist/managers/slashCommandManager.js +208 -0
- package/dist/managers/toolManager.d.ts +23 -0
- package/dist/managers/toolManager.d.ts.map +1 -0
- package/dist/managers/toolManager.js +79 -0
- package/dist/services/aiService.d.ts +28 -0
- package/dist/services/aiService.d.ts.map +1 -0
- package/dist/services/aiService.js +180 -0
- package/dist/services/memory.d.ts +8 -0
- package/dist/services/memory.d.ts.map +1 -0
- package/dist/services/memory.js +128 -0
- package/dist/services/session.d.ts +54 -0
- package/dist/services/session.d.ts.map +1 -0
- package/dist/services/session.js +196 -0
- package/dist/tools/bashTool.d.ts +14 -0
- package/dist/tools/bashTool.d.ts.map +1 -0
- package/dist/tools/bashTool.js +351 -0
- package/dist/tools/deleteFileTool.d.ts +6 -0
- package/dist/tools/deleteFileTool.d.ts.map +1 -0
- package/dist/tools/deleteFileTool.js +67 -0
- package/dist/tools/editTool.d.ts +6 -0
- package/dist/tools/editTool.d.ts.map +1 -0
- package/dist/tools/editTool.js +168 -0
- package/dist/tools/globTool.d.ts +6 -0
- package/dist/tools/globTool.d.ts.map +1 -0
- package/dist/tools/globTool.js +113 -0
- package/dist/tools/grepTool.d.ts +6 -0
- package/dist/tools/grepTool.d.ts.map +1 -0
- package/dist/tools/grepTool.js +268 -0
- package/dist/tools/lsTool.d.ts +6 -0
- package/dist/tools/lsTool.d.ts.map +1 -0
- package/dist/tools/lsTool.js +160 -0
- package/dist/tools/multiEditTool.d.ts +6 -0
- package/dist/tools/multiEditTool.d.ts.map +1 -0
- package/dist/tools/multiEditTool.js +222 -0
- package/dist/tools/readTool.d.ts +6 -0
- package/dist/tools/readTool.d.ts.map +1 -0
- package/dist/tools/readTool.js +136 -0
- package/dist/tools/types.d.ts +35 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +4 -0
- package/dist/tools/writeTool.d.ts +6 -0
- package/dist/tools/writeTool.d.ts.map +1 -0
- package/dist/tools/writeTool.js +138 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/utils/bashHistory.d.ts +46 -0
- package/dist/utils/bashHistory.d.ts.map +1 -0
- package/dist/utils/bashHistory.js +236 -0
- package/dist/utils/commandArgumentParser.d.ts +34 -0
- package/dist/utils/commandArgumentParser.d.ts.map +1 -0
- package/dist/utils/commandArgumentParser.js +123 -0
- package/dist/utils/constants.d.ts +27 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +28 -0
- package/dist/utils/convertMessagesForAPI.d.ts +9 -0
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -0
- package/dist/utils/convertMessagesForAPI.js +189 -0
- package/dist/utils/customCommands.d.ts +14 -0
- package/dist/utils/customCommands.d.ts.map +1 -0
- package/dist/utils/customCommands.js +71 -0
- package/dist/utils/fileFilter.d.ts +26 -0
- package/dist/utils/fileFilter.d.ts.map +1 -0
- package/dist/utils/fileFilter.js +177 -0
- package/dist/utils/markdownParser.d.ts +27 -0
- package/dist/utils/markdownParser.d.ts.map +1 -0
- package/dist/utils/markdownParser.js +109 -0
- package/dist/utils/mcpUtils.d.ts +24 -0
- package/dist/utils/mcpUtils.d.ts.map +1 -0
- package/dist/utils/mcpUtils.js +51 -0
- package/dist/utils/messageOperations.d.ts +118 -0
- package/dist/utils/messageOperations.d.ts.map +1 -0
- package/dist/utils/messageOperations.js +334 -0
- package/dist/utils/path.d.ts +25 -0
- package/dist/utils/path.d.ts.map +1 -0
- package/dist/utils/path.js +109 -0
- package/dist/utils/skillParser.d.ts +18 -0
- package/dist/utils/skillParser.d.ts.map +1 -0
- package/dist/utils/skillParser.js +147 -0
- package/dist/utils/stringUtils.d.ts +13 -0
- package/dist/utils/stringUtils.d.ts.map +1 -0
- package/dist/utils/stringUtils.js +44 -0
- package/package.json +51 -0
- package/src/agent.ts +405 -0
- package/src/hooks/executor.ts +440 -0
- package/src/hooks/index.ts +52 -0
- package/src/hooks/manager.ts +618 -0
- package/src/hooks/matcher.ts +187 -0
- package/src/hooks/settings.ts +129 -0
- package/src/hooks/types.ts +169 -0
- package/src/index.ts +24 -0
- package/src/managers/aiManager.ts +573 -0
- package/src/managers/backgroundBashManager.ts +203 -0
- package/src/managers/bashManager.ts +97 -0
- package/src/managers/mcpManager.ts +493 -0
- package/src/managers/messageManager.ts +415 -0
- package/src/managers/skillManager.ts +404 -0
- package/src/managers/slashCommandManager.ts +293 -0
- package/src/managers/toolManager.ts +106 -0
- package/src/services/aiService.ts +252 -0
- package/src/services/memory.ts +149 -0
- package/src/services/session.ts +265 -0
- package/src/tools/bashTool.ts +402 -0
- package/src/tools/deleteFileTool.ts +81 -0
- package/src/tools/editTool.ts +192 -0
- package/src/tools/globTool.ts +135 -0
- package/src/tools/grepTool.ts +326 -0
- package/src/tools/lsTool.ts +187 -0
- package/src/tools/multiEditTool.ts +268 -0
- package/src/tools/readTool.ts +165 -0
- package/src/tools/types.ts +47 -0
- package/src/tools/writeTool.ts +163 -0
- package/src/types.ts +260 -0
- package/src/utils/bashHistory.ts +303 -0
- package/src/utils/commandArgumentParser.ts +153 -0
- package/src/utils/constants.ts +37 -0
- package/src/utils/convertMessagesForAPI.ts +236 -0
- package/src/utils/customCommands.ts +85 -0
- package/src/utils/fileFilter.ts +202 -0
- package/src/utils/markdownParser.ts +156 -0
- package/src/utils/mcpUtils.ts +81 -0
- package/src/utils/messageOperations.ts +506 -0
- package/src/utils/path.ts +118 -0
- package/src/utils/skillParser.ts +188 -0
- package/src/utils/stringUtils.ts +50 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook Command Executor
|
|
3
|
+
*
|
|
4
|
+
* Handles the execution of hook commands in isolated processes with proper
|
|
5
|
+
* timeout handling, environment variable injection, and cross-platform support.
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from "child_process";
|
|
8
|
+
import { getSessionFilePath, } from "./types.js";
|
|
9
|
+
/**
|
|
10
|
+
* Build JSON input data for hook stdin
|
|
11
|
+
*/
|
|
12
|
+
function buildHookJsonInput(context) {
|
|
13
|
+
const jsonInput = {
|
|
14
|
+
session_id: context.sessionId || "unknown",
|
|
15
|
+
transcript_path: context.transcriptPath ||
|
|
16
|
+
(context.sessionId ? getSessionFilePath(context.sessionId) : ""),
|
|
17
|
+
cwd: context.cwd || context.projectDir,
|
|
18
|
+
hook_event_name: context.event,
|
|
19
|
+
};
|
|
20
|
+
// Add optional fields based on event type
|
|
21
|
+
if (context.event === "PreToolUse" || context.event === "PostToolUse") {
|
|
22
|
+
if (context.toolName) {
|
|
23
|
+
jsonInput.tool_name = context.toolName;
|
|
24
|
+
}
|
|
25
|
+
if (context.toolInput !== undefined) {
|
|
26
|
+
jsonInput.tool_input = context.toolInput;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (context.event === "PostToolUse" && context.toolResponse !== undefined) {
|
|
30
|
+
jsonInput.tool_response = context.toolResponse;
|
|
31
|
+
}
|
|
32
|
+
if (context.event === "UserPromptSubmit" &&
|
|
33
|
+
context.userPrompt !== undefined) {
|
|
34
|
+
jsonInput.user_prompt = context.userPrompt;
|
|
35
|
+
}
|
|
36
|
+
return jsonInput;
|
|
37
|
+
}
|
|
38
|
+
export class HookExecutor {
|
|
39
|
+
constructor(logger, options) {
|
|
40
|
+
this.defaultTimeout = 10000; // 10 seconds
|
|
41
|
+
this.maxTimeout = 300000; // 5 minutes
|
|
42
|
+
this.logger = logger;
|
|
43
|
+
// Skip execution in test environment unless we're specifically testing the executor
|
|
44
|
+
this.skipExecution =
|
|
45
|
+
options?.skipExecution ??
|
|
46
|
+
((process.env.NODE_ENV === "test" || process.env.VITEST === "true") &&
|
|
47
|
+
!process.env.WAVE_TEST_HOOKS_EXECUTION);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Execute a single hook command in an isolated process
|
|
51
|
+
*/
|
|
52
|
+
async executeCommand(command, context, options = {}) {
|
|
53
|
+
const startTime = Date.now();
|
|
54
|
+
const timeout = Math.min(options.timeout ?? this.defaultTimeout, this.maxTimeout);
|
|
55
|
+
const cwd = options.cwd ?? context.projectDir;
|
|
56
|
+
// Skip command execution in test environment (unless specifically testing hooks)
|
|
57
|
+
if (this.skipExecution) {
|
|
58
|
+
this.logger?.debug(`[Hook] Skipping command execution: ${command}`);
|
|
59
|
+
return {
|
|
60
|
+
success: true,
|
|
61
|
+
exitCode: 0,
|
|
62
|
+
stdout: "Test environment: command execution skipped",
|
|
63
|
+
duration: Date.now() - startTime,
|
|
64
|
+
timedOut: false,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// Log hook execution start
|
|
68
|
+
this.logger?.debug(`[Hook] Executing ${context.event} hook: ${command}`);
|
|
69
|
+
this.logger?.debug(`[Hook] Context: event=${context.event}, tool=${context.toolName || "N/A"}, cwd=${cwd}`);
|
|
70
|
+
// Validate command safety
|
|
71
|
+
if (!this.isCommandSafe(command)) {
|
|
72
|
+
const error = "Command contains potentially unsafe characters";
|
|
73
|
+
this.logger?.warn(`[Hook] Command safety validation failed: ${error}`);
|
|
74
|
+
return {
|
|
75
|
+
success: false,
|
|
76
|
+
exitCode: -1,
|
|
77
|
+
stderr: error,
|
|
78
|
+
duration: Date.now() - startTime,
|
|
79
|
+
timedOut: false,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
// Prepare environment variables
|
|
83
|
+
const env = this.buildEnvironment(context);
|
|
84
|
+
// Resolve command with environment variables
|
|
85
|
+
const resolvedCommand = this.resolveEnvironmentVariables(command, env);
|
|
86
|
+
// Prepare JSON input for stdin (if extended context is provided)
|
|
87
|
+
const isExtendedContext = "sessionId" in context ||
|
|
88
|
+
"toolInput" in context ||
|
|
89
|
+
"toolResponse" in context ||
|
|
90
|
+
"userPrompt" in context;
|
|
91
|
+
const jsonInput = isExtendedContext
|
|
92
|
+
? buildHookJsonInput(context)
|
|
93
|
+
: null;
|
|
94
|
+
this.logger?.debug(`[Hook] Resolved command: ${resolvedCommand}`);
|
|
95
|
+
if (jsonInput) {
|
|
96
|
+
this.logger?.debug(`[Hook] JSON input: ${JSON.stringify(jsonInput)}`);
|
|
97
|
+
}
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
let stdout = "";
|
|
100
|
+
let stderr = "";
|
|
101
|
+
let timedOut = false;
|
|
102
|
+
let childProcess;
|
|
103
|
+
// Setup timeout
|
|
104
|
+
const timeoutId = setTimeout(() => {
|
|
105
|
+
timedOut = true;
|
|
106
|
+
this.logger?.warn(`[Hook] Command timed out after ${timeout}ms: ${resolvedCommand}`);
|
|
107
|
+
if (childProcess && !childProcess.killed) {
|
|
108
|
+
childProcess.kill("SIGTERM");
|
|
109
|
+
// Force kill after 5 seconds if SIGTERM doesn't work
|
|
110
|
+
setTimeout(() => {
|
|
111
|
+
if (childProcess && !childProcess.killed) {
|
|
112
|
+
this.logger?.warn(`[Hook] Force killing process: ${resolvedCommand}`);
|
|
113
|
+
childProcess.kill("SIGKILL");
|
|
114
|
+
}
|
|
115
|
+
}, 5000);
|
|
116
|
+
}
|
|
117
|
+
}, timeout);
|
|
118
|
+
try {
|
|
119
|
+
// Execute command using shell for cross-platform compatibility
|
|
120
|
+
const shell = process.platform === "win32" ? "cmd.exe" : "/bin/sh";
|
|
121
|
+
const shellArgs = process.platform === "win32" ? ["/c"] : ["-c"];
|
|
122
|
+
childProcess = spawn(shell, [...shellArgs, resolvedCommand], {
|
|
123
|
+
cwd,
|
|
124
|
+
env,
|
|
125
|
+
stdio: "pipe",
|
|
126
|
+
windowsHide: true, // Hide console window on Windows
|
|
127
|
+
});
|
|
128
|
+
this.logger?.debug(`[Hook] Process started (PID: ${childProcess.pid})`);
|
|
129
|
+
// Write JSON input to stdin if available
|
|
130
|
+
if (jsonInput && childProcess.stdin) {
|
|
131
|
+
try {
|
|
132
|
+
const jsonString = JSON.stringify(jsonInput, null, 2);
|
|
133
|
+
childProcess.stdin.write(jsonString);
|
|
134
|
+
childProcess.stdin.end();
|
|
135
|
+
this.logger?.debug(`[Hook] JSON input written to stdin`);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
this.logger?.warn(`[Hook] Failed to write JSON to stdin: ${error}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Collect stdout
|
|
142
|
+
childProcess.stdout?.on("data", (data) => {
|
|
143
|
+
stdout += data.toString();
|
|
144
|
+
});
|
|
145
|
+
// Collect stderr
|
|
146
|
+
childProcess.stderr?.on("data", (data) => {
|
|
147
|
+
stderr += data.toString();
|
|
148
|
+
});
|
|
149
|
+
// Handle process completion
|
|
150
|
+
childProcess.on("close", (exitCode) => {
|
|
151
|
+
clearTimeout(timeoutId);
|
|
152
|
+
const duration = Date.now() - startTime;
|
|
153
|
+
const success = !timedOut && exitCode === 0;
|
|
154
|
+
// Log execution result
|
|
155
|
+
if (success) {
|
|
156
|
+
this.logger?.debug(`[Hook] Command completed successfully in ${duration}ms (exit code: ${exitCode})`);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
this.logger?.warn(`[Hook] Command failed in ${duration}ms (exit code: ${exitCode}, timed out: ${timedOut})`);
|
|
160
|
+
if (stderr) {
|
|
161
|
+
this.logger?.warn(`[Hook] stderr: ${stderr.trim()}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (stdout && stdout.trim()) {
|
|
165
|
+
this.logger?.debug(`[Hook] stdout: ${stdout.trim()}`);
|
|
166
|
+
}
|
|
167
|
+
resolve({
|
|
168
|
+
success,
|
|
169
|
+
exitCode: exitCode ?? undefined,
|
|
170
|
+
stdout: stdout.trim() || undefined,
|
|
171
|
+
stderr: stderr.trim() || undefined,
|
|
172
|
+
duration,
|
|
173
|
+
timedOut,
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
// Handle process errors
|
|
177
|
+
childProcess.on("error", (error) => {
|
|
178
|
+
clearTimeout(timeoutId);
|
|
179
|
+
const duration = Date.now() - startTime;
|
|
180
|
+
this.logger?.error(`[Hook] Process error in ${duration}ms: ${error.message}`);
|
|
181
|
+
resolve({
|
|
182
|
+
success: false,
|
|
183
|
+
stderr: error.message,
|
|
184
|
+
duration,
|
|
185
|
+
timedOut,
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
clearTimeout(timeoutId);
|
|
191
|
+
const duration = Date.now() - startTime;
|
|
192
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown execution error";
|
|
193
|
+
this.logger?.error(`[Hook] Execution error in ${duration}ms: ${errorMessage}`);
|
|
194
|
+
resolve({
|
|
195
|
+
success: false,
|
|
196
|
+
stderr: errorMessage,
|
|
197
|
+
duration,
|
|
198
|
+
timedOut,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Execute multiple commands in sequence
|
|
205
|
+
* Stops on first failure unless configured otherwise
|
|
206
|
+
*/
|
|
207
|
+
async executeCommands(commands, context, options = {}) {
|
|
208
|
+
const results = [];
|
|
209
|
+
this.logger?.debug(`[Hook] Executing ${commands.length} commands in sequence for ${context.event} event`);
|
|
210
|
+
for (let i = 0; i < commands.length; i++) {
|
|
211
|
+
const command = commands[i];
|
|
212
|
+
this.logger?.debug(`[Hook] Command ${i + 1}/${commands.length}: ${command}`);
|
|
213
|
+
try {
|
|
214
|
+
const result = await this.executeCommand(command, context, options);
|
|
215
|
+
results.push(result);
|
|
216
|
+
// Stop on first failure to prevent cascading issues
|
|
217
|
+
if (!result.success) {
|
|
218
|
+
this.logger?.warn(`[Hook] Stopping sequence at command ${i + 1} due to failure`);
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
// This shouldn't happen as executeCommand handles all errors,
|
|
224
|
+
// but include defensive handling
|
|
225
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
226
|
+
this.logger?.error(`[Hook] Unexpected error in command ${i + 1}: ${errorMessage}`);
|
|
227
|
+
results.push({
|
|
228
|
+
success: false,
|
|
229
|
+
stderr: errorMessage,
|
|
230
|
+
duration: 0,
|
|
231
|
+
timedOut: false,
|
|
232
|
+
});
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const successCount = results.filter((r) => r.success).length;
|
|
237
|
+
this.logger?.debug(`[Hook] Completed sequence: ${successCount}/${results.length} commands succeeded`);
|
|
238
|
+
return results;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Validate command safety to prevent injection attacks
|
|
242
|
+
*/
|
|
243
|
+
isCommandSafe(command) {
|
|
244
|
+
if (!command || typeof command !== "string")
|
|
245
|
+
return false;
|
|
246
|
+
// Command cannot be empty
|
|
247
|
+
const trimmed = command.trim();
|
|
248
|
+
if (trimmed.length === 0)
|
|
249
|
+
return false;
|
|
250
|
+
// Basic safety checks - these could be expanded based on security requirements
|
|
251
|
+
const dangerousPatterns = [
|
|
252
|
+
/\b(rm\s+-rf\s+\/|rm\s+-rf\s+~|rm\s+-rf\s+\*)/i, // Dangerous rm commands
|
|
253
|
+
/\bdd\s+if=/i, // dd commands that could be destructive
|
|
254
|
+
/:\(\)\{.*\}/, // Fork bomb patterns
|
|
255
|
+
/\beval\s*[(\s]/i, // Code evaluation
|
|
256
|
+
/\bexec\s+/i, // Process execution in shells
|
|
257
|
+
];
|
|
258
|
+
// Check for obviously dangerous patterns
|
|
259
|
+
if (dangerousPatterns.some((pattern) => pattern.test(trimmed))) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Build environment variables for hook execution
|
|
266
|
+
*/
|
|
267
|
+
buildEnvironment(context) {
|
|
268
|
+
return {
|
|
269
|
+
...process.env, // Inherit parent environment
|
|
270
|
+
WAVE_PROJECT_DIR: context.projectDir,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Resolve environment variables in command string
|
|
275
|
+
*/
|
|
276
|
+
resolveEnvironmentVariables(command, env) {
|
|
277
|
+
let resolved = command;
|
|
278
|
+
// Replace $VAR and ${VAR} patterns
|
|
279
|
+
Object.entries(env).forEach(([key, value]) => {
|
|
280
|
+
if (value !== undefined) {
|
|
281
|
+
// Handle both $VAR and ${VAR} syntax
|
|
282
|
+
const dollarPattern = new RegExp(`\\$${key}\\b`, "g");
|
|
283
|
+
const bracesPattern = new RegExp(`\\$\\{${key}\\}`, "g");
|
|
284
|
+
resolved = resolved.replace(dollarPattern, value);
|
|
285
|
+
resolved = resolved.replace(bracesPattern, value);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
return resolved;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Get process statistics for monitoring
|
|
292
|
+
*/
|
|
293
|
+
getExecutionStats() {
|
|
294
|
+
return {
|
|
295
|
+
platform: process.platform,
|
|
296
|
+
defaultTimeout: this.defaultTimeout,
|
|
297
|
+
maxTimeout: this.maxTimeout,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Test if the executor can run commands on this platform
|
|
302
|
+
*/
|
|
303
|
+
isSupported() {
|
|
304
|
+
try {
|
|
305
|
+
// Try to detect shell availability - basic check could be enhanced with actual shell testing
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks System Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Exports all public APIs for the Wave Code hooks system.
|
|
5
|
+
* This module provides a clean interface for integrating hooks
|
|
6
|
+
* into AI workflows and CLI applications.
|
|
7
|
+
*/
|
|
8
|
+
export type { HookEvent, HookCommand, HookEventConfig, HookConfiguration, HookExecutionContext, HookExecutionResult, HookExecutionOptions, ValidationResult, HookEnvironment, } from "./types.js";
|
|
9
|
+
export { isValidHookEvent, isValidHookCommand, isValidHookEventConfig, HookExecutionError, HookConfigurationError, } from "./types.js";
|
|
10
|
+
export type { IHookMatcher } from "./matcher.js";
|
|
11
|
+
export { HookMatcher } from "./matcher.js";
|
|
12
|
+
export type { IHookExecutor } from "./executor.js";
|
|
13
|
+
export { HookExecutor } from "./executor.js";
|
|
14
|
+
export type { IHookManager } from "./manager.js";
|
|
15
|
+
export { HookManager } from "./manager.js";
|
|
16
|
+
export { getUserHooksConfigPath, getProjectHooksConfigPath, loadUserHooksConfig, loadProjectHooksConfig, loadMergedHooksConfig, hasHooksConfiguration, getHooksConfigurationInfo, } from "./settings.js";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,YAAY,EACV,SAAS,EACT,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,gBAAgB,EAChB,eAAe,GAChB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,sBAAsB,EACtB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAGpB,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG3C,YAAY,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAG7C,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG3C,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,mBAAmB,EACnB,sBAAsB,EACtB,qBAAqB,EACrB,qBAAqB,EACrB,yBAAyB,GAC1B,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks System Entry Point
|
|
3
|
+
*
|
|
4
|
+
* Exports all public APIs for the Wave Code hooks system.
|
|
5
|
+
* This module provides a clean interface for integrating hooks
|
|
6
|
+
* into AI workflows and CLI applications.
|
|
7
|
+
*/
|
|
8
|
+
// Type guards and utility functions
|
|
9
|
+
export { isValidHookEvent, isValidHookCommand, isValidHookEventConfig, HookExecutionError, HookConfigurationError, } from "./types.js";
|
|
10
|
+
export { HookMatcher } from "./matcher.js";
|
|
11
|
+
export { HookExecutor } from "./executor.js";
|
|
12
|
+
export { HookManager } from "./manager.js";
|
|
13
|
+
// Settings and configuration loading
|
|
14
|
+
export { getUserHooksConfigPath, getProjectHooksConfigPath, loadUserHooksConfig, loadProjectHooksConfig, loadMergedHooksConfig, hasHooksConfiguration, getHooksConfigurationInfo, } from "./settings.js";
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook Manager
|
|
3
|
+
*
|
|
4
|
+
* Central orchestrator for the hooks system. Handles configuration loading,
|
|
5
|
+
* validation, and hook execution across all supported events.
|
|
6
|
+
*/
|
|
7
|
+
import { type HookEvent, type HookConfiguration, type PartialHookConfiguration, type HookExecutionContext, type ExtendedHookExecutionContext, type HookExecutionResult, type ValidationResult } from "./types.js";
|
|
8
|
+
import { type IHookMatcher } from "./matcher.js";
|
|
9
|
+
import { type IHookExecutor } from "./executor.js";
|
|
10
|
+
import type { Logger } from "../types.js";
|
|
11
|
+
export interface IHookManager {
|
|
12
|
+
loadConfiguration(userHooks?: PartialHookConfiguration, projectHooks?: PartialHookConfiguration): void;
|
|
13
|
+
loadConfigurationFromSettings(): void;
|
|
14
|
+
executeHooks(event: HookEvent, context: HookExecutionContext | ExtendedHookExecutionContext): Promise<HookExecutionResult[]>;
|
|
15
|
+
hasHooks(event: HookEvent, toolName?: string): boolean;
|
|
16
|
+
validateConfiguration(config: HookConfiguration): ValidationResult;
|
|
17
|
+
getConfiguration(): PartialHookConfiguration | undefined;
|
|
18
|
+
}
|
|
19
|
+
export declare class HookManager implements IHookManager {
|
|
20
|
+
private configuration;
|
|
21
|
+
private readonly matcher;
|
|
22
|
+
private readonly executor;
|
|
23
|
+
private readonly logger?;
|
|
24
|
+
private readonly workdir;
|
|
25
|
+
constructor(workdir: string, matcher?: IHookMatcher, executor?: IHookExecutor, logger?: Logger);
|
|
26
|
+
/**
|
|
27
|
+
* Load and merge hook configurations from user and project settings
|
|
28
|
+
* Project settings take precedence over user settings
|
|
29
|
+
*/
|
|
30
|
+
loadConfiguration(userHooks?: PartialHookConfiguration, projectHooks?: PartialHookConfiguration): void;
|
|
31
|
+
/**
|
|
32
|
+
* Load configuration from filesystem settings
|
|
33
|
+
* Automatically loads and merges user and project hooks configuration
|
|
34
|
+
*/
|
|
35
|
+
loadConfigurationFromSettings(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Execute hooks for a specific event
|
|
38
|
+
*/
|
|
39
|
+
executeHooks(event: HookEvent, context: HookExecutionContext | ExtendedHookExecutionContext): Promise<HookExecutionResult[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Check if hooks are configured for an event/tool combination
|
|
42
|
+
*/
|
|
43
|
+
hasHooks(event: HookEvent, toolName?: string): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Validate hook configuration structure and content
|
|
46
|
+
*/
|
|
47
|
+
validateConfiguration(config: HookConfiguration): ValidationResult;
|
|
48
|
+
/**
|
|
49
|
+
* Validate partial hook configuration structure and content
|
|
50
|
+
*/
|
|
51
|
+
private validatePartialConfiguration;
|
|
52
|
+
/**
|
|
53
|
+
* Get current configuration
|
|
54
|
+
*/
|
|
55
|
+
getConfiguration(): PartialHookConfiguration | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Clear current configuration
|
|
58
|
+
*/
|
|
59
|
+
clearConfiguration(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Validate execution context for a specific event
|
|
62
|
+
*/
|
|
63
|
+
private validateExecutionContext;
|
|
64
|
+
/**
|
|
65
|
+
* Generate a summary of hook execution results
|
|
66
|
+
*/
|
|
67
|
+
private generateExecutionSummary;
|
|
68
|
+
/**
|
|
69
|
+
* Merge hook configurations, with the second taking precedence
|
|
70
|
+
*/
|
|
71
|
+
private mergeHooksConfiguration;
|
|
72
|
+
/**
|
|
73
|
+
* Check if a hook configuration applies to the current context
|
|
74
|
+
*/
|
|
75
|
+
private configApplies;
|
|
76
|
+
/**
|
|
77
|
+
* Validate a single event configuration
|
|
78
|
+
*/
|
|
79
|
+
private validateEventConfig;
|
|
80
|
+
/**
|
|
81
|
+
* Get statistics about current configuration
|
|
82
|
+
*/
|
|
83
|
+
getConfigurationStats(): {
|
|
84
|
+
totalEvents: number;
|
|
85
|
+
totalConfigs: number;
|
|
86
|
+
totalCommands: number;
|
|
87
|
+
eventBreakdown: Record<HookEvent, number>;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../src/hooks/manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACL,KAAK,SAAS,EAEd,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EACzB,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EAItB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,YAAY,EAAe,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,KAAK,aAAa,EAAgB,MAAM,eAAe,CAAC;AAEjE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,WAAW,YAAY;IAE3B,iBAAiB,CACf,SAAS,CAAC,EAAE,wBAAwB,EACpC,YAAY,CAAC,EAAE,wBAAwB,GACtC,IAAI,CAAC;IAGR,6BAA6B,IAAI,IAAI,CAAC;IAGtC,YAAY,CACV,KAAK,EAAE,SAAS,EAChB,OAAO,EAAE,oBAAoB,GAAG,4BAA4B,GAC3D,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAAC;IAGlC,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IAGvD,qBAAqB,CAAC,MAAM,EAAE,iBAAiB,GAAG,gBAAgB,CAAC;IAGnE,gBAAgB,IAAI,wBAAwB,GAAG,SAAS,CAAC;CAC1D;AAED,qBAAa,WAAY,YAAW,YAAY;IAC9C,OAAO,CAAC,aAAa,CAAuC;IAC5D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAe;IACvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IACzC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAG/B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,YAAgC,EACzC,QAAQ,CAAC,EAAE,aAAa,EACxB,MAAM,CAAC,EAAE,MAAM;IAWjB;;;OAGG;IACH,iBAAiB,CACf,SAAS,CAAC,EAAE,wBAAwB,EACpC,YAAY,CAAC,EAAE,wBAAwB,GACtC,IAAI;IAyBP;;;OAGG;IACH,6BAA6B,IAAI,IAAI;IAmCrC;;OAEG;IACG,YAAY,CAChB,KAAK,EAAE,SAAS,EAChB,OAAO,EAAE,oBAAoB,GAAG,4BAA4B,GAC3D,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAsHjC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO;IAWtD;;OAEG;IACH,qBAAqB,CAAC,MAAM,EAAE,iBAAiB,GAAG,gBAAgB;IA8ClE;;OAEG;IACH,OAAO,CAAC,4BAA4B;IAyCpC;;OAEG;IACH,gBAAgB,IAAI,wBAAwB,GAAG,SAAS;IAOxD;;OAEG;IACH,kBAAkB,IAAI,IAAI;IAI1B;;OAEG;IACH,OAAO,CAAC,wBAAwB;IAmDhC;;OAEG;IACH,OAAO,CAAC,wBAAwB;IA8BhC;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAa/B;;OAEG;IACH,OAAO,CAAC,aAAa;IA4BrB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAqC3B;;OAEG;IACH,qBAAqB,IAAI;QACvB,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,aAAa,EAAE,MAAM,CAAC;QACtB,cAAc,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;KAC3C;CA2CF"}
|