@@ -86,34 +86,35 @@ def _resolve_api_key(provider: str) -> str:
8686 )
8787
8888
89+ def _extract_legal_actions (messages : list [dict [str , Any ]]) -> list [int ]:
90+ for message in reversed (messages ):
91+ content = message .get ("content" ) or ""
92+ if not isinstance (content , str ):
93+ continue
94+ try :
95+ payload = json .loads (content )
96+ except (json .JSONDecodeError , ValueError ):
97+ payload = None
98+ if isinstance (payload , dict ) and "legal_actions" in payload :
99+ legal = payload ["legal_actions" ]
100+ if isinstance (legal , list ):
101+ return [int (a ) for a in legal ]
102+ match = re .search (r"Legal actions:\s*\[([^\]]*)\]" , content )
103+ if match :
104+ raw = match .group (1 ).strip ()
105+ if not raw :
106+ return []
107+ return [int (x ) for x in raw .split ("," )]
108+ return []
109+
110+
89111def _build_scripted_model_step ():
90112 """Default teacher: pick the first legal action from the latest observation.
91113
92114 Matches the scripted teacher in ``examples/ttt_collect_demo.py``. Lets
93115 users smoke-test the CLI without any API key configured.
94116 """
95117
96- def _extract_legal_actions (messages : list [dict [str , Any ]]) -> list [int ]:
97- for message in reversed (messages ):
98- content = message .get ("content" ) or ""
99- if not isinstance (content , str ):
100- continue
101- try :
102- payload = json .loads (content )
103- except (json .JSONDecodeError , ValueError ):
104- payload = None
105- if isinstance (payload , dict ) and "legal_actions" in payload :
106- legal = payload ["legal_actions" ]
107- if isinstance (legal , list ):
108- return [int (a ) for a in legal ]
109- match = re .search (r"Legal actions:\s*\[([^\]]*)\]" , content )
110- if match :
111- raw = match .group (1 ).strip ()
112- if not raw :
113- return []
114- return [int (x ) for x in raw .split ("," )]
115- return []
116-
117118 def model_step (messages , tools , sampling ):
118119 del sampling
119120 legal = _extract_legal_actions (messages )
0 commit comments