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

from __future__ import annotations
from .messagecontent import MessageContent, MessageContentTypedDict
from .toolcall import ToolCall, ToolCallTypedDict
from pydantic import model_serializer
from r9s.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from typing import List, Literal, Optional, Union
from typing_extensions import NotRequired, TypeAliasType, TypedDict


Role = Literal[
    "system",
    "user",
    "assistant",
    "tool",
]
r"""Message role"""


ContentTypedDict = TypeAliasType(
    "ContentTypedDict", Union[str, List[MessageContentTypedDict]]
)
r"""Message content. Can be null when assistant message contains tool_calls.
- user/system messages: Required, contains text or multimodal content
- assistant messages: Optional when tool_calls is present; can be null or omitted
- tool messages: Required, contains tool return results (usually JSON string)

**Important:** In /v1/responses API, content field must exist and cannot be null.
For /v1/chat/completions, content can be null when tool_calls is present.

"""


Content = TypeAliasType("Content", Union[str, List[MessageContent]])
r"""Message content. Can be null when assistant message contains tool_calls.
- user/system messages: Required, contains text or multimodal content
- assistant messages: Optional when tool_calls is present; can be null or omitted
- tool messages: Required, contains tool return results (usually JSON string)

**Important:** In /v1/responses API, content field must exist and cannot be null.
For /v1/chat/completions, content can be null when tool_calls is present.

"""


class MessageTypedDict(TypedDict):
    r"""Message object. Note: Different API endpoints have different field requirements and support.

    **Field support:**
    - /v1/chat/completions: Supports all fields (tool_calls, tool_call_id, etc.)
    - /v1/responses: Only supports basic fields (role, content, name); does not support tool_calls and tool_call_id

    **content field requirements:**
    - /v1/chat/completions: When tool_calls is present, content can be omitted or null
    - /v1/responses: content field is always required and cannot be null; tool_calls field is not supported

    """

    role: Role
    r"""Message role"""
    content: NotRequired[Nullable[ContentTypedDict]]
    r"""Message content. Can be null when assistant message contains tool_calls.
    - user/system messages: Required, contains text or multimodal content
    - assistant messages: Optional when tool_calls is present; can be null or omitted
    - tool messages: Required, contains tool return results (usually JSON string)

    **Important:** In /v1/responses API, content field must exist and cannot be null.
    For /v1/chat/completions, content can be null when tool_calls is present.

    """
    name: NotRequired[str]
    r"""Sender name"""
    reasoning_content: NotRequired[str]
    r"""Reasoning content"""
    tool_calls: NotRequired[List[ToolCallTypedDict]]
    r"""Tool calls list"""
    tool_call_id: NotRequired[str]
    r"""Tool call ID"""


class Message(BaseModel):
    r"""Message object. Note: Different API endpoints have different field requirements and support.

    **Field support:**
    - /v1/chat/completions: Supports all fields (tool_calls, tool_call_id, etc.)
    - /v1/responses: Only supports basic fields (role, content, name); does not support tool_calls and tool_call_id

    **content field requirements:**
    - /v1/chat/completions: When tool_calls is present, content can be omitted or null
    - /v1/responses: content field is always required and cannot be null; tool_calls field is not supported

    """

    role: Role
    r"""Message role"""

    content: OptionalNullable[Content] = UNSET
    r"""Message content. Can be null when assistant message contains tool_calls.
    - user/system messages: Required, contains text or multimodal content
    - assistant messages: Optional when tool_calls is present; can be null or omitted
    - tool messages: Required, contains tool return results (usually JSON string)

    **Important:** In /v1/responses API, content field must exist and cannot be null.
    For /v1/chat/completions, content can be null when tool_calls is present.

    """

    name: Optional[str] = None
    r"""Sender name"""

    reasoning_content: Optional[str] = None
    r"""Reasoning content"""

    tool_calls: Optional[List[ToolCall]] = None
    r"""Tool calls list"""

    tool_call_id: Optional[str] = None
    r"""Tool call ID"""

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