byte-vortex commited on
Commit
eb3d18a
·
verified ·
1 Parent(s): 9566abc

Deploy Myco from CI

Browse files
Files changed (3) hide show
  1. game/engine.py +1 -4
  2. tests/test_myco_core.py +10 -0
  3. ui/gradio_app.py +75 -12
game/engine.py CHANGED
@@ -133,6 +133,7 @@ def study_current(current=None, history=None):
133
  current_history,
134
  "Let's find a mushroom before we study anything.",
135
  )
 
136
  def collect_current(
137
  current: MushroomState | None,
138
  collection: CollectionState | None,
@@ -222,25 +223,21 @@ def _assistant_message(content: str) -> dict:
222
  return {"role": "assistant", "content": content}
223
 
224
 
225
- def _append_assistant(history, content):
226
  def _append_assistant(history: ChatHistory, content: str) -> ChatHistory:
227
  """Return chat history with one assistant message appended."""
228
  return [*_safe_history(history), _assistant_message(content)]
229
 
230
 
231
- def _safe_collection(collection):
232
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
233
  """Return a copy of the collection, tolerating an uninitialized Gradio state."""
234
  return list(collection or [])
235
 
236
 
237
- def _safe_history(history):
238
  def _safe_history(history: ChatHistory | None) -> ChatHistory:
239
  """Return a copy of chat history, tolerating an uninitialized Gradio state."""
240
  return list(history or welcome_history())
241
 
242
 
243
- def _study_hint(rarity):
244
  def _study_hint(rarity: str) -> str:
245
  """Return a premium-feeling but cautious study result by rarity."""
246
  if rarity == "Legendary":
 
133
  current_history,
134
  "Let's find a mushroom before we study anything.",
135
  )
136
+
137
  def collect_current(
138
  current: MushroomState | None,
139
  collection: CollectionState | None,
 
223
  return {"role": "assistant", "content": content}
224
 
225
 
 
226
  def _append_assistant(history: ChatHistory, content: str) -> ChatHistory:
227
  """Return chat history with one assistant message appended."""
228
  return [*_safe_history(history), _assistant_message(content)]
229
 
230
 
 
231
  def _safe_collection(collection: CollectionState | None) -> CollectionState:
232
  """Return a copy of the collection, tolerating an uninitialized Gradio state."""
233
  return list(collection or [])
234
 
235
 
 
236
  def _safe_history(history: ChatHistory | None) -> ChatHistory:
237
  """Return a copy of chat history, tolerating an uninitialized Gradio state."""
238
  return list(history or welcome_history())
239
 
240
 
 
241
  def _study_hint(rarity: str) -> str:
242
  """Return a premium-feeling but cautious study result by rarity."""
243
  if rarity == "Legendary":
tests/test_myco_core.py CHANGED
@@ -96,6 +96,14 @@ class MycoCoreTests(unittest.TestCase):
96
  self.assertEqual(duplicates, {}, f"duplicate functions in {path}")
97
  self.assertNotIn(future_import, source, f"future import in {path}")
98
 
 
 
 
 
 
 
 
 
99
  def test_gradio_app_avoids_fragile_delimiters(self):
100
  source = Path("ui/gradio_app.py").read_text()
101
  self.assertNotIn("return (", source)
@@ -116,9 +124,11 @@ class MycoCoreTests(unittest.TestCase):
116
  )
117
  for fragment in forbidden_fragments:
118
  self.assertNotIn(fragment, source)
 
119
  def test_gradio_app_avoids_fragile_multiline_return_tuples(self):
120
  source = Path("ui/gradio_app.py").read_text()
121
  self.assertNotIn("return (", source)
 
122
  def test_gradio_app_file_has_no_duplicate_functions_or_future_import(self):
123
  source = Path("ui/gradio_app.py").read_text()
124
  tree = ast.parse(source)
 
96
  self.assertEqual(duplicates, {}, f"duplicate functions in {path}")
97
  self.assertNotIn(future_import, source, f"future import in {path}")
98
 
99
+ def test_gradio_app_has_single_clean_composition(self):
100
+ source = Path("ui/gradio_app.py").read_text()
101
+ self.assertEqual(source.count('"""Gradio app composition for Myco."""'), 1)
102
+ self.assertEqual(source.count("import gradio as gr"), 1)
103
+ self.assertEqual(source.count("def build_app"), 1)
104
+ self.assertNotIn("outputs=[", source)
105
+ self.assertNotIn("textbox_component", source)
106
+
107
  def test_gradio_app_avoids_fragile_delimiters(self):
108
  source = Path("ui/gradio_app.py").read_text()
109
  self.assertNotIn("return (", source)
 
124
  )
125
  for fragment in forbidden_fragments:
126
  self.assertNotIn(fragment, source)
127
+
128
  def test_gradio_app_avoids_fragile_multiline_return_tuples(self):
129
  source = Path("ui/gradio_app.py").read_text()
130
  self.assertNotIn("return (", source)
131
+
132
  def test_gradio_app_file_has_no_duplicate_functions_or_future_import(self):
133
  source = Path("ui/gradio_app.py").read_text()
134
  tree = ast.parse(source)
ui/gradio_app.py CHANGED
@@ -1,11 +1,24 @@
1
- # Gradio app composition for Myco.
2
 
3
  import gradio as gr
4
 
5
- from game.engine import collect_current, discover_mushroom, eat_current, myco_reply, study_current
 
 
 
 
 
 
6
  from game.state import welcome_history
7
  from models.mushroom import Mushroom
8
- from ui.renderers import dex_markdown, forest_scene, game_status_markdown, hero_markdown, mushroom_card, progress_markdown
 
 
 
 
 
 
 
9
 
10
  EMPTY_COLLECTION = tuple()
11
 
@@ -28,15 +41,6 @@ GUIDE_MARKDOWN = """
28
  4. Choose **Study** for a safer hint, **Collect** for the MycoDex, or **Eat** to see Myco stop you.
29
  5. Check **📖 MycoDex** to track discoveries and area unlocks.
30
  """
31
- from ui.renderers import (
32
- dex_markdown,
33
- forest_scene,
34
- game_status_markdown,
35
- hero_markdown,
36
- mushroom_card,
37
- progress_markdown,
38
- )
39
-
40
 
41
  def mushroom_card_from_state(current):
42
  """Render a mushroom card from Gradio state."""
@@ -48,6 +52,9 @@ def mushroom_card_from_state(current):
48
  def search_forest(collection):
49
  """Explore the forest and refresh every visible gameplay panel."""
50
  mushroom, current, history = discover_mushroom(collection)
 
 
 
51
  scene = forest_scene(current, collection)
52
  status = game_status_markdown(current, collection)
53
  card = mushroom_card(mushroom)
@@ -242,6 +249,17 @@ def build_app() -> gr.Blocks:
242
 
243
  with gr.Tab("🎮 Play"):
244
  scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION))
 
 
 
 
 
 
 
 
 
 
 
245
  status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION), elem_id="quest-log")
246
  with gr.Row():
247
  with gr.Column(scale=2):
@@ -276,6 +294,13 @@ def build_app() -> gr.Blocks:
276
  collect_button = gr.Button("Collect")
277
  study_button = gr.Button("Study")
278
  with gr.Column(scale=3):
 
 
 
 
 
 
 
279
  chat = gr.Chatbot(value=welcome_history(), label="Myco Chat", height=360)
280
  with gr.Row():
281
  prompt = gr.Textbox(label="Ask Myco", placeholder="Ask Myco about this mushroom", scale=4)
@@ -290,12 +315,50 @@ def build_app() -> gr.Blocks:
290
 
291
  with gr.Tab("📖 MycoDex"):
292
  with gr.Row():
 
 
 
 
 
 
 
 
293
  progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION), elem_id="progress-panel")
294
  dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION), elem_id="mycodex")
295
 
296
  with gr.Tab("🧭 Guide"):
297
  gr.Markdown(GUIDE_MARKDOWN)
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  search_button.click(search_forest, inputs=(collection,), outputs=(scene, status, discovery, current_mushroom, chat, dex, progress))
300
  ask_button.click(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))
301
  prompt.submit(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))
 
1
+ """Gradio app composition for Myco."""
2
 
3
  import gradio as gr
4
 
5
+ from game.engine import (
6
+ collect_current,
7
+ discover_mushroom,
8
+ eat_current,
9
+ myco_reply,
10
+ study_current,
11
+ )
12
  from game.state import welcome_history
13
  from models.mushroom import Mushroom
14
+ from ui.renderers import (
15
+ dex_markdown,
16
+ forest_scene,
17
+ game_status_markdown,
18
+ hero_markdown,
19
+ mushroom_card,
20
+ progress_markdown,
21
+ )
22
 
23
  EMPTY_COLLECTION = tuple()
24
 
 
41
  4. Choose **Study** for a safer hint, **Collect** for the MycoDex, or **Eat** to see Myco stop you.
42
  5. Check **📖 MycoDex** to track discoveries and area unlocks.
43
  """
 
 
 
 
 
 
 
 
 
44
 
45
  def mushroom_card_from_state(current):
46
  """Render a mushroom card from Gradio state."""
 
52
  def search_forest(collection):
53
  """Explore the forest and refresh every visible gameplay panel."""
54
  mushroom, current, history = discover_mushroom(collection)
55
+ return (
56
+ forest_scene(current, collection),
57
+ game_status_markdown(current, collection),
58
  scene = forest_scene(current, collection)
59
  status = game_status_markdown(current, collection)
60
  card = mushroom_card(mushroom)
 
249
 
250
  with gr.Tab("🎮 Play"):
251
  scene = gr.HTML(forest_scene(None, EMPTY_COLLECTION))
252
+ status = gr.Markdown(
253
+ game_status_markdown(None, EMPTY_COLLECTION),
254
+ elem_id="quest-log",
255
+ )
256
+ with gr.Row():
257
+ with gr.Column(scale=2):
258
+ discovery = gr.Markdown(
259
+ mushroom_card(None),
260
+ elem_id="discovery-card",
261
+ )
262
+ search_button = gr.Button("Start Exploring", variant="primary")
263
  status = gr.Markdown(game_status_markdown(None, EMPTY_COLLECTION), elem_id="quest-log")
264
  with gr.Row():
265
  with gr.Column(scale=2):
 
294
  collect_button = gr.Button("Collect")
295
  study_button = gr.Button("Study")
296
  with gr.Column(scale=3):
297
+ chat = gr.Chatbot(
298
+ value=welcome_history(),
299
+ label="Myco Chat",
300
+ height=360,
301
+ )
302
+ with gr.Row():
303
+ prompt = gr.Textbox(
304
  chat = gr.Chatbot(value=welcome_history(), label="Myco Chat", height=360)
305
  with gr.Row():
306
  prompt = gr.Textbox(label="Ask Myco", placeholder="Ask Myco about this mushroom", scale=4)
 
315
 
316
  with gr.Tab("📖 MycoDex"):
317
  with gr.Row():
318
+ progress = gr.Markdown(
319
+ progress_markdown(EMPTY_COLLECTION),
320
+ elem_id="progress-panel",
321
+ )
322
+ dex = gr.Markdown(
323
+ dex_markdown(EMPTY_COLLECTION),
324
+ elem_id="mycodex",
325
+ )
326
  progress = gr.Markdown(progress_markdown(EMPTY_COLLECTION), elem_id="progress-panel")
327
  dex = gr.Markdown(dex_markdown(EMPTY_COLLECTION), elem_id="mycodex")
328
 
329
  with gr.Tab("🧭 Guide"):
330
  gr.Markdown(GUIDE_MARKDOWN)
331
 
332
+ search_button.click(
333
+ search_forest,
334
+ inputs=(collection,),
335
+ outputs=(scene, status, discovery, current_mushroom, chat, dex, progress),
336
+ )
337
+ ask_button.click(
338
+ ask_companion,
339
+ inputs=(prompt, chat, current_mushroom),
340
+ outputs=(prompt, chat),
341
+ )
342
+ prompt.submit(
343
+ ask_companion,
344
+ inputs=(prompt, chat, current_mushroom),
345
+ outputs=(prompt, chat),
346
+ )
347
+ collect_button.click(
348
+ collect_discovery,
349
+ inputs=(current_mushroom, collection, chat),
350
+ outputs=(collection, scene, status, dex, chat, progress),
351
+ )
352
+ study_button.click(
353
+ study_discovery,
354
+ inputs=(current_mushroom, chat, collection),
355
+ outputs=(current_mushroom, scene, status, discovery, chat),
356
+ )
357
+ eat_button.click(
358
+ block_unsafe_eating,
359
+ inputs=(current_mushroom, chat),
360
+ outputs=(chat,),
361
+ )
362
  search_button.click(search_forest, inputs=(collection,), outputs=(scene, status, discovery, current_mushroom, chat, dex, progress))
363
  ask_button.click(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))
364
  prompt.submit(ask_companion, inputs=(prompt, chat, current_mushroom), outputs=(prompt, chat))