
    jY"                         d dl 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 d dlmZ d d	lmZ d
dlmZ d
dlmZ  G d d      Zy)    N)client)core)RequestOptions)AgentBlueprintWrite)AgentConfigValueWrite)AgentConfigEnv)rest_helpers)
id_helpers   )	Blueprint)type_helpersc                   J   e Zd ZdZdedej                  ddfdZedefd       Z	e
dej                  ej                  eej                  f      d	ej                  ej                  eej                  ej                  ej                  ej                  e   f   f      dej                  eej                  ej                  ej                  ej                  e   f   f   fd
       Z	 	 dd	ej                  eej                  ej                  ej                  ej                  e   f   f   dej                  e   dej                  e   dedef
dZdddddddej                  e   dej                  e   dej                  e   dej                  ej                  eej                  f      dej                  e   dej                  e   fdZ	 	 	 	 ddej                  ej                  eej                  f      d	ej                  ej                  eej                  ej                  ej                  ej                  e   f   f      dej                  e   dej                  ej                  eej                  f      def
dZdededdfdZ	 	 	 ddej                  ej                  eej                  f      d	ej                  ej                  eej                  ej                  ej                  ej                  e   f   f      dej                  e   defdZy)AgentConfigManageru?   Project-level agent config entity — internal REST operations.project_namerest_client_returnNc                      || _         || _        y N)_project_name_rest_client)selfr   r   s      }/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/api_objects/agent_config/config.py__init__zAgentConfigManager.__init__   s    
 *(    c                     | j                   S r   )r   )r   s    r   r   zAgentConfigManager.project_name   s    !!!r   
parametersfields_with_valuesc                     ||S | xs i j                         D ci c]  \  }}||t        |      |d f c}}S c c}}w r   )itemstype)r   r   kvs       r   _resolve_fields_with_valuesz.AgentConfigManager._resolve_fields_with_values   s^     )%%
 $)r002
21} QD!!2
 	
 
s   ;descriptionidconfig_typec                     g }|j                         D ]Q  \  }\  }}}	||j                  t        |t        j                  |      t        j
                  ||      |	             S t        ||||      S )N)keyr    valuer$   )r%   r    valuesr$   )r   appendr   r   python_type_to_backend_typepython_value_to_backend_valuer   )
r   r   r$   r%   r&   backend_values
field_namepy_typer)   
field_descs
             r   _build_blueprint_payloadz+AgentConfigManager._build_blueprint_payload/   s     8J8P8P8R4J4%}!!%"%AA'J&DDUGT *	 9S #!#	
 	
r   )nameenvmask_idfield_typestimeout_in_secondsr3   r4   r5   r6   r7   c                   |t        |      nd}	 t        j                  | j                  | j                        }|*| j                  j
                  j                  ||||      }nT|*| j                  j
                  j                  ||||      }n(| j                  j
                  j                  |||      }t        ||| j                        S # t        j                  $ r}	|	j                  dk(  rY d}	~	y d}	~	ww xY w)a0  Fetch a blueprint by name, environment name, or latest.

        Priority: ``name`` > ``env`` > latest.
        Returns ``None`` if not found.

        Args:
            name: Fetch the blueprint with this version name.
            env: Fetch the blueprint tagged with this environment name.
            mask_id: ID of a mask blueprint to overlay on the result.
            field_types: Mapping of prefixed field key to Python type used
                for deserialising backend values.
            timeout_in_seconds: HTTP request timeout in seconds.
        N)r7   )
project_idr3   r5   request_options)env_namer9   r5   r:   )r9   r5   r:   i  raw_blueprintr6   r   )r   r	   resolve_project_id_by_namer   r   agent_configsget_blueprint_by_nameget_blueprint_by_envget_latest_blueprintrest_api_coreApiErrorstatus_coder   )
r   r3   r4   r5   r6   r7   r:   r9   rawes
             r   get_blueprintz AgentConfigManager.get_blueprintK   s#   0 "- .@A 	
	%@@!!4#5#5J ''55KK)#$3	 L  ''55JJ )#$3	 K  ''55JJ)#$3 K  #**
 	
	 %% 	}}#	s   B*C D'C<;C<<Dc                 R   | j                  ||      }t        j                         }| j                  |||      }| j                  j
                  j                  || j                         | j                  j
                  j                  |      }t        ||| j                        S )a  Create a new blueprint and return it.

        Pass either ``parameters`` (plain key-value pairs whose types are
        inferred) or ``fields_with_values`` (explicit ``{key: (type, value)}``
        mapping). If both are given ``fields_with_values`` takes precedence.

        Args:
            parameters: Plain ``{field_name: value}`` dict; types are inferred
                via ``type(value)``.
            fields_with_values: Explicit ``{field_name: (python_type, value)}``
                mapping, bypassing type inference.
            description: Human-readable description stored with the blueprint.
            field_types: Mapping of prefixed field key to Python type used
                when fetching back the created blueprint.
        )r%   	blueprintr   r<   )
r#   r
   generate_idr2   r   r?   create_agent_configr   get_blueprint_by_idr   )r   r   r   r$   r6   blueprint_idpayloadrF   s           r   create_blueprintz#AgentConfigManager.create_blueprint   s    0 "==*
 "--/// 0 
 	'';;++ 	< 	
 --AA,O#**
 	
r   rO   c                     t        j                  | j                  | j                        }| j                  j                  j                  |t        ||      g       y)zAssociate a blueprint with an environment name.

        Args:
            env: Environment name (e.g. ``"production"``).
            blueprint_id: ID of the blueprint to tag.
        )r;   rO   )r9   envsN)r	   r>   r   r   r?   create_or_update_envsr   )r   r4   rO   r9   s       r   tag_blueprint_with_envz)AgentConfigManager.tag_blueprint_with_env   sV     "<<t11

 	''==! #LIJ 	> 	
r   c                     | j                  ||      }t        j                         }| j                  |||d      }| j                  j
                  j                  || j                         |S )a  Create a mask blueprint and return its ID.

        A mask overlays a subset of fields on top of an existing blueprint.
        Apply it by passing the returned ID to ``get_blueprint(mask_id=...)``.

        Args:
            parameters: Plain ``{field_name: value}`` dict; types are inferred
                via ``type(value)``.
            fields_with_values: Explicit ``{field_name: (python_type, value)}``
                mapping, bypassing type inference.
            description: Human-readable description stored with the mask.
        mask)r%   r&   rJ   )r#   r
   rL   r2   r   r?   rM   r   )r   r   r   r$   r5   rP   s         r   create_maskzAgentConfigManager.create_mask   s}    ( "==*
 ((*//V 0 
 	'';;++ 	< 	
 r   )NrK   )NNNN)NNN)__name__
__module____qualname____doc__strrest_clientOpikApir   propertyr   staticmethodtypingOptionalDictAnyTupler#   r   r2   intr   rH   rQ   rU   rX    r   r   r   r      s   I)) "))) 
	) "c " " 
OOFKKVZZ$@A
"OOKKV\\&**fjj&//RUBV*VWWX


 
S&,,vzz6::vs?S'STT	U
 
, $(&
"KKfjj&**fooc6JJKK


 __S)
 OOC 
 
 

> &*$((,EI37;
 ooc";
 __S!	;

 %;
 __V[[fjj%AB;
 #OOC0;
 
	#;
~ EI ,0EI(
OOFKKVZZ$@A(
 #OOKKV\\&**fjj&//RUBV*VWWX
(
 __S)(
 __V[[fjj%AB(
 
(
T
# 
S 
T 
" EI ,0OOFKKVZZ$@A #OOKKV\\&**fjj&//RUBV*VWWX
 __S) 
r   r   )rb   opik.rest_apir   r^   r   rC   "opik.rest_api.core.request_optionsr   )opik.rest_api.types.agent_blueprint_writer   ,opik.rest_api.types.agent_config_value_writer   $opik.rest_api.types.agent_config_envr   opik.api_objectsr	   opikr
   rK   r    r   r   rh   r   r   <module>rq      s2     / / = I N ? )    Q Qr   