|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { z } from 'zod'; |
| 8 | +import type { LocalAgentDefinition } from './types.js'; |
| 9 | + |
| 10 | +const MemoryManagerSchema = z.object({ |
| 11 | + response: z |
| 12 | + .string() |
| 13 | + .describe('A summary of the memory operations performed.'), |
| 14 | +}); |
| 15 | + |
| 16 | +const MEMORY_MANAGER_SYSTEM_PROMPT = ` |
| 17 | +You are a memory management agent. You maintain the user's memories stored in |
| 18 | +GEMINI.md files. |
| 19 | +
|
| 20 | +# Memory Hierarchy |
| 21 | +
|
| 22 | +## Global (~/.gemini/) |
| 23 | +- \`~/.gemini/GEMINI.md\` — Cross-project user preferences, key personal info, |
| 24 | + and habits that apply everywhere. |
| 25 | +
|
| 26 | +## Project (.gemini/) |
| 27 | +- \`.gemini/GEMINI.md\` — **Table of Contents** for project-specific context: |
| 28 | + architecture decisions, conventions, key contacts, and references to |
| 29 | + subdirectory GEMINI.md files for detailed context. |
| 30 | +- Subdirectory GEMINI.md files (e.g. \`src/GEMINI.md\`, \`docs/GEMINI.md\`) — |
| 31 | + detailed, domain-specific context for that part of the project. Reference |
| 32 | + these from the root \`.gemini/GEMINI.md\`. |
| 33 | +
|
| 34 | +## Routing |
| 35 | +
|
| 36 | +When adding a memory, route it to the right store: |
| 37 | +- User preferences, personal info, tool aliases, cross-project habits → **global** |
| 38 | +- Project architecture, conventions, workflows, team info → **project root** |
| 39 | +- Detailed context about a specific module or directory → **subdirectory |
| 40 | + GEMINI.md**, with a reference added to the project root |
| 41 | +
|
| 42 | +# Operations |
| 43 | +
|
| 44 | +Always read the target file(s) before writing. When editing any memory file, |
| 45 | +use \`grep_search\` to scan related files for duplicates before finishing. |
| 46 | +
|
| 47 | +1. **Adding** — Route to the correct store and file. Check for duplicates first. |
| 48 | +2. **Removing stale entries** — Delete outdated or unwanted entries. Clean up |
| 49 | + dangling references. |
| 50 | +3. **De-duplicating** — Search across related memory files for semantically |
| 51 | + equivalent entries. Keep the most informative version. |
| 52 | +4. **Organizing** — Restructure for clarity. Update references between files. |
| 53 | +
|
| 54 | +# Guidelines |
| 55 | +
|
| 56 | +- Keep GEMINI.md files lean — they are loaded into context every session. |
| 57 | +- Keep entries concise. |
| 58 | +- Edit surgically — preserve existing structure and user-authored content. |
| 59 | +- Always read before write to avoid overwriting concurrent changes. |
| 60 | +`.trim(); |
| 61 | + |
| 62 | +/** |
| 63 | + * A memory management agent that replaces the built-in save_memory tool. |
| 64 | + * It provides richer memory operations: adding, removing, de-duplicating, |
| 65 | + * and organizing memories in the global GEMINI.md file. |
| 66 | + * |
| 67 | + * Users can override this agent by placing a custom save_memory.md |
| 68 | + * in ~/.gemini/agents/ or .gemini/agents/. |
| 69 | + */ |
| 70 | +export const MemoryManagerAgent = (): LocalAgentDefinition< |
| 71 | + typeof MemoryManagerSchema |
| 72 | +> => ({ |
| 73 | + kind: 'local', |
| 74 | + name: 'save_memory', |
| 75 | + displayName: 'Memory Manager', |
| 76 | + description: |
| 77 | + 'Manages the global memory file (~/.gemini/GEMINI.md). Use this agent to add, remove, de-duplicate, and organize persistent user memories. It replaces the built-in save_memory tool with structured memory management including categorization and a table of contents.', |
| 78 | + inputConfig: { |
| 79 | + inputSchema: { |
| 80 | + type: 'object', |
| 81 | + properties: { |
| 82 | + request: { |
| 83 | + type: 'string', |
| 84 | + description: |
| 85 | + 'The memory operation to perform. Examples: "Remember that I prefer tabs over spaces", "Clean up stale memories", "De-duplicate my memories", "Organize my memories".', |
| 86 | + }, |
| 87 | + }, |
| 88 | + required: ['request'], |
| 89 | + }, |
| 90 | + }, |
| 91 | + outputConfig: { |
| 92 | + outputName: 'result', |
| 93 | + description: 'A summary of the memory operations performed.', |
| 94 | + schema: MemoryManagerSchema, |
| 95 | + }, |
| 96 | + modelConfig: { |
| 97 | + model: 'inherit', |
| 98 | + }, |
| 99 | + toolConfig: { |
| 100 | + tools: [ |
| 101 | + 'read_file', |
| 102 | + 'replace', |
| 103 | + 'write_file', |
| 104 | + 'list_directory', |
| 105 | + 'glob', |
| 106 | + 'grep_search', |
| 107 | + ], |
| 108 | + }, |
| 109 | + promptConfig: { |
| 110 | + systemPrompt: MEMORY_MANAGER_SYSTEM_PROMPT, |
| 111 | + query: '${request}', |
| 112 | + }, |
| 113 | + runConfig: { |
| 114 | + maxTimeMinutes: 5, |
| 115 | + maxTurns: 10, |
| 116 | + }, |
| 117 | +}); |
0 commit comments