Skip to content

Tool ABC

pycodeloop.abc.tool.Tool

Bases: ABC

Action the agent can take. Set dangerous = True on subclasses that change state (filesystem, shell, remote calls) so Agent asks for confirmation before running them. Set concurrent_safe = True on subclasses whose run() has no shared mutable state, so Agent may run several calls to the same tool concurrently within one batch (distinct-named tools in a batch already run concurrently regardless of this flag — it only affects repeated same-name calls).

Source code in pycodeloop/abc/tool.py
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
class Tool(ABC):
    """Action the agent can take. Set `dangerous = True` on subclasses that
    change state (filesystem, shell, remote calls) so Agent asks for
    confirmation before running them. Set `concurrent_safe = True` on
    subclasses whose `run()` has no shared mutable state, so Agent may
    run several calls to the *same* tool concurrently within one batch
    (distinct-named tools in a batch already run concurrently regardless
    of this flag — it only affects repeated same-name calls)."""

    name: str
    description: str
    parameters: dict[str, Any] = {"type": "object", "properties": {}}
    dangerous: bool = False
    concurrent_safe: bool = False

    def schema(self) -> dict:
        return {
            "name": self.name,
            "description": self.description,
            "parameters": self.parameters,
        }

    def preview(self, **kwargs) -> str:
        """Human-readable summary of what `run(**kwargs)` would do.

        Shown to the user in a confirmation prompt before a dangerous tool
        actually runs. Override for a diff, a shell command line, etc.
        """
        return ", ".join(f"{key}={value!r}" for key, value in kwargs.items())

    @abstractmethod
    def run(self, **kwargs) -> ToolResult:
        raise NotImplementedError

concurrent_safe = False class-attribute instance-attribute

dangerous = False class-attribute instance-attribute

description instance-attribute

name instance-attribute

parameters = {'type': 'object', 'properties': {}} class-attribute instance-attribute

preview(**kwargs)

Human-readable summary of what run(**kwargs) would do.

Shown to the user in a confirmation prompt before a dangerous tool actually runs. Override for a diff, a shell command line, etc.

Source code in pycodeloop/abc/tool.py
38
39
40
41
42
43
44
def preview(self, **kwargs) -> str:
    """Human-readable summary of what `run(**kwargs)` would do.

    Shown to the user in a confirmation prompt before a dangerous tool
    actually runs. Override for a diff, a shell command line, etc.
    """
    return ", ".join(f"{key}={value!r}" for key, value in kwargs.items())

run(**kwargs) abstractmethod

Source code in pycodeloop/abc/tool.py
46
47
48
@abstractmethod
def run(self, **kwargs) -> ToolResult:
    raise NotImplementedError

schema()

Source code in pycodeloop/abc/tool.py
31
32
33
34
35
36
def schema(self) -> dict:
    return {
        "name": self.name,
        "description": self.description,
        "parameters": self.parameters,
    }

pycodeloop.abc.tool.ToolResult dataclass

Source code in pycodeloop/abc/tool.py
10
11
12
13
@dataclass
class ToolResult:
    output: str
    is_error: bool = False

is_error = False class-attribute instance-attribute

output instance-attribute

__init__(output, is_error=False)