
    D_iy1                     *   S r SSKrSSKJrJrJrJr  SSKJr  SSK	J
r
Jr  SSKJrJrJr  SSKJr  SSKJrJrJr  SS	KJr  SS
KJrJr   " S S5      r " S S\\5      r " S S\\5      r " S S\5      rS\ S\!\ \4   S\ S-  S\4S jr" " S S\5      r#SSSSS.S\ S-  S\ S-  S\ S-  S\$S-  S\#4
S jjr%SSSSS.S\ S-  S\ S-  S\ S-  S\ S-  S\4
S jjr&S \'\!   S\(\'\   \'\   4   4S! jr)S \'\!   S\'\#   4S" jr*S#\S$   S%\S$   S\S$   4S& jr+g)'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                       \ rS rSrSrSrg)ToolOutputMixin   a  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____firstlineno____doc____static_attributes__r       V/home/james-whalen/.local/lib/python3.13/site-packages/langchain_core/messages/tool.pyr   r      s    r   r   c            
         ^  \ rS rSr% Sr\\S'    Sr\S   \S'    Sr	\
\S'    Sr\S	   \S
'    \" \SS9r\\S'    \" \SS9r\\S'    \" SS9\S\S\4S j5       5       r\S\\\\-     -  S\
SS4S j5       r\  SS\\\\-     -  S-  S\\R.                     S-  S\
SS4S jj5       r  SS\\\\-     -  S-  S\\R.                     S-  S\
SS4U 4S jjjrSrU =r$ )ToolMessage   a  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                    US   n[        U[        5      (       a  [        U5      n[        U[        [        45      (       d   [        U5      US'   O}[        U[        5      (       ah  / US'   [        U5       HT  u  pV[        U[        [        45      (       d    US   R                  [        U5      5        M@  US   R                  U5        MV     US   n[        U[        [        [        45      (       a  [        U5      US'   U$ ! [         a  nSU< S3n[	        U5      UeSnAff = f! [         a  nSU SU 3n[	        U5      UeSnAff = f)zZCoerce 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)clsr2   r   emsgixr$   s           r    coerce_argsToolMessage.coerce_argsZ   s_    #gu%%7mG'C;//-$'Ly! && "F9!'*!!c4[11	5y)00Q8 9%,,Q/ + n-lT3$677%(%6F>"7  --$+: . 
 !o1,- & 5;;<# >??@cC  )o145s0   D D(
D%D  D%(
E2EEr   kwargsc                     g Nr   )selfr   rF   s      r    __init__ToolMessage.__init__   s    
 r   content_blocksc                     g rH   r   )rI   r   rL   rF   s       r    rJ   rK      s     r   c                 j   > Ub  [         TU ]  " SS[        SU5      0UD6  g[         TU ]  " SSU0UD6  g)zInitialize 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   )superrJ   r   )rI   r   rL   rF   	__class__s       r    rJ   rK      sG     %G 5~F
 G7W77r   r   )NN)r   r   r   r   r   r8   __annotations__r&   r   r'   r   r+   r   r;   r.   r/   r	   classmethodrD   r   r7   rJ   typesContentBlockr   __classcell__rP   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                   X   ^  \ rS rSr% SrS r\S    \S'   \S\	S\
4U 4S jj5       rSrU =r$ )ToolMessageChunk   zTool Message chunk.r&   otherr3   c                   > [        U[        5      (       a  U R                  UR                  :w  a  Sn[        U5      eU R	                  U R                  [        U R                  UR                  5      [        U R                  UR                  5      [        U R                  UR                  5      [        U R                  UR                  5      U R                  [        U R                  UR                  5      S9$ [        TU ]A  U5      $ )Nz:Cannot concatenate ToolMessageChunks with different names.)r$   r   r'   r.   r/   idr+   )r5   rX   r$   r9   rP   r   r   r   r'   r   r.   r/   r\   _merge_statusr+   rO   __add__)rI   rZ   rA   rP   s      r    r^   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   r&   r   rQ   r   r   r   r^   r   rU   rV   s   @r    rX   rX      s9    
 );D'$
%:&S &%5 & &r   rX   c                   d    \ rS rSr% Sr\\S'    \\\4   \S'    \S-  \S'    \	\
S      \S'   S	rg)
ToolCall   zRepresents 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argsNr\   	tool_callr&   r   )r   r   r   r   r   r8   rQ   r;   r   r
   r   r   r   r   r    ra   ra      sB    
 I,
sCx.)d
N gk*
++r   ra   rc   rd   r\   r3   c                     [        XUSS9$ )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.
re   )rc   rd   r\   r&   )ra   rc   rd   r\   s      r    re   re      s      RkBBr   c                   x    \ rS rSr% Sr\S-  \S'    \S-  \S'    \S-  \S'    \S-  \S'    \\	S      \S	'   S
r
g)ToolCallChunk   aJ  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)]
```
Nrc   rd   r\   indextool_call_chunkr&   r   )r   r   r   r   r   r8   rQ   r=   r
   r   r   r   r   r    ri   ri      sM    $ *,
*)d
N6:3
g/0
11r   ri   rc   rd   r\   rk   rk   c                     [        XX#SS9$ )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.
rl   )rc   rd   r\   rk   r&   )ri   rm   s       r    rl   rl     s    $ 7H r   rc   rd   r\   r*   r*   c                     [        XX#SS9$ )a  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)rc   rd   r\   r*   r&   r   ro   s       r    rq   rq   1  s    $ 7J r   raw_tool_callsc                    / n/ nU  Hm  nSU;  a  M  US   S   n [         R                  " US   S   5      n[        U=(       d    SU=(       d    0 UR                  S5      S9nUR	                  U5        Mo     X4$ ! [         R
                   a4    UR	                  [        UUS   S   UR                  S5      SS95         M  f = f)	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.
functionrc   	arguments r\   rg   Nro   )jsonloadsre   getr<   JSONDecodeErrorrq   )rr   
tool_callsinvalid_tool_callsraw_tool_callfunction_namefunction_argsparseds          r    default_tool_parserr   H  s     J']*%j1&9	 JJ}Z'@'MNM"(b"(b $$T*F
 f% (* )) ## 	%%!&&z2;?$((.		s   AA;;ACCc           	          / nU  HY  nSU;  a  SnSnOUS   S   nUS   S   n[        UUUR                  S5      UR                  S5      S9nUR                  U5        M[     U$ )zBest-effort parsing of tool chunks.

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

Returns:
    List of parsed ToolCallChunk objects.
rt   Nru   rc   r\   rk   rm   )rl   ry   r<   )rr   tool_call_chunksre   r   r~   r   s         r    default_tool_chunk_parserr   m  s     #	Y& M M%j1+>M%j1&9M }}T"--(	
 	' $ r   leftr)   rightc                     SX1;   a  S$ S$ )Nr*   r(   r   )r   r   s     r    r]   r]     s     $.7=I=r   ),r   rw   typingr   r   r   r   uuidr   pydanticr   r	   typing_extensionsr
   r   r   langchain_core.messagesr   rS   langchain_core.messages.baser   r   r   langchain_core.messages.contentr   langchain_core.utils._merger   r   r   r"   rX   ra   r8   r;   re   ri   r=   rl   rq   r7   r6   r   r   r]   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   