From 9de20396ce1faebbae555f93b69d0ca918faad21 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Tue, 7 Oct 2025 13:22:19 -0700 Subject: [PATCH] improv: architecture --- extensions/cli/src/ui/EditMessageSelector.tsx | 14 +---- extensions/cli/src/ui/TUIChat.tsx | 2 - .../cli/src/ui/components/ScreenContent.tsx | 3 -- extensions/cli/src/ui/hooks/useChat.ts | 53 ++++++------------- 4 files changed, 18 insertions(+), 54 deletions(-) diff --git a/extensions/cli/src/ui/EditMessageSelector.tsx b/extensions/cli/src/ui/EditMessageSelector.tsx index 7797b297f..24fdcc408 100644 --- a/extensions/cli/src/ui/EditMessageSelector.tsx +++ b/extensions/cli/src/ui/EditMessageSelector.tsx @@ -9,14 +9,12 @@ import { defaultBoxStyles } from "./styles.js"; interface EditMessageSelectorProps { chatHistory: ChatHistoryItem[]; onEdit: (messageIndex: number, newContent: string) => void; - onMessageSelected?: (originalIndex: number) => void; onExit: () => void; } export function EditMessageSelector({ chatHistory, onEdit, - onMessageSelected, onExit, }: EditMessageSelectorProps) { // Filter to only show user messages @@ -59,17 +57,7 @@ export function EditMessageSelector({ prev < userMessages.length - 1 ? prev + 1 : 0, ); } else if (key.return) { - // Notify parent that a message was selected for editing - if (onMessageSelected && userMessages[selectedIndex]) { - const originalIndex = userMessages[selectedIndex].originalIndex; - console.log("EditMessageSelector: User pressed Enter", { - selectedIndex, - originalIndex, - totalUserMessages: userMessages.length, - totalChatHistory: chatHistory.length, - }); - onMessageSelected(originalIndex); - } + // Start editing the selected message setIsEditing(true); } else if (key.escape || (key.ctrl && input === "d")) { onExit(); diff --git a/extensions/cli/src/ui/TUIChat.tsx b/extensions/cli/src/ui/TUIChat.tsx index bf8414cc2..0ecaa0fe8 100644 --- a/extensions/cli/src/ui/TUIChat.tsx +++ b/extensions/cli/src/ui/TUIChat.tsx @@ -227,7 +227,6 @@ const TUIChat: React.FC = ({ handleInterrupt, handleFileAttached, resetChatHistory, - handleSelectMessageForEdit, handleEditMessage, handleToolPermissionResponse, } = useChat({ @@ -382,7 +381,6 @@ const TUIChat: React.FC = ({ onImageInClipboardChange={setHasImageInClipboard} diffContent={diffContent} chatHistory={chatHistory} - handleSelectMessageForEdit={handleSelectMessageForEdit} handleEditMessage={handleEditMessage} onShowEditSelector={() => navigateTo("edit")} /> diff --git a/extensions/cli/src/ui/components/ScreenContent.tsx b/extensions/cli/src/ui/components/ScreenContent.tsx index 77aa58ee4..3be048246 100644 --- a/extensions/cli/src/ui/components/ScreenContent.tsx +++ b/extensions/cli/src/ui/components/ScreenContent.tsx @@ -47,7 +47,6 @@ interface ScreenContentProps { onImageInClipboardChange?: (hasImage: boolean) => void; diffContent?: string; chatHistory?: ChatHistoryItem[]; - handleSelectMessageForEdit?: (originalIndex: number) => void; handleEditMessage?: (messageIndex: number, newContent: string) => void; onShowEditSelector?: () => void; } @@ -84,7 +83,6 @@ export const ScreenContent: React.FC = ({ onImageInClipboardChange, diffContent, chatHistory = [], - handleSelectMessageForEdit, handleEditMessage, onShowEditSelector, }) => { @@ -177,7 +175,6 @@ export const ScreenContent: React.FC = ({ return ( { if (handleEditMessage) { handleEditMessage(messageIndex, newContent); diff --git a/extensions/cli/src/ui/hooks/useChat.ts b/extensions/cli/src/ui/hooks/useChat.ts index 6c7fd65e4..2b1028be1 100644 --- a/extensions/cli/src/ui/hooks/useChat.ts +++ b/extensions/cli/src/ui/hooks/useChat.ts @@ -457,7 +457,11 @@ export function useChat({ message: string, imageMap?: Map, isQueuedMessage: boolean = false, + baseHistory?: ChatHistoryItem[], ) => { + // Use baseHistory if provided (e.g., when editing a message), + // otherwise use current chatHistory + const currentHistory = baseHistory ?? chatHistory; // Handle special commands const handled = await handleSpecialCommands({ message, @@ -526,7 +530,7 @@ export function useChat({ let currentChatHistory, currentCompactionIndex; try { const result = await handleAutoCompaction({ - chatHistory, + chatHistory: currentHistory, model, llmApi, compactionIndex, @@ -593,7 +597,7 @@ export function useChat({ let currentChatHistory, currentCompactionIndex; try { const result = await handleAutoCompaction({ - chatHistory, + chatHistory: currentHistory, model, llmApi, compactionIndex, @@ -723,26 +727,23 @@ export function useChat({ setQueuedMessages([]); }; - const handleSelectMessageForEdit = ( - originalIndex: number, - onComplete?: () => void, + const handleEditMessage = async ( + messageIndex: number, + newContent: string, ) => { - logger.debug("handleSelectMessageForEdit called", { - originalIndex, + logger.debug("handleEditMessage called", { + messageIndex, currentHistoryLength: chatHistory.length, }); - // Rewind chat history to the selected message index (up to and including the message) - const rewindedHistory = chatHistory.slice(0, originalIndex + 1); + // Rewind chat history to exclude the message being edited + const rewindedHistory = chatHistory.slice(0, messageIndex); - logger.debug("Rewinding history", { + logger.debug("Rewinding history for edit", { from: chatHistory.length, to: rewindedHistory.length, }); - // Update the chat history with the rewound version - setChatHistory(rewindedHistory); - // Clear any queued messages setQueuedMessages([]); @@ -757,28 +758,9 @@ export function useChat({ onRefreshStatic(); } - // Call onComplete callback if provided - if (onComplete) { - onComplete(); - } - }; - - const handleEditMessage = async ( - messageIndex: number, - newContent: string, - ) => { - // At this point, history is already rewound to the selected message - // We need to rewind one more step to exclude the original message, then resubmit - const rewindedHistory = chatHistory.slice(0, messageIndex); - - // Update the chat history to exclude the message being edited - setChatHistory(rewindedHistory); - - // Update the session - updateSessionHistory(rewindedHistory); - - // Resubmit the edited message - await processMessage(newContent, undefined, false); + // Resubmit the edited message with the rewound history as the base + // This ensures processMessage uses the correct base history + await processMessage(newContent, undefined, false, rewindedHistory); }; const handleToolPermissionResponse = async ( @@ -863,7 +845,6 @@ export function useChat({ handleInterrupt, handleFileAttached, resetChatHistory, - handleSelectMessageForEdit, handleEditMessage, handleToolPermissionResponse, };