diff --git a/packages/cli/src/ui/commands/memoryCommand.test.ts b/packages/cli/src/ui/commands/memoryCommand.test.ts index f555a9c925..a1d9642d37 100644 --- a/packages/cli/src/ui/commands/memoryCommand.test.ts +++ b/packages/cli/src/ui/commands/memoryCommand.test.ts @@ -182,6 +182,9 @@ describe('memoryCommand', () => { include: [], }), isTrustedFolder: () => false, + updateSystemInstructionIfInitialized: vi + .fn() + .mockResolvedValue(undefined), }; mockContext = createMockCommandContext({ diff --git a/packages/cli/src/ui/commands/memoryCommand.ts b/packages/cli/src/ui/commands/memoryCommand.ts index 61d0ec4467..953a9e7633 100644 --- a/packages/cli/src/ui/commands/memoryCommand.ts +++ b/packages/cli/src/ui/commands/memoryCommand.ts @@ -86,6 +86,8 @@ export const memoryCommand: SlashCommand = { const { memoryContent, fileCount } = await refreshServerHierarchicalMemory(config); + await config.updateSystemInstructionIfInitialized(); + const successMessage = memoryContent.length > 0 ? `Memory refreshed successfully. Loaded ${memoryContent.length} characters from ${fileCount} file(s).` diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index c7d1f60edd..6c2a962cfc 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -1020,6 +1020,17 @@ export class Config { return this.geminiClient; } + /** + * Updates the system instruction with the latest user memory. + * Whenever the user memory (GEMINI.md files) is updated. + */ + async updateSystemInstructionIfInitialized(): Promise { + const geminiClient = this.getGeminiClient(); + if (geminiClient?.isInitialized()) { + await geminiClient.updateSystemInstruction(); + } + } + getModelRouterService(): ModelRouterService { return this.modelRouterService; } diff --git a/packages/core/src/core/client.ts b/packages/core/src/core/client.ts index 165ce348d5..e2addd0925 100644 --- a/packages/core/src/core/client.ts +++ b/packages/core/src/core/client.ts @@ -168,6 +168,16 @@ export class GeminiClient { }); } + async updateSystemInstruction(): Promise { + if (!this.isInitialized()) { + return; + } + + const userMemory = this.config.getUserMemory(); + const systemInstruction = getCoreSystemPrompt(this.config, userMemory); + this.getChat().setSystemInstruction(systemInstruction); + } + async startChat( extraHistory?: Content[], resumedSessionData?: ResumedSessionData,