Skip to content

Commit 1d2e27a

Browse files
authored
Fix: Update system instruction when GEMINI.md memory is loaded or refreshed (#12136)
1 parent 049a299 commit 1d2e27a

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

packages/cli/src/ui/commands/memoryCommand.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ describe('memoryCommand', () => {
182182
include: [],
183183
}),
184184
isTrustedFolder: () => false,
185+
updateSystemInstructionIfInitialized: vi
186+
.fn()
187+
.mockResolvedValue(undefined),
185188
};
186189

187190
mockContext = createMockCommandContext({

packages/cli/src/ui/commands/memoryCommand.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export const memoryCommand: SlashCommand = {
8686
const { memoryContent, fileCount } =
8787
await refreshServerHierarchicalMemory(config);
8888

89+
await config.updateSystemInstructionIfInitialized();
90+
8991
const successMessage =
9092
memoryContent.length > 0
9193
? `Memory refreshed successfully. Loaded ${memoryContent.length} characters from ${fileCount} file(s).`

packages/core/src/config/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,17 @@ export class Config {
10201020
return this.geminiClient;
10211021
}
10221022

1023+
/**
1024+
* Updates the system instruction with the latest user memory.
1025+
* Whenever the user memory (GEMINI.md files) is updated.
1026+
*/
1027+
async updateSystemInstructionIfInitialized(): Promise<void> {
1028+
const geminiClient = this.getGeminiClient();
1029+
if (geminiClient?.isInitialized()) {
1030+
await geminiClient.updateSystemInstruction();
1031+
}
1032+
}
1033+
10231034
getModelRouterService(): ModelRouterService {
10241035
return this.modelRouterService;
10251036
}

packages/core/src/core/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ export class GeminiClient {
168168
});
169169
}
170170

171+
async updateSystemInstruction(): Promise<void> {
172+
if (!this.isInitialized()) {
173+
return;
174+
}
175+
176+
const userMemory = this.config.getUserMemory();
177+
const systemInstruction = getCoreSystemPrompt(this.config, userMemory);
178+
this.getChat().setSystemInstruction(systemInstruction);
179+
}
180+
171181
async startChat(
172182
extraHistory?: Content[],
173183
resumedSessionData?: ResumedSessionData,

0 commit comments

Comments
 (0)