Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
70 changes: 63 additions & 7 deletions 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 @@ -84,8 +84,24 @@ describe('toolsCommand', () => {
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 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);
});

it('should list tools with descriptions when "desc" arg is passed', async () => {
Expand All @@ -105,9 +121,49 @@ describe('toolsCommand', () => {
expect(message.type).toBe(MessageType.TOOLS_LIST);
expect(message.showDescriptions).toBe(true);
expect(message.tools).toHaveLength(2);
expect(message.tools[0].description).toBe(
'Reads files from the local system.',
);
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);
});

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);
});
});
102 changes: 72 additions & 30 deletions packages/cli/src/ui/commands/toolsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,85 @@ 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 toolRegistry = context.services.config?.getToolRegistry();
if (!toolRegistry) {
context.ui.addItem({
type: MessageType.ERROR,
text: 'Could not retrieve tool registry.',
});
return;
if (subCommand === 'desc') {
const item = getToolsList(context, true);
if (item) {
context.ui.addItem(item);
}
} else if (!subCommand || subCommand === 'list') {
const item = getToolsList(context, false);
if (item) {
context.ui.addItem(item);
}
} else {
// For any other argument, default to list or let the UI handle it if it was a subcommand.
// But since we want to be strict and simple:
const item = getToolsList(context, false);
if (item) {
context.ui.addItem(item);
}
}

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);
},
};