SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]
Cleanup run_traj_to_demo
Kilian Lieret8d3d4e5b2da9
diff --git a/sweagent/run/run_traj_to_demo.py b/sweagent/run/run_traj_to_demo.pyindex 021973d1..0d45aa51 100644--- a/sweagent/run/run_traj_to_demo.py+++ b/sweagent/run/run_traj_to_demo.py@@ -1,3 +1,8 @@+"""Convert a trajectory file to a yaml file for editing of demos.+You can then load the yaml file with `run_replay.py` to replay the actions in an environment to get+output data.+"""+from __future__ import annotationsimport io@@ -8,6 +13,10 @@ from pathlib import Pathfrom ruamel.yaml import YAMLfrom ruamel.yaml.scalarstring import LiteralScalarString as LSS+from sweagent.utils.log import get_logger++logger = get_logger("traj2demo")+DEMO_COMMENT = """# This is a demo file generated from trajectory file:# {traj_path}# You can use this demo file to replay the actions in the trajectory with run_replay.py.@@ -18,22 +27,20 @@ DEMO_COMMENT = """# This is a demo file generated from trajectory file:# You can add or remove actions to modify the replay behavior."""-def convert_to_literal_string(d):- """- Convert any multi-line strings to LiteralScalarString- """+def _convert_to_literal_string(d: str | dict | list) -> str | dict | list:+ """Convert any multi-line strings in nested data object to LiteralScalarString"""if isinstance(d, dict):for key, value in d.items():if isinstance(value, str) and "\n" in value:d[key] = LSS(value.replace("\r\n", "\n").replace("\r", "\n"))elif isinstance(value, dict):- convert_to_literal_string(value)+ _convert_to_literal_string(value)elif isinstance(d, list):for i, item in enumerate(d):if isinstance(item, str) and "\n" in item:d[i] = LSS(item.replace("\r\n", "\n").replace("\r", "\n"))elif isinstance(item, dict):- convert_to_literal_string(item)+ _convert_to_literal_string(item)elif isinstance(d, str) and "\n" in d:d = LSS(d.replace("\r\n", "\n").replace("\r", "\n"))else:@@ -42,11 +49,9 @@ def convert_to_literal_string(d):return d-def save_demo(data, file, traj_path):- """- Save a single task instance as a yaml file- """- data = convert_to_literal_string(data)+def save_demo(data: str | dict | list, file: Path, traj_path: Path) -> None:+ """Save demo data as a yaml file. Takes care of multi-line strings and adds a header."""+ data = _convert_to_literal_string(data)yaml = YAML()yaml.indent(mapping=2, sequence=4, offset=2)yaml.width = float("inf")@@ -54,12 +59,12 @@ def save_demo(data, file, traj_path):buffer = io.StringIO()yaml.dump(data, buffer)content = buffer.getvalue()- header = DEMO_COMMENT.format(traj_path=traj_path)+ header = DEMO_COMMENT.format(traj_path=str(traj_path))with open(file, "w") as f:f.write(f"{header}\n{content}")-def convert_traj_to_action_demo(traj_path: str, output_file: str | Path, include_user: bool = False):+def convert_traj_to_action_demo(traj_path: Path, output_file: Path, include_user: bool = False) -> None:with open(traj_path) as file:traj = json.load(file)replay_config = traj["replay_config"]@@ -73,43 +78,29 @@ def convert_traj_to_action_demo(traj_path: str, output_file: str | Path, includeoutput_data = {"history": filtered_history, "replay_config": replay_config}save_demo(output_data, output_file, traj_path)- print(f"Saved demo to {output_file}")+ logger.info(f"Saved demo to {output_file}")-def main(traj_path: str, output_dir: str, suffix: str = "", overwrite: bool = False, include_user: bool = False):- filename = (- "/".join([Path(traj_path).parent.name + suffix, Path(traj_path).name.rsplit(".traj", 1)[0]]) + ".demo.yaml"- )- output_file = Path(output_dir) / filename+def main(traj_path: Path, output_dir: Path, suffix: str = "", overwrite: bool = False, include_user: bool = False):+ output_file = output_dir / (traj_path.parent.name + suffix) / (traj_path.stem.removesuffix(".traj") + ".demo.yaml")if output_file.exists() and not overwrite:- msg = f"Output file already exists: {output_file}"+ msg = f"Output file already exists: {output_file}. Use --overwrite to overwrite."raise FileExistsError(msg)output_file.parent.mkdir(parents=True, exist_ok=True)convert_traj_to_action_demo(traj_path, output_file, include_user)-def string2bool(s):- if s.lower() in {"true", "1"}:- return True- elif s.lower() in {"false", "0"}:- return False- else:- msg = f"Invalid boolean string: {s}"- raise ValueError(msg)--def run_from_cli(args: list[str] | None = None):- parser = ArgumentParser()- parser.add_argument("traj_path", type=str, help="Path to trajectory file")- parser.add_argument("--output_dir", type=str, help="Output directory for action demos", default="./demos")+ """Convert a trajectory file to a demo file."""+ parser = ArgumentParser(description=__doc__)+ parser.add_argument("traj_path", type=Path, help="Path to trajectory file")+ parser.add_argument("--output_dir", type=Path, help="Output directory for action demos", default=Path("./demos"))parser.add_argument("--suffix", type=str, help="Suffix for the output file", default="")- parser.add_argument("--overwrite", type=string2bool, help="Overwrite existing files", default=False, nargs="?")+ parser.add_argument("--overwrite", help="Overwrite existing files", action="store_true")parser.add_argument("--include_user",- type=string2bool,help="Include user responses (computer)",- default=False,- nargs="?",+ action="store_true",)parsed_args = parser.parse_args(args)main(**vars(parsed_args))