o
    i                  
   @  s~  d 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 ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) z(ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 W n e7y Z8 ze7de8dZ8[8ww G dd deeef e+Z9dS )z2AG-UI protocol integration for Pydantic AI agents.    )annotations)CallableMappingSequence)replace)AnyGeneric)Self)DeferredToolResults)AbstractAgent)AbstractBuiltinTool)ModelMessage)KnownModelNameModel)OutputDataT
OutputSpec)ModelSettings)
AgentDepsT)AbstractToolset)RunUsageUsageLimits   )OnCompleteFuncStateHandler   )AGUIAdapter)	Starlette)
Middleware)Request)Response)	BaseRoute)ExceptionHandlerLifespanu   Please install the `starlette` package to use `AGUIApp`, you can use the `ag-ui` optional group — `pip install "pydantic-ai-slim[ag-ui]"`Nc                      sL   e Zd ZdZddddddddddddddddddddd0 fd.d/Z  ZS )1AGUIAppzLASGI application for running Pydantic AI agents with AG-UI protocol support.NTF)output_typemessage_historydeferred_tool_resultsmodeldepsmodel_settingsusage_limitsusage
infer_nametoolsetsbuiltin_toolson_completedebugroutes
middlewareexception_handlers
on_startupon_shutdownlifespanagent&AbstractAgent[AgentDepsT, OutputDataT]r$   OutputSpec[Any] | Noner%   Sequence[ModelMessage] | Noner&   DeferredToolResults | Noner'   #Model | KnownModelName | str | Noner(   r   r)   ModelSettings | Noner*   UsageLimits | Noner+   RunUsage | Noner,   boolr-   ,Sequence[AbstractToolset[AgentDepsT]] | Noner.   $Sequence[AbstractBuiltinTool] | Noner/   OnCompleteFunc[Any] | Noner0   r1   Sequence[BaseRoute] | Noner2   Sequence[Middleware] | Noner3   %Mapping[Any, ExceptionHandler] | Noner4   "Sequence[Callable[[], Any]] | Noner5   r6   Lifespan[Self] | NonereturnNonec                  sX   t  j|||||||d d 	
fdd}| jjd|d	gd
 dS )as  An ASGI application that handles every request by running the agent and streaming the response.

        Note that the `deps` will be the same for each request, with the exception of the frontend state that's
        injected into the `state` field of a `deps` object that implements the [`StateHandler`][pydantic_ai.ui.StateHandler] protocol.
        To provide different `deps` for each request (e.g. based on the authenticated user),
        use [`AGUIAdapter.run_stream()`][pydantic_ai.ui.ag_ui.AGUIAdapter.run_stream] or
        [`AGUIAdapter.dispatch_request()`][pydantic_ai.ui.ag_ui.AGUIAdapter.dispatch_request] instead.

        Args:
            agent: The agent to run.

            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.
            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.
            on_complete: Optional callback function called when the agent run completes successfully.
                The callback receives the completed [`AgentRunResult`][pydantic_ai.agent.AgentRunResult] and can access `all_messages()` and other result data.

            debug: Boolean indicating if debug tracebacks should be returned on errors.
            routes: A list of routes to serve incoming HTTP and WebSocket requests.
            middleware: A list of middleware to run for every request. A starlette application will always
                automatically include two middleware classes. `ServerErrorMiddleware` is added as the very
                outermost middleware, to handle any uncaught errors occurring anywhere in the entire stack.
                `ExceptionMiddleware` is added as the very innermost middleware, to deal with handled
                exception cases occurring in the routing or endpoints.
            exception_handlers: A mapping of either integer status codes, or exception class types onto
                callables which handle the exceptions. Exception handler callables should be of the form
                `handler(request, exc) -> response` and may be either standard functions, or async functions.
            on_startup: A list of callables to run on application startup. Startup handler callables do not
                take any arguments, and may be either standard functions, or async functions.
            on_shutdown: A list of callables to run on application shutdown. Shutdown handler callables do
                not take any arguments, and may be either standard functions, or async functions.
            lifespan: A lifespan context function, which can be used to perform startup and shutdown tasks.
                This is a newer style that replaces the `on_startup` and `on_shutdown` handlers. Use one or
                the other, not both.
        )r0   r1   r2   r3   r4   r5   r6   requestr   rI   r   c                   sH   t tr
ttttf j|  	
dI dH S )z7Endpoint to run the agent with the provided input data.)r7   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   N)
isinstancer   r   r   r   r   dispatch_request)rK   r7   r.   r&   r(   r,   r%   r'   r)   r/   r$   r-   r+   r*    ^/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/pydantic_ai/ui/ag_ui/app.py	run_agent{   s&   
z#AGUIApp.__init__.<locals>.run_agent/POST)methodsN)rK   r   rI   r   )super__init__router	add_route)selfr7   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   rQ   	__class__rN   rP   rV   *   s   G&
zAGUIApp.__init__)*r7   r8   r$   r9   r%   r:   r&   r;   r'   r<   r(   r   r)   r=   r*   r>   r+   r?   r,   r@   r-   rA   r.   rB   r/   rC   r0   r@   r1   rD   r2   rE   r3   rF   r4   rG   r5   rG   r6   rH   rI   rJ   )__name__
__module____qualname____doc__rV   __classcell__rO   rO   rZ   rP   r#   '   s,    r#   ):r_   
__future__r   collections.abcr   r   r   dataclassesr   typingr   r   typing_extensionsr	   pydantic_air
   pydantic_ai.agentr   pydantic_ai.builtin_toolsr   pydantic_ai.messagesr   pydantic_ai.modelsr   r   pydantic_ai.outputr   r   pydantic_ai.settingsr   pydantic_ai.toolsr   pydantic_ai.toolsetsr   pydantic_ai.usager   r    r   r   _adapterr   starlette.applicationsr   starlette.middlewarer   starlette.requestsr   starlette.responsesr   starlette.routingr    starlette.typesr!   r"   ImportErrorer#   rO   rO   rO   rP   <module>   sD    