File size: 1,620 Bytes
1154bfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86cbfce
1154bfc
86cbfce
 
 
 
 
1154bfc
 
 
86cbfce
1154bfc
 
 
 
 
 
 
 
 
 
 
 
 
86cbfce
 
cefe018
1154bfc
 
cefe018
 
 
1154bfc
 
cefe018
1154bfc
 
cefe018
1154bfc
 
cefe018
1154bfc
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from smolagents import Tool
import numpy as np
import textwrap


#AUTHORIZED_TYPES = [
#    "string",
#    "boolean",
#    "integer",
#    "number",
#    "image",
#    "audio",
#    "array",
#    "object",
#    "any",
#    "null",
#]

# https://chessvision.ai/docs/tools/fen2image/
# https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation


class FENTool(Tool):
    name = "_my_tool_fen"
    description = """
        Convert a list of chess pieces into FEN notation
        To invoke the tool use code as below
        <code>
        loaded_fen = _my_tool_fen(chest_pieces="dummy")
        </code>
    """

    inputs = {
        "chest_pieces": {
            "type": "string",
            "description": "string with chest pieces",
        }
    }

    output_type = "string"

    def __shortenFEN(self, fen):
        """Reduce FEN to shortest form (ex. '111p11Q' becomes '3p2Q')"""
        return fen.replace('11111111','8').replace('1111111','7') \
            .replace('111111','6').replace('11111','5') \
            .replace('1111','4').replace('111','3').replace('11','2')

    def forward(self, chest_pieces: str) -> str:
        arr = textwrap.wrap(chest_pieces, 8)
        #print(f"Arr: \n{arr}")

        arr = arr[::-1]
        #print(f"Arr: \n{arr}")

        arr = [row[::-1] for row in arr]

        fen_long = "/".join(arr)
        #print(f"FEN long: \n{fen_long}")

        fen_short = self.__shortenFEN(fen_long)
        #print(f"FEN short: \n{fen_short}")

        fen_enhanced = f"{fen_short} b - - 0 1"
        print(f"FEN: {fen_enhanced}")

        _out = fen_enhanced
        return _out