Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/agent_controller.py +53 -47
game/agent_controller.py
CHANGED
|
@@ -1,71 +1,77 @@
|
|
|
|
|
|
|
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
if not response:
|
| 26 |
return {"action": "wait", "target": None, "thought": "Engine returned no data."}
|
| 27 |
-
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if
|
| 32 |
-
raise ValueError("
|
| 33 |
-
|
| 34 |
-
return json.loads(response[start:end])
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"[Myco] Controller
|
| 37 |
-
return {"action": "wait", "target": None, "thought": "Failed to parse
|
| 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 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return result
|
|
|
|
|
|
| 1 |
+
# controller.py — full replacement
|
| 2 |
+
|
| 3 |
from . import engine
|
| 4 |
import json
|
| 5 |
|
| 6 |
+
|
| 7 |
class MycoController:
|
| 8 |
def __init__(self):
|
| 9 |
self.position = [1, 1]
|
| 10 |
self.history = []
|
| 11 |
|
| 12 |
def get_agent_decision(self, current_mushroom, collection):
|
| 13 |
+
# Build a real context so _llm has score/health/mystery data.
|
| 14 |
+
# This also prevents the system prompt from having blank lines.
|
| 15 |
+
ctx = engine._ctx(current_mushroom, list(collection or []))
|
| 16 |
+
|
| 17 |
+
prompt = (
|
| 18 |
+
f"Current position: {self.position}. "
|
| 19 |
+
f"Mushroom present: {'Yes' if current_mushroom else 'No'}. "
|
| 20 |
+
f"Collection count: {ctx.get('collection_count', 0)}. "
|
| 21 |
+
f"Score: {ctx.get('score', 0)}. Health: {ctx.get('health', 3)}/3. "
|
| 22 |
+
"Decide the next action: move, search, study, collect, or wait. "
|
| 23 |
+
"If move, provide a target coordinate. "
|
| 24 |
+
'Respond ONLY in valid JSON: {"action":"...","target":[x,y],"thought":"..."}'
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
response = engine._llm(prompt, ctx)
|
| 28 |
+
|
| 29 |
if not response:
|
| 30 |
return {"action": "wait", "target": None, "thought": "Engine returned no data."}
|
| 31 |
+
|
| 32 |
try:
|
| 33 |
+
parsed = json.loads(response)
|
| 34 |
+
# Validate the action field exists and is a known value
|
| 35 |
+
if parsed.get("action") not in {"move", "search", "study", "collect", "wait"}:
|
| 36 |
+
raise ValueError(f"Unknown action: {parsed.get('action')}")
|
| 37 |
+
return parsed
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"[Myco] Controller parse error: {e} — raw: {response!r}")
|
| 40 |
+
return {"action": "wait", "target": None, "thought": "Failed to parse response."}
|
| 41 |
|
| 42 |
def run_tick(self, current_mushroom, collection):
|
| 43 |
decision = self.get_agent_decision(current_mushroom, collection)
|
| 44 |
+
action = decision.get("action", "wait")
|
| 45 |
+
result = {"action_taken": action, "thought": decision.get("thought", "")}
|
| 46 |
+
|
|
|
|
|
|
|
| 47 |
if action == "move":
|
|
|
|
| 48 |
target = decision.get("target")
|
| 49 |
if isinstance(target, list) and len(target) == 2:
|
| 50 |
self.position = target
|
| 51 |
+
else:
|
| 52 |
+
result["thought"] = "Move action had no valid target coordinate."
|
| 53 |
+
|
| 54 |
elif action == "search":
|
| 55 |
+
mushroom, current, history = engine.discover_mushroom(collection)
|
| 56 |
+
result["data"] = {"mushroom": mushroom, "current": current}
|
| 57 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
elif action == "collect":
|
| 59 |
if current_mushroom:
|
| 60 |
+
coll, hist = engine.collect_current(
|
| 61 |
+
current_mushroom, list(collection or []), self.history
|
| 62 |
+
)
|
| 63 |
+
self.history = hist
|
| 64 |
+
result["data"] = {"collection": coll}
|
| 65 |
+
else:
|
| 66 |
+
result["thought"] = "Nothing to collect — no mushroom in clearing."
|
| 67 |
+
|
| 68 |
+
elif action == "study":
|
| 69 |
+
if current_mushroom:
|
| 70 |
+
reply, hist = engine.study_current(current_mushroom, self.history)
|
| 71 |
+
self.history = hist
|
| 72 |
+
result["data"] = {"study_reply": reply}
|
| 73 |
+
else:
|
| 74 |
+
result["thought"] = "Nothing to study."
|
| 75 |
+
|
| 76 |
return result
|
| 77 |
+
|