"""Auto-generated by bindgen. Do not edit."""
# ruff: noqa: E501, F401, I001
# fmt: off

from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Optional, Union

from wasmtime.component import Variant as _WitVariant
from wasmtime.component import VariantCase as _WitVariantCase

from .messages import ToolResultContent


@dataclass(kw_only=True)
class ToolSpec:
    """Declaration of a tool the model can call."""
    name: str
    description: str
    input_schema: str


@dataclass(kw_only=True)
class AgentAsToolConfig:
    """Wrap a configured agent as a tool callable by the parent agent. The
child agent is instantiated at registration time."""
    name: str | None
    description: str | None
    preserve_context: bool
    agent_config: str


@dataclass(kw_only=True)
class CallToolArgs:
    """Arguments for a single tool call."""
    name: str
    input: str
    tool_use_id: str


class ToolChoice:
    """Policy controlling whether and how the model calls tools on the next
generation step."""

    class Auto(_WitVariantCase):
        """Model decides whether to call a tool."""
        tag = 'auto'

    class Any(_WitVariantCase):
        """Model must call at least one tool."""
        tag = 'any'

    class Named(_WitVariantCase):
        """Model must call the tool with this name."""
        tag = 'named'

    _CASES: dict[str, type] = {
        'auto': Auto,
        'any': Any,
        'named': Named,
    }

    @staticmethod
    def lift(raw: _WitVariant) -> ToolChoice:
        cls = ToolChoice._CASES.get(raw.tag)
        if cls is None:
            raise ValueError(f'unknown ToolChoice arm: {raw.tag!r}')
        return cls(raw.payload)

class ToolEventStream:
    """Pull-based stream of tool events. Sync-WIT placeholder for
`stream<tool-stream-event>`."""
    # Wraps a wasmtime-py ResourceAny / ResourceHost handle.
    # The runtime sets ._handle to the underlying resource and
    # ._invoke to a callable that dispatches a method by WIT name.

    def __init__(self, handle: Any, invoke: Any = None) -> None:
        self._handle = handle
        self._invoke = invoke

    def read(self) -> ToolStreamEvent | None:
        return self._invoke('[method]tool-event-stream.read', (self._handle,))


class ToolError:
    """Why a tool call failed."""

    class Unknown(_WitVariantCase):
        """No tool registered under the given name."""
        tag = 'unknown'

    class InvalidInput(_WitVariantCase):
        """Tool input didn't match the declared input schema."""
        tag = 'invalid-input'

    class ExecutionFailed(_WitVariantCase):
        """Tool ran but returned an error result."""
        tag = 'execution-failed'

    class TimedOut(_WitVariantCase):
        """Tool exceeded its time budget."""
        tag = 'timed-out'

    class Cancelled(_WitVariantCase):
        """Tool was cancelled before completion."""
        tag = 'cancelled'

    class Internal(_WitVariantCase):
        """Catch-all for internal failures."""
        tag = 'internal'

    _CASES: dict[str, type] = {
        'unknown': Unknown,
        'invalid-input': InvalidInput,
        'execution-failed': ExecutionFailed,
        'timed-out': TimedOut,
        'cancelled': Cancelled,
        'internal': Internal,
    }

    @staticmethod
    def lift(raw: _WitVariant) -> ToolError:
        cls = ToolError._CASES.get(raw.tag)
        if cls is None:
            raise ValueError(f'unknown ToolError arm: {raw.tag!r}')
        return cls(raw.payload)

ToolStreamEvent = str | list[ToolResultContent] | ToolError
"""Incremental event emitted by a streaming tool while running."""
