mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-29 06:25:16 -07:00
feat(hooks): Hook Session Lifecycle & Compression Integration (#14151)
This commit is contained in:
@@ -31,6 +31,7 @@ import {
|
||||
registerCleanup,
|
||||
registerSyncCleanup,
|
||||
runExitCleanup,
|
||||
registerTelemetryConfig,
|
||||
} from './utils/cleanup.js';
|
||||
import { getCliVersion } from './utils/version.js';
|
||||
import {
|
||||
@@ -58,6 +59,10 @@ import {
|
||||
shouldEnterAlternateScreen,
|
||||
startupProfiler,
|
||||
ExitCodes,
|
||||
SessionStartSource,
|
||||
SessionEndReason,
|
||||
fireSessionStartHook,
|
||||
fireSessionEndHook,
|
||||
} from '@google/gemini-cli-core';
|
||||
import {
|
||||
initializeApp,
|
||||
@@ -459,10 +464,22 @@ export async function main() {
|
||||
const config = await loadCliConfig(settings.merged, sessionId, argv);
|
||||
loadConfigHandle?.end();
|
||||
|
||||
// Register config for telemetry shutdown
|
||||
// This ensures telemetry (including SessionEnd hooks) is properly flushed on exit
|
||||
registerTelemetryConfig(config);
|
||||
|
||||
const policyEngine = config.getPolicyEngine();
|
||||
const messageBus = config.getMessageBus();
|
||||
createPolicyUpdater(policyEngine, messageBus);
|
||||
|
||||
// Register SessionEnd hook to fire on graceful exit
|
||||
// This runs before telemetry shutdown in runExitCleanup()
|
||||
if (config.getEnableHooks() && messageBus) {
|
||||
registerCleanup(async () => {
|
||||
await fireSessionEndHook(messageBus, SessionEndReason.Exit);
|
||||
});
|
||||
}
|
||||
|
||||
// Cleanup sessions after config initialization
|
||||
try {
|
||||
await cleanupExpiredSessions(config, settings.merged);
|
||||
@@ -586,6 +603,22 @@ export async function main() {
|
||||
await config.initialize();
|
||||
startupProfiler.flush(config);
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
// If not a TTY, read from stdin
|
||||
// This is for cases where the user pipes input directly into the command
|
||||
if (!process.stdin.isTTY) {
|
||||
|
||||
Reference in New Issue
Block a user