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

from __future__ import annotations
from .advisorservertool_openrouter import (
    AdvisorServerToolOpenRouter,
    AdvisorServerToolOpenRouterTypedDict,
)
from .anthropiccachecontroldirective import (
    AnthropicCacheControlDirective,
    AnthropicCacheControlDirectiveTypedDict,
)
from .applypatchservertool import ApplyPatchServerTool, ApplyPatchServerToolTypedDict
from .applypatchservertool_openrouter import (
    ApplyPatchServerToolOpenRouter,
    ApplyPatchServerToolOpenRouterTypedDict,
)
from .autorouterplugin import AutoRouterPlugin, AutoRouterPluginTypedDict
from .bashservertool import BashServerTool, BashServerToolTypedDict
from .chatsearchmodelsservertool import (
    ChatSearchModelsServerTool,
    ChatSearchModelsServerToolTypedDict,
)
from .codeinterpreterservertool import (
    CodeInterpreterServerTool,
    CodeInterpreterServerToolTypedDict,
)
from .codexlocalshelltool import CodexLocalShellTool, CodexLocalShellToolTypedDict
from .computeruseservertool import ComputerUseServerTool, ComputerUseServerToolTypedDict
from .contextcompressionplugin import (
    ContextCompressionPlugin,
    ContextCompressionPluginTypedDict,
)
from .customtool import CustomTool, CustomToolTypedDict
from .datetimeservertool import DatetimeServerTool, DatetimeServerToolTypedDict
from .fileparserplugin import FileParserPlugin, FileParserPluginTypedDict
from .filesearchservertool import FileSearchServerTool, FileSearchServerToolTypedDict
from .fusionplugin import FusionPlugin, FusionPluginTypedDict
from .fusionservertool_openrouter import (
    FusionServerToolOpenRouter,
    FusionServerToolOpenRouterTypedDict,
)
from .imageconfig import ImageConfig, ImageConfigTypedDict
from .imagegenerationservertool import (
    ImageGenerationServerTool,
    ImageGenerationServerToolTypedDict,
)
from .imagegenerationservertool_openrouter import (
    ImageGenerationServerToolOpenRouter,
    ImageGenerationServerToolOpenRouterTypedDict,
)
from .inputs_union import InputsUnion, InputsUnionTypedDict
from .legacy_websearchservertool import (
    LegacyWebSearchServerTool,
    LegacyWebSearchServerToolTypedDict,
)
from .mcpservertool import McpServerTool, McpServerToolTypedDict
from .moderationplugin import ModerationPlugin, ModerationPluginTypedDict
from .openairesponsestoolchoice_union import (
    OpenAIResponsesToolChoiceUnion,
    OpenAIResponsesToolChoiceUnionTypedDict,
)
from .openairesponsestruncation import OpenAIResponsesTruncation
from .outputmodalityenum import OutputModalityEnum
from .paretorouterplugin import ParetoRouterPlugin, ParetoRouterPluginTypedDict
from .preview_20250311_websearchservertool import (
    Preview20250311WebSearchServerTool,
    Preview20250311WebSearchServerToolTypedDict,
)
from .preview_websearchservertool import (
    PreviewWebSearchServerTool,
    PreviewWebSearchServerToolTypedDict,
)
from .providerpreferences import ProviderPreferences, ProviderPreferencesTypedDict
from .reasoningconfig import ReasoningConfig, ReasoningConfigTypedDict
from .responsehealingplugin import ResponseHealingPlugin, ResponseHealingPluginTypedDict
from .responseincludesenum import ResponseIncludesEnum
from .shellservertool import ShellServerTool, ShellServerToolTypedDict
from .shellservertool_openrouter import (
    ShellServerToolOpenRouter,
    ShellServerToolOpenRouterTypedDict,
)
from .stopservertoolswhencondition import (
    StopServerToolsWhenCondition,
    StopServerToolsWhenConditionTypedDict,
)
from .storedprompttemplate import StoredPromptTemplate, StoredPromptTemplateTypedDict
from .subagentservertool_openrouter import (
    SubagentServerToolOpenRouter,
    SubagentServerToolOpenRouterTypedDict,
)
from .textextendedconfig import TextExtendedConfig, TextExtendedConfigTypedDict
from .traceconfig import TraceConfig, TraceConfigTypedDict
from .webfetchplugin import WebFetchPlugin, WebFetchPluginTypedDict
from .webfetchservertool import WebFetchServerTool, WebFetchServerToolTypedDict
from .websearchplugin import WebSearchPlugin, WebSearchPluginTypedDict
from .websearchservertool import WebSearchServerTool, WebSearchServerToolTypedDict
from .websearchservertool_openrouter import (
    WebSearchServerToolOpenRouter,
    WebSearchServerToolOpenRouterTypedDict,
)
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
    UnrecognizedStr,
)
from openrouter.utils import get_discriminator, validate_const, validate_open_enum
import pydantic
from pydantic import Discriminator, Tag, model_serializer
from pydantic.functional_validators import AfterValidator, PlainValidator
from typing import Any, Dict, List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ResponsesRequestPluginTypedDict = TypeAliasType(
    "ResponsesRequestPluginTypedDict",
    Union[
        ModerationPluginTypedDict,
        ResponseHealingPluginTypedDict,
        FileParserPluginTypedDict,
        ContextCompressionPluginTypedDict,
        ParetoRouterPluginTypedDict,
        AutoRouterPluginTypedDict,
        WebFetchPluginTypedDict,
        FusionPluginTypedDict,
        WebSearchPluginTypedDict,
    ],
)


ResponsesRequestPlugin = Annotated[
    Union[
        Annotated[AutoRouterPlugin, Tag("auto-router")],
        Annotated[ContextCompressionPlugin, Tag("context-compression")],
        Annotated[FileParserPlugin, Tag("file-parser")],
        Annotated[FusionPlugin, Tag("fusion")],
        Annotated[ModerationPlugin, Tag("moderation")],
        Annotated[ParetoRouterPlugin, Tag("pareto-router")],
        Annotated[ResponseHealingPlugin, Tag("response-healing")],
        Annotated[WebSearchPlugin, Tag("web")],
        Annotated[WebFetchPlugin, Tag("web-fetch")],
    ],
    Discriminator(lambda m: get_discriminator(m, "id", "id")),
]


ResponsesRequestServiceTier = Union[
    Literal[
        "auto",
        "default",
        "flex",
        "priority",
        "scale",
    ],
    UnrecognizedStr,
]


ResponsesRequestType = Literal["function",]


class ResponsesRequestToolFunctionTypedDict(TypedDict):
    r"""Function tool definition"""

    name: str
    parameters: Nullable[Dict[str, Nullable[Any]]]
    type: ResponsesRequestType
    description: NotRequired[Nullable[str]]
    strict: NotRequired[Nullable[bool]]


class ResponsesRequestToolFunction(BaseModel):
    r"""Function tool definition"""

    name: str

    parameters: Nullable[Dict[str, Nullable[Any]]]

    type: ResponsesRequestType

    description: OptionalNullable[str] = UNSET

    strict: OptionalNullable[bool] = UNSET

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


ResponsesRequestToolUnionTypedDict = TypeAliasType(
    "ResponsesRequestToolUnionTypedDict",
    Union[
        CodexLocalShellToolTypedDict,
        ApplyPatchServerToolTypedDict,
        ShellServerToolTypedDict,
        ImageGenerationServerToolOpenRouterTypedDict,
        ChatSearchModelsServerToolTypedDict,
        ShellServerToolOpenRouterTypedDict,
        BashServerToolTypedDict,
        CodeInterpreterServerToolTypedDict,
        ApplyPatchServerToolOpenRouterTypedDict,
        WebSearchServerToolOpenRouterTypedDict,
        WebFetchServerToolTypedDict,
        FusionServerToolOpenRouterTypedDict,
        DatetimeServerToolTypedDict,
        SubagentServerToolOpenRouterTypedDict,
        AdvisorServerToolOpenRouterTypedDict,
        CustomToolTypedDict,
        ComputerUseServerToolTypedDict,
        ResponsesRequestToolFunctionTypedDict,
        FileSearchServerToolTypedDict,
        PreviewWebSearchServerToolTypedDict,
        Preview20250311WebSearchServerToolTypedDict,
        WebSearchServerToolTypedDict,
        LegacyWebSearchServerToolTypedDict,
        McpServerToolTypedDict,
        ImageGenerationServerToolTypedDict,
    ],
)


ResponsesRequestToolUnion = Annotated[
    Union[
        Annotated[ResponsesRequestToolFunction, Tag("function")],
        Annotated[PreviewWebSearchServerTool, Tag("web_search_preview")],
        Annotated[
            Preview20250311WebSearchServerTool, Tag("web_search_preview_2025_03_11")
        ],
        Annotated[LegacyWebSearchServerTool, Tag("web_search")],
        Annotated[WebSearchServerTool, Tag("web_search_2025_08_26")],
        Annotated[FileSearchServerTool, Tag("file_search")],
        Annotated[ComputerUseServerTool, Tag("computer_use_preview")],
        Annotated[CodeInterpreterServerTool, Tag("code_interpreter")],
        Annotated[McpServerTool, Tag("mcp")],
        Annotated[ImageGenerationServerTool, Tag("image_generation")],
        Annotated[CodexLocalShellTool, Tag("local_shell")],
        Annotated[ShellServerTool, Tag("shell")],
        Annotated[ApplyPatchServerTool, Tag("apply_patch")],
        Annotated[CustomTool, Tag("custom")],
        Annotated[AdvisorServerToolOpenRouter, Tag("openrouter:advisor")],
        Annotated[SubagentServerToolOpenRouter, Tag("openrouter:subagent")],
        Annotated[DatetimeServerTool, Tag("openrouter:datetime")],
        Annotated[FusionServerToolOpenRouter, Tag("openrouter:fusion")],
        Annotated[
            ImageGenerationServerToolOpenRouter, Tag("openrouter:image_generation")
        ],
        Annotated[
            ChatSearchModelsServerTool, Tag("openrouter:experimental__search_models")
        ],
        Annotated[WebFetchServerTool, Tag("openrouter:web_fetch")],
        Annotated[WebSearchServerToolOpenRouter, Tag("openrouter:web_search")],
        Annotated[ApplyPatchServerToolOpenRouter, Tag("openrouter:apply_patch")],
        Annotated[BashServerTool, Tag("openrouter:bash")],
        Annotated[ShellServerToolOpenRouter, Tag("openrouter:shell")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


class ResponsesRequestTypedDict(TypedDict):
    r"""Request schema for Responses endpoint"""

    background: NotRequired[Nullable[bool]]
    cache_control: NotRequired[AnthropicCacheControlDirectiveTypedDict]
    r"""Enable automatic prompt caching. When set at the top level, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""
    frequency_penalty: NotRequired[Nullable[float]]
    image_config: NotRequired[Dict[str, ImageConfigTypedDict]]
    r"""Provider-specific image configuration options. Keys and values vary by model/provider. See https://openrouter.ai/docs/guides/overview/multimodal/image-generation for more details."""
    include: NotRequired[Nullable[List[ResponseIncludesEnum]]]
    input: NotRequired[InputsUnionTypedDict]
    r"""Input for a response request - can be a string or array of items"""
    instructions: NotRequired[Nullable[str]]
    max_output_tokens: NotRequired[Nullable[int]]
    max_tool_calls: NotRequired[Nullable[int]]
    metadata: NotRequired[Nullable[Dict[str, str]]]
    r"""Metadata key-value pairs for the request. Keys must be ≤64 characters and cannot contain brackets. Values must be ≤512 characters. Maximum 16 pairs allowed."""
    modalities: NotRequired[List[OutputModalityEnum]]
    r"""Output modalities for the response. Supported values are \"text\" and \"image\"."""
    model: NotRequired[str]
    models: NotRequired[List[str]]
    parallel_tool_calls: NotRequired[Nullable[bool]]
    plugins: NotRequired[List[ResponsesRequestPluginTypedDict]]
    r"""Plugins you want to enable for this request, including their settings."""
    presence_penalty: NotRequired[Nullable[float]]
    previous_response_id: NotRequired[Nullable[str]]
    prompt: NotRequired[Nullable[StoredPromptTemplateTypedDict]]
    prompt_cache_key: NotRequired[Nullable[str]]
    provider: NotRequired[Nullable[ProviderPreferencesTypedDict]]
    r"""When multiple model providers are available, optionally indicate your routing preference."""
    reasoning: NotRequired[Nullable[ReasoningConfigTypedDict]]
    r"""Configuration for reasoning mode in the response"""
    safety_identifier: NotRequired[Nullable[str]]
    service_tier: NotRequired[Nullable[ResponsesRequestServiceTier]]
    session_id: NotRequired[str]
    r"""A unique identifier for grouping related requests (e.g., a conversation or agent workflow). When provided, OpenRouter uses it as the sticky routing key, routing all requests in the session to the same provider to maximize prompt cache hits. Also used for observability grouping. If provided in both the request body and the x-session-id header, the body value takes precedence. Maximum of 256 characters."""
    stop_server_tools_when: NotRequired[List[StopServerToolsWhenConditionTypedDict]]
    r"""Stop conditions for the server-tool agent loop. Any condition firing halts the loop (OR logic). When set, this overrides `max_tool_calls`."""
    store: Literal[False]
    stream: NotRequired[bool]
    temperature: NotRequired[Nullable[float]]
    text: NotRequired[TextExtendedConfigTypedDict]
    r"""Text output configuration including format and verbosity"""
    tool_choice: NotRequired[OpenAIResponsesToolChoiceUnionTypedDict]
    tools: NotRequired[List[ResponsesRequestToolUnionTypedDict]]
    top_k: NotRequired[int]
    top_logprobs: NotRequired[Nullable[int]]
    top_p: NotRequired[Nullable[float]]
    trace: NotRequired[TraceConfigTypedDict]
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""
    truncation: NotRequired[Nullable[OpenAIResponsesTruncation]]
    user: NotRequired[str]
    r"""A unique identifier representing your end-user, which helps distinguish between different users of your app. This allows your app to identify specific users in case of abuse reports, preventing your entire app from being affected by the actions of individual users. Maximum of 256 characters."""


class ResponsesRequest(BaseModel):
    r"""Request schema for Responses endpoint"""

    background: OptionalNullable[bool] = UNSET

    cache_control: Optional[AnthropicCacheControlDirective] = None
    r"""Enable automatic prompt caching. When set at the top level, the system automatically applies cache breakpoints to the last cacheable block in the request. Currently supported for Anthropic Claude models."""

    frequency_penalty: OptionalNullable[float] = UNSET

    image_config: Optional[Dict[str, ImageConfig]] = None
    r"""Provider-specific image configuration options. Keys and values vary by model/provider. See https://openrouter.ai/docs/guides/overview/multimodal/image-generation for more details."""

    include: OptionalNullable[
        List[Annotated[ResponseIncludesEnum, PlainValidator(validate_open_enum(False))]]
    ] = UNSET

    input: Optional[InputsUnion] = None
    r"""Input for a response request - can be a string or array of items"""

    instructions: OptionalNullable[str] = UNSET

    max_output_tokens: OptionalNullable[int] = UNSET

    max_tool_calls: OptionalNullable[int] = UNSET

    metadata: OptionalNullable[Dict[str, str]] = UNSET
    r"""Metadata key-value pairs for the request. Keys must be ≤64 characters and cannot contain brackets. Values must be ≤512 characters. Maximum 16 pairs allowed."""

    modalities: Optional[
        List[Annotated[OutputModalityEnum, PlainValidator(validate_open_enum(False))]]
    ] = None
    r"""Output modalities for the response. Supported values are \"text\" and \"image\"."""

    model: Optional[str] = None

    models: Optional[List[str]] = None

    parallel_tool_calls: OptionalNullable[bool] = UNSET

    plugins: Optional[List[ResponsesRequestPlugin]] = None
    r"""Plugins you want to enable for this request, including their settings."""

    presence_penalty: OptionalNullable[float] = UNSET

    previous_response_id: OptionalNullable[str] = UNSET

    prompt: OptionalNullable[StoredPromptTemplate] = UNSET

    prompt_cache_key: OptionalNullable[str] = UNSET

    provider: OptionalNullable[ProviderPreferences] = UNSET
    r"""When multiple model providers are available, optionally indicate your routing preference."""

    reasoning: OptionalNullable[ReasoningConfig] = UNSET
    r"""Configuration for reasoning mode in the response"""

    safety_identifier: OptionalNullable[str] = UNSET

    service_tier: Annotated[
        OptionalNullable[ResponsesRequestServiceTier],
        PlainValidator(validate_open_enum(False)),
    ] = "auto"

    session_id: Optional[str] = None
    r"""A unique identifier for grouping related requests (e.g., a conversation or agent workflow). When provided, OpenRouter uses it as the sticky routing key, routing all requests in the session to the same provider to maximize prompt cache hits. Also used for observability grouping. If provided in both the request body and the x-session-id header, the body value takes precedence. Maximum of 256 characters."""

    stop_server_tools_when: Optional[List[StopServerToolsWhenCondition]] = None
    r"""Stop conditions for the server-tool agent loop. Any condition firing halts the loop (OR logic). When set, this overrides `max_tool_calls`."""

    STORE: Annotated[
        Annotated[Optional[Literal[False]], AfterValidator(validate_const(False))],
        pydantic.Field(alias="store"),
    ] = False

    stream: Optional[bool] = False

    temperature: OptionalNullable[float] = UNSET

    text: Optional[TextExtendedConfig] = None
    r"""Text output configuration including format and verbosity"""

    tool_choice: Optional[OpenAIResponsesToolChoiceUnion] = None

    tools: Optional[List[ResponsesRequestToolUnion]] = None

    top_k: Optional[int] = None

    top_logprobs: OptionalNullable[int] = UNSET

    top_p: OptionalNullable[float] = UNSET

    trace: Optional[TraceConfig] = None
    r"""Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations."""

    truncation: Annotated[
        OptionalNullable[OpenAIResponsesTruncation],
        PlainValidator(validate_open_enum(False)),
    ] = UNSET

    user: Optional[str] = None
    r"""A unique identifier representing your end-user, which helps distinguish between different users of your app. This allows your app to identify specific users in case of abuse reports, preventing your entire app from being affected by the actions of individual users. Maximum of 256 characters."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "background",
            "cache_control",
            "frequency_penalty",
            "image_config",
            "include",
            "input",
            "instructions",
            "max_output_tokens",
            "max_tool_calls",
            "metadata",
            "modalities",
            "model",
            "models",
            "parallel_tool_calls",
            "plugins",
            "presence_penalty",
            "previous_response_id",
            "prompt",
            "prompt_cache_key",
            "provider",
            "reasoning",
            "safety_identifier",
            "service_tier",
            "session_id",
            "stop_server_tools_when",
            "store",
            "stream",
            "temperature",
            "text",
            "tool_choice",
            "tools",
            "top_k",
            "top_logprobs",
            "top_p",
            "trace",
            "truncation",
            "user",
        ]
        nullable_fields = [
            "background",
            "frequency_penalty",
            "include",
            "instructions",
            "max_output_tokens",
            "max_tool_calls",
            "metadata",
            "parallel_tool_calls",
            "presence_penalty",
            "previous_response_id",
            "prompt",
            "prompt_cache_key",
            "provider",
            "reasoning",
            "safety_identifier",
            "service_tier",
            "temperature",
            "top_logprobs",
            "top_p",
            "truncation",
        ]
        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
