
    3fiF                        d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZmZ ddl
mZ dZ	 d	 	 	 	 	 dd	Zdd
ZddZddZddZ	 	 	 	 	 	 ddZy)a  Converts between AIMessage output formats, governed by `output_version`.

`output_version` is an attribute on ChatOpenAI.

Supported values are `None`, `'v0'`, and `'responses/v1'`.

`'v0'` corresponds to the format as of `ChatOpenAI` v0.3. For the Responses API, it
stores reasoning and tool outputs in `AIMessage.additional_kwargs`:

```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",
)
```

`'responses/v1'` is only applicable to the Responses API. It retains information
about response item sequencing and accommodates multiple reasoning items by
representing these items in the content sequence:

```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
formats. The functions are used internally by ChatOpenAI.
    )annotationsN)IterableIterator)Anycast)	AIMessageis_data_content_block)content__openai_function_call_ids__c           	        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 )z3Mutate 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_)
isinstancer
   listdictgetpopadditional_kwargsappend_FUNCTION_CALL_IDS_MAP_KEYr   itemssetkeysstr
startswith)
messagehas_reasoningnew_contentblock_r   function_call_idr   kvs
             b/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_openai/chat_models/_compat.py_convert_to_v03_ai_messager6   Q   s    '//4((*__ 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                T   t        | j                  t              rg }| j                  D ]h  }t        |t              rE|j	                  d      }|dk(  r|j                  d|d   d       A|dv rF|j                  |       X|j                  |       j | j                  d|i      S | S )z4Convert a v1 message to the Chat Completions format.r   r   r   r   )r   	tool_callr
   )update)r    r
   r!   r"   r#   r&   
model_copy)r-   r/   r0   
block_types       r5   $_convert_from_v1_to_chat_completionsr=      s    '//4(__ 	*E%&"YYv.
'&&f'NO#==&&u-""5)	* !!)[)A!BBN    c                   | d   dk(  ri }dD ]  }|| v s| |   ||<    d| v rVd| v r| d   |d<   d|d<   | d   |d<   | j                  d      x}r(|j                  t        |j                                      |S d|d<   | j                  d      x}r(|j                  t        |j                                      d| v r| d   |d	<   |S | d   d
k(  r| d   S t        |       S )zAConvert a v1 `Annotation` to the v0.3 format (for Responses API).r   citation)	end_indexstart_indexurltitleurl_citationextrasfile_citationfilenamenon_standard_annotationvalue)r#   r:   r"   r(   )
annotationnew_annfieldextra_fieldss       r5   _convert_annotation_from_v1rO      s   &Z'"$1 	3E
"!+E!2	3 J*$#-g#6 ,GFO'.GEN)~~h77|7tL$6$6$89:  .GFO)~~h77|7tL$6$6$89:*$&0&9
#&66'""
r>   c              #  N  K   d}t        |       }||k  r| |   }|j                  d      dk7  sd|v rt        |       |dz  }6d|vrgd|vrci |dg i}d|v r |j                  |j	                  d             |j	                  dd      |d<   d|v r|j	                  d      |d<   | |dz  }	 d|j                  dd	      d
g}|j                         D ci c]  \  }}|dk7  s|| }}}d|v r |j                  |j	                  d             |dz  }||k  rN| |   }	|	j                  d      dk(  r.d|	v r*|j                  d|	j                  dd	      d
       |dz  }nn||k  rNt        |      }
||
d<   |
j	                  dd      |
d<   |
 ||k  ry y c c}}w w)Nr   r   r   summary   rF   encrypted_contentsummary_text r8   )lenr#   r"   r:   r$   r(   r&   )blocksinr0   
oai_formatrQ   r3   r4   commonnext_mergeds              r5   _implode_reasoning_blocksr^      s    	AFA
a%q	 99V+yE/Au+FA%)5*@1E19b1J:%!!*..":;!+!DJv"j02<..AT2U
./FA $UYY{B-GH)
 $);;=E41aA4D!Q$EEvMM&**X./	Q!e1IEyy K/K54H+UYY{B5OP Q !e f#yFK8vY a%4 Fs%   CF%F)F.A>F%-.F%F%c              #    K   t        |       } | D ]M  }|j                  d      dk7  r| 	 t        |       }|j                  d      dk(  r|j                  d      |j                  d      k(  r|j                  d      dk(  rod|d   i}d|v r|d   |d	<   |j                  d
      x}r|dk(  rd|d
<   n9|dk(  r4d|d
<   n.|j                  di       j                  d
      r|d   d
   |d
<   n	 d|d<   |j                  d      dk(  rd|d   i}d|v rd|d   v r|d   d   |d<   d|v r|d   |d<   |j                  d
      x}r|dk(  rd|d
<   n9|dk(  r4d|d
<   n.|j                  di       j                  d
      r|d   d
   |d
<   n	 d|d<   n|j                  d      dk(  rd|d   i}d|v rd|d   v r|d   d   |d<   dD ]/  }||v r	||   ||<   ||j                  di       v r|d   |   ||<   01 d|v r|d   |d<   |j                  d
      x}r|dk(  rd|d
<   n7|dk(  r2d|d
<   n,|j                  di       j                  d
      r|d   d
   |d
<   d|d<   n|j                  d      dk(  r.d|d   i}d|v rt	        j
                  |d   d      |d<   n!d|j                  di       v r|d   d   |d<   n	 |j                  di       j                  d      x}r||d<   |j                  di       j                  d       x}r||d <   d!|d<   |j                  di       j                  d"      x}r||d"<   |j                  di       j                  d      x}	r|	|d<   d|v r|d   |d<   |j                  di       j                         D ]  \  }
}|
d#vs|||
<    n|j                  d      d$k(  rd|d   i}|j                  di       j                  d       x}r||d <   d|v r|d   |d%<   d$|d<   |j                  di       j                  d      x}	r|	|d<   |j                  di       j                         D ]  \  }
}|
d&vs|||
<    n	  F| | P y'# t        $ r | Y  y'w xY ww)(am  Generator that walks through *items* and, whenever it meets the pair.

        {"type": "server_tool_call", "name": "web_search", "id": X, ...}
        {"type": "server_tool_result", "id": X}

    merges them into

        {"id": X,
         "output": ...,
         "status": ...,
         "type": "web_search_call"}

    keeping every other element untouched.
    r   server_tool_callserver_tool_resulttool_call_idr   name
web_searchargsactionstatussuccess	completederrorfailedrF   r   file_searchqueriesoutputresultsr   code_interpretercode)container_idoutputsr   
remote_mcp),:)
separators	arguments	tool_nameserver_labelr   approval_request_id)rz   rx   ry   rj   r   tools)rz   rj   N)iterr#   nextStopIterationjsondumpsr(   )r(   currentnxt	collapsedrg   keyry   rz   approval_idrj   r3   r4   s               r5   _consolidate_callsr     s|     KE z;;v"44M	u+C 776?22sww8
[[8 {{6"l2!74=1	W$ +2&/Ih' WWX..6.*.9	(+7*.6	(+WWXr*..x8*-h-*AIh'$5	&!{{6"m3!74=1	W$gfo)E+26?9+EIi(s?+.x=Ii( WWX..6.*.9	(+7*.6	(+WWXr*..x8*-h-*AIh'$6	&!V$(::!74=1	W$76?)B(/(?If%, Cg~)0	#Hb 99)0):3)?	# s?+.x=Ii( WWX..6.*.9	(+7*.6	(+WWXr*..x8*-h-*AIh'$;	&!V$4!74=1	W$-1ZZJ.Ik* !GKK"$==-4X->{-KIk* 'Hb 9 = =k JJ9J(1If%#*;;x#<#@#@#PP<P0<In-$.	&!")++h";"?"?@U"VV;V7BI34GGHb155g>>5>).Ig&s?*-h-Ih'#KK"5;;= )DAq SS'(	!) V$(88!74=1	#*;;x#<#@#@#PP<P0<In-s?),XIg&$4	&!GGHb155g>>5>).Ig&#KK"5;;= )DAq 99'(	!) O MIuz  	M	s5   +Q6Q"MQ6B.Q6Q6"Q3/Q62Q33Q6c                :  	 g }| D ]  		d   dk(  r@d	v r<t        	      }	d   D cg c]  }t        |       c}|d<   |j                  |       L	d   dk(  rd	d   d}d	v rd		d   v r	d   d	   |d<   d
	v r	d
   |d
<   d	v rd	d   v r	d   d   |d<   t        	fddD              rM|D cg c]  }|d   	d   k(  s| }}|r0|d   }d
	vr|d
   |d
<   d	vrt	        j
                  |d         |d<   |j                  |       t        t        t         	            r	d   dk(  rd	v rt        	j                  d      t              rc	d   j                  d      rOd	d   d}dD ]/  }|	v r		|   ||<   |	j                  di       v s%	d   |   ||<   1 |j                  |       	d   dk(  rd	v r|j                  	d          |j                  	        t        t        |            }t        t        |            S c c}w c c}w )Nr   r   r   r9   r   r   )r   r   rF   item_idrc   rx   c              3  &   K   | ]  }|v 
 y w)N ).0r   r0   s     r5   	<genexpr>z0_convert_from_v1_to_responses.<locals>.<genexpr>  s     E3e#Es   )rc   rx   r   re   imagebase64ig_r   )r   result)r   rg   non_standardrJ   )r"   rO   r&   anyr   r   r	   r   r    r#   r+   r,   r!   r^   r   )
r
   
tool_callsr/   	new_blockacallmatching_tool_callsr9   	extra_keyr0   s
            @r5   _convert_from_v1_to_responsesr     s    K ,&=F"}'=UI8=m8L(34+A.(Im$ y)6]k)!0U4[II5 Y%/%A"'/)"<	$$)&M	&!5 [E(O%C).x)E	+&E/DEE%/'!4:t3LD'# ' ' 3A 6IU*,5f,=	&)"%/15If<M1N	+.y)!$tU"34f(E!599T?C0d&&u-!8E(OTI- F	%+0+;Ii(%))Hb"99+0?9+EIi(	F
 y)6]n,E1AuW~.u%Y,&\ 0=>K";/00W('s   H2HH)F)r-   r   r.   boolreturnr   )r-   r   r   r   )rK   ztypes.Annotationr   zdict[str, Any])rW   list[dict[str, Any]]r   Iterable[dict[str, Any]])r(   r   r   zIterator[dict[str, Any]])r
   zlist[types.ContentBlock]r   zlist[types.ToolCall]r   r   )__doc__
__future__r   r   collections.abcr   r   typingr   r   langchain_core.messagesr   r	   r
   typesr'   r6   r=   rO   r^   r   r   r   r>   r5   <module>r      s   AF #  .  D 4; 
 /4EE'+EER, F0fJZ31%313G3131r>   