Repository navigation
Expand file tree
/
Copy pathpatch_process.py
More file actions
352 lines (306 loc) · 18.9 KB
/
Copy pathpatch_process.py
File metadata and controls
352 lines (306 loc) · 18.9 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
import sys
with open('AgentContextManager.cs', 'r', encoding='utf-8') as f:
lines = f.readlines()
new_lines = []
skip = False
# We want to replace everything from " // --- ROUTING PHASE ---" up to " if (NativeTools._fileBackups.Count > 0"
process_replacement = """ // --- ROUTING PHASE ---
string routeDecision = "API";
CodingSahayi.Data.ModelEndpointConfig activeConfig = null;
string currentModel = SettingsManager.ModelName;
if (currentModel == "Local Model Only")
{
routeDecision = "LOCAL";
activeConfig = SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Local && m.IsEnabled);
onStatusUpdate("Using LOCAL model...");
}
else if (currentModel == "Hybrid Router (Auto)")
{
onStatusUpdate("Checking local model availability...");
bool localAvailable = await IsLocalAvailableAsync();
if (localAvailable)
{
onStatusUpdate("Routing task...");
var (route, plan) = await PlanAndRouteTask(userMessage, cancellationToken);
routeDecision = route;
if (routeDecision == "LOCAL")
{
activeConfig = SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Local && m.IsEnabled);
onStatusUpdate(!string.IsNullOrEmpty(plan)
? $"Routing via LOCAL model — {plan}"
: "Routing via LOCAL model...");
// --- MEMORY BANK INJECTION ---
try {
using var db = new CodingSahayi.Data.AppDbContext();
var activeProject = db.Projects.FirstOrDefault(p => p.WorkspacePath == WorkspaceDirectory);
if (activeProject != null) {
var pastLessons = db.ProjectKnowledgeBase.Where(k => k.ProjectId == activeProject.Id).Select(k => k.LearnedImplementation).ToList();
if (pastLessons.Any()) {
string appendedLessons = "\\n\\nProject Context & Past Lessons:\\n" + string.Join("\\n", pastLessons);
if (_apiHistory.FirstOrDefault() is SystemChatMessage sysMsg) {
if (!sysMsg.Content[0].Text.Contains("Project Context & Past Lessons")) {
_apiHistory[0] = new SystemChatMessage(sysMsg.Content[0].Text + appendedLessons);
}
}
}
}
} catch { }
}
else
{
activeConfig = SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Cloud && m.IsEnabled);
onStatusUpdate(!string.IsNullOrEmpty(plan)
? $"Routing via CLOUD API — {plan}"
: "Routing via CLOUD API...");
}
}
else
{
activeConfig = SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Cloud && m.IsEnabled);
onStatusUpdate("Local model offline, using CLOUD API...");
}
}
else
{
onStatusUpdate("Using CLOUD API...");
string displayMatch = currentModel;
int parenIdx = currentModel.IndexOf(" (");
if (parenIdx > 0) displayMatch = currentModel.Substring(0, parenIdx);
activeConfig = SettingsManager.ConfiguredModels.FirstOrDefault(m => m.DisplayName == displayMatch) ??
SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Cloud && m.IsEnabled);
routeDecision = "API";
}
if (activeConfig == null)
{
activeConfig = SettingsManager.ConfiguredModels.FirstOrDefault();
}
// --- AGENTIC LOOP ---
int iterationCount = 0;
bool requiresAction = true;
string finalResponse = string.Empty;
while (requiresAction)
{
if (iterationCount >= maxIterations)
{
finalResponse = "I've hit my iteration limit. How would you like me to proceed?";
break;
}
iterationCount++;
requiresAction = false;
try
{
onStatusUpdate($"Calling model {activeConfig?.DisplayName}... (Iteration {iterationCount}/{maxIterations})");
ChatCompletion completion = await ExecuteWithFallbackAsync(activeConfig, _apiHistory, _chatOptions, onStatusUpdate, cancellationToken);
if (completion.FinishReason == ChatFinishReason.ToolCalls)
{
_apiHistory.Add(new AssistantChatMessage(completion));
onStatusUpdate($"Running {completion.ToolCalls.Count} tool(s)... (Iteration {iterationCount}/{maxIterations})");
foreach (var toolCall in completion.ToolCalls)
{
string toolResult = string.Empty;
bool success = true;
try
{
var argsStr = toolCall.FunctionArguments.ToString();
onToolStart?.Invoke(toolCall.Id, toolCall.FunctionName, argsStr);
int retryCount = 0;
bool toolIsModifying = toolCall.FunctionName == "patch_file" || toolCall.FunctionName == "write_file" || toolCall.FunctionName == "batch_patch_file";
while (retryCount < 2)
{
var args = System.Text.Json.JsonSerializer.Deserialize<System.Text.Json.JsonElement>(argsStr);
var execution = await ExecuteToolAsync(toolCall.FunctionName, args, cancellationToken);
toolResult = execution.result;
success = execution.success;
if (toolIsModifying && success)
{
string filePath = ResolvePath(args.TryGetProperty("filePath", out var fp) ? (fp.GetString() ?? "") : "");
if (toolCall.FunctionName == "batch_patch_file") {
if (args.TryGetProperty("patches", out var pArr) && pArr.ValueKind == System.Text.Json.JsonValueKind.Array && pArr.GetArrayLength() > 0)
filePath = ResolvePath(pArr[0].GetProperty("filePath").GetString() ?? "");
}
if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath) && filePath.EndsWith(".cs"))
{
bool patchValid = await EvaluatePatchAsync(filePath, argsStr);
if (!patchValid)
{
retryCount++;
if (retryCount >= 2)
{
NativeTools.RollbackChanges();
toolResult = $"CRITICAL FAILURE: Patch was invalid or caused syntax errors. Rolled back all changes.";
success = false;
finalResponse = toolResult;
break;
}
else
{
string syntaxStatus = WorkspaceCodeAnalysisService.VerifySyntax(filePath);
NativeTools.RollbackChanges();
var retryHistory = new List<ChatMessage>(_apiHistory);
retryHistory.Add(new ToolChatMessage(toolCall.Id, $"Error: The patch caused syntax errors:\\n{syntaxStatus}\\nPlease generate a corrected tool call."));
var clientRetry = GetChatClient(activeConfig);
var retryResp = await clientRetry.CompleteChatAsync(retryHistory, _chatOptions, cancellationToken);
if (retryResp.Value.FinishReason == ChatFinishReason.ToolCalls && retryResp.Value.ToolCalls.Count > 0)
{
argsStr = retryResp.Value.ToolCalls[0].FunctionArguments.ToString();
continue;
}
}
}
}
}
// --- MEMORY BANK EXTRACTION ---
if (routeDecision == "API" && toolCall.FunctionName == "execute_terminal" && argsStr.Contains("dotnet build") && success && toolResult.Contains("Build succeeded"))
{
_ = Task.Run(async () => {
try {
var summaryPrompt = "Summarize the architectural change you just made in 1-2 sentences. Focus on file structure and logic.";
var bgHistory = new List<ChatMessage>(_apiHistory) { new UserChatMessage(summaryPrompt) };
var clientBg = GetChatClient(SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Cloud && m.IsEnabled));
var resp = await clientBg.CompleteChatAsync(bgHistory, new ChatCompletionOptions { AllowParallelToolCalls = false });
var summaryResponse = resp.Value.Content[0].Text;
using var bgDb = new CodingSahayi.Data.AppDbContext();
var proj = bgDb.Projects.FirstOrDefault(p => p.WorkspacePath == WorkspaceDirectory);
if (proj != null) {
bgDb.ProjectKnowledgeBase.Add(new CodingSahayi.Data.ProjectKnowledge {
ProjectId = proj.Id,
TaskDescription = userMessage,
LearnedImplementation = summaryResponse,
DateLearned = DateTime.UtcNow
});
bgDb.SaveChanges();
}
} catch { }
});
}
break;
}
}
catch (Exception ex)
{
toolResult = $"Error parsing tool args: {ex.Message}";
success = false;
}
onToolEnd?.Invoke(toolCall.Id, toolResult, success);
_apiHistory.Add(new ToolChatMessage(toolCall.Id, toolResult));
}
PruneContextIfNecessary();
requiresAction = true;
}
else
{
_apiHistory.Add(new AssistantChatMessage(completion));
finalResponse = completion.Content[0].Text;
if (finalResponse.TrimStart().StartsWith("{") && finalResponse.Contains("\\"name\\"") && finalResponse.Contains("\\"parameters\\""))
{
try
{
using var doc = System.Text.Json.JsonDocument.Parse(finalResponse);
var root = doc.RootElement;
if (root.TryGetProperty("name", out var nameProp) && root.TryGetProperty("parameters", out var paramsProp))
{
string toolName = nameProp.GetString() ?? "";
string argsStr = paramsProp.ToString();
string toolId = "fallback_" + Guid.NewGuid().ToString().Substring(0, 8);
onToolStart?.Invoke(toolId, toolName, argsStr);
var execution = await ExecuteToolAsync(toolName, paramsProp, cancellationToken);
onToolEnd?.Invoke(toolId, execution.result, execution.success);
_apiHistory.Add(new UserChatMessage($"[Tool Execution Result]:\\n{execution.result}"));
requiresAction = true;
continue;
}
}
catch { }
}
PruneContextIfNecessary();
}
}
catch (Exception ex)
{
Serilog.Log.Error(ex, "Error in agent loop");
finalResponse = $"**Error:** {ex.Message}";
requiresAction = false;
}
}
"""
eval_replacement = """ private async Task<bool> EvaluatePatchAsync(string filePath, string proposedPatch)
{
string syntaxStatus = WorkspaceCodeAnalysisService.VerifySyntax(filePath);
if (syntaxStatus == "Syntax OK" || syntaxStatus.Contains("Syntax OK - Fallback")) return true;
var systemPrompt = new SystemChatMessage("You are a code critic. The following file has syntax errors after a patch. Analyze the errors and the proposed patch. Reply with ONLY 'REJECT' if the patch is broken, or 'ACCEPT' if the error is a false positive.");
var userPrompt = new UserChatMessage($"File: {filePath}\\nErrors:\\n{syntaxStatus}\\nPatch:\\n{proposedPatch}");
try
{
var localConfig = SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Local && m.IsEnabled);
var client = GetChatClient(localConfig ?? SettingsManager.ConfiguredModels.First());
var resp = await client.CompleteChatAsync(new List<ChatMessage> { systemPrompt, userPrompt });
if (resp.Value.Content[0].Text.Contains("REJECT")) return false;
return true;
}
catch
{
return false;
}
}
private async Task<ChatCompletion> ExecuteWithFallbackAsync(CodingSahayi.Data.ModelEndpointConfig primaryConfig, List<ChatMessage> history, ChatCompletionOptions options, Action<string> onStatusUpdate, System.Threading.CancellationToken cancellationToken)
{
var currentConfig = primaryConfig;
while (currentConfig != null)
{
var client = GetChatClient(currentConfig);
int maxRetries = 3;
for (int retry = 0; retry <= maxRetries; retry++)
{
try
{
return await client.CompleteChatAsync(history, options, cancellationToken);
}
catch (Exception ex) when (retry < maxRetries && IsRateLimitError(ex))
{
int delaySeconds = (int)Math.Pow(2, retry + 1);
onStatusUpdate($"Rate limited. Retrying in {delaySeconds}s... (attempt {retry + 1}/{maxRetries})");
await Task.Delay(delaySeconds * 1000, cancellationToken);
}
catch (Exception ex) when (retry == maxRetries || !IsRateLimitError(ex))
{
if (!string.IsNullOrEmpty(currentConfig.FallbackModelId))
{
var fallbackConfig = SettingsManager.ConfiguredModels.FirstOrDefault(m => m.Id == currentConfig.FallbackModelId && m.IsEnabled);
if (fallbackConfig != null)
{
Serilog.Log.Warning(ex, "Primary model {ModelName} failed. Falling back to {FallbackName}", currentConfig.DisplayName, fallbackConfig.DisplayName);
onStatusUpdate($"Model {currentConfig.DisplayName} failed. Falling back to {fallbackConfig.DisplayName}...");
currentConfig = fallbackConfig;
break; // break retry loop to try fallback
}
}
throw; // No fallback or fallback failed
}
}
}
throw new Exception("No available models to execute the request.");
}"""
for i in range(len(lines)):
line = lines[i]
if line.startswith(" // --- ROUTING PHASE ---"):
skip = True
new_lines.append(process_replacement + "\n")
continue
if skip and line.startswith(" if (NativeTools._fileBackups.Count > 0"):
skip = False
if skip:
continue
if "private async Task<bool> EvaluatePatchAsync" in line:
skip = True
new_lines.append(eval_replacement + "\n")
continue
if skip and "private async Task<(string result, bool success)> ExecuteToolAsync" in line:
skip = False
if skip:
continue
new_lines.append(line)
code = "".join(new_lines)
code = code.replace("var resp = await _localApiClient.CompleteChatAsync",
"var clientBg = GetChatClient(SettingsManager.ConfiguredModels.OrderBy(m => m.Priority).FirstOrDefault(m => m.Type == CodingSahayi.Data.ModelType.Local && m.IsEnabled));\\n var resp = await clientBg.CompleteChatAsync")
with open('AgentContextManager.cs', 'w', encoding='utf-8') as f:
f.write(code)