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

from __future__ import annotations
from .assistantmessageimages import (
    AssistantMessageImages,
    AssistantMessageImagesTypedDict,
)
from .chatcompletionaudiooutput import (
    ChatCompletionAudioOutput,
    ChatCompletionAudioOutputTypedDict,
)
from .chatmessagecontentitem import (
    ChatMessageContentItem,
    ChatMessageContentItemTypedDict,
)
from .chatmessagetoolcall import ChatMessageToolCall, ChatMessageToolCallTypedDict
from .reasoningdetailunion import ReasoningDetailUnion, ReasoningDetailUnionTypedDict
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Any, List, Literal, Optional, Union
from typing_extensions import NotRequired, TypeAliasType, TypedDict


AssistantMessageRole = Literal["assistant",]


AssistantMessageContentTypedDict = TypeAliasType(
    "AssistantMessageContentTypedDict",
    Union[str, List[ChatMessageContentItemTypedDict], Any],
)
r"""Assistant message content"""


AssistantMessageContent = TypeAliasType(
    "AssistantMessageContent", Union[str, List[ChatMessageContentItem], Any]
)
r"""Assistant message content"""


class AssistantMessageTypedDict(TypedDict):
    r"""Assistant message for requests and responses"""

    role: AssistantMessageRole
    content: NotRequired[Nullable[AssistantMessageContentTypedDict]]
    r"""Assistant message content"""
    name: NotRequired[str]
    r"""Optional name for the assistant"""
    tool_calls: NotRequired[List[ChatMessageToolCallTypedDict]]
    r"""Tool calls made by the assistant"""
    refusal: NotRequired[Nullable[str]]
    r"""Refusal message if content was refused"""
    reasoning: NotRequired[Nullable[str]]
    r"""Reasoning output"""
    reasoning_details: NotRequired[List[ReasoningDetailUnionTypedDict]]
    r"""Reasoning details for extended thinking models"""
    images: NotRequired[List[AssistantMessageImagesTypedDict]]
    r"""Generated images from image generation models"""
    audio: NotRequired[ChatCompletionAudioOutputTypedDict]
    r"""Audio output data or reference"""


class AssistantMessage(BaseModel):
    r"""Assistant message for requests and responses"""

    role: AssistantMessageRole

    content: OptionalNullable[AssistantMessageContent] = UNSET
    r"""Assistant message content"""

    name: Optional[str] = None
    r"""Optional name for the assistant"""

    tool_calls: Optional[List[ChatMessageToolCall]] = None
    r"""Tool calls made by the assistant"""

    refusal: OptionalNullable[str] = UNSET
    r"""Refusal message if content was refused"""

    reasoning: OptionalNullable[str] = UNSET
    r"""Reasoning output"""

    reasoning_details: Optional[List[ReasoningDetailUnion]] = None
    r"""Reasoning details for extended thinking models"""

    images: Optional[List[AssistantMessageImages]] = None
    r"""Generated images from image generation models"""

    audio: Optional[ChatCompletionAudioOutput] = None
    r"""Audio output data or reference"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "content",
            "name",
            "tool_calls",
            "refusal",
            "reasoning",
            "reasoning_details",
            "images",
            "audio",
        ]
        nullable_fields = ["content", "refusal", "reasoning"]
        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
