Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- game/engine.py +41 -25
game/engine.py
CHANGED
|
@@ -840,35 +840,51 @@ def check_auto_pickup(new_pos_x, new_pos_y, current, collection, active_mushroom
|
|
| 840 |
|
| 841 |
# Myco narrative
|
| 842 |
def get_myco_narrative(current=None):
|
| 843 |
-
# 1.
|
| 844 |
prompt = (
|
| 845 |
-
"You are Myco Poet, a tiny
|
| 846 |
-
"
|
| 847 |
-
"
|
| 848 |
-
"
|
| 849 |
-
"
|
|
|
|
| 850 |
)
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
reply = _llm(prompt, context=context)
|
| 856 |
-
|
| 857 |
if not reply:
|
| 858 |
return "The forest feels deep today... ✨"
|
| 859 |
|
| 860 |
-
# 3.
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
|
| 867 |
-
# Remove extra quotes and whitespace
|
| 868 |
-
cleaned_reply = cleaned_reply.replace('"', '').strip()
|
| 869 |
-
|
| 870 |
-
# If the clean-up resulted in an empty string, fallback
|
| 871 |
-
if not cleaned_reply:
|
| 872 |
-
return "The forest feels deep today... ✨"
|
| 873 |
-
|
| 874 |
-
return cleaned_reply
|
|
|
|
| 840 |
|
| 841 |
# Myco narrative
|
| 842 |
def get_myco_narrative(current=None):
|
| 843 |
+
# 1. Strong, format-constrained prompt (prevents structured leakage)
|
| 844 |
prompt = (
|
| 845 |
+
"You are Myco Poet, a tiny emotional mushroom companion living in a forest.\n"
|
| 846 |
+
"Write EXACTLY 1–2 short poetic sentences.\n"
|
| 847 |
+
"Do NOT output JSON, brackets, lists, labels, or metadata.\n"
|
| 848 |
+
"Do NOT use {} or [].\n"
|
| 849 |
+
"Do NOT describe yourself as an AI or system.\n"
|
| 850 |
+
"Only output natural language forest poetry.\n"
|
| 851 |
)
|
| 852 |
+
|
| 853 |
+
# 2. Context (kept minimal but useful)
|
| 854 |
+
context = None
|
| 855 |
+
if current:
|
| 856 |
+
context = {
|
| 857 |
+
"name": current.get("name", "mystery"),
|
| 858 |
+
"rarity": current.get("rarity", "common")
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
reply = _llm(prompt, context=context)
|
| 862 |
+
|
| 863 |
if not reply:
|
| 864 |
return "The forest feels deep today... ✨"
|
| 865 |
|
| 866 |
+
# 3. Safer cleanup (non-greedy, avoids nuking valid text)
|
| 867 |
+
def strip_noise(text: str) -> str:
|
| 868 |
+
text = re.sub(r'\{[^{}]*\}', '', text) # remove simple JSON blocks only
|
| 869 |
+
text = re.sub(r'\[[^\[\]]*\]', '', text) # remove simple bracket blocks only
|
| 870 |
+
text = re.sub(r'"(thought|response|text)"\s*:\s*', '', text, flags=re.I)
|
| 871 |
+
text = text.replace('"', '')
|
| 872 |
+
return text.strip()
|
| 873 |
+
|
| 874 |
+
cleaned = strip_noise(reply)
|
| 875 |
+
|
| 876 |
+
# 4. Validation gate (prevents garbage passing through)
|
| 877 |
+
def is_valid(text: str) -> bool:
|
| 878 |
+
if not text:
|
| 879 |
+
return False
|
| 880 |
+
if text.strip().startswith(("{", "[")):
|
| 881 |
+
return False
|
| 882 |
+
if len(text) < 5:
|
| 883 |
+
return False
|
| 884 |
+
return True
|
| 885 |
+
|
| 886 |
+
if not is_valid(cleaned):
|
| 887 |
+
return "The forest hums softly with unseen life... ✨"
|
| 888 |
+
|
| 889 |
+
return cleaned
|
| 890 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|