olcapone commited on
Commit
448833b
·
verified ·
1 Parent(s): 156c04d
Files changed (1) hide show
  1. app.py +21 -5
app.py CHANGED
@@ -238,10 +238,11 @@ class BasicAgent:
238
  m = FEN_RX.search(text)
239
  if m:
240
  return " ".join(m.group(1).split()) # normalize whitespace
 
241
  # fallback: sometimes file is just the fen line
242
- t = text.strip()
243
  if "/" in t and " w " in t or " b " in t:
244
- return " ".join(t.split())
245
  return None
246
 
247
  def _solve_chess_mate_in_one(self, fen: str) -> str | None:
@@ -263,6 +264,21 @@ class BasicAgent:
263
  return san # e.g., "Qg2#"
264
  return None
265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  # change the template call to pass task_id as second arg
267
  def __call__(self, question: str, task_id: str | None = None) -> str:
268
  ql = question.lower()
@@ -274,11 +290,11 @@ class BasicAgent:
274
 
275
 
276
  # CHESS fast-path
277
- if ("chess" in ql or "algebraic notation" in ql or "diagram" in ql or "board" in ql) and task_id:
278
  file_text = self._fetch_file_text(task_id)
279
- fen = self._extract_fen(file_text or "")
280
  if fen:
281
- san = self._solve_chess_mate_in_one(fen)
282
  if san:
283
  return san
284
 
 
238
  m = FEN_RX.search(text)
239
  if m:
240
  return " ".join(m.group(1).split()) # normalize whitespace
241
+
242
  # fallback: sometimes file is just the fen line
243
+ t = " ".join(text.strip().split())
244
  if "/" in t and " w " in t or " b " in t:
245
+ return t
246
  return None
247
 
248
  def _solve_chess_mate_in_one(self, fen: str) -> str | None:
 
264
  return san # e.g., "Qg2#"
265
  return None
266
 
267
+ def _mate_in_one_san(self, fen: str) -> str | None:
268
+ try:
269
+ board = chess.Board(fen)
270
+ except Exception:
271
+ return None
272
+ # enumerate legal moves; if any leads to mate, return SAN
273
+ for mv in list(board.legal_moves):
274
+ board.push(mv)
275
+ is_mate = board.is_checkmate()
276
+ san = board.san(mv)
277
+ board.pop()
278
+ if is_mate:
279
+ return san # e.g., "Qg2#"
280
+ return None
281
+
282
  # change the template call to pass task_id as second arg
283
  def __call__(self, question: str, task_id: str | None = None) -> str:
284
  ql = question.lower()
 
290
 
291
 
292
  # CHESS fast-path
293
+ if ("chess" in ql or "algebraic notation" in ql or "board" in ql) and task_id:
294
  file_text = self._fetch_file_text(task_id)
295
+ fen = self._extract_fen(file_text)
296
  if fen:
297
+ san = self._mate_in_one_san(fen)
298
  if san:
299
  return san
300