
    Z)iL                     :   S SK JrJrJrJrJrJrJrJr  S SK	J
r
JrJrJr  S SKJrJr  S SKJr  S SKJrJrJrJrJr  S SKrS SKr\R6                  R9                  \R6                  R;                  \R6                  R=                  \5      5      5        SSK J!r!  SS	K"J#r#  S SK$r$S
SK%J%r%  S
SK&J'r'  S
SK(J)r)  S
SK*J+r+  S
SK,J-r-  S
SK.J/r/  S SK0J1r1  \" S\25      r3\3Ri                  S5      \
S 5       5       r5\3Ri                  S5      \
S 5       5       r6\3Ri                  S5      \
S 5       5       r7\3Ri                  S5      \
S 5       5       r8\3Ri                  S5      \
S 5       5       r9\3Ri                  S5      \
S 5       5       r:\3Ri                  S5      \
S  5       5       r;S! r<g)"    )	Blueprintrender_templaterequestjsonifyflashredirecturl_forsend_from_directory)login_requiredcurrent_user
login_userlogout_user)generate_password_hashcheck_password_hash)db)AgenteTreinoUserChatMessageAgenteN   )AgentService)ChatService   )auth)chat_bp)stats_bp)playground_bp)agent_bp)
profile_bp)send_discord_audit_logmainz/historyc                      [        S5      $ )Nzhistory.htmlr        TC:\Users\fernando.barreto\Documents\GitLab\ollama\chat-ollama\src\routes\__init__.pyhistoryr(      s     >**r&   z	/settingsc                      [        S5      $ )Nzsettings.htmlr$   r%   r&   r'   settingsr*      s     ?++r&   z/agentsc                      [        S5      $ )Nzagents.htmlr$   r%   r&   r'   agentsr,            =))r&   z
/new-agentc                      [        S[        S9$ )Nznew-agent.htmluserr   r   r%   r&   r'   	new_agentr2   %   s     +,??r&   z/profilec                     [         n [        U SU R                  5      [        U SS5      [        U SS 5      [        U SS5      [        U SS5      [        U S	S
5      [        U SS5      [        U SS5      [        U SS5      [        U S[        U S5      (       a  U R                  R                  S5      OS5      S.
n[        SUS9$ )Nnameemail 
avatar_urldefault_modelzllama3:latestdefault_temperaturegffffff?default_max_tokensi  save_historyTtotal_conversationsr   total_tokens	join_date
created_atz%d/%m/%Y)
r4   r5   r7   r8   r9   r:   r;   r<   r=   r>   zprofile.htmlr/   )r   getattrusernamehasattrr?   strftimer   )r0   	user_datas     r'   profilerE   *   s     Dfdmm4w+dL$7 H&t-BCH%d,@$Gnd;&t-BAFna8T;X_`dfrXsXs0H0H0Ty{|I >	::r&   z/helpc                      [        S[        S9$ )Nz	help.htmlr/   r1   r%   r&   r'   helprG   <   s     ;\::r&   z/statusc                      [        S5      $ )Nzstatus.htmlr$   r%   r&   r'   statusrI   A   r-   r&   c                   ^ ^ SSK Jn  U" 5       nUR                  T 5        SUl        UR                  S 5       nUR
                  S 5       nT R                  [        SS9  T R                  [        5        T R                  [        SS9  T R                  [        5        T R                  [        5        T R                  [        5        T R                  [        5        SS	KJn  SS
KJm  SS KnT R'                  U5      U4S j5       nT R'                  S5      U4S j5       nT R'                  [(        5      U 4S j5       n	T R+                  S5      U4S j5       n
g )Nr   )LoginManager
auth.loginc                 R    [         R                  R                  [        U 5      5      $ )N)r   querygetint)user_ids    r'   	load_userinit_app.<locals>.load_userL   s    zz~~c'l++r&   c                      [         R                  (       d  [         R                  S   (       a  [        SS05      S4$ [	        [        S5      5      $ )Napplication/jsonerroru   Não autenticadoi  rL   )r   is_jsonaccept_mimetypesr   r   r	   r%   r&   r'   unauthorized_callback'init_app.<locals>.unauthorized_callbackP   s=    ??g667IJG%7893>>-..r&   z/auth)
url_prefixz/agente)AppExceptionr$   c                 
  > [         R                  (       d  [         R                  S   (       a#  [        SU R                  05      U R
                  4$ [        U R                  S5        T" SU R                  S9U R
                  4$ )NrU   rV   danger
error.htmlrV   )r   rW   rX   r   messagestatus_coder   )er   s    r'   handle_app_exception&init_app.<locals>.handle_app_exceptionb   s]    ??g667IJGQYY/0!--??aii"|199=q}}LLr&     c                    > Sn[         R                  (       d  [         R                  S   (       a  [        SU05      S4$ T" SUS9S4$ )Nu   Recurso não encontradorU   rV   rf   r_   r`   )r   rW   rX   r   )rc   msgr   s     r'   	not_foundinit_app.<locals>.not_foundi   sC    '??g667IJGS>*C//|37<<r&   c                   > SS K nSSKJn  SSKJn  TR
                  R                  S[        U 5       SUR                  5        35         [        S[        [        SS 5      SS[        U 5       SUR                  5       S S	  3[        R                  S
9  S[        U 5      ;   d'  S[        [        U 5      5      ;   d  S[        U 5      ;   a  U" SS5        [!        [#        S5      5      $ Sn[        R$                  (       d  [        R&                  S   (       a  [)        SU05      S	4$ [!        [#        S5      5      $ ! [         a(  nTR
                  R                  SU 35         S nANS nAff = f)Nr   )OperationalError)r   u   Erro não tratado: 
u   Erro críticoidBackendi  )actionrQ   object_typedetails
ip_addresszFalha ao enviar log Discord: zCan't connect to MySQL serverrl   z	timed outuY   Erro crítico: Não foi possível conectar ao banco de dados. Tente novamente mais tarde.r^   rL   zErro interno do servidorrU   rV   zmain.history)	tracebacksqlalchemy.excrl   flaskr   loggerrV   str
format_excr!   r@   r   r   remote_addr	Exceptiontyper   r	   rW   rX   r   )rc   rt   rl   r   discord_errrh   apps         r'   handle_generic_exception*init_app.<locals>.handle_generic_exceptionp   sD   3

.s1vhb9M9M9O8PQR		L"&dD9%-c!fXR	8L8L8NtPS8T7UV".. +c!f48JcRVWXRYl8Z^impqrms^smowxGL122(??g667IJGS>*C///00  	LJJ<[MJKK	Ls   A
D: :
E,E''E,/c                  f   > [         R                  (       a  [        [        S5      5      $ T " S5      $ )Nzplayground.playgroundz	home.html)r   is_authenticatedr   r	   r$   s   r'   
root_indexinit_app.<locals>.root_index   s(    ((G$;<=={++r&   )flask_loginrK   init_app
login_viewuser_loaderunauthorized_handlerregister_blueprintr   r   r   main_bpr   r   r    src.exceptionsr\   rv   r   rt   errorhandlerr{   route)r~   rK   login_managerrR   rY   r\   rt   rd   ri   r   r   r   s   `          @r'   r   r   F   sE   ( NM3+M, , ''/ (/
 4G47#8	:7#8$=):&+%l#M $M 	c= = 	i 1 !1. 	YYs^, ,r&   )=rv   r   r   r   r   r   r   r	   r
   r   r   r   r   r   werkzeug.securityr   r   src.extensionsr   
src.modelsr   r   r   r   r   sysospathappenddirnameabspath__file__services.agent_servicer   services.chat_servicer   requestsr   chat_routesr   stats_routesr   playground_routesr   agenter   profile_routesr    src.discord.discord_webhookr!   __name__r   r   r(   r*   r,   r2   rE   rG   rI   r   r%   r&   r'   <module>r      s   m m m M M I  @ @ 
 	  9: ; 1 /     " ,  & >
FH
%z+  + 	{,  , 	y*  * 	|@  @ 	z;  ;  	w;  ; 	y*  *F,r&   