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

from __future__ import annotations
from .referencechunk import ReferenceChunk, ReferenceChunkTypedDict
from .textchunk import TextChunk, TextChunkTypedDict
from .toolreferencechunk import ToolReferenceChunk, ToolReferenceChunkTypedDict
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ThinkingTypedDict = TypeAliasType(
    "ThinkingTypedDict",
    Union[TextChunkTypedDict, ReferenceChunkTypedDict, ToolReferenceChunkTypedDict],
)


Thinking = TypeAliasType(
    "Thinking", Union[TextChunk, ReferenceChunk, ToolReferenceChunk]
)


class ThinkChunkTypedDict(TypedDict):
    thinking: List[ThinkingTypedDict]
    type: Literal["thinking"]
    signature: NotRequired[Nullable[str]]
    r"""Signature to replay some reasoning blocks across turns."""
    closed: NotRequired[bool]
    r"""Whether the thinking chunk is closed or not. Currently only used for prefixing."""


class ThinkChunk(BaseModel):
    thinking: List[Thinking]

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

    signature: OptionalNullable[str] = UNSET
    r"""Signature to replay some reasoning blocks across turns."""

    closed: Optional[bool] = None
    r"""Whether the thinking chunk is closed or not. Currently only used for prefixing."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["type", "signature", "closed"])
        nullable_fields = set(["signature"])
        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:
    ThinkChunk.model_rebuild()
except NameError:
    pass
