|
| 1 | +# Memory Import Processor |
| 2 | + |
| 3 | +The Memory Import Processor is a feature that allows you to modularize your GEMINI.md files by importing content from other markdown files using the `@file.md` syntax. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This feature enables you to break down large GEMINI.md files into smaller, more manageable components that can be reused across different contexts. The import processor supports both relative and absolute paths, with built-in safety features to prevent circular imports and ensure file access security. |
| 8 | + |
| 9 | +## Important Limitations |
| 10 | + |
| 11 | +**This feature only supports `.md` (markdown) files.** Attempting to import files with other extensions (like `.txt`, `.json`, etc.) will result in a warning and the import will fail. |
| 12 | + |
| 13 | +## Syntax |
| 14 | + |
| 15 | +Use the `@` symbol followed by the path to the markdown file you want to import: |
| 16 | + |
| 17 | +```markdown |
| 18 | +# Main GEMINI.md file |
| 19 | + |
| 20 | +This is the main content. |
| 21 | + |
| 22 | +@./components/instructions.md |
| 23 | + |
| 24 | +More content here. |
| 25 | + |
| 26 | +@./shared/configuration.md |
| 27 | +``` |
| 28 | + |
| 29 | +## Supported Path Formats |
| 30 | + |
| 31 | +### Relative Paths |
| 32 | + |
| 33 | +- `@./file.md` - Import from the same directory |
| 34 | +- `@../file.md` - Import from parent directory |
| 35 | +- `@./components/file.md` - Import from subdirectory |
| 36 | + |
| 37 | +### Absolute Paths |
| 38 | + |
| 39 | +- `@/absolute/path/to/file.md` - Import using absolute path |
| 40 | + |
| 41 | +## Examples |
| 42 | + |
| 43 | +### Basic Import |
| 44 | + |
| 45 | +```markdown |
| 46 | +# My GEMINI.md |
| 47 | + |
| 48 | +Welcome to my project! |
| 49 | + |
| 50 | +@./getting-started.md |
| 51 | + |
| 52 | +## Features |
| 53 | + |
| 54 | +@./features/overview.md |
| 55 | +``` |
| 56 | + |
| 57 | +### Nested Imports |
| 58 | + |
| 59 | +The imported files can themselves contain imports, creating a nested structure: |
| 60 | + |
| 61 | +```markdown |
| 62 | +# main.md |
| 63 | + |
| 64 | +@./header.md |
| 65 | +@./content.md |
| 66 | +@./footer.md |
| 67 | +``` |
| 68 | + |
| 69 | +```markdown |
| 70 | +# header.md |
| 71 | + |
| 72 | +# Project Header |
| 73 | + |
| 74 | +@./shared/title.md |
| 75 | +``` |
| 76 | + |
| 77 | +## Safety Features |
| 78 | + |
| 79 | +### Circular Import Detection |
| 80 | + |
| 81 | +The processor automatically detects and prevents circular imports: |
| 82 | + |
| 83 | +```markdown |
| 84 | +# file-a.md |
| 85 | + |
| 86 | +@./file-b.md |
| 87 | + |
| 88 | +# file-b.md |
| 89 | + |
| 90 | +@./file-a.md <!-- This will be detected and prevented --> |
| 91 | +``` |
| 92 | + |
| 93 | +### File Access Security |
| 94 | + |
| 95 | +The `validateImportPath` function ensures that imports are only allowed from specified directories, preventing access to sensitive files outside the allowed scope. |
| 96 | + |
| 97 | +### Maximum Import Depth |
| 98 | + |
| 99 | +To prevent infinite recursion, there's a configurable maximum import depth (default: 10 levels). |
| 100 | + |
| 101 | +## Error Handling |
| 102 | + |
| 103 | +### Non-MD File Attempts |
| 104 | + |
| 105 | +If you try to import a non-markdown file, you'll see a warning: |
| 106 | + |
| 107 | +```markdown |
| 108 | +@./instructions.txt <!-- This will show a warning and fail --> |
| 109 | +``` |
| 110 | + |
| 111 | +Console output: |
| 112 | + |
| 113 | +``` |
| 114 | +[WARN] [ImportProcessor] Import processor only supports .md files. Attempting to import non-md file: ./instructions.txt. This will fail. |
| 115 | +``` |
| 116 | + |
| 117 | +### Missing Files |
| 118 | + |
| 119 | +If a referenced file doesn't exist, the import will fail gracefully with an error comment in the output. |
| 120 | + |
| 121 | +### File Access Errors |
| 122 | + |
| 123 | +Permission issues or other file system errors are handled gracefully with appropriate error messages. |
| 124 | + |
| 125 | +## API Reference |
| 126 | + |
| 127 | +### `processImports(content, basePath, debugMode?, importState?)` |
| 128 | + |
| 129 | +Processes import statements in GEMINI.md content. |
| 130 | + |
| 131 | +**Parameters:** |
| 132 | + |
| 133 | +- `content` (string): The content to process for imports |
| 134 | +- `basePath` (string): The directory path where the current file is located |
| 135 | +- `debugMode` (boolean, optional): Whether to enable debug logging (default: false) |
| 136 | +- `importState` (ImportState, optional): State tracking for circular import prevention |
| 137 | + |
| 138 | +**Returns:** Promise<string> - Processed content with imports resolved |
| 139 | + |
| 140 | +### `validateImportPath(importPath, basePath, allowedDirectories)` |
| 141 | + |
| 142 | +Validates import paths to ensure they are safe and within allowed directories. |
| 143 | + |
| 144 | +**Parameters:** |
| 145 | + |
| 146 | +- `importPath` (string): The import path to validate |
| 147 | +- `basePath` (string): The base directory for resolving relative paths |
| 148 | +- `allowedDirectories` (string[]): Array of allowed directory paths |
| 149 | + |
| 150 | +**Returns:** boolean - Whether the import path is valid |
| 151 | + |
| 152 | +## Best Practices |
| 153 | + |
| 154 | +1. **Use descriptive file names** for imported components |
| 155 | +2. **Keep imports shallow** - avoid deeply nested import chains |
| 156 | +3. **Document your structure** - maintain a clear hierarchy of imported files |
| 157 | +4. **Test your imports** - ensure all referenced files exist and are accessible |
| 158 | +5. **Use relative paths** when possible for better portability |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +### Common Issues |
| 163 | + |
| 164 | +1. **Import not working**: Check that the file exists and has a `.md` extension |
| 165 | +2. **Circular import warnings**: Review your import structure for circular references |
| 166 | +3. **Permission errors**: Ensure the files are readable and within allowed directories |
| 167 | +4. **Path resolution issues**: Use absolute paths if relative paths aren't resolving correctly |
| 168 | + |
| 169 | +### Debug Mode |
| 170 | + |
| 171 | +Enable debug mode to see detailed logging of the import process: |
| 172 | + |
| 173 | +```typescript |
| 174 | +const result = await processImports(content, basePath, true); |
| 175 | +``` |
0 commit comments