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

from __future__ import annotations
from opperai.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from opperai.utils import eventstreaming
from pydantic import SkipValidation, model_serializer
from typing import Dict, List, Optional, Union
from typing_extensions import NotRequired, TypeAliasType, TypedDict


DeltaTypedDict = TypeAliasType("DeltaTypedDict", Union[str, int, float, bool])
r"""Incremental content for streaming. Used for both unstructured text streaming (when no output_schema) and structured streaming (when output_schema is provided). For structured streaming, contains actual field values being streamed to the json_path location. Supports all JSON types: strings, numbers, booleans."""


Delta = TypeAliasType("Delta", Union[str, int, float, bool])
r"""Incremental content for streaming. Used for both unstructured text streaming (when no output_schema) and structured streaming (when output_schema is provided). For structured streaming, contains actual field values being streamed to the json_path location. Supports all JSON types: strings, numbers, booleans."""


class StreamingChunkTypedDict(TypedDict):
    r"""Represents the data payload within a Server-Sent Event for streaming function execution.

    This model contains the JSON content sent in the 'data' field of each SSE event.
    The fields present depend on the streaming mode:

    **Text Mode (no output_schema):**
    - Uses `delta` field for incremental text content
    - `chunk_type` will be \"text\"

    **Structured Mode (with output_schema):**
    - Uses `delta` and `json_path` for precise field tracking
    - Enables real-time UI updates by showing which schema field is being populated
    - `chunk_type` will be \"json\"

    **JSON Path Examples:**
    - `response.summary` - Top-level string field
    - `response.people[0].name` - Name of first person in array
    - `response.metadata.created_at` - Nested object field
    """

    delta: NotRequired[Nullable[DeltaTypedDict]]
    r"""Incremental content for streaming. Used for both unstructured text streaming (when no output_schema) and structured streaming (when output_schema is provided). For structured streaming, contains actual field values being streamed to the json_path location. Supports all JSON types: strings, numbers, booleans."""
    json_path: NotRequired[Nullable[str]]
    r"""Dot-notation path showing exactly which field in your output_schema is being populated. Enables precise UI updates by routing content to specific components. Format: field[index].nested_field"""
    span_id: NotRequired[Nullable[str]]
    r"""Unique identifier for the execution span, included in the first streaming chunk for tracing"""
    chunk_type: NotRequired[Nullable[str]]
    r"""Indicates the streaming mode: 'text' for unstructured streaming, 'json' for structured streaming with output_schema, 'error' for error events. Only present when delta content is included."""
    error_type: NotRequired[Nullable[str]]
    r"""Error type when chunk_type is 'error'"""
    error_message: NotRequired[Nullable[str]]
    r"""Error message when chunk_type is 'error'"""


class StreamingChunk(BaseModel):
    r"""Represents the data payload within a Server-Sent Event for streaming function execution.

    This model contains the JSON content sent in the 'data' field of each SSE event.
    The fields present depend on the streaming mode:

    **Text Mode (no output_schema):**
    - Uses `delta` field for incremental text content
    - `chunk_type` will be \"text\"

    **Structured Mode (with output_schema):**
    - Uses `delta` and `json_path` for precise field tracking
    - Enables real-time UI updates by showing which schema field is being populated
    - `chunk_type` will be \"json\"

    **JSON Path Examples:**
    - `response.summary` - Top-level string field
    - `response.people[0].name` - Name of first person in array
    - `response.metadata.created_at` - Nested object field
    """

    delta: OptionalNullable[Delta] = UNSET
    r"""Incremental content for streaming. Used for both unstructured text streaming (when no output_schema) and structured streaming (when output_schema is provided). For structured streaming, contains actual field values being streamed to the json_path location. Supports all JSON types: strings, numbers, booleans."""

    json_path: OptionalNullable[str] = UNSET
    r"""Dot-notation path showing exactly which field in your output_schema is being populated. Enables precise UI updates by routing content to specific components. Format: field[index].nested_field"""

    span_id: OptionalNullable[str] = UNSET
    r"""Unique identifier for the execution span, included in the first streaming chunk for tracing"""

    chunk_type: OptionalNullable[str] = UNSET
    r"""Indicates the streaming mode: 'text' for unstructured streaming, 'json' for structured streaming with output_schema, 'error' for error events. Only present when delta content is included."""

    error_type: OptionalNullable[str] = UNSET
    r"""Error type when chunk_type is 'error'"""

    error_message: OptionalNullable[str] = UNSET
    r"""Error message when chunk_type is 'error'"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "delta",
            "json_path",
            "span_id",
            "chunk_type",
            "error_type",
            "error_message",
        ]
        nullable_fields = [
            "delta",
            "json_path",
            "span_id",
            "chunk_type",
            "error_type",
            "error_message",
        ]
        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


class FunctionStreamCallStreamPostResponseBodyTypedDict(TypedDict):
    r"""Server-Sent Event following the SSE specification"""

    data: StreamingChunkTypedDict
    r"""Represents the data payload within a Server-Sent Event for streaming function execution.

    This model contains the JSON content sent in the 'data' field of each SSE event.
    The fields present depend on the streaming mode:

    **Text Mode (no output_schema):**
    - Uses `delta` field for incremental text content
    - `chunk_type` will be \"text\" 

    **Structured Mode (with output_schema):**
    - Uses `delta` and `json_path` for precise field tracking
    - Enables real-time UI updates by showing which schema field is being populated
    - `chunk_type` will be \"json\" 

    **JSON Path Examples:**
    - `response.summary` - Top-level string field
    - `response.people[0].name` - Name of first person in array
    - `response.metadata.created_at` - Nested object field
    """
    id: NotRequired[str]
    r"""Event ID for the SSE event"""
    event: NotRequired[str]
    r"""Event type for the SSE event"""
    retry: NotRequired[int]
    r"""Retry interval in milliseconds for the SSE connection"""


class FunctionStreamCallStreamPostResponseBody(BaseModel):
    r"""Server-Sent Event following the SSE specification"""

    data: StreamingChunk
    r"""Represents the data payload within a Server-Sent Event for streaming function execution.

    This model contains the JSON content sent in the 'data' field of each SSE event.
    The fields present depend on the streaming mode:

    **Text Mode (no output_schema):**
    - Uses `delta` field for incremental text content
    - `chunk_type` will be \"text\" 

    **Structured Mode (with output_schema):**
    - Uses `delta` and `json_path` for precise field tracking
    - Enables real-time UI updates by showing which schema field is being populated
    - `chunk_type` will be \"json\" 

    **JSON Path Examples:**
    - `response.summary` - Top-level string field
    - `response.people[0].name` - Name of first person in array
    - `response.metadata.created_at` - Nested object field
    """

    id: Optional[str] = None
    r"""Event ID for the SSE event"""

    event: Optional[str] = None
    r"""Event type for the SSE event"""

    retry: Optional[int] = None
    r"""Retry interval in milliseconds for the SSE connection"""


class FunctionStreamCallStreamPostResponseTypedDict(TypedDict):
    headers: Dict[str, List[str]]
    result: Union[
        eventstreaming.EventStream[FunctionStreamCallStreamPostResponseBodyTypedDict],
        eventstreaming.EventStreamAsync[
            FunctionStreamCallStreamPostResponseBodyTypedDict
        ],
    ]


class FunctionStreamCallStreamPostResponse(BaseModel):
    headers: Dict[str, List[str]]

    result: SkipValidation[
        Union[
            eventstreaming.EventStream[FunctionStreamCallStreamPostResponseBody],
            eventstreaming.EventStreamAsync[FunctionStreamCallStreamPostResponseBody],
        ]
    ]
