-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolParser.cs
More file actions
310 lines (270 loc) · 13.7 KB
/
Copy pathToolParser.cs
File metadata and controls
310 lines (270 loc) · 13.7 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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace GIDE
{
public class ToolCall
{
public string Type;
public string Path;
public string Content;
public string Command;
public string Destination; // For RENAME: new path
public string OldText; // For PATCH: text to find
public string NewText; // For PATCH: replacement text
}
public static class ToolParser
{
public static List<ToolCall> Parse(string response)
{
var tools = new List<ToolCall>();
MatchCollection matches = Regex.Matches(response,
@"<<<TOOL:(.+?)>>>(.*?)<<<END_TOOL>>>",
RegexOptions.Singleline);
foreach (Match match in matches)
{
var tool = new ToolCall();
tool.Type = match.Groups[1].Value.Trim().ToUpper();
string body = match.Groups[2].Value;
switch (tool.Type)
{
case "WRITE":
{
// First line is the file path
string trimmed = body.TrimStart('\r', '\n', ' ');
int newline = trimmed.IndexOfAny(new char[] { '\n', '\r' });
if (newline < 0)
{
tool.Path = trimmed.Trim();
tool.Content = "";
}
else
{
tool.Path = trimmed.Substring(0, newline).Trim();
string rest = trimmed.Substring(newline + 1);
int contentStart = rest.IndexOf("<<<CONTENT>>>");
int contentEnd = rest.IndexOf("<<<END_CONTENT>>>");
if (contentStart >= 0 && contentEnd > contentStart)
{
// Extract content between markers, preserving internal whitespace
// but stripping exactly one leading newline after the marker
string raw = rest.Substring(contentStart + 13, contentEnd - contentStart - 13);
if (raw.StartsWith("\r\n")) raw = raw.Substring(2);
else if (raw.StartsWith("\n")) raw = raw.Substring(1);
// Strip only trailing newline added by the model after <<<END_CONTENT>>>
tool.Content = raw.TrimEnd('\r', '\n');
}
else
{
// Fallback: Try to extract from markdown code blocks (```lang ... ```)
// This handles cases where the model uses markdown instead of <<<CONTENT>>>
Match mdMatch = Regex.Match(rest, @"```[a-zA-Z]*\r?\n(.*?)```", RegexOptions.Singleline);
if (mdMatch.Success)
{
tool.Content = mdMatch.Groups[1].Value.TrimEnd('\r', '\n');
}
else
{
// No content markers and no markdown — use everything after the path line
tool.Content = rest.TrimEnd('\r', '\n');
}
}
}
break;
}
case "READ":
case "LIST":
case "DELETE":
tool.Path = body.Trim();
break;
case "PATCH":
{
// Format:
// path/to/file.cs
// <<<OLD>>>
// text to find
// <<<NEW>>>
// replacement text
// <<<END_PATCH>>>
string trimmedPatch = body.TrimStart('\r', '\n', ' ');
int firstNewline = trimmedPatch.IndexOfAny(new char[] { '\n', '\r' });
if (firstNewline < 0) break;
tool.Path = trimmedPatch.Substring(0, firstNewline).Trim();
string patchBody = trimmedPatch.Substring(firstNewline + 1);
int oldStart = patchBody.IndexOf("<<<OLD>>>");
int newStart = patchBody.IndexOf("<<<NEW>>>");
int endPatch = patchBody.IndexOf("<<<END_PATCH>>>");
if (oldStart >= 0 && newStart > oldStart)
{
// Extract OLD text
string oldRaw = patchBody.Substring(oldStart + 9,
newStart - oldStart - 9);
if (oldRaw.StartsWith("\r\n")) oldRaw = oldRaw.Substring(2);
else if (oldRaw.StartsWith("\n")) oldRaw = oldRaw.Substring(1);
tool.OldText = oldRaw.TrimEnd('\r', '\n');
// Extract NEW text
int newContentStart = newStart + 9;
int newContentEnd = endPatch >= 0 ? endPatch : patchBody.Length;
string newRaw = patchBody.Substring(newContentStart,
newContentEnd - newContentStart);
if (newRaw.StartsWith("\r\n")) newRaw = newRaw.Substring(2);
else if (newRaw.StartsWith("\n")) newRaw = newRaw.Substring(1);
tool.NewText = newRaw.TrimEnd('\r', '\n');
}
break;
}
case "RENAME":
{
// Two lines: old path, then new path
string trimmedRename = body.TrimStart('\r', '\n', ' ');
string[] renameLines = trimmedRename.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (renameLines.Length >= 2)
{
tool.Path = renameLines[0].Trim();
tool.Destination = renameLines[1].Trim();
}
else if (renameLines.Length == 1)
{
tool.Path = renameLines[0].Trim();
}
break;
}
case "RUN":
tool.Command = body.Trim();
break;
}
tools.Add(tool);
}
return tools;
}
public static string StripTools(string response)
{
// Remove all tool blocks
string result = Regex.Replace(response,
@"<<<TOOL:(.+?)>>>(.*?)<<<END_TOOL>>>",
"", RegexOptions.Singleline);
return result.Trim();
}
/// <summary>
/// Attempts to convert a response that contains markdown code blocks (but no tool markers)
/// into proper tool format. Returns the converted response if successful, or null if no conversion possible.
/// </summary>
public static string TryConvertMarkdownToTools(string response, string workDir)
{
// If already has tool markers, no conversion needed
if (response.Contains("<<<TOOL:"))
return null;
// Look for markdown code blocks with file paths mentioned nearby
var conversions = new List<string>();
// Try to find code blocks with file paths
MatchCollection codeBlocks = Regex.Matches(response,
@"```([a-zA-Z#]*)\r?\n(.*?)```",
RegexOptions.Singleline);
if (codeBlocks.Count == 0)
return null;
// First pass: collect all file paths mentioned anywhere in the response
var mentionedPaths = new List<string>();
MatchCollection allPathMentions = Regex.Matches(response,
@"[`'\""]([\\\/\w.-]+\.(?:cs|js|ts|py|java|go|rs|cpp|c|h|rb|php|swift|kt|vb|fs|jsx|tsx|vue|svelte|html|css|scss|json|xml|yaml|yml|toml|sql|sh|bat|ps1|md|txt))[`'\"":,\s]",
RegexOptions.IgnoreCase);
foreach (Match m in allPathMentions)
mentionedPaths.Add(m.Groups[1].Value.Replace("\\", "/"));
int pathIndex = 0;
foreach (Match block in codeBlocks)
{
string lang = block.Groups[1].Value.ToLower();
string code = block.Groups[2].Value;
// Skip very short code blocks (likely not full files)
if (code.Length < 50)
continue;
// Try to find a file path mentioned near this code block
int blockPos = block.Index;
string textBefore = response.Substring(Math.Max(0, blockPos - 500), Math.Min(500, blockPos));
// Look for file paths in the text before the code block
Match pathMatch = Regex.Match(textBefore,
@"[`'\""]([\w\\/.-]+\.(?:cs|js|ts|py|java|go|rs|cpp|c|h|rb|php|swift|kt|vb|fs|jsx|tsx|vue|svelte|html|css|scss|json|xml|yaml|yml|toml|sql|sh|bat|ps1|md|txt))[`'\"":]",
RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
if (pathMatch.Success)
{
string filePath = pathMatch.Groups[1].Value.Replace("\\", "/");
conversions.Add("<<<TOOL:WRITE>>>\n" + filePath + "\n<<<CONTENT>>>\n" + code.TrimEnd('\r', '\n') + "\n<<<END_CONTENT>>>\n<<<END_TOOL>>>");
}
else if (pathIndex < mentionedPaths.Count)
{
// Use the next mentioned path from the full response as fallback
// (model often lists files at the top then shows code blocks in order)
string filePath = mentionedPaths[pathIndex];
pathIndex++;
conversions.Add("<<<TOOL:WRITE>>>\n" + filePath + "\n<<<CONTENT>>>\n" + code.TrimEnd('\r', '\n') + "\n<<<END_CONTENT>>>\n<<<END_TOOL>>>");
}
else if (!string.IsNullOrEmpty(lang))
{
// Try to infer file path from language and code content
string inferredPath = InferFilePath(lang, code, workDir);
if (!string.IsNullOrEmpty(inferredPath))
{
conversions.Add("<<<TOOL:WRITE>>>\n" + inferredPath + "\n<<<CONTENT>>>\n" + code.TrimEnd('\r', '\n') + "\n<<<END_CONTENT>>>\n<<<END_TOOL>>>");
}
}
}
if (conversions.Count > 0)
{
return string.Join("\n\n", conversions.ToArray()) + "\n\n[Auto-converted from markdown]";
}
return null;
}
private static string InferFilePath(string lang, string code, string workDir)
{
// Try to infer file path from namespace/class declarations
if (lang == "csharp" || lang == "cs" || lang == "c#")
{
// Look for namespace and class
Match nsMatch = Regex.Match(code, @"namespace\s+([\w.]+)");
Match classMatch = Regex.Match(code, @"(?:public|internal|private)?\s*(?:static|abstract|sealed|partial)?\s*class\s+(\w+)");
if (classMatch.Success)
{
string className = classMatch.Groups[1].Value;
// Try to find existing file with this class name
try
{
string[] files = System.IO.Directory.GetFiles(workDir, className + ".cs", System.IO.SearchOption.AllDirectories);
if (files.Length == 1)
{
// Return relative path
return files[0].Substring(workDir.Length).TrimStart(System.IO.Path.DirectorySeparatorChar).Replace("\\", "/");
}
else if (files.Length > 1)
{
// Ambiguous file name, don't guess
return null;
}
}
catch { }
// Default to src folder
return "src/" + className + ".cs";
}
}
return null;
}
/// <summary>
/// Detects if the model's response contains code blocks that weren't written to disk.
/// Used to trigger the nudge flow.
/// </summary>
public static bool HasUnwrittenCode(string response)
{
if (string.IsNullOrEmpty(response)) return false;
// If it already has tool markers, the code was handled
if (response.Contains("<<<TOOL:WRITE>>>")) return false;
// Check for substantial markdown code blocks (at least 5 lines)
MatchCollection blocks = Regex.Matches(response, @"```[a-zA-Z]*\r?\n(.*?)```", RegexOptions.Singleline);
foreach (Match block in blocks)
{
string code = block.Groups[1].Value;
int lineCount = code.Split('\n').Length;
if (lineCount >= 5)
return true;
}
return false;
}
}
}