Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/agent_controller.py +31 -24
game/agent_controller.py
CHANGED
|
@@ -1,17 +1,12 @@
|
|
| 1 |
-
from
|
| 2 |
import json
|
| 3 |
-
import random
|
| 4 |
|
| 5 |
class MycoController:
|
| 6 |
def __init__(self):
|
| 7 |
-
self.position = [1, 1]
|
| 8 |
self.history = []
|
| 9 |
|
| 10 |
def get_agent_decision(self, current_mushroom, collection):
|
| 11 |
-
"""
|
| 12 |
-
Queries Gemma for a decision.
|
| 13 |
-
Note: We use engine._llm but provide a specific 'Agent' prompt.
|
| 14 |
-
"""
|
| 15 |
prompt = f"""
|
| 16 |
Current Position: {self.position}
|
| 17 |
Mushroom in clearing: {'Yes' if current_mushroom else 'No'}
|
|
@@ -20,44 +15,56 @@ class MycoController:
|
|
| 20 |
Decide your next action: 'move', 'search', 'study', 'collect', or 'wait'.
|
| 21 |
If 'move', provide target coordinate (e.g., [1, 2]).
|
| 22 |
|
| 23 |
-
Respond ONLY in JSON format:
|
| 24 |
{{"action": "...", "target": [x, y], "thought": "..."}}
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
# We reuse the existing engine._llm functionality
|
| 28 |
response = engine._llm(prompt)
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
# Extract JSON from potential markdown text
|
| 33 |
start = response.find("{")
|
| 34 |
end = response.rfind("}") + 1
|
|
|
|
|
|
|
|
|
|
| 35 |
return json.loads(response[start:end])
|
| 36 |
-
except:
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
def run_tick(self, current_mushroom, collection):
|
| 40 |
-
"""
|
| 41 |
-
This is the heartbeat of the AI.
|
| 42 |
-
It decides and then executes via engine functions.
|
| 43 |
-
"""
|
| 44 |
decision = self.get_agent_decision(current_mushroom, collection)
|
| 45 |
action = decision.get("action")
|
| 46 |
|
| 47 |
result = {"action_taken": action, "thought": decision.get("thought")}
|
| 48 |
|
| 49 |
-
# EXECUTION LAYER
|
| 50 |
if action == "move":
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
elif action == "search":
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
elif action == "collect":
|
| 59 |
if current_mushroom:
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
return result
|
|
|
|
| 1 |
+
from . import engine
|
| 2 |
import json
|
|
|
|
| 3 |
|
| 4 |
class MycoController:
|
| 5 |
def __init__(self):
|
| 6 |
+
self.position = [1, 1]
|
| 7 |
self.history = []
|
| 8 |
|
| 9 |
def get_agent_decision(self, current_mushroom, collection):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
prompt = f"""
|
| 11 |
Current Position: {self.position}
|
| 12 |
Mushroom in clearing: {'Yes' if current_mushroom else 'No'}
|
|
|
|
| 15 |
Decide your next action: 'move', 'search', 'study', 'collect', or 'wait'.
|
| 16 |
If 'move', provide target coordinate (e.g., [1, 2]).
|
| 17 |
|
| 18 |
+
Respond ONLY in valid JSON format:
|
| 19 |
{{"action": "...", "target": [x, y], "thought": "..."}}
|
| 20 |
"""
|
| 21 |
|
|
|
|
| 22 |
response = engine._llm(prompt)
|
| 23 |
|
| 24 |
+
# FIX: Validate response exists before parsing
|
| 25 |
+
if not response:
|
| 26 |
+
return {"action": "wait", "target": None, "thought": "Engine returned no data."}
|
| 27 |
+
|
| 28 |
try:
|
|
|
|
| 29 |
start = response.find("{")
|
| 30 |
end = response.rfind("}") + 1
|
| 31 |
+
if start == -1 or end == 0:
|
| 32 |
+
raise ValueError("No JSON found in response")
|
| 33 |
+
|
| 34 |
return json.loads(response[start:end])
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"[Myco] Controller Parse Error: {e}")
|
| 37 |
+
return {"action": "wait", "target": None, "thought": "Failed to parse JSON."}
|
| 38 |
|
| 39 |
def run_tick(self, current_mushroom, collection):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
decision = self.get_agent_decision(current_mushroom, collection)
|
| 41 |
action = decision.get("action")
|
| 42 |
|
| 43 |
result = {"action_taken": action, "thought": decision.get("thought")}
|
| 44 |
|
| 45 |
+
# EXECUTION LAYER
|
| 46 |
if action == "move":
|
| 47 |
+
# Add basic validation for grid boundaries if necessary
|
| 48 |
+
target = decision.get("target")
|
| 49 |
+
if isinstance(target, list) and len(target) == 2:
|
| 50 |
+
self.position = target
|
| 51 |
|
| 52 |
elif action == "search":
|
| 53 |
+
# Ensure engine functions exist and return expected values
|
| 54 |
+
try:
|
| 55 |
+
mushroom, current, history = engine.discover_mushroom(collection)
|
| 56 |
+
result["data"] = {"mushroom": mushroom, "current": current}
|
| 57 |
+
except AttributeError:
|
| 58 |
+
result["thought"] = "Search function not found in engine."
|
| 59 |
|
| 60 |
elif action == "collect":
|
| 61 |
if current_mushroom:
|
| 62 |
+
try:
|
| 63 |
+
coll, hist = engine.collect_current(current_mushroom, collection, self.history)
|
| 64 |
+
result["data"] = {"collection": coll}
|
| 65 |
+
self.history = hist # Keep track of history
|
| 66 |
+
except AttributeError:
|
| 67 |
+
|
| 68 |
+
result["thought"] = "Collect function not found in engine."
|
| 69 |
|
| 70 |
return result
|