initialize FileDiscoveryService once (#1029)

This commit is contained in:
Anas H. Sulaiman
2025-06-13 20:25:59 -04:00
committed by GitHub
parent 209381f06f
commit 8eb505fbba
7 changed files with 42 additions and 12 deletions

View File

@@ -86,6 +86,7 @@ export interface ConfigParameters {
checkpoint?: boolean;
proxy?: string;
cwd: string;
fileDiscoveryService?: FileDiscoveryService;
}
export class Config {
@@ -152,6 +153,7 @@ export class Config {
this.checkpoint = params.checkpoint ?? false;
this.proxy = params.proxy;
this.cwd = params.cwd ?? process.cwd();
this.fileDiscoveryService = params.fileDiscoveryService ?? null;
if (params.contextFileName) {
setGeminiMdFilename(params.contextFileName);

View File

@@ -47,14 +47,17 @@ export class GitIgnoreParser implements GitIgnoreFilter {
}
async loadPatterns(patternsFileName: string): Promise<void> {
const content = await fs.readFile(
path.join(this.projectRoot, patternsFileName),
'utf-8',
);
const patternsFilePath = path.join(this.projectRoot, patternsFileName);
const content = await fs.readFile(patternsFilePath, 'utf-8');
const patterns = content
.split('\n')
.map((p) => p.trim())
.filter((p) => p !== '' && !p.startsWith('#'));
if (patterns.length > 0) {
console.log(
`Loaded ${patterns.length} patterns from ${patternsFilePath}`,
);
}
this.addPatterns(patterns);
}

View File

@@ -17,6 +17,7 @@ import {
getCurrentGeminiMdFilename,
DEFAULT_CONTEXT_FILENAME,
} from '../tools/memoryTool.js';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
const ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST = DEFAULT_CONTEXT_FILENAME;
@@ -43,6 +44,7 @@ describe('loadServerHierarchicalMemory', () => {
let GLOBAL_GEMINI_DIR: string;
let GLOBAL_GEMINI_FILE: string; // Defined in beforeEach
const fileService = new FileDiscoveryService(PROJECT_ROOT);
beforeEach(() => {
vi.resetAllMocks();
// Set environment variables to indicate test environment
@@ -69,6 +71,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
expect(memoryContent).toBe('');
expect(fileCount).toBe(0);
@@ -95,6 +98,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
expect(memoryContent).toBe(
@@ -125,6 +129,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
expect(memoryContent).toBe(
@@ -167,6 +172,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const expectedContent =
`--- Context from: ${path.relative(CWD, projectRootCustomFile)} ---\nProject root custom memory\n--- End of Context from: ${path.relative(CWD, projectRootCustomFile)} ---\n\n` +
@@ -231,6 +237,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const expectedContent =
`--- Context from: ${customFilename} ---\nCWD custom memory\n--- End of Context from: ${customFilename} ---\n\n` +
@@ -277,6 +284,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const expectedContent =
`--- Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\nProject root memory\n--- End of Context from: ${path.relative(CWD, projectRootGeminiFile)} ---\n\n` +
@@ -345,6 +353,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const expectedContent =
`--- Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\nCWD memory\n--- End of Context from: ${ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST} ---\n\n` +
@@ -438,6 +447,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const relPathGlobal = path.relative(CWD, GLOBAL_GEMINI_FILE);
@@ -520,6 +530,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
);
const expectedContent = `--- Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---\nMy code memory\n--- End of Context from: ${path.join('my_code', ORIGINAL_GEMINI_MD_FILENAME_CONST_FOR_TEST)} ---`;
@@ -556,7 +567,7 @@ describe('loadServerHierarchicalMemory', () => {
}) as unknown as typeof fsPromises.readdir);
mockFs.access.mockRejectedValue(new Error('not found'));
await loadServerHierarchicalMemory(CWD, true);
await loadServerHierarchicalMemory(CWD, true, fileService);
expect(consoleDebugSpy).toHaveBeenCalledWith(
expect.stringContaining('[DEBUG] [BfsFileSearch]'),
@@ -583,6 +594,7 @@ describe('loadServerHierarchicalMemory', () => {
const { memoryContent, fileCount } = await loadServerHierarchicalMemory(
CWD,
false,
fileService,
[extensionFilePath],
);

View File

@@ -82,6 +82,7 @@ async function getGeminiMdFilePathsInternal(
currentWorkingDirectory: string,
userHomePath: string,
debugMode: boolean,
fileService: FileDiscoveryService,
extensionContextFilePaths: string[] = [],
): Promise<string[]> {
const allPaths = new Set<string>();
@@ -179,8 +180,6 @@ async function getGeminiMdFilePathsInternal(
}
upwardPaths.forEach((p) => allPaths.add(p));
const fileService = new FileDiscoveryService(projectRoot || resolvedCwd);
await fileService.initialize();
const downwardPaths = await bfsFileSearch(resolvedCwd, {
fileName: geminiMdFilename,
maxDirs: MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY,
@@ -272,6 +271,7 @@ function concatenateInstructions(
export async function loadServerHierarchicalMemory(
currentWorkingDirectory: string,
debugMode: boolean,
fileService: FileDiscoveryService,
extensionContextFilePaths: string[] = [],
): Promise<{ memoryContent: string; fileCount: number }> {
if (debugMode)
@@ -285,6 +285,7 @@ export async function loadServerHierarchicalMemory(
currentWorkingDirectory,
userHomePath,
debugMode,
fileService,
extensionContextFilePaths,
);
if (filePaths.length === 0) {