Mayank2027 commited on
Commit
61c52c7
·
verified ·
1 Parent(s): 919aed5

Update backend/agent.py

Browse files
Files changed (1) hide show
  1. backend/agent.py +21 -15
backend/agent.py CHANGED
@@ -9,12 +9,12 @@ class CodingAgent:
9
  self.model = model
10
  self.status = "idle"
11
  self.logs = []
12
- self.proposed_changes = [] # list of {file, search, replace}
13
 
14
  async def plan_task(self, prompt: str) -> List[dict]:
15
  """Generate a plan with file edits (diffs) but do not apply them."""
16
  self.status = "planning"
17
- system = """You are a coding agent. For the given task, you must output a JSON array of file changes.
18
  Each change is an object with:
19
  - "filepath": relative path
20
  - "action": "create" or "edit"
@@ -27,7 +27,6 @@ class CodingAgent:
27
  ]
28
  response = await self._generate(messages)
29
  try:
30
- # Extract JSON array from response (may be surrounded by markdown)
31
  json_match = re.search(r"\[.*\]", response, re.DOTALL)
32
  if json_match:
33
  changes = json.loads(json_match.group())
@@ -64,6 +63,24 @@ class CodingAgent:
64
  self.logs.append(f"Error applying change {idx}: {e}")
65
  self.status = "completed"
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  async def fix_error(self, file_path: str, error_output: str):
68
  p = self.project_path / file_path
69
  code = p.read_text() if p.exists() else ""
@@ -78,15 +95,4 @@ class CodingAgent:
78
  full = ""
79
  async for token in route_chat(self.model, messages):
80
  full += token
81
- return full
82
-
83
- async def execute_task_stream(self, prompt: str, auto_approve: bool = False):
84
- """Stream results for the old execute_task (we'll keep for backward compat)."""
85
- yield {"type": "status", "content": "Agent planning..."}
86
- changes = await self.plan_task(prompt)
87
- if auto_approve:
88
- await self.apply_changes(list(range(len(changes))))
89
- yield {"type": "result", "content": "Changes applied."}
90
- else:
91
- yield {"type": "plan", "changes": changes}
92
- yield {"type": "done", "content": ""}
 
9
  self.model = model
10
  self.status = "idle"
11
  self.logs = []
12
+ self.proposed_changes = []
13
 
14
  async def plan_task(self, prompt: str) -> List[dict]:
15
  """Generate a plan with file edits (diffs) but do not apply them."""
16
  self.status = "planning"
17
+ system = """You are a coding agent. For the given task, output a JSON array of file changes.
18
  Each change is an object with:
19
  - "filepath": relative path
20
  - "action": "create" or "edit"
 
27
  ]
28
  response = await self._generate(messages)
29
  try:
 
30
  json_match = re.search(r"\[.*\]", response, re.DOTALL)
31
  if json_match:
32
  changes = json.loads(json_match.group())
 
63
  self.logs.append(f"Error applying change {idx}: {e}")
64
  self.status = "completed"
65
 
66
+ async def execute_task(self, prompt: str, auto_approve: bool = False):
67
+ """Old execute_task interface; kept for backward compatibility."""
68
+ changes = await self.plan_task(prompt)
69
+ if auto_approve:
70
+ await self.apply_changes(list(range(len(changes))))
71
+ return "\n".join(self.logs)
72
+
73
+ async def execute_task_stream(self, prompt: str, auto_approve: bool = False):
74
+ """Stream wrapper for the old agent run endpoint."""
75
+ yield {"type": "status", "content": "Agent planning..."}
76
+ changes = await self.plan_task(prompt)
77
+ if auto_approve:
78
+ await self.apply_changes(list(range(len(changes))))
79
+ yield {"type": "result", "content": "Changes applied."}
80
+ else:
81
+ yield {"type": "plan", "changes": changes}
82
+ yield {"type": "done", "content": ""}
83
+
84
  async def fix_error(self, file_path: str, error_output: str):
85
  p = self.project_path / file_path
86
  code = p.read_text() if p.exists() else ""
 
95
  full = ""
96
  async for token in route_chat(self.model, messages):
97
  full += token
98
+ return full