diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index 046da1021..7c1ace735 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -301,7 +301,9 @@ function TipTapEditor(props: TipTapEditorProps) { } } - const { prevRef, nextRef, addRef } = useInputHistory(props.historyKey); + // Ensure we're using the correct history key - chat for main input, edit for edit mode + const historyKey = props.historyKey === 'edit' ? 'edit' : 'chat'; + const { prevRef, nextRef, addRef } = useInputHistory(historyKey); const editor: Editor | null = useEditor({ extensions: [ @@ -660,9 +662,9 @@ function TipTapEditor(props: TipTapEditorProps) { return; } - if (props.isMainInput) { - addRef.current(json); - } + // Always add to input history regardless of input type + // This ensures that both chat and edit modes maintain their own history + addRef.current(json); props.onEnter(json, modifiers, editor); }, diff --git a/gui/src/hooks/useInputHistory.ts b/gui/src/hooks/useInputHistory.ts index 3d63886fe..d936e5e9c 100644 --- a/gui/src/hooks/useInputHistory.ts +++ b/gui/src/hooks/useInputHistory.ts @@ -1,5 +1,5 @@ import { JSONContent } from "@tiptap/react"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import { getLocalStorage, setLocalStorage } from "../util/localStorage"; import useUpdatingRef from "./useUpdatingRef"; @@ -10,58 +10,112 @@ const emptyJsonContent = () => ({ const MAX_HISTORY_LENGTH = 100; +// Completely isolated memory stores for different history types +// This ensures no crossover between edit and chat histories +const CHAT_HISTORY_STORE: JSONContent[] = []; +const EDIT_HISTORY_STORE: JSONContent[] = []; + +// Initialize the history stores from localStorage (only do this once) +let storesInitialized = false; +function initializeStores() { + + if (!storesInitialized) { + // Initialize chat history + const chatHistory = getLocalStorage('inputHistory_chat'); + if (chatHistory) { + CHAT_HISTORY_STORE.push(...chatHistory.slice(-MAX_HISTORY_LENGTH)); + } + + // Initialize edit history + const editHistory = getLocalStorage('inputHistory_edit'); + if (editHistory) { + EDIT_HISTORY_STORE.push(...editHistory.slice(-MAX_HISTORY_LENGTH)); + } + + storesInitialized = true; + } +} + + +// Initialize stores immediately +initializeStores(); + +/** + * Custom hook for managing input history with complete separation between history types + */ export function useInputHistory(historyKey: string) { - const [inputHistory, setInputHistory] = useState( - getLocalStorage(`inputHistory_${historyKey}`)?.slice(-MAX_HISTORY_LENGTH) ?? - [], - ); - const [pendingInput, setPendingInput] = - useState(emptyJsonContent()); + // Determine which history store to use + const historyStore = historyKey === 'edit' ? EDIT_HISTORY_STORE : CHAT_HISTORY_STORE; + const storageKey = historyKey === 'edit' ? 'inputHistory_edit' as const : 'inputHistory_chat' as const; + + // Use a reference to the appropriate history store + const [inputHistory, setInputHistory] = useState(historyStore); + const [pendingInput, setPendingInput] = useState(emptyJsonContent()); const [currentIndex, setCurrentIndex] = useState(inputHistory.length); + + // Keep the view of history updated when the underlying store changes + useEffect(() => { + setInputHistory([...historyStore]); + setCurrentIndex(historyStore.length); + }, [historyStore.length]); function prev(currentInput: JSONContent) { - let index = currentIndex; - - if (index === inputHistory.length) { + if (currentIndex === inputHistory.length) { setPendingInput(currentInput); } - if (index > 0 && index <= inputHistory.length) { - setCurrentIndex((prevState) => prevState - 1); - return inputHistory[index - 1]; + if (currentIndex > 0) { + setCurrentIndex(currentIndex - 1); + return inputHistory[currentIndex - 1]; } + + // Stay at the first history item if we're already there + if (currentIndex === 0 && inputHistory.length > 0) { + return inputHistory[0]; + } + + return currentInput; } function next() { - let index = currentIndex; - if (index >= 0 && index < inputHistory.length) { - setCurrentIndex((prevState) => prevState + 1); - if (index === inputHistory.length - 1) { + if (currentIndex < inputHistory.length) { + setCurrentIndex(currentIndex + 1); + + if (currentIndex === inputHistory.length - 1) { return pendingInput; } - return inputHistory[index + 1]; + + return inputHistory[currentIndex + 1]; } + + // Stay at pending input if we're already at the end + return pendingInput; } function add(inputValue: JSONContent) { setPendingInput(emptyJsonContent()); + // Don't add duplicates if ( - JSON.stringify(inputHistory[inputHistory.length - 1]) === - JSON.stringify(inputValue) + historyStore.length > 0 && + JSON.stringify(historyStore[historyStore.length - 1]) === JSON.stringify(inputValue) ) { - setCurrentIndex(inputHistory.length); + setCurrentIndex(historyStore.length); return; } - setCurrentIndex(inputHistory.length + 1); - setInputHistory((prev) => { - return [...prev, inputValue].slice(-MAX_HISTORY_LENGTH); - }); - setLocalStorage( - `inputHistory_${historyKey}`, - [...inputHistory, inputValue].slice(-MAX_HISTORY_LENGTH), - ); + // Update the appropriate history store + while (historyStore.length >= MAX_HISTORY_LENGTH) { + historyStore.shift(); // Remove oldest items if we exceed the limit + } + historyStore.push(inputValue); + + // Update the view of history + setInputHistory([...historyStore]); + setCurrentIndex(historyStore.length); + + // Update localStorage + setLocalStorage(storageKey, [...historyStore]); } const prevRef = useUpdatingRef(prev, [inputHistory]);