
    iA                        d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZ  ej                  e      ZddlmZ  e         e
dedef   	      Zd
efdZdZdZ	 ddlmZ ej0                  j3                  dd      j5                         dk(  rej7                  d       dZ	 	 	 	 d!dedz  dededeeef   dz  ded
eegef   fdZ	 	 	 	 	 d"dedz  dededeeef   dz  de	d   ded
eegef   fdZ	 	 	 	 	 d"dedz  dededeeef   dz  de	d   ded
eegef   fdZ d
efdZ!d
efdZ"d
eeef   fd Z#y# e$ rC ej0                  j3                  dd      j5                         dk(  rej7                  d       dZY w xY w)#a  
Observability module for browser-use

This module provides observability decorators that optionally integrate with lmnr (Laminar) for tracing.
If lmnr is not installed, it provides no-op wrappers that accept the same parameters.

Features:
- Optional lmnr integration - works with or without lmnr installed
- Debug mode support - observe_debug only traces when in debug mode
- Full parameter compatibility with lmnr observe decorator
- No-op fallbacks when lmnr is unavailable
    N)Callable)wraps)AnyLiteralTypeVarcast)load_dotenvF.)boundreturnc                  X    t        j                  dd      j                         } | dk(  ryy)zMCheck if we're in debug mode based on environment variables or logging level.LMNR_LOGGING_LEVEL debugTF)osgetenvlower)lmnr_debug_modes    c/mnt/e/genesis-system/.venvs/browser-army/lib/python3.12/site-packages/browser_use/observability.py_is_debug_moder      s,     99126<<>w	    F)observe!BROWSER_USE_VERBOSE_OBSERVABILITYfalsetruez#Lmnr is available for observabilityTz'Lmnr is not available for observabilitynameignore_inputignore_outputmetadatakwargsc                 4    ddl dt        dt        ffd}|S )zSCreate a no-op decorator that accepts all lmnr observe parameters but does nothing.r   Nfuncr   c                      j                         r#t                fd       }t        t        |      S t                fd       }t        t        |      S )Nc                  0   K    | i | d {   S 7 wN argsr    r"   s     r   async_wrapperzA_create_no_op_decorator.<locals>.decorator.<locals>.async_wrapperG   s     t&v&&&&s   c                       | i |S r%   r&   r'   s     r   sync_wrapperz@_create_no_op_decorator.<locals>.decorator.<locals>.sync_wrapperN   s       r   )iscoroutinefunctionr   r   r
   )r"   r)   r+   asyncios   `  r   	decoratorz*_create_no_op_decorator.<locals>.decoratorD   s]      &	$K' ' q-
   
$K! ! q,
r   )r-   r
   )r   r   r   r   r    r.   r-   s         @r   _create_no_op_decoratorr/   :   s#      Q  1    	r   	span_type)DEFAULTLLMTOOLc                     | ||||ddgd|}t         r-t        r't        t        t        gt        f   t        di |      S t        di |S )a  
	Observability decorator that traces function execution when lmnr is available.

	This decorator will use lmnr's observe decorator if lmnr is installed,
	otherwise it will be a no-op that accepts the same parameters.

	Args:
	    name: Name of the span/trace
	    ignore_input: Whether to ignore function input parameters in tracing
	    ignore_output: Whether to ignore function output in tracing
	    metadata: Additional metadata to attach to the span
	    **kwargs: Additional parameters passed to lmnr observe

	Returns:
	    Decorated function that may be traced depending on lmnr availability

	Example:
	    @observe(name="my_function", metadata={"version": "1.0"})
	    def my_function(param1, param2):
	        return param1 + param2
	r   observe_debugr   r   r   r   r0   tagsr&   )_LMNR_AVAILABLE_lmnr_observer   r   r
   r/   r   r   r   r   r0   r    s         r   r   r   W   sc    <  o
&  	hsAv 7 7	88 
!	*6	**r   c                     | ||||dgd|}t         r7t        r1t               r't        t        t
        gt
        f   t        di |      S t        di |S )a  
	Debug-only observability decorator that only traces when in debug mode.

	This decorator will use lmnr's observe decorator if both lmnr is installed
	AND we're in debug mode, otherwise it will be a no-op.

	Debug mode is determined by:
	- DEBUG environment variable set to 1/true/yes/on
	- BROWSER_USE_DEBUG environment variable set to 1/true/yes/on
	- Root logging level set to DEBUG or lower

	Args:
	    name: Name of the span/trace
	    ignore_input: Whether to ignore function input parameters in tracing
	    ignore_output: Whether to ignore function output in tracing
	    metadata: Additional metadata to attach to the span
	    **kwargs: Additional parameters passed to lmnr observe

	Returns:
	    Decorated function that may be traced only in debug mode

	Example:
	    @observe_debug(ignore_input=True, ignore_output=True,name="debug_function", metadata={"debug": True})
	    def debug_function(param1, param2):
	        return param1 + param2
	r5   r6   r&   )r8   r9   r   r   r   r
   r/   r:   s         r   r5   r5      sg    F  
  .*:	hsAv 7 7	88 
!	*6	**r   c                      t         S )z'Check if lmnr is available for tracing.)r8   r&   r   r   is_lmnr_availabler=      s    r   c                      t               S )z'Check if we're currently in debug mode.)r   r&   r   r   is_debug_moder?      s    r   c                  P    t         t               t         t         xr
 t               dS )z1Get the current status of observability features.)lmnr_available
debug_modeobserve_activeobserve_debug_active)r8   r   r&   r   r   get_observability_statusrE      s%     $ #)>n.>		 r   )NFFN)NFFNr1   )$__doc__loggingr   collections.abcr   	functoolsr   typingr   r   r   r   	getLogger__name__loggerdotenvr	   r
   boolr   r8   r9   lmnrr   environgetr   r   ImportErrorstrdictr/   r5   r=   r?   rE   r&   r   r   <module>rV      sp    	 $  . .			8	$   CxS)*  	*JJNN6@FFHFR,,45 #'	
Tz  S>D 	
  qc1f< #'09,+
Tz,+,+ ,+ S>D 	,+
 ,-,+ ,+ qc1f,+` #'091+
Tz1+1+ 1+ S>D 	1+
 ,-1+ 1+ qc1f1+j4 
t 
$sDy/ c  JJNN6@FFHFR,,89s   AD8 8AF ?F 