
    &`iD              	       x   d dl Z d dlZd dlmZ d dl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 d d	lmZ  ej        e          Ze
fd
ededefdZ ed          d
edefd            Z ed          d
efd            Z ed          	 ddddee         dee         defd            ZdS )    N)wraps)CallableOptional)import_attr)DEFAULT_CONSUMER_CONCURRENCYSERVE_LOGGER_NAME)TaskConsumerWrapper)copy_class_metadata)TaskProcessorAdapterTaskProcessorConfig)	PublicAPItask_processor_configconsumer_concurrencyreturnc                 H   | j         }t          |t                    rt          |          }n9t	          |          r|}n't          dt          |          j         d|           	  ||           }n,# t          $ r}t          d|j         d|           d }~ww xY wt          |t                    s+t          |j         dt          |          j                   	 |                    |           n,# t          $ r}t          d|j         d|           d }~ww xY w|S )Nz>Adapter must be either a string path or a callable class, got z: zFailed to instantiate z- must inherit from TaskProcessorAdapter, got zFailed to initialize )adapter
isinstancestrr   callable	TypeErrortype__name__	ExceptionRuntimeErrorr   
initialize)r   r   r   adapter_classadapter_instancees         k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/serve/task_consumer.py_instantiate_adapterr       s    $+G '3 	
#G,,	'		 
 pTRY]]Mcppgnpp
 
 	
S(=)>?? S S SQM4JQQaQQRRRS &(<== 
%uuTXYiTjTjTsuu
 
 	
R##$89999 R R RP=3IPPQPPQQQR s0   'A3 3
B=BB C6 6
D DDalpha)	stabilityc                      t          |           S )a  
    Create a TaskProcessorAdapter instance from the provided configuration and call .initialize(). This function supports two ways to specify an adapter:

    1. String path: A fully qualified module path to an adapter class
       Example: "ray.serve.task_processor.CeleryTaskProcessorAdapter"

    2. Class reference: A direct reference to an adapter class
       Example: CeleryTaskProcessorAdapter

    Args:
        task_processor_config: Configuration object containing adapter specification.
    Returns:
        An initialized TaskProcessorAdapter instance ready for use.

    Raises:
        ValueError: If the adapter string path is malformed or cannot be imported.
        TypeError: If the adapter is not a string or callable class.

    Example:
        .. code-block:: python

            config = TaskProcessorConfig(
                adapter="my.module.CustomAdapter",
                adapter_config={"param": "value"},
                queue_name="my_queue"
            )
            adapter = instantiate_adapter_from_config(config)
    )r    )r   s    r   instantiate_adapter_from_configr$   :   s    B   5666    c                       fd}|S )a  
    Decorator to mark a class as a TaskConsumer.

    Args:
        task_processor_config: Configuration for the task processor (required)

    Note:
        This decorator must be used with parentheses:
        @task_consumer(task_processor_config=config)

    Returns:
        A wrapper class that inherits from the target class and implements the task consumer functionality.

    Example:
        .. code-block:: python

            from ray import serve
            from ray.serve.task_consumer import task_consumer, task_handler

            @serve.deployment
            @task_consumer(task_processor_config=config)
            class MyTaskConsumer:

                @task_handler(name="my_task")
                def my_task(self, *args, **kwargs):
                    pass

    c                 X      G  fdd t                     }t          |            |S )Nc                   B    e Zd ZU eed<    fdZdef fdZ fdZdS )>task_consumer.<locals>.decorator.<locals>._TaskConsumerWrapper_adapterc                 *     j         | g|R i | d S N)__init__)selfargskwargs
target_clss      r   r-   zGtask_consumer.<locals>.decorator.<locals>._TaskConsumerWrapper.__init__   s,    #
#D:4:::6:::::r%   r   c                    t          |          | _        t          j        t          j                  D ]R\  }}t          |dd          r<t          |d|          }t          | |          }| j                            ||           S	 | j                                         t          	                    d           d S # t          $ r#}t                              d|             d }~ww xY w)N)	predicate_is_task_handlerF
_task_namez"task consumer started successfullyzFailed to start task consumer: )r    r*   inspect
getmembers
isfunctiongetattrregister_task_handlestart_consumerloggerinfor   error)	r.   r   namemethod	task_namebound_methodr   r1   r   s	          r   initialize_callablezRtask_consumer.<locals>.decorator.<locals>._TaskConsumerWrapper.initialize_callable   s    4)+?! ! %,$6'*<% % % 	T 	TLD& v'95AA T$+FL$$G$G	 (/tT':':::<SSSM00222KK DEEEEE    LL!F1!F!FGGGs   3C   
C-
C((C-c                     | j                                          | j                                          t          d          r                    |            d S d S )N__del__)r*   stop_consumershutdownhasattrrE   )r.   r1   s    r   rE   zFtask_consumer.<locals>.decorator.<locals>._TaskConsumerWrapper.__del__   sa    ++---&&(((:y11 -&&t,,,,,- -r%   N)	r   
__module____qualname__r   __annotations__r-   intrC   rE   )r1   r   s   r   _TaskConsumerWrapperr)   ~   s         ****; ; ; ; ;       .- - - - - - -r%   rM   )r	   r
   )r1   rM   r   s   ` r   	decoratorz task_consumer.<locals>.decorator}   sZ    "	- "	- "	- "	- "	- "	- "	- "	-:/B "	- "	- "	-H 	0*===##r%    )r   rN   s   ` r   task_consumerrP   ^   s%    >'$ '$ '$ '$ '$R r%   )r?   _funcr?   c                    ;t          t                    r                                st          d           fd}|  ||           S |S )a  
    Decorator to mark a method as a task handler.
    Optionally specify a task name. Default is the method name.

    Arguments:
        _func: The function to decorate.
        name: The name of the task. Default is the method name.

    Returns:
        A wrapper function that is marked as a task handler.

    Example:
        .. code-block:: python

            from ray import serve
            from ray.serve.task_consumer import task_consumer, task_handler

            @serve.deployment
            @task_consumer(task_processor_config=config)
            class MyTaskConsumer:

                @task_handler(name="my_task")
                def my_task(self, *args, **kwargs):
                    pass

    Nz*Task name must be a non-empty string, got c                      t          j                   s1t                      fd            }d|_        p j        |_        |S t          d          )Nc                       | i |S r,   rO   )r/   r0   fs     r   wrapperz0task_handler.<locals>.decorator.<locals>.wrapper   s    q$)&)))r%   Tz)Async task handlers are not supported yet)r6   iscoroutinefunctionr   r4   r   r5   NotImplementedError)rU   rV   r?   s   ` r   rN   ztask_handler.<locals>.decorator   sp    *1-- 	S1XX* * * * X* (,G$!%!3GN &&QRRRr%   )r   r   strip
ValueError)rQ   r?   rN   s    ` r   task_handlerr[      s}    @ D#!6!6djjllLdLLMMMS S S S S y r%   r,   )r6   logging	functoolsr   typingr   r   ray._common.utilsr   ray.serve._private.constantsr   r    ray.serve._private.task_consumerr	   ray.serve._private.utilsr
   ray.serve.schemar   r   ray.util.annotationsr   	getLoggerr<   rL   r    r$   rP   r   r[   rO   r%   r   <module>rf      s           % % % % % % % % ) ) ) ) ) )        A @ @ @ @ @ 8 8 8 8 8 8        + * * * * *		,	-	-
 !=! !.!! ! ! ! !H W 7. 7 7  7  7  7F WG,? G G G GT W $6?C6 6 6H6/7}66 6 6 6 6 6r%   