PYAE1994 commited on
Commit
85c613a
·
verified ·
1 Parent(s): 6037ed6

Update app/agent/agent.py

Browse files
Files changed (1) hide show
  1. app/agent/agent.py +76 -4
app/agent/agent.py CHANGED
@@ -1,13 +1,17 @@
1
  import requests
2
  import json
3
  from config import Config
4
- import tools
 
5
 
6
  class Agent:
7
 
8
  def __init__(self):
9
  self.history = []
10
 
 
 
 
11
  def call_llm(self, messages):
12
  res = requests.post(
13
  Config.LLM_API_URL,
@@ -21,20 +25,88 @@ class Agent:
21
  "temperature": 0.2
22
  }
23
  )
 
24
  return res.json()
25
 
 
 
 
26
  def run(self, user_input):
27
 
28
  messages = [
29
- {"role": "system", "content": Config.SYSTEM_PROMPT},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  {"role": "user", "content": user_input}
31
  ]
32
 
33
  response = self.call_llm(messages)
34
 
35
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
 
 
 
 
38
  if __name__ == "__main__":
39
  agent = Agent()
40
- print(agent.run("Explain this project structure"))
 
1
  import requests
2
  import json
3
  from config import Config
4
+ from tools_runtime import execute_tool_call
5
+
6
 
7
  class Agent:
8
 
9
  def __init__(self):
10
  self.history = []
11
 
12
+ # ========================
13
+ # LLM CALL LAYER
14
+ # ========================
15
  def call_llm(self, messages):
16
  res = requests.post(
17
  Config.LLM_API_URL,
 
25
  "temperature": 0.2
26
  }
27
  )
28
+
29
  return res.json()
30
 
31
+ # ========================
32
+ # MAIN EXECUTION LOOP
33
+ # ========================
34
  def run(self, user_input):
35
 
36
  messages = [
37
+ {
38
+ "role": "system",
39
+ "content": Config.SYSTEM_PROMPT + """
40
+
41
+ You are an autonomous coding agent.
42
+
43
+ You can respond in TWO ways:
44
+
45
+ 1) NORMAL RESPONSE:
46
+ Explain or answer normally.
47
+
48
+ 2) TOOL CALL (STRICT JSON ONLY):
49
+ {
50
+ "tool": "write_file",
51
+ "args": {
52
+ "path": "test.txt",
53
+ "content": "hello"
54
+ }
55
+ }
56
+
57
+ Available tools:
58
+ - run_shell(cmd)
59
+ - write_file(path, content)
60
+ - read_file(path)
61
+ - list_files(path)
62
+ - delete_file(path)
63
+
64
+ If tool is required → respond ONLY JSON.
65
+ If not → normal text.
66
+ """
67
+ },
68
  {"role": "user", "content": user_input}
69
  ]
70
 
71
  response = self.call_llm(messages)
72
 
73
+ # ========================
74
+ # Extract LLM output
75
+ # ========================
76
+ content = response["choices"][0]["message"]["content"]
77
+
78
+ # ========================
79
+ # TOOL PARSING LAYER
80
+ # ========================
81
+ try:
82
+ tool_call = json.loads(content)
83
+
84
+ if isinstance(tool_call, dict) and "tool" in tool_call:
85
+ result = execute_tool_call(tool_call)
86
+
87
+ # store history
88
+ self.history.append({
89
+ "input": user_input,
90
+ "tool_call": tool_call,
91
+ "result": result
92
+ })
93
+
94
+ return result
95
+
96
+ except json.JSONDecodeError:
97
+ pass
98
+
99
+ # ========================
100
+ # NORMAL RESPONSE
101
+ # ========================
102
+ return {
103
+ "response": content
104
+ }
105
 
106
 
107
+ # ========================
108
+ # DEBUG RUN
109
+ # ========================
110
  if __name__ == "__main__":
111
  agent = Agent()
112
+ print(agent.run("List files in current directory"))