Download src/plugins/hook-runner-global.ts from shimen/shinclaw: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/spaces/shimen/shinclaw/resolve/main/src/plugins/hook-runner-global.ts
- Command line
-
hf download hf://spaces/shimen/shinclaw/src/plugins/hook-runner-global.ts
-
curl -L -o hook-runner-global.ts https://huggingface.co/spaces/shimen/shinclaw/resolve/main/src/plugins/hook-runner-global.ts
2.44 kB
| /** | |
| * Global Plugin Hook Runner | |
| * | |
| * Singleton hook runner that's initialized when plugins are loaded | |
| * and can be called from anywhere in the codebase. | |
| */ | |
| import type { PluginRegistry } from "./registry.js"; | |
| import type { PluginHookGatewayContext, PluginHookGatewayStopEvent } from "./types.js"; | |
| import { createSubsystemLogger } from "../logging/subsystem.js"; | |
| import { createHookRunner, type HookRunner } from "./hooks.js"; | |
| const log = createSubsystemLogger("plugins"); | |
| let globalHookRunner: HookRunner | null = null; | |
| let globalRegistry: PluginRegistry | null = null; | |
| /** | |
| * Initialize the global hook runner with a plugin registry. | |
| * Called once when plugins are loaded during gateway startup. | |
| */ | |
| export function initializeGlobalHookRunner(registry: PluginRegistry): void { | |
| globalRegistry = registry; | |
| globalHookRunner = createHookRunner(registry, { | |
| logger: { | |
| debug: (msg) => log.debug(msg), | |
| warn: (msg) => log.warn(msg), | |
| error: (msg) => log.error(msg), | |
| }, | |
| catchErrors: true, | |
| }); | |
| const hookCount = registry.hooks.length; | |
| if (hookCount > 0) { | |
| log.info(`hook runner initialized with ${hookCount} registered hooks`); | |
| } | |
| } | |
| /** | |
| * Get the global hook runner. | |
| * Returns null if plugins haven't been loaded yet. | |
| */ | |
| export function getGlobalHookRunner(): HookRunner | null { | |
| return globalHookRunner; | |
| } | |
| /** | |
| * Get the global plugin registry. | |
| * Returns null if plugins haven't been loaded yet. | |
| */ | |
| export function getGlobalPluginRegistry(): PluginRegistry | null { | |
| return globalRegistry; | |
| } | |
| /** | |
| * Check if any hooks are registered for a given hook name. | |
| */ | |
| export function hasGlobalHooks(hookName: Parameters<HookRunner["hasHooks"]>[0]): boolean { | |
| return globalHookRunner?.hasHooks(hookName) ?? false; | |
| } | |
| export async function runGlobalGatewayStopSafely(params: { | |
| event: PluginHookGatewayStopEvent; | |
| ctx: PluginHookGatewayContext; | |
| onError?: (err: unknown) => void; | |
| }): Promise<void> { | |
| const hookRunner = getGlobalHookRunner(); | |
| if (!hookRunner?.hasHooks("gateway_stop")) { | |
| return; | |
| } | |
| try { | |
| await hookRunner.runGatewayStop(params.event, params.ctx); | |
| } catch (err) { | |
| if (params.onError) { | |
| params.onError(err); | |
| return; | |
| } | |
| log.warn(`gateway_stop hook failed: ${String(err)}`); | |
| } | |
| } | |
| /** | |
| * Reset the global hook runner (for testing). | |
| */ | |
| export function resetGlobalHookRunner(): void { | |
| globalHookRunner = null; | |
| globalRegistry = null; | |
| } | |