
    g<                        d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZ ddlmZ ddlmZ  e       Zej3                  d	d
g ee      g      e ee       edd      fdede
dededee   f
d              Zej3                  dd
g ee      g      e ee      fde
dedefd              Zej;                  d	d
g ee      g      e ee      fde
dedefd              Zy)zc
Endpoints to control callbacks per team

Use this when each team should control its own callbacks
    N)Optional)	APIRouterDependsHeaderHTTPExceptionRequeststatus)verbose_proxy_logger)AddTeamCallbackProxyErrorTypesProxyExceptionTeamCallbackMetadataUserAPIKeyAuth)user_api_key_auth)management_endpoint_wrapperz/team/{team_id:path}/callbackzteam management)tagsdependencieszThe litellm-changed-by header enables tracking of actions performed by authorized users on behalf of other users, providing an audit trail for accountability)descriptiondatahttp_requestteam_iduser_api_key_dictlitellm_changed_byc                 	  K   	 ddl m} |t        dddi      |j                  |dd	
       d{   }|t        ddd| di      |j                  }|j                  di       }t        d&i |}	| j                  dk(  r|	j                  g |	_        | j                  |	j                  v rHt        d| j                   d| d|	j                   t        j                  t        j                  d      |	j                  j                  | j                         n| j                  dk(  r|	j                   g |	_        | j                  |	j                   v rHt        d| j                   d| d|	j                    t        j                  t        j                  d      |	j                   j                  | j                         n@| j                  dk(  r0|	j                  g |	_        |	j                   g |	_        | j                  |	j                  v rHt        d| j                   d| d|	j                   t        j                  t        j                  d      | j                  |	j                   v rHt        d| j                   d| d|	j                    t        j                  t        j                  d      |	j                  j                  | j                         |	j                   j                  | j                         | j"                  j%                         D ]'  \  }
}|	j"                  i |	_        ||	j"                  |
<   ) |	j'                         }||d<   t)        j*                  |      }|j,                  j.                  j1                  d|id|i       d{   }d|dS 7 {7 # t2        $ r*}t5        j6                  dj9                  t;        |                   t5        j<                  t?        j@                                tC        |t              rct        tE        |ddt;        |       d       t        jF                  jH                  tE        |d!d"      tE        |d#t        jJ                        $      tC        |t              r|t        d%t;        |      z   t        jF                  jH                  tE        |d!d"      t        jJ                  $      d}~ww xY ww)'a_  
    Add a success/failure callback to a team

    Use this if if you want different teams to have different success/failure callbacks

    Parameters:
    - callback_name (Literal["langfuse", "langsmith", "gcs"], required): The name of the callback to add
    - callback_type (Literal["success", "failure", "success_and_failure"], required): The type of callback to add. One of:
        - "success": Callback for successful LLM calls
        - "failure": Callback for failed LLM calls
        - "success_and_failure": Callback for both successful and failed LLM calls
    - callback_vars (StandardCallbackDynamicParams, required): A dictionary of variables to pass to the callback
        - langfuse_public_key: The public key for the Langfuse callback
        - langfuse_secret_key: The secret key for the Langfuse callback
        - langfuse_secret: The secret for the Langfuse callback
        - langfuse_host: The host for the Langfuse callback
        - gcs_bucket_name: The name of the GCS bucket
        - gcs_path_service_account: The path to the GCS service account
        - langsmith_api_key: The API key for the Langsmith callback
        - langsmith_project: The project for the Langsmith callback
        - langsmith_base_url: The base URL for the Langsmith callback

    Example curl:
    ```
    curl -X POST 'http:/localhost:4000/team/dbe2f686-a686-4896-864a-4c3924458709/callback'         -H 'Content-Type: application/json'         -H 'Authorization: Bearer sk-1234'         -d '{
        "callback_name": "langfuse",
        "callback_type": "success",
        "callback_vars": {"langfuse_public_key": "pk-lf-xxxx1", "langfuse_secret_key": "sk-xxxxx"}
        
    }'
    ```

    This means for the team where team_id = dbe2f686-a686-4896-864a-4c3924458709, all LLM calls will be logged to langfuse using the public key pk-lf-xxxx1 and the secret key sk-xxxxx

    r   prisma_clientN  errorNo db connectedstatus_codedetailteamfind_uniquer   
table_name
query_typei  
Team id = z0 does not exist. Please use a different team id.callback_settingssuccesszcallback_name = z3 already exists in failure_callback, for team_id = z . 
 Existing failure_callback = callback_name)messagecodetypeparamfailuresuccess_and_failurez3 already exists in success_callback, for team_id = z . 
 Existing success_callback = r   metadatawherer   r	   r   zGlitellm.proxy.proxy_server.add_team_callbacks(): Exception occured - {}r"   Internal Server Error()r/   Noner!   r,   r.   r/   r-   Internal Server Error,  )&litellm.proxy.proxy_serverr   r   get_datar2   getr   callback_typesuccess_callbackr+   r   r	   HTTP_400_BAD_REQUESTr   bad_request_errorappendfailure_callbackcallback_varsitems
model_dumpjsondumpsdblitellm_teamtableupdate	Exceptionr
   r   formatstrdebug	traceback
format_exc
isinstancegetattrinternal_server_errorvalueHTTP_500_INTERNAL_SERVER_ERROR)r   r   r   r   r   r   _existing_teamteam_metadatateam_callback_settingsteam_callback_settings_objvarrV   team_callback_settings_obj_dictteam_metadata_jsonnew_team_rowes                   o/var/www/openai/venv/lib/python3.12/site-packages/litellm/proxy/management_endpoints/team_callback_endpoints.pyadd_team_callbacksrb      s1    lo
< CBS8TUU  -55=  6  
 
 !z'2bc  '//!.!2!23F!K%9%S<R%S"*)::B>@*;!!%?%P%PP$.t/A/A.BBuv}u~  `  a{  aL  aL  `M  N44(::)	  '77>>t?Q?QR9,)::B>@*;!!%?%P%PP$.t/A/A.BBuv}u~  `  a{  aL  aL  `M  N44(::)	  '77>>t?Q?QR#88)::B>@*;)::B>@*;!!%?%P%PP$.t/A/A.BBuv}u~  `  a{  aL  aL  `M  N44(::)	  !!%?%P%PP$.t/A/A.BBuv}u~  `  a{  aL  aL  `M  N44(::)	  '77>>t?Q?QR&77>>t?Q?QR,,224JC)77?;=*8<A&44S9 5
 +E*O*O*Q'-L)*!ZZ6*--??FFg&j:L-M G 
 

   
 	
]
T
  
""U\\A	

 	""9#7#7#9:a' 8/Ec!fXQ-OP$::@@a&1Qv/T/TU	  >*G-A6 66<<!Wf-66	
 	
!
sL   S).N2 N-M0N2 #N0$N2 ,S)-N2 0N2 2
S&<D%S!!S&&S)z/team/{team_id}/disable_loggingc                 v  K   	 ddl m} |t        dddi      |j                  |dd	
       d{   }|t        ddd| di      |j                  }|j                  di       }t        d i |}g |_        g |_        |j                         |d<   t        j                  |      }|j                  j                  j                  d|id|i       d{   }	|	t        ddd| di      dd| |	j                  g g ddS 7 7 2# t         $ r}
t#        j$                  dt'        |
              t#        j(                  t+        j,                                t/        |
t              rct1        t3        |
ddt'        |
       d      t4        j6                  j8                  t3        |
dd      t3        |
dt:        j<                              t/        |
t0              r|
t1        dt'        |
      z   t4        j6                  j8                  t3        |
dd      t:        j<                        d}
~
ww xY ww)!a8  
    Disable all logging callbacks for a team

    Parameters:
    - team_id (str, required): The unique identifier for the team

    Example curl:
    ```
    curl -X POST 'http://localhost:4000/team/dbe2f686-a686-4896-864a-4c3924458709/disable_logging'         -H 'Authorization: Bearer sk-1234'
    ```


    r   r   Nr   r   r   r    r#   r$   r%     r(    does not exist.r)   r   r2   r3   z, does not exist. Error updating team loggingr*   zLogging disabled for team )r   success_callbacksfailure_callbacks)r	   r,   r   zHlitellm.proxy.proxy_server.disable_team_logging(): Exception occurred - r"   r6   r7   r/   r8   r!   r9   r:   r;   )r<   r   r   r=   r2   r>   r   r@   rD   rG   rH   rI   rJ   rK   rL   r   rM   r
   r   rO   rP   rQ   rR   rS   r   rT   r   rU   rV   r	   rW   )r   r   r   r   rX   rY   rZ   r[   r^   updated_teamr`   s              ra   disable_team_loggingri      ss    2G
< CBS8TUU  -55=  6  
 
 !:gY6F!GH  '//!.!2!23F!K%9%S<R%S" 79"368"3 .H-R-R-T)*!ZZ6 +--??FFg&j:L-M G 
 
 z'2^_   3G9='//%'%'
 	
G
.
,  
""VWZ[\W]V^_	
 	""9#7#7#9:a' 8/Ec!fXQ-OP$::@@a&1Qv/T/TU	  >*G-A6 66<<!Wf-66	
 	

sL   H9.D D
B&D D/D 	H9
D D 
H6DH11H66H9c                   K   	 ddl m} |t        dddi      |j                  |dd	
       d{   }|t        ddd| di      |j                  }|j                  di       }t        di |}d||j                  |j                  |j                  ddS 7 j# t        $ r*}t        j                  dj                  t        |                   t        j                  t!        j"                                t%        |t              rct'        t)        |ddt        |       d      t*        j,                  j.                  t)        |dd      t)        |dt0        j2                              t%        |t&              r|t'        dt        |      z   t*        j,                  j.                  t)        |dd      t0        j2                        d}~ww xY ww)a  
    Get the success/failure callbacks and variables for a team

    Parameters:
    - team_id (str, required): The unique identifier for the team

    Example curl:
    ```
    curl -X GET 'http://localhost:4000/team/dbe2f686-a686-4896-864a-4c3924458709/callback'         -H 'Authorization: Bearer sk-1234'
    ```

    This will return the callback settings for the team with id dbe2f686-a686-4896-864a-4c3924458709

    Returns {
            "status": "success",
            "data": {
                "team_id": team_id,
                "success_callbacks": team_callback_settings_obj.success_callback,
                "failure_callbacks": team_callback_settings_obj.failure_callback,
                "callback_vars": team_callback_settings_obj.callback_vars,
            },
        }
    r   r   Nr   r   r   r    r#   r$   r%   rd   r(   re   r)   r*   )r   rf   rg   rE   r5   zHlitellm.proxy.proxy_server.get_team_callbacks(): Exception occurred - {}r"   r6   r7   r/   r8   r!   r9   r:   r;   )r<   r   r   r=   r2   r>   r   r@   rD   rE   rM   r
   r   rN   rO   rP   rQ   rR   rS   r   rT   r   rU   rV   r	   rW   )	r   r   r   r   rX   rY   rZ   r[   r`   s	            ra   get_team_callbacksrk   &  s    F6
< CBS8TUU  -55=  6  
 
 !:gY6F!GH  '//!.!2!23F!K &:%S<R%S"  "%?%P%P%?%P%P!;!I!I	
 	
!
4  
""V]]A	

 	""9#7#7#9:a' 8/Ec!fXQ-OP$::@@a&1Qv/T/TU	  >*G-A6 66<<!Wf-66	
 	
!
s:   G.B BA)B GB 
G)D%GGG)__doc__rH   rQ   typingr   fastapir   r   r   r   r   r	   litellm._loggingr
   litellm.proxy._typesr   r   r   r   r   $litellm.proxy.auth.user_api_key_authr   &litellm.proxy.management_helpers.utilsr   routerpostrO   rb   ri   r>   rk   r;       ra   <module>rv      s      N N 1  C N	 #
	+,-  
 
 )00A(B(. t)_

_
_
 _
 &	_

 !_
 _
D %
	+,-  
  )00A(BZ
Z
Z
 &Z
 Z
z #
	+,-  
  )00A(BS
S
S
 &S
 S
ru   