Ë
    uPêjY  ã                   ó|   — d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
  G d„ de«      Zde
de	d	z  fd
„Z G d„ d«      Zy	)zy
Streaming utilities for ChatGPT provider.

Normalizes non-spec-compliant tool_call chunks from the ChatGPT backend API.
é    )Ú	Awaitable)ÚFinalÚProtocol)Ú!ChatCompletionDeltaCustomToolCallÚChatCompletionDeltaToolCallÚDeltaÚModelResponseStreamc                   ó.   — e Zd ZdZdefd„Zdee   fd„Zy)ÚChatGPTChunkStreamzEA ChatGPT chunk source driven either synchronously or asynchronously.Úreturnc                  ó   — y ©N© ©Úselfs    ún/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/chatgpt/chat/streaming_utils.pyÚ__next__zChatGPTChunkStream.__next__   ó   � ó    c                  ó   — y r   r   r   s    r   Ú	__anext__zChatGPTChunkStream.__anext__   r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r      s   „ ÙOà2Ð-Ó2à>˜9Ð%8Ñ9Ô>r   r   Úchunkr   Nc                 ó4   — | j                   d   j                  S ©Nr   )ÚchoicesÚdelta)r   s    r   Ú_first_choice_deltar!      s   € Ø�=‰=˜Ñ×!Ñ!Ð!r   c                   óf   — e Zd ZdZdefd„Zdedefd„Zd„ Z	d„ Z
defd	„Zdefd
„Zdededz  fd„Zy)ÚChatGPTToolCallNormalizera¸  
    Wraps a streaming response and fixes tool_call index/dedup issues.

    The ChatGPT backend API (chatgpt.com/backend-api) sends non-spec-compliant
    streaming tool call chunks:
    1. `index` is always 0, even for multiple parallel tool calls
    2. `id` and `name` get repeated in "closing" chunks that shouldn't exist

    This wrapper normalizes the stream to match the OpenAI spec before yielding
    chunks to the consumer.
    Ústreamc                 ó<   — || _         i | _        d| _        d | _        y r   )Ú_streamÚ	_seen_idsÚ_next_indexÚ_last_id)r   r$   s     r   Ú__init__z"ChatGPTToolCallNormalizer.__init__+   s   € Ø$ˆŒØ)+ˆŒØ !ˆÔØ$(ˆ�r   Únamer   c                 ó.   — t        | j                  |«      S r   )Úgetattrr&   )r   r+   s     r   Ú__getattr__z%ChatGPTToolCallNormalizer.__getattr__1   s   € Ü�t—|‘| TÓ*Ð*r   c                 ó   — | S r   r   r   s    r   Ú__iter__z"ChatGPTToolCallNormalizer.__iter__4   ó   € Øˆr   c                 ó   — | S r   r   r   s    r   Ú	__aiter__z#ChatGPTToolCallNormalizer.__aiter__7   r1   r   c                 óZ   — 	 t        | j                  «      }| j                  |«      }|�|S Œ+r   )Únextr&   Ú
_normalize©r   r   Úresults      r   r   z"ChatGPTToolCallNormalizer.__next__:   s1   € ØÜ˜Ÿ™Ó&ˆEØ—_‘_ UÓ+ˆFØÐ!Ø�ð	 r   c              ƒ   ó€   K  — 	 | j                   j                  «       ƒ d {  –—† }| j                  |«      }|�|S Œ87 Œ­wr   )r&   r   r6   r7   s      r   r   z#ChatGPTToolCallNormalizer.__anext__A   s?   è ø€ ØØŸ,™,×0Ñ0Ó2×2ˆEØ—_‘_ UÓ+ˆFØÐ!Ø�ð	 Ø2ús   ‚>¡<¢>r   Nc                 ór  — |j                   s|S t        |«      }|�|j                  s|S g }|j                  D ]ò  }|j                  r„|j                  | j                  vrl| j
                  | j                  |j                  <   | j
                  |_        |j                  | _        | xj
                  dz  c_        |j                  |«       Œ“|j                  r|j                  | j                  v rŒ¸| j                  r| j                  | j                     |_        |j                  |«       Œô |sy||_        |S )zCFix tool_calls in the chunk. Returns None to skip duplicate chunks.Né   )	r   r!   Ú
tool_callsÚidr'   r(   Úindexr)   Úappend)r   r   r    Ú
normalizedÚtcs        r   r6   z$ChatGPTToolCallNormalizer._normalizeH   s  € à�}Š}ØˆLä*¨5Ó1ˆØˆ= × 0Ò 0ØˆLàceˆ
Ø×"Ñ"ò 	&ˆBØ�uŠu˜Ÿ™ d§n¡nÑ4à(,×(8Ñ(8�—‘˜rŸu™uÑ%Ø×+Ñ+�”Ø "§¡�”Ø× Ò  AÑ%Õ Ø×!Ñ! "Õ%Ø—’˜2Ÿ5™5 D§N¡NÑ2àð —=’=Ø#Ÿ~™~¨d¯m©mÑ<�B”HØ×!Ñ! "Õ%ð	&ñ" Øà%ˆÔØˆr   )r   r   r   r   r   r*   ÚstrÚobjectr.   r0   r3   r	   r   r   r6   r   r   r   r#   r#      se   „ ñ
ð)Ð1ó )ð+ ð +¨ó +òòðÐ-ó ðÐ!4ó ðÐ 3ð Ð8KÈdÑ8Rô r   r#   )r   Úcollections.abcr   Útypingr   r   Úlitellm.types.utilsr   r   r   r	   r   r!   r#   r   r   r   ú<module>rG      sL   ðñõ &ß "÷ó ô?˜ô ?ð"Ð2ð "°u¸t±|ó "÷Iò Ir   