Refac: Centralize storage file management (#4078)

Co-authored-by: Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Yuki Okita
2025-08-20 10:55:47 +09:00
committed by GitHub
parent 1049d38845
commit 21c6480b65
50 changed files with 889 additions and 532 deletions

View File

@@ -7,9 +7,8 @@
import { useState, useEffect, useCallback } from 'react';
import * as fs from 'fs/promises';
import * as path from 'path';
import { isNodeError, getProjectTempDir } from '@google/gemini-cli-core';
import { isNodeError, Storage } from '@google/gemini-cli-core';
const HISTORY_FILE = 'shell_history';
const MAX_HISTORY_LENGTH = 100;
export interface UseShellHistoryReturn {
@@ -20,9 +19,12 @@ export interface UseShellHistoryReturn {
resetHistoryPosition: () => void;
}
async function getHistoryFilePath(projectRoot: string): Promise<string> {
const historyDir = getProjectTempDir(projectRoot);
return path.join(historyDir, HISTORY_FILE);
async function getHistoryFilePath(
projectRoot: string,
configStorage?: Storage,
): Promise<string> {
const storage = configStorage ?? new Storage(projectRoot);
return storage.getHistoryFilePath();
}
// Handle multiline commands
@@ -67,20 +69,23 @@ async function writeHistoryFile(
}
}
export function useShellHistory(projectRoot: string): UseShellHistoryReturn {
export function useShellHistory(
projectRoot: string,
storage?: Storage,
): UseShellHistoryReturn {
const [history, setHistory] = useState<string[]>([]);
const [historyIndex, setHistoryIndex] = useState(-1);
const [historyFilePath, setHistoryFilePath] = useState<string | null>(null);
useEffect(() => {
async function loadHistory() {
const filePath = await getHistoryFilePath(projectRoot);
const filePath = await getHistoryFilePath(projectRoot, storage);
setHistoryFilePath(filePath);
const loadedHistory = await readHistoryFile(filePath);
setHistory(loadedHistory.reverse()); // Newest first
}
loadHistory();
}, [projectRoot]);
}, [projectRoot, storage]);
const addCommandToHistory = useCallback(
(command: string) => {