feat(cli): add /chat debug command for nightly builds (#16339)

This commit is contained in:
Abhi
2026-01-11 14:11:06 -05:00
committed by GitHub
parent 39b3f20a22
commit 0e955da171
11 changed files with 555 additions and 5 deletions
@@ -12,7 +12,7 @@ import type { Content } from '@google/genai';
import { AuthType, type GeminiClient } from '@google/gemini-cli-core';
import * as fsPromises from 'node:fs/promises';
import { chatCommand } from './chatCommand.js';
import { chatCommand, debugCommand } from './chatCommand.js';
import {
serializeHistoryToMarkdown,
exportHistoryToFile,
@@ -693,5 +693,66 @@ Hi there!`;
const result = serializeHistoryToMarkdown(history as Content[]);
expect(result).toBe(expectedMarkdown);
});
describe('debug subcommand', () => {
let mockGetLatestApiRequest: ReturnType<typeof vi.fn>;
beforeEach(() => {
mockGetLatestApiRequest = vi.fn();
mockContext.services.config!.getLatestApiRequest =
mockGetLatestApiRequest;
vi.spyOn(process, 'cwd').mockReturnValue('/project/root');
vi.spyOn(Date, 'now').mockReturnValue(1234567890);
mockFs.writeFile.mockClear();
});
it('should return an error if no API request is found', async () => {
mockGetLatestApiRequest.mockReturnValue(undefined);
const result = await debugCommand.action?.(mockContext, '');
expect(result).toEqual({
type: 'message',
messageType: 'error',
content: 'No recent API request found to export.',
});
expect(mockFs.writeFile).not.toHaveBeenCalled();
});
it('should convert and write the API request to a json file', async () => {
const mockRequest = {
contents: [{ role: 'user', parts: [{ text: 'test' }] }],
};
mockGetLatestApiRequest.mockReturnValue(mockRequest);
const result = await debugCommand.action?.(mockContext, '');
const expectedFilename = 'gcli-request-1234567890.json';
const expectedPath = path.join('/project/root', expectedFilename);
expect(mockFs.writeFile).toHaveBeenCalledWith(
expectedPath,
expect.stringContaining('"role": "user"'),
);
expect(result).toEqual({
type: 'message',
messageType: 'info',
content: `Debug API request saved to ${expectedFilename}`,
});
});
it('should handle errors during file write', async () => {
const mockRequest = { contents: [] };
mockGetLatestApiRequest.mockReturnValue(mockRequest);
mockFs.writeFile.mockRejectedValue(new Error('Write failed'));
const result = await debugCommand.action?.(mockContext, '');
expect(result).toEqual({
type: 'message',
messageType: 'error',
content: 'Error saving debug request: Write failed',
});
});
});
});
});
@@ -27,6 +27,7 @@ import type {
} from '../types.js';
import { MessageType } from '../types.js';
import { exportHistoryToFile } from '../utils/historyExportUtils.js';
import { convertToRestPayload } from '@google/gemini-cli-core';
const getSavedChatTags = async (
context: CommandContext,
@@ -334,6 +335,46 @@ const shareCommand: SlashCommand = {
},
};
export const debugCommand: SlashCommand = {
name: 'debug',
description: 'Export the most recent API request as a JSON payload',
kind: CommandKind.BUILT_IN,
autoExecute: true,
action: async (context): Promise<MessageActionReturn> => {
const req = context.services.config?.getLatestApiRequest();
if (!req) {
return {
type: 'message',
messageType: 'error',
content: 'No recent API request found to export.',
};
}
const restPayload = convertToRestPayload(req);
const filename = `gcli-request-${Date.now()}.json`;
const filePath = path.join(process.cwd(), filename);
try {
await fsPromises.writeFile(
filePath,
JSON.stringify(restPayload, null, 2),
);
return {
type: 'message',
messageType: 'info',
content: `Debug API request saved to ${filename}`,
};
} catch (err) {
const errorMessage = err instanceof Error ? err.message : String(err);
return {
type: 'message',
messageType: 'error',
content: `Error saving debug request: ${errorMessage}`,
};
}
},
};
export const chatCommand: SlashCommand = {
name: 'chat',
description: 'Manage conversation history',