
    g3fiy1                        d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZ  G d d      Z G d dee      Z G d dee      Z G d de      Zde de!e ef   de dz  defdZ" G d de      Z#dddddde dz  de dz  de dz  de$dz  de#f
dZ%dddddde dz  de dz  de dz  de dz  def
dZ&d e'e!   de(e'e   e'e   f   fd!Z)d e'e!   de'e#   fd"Z*d#ed$   d%ed$   ded$   fd&Z+y)'zMessages for tools.    N)AnyLiteralcastoverload)UUID)Fieldmodel_validator)NotRequired	TypedDictoverride)content)BaseMessageBaseMessageChunkmerge_contentInvalidToolCall)merge_dicts	merge_objc                       e Zd ZdZy)ToolOutputMixina  Mixin for objects that tools can return directly.

    If a custom BaseTool is invoked with a `ToolCall` and the output of custom code is
    not an instance of `ToolOutputMixin`, the output will automatically be coerced to
    a string and wrapped in a `ToolMessage`.

    N)__name__
__module____qualname____doc__     Z/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_core/messages/tool.pyr   r      s    r   r   c            
           e Zd ZU dZeed<   	 dZed   ed<   	 dZe	ed<   	 dZ
ed   ed	<   	  eed
      Zeed<   	  eed
      Zeed<   	  ed      ededefd              Zedeeeez     z  de	ddfd       Ze	 	 ddeeeez     z  dz  deej,                     dz  de	ddfd       Z	 	 ddeeeez     z  dz  deej,                     dz  de	ddf fdZ xZS )ToolMessagea  Message for passing the result of executing a tool back to a model.

    `ToolMessage` objects contain the result of a tool invocation. Typically, the result
    is encoded inside the `content` field.

    `tool_call_id` is used to associate the tool call request with the tool call
    response. Useful in situations where a chat model is able to request multiple tool
    calls in parallel.

    Example:
        A `ToolMessage` representing a result of `42` from a tool call with id

        ```python
        from langchain_core.messages import ToolMessage

        ToolMessage(content="42", tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL")
        ```

    Example:
        A `ToolMessage` where only part of the tool output is sent to the model
        and the full output is passed in to artifact.

        ```python
        from langchain_core.messages import ToolMessage

        tool_output = {
            "stdout": "From the graph we can see that the correlation between "
            "x and y is ...",
            "stderr": None,
            "artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
        }

        ToolMessage(
            content=tool_output["stdout"],
            artifact=tool_output,
            tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL",
        )
        ```
    tool_call_idtooltypeNartifactsuccessr$   errorstatusF)default_factoryrepradditional_kwargsresponse_metadatabefore)modevaluesreturnc                 `   |d   }t        |t              rt        |      }t        |t        t        f      s	 t        |      |d<   nqt        |t              rag |d<   t        |      D ]N  \  }}t        |t        t        f      s	 |d   j                  t        |             ;|d   j                  |       P |d   }t        |t        t        t        f      rt        |      |d<   |S # t        $ r}d|d}t	        |      |d}~ww xY w# t        $ r}d| d| }t	        |      |d}~ww xY w)zrCoerce the model arguments to the correct types.

        Args:
            values: The model arguments.

        r   zUToolMessage content should be a string or a list of string/dicts. Received:

content=z,

 which could not be coerced into a string.NzrToolMessage content should be a string or a list of string/dicts. Received a list but element ToolMessage.content[z8] is not a dict and could not be coerced to a string.:

r    )
isinstancetupleliststr
ValueError	enumeratedictappendr   intfloat)clsr.   r   emsgixr    s           r   coerce_argszToolMessage.coerce_argsZ   sW    #gu%7mG'C;/-$'Ly! & "F9!'* 01!!c4[1	5y)00Q8 9%,,Q/0 n-lT3$67%(%6F>"7  --$+: . 
 !o1,- & 5;;<# >??@cC  )o145s/   C( D(	D1DD	D-D((D-r   kwargsc                      y Nr   )selfr   rA   s      r   __init__zToolMessage.__init__   s    
 r   content_blocksc                      y rC   r   )rD   r   rF   rA   s       r   rE   zToolMessage.__init__   s     r   c                 d    |t        |   ddt        d|      i| yt        |   dd|i| y)a  Initialize a `ToolMessage`.

        Specify `content` as positional arg or `content_blocks` for typing.

        Args:
            content: The contents of the message.
            content_blocks: Typed standard content.
            **kwargs: Additional fields.
        Nr   zstr | list[str | dict]r   )superrE   r   )rD   r   rF   rA   	__class__s       r   rE   zToolMessage.__init__   sG     %G 5~F
 G7W77r   )NN)r   r   r   r   r4   __annotations__r"   r   r#   r   r'   r   r7   r*   r+   r	   classmethodr@   r   r3   rE   typesContentBlock__classcell__rJ   s   @r   r   r      s   &P 7"D'&/";Hc +4FG&'3(#DuEtE?#DuEtE?(#) )$ )  $)V tC$J''  
	   26:>tC$J''$. U//047 	
 
  26:>8tC$J''$.8 U//0478 	8
 
8 8r   r   c                   L     e Zd ZU dZd Zed    ed<   edede	f fd       Z
 xZS )ToolMessageChunkzTool Message chunk.r"   otherr/   c                    t        |t              r| j                  |j                  k7  rd}t        |      | j	                  | j                  t        | j                  |j                        t        | j                  |j                        t        | j                  |j                        t        | j                  |j                        | j                  t        | j                  |j                              S t        | A  |      S )Nz:Cannot concatenate ToolMessageChunks with different names.)r    r   r#   r*   r+   idr'   )r1   rR   r    r5   rJ   r   r   r   r#   r   r*   r+   rU   _merge_statusr'   rI   __add__)rD   rS   r=   rJ   s      r   rW   zToolMessageChunk.__add__   s    e-.  E$6$66R o%>>!..%dllEMMB"4==%..A"-**E,C,C# #.**E,C,C# 77$T[[%,,? "   wu%%r   )r   r   r   r   r"   r   rK   r   r   r   rW   rO   rP   s   @r   rR   rR      s9    
 );D'$
%:&S &%5 & &r   rR   c                   \    e Zd ZU dZeed<   	 eeef   ed<   	 edz  ed<   	 ee	d      ed<   y)ToolCalla  Represents an AI's request to call a tool.

    Example:
        ```python
        {"name": "foo", "args": {"a": 1}, "id": "123"}
        ```

        This represents a request to call the tool named `'foo'` with arguments
        `{"a": 1}` and an identifier of `'123'`.

    nameargsNrU   	tool_callr"   )
r   r   r   r   r4   rK   r7   r   r
   r   r   r   r   rY   rY      sB    
 I,
sCx.)d
N gk*
++r   rY   rZ   r[   rU   r/   c                      t        | ||d      S )zCreate a tool call.

    Args:
        name: The name of the tool to be called.
        args: The arguments to the tool call.
        id: An identifier associated with the tool call.

    Returns:
        The created tool call.
    r\   )rZ   r[   rU   r"   )rY   rZ   r[   rU   s      r   r\   r\      s      DRkBBr   c                   p    e Zd ZU dZedz  ed<   	 edz  ed<   	 edz  ed<   	 edz  ed<   	 eed      ed<   y)	ToolCallChunka~  A chunk of a tool call (yielded when streaming).

    When merging `ToolCallChunk`s (e.g., via `AIMessageChunk.__add__`),
    all string attributes are concatenated. Chunks are only merged if their
    values of `index` are equal and not None.

    Example:
    ```python
    left_chunks = [ToolCallChunk(name="foo", args='{"a":', index=0)]
    right_chunks = [ToolCallChunk(name=None, args="1}", index=0)]

    (
        AIMessageChunk(content="", tool_call_chunks=left_chunks)
        + AIMessageChunk(content="", tool_call_chunks=right_chunks)
    ).tool_call_chunks == [ToolCallChunk(name="foo", args='{"a":1}', index=0)]
    ```
    NrZ   r[   rU   indextool_call_chunkr"   )	r   r   r   r   r4   rK   r9   r
   r   r   r   r   r`   r`      sM    $ *,
*)d
N6:3
g/0
11r   r`   rZ   r[   rU   ra   ra   c                 "    t        | |||d      S )a-  Create a tool call chunk.

    Args:
        name: The name of the tool to be called.
        args: The arguments to the tool call.
        id: An identifier associated with the tool call.
        index: The index of the tool call in a sequence.

    Returns:
        The created tool call chunk.
    rb   )rZ   r[   rU   ra   r"   )r`   rc   s       r   rb   rb     s    $ 57H r   rZ   r[   rU   r&   r&   c                 "    t        | |||d      S )a8  Create an invalid tool call.

    Args:
        name: The name of the tool to be called.
        args: The arguments to the tool call.
        id: An identifier associated with the tool call.
        error: An error message associated with the tool call.

    Returns:
        The created invalid tool call.
    invalid_tool_call)rZ   r[   rU   r&   r"   r   re   s       r   rg   rg   1  s    $ 57J r   raw_tool_callsc                 v   g }g }| D ]a  }d|vr|d   d   }	 t        j                  |d   d         }t        |xs d|xs i |j                  d            }|j	                  |       c ||fS # t         j
                  $ r6 |j	                  t        ||d   d   |j                  d      d             Y w xY w)	zBest-effort parsing of tools.

    Args:
        raw_tool_calls: List of raw tool call dicts to parse.

    Returns:
        A list of tool calls and invalid tool calls.
    functionrZ   	arguments rU   r^   Nre   )jsonloadsr\   getr8   JSONDecodeErrorrg   )rh   
tool_callsinvalid_tool_callsraw_tool_callfunction_namefunction_argsparseds          r   default_tool_parserrw   H  s     J' ]*%j1&9	 JJ}Z'@'MNM"(b"(b $$T*F
 f%* ))) ## 	%%!&&z2;?$((.		s   AA//AB87B8c           	          g }| D ]Y  }d|vrd}d}n|d   d   }|d   d   }t        |||j                  d      |j                  d            }|j                  |       [ |S )zBest-effort parsing of tool chunks.

    Args:
        raw_tool_calls: List of raw tool call dicts to parse.

    Returns:
        List of parsed ToolCallChunk objects.
    rj   Nrk   rZ   rU   ra   rc   )rb   ro   r8   )rh   tool_call_chunksr\   ru   rt   rv   s         r   default_tool_chunk_parserrz   m  s     # (	Y& M M%j1+>M%j1&9M }}T"--(	
 	'( r   leftr%   rightc                     d| |hv rdS dS )Nr&   r$   r   )r{   r|   s     r   rV   rV     s     $.7=I=r   ),r   rm   typingr   r   r   r   uuidr   pydanticr   r	   typing_extensionsr
   r   r   langchain_core.messagesr   rM   langchain_core.messages.baser   r   r   langchain_core.messages.contentr   langchain_core.utils._merger   r   r   r   rR   rY   r4   r7   r\   r`   r9   rb   rg   r3   r2   rw   rz   rV   r   r   r   <module>r      s     / /  + > > 4 U U ; > Q8+ Q8h&{$4 &@,y ,6C
C sCx.C 	d
	C
 C&2I 2@ 
* * 	d
	
 : 2 
* * 	d
	
 : ."*J"*
4>4001"*Jd4j T-=P 6>
$
%>.56H.I> >r   