import type { AppContext, HookDispatcher, LifecycleHooks, MoxxyEvent, Plugin, ProviderRequest, ToolCallContext, ToolCallVerdict, ToolResultContext, ToolResultEvent, TurnContext, } from '@moxxy/sdk'; import type { Logger } from '../logger.js'; export interface DispatcherOptions { readonly hookTimeoutMs?: number; readonly logger: Logger; readonly onHookFailed?: (err: Error, plugin: string, hook: keyof LifecycleHooks) => void; } interface PluginEntry { readonly plugin: Plugin; readonly hooks: LifecycleHooks; } export class HookDispatcherImpl implements HookDispatcher { private entries: PluginEntry[] = []; private readonly timeoutMs: number; private readonly logger: Logger; private readonly onHookFailed?: DispatcherOptions['onHookFailed']; constructor(opts: DispatcherOptions) { this.timeoutMs = opts.hookTimeoutMs ?? 5_000; this.logger = opts.logger; this.onHookFailed = opts.onHookFailed; } setPlugins(plugins: ReadonlyArray): void { this.entries = plugins.map((plugin) => ({ plugin, hooks: plugin.hooks ?? {} })); } /** * Whether any registered plugin declares an `onEvent` hook. Lets the * per-event fan-out skip building an AppContext (which clones the whole * `process.env`) when no plugin would consume it — the common case. */ hasEventHooks(): boolean { return this.entries.some((e) => typeof e.hooks.onEvent === 'function'); } async dispatchInit(ctx: AppContext): Promise { for (const e of this.entries) await this.safe(e, 'onInit', () => e.hooks.onInit?.(ctx)); } async dispatchTurnStart(ctx: TurnContext): Promise { for (const e of this.entries) await this.safe(e, 'onTurnStart', () => e.hooks.onTurnStart?.(ctx)); } async dispatchBeforeProviderCall(req: ProviderRequest, ctx: TurnContext): Promise { let current = req; for (const e of this.entries) { const next = await this.safe(e, 'onBeforeProviderCall', () => e.hooks.onBeforeProviderCall?.(current, ctx), ); if (next) current = next; } return current; } async dispatchToolCall(ctx: ToolCallContext): Promise { let verdict: ToolCallVerdict = { action: 'allow' }; for (const e of this.entries) { const next = await this.safe(e, 'onToolCall', () => e.hooks.onToolCall?.(ctx)); if (!next) continue; if (next.action === 'deny') return next; if (next.action === 'rewrite') verdict = next; } return verdict; } async dispatchToolResult(ctx: ToolResultContext): Promise { let result = ctx.result; for (const e of this.entries) { const next = await this.safe(e, 'onToolResult', () => e.hooks.onToolResult?.({ ...ctx, result }), ); if (next) result = next; } return result; } async dispatchEvent(event: MoxxyEvent, ctx: AppContext): Promise { for (const e of this.entries) { await this.safe(e, 'onEvent', () => e.hooks.onEvent?.(event, ctx)); } } async dispatchTurnEnd(ctx: TurnContext): Promise { for (const e of this.entries) await this.safe(e, 'onTurnEnd', () => e.hooks.onTurnEnd?.(ctx)); } async dispatchShutdown(ctx: AppContext): Promise { for (const e of this.entries) await this.safe(e, 'onShutdown', () => e.hooks.onShutdown?.(ctx)); } private async safe( entry: PluginEntry, hook: keyof LifecycleHooks, fn: () => T | Promise | void | Promise, ): Promise { let timer: ReturnType | undefined; try { const p = Promise.resolve(fn() as Promise); // If the timeout wins the race, `p` is still pending and unobserved; a // slow hook that later rejects would surface as an unhandledRejection // (process-level crash/log spam in strict configs / the desktop main). // Attach a no-op handler so a post-timeout rejection is swallowed too. // (Promise.race still settles on the original `p`, so this doesn't mask // the in-time error path below — that one is observed via the race.) p.catch(() => undefined); const timeout = new Promise((_, reject) => { timer = setTimeout( () => reject(new Error(`Hook ${hook} on ${entry.plugin.name} timed out`)), this.timeoutMs, ); }); // Attach a no-op rejection handler to the timeout promise so if the // fast path wins, the timer's eventual rejection is swallowed // silently rather than surfacing as an unhandled rejection. timeout.catch(() => undefined); const winner = await Promise.race([p, timeout]); return winner as T | undefined; } catch (err) { const error = err instanceof Error ? err : new Error(String(err)); this.logger.warn('hook failed', { plugin: entry.plugin.name, hook, err: error.message }); this.onHookFailed?.(error, entry.plugin.name, hook); return undefined; } finally { if (timer !== undefined) clearTimeout(timer); } } }