Merge branch 'main' into adibakm/clear-context-conversation-approval

This commit is contained in:
Adib234
2026-03-11 11:46:54 -04:00
committed by GitHub
13 changed files with 301 additions and 39 deletions

View File

@@ -204,7 +204,8 @@ const SETTINGS_SCHEMA = {
description: oneLine`
The default approval mode for tool execution.
'default' prompts for approval, 'auto_edit' auto-approves edit tools,
and 'plan' is read-only mode. 'yolo' is not supported yet.
and 'plan' is read-only mode. YOLO mode (auto-approve all actions) can
only be enabled via command line (--yolo or --approval-mode=yolo).
`,
showInDialog: true,
options: [

View File

@@ -17,6 +17,7 @@ vi.mock('@google/gemini-cli-core', async () => {
...actual,
uiTelemetryService: {
setLastPromptTokenCount: vi.fn(),
clear: vi.fn(),
},
};
});
@@ -74,17 +75,16 @@ describe('clearCommand', () => {
expect(mockResetChat).toHaveBeenCalledTimes(1);
expect(mockHintClear).toHaveBeenCalledTimes(1);
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
expect(uiTelemetryService.clear).toHaveBeenCalled();
expect(uiTelemetryService.clear).toHaveBeenCalledTimes(1);
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
// Check the order of operations.
const setDebugMessageOrder = (mockContext.ui.setDebugMessage as Mock).mock
.invocationCallOrder[0];
const resetChatOrder = mockResetChat.mock.invocationCallOrder[0];
const resetTelemetryOrder = (
uiTelemetryService.setLastPromptTokenCount as Mock
).mock.invocationCallOrder[0];
const resetTelemetryOrder = (uiTelemetryService.clear as Mock).mock
.invocationCallOrder[0];
const clearOrder = (mockContext.ui.clear as Mock).mock
.invocationCallOrder[0];
@@ -110,8 +110,8 @@ describe('clearCommand', () => {
'Clearing terminal.',
);
expect(mockResetChat).not.toHaveBeenCalled();
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
expect(uiTelemetryService.clear).toHaveBeenCalled();
expect(uiTelemetryService.clear).toHaveBeenCalledTimes(1);
expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
});
});

View File

@@ -23,10 +23,6 @@ export const clearCommand: SlashCommand = {
action: async (context, _args) => {
const geminiClient = context.services.config?.getGeminiClient();
const config = context.services.config;
const chatRecordingService = context.services.config
?.getGeminiClient()
?.getChat()
.getChatRecordingService();
// Fire SessionEnd hook before clearing
const hookSystem = config?.getHookSystem();
@@ -34,6 +30,18 @@ export const clearCommand: SlashCommand = {
await hookSystem.fireSessionEndEvent(SessionEndReason.Clear);
}
// Reset user steering hints
config?.userHintService.clear();
// Start a new conversation recording with a new session ID
// We MUST do this before calling resetChat() so the new ChatRecordingService
// initialized by GeminiChat picks up the new session ID.
let newSessionId: string | undefined;
if (config) {
newSessionId = randomUUID();
config.setSessionId(newSessionId);
}
if (geminiClient) {
context.ui.setDebugMessage('Clearing terminal and resetting chat.');
// If resetChat fails, the exception will propagate and halt the command,
@@ -43,16 +51,6 @@ export const clearCommand: SlashCommand = {
context.ui.setDebugMessage('Clearing terminal.');
}
// Reset user steering hints
config?.userHintService.clear();
// Start a new conversation recording with a new session ID
if (config && chatRecordingService) {
const newSessionId = randomUUID();
config.setSessionId(newSessionId);
chatRecordingService.initialize();
}
// Fire SessionStart hook after clearing
let result;
if (hookSystem) {
@@ -69,7 +67,7 @@ export const clearCommand: SlashCommand = {
await flushTelemetry(config);
}
uiTelemetryService.setLastPromptTokenCount(0);
uiTelemetryService.clear(newSessionId);
context.ui.clear();
if (result?.systemMessage) {

View File

@@ -238,6 +238,34 @@ describe('SessionStatsContext', () => {
unmount();
});
it('should update session ID and reset stats when the uiTelemetryService emits a clear event', () => {
const contextRef: MutableRefObject<
ReturnType<typeof useSessionStats> | undefined
> = { current: undefined };
const { unmount } = render(
<SessionStatsProvider>
<TestHarness contextRef={contextRef} />
</SessionStatsProvider>,
);
const initialStartTime = contextRef.current?.stats.sessionStartTime;
const newSessionId = 'new-session-id';
act(() => {
uiTelemetryService.emit('clear', newSessionId);
});
const stats = contextRef.current?.stats;
expect(stats?.sessionId).toBe(newSessionId);
expect(stats?.promptCount).toBe(0);
expect(stats?.sessionStartTime.getTime()).toBeGreaterThanOrEqual(
initialStartTime!.getTime(),
);
unmount();
});
it('should throw an error when useSessionStats is used outside of a provider', () => {
const onError = vi.fn();
// Suppress console.error from React for this test

View File

@@ -216,7 +216,17 @@ export const SessionStatsProvider: React.FC<{ children: React.ReactNode }> = ({
});
};
const handleClear = (newSessionId?: string) => {
setStats((prevState) => ({
...prevState,
sessionId: newSessionId || prevState.sessionId,
sessionStartTime: new Date(),
promptCount: 0,
}));
};
uiTelemetryService.on('update', handleUpdate);
uiTelemetryService.on('clear', handleClear);
// Set initial state
handleUpdate({
metrics: uiTelemetryService.getMetrics(),
@@ -225,6 +235,7 @@ export const SessionStatsProvider: React.FC<{ children: React.ReactNode }> = ({
return () => {
uiTelemetryService.off('update', handleUpdate);
uiTelemetryService.off('clear', handleClear);
};
}, []);

View File

@@ -23,6 +23,7 @@ import {
import {
coreEvents,
convertSessionToClientHistory,
uiTelemetryService,
} from '@google/gemini-cli-core';
// Mock modules
@@ -36,6 +37,17 @@ vi.mock('../../utils/sessionUtils.js', async (importOriginal) => {
getSessionFiles: vi.fn(),
};
});
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
uiTelemetryService: {
clear: vi.fn(),
hydrate: vi.fn(),
},
};
});
const MOCKED_PROJECT_TEMP_DIR = '/test/project/temp';
const MOCKED_CHATS_DIR = '/test/project/temp/chats';
@@ -102,6 +114,7 @@ describe('useSessionBrowser', () => {
expect(mockConfig.setSessionId).toHaveBeenCalledWith(
'existing-session-456',
);
expect(uiTelemetryService.hydrate).toHaveBeenCalledWith(mockConversation);
expect(result.current.isSessionBrowserOpen).toBe(false);
expect(mockOnLoadHistory).toHaveBeenCalled();
});

View File

@@ -16,6 +16,7 @@ import type {
import {
coreEvents,
convertSessionToClientHistory,
uiTelemetryService,
} from '@google/gemini-cli-core';
import type { SessionInfo } from '../../utils/sessionUtils.js';
import { convertSessionToHistoryFormats } from '../../utils/sessionUtils.js';
@@ -68,6 +69,7 @@ export const useSessionBrowser = (
// Use the old session's ID to continue it.
const existingSessionId = conversation.sessionId;
config.setSessionId(existingSessionId);
uiTelemetryService.hydrate(conversation);
const resumedSessionData = {
conversation,