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

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


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


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


_MODEL_CONVERSATION_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,
}


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


class ModelConversationTypedDict(TypedDict):
    id: str
    created_at: datetime
    updated_at: datetime
    model: str
    instructions: NotRequired[Nullable[str]]
    r"""Instruction prompt the model will follow during the conversation."""
    tools: NotRequired[List[ModelConversationToolTypedDict]]
    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]]]
    name: NotRequired[Nullable[str]]
    r"""Name given to the conversation."""
    description: NotRequired[Nullable[str]]
    r"""Description of the what the conversation is about."""
    metadata: NotRequired[Nullable[Dict[str, Any]]]
    r"""Custom metadata for the conversation."""
    object: Literal["conversation"]


class ModelConversation(BaseModel):
    id: str

    created_at: datetime

    updated_at: datetime

    model: str

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

    tools: Optional[List[ModelConversationTool]] = 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

    name: OptionalNullable[str] = UNSET
    r"""Name given to the conversation."""

    description: OptionalNullable[str] = UNSET
    r"""Description of the what the conversation is about."""

    metadata: OptionalNullable[Dict[str, Any]] = UNSET
    r"""Custom metadata for the conversation."""

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

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "instructions",
                "tools",
                "completion_args",
                "guardrails",
                "name",
                "description",
                "metadata",
                "object",
            ]
        )
        nullable_fields = set(
            ["instructions", "guardrails", "name", "description", "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))
            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:
    ModelConversation.model_rebuild()
except NameError:
    pass
