-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolExecutor.cs
More file actions
469 lines (396 loc) · 19.1 KB
/
Copy pathToolExecutor.cs
File metadata and controls
469 lines (396 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace GIDE
{
public class ToolExecutor
{
private string _workDir;
public ToolExecutor(string workDir)
{
_workDir = Path.GetFullPath(workDir);
}
public string Execute(ToolCall tool)
{
try
{
switch (tool.Type.ToUpper())
{
case "WRITE": return WriteFile(tool.Path, tool.Content);
case "READ": return ReadFile(tool.Path);
case "RUN": return RunCommand(tool.Command);
case "LIST": return ListFiles(tool.Path);
case "DELETE": return DeleteFile(tool.Path);
case "RENAME": return RenameFile(tool.Path, tool.Destination);
case "PATCH": return PatchFile(tool.Path, tool.OldText, tool.NewText);
default: return "[ERROR] Unknown tool: " + tool.Type;
}
}
catch (Exception ex)
{
return "[ERROR] " + tool.Type + ": " + ex.Message;
}
}
private string WriteFile(string relativePath, string content)
{
if (string.IsNullOrWhiteSpace(relativePath))
return "[WRITE ERROR] No file path provided";
if (content == null)
content = "";
// Resolve full path safely
relativePath = relativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
string fullPath = Path.GetFullPath(Path.Combine(_workDir, relativePath));
// Safety: don't write outside the work directory
if (!fullPath.StartsWith(_workDir, StringComparison.OrdinalIgnoreCase))
return "[WRITE ERROR] Path escapes work directory: " + relativePath;
// Ensure parent directory exists
string dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
Directory.CreateDirectory(dir);
// Clear read-only attribute if set — common cause of silent write failures
if (File.Exists(fullPath))
{
try
{
FileAttributes attrs = File.GetAttributes(fullPath);
if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(fullPath, attrs & ~FileAttributes.ReadOnly);
}
catch { /* non-fatal, attempt write anyway */ }
}
// Write with retry — handles transient locks (antivirus, indexers, etc.)
Exception lastEx = null;
for (int attempt = 1; attempt <= 3; attempt++)
{
try
{
// Write to a temp file first, then atomic-replace
string tempPath = fullPath + ".gide_tmp";
File.WriteAllText(tempPath, content, new UTF8Encoding(false));
// Delete destination and move temp into place
if (File.Exists(fullPath))
File.Delete(fullPath);
File.Move(tempPath, fullPath);
long written = new FileInfo(fullPath).Length;
return string.Format("[WRITE OK] {0} ({1} bytes)", relativePath, written);
}
catch (Exception ex)
{
lastEx = ex;
if (attempt < 3)
System.Threading.Thread.Sleep(300 * attempt);
}
}
return "[WRITE FAILED] " + relativePath + " — " + (lastEx != null ? lastEx.Message : "unknown error");
}
private string PatchFile(string relativePath, string oldText, string newText)
{
if (string.IsNullOrWhiteSpace(relativePath))
return "[PATCH ERROR] No file path provided";
if (oldText == null)
return "[PATCH ERROR] No OLD text provided";
if (newText == null)
newText = ""; // Allow replacing with empty (deletion)
relativePath = relativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
string fullPath = Path.GetFullPath(Path.Combine(_workDir, relativePath));
// Safety: don't patch outside the work directory
if (!fullPath.StartsWith(_workDir, StringComparison.OrdinalIgnoreCase))
return "[PATCH ERROR] Path escapes work directory: " + relativePath;
if (!File.Exists(fullPath))
return "[PATCH ERROR] File not found: " + relativePath;
string content;
try
{
content = File.ReadAllText(fullPath, Encoding.UTF8);
}
catch (Exception ex)
{
return "[PATCH ERROR] Cannot read file: " + ex.Message;
}
// Try exact match first
int matchIndex = content.IndexOf(oldText, StringComparison.Ordinal);
// If exact match fails, try with normalized line endings
if (matchIndex < 0)
{
string normalizedContent = content.Replace("\r\n", "\n");
string normalizedOld = oldText.Replace("\r\n", "\n");
int normIndex = normalizedContent.IndexOf(normalizedOld, StringComparison.Ordinal);
if (normIndex >= 0)
{
// Found with normalized endings — do the replacement on normalized, then restore
string normalizedNew = newText.Replace("\r\n", "\n");
string result = normalizedContent.Substring(0, normIndex) +
normalizedNew +
normalizedContent.Substring(normIndex + normalizedOld.Length);
// Restore original line ending style
if (content.Contains("\r\n"))
result = result.Replace("\n", "\r\n");
return WritePatchResult(fullPath, relativePath, result);
}
// Try trimmed-line matching (ignore leading/trailing whitespace per line)
int fuzzyIndex = FuzzyFindPatch(content, oldText);
if (fuzzyIndex >= 0)
{
// Fuzzy matched — calculate actual length of matched region in original
string[] oldLines = oldText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
string[] contentLines = content.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
string lineEnding = content.Contains("\r\n") ? "\r\n" : "\n";
// Find which content line the fuzzyIndex corresponds to
int lineStart = 0;
int charCount = 0;
for (int i = 0; i < contentLines.Length; i++)
{
if (charCount >= fuzzyIndex) { lineStart = i; break; }
charCount += contentLines[i].Length + lineEnding.Length;
}
// Calculate actual matched length in original content
int matchLen = 0;
for (int i = lineStart; i < lineStart + oldLines.Length && i < contentLines.Length; i++)
{
matchLen += contentLines[i].Length;
if (i < lineStart + oldLines.Length - 1)
matchLen += lineEnding.Length;
}
string fuzzyPatched = content.Substring(0, fuzzyIndex) +
newText +
content.Substring(fuzzyIndex + matchLen);
return WritePatchResult(fullPath, relativePath, fuzzyPatched);
}
}
if (matchIndex < 0)
{
// Show what we were looking for to help debugging
string preview = oldText.Length > 80 ? oldText.Substring(0, 80) + "..." : oldText;
return "[PATCH FAILED] Text not found in " + relativePath +
". Looking for: \"" + preview + "\"" +
"\n\nHint: Use <<<TOOL:WRITE>>> with full file content instead.";
}
// Check for multiple matches (ambiguous)
int secondMatch = content.IndexOf(oldText, matchIndex + 1, StringComparison.Ordinal);
if (secondMatch >= 0)
{
return "[PATCH FAILED] Multiple matches found in " + relativePath +
". The OLD text is ambiguous — add more context lines to make it unique." +
"\n\nHint: Include 2-3 lines before and after the change.";
}
// Apply the patch
string patched = content.Substring(0, matchIndex) +
newText +
content.Substring(matchIndex + oldText.Length);
return WritePatchResult(fullPath, relativePath, patched);
}
private string WritePatchResult(string fullPath, string relativePath, string patched)
{
try
{
string tempPath = fullPath + ".gide_tmp";
File.WriteAllText(tempPath, patched, new UTF8Encoding(false));
if (File.Exists(fullPath))
File.Delete(fullPath);
File.Move(tempPath, fullPath);
return "[PATCH OK] " + relativePath;
}
catch (Exception ex)
{
return "[PATCH FAILED] " + relativePath + " — " + ex.Message;
}
}
/// <summary>
/// Fuzzy matching: tries to find the old text by trimming whitespace from each line.
/// Handles cases where the model gets indentation slightly wrong.
/// Returns the start char index in content, or -1 if not found.
/// </summary>
private static int FuzzyFindPatch(string content, string oldText)
{
// Normalize both to trimmed lines for comparison
string[] oldLines = oldText.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
string[] contentLines = content.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
if (oldLines.Length == 0 || oldLines.Length > contentLines.Length)
return -1;
// Trim each old line for comparison
string[] oldTrimmed = new string[oldLines.Length];
for (int i = 0; i < oldLines.Length; i++)
oldTrimmed[i] = oldLines[i].Trim();
// Skip empty-only searches
bool allEmpty = true;
foreach (var line in oldTrimmed)
if (line.Length > 0) { allEmpty = false; break; }
if (allEmpty) return -1;
string lineEnding = content.Contains("\r\n") ? "\r\n" : "\n";
// Slide through content looking for a match
int charOffset = 0;
for (int start = 0; start <= contentLines.Length - oldLines.Length; start++)
{
bool match = true;
for (int j = 0; j < oldLines.Length; j++)
{
if (contentLines[start + j].Trim() != oldTrimmed[j])
{
match = false;
break;
}
}
if (match)
return charOffset;
charOffset += contentLines[start].Length + lineEnding.Length;
}
return -1;
}
private string ReadFile(string relativePath)
{
if (string.IsNullOrWhiteSpace(relativePath))
return "[READ ERROR] No file path provided";
relativePath = relativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
string fullPath = Path.GetFullPath(Path.Combine(_workDir, relativePath));
if (!File.Exists(fullPath))
return "[READ ERROR] File not found: " + relativePath;
try
{
string text = File.ReadAllText(fullPath, Encoding.UTF8);
return "[READ] " + relativePath + " (" + text.Length + " chars)\n" + text;
}
catch (Exception ex)
{
return "[READ ERROR] " + relativePath + " — " + ex.Message;
}
}
private string RunCommand(string command)
{
if (string.IsNullOrWhiteSpace(command))
return "[RUN ERROR] No command provided";
try
{
var psi = new ProcessStartInfo("cmd.exe", "/c " + command);
psi.WorkingDirectory = _workDir;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.StandardOutputEncoding = Encoding.UTF8;
psi.StandardErrorEncoding = Encoding.UTF8;
using (var process = Process.Start(psi))
{
// Read both streams concurrently to avoid deadlock on large output
string stdout = "";
string stderr = "";
var outTask = System.Threading.Tasks.Task.Run(() => stdout = process.StandardOutput.ReadToEnd());
var errTask = System.Threading.Tasks.Task.Run(() => stderr = process.StandardError.ReadToEnd());
process.WaitForExit(30000); // 30s timeout
System.Threading.Tasks.Task.WaitAll(outTask, errTask);
string combined = stdout;
if (!string.IsNullOrEmpty(stderr))
combined += (combined.Length > 0 ? "\n" : "") + "[stderr] " + stderr;
int exitCode = process.ExitCode;
string status = exitCode == 0 ? "[RUN OK]" : "[RUN EXIT " + exitCode + "]";
return status + " " + command + "\n" + combined.Trim();
}
}
catch (Exception ex)
{
return "[RUN ERROR] " + command + " — " + ex.Message;
}
}
private string ListFiles(string path)
{
try
{
string targetPath = string.IsNullOrWhiteSpace(path)
? _workDir
: Path.GetFullPath(Path.Combine(_workDir, path.Trim()));
if (!Directory.Exists(targetPath))
return "[LIST ERROR] Directory not found: " + path;
var sb = new StringBuilder();
sb.AppendLine("[LIST] " + targetPath);
foreach (string entry in Directory.GetFileSystemEntries(targetPath, "*", SearchOption.AllDirectories))
{
string rel = entry.Substring(_workDir.Length).TrimStart(Path.DirectorySeparatorChar);
bool isDir = Directory.Exists(entry);
sb.AppendLine((isDir ? "[DIR] " : "[FILE] ") + rel);
}
return sb.ToString().Trim();
}
catch (Exception ex)
{
return "[LIST ERROR] " + ex.Message;
}
}
private string DeleteFile(string relativePath)
{
if (string.IsNullOrWhiteSpace(relativePath))
return "[DELETE ERROR] No file path provided";
relativePath = relativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
string fullPath = Path.GetFullPath(Path.Combine(_workDir, relativePath));
// Safety: don't delete outside the work directory
if (!fullPath.StartsWith(_workDir, StringComparison.OrdinalIgnoreCase))
return "[DELETE ERROR] Path escapes work directory: " + relativePath;
if (File.Exists(fullPath))
{
try
{
// Clear read-only if set
FileAttributes attrs = File.GetAttributes(fullPath);
if ((attrs & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
File.SetAttributes(fullPath, attrs & ~FileAttributes.ReadOnly);
File.Delete(fullPath);
return "[DELETE OK] " + relativePath;
}
catch (Exception ex)
{
return "[DELETE FAILED] " + relativePath + " — " + ex.Message;
}
}
else if (Directory.Exists(fullPath))
{
try
{
Directory.Delete(fullPath, recursive: true);
return "[DELETE OK] " + relativePath + " (directory)";
}
catch (Exception ex)
{
return "[DELETE FAILED] " + relativePath + " — " + ex.Message;
}
}
else
{
return "[DELETE ERROR] Not found: " + relativePath;
}
}
private string RenameFile(string relativePath, string newRelativePath)
{
if (string.IsNullOrWhiteSpace(relativePath))
return "[RENAME ERROR] No source path provided";
if (string.IsNullOrWhiteSpace(newRelativePath))
return "[RENAME ERROR] No destination path provided";
relativePath = relativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
newRelativePath = newRelativePath.Trim().Replace('/', Path.DirectorySeparatorChar);
string fullSource = Path.GetFullPath(Path.Combine(_workDir, relativePath));
string fullDest = Path.GetFullPath(Path.Combine(_workDir, newRelativePath));
// Safety: both paths must be within work directory
if (!fullSource.StartsWith(_workDir, StringComparison.OrdinalIgnoreCase))
return "[RENAME ERROR] Source escapes work directory: " + relativePath;
if (!fullDest.StartsWith(_workDir, StringComparison.OrdinalIgnoreCase))
return "[RENAME ERROR] Destination escapes work directory: " + newRelativePath;
if (!File.Exists(fullSource) && !Directory.Exists(fullSource))
return "[RENAME ERROR] Source not found: " + relativePath;
if (File.Exists(fullDest) || Directory.Exists(fullDest))
return "[RENAME ERROR] Destination already exists: " + newRelativePath;
try
{
// Ensure destination directory exists
string destDir = Path.GetDirectoryName(fullDest);
if (!string.IsNullOrEmpty(destDir) && !Directory.Exists(destDir))
Directory.CreateDirectory(destDir);
if (File.Exists(fullSource))
File.Move(fullSource, fullDest);
else
Directory.Move(fullSource, fullDest);
return "[RENAME OK] " + relativePath + " → " + newRelativePath;
}
catch (Exception ex)
{
return "[RENAME FAILED] " + relativePath + " — " + ex.Message;
}
}
}
}