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

from __future__ import annotations
from .message import Message, MessageTypedDict
from .tool import Tool, ToolTypedDict
from friendli.core.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Any, Dict, List
from typing_extensions import NotRequired, TypedDict


class ServerlessChatRenderBodyTypedDict(TypedDict):
    model: str
    "Code of the model to use. See [available model list](https://friendli.ai/docs/guides/serverless_endpoints/pricing#billing-methods)."
    messages: List[MessageTypedDict]
    "A list of messages comprising the conversation so far."
    chat_template_kwargs: NotRequired[Nullable[Dict[str, Any]]]
    "Additional keyword arguments supplied to the template renderer. These parameters will be available for use within the chat template."
    tools: NotRequired[Nullable[List[ToolTypedDict]]]
    "A list of tools the model may call.\n    Use this to provide a list of functions the model may generate JSON inputs for.\n\n    **When `tools` is specified, `min_tokens` and `response_format` fields are unsupported.**\n    "


class ServerlessChatRenderBody(BaseModel):
    model: str
    "Code of the model to use. See [available model list](https://friendli.ai/docs/guides/serverless_endpoints/pricing#billing-methods)."
    messages: List[Message]
    "A list of messages comprising the conversation so far."
    chat_template_kwargs: OptionalNullable[Dict[str, Any]] = UNSET
    "Additional keyword arguments supplied to the template renderer. These parameters will be available for use within the chat template."
    tools: OptionalNullable[List[Tool]] = UNSET
    "A list of tools the model may call.\n    Use this to provide a list of functions the model may generate JSON inputs for.\n\n    **When `tools` is specified, `min_tokens` and `response_format` fields are unsupported.**\n    "

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["chat_template_kwargs", "tools"]
        nullable_fields = ["chat_template_kwargs", "tools"]
        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
            )
            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                k not in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val
        return m
