
    i?                        S SK Jr  S SKrS SKJrJrJrJrJrJ	r	  S SK
JrJr  \" S5      r " S S\\   5      r " S S	\5      rSSS
 jjr " S S5      rg)    )annotationsN)Any	AwaitableCallableGenericTypeVarcast)	BaseModel
ConfigDictTc                  ,    \ rS rSrSrSS jrSS jrSrg)		_Resource   zInternal wrapper for resource factories.

Wraps sync/async factories and records metadata such as the qualified name
and cache behavior.
c                t    Xl         [        R                  " U5      U l        UR                  U l        X l        g N)_factoryinspectiscoroutinefunction	_is_async__qualname__namecache)selffactoryr   s      L/home/james-whalen/.local/lib/python3.13/site-packages/workflows/resource.py__init___Resource.__init__   s+     44W=((	
    c                   #    U R                   (       a9  [        [        S[        [           4   U R
                  5      " 5       I Sh  vN nU$ [        [        S[        4   U R
                  5      " 5       nU$  N07f)z5Invoke the underlying factory, awaiting if necessary..N)r   r	   r   r   r   r   )r   results     r   call_Resource.call%   s`     >>il): ;T]]KMMF  (36*DMM:<F Ns   AA9A71A9)r   r   r   r   N)r   zCallable[..., T | Awaitable[T]]r   boolreturnNone)r$   r   )__name__
__module__r   __firstlineno____doc__r   r!   __static_attributes__ r   r   r   r      s    r   r   c                  :    \ rS rSr% Sr\" SS9rS\S'   S\S'   S	rg
)ResourceDefinition.   zDefinition 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   resourcer+   N)	r&   r'   r   r(   r)   r   model_config__annotations__r*   r+   r   r   r-   r-   .   s     d;L
Ir   r-   c                    [        X5      $ )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   Resourcer5   ;   s    : W$$r   c                  @    \ rS rSrSrS	S jrS
S jrSS jrSS jrSr	g)ResourceManager[   zManage 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                    0 U l         g r   	resourcesr   s    r   r   ResourceManager.__init__d   s	    )+r   c                D   #    U R                   R                  X05        g7f)z*Register a resource instance under a name.N)r;   update)r   r   vals      r   setResourceManager.setg   s     tk*s    c                  #    UR                   (       d  UR                  5       I Sh  vN nU$ UR                   (       ai  U R                  R                  UR                  S5      (       d>  UR                  5       I Sh  vN nU R                  UR                  U5      I Sh  vN   U$ U R                  R                  UR                  5      nU$  N NS N17f)z4Return a resource instance, honoring cache settings.N)r   r!   r;   getr   rA   )r   r1   r@   s      r   rD   ResourceManager.getk   s     ~~ 'C 
 ^^DNN$6$6x}}d$K$K 'C((8==#... 
 ..$$X]]3C
 ('.s4   %CCAC=C>#C!C".CCCc                    U R                   $ )z"Return all materialized resources.r:   r<   s    r   get_allResourceManager.get_allv   s    ~~r   r:   N)r$   r%   )r   r0   r@   r   r$   r%   )r1   r   r$   r   )r$   zdict[str, Any])
r&   r'   r   r(   r)   r   rA   rD   rG   r*   r+   r   r   r7   r7   [   s    ,+	r   r7   )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-   r5   r7   r+   r   r   <module>rL      sS    #  
 CL
 ,
 
%@ r   