"""Base classes for Anthropic native tools.

Ported from anthropic-quickstarts/computer-use-demo.
"""

from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Any


@dataclass(kw_only=True, frozen=True)
class ToolResult:
    """Result from tool execution.

    Structured result that can contain text output, errors, images, and system messages.
    """
    output: str | None = None
    error: str | None = None
    base64_image: str | None = None
    system: str | None = None


@dataclass(kw_only=True, frozen=True)
class CLIResult:
    """Result from CLI-style tools (like text editor).

    Similar to ToolResult but simpler for text-only tools.
    """
    exit_code: int
    output: str
    error: str


class ToolError(Exception):
    """Exception raised by tool execution."""
    pass


class BaseAnthropicTool(metaclass=ABCMeta):
    """Base class for Anthropic native tools.

    Native tools are version-coupled to model training and don't require schemas.
    """

    api_type: str  # e.g., "bash_20250124"
    name: str  # e.g., "bash"
    beta_flag: str | None = None  # e.g., "computer-use-2025-11-24"

    @abstractmethod
    async def __call__(self, **kwargs: Any) -> ToolResult | CLIResult:
        """Execute the tool.

        Args:
            **kwargs: Tool-specific parameters

        Returns:
            ToolResult or CLIResult with execution output
        """
        ...

    @abstractmethod
    def to_params(self) -> dict[str, Any]:
        """Return tool definition for API.

        Returns:
            Dict with type and name (no schema for native tools)
        """
        ...
