fix: skip UserPromptSubmit hooks for slash commands (#734)
This commit is contained in:
@@ -139,6 +139,7 @@ export async function runPermissionRequestHooks(
|
||||
/**
|
||||
* Run UserPromptSubmit hooks before processing a user's prompt
|
||||
* Can block the prompt from being processed
|
||||
* Skips execution for slash commands (e.g., /help, /clear)
|
||||
*/
|
||||
export async function runUserPromptSubmitHooks(
|
||||
prompt: string,
|
||||
@@ -147,6 +148,11 @@ export async function runUserPromptSubmitHooks(
|
||||
conversationId?: string,
|
||||
workingDirectory: string = process.cwd(),
|
||||
): Promise<HookExecutionResult> {
|
||||
// Skip hooks for slash commands - they don't trigger agent execution
|
||||
if (isCommand) {
|
||||
return { blocked: false, errored: false, feedback: [], results: [] };
|
||||
}
|
||||
|
||||
const hooks = await getHooksForEvent(
|
||||
"UserPromptSubmit",
|
||||
undefined,
|
||||
|
||||
@@ -184,7 +184,7 @@ describe.skipIf(isWindows)("Hooks Executor", () => {
|
||||
test("LETTA_AGENT_ID is not set when agent_id is not provided", async () => {
|
||||
const hook: HookCommand = {
|
||||
type: "command",
|
||||
command: "echo \"agent_id:${LETTA_AGENT_ID:-empty}\"",
|
||||
command: 'echo "agent_id:${LETTA_AGENT_ID:-empty}"',
|
||||
};
|
||||
|
||||
const input: PreToolUseHookInput = {
|
||||
|
||||
@@ -405,12 +405,12 @@ describe.skipIf(isWindows)("Hooks Integration Tests", () => {
|
||||
expect(result.blocked).toBe(true);
|
||||
});
|
||||
|
||||
test("receives prompt and command flag in input", async () => {
|
||||
test("skips hooks for slash commands", async () => {
|
||||
createHooksConfig({
|
||||
UserPromptSubmit: [
|
||||
{
|
||||
matcher: "*",
|
||||
hooks: [{ type: "command", command: "cat" }],
|
||||
hooks: [{ type: "command", command: "echo 'should not run'" }],
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -423,9 +423,9 @@ describe.skipIf(isWindows)("Hooks Integration Tests", () => {
|
||||
tempDir,
|
||||
);
|
||||
|
||||
const parsed = JSON.parse(result.results[0]?.stdout || "{}");
|
||||
expect(parsed.prompt).toBe("/clear");
|
||||
expect(parsed.is_command).toBe(true);
|
||||
// Hooks should not run for slash commands
|
||||
expect(result.blocked).toBe(false);
|
||||
expect(result.results).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user