* Adds sources and extensions to the index. * @param config The configuration containing the sources and extensions to add.
(config: Partial<CodeIndexConfig>)
| 119 | * @param config The configuration containing the sources and extensions to add. |
| 120 | */ |
| 121 | public async add(config: Partial<CodeIndexConfig>): Promise<void> { |
| 122 | if (!await this.isCreated()) { |
| 123 | throw new Error('Index has not been created yet. Please run `codepilot create` first.'); |
| 124 | } |
| 125 | |
| 126 | // Ensure config loaded |
| 127 | const configPath = path.join(this.folderPath, 'config.json'); |
| 128 | if (!this._config) { |
| 129 | this._config = JSON.parse(await fs.readFile(configPath, 'utf-8')); |
| 130 | } |
| 131 | |
| 132 | // Clone config |
| 133 | const newConfig = Object.assign({}, this._config); |
| 134 | |
| 135 | // Add sources |
| 136 | if (Array.isArray(config.sources)) { |
| 137 | config.sources.forEach(source => { |
| 138 | if (!newConfig.sources.includes(source)) { |
| 139 | newConfig.sources.push(source); |
| 140 | } |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | // Add extensions |
| 145 | if (Array.isArray(config.extensions)) { |
| 146 | if (!newConfig.extensions) { |
| 147 | newConfig.extensions = []; |
| 148 | } |
| 149 | config.extensions.forEach(extension => { |
| 150 | if (!newConfig.extensions!.includes(extension)) { |
| 151 | newConfig.extensions!.push(extension); |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | // Write config |
| 157 | await fs.writeFile(configPath, JSON.stringify(newConfig)); |
| 158 | this._config = newConfig; |
| 159 | } |
| 160 | |
| 161 | // LLM-REGION |
| 162 |