
    Jic                        d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZmZ  ed      Z G d dee         Z G d d	e      Zddd
Z G d d      Zy)    )annotationsN)Any	AwaitableCallableGenericTypeVarcast)	BaseModel
ConfigDictTc                       e Zd ZdZddZddZy)	_ResourcezInternal wrapper for resource factories.

    Wraps sync/async factories and records metadata such as the qualified name
    and cache behavior.
    c                    || _         t        j                  |      | _        t	        |dt        |      j                        | _        || _        y )N__qualname__)	_factoryinspectiscoroutinefunction	_is_asyncgetattrtype__name__namecache)selffactoryr   s      d/var/www/html/BankruptcyAI-uat/bankruptcy-ai/venv/lib/python3.12/site-packages/workflows/resource.py__init__z_Resource.__init__   s<     44W=G^T']5K5KL	
    c                   K   | j                   r9 t        t        dt        t           f   | j
                                d{   }|S  t        t        dt        f   | j
                               }|S 7 0w)z5Invoke the underlying factory, awaiting if necessary..N)r   r	   r   r   r   r   )r   results     r   callz_Resource.call%   se     >>K4il): ;T]]KMMF  ;T(36*DMM:<F Ns   ?A4A21A4N)r   zCallable[..., T | Awaitable[T]]r   boolreturnNone)r#   r   )r   
__module__r   __doc__r   r!    r   r   r   r      s    r   r   c                  8    e Zd ZU dZ ed      Zded<   ded<   y)	ResourceDefinitionzDefinition for a resource injection requested by a step signature.

    Attributes:
        name (str): Parameter name in the step function.
        resource (_Resource): Factory wrapper used by the manager to produce the dependency.
    T)arbitrary_types_allowedstrr   r   resourceN)r   r%   r   r&   r   model_config__annotations__r'   r   r   r)   r)   .   s     d;L
Ir   r)   c                    t        | |      S )a  Declare a resource to inject into step functions.

    Args:
        factory (Callable[..., T]): Function returning the resource instance. May be async.
        cache (bool): If True, reuse the produced resource across steps. Defaults to True.

    Returns:
        _Resource[T]: A resource descriptor to be used in `typing.Annotated`.

    Examples:
        ```python
        from typing import Annotated
        from workflows.resource import Resource

        def get_memory(**kwargs) -> Memory:
            return Memory.from_defaults("user123", token_limit=60000)

        class MyWorkflow(Workflow):
            @step
            async def first(
                self,
                ev: StartEvent,
                memory: Annotated[Memory, Resource(get_memory)],
            ) -> StopEvent:
                await memory.aput(...)
                return StopEvent(result="ok")
        ```
    )r   )r   r   s     r   Resourcer0   ;   s    : We$$r   c                  0    e Zd ZdZddZddZd	dZd
dZy)ResourceManagerzManage resource lifecycles and caching across workflow steps.

    Methods:
        set: Manually set a resource by name.
        get: Produce or retrieve a resource via its descriptor.
        get_all: Return the internal name->resource map.
    c                    i | _         y )N	resourcesr   s    r   r   zResourceManager.__init__d   s	    )+r   c                F   K   | j                   j                  ||i       yw)z*Register a resource instance under a name.N)r5   update)r   r   vals      r   setzResourceManager.setg   s     tSk*s   !c                  K   |j                   s|j                          d{   }|S |j                   rd| j                  j                  |j                  d      s>|j                          d{   }| j                  |j                  |       d{    |S | j                  j                  |j                        }|S 7 7 S7 1w)z4Return a resource instance, honoring cache settings.N)r   r!   r5   getr   r:   )r   r,   r9   s      r   r<   zResourceManager.getk   s     ~~ 'C 
 ^^DNN$6$6x}}d$K 'C((8==#... 
 ..$$X]]3C
 ('.s4    CC AC.C/#CC.CCCc                    | j                   S )z"Return all materialized resources.r4   r6   s    r   get_allzResourceManager.get_allv   s    ~~r   N)r#   r$   )r   r+   r9   r   r#   r$   )r,   r   r#   r   )r#   zdict[str, Any])r   r%   r   r&   r   r:   r<   r>   r'   r   r   r2   r2   [   s    ,+	r   r2   )T)r   zCallable[..., T]r   r"   r#   z_Resource[T])
__future__r   r   typingr   r   r   r   r   r	   pydanticr
   r   r   r   r)   r0   r2   r'   r   r   <module>rB      sS    #  
 CL
 ,
 
%@ r   