Files
odysseus/src/agent_tools.py
Kenny Van de Maele 7443c36bd9 feat: Add edit_file tool + file-change diffs (#1239)
* Add edit_file tool + file-change diffs

edit_file is an exact old_string -> new_string replacement on a file on disk
(fails if old_string is missing or non-unique unless replace_all); write_file
also returns a unified diff. Diffs render collapsed in the tool bubble
(filename + +adds/-dels, theme colors); the raw JSON command box is hidden.

Security: edit_file is a sensitive filesystem-write tool, treated everywhere
write_file is —
  - added to NON_ADMIN_BLOCKED_TOOLS (is_public_blocked_tool / blocked_tools_for_owner),
    so on auth-enabled deployments a non-admin cannot run it; execute_tool_block
    refuses it for non-admin owners.
  - confined by the same path policy as read_file/write_file (allowlist +
    sensitive-file deny) via _resolve_tool_path.

Disambiguation in tool descriptions + bash prompt: edit_file/write_file are the
only way to write files (they show a diff) — never edit_document (editor panel)
or a bash heredoc/redirect.

Tests (tests/test_edit_file.py): non-admin block (policy + execution gate),
successful edit, not-found old_string, non-unique old_string (+ replace_all),
and path outside the allowed roots.

Files: src/tool_execution.py, src/agent_loop.py, src/tool_schemas.py,
src/agent_tools.py, src/tool_index.py, static/js/chat.js, static/style.css,
tests/test_edit_file.py.

* Drop redundant import os in write_file closure

os is already imported at module top.
2026-06-04 18:29:10 +02:00

140 lines
5.0 KiB
Python

"""
agent_tools.py — Facade module.
Re-exports tool parsing, schemas, execution, and implementations
for backward compatibility. All importers continue to work unchanged.
Sub-modules:
- tool_parsing.py: regex patterns, parse/strip functions
- tool_schemas.py: FUNCTION_TOOL_SCHEMAS, function_call_to_tool_block
- tool_execution.py: execute_tool_block, format_tool_result, MCP helpers
- tool_implementations.py: all do_* tool functions
"""
import logging
from collections import namedtuple
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Constants (kept here — sub-modules import from here)
# ---------------------------------------------------------------------------
MAX_AGENT_ROUNDS = 20
SHELL_TIMEOUT = 60
PYTHON_TIMEOUT = 30
MAX_OUTPUT_CHARS = 10_000
MAX_READ_CHARS = 20_000
# Tool types that trigger execution
TOOL_TAGS = {"bash", "python", "web_search", "web_fetch", "read_file", "write_file", "edit_file",
"create_document", "update_document", "edit_document",
"search_chats",
"chat_with_model", "create_session", "list_sessions",
"send_to_session",
"pipeline",
"manage_session", "manage_memory", "list_models",
"ui_control", "generate_image",
"manage_tasks", "api_call", "ask_teacher", "manage_skills",
"suggest_document",
"manage_endpoints", "manage_mcp", "manage_webhooks",
"manage_tokens", "manage_documents", "manage_settings",
"manage_notes", "manage_calendar",
"resolve_contact", "manage_contact", "list_email_accounts", "send_email", "list_emails",
"read_email", "reply_to_email", "bulk_email", "archive_email",
"delete_email", "mark_email_read",
# Cookbook tools (LLM serving + downloads). Without these
# entries, native function calls to e.g. list_served_models
# are rejected as "Unknown function call" before reaching
# the dispatcher — silent failure for the whole cookbook
# surface.
"download_model", "serve_model",
"list_served_models", "stop_served_model",
"list_downloads", "cancel_download",
"search_hf_models", "list_cached_models",
"list_serve_presets", "serve_preset", "adopt_served_model",
"list_cookbook_servers",
# Other tools the agent reaches for that were also missing.
"edit_image", "trigger_research", "manage_research",
# Generic loopback to any UI-button endpoint (cookbook,
# gallery, email folders, etc.) — agent uses this when
# there's no named tool wrapper for the action.
"app_api"}
ToolBlock = namedtuple("ToolBlock", ["tool_type", "content"])
# ---------------------------------------------------------------------------
# MCP Manager (kept here — used by execution and agent_loop)
# ---------------------------------------------------------------------------
_mcp_manager = None
def set_mcp_manager(manager):
"""Set the global MCP manager instance."""
global _mcp_manager
_mcp_manager = manager
def get_mcp_manager():
"""Get the global MCP manager instance."""
return _mcp_manager
# ---------------------------------------------------------------------------
# Helpers (kept here — used by sub-modules)
# ---------------------------------------------------------------------------
def _truncate(text: str, limit: int = MAX_OUTPUT_CHARS) -> str:
# Callers treat the result as text, so always return a string: coerce a
# non-string (None -> "", otherwise str(...)) instead of returning it raw,
# which would just move the crash downstream.
if not isinstance(text, str):
text = "" if text is None else str(text)
if len(text) > limit:
return text[:limit] + f"\n... (truncated, {len(text)} chars total)"
return text
# ---------------------------------------------------------------------------
# Re-exports from sub-modules
# ---------------------------------------------------------------------------
# Parsing
from src.tool_parsing import ( # noqa: E402, F401
parse_tool_blocks,
strip_tool_blocks,
_TOOL_NAME_MAP,
_TOOL_BLOCK_RE,
_TOOL_CALL_RE,
_XML_TOOL_CALL_RE,
_XML_INVOKE_RE,
_XML_PARAM_RE,
)
# Schemas
from src.tool_schemas import ( # noqa: E402, F401
FUNCTION_TOOL_SCHEMAS,
function_call_to_tool_block,
)
# Execution
from src.tool_execution import ( # noqa: E402, F401
execute_tool_block,
format_tool_result,
)
# Implementations
from src.tool_implementations import ( # noqa: E402, F401
set_active_document,
set_active_model,
get_active_document,
do_create_document,
do_update_document,
do_edit_document,
do_suggest_document,
do_search_chats,
do_manage_skills,
do_manage_tasks,
do_manage_endpoints,
do_manage_mcp,
do_manage_webhooks,
do_manage_tokens,
do_manage_documents,
do_manage_settings,
do_api_call,
)