
    6Ii	/                         S SK r S SKJrJr  S SKrS SKrS SKJr  S SKJ	r	  S SK
JrJr  S SKJr  S SKJr   " S S	\5      r " S
 S5      rg)    N)Any	TypedDict)asizeofconfig)*get_last_workflow_execution_by_workflow_idget_session)AlertDto)IncidentDtoc                   <    \ rS rSr% \S-  \S'   \\   S-  \S'   Srg)ForeachContext   Nvalueitems )__name__
__module____qualname____firstlineno__r   __annotations__list__static_attributes__r       H/venv/lib/python3.13/site-packages/keep/contextmanager/contextmanager.pyr   r      s    :9tr   r   c                       \ rS rSr   SS\S-  4S jjr\S 5       r\S 5       rS r	S r
S	 rS
 rS rS rS rSS jrSS\\   S-  4S jjrSS\S-  4S jjrS r  SS jrS rSS jrS rS rS rSrg) ContextManager   Nworkflowc                 ,   [         R                  " [        5      U l        X l        X0l        Xl        0 U l        SU l        0 U l	        0 U l
        0 U l        S U l        S S S.U l        0 U l        0 U l        0 U l        0 U l         [$        R&                  " 5       U l        0 U l        S U l        U R                  (       ae  U(       a^   [0        R2                  " U5      nSU;   =(       d    SU;   nU(       a/  [5        XSS9nUb"  UR6                  U l        UR8                  U l        0 U l        [A        5       U l!        S U l        S U l"        S U l#        0 U l$        g ! [*         a
    0 U l         Nf = f! [:         a    U R                  R=                  S5         Nqf = f)Nr   r   r   last_workflow_resultslast_workflow_run_timesuccess)statusz%Failed to get last workflow execution)%logging	getLoggerr   loggerworkflow_idworkflow_execution_id	tenant_idsteps_contextsteps_context_sizeproviders_contextactions_contextevent_contextincident_contextforeach_contextconsts_contextcurrent_step_varscurrent_step_aliasessecret_contextclickget_current_contextclick_contextRuntimeErrorlast_workflow_execution_resultsr"   json5dumpsr   resultsstarted	Exception	exceptionaliasessetdependenciesworkflow_inputs_api_key_ContextManager__loggers)selfr*   r(   r)   r   workflow_str!last_workflow_results_in_workflowlast_workflow_executions           r   __init__ContextManager.__init__   s    ''1&%:"""#!#!')480
 !!#$&! 	$!&!:!:!<D 02,&*#  %{{84+|; @/<? 2 5B%9 ,
 /:3;; < 7N6U6U3   E%)"#I  	$!#D	$2  %%&MNs%   E AE+ E('E(+%FFc                     [        S5      $ )z
The URL of the Keep API
KEEP_API_URLr   rG   s    r   api_urlContextManager.api_urlW   s    
 n%%r   c                     SSK Jn  U R                  c;  [        [	        5       5      nU" USU R
                  SS9U l        UR                  5         U R                  $ )Nr   )get_or_create_api_keysystemwebhook)session
created_byr*   unique_api_key_id)keep.api.utils.tenant_utilsrS   rE   nextr	   r*   close)rG   rS   rV   s      r   api_keyContextManager.api_key^   sO     	F== ;=)G1#.."+	DM MMO}}r   c                 j    X l         Xl        U R                  R                  5        H	  nX#l         M     g N)r)   r(   rF   values)rG   r(   r)   r'   s       r   set_execution_context$ContextManager.set_execution_contextn   s+    %:"&nn++-F+@( .r   c                     Xl         g r_   )rD   )rG   inputss     r   
set_inputsContextManager.set_inputst   s    %r   c                     Xl         g r_   )r/   )rG   events     r   set_event_context ContextManager.set_event_contextw   s    "r   c                     Xl         g r_   )r0   )rG   incidents     r   set_incident_context#ContextManager.set_incident_contextz   s     (r   c                     Xl         g r_   )r2   )rG   constss     r   set_consts_context!ContextManager.set_consts_context}   s    $r   c                     U R                   $ r_   )r(   rO   s    r   get_workflow_idContextManager.get_workflow_id   s    r   c                 <   SSK Jn  UR                  U 5      nU R                   SU R                   S3n UR                  USS9nU=(       d    0 U l        g
! [         a;    U R                  R                  SU R                  U R                  S.S	9  0 U l         g
f = f)zp
Set the secret context for the workflow.
If no secret is provided, attempt to load it from the secret manager.
r   )SecretManagerFactory__secretsT)secret_nameis_jsonz#Could not load secrets for workflow)r(   r*   )extraN)
'keep.secretmanager.secretmanagerfactoryrw   get_secret_managerr*   r(   read_secretr5   r?   r'   warning)rG   rw   secret_manager
secret_keysecrets        r   set_secret_context!ContextManager.set_secret_context   s    
 	Q-@@F'q)9)9(:(C
	%#//JPT/UF"(,BD 	%KK5&*&6&6T^^T    #%D	%s    A ABBc                    U R                   U R                   U R                  U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  S.nU(       d  U R                  US'   UR                  U R                  5        U$ )a  
Gets full context on the workflows

Usage: context injection used, for example, in iohandler

Returns:
    dict: dictinoary contains all context about this workflow
          providers - all context about providers (configuration, etc)
          steps - all context about steps (output, conditions, etc)
          foreach - all context about the current 'foreach'
                    foreach can be in two modes:
                        1. "step foreach" - for step result
                        2. "condition foreach" - for each condition result
                    whereas in (2), the {{ foreach.value }} contains (1), in the (1) case, we need to explicitly put in under (value)
                    anyway, this should be refactored to something more structured
)stepsactionsforeachrh   r!   r"   alertrl   rp   varsrA   secretsrd   	providers)r+   r1   r/   r:   r"   r0   r2   r3   r4   r5   rD   r-   updaterA   )rG   exclude_providersexclude_envfull_contexts       r   get_full_contextContextManager.get_full_context   s    $ ''))++''%)%I%I&*&A&A''--))**00****
  !(,(>(>L%DLL)r   r   c                      XR                   S'   g )Nr   r1   )rG   r   s     r   set_foreach_items ContextManager.set_foreach_items       (-W%r   r   c                      XR                   S'   g )Nr   r   )rG   r   s     r   set_foreach_value ContextManager.set_foreach_value   r   r   c                     S S S.U l         g )Nr    r   rO   s    r   reset_foreach_context$ContextManager.reset_foreach_context   s     
r   c	           	         XR                   ;  a  0 0 S.U R                   U'   SU R                   U   ;  a  U/ 0U R                   U   S'   X R                   U   S   ;  a  / U R                   U   S   U'   U R                   U   S   U   R                  UUUUUUS.U	E5        U R                  R                  XTS.U	E5        U(       a  X`R                  U'   gg)a  _summary_

Args:
    action_id (_type_): id of the step
    condition_type (_type_): type of the condition
    compare_to (_type_): _description_
    compare_value (_type_): _description_
    result (_type_): _description_
    condition_alias (_type_, optional): _description_. Defaults to None.
    value (_type_): the raw value which the condition was compared to. this is relevant only for foreach conditions
)
conditionsr=   r   )r   compare_value
compare_toresulttypealias)r   r   N)r+   appendr1   r   rA   )
rG   	action_idcondition_namecondition_typer   r   r   condition_aliasr   kwargss
             r   set_condition_results$ContextManager.set_condition_results   s    . ...;=",MDy)t11)<<;I2:NDy),7!3!3I!>|!LLJLDy),7G9%l3NCJJ!.( &( 
	
 	##+PP	
 ,2LL) r   c                 j    XR                   ;  a  0 / 0 S.U R                   U'   X R                   U   S'   g )Nprovider_parametersr=   r   r   )r+   )rG   step_idr   s      r   set_step_provider_paremeters+ContextManager.set_step_provider_paremeters   s@    ,,,')+Dw'
 >Q7#$9:r   c                 (   XR                   ;  a  0 / 0 S.U R                   U'   U(       a"  U R                   U   S   R                  U5        OX R                   U   S'   U R                   U   U R                   S'   [        U R                   5      U l        g )Nr   r=   this)r+   r   r   r,   )rG   r   r=   r   s       r   set_step_contextContextManager.set_step_context  s    ,,,')+Dw' w'	299'B5<w'	2%)%7%7%@6"")$*<*<"=r   c                     XR                   ;  a  0 / 0 0 S.U R                   U'   X l        X0l        X R                   U   S'   X0R                   U   S'   0 U R                  EUEU l        g )N)r   r=   r   rA   r   rA   )r+   r3   r4   r5   )rG   r   _vars_aliasess       r   set_step_varsContextManager.set_step_vars  sx    ,,,')	+Dw' "'$,!.37#F+197#I.>!4!4>>r   c                 .    [        U R                  U5      $ r_   )r   r*   )rG   r(   s     r   get_last_workflow_run$ContextManager.get_last_workflow_run$  s    9$..+VVr   c                     g r_   r   )rG   r(   workflow_contextworkflow_statuss       r   set_last_workflow_run$ContextManager.set_last_workflow_run'  s    , 	r   )	__loggersrE   r.   rA   r8   r2   r4   r3   rC   r/   r1   r0   r:   r"   r'   r-   r5   r+   r,   r*   r)   r(   rD   )NNN)FFr_   )NN)F)r   r   r   r   dictrK   propertyrP   r\   ra   re   ri   rm   rq   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s     " $@
 +@D & &  A&#)% %(%N.tCy4'7 ..sTz .
 .3`Q>$?Wr   r   )r%   typingr   r   r6   r;   pympler.asizeofr   keep.api.core.configr   keep.api.core.dbr   r	   keep.api.models.alertr
   keep.api.models.incidentr   r   r   r   r   r   <module>r      s8     !   # ' T * 0Y 
i ir   