
    hD$                     T    d Z ddlZddlmZ ddlmZ dZ	 ddededefd	Zdedefd
Z	y)a  
This module converts between AIMessage output formats for the Responses API.

ChatOpenAI v0.3 stores reasoning and tool outputs in AIMessage.additional_kwargs:

.. code-block:: python

    AIMessage(
        content=[
            {"type": "text", "text": "Hello, world!", "annotations": [{"type": "foo"}]}
        ],
        additional_kwargs={
            "reasoning": {
                "type": "reasoning",
                "id": "rs_123",
                "summary": [{"type": "summary_text", "text": "Reasoning summary"}],
            },
            "tool_outputs": [
                {
                    "type": "web_search_call",
                    "id": "websearch_123",
                    "status": "completed",
                }
            ],
            "refusal": "I cannot assist with that.",
        },
        response_metadata={"id": "resp_123"},
        id="msg_123",
    )

To retain information about response item sequencing (and to accommodate multiple
reasoning items), ChatOpenAI now stores these items in the content sequence:

.. code-block:: python

    AIMessage(
        content=[
            {
                "type": "reasoning",
                "summary": [{"type": "summary_text", "text": "Reasoning summary"}],
                "id": "rs_123",
            },
            {
                "type": "text",
                "text": "Hello, world!",
                "annotations": [{"type": "foo"}],
                "id": "msg_123",
            },
            {"type": "refusal", "refusal": "I cannot assist with that."},
            {"type": "web_search_call", "id": "websearch_123", "status": "completed"},
        ],
        response_metadata={"id": "resp_123"},
        id="resp_123",
    )

There are other, small improvements as well-- e.g., we store message IDs on text
content blocks, rather than on the AIMessage.id, which now stores the response ID.

For backwards compatibility, this module provides functions to convert between the
old and new formats. The functions are used internally by ChatOpenAI.

    N)Union)	AIMessage__openai_function_call_ids__messagehas_reasoningreturnc           	         t        | j                  t              rg }| j                  D ]P  }t        |t              r+|j	                  d      dk(  rH|j                  dd      }|r$|j                  dd      }|j                  dd      }|| j                  d<   q|j	                  d      dv r<d| j                  vrg | j                  d<   | j                  d   j                  |       |j	                  d      dk(  rgt        | j                  vri | j                  t        <   |j	                  d	      x}s|j	                  d      x}s#|| j                  t           |<   ;|j	                  d      d
k(  r$|j	                  d
      x}r|| j                  d
<   s|j	                  d      dk(  rId|v r
|d   | _	        |j                  |j                         D 	ci c]  \  }}	|dk7  s||	 c}	}       t        |j                               ddhk(  r?t        |d   t              r,|d   j                  d      r|j                  d|d   i       -|j                  |       @|j                  |       S || _        t        | j                  t              r"| j                  j                  d      rd| _	        | S 	 | S c c}	}w )z1Mutate an AIMessage to the old-style v0.3 format.type	reasoningindexNid)web_search_callfile_search_callcomputer_callcode_interpreter_callmcp_callmcp_list_toolsmcp_approval_requestimage_generation_calltool_outputsfunction_callcall_idrefusaltextmsg_resp_)
isinstancecontentlistdictgetpopadditional_kwargsappend_FUNCTION_CALL_IDS_MAP_KEYr   itemssetkeysstr
startswith)
r   r   new_contentblock_r   function_call_idr   kvs
             b/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_openai/chat_models/_compat.py_convert_to_v03_ai_messager2   H   s    '//4(.0__ 8	*E%&99V$3 		'40A$!IIdD1!IIfd3=BG--k:YYv& 	+ 	 &W-F-FFDF11.A--n=DDUKYYv&/9 29R9RRPR112LM#(99Y#777,1IIdO;(; -  112LM# ii'94$yy33G3 <CG--i8YYv&&0u}%*4[
&&'TA!t)1'TU

%$8"5;4d..v6  &&w'@A&&u-""5)q8	*r &gjj#&7::+@+@+IGJ N 	N% (Us   K+Kc                 p    t         j                  t              xr t        d  j                  D              xr t	         fddddt
        fD              xs{ t         j                  t              xr_  j                  j                  d      xrB  j                  j                  d      x}xr# t        |t              xr |j                  d      }|s S g d	}|D ci c]  }|g  }}g } j                  j                  d      x}r|d   j                  |        j                  j                  d      x}r|d   j                  d|d
        j                  D ]  }	t        |	t              r}|	j                  d      dk(  ri|	j                         }
t         j                  t              r* j                  j                  d      r j                  |
d<   |d   j                  |
       |j                  |	         j                  j                  t
              } j                  D ]X  }d|d   t!        j"                  |d   d      |d   d}||j                  |d         x}r||d<   |d   j                  |       Z  j                  j                  dg       }|D ]O  }	t        |	t              r,|	j                  d      x}r||v r||   j                  |	       ?|j                  |	       Q g }|D ]  }|j%                  ||           |j%                  |       t         j                        }|j'                  dd       |j'                  dd       |j'                  dd       d j                  v r j                  d   }n j                  } j)                  |||dd      S c c}w )zFConvert an old-style v0.3 AIMessage into the new content-block format.c              3   <   K   | ]  }t        |t                y wN)r   r    ).0bs     r1   	<genexpr>z/_convert_from_v03_ai_message.<locals>.<genexpr>   s     =
1d#=s   c              3   :   K   | ]  }|j                   v   y wr5   )r#   )r6   itemr   s     r1   r8   z/_convert_from_v03_ai_message.<locals>.<genexpr>   s$      
 G---
s   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r
   r   r
   r   r   nameargsF)ensure_ascii)r
   r;   	argumentsr   N)r   r#   r   )updatedeep)r   r   r   allanyr%   r   r)   r*   response_metadatar!   r#   r$   r    copy
tool_callsjsondumpsextendr"   
model_copy)r   response_idis_chatopenai_v03content_orderkeybucketsunknown_blocksr   r   r,   
block_copyfunction_call_ids	tool_callr   _idr   r+   new_additional_kwargsnew_ids   `                  r1   _convert_from_v03_ai_messagerV      s   
 	7??D) 	>=W__== 	 
 *	
 	
 	
 wzz3' 0

%%f-0 ' 9 9 = =d CC0 ;,0 &&w/% * M  4AACRAGAN --11+>>y>##I. ++//	::w:	!!9"IJ  )eT"uyy'8F'BJ'**c*wzz/D/DV/L#*::
4 FO"":.!!%()  11556PQ'' 7	#f%If$5EJ 	
 ($((499C9"%M$ ''67 ,,00DL )eT"uyy/@(@(@cWnCL&!!%(	) K )73<()~& !:!:;k40i.nd3w(((**40"!6

    y  Bs   !
N3)F)
__doc__rF   typingr   langchain_core.messagesr   r%   boolr2   rV        r1   <module>r]      sY   =~   -;  /4EE'+EEPo) o	 or\   