mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 17:27:54 +00:00
Refac: Centralize storage file management (#4078)
Co-authored-by: Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user