Spaces:
Sleeping
Sleeping
Update app/agent/agent.py
Browse files- app/agent/agent.py +52 -30
app/agent/agent.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
import requests
|
| 2 |
import json
|
|
|
|
| 3 |
from config import Config
|
|
|
|
|
|
|
| 4 |
from tools_runtime import execute_tool_call
|
| 5 |
|
| 6 |
|
|
@@ -8,6 +11,7 @@ class Agent:
|
|
| 8 |
|
| 9 |
def __init__(self):
|
| 10 |
self.history = []
|
|
|
|
| 11 |
|
| 12 |
# ========================
|
| 13 |
# LLM CALL LAYER
|
|
@@ -29,7 +33,25 @@ class Agent:
|
|
| 29 |
return res.json()
|
| 30 |
|
| 31 |
# ========================
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
# ========================
|
| 34 |
def run(self, user_input):
|
| 35 |
|
|
@@ -38,18 +60,13 @@ class Agent:
|
|
| 38 |
"role": "system",
|
| 39 |
"content": Config.SYSTEM_PROMPT + """
|
| 40 |
|
| 41 |
-
You are an autonomous coding + browser agent.
|
| 42 |
|
| 43 |
-
You can
|
| 44 |
|
| 45 |
-
========================
|
| 46 |
-
1)
|
| 47 |
-
========================
|
| 48 |
-
Explain or answer normally.
|
| 49 |
-
|
| 50 |
-
========================
|
| 51 |
-
2) TOOL CALL (STRICT JSON ONLY)
|
| 52 |
-
========================
|
| 53 |
{
|
| 54 |
"tool": "write_file",
|
| 55 |
"args": {
|
|
@@ -58,28 +75,28 @@ Explain or answer normally.
|
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
========================
|
| 64 |
-
|
| 65 |
-
SYSTEM TOOLS:
|
| 66 |
- run_shell(cmd)
|
| 67 |
- write_file(path, content)
|
| 68 |
- read_file(path)
|
| 69 |
- list_files(path)
|
| 70 |
- delete_file(path)
|
| 71 |
|
| 72 |
-
BROWSER
|
| 73 |
- browser_open(url)
|
| 74 |
- browser_click(selector)
|
| 75 |
- browser_type(selector, text)
|
| 76 |
- browser_screenshot(path)
|
| 77 |
|
| 78 |
-
========================
|
| 79 |
-
|
| 80 |
-
========================
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
"""
|
| 84 |
},
|
| 85 |
{
|
|
@@ -90,13 +107,10 @@ If explanation → normal text only
|
|
| 90 |
|
| 91 |
response = self.call_llm(messages)
|
| 92 |
|
| 93 |
-
# ========================
|
| 94 |
-
# Extract LLM output
|
| 95 |
-
# ========================
|
| 96 |
content = response["choices"][0]["message"]["content"]
|
| 97 |
|
| 98 |
# ========================
|
| 99 |
-
# TOOL
|
| 100 |
# ========================
|
| 101 |
try:
|
| 102 |
tool_call = json.loads(content)
|
|
@@ -105,22 +119,25 @@ If explanation → normal text only
|
|
| 105 |
|
| 106 |
result = execute_tool_call(tool_call)
|
| 107 |
|
| 108 |
-
# save memory
|
| 109 |
self.history.append({
|
| 110 |
"input": user_input,
|
| 111 |
"tool_call": tool_call,
|
| 112 |
"result": result
|
| 113 |
})
|
| 114 |
|
| 115 |
-
return
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
except json.JSONDecodeError:
|
| 118 |
pass
|
| 119 |
|
| 120 |
# ========================
|
| 121 |
-
# NORMAL RESPONSE
|
| 122 |
# ========================
|
| 123 |
return {
|
|
|
|
| 124 |
"response": content
|
| 125 |
}
|
| 126 |
|
|
@@ -130,4 +147,9 @@ If explanation → normal text only
|
|
| 130 |
# ========================
|
| 131 |
if __name__ == "__main__":
|
| 132 |
agent = Agent()
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
+
|
| 4 |
from config import Config
|
| 5 |
+
from planner import create_plan
|
| 6 |
+
from executor import Executor
|
| 7 |
from tools_runtime import execute_tool_call
|
| 8 |
|
| 9 |
|
|
|
|
| 11 |
|
| 12 |
def __init__(self):
|
| 13 |
self.history = []
|
| 14 |
+
self.executor = Executor()
|
| 15 |
|
| 16 |
# ========================
|
| 17 |
# LLM CALL LAYER
|
|
|
|
| 33 |
return res.json()
|
| 34 |
|
| 35 |
# ========================
|
| 36 |
+
# STEP MODE (AUTONOMOUS GOAL EXECUTION)
|
| 37 |
+
# ========================
|
| 38 |
+
def run_goal(self, goal: str):
|
| 39 |
+
|
| 40 |
+
# STEP 1: PLAN
|
| 41 |
+
plan = create_plan(goal)
|
| 42 |
+
|
| 43 |
+
# STEP 2: EXECUTE PLAN
|
| 44 |
+
results = self.executor.run_plan(plan)
|
| 45 |
+
|
| 46 |
+
return {
|
| 47 |
+
"mode": "goal_execution",
|
| 48 |
+
"goal": goal,
|
| 49 |
+
"plan": plan,
|
| 50 |
+
"results": results
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# ========================
|
| 54 |
+
# TOOL MODE (DIRECT AGENT CALL)
|
| 55 |
# ========================
|
| 56 |
def run(self, user_input):
|
| 57 |
|
|
|
|
| 60 |
"role": "system",
|
| 61 |
"content": Config.SYSTEM_PROMPT + """
|
| 62 |
|
| 63 |
+
You are an autonomous coding + browser + planning agent.
|
| 64 |
|
| 65 |
+
You can operate in TWO modes:
|
| 66 |
|
| 67 |
+
================================================
|
| 68 |
+
1) TOOL MODE (STRICT JSON ONLY)
|
| 69 |
+
================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
{
|
| 71 |
"tool": "write_file",
|
| 72 |
"args": {
|
|
|
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
+
Available tools:
|
| 79 |
+
SYSTEM:
|
|
|
|
|
|
|
|
|
|
| 80 |
- run_shell(cmd)
|
| 81 |
- write_file(path, content)
|
| 82 |
- read_file(path)
|
| 83 |
- list_files(path)
|
| 84 |
- delete_file(path)
|
| 85 |
|
| 86 |
+
BROWSER:
|
| 87 |
- browser_open(url)
|
| 88 |
- browser_click(selector)
|
| 89 |
- browser_type(selector, text)
|
| 90 |
- browser_screenshot(path)
|
| 91 |
|
| 92 |
+
================================================
|
| 93 |
+
2) NORMAL MODE
|
| 94 |
+
================================================
|
| 95 |
+
Explain or answer normally.
|
| 96 |
+
|
| 97 |
+
RULE:
|
| 98 |
+
- If action needed → JSON ONLY
|
| 99 |
+
- If explanation → text only
|
| 100 |
"""
|
| 101 |
},
|
| 102 |
{
|
|
|
|
| 107 |
|
| 108 |
response = self.call_llm(messages)
|
| 109 |
|
|
|
|
|
|
|
|
|
|
| 110 |
content = response["choices"][0]["message"]["content"]
|
| 111 |
|
| 112 |
# ========================
|
| 113 |
+
# TOOL EXECUTION PATH
|
| 114 |
# ========================
|
| 115 |
try:
|
| 116 |
tool_call = json.loads(content)
|
|
|
|
| 119 |
|
| 120 |
result = execute_tool_call(tool_call)
|
| 121 |
|
|
|
|
| 122 |
self.history.append({
|
| 123 |
"input": user_input,
|
| 124 |
"tool_call": tool_call,
|
| 125 |
"result": result
|
| 126 |
})
|
| 127 |
|
| 128 |
+
return {
|
| 129 |
+
"mode": "tool_execution",
|
| 130 |
+
"result": result
|
| 131 |
+
}
|
| 132 |
|
| 133 |
except json.JSONDecodeError:
|
| 134 |
pass
|
| 135 |
|
| 136 |
# ========================
|
| 137 |
+
# NORMAL RESPONSE PATH
|
| 138 |
# ========================
|
| 139 |
return {
|
| 140 |
+
"mode": "normal_response",
|
| 141 |
"response": content
|
| 142 |
}
|
| 143 |
|
|
|
|
| 147 |
# ========================
|
| 148 |
if __name__ == "__main__":
|
| 149 |
agent = Agent()
|
| 150 |
+
|
| 151 |
+
# Example 1: normal tool call
|
| 152 |
+
print(agent.run("List files in current directory"))
|
| 153 |
+
|
| 154 |
+
# Example 2: autonomous goal mode
|
| 155 |
+
print(agent.run_goal("Create a simple python web server"))
|