diff options
| author | Allen Hutchison <[email protected]> | 2025-05-31 12:49:28 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-05-31 12:49:28 -0700 |
| commit | 53bf77849760593cc9c1af9a4fb110a1a74acc4f (patch) | |
| tree | c188b0fa9c295faa198b65d80143491858c18048 /packages/core/src/tools | |
| parent | cbc1614b8441dc3dcf35cf4f2e6b3c3457045fcb (diff) | |
feat: allow custom filename for context files (#654)
Co-authored-by: N. Taylor Mullen <[email protected]>
Diffstat (limited to 'packages/core/src/tools')
| -rw-r--r-- | packages/core/src/tools/memoryTool.test.ts | 39 | ||||
| -rw-r--r-- | packages/core/src/tools/memoryTool.ts | 18 | ||||
| -rw-r--r-- | packages/core/src/tools/read-many-files.ts | 4 |
3 files changed, 54 insertions, 7 deletions
diff --git a/packages/core/src/tools/memoryTool.test.ts b/packages/core/src/tools/memoryTool.test.ts index 42b1329d..612a08dc 100644 --- a/packages/core/src/tools/memoryTool.test.ts +++ b/packages/core/src/tools/memoryTool.test.ts @@ -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 = { diff --git a/packages/core/src/tools/memoryTool.ts b/packages/core/src/tools/memoryTool.ts index 49dce59d..a0c62eae 100644 --- a/packages/core/src/tools/memoryTool.ts +++ b/packages/core/src/tools/memoryTool.ts @@ -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()); } /** diff --git a/packages/core/src/tools/read-many-files.ts b/packages/core/src/tools/read-many-files.ts index d826c9ba..4ba09ef0 100644 --- a/packages/core/src/tools/read-many-files.ts +++ b/packages/core/src/tools/read-many-files.ts @@ -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} ---'; |
