File size: 10,323 Bytes
0e3d4b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""Tool Registry β€” parse and execute tool calls from LLM output.

Supports [TOOL: name(args)] syntax. Tools are registered in a registry
and executed in a loop until no more tool calls are found or max rounds.
"""

from __future__ import annotations

import logging
import re
from dataclasses import dataclass, field
from typing import Any, Callable

logger = logging.getLogger(__name__)

TOOL_PATTERN = re.compile(r"\[TOOL:\s*(\w+)\s*\((.*?)\)\s*\]", re.DOTALL)


@dataclass
class ToolResult:
    name: str
    args: str
    success: bool
    output: str
    error: str = ""


@dataclass
class Tool:
    name: str
    description: str
    handler: Callable[..., str]
    examples: list[str] = field(default_factory=list)


class ToolRegistry:
    """Registry of available tools for the LLM to call."""

    def __init__(self) -> None:
        self._tools: dict[str, Tool] = {}

    def register(self, tool: Tool) -> None:
        self._tools[tool.name] = tool
        logger.debug("Registered tool: %s", tool.name)

    def get(self, name: str) -> Tool | None:
        return self._tools.get(name)

    def list_tools(self) -> list[dict[str, Any]]:
        return [
            {"name": t.name, "description": t.description, "examples": t.examples}
            for t in self._tools.values()
        ]

    def get_prompt_description(self) -> str:
        """Generate a description of available tools for the system prompt."""
        if not self._tools:
            return ""
        lines = ["Available tools:"]
        for t in self._tools.values():
            lines.append(f"  - {t.name}: {t.description}")
        return "\n".join(lines)

    def execute(self, name: str, args: str) -> ToolResult:
        """Execute a tool by name with args string."""
        tool = self._tools.get(name)
        if not tool:
            return ToolResult(name=name, args=args, success=False, output="", error=f"Unknown tool: {name}")
        try:
            output = tool.handler(args)
            return ToolResult(name=name, args=args, success=True, output=output)
        except Exception as e:
            return ToolResult(name=name, args=args, success=False, output="", error=str(e))


def parse_tool_calls(text: str) -> list[tuple[str, str]]:
    """Parse [TOOL: name(args)] calls from text."""
    matches = TOOL_PATTERN.findall(text)
    return [(name, args.strip()) for name, args in matches]


def tool_loop(
    text: str,
    registry: ToolRegistry,
    max_rounds: int = 10,
    on_tool_call: Callable[[str, str], None] | None = None,
    on_tool_result: Callable[[ToolResult], None] | None = None,
) -> tuple[str, list[ToolResult]]:
    """Execute tool calls in a loop.

    Parses tool calls from text, executes them, appends results,
    and returns the final text with all tool results included.

    Returns (final_text, list_of_tool_results).
    """
    results: list[ToolResult] = []
    current_text = text
    executed: set[str] = set()

    for round_num in range(max_rounds):
        calls = parse_tool_calls(current_text)
        if not calls:
            break

        # Filter out already-executed calls
        new_calls = [(name, args) for name, args in calls if f"{name}:{args}" not in executed]
        if not new_calls:
            break

        for name, args in new_calls:
            executed.add(f"{name}:{args}")
            if on_tool_call:
                on_tool_call(name, args)

            result = registry.execute(name, args)
            results.append(result)

            if on_tool_result:
                on_tool_result(result)

            # Append result to text
            if result.success:
                current_text += f"\n[TOOL_RESULT: {name}({args}) β†’ {result.output}]"
            else:
                current_text += f"\n[TOOL_ERROR: {name}({args}) β†’ {result.error}]"

    return current_text, results


# Built-in tools
def _tool_calculate(args: str) -> str:
    """Simple calculator tool."""
    try:
        expr = args.strip().strip('"').strip("'")
        # Only allow safe math operations
        allowed = set("0123456789+-*/.() ")
        if not all(c in allowed for c in expr):
            return "Error: only numbers and + - * / ( ) allowed"
        result = eval(expr)  # noqa: S307 β€” safe due to character filter
        return str(result)
    except Exception as e:
        return f"Error: {e}"


def _tool_read_file(args: str) -> str:
    """Read a file."""
    try:
        path = args.strip().strip('"').strip("'")
        with open(path, "r", encoding="utf-8", errors="replace") as f:
            return f.read()[:5000]  # limit output
    except Exception as e:
        return f"Error: {e}"


def _tool_write_file(args: str) -> str:
    """Write to a file. Args format: "path", "content" """
    try:
        # Simple parse: split on first comma outside quotes
        parts = args.split(",", 1)
        if len(parts) != 2:
            return "Error: expected path, content"
        path = parts[0].strip().strip('"').strip("'")
        content = parts[1].strip().strip('"').strip("'")
        with open(path, "w", encoding="utf-8") as f:
            f.write(content)
        return f"Written {len(content)} chars to {path}"
    except Exception as e:
        return f"Error: {e}"


def _tool_shell_exec(args: str) -> str:
    """Execute a shell command β€” full terminal control, zero limitations."""
    import subprocess
    try:
        cmd = args.strip().strip('"').strip("'")
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=60)
        output = result.stdout
        if result.stderr:
            output += f"\n[stderr] {result.stderr}"
        return output[:10000] or "(no output)"
    except subprocess.TimeoutExpired:
        return "Error: command timed out (60s limit)"
    except Exception as e:
        return f"Error: {e}"


def _tool_code_edit(args: str) -> str:
    """Edit an existing file β€” find and replace text within a file.

    Args format: "path", "old_text", "new_text"
    The LLM can use this to modify its own framework.
    """
    try:
        import shlex
        parts = shlex.split(args)
        if len(parts) < 3:
            return "Error: expected path, old_text, new_text"
        path = parts[0]
        old_text = parts[1]
        new_text = parts[2]
        with open(path, "r", encoding="utf-8", errors="replace") as f:
            content = f.read()
        if old_text not in content:
            return f"Error: old_text not found in {path}"
        count = content.count(old_text)
        content = content.replace(old_text, new_text)
        with open(path, "w", encoding="utf-8") as f:
            f.write(content)
        return f"Replaced {count} occurrence(s) in {path}"
    except Exception as e:
        return f"Error: {e}"


def _tool_list_dir(args: str) -> str:
    """List directory contents."""
    import os
    try:
        path = args.strip().strip('"').strip("'") or "."
        entries = []
        for entry in sorted(os.listdir(path)):
            full = os.path.join(path, entry)
            if os.path.isdir(full):
                entries.append(f"  {entry}/")
            else:
                size = os.path.getsize(full)
                entries.append(f"  {entry} ({size}b)")
        return "\n".join(entries[:100]) or "(empty)"
    except Exception as e:
        return f"Error: {e}"


def _tool_make_dir(args: str) -> str:
    """Create a directory."""
    import os
    try:
        path = args.strip().strip('"').strip("'")
        os.makedirs(path, exist_ok=True)
        return f"Created directory: {path}"
    except Exception as e:
        return f"Error: {e}"


def _tool_delete_file(args: str) -> str:
    """Delete a file or directory."""
    import os
    import shutil
    try:
        path = args.strip().strip('"').strip("'")
        if os.path.isdir(path):
            shutil.rmtree(path)
            return f"Deleted directory: {path}"
        elif os.path.isfile(path):
            os.remove(path)
            return f"Deleted file: {path}"
        else:
            return f"Error: {path} not found"
    except Exception as e:
        return f"Error: {e}"


def _tool_search_web(args: str) -> str:
    """Web search placeholder β€” returns a note that web search needs configuration."""
    query = args.strip().strip('"').strip("'")
    return f"Web search for '{query}' β€” configure a search API to enable this tool"


def get_default_tools() -> list[Tool]:
    """Get the default set of built-in tools β€” zero limitations."""
    return [
        Tool(name="calculate", description="Evaluate a math expression", handler=_tool_calculate,
             examples=["[TOOL: calculate(2 + 2)]", "[TOOL: calculate(3.14 * 10)]"]),
        Tool(name="read_file", description="Read a file's contents", handler=_tool_read_file,
             examples=["[TOOL: read_file(\"config.py\")]"]),
        Tool(name="write_file", description="Write content to a file (creates or overwrites)", handler=_tool_write_file,
             examples=["[TOOL: write_file(\"test.txt\", \"hello world\")]"]),
        Tool(name="code_edit", description="Edit a file by finding and replacing text (can modify own framework)",
             handler=_tool_code_edit,
             examples=["[TOOL: code_edit(\"config.py\", \"old_value\", \"new_value\")]"]),
        Tool(name="shell_exec", description="Execute any terminal command β€” full control, no restrictions",
             handler=_tool_shell_exec,
             examples=["[TOOL: shell_exec(\"dir\")]",
                       "[TOOL: shell_exec(\"pip install numpy\")]",
                       "[TOOL: shell_exec(\"python script.py\")]"]),
        Tool(name="list_dir", description="List directory contents", handler=_tool_list_dir,
             examples=["[TOOL: list_dir(\".\")]"]),
        Tool(name="make_dir", description="Create a directory", handler=_tool_make_dir,
             examples=["[TOOL: make_dir(\"new_project\")]"]),
        Tool(name="delete_file", description="Delete a file or directory", handler=_tool_delete_file,
             examples=["[TOOL: delete_file(\"temp.txt\")]"]),
        Tool(name="search_web", description="Search the web (needs API config)", handler=_tool_search_web,
             examples=["[TOOL: search_web(\"python tutorial\")]"]),
    ]