bytefight-core 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bytefight_core-0.1.0/LICENSE +21 -0
- bytefight_core-0.1.0/PKG-INFO +15 -0
- bytefight_core-0.1.0/README.md +3 -0
- bytefight_core-0.1.0/pyproject.toml +18 -0
- bytefight_core-0.1.0/src/bytefight_core/__init__.py +28 -0
- bytefight_core-0.1.0/src/bytefight_core/interface.py +57 -0
- bytefight_core-0.1.0/src/bytefight_core/local.py +21 -0
- bytefight_core-0.1.0/src/bytefight_core/protocol.py +91 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jaeheon Shim
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bytefight-core
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The SDK for the ByteFight competition platform
|
|
5
|
+
Author: Jaeheon Shim
|
|
6
|
+
Author-email: Jaeheon Shim <jaeheon.shim@gatech.edu>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: pydantic>=2.0
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# bytefight-core
|
|
14
|
+
|
|
15
|
+
The SDK for the ByteFight competition platform
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "bytefight-core"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "The SDK for the ByteFight competition platform"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Jaeheon Shim", email = "jaeheon.shim@gatech.edu" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"pydantic>=2.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["uv_build>=0.11.21,<0.12.0"]
|
|
18
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
from importlib.metadata import version
|
|
2
|
+
|
|
3
|
+
from bytefight_core.interface import (Action, GameController, GameResult,
|
|
4
|
+
GameResultFile, MatchStatus, Obs,
|
|
5
|
+
PlayerController, PlayerHandle)
|
|
6
|
+
from bytefight_core.local import LocalPlayerHandle
|
|
7
|
+
from bytefight_core.protocol import (StdioPlayerHandle,
|
|
8
|
+
isolate_protocol_stream, model_args,
|
|
9
|
+
run_agent, serve_stdio)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"Action",
|
|
13
|
+
"Obs",
|
|
14
|
+
"MatchStatus",
|
|
15
|
+
"GameResult",
|
|
16
|
+
"GameResultFile",
|
|
17
|
+
"PlayerController",
|
|
18
|
+
"PlayerHandle",
|
|
19
|
+
"GameController",
|
|
20
|
+
"LocalPlayerHandle",
|
|
21
|
+
"StdioPlayerHandle",
|
|
22
|
+
"serve_stdio",
|
|
23
|
+
"run_agent",
|
|
24
|
+
"isolate_protocol_stream",
|
|
25
|
+
"model_args",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
__version__ = version("bytefight-core")
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from typing import Generic, TypeVar
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
|
|
9
|
+
Action = TypeVar("Action", bound=BaseModel)
|
|
10
|
+
Obs = TypeVar("Obs", bound=BaseModel)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MatchStatus(Enum):
|
|
14
|
+
CREATED = "created"
|
|
15
|
+
SCHEDULING = "scheduling"
|
|
16
|
+
WAITING = "waiting"
|
|
17
|
+
IN_PROGRESS = "in_progress"
|
|
18
|
+
FAILED = "failed"
|
|
19
|
+
PLAYER_A_WIN = "team_a_win"
|
|
20
|
+
PLAYER_B_WIN = "team_b_win"
|
|
21
|
+
DRAW = "draw"
|
|
22
|
+
SUBMISSION_VALID = "submission_valid"
|
|
23
|
+
SUBMISSION_INVALID = "submission_invalid"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class GameResultFile:
|
|
28
|
+
path: str | Path
|
|
29
|
+
slug: str
|
|
30
|
+
visibility: str
|
|
31
|
+
team_uuid: str | None = None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class GameResult:
|
|
36
|
+
status: MatchStatus
|
|
37
|
+
files: list[GameResultFile] = field(default_factory=list)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class GameController(ABC, Generic[Action, Obs]):
|
|
41
|
+
@abstractmethod
|
|
42
|
+
def play(self) -> GameResult: ...
|
|
43
|
+
|
|
44
|
+
@abstractmethod
|
|
45
|
+
def validate(self) -> GameResult: ...
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class PlayerHandle(ABC, Generic[Action, Obs]):
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def exchange(self, obs: Obs, *, timeout: float | None = None) -> Action | None: ...
|
|
51
|
+
|
|
52
|
+
def close(self) -> None: ...
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class PlayerController(ABC, Generic[Action, Obs]):
|
|
56
|
+
@abstractmethod
|
|
57
|
+
def play(self, obs: Obs) -> Action: ...
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from bytefight_core.interface import (Action, Obs, PlayerController,
|
|
2
|
+
PlayerHandle)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class LocalPlayerHandle(PlayerHandle[Action, Obs]):
|
|
6
|
+
def __init__(
|
|
7
|
+
self,
|
|
8
|
+
controller: PlayerController[Action, Obs],
|
|
9
|
+
*,
|
|
10
|
+
raise_errors: bool = False,
|
|
11
|
+
):
|
|
12
|
+
self._controller = controller
|
|
13
|
+
self._raise_errors = raise_errors
|
|
14
|
+
|
|
15
|
+
def exchange(self, obs: Obs, *, timeout: float | None = None) -> Action | None:
|
|
16
|
+
try:
|
|
17
|
+
return self._controller.play(obs)
|
|
18
|
+
except Exception:
|
|
19
|
+
if self._raise_errors:
|
|
20
|
+
raise
|
|
21
|
+
return None
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import select
|
|
3
|
+
import sys
|
|
4
|
+
from typing import IO, Any, get_args, get_origin
|
|
5
|
+
|
|
6
|
+
from bytefight_core.interface import (Action, Obs, PlayerController,
|
|
7
|
+
PlayerHandle)
|
|
8
|
+
from pydantic import BaseModel
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def model_args(
|
|
12
|
+
controller: PlayerController[Action, Obs] | type[PlayerController[Action, Obs]],
|
|
13
|
+
) -> tuple[type[Any], type[Any]]:
|
|
14
|
+
cls = controller if isinstance(controller, type) else type(controller)
|
|
15
|
+
for klass in cls.__mro__:
|
|
16
|
+
for base in getattr(klass, "__orig_bases__", ()):
|
|
17
|
+
if get_origin(base) is PlayerController:
|
|
18
|
+
args = get_args(base)
|
|
19
|
+
if len(args) == 2 and all(
|
|
20
|
+
isinstance(a, type) and issubclass(a, BaseModel) for a in args
|
|
21
|
+
):
|
|
22
|
+
return args # type: ignore[return-value]
|
|
23
|
+
raise TypeError(
|
|
24
|
+
f"{cls.__name__} must subclass "
|
|
25
|
+
"PlayerController[YourAction, YourObs] with concrete pydantic models so the "
|
|
26
|
+
"protocol knows how to parse observations"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class StdioPlayerHandle(PlayerHandle[Action, Obs]):
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
stdin: IO[str],
|
|
34
|
+
stdout: IO[str],
|
|
35
|
+
action_type: type[Action],
|
|
36
|
+
):
|
|
37
|
+
self._stdin = stdin
|
|
38
|
+
self._stdout = stdout
|
|
39
|
+
self._action_type = action_type
|
|
40
|
+
|
|
41
|
+
def exchange(self, obs: Obs, *, timeout: float | None = None) -> Action | None:
|
|
42
|
+
try:
|
|
43
|
+
self._stdin.write(obs.model_dump_json() + "\n")
|
|
44
|
+
self._stdin.flush()
|
|
45
|
+
if timeout is not None:
|
|
46
|
+
ready, _, _ = select.select([self._stdout], [], [], timeout)
|
|
47
|
+
if not ready:
|
|
48
|
+
return None
|
|
49
|
+
line = self._stdout.readline()
|
|
50
|
+
if not line:
|
|
51
|
+
return None
|
|
52
|
+
return self._action_type.model_validate_json(line)
|
|
53
|
+
except (OSError, ValueError):
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
def close(self) -> None:
|
|
57
|
+
self._stdin.close()
|
|
58
|
+
self._stdout.close()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def serve_stdio(
|
|
62
|
+
controller: PlayerController[Action, Obs],
|
|
63
|
+
stdin: IO[str],
|
|
64
|
+
stdout: IO[str],
|
|
65
|
+
obs_type: type[Obs] | None = None,
|
|
66
|
+
) -> None:
|
|
67
|
+
model = obs_type if obs_type is not None else model_args(controller)[1]
|
|
68
|
+
for line in stdin:
|
|
69
|
+
obs = model.model_validate_json(line)
|
|
70
|
+
action = controller.play(obs)
|
|
71
|
+
stdout.write(action.model_dump_json() + "\n")
|
|
72
|
+
stdout.flush()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def isolate_protocol_stream() -> IO[str]:
|
|
76
|
+
sys.stdout.flush()
|
|
77
|
+
protocol_out = os.fdopen(os.dup(1), "w", buffering=1)
|
|
78
|
+
os.dup2(2, 1)
|
|
79
|
+
sys.stdout = sys.stderr
|
|
80
|
+
return protocol_out
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def run_agent(
|
|
84
|
+
controller: PlayerController[Action, Obs],
|
|
85
|
+
*,
|
|
86
|
+
stdin: IO[str] | None = None,
|
|
87
|
+
stdout: IO[str] | None = None,
|
|
88
|
+
obs_type: type[Obs] | None = None,
|
|
89
|
+
) -> None:
|
|
90
|
+
protocol_out = stdout if stdout is not None else isolate_protocol_stream()
|
|
91
|
+
serve_stdio(controller, stdin or sys.stdin, protocol_out, obs_type)
|