mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-24 12:04:56 -07:00
Fixes /clear command to preserve input history for up-arrow navigation while still clearing the context window and screen (#14182)
This commit is contained in:
@@ -118,6 +118,7 @@ import { isWorkspaceTrusted } from '../config/trustedFolders.js';
|
||||
import { useAlternateBuffer } from './hooks/useAlternateBuffer.js';
|
||||
import { useSettings } from './contexts/SettingsContext.js';
|
||||
import { enableSupportedProtocol } from './utils/kittyProtocolDetector.js';
|
||||
import { useInputHistoryStore } from './hooks/useInputHistoryStore.js';
|
||||
import { enableBracketedPaste } from './utils/bracketedPaste.js';
|
||||
|
||||
const WARNING_PROMPT_DURATION_MS = 1000;
|
||||
@@ -252,7 +253,8 @@ export const AppContainer = (props: AppContainerProps) => {
|
||||
const [isConfigInitialized, setConfigInitialized] = useState(false);
|
||||
|
||||
const logger = useLogger(config.storage);
|
||||
const [userMessages, setUserMessages] = useState<string[]>([]);
|
||||
const { inputHistory, addInput, initializeFromLogger } =
|
||||
useInputHistoryStore();
|
||||
|
||||
// Terminal and layout hooks
|
||||
const { columns: terminalWidth, rows: terminalHeight } = useTerminalSize();
|
||||
@@ -343,35 +345,10 @@ export const AppContainer = (props: AppContainerProps) => {
|
||||
shellModeActive,
|
||||
});
|
||||
|
||||
// Initialize input history from logger (past sessions)
|
||||
useEffect(() => {
|
||||
const fetchUserMessages = async () => {
|
||||
const pastMessagesRaw = (await logger?.getPreviousUserMessages()) || [];
|
||||
const currentSessionUserMessages = historyManager.history
|
||||
.filter(
|
||||
(item): item is HistoryItem & { type: 'user'; text: string } =>
|
||||
item.type === 'user' &&
|
||||
typeof item.text === 'string' &&
|
||||
item.text.trim() !== '',
|
||||
)
|
||||
.map((item) => item.text)
|
||||
.reverse();
|
||||
const combinedMessages = [
|
||||
...currentSessionUserMessages,
|
||||
...pastMessagesRaw,
|
||||
];
|
||||
const deduplicatedMessages: string[] = [];
|
||||
if (combinedMessages.length > 0) {
|
||||
deduplicatedMessages.push(combinedMessages[0]);
|
||||
for (let i = 1; i < combinedMessages.length; i++) {
|
||||
if (combinedMessages[i] !== combinedMessages[i - 1]) {
|
||||
deduplicatedMessages.push(combinedMessages[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
setUserMessages(deduplicatedMessages.reverse());
|
||||
};
|
||||
fetchUserMessages();
|
||||
}, [historyManager.history, logger]);
|
||||
initializeFromLogger(logger);
|
||||
}, [logger, initializeFromLogger]);
|
||||
|
||||
const refreshStatic = useCallback(() => {
|
||||
if (!isAlternateBuffer) {
|
||||
@@ -710,7 +687,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
const lastHistoryUserMsg = historyManager.history.findLast(
|
||||
(h) => h.type === 'user',
|
||||
);
|
||||
const lastUserMsg = userMessages.at(-1);
|
||||
const lastUserMsg = inputHistory.at(-1);
|
||||
|
||||
if (
|
||||
!lastHistoryUserMsg ||
|
||||
@@ -721,7 +698,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
setPendingRestorePrompt(false);
|
||||
}
|
||||
}
|
||||
}, [pendingRestorePrompt, userMessages, historyManager.history]);
|
||||
}, [pendingRestorePrompt, inputHistory, historyManager.history]);
|
||||
|
||||
const {
|
||||
streamingState,
|
||||
@@ -785,7 +762,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
return;
|
||||
}
|
||||
|
||||
const lastUserMessage = userMessages.at(-1);
|
||||
const lastUserMessage = inputHistory.at(-1);
|
||||
let textToSet = shouldRestorePrompt ? lastUserMessage || '' : '';
|
||||
|
||||
const queuedText = getQueuedMessagesText();
|
||||
@@ -800,7 +777,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
},
|
||||
[
|
||||
buffer,
|
||||
userMessages,
|
||||
inputHistory,
|
||||
getQueuedMessagesText,
|
||||
clearQueue,
|
||||
pendingSlashCommandHistoryItems,
|
||||
@@ -811,8 +788,9 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
const handleFinalSubmit = useCallback(
|
||||
(submittedValue: string) => {
|
||||
addMessage(submittedValue);
|
||||
addInput(submittedValue); // Track input for up-arrow history
|
||||
},
|
||||
[addMessage],
|
||||
[addMessage, addInput],
|
||||
);
|
||||
|
||||
const handleClearScreen = useCallback(() => {
|
||||
@@ -1438,7 +1416,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
pendingGeminiHistoryItems,
|
||||
thought,
|
||||
shellModeActive,
|
||||
userMessages,
|
||||
userMessages: inputHistory,
|
||||
buffer,
|
||||
inputWidth,
|
||||
suggestionsWidth,
|
||||
@@ -1529,7 +1507,7 @@ Logging in with Google... Restarting Gemini CLI to continue.
|
||||
pendingGeminiHistoryItems,
|
||||
thought,
|
||||
shellModeActive,
|
||||
userMessages,
|
||||
inputHistory,
|
||||
buffer,
|
||||
inputWidth,
|
||||
suggestionsWidth,
|
||||
|
||||
Reference in New Issue
Block a user