Download source/codedesk/interface.py from rca-umb/five-desks-code: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-code/resolve/main/source/codedesk/interface.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-code/source/codedesk/interface.py
-
curl -L -o interface.py https://huggingface.co/datasets/rca-umb/five-desks-code/resolve/main/source/codedesk/interface.py
3.31 kB
| """Per-episode tool and argument names. | |
| Every private arena task brings its own interface. If one task always calls its tools read_file and run_tests, a | |
| policy can memorise the names instead of reading the tool list. Each reset therefore draws names from these pools | |
| (the canonical name is one of the options). `submit` keeps its name and arguments: it is also the arena's finish | |
| action. | |
| """ | |
| import random | |
| import re | |
| from .base import Tool | |
| TOOL_NAMES = { | |
| "list_files": ["list_files", "ls", "tree", "list_dir"], | |
| "read_file": ["read_file", "open_file", "cat", "view_file", "show_file"], | |
| "search": ["search", "grep", "find_text", "search_code"], | |
| "edit_file": ["edit_file", "str_replace", "replace_in_file", "patch_file"], | |
| "write_file": ["write_file", "save_file", "create_file", "put_file"], | |
| "run": ["run", "bash", "shell", "exec", "run_command"], | |
| "run_tests": ["run_tests", "pytest", "test", "check_tests"], | |
| } | |
| ARG_NAMES = { | |
| "path": ["path", "file", "filename"], | |
| "start": ["start", "from_line", "first"], | |
| "end": ["end", "to_line", "last"], | |
| "pattern": ["pattern", "regex", "query"], | |
| "old": ["old", "old_str", "find", "before"], | |
| "new": ["new", "new_str", "replace", "after"], | |
| "content": ["content", "text", "body"], | |
| "command": ["command", "cmd", "script"], | |
| "target": ["target", "path", "tests"], | |
| } | |
| # Names that are also ordinary words ("the style guide", "check how...") are renamed in the tool list but not in | |
| # running text; the others are replaced wherever they appear as a word. | |
| TEXT_SAFE = {name for name in TOOL_NAMES if "_" in name} | |
| class Interface: | |
| def __init__(self, tools: list[Tool], rng: random.Random): | |
| self.tool_names, used = {}, {"submit"} | |
| for tool in tools: | |
| options = [n for n in TOOL_NAMES.get(tool.name, [tool.name]) if n not in used] or [tool.name] | |
| name = tool.name if tool.name == "submit" else rng.choice(options) | |
| used.add(name) | |
| self.tool_names[tool.name] = name | |
| pairs = [(old, new) for old, new in self.tool_names.items() if old in TEXT_SAFE and old != new] | |
| self._pattern = re.compile(r"\b(" + "|".join(re.escape(old) for old, _ in pairs) + r")\b") if pairs else None | |
| self._lookup = dict(pairs) | |
| self.tools = [self._rename(tool, rng) for tool in tools] | |
| def _rename(self, tool: Tool, rng: random.Random) -> Tool: | |
| if tool.name == "submit": | |
| return tool | |
| args, used = {}, set() | |
| for arg in tool.args: | |
| options = [n for n in ARG_NAMES.get(arg, [arg]) if n not in used] or [arg] | |
| args[arg] = rng.choice(options) | |
| used.add(args[arg]) | |
| back = {new: old for old, new in args.items()} | |
| def fn(a, _fn=tool.fn): | |
| return _fn({back.get(k, k): v for k, v in a.items()}) | |
| return Tool(self.tool_names[tool.name], self.text(tool.help), fn, | |
| {args[k]: t for k, t in tool.args.items()}, tuple(args[k] for k in tool.optional), tool.final) | |
| def text(self, s: str) -> str: | |
| """Rewrite canonical tool names in briefs and tool output to this episode's names.""" | |
| if not self._pattern or not isinstance(s, str): | |
| return s | |
| return self._pattern.sub(lambda m: self._lookup[m.group(1)], s) | |