
    ¿#j                        U d dl mZmZ d dlmZ d dlmZmZmZm	Z	 d dl
mZ dddiddiddidd d	d
dZeeef   ed<    G d de      Zdedz  dededz  fdZdedz  dedz  fdZdedz  dedz  fdZ G d de      Zdddddddeedz   ed      f   deedz   ed      f   deedz   ed      f   deedz   ed       f   d!eedz   ed"      f   defd#Zd$Zd%Zeed&<   y)'    )	AnnotatedAny)Doc)AfterValidator	BaseModelFieldmodel_validator)StreamingResponseobjecttypestringinteger)r   minimum)dataeventidretry)r   
properties_SSE_EVENT_SCHEMAc                       e Zd ZdZdZy)EventSourceResponsea  Streaming response with `text/event-stream` media type.

    Use as `response_class=EventSourceResponse` on a *path operation* that uses `yield`
    to enable Server Sent Events (SSE) responses.

    Works with **any HTTP method** (`GET`, `POST`, etc.), which makes it compatible
    with protocols like MCP that stream SSE over `POST`.

    The actual encoding logic lives in the FastAPI routing layer. This class
    serves mainly as a marker and sets the correct `Content-Type`.
    ztext/event-streamN)__name__
__module____qualname____doc__
media_type     a/home/mi/.openclaw/workspace/fridge-app/backend/.venv/lib/python3.12/site-packages/fastapi/sse.pyr   r      s    
 %Jr   r   vN
field_namereturnc                 8    | d| v sd| v rt        d| d      | S )N
zSSE 'z' must be a single line)
ValueError)r    r!   s     r   _check_single_liner'   $   s-    }$!)tqy5,CDEEHr   c                     t        | d      S )Nr   )r'   r    s    r   _check_event_single_liner*   *   s    a))r   c                 <    | d| v rt        d      t        | d      S )N z)SSE 'id' must not contain null charactersr   )r&   r'   r)   s    r   _check_id_validr-   .   s&    }DEEa&&r   c                   T   e Zd ZU dZdZee ed      f   ed<   dZ	ee
dz   ed      f   ed<   dZee
dz   ee       ed      f   ed<   dZee
dz   ee       ed	      f   ed
<   dZeedz   ed       ed      f   ed<   dZee
dz   ed      f   ed<    ed      dd       Zy)ServerSentEventat  Represents a single Server-Sent Event.

    When `yield`ed from a *path operation function* that uses
    `response_class=EventSourceResponse`, each `ServerSentEvent` is encoded
    into the [SSE wire format](https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream)
    (`text/event-stream`).

    If you yield a plain object (dict, Pydantic model, etc.) instead, it is
    automatically JSON-encoded and sent as the `data:` field.

    All `data` values **including plain strings** are JSON-serialized.

    For example, `data="hello"` produces `data: "hello"` on the wire (with
    quotes).
    NaB  
            The event payload.

            Can be any JSON-serializable value: a Pydantic model, dict, list,
            string, number, etc. It is **always** serialized to JSON: strings
            are quoted (`"hello"` becomes `data: "hello"` on the wire).

            Mutually exclusive with `raw_data`.
            r   aQ  
            Raw string to send as the `data:` field **without** JSON encoding.

            Use this when you need to send pre-formatted text, HTML fragments,
            CSV lines, or any non-JSON payload. The string is placed directly
            into the `data:` field as-is.

            Mutually exclusive with `data`.
            raw_dataz
            Optional event type name.

            Maps to `addEventListener(event, ...)` on the browser. When omitted,
            the browser dispatches on the generic `message` event. Must be a
            single line.
            r   z
            Optional event ID.

            The browser sends this value back as the `Last-Event-ID` header on
            automatic reconnection. **Must be a single line** and must not contain
            null (`\0`) characters.
            r   r   )gez
            Optional reconnection time in **milliseconds**.

            Tells the browser how long to wait before reconnecting after the
            connection is lost. Must be a non-negative integer.
            r   z
            Optional comment line(s).

            Comment lines start with `:` in the SSE wire format and are ignored by
            `EventSource` clients. Useful for keep-alive pings to prevent
            proxy/load-balancer timeouts.
            commentafter)modec                 L    | j                   | j                  t        d      | S )NzCannot set both 'data' and 'raw_data' on the same ServerSentEvent. Use 'data' for JSON-serialized payloads or 'raw_data' for pre-formatted strings.)r   r0   r&   )selfs    r   _check_data_exclusivez%ServerSentEvent._check_data_exclusive   s.    99 T]]%>; 
 r   )r"   r/   )r   r   r   r   r   r   r   r   __annotations__r0   strr   r   r*   r   r-   r   intr   r2   r	   r7   r   r   r   r/   r/   4   sN   : 	 	)
	
	 6 	 id

	
	 4 	 
9d
/0	

	 2 	 	d
'	

		 0 	 
9d
	
		 . 	 Yd
	
		  '" #r   r/   )data_strr   r   r   r2   r;   zQ
            Pre-serialized data string to use as the `data:` field.
            r   zD
            Optional event type name (`event:` field).
            r   z:
            Optional event ID (`id:` field).
            r   zV
            Optional reconnection time in milliseconds (`retry:` field).
            r2   z@
            Optional comment line(s) (`:` prefix).
            c                    g }|)|j                         D ]  }|j                  d|         ||j                  d|        | )| j                         D ]  }|j                  d|         ||j                  d|        ||j                  d|        |j                  d       |j                  d       dj                  |      j                  d      S )	z|Build SSE wire-format bytes from **pre-serialized** data.

    The result always ends with `

` (the event terminator).
    z: zevent: zdata: zid: zretry:  r%   zutf-8)
splitlinesappendjoinencode)r;   r   r   r   r2   lineslines          r   format_sse_eventrD      s    ^ E&&( 	&DLL2dV%	& wug&''') 	*DLL6$)	* 
~tB4[!wug&'	LL	LL99U""7++r   s   : ping

g      .@_PING_INTERVAL)typingr   r   annotated_docr   pydanticr   r   r   r	   starlette.responsesr
   r   dictr9   r8   r   r'   r*   r-   r/   r:   bytesrD   KEEPALIVE_COMMENTrE   floatr   r   r   <module>rN      s   ! !  F F 1
 "(#x #2	% 4S> %+ % #* # #* *d
 *sTz *'sTz 'cDj 'hi hh 	 	 	 	 	SD,d
	
	D, d
	
	D,$ 	d
	
		%D,4 d
	
	5D,D d
	
	ED,T UD,P "   r   