@@ -12,16 +12,31 @@ import type { PluginHookGatewayContext, PluginHookGatewayStopEvent } from "./typ
1212
1313const log = createSubsystemLogger ( "plugins" ) ;
1414
15- let globalHookRunner : HookRunner | null = null ;
16- let globalRegistry : PluginRegistry | null = null ;
15+ type HookRunnerGlobalState = {
16+ hookRunner : HookRunner | null ;
17+ registry : PluginRegistry | null ;
18+ } ;
19+
20+ const hookRunnerGlobalStateKey = Symbol . for ( "openclaw.plugins.hook-runner-global-state" ) ;
21+
22+ function getHookRunnerGlobalState ( ) : HookRunnerGlobalState {
23+ const globalStore = globalThis as typeof globalThis & {
24+ [ hookRunnerGlobalStateKey ] ?: HookRunnerGlobalState ;
25+ } ;
26+ return ( globalStore [ hookRunnerGlobalStateKey ] ??= {
27+ hookRunner : null ,
28+ registry : null ,
29+ } ) ;
30+ }
1731
1832/**
1933 * Initialize the global hook runner with a plugin registry.
2034 * Called once when plugins are loaded during gateway startup.
2135 */
2236export function initializeGlobalHookRunner ( registry : PluginRegistry ) : void {
23- globalRegistry = registry ;
24- globalHookRunner = createHookRunner ( registry , {
37+ const state = getHookRunnerGlobalState ( ) ;
38+ state . registry = registry ;
39+ state . hookRunner = createHookRunner ( registry , {
2540 logger : {
2641 debug : ( msg ) => log . debug ( msg ) ,
2742 warn : ( msg ) => log . warn ( msg ) ,
@@ -41,22 +56,22 @@ export function initializeGlobalHookRunner(registry: PluginRegistry): void {
4156 * Returns null if plugins haven't been loaded yet.
4257 */
4358export function getGlobalHookRunner ( ) : HookRunner | null {
44- return globalHookRunner ;
59+ return getHookRunnerGlobalState ( ) . hookRunner ;
4560}
4661
4762/**
4863 * Get the global plugin registry.
4964 * Returns null if plugins haven't been loaded yet.
5065 */
5166export function getGlobalPluginRegistry ( ) : PluginRegistry | null {
52- return globalRegistry ;
67+ return getHookRunnerGlobalState ( ) . registry ;
5368}
5469
5570/**
5671 * Check if any hooks are registered for a given hook name.
5772 */
5873export function hasGlobalHooks ( hookName : Parameters < HookRunner [ "hasHooks" ] > [ 0 ] ) : boolean {
59- return globalHookRunner ?. hasHooks ( hookName ) ?? false ;
74+ return getHookRunnerGlobalState ( ) . hookRunner ?. hasHooks ( hookName ) ?? false ;
6075}
6176
6277export async function runGlobalGatewayStopSafely ( params : {
@@ -83,6 +98,7 @@ export async function runGlobalGatewayStopSafely(params: {
8398 * Reset the global hook runner (for testing).
8499 */
85100export function resetGlobalHookRunner ( ) : void {
86- globalHookRunner = null ;
87- globalRegistry = null ;
101+ const state = getHookRunnerGlobalState ( ) ;
102+ state . hookRunner = null ;
103+ state . registry = null ;
88104}
0 commit comments