Skip to content

feat: Add /undo command to revert last conversation turn #18708#20027

Open
lin88225 wants to merge 2 commits intogoogle-gemini:mainfrom
lin88225:feat/undo-command
Open

feat: Add /undo command to revert last conversation turn #18708#20027
lin88225 wants to merge 2 commits intogoogle-gemini:mainfrom
lin88225:feat/undo-command

Conversation

@lin88225
Copy link
Copy Markdown

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

@lin88225 lin88225 requested review from a team as code owners February 23, 2026 15:55
@google-cla
Copy link
Copy Markdown

google-cla bot commented Feb 23, 2026

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.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 /undo command for the CLI, significantly enhancing the user experience by providing a quick and consistent way to revert the most recent interaction in a conversation. This functionality is built upon the existing conversation rewind mechanism, which has been refactored for broader utility, and includes a user-friendly feature that restores the last user prompt to the input field for easy modification.

Highlights

  • New /undo command: A new built-in slash command, /undo, has been introduced, allowing users to revert the last conversation turn (user prompt and model response).
  • Rewind Conversation Refactoring: The rewindConversation function was refactored and exported from rewindCommand.tsx to make it a reusable utility for other commands, specifically the new /undo command.
  • Conversation State Management: The /undo command leverages the existing rewindConversation logic to ensure state consistency when reverting the conversation history.
  • Enhanced User Experience: After an undo operation, the last user prompt is restored to the input buffer, enabling users to easily refine and resubmit their question without retyping.
Changelog
  • package-lock.json
    • Added strip-json-comments dependency.
  • packages/cli/src/services/BuiltinCommandLoader.ts
    • Imported the new undoCommand.
    • Registered undoCommand in the list of available built-in commands.
  • packages/cli/src/ui/commands/rewindCommand.tsx
    • Exported the rewindConversation function to allow its reuse by other commands.
  • packages/cli/src/ui/commands/undoCommand.tsx
    • Created a new file defining the /undo slash command, implementing its logic to rewind the conversation by two messages (user and model) and restore the user's last prompt.
Activity
  • The author implemented the /undo command, leveraging existing rewindConversation logic.
  • The rewindConversation function was refactored to be exportable and reusable.
  • Documentation was updated to register the new command in BuiltinCommandLoader.
  • Manual integration testing was performed on MacOS, confirming the command's functionality and user experience.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-cli gemini-cli bot added priority/p3 Backlog - a good idea but not currently a priority. area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! labels Feb 23, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lin88225
Copy link
Copy Markdown
Author

Updated to use findLastIndex and partToString as suggested. Branch is now rebased and up-to-date with main.

Comment on lines +29 to +32
const lastUserIndex = messages.findLastIndex((m) => m.type === 'user');

// User message and a model response to undo turn
if (messages.length < 2) {
Copy link
Copy Markdown
Contributor

@mrpmohiburrahman mrpmohiburrahman Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality help wanted We will accept PRs from all issues marked as "help wanted". Thanks for your support! priority/p3 Backlog - a good idea but not currently a priority.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add /undo command to revert last conversation turn

2 participants