|
| 1 | +import path from "path" |
| 2 | +import fs from "fs/promises" |
| 3 | + |
| 4 | +import { TelemetryService } from "@roo-code/telemetry" |
| 5 | + |
| 6 | +import { ClineSayTool } from "../../shared/ExtensionMessage" |
| 7 | +import { getReadablePath } from "../../utils/path" |
| 8 | +import { Task } from "../task/Task" |
| 9 | +import { ToolUse, RemoveClosingTag, AskApproval, HandleError, PushToolResult } from "../../shared/tools" |
| 10 | +import { formatResponse } from "../prompts/responses" |
| 11 | +import { fileExistsAtPath } from "../../utils/fs" |
| 12 | +import { RecordSource } from "../context-tracking/FileContextTrackerTypes" |
| 13 | + |
| 14 | +interface MorphApiResponse { |
| 15 | + success: boolean |
| 16 | + content?: string |
| 17 | + error?: string |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Calls the Morph Fast Apply API to apply diffs with high accuracy |
| 22 | + * @param apiKey The Morph API key |
| 23 | + * @param originalContent The original file content |
| 24 | + * @param diffContent The diff content to apply |
| 25 | + * @returns The result from the Morph API |
| 26 | + */ |
| 27 | +async function callMorphApi(apiKey: string, originalContent: string, diffContent: string): Promise<MorphApiResponse> { |
| 28 | + try { |
| 29 | + // Using native fetch API (available in Node.js 18+) |
| 30 | + const response = await fetch("https://api.morph.so/v1/fast-apply", { |
| 31 | + method: "POST", |
| 32 | + headers: { |
| 33 | + "Content-Type": "application/json", |
| 34 | + Authorization: `Bearer ${apiKey}`, |
| 35 | + }, |
| 36 | + body: JSON.stringify({ |
| 37 | + original: originalContent, |
| 38 | + diff: diffContent, |
| 39 | + }), |
| 40 | + }) |
| 41 | + |
| 42 | + if (!response.ok) { |
| 43 | + const errorText = await response.text() |
| 44 | + return { |
| 45 | + success: false, |
| 46 | + error: `Morph API error (${response.status}): ${errorText}`, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const data = (await response.json()) as any |
| 51 | + return { |
| 52 | + success: true, |
| 53 | + content: data.result || data.content, |
| 54 | + } |
| 55 | + } catch (error: any) { |
| 56 | + return { |
| 57 | + success: false, |
| 58 | + error: `Failed to call Morph API: ${error.message}`, |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export async function morphFastApplyTool( |
| 64 | + cline: Task, |
| 65 | + block: ToolUse, |
| 66 | + askApproval: AskApproval, |
| 67 | + handleError: HandleError, |
| 68 | + pushToolResult: PushToolResult, |
| 69 | + removeClosingTag: RemoveClosingTag, |
| 70 | +) { |
| 71 | + const relPath: string | undefined = block.params.path |
| 72 | + let diffContent: string | undefined = block.params.diff |
| 73 | + |
| 74 | + const sharedMessageProps: ClineSayTool = { |
| 75 | + tool: "appliedDiff", |
| 76 | + path: getReadablePath(cline.cwd, removeClosingTag("path", relPath)), |
| 77 | + diff: diffContent, |
| 78 | + } |
| 79 | + |
| 80 | + try { |
| 81 | + if (block.partial) { |
| 82 | + // Update GUI message for partial blocks |
| 83 | + await cline.ask("tool", JSON.stringify(sharedMessageProps), block.partial).catch(() => {}) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + if (!relPath) { |
| 88 | + cline.consecutiveMistakeCount++ |
| 89 | + cline.recordToolError("morph_fast_apply") |
| 90 | + pushToolResult(await cline.sayAndCreateMissingParamError("morph_fast_apply", "path")) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + if (!diffContent) { |
| 95 | + cline.consecutiveMistakeCount++ |
| 96 | + cline.recordToolError("morph_fast_apply") |
| 97 | + pushToolResult(await cline.sayAndCreateMissingParamError("morph_fast_apply", "diff")) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Check if file access is allowed |
| 102 | + const accessAllowed = cline.rooIgnoreController?.validateAccess(relPath) |
| 103 | + if (!accessAllowed) { |
| 104 | + await cline.say("rooignore_error", relPath) |
| 105 | + pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath))) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + const absolutePath = path.resolve(cline.cwd, relPath) |
| 110 | + const fileExists = await fileExistsAtPath(absolutePath) |
| 111 | + |
| 112 | + if (!fileExists) { |
| 113 | + cline.consecutiveMistakeCount++ |
| 114 | + cline.recordToolError("morph_fast_apply") |
| 115 | + const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>` |
| 116 | + await cline.say("error", formattedError) |
| 117 | + pushToolResult(formattedError) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + // Get the Morph API key from provider state |
| 122 | + const provider = cline.providerRef.deref() |
| 123 | + const state = await provider?.getState() |
| 124 | + // Check if Morph API key is configured (we'll add this to provider settings later) |
| 125 | + const morphApiKey = (state as any)?.morphApiKey |
| 126 | + |
| 127 | + if (!morphApiKey) { |
| 128 | + // Morph API key not configured, fall back to regular apply_diff |
| 129 | + await cline.say("text", "Morph API key not configured. Falling back to standard diff application.") |
| 130 | + pushToolResult("Morph API key not configured. Please configure it in settings to use Morph Fast Apply.") |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + const originalContent = await fs.readFile(absolutePath, "utf-8") |
| 135 | + |
| 136 | + // Call Morph API to apply the diff |
| 137 | + const morphResult = await callMorphApi(morphApiKey, originalContent, diffContent) |
| 138 | + |
| 139 | + if (!morphResult.success) { |
| 140 | + cline.consecutiveMistakeCount++ |
| 141 | + const currentCount = (cline.consecutiveMistakeCountForApplyDiff.get(relPath) || 0) + 1 |
| 142 | + cline.consecutiveMistakeCountForApplyDiff.set(relPath, currentCount) |
| 143 | + |
| 144 | + TelemetryService.instance.captureDiffApplicationError(cline.taskId, currentCount) |
| 145 | + |
| 146 | + const formattedError = `Unable to apply diff using Morph Fast Apply: ${absolutePath}\n\n<error_details>\n${morphResult.error}\n</error_details>` |
| 147 | + |
| 148 | + if (currentCount >= 2) { |
| 149 | + await cline.say("diff_error", formattedError) |
| 150 | + } |
| 151 | + |
| 152 | + cline.recordToolError("morph_fast_apply", formattedError) |
| 153 | + pushToolResult(formattedError) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + cline.consecutiveMistakeCount = 0 |
| 158 | + cline.consecutiveMistakeCountForApplyDiff.delete(relPath) |
| 159 | + |
| 160 | + // Check if file is write-protected |
| 161 | + const isWriteProtected = cline.rooProtectedController?.isWriteProtected(relPath) || false |
| 162 | + |
| 163 | + // Show diff view and ask for approval |
| 164 | + cline.diffViewProvider.editType = "modify" |
| 165 | + await cline.diffViewProvider.open(relPath) |
| 166 | + await cline.diffViewProvider.update(morphResult.content!, true) |
| 167 | + cline.diffViewProvider.scrollToFirstDiff() |
| 168 | + |
| 169 | + const completeMessage = JSON.stringify({ |
| 170 | + ...sharedMessageProps, |
| 171 | + diff: diffContent, |
| 172 | + isProtected: isWriteProtected, |
| 173 | + } satisfies ClineSayTool) |
| 174 | + |
| 175 | + const didApprove = await askApproval("tool", completeMessage, undefined, isWriteProtected) |
| 176 | + |
| 177 | + if (!didApprove) { |
| 178 | + await cline.diffViewProvider.revertChanges() |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + // Save the changes |
| 183 | + const diagnosticsEnabled = state?.diagnosticsEnabled ?? true |
| 184 | + const writeDelayMs = state?.writeDelayMs ?? 0 |
| 185 | + await cline.diffViewProvider.saveChanges(diagnosticsEnabled, writeDelayMs) |
| 186 | + |
| 187 | + // Track file edit operation |
| 188 | + await cline.fileContextTracker.trackFileContext(relPath, "roo_edited" as RecordSource) |
| 189 | + |
| 190 | + cline.didEditFile = true |
| 191 | + |
| 192 | + // Get the formatted response message |
| 193 | + const message = await cline.diffViewProvider.pushToolWriteResult(cline, cline.cwd, !fileExists) |
| 194 | + |
| 195 | + // Add success notice about using Morph |
| 196 | + const morphNotice = "\n<notice>Successfully applied diff using Morph Fast Apply (98% accuracy)</notice>" |
| 197 | + pushToolResult(message + morphNotice) |
| 198 | + |
| 199 | + await cline.diffViewProvider.reset() |
| 200 | + |
| 201 | + // Track successful Morph usage |
| 202 | + TelemetryService.instance.captureToolUsage(cline.taskId, "morph_fast_apply") |
| 203 | + } catch (error) { |
| 204 | + await handleError("applying diff with Morph Fast Apply", error) |
| 205 | + await cline.diffViewProvider.reset() |
| 206 | + } |
| 207 | +} |
0 commit comments