fix: display diff

This commit is contained in:
Nate 2025-07-14 00:31:01 -07:00
parent daae97242f
commit fde6098ef2
3 changed files with 77 additions and 4 deletions

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "@continuedev/cli", "name": "@continuedev/cli",
"version": "1.0.1", "version": "1.1.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@continuedev/cli", "name": "@continuedev/cli",
"version": "1.0.1", "version": "1.1.0",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@continuedev/openai-adapters": "^1.0.18", "@continuedev/openai-adapters": "^1.0.18",

View file

@ -2,6 +2,58 @@ import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { Tool } from "./types.js"; 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 = { export const writeFileTool: Tool = {
name: "write_file", name: "write_file",
displayName: "Write", displayName: "Write",
@ -25,8 +77,22 @@ export const writeFileTool: Tool = {
fs.mkdirSync(dirPath, { recursive: true }); 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"); 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) { } catch (error) {
return `Error writing to file: ${error instanceof Error ? error.message : String(error)}`; return `Error writing to file: ${error instanceof Error ? error.message : String(error)}`;
} }

View file

@ -30,7 +30,14 @@ const ToolResultSummary: React.FC<ToolResultSummaryProps> = ({
return `${displayName} tool output (${lines} lines)`; return `${displayName} tool output (${lines} lines)`;
case "write_file": 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": case "list_files":
return `Listed ${lines} ${lines === 1 ? "item" : "items"}`; return `Listed ${lines} ${lines === 1 ? "item" : "items"}`;