
    "+Pj                        d Z ddlmZ ddlZddlZddlZddlmZ ej                  j                  dd      Zej                  j                  dej                  j                  dd	            Zd
ZddZdddZy)u?  Passive memory extractor — the background 'remember' brain.

Runs on EVERY user utterance in a background thread, independent of which
conversational brain answered. It decides if what Ahmed said contains a durable
fact/event/preference worth keeping, and if so saves it to the shared Railway
brain. So saving ALWAYS works — even when the tool-less local model handled the
turn, even for casual mentions Ahmed never explicitly asked to save.

Uses the local model (Ollama) for the extraction — fast, free, off the
conversation's critical path. MEM_AUTOSAVE=0 disables.
    )annotationsN)memory_control
OLLAMA_URLzhttp://127.0.0.1:11434MEM_EXTRACT_MODELLOCAL_MODELzqwen3:4b-instructu4  You pull durable long-term memories out of what Ahmed tells his assistant, so they're remembered for weeks.

From his message, output ANY fact worth keeping — an event/meeting (with its time and people), a decision, a plan, a person, a number, a preference, a commitment — as ONE concise third-person sentence beginning with "Ahmed". Keep the specifics (names, times, places, numbers).

If the message is small talk, a command, a question, or has nothing durable, output exactly: NONE

Output ONLY the one sentence, or NONE. Nothing else.

Ahmed said: "{text}"c                   t        j                  t        ddt        j	                  |       dgddid      j                         }t        j                  j                  t         d|d	d
i      }t        j                  j                  |d      5 }t        j                  |j                               }d d d        j                  d      xs i j                  d      xs dj                         S # 1 sw Y   ?xY w)NFuser)text)rolecontenttemperaturer   )modelstreammessagesoptionsz	/api/chatzContent-Typezapplication/json)dataheaders   )timeoutmessager    )jsondumpsMODEL_PROMPTformatencodeurllibrequestRequestOLLAMAurlopenloadsreadgetstrip)r
   bodyreqros        B/Users/ahmed/devFolder/Ultron/claude-voice/voice/memory_extract.py_extractr,   (   s    ::%$T1JKL!1%  vx	 	
 ..
 
 ()4!34 ! 6C 
		R		0AJJqvvx  
1UU9#((39r@@BB 
1	0s   $C55C>c                P   | xs dj                         } t        j                  j                  dd      dk(  ryt	        j
                         rt        |       dk  ry	 t        |       }|j                         j                  d
      j                         }|r#d|j                         dd v st        |      dk  ryt	        j                  ||       t        d|dd  d
|rd| dz          ydz          y# t        $ r%}t        dt        |      dd  d	       Y d}~yd}~ww xY w)u'  Blocking — call in a background thread. Extracts and saves a durable
    fact from `text` if there is one; otherwise does nothing. `tone` is the
    vocal delivery this was said with (e.g. 'slow, flat'); it's attached to the
    saved memory so the nightly reflection can weigh HOW he said it.r   MEM_AUTOSAVE10N   z  [memory-extract] skipped (P   )"NONE)tonez  [memory: auto-saved "F   z (said z)]])r&   osenvironr%   r   enabledlenr,   	Exceptionprintstrupper	fire_save)r
   r6   factes       r+   save_if_durablerD   6   s   
 JBD	zz~~nc*c1!!#s4y1}~ ::<c"((*D6TZZ\"1--TQT-	$T#2YKr
2!%WTF"0 1+.0 1  ,SVCR[M;<s   C7 7	D% D  D%)r
   r?   returnr?   )N)r
   r?   r6   z
str | NonerE   None)__doc__
__future__r   r   r9   urllib.requestr   voicer   r:   r%   r!   r   r   r,   rD        r+   <module>rM      sk   
 #  	   	&>	?


*zz~~m5HI	K C1rL   