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

from __future__ import annotations
from .codeinterpretertool import CodeInterpreterTool, CodeInterpreterToolTypedDict
from .completionargs import CompletionArgs, CompletionArgsTypedDict
from .customconnector import CustomConnector, CustomConnectorTypedDict
from .documentlibrarytool import DocumentLibraryTool, DocumentLibraryToolTypedDict
from .functiontool import FunctionTool, FunctionToolTypedDict
from .guardrailconfig import GuardrailConfig, GuardrailConfigTypedDict
from .imagegenerationtool import ImageGenerationTool, ImageGenerationToolTypedDict
from .websearchpremiumtool import WebSearchPremiumTool, WebSearchPremiumToolTypedDict
from .websearchtool import WebSearchTool, WebSearchToolTypedDict
from datetime import datetime
from functools import partial
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils import validate_const
from mistralai.client.utils.unions import parse_open_union
import pydantic
from pydantic import ConfigDict, model_serializer
from pydantic.functional_validators import AfterValidator, BeforeValidator
from typing import Any, Dict, List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


AgentToolTypedDict = TypeAliasType(
    "AgentToolTypedDict",
    Union[
        FunctionToolTypedDict,
        WebSearchToolTypedDict,
        WebSearchPremiumToolTypedDict,
        CodeInterpreterToolTypedDict,
        ImageGenerationToolTypedDict,
        DocumentLibraryToolTypedDict,
        CustomConnectorTypedDict,
    ],
)


class UnknownAgentTool(BaseModel):
    r"""A AgentTool 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)


_AGENT_TOOL_VARIANTS: dict[str, Any] = {
    "code_interpreter": CodeInterpreterTool,
    "connector": CustomConnector,
    "document_library": DocumentLibraryTool,
    "function": FunctionTool,
    "image_generation": ImageGenerationTool,
    "web_search": WebSearchTool,
    "web_search_premium": WebSearchPremiumTool,
}


AgentTool = Annotated[
    Union[
        CodeInterpreterTool,
        CustomConnector,
        DocumentLibraryTool,
        FunctionTool,
        ImageGenerationTool,
        WebSearchTool,
        WebSearchPremiumTool,
        UnknownAgentTool,
    ],
    BeforeValidator(
        partial(
            parse_open_union,
            disc_key="type",
            variants=_AGENT_TOOL_VARIANTS,
            unknown_cls=UnknownAgentTool,
            union_name="AgentTool",
        )
    ),
]


class AgentTypedDict(TypedDict):
    model: str
    name: str
    id: str
    version: int
    versions: List[int]
    created_at: datetime
    updated_at: datetime
    deployment_chat: bool
    source: str
    instructions: NotRequired[Nullable[str]]
    r"""Instruction prompt the model will follow during the conversation."""
    tools: NotRequired[List[AgentToolTypedDict]]
    r"""List of tools which are available to the model during the conversation."""
    completion_args: NotRequired[CompletionArgsTypedDict]
    r"""White-listed arguments from the completion API"""
    guardrails: NotRequired[Nullable[List[GuardrailConfigTypedDict]]]
    description: NotRequired[Nullable[str]]
    handoffs: NotRequired[Nullable[List[str]]]
    metadata: NotRequired[Nullable[Dict[str, Any]]]
    object: Literal["agent"]
    version_message: NotRequired[Nullable[str]]


class Agent(BaseModel):
    model: str

    name: str

    id: str

    version: int

    versions: List[int]

    created_at: datetime

    updated_at: datetime

    deployment_chat: bool

    source: str

    instructions: OptionalNullable[str] = UNSET
    r"""Instruction prompt the model will follow during the conversation."""

    tools: Optional[List[AgentTool]] = None
    r"""List of tools which are available to the model during the conversation."""

    completion_args: Optional[CompletionArgs] = None
    r"""White-listed arguments from the completion API"""

    guardrails: OptionalNullable[List[GuardrailConfig]] = UNSET

    description: OptionalNullable[str] = UNSET

    handoffs: OptionalNullable[List[str]] = UNSET

    metadata: OptionalNullable[Dict[str, Any]] = UNSET

    object: Annotated[
        Annotated[Optional[Literal["agent"]], AfterValidator(validate_const("agent"))],
        pydantic.Field(alias="object"),
    ] = "agent"

    version_message: OptionalNullable[str] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "instructions",
                "tools",
                "completion_args",
                "guardrails",
                "description",
                "handoffs",
                "metadata",
                "object",
                "version_message",
            ]
        )
        nullable_fields = set(
            [
                "instructions",
                "guardrails",
                "description",
                "handoffs",
                "metadata",
                "version_message",
            ]
        )
        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))
            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

        return m


try:
    Agent.model_rebuild()
except NameError:
    pass
