Skip to content

Commit 13a0aca

Browse files
committed
Migrate dataclass to pydantic BaseModel
1 parent e24cf3f commit 13a0aca

4 files changed

Lines changed: 11 additions & 15 deletions

File tree

codeclash/agents/utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import os
2-
from dataclasses import asdict, dataclass
32
from pathlib import Path
43

54
from dotenv import load_dotenv
65
from jinja2 import Template
6+
from pydantic import BaseModel
77

88
load_dotenv()
99

@@ -16,8 +16,7 @@ def resolve_api_key(model: str) -> str:
1616
return ""
1717

1818

19-
@dataclass
20-
class GameContext:
19+
class GameContext(BaseModel):
2120
"""
2221
A class that gives agent access to a partial view of the game state.
2322
@@ -38,11 +37,11 @@ class GameContext:
3837
working_dir: str
3938

4039
def _render_prompt_templates(self) -> dict:
41-
context = asdict(self)
40+
context = self.dict()
4241
return {key: Template(template_str).render(**context) for key, template_str in self.prompts.items()}
4342

4443
def to_template_vars(self) -> dict[str, str]:
4544
"""Convert the GameContext to a dictionary for rendering prompts in the agent"""
46-
out = asdict(self) | self._render_prompt_templates()
45+
out = self.dict() | self._render_prompt_templates()
4746
out.pop("prompts")
4847
return out

codeclash/games/abstract.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,33 @@
22
import subprocess
33
import time
44
from abc import ABC, abstractmethod
5-
from dataclasses import dataclass
65
from pathlib import Path
76
from typing import Any
87

98
from minisweagent.environments.docker import DockerEnvironment
9+
from pydantic import BaseModel
1010

1111
from codeclash.agents.abstract import Player
1212
from codeclash.constants import DIR_LOGS, DIR_WORK, GH_ORG
1313
from codeclash.utils.environment import assert_zero_exit_code, copy_between_containers
1414
from codeclash.utils.log import get_logger
1515

1616

17-
@dataclass
18-
class RoundStats:
17+
class RoundStats(BaseModel):
1918
winner: str
2019
scores: dict[str, float] # Map of player to game metric (e.g. # of wins, assets accumulated)
21-
details: dict[str, Any] = None # Optional, for game-specific info
20+
details: dict[str, Any] | None = None # Optional, for game-specific info
2221

2322
def __str__(self) -> str:
2423
return "\n".join([f"- Winner: {self.winner}", f"- Scores: {self.scores}"])
2524

2625

27-
@dataclass
28-
class RoundData:
26+
class RoundData(BaseModel):
2927
logs: list[str]
3028
results: list[str]
3129

3230

33-
@dataclass
34-
class RoundRecord:
31+
class RoundRecord(BaseModel):
3532
data: RoundData
3633
stats: RoundStats
3734

codeclash/games/battlesnake/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def execute_round(self, agents: list[Player]) -> RoundData:
7575
self.environment.execute(f"rm -f game/{output_file}")
7676
time.sleep(0.05)
7777

78-
return RoundData(log_outputs, result_outputs)
78+
return RoundData(logs=log_outputs, results=result_outputs)
7979
finally:
8080
# Kill all python servers when done
8181
self.environment.execute("pkill -f 'python main.py' || true")

codeclash/games/robocode/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ def execute_round(self, agents: list[Player]) -> RoundData:
108108
# Clean up the results file
109109
self.environment.execute(f"rm -f {results_file}")
110110

111-
return RoundData([response["output"]], [result_output])
111+
return RoundData(logs=[response["output"]], results=[result_output])

0 commit comments

Comments
 (0)