§
     ’j!  ã                  óÊ  — d dl mZ d dlZd dlZd dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZmZ  ej        d	¦  «        Z e¦   «         Zd"d„Zd#d„Ze                     de¬¦  «         ee¦  «        fd$d„¦   «         Ze                     de¬¦  «         ee¦  «        fd%d„¦   «         Ze                     d e¬¦  «         ee¦  «        fd&d!„¦   «         ZdS )'é    )ÚannotationsN)ÚAny)Ú	APIRouterÚDependsÚHTTPException)Úget_orchestrator)Úget_current_user)ÚErpDatabaseClient)ÚAgentRunRequestÚAgentRunResponseÚRouteTaskRequestÚRouteTaskResponsez dscons.api.knowledge_agents.execÚ
agent_codeÚstrÚ	task_typeÚqueryÚanswerÚcurrent_userúdict[str, Any] | NoneÚreturnÚNonec                ó   — 	 t          |t          ¦  «        r|                     d¦  «        nd}t          t	          |                     ¦   «         ¦  «        dz  d¦  «        }t          t	          |pd                     ¦   «         ¦  «        dz  d¦  «        }t          ¦   «         }|                     | |||||¬¦  «         dS # t          $ r Y dS w xY w)u^   Ghi nháº­n nháº­t kÃ½ tiÃªu thá»¥ token vÃ  ngÃ¢n sÃ¡ch an toÃ n khÃ´ng cháº·n luá»“ng chÃ­nh.ÚemailNé   é   Ú é-   )r   r   Úprompt_tokensÚcompletion_tokensÚ
user_emailÚquery_preview)	Ú
isinstanceÚdictÚgetÚmaxÚlenÚsplitr
   Úlog_ai_agent_usageÚ	Exception)	r   r   r   r   r   r    r   r   Úclients	            ú\C:\Projects\DSCons\app\modules\agents\presentation\knowledge_agent_routes\agent_execution.pyÚ_log_agent_usage_safelyr,      sì   € ðå)3°LÅ$Ñ)GÔ)GÐQˆL×Ò˜WÑ%Ô%Ð%ÈTð 	õ �C §¢¡¤Ñ.Ô.°Ñ2°BÑ7Ô7ˆÝ¥ V \¨r×$8Ò$8Ñ$:Ô$:Ñ ;Ô ;¸aÑ ?ÀÑDÔDÐÝ"Ñ$Ô$ˆØ×!Ò!Ø!ØØ'Ø/Ø!Øð 	"ñ 	
ô 	
ð 	
ð 	
ð 	
øõ ð ð ð Øˆˆðøøøs   ‚B;B? Â?
CÃCr   c                ó\   — t          | j        | j        | j        | j        | j        ¬¦  «        S )z=Normalize orchestrator result into the public response model.)Úselected_agentr   r   Úretrieved_chunks_countr   )r   r.   r   r   r/   r   )Úresults    r+   Ú_build_agent_run_responser1   2   s6   € åØÔ,ØÔ"ØŒ}Ø%Ô<ØÔ$ðñ ô ð ó    z/route-task)Úresponse_modelÚpayloadr   údict[str, Any]r   c                óò  — t          ¦   «         }	 t          j        |                     | j        | j        | j        | j        | j        r| j         	                    d¬¦  «        nd¬¦  «        ¦  «        }n1# t          $ r$}t          dt          |¦  «        ¬¦  «        |‚d}~ww xY wt          |j        |j        | j        |j        |¬¦  «         t          |j        |j        |j        |j        |j        ¬¦  «        S )	zCRoute a task through the orchestrator with persona-aware retrieval.T©Úexclude_noneN©r   r   Útop_kr   Úretrieval_filtersé�  ©Ústatus_codeÚdetail©r   r   r   r   r   )r.   r   r/   r   r   )r   ÚasyncioÚrunÚrouter   r   r:   r   ÚfiltersÚ
model_dumpÚ
ValueErrorr   r   r,   r   r   r.   r/   )r4   r   Úorchestratorr0   Úexcs        r+   Ú
route_taskrI   =   s#  € õ
 $Ñ%Ô%€LðGÝ”Ø×ÒØ!Ô+Ø”mØ”mØ"Ô-à”?ð# '¤/×"<Ò"<È$Ð"<Ñ"OÔ"OÐ"Oàð ñ ô ñ

ô 

ˆˆøõ ð Gð Gð GÝ¨µC¸±H´HÐ=Ñ=Ô=À3ÐFøøøøðGøøøõ ØÔ$ØÔ"ØŒmØŒ}Ø!ðñ ô ð õ ØÔ,ØŒ}Ø%Ô<ØÔ"ØÔ$ðñ ô ð s   �A"A3 Á3
B!Á=BÂB!z/v1/agents/{task_type}/runr   c                ó°  — t          ¦   «         }	 t          j        |                     | |j        |j        |j        |j        r|j                             d¬¦  «        nd¬¦  «        ¦  «        }n1# t          $ r$}t          dt          |¦  «        ¬¦  «        |‚d}~ww xY wt          |j        |j        p| |j        |j        |¬¦  «         t          |¦  «        S )z=Direct specialist execution endpoint for explicit task types.Tr7   Nr9   r<   r=   r@   )r   rA   rB   rC   r   r:   r   rD   rE   rF   r   r   r,   r   r   r1   )r   r4   r   rG   r0   rH   s         r+   Ú	run_agentrK   c   sÿ   € õ $Ñ%Ô%€LðGÝ”Ø×ÒØ#Ø”mØ”mØ"Ô-à”?ð# '¤/×"<Ò"<È$Ð"<Ñ"OÔ"OÐ"Oàð ñ ô ñ

ô 

ˆˆøõ ð Gð Gð GÝ¨µC¸±H´HÐ=Ñ=Ô=À3ÐFøøøøðGøøøõ ØÔ$ØÔ"Ð/ iØŒmØŒ}Ø!ðñ ô ð õ % VÑ,Ô,Ð,s   �AA. Á.
BÁ8BÂBz#/v1/agents/by-code/{agent_code}/runc                ó¦  — t          ¦   «         }	 t          j        |                     d|j        |j        | |j        r|j                             d¬¦  «        nd¬¦  «        ¦  «        }n1# t          $ r$}t          dt          |¦  «        ¬¦  «        |‚d}~ww xY wt          |j        |j        pd|j        |j        |¬	¦  «         t          |¦  «        S )
zMRun an AI employee persona directly using its default capability when needed.r   Tr7   Nr9   r<   r=   Úpersona_directr@   )r   rA   rB   rC   r   r:   rD   rE   rF   r   r   r,   r   r   r   r1   )r   r4   r   rG   r0   rH   s         r+   Úrun_agent_by_coderN   …   sý   € õ $Ñ%Ô%€LðGÝ”Ø×ÒØØ”mØ”mØ%à”?ð# '¤/×"<Ò"<È$Ð"<Ñ"OÔ"OÐ"Oàð ñ ô ñ

ô 

ˆˆøõ ð Gð Gð GÝ¨µC¸±H´HÐ=Ñ=Ô=À3ÐFøøøøðGøøøõ ØÔ$ØÔ"Ð6Ð&6ØŒmØŒ}Ø!ðñ ô ð õ % VÑ,Ô,Ð,s   �AA) Á)
BÁ3BÂB)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )r4   r   r   r5   r   r   )r   r   r4   r   r   r5   r   r   )r   r   r4   r   r   r5   r   r   )Ú
__future__r   rA   ÚloggingÚtypingr   Úfastapir   r   r   Úapp.api.dependenciesr   Ú"app.modules.auth.presentation.authr	   Úapp.core.postgres.erp_clientr
   Úapp.models.schemasr   r   r   r   Ú	getLoggerÚloggerÚrouterr,   r1   ÚpostrI   rK   rN   © r2   r+   ú<module>r\      sö  ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø Ð Ð Ð Ð Ð à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5à 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø :Ð :Ð :Ð :Ð :Ð :ðð ð ð ð ð ð ð ð ð ð ð ð 
ˆÔ	Ð=Ñ	>Ô	>€Ø	ˆ‰Œ€ðð ð ð ð6ð ð ð ð ‡‚ˆ]Ð+<€Ñ=Ô=à>E¸gÐFVÑ>WÔ>Wð"ð "ð "ð "ñ >Ô=ð"ðJ ‡‚Ð)Ð:J€ÑKÔKð $+ 7Ð+;Ñ#<Ô#<ð-ð -ð -ð -ñ LÔKð-ðB ‡‚Ð2ÐCS€ÑTÔTð $+ 7Ð+;Ñ#<Ô#<ð-ð -ð -ð -ñ UÔTð-ð -ð -r2   