feat(hooks): Hook Session Lifecycle & Compression Integration (#14151)

This commit is contained in:
Edilmo Palencia
2025-12-03 09:04:13 -08:00
committed by GitHub
parent 7a6d3067c6
commit 1c12da1fad
27 changed files with 1026 additions and 302 deletions

View File

@@ -4,7 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { uiTelemetryService } from '@google/gemini-cli-core';
import {
uiTelemetryService,
fireSessionEndHook,
fireSessionStartHook,
SessionEndReason,
SessionStartSource,
flushTelemetry,
} from '@google/gemini-cli-core';
import type { SlashCommand } from './types.js';
import { CommandKind } from './types.js';
import { randomUUID } from 'node:crypto';
@@ -21,6 +28,12 @@ export const clearCommand: SlashCommand = {
?.getGeminiClient()
?.getChat()
.getChatRecordingService();
const messageBus = config?.getMessageBus();
// Fire SessionEnd hook before clearing
if (config?.getEnableHooks() && messageBus) {
await fireSessionEndHook(messageBus, SessionEndReason.Clear);
}
if (geminiClient) {
context.ui.setDebugMessage('Clearing terminal and resetting chat.');
@@ -38,6 +51,21 @@ export const clearCommand: SlashCommand = {
chatRecordingService.initialize();
}
// Fire SessionStart hook after clearing
if (config?.getEnableHooks() && messageBus) {
await fireSessionStartHook(messageBus, SessionStartSource.Clear);
}
// Give the event loop a chance to process any pending telemetry operations
// This ensures logger.emit() calls have fully propagated to the BatchLogRecordProcessor
await new Promise((resolve) => setImmediate(resolve));
// Flush telemetry to ensure hooks are written to disk immediately
// This is critical for tests and environments with I/O latency
if (config) {
await flushTelemetry(config);
}
uiTelemetryService.setLastPromptTokenCount(0);
context.ui.clear();
},