
    i              	           d Z ddlZej                  j                  dd       ddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ  ej                  e      Z ed      Z	 	 	 dded	ed
edefdZded	ededdfdZy)u   
AIVA RLM Nexus — pre_call_hook
Standalone async function called on every call.initiated webhook event.
Triggers BinduHydrator and returns the context envelope.

Story 4.06 — Track A
File: core/hydrators/pre_call_hook.py
    Nz/mnt/e/genesis-system)Path)Optional)BinduHydratorz5/mnt/e/genesis-system/data/observability/events.jsonl
session_idcall_idcaller_numberreturnc                   K   t        j                         }|6	 |j                  d|  d|       d{    t        j	                  d||        t        |||      }|j                  | |       d{    |j                  | |       d{   }	t        j                         |z
  dz  }
t        | ||
       t        j                  d| ||
       |	S 7 # t
        $ r!}t        j                  d| |       Y d}~d}~ww xY w7 7 vw)	a!  
    Called on every call.initiated webhook.

    Steps:
      1. Stores caller_number in Redis aiva:state:{session_id} via HSET
      2. Instantiates BinduHydrator with injected clients (never a singleton)
      3. Calls start_hydration(session_id, call_id)
      4. Calls gather_and_assemble(session_id, call_id)
      5. Logs timing to data/observability/events.jsonl
      6. Returns the ROYAL_CHAMBER_CONTEXT XML envelope string

    All clients are injected as parameters so the function is fully testable
    without real infrastructure.

    Args:
        session_id:      Unique identifier for this hydration cycle.
        call_id:         Telnyx call_control_id (or internal call UUID).
        caller_number:   E.164 phone number of the incoming caller.
        redis_client:    Optional async Redis client (must support hset/setex/get).
        postgres_client: Optional psycopg2 connection pool (getconn/putconn).
        qdrant_client:   Optional async Qdrant client.

    Returns:
        ROYAL_CHAMBER_CONTEXT XML envelope as a string.
    Nzaiva:state:r   z9pre_call_hook: stored caller_number '%s' in aiva:state:%suD   pre_call_hook: Redis HSET failed for session '%s': %s — proceeding)redis_clientpostgres_clientqdrant_clienti  z5pre_call_hook: session=%s call=%s completed in %.1fms)time	monotonichsetloggerdebug	Exceptionerrorr   start_hydrationgather_and_assemble_log_timinginfo)r   r   r   r   r   r   startexchydratorxml_envelope
elapsed_mss              5/mnt/e/genesis-system/core/hydrators/pre_call_hook.pypre_call_hookr      s'    B NNE 	##j\*  
 LLK !'#H 
"
":w
777 "55j'JJL .."U*d2J
GZ0
KK?	 W  	LLV 	  8 Ks]   DC CC #D2D3DDADC 	C>C94D9C>>DDr   c                    d| |t        |d      d}	 t        j                  j                  dd       t        j	                  dd      5 }|j                  t        j                  |      d	z          d
d
d
       t        j                  d|t               y
# 1 sw Y   %xY w# t        $ r }t        j                  d|       Y d
}~y
d
}~ww xY w)uV  
    Append a JSON timing event to EVENTS_PATH (data/observability/events.jsonl).

    Each line is a valid JSON object:
    {
        "event": "pre_call_hook_complete",
        "session_id": "...",
        "call_id": "...",
        "elapsed_ms": 123.4
    }

    File I/O failure is NON-FATAL — a timing log should never crash a call.
    pre_call_hook_complete   )eventr   r   r   T)parentsexist_okazutf-8)encoding
Nz1_log_timing: appended timing event (%.1fms) to %suH   _log_timing: failed to write events.jsonl: %s — timing event discarded)roundEVENTS_PATHparentmkdiropenwritejsondumpsr   r   r   r   )r   r   r   r#   fhr   s         r   r   r   m   s     * J*	E
   =cG4 	/HHTZZ&-.	/?	
	/ 	/  
V	
 	

s/   8B# (B3#B# B B# #	C,CC)NNN)__doc__syspathinsertr/   loggingr   pathlibr   typingr   core.hydrators.bindu_hydratorr   	getLogger__name__r   r*   strr   floatr        r   <module>r@      s     * +      7			8	$JK QQQ Q 	Qh!
C !
# !
5 !
T !
r?   