File size: 1,402 Bytes
afde4de
 
 
 
 
 
 
 
e6ad342
afde4de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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.")