mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-03 00:14:28 -07:00
Merge branch 'main' into adibakm/clear-context-conversation-approval
This commit is contained in:
+13
-13
@@ -22,20 +22,20 @@ they appear in the UI.
|
|||||||
|
|
||||||
### General
|
### General
|
||||||
|
|
||||||
| UI Label | Setting | Description | Default |
|
| UI Label | Setting | Description | Default |
|
||||||
| ------------------------------ | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------- |
|
| ----------------------- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
|
||||||
| Vim Mode | `general.vimMode` | Enable Vim keybindings | `false` |
|
| Vim Mode | `general.vimMode` | Enable Vim keybindings | `false` |
|
||||||
| Default Approval Mode | `general.defaultApprovalMode` | 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. | `"default"` |
|
| Default Approval Mode | `general.defaultApprovalMode` | The default approval mode for tool execution. 'default' prompts for approval, 'auto_edit' auto-approves edit tools, and 'plan' is read-only mode. YOLO mode (auto-approve all actions) can only be enabled via command line (--yolo or --approval-mode=yolo). | `"default"` |
|
||||||
| Enable Auto Update | `general.enableAutoUpdate` | Enable automatic updates. | `true` |
|
| Enable Auto Update | `general.enableAutoUpdate` | Enable automatic updates. | `true` |
|
||||||
| Enable Notifications | `general.enableNotifications` | Enable run-event notifications for action-required prompts and session completion. Currently macOS only. | `false` |
|
| Enable Notifications | `general.enableNotifications` | Enable run-event notifications for action-required prompts and session completion. Currently macOS only. | `false` |
|
||||||
| Plan Directory | `general.plan.directory` | The directory where planning artifacts are stored. If not specified, defaults to the system temporary directory. | `undefined` |
|
| Plan Directory | `general.plan.directory` | The directory where planning artifacts are stored. If not specified, defaults to the system temporary directory. | `undefined` |
|
||||||
| Plan Model Routing | `general.plan.modelRouting` | Automatically switch between Pro and Flash models based on Plan Mode status. Uses Pro for the planning phase and Flash for the implementation phase. | `true` |
|
| Plan Model Routing | `general.plan.modelRouting` | Automatically switch between Pro and Flash models based on Plan Mode status. Uses Pro for the planning phase and Flash for the implementation phase. | `true` |
|
||||||
| Clear Context on Plan Approval | `general.plan.clearContextOnApproval` | Automatically clear conversation context after a plan is approved and implementation begins. | `undefined` |
|
| Clear Context on Plan Approval | `general.plan.clearContextOnApproval` | Automatically clear conversation context after a plan is approved and implementation begins. | `undefined` |
|
||||||
| Retry Fetch Errors | `general.retryFetchErrors` | Retry on "exception TypeError: fetch failed sending request" errors. | `true` |
|
| Retry Fetch Errors | `general.retryFetchErrors` | Retry on "exception TypeError: fetch failed sending request" errors. | `true` |
|
||||||
| Max Chat Model Attempts | `general.maxAttempts` | Maximum number of attempts for requests to the main chat model. Cannot exceed 10. | `10` |
|
| Max Chat Model Attempts | `general.maxAttempts` | Maximum number of attempts for requests to the main chat model. Cannot exceed 10. | `10` |
|
||||||
| Debug Keystroke Logging | `general.debugKeystrokeLogging` | Enable debug logging of keystrokes to the console. | `false` |
|
| Debug Keystroke Logging | `general.debugKeystrokeLogging` | Enable debug logging of keystrokes to the console. | `false` |
|
||||||
| Enable Session Cleanup | `general.sessionRetention.enabled` | Enable automatic session cleanup | `true` |
|
| Enable Session Cleanup | `general.sessionRetention.enabled` | Enable automatic session cleanup | `true` |
|
||||||
| Keep chat history | `general.sessionRetention.maxAge` | Automatically delete chats older than this time period (e.g., "30d", "7d", "24h", "1w") | `"30d"` |
|
| Keep chat history | `general.sessionRetention.maxAge` | Automatically delete chats older than this time period (e.g., "30d", "7d", "24h", "1w") | `"30d"` |
|
||||||
|
|
||||||
### Output
|
### Output
|
||||||
|
|
||||||
|
|||||||
@@ -105,7 +105,8 @@ their corresponding top-level category object in your `settings.json` file.
|
|||||||
- **`general.defaultApprovalMode`** (enum):
|
- **`general.defaultApprovalMode`** (enum):
|
||||||
- **Description:** The default approval mode for tool execution. 'default'
|
- **Description:** The default approval mode for tool execution. 'default'
|
||||||
prompts for approval, 'auto_edit' auto-approves edit tools, and 'plan' is
|
prompts for approval, 'auto_edit' auto-approves edit tools, and 'plan' is
|
||||||
read-only mode. 'yolo' is not supported yet.
|
read-only mode. YOLO mode (auto-approve all actions) can only be enabled via
|
||||||
|
command line (--yolo or --approval-mode=yolo).
|
||||||
- **Default:** `"default"`
|
- **Default:** `"default"`
|
||||||
- **Values:** `"default"`, `"auto_edit"`, `"plan"`
|
- **Values:** `"default"`, `"auto_edit"`, `"plan"`
|
||||||
|
|
||||||
|
|||||||
@@ -204,7 +204,8 @@ const SETTINGS_SCHEMA = {
|
|||||||
description: oneLine`
|
description: oneLine`
|
||||||
The default approval mode for tool execution.
|
The default approval mode for tool execution.
|
||||||
'default' prompts for approval, 'auto_edit' auto-approves edit tools,
|
'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,
|
showInDialog: true,
|
||||||
options: [
|
options: [
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ vi.mock('@google/gemini-cli-core', async () => {
|
|||||||
...actual,
|
...actual,
|
||||||
uiTelemetryService: {
|
uiTelemetryService: {
|
||||||
setLastPromptTokenCount: vi.fn(),
|
setLastPromptTokenCount: vi.fn(),
|
||||||
|
clear: vi.fn(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -74,17 +75,16 @@ describe('clearCommand', () => {
|
|||||||
|
|
||||||
expect(mockResetChat).toHaveBeenCalledTimes(1);
|
expect(mockResetChat).toHaveBeenCalledTimes(1);
|
||||||
expect(mockHintClear).toHaveBeenCalledTimes(1);
|
expect(mockHintClear).toHaveBeenCalledTimes(1);
|
||||||
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
|
expect(uiTelemetryService.clear).toHaveBeenCalled();
|
||||||
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
expect(uiTelemetryService.clear).toHaveBeenCalledTimes(1);
|
||||||
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
|
expect(mockContext.ui.clear).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
// Check the order of operations.
|
// Check the order of operations.
|
||||||
const setDebugMessageOrder = (mockContext.ui.setDebugMessage as Mock).mock
|
const setDebugMessageOrder = (mockContext.ui.setDebugMessage as Mock).mock
|
||||||
.invocationCallOrder[0];
|
.invocationCallOrder[0];
|
||||||
const resetChatOrder = mockResetChat.mock.invocationCallOrder[0];
|
const resetChatOrder = mockResetChat.mock.invocationCallOrder[0];
|
||||||
const resetTelemetryOrder = (
|
const resetTelemetryOrder = (uiTelemetryService.clear as Mock).mock
|
||||||
uiTelemetryService.setLastPromptTokenCount as Mock
|
.invocationCallOrder[0];
|
||||||
).mock.invocationCallOrder[0];
|
|
||||||
const clearOrder = (mockContext.ui.clear as Mock).mock
|
const clearOrder = (mockContext.ui.clear as Mock).mock
|
||||||
.invocationCallOrder[0];
|
.invocationCallOrder[0];
|
||||||
|
|
||||||
@@ -110,8 +110,8 @@ describe('clearCommand', () => {
|
|||||||
'Clearing terminal.',
|
'Clearing terminal.',
|
||||||
);
|
);
|
||||||
expect(mockResetChat).not.toHaveBeenCalled();
|
expect(mockResetChat).not.toHaveBeenCalled();
|
||||||
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(0);
|
expect(uiTelemetryService.clear).toHaveBeenCalled();
|
||||||
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
expect(uiTelemetryService.clear).toHaveBeenCalledTimes(1);
|
||||||
expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
|
expect(nullConfigContext.ui.clear).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -23,10 +23,6 @@ export const clearCommand: SlashCommand = {
|
|||||||
action: async (context, _args) => {
|
action: async (context, _args) => {
|
||||||
const geminiClient = context.services.config?.getGeminiClient();
|
const geminiClient = context.services.config?.getGeminiClient();
|
||||||
const config = context.services.config;
|
const config = context.services.config;
|
||||||
const chatRecordingService = context.services.config
|
|
||||||
?.getGeminiClient()
|
|
||||||
?.getChat()
|
|
||||||
.getChatRecordingService();
|
|
||||||
|
|
||||||
// Fire SessionEnd hook before clearing
|
// Fire SessionEnd hook before clearing
|
||||||
const hookSystem = config?.getHookSystem();
|
const hookSystem = config?.getHookSystem();
|
||||||
@@ -34,6 +30,18 @@ export const clearCommand: SlashCommand = {
|
|||||||
await hookSystem.fireSessionEndEvent(SessionEndReason.Clear);
|
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) {
|
if (geminiClient) {
|
||||||
context.ui.setDebugMessage('Clearing terminal and resetting chat.');
|
context.ui.setDebugMessage('Clearing terminal and resetting chat.');
|
||||||
// If resetChat fails, the exception will propagate and halt the command,
|
// If resetChat fails, the exception will propagate and halt the command,
|
||||||
@@ -43,16 +51,6 @@ export const clearCommand: SlashCommand = {
|
|||||||
context.ui.setDebugMessage('Clearing terminal.');
|
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
|
// Fire SessionStart hook after clearing
|
||||||
let result;
|
let result;
|
||||||
if (hookSystem) {
|
if (hookSystem) {
|
||||||
@@ -69,7 +67,7 @@ export const clearCommand: SlashCommand = {
|
|||||||
await flushTelemetry(config);
|
await flushTelemetry(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
uiTelemetryService.setLastPromptTokenCount(0);
|
uiTelemetryService.clear(newSessionId);
|
||||||
context.ui.clear();
|
context.ui.clear();
|
||||||
|
|
||||||
if (result?.systemMessage) {
|
if (result?.systemMessage) {
|
||||||
|
|||||||
@@ -238,6 +238,34 @@ describe('SessionStatsContext', () => {
|
|||||||
unmount();
|
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', () => {
|
it('should throw an error when useSessionStats is used outside of a provider', () => {
|
||||||
const onError = vi.fn();
|
const onError = vi.fn();
|
||||||
// Suppress console.error from React for this test
|
// Suppress console.error from React for this test
|
||||||
|
|||||||
@@ -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('update', handleUpdate);
|
||||||
|
uiTelemetryService.on('clear', handleClear);
|
||||||
// Set initial state
|
// Set initial state
|
||||||
handleUpdate({
|
handleUpdate({
|
||||||
metrics: uiTelemetryService.getMetrics(),
|
metrics: uiTelemetryService.getMetrics(),
|
||||||
@@ -225,6 +235,7 @@ export const SessionStatsProvider: React.FC<{ children: React.ReactNode }> = ({
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
uiTelemetryService.off('update', handleUpdate);
|
uiTelemetryService.off('update', handleUpdate);
|
||||||
|
uiTelemetryService.off('clear', handleClear);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
coreEvents,
|
coreEvents,
|
||||||
convertSessionToClientHistory,
|
convertSessionToClientHistory,
|
||||||
|
uiTelemetryService,
|
||||||
} from '@google/gemini-cli-core';
|
} from '@google/gemini-cli-core';
|
||||||
|
|
||||||
// Mock modules
|
// Mock modules
|
||||||
@@ -36,6 +37,17 @@ vi.mock('../../utils/sessionUtils.js', async (importOriginal) => {
|
|||||||
getSessionFiles: vi.fn(),
|
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_PROJECT_TEMP_DIR = '/test/project/temp';
|
||||||
const MOCKED_CHATS_DIR = '/test/project/temp/chats';
|
const MOCKED_CHATS_DIR = '/test/project/temp/chats';
|
||||||
@@ -102,6 +114,7 @@ describe('useSessionBrowser', () => {
|
|||||||
expect(mockConfig.setSessionId).toHaveBeenCalledWith(
|
expect(mockConfig.setSessionId).toHaveBeenCalledWith(
|
||||||
'existing-session-456',
|
'existing-session-456',
|
||||||
);
|
);
|
||||||
|
expect(uiTelemetryService.hydrate).toHaveBeenCalledWith(mockConversation);
|
||||||
expect(result.current.isSessionBrowserOpen).toBe(false);
|
expect(result.current.isSessionBrowserOpen).toBe(false);
|
||||||
expect(mockOnLoadHistory).toHaveBeenCalled();
|
expect(mockOnLoadHistory).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import type {
|
|||||||
import {
|
import {
|
||||||
coreEvents,
|
coreEvents,
|
||||||
convertSessionToClientHistory,
|
convertSessionToClientHistory,
|
||||||
|
uiTelemetryService,
|
||||||
} from '@google/gemini-cli-core';
|
} from '@google/gemini-cli-core';
|
||||||
import type { SessionInfo } from '../../utils/sessionUtils.js';
|
import type { SessionInfo } from '../../utils/sessionUtils.js';
|
||||||
import { convertSessionToHistoryFormats } 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.
|
// Use the old session's ID to continue it.
|
||||||
const existingSessionId = conversation.sessionId;
|
const existingSessionId = conversation.sessionId;
|
||||||
config.setSessionId(existingSessionId);
|
config.setSessionId(existingSessionId);
|
||||||
|
uiTelemetryService.hydrate(conversation);
|
||||||
|
|
||||||
const resumedSessionData = {
|
const resumedSessionData = {
|
||||||
conversation,
|
conversation,
|
||||||
|
|||||||
@@ -171,6 +171,7 @@ export class ChatRecordingService {
|
|||||||
this.cachedConversation = null;
|
this.cachedConversation = null;
|
||||||
} else {
|
} else {
|
||||||
// Create new session
|
// Create new session
|
||||||
|
this.sessionId = this.config.getSessionId();
|
||||||
const chatsDir = path.join(
|
const chatsDir = path.join(
|
||||||
this.config.storage.getProjectTempDir(),
|
this.config.storage.getProjectTempDir(),
|
||||||
'chats',
|
'chats',
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
type ApiErrorEvent,
|
type ApiErrorEvent,
|
||||||
type ApiResponseEvent,
|
type ApiResponseEvent,
|
||||||
} from './types.js';
|
} from './types.js';
|
||||||
|
import { type ConversationRecord } from '../services/chatRecordingService.js';
|
||||||
import type {
|
import type {
|
||||||
CompletedToolCall,
|
CompletedToolCall,
|
||||||
ErroredToolCall,
|
ErroredToolCall,
|
||||||
@@ -698,6 +699,121 @@ describe('UiTelemetryService', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('clear', () => {
|
||||||
|
it('should reset metrics and last prompt token count', () => {
|
||||||
|
// Set up initial state with some metrics
|
||||||
|
const event = {
|
||||||
|
'event.name': EVENT_API_RESPONSE,
|
||||||
|
model: 'gemini-2.5-pro',
|
||||||
|
duration_ms: 500,
|
||||||
|
usage: {
|
||||||
|
input_token_count: 100,
|
||||||
|
output_token_count: 200,
|
||||||
|
total_token_count: 300,
|
||||||
|
cached_content_token_count: 50,
|
||||||
|
thoughts_token_count: 20,
|
||||||
|
tool_token_count: 30,
|
||||||
|
},
|
||||||
|
} as ApiResponseEvent & { 'event.name': typeof EVENT_API_RESPONSE };
|
||||||
|
|
||||||
|
service.addEvent(event);
|
||||||
|
service.setLastPromptTokenCount(123);
|
||||||
|
|
||||||
|
expect(service.getMetrics().models['gemini-2.5-pro']).toBeDefined();
|
||||||
|
expect(service.getLastPromptTokenCount()).toBe(123);
|
||||||
|
|
||||||
|
service.clear();
|
||||||
|
|
||||||
|
expect(service.getMetrics().models).toEqual({});
|
||||||
|
expect(service.getLastPromptTokenCount()).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should emit clear and update events', () => {
|
||||||
|
const clearSpy = vi.fn();
|
||||||
|
const updateSpy = vi.fn();
|
||||||
|
service.on('clear', clearSpy);
|
||||||
|
service.on('update', updateSpy);
|
||||||
|
|
||||||
|
const newSessionId = 'new-session-id';
|
||||||
|
service.clear(newSessionId);
|
||||||
|
|
||||||
|
expect(clearSpy).toHaveBeenCalledWith(newSessionId);
|
||||||
|
expect(updateSpy).toHaveBeenCalledOnce();
|
||||||
|
const { metrics, lastPromptTokenCount } = updateSpy.mock.calls[0][0];
|
||||||
|
expect(metrics.models).toEqual({});
|
||||||
|
expect(lastPromptTokenCount).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('hydrate', () => {
|
||||||
|
it('should aggregate metrics from a ConversationRecord', () => {
|
||||||
|
const conversation = {
|
||||||
|
sessionId: 'resumed-session',
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
type: 'user',
|
||||||
|
content: 'Hello',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'gemini',
|
||||||
|
model: 'gemini-1.5-pro',
|
||||||
|
tokens: {
|
||||||
|
input: 10,
|
||||||
|
output: 20,
|
||||||
|
total: 30,
|
||||||
|
cached: 5,
|
||||||
|
thoughts: 2,
|
||||||
|
tool: 3,
|
||||||
|
},
|
||||||
|
toolCalls: [
|
||||||
|
{ name: 'test_tool', status: 'success' },
|
||||||
|
{ name: 'test_tool', status: 'error' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'gemini',
|
||||||
|
model: 'gemini-1.5-pro',
|
||||||
|
tokens: {
|
||||||
|
input: 100,
|
||||||
|
output: 200,
|
||||||
|
total: 300,
|
||||||
|
cached: 50,
|
||||||
|
thoughts: 20,
|
||||||
|
tool: 30,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as unknown as ConversationRecord;
|
||||||
|
|
||||||
|
const clearSpy = vi.fn();
|
||||||
|
const updateSpy = vi.fn();
|
||||||
|
service.on('clear', clearSpy);
|
||||||
|
service.on('update', updateSpy);
|
||||||
|
|
||||||
|
service.hydrate(conversation);
|
||||||
|
|
||||||
|
expect(clearSpy).toHaveBeenCalledWith('resumed-session');
|
||||||
|
const metrics = service.getMetrics();
|
||||||
|
const modelMetrics = metrics.models['gemini-1.5-pro'];
|
||||||
|
|
||||||
|
expect(modelMetrics).toBeDefined();
|
||||||
|
expect(modelMetrics.tokens.prompt).toBe(110); // 10 + 100
|
||||||
|
expect(modelMetrics.tokens.candidates).toBe(220); // 20 + 200
|
||||||
|
expect(modelMetrics.tokens.cached).toBe(55); // 5 + 50
|
||||||
|
expect(modelMetrics.tokens.thoughts).toBe(22); // 2 + 20
|
||||||
|
expect(modelMetrics.tokens.tool).toBe(33); // 3 + 30
|
||||||
|
expect(modelMetrics.tokens.input).toBe(55); // 110 - 55
|
||||||
|
|
||||||
|
expect(metrics.tools.totalCalls).toBe(2);
|
||||||
|
expect(metrics.tools.totalSuccess).toBe(1);
|
||||||
|
expect(metrics.tools.totalFail).toBe(1);
|
||||||
|
expect(metrics.tools.byName['test_tool'].count).toBe(2);
|
||||||
|
|
||||||
|
expect(service.getLastPromptTokenCount()).toBe(300); // 100 (input) + 200 (output)
|
||||||
|
expect(updateSpy).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Tool Call Event with Line Count Metadata', () => {
|
describe('Tool Call Event with Line Count Metadata', () => {
|
||||||
it('should aggregate valid line count metadata', () => {
|
it('should aggregate valid line count metadata', () => {
|
||||||
const toolCall = createFakeCompletedToolCall('test_tool', true, 100);
|
const toolCall = createFakeCompletedToolCall('test_tool', true, 100);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
} from './types.js';
|
} from './types.js';
|
||||||
|
|
||||||
import { ToolCallDecision } from './tool-call-decision.js';
|
import { ToolCallDecision } from './tool-call-decision.js';
|
||||||
|
import { type ConversationRecord } from '../services/chatRecordingService.js';
|
||||||
|
|
||||||
export type UiEvent =
|
export type UiEvent =
|
||||||
| (ApiResponseEvent & { 'event.name': typeof EVENT_API_RESPONSE })
|
| (ApiResponseEvent & { 'event.name': typeof EVENT_API_RESPONSE })
|
||||||
@@ -185,6 +186,96 @@ export class UiTelemetryService extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clear(newSessionId?: string): void {
|
||||||
|
this.#metrics = createInitialMetrics();
|
||||||
|
this.#lastPromptTokenCount = 0;
|
||||||
|
this.emit('clear', newSessionId);
|
||||||
|
this.emit('update', {
|
||||||
|
metrics: this.#metrics,
|
||||||
|
lastPromptTokenCount: this.#lastPromptTokenCount,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hydrates the telemetry metrics from a historical conversation record.
|
||||||
|
* This is used when resuming a session to restore token counts and tool stats.
|
||||||
|
*/
|
||||||
|
hydrate(conversation: ConversationRecord): void {
|
||||||
|
this.clear(conversation.sessionId);
|
||||||
|
|
||||||
|
let totalTokensInContext = 0;
|
||||||
|
|
||||||
|
for (const message of conversation.messages) {
|
||||||
|
if (message.type === 'gemini') {
|
||||||
|
const model = message.model || 'unknown';
|
||||||
|
const modelMetrics = this.getOrCreateModelMetrics(model);
|
||||||
|
|
||||||
|
// Restore API request stats
|
||||||
|
modelMetrics.api.totalRequests++;
|
||||||
|
|
||||||
|
// Restore token metrics
|
||||||
|
if (message.tokens) {
|
||||||
|
modelMetrics.tokens.prompt += message.tokens.input;
|
||||||
|
modelMetrics.tokens.candidates += message.tokens.output;
|
||||||
|
modelMetrics.tokens.total += message.tokens.total;
|
||||||
|
modelMetrics.tokens.cached += message.tokens.cached;
|
||||||
|
modelMetrics.tokens.thoughts += message.tokens.thoughts || 0;
|
||||||
|
modelMetrics.tokens.tool += message.tokens.tool || 0;
|
||||||
|
modelMetrics.tokens.input = Math.max(
|
||||||
|
0,
|
||||||
|
modelMetrics.tokens.prompt - modelMetrics.tokens.cached,
|
||||||
|
);
|
||||||
|
|
||||||
|
// The total tokens of the last Gemini message represents the context
|
||||||
|
// size at that point in time.
|
||||||
|
totalTokensInContext = message.tokens.total;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore tool metrics
|
||||||
|
if (message.toolCalls) {
|
||||||
|
for (const toolCall of message.toolCalls) {
|
||||||
|
this.#metrics.tools.totalCalls++;
|
||||||
|
if (toolCall.status === 'success') {
|
||||||
|
this.#metrics.tools.totalSuccess++;
|
||||||
|
} else if (toolCall.status === 'error') {
|
||||||
|
this.#metrics.tools.totalFail++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.#metrics.tools.byName[toolCall.name]) {
|
||||||
|
this.#metrics.tools.byName[toolCall.name] = {
|
||||||
|
count: 0,
|
||||||
|
success: 0,
|
||||||
|
fail: 0,
|
||||||
|
durationMs: 0,
|
||||||
|
decisions: {
|
||||||
|
[ToolCallDecision.ACCEPT]: 0,
|
||||||
|
[ToolCallDecision.REJECT]: 0,
|
||||||
|
[ToolCallDecision.MODIFY]: 0,
|
||||||
|
[ToolCallDecision.AUTO_ACCEPT]: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const toolStats = this.#metrics.tools.byName[toolCall.name];
|
||||||
|
toolStats.count++;
|
||||||
|
if (toolCall.status === 'success') {
|
||||||
|
toolStats.success++;
|
||||||
|
} else if (toolCall.status === 'error') {
|
||||||
|
toolStats.fail++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.#lastPromptTokenCount = totalTokensInContext;
|
||||||
|
|
||||||
|
this.emit('update', {
|
||||||
|
metrics: this.#metrics,
|
||||||
|
lastPromptTokenCount: this.#lastPromptTokenCount,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private getOrCreateModelMetrics(modelName: string): ModelMetrics {
|
private getOrCreateModelMetrics(modelName: string): ModelMetrics {
|
||||||
if (!this.#metrics.models[modelName]) {
|
if (!this.#metrics.models[modelName]) {
|
||||||
this.#metrics.models[modelName] = createInitialModelMetrics();
|
this.#metrics.models[modelName] = createInitialModelMetrics();
|
||||||
|
|||||||
@@ -54,8 +54,8 @@
|
|||||||
},
|
},
|
||||||
"defaultApprovalMode": {
|
"defaultApprovalMode": {
|
||||||
"title": "Default Approval Mode",
|
"title": "Default Approval Mode",
|
||||||
"description": "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.",
|
"description": "The default approval mode for tool execution. 'default' prompts for approval, 'auto_edit' auto-approves edit tools, and 'plan' is read-only mode. YOLO mode (auto-approve all actions) can only be enabled via command line (--yolo or --approval-mode=yolo).",
|
||||||
"markdownDescription": "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.\n\n- Category: `General`\n- Requires restart: `no`\n- Default: `default`",
|
"markdownDescription": "The default approval mode for tool execution. 'default' prompts for approval, 'auto_edit' auto-approves edit tools, and 'plan' is read-only mode. YOLO mode (auto-approve all actions) can only be enabled via command line (--yolo or --approval-mode=yolo).\n\n- Category: `General`\n- Requires restart: `no`\n- Default: `default`",
|
||||||
"default": "default",
|
"default": "default",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["default", "auto_edit", "plan"]
|
"enum": ["default", "auto_edit", "plan"]
|
||||||
|
|||||||
Reference in New Issue
Block a user