feat: Add /undo command to revert last conversation turn #18708#20027
feat: Add /undo command to revert last conversation turn #18708#20027lin88225 wants to merge 2 commits intogoogle-gemini:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @lin88225, 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 introduces a new Highlights
Changelog
Activity
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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This PR adds the /undo command to revert the last conversation turn. While the feature is a valuable addition for UX, the current implementation for identifying the message to rewind to is fragile and contains logic errors that lead to incorrect history states (over-deleting previous turns) or doing nothing on the first turn. I've provided suggestions to use a more robust findLastIndex approach and to use the existing partToString utility for restoring the user prompt to the input buffer.
5f75af7 to
8af3a86
Compare
8af3a86 to
5b2ddb5
Compare
|
Updated to use findLastIndex and partToString as suggested. Branch is now rebased and up-to-date with main. |
| const lastUserIndex = messages.findLastIndex((m) => m.type === 'user'); | ||
|
|
||
| // User message and a model response to undo turn | ||
| if (messages.length < 2) { |
There was a problem hiding this comment.
When no user message exists, findLastIndex returns -1. The guard on line 32 checks messages.length < 2, but that only counts messages. It doesn't check whether any of them are actually 'user' type.
The codebase defines 5 message types ('user' | 'info' | 'error' | 'warning' | 'gemini' — chatRecordingService.ts line 78), so a conversation can have 2+ non-user messages, pass the guard, and crash on line 40 when the code tries to access messages[-1].id while lastUserIndex is still -1.
Simple fix on line 32:
// before
if (messages.length < 2) {
// after
if (lastUserIndex === -1) {
Summary
This PR adds a new built-in slash command, /undo, which allows users to quickly revert the last conversation turn (the most recent User prompt and the subsequent Model response).
Details
As suggested by @jacob314, this implementation leverages the existing rewindConversation logic to ensure state consistency.
Refactoring: Exported rewindConversation from rewindCommand.tsx to make it a reusable utility for other commands.
Logic: The command calculates the target message ID at messages.length - 3 to revert the history to the state immediately preceding the last turn.
UX: The undone user prompt is restored to the input buffer, allowing the user to refine and resubmit their question without retyping.
Related Issues
Fixes #18708
How to Validate
Build: Run npm run build from the root.
Setup: Launch the CLI (./packages/cli/bin/gemini-cli) and trust the folder.
Populate: Send at least two messages (e.g., "Hello" followed by "What is 10+10?").
Execute: Type /undo.
Result:
The "What is 10+10?" message and its response should be removed from the view.
"What is 10+10?" should be prepopulated in the input buffer.
History should accurately reflect only the "Hello" turn.
Pre-Merge Checklist
[x] Updated relevant documentation (Command registered in BuiltinCommandLoader)
[ ] Added/updated tests (Verified via manual integration, happy to add automated unit tests if requested during review.)
[x] Noted breaking changes (None)
[x] Validated on required platforms/methods:
[x] MacOS
[x] npm run