From fde6098ef2c4d99a9b9b8d6ffc1299436e0f52c1 Mon Sep 17 00:00:00 2001 From: Nate Date: Mon, 14 Jul 2025 00:31:01 -0700 Subject: [PATCH] fix: display diff --- package-lock.json | 4 +-- src/tools/writeFile.ts | 68 +++++++++++++++++++++++++++++++++++- src/ui/ToolResultSummary.tsx | 9 ++++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ea880e57..93e9eafa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@continuedev/cli", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@continuedev/cli", - "version": "1.0.1", + "version": "1.1.0", "license": "Apache-2.0", "dependencies": { "@continuedev/openai-adapters": "^1.0.18", diff --git a/src/tools/writeFile.ts b/src/tools/writeFile.ts index 2ab64799f..50b27322a 100644 --- a/src/tools/writeFile.ts +++ b/src/tools/writeFile.ts @@ -2,6 +2,58 @@ import * as fs from "fs"; import * as path from "path"; import { Tool } from "./types.js"; +function generateDiff(oldContent: string, newContent: string, filePath: string): string { + const oldLines = oldContent.split('\n'); + const newLines = newContent.split('\n'); + + let diff = `--- ${filePath}\n+++ ${filePath}\n`; + + // Simple line-by-line diff + const maxLines = Math.max(oldLines.length, newLines.length); + let hunkStart = -1; + let hunkOldCount = 0; + let hunkNewCount = 0; + let hunkLines: string[] = []; + + for (let i = 0; i < maxLines; i++) { + const oldLine = oldLines[i] || ''; + const newLine = newLines[i] || ''; + + if (oldLine !== newLine) { + if (hunkStart === -1) { + hunkStart = i; + } + + if (i < oldLines.length) { + hunkLines.push(`-${oldLine}`); + hunkOldCount++; + } + if (i < newLines.length) { + hunkLines.push(`+${newLine}`); + hunkNewCount++; + } + } else { + if (hunkStart !== -1) { + // End of hunk + diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`; + diff += hunkLines.join('\n') + '\n'; + hunkStart = -1; + hunkOldCount = 0; + hunkNewCount = 0; + hunkLines = []; + } + } + } + + // Handle final hunk + if (hunkStart !== -1) { + diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`; + diff += hunkLines.join('\n') + '\n'; + } + + return diff; +} + export const writeFileTool: Tool = { name: "write_file", displayName: "Write", @@ -25,8 +77,22 @@ export const writeFileTool: Tool = { fs.mkdirSync(dirPath, { recursive: true }); } + // Read existing file content if it exists + let oldContent = ''; + if (fs.existsSync(args.filepath)) { + oldContent = fs.readFileSync(args.filepath, "utf-8"); + } + + // Write new content fs.writeFileSync(args.filepath, args.content, "utf-8"); - return `Successfully wrote to file: ${args.filepath}`; + + // Generate diff if file existed before + if (oldContent) { + const diff = generateDiff(oldContent, args.content, args.filepath); + return `Successfully wrote to file: ${args.filepath}\n\nDiff:\n${diff}`; + } else { + return `Successfully created file: ${args.filepath}`; + } } catch (error) { return `Error writing to file: ${error instanceof Error ? error.message : String(error)}`; } diff --git a/src/ui/ToolResultSummary.tsx b/src/ui/ToolResultSummary.tsx index c360f9f0b..6a4c32288 100644 --- a/src/ui/ToolResultSummary.tsx +++ b/src/ui/ToolResultSummary.tsx @@ -30,7 +30,14 @@ const ToolResultSummary: React.FC = ({ return `${displayName} tool output (${lines} lines)`; case "write_file": - return "File written successfully"; + // For write_file, we want to display the diff content if it exists + if (content.includes("Diff:\n")) { + const diffSection = content.split("Diff:\n")[1]; + if (diffSection) { + return `File written successfully\n${diffSection}`; + } + } + return content.includes("Successfully created file") ? "File created successfully" : "File written successfully"; case "list_files": return `Listed ${lines} ${lines === 1 ? "item" : "items"}`;