feat: allow custom filename for context files (#654)

Co-authored-by: N. Taylor Mullen <ntaylormullen@google.com>
This commit is contained in:
Allen Hutchison
2025-05-31 12:49:28 -07:00
committed by GitHub
parent cbc1614b84
commit 53bf778497
15 changed files with 1710 additions and 888 deletions

View File

@@ -5,7 +5,12 @@
*/
import { vi, describe, it, expect, beforeEach, afterEach, Mock } from 'vitest';
import { MemoryTool } from './memoryTool.js';
import {
MemoryTool,
setGeminiMdFilename,
getCurrentGeminiMdFilename,
DEFAULT_CONTEXT_FILENAME,
} from './memoryTool.js';
import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
@@ -50,10 +55,33 @@ describe('MemoryTool', () => {
afterEach(() => {
vi.restoreAllMocks();
// Reset GEMINI_MD_FILENAME to its original value after each test
setGeminiMdFilename(DEFAULT_CONTEXT_FILENAME);
});
describe('setGeminiMdFilename', () => {
it('should update currentGeminiMdFilename when a valid new name is provided', () => {
const newName = 'CUSTOM_CONTEXT.md';
setGeminiMdFilename(newName);
expect(getCurrentGeminiMdFilename()).toBe(newName);
});
it('should not update currentGeminiMdFilename if the new name is empty or whitespace', () => {
const initialName = getCurrentGeminiMdFilename(); // Get current before trying to change
setGeminiMdFilename(' ');
expect(getCurrentGeminiMdFilename()).toBe(initialName);
setGeminiMdFilename('');
expect(getCurrentGeminiMdFilename()).toBe(initialName);
});
});
describe('performAddMemoryEntry (static method)', () => {
const testFilePath = path.join('/mock/home', '.gemini', 'GEMINI.md');
const testFilePath = path.join(
'/mock/home',
'.gemini',
DEFAULT_CONTEXT_FILENAME, // Use the default for basic tests
);
it('should create section and save a fact if file does not exist', async () => {
mockFsAdapter.readFile.mockRejectedValue({ code: 'ENOENT' }); // Simulate file not found
@@ -168,7 +196,12 @@ describe('MemoryTool', () => {
it('should call performAddMemoryEntry with correct parameters and return success', async () => {
const params = { fact: 'The sky is blue' };
const result = await memoryTool.execute(params, mockAbortSignal);
const expectedFilePath = path.join('/mock/home', '.gemini', 'GEMINI.md');
// Use getCurrentGeminiMdFilename for the default expectation before any setGeminiMdFilename calls in a test
const expectedFilePath = path.join(
'/mock/home',
'.gemini',
getCurrentGeminiMdFilename(), // This will be DEFAULT_CONTEXT_FILENAME unless changed by a test
);
// For this test, we expect the actual fs methods to be passed
const expectedFsArgument = {

View File

@@ -46,15 +46,29 @@ Do NOT use this tool:
`;
export const GEMINI_CONFIG_DIR = '.gemini';
export const GEMINI_MD_FILENAME = 'GEMINI.md';
export const DEFAULT_CONTEXT_FILENAME = 'GEMINI.md';
export const MEMORY_SECTION_HEADER = '## Gemini Added Memories';
// This variable will hold the currently configured filename for GEMINI.md context files.
// It defaults to DEFAULT_CONTEXT_FILENAME but can be overridden by setGeminiMdFilename.
let currentGeminiMdFilename = DEFAULT_CONTEXT_FILENAME;
export function setGeminiMdFilename(newFilename: string): void {
if (newFilename && newFilename.trim() !== '') {
currentGeminiMdFilename = newFilename.trim();
}
}
export function getCurrentGeminiMdFilename(): string {
return currentGeminiMdFilename;
}
interface SaveMemoryParams {
fact: string;
}
function getGlobalMemoryFilePath(): string {
return path.join(homedir(), GEMINI_CONFIG_DIR, GEMINI_MD_FILENAME);
return path.join(homedir(), GEMINI_CONFIG_DIR, getCurrentGeminiMdFilename());
}
/**

View File

@@ -9,7 +9,7 @@ import { SchemaValidator } from '../utils/schemaValidator.js';
import { getErrorMessage } from '../utils/errors.js';
import * as path from 'path';
import fg from 'fast-glob';
import { GEMINI_MD_FILENAME } from './memoryTool.js';
import { getCurrentGeminiMdFilename } from './memoryTool.js';
import {
detectFileType,
processSingleFileContent,
@@ -98,7 +98,7 @@ const DEFAULT_EXCLUDES: string[] = [
'**/*.odp',
'**/*.DS_Store',
'**/.env',
`**/${GEMINI_MD_FILENAME}`,
`**/${getCurrentGeminiMdFilename()}`,
];
const DEFAULT_OUTPUT_SEPARATOR_FORMAT = '--- {filePath} ---';