"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .applypatchcallitem import ApplyPatchCallItem, ApplyPatchCallItemTypedDict
from .applypatchcalloutputitem import (
    ApplyPatchCallOutputItem,
    ApplyPatchCallOutputItemTypedDict,
)
from .inputaudio import InputAudio, InputAudioTypedDict
from .inputfile import InputFile, InputFileTypedDict
from .inputimage import InputImage, InputImageTypedDict
from .inputtext import InputText, InputTextTypedDict
from .openairesponsecustomtoolcall import (
    OpenAIResponseCustomToolCall,
    OpenAIResponseCustomToolCallTypedDict,
)
from .openairesponsecustomtoolcalloutput import (
    OpenAIResponseCustomToolCallOutput,
    OpenAIResponseCustomToolCallOutputTypedDict,
)
from .openairesponsefunctiontoolcall import (
    OpenAIResponseFunctionToolCall,
    OpenAIResponseFunctionToolCallTypedDict,
)
from .openairesponsefunctiontoolcalloutput import (
    OpenAIResponseFunctionToolCallOutput,
    OpenAIResponseFunctionToolCallOutputTypedDict,
)
from .openairesponseinputmessageitem import (
    OpenAIResponseInputMessageItem,
    OpenAIResponseInputMessageItemTypedDict,
)
from .outputitemimagegenerationcall import (
    OutputItemImageGenerationCall,
    OutputItemImageGenerationCallTypedDict,
)
from .outputmessage import OutputMessage, OutputMessageTypedDict
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import get_discriminator
from pydantic import Discriminator, Tag, model_serializer
from typing import Any, List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


BaseInputsContent1TypedDict = TypeAliasType(
    "BaseInputsContent1TypedDict",
    Union[
        InputTextTypedDict, InputAudioTypedDict, InputImageTypedDict, InputFileTypedDict
    ],
)


BaseInputsContent1 = Annotated[
    Union[
        Annotated[InputAudio, Tag("input_audio")],
        Annotated[InputFile, Tag("input_file")],
        Annotated[InputImage, Tag("input_image")],
        Annotated[InputText, Tag("input_text")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


BaseInputsContent2TypedDict = TypeAliasType(
    "BaseInputsContent2TypedDict", Union[List[BaseInputsContent1TypedDict], str]
)


BaseInputsContent2 = TypeAliasType(
    "BaseInputsContent2", Union[List[BaseInputsContent1], str]
)


BaseInputsPhaseFinalAnswer = Literal["final_answer",]


BaseInputsPhaseCommentary = Literal["commentary",]


BaseInputsPhaseUnionTypedDict = TypeAliasType(
    "BaseInputsPhaseUnionTypedDict",
    Union[BaseInputsPhaseCommentary, BaseInputsPhaseFinalAnswer, Any],
)


BaseInputsPhaseUnion = TypeAliasType(
    "BaseInputsPhaseUnion",
    Union[BaseInputsPhaseCommentary, BaseInputsPhaseFinalAnswer, Any],
)


BaseInputsRoleDeveloper = Literal["developer",]


BaseInputsRoleAssistant = Literal["assistant",]


BaseInputsRoleSystem = Literal["system",]


BaseInputsRoleUser = Literal["user",]


BaseInputsRoleUnionTypedDict = TypeAliasType(
    "BaseInputsRoleUnionTypedDict",
    Union[
        BaseInputsRoleUser,
        BaseInputsRoleSystem,
        BaseInputsRoleAssistant,
        BaseInputsRoleDeveloper,
    ],
)


BaseInputsRoleUnion = TypeAliasType(
    "BaseInputsRoleUnion",
    Union[
        BaseInputsRoleUser,
        BaseInputsRoleSystem,
        BaseInputsRoleAssistant,
        BaseInputsRoleDeveloper,
    ],
)


BaseInputsType = Literal["message",]


class BaseInputsMessageTypedDict(TypedDict):
    content: BaseInputsContent2TypedDict
    role: BaseInputsRoleUnionTypedDict
    phase: NotRequired[Nullable[BaseInputsPhaseUnionTypedDict]]
    type: NotRequired[BaseInputsType]


class BaseInputsMessage(BaseModel):
    content: BaseInputsContent2

    role: BaseInputsRoleUnion

    phase: OptionalNullable[BaseInputsPhaseUnion] = UNSET

    type: Optional[BaseInputsType] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["phase", "type"]
        nullable_fields = ["phase"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


BaseInputsUnion1TypedDict = TypeAliasType(
    "BaseInputsUnion1TypedDict",
    Union[
        BaseInputsMessageTypedDict,
        OpenAIResponseInputMessageItemTypedDict,
        OutputItemImageGenerationCallTypedDict,
        OpenAIResponseCustomToolCallOutputTypedDict,
        OpenAIResponseFunctionToolCallOutputTypedDict,
        ApplyPatchCallItemTypedDict,
        ApplyPatchCallOutputItemTypedDict,
        OutputMessageTypedDict,
        OpenAIResponseCustomToolCallTypedDict,
        OpenAIResponseFunctionToolCallTypedDict,
    ],
)


BaseInputsUnion1 = TypeAliasType(
    "BaseInputsUnion1",
    Union[
        BaseInputsMessage,
        OpenAIResponseInputMessageItem,
        OutputItemImageGenerationCall,
        OpenAIResponseCustomToolCallOutput,
        OpenAIResponseFunctionToolCallOutput,
        ApplyPatchCallItem,
        ApplyPatchCallOutputItem,
        OutputMessage,
        OpenAIResponseCustomToolCall,
        OpenAIResponseFunctionToolCall,
    ],
)


BaseInputsUnionTypedDict = TypeAliasType(
    "BaseInputsUnionTypedDict", Union[str, List[BaseInputsUnion1TypedDict], Any]
)


BaseInputsUnion = TypeAliasType(
    "BaseInputsUnion", Union[str, List[BaseInputsUnion1], Any]
)
