"""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, ClassVar, Optional, TYPE_CHECKING, Union

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

from .messages import ContentBlock, Message, Metrics, ToolResultBlock, ToolUseBlock, Usage
from .models import ModelError
from .tools import ToolError


@dataclass(kw_only=True)
class Interrupt:
    """Human-in-the-loop interrupt raised by a tool or hook."""
    id: str
    name: str
    reason: str | None


class StopReason(str):
    """Why the model stopped generating."""
    __slots__ = ()

    END_TURN: StopReason
    TOOL_USE: StopReason
    MAX_TOKENS: StopReason
    ERROR: StopReason
    CONTENT_FILTERED: StopReason
    GUARDRAIL_INTERVENED: StopReason
    STOP_SEQUENCE: StopReason
    MODEL_CONTEXT_WINDOW_EXCEEDED: StopReason
    CANCELLED: StopReason

StopReason.END_TURN = StopReason('end-turn')  # type: ignore[attr-defined]
StopReason.TOOL_USE = StopReason('tool-use')  # type: ignore[attr-defined]
StopReason.MAX_TOKENS = StopReason('max-tokens')  # type: ignore[attr-defined]
StopReason.ERROR = StopReason('error')  # type: ignore[attr-defined]
StopReason.CONTENT_FILTERED = StopReason('content-filtered')  # type: ignore[attr-defined]
StopReason.GUARDRAIL_INTERVENED = StopReason('guardrail-intervened')  # type: ignore[attr-defined]
StopReason.STOP_SEQUENCE = StopReason('stop-sequence')  # type: ignore[attr-defined]
StopReason.MODEL_CONTEXT_WINDOW_EXCEEDED = StopReason('model-context-window-exceeded')  # type: ignore[attr-defined]
StopReason.CANCELLED = StopReason('cancelled')  # type: ignore[attr-defined]


@dataclass(kw_only=True)
class MetadataEvent:
    """Usage and metrics accumulated so far."""
    usage: Usage | None
    metrics: Metrics | None


@dataclass(kw_only=True)
class TraceMetadataEntry:
    """Single key-value pair attached to a trace. Values are string-typed
to keep traces compact; structured payloads belong on `message`."""
    key: str
    value: str


@dataclass(kw_only=True)
class AgentTrace:
    """In-memory trace node. Returned flat; reconstruct the tree via `parent-id`."""
    id: str
    name: str
    parent_id: str | None
    start_time_ms: int
    end_time_ms: int | None
    duration_ms: int
    metadata: list[TraceMetadataEntry]
    message: Message | None


@dataclass(kw_only=True)
class ToolMetrics:
    """Per-tool execution metrics keyed by tool name in `agent-metrics`."""
    tool_name: str
    call_count: int
    success_count: int
    error_count: int
    total_time_ms: int


@dataclass(kw_only=True)
class InvocationMetrics:
    """Per-invocation metrics. Cycles are flattened into `agent-metrics.cycles`
and linked back via `invocation-id`."""
    invocation_id: str
    usage: Usage


@dataclass(kw_only=True)
class AgentLoopMetrics:
    """Per-cycle usage tracking."""
    cycle_id: str
    invocation_id: str
    duration_ms: int
    usage: Usage


@dataclass(kw_only=True)
class AgentMetrics:
    """Snapshot of agent metrics. Returned by `agent.get-metrics`."""
    cycle_count: int
    accumulated_usage: Usage
    accumulated_metrics: Metrics
    invocations: list[InvocationMetrics]
    cycles: list[AgentLoopMetrics]
    tool_metrics: list[ToolMetrics]
    latest_context_size: int | None
    projected_context_size: int | None


@dataclass(kw_only=True)
class ToolUseData:
    """Mutable tool-use descriptor. `before-tool-call` hooks may rewrite fields."""
    name: str
    tool_use_id: str
    input: str


@dataclass(kw_only=True)
class HookRedaction:
    """Redaction information when guardrails block content."""
    user_message: str


@dataclass(kw_only=True)
class ModelStopData:
    """Model response surfaced on `after-model-call`."""
    message: Message
    stop_reason: StopReason
    redaction: HookRedaction | None


@dataclass(kw_only=True)
class BeforeInvocationData:
    """Payload for `before-invocation`."""
    invocation_state: str


@dataclass(kw_only=True)
class AfterInvocationData:
    """Payload for `after-invocation`."""
    invocation_state: str


@dataclass(kw_only=True)
class MessageAddedData:
    """Payload for `message-added`."""
    message: Message


@dataclass(kw_only=True)
class BeforeModelCallData:
    """Payload for `before-model-call`."""
    projected_input_tokens: int | None


@dataclass(kw_only=True)
class AfterModelCallData:
    """Payload for `after-model-call`."""
    attempt_count: int
    stop_data: ModelStopData | None
    error: ModelError | None


@dataclass(kw_only=True)
class BeforeToolCallData:
    """Payload for `before-tool-call`."""
    tool_use: ToolUseData


@dataclass(kw_only=True)
class AfterToolCallData:
    """Payload for `after-tool-call`."""
    tool_use: ToolUseData
    tool_result: ToolResultBlock
    error: ToolError | None


@dataclass(kw_only=True)
class ToolsBatchData:
    """Payload for `before-tools` / `after-tools`."""
    message: Message


@dataclass(kw_only=True)
class ContentBlockData:
    """Payload for `content-block`."""
    content_block: ContentBlock


@dataclass(kw_only=True)
class ModelMessageData:
    """Payload for `model-message`."""
    message: Message
    stop_reason: StopReason


@dataclass(kw_only=True)
class ToolResultData:
    """Payload for `tool-result-hook`."""
    tool_result: ToolResultBlock


@dataclass(kw_only=True)
class ToolStreamUpdateData:
    """Payload for `tool-stream-update`."""
    data: str


@dataclass(kw_only=True)
class ModelStreamUpdateData:
    """Payload for `model-stream-update`."""
    event: str


@dataclass(kw_only=True)
class InputRedaction:
    """Input redaction emitted when a guardrail blocks input. Original is in history."""
    replace_content: str


@dataclass(kw_only=True)
class OutputRedaction:
    """Output redaction emitted when a guardrail blocks output."""
    redacted_content: str | None
    replace_content: str


@dataclass(kw_only=True)
class RedactionEvent:
    """Redaction event. Input and output fields are independent; at least one is set."""
    input_redaction: InputRedaction | None
    output_redaction: OutputRedaction | None


@dataclass(kw_only=True)
class StopEvent:
    """Terminal event for a stream."""
    reason: StopReason
    usage: Usage | None
    metrics: Metrics | None
    structured_output: str | None


@dataclass(kw_only=True)
class AgentResultData:
    """Payload for `agent-result`."""
    stop: StopEvent


class StreamError:
    """Why the agent loop surfaced an error mid-stream."""
    if TYPE_CHECKING:
        Model: ClassVar[type["_StreamError_Model"]]
        Tool: ClassVar[type["_StreamError_Tool"]]
        ContextWindowExceeded: ClassVar[type["_StreamError_ContextWindowExceeded"]]
        MaxTokensReached: ClassVar[type["_StreamError_MaxTokensReached"]]
        StructuredOutputUnavailable: ClassVar[type["_StreamError_StructuredOutputUnavailable"]]
        Internal: ClassVar[type["_StreamError_Internal"]]
        _CASES: ClassVar[dict[str, type]]
        @staticmethod
        def lift(raw: _WitVariant) -> "StreamError": ...

class _StreamError_Model(_WitVariantCase, StreamError):
    """A model call failed."""
    tag = 'model'
StreamError.Model = _StreamError_Model  # type: ignore[attr-defined]

class _StreamError_Tool(_WitVariantCase, StreamError):
    """A tool call failed."""
    tag = 'tool'
StreamError.Tool = _StreamError_Tool  # type: ignore[attr-defined]

class _StreamError_ContextWindowExceeded(_WitVariantCase, StreamError):
    """Input exceeded the model's context window and no conversation
manager could recover."""
    tag = 'context-window-exceeded'
StreamError.ContextWindowExceeded = _StreamError_ContextWindowExceeded  # type: ignore[attr-defined]

class _StreamError_MaxTokensReached(_WitVariantCase, StreamError):
    """Exceeded the model's max-tokens budget mid-response."""
    tag = 'max-tokens-reached'
StreamError.MaxTokensReached = _StreamError_MaxTokensReached  # type: ignore[attr-defined]

class _StreamError_StructuredOutputUnavailable(_WitVariantCase, StreamError):
    """Structured output was requested but the model never called the
tool, even after being forced."""
    tag = 'structured-output-unavailable'
StreamError.StructuredOutputUnavailable = _StreamError_StructuredOutputUnavailable  # type: ignore[attr-defined]

class _StreamError_Internal(_WitVariantCase, StreamError):
    """Catch-all for internal failures."""
    tag = 'internal'
StreamError.Internal = _StreamError_Internal  # type: ignore[attr-defined]

StreamError._CASES = {  # type: ignore[attr-defined]
    'model': StreamError.Model,
    'tool': StreamError.Tool,
    'context-window-exceeded': StreamError.ContextWindowExceeded,
    'max-tokens-reached': StreamError.MaxTokensReached,
    'structured-output-unavailable': StreamError.StructuredOutputUnavailable,
    'internal': StreamError.Internal,
}


def _StreamError_lift(raw: _WitVariant) -> StreamError:
    cls = StreamError._CASES.get(raw.tag)  # type: ignore[attr-defined]
    if cls is None:
        raise ValueError(f'unknown StreamError arm: {raw.tag!r}')
    return cls(raw.payload)
StreamError.lift = staticmethod(_StreamError_lift)  # type: ignore[attr-defined]

class StreamEvent:
    """Events yielded during agent streaming.
Hot-path arms: `text-delta`, `tool-use`, `tool-result`. Other content
blocks flow through `content`. Lifecycle arms (`before-invocation`
through `agent-result`) mirror a hook system and can be filtered by tag."""
    if TYPE_CHECKING:
        TextDelta: ClassVar[type["_StreamEvent_TextDelta"]]
        ToolUse: ClassVar[type["_StreamEvent_ToolUse"]]
        ToolResult: ClassVar[type["_StreamEvent_ToolResult"]]
        Content: ClassVar[type["_StreamEvent_Content"]]
        Metadata: ClassVar[type["_StreamEvent_Metadata"]]
        Stop: ClassVar[type["_StreamEvent_Stop"]]
        Redaction: ClassVar[type["_StreamEvent_Redaction"]]
        Error: ClassVar[type["_StreamEvent_Error"]]
        Interrupt: ClassVar[type["_StreamEvent_Interrupt"]]
        Initialized: ClassVar[type["_StreamEvent_Initialized"]]
        BeforeInvocation: ClassVar[type["_StreamEvent_BeforeInvocation"]]
        AfterInvocation: ClassVar[type["_StreamEvent_AfterInvocation"]]
        MessageAdded: ClassVar[type["_StreamEvent_MessageAdded"]]
        BeforeModelCall: ClassVar[type["_StreamEvent_BeforeModelCall"]]
        AfterModelCall: ClassVar[type["_StreamEvent_AfterModelCall"]]
        BeforeTools: ClassVar[type["_StreamEvent_BeforeTools"]]
        AfterTools: ClassVar[type["_StreamEvent_AfterTools"]]
        BeforeToolCall: ClassVar[type["_StreamEvent_BeforeToolCall"]]
        AfterToolCall: ClassVar[type["_StreamEvent_AfterToolCall"]]
        ContentBlock: ClassVar[type["_StreamEvent_ContentBlock"]]
        ModelMessage: ClassVar[type["_StreamEvent_ModelMessage"]]
        ToolResultHook: ClassVar[type["_StreamEvent_ToolResultHook"]]
        ToolUpdate: ClassVar[type["_StreamEvent_ToolUpdate"]]
        ModelUpdate: ClassVar[type["_StreamEvent_ModelUpdate"]]
        AgentResult: ClassVar[type["_StreamEvent_AgentResult"]]
        _CASES: ClassVar[dict[str, type]]
        @staticmethod
        def lift(raw: _WitVariant) -> "StreamEvent": ...

class _StreamEvent_TextDelta(_WitVariantCase, StreamEvent):
    """Incremental text from the model."""
    tag = 'text-delta'
StreamEvent.TextDelta = _StreamEvent_TextDelta  # type: ignore[attr-defined]

class _StreamEvent_ToolUse(_WitVariantCase, StreamEvent):
    """Model requested a tool call."""
    tag = 'tool-use'
StreamEvent.ToolUse = _StreamEvent_ToolUse  # type: ignore[attr-defined]

class _StreamEvent_ToolResult(_WitVariantCase, StreamEvent):
    """Tool call completed."""
    tag = 'tool-result'
StreamEvent.ToolResult = _StreamEvent_ToolResult  # type: ignore[attr-defined]

class _StreamEvent_Content(_WitVariantCase, StreamEvent):
    """Non-hot-path content block (image, reasoning, citations, etc)."""
    tag = 'content'
StreamEvent.Content = _StreamEvent_Content  # type: ignore[attr-defined]

class _StreamEvent_Metadata(_WitVariantCase, StreamEvent):
    """Cumulative usage and metrics snapshot."""
    tag = 'metadata'
StreamEvent.Metadata = _StreamEvent_Metadata  # type: ignore[attr-defined]

class _StreamEvent_Stop(_WitVariantCase, StreamEvent):
    """Terminal event for the stream."""
    tag = 'stop'
StreamEvent.Stop = _StreamEvent_Stop  # type: ignore[attr-defined]

class _StreamEvent_Redaction(_WitVariantCase, StreamEvent):
    """Guardrail redaction fired."""
    tag = 'redaction'
StreamEvent.Redaction = _StreamEvent_Redaction  # type: ignore[attr-defined]

class _StreamEvent_Error(_WitVariantCase, StreamEvent):
    """Recoverable error surfaced mid-stream."""
    tag = 'error'
StreamEvent.Error = _StreamEvent_Error  # type: ignore[attr-defined]

class _StreamEvent_Interrupt(_WitVariantCase, StreamEvent):
    """Human-in-the-loop pause; resume via `response-stream.respond`."""
    tag = 'interrupt'
StreamEvent.Interrupt = _StreamEvent_Interrupt  # type: ignore[attr-defined]

class _StreamEvent_Initialized(_WitVariantCase, StreamEvent):
    """Agent finished construction."""
    tag = 'initialized'
StreamEvent.Initialized = _StreamEvent_Initialized  # type: ignore[attr-defined]

class _StreamEvent_BeforeInvocation(_WitVariantCase, StreamEvent):
    """About to process a user invocation."""
    tag = 'before-invocation'
StreamEvent.BeforeInvocation = _StreamEvent_BeforeInvocation  # type: ignore[attr-defined]

class _StreamEvent_AfterInvocation(_WitVariantCase, StreamEvent):
    """Finished processing a user invocation."""
    tag = 'after-invocation'
StreamEvent.AfterInvocation = _StreamEvent_AfterInvocation  # type: ignore[attr-defined]

class _StreamEvent_MessageAdded(_WitVariantCase, StreamEvent):
    """A message was appended to the conversation."""
    tag = 'message-added'
StreamEvent.MessageAdded = _StreamEvent_MessageAdded  # type: ignore[attr-defined]

class _StreamEvent_BeforeModelCall(_WitVariantCase, StreamEvent):
    """About to call the model."""
    tag = 'before-model-call'
StreamEvent.BeforeModelCall = _StreamEvent_BeforeModelCall  # type: ignore[attr-defined]

class _StreamEvent_AfterModelCall(_WitVariantCase, StreamEvent):
    """Model call returned."""
    tag = 'after-model-call'
StreamEvent.AfterModelCall = _StreamEvent_AfterModelCall  # type: ignore[attr-defined]

class _StreamEvent_BeforeTools(_WitVariantCase, StreamEvent):
    """About to run a batch of tool calls from one assistant turn."""
    tag = 'before-tools'
StreamEvent.BeforeTools = _StreamEvent_BeforeTools  # type: ignore[attr-defined]

class _StreamEvent_AfterTools(_WitVariantCase, StreamEvent):
    """Tool batch finished."""
    tag = 'after-tools'
StreamEvent.AfterTools = _StreamEvent_AfterTools  # type: ignore[attr-defined]

class _StreamEvent_BeforeToolCall(_WitVariantCase, StreamEvent):
    """About to call a single tool."""
    tag = 'before-tool-call'
StreamEvent.BeforeToolCall = _StreamEvent_BeforeToolCall  # type: ignore[attr-defined]

class _StreamEvent_AfterToolCall(_WitVariantCase, StreamEvent):
    """Tool call returned."""
    tag = 'after-tool-call'
StreamEvent.AfterToolCall = _StreamEvent_AfterToolCall  # type: ignore[attr-defined]

class _StreamEvent_ContentBlock(_WitVariantCase, StreamEvent):
    """A content block was assembled during streaming."""
    tag = 'content-block'
StreamEvent.ContentBlock = _StreamEvent_ContentBlock  # type: ignore[attr-defined]

class _StreamEvent_ModelMessage(_WitVariantCase, StreamEvent):
    """Model finished producing a full message."""
    tag = 'model-message'
StreamEvent.ModelMessage = _StreamEvent_ModelMessage  # type: ignore[attr-defined]

class _StreamEvent_ToolResultHook(_WitVariantCase, StreamEvent):
    """Tool finished execution (completion event, not streaming update)."""
    tag = 'tool-result-hook'
StreamEvent.ToolResultHook = _StreamEvent_ToolResultHook  # type: ignore[attr-defined]

class _StreamEvent_ToolUpdate(_WitVariantCase, StreamEvent):
    """Streaming update from a tool."""
    tag = 'tool-update'
StreamEvent.ToolUpdate = _StreamEvent_ToolUpdate  # type: ignore[attr-defined]

class _StreamEvent_ModelUpdate(_WitVariantCase, StreamEvent):
    """Streaming update from the model."""
    tag = 'model-update'
StreamEvent.ModelUpdate = _StreamEvent_ModelUpdate  # type: ignore[attr-defined]

class _StreamEvent_AgentResult(_WitVariantCase, StreamEvent):
    """Final event for an invocation, carrying the terminal result."""
    tag = 'agent-result'
StreamEvent.AgentResult = _StreamEvent_AgentResult  # type: ignore[attr-defined]

StreamEvent._CASES = {  # type: ignore[attr-defined]
    'text-delta': StreamEvent.TextDelta,
    'tool-use': StreamEvent.ToolUse,
    'tool-result': StreamEvent.ToolResult,
    'content': StreamEvent.Content,
    'metadata': StreamEvent.Metadata,
    'stop': StreamEvent.Stop,
    'redaction': StreamEvent.Redaction,
    'error': StreamEvent.Error,
    'interrupt': StreamEvent.Interrupt,
    'initialized': StreamEvent.Initialized,
    'before-invocation': StreamEvent.BeforeInvocation,
    'after-invocation': StreamEvent.AfterInvocation,
    'message-added': StreamEvent.MessageAdded,
    'before-model-call': StreamEvent.BeforeModelCall,
    'after-model-call': StreamEvent.AfterModelCall,
    'before-tools': StreamEvent.BeforeTools,
    'after-tools': StreamEvent.AfterTools,
    'before-tool-call': StreamEvent.BeforeToolCall,
    'after-tool-call': StreamEvent.AfterToolCall,
    'content-block': StreamEvent.ContentBlock,
    'model-message': StreamEvent.ModelMessage,
    'tool-result-hook': StreamEvent.ToolResultHook,
    'tool-update': StreamEvent.ToolUpdate,
    'model-update': StreamEvent.ModelUpdate,
    'agent-result': StreamEvent.AgentResult,
}


def _StreamEvent_lift(raw: _WitVariant) -> StreamEvent:
    cls = StreamEvent._CASES.get(raw.tag)  # type: ignore[attr-defined]
    if cls is None:
        raise ValueError(f'unknown StreamEvent arm: {raw.tag!r}')
    return cls(raw.payload)
StreamEvent.lift = staticmethod(_StreamEvent_lift)  # type: ignore[attr-defined]
