byte-vortex commited on
Commit
b255e59
·
verified ·
1 Parent(s): c30b63f

Deploy Myco from CI

Browse files
Files changed (1) hide show
  1. 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
- prompt = f"""
11
- Current Position: {self.position}
12
- Mushroom in clearing: {'Yes' if current_mushroom else 'No'}
13
- Collection count: {len(collection)}
14
-
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
-
 
 
 
 
 
 
 
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
+