feat: add prompt based hooks (#795)
Co-authored-by: Letta <noreply@letta.com>
This commit is contained in:
@@ -8,9 +8,11 @@ import {
|
||||
type HookEvent,
|
||||
type HookMatcher,
|
||||
type HooksConfig,
|
||||
isPromptHook,
|
||||
isToolEvent,
|
||||
type SimpleHookEvent,
|
||||
type SimpleHookMatcher,
|
||||
supportsPromptHooks,
|
||||
type ToolHookEvent,
|
||||
} from "./types";
|
||||
|
||||
@@ -177,6 +179,33 @@ export function matchesTool(pattern: string, toolName: string): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter hooks, removing prompt hooks from unsupported events with a warning
|
||||
*/
|
||||
function filterHooksForEvent(
|
||||
hooks: HookCommand[],
|
||||
event: HookEvent,
|
||||
): HookCommand[] {
|
||||
const filtered: HookCommand[] = [];
|
||||
const promptHooksSupported = supportsPromptHooks(event);
|
||||
|
||||
for (const hook of hooks) {
|
||||
if (isPromptHook(hook)) {
|
||||
if (!promptHooksSupported) {
|
||||
// Warn about unsupported prompt hook
|
||||
console.warn(
|
||||
`\x1b[33m[hooks] Warning: Prompt hooks are not supported for the ${event} event. ` +
|
||||
`Ignoring prompt hook.\x1b[0m`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
filtered.push(hook);
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all hooks that match a specific event and tool name
|
||||
*/
|
||||
@@ -200,7 +229,7 @@ export function getMatchingHooks(
|
||||
hooks.push(...matcher.hooks);
|
||||
}
|
||||
}
|
||||
return hooks;
|
||||
return filterHooksForEvent(hooks, event);
|
||||
} else {
|
||||
// Simple events use SimpleHookMatcher[] - extract hooks from each matcher
|
||||
const matchers = config[event as SimpleHookEvent] as
|
||||
@@ -214,7 +243,7 @@ export function getMatchingHooks(
|
||||
for (const matcher of matchers) {
|
||||
hooks.push(...matcher.hooks);
|
||||
}
|
||||
return hooks;
|
||||
return filterHooksForEvent(hooks, event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user