
    @=j                        d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
  e	d          ZddZedd            Ze                    d          ddd            Ze                    d          ddd            ZdS )a  HTTP routes for memory-provider OAuth connect, mounted by ``web_server``.

Kept out of ``web_server.py`` so the memory feature's surface stays in the
memory layer. Dispatch is by convention: a provider's flow lives at
``plugins.memory.<provider>.oauth_flow`` exposing ``start_loopback_flow_background``
and ``get_flow_status``; a provider without that module simply 404s. No provider
is named here.
    )annotationsN)contextmanager)Optional)	APIRouterHTTPExceptionz/api/memory/providers)prefixproviderstrc                    |                                  st          dd|           	 t          j        d|  d          S # t          $ r t          d|  d          w xY w)zBReturn a provider's OAuth flow module by convention, or raise 404.  zunknown memory provider status_codedetailzplugins.memory.z.oauth_flowz does not support OAuth connect)isidentifierr   	importlibimport_moduleImportError)r	   s    =/home/rurouni/.hermes/hermes-agent/hermes_cli/memory_oauth.py_resolve_flowr      s      "" ]4[x4[4[\\\\b&'N'N'N'NOOO b b bx4`4`4`aaaabs   A A!profileOptional[str]c              #  
  K   | pd                                 }|r|                                dk    rdV  dS ddlm} ddlm}m} 	 |                    |           n0# t          $ r#}t          dt          |                    d}~ww xY w|                    |          st          d	d
| d           |t          |                    |                              }	 dV   ||           dS #  ||           w xY w)zScope config resolution to ``profile`` so the flow's eager path resolve
    targets that profile's honcho.json. None/""/"current" leaves it untouched. currentNr   )profiles)reset_hermes_home_overrideset_hermes_home_overridei  r   r   z	Profile 'z' does not exist.)striplower
hermes_clir   hermes_constantsr   r   validate_profile_name
ValueErrorr   r
   profile_existsget_profile_dir)r   	requestedprofiles_modr   r   etokens          r   _scope_to_profiler*      s^      B%%''I 	))Y66333333UUUUUUUU<**95555 < < <CFF;;;;<&&y11 ^4\	4\4\4\]]]]$$S)E)Ei)P)P%Q%QRRE*""5)))))""5))))s$   A 
B(BB$C5 5Dz/{provider}/oauth/startc                  K   t          |           }	 t          |          5  |                                cddd           S # 1 swxY w Y   dS # t          $ r  t          $ r}t          dd|  d|           d}~ww xY w)u   Begin a provider's zero-CLI OAuth flow — opens the browser and captures
    the grant via the loopback listener. Returns immediately; poll status.N  zFailed to start z OAuth: r   )r   r*   start_loopback_flow_backgroundr   	Exceptionr	   r   flowexcs       r   start_memory_oauthr2   9   s       ""D` w'' 	9 	96688	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9    ` ` `4^x4^4^Y\4^4^____`9   A AA AA 
AA B $A;;B z/{provider}/oauth/statusc                  K   t          |           }	 t          |          5  |                                cddd           S # 1 swxY w Y   dS # t          $ r  t          $ r}t          dd|  d|           d}~ww xY w)zAPoll a provider's OAuth flow: idle | pending | connected | error.Nr,   zFailed to read z OAuth status: r   )r   r*   get_flow_statusr   r.   r/   s       r   memory_oauth_statusr6   I   s       ""Dfw'' 	* 	*''))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*    f f f4dh4d4d_b4d4deeeefr3   )r	   r
   )r   r   )N)r	   r
   r   r   )__doc__
__future__r   r   
contextlibr   typingr   fastapir   r   routerr   r*   postr2   getr6        r   <module>rA      s2    # " " " " "     % % % % % %       , , , , , , , ,	1	2	2	2b b b b * * * *2 &''` ` ` ` ('` &''	f 	f 	f 	f ('	f 	f 	fr@   