
    ii*                        d Z ddlZddlZddlZddlZddlmZ  ej                  d      Zej                  j                  ej                  j                  e            Zej                  j                  ed      Zeej                  vrej                  j                  de       eej                  vrej                  j                  de        G d d      Zdded	ed
efdZy)z]
Genesis V2 Agent
================
Main conversational agent powered by Google ADK / Gemini.
    N)Optionalzgenesis_v2.agentlibc                   T    e Zd ZdZddedefdZd Zddededefd	Zddededefd
Zy)GenesisAgentz)Conversational agent wrapping Google ADK.modelsystem_instructionc                     d | _         d | _        d | _        || _        || _        g | _        d | _        | j                          y )N)_agent_runner_session_servicer   r   _tools_init_error_initialize)selfr   r   s      )/mnt/e/genesis-system/genesis_v2/agent.py__init__zGenesisAgent.__init__   sB     $
"4    c                 ^   	 ddl m} ddlm} ddlm}  |       | _         |d| j                  | j                  xs d| j                        | _
         || j                  d| j                  	      | _        t        j                  d
| j                          y# t        $ rA}d| | _        t        j!                  | j                         d| _
        d| _        Y d}~yd}~wt"        $ rA}d| | _        t        j!                  | j                         d| _
        d| _        Y d}~yd}~ww xY w)z Initialize the Google ADK agent.r   )Agent)InMemorySessionService)Runner
genesis_v2zkYou are Genesis, a powerful AI assistant. Help the user with coding, system management, and research tasks.)namer   instructiontools
genesis-v2)agentapp_namesession_servicezAgent initialized: model=zGoogle ADK not available: NzAgent init error: )google.adk.agentsr   google.adk.sessionsr   google.adk.runnersr   r   r   r   r   r
   r   loggerinfoImportErrorr   error	Exception)r   r   r   r   es        r   r   zGenesisAgent._initialize%   s   !	 /B1$:$<D!!jj 33 X kkDK "kk% $ 5 5DL KK3DJJ<@A 	 !;A3?DLL))*DKDLL 	 !3A37DLL))*DKDLL		 s$   BB 	D,#7CD,+7D''D,messageuser_idreturnc                 L  K   | j                   sd| j                  xs d S d| }| j                  j                  d||       d{   }|s&| j                  j	                  d||       d{   }ddlm}m}  |d	 ||
      g      }d}| j                   j                  |||      2 3 d{   }	t        |	d      s|	j                  s#|	j                  j                  D ]+  }
t        |
d      s|
j                  s||
j                  z  }- h7 7 7 g6 |xs dS w)z"Send a message and get a response.zAgent not initialized. Error: zUnknown error. Check logs.session_r   )r   r*   
session_idNr   )ContentPartuser)text)roleparts )r*   r.   new_messagecontentr2   zNo response generated.)r   r   r   get_sessioncreate_sessiongoogle.genai.typesr/   r0   	run_asynchasattrr7   r4   r2   )r   r)   r*   r.   sessionr/   r0   user_contentresponse_texteventparts              r   chatzGenesisAgent.chatJ   sE    ||3D4D4D4dHd3effy)
 --99!! : 
 

  11@@%% A  G 	5F4W3E2FG<<11!$ 2 
 	3 	3%
 ui(U]]!MM// 3DtV,%23/
	3 
 8 88s`   AD$D	'D$0D1>D$/D3D4D7D$D$%D$8D$D$D$DD$c                    	 t        j                         }|j                         rjddl}|j                  j                         5 }|j                  t         j                  | j                  ||            }|j                  d      cddd       S |j                  | j                  ||            S # 1 sw Y   yxY w# t        $ r( t        j                  | j                  ||            cY S w xY w)zSynchronous wrapper for chat.r   Nx   )timeout)asyncioget_event_loop
is_runningconcurrent.futuresfuturesThreadPoolExecutorsubmitrunrB   resultrun_until_completeRuntimeError)r   r)   r*   loop
concurrentpoolfutures          r   	chat_synczGenesisAgent.chat_synco   s    
	<))+D )''::< 6![[dii6QRF!===56 6 ..tyy'/JKK	6 6
  	<;;tyy':;;	<s7   AB= AB1	B=  B= 1B:6B= :B= =.C.-C.Nzgemini-2.0-flashr5   )r1   )	__name__
__module____qualname____doc__strr   r   rB   rU    r   r   r   r      sS    3c C # J#9# #9 #9 #9J< <s < <r   r   r   r   r+   c                 X    ddl m}  |       t        j                  d<   t	        | |      S )z+Factory function to create a Genesis agent.r   )get_api_keyGOOGLE_API_KEY)r   r   )genesis_v2.core.configr^   osenvironr   )r   r   r^   s      r   create_agentrc   ~   s$    2#.=BJJ e8JKKr   rV   )rZ   ra   sysrF   loggingtypingr   	getLoggerr#   pathdirname__file__root_dirjoinlib_dirinsertr   r[   rc   r\   r   r   <module>ro      s    
 
   			-	. 77??277??845
'',,x
'
#((HHOOAw388HHOOAx c< c<LL Lc LS_ Lr   