| 1 | import * as fs from "fs"; |
| 2 | import * as path from "path"; |
| 3 | |
| 4 | import { createTwoFilesPatch } from "diff"; |
| 5 | |
| 6 | import { telemetryService } from "../telemetry/telemetryService.js"; |
| 7 | import { |
| 8 | calculateLinesOfCodeDiff, |
| 9 | getLanguageFromFilePath, |
| 10 | } from "../telemetry/utils.js"; |
| 11 | |
| 12 | import { Tool, ToolCallPreview } from "./types.js"; |
| 13 | |
| 14 | export function generateDiff( |
| 15 | oldContent: string, |
| 16 | newContent: string, |
| 17 | filePath: string, |
| 18 | ): string { |
| 19 | return createTwoFilesPatch( |
| 20 | filePath, |
| 21 | filePath, |
| 22 | oldContent, |
| 23 | newContent, |
| 24 | undefined, |
| 25 | undefined, |
| 26 | { context: 3 }, |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | export const writeFileTool: Tool = { |
| 31 | name: "Write", |
| 32 | displayName: "Write", |
| 33 | description: "Write content to a file at the specified path", |
| 34 | parameters: { |
| 35 | type: "object", |
| 36 | required: ["filepath", "content"], |
| 37 | properties: { |
| 38 | filepath: { |
| 39 | type: "string", |
| 40 | description: "The path to the file to write", |
| 41 | }, |
| 42 | content: { |
| 43 | type: "string", |
| 44 | description: "The content to write to the file", |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | readonly: false, |
| 49 | isBuiltIn: true, |
| 50 | preprocess: async (args) => { |
| 51 | try { |
| 52 | if (fs.existsSync(args.filepath)) { |
| 53 | const oldContent = fs.readFileSync(args.filepath, "utf-8"); |
| 54 | const newContent = args.content; |
| 55 | |
| 56 | const diff = createTwoFilesPatch( |
| 57 | args.filepath, |
| 58 | args.filepath, |
| 59 | oldContent, |
| 60 | newContent, |
| 61 | undefined, |
| 62 | undefined, |
| 63 | { context: 2 }, |
| 64 | ); |
| 65 | |
| 66 | return { |
| 67 | args, |
| 68 | preview: [ |
| 69 | { |
| 70 | type: "text", |
| 71 | content: "Preview of changes:", |
| 72 | }, |
| 73 | { |
| 74 | type: "diff", |
| 75 | content: diff, |
| 76 | }, |
| 77 | ], |
| 78 | }; |
| 79 | } |
| 80 | } catch { |
| 81 | // do nothing |
| 82 | } |
| 83 | const lines: string[] = args.content.split("\n"); |
| 84 | const previewLines = lines.slice(0, 3); |
| 85 | |
| 86 | const preview: ToolCallPreview[] = [ |
| 87 | { |
| 88 | type: "text", |
| 89 | content: "New file content:", |
| 90 | }, |
| 91 | ...previewLines.map((line) => ({ |
| 92 | type: "text" as const, |
| 93 | content: line || " ", |
| 94 | paddingLeft: 2, |
| 95 | })), |
| 96 | ]; |
| 97 | if (lines.length > 3) { |
| 98 | preview.push({ |
| 99 | type: "text", |
| 100 | content: `... (${lines.length - 3} more lines)`, |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | return { |
| 105 | args, |
| 106 | preview, |
| 107 | }; |
| 108 | }, |
| 109 | run: async (args: { filepath: string; content: string }): Promise<string> => { |
| 110 | try { |
| 111 | const dirPath = path.dirname(args.filepath); |
| 112 | if (!fs.existsSync(dirPath)) { |
| 113 | fs.mkdirSync(dirPath, { recursive: true }); |
| 114 | } |
| 115 | |
| 116 | // Read existing file content if it exists |
| 117 | let oldContent = ""; |
| 118 | if (fs.existsSync(args.filepath)) { |
| 119 | oldContent = fs.readFileSync(args.filepath, "utf-8"); |
| 120 | } |
| 121 | |
| 122 | // Write new content |
| 123 | fs.writeFileSync(args.filepath, args.content, "utf-8"); |
| 124 | |
| 125 | // Track lines of code changes if file existed before |
| 126 | if (oldContent) { |
| 127 | const { added, removed } = calculateLinesOfCodeDiff( |
| 128 | oldContent, |
| 129 | args.content, |
| 130 | ); |
| 131 | const language = getLanguageFromFilePath(args.filepath); |
| 132 | |
| 133 | if (added > 0) { |
| 134 | telemetryService.recordLinesOfCodeModified("added", added, language); |
| 135 | } |
| 136 | if (removed > 0) { |
| 137 | telemetryService.recordLinesOfCodeModified( |
| 138 | "removed", |
| 139 | removed, |
| 140 | language, |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | // Generate diff for result display |
| 145 | const diff = generateDiff(oldContent, args.content, args.filepath); |
| 146 | |
| 147 | return `Successfully wrote to file: ${args.filepath}\nDiff:\n${diff}`; |
| 148 | } else { |
| 149 | // New file creation - count all lines as added |
| 150 | const lineCount = args.content.split("\n").length; |
| 151 | const language = getLanguageFromFilePath(args.filepath); |
| 152 | |
| 153 | telemetryService.recordLinesOfCodeModified( |
| 154 | "added", |
| 155 | lineCount, |
| 156 | language, |
| 157 | ); |
| 158 | |
| 159 | return `Successfully created file: ${args.filepath}`; |
| 160 | } |
| 161 | } catch (error) { |
| 162 | const errorMessage = |
| 163 | error instanceof Error ? error.message : String(error); |
| 164 | |
| 165 | return `Error writing to file: ${errorMessage}`; |
| 166 | } |
| 167 | }, |
| 168 | }; |