Ë
    òÏjû  ã                  ó´   — d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZmZmZ e	rddlmZ dd„Zdd	„Zdd
„Zdd„Zdd„Z	 	 	 	 	 	 	 	 dd„Zdd„Zy)zCConvert `OpenAIDecisions` state to the Decisions API `input` field.é    )ÚannotationsN)ÚSequence)ÚTYPE_CHECKINGÚAny)ÚBaseMessageÚHumanMessageÚconvert_to_openai_messages)ÚStatec                óì   — t        | t        «      r| S t        | t        «      r| gn| }t        |t        «      r-|r+t	        d„ |D «       «      r|D �cg c]  }t        |«      ‘Œ c}S t        | «      gS c c}w )aŠ  Convert state to a string or a list of user messages.

    Strings are sent as is, and sequences of `HumanMessage` objects as user messages.
    Anything else, such as conversations with system, AI, or tool messages, is
    serialized to JSON text in a single user message, with base64 images from any
    message kept in place as image parts.

    Args:
        state: Text, messages, or structured state to evaluate.

    Returns:
        A value suitable for the Decisions API `input` field.

    Raises:
        ValueError: If a message contains content the API does not accept.
        TypeError: If the state cannot be represented as JSON.
    c              3  ó<   K  — | ]  }t        |t        «      –— Œ y ­w©N)Ú
isinstancer   )Ú.0Úmessages     ú^/var/www/html/gaurav-v2/venv/lib/python3.12/site-packages/langchain_openai/decisions/_input.pyÚ	<genexpr>z$to_decision_input.<locals>.<genexpr>-   s   è ø€ ÐJÁ°g”
˜7¤L×1Áùó   ‚)r   Ústrr   r   ÚallÚ_to_user_messageÚ_to_json_user_message)ÚstateÚmessagesr   s      r   Úto_decision_inputr      so   € ô$ �%œÔØˆÜ$ U¬KÔ8�‰w¸e€Hä�8œXÔ&ÙÜÑJÁÓJÔJá9AÓB¹¨gÔ  Õ)¸ÑBÐBÜ! %Ó(Ð)Ð)ùò Cs   ÁA1c                óŠ   — t        | «      d   }t        |t        «      rd|dœS d|D �cg c]  }t        |«      ‘Œ c}dœS c c}w )NÚcontentÚuser©Úroler   )r	   r   r   Ú_to_input_part)r   r   Úblocks      r   r   r   3   sH   € Ü(¨Ó1°)Ñ<€GÜ�'œ3ÔØ¨7Ñ3Ð3ØÉ7Ó'SÉ7À%¬°uÕ(=È7Ñ'SÑTÐTùÒ'Ss   ©A c                óø   — | j                  d«      dk(  rd| d   dœS | j                  d«      dk(  r+| d   d   }|j                  d«      sd}t        |«      ‚d	|d
œS d| j                  d«      ›d�}t        |«      ‚)NÚtypeÚtextÚ
input_text©r#   r$   Ú	image_urlÚurlúdata:zfThe OpenAI Decisions API only accepts images as base64 data URLs. Hosted image URLs are not supported.Úinput_image©r#   r'   z=Unsupported content block type for the OpenAI Decisions API: z,. Only text and base64 images are supported.)ÚgetÚ
startswithÚ
ValueError)r!   r(   Úmsgs      r   r    r    :   s›   € Ø‡y�y�Ó˜FÒ"Ø$¨e°F©mÑ<Ð<Ø‡y�y�Ó˜KÒ'Ø�KÑ  Ñ'ˆØ�~‰~˜gÔ&ð7ð ô ˜S“/Ð!Ø%°CÑ8Ð8à
GØ�9‰9�VÓÐ
ÐKð	Mð ô �S‹/Ðó    c           	     ó¸  — g }dt        j                  «       j                  › �}t        j                  t        | ||«      d¬«      }g }d}t        j                  |› d�|«      D ]Z  }|j                  d|||j                  «        dœ«       |j                  d|t        |d	   «         d
œ«       |j                  «       }Œ\ |j                  d||d dœ«       d|dœS )a›  Serialize state the API cannot accept natively to a single user message.

    Covers conversations with non-user messages and JSON objects or arrays. Messages
    at any depth are converted to role/content objects. Base64 images in any message
    are sent as `input_image` parts at the position where they appear in the text, so
    the format stays the same whether or not a conversation contains images.
    z	lc-image-F)Úensure_asciir   z-(\d+)r%   r&   r*   é   r+   Nr   r   )ÚuuidÚuuid4ÚhexÚjsonÚdumpsÚ_to_json_valueÚreÚfinditerÚappendÚstartÚintÚend)r   ÚimagesÚtokenr$   r   ÚpositionÚmatchs          r   r   r   M   sÌ   € ð €FØœŸ
™
›×(Ñ(Ð)Ð*€EÜ�:‰:”n U¨F°EÓ:ÈÔO€DØ$&€GØ€HÜ—‘   vÐ.°Ö5ˆØ�‰ °d¸8ÀeÇkÁkÃmÐ6TÑUÔVØ�‰ ¸FÄ3ÀuÈQÁxÃ=Ñ<QÑRÔSØ—9‘9“;‰ð 6ð ‡N�N˜L°$°x°y°/ÑBÔCØ wÑ/Ð/r0   c           
     ó<  — t        | t        «      rt        t        | ||«      «      S t        | t        «      rNt        d„ | D «       «      sd}t        |«      ‚| j                  «       D ��ci c]  \  }}|t        |||«      “Œ c}}S t        | t        «      r6t        | t        t        t        f«      s| D �cg c]  }t        |||«      ‘Œ c}S | � t        | t        t        t        t        f«      r| S dt!        | «      j"                  › d�}t        |«      ‚c c}}w c c}w )Nc              3  ó<   K  — | ]  }t        |t        «      –— Œ y ­wr   )r   r   )r   Úkeys     r   r   z!_to_json_value.<locals>.<genexpr>f   s   è ø€ Ð9±5¨C”:˜c¤3×'±5ùr   z3OpenAI Decisions state object keys must be strings.z*Unsupported OpenAI Decisions state value: Ú.)r   r   r	   Ú_with_image_tokensÚdictr   Ú	TypeErrorÚitemsr9   r   r   ÚbytesÚ	bytearrayr>   ÚfloatÚboolr#   Ú__name__)Úvaluer@   rA   r/   rF   Úitems         r   r9   r9   b   sõ   € Ü�%œÔ%Ü)Ô*<¸UÀFÈEÓ*RÓSÐSÜ�%œÔÜÑ9±5Ó9Ô9ØGˆCÜ˜C“.Ð ØJOÏ+É+Ì-ÔXÉ-¹Y¸SÀ$�”^ D¨&°%Ó8Ñ8È-ÒXÐXÜ�%œÔ"¬:°e¼cÄ5Ì)Ð=TÔ+UÙ@EÓFÁ¸”˜t V¨UÕ3ÀÑFÐFØ€}œ
 5¬3´´U¼DÐ*AÔBØˆØ6´t¸E³{×7KÑ7KÐ6LÈAÐ
N€CÜ
�C‹.Ðùó YùâFs   Á)DÂ4Dc           	     óP  — t        | j                  t        «      r| S g }| j                  D ]f  }t        |t        «      rt	        |«      nd}|€|j                  |«       Œ4|j                  d|› dt        |«      › �dœ«       |j                  |«       Œh | j                  d|i¬«      S )zIReplace base64 image blocks with tokens marking where each image belongs.Nr$   Ú-r&   r   )Úupdate)r   r   r   rI   Ú_base64_image_urlr<   ÚlenÚ
model_copy)r   r@   rA   r   r!   r(   s         r   rH   rH   r   s˜   € ô �'—/‘/¤3Ô'ØˆØ*,€GØ—”ˆÜ*4°U¼DÔ*AÔ Ô&ÀtˆØˆ;Ø�N‰N˜5Õ!à�N‰N F°u°g¸Q¼sÀ6»{¸mÐ4LÑMÔNØ�M‰M˜#Õð !ð ×Ñ i°Ð%9ÐÓ:Ð:r0   c                ó*  — | j                  d«      dvry	 t        t        | g¬«      «      d   d   }t        |t        «      r"|j                  di «      j                  dd	«      nd	}|j                  d
«      r|S dS # t        t        t
        f$ r Y yw xY w)zAReturn the data URL of a base64 image block, or `None` otherwise.r#   >   Úimager'   N)r   r   r   r'   r(   Ú r)   )	r,   r	   r   r.   ÚKeyErrorrJ   r   rI   r-   )r!   Úpartr(   s      r   rV   rV   ƒ   s•   € à‡y�y�ÓÐ 6Ñ6ØðÜ)¬,À¸wÔ*GÓHÈÑSÐTUÑVˆô 7AÀÄtÔ6Lˆ$�(‰(�; Ó
#×
'Ñ
'¨¨rÔ
2ÐRT€CØ—.‘. Ô)ˆ3Ð3¨tÐ3øô œ¤)Ð,ò Ùðús   –A; Á;BÂB)r   r
   Úreturnzstr | list[dict[str, Any]])r   r   r^   údict[str, Any])r!   r_   r^   r_   )r   r
   r^   r_   )rQ   Úobjectr@   ú	list[str]rA   r   r^   r   )r   r   r@   ra   rA   r   r^   r   )r!   r_   r^   z
str | None)Ú__doc__Ú
__future__r   r7   r:   r4   Úcollections.abcr   Útypingr   r   Úlangchain_core.messagesr   r   r	   Ú langchain_openai.decisions.typesr
   r   r   r    r   r9   rH   rV   © r0   r   Ú<module>ri      sp   ðÙ Iå "ã Û 	Û Ý $ß %÷ñ ñ Ý6ó*ó<Uóó&0ó*ð ;Øð;Ø"+ð;Ø47ð;àó;ô"	4r0   