Elffuss BraidHub

kikocis/continue public

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