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

from __future__ import annotations
from .chatassistantimages import ChatAssistantImages, ChatAssistantImagesTypedDict
from .chataudiooutput import ChatAudioOutput, ChatAudioOutputTypedDict
from .chatcontentitems import ChatContentItems, ChatContentItemsTypedDict
from .chattoolcall import ChatToolCall, ChatToolCallTypedDict
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


ChatAssistantMessageRole = Literal["assistant",]


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


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


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

    role: ChatAssistantMessageRole
    content: NotRequired[Nullable[ChatAssistantMessageContentTypedDict]]
    r"""Assistant message content"""
    name: NotRequired[str]
    r"""Optional name for the assistant"""
    tool_calls: NotRequired[List[ChatToolCallTypedDict]]
    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[ChatAssistantImagesTypedDict]]
    r"""Generated images from image generation models"""
    audio: NotRequired[ChatAudioOutputTypedDict]
    r"""Audio output data or reference"""


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

    role: ChatAssistantMessageRole

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

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

    tool_calls: Optional[List[ChatToolCall]] = 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[ChatAssistantImages]] = None
    r"""Generated images from image generation models"""

    audio: Optional[ChatAudioOutput] = 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
