
    `gj                        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j                  d      ddd       Zej!                  d      ddd       Zy)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)prefixc                    | j                         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)providers    J/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/memory_oauth.py_resolve_flowr      si      "6Nxl4[\\b&&
+'NOO bxj@_4`aabs	   ; Ac              #    K   | xs dj                         }|r|j                         dk(  rd yddlm} ddlm}m} 	 |j                  |       |j                  |      st        d	d
| d       |t        |j                  |                  }	 d  ||       y# t        $ r}t        dt        |            d}~ww xY w#  ||       w xY w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   strprofile_existsget_profile_dir)profile	requestedprofiles_modr   r   etokens          r   _scope_to_profiler)      s      B%%'I	)Y63U<**95 &&y1i	{J[4\]]$S)E)Ei)P%QRE*"5)  <CF;;< 	#5)sB   =C B! ACC 	C!	C*C  CC
CCz/{provider}/oauth/startc                   K   t        |       }	 t        |      5  |j                         cddd       S # 1 sw Y   yxY w# t        $ r  t        $ r}t        dd|  d|       d}~ww xY w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_oauthr1   9   s|      "D` w' 	9668	9 	9 	9  `6FxjPXY\X]4^__`C   A-A  4	A  A-=A  A-A   A*A%%A**A-z/{provider}/oauth/statusc                   K   t        |       }	 t        |      5  |j                         cddd       S # 1 sw Y   yxY w# t        $ r  t        $ r}t        dd|  d|       d}~ww xY w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_statusr5   I   sx      "Dfw' 	*'')	* 	* 	*  fohZ_b^c4deefr2   )r   r!   )r$   Optional[str])N)r   r!   r$   r6   )__doc__
__future__r   r   
contextlibr   typingr   fastapir   r   routerr   r)   postr1   getr5        r   <module>rA      s    #  %  ,	1	2b * *2 &'` (` &'	f (	fr@   