-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
example-provider.py
executable file
·307 lines (254 loc) · 9.9 KB
/
example-provider.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
"""External engine provider example for lichess.org"""
import argparse
import concurrent.futures
import contextlib
import logging
import multiprocessing
import os
import requests
import secrets
import subprocess
import sys
import time
import threading
_LOG_LEVEL_MAP = {
"critical": logging.CRITICAL,
"error": logging.CRITICAL,
"warning": logging.WARNING,
"info": logging.INFO,
"debug": logging.DEBUG,
"notset": logging.NOTSET,
}
def ok(res):
try:
res.raise_for_status()
except requests.exceptions.HTTPError:
logging.error("Response: %s", res.text)
raise
return res
def register_engine(args, http, engine):
res = ok(http.get(f"{args.lichess}/api/external-engine"))
secret = args.provider_secret or secrets.token_urlsafe(32)
variants = {
"chess",
"antichess",
"atomic",
"crazyhouse",
"horde",
"kingofthehill",
"racingkings",
"3check",
}
registration = {
"name": args.name,
"maxThreads": args.max_threads,
"maxHash": args.max_hash,
"variants": [variant for variant in engine.supported_variants or ["chess"] if variant in variants],
"providerSecret": secret,
}
for engine in res.json():
if engine["name"] == args.name:
logging.info("Updating engine %s", engine["id"])
ok(http.put(f"{args.lichess}/api/external-engine/{engine['id']}", json=registration))
break
else:
logging.info("Registering new engine")
ok(http.post(f"{args.lichess}/api/external-engine", json=registration))
return secret
def main(args):
executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
engine = Engine(args)
http = requests.Session()
http.headers["Authorization"] = f"Bearer {args.token}"
secret = register_engine(args, http, engine)
last_future = concurrent.futures.Future()
last_future.set_result(None)
backoff = 1
while True:
try:
res = ok(http.post(f"{args.broker}/api/external-engine/work", json={"providerSecret": secret}, timeout=12))
if res.status_code != 200:
if engine.alive and engine.idle_time() > args.keep_alive:
logging.info("Terminating idle engine")
engine.terminate()
continue
job = res.json()
except requests.exceptions.RequestException as err:
logging.error("Error while trying to acquire work: %s", err)
backoff = min(backoff * 1.5, 10)
time.sleep(backoff)
continue
else:
backoff = 1
try:
engine.stop()
except EOFError:
pass
last_future.result()
if not engine.alive:
engine = Engine(args)
job_started = threading.Event()
last_future = executor.submit(handle_job, args, engine, job, job_started)
job_started.wait()
def handle_job(args, engine, job, job_started):
try:
logging.info("Handling job %s", job["id"])
with engine.analyse(job, job_started) as analysis_stream:
ok(requests.post(f"{args.broker}/api/external-engine/work/{job['id']}", data=analysis_stream))
except requests.exceptions.ConnectionError:
logging.info("Connection closed while streaming analysis")
except requests.exceptions.RequestException as err:
logging.exception("Error while submitting work")
time.sleep(5)
except EOFError:
logging.exception("Engine died")
time.sleep(5)
finally:
job_started.set()
class Engine:
def __init__(self, args):
self.process = subprocess.Popen(args.engine, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)
self.args = args
self.session_id = None
self.hash = None
self.threads = None
self.multi_pv = None
self.uci_variant = None
self.supported_variants = []
self.last_used = time.monotonic()
self.alive = True
self.stop_lock = threading.Lock()
self.uci()
self.setoption("UCI_AnalyseMode", "true")
self.setoption("UCI_Chess960", "true")
for name, value in args.setoption:
self.setoption(name, value)
def idle_time(self):
return time.monotonic() - self.last_used
def terminate(self):
self.process.terminate()
self.alive = False
def send(self, command):
logging.debug("%d << %s", self.process.pid, command)
self.process.stdin.write(command + "\n")
self.process.stdin.flush()
def recv(self):
while True:
line = self.process.stdout.readline()
if line == "":
self.alive = False
raise EOFError()
line = line.rstrip()
if not line:
continue
logging.debug("%d >> %s", self.process.pid, line)
command_and_params = line.split(None, 1)
if len(command_and_params) == 1:
return command_and_params[0], ""
else:
return command_and_params
def uci(self):
self.send("uci")
while True:
command, args = self.recv()
if command == "option":
name = None
args = args.split()
while args:
arg = args.pop(0)
if arg == "name":
name = args.pop(0)
elif name == "UCI_Variant" and arg == "var":
self.supported_variants.append(args.pop(0))
elif command == "uciok":
break
if self.supported_variants:
logging.info("Supported variants: %s", ", ".join(self.supported_variants))
def isready(self):
self.send("isready")
while True:
line, _ = self.recv()
if line == "readyok":
break
def setoption(self, name, value):
self.send(f"setoption name {name} value {value}")
@contextlib.contextmanager
def analyse(self, job, job_started):
work = job["work"]
if work["sessionId"] != self.session_id:
self.session_id = work["sessionId"]
self.send("ucinewgame")
self.isready()
options_changed = False
if self.threads != work["threads"]:
self.setoption("Threads", work["threads"])
self.threads = work["threads"]
options_changed = True
if self.hash != work["hash"]:
self.setoption("Hash", work["hash"])
self.hash = work["hash"]
options_changed = True
if self.multi_pv != work["multiPv"]:
self.setoption("MultiPV", work["multiPv"])
self.multi_pv = work["multiPv"]
options_changed = True
if self.uci_variant != work["variant"]:
self.setoption("UCI_Variant", work["variant"])
self.uci_variant = work["variant"]
options_changed = True
if options_changed:
self.isready()
self.send(f"position fen {work['initialFen']} moves {' '.join(work['moves'])}")
for key in ["movetime", "depth", "nodes"]:
if key in work:
self.send(f"go {key} {work[key]}")
break
job_started.set()
def stream():
while True:
command, params = self.recv()
if command == "bestmove":
break
elif command == "info":
if "score" in params:
yield (command + " " + params + "\n").encode("utf-8")
else:
logging.warning("Unexpected engine command: %s", command)
analysis = stream()
try:
yield analysis
finally:
self.stop()
for _ in analysis:
pass
self.last_used = time.monotonic()
def stop(self):
if self.alive:
with self.stop_lock:
self.send("stop")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__, fromfile_prefix_chars='@')
parser.add_argument("--name", default="Alpha 2", help="Engine name to register")
parser.add_argument("--engine", help="Shell command to launch UCI engine", required=True)
parser.add_argument("--setoption", nargs=2, action="append", default=[], metavar=("NAME", "VALUE"), help="Set a custom UCI option")
parser.add_argument("--lichess", default="https://lichess.org", help="Defaults to https://lichess.org")
parser.add_argument("--broker", default="https://engine.lichess.ovh", help="Defaults to https://engine.lichess.ovh")
parser.add_argument("--token", default=os.environ.get("LICHESS_API_TOKEN"), help="API token with engine:read and engine:write scopes")
parser.add_argument("--provider-secret", default=os.environ.get("PROVIDER_SECRET"), help="Optional fixed provider secret")
parser.add_argument("--max-threads", type=int, default=multiprocessing.cpu_count(), help="Maximum number of available threads")
parser.add_argument("--max-hash", type=int, default=512, help="Maximum hash table size in MiB")
parser.add_argument("--keep-alive", type=int, default=300, help="Number of seconds to keep an idle/unused engine process around")
parser.add_argument("--log-level", default="info", choices=_LOG_LEVEL_MAP.keys(), help="Logging verbosity")
try:
import argcomplete
except ImportError:
pass
else:
argcomplete.autocomplete(parser)
args = parser.parse_args()
logging.basicConfig(level=_LOG_LEVEL_MAP[args.log_level])
if not args.token:
print(f"Need LICHESS_API_TOKEN environment variable from {args.lichess}/account/oauth/token/create?scopes[]=engine:read&scopes[]=engine:write")
sys.exit(128)
main(args)