| import os | |
| from langchain.tools import BaseTool | |
| import chess | |
| import chess.svg | |
| class ChessTool(BaseTool): | |
| name: str = "chess_tool" | |
| description: str = ( | |
| "Analyze chess positions from an image or FEN and return the best move in algebraic notation. Use for any chess-position-analysis question, especially when it’s ‘Black to move’ or ‘White to move’ tactical puzzles." | |
| ) | |
| def _run(self, board_or_path: str) -> str: | |
| # если путь к файлу есть на диске — placeholder | |
| if os.path.exists(board_or_path) and board_or_path.lower().endswith((".png",".jpg")): | |
| return "ERROR: Image‐to‐move logic not implemented." | |
| # иначе считаем, что это FEN | |
| try: | |
| board = chess.Board(board_or_path) | |
| # простой мини-алгоритм: выбираем первый ход, ведущий к выигрышной оценке | |
| for move in board.legal_moves: | |
| board.push(move) | |
| if board.is_checkmate(): | |
| return board.san(move) | |
| board.pop() | |
| return board.san(next(board.legal_moves)) | |
| except Exception as e: | |
| return f"ERROR: Invalid FEN or cannot solve: {e}" | |
| async def _arun(self, board_or_path: str) -> str: | |
| raise NotImplementedError("Async not supported.") | |