File size: 4,032 Bytes
ab59633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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", "list_dir", "files", "dir"],
    "read_file": ["read_file", "open_file", "cat", "view_file", "show_file"],
    "write_file": ["write_file", "save_file", "put_file", "replace_file"],
    "run_tests": ["run_tests", "test", "pytest", "check_tests"],
    "python": ["python", "run_python", "py", "exec_python"],
    "sql": ["sql", "query", "run_sql", "db"],
    "search": ["search", "grep", "find_text"],
    "list_emails": ["list_emails", "inbox", "list_mail"],
    "read_email": ["read_email", "open_email", "get_mail"],
    "get_calendar": ["get_calendar", "calendar", "busy_times"],
    "check_slot": ["check_slot", "conflicts", "check_time"],
    "find_free_slots": ["find_free_slots", "free_slots", "availability"],
    "create_event": ["create_event", "send_invite", "book_meeting"],
    "show": ["show", "view", "display"],
    "guide": ["guide", "style_guide", "rules"],
    "set_line": ["set_line", "replace_line", "edit_line"],
    "check": ["check", "lint", "style_check"],
}
ARG_NAMES = {
    "path": ["path", "file", "filename"],
    "code": ["code", "source", "script"],
    "query": ["query", "statement", "sql"],
    "pattern": ["pattern", "text", "needle"],
    "email_id": ["email_id", "id", "message_id"],
    "content": ["content", "text", "body"],
    "person": ["person", "name", "who"],
    "date": ["date", "day"],
    "attendees": ["attendees", "people", "participants"],
    "page": ["page", "number"],
    "minutes": ["minutes", "duration", "length_min"],
    "line": ["line", "line_number", "row"],
    "text": ["text", "new_text", "replacement"],
}
# 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} | {"python"}


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)