2025-07-15 16:10:04 -04:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-11-26 20:21:33 -05:00
|
|
|
import { CodeAssistServer, getCodeAssistServer } from '@google/gemini-cli-core';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { HistoryItemStats } from '../types.js';
|
|
|
|
|
import { MessageType } from '../types.js';
|
2025-07-15 16:10:04 -04:00
|
|
|
import { formatDuration } from '../utils/formatters.js';
|
2025-07-20 16:57:34 -04:00
|
|
|
import {
|
|
|
|
|
type CommandContext,
|
|
|
|
|
type SlashCommand,
|
|
|
|
|
CommandKind,
|
|
|
|
|
} from './types.js';
|
2025-07-15 16:10:04 -04:00
|
|
|
|
2025-11-26 20:21:33 -05:00
|
|
|
async function defaultSessionView(context: CommandContext) {
|
2025-11-24 11:01:41 -08:00
|
|
|
const now = new Date();
|
|
|
|
|
const { sessionStartTime } = context.session.stats;
|
|
|
|
|
if (!sessionStartTime) {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
|
|
|
|
type: MessageType.ERROR,
|
|
|
|
|
text: 'Session start time is unavailable, cannot calculate stats.',
|
|
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const wallDuration = now.getTime() - sessionStartTime.getTime();
|
|
|
|
|
|
|
|
|
|
const statsItem: HistoryItemStats = {
|
|
|
|
|
type: MessageType.STATS,
|
|
|
|
|
duration: formatDuration(wallDuration),
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-26 20:21:33 -05:00
|
|
|
if (context.services.config) {
|
|
|
|
|
const server = getCodeAssistServer(context.services.config);
|
|
|
|
|
if (server instanceof CodeAssistServer && server.projectId) {
|
|
|
|
|
const quota = await server.retrieveUserQuota({
|
|
|
|
|
project: server.projectId,
|
|
|
|
|
});
|
|
|
|
|
statsItem.quotas = quota;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 11:01:41 -08:00
|
|
|
context.ui.addItem(statsItem, Date.now());
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-15 16:10:04 -04:00
|
|
|
export const statsCommand: SlashCommand = {
|
|
|
|
|
name: 'stats',
|
2025-07-20 16:57:34 -04:00
|
|
|
altNames: ['usage'],
|
2025-11-24 11:01:41 -08:00
|
|
|
description: 'Check session stats. Usage: /stats [session|model|tools]',
|
2025-07-20 16:57:34 -04:00
|
|
|
kind: CommandKind.BUILT_IN,
|
2025-11-26 20:21:33 -05:00
|
|
|
action: async (context: CommandContext) => {
|
|
|
|
|
await defaultSessionView(context);
|
2025-07-15 16:10:04 -04:00
|
|
|
},
|
|
|
|
|
subCommands: [
|
2025-11-24 11:01:41 -08:00
|
|
|
{
|
|
|
|
|
name: 'session',
|
|
|
|
|
description: 'Show session-specific usage statistics',
|
|
|
|
|
kind: CommandKind.BUILT_IN,
|
2025-11-26 20:21:33 -05:00
|
|
|
action: async (context: CommandContext) => {
|
|
|
|
|
await defaultSessionView(context);
|
2025-11-24 11:01:41 -08:00
|
|
|
},
|
|
|
|
|
},
|
2025-07-15 16:10:04 -04:00
|
|
|
{
|
|
|
|
|
name: 'model',
|
2025-10-17 13:20:15 -07:00
|
|
|
description: 'Show model-specific usage statistics',
|
2025-07-20 16:57:34 -04:00
|
|
|
kind: CommandKind.BUILT_IN,
|
2025-07-15 16:10:04 -04:00
|
|
|
action: (context: CommandContext) => {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
|
|
|
|
type: MessageType.MODEL_STATS,
|
|
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: 'tools',
|
2025-10-17 13:20:15 -07:00
|
|
|
description: 'Show tool-specific usage statistics',
|
2025-07-20 16:57:34 -04:00
|
|
|
kind: CommandKind.BUILT_IN,
|
2025-07-15 16:10:04 -04:00
|
|
|
action: (context: CommandContext) => {
|
|
|
|
|
context.ui.addItem(
|
|
|
|
|
{
|
|
|
|
|
type: MessageType.TOOL_STATS,
|
|
|
|
|
},
|
|
|
|
|
Date.now(),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
};
|