From da09431be975e6ccc26db536a85313c0a6069360 Mon Sep 17 00:00:00 2001 From: Billy Biggs Date: Sun, 15 Jun 2025 07:56:07 -0700 Subject: [PATCH] Add support for showing descriptions of CLI tools (#1052) Adds support for /tools desc to show the full description of tools as provided to the model. --- docs/cli/commands.md | 7 +++ .../ui/hooks/slashCommandProcessor.test.ts | 54 ++++++++++++----- .../cli/src/ui/hooks/slashCommandProcessor.ts | 59 ++++++++++++++++++- 3 files changed, 103 insertions(+), 17 deletions(-) diff --git a/docs/cli/commands.md b/docs/cli/commands.md index d5d8bc18..26c3ed49 100644 --- a/docs/cli/commands.md +++ b/docs/cli/commands.md @@ -61,6 +61,13 @@ Slash commands provide meta-level control over the CLI itself. They can typicall - **Description:** Displays a list of all the tools that are currently available to the model. - **Action:** Outputs a list of the available tools. + - **Sub-commands:** + - **`desc`** or **`descriptions`**: + - **Description:** Shows detailed descriptions of each tool. + - **Action:** Displays each tool's name with its full description as provided to the model. + - **`nodesc`** or **`nodescriptions`**: + - **Description:** Hides tool descriptions, showing only the tool names. + - **Action:** Displays a compact list with only tool names. - **`/compress`** diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts index 3e0a768c..845cbe92 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.test.ts @@ -595,14 +595,9 @@ Add any other context about the problem here. }); // Should only show tool1 and tool2, not the MCP tools - expect(mockAddItem).toHaveBeenNthCalledWith( - 2, - expect.objectContaining({ - type: MessageType.INFO, - text: 'Available Gemini CLI tools:\n\nTool1\nTool2', - }), - expect.any(Number), - ); + const message = mockAddItem.mock.calls[1][0].text; + expect(message).toContain('\u001b[36mTool1\u001b[0m'); + expect(message).toContain('\u001b[36mTool2\u001b[0m'); expect(commandResult).toBe(true); }); @@ -626,14 +621,43 @@ Add any other context about the problem here. commandResult = await handleSlashCommand('/tools'); }); - expect(mockAddItem).toHaveBeenNthCalledWith( - 2, - expect.objectContaining({ - type: MessageType.INFO, - text: 'Available Gemini CLI tools:\n\n', + const message = mockAddItem.mock.calls[1][0].text; + expect(message).toContain('No tools available'); + expect(commandResult).toBe(true); + }); + + it('should display tool descriptions when /tools desc is used', async () => { + const mockTools = [ + { + name: 'tool1', + displayName: 'Tool1', + description: 'Description for Tool1', + }, + { + name: 'tool2', + displayName: 'Tool2', + description: 'Description for Tool2', + }, + ]; + + mockConfig = { + ...mockConfig, + getToolRegistry: vi.fn().mockResolvedValue({ + getAllTools: vi.fn().mockReturnValue(mockTools), }), - expect.any(Number), - ); + } as unknown as Config; + + const { handleSlashCommand } = getProcessor(); + let commandResult: SlashCommandActionReturn | boolean = false; + await act(async () => { + commandResult = await handleSlashCommand('/tools desc'); + }); + + const message = mockAddItem.mock.calls[1][0].text; + expect(message).toContain('\u001b[36mTool1\u001b[0m'); + expect(message).toContain('Description for Tool1'); + expect(message).toContain('\u001b[36mTool2\u001b[0m'); + expect(message).toContain('Description for Tool2'); expect(commandResult).toBe(true); }); }); diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.ts index 9a9b7596..f03761ff 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.ts @@ -419,6 +419,21 @@ export const useSlashCommandProcessor = ( name: 'tools', description: 'list available Gemini CLI tools', action: async (_mainCommand, _subCommand, _args) => { + // Check if the _subCommand includes a specific flag to control description visibility + let useShowDescriptions = showToolDescriptions; + if (_subCommand === 'desc' || _subCommand === 'descriptions') { + useShowDescriptions = true; + } else if ( + _subCommand === 'nodesc' || + _subCommand === 'nodescriptions' + ) { + useShowDescriptions = false; + } else if (_args === 'desc' || _args === 'descriptions') { + useShowDescriptions = true; + } else if (_args === 'nodesc' || _args === 'nodescriptions') { + useShowDescriptions = false; + } + const toolRegistry = await config?.getToolRegistry(); const tools = toolRegistry?.getAllTools(); if (!tools) { @@ -432,11 +447,51 @@ export const useSlashCommandProcessor = ( // Filter out MCP tools by checking if they have a serverName property const geminiTools = tools.filter((tool) => !('serverName' in tool)); - const geminiToolList = geminiTools.map((tool) => tool.displayName); + + let message = 'Available Gemini CLI tools:\n\n'; + + if (geminiTools.length > 0) { + geminiTools.forEach((tool) => { + if (useShowDescriptions && tool.description) { + // Format tool name in cyan using simple ANSI cyan color + message += ` - \u001b[36m${tool.displayName}\u001b[0m: `; + + // Apply green color to the description text + const greenColor = '\u001b[32m'; + const resetColor = '\u001b[0m'; + + // Handle multi-line descriptions by properly indenting and preserving formatting + const descLines = tool.description.split('\n'); + message += `${greenColor}${descLines[0]}${resetColor}\n`; + + // If there are multiple lines, add proper indentation for each line + if (descLines.length > 1) { + for (let i = 1; i < descLines.length; i++) { + // Skip empty lines at the end + if ( + i === descLines.length - 1 && + descLines[i].trim() === '' + ) + continue; + message += ` ${greenColor}${descLines[i]}${resetColor}\n`; + } + } + } else { + // Use cyan color for the tool name even when not showing descriptions + message += ` - \u001b[36m${tool.displayName}\u001b[0m\n`; + } + }); + } else { + message += ' No tools available\n'; + } + message += '\n'; + + // Make sure to reset any ANSI formatting at the end to prevent it from affecting the terminal + message += '\u001b[0m'; addMessage({ type: MessageType.INFO, - content: `Available Gemini CLI tools:\n\n${geminiToolList.join('\n')}`, + content: message, timestamp: new Date(), }); },