improv: architecture

This commit is contained in:
Tomasz Stefaniak 2025-10-07 13:22:19 -07:00
parent 23ba9a5400
commit 9de20396ce
4 changed files with 18 additions and 54 deletions

View file

@ -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();

View file

@ -227,7 +227,6 @@ const TUIChat: React.FC<TUIChatProps> = ({
handleInterrupt,
handleFileAttached,
resetChatHistory,
handleSelectMessageForEdit,
handleEditMessage,
handleToolPermissionResponse,
} = useChat({
@ -382,7 +381,6 @@ const TUIChat: React.FC<TUIChatProps> = ({
onImageInClipboardChange={setHasImageInClipboard}
diffContent={diffContent}
chatHistory={chatHistory}
handleSelectMessageForEdit={handleSelectMessageForEdit}
handleEditMessage={handleEditMessage}
onShowEditSelector={() => navigateTo("edit")}
/>

View file

@ -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<ScreenContentProps> = ({
onImageInClipboardChange,
diffContent,
chatHistory = [],
handleSelectMessageForEdit,
handleEditMessage,
onShowEditSelector,
}) => {
@ -177,7 +175,6 @@ export const ScreenContent: React.FC<ScreenContentProps> = ({
return (
<EditMessageSelector
chatHistory={chatHistory}
onMessageSelected={handleSelectMessageForEdit}
onEdit={(messageIndex, newContent) => {
if (handleEditMessage) {
handleEditMessage(messageIndex, newContent);

View file

@ -457,7 +457,11 @@ export function useChat({
message: string,
imageMap?: Map<string, Buffer>,
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,
};