Skip to content

Add branching conversations through an edit message option - #310

Open
Yahiewi wants to merge 9 commits into
jupyterlite:mainfrom
Yahiewi:edit
Open

Add branching conversations through an edit message option#310
Yahiewi wants to merge 9 commits into
jupyterlite:mainfrom
Yahiewi:edit

Conversation

@Yahiewi

@Yahiewi Yahiewi commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

This PR closes issue #288 by adding an option to edit previously sent user messages which creates a branch or multiple branches if that message was edited more than once.
This uses updateMessage from @jupyter/chat.

Code changes:

src/components/edit-message-button.tsx and src/index.ts:

  • This new React component is added as a section using createEditMessageSection: MessageFooterSection in a new messageFooterRegistry (from @jupyter/chat). It has a BranchNavigation component which shows the branch navigation arrows on edited user messages. It also shows a counter for the branches (for example 2/3). It also preserves scroll position so when editing a message, the conversation isn't scrolled to end.

src/agent.ts:

  • Added history getters and setters as well as a truncateHistory(n) function (which keeps the first n turns (user message and response) in the history) in the AgentManager class.
    EDIT: Since a setHistory function was also defined in PR Save and restore chat #303, I renamed the setHistory function defined here to restoreHistory. Although I think it makes more sense to switch the names since the function defined in this PR is a simple setter.

src/chat-model.ts:

  • Added _branchPoints private field in AIChatModel class of type Map<string,{ branches: AIChatModel.IBranchSnapshot[]; currentIndex: number } which maps to every edited message messageId an array of branches and the currently active branch index.
  • clearMessages method now also clears the branch points.
  • Added several functions: editMessage, switchBranch, updateMessage (which is called by @jupyter/chat's native edit UI and basically calls editMessage), getBranchInfo, a new private _snapshotFrom method and refactored out a _createErrorMessage method that's used in editMessage and sendMessage.
  • In the AIChatModel namespace, added an IBranchSnapshot interface.

Demo:

Screencast.from.2026-04-07.14-27-32.mp4

@Yahiewi

Yahiewi commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

I'm thinking about how we can undo the code changes when editing a message, or maybe give the user the option to undo the the code changes. However, I'm still not quite sure how to do this or if this is doable. The main challenge I think is implementing this in a collaborative context:
-If a user undoes his changes by editing the message, should this also undo the changes for his collaborators? What happens if a user is editing a section of the code which is generated by the AI and has been undone by his collaborator? I haven't worked on collaborative issues in Jupyter, so I'm not sure how feasible this is.

@Yahiewi
Yahiewi marked this pull request as ready for review April 7, 2026 14:53
@brichet brichet added the enhancement New feature or request label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants