"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: c4f7a932bd2e

from __future__ import annotations
from .audiocontent import AudioContent, AudioContentTypedDict
from .connectortoolcallmetadata import (
    ConnectorToolCallMetadata,
    ConnectorToolCallMetadataTypedDict,
)
from .embeddedresource import EmbeddedResource, EmbeddedResourceTypedDict
from .imagecontent import ImageContent, ImageContentTypedDict
from .resourcelink import ResourceLink, ResourceLinkTypedDict
from .textcontent import TextContent, TextContentTypedDict
from functools import partial
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils.unions import parse_open_union
import pydantic
from pydantic import ConfigDict, model_serializer
from pydantic.functional_validators import BeforeValidator
from typing import Any, Dict, List, Literal, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ConnectorToolCallResponseContentTypedDict = TypeAliasType(
    "ConnectorToolCallResponseContentTypedDict",
    Union[
        TextContentTypedDict,
        EmbeddedResourceTypedDict,
        ImageContentTypedDict,
        AudioContentTypedDict,
        ResourceLinkTypedDict,
    ],
)


class UnknownConnectorToolCallResponseContent(BaseModel):
    r"""A ConnectorToolCallResponseContent variant the SDK doesn't recognize. Preserves the raw payload."""

    type: Literal["UNKNOWN"] = "UNKNOWN"
    raw: Any
    is_unknown: Literal[True] = True

    model_config = ConfigDict(frozen=True)


_CONNECTOR_TOOL_CALL_RESPONSE_CONTENT_VARIANTS: dict[str, Any] = {
    "text": TextContent,
    "image": ImageContent,
    "audio": AudioContent,
    "resource_link": ResourceLink,
    "resource": EmbeddedResource,
}


ConnectorToolCallResponseContent = Annotated[
    Union[
        TextContent,
        ImageContent,
        AudioContent,
        ResourceLink,
        EmbeddedResource,
        UnknownConnectorToolCallResponseContent,
    ],
    BeforeValidator(
        partial(
            parse_open_union,
            disc_key="type",
            variants=_CONNECTOR_TOOL_CALL_RESPONSE_CONTENT_VARIANTS,
            unknown_cls=UnknownConnectorToolCallResponseContent,
            union_name="ConnectorToolCallResponseContent",
        )
    ),
]


class ConnectorToolCallResponseTypedDict(TypedDict):
    r"""Response from calling an MCP tool.

    We override mcp_types.CallToolResult because:
    - Models only support `content`, not `structuredContent` at top level
    - Downstream consumers (le-chat, etc.) need structuredContent/isError/_meta via metadata

    SYNC: Keep in sync with Harmattan (orchestrator) for harmonized tool result processing.
    """

    content: List[ConnectorToolCallResponseContentTypedDict]
    metadata: NotRequired[Nullable[ConnectorToolCallMetadataTypedDict]]


class ConnectorToolCallResponse(BaseModel):
    r"""Response from calling an MCP tool.

    We override mcp_types.CallToolResult because:
    - Models only support `content`, not `structuredContent` at top level
    - Downstream consumers (le-chat, etc.) need structuredContent/isError/_meta via metadata

    SYNC: Keep in sync with Harmattan (orchestrator) for harmonized tool result processing.
    """

    model_config = ConfigDict(
        populate_by_name=True, arbitrary_types_allowed=True, extra="allow"
    )
    __pydantic_extra__: Dict[str, Any] = pydantic.Field(init=False)

    content: List[ConnectorToolCallResponseContent]

    metadata: OptionalNullable[ConnectorToolCallMetadata] = UNSET

    @property
    def additional_properties(self):
        return self.__pydantic_extra__

    @additional_properties.setter
    def additional_properties(self, value):
        self.__pydantic_extra__ = value  # pyright: ignore[reportIncompatibleVariableOverride]

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["metadata"])
        nullable_fields = set(["metadata"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            serialized.pop(k, serialized.pop(n, None))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val
        for k, v in serialized.items():
            m[k] = v

        return m
