* Process the response from the OpenAI API * @param response Response text * @param files Files that were processed * @returns Results of the modifications
(
response: string,
files: FileToProcess[],
)
| 286 | * @returns Results of the modifications |
| 287 | */ |
| 288 | private async processResponse( |
| 289 | response: string, |
| 290 | files: FileToProcess[], |
| 291 | ): Promise<ModificationResult[]> { |
| 292 | const results: ModificationResult[] = []; |
| 293 | |
| 294 | // Create a map of file paths to their original content |
| 295 | const fileMap = new Map<string, string>(); |
| 296 | for (const file of files) { |
| 297 | fileMap.set(file.path, file.originalContent); |
| 298 | } |
| 299 | |
| 300 | // Extract code blocks from the response |
| 301 | const codeBlockRegex = /File: ([^\n]+)\n```(?:[^\n]+)?\n([\s\S]*?)\n```/g; |
| 302 | let match; |
| 303 | |
| 304 | while ((match = codeBlockRegex.exec(response)) !== null) { |
| 305 | const [, path, code] = match; |
| 306 | const trimmedPath = path.trim(); |
| 307 | |
| 308 | // Check if the file exists in the input files |
| 309 | if (!fileMap.has(trimmedPath)) { |
| 310 | await logWarn(`File not found in input: ${trimmedPath}`); |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | const originalContent = fileMap.get(trimmedPath)!; |
| 315 | const modifiedContent = code.trim(); |
| 316 | |
| 317 | // Compute diff |
| 318 | const diff = await this.analyzeAndDiff(trimmedPath, originalContent, modifiedContent); |
| 319 | |
| 320 | // If there are changes, write the file and commit |
| 321 | if (diff) { |
| 322 | // Write the file |
| 323 | await Deno.writeTextFile(trimmedPath, modifiedContent); |
| 324 | |
| 325 | // Commit the changes |
| 326 | const commitHash = await this.applyChanges( |
| 327 | trimmedPath, |
| 328 | `SPARC2: Updated ${trimmedPath}`, |
| 329 | ); |
| 330 | |
| 331 | // Add to results |
| 332 | results.push({ |
| 333 | path: trimmedPath, |
| 334 | originalContent, |
| 335 | modifiedContent, |
| 336 | commitHash, |
| 337 | }); |
| 338 | } else { |
| 339 | // No changes, just add to results |
| 340 | results.push({ |
| 341 | path: trimmedPath, |
| 342 | originalContent, |
| 343 | modifiedContent: originalContent, |
| 344 | }); |
| 345 | } |
no test coverage detected