o
    RgŠi=  ã                   @   s†   d dl mZ d dlZ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eƒZej e ¡ d¡ZdZe	jd	d
�dd„ ƒZdS )é    )Ú	getLoggerN)Údatetime)Ú
celery_appé   )Úquery_xai_apizintelligence.jsonÚhomogenization_statusz:keepa_deals.maintenance_tasks.homogenize_intelligence_task)Únamec                  C   sæ  t j tjj¡} |  tt 	ddddœ¡¡ t
 dt ¡ › �¡ t
 dt› �¡ tj t¡sEdt› �}t
 |¡ |  tt 	d|d	œ¡¡ dS �zttd
dd��}t |¡}W d  ƒ n1 s^w   Y  |st|  tt 	dddœ¡¡ W dS i }|D ]&}t|tƒr‘d|v r‘| d¡|t|d ƒ ¡ < qxt|tƒržd|t|ƒ ¡ < qxg }|D ] }t|tƒrºd|v rº| t|d ƒ ¡ ¡ q£| t|ƒ ¡ ¡ q£d}t|ƒ}g }	t
 d|› d�¡ td||ƒD ]Ð}
||
|
| … }|
| d }|| d | }|  tt 	dd|› d|› d�ddœ¡¡ dt 	|¡› d�}dddœd|dœgdd d!d"œ}t|ƒ}d#|v �r;t
 d$|
› d%|d# › �¡ |	 |¡ qÛzI|d& d d' d  ¡ }tjd(d)|tj d*�}t !|¡}t|t"ƒ�ru|	 |¡ t
 d+|
› d,t|ƒ› d-t|ƒ› �¡ nt
 d.|
› d/�¡ |	 |¡ W qÛ tj#t$t%f�y« } zt
 d0|
› d%|› �¡ |	 |¡ W Y d}~qÛd}~ww g }t& '¡  (d1¡}|	D ]}t|ƒ ¡ }| |¡}| ||�rÌ|n|d2œ¡ �q·t|ƒ}|| }t
 d3|› d4|› d5|› �¡ |dk�r5t
 d6t› �¡ z'ttd7dd��}tj)||d8d9� W d  ƒ n	1 �sw   Y  t
 d:¡ W n t*�y4 } z
t
 d;|› �¡ |‚d}~ww t
 d<¡ |  tt 	d|d=|› d>�d?œ¡¡ |W S  t*�yr } zt
 d@|› �¡ |  tt 	dt|ƒd	œ¡¡ |‚d}~ww )Az:Background task to homogenize intelligence.json using LLM.ÚRunningzStarting...r   )ÚstatusÚprogressÚremoved_countz"Homogenization Task Started. CWD: zTarget Intelligence File: zFile not found: ÚError)r
   ÚmessageÚrzutf-8)ÚencodingNÚComplete)r
   r   ÚcontentÚ
date_addediô  zStarting processing for z	 items...r   zProcessing batch z of z...a9  
            You are a strict data cleaner. Below is a JSON list of "intelligence" items.

            **CRITICAL INSTRUCTIONS:**
            1. Aggressively identify concepts that mean the same thing, even if phrased differently.
            2. Merge them into a SINGLE, concise entry.
            3. If two items share >50% conceptual overlap, KEEP ONLY THE BEST ONE.
            4. Your goal is to REDUCE the list size by removing redundancy.
            5. Return ONLY the final JSON list of strings. No markdown, no intro.

            **Input List:**
            z
            ÚsystemzYou are a data cleaner.)Úroler   Úuserzgrok-4-fast-reasoningFgš™™™™™¹?)ÚmessagesÚmodelÚstreamÚtemperatureÚerrorz"xAI Error in homogenization chunk z: Úchoicesr   z^```json\s*|\s*```$Ú )ÚflagszChunk z
: Reduced z -> z0Homogenization returned non-list JSON for chunk Ú.z0Error parsing homogenization response for chunk z%Y-%m-%d)r   r   z#Homogenization complete. Original: z	, Final: z, Removed: zWriting updated list to file: Úwé   )ÚindentzFile write successful.z#Failed to write intelligence file: z#No items removed. File not updated.zComplete! Semantically merged z duplicate ideas.)r
   r   r   zError in homogenization task: )+ÚredisÚRedisÚfrom_urlÚceleryÚconfÚ
broker_urlÚsetÚHOMOGENIZATION_STATUS_KEYÚjsonÚdumpsÚloggerÚinfoÚosÚgetcwdÚINTELLIGENCE_FILEÚpathÚexistsr   ÚopenÚloadÚ
isinstanceÚdictÚgetÚstrÚstripÚappendÚlenÚranger   ÚextendÚreÚsubÚ	MULTILINEÚloadsÚlistÚJSONDecodeErrorÚKeyErrorÚ
IndexErrorr   ÚnowÚstrftimeÚdumpÚ	Exception)Úredis_clientÚ	error_msgÚfÚintelligenceÚcontent_date_mapÚitemÚintelligence_content_listÚ
CHUNK_SIZEÚtotal_originalÚall_cleaned_stringsÚiÚchunkÚcurrent_chunk_numÚtotal_chunksÚpromptÚpayloadÚresultr   Úcleaned_chunkÚeÚfinal_objects_listÚ	today_strÚsÚs_cleanÚoriginal_dateÚfinal_countÚremovedÚ	write_err© rf   ú8/var/www/agentarbitrage/keepa_deals/maintenance_tasks.pyÚhomogenize_intelligence_task   sà   

ý

ÿ
€

ýõþù




&
€€þ
þ
ÿ€þ



ý€ýrh   )Úloggingr   r/   r+   r#   r?   r   Úworkerr   r&   Úava_advisorr   Ú__name__r-   r2   Újoinr0   r1   r*   Útaskrh   rf   rf   rf   rg   Ú<module>   s    
