
    `gjE                       d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZmZ ddlmZ  e
e      j$                  dz  Zdd	Zdd
ZddZddZddZddZddZddZddZddZddZddZddddZ y) u  
``hermes photon ...`` CLI subcommands — registered by the plugin via
``ctx.register_cli_command()``.

Subcommands:

    setup              full first-time setup (device login + project + user + sidecar)
    status             show login + project + sidecar dep state
    install-sidecar    npm install inside plugins/platforms/photon/sidecar/
    telemetry          show or toggle Spectrum SDK telemetry (on/off)

The device-code login runs automatically as the first step of ``setup``;
there is no standalone ``login`` verb (matching how every other Hermes
gateway channel onboards through a single setup surface).

Photon uses the spectrum-ts gRPC stream for inbound — there is no webhook
to register, so there are no webhook subcommands.
    )annotationsN)Path)Colorscolor   )authsidecarc                *   | j                  dd      }|j                  dd      }|j                  ddd	
       |j                  ddd
       |j                  dd       |j                  dd       |j                  dd       |j                  ddd       |j                  ddd       |j                  dd       |j                  dd       |j                  dd      }|j                  dddd !       | j                  t        "       y)#z(Wire up `hermes photon ...` subcommands.photon_commandF)destrequiredsetupz:First-time setup (device login + project + user + sidecar))helpz--project-nameNz&Project name (default: 'Hermes Agent'))defaultr   z--phonez+Your E.164 phone number (e.g. +15551234567)z--first-name)r   z--last-namez--emailz--no-browser
store_truez@Don't try to open a browser for device login; print the URL only)actionr   z--skip-sidecar-installz/Skip `npm install` inside the sidecar directorystatusz(Show login + project + sidecar dep stateinstall-sidecarz,Run npm install inside the sidecar directory	telemetryz.Show or toggle Spectrum SDK telemetry (on/off)state?)onoffz9Turn telemetry on or off (omit to show the current state))nargschoicesr   )func)add_subparsers
add_parseradd_argumentset_defaultsdispatch)parsersubsp_setupp_telemetrys       O/root/.hermes/venv/lib/python3.12/site-packages/plugins/platforms/photon/cli.pyregister_clir'   '   sJ     &6 GDooI  G )4F  HDK  M65D1`  b1,O  Q 	OOH#MONOO%,ZO[//= " K sMH  
 X&    c                    t        | dd       }|t        |       S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt        |       S |dk(  rt	        |       S t        d| t        j                         y)	Nr   r   r   r   r   zunknown subcommand: file   )getattr_cmd_status
_cmd_setup_cmd_install_sidecar_cmd_telemetryprintsysstderr)argssubs     r&   r!   r!   M   s    
$($
/C
{4  
g~$
h4  
#D))
kd##	 
&SZZ8r(   c                    d }	 t        j                  | j                   |      }|}t	        dt        j                                 y# t        $ r(}t	        d| t
        j                         Y d}~yd}~ww xY w)	u  Run the RFC 8628 device-code login flow and persist the token.

    Internal helper — invoked as the first step of ``setup``. There is
    no standalone ``hermes photon login`` command; Photon onboards
    through the single ``setup`` surface like every other channel.
    c                    | j                   xs | j                  }t                t        d       t        d|        t        d| j                          t        d       t        d       t                y )Nu   ┌─ Photon device login ────────────────────────────────────────u   │  Open this URL:  u   │  Enter the code: u0   │  (waiting for approval — Ctrl-C to cancel)   └──────────────────────────────────────────────────────────────)verification_uri_completeverification_urir2   	user_code)codetargets     r&   _print_codez&_run_device_login.<locals>._print_codeh   sn    //H43H3H  d  	e%fX./%dnn%567@A  N  	Or(   )open_browseron_user_codezlogin failed: r*   Nr   u!   ✓ logged in — token saved to r   )photon_authlogin_device_flow
no_browser	Exceptionr2   r3   r4   _auth_json_path)r5   r?   tokene_s        r&   _run_device_loginrJ   a   sw    --!__,$
 	A	-k.I.I.K-L
MN  qc"4s   "A
 
	A;A66A;c                8
   t        j                         }|sOt        d       t        |       }|dk7  r|S t        j                         }|s't        dt        j
                         yt        d       | j                  xs t         j                  }t        j                         }	 |rt        d       nt        j                  ||      }|r&|j                  d      r|d   }t        d	| d
       nBt        d| d       t        j                  ||      }|j                  d      }t        d       |st        dt        j
                         y	 t        d       |}t        j                  ||      }	t        j                  ||	||       t        d| d       | j                  xs# t!        t#        dt$        j&                              }
d }d }d }|
st        d       n| j(                  }| j*                  }	 t        j,                  ||	|
|| j.                  |      \  }}t        |rdnd       |
}|j                  d      }t        j2                  |      }t5        |
       |s*	 t        j6                  ||      }|r|j                  d      }|rt                t        t#        d t$        j8                               t        t#        d!t$        j8                        t#        t;        |      t$        j8                  t$        j<                        z          t        t#        d"t$        j8                               t        t#        d#t$        j8                               nt        d$       |r'	 t        j>                  |||rt;        |      nd |%       | j@                  rt        d'       nt        d(       tC               }|dk7  r|S t                t        d)       t        d*       y# t        $ r(}t        d| t        j
                         Y d }~yd }~ww xY w# t        $ r(}t        d| t        j
                         Y d }~yd }~ww xY w# t0        $ r(}t        d| t        j
                         Y d }~yd }~wt        $ r(}t        d| t        j
                         Y d }~yd }~ww xY w# t        $ r*}t        d| dt        j
                         Y d }~Nd }~ww xY w# t        $ r*}t        d&| dt        j
                         Y d }~d }~ww xY w)+Nu7   [1/5] No Photon token found — running device login...r   z(login completed but token was not storedr*   r   z#[1/5] Reusing existing Photon tokenz'[2/5] Reusing configured Photon projectidz[2/5] Found existing project ''z[2/5] Creating Photon project 'z'...)nameu     ✓ project createdzproject setup failed: z%could not resolve a Photon project idz*[3/5] Provisioning Spectrum credentials...)spectrum_project_idproject_secretdashboard_project_idrN   u!     ✓ Spectrum ready (project id u   ) — secret savedzspectrum provisioning failed: z=[4/5] Your iMessage phone number (E.164, e.g. +15551234567): zL      Skipped user registration (no phone given). Re-run with --phone later.)phone_number
first_name	last_nameemailz      invalid phone number: z       user registration failed: u     ✓ phone registeredu     ✓ phone already registeredphoneNumberz*      (could not fetch the assigned line: )u   ┌─ Your agent's iMessage number ───────────────────────────────u
   │  📱 u<   │  Text this number from your phone to talk to your agent.r9   uC         No iMessage line assigned yet — check the Photon dashboard.)rR   assigned_phone_numberuser_idrQ   z.      (could not save Photon status metadata: z;[5/5] Skipping sidecar npm install (--skip-sidecar-install)z3[5/5] Installing Node sidecar deps (spectrum-ts)...u   ✓ Photon setup complete.z*  Start the gateway:  hermes gateway start)"rB   load_photon_tokenr2   rJ   r3   r4   project_nameDEFAULT_PROJECT_NAMEload_dashboard_project_idfind_project_by_namegetcreate_projectrE   regenerate_project_secretstore_project_credentialsphone_promptr   r   CYANrS   rU   register_user_if_absentrT   
ValueErroruser_assigned_line_autoconfigure_accessget_imessage_lineGREENstrBOLDstore_user_numbersskip_sidecar_install_install_sidecar)r5   rG   rcrN   dashboard_idexistingcreatedrH   spectrum_idsecretrc   agent_numberregistered_phoneregistered_user_idrS   rU   userlines                     r&   r/   r/      s   ))+EGHt$7I--/<3::N34 @ @ @D88:L;<"77tDHHLL.'~6tfA>?7vTBC%44UF&{{40-. 5CJJG:;"66ulK-- +!!-		
 	1+>PQR JJ 'KKK	
E L\] __


	'??V"%..MD' 	'&7WX !XXd^
 #55d; 	e$ 	V00ED#xx6 e  X  Z`  Zf  Zf  g  	h,-C%v||V[[AB	
 	eRTZT`T`abe  T  V\  Vb  Vb  c  	dST	Z**-&23E./4%1	   KLCD7I	G	
&'	
67i  &qc*<0  .qc2D:  	043::F 	4QC8szzJ	0  	V>qcC#**UU	V*  	ZB1#QGcjjYY	Zs   $BO% AP :(Q ')R0 *&S& %	P.PP	Q
"QQ
	R-Q99R-R((R-0	S#9SS#&	T/TTc                   	 ddl m}m} dD ]9  \  }}	  ||      rt	        d| d        |||        t	        d| d| d	       ; y# t        $ r Y yw xY w# t
        $ r+}t	        d
| d| t        j                         Y d}~{d}~ww xY w)a  Allowlist the operator and set their DM as the cron home channel.

    Writes ``PHOTON_ALLOWED_USERS`` (so the gateway authorizes the operator's
    own inbound messages instead of denying them) and ``PHOTON_HOME_CHANNEL``
    (the default space for cron delivery) to the operator's E.164 number. Each
    is only filled when unset, so a hand-tuned allowlist / home channel is
    never clobbered on a re-run.
    r   )get_env_valuesave_env_valueN))PHOTON_ALLOWED_USERSzallowlisted your number)PHOTON_HOME_CHANNELz$set your DM as the cron home channelz      u"    already set — leaving it as-is.u     ✓ z (rW   z      could not set z: r*   )hermes_cli.configr}   r~   ImportErrorr2   rE   r3   r4   )rc   r}   r~   keylabelrH   s         r&   ri   ri     s    C F
U	FS!se#EFG3&F5'C5*+F    	F(Rs3#**EE	Fs-   A	 AA		AA	B!!BBc                N   t                t        j                  t               t	        j
                  d      xs t        j                  d      }t        dz  j                         }t        d|xs d        t        d|rdnd        t        d	t               rd
nd d       y)NPHOTON_NODE_BINnodenode_modulesz  node binary         : u   ✗ missing (install Node 18+)z  sidecar deps        : u   ✓ installedu'   ✗ run `hermes photon install-sidecar`z  telemetry           : r   r   z# (`hermes photon telemetry on|off`)r   )_refresh_status_numbersrB   print_credential_summaryr2   osgetenvshutilwhich_SIDECAR_DIRexists_telemetry_enabled)_argsnode_binsidecar_installeds      r&   r.   r.   2  s     ((/yy*+Cv||F/CH%6>>@	$X%Q1Q$R
ST	$8I_Ox$y
z{	$-?-ATu$MMp
qrr(   c                    t        j                         \  } }| r|ry t        j                         \  }}|r|sy 	 t        j                  ||       y # t        $ r)}t        d| dt        j                         Y d }~y d }~ww xY w)Nz.      (could not refresh Photon user numbers: rW   r*   )rB   load_user_numbersload_project_credentialsrefresh_user_numbersrE   r2   r3   r4   )rc   assignedru   rP   rH   s        r&   r   r   @  sz    !335OE8"-"F"F"HKnV((nE V>qcC#**UUVs   A 	BA>>Bc                    t               S )N)rp   )r   s    r&   r0   r0   M  s    r(   c                     	 ddl m}   | d      }|xs dj                         j                         dv S # t        $ r t        j                  d      }Y Dw xY w)zRead PHOTON_TELEMETRY from the env / ~/.hermes/.env.

    Mirrors the sidecar's truthy set (index.mjs) so the state shown here
    always matches what the sidecar will actually do.
    r   )r}   PHOTON_TELEMETRY )1trueyesr   )r   r}   r   r   r   striplower)r}   raws     r&   r   r   Q  sY    ,3./ I2$$&*DDD  ,ii*+,s   4 AAc                8   t        | dd       }|&t        dt               rdnd        t        d       y	 ddlm}  |d|dk(  rd	nd
       t        d| d       t        d       y# t
        $ r(}t        d| t        j                         Y d }~yd }~ww xY w)Nr   zPhoton telemetry: r   r   zK  Toggle with `hermes photon telemetry on` / `hermes photon telemetry off`.r   )r~   r   r   falsez!could not save PHOTON_TELEMETRY: r*   r   u   ✓ Spectrum telemetry turned z% (PHOTON_TELEMETRY in ~/.hermes/.env)zL  Restart the gateway for the sidecar to pick it up:  hermes gateway restart)r-   r2   r   r   r~   rE   r3   r4   )r5   r   r~   rH   s       r&   r1   r1   _  s    D'4(E}"+=+?4U"KLM[\4)Ud]6P 
*5'1V
WX	
XY  1!5CJJGs   A( (	B1BBc                    t        j                  d      xs d} t        j                  |       st        dt        j                         yt        dt
         d|  d       t        j                  | dgt        t
              d	
      }|j                  dk7  r6t        d|  d       t        j                  | dgt        t
              d	
      }|j                  dk7  rt        dt        j                         |j                  S )NnpmzInpm is not on PATH. Install Node.js 18+ (https://nodejs.org/) and re-run.r*   r   z  $ cd z && z ciciF)cwdcheckr   u&     npm ci failed — falling back to:  z installinstallznpm install failed)
r   r   r2   r3   r4   r   
subprocessrunrl   
returncode)r   procs     r&   rp   rp   p  s    
,,u

&C<<	

  
GL>cU#
./>>	dD
 !6se8DE~~)L!

 !"4??r(   c            
     T    t        j                  dddddddd      } t        |        y)zCRun Photon first-time setup from the `hermes gateway setup` wizard.r   NF)r   r[   rc   rS   rT   rU   rD   ro   )argparse	Namespacer/   )r5   s    r&   gateway_setupr     s4    "	D tr(   F)rv   c                   t         j                  j                         sy	 |r#t        j                  |       j	                         S t        |       j	                         S # t        t        f$ r t                Y yw xY w)Nr   )	r3   stdinisattygetpassr   inputKeyboardInterruptEOFErrorr2   )promptrv   s     r&   rd   rd     sd    99??6*0022V}""$$x( s   $A A A;:A;)r"   zargparse.ArgumentParserreturnNone)r5   argparse.Namespacer   int)rc   rl   r   r   )r   r   r   r   )r   r   )r   bool)r   r   )r   rl   rv   r   r   rl   )!__doc__
__future__r   r   r   r   r   r   r3   pathlibr   hermes_cli.colorsr   r   r   r   rB   __file__parentr   r'   r!   rJ   r/   ri   r.   r   r0   r   r1   rp   r   rd    r(   r&   <module>r      s   $ #   	   
  + !H~$$y0 'L(@SlF6
VE" Z$ ,1 	r(   