tests — hooks

Module: tests-hooks Cohesion: 0.80 Members: 0

tests — hooks

This documentation covers the various hook systems and related input handling features within the application, as revealed by their respective test suites. These modules provide extensibility points, control mechanisms, and specialized functionalities for managing application lifecycle events, AI agent behavior, and user interactions.

1. Advanced Hooks System (src/hooks/advanced-hooks.ts)

The Advanced Hooks system provides a granular, event-driven mechanism for intercepting and influencing core application actions. It allows for custom logic to be executed at specific points, enabling decisions (allow, deny, ask) and even modification of input parameters.

Purpose

To offer fine-grained control over critical application events, such as tool usage, bash command execution, and session lifecycle, allowing for custom validation, logging, or modification of behavior.

Key Concepts

    enum HookEvent {
      PreToolUse = 'PreToolUse',
      PostToolUse = 'PostToolUse',
      PreBash = 'PreBash',
      PostBash = 'PostBash',
      PreEdit = 'PreEdit',
      PostEdit = 'PostEdit',
      SessionStart = 'SessionStart',
      SessionEnd = 'SessionEnd',
      PreCompact = 'PreCompact',
      Notification = 'Notification',
      SubagentStart = 'SubagentStart',
      SubagentStop = 'SubagentStop',
      PermissionRequest = 'PermissionRequest',
      TaskCompleted = 'TaskCompleted',
      ConfigChange = 'ConfigChange',
    }

Core Components

HookRegistry

Manages the registration, storage, and retrieval of AdvancedHook instances.

AdvancedHookRunner

Responsible for executing individual AdvancedHook instances and interpreting their results.

Singleton Access

The system provides singleton helpers to ensure consistent access to the HookRegistry and AdvancedHookRunner instances across the application.

Execution Flow

The Advanced Hooks system integrates into the application's event pipeline.

graph TD
    A[Application Event Triggered] --> B{getHooksForEvent(event, toolName)};
    B --> C{For each matching AdvancedHook};
    C --> D[AdvancedHookRunner.runHook(hook, context)];
    D --> E{Hook Type?};
    E -- "command" --> F[Execute Subprocess];
    E -- "prompt/agent" --> G[Evaluate AI Logic];
    F --> H{Parse Subprocess Output};
    G --> H;
    H --> I[Return HookDecision];
    I --> J{Application Action};
    J -- "allow" --> K[Continue Operation];
    J -- "deny" --> L[Abort Operation];
    J -- "ask" --> M[Request User Permission];
    J -- "updatedInput" --> N[Modify Input & Continue];

2. Lifecycle Hooks System (src/hooks/lifecycle-hooks.ts)

The Lifecycle Hooks system provides a structured way to define and execute hooks at predefined stages of the application's operational flow, such as before/after edits or bash commands.

Purpose

To enable custom actions (e.g., linting, formatting, testing, logging) to be automatically performed at specific points in the application's workflow, enhancing automation and developer experience.

Key Concepts

    type HookType =
      | 'pre-edit'
      | 'post-edit'
      | 'pre-bash'
      | 'post-bash'
      | 'pre-commit'
      | 'post-commit'
      | 'pre-prompt'
      | 'post-response'
      | 'on-error'
      | 'on-tool-call';

Core Component

HooksManager

The central component for managing and executing lifecycle hooks.

Built-in Hooks

The system includes a set of predefined hooks (BUILTIN_HOOKS) that can be enabled or disabled:

Singleton Access

3. Moltbot-Inspired Features (src/hooks/moltbot-hooks.ts)

This module provides a suite of features inspired by Moltbot, focusing on AI agent configuration, session management, and command logging. It acts as an orchestrator for several sub-managers.

Purpose

To enhance the AI agent's capabilities by providing persistent context (intro hooks), session memory, and detailed logging of interactions and actions.

Core Component

MoltbotHooksManager

The main manager that composes and coordinates the IntroHookManager, SessionPersistenceManager, and CommandLogger.

Sub-Managers

graph TD
    A[MoltbotHooksManager] --> B[IntroHookManager];
    A --> C[SessionPersistenceManager];
    A --> D[CommandLogger];

IntroHookManager

Manages the "intro" content provided to the AI agent, which typically defines its role, rules, and context.

SessionPersistenceManager

Handles the storage and retrieval of chat session history, including user messages and tool calls.

CommandLogger

Records detailed logs of commands executed by the AI agent, including tool calls, bash commands, and file edits.

Setup Utilities

A set of helper functions simplify the initial setup and management of Moltbot-inspired features within a project.

Singleton Access

4. Input Handling Features (Inferred from tests/hooks/input-handler.test.ts)

This module provides utilities for processing user input, specifically for persistent instructions and UI interaction.

Purpose

To enhance user interaction by allowing persistent instructions and enabling special UI actions based on input patterns.

Key Features

# Instruction Capture

Allows users to define persistent instructions for the AI agent directly from the input prompt.

Double Escape Detection

A mechanism to detect rapid consecutive escape key presses, typically used to trigger special UI actions like editing the previous prompt.

Get Last User Message

A utility function to retrieve the most recent user-provided message from a chat history.

Relationship and Integration

These hook systems and input handling features work together to provide a robust and extensible AI agent experience:

Together, these modules create a powerful and flexible framework for building intelligent agents that are both controllable and adaptable to user and project requirements.