Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 78 additions & 1 deletion packages/cli/src/ui/commands/toolsCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('toolsCommand', () => {
});
});

it('should list tools without descriptions by default', async () => {
it('should list tools without descriptions by default (no args)', async () => {
const mockContext = createMockCommandContext({
services: {
config: {
Expand All @@ -88,6 +88,27 @@ describe('toolsCommand', () => {
expect(message.tools[1].displayName).toBe('Code Editor');
});

it('should list tools without descriptions when "list" arg is passed', async () => {
const mockContext = createMockCommandContext({
services: {
config: {
getToolRegistry: () => ({ getAllTools: () => mockTools }),
},
},
});

if (!toolsCommand.action) throw new Error('Action not defined');
await toolsCommand.action(mockContext, 'list');

const [message] = (mockContext.ui.addItem as ReturnType<typeof vi.fn>).mock
.calls[0];
expect(message.type).toBe(MessageType.TOOLS_LIST);
expect(message.showDescriptions).toBe(false);
expect(message.tools).toHaveLength(2);
expect(message.tools[0].displayName).toBe('File Reader');
expect(message.tools[1].displayName).toBe('Code Editor');
});

it('should list tools with descriptions when "desc" arg is passed', async () => {
const mockContext = createMockCommandContext({
services: {
Expand All @@ -105,9 +126,65 @@ describe('toolsCommand', () => {
expect(message.type).toBe(MessageType.TOOLS_LIST);
expect(message.showDescriptions).toBe(true);
expect(message.tools).toHaveLength(2);
expect(message.tools[0].displayName).toBe('File Reader');
expect(message.tools[0].description).toBe(
'Reads files from the local system.',
);
expect(message.tools[1].displayName).toBe('Code Editor');
expect(message.tools[1].description).toBe('Edits code files.');
});

it('should have "list" and "desc" subcommands', () => {
expect(toolsCommand.subCommands).toBeDefined();
const names = toolsCommand.subCommands?.map((s) => s.name);
expect(names).toContain('list');
expect(names).toContain('desc');
expect(names).not.toContain('descriptions');
});

it('subcommand "list" should display tools without descriptions', async () => {
const mockContext = createMockCommandContext({
services: {
config: {
getToolRegistry: () => ({ getAllTools: () => mockTools }),
},
},
});

const listCmd = toolsCommand.subCommands?.find((s) => s.name === 'list');
if (!listCmd?.action) throw new Error('Action not defined');
await listCmd.action(mockContext, '');

const [message] = (mockContext.ui.addItem as ReturnType<typeof vi.fn>).mock
.calls[0];
expect(message.showDescriptions).toBe(false);
expect(message.tools).toHaveLength(2);
expect(message.tools[0].displayName).toBe('File Reader');
expect(message.tools[1].displayName).toBe('Code Editor');
});

it('subcommand "desc" should display tools with descriptions', async () => {
const mockContext = createMockCommandContext({
services: {
config: {
getToolRegistry: () => ({ getAllTools: () => mockTools }),
},
},
});

const descCmd = toolsCommand.subCommands?.find((s) => s.name === 'desc');
if (!descCmd?.action) throw new Error('Action not defined');
await descCmd.action(mockContext, '');

const [message] = (mockContext.ui.addItem as ReturnType<typeof vi.fn>).mock
.calls[0];
expect(message.showDescriptions).toBe(true);
expect(message.tools).toHaveLength(2);
expect(message.tools[0].displayName).toBe('File Reader');
expect(message.tools[0].description).toBe(
'Reads files from the local system.',
);
expect(message.tools[1].displayName).toBe('Code Editor');
expect(message.tools[1].description).toBe('Edits code files.');
});
});
93 changes: 62 additions & 31 deletions packages/cli/src/ui/commands/toolsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,74 @@ import {
} from './types.js';
import { MessageType, type HistoryItemToolsList } from '../types.js';

const getToolsList = (context: CommandContext, showDescriptions: boolean) => {
const toolRegistry = context.services.config?.getToolRegistry();
if (!toolRegistry) {
context.ui.addItem({
type: MessageType.ERROR,
text: 'Could not retrieve tool registry.',
});
return null;
}

const tools = toolRegistry.getAllTools();
// Filter out MCP tools by checking for the absence of a serverName property
const geminiTools = tools.filter((tool) => !('serverName' in tool));

const toolsListItem: HistoryItemToolsList = {
type: MessageType.TOOLS_LIST,
tools: geminiTools.map((tool) => ({
name: tool.name,
displayName: tool.displayName,
description: tool.description,
})),
showDescriptions,
};

return toolsListItem;
};

const listSubCommand: SlashCommand = {
name: 'list',
description: 'List available Gemini CLI tools',
kind: CommandKind.BUILT_IN,
autoExecute: true,
action: async (context: CommandContext): Promise<void> => {
const item = getToolsList(context, false);
if (item) {
context.ui.addItem(item);
}
},
};

const descSubCommand: SlashCommand = {
name: 'desc',
description: 'List available Gemini CLI tools with descriptions',
kind: CommandKind.BUILT_IN,
autoExecute: true,
action: async (context: CommandContext): Promise<void> => {
const item = getToolsList(context, true);
if (item) {
context.ui.addItem(item);
}
},
};

export const toolsCommand: SlashCommand = {
name: 'tools',
description: 'List available Gemini CLI tools. Usage: /tools [desc]',
description: 'List available Gemini CLI tools',
kind: CommandKind.BUILT_IN,
autoExecute: false,
subCommands: [listSubCommand, descSubCommand],
action: async (context: CommandContext, args?: string): Promise<void> => {
const subCommand = args?.trim();

// Default to NOT showing descriptions. The user must opt in with an argument.
let useShowDescriptions = false;
if (subCommand === 'desc' || subCommand === 'descriptions') {
useShowDescriptions = true;
}
const subCommandName = args?.trim().split(' ')[0];

const toolRegistry = context.services.config?.getToolRegistry();
if (!toolRegistry) {
context.ui.addItem({
type: MessageType.ERROR,
text: 'Could not retrieve tool registry.',
});
return;
}
// Find the subcommand, defaulting to 'list' if not found or not provided.
const subCommandToRun =
toolsCommand.subCommands?.find((cmd) => cmd.name === subCommandName) ??
listSubCommand;

const tools = toolRegistry.getAllTools();
// Filter out MCP tools by checking for the absence of a serverName property
const geminiTools = tools.filter((tool) => !('serverName' in tool));

const toolsListItem: HistoryItemToolsList = {
type: MessageType.TOOLS_LIST,
tools: geminiTools.map((tool) => ({
name: tool.name,
displayName: tool.displayName,
description: tool.description,
})),
showDescriptions: useShowDescriptions,
};

context.ui.addItem(toolsListItem);
// The action is guaranteed to exist on our own subcommand definitions.
await subCommandToRun.action!(context, '');
},
};