gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Introduce HookRunner to shrink hook footprint in other code

Open scidomino opened this issue 3 weeks ago • 0 comments

A follow up on https://github.com/google-gemini/gemini-cli/pull/14151 I'd like to see a refactor to reduce how much boiler plate we have in the classes where hooks are executed.

For example, our current code has:

 // Fire SessionStart hook through MessageBus (only if hooks are enabled)
    // Must be called AFTER config.initialize() to ensure HookRegistry is loaded
    const hooksEnabled = config.getEnableHooks();
    const hookMessageBus = config.getMessageBus();
    if (hooksEnabled && hookMessageBus) {
      const sessionStartSource = resumedSessionData
        ? SessionStartSource.Resume
        : SessionStartSource.Startup;
      await fireSessionStartHook(hookMessageBus, sessionStartSource);

      // Register SessionEnd hook for graceful exit
      registerCleanup(async () => {
        await fireSessionEndHook(hookMessageBus, SessionEndReason.Exit);
      });
    }

I'd like to see something like:

      await config.getHookRunner().fireSessionStart(resumedSessionData);
      registerCleanup(async () => {
        await config.getHookRunner().fireEnd(SessionEndReason.Exit);
      });
    }

or maybe

      if(resumedSessionData) {
        await config.getHookRunner().fireSessionResume();
      } else {
        await config.getHookRunner().fireSessionStart();
      }
      registerCleanup(async () =>  await config.getHookRunner().fireSessionExit());
    }

If hooks are disabled, config.getHookRunner() would just return a no-op runner.

scidomino avatar Dec 01 '25 19:12 scidomino