Class DeclarativeHookRegistry

java.lang.Object
com.tnsai.server.hooks.DeclarativeHookRegistry

public final class DeclarativeHookRegistry extends Object
Parses declarative hook definitions from YAML and registers them as runtime listeners on agents.

YAML schema


 hooks:
   - event: preToolUse
     matcher: "file_*"
     action:
       type: log
       message: "Tool ${tool} is about to execute"

   - event: postToolUse
     matcher: "shell"
     action:
       type: command
       command: "echo 'shell tool finished'"

   - event: onStart
     action:
       type: log
       message: "Agent started"
 
  • Method Details

    • fromYaml

      public static DeclarativeHookRegistry fromYaml(InputStream yamlInput)
      Parse hooks from a YAML input stream.
      Parameters:
      yamlInput - YAML content containing a hooks array
      Returns:
      a registry with the parsed hooks
    • fromYaml

      public static DeclarativeHookRegistry fromYaml(InputStream yamlInput, HookExecutor executor)
      Parse hooks from a YAML input stream with a custom executor.
    • of

      public static DeclarativeHookRegistry of(List<HookConfig> hooks)
      Create a registry from pre-built hook configs (for programmatic/test use).
    • of

      public static DeclarativeHookRegistry of(List<HookConfig> hooks, HookExecutor executor)
      Create a registry from pre-built hook configs with a custom executor.
    • applyTo

      public void applyTo(com.tnsai.agents.Agent agent)
      Register this registry's hooks as a ToolCallListener on the agent. Also fires any onStart hooks immediately.
    • fireEvent

      public void fireEvent(HookEvent event, Map<String,String> context)
      Fire hooks for the given event with context variables.
    • fireToolEvent

      public void fireToolEvent(HookEvent event, String toolName, Map<String,String> extraContext)
      Fire hooks for a tool-specific event.
    • getHooks

      public List<HookConfig> getHooks()
      Returns an unmodifiable view of all registered hooks.
    • getHooksForEvent

      public List<HookConfig> getHooksForEvent(HookEvent event)
      Returns hooks registered for a specific event.