o
    i)                     @  s   d dl mZ d dlmZmZmZ d dlmZm	Z	m
Z
 d dlmZm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 dd
lmZ ddlmZm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z) G dd de&eef Z*dS )    )annotations)AsyncIteratorIteratorSequence)AbstractAsyncContextManagerasynccontextmanagercontextmanager)Anyoverload   )_utilsmessagesmodelsusage)AbstractBuiltinTool)OutputDataT
OutputSpec)AgentRun)ModelSettings)
AgentDepsTDeferredToolResultsToolToolFuncEither)AbstractToolset   )AbstractAgentEventStreamHandlerInstructionsRunOutputDataTc                   @  sD  e Zd ZdZdTddZedUdd	ZedVddZejdWddZedXddZ	edYddZ
edZddZed[ddZd\ddZd]d!d"Ze	#d^d#d#d#d#d#d#d#d#d#d$d#d#d%d_d?d@Ze	#d^d#d#d#d#d#d#d#d#d$d#d#dAd`dDd@Ze	#d^d#d#d#d#d#d#d#d#d#d$d#d#d%dadGd@ZeejejejejejejdHdbdRdSZd#S )cWrapperAgentzYAgent which wraps another agent.

    Does nothing on its own, used as a base class.
    wrapped&AbstractAgent[AgentDepsT, OutputDataT]c                 C  s
   || _ d S N)r    )selfr     r$   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/agent/wrapper.py__init__!   s   
zWrapperAgent.__init__return1models.Model | models.KnownModelName | str | Nonec                 C     | j jS r"   )r    modelr#   r$   r$   r%   r*   $      zWrapperAgent.model
str | Nonec                 C  r)   r"   r    namer+   r$   r$   r%   r/   (   r,   zWrapperAgent.namevalueNonec                 C  s   || j _d S r"   r.   )r#   r0   r$   r$   r%   r/   ,   s   typec                 C  r)   r"   )r    	deps_typer+   r$   r$   r%   r3   0   r,   zWrapperAgent.deps_typeOutputSpec[OutputDataT]c                 C  r)   r"   )r    output_typer+   r$   r$   r%   r5   4   r,   zWrapperAgent.output_type%EventStreamHandler[AgentDepsT] | Nonec                 C  r)   r"   )r    event_stream_handlerr+   r$   r$   r%   r7   8   r,   z!WrapperAgent.event_stream_handler%Sequence[AbstractToolset[AgentDepsT]]c                 C  r)   r"   )r    toolsetsr+   r$   r$   r%   r9   <   r,   zWrapperAgent.toolsetsc                   s   | j  I d H S r"   )r    
__aenter__r+   r$   r$   r%   r:   @   s   zWrapperAgent.__aenter__argsr	   bool | Nonec                   s   | j j| I d H S r"   )r    	__aexit__)r#   r;   r$   r$   r%   r=   C   s   zWrapperAgent.__aexit__NT)r5   message_historydeferred_tool_resultsr*   instructionsdepsmodel_settingsusage_limitsr   
infer_namer9   builtin_toolsuser_prompt,str | Sequence[_messages.UserContent] | Noner5   r>   'Sequence[_messages.ModelMessage] | Noner?   DeferredToolResults | Noner*   r@   Instructions[AgentDepsT]rA   r   rB   ModelSettings | NonerC   _usage.UsageLimits | Noner   _usage.RunUsage | NonerD   boolr9   ,Sequence[AbstractToolset[AgentDepsT]] | NonerE   $Sequence[AbstractBuiltinTool] | None>AbstractAsyncContextManager[AgentRun[AgentDepsT, OutputDataT]]c                C     d S r"   r$   r#   rF   r5   r>   r?   r*   r@   rA   rB   rC   r   rD   r9   rE   r$   r$   r%   iterF      zWrapperAgent.iter)r>   r?   r*   r@   rA   rB   rC   r   rD   r9   rE   OutputSpec[RunOutputDataT]AAbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]]c                C  rR   r"   r$   rS   r$   r$   r%   rT   Y   rU   !OutputSpec[RunOutputDataT] | None(AsyncIterator[AgentRun[AgentDepsT, Any]]c                C sh   | j j|||||||||	|
|||d4 I dH }|V  W d  I dH  dS 1 I dH s-w   Y  dS )a  A contextmanager which can be used to iterate over the agent graph's nodes as they are executed.

        This method builds an internal agent graph (using system prompts, tools and output schemas) and then returns an
        `AgentRun` object. The `AgentRun` can be used to async-iterate over the nodes of the graph as they are
        executed. This is the API to use if you want to consume the outputs coming from each LLM model response, or the
        stream of events coming from the execution of tools.

        The `AgentRun` also provides methods to access the full message history, new messages, and usage statistics,
        and the final result of the run once it has completed.

        For more details, see the documentation of `AgentRun`.

        Example:
        ```python
        from pydantic_ai import Agent

        agent = Agent('openai:gpt-4o')

        async def main():
            nodes = []
            async with agent.iter('What is the capital of France?') as agent_run:
                async for node in agent_run:
                    nodes.append(node)
            print(nodes)
            '''
            [
                UserPromptNode(
                    user_prompt='What is the capital of France?',
                    instructions_functions=[],
                    system_prompts=(),
                    system_prompt_functions=[],
                    system_prompt_dynamic_functions={},
                ),
                ModelRequestNode(
                    request=ModelRequest(
                        parts=[
                            UserPromptPart(
                                content='What is the capital of France?',
                                timestamp=datetime.datetime(...),
                            )
                        ],
                        run_id='...',
                    )
                ),
                CallToolsNode(
                    model_response=ModelResponse(
                        parts=[TextPart(content='The capital of France is Paris.')],
                        usage=RequestUsage(input_tokens=56, output_tokens=7),
                        model_name='gpt-4o',
                        timestamp=datetime.datetime(...),
                        run_id='...',
                    )
                ),
                End(data=FinalResult(output='The capital of France is Paris.')),
            ]
            '''
            print(agent_run.result.output)
            #> The capital of France is Paris.
        ```

        Args:
            user_prompt: User input to start/continue the conversation.
            output_type: Custom output type to use for this run, `output_type` may only be used if the agent has no
                output validators since output validators would expect an argument that matches the agent's output type.
            message_history: History of the conversation so far.
            deferred_tool_results: Optional results for deferred tool calls in the message history.
            model: Optional model to use for this run, required if `model` was not set when creating the agent.
            instructions: Optional additional instructions to use for this run.
            deps: Optional dependencies to use for this run.
            model_settings: Optional settings to use for this model's request.
            usage_limits: Optional limits on model request count or token usage.
            usage: Optional usage to start with, useful for resuming a conversation or agents used in tools.
            infer_name: Whether to try to infer the agent name from the call frame if it's not set.
            toolsets: Optional additional toolsets for this run.
            builtin_tools: Optional additional builtin tools for this run.

        Returns:
            The result of the run.
        )rF   r5   r>   r?   r*   r@   rA   rB   rC   r   rD   r9   rE   N)r    rT   )r#   rF   r5   r>   r?   r*   r@   rA   rB   rC   r   rD   r9   rE   runr$   r$   r%   rT   l   s&   a.r/   rA   r*   r9   toolsr@   r/   str | _utils.UnsetAgentDepsT | _utils.Unset9models.Model | models.KnownModelName | str | _utils.Unset4Sequence[AbstractToolset[AgentDepsT]] | _utils.Unsetr\   KSequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]] | _utils.Unset'Instructions[AgentDepsT] | _utils.UnsetIterator[None]c                c  sF    | j j||||||d dV  W d   dS 1 sw   Y  dS )a]  Context manager to temporarily override agent name, dependencies, model, toolsets, tools, or instructions.

        This is particularly useful when testing.
        You can find an example of this [here](../testing.md#overriding-model-via-pytest-fixtures).

        Args:
            name: The name to use instead of the name passed to the agent constructor and agent run.
            deps: The dependencies to use instead of the dependencies passed to the agent run.
            model: The model to use instead of the model passed to the agent run.
            toolsets: The toolsets to use instead of the toolsets passed to the agent constructor and agent run.
            tools: The tools to use instead of the tools registered with the agent.
            instructions: The instructions to use instead of the instructions registered with the agent.
        r[   N)r    override)r#   r/   rA   r*   r9   r\   r@   r$   r$   r%   rd      s   "zWrapperAgent.override)r    r!   )r'   r(   )r'   r-   )r0   r-   r'   r1   )r'   r2   )r'   r4   )r'   r6   )r'   r8   )r'   r!   )r;   r	   r'   r<   r"   )rF   rG   r5   r1   r>   rH   r?   rI   r*   r(   r@   rJ   rA   r   rB   rK   rC   rL   r   rM   rD   rN   r9   rO   rE   rP   r'   rQ   )rF   rG   r5   rV   r>   rH   r?   rI   r*   r(   r@   rJ   rA   r   rB   rK   rC   rL   r   rM   rD   rN   r9   rO   rE   rP   r'   rW   )rF   rG   r5   rX   r>   rH   r?   rI   r*   r(   r@   rJ   rA   r   rB   rK   rC   rL   r   rM   rD   rN   r9   rO   rE   rP   r'   rY   )r/   r]   rA   r^   r*   r_   r9   r`   r\   ra   r@   rb   r'   rc   )__name__
__module____qualname____doc__r&   propertyr*   r/   setterr3   r5   r7   r9   r:   r=   r
   rT   r   r   r   UNSETrd   r$   r$   r$   r%   r      s    


qr   N)+
__future__r   _annotationscollections.abcr   r   r   
contextlibr   r   r   typingr	   r
    r   r   	_messagesr   r   _usagerE   r   outputr   r   rZ   r   settingsr   r\   r   r   r   r   r9   r   abstractr   r   r   r   r   r$   r$   r$   r%   <module>   s    