mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 15:01:24 +00:00
Add /branch command for conversation branching (fixes #16)
- Add /branch slash command to create conversation branches - New UserMessageSelectorComponent shows all user messages chronologically - Selecting a message creates new session with messages before selection - Selected message is placed in editor for modification/resubmission - SessionManager.createBranchedSession() creates new session files - Updated README.md and CHANGELOG.md with /branch documentation
This commit is contained in:
parent
85ea9f500c
commit
8ae236f956
6 changed files with 379 additions and 66 deletions
|
|
@ -23,6 +23,7 @@ import { ModelSelectorComponent } from "./model-selector.js";
|
|||
import { ThinkingSelectorComponent } from "./thinking-selector.js";
|
||||
import { ToolExecutionComponent } from "./tool-execution.js";
|
||||
import { UserMessageComponent } from "./user-message.js";
|
||||
import { UserMessageSelectorComponent } from "./user-message-selector.js";
|
||||
|
||||
/**
|
||||
* TUI renderer for the coding agent
|
||||
|
|
@ -56,6 +57,9 @@ export class TuiRenderer {
|
|||
// Model selector
|
||||
private modelSelector: ModelSelectorComponent | null = null;
|
||||
|
||||
// User message selector (for branching)
|
||||
private userMessageSelector: UserMessageSelectorComponent | null = null;
|
||||
|
||||
// Track if this is the first user message (to skip spacer)
|
||||
private isFirstUserMessage = true;
|
||||
|
||||
|
|
@ -98,9 +102,14 @@ export class TuiRenderer {
|
|||
description: "Show changelog entries",
|
||||
};
|
||||
|
||||
const branchCommand: SlashCommand = {
|
||||
name: "branch",
|
||||
description: "Create a new branch from a previous message",
|
||||
};
|
||||
|
||||
// Setup autocomplete for file paths and slash commands
|
||||
const autocompleteProvider = new CombinedAutocompleteProvider(
|
||||
[thinkingCommand, modelCommand, exportCommand, sessionCommand, changelogCommand],
|
||||
[thinkingCommand, modelCommand, exportCommand, sessionCommand, changelogCommand, branchCommand],
|
||||
process.cwd(),
|
||||
);
|
||||
this.editor.setAutocompleteProvider(autocompleteProvider);
|
||||
|
|
@ -207,6 +216,13 @@ export class TuiRenderer {
|
|||
return;
|
||||
}
|
||||
|
||||
// Check for /branch command
|
||||
if (text === "/branch") {
|
||||
this.showUserMessageSelector();
|
||||
this.editor.setText("");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.onInputCallback) {
|
||||
this.onInputCallback(text);
|
||||
}
|
||||
|
|
@ -566,6 +582,90 @@ export class TuiRenderer {
|
|||
this.ui.setFocus(this.editor);
|
||||
}
|
||||
|
||||
private showUserMessageSelector(): void {
|
||||
// Extract all user messages from the current state
|
||||
const userMessages: Array<{ index: number; text: string }> = [];
|
||||
|
||||
for (let i = 0; i < this.agent.state.messages.length; i++) {
|
||||
const message = this.agent.state.messages[i];
|
||||
if (message.role === "user") {
|
||||
const userMsg = message as any;
|
||||
const textBlocks = userMsg.content.filter((c: any) => c.type === "text");
|
||||
const textContent = textBlocks.map((c: any) => c.text).join("");
|
||||
if (textContent) {
|
||||
userMessages.push({ index: i, text: textContent });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't show selector if there are no messages or only one message
|
||||
if (userMessages.length <= 1) {
|
||||
this.chatContainer.addChild(new Spacer(1));
|
||||
this.chatContainer.addChild(new Text(chalk.dim("No messages to branch from"), 1, 0));
|
||||
this.ui.requestRender();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create user message selector
|
||||
this.userMessageSelector = new UserMessageSelectorComponent(
|
||||
userMessages,
|
||||
(messageIndex) => {
|
||||
// Get the selected user message text to put in the editor
|
||||
const selectedMessage = this.agent.state.messages[messageIndex];
|
||||
const selectedUserMsg = selectedMessage as any;
|
||||
const textBlocks = selectedUserMsg.content.filter((c: any) => c.type === "text");
|
||||
const selectedText = textBlocks.map((c: any) => c.text).join("");
|
||||
|
||||
// Create a branched session with messages UP TO (but not including) the selected message
|
||||
const newSessionFile = this.sessionManager.createBranchedSession(this.agent.state, messageIndex - 1);
|
||||
|
||||
// Set the new session file as active
|
||||
this.sessionManager.setSessionFile(newSessionFile);
|
||||
|
||||
// Truncate messages in agent state to before the selected message
|
||||
const truncatedMessages = this.agent.state.messages.slice(0, messageIndex);
|
||||
this.agent.replaceMessages(truncatedMessages);
|
||||
|
||||
// Clear and re-render the chat
|
||||
this.chatContainer.clear();
|
||||
this.isFirstUserMessage = true;
|
||||
this.renderInitialMessages(this.agent.state);
|
||||
|
||||
// Show confirmation message
|
||||
this.chatContainer.addChild(new Spacer(1));
|
||||
this.chatContainer.addChild(
|
||||
new Text(chalk.dim(`Branched to new session from message ${messageIndex}`), 1, 0),
|
||||
);
|
||||
|
||||
// Put the selected message in the editor
|
||||
this.editor.setText(selectedText);
|
||||
|
||||
// Hide selector and show editor again
|
||||
this.hideUserMessageSelector();
|
||||
this.ui.requestRender();
|
||||
},
|
||||
() => {
|
||||
// Just hide the selector
|
||||
this.hideUserMessageSelector();
|
||||
this.ui.requestRender();
|
||||
},
|
||||
);
|
||||
|
||||
// Replace editor with selector
|
||||
this.editorContainer.clear();
|
||||
this.editorContainer.addChild(this.userMessageSelector);
|
||||
this.ui.setFocus(this.userMessageSelector.getMessageList());
|
||||
this.ui.requestRender();
|
||||
}
|
||||
|
||||
private hideUserMessageSelector(): void {
|
||||
// Replace selector with editor in the container
|
||||
this.editorContainer.clear();
|
||||
this.editorContainer.addChild(this.editor);
|
||||
this.userMessageSelector = null;
|
||||
this.ui.setFocus(this.editor);
|
||||
}
|
||||
|
||||
private handleExportCommand(text: string): void {
|
||||
// Parse optional filename from command: /export [filename]
|
||||
const parts = text.split(/\s+/);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue