|
| 1 | +import { Task } from "../../core/task/Task" |
| 2 | +import { getCheckpointService } from "../../core/checkpoints" |
| 3 | +import { FileChangeType } from "@roo-code/types" |
| 4 | +import { FileChangeManager } from "./FileChangeManager" |
| 5 | + |
| 6 | +/** |
| 7 | + * Updates FCO immediately after a file edit without changing checkpoint timing. |
| 8 | + * This provides immediate visibility of changes while preserving rollback safety. |
| 9 | + */ |
| 10 | +export async function updateFCOAfterEdit(task: Task): Promise<void> { |
| 11 | + const provider = task.providerRef.deref() |
| 12 | + if (!provider) { |
| 13 | + return |
| 14 | + } |
| 15 | + |
| 16 | + try { |
| 17 | + const fileChangeManager = provider.getFileChangeManager() |
| 18 | + const checkpointService = await getCheckpointService(task) |
| 19 | + |
| 20 | + if (!fileChangeManager || !checkpointService || !task.taskId || !task.fileContextTracker) { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + // Get current baseline for FCO |
| 25 | + const baseline = fileChangeManager.getChanges().baseCheckpoint |
| 26 | + |
| 27 | + // Calculate diff from baseline to current working directory state |
| 28 | + // We use the checkpointService to get a diff from baseline to HEAD (current state) |
| 29 | + try { |
| 30 | + const changes = await checkpointService.getDiff({ |
| 31 | + from: baseline, |
| 32 | + to: "HEAD", // Current working directory state |
| 33 | + }) |
| 34 | + |
| 35 | + if (!changes || changes.length === 0) { |
| 36 | + // No changes detected, keep current FCO state |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // Convert checkpoint service changes to FileChange format |
| 41 | + const fileChanges = changes.map((change: any) => { |
| 42 | + const type = ( |
| 43 | + change.paths.newFile ? "create" : change.paths.deletedFile ? "delete" : "edit" |
| 44 | + ) as FileChangeType |
| 45 | + |
| 46 | + // Calculate line differences |
| 47 | + let linesAdded = 0 |
| 48 | + let linesRemoved = 0 |
| 49 | + |
| 50 | + if (type === "create") { |
| 51 | + linesAdded = change.content.after ? change.content.after.split("\n").length : 0 |
| 52 | + linesRemoved = 0 |
| 53 | + } else if (type === "delete") { |
| 54 | + linesAdded = 0 |
| 55 | + linesRemoved = change.content.before ? change.content.before.split("\n").length : 0 |
| 56 | + } else { |
| 57 | + const lineDifferences = FileChangeManager.calculateLineDifferences( |
| 58 | + change.content.before || "", |
| 59 | + change.content.after || "", |
| 60 | + ) |
| 61 | + linesAdded = lineDifferences.linesAdded |
| 62 | + linesRemoved = lineDifferences.linesRemoved |
| 63 | + } |
| 64 | + |
| 65 | + return { |
| 66 | + uri: change.paths.relative, |
| 67 | + type, |
| 68 | + fromCheckpoint: baseline, |
| 69 | + toCheckpoint: "HEAD", // This represents current state, not an actual checkpoint |
| 70 | + linesAdded, |
| 71 | + linesRemoved, |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + // Update FileChangeManager with the new files |
| 76 | + fileChangeManager.setFiles(fileChanges) |
| 77 | + |
| 78 | + // Get LLM-only changes for the webview (filters out accepted/rejected files) |
| 79 | + const filteredChangeset = await fileChangeManager.getLLMOnlyChanges(task.taskId, task.fileContextTracker) |
| 80 | + |
| 81 | + // Send updated changes to webview only if there are changes to show |
| 82 | + if (filteredChangeset.files.length > 0) { |
| 83 | + provider.postMessageToWebview({ |
| 84 | + type: "filesChanged", |
| 85 | + filesChanged: filteredChangeset, |
| 86 | + }) |
| 87 | + |
| 88 | + provider.log( |
| 89 | + `[updateFCOAfterEdit] Updated FCO with ${filteredChangeset.files.length} LLM-only file changes`, |
| 90 | + ) |
| 91 | + } |
| 92 | + } catch (diffError) { |
| 93 | + // If we can't calculate diff (e.g., baseline is invalid), don't update FCO |
| 94 | + provider.log(`[updateFCOAfterEdit] Failed to calculate diff from ${baseline} to HEAD: ${diffError}`) |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + // Non-critical error, don't throw - just log and continue |
| 98 | + provider?.log(`[updateFCOAfterEdit] Error updating FCO after edit: ${error}`) |
| 99 | + } |
| 100 | +} |
0 commit comments