/** * @license * Copyright 2025 Vybestack LLC * SPDX-License-Identifier: Apache-2.0 */ /** * @plan:PLAN-20260216-HOOKSYSTEMREWRITE.P03,P06,P08 * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement:HOOK-061,HOOK-062,HOOK-143,HOOK-144,HOOK-145,HOOK-146,HOOK-147 * @requirement DELTA-HSYS-001,DELTA-HEVT-004,DELTA-HFAIL-005,DELTA-HPAY-006 * @pseudocode:analysis/pseudocode/02-hook-event-handler-flow.md */ import type { Config } from '../config/config.js'; import type { HookRegistry } from './hookRegistry.js'; import type { HookPlanner } from './hookPlanner.js'; import type { HookRunner } from './hookRunner.js'; import type { HookAggregator, AggregatedHookResult } from './hookAggregator.js'; import type { DefaultHookOutput, SessionStartSource, SessionEndReason, PreCompressTrigger } from './types.js'; import { NotificationType } from './types.js'; import { DebugLogger } from '../debug/index.js'; import type { MessageBus } from '../confirmation-bus/message-bus.js'; /** * Metadata for failure envelopes * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HFAIL-005 */ export interface FailureMeta { hookId?: string; eventName?: string; [key: string]: unknown; } /** * Processed hook result with common-output semantics. * * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HRUN-004 */ export interface ProcessedHookResult { aggregated: AggregatedHookResult; shouldStop: boolean; stopReason: string | undefined; systemMessage: string | undefined; suppressOutput: boolean; } /** * HookEventHandler coordinates hook execution for all event types. * It builds HookInput payloads, delegates to planner/runner/aggregator, * and returns typed results to callers. * * @requirement:HOOK-143 - Exposes fire*Event methods for all hook events * @requirement:HOOK-144 - Builds HookInput payloads with base fields from Config * @requirement:HOOK-145 - Returns empty success result when no hooks match * @requirement:HOOK-146 - Logs telemetry at debug level for every event fire * @requirement:HOOK-147 - Wraps fire*Event body in try/catch, never propagates exceptions */ export declare class HookEventHandler { private readonly config; private readonly planner; private readonly runner; private readonly aggregator; /** * Reserved for future pub/sub integration; injected but not yet consumed. * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HSYS-001 */ private readonly messageBus; /** * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HSYS-001 */ private readonly debugLogger; /** * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HEVT-004 */ private disposed; /** * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HEVT-004 */ private subscriptionHandle; /** * @plan PLAN-20250218-HOOKSYSTEM.P03 * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HSYS-001, DELTA-HEVT-001 * @pseudocode message-bus-integration.md lines 50-56 */ constructor(config: Config, _registry: HookRegistry, // Retained for future use, planner already has reference planner: HookPlanner, runner: HookRunner, aggregator: HookAggregator, messageBus?: MessageBus, injectedDebugLogger?: DebugLogger); /** * Build base HookInput fields from Config * @requirement:HOOK-144 * @plan PLAN-20250219-GMERGE022.B2 * @requirement R2 */ private buildBaseInput; /** * Fire BeforeTool event * @requirement:HOOK-143 */ fireBeforeToolEvent(toolName: string, toolInput: Record): Promise; /** * Fire AfterTool event * @requirement:HOOK-143 */ fireAfterToolEvent(toolName: string, toolInput: Record, toolResponse: Record): Promise; /** * Fire BeforeModel event * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireBeforeModelEvent(llmRequest: unknown): Promise; /** * Fire AfterModel event * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireAfterModelEvent(llmRequest: unknown, llmResponse: unknown): Promise; /** * Fire BeforeToolSelection event * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireBeforeToolSelectionEvent(llmRequest: unknown): Promise; /** * Fire SessionStart event * * @plan PLAN-20250218-HOOKSYSTEM.P03, PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HPAY-006, DELTA-HFAIL-001 * @requirement:HOOK-143 */ fireSessionStartEvent(context: { source: SessionStartSource; }): Promise; /** * Fire SessionEnd event * * @plan PLAN-20250218-HOOKSYSTEM.P03, PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HPAY-006, DELTA-HFAIL-001 * @requirement:HOOK-143 */ fireSessionEndEvent(context: { reason: SessionEndReason; }): Promise; /** * Fire PreCompress event * * @plan PLAN-20250219-GMERGE021.R4.P01 * @requirement REQ-P01-1 */ firePreCompressEvent(context: { trigger: PreCompressTrigger; }): Promise; /** * Fire BeforeAgent event * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireBeforeAgentEvent(context: { prompt: string; }): Promise; /** * Fire AfterAgent event * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireAfterAgentEvent(context: { prompt: string; prompt_response: string; stop_hook_active: boolean; }): Promise; /** * Fire Notification event (e.g., ToolPermission before confirmation dialog) * @requirement:HOOK-143 * @plan PLAN-20250218-HOOKSYSTEM.P12 * @requirement DELTA-HFAIL-001 */ fireNotificationEvent(type: NotificationType, message: string, details: Record): Promise; /** * Execute an event and return the final output (or undefined) * @requirement:HOOK-147 - Wraps in try/catch */ private executeEvent; /** * Execute an event and return the full aggregated result * @requirement:HOOK-145,HOOK-146 * * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HFAIL-005 */ private executeEventWithFullResult; /** * Return a copy of the empty success result for use in no-op scenarios. * * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HFAIL-005 */ makeEmptySuccessResult(): AggregatedHookResult; /** * Build a failure envelope for error reporting. * * @plan PLAN-20250218-HOOKSYSTEM.P03 * @requirement DELTA-HFAIL-005 */ buildFailureEnvelope(error: unknown, stage: string, meta?: FailureMeta): AggregatedHookResult; /** * Process common hook output fields (shouldStop, systemMessage, suppressOutput). * * @plan PLAN-20250218-HOOKSYSTEM.P12, PLAN-20250218-HOOKSYSTEM.P14 * @requirement DELTA-HRUN-001, DELTA-HRUN-004 */ private processCommonHookOutputFields; /** * Normalize a raw stopReason value to a trimmed string or undefined. * * @plan PLAN-20250218-HOOKSYSTEM.P14 * @requirement DELTA-HRUN-002 */ private normalizeStopReason; /** * Get hook name from execution result for telemetry and user feedback */ private getHookNameFromResult; /** * Emit per-hook log records via DebugLogger. * * @plan PLAN-20250218-HOOKSYSTEM.P12, PLAN-20250218-HOOKSYSTEM.P14 * @requirement DELTA-HTEL-001 * @requirement DELTA-HTEL-003 */ private emitPerHookLogs; /** * Emit batch-level summary via DebugLogger. * * @plan PLAN-20250218-HOOKSYSTEM.P12, PLAN-20250218-HOOKSYSTEM.P14 * @requirement DELTA-HTEL-002 */ private emitBatchSummary; /** * Handle incoming HOOK_EXECUTION_REQUEST from the message bus. * Routes to appropriate executeEventWithFullResult based on eventName. * * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HEVT-001, DELTA-HEVT-002 * @pseudocode message-bus-integration.md lines 60-81 */ private onBusRequest; /** * Route and execute a mediated hook request. * * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HEVT-003, DELTA-HPAY-003 * @pseudocode message-bus-integration.md lines 90-114 */ private routeAndExecuteMediated; /** * Publish a HOOK_EXECUTION_RESPONSE to the message bus. * * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HEVT-002 * @pseudocode message-bus-integration.md lines 120-123 */ private publishResponse; /** * Extract or generate correlationId from a raw message. * If absent, generates one via crypto.randomUUID(). * * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HBUS-003 * @pseudocode hook-event-handler.md lines 260-264 */ private extractCorrelationId; /** * Translate model payloads for BeforeModel/AfterModel/BeforeToolSelection. * Both mediated and direct paths use this translation. * * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HPAY-003 * @pseudocode message-bus-integration.md lines 140-161 */ private translateModelPayload; /** * Validates event payload before execution. * Returns false if payload is invalid for the given event type. * * @plan PLAN-20250218-HOOKSYSTEM.P09 * @requirement DELTA-HPAY-001 * @pseudocode validation-boundary.md lines 50-81 */ /** * Route validation to the appropriate type-predicate validator. * * @plan PLAN-20250218-HOOKSYSTEM.P11 * @requirement DELTA-HPAY-001, DELTA-HPAY-002 */ private validateEventPayload; /** * Dispose this HookEventHandler, releasing subscriptions and preventing further use. * * @plan PLAN-20250218-HOOKSYSTEM.P03 * @plan PLAN-20250218-HOOKSYSTEM.P08 * @requirement DELTA-HEVT-004 * @pseudocode message-bus-integration.md lines 130-136 */ dispose(): void; }