feat(cli): add /enhance command to improve user prompts - #25135
feat(cli): add /enhance command to improve user prompts#25135akh64bit wants to merge 14 commits into
Conversation
This adds the /enhance command which uses an LLM to refine user prompts based on conversation history. Closes #25133
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements a new /enhance command for the CLI, enabling users to improve their prompt drafts through LLM-based rephrasing. By integrating with the existing conversation history and adding a mechanism to update the input buffer, the feature provides a seamless workflow for refining prompts directly within the terminal interface. Highlights
Ignored Files
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new /enhance built-in command to the CLI, which uses a Gemini model to rephrase and add context to user prompts based on the conversation history. The changes include the command implementation, a new setInput method in the UI context to update the input buffer with the enhanced text, and comprehensive unit tests. Feedback is provided to set autoExecute to false to prevent errors when selecting the command from autocomplete without arguments, and to improve the robustness of response parsing by handling 'thought' parts and sanitizing the output to prevent prompt injection.
| name: 'enhance', | ||
| description: 'Enhance a prompt with additional context and rephrasing', | ||
| kind: CommandKind.BUILT_IN, | ||
| autoExecute: true, |
There was a problem hiding this comment.
Since the /enhance command requires a prompt argument to function correctly, autoExecute should be set to false. When autoExecute is true, selecting the command from the autocomplete list (e.g., by pressing Enter) will trigger it immediately with empty arguments, resulting in an error message. Setting it to false allows the user to complete the command name and then type the prompt they wish to enhance.
| autoExecute: true, | |
| autoExecute: false, |
| LlmRole.UTILITY_TOOL, | ||
| ); | ||
|
|
||
| const enhancedText = response.candidates?.[0]?.content?.parts?.[0]?.text; |
There was a problem hiding this comment.
The current implementation assumes the enhanced prompt is always in the first part of the first candidate. Gemini 2.0 models often include a 'thought' part before the actual response text. It is safer to find the first text part that isn't a thought. Additionally, to prevent prompt injection, sanitize the output by removing newlines and context-breaking characters like ']' before it is used in further prompts, as per repository rules.
| const enhancedText = response.candidates?.[0]?.content?.parts?.[0]?.text; | |
| const enhancedText = response.candidates?.[0]?.content?.parts?.find((part) => part.text !== undefined && !part.thought)?.text?.replace(/\n/g, '').replace(/]/g, ''); |
References
- Sanitize data from LLM-driven tools before injecting it into a system prompt to prevent prompt injection. At a minimum, remove newlines and context-breaking characters (e.g., ']').
This adds the Alt+E keyboard shortcut to quickly trigger the prompt enhancement functionality alongside the existing /enhance slash command.
fb93c4f to
48d7a72
Compare
|
Size Change: +4.53 kB (+0.01%) Total Size: 33.9 MB
ℹ️ View Unchanged
|
This commit resolves the merge conflicts that were present in several SVG snapshot files by regenerating them via 'vitest run -u'.
This commit addresses feedback by setting autoExecute to false for the enhance command, filtering out 'thought' parts from model responses, and sanitizing the output to prevent prompt injection.
|
Thanks for the feedback! I've pushed a new commit that addresses these issues:
|
|
Thanks for the review! I have fully addressed the comments by
|
…dress review comments
|
Per our guidelines, we do not accept pull requests from external contributors unless they are linked to issues with the 'help wanted' label. Thank you for understanding! |
Summary
This PR introduces the
/enhancecommand, allowing users to leverage the LLM to refine and expand their prompts. It uses the conversation history as context for generating high-quality prompt rephrasings.Details
The new command reads the user's initial prompt draft, requests an enhancement from the configured model, and automatically populates the input buffer with the refined prompt via a new
setInputmethod onCommandContext. It also updatessettings.jsonto organize plan settings.Related Issues
Resolves #25133
How to Validate
gh pr checkout <pr-number>npm run start/enhance write a functionnpm test -w @google/gemini-cli -- packages/cli/src/ui/commands/enhanceCommand.test.tsPre-Merge Checklist