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

from __future__ import annotations
from .openairesponsesrefusalcontent import (
    OpenAIResponsesRefusalContent,
    OpenAIResponsesRefusalContentTypedDict,
)
from .responseoutputtext import ResponseOutputText, ResponseOutputTextTypedDict
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


OutputMessageItemContentTypedDict = TypeAliasType(
    "OutputMessageItemContentTypedDict",
    Union[OpenAIResponsesRefusalContentTypedDict, ResponseOutputTextTypedDict],
)


OutputMessageItemContent = Annotated[
    Union[
        Annotated[ResponseOutputText, Tag("output_text")],
        Annotated[OpenAIResponsesRefusalContent, Tag("refusal")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


OutputMessageItemPhaseFinalAnswer = Literal["final_answer",]


OutputMessageItemPhaseCommentary = Literal["commentary",]


OutputMessageItemPhaseUnionTypedDict = TypeAliasType(
    "OutputMessageItemPhaseUnionTypedDict",
    Union[OutputMessageItemPhaseCommentary, OutputMessageItemPhaseFinalAnswer, Any],
)
r"""The phase of an assistant message. Use `commentary` for an intermediate assistant message and `final_answer` for the final assistant message. For follow-up requests with models like `gpt-5.3-codex` and later, preserve and resend phase on all assistant messages. Omitting it can degrade performance. Not used for user messages."""


OutputMessageItemPhaseUnion = TypeAliasType(
    "OutputMessageItemPhaseUnion",
    Union[OutputMessageItemPhaseCommentary, OutputMessageItemPhaseFinalAnswer, Any],
)
r"""The phase of an assistant message. Use `commentary` for an intermediate assistant message and `final_answer` for the final assistant message. For follow-up requests with models like `gpt-5.3-codex` and later, preserve and resend phase on all assistant messages. Omitting it can degrade performance. Not used for user messages."""


OutputMessageItemRole = Literal["assistant",]


OutputMessageItemStatusInProgress = Literal["in_progress",]


OutputMessageItemStatusIncomplete = Literal["incomplete",]


OutputMessageItemStatusCompleted = Literal["completed",]


OutputMessageItemStatusUnionTypedDict = TypeAliasType(
    "OutputMessageItemStatusUnionTypedDict",
    Union[
        OutputMessageItemStatusCompleted,
        OutputMessageItemStatusIncomplete,
        OutputMessageItemStatusInProgress,
    ],
)


OutputMessageItemStatusUnion = TypeAliasType(
    "OutputMessageItemStatusUnion",
    Union[
        OutputMessageItemStatusCompleted,
        OutputMessageItemStatusIncomplete,
        OutputMessageItemStatusInProgress,
    ],
)


OutputMessageItemType = Literal["message",]


class OutputMessageItemTypedDict(TypedDict):
    r"""An output message item"""

    content: List[OutputMessageItemContentTypedDict]
    id: str
    role: OutputMessageItemRole
    type: OutputMessageItemType
    phase: NotRequired[Nullable[OutputMessageItemPhaseUnionTypedDict]]
    r"""The phase of an assistant message. Use `commentary` for an intermediate assistant message and `final_answer` for the final assistant message. For follow-up requests with models like `gpt-5.3-codex` and later, preserve and resend phase on all assistant messages. Omitting it can degrade performance. Not used for user messages."""
    status: NotRequired[OutputMessageItemStatusUnionTypedDict]


class OutputMessageItem(BaseModel):
    r"""An output message item"""

    content: List[OutputMessageItemContent]

    id: str

    role: OutputMessageItemRole

    type: OutputMessageItemType

    phase: OptionalNullable[OutputMessageItemPhaseUnion] = UNSET
    r"""The phase of an assistant message. Use `commentary` for an intermediate assistant message and `final_answer` for the final assistant message. For follow-up requests with models like `gpt-5.3-codex` and later, preserve and resend phase on all assistant messages. Omitting it can degrade performance. Not used for user messages."""

    status: Optional[OutputMessageItemStatusUnion] = None

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["phase", "status"]
        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
