#!/usr/bin/env python3 # SPDX-License-Identifier: AGPL-3.0-or-later # # tagserve: the lichess puzzle tagger as a program of its own. # Copyright (C) 2026 StudyChess # # This program is free software: you can redistribute it and/or modify it under the terms of the # GNU Affero General Public License as published by the Free Software Foundation, either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Affero General Public License for more details: . # # It loads the tagger of lichess-puzzler (cook.py and model.py, AGPL-3.0, # https://github.com/ornicar/lichess-puzzler, directory tagger/), unmodified, from the directory # given as its only argument, and answers questions on its standard input, one JSON object a line: # # {"id": 7, "fen": "", "moves": ["e2e4", "e7e5", ...], "cp": 310} # # `moves` is the move that led to the puzzle position followed by the line to name; `cp` is the # engine's evaluation of the line for the side that plays it (a very large number for a mate). # Each question gets one line on standard output, in order: # # {"id": 7, "tags": ["fork", "middlegame"]} the tagger's answer, sorted # {"id": 7, "error": "ValueError"} the tagger raised; the name of the exception # # The first line written, before any question is read, is {"ready": true, "tagger": ""}, # or {"ready": false, "error": "..."} when the tagger cannot be loaded (and the program ends, 1). # The program ends when its input does. It reads and writes nothing else and keeps nothing. # # This file and the tagger form one program under the AGPL. Whatever asks the questions is a # separate program: it sees JSON and never the tagger's code. The published copy of this file is # at /licences/tagserve.py.txt of the site that runs it. import json import logging import sys def main(argv) -> int: if len(argv) != 2: print("usage: tagserve.py ", file=sys.stderr) return 2 out = sys.stdout try: sys.path.insert(0, argv[1]) import chess import chess.pgn import cook from model import Puzzle except Exception as exc: out.write(json.dumps({"ready": False, "error": f"{type(exc).__name__}: {exc}"}) + "\n") out.flush() return 1 logging.disable(logging.CRITICAL) # the tagger logs a line per puzzle out.write(json.dumps({"ready": True, "tagger": argv[1]}) + "\n") out.flush() for line in sys.stdin: line = line.strip() if not line: continue ask = None try: ask = json.loads(line) node = chess.pgn.Game.from_board(chess.Board(ask["fen"])) for uci in ask["moves"]: node = node.add_main_variation(chess.Move.from_uci(uci)) answer = {"id": ask.get("id"), "tags": sorted(set(cook.cook(Puzzle("x", node.game(), int(ask["cp"])))))} except Exception as exc: answer = {"id": ask.get("id") if isinstance(ask, dict) else None, "error": type(exc).__name__} out.write(json.dumps(answer) + "\n") out.flush() return 0 if __name__ == "__main__": sys.exit(main(sys.argv))