okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { tr } from '../lib/i18n' ; 2import { useEffect , useRef , useState } from 'react' ; 3import { Btn , ErrorNotice , Dialog , Icon } from './ui' ; 4import { CopyBtn } from './CopyBtn' ; 5import { api } from '../lib/client' ; 6import { setCloudflare , useStore } from '../lib/store' ; 7import type { TunnelState } from '../lib/types' ; 8export function CloudflareDialog ({ open, onClose, onCreateKey} :{ 9open :boolean ; 10onClose :() => void ; 11onCreateKey :() => void ; 12}) { 13const serving = useStore ( s => s . serving ); 14const [ mode , setMode ] = useState < 'quick' | 'domain' >( 'quick' ), [ step , setStep ] = useState ( 0 ); 15const [ hostname , setHostname ] = useState ( '' ), [ token , setToken ] = useState ( '' ), [ pending , setPending ] = useState ( false ), [ error , setError ] = useState ( '' ); 16const [ detected , setDetected ] = useState < TunnelState | null >( null ), [ retry , setRetry ] = useState ( 0 ); 17const heading = useRef < HTMLHeadingElement >( null ); 18const tunnel = serving ?. tunnel ?? detected ; 19const active = !! tunnel && [ 'starting' , 'connected' , 'stopping' ]. includes ( tunnel . status ); 20const steps = mode === 'quick' ?[ 'Address' , 'Connector' , 'API key' , 'Connect' ] :[ 'Address' , 'Connector' , 'API key' , 'Domain' , 'Connect' ]; 21const last = steps . length - 1 ; 22const installed = !! tunnel ?. installed , keyReady = !! serving ?. access . apiKeySet , other = tunnel ?. external ?? null ; 23const canContinue = step === 1 ?installed :step === 2 ?true :step === 3 && mode === 'domain' ?!! hostname . trim () && !! token . trim () :true ; 24async function inspect () { setPending ( true ); setError ( '' ); try { 25setDetected ( await api . get < TunnelState >( '/api/serving/tunnel' )); 26} 27catch ( e ) { 28setError (( e as Error ). message ); 29} 30finally { 31setPending ( false ); 32} } 33useEffect (() => { if ( open ) 34void inspect (); 35else { 36setToken ( '' ); 37setError ( '' ); 38} }, [ open ]); 39useEffect (() => { 40if ( ! open || step !== 1 || installed ) 41return ; 42let alive = true ; 43setPending ( true ); 44setError ( '' ); 45void setCloudflare ({ action :'prepare' }). then (() => { if ( alive ) { 46setPending ( false ); 47setStep ( 2 ); 48} }). catch ( e => { if ( alive ) { 49setError (( e as Error ). message ); 50setPending ( false ); 51} }); 52return () => { alive = false ; }; 53}, [ open , step , retry ]); 54useEffect (() => { if ( open && step > 0 ) 55heading . current ?. focus ({ preventScroll :true }); }, [ step , open ]); 56function dismiss () { setStep ( 0 ); onClose (); } 57function next () { setError ( '' ); setStep ( s => s === 0 && installed ?2 :Math . min ( last , s + 1 )); } 58function back () { setError ( '' ); if ( ! step ) 59dismiss (); 60else 61setStep ( s => s === 2 && installed ?0 :s - 1 ); } 62async function connect () { setPending ( true ); setError ( '' ); try { 63await setCloudflare ({ action :'start' , mode, hostname, token}); 64setToken ( '' ); 65} 66catch ( e ) { 67setError (( e as Error ). message ); 68} 69finally { 70setPending ( false ); 71} } 72async function stop () { setPending ( true ); setError ( '' ); try { 73await setCloudflare ({ action :'stop' }); 74setStep ( 0 ); 75} 76catch ( e ) { 77setError (( e as Error ). message ); 78} 79finally { 80setPending ( false ); 81} } 82const titles = [ 'Choose your address' , installed ?'Connector ready' :'Preparing your connector' , 'Protect your connection' , ...( mode === 'domain' ?[ 'Set up your domain' ] :[]), 'Ready to connect' ]; 83return < Dialog open = { open } title = { tr ( "Connect with Cloudflare" )} onClose = { dismiss } actions = {<> 84{ tr ( active ?<>< Btn onClick = { dismiss }>{ tr ( "Done" )}</ Btn >< Btn disabled = { pending || tunnel ?. status === 'stopping' } onClick = {() => void stop ()}>{ tr ( tunnel ?. status === 'stopping' ?'Stopping…' :'Stop tunnel' )}</ Btn ></> :<>< Btn disabled = { pending } onClick = { back }>{ tr ( step ?'Back' :'Cancel' )}</ Btn >{ tr ( step === last ?< Btn variant = "primary" disabled = { pending || ! installed || ( mode === 'domain' && ( ! hostname . trim () || ! token . trim ()))} onClick = {() => void connect ()}>{ tr ( pending ?'Connecting…' :( error || tunnel ?. error ) ?'Try again' :mode === 'quick' ?'Create temporary URL' :'Connect domain' )}</ Btn > :< Btn variant = "primary" disabled = { pending || ! canContinue } onClick = { next }>{ tr ( "Continue" )}< Icon name = "chevron" className = "wizard-next" size = { 14 }/></ Btn >)}</>)} 85</>}> 86< div className = "tunnel-wizard" > 87{ tr ( ! active ?< ol className = "tunnel-progress" aria-label = { tr ( "Connection progress" )}>{ tr ( steps . map (( label , i ) => < li key = { label } data-current = { step === i || undefined } data-complete = { step > i || undefined }>< button disabled = { i >= step || pending } aria-current = { step === i ?'step' :undefined } onClick = {() => { setError ( '' ); setStep ( i ); }}>< span >{ tr ( step > i ?< Icon name = "check" size = { 13 }/> :i + 1 )}</ span >< small >{ tr ( label )}</ small ></ button ></ li >))}</ ol > :null )} 88{ tr ( active ?< section className = "tunnel-stage tunnel-stage--status" key = { tunnel ?. status } aria-live = "polite" >< span className = "tunnel-stage__symbol" >< Icon name = { tunnel ?. status === 'connected' ?'check' :'clock' } size = { 26 }/></ span >< h3 className = "h3" >{ tr ( tunnel ?. status === 'connected' ?'You’re connected' :tunnel ?. status === 'stopping' ?'Stopping the tunnel…' :'Connecting to Cloudflare…' )}</ h3 >{ tr ( tunnel ?. url ?<>< p className = "small muted" >{ tr ( "Use this address and your Strata API key in your other app." )}</ p >< div className = "install-command" >< code >{ tunnel . url }</ code >< CopyBtn text = { tunnel . url ! } label = "Tunnel URL" title = "Copy tunnel URL" /></ div >< p className = "xs muted" >{ tr ( tunnel . mode === 'quick' ?'Temporary address · turn off streaming in your client.' :`Your Cloudflare route must point to ${ tunnel . originUrl } .` )}</ p ></> :< p className = "small muted" >{ tr ( "This usually takes a few seconds. You can close this window while it connects." )}</ p >)}</ section > :< section className = "tunnel-stage" key = { step } aria-labelledby = "tunnel-step-title" > 89< h3 ref = { heading } id = "tunnel-step-title" className = "h3" tabIndex = { - 1 }>{ tr ( titles [ step ])}</ h3 > 90{ tr ( step === 0 ?<>< p className = "small muted" >{ tr ( "Give your local API an HTTPS address that another app can reach." )}</ p >< div className = "tunnel-choices" role = "radiogroup" aria-label = { tr ( "Cloudflare connection type" )}>{ tr ([{ value :'quick' as const , title :'Free temporary URL' , detail :'No account or domain needed. Good for a quick connection.' , icon :'bolt' as const }, { value :'domain' as const , title :'My own domain' , detail :'A stable address on a domain you manage with Cloudflare.' , icon :'link' as const }]. map ( choice => < button key = { choice . value } role = "radio" aria-checked = { mode === choice . value } tabIndex = { mode === choice . value ?0 :- 1 } onKeyDown = { e => { if ([ 'ArrowRight' , 'ArrowLeft' , 'ArrowUp' , 'ArrowDown' ]. includes ( e . key )) { 91e . preventDefault (); 92setMode ( mode === 'quick' ?'domain' :'quick' ); 93( e . currentTarget . parentElement ?. children [ mode === 'quick' ?1 :0 ] as HTMLElement )?. focus (); 94} }} onClick = {() => setMode ( choice . value )}>< Icon name = { choice . icon } size = { 20 }/>< span >< strong >{ tr ( choice . title )}</ strong >< small >{ tr ( choice . detail )}</ small ></ span >< span className = "tunnel-choice-dot" >{ tr ( mode === choice . value ?< Icon name = "check" size = { 13 }/> :null )}</ span ></ button >))}</ div ></> :null )} 95{ tr ( step === 1 ?< div className = "tunnel-automatic" data-ready = { installed || undefined } aria-live = "polite" aria-busy = { pending }>< span className = "tunnel-stage__symbol" >< Icon name = { installed ?'check' :error ?'warning' :'refresh' } size = { 28 }/></ span >< p className = "small muted" >{ tr ( installed ?'Your Cloudflare connector is installed and ready.' :error ?'Automatic setup needs another try.' :'Strata downloads and sets up the Cloudflare connector for you.' )}</ p >{ tr ( installed ?<>{ tr ( tunnel ?. connector ?< p className = "xs muted" >{ tr ( "Using {0}" , [ tunnel . connector . summary ])}</ p > :null )}{ tr ( other ?< p className = "xs muted" role = "status" >{ tr ( other . others . length === 1 && ! other . own . length ?"cloudflared is already running for another tunnel (PID {0})." :other . own . length ?"Strata’s tunnel for this address is already running (PID {0})." :"cloudflared is already running for other tunnels (PIDs {0})." , [( other . own . length ?other . own :other . others ). join ( ", " )])}</ p > :null )}</> :error ?< Btn onClick = {() => setRetry ( v => v + 1 )}>{ tr ( "Retry setup" )}</ Btn > :< span className = "small muted" role = "status" >{ tr ( "Setting up\u2026" )}</ span >)}</ div > :null )} 96{ tr ( step === 2 ?<>< p className = "small muted" >{ tr ( keyReady ?"Only apps with your Strata API key can use this connection." :"Without an API key, anyone who finds the link can use your model." )}</ p >{ tr ( keyReady ?< div className = "tunnel-check" >< Icon name = "shield" />< div >< strong >{ tr ( "API key ready" )}</ strong >< span >{ tr ( "Use your existing key in the app you connect." )}</ span ></ div ></ div > :< div className = "tunnel-key-step" >< Icon name = "shield" size = { 30 }/>< p className = "small" >{ tr ( "Create a key to protect your API. You\u2019ll return here when it\u2019s ready. You can also continue without one, but anyone who finds the link can then use your model." )}</ p >< Btn onClick = { onCreateKey }>{ tr ( "Create an API key (recommended)" )}</ Btn ></ div >)}</> :null )} 97{ tr ( mode === 'domain' && step === 3 ?<>< p className = "small muted" >{ tr ( "Create a tunnel in your " )}< a href = "https://dash.cloudflare.com/" target = "_blank" rel = "noreferrer" >{ tr ( "Cloudflare dashboard \u2197" )}</ a >{ tr ( ". Copy the token from its connector setup." )}</ p >< label className = "input-field" >< span >{ tr ( "Tunnel token" )}</ span >< input type = "password" className = "st-input" autoComplete = "off" spellCheck = { false } value = { token } onChange = { e => setToken ( e . target . value )} placeholder = { tr ( "Paste only the token after --token" )}/></ label >< label className = "input-field" >< span >{ tr ( "Your hostname" )}</ span >< input className = "st-input" autoComplete = "off" spellCheck = { false } value = { hostname } onChange = { e => setHostname ( e . target . value )} placeholder = { tr ( "ai.example.com" )}/></ label >< div >< p className = "small muted" >{ tr ( "Add a published application route for this hostname with this service URL:" )}</ p >< div className = "install-command" >< code >{ tunnel ?. originUrl ?? 'Loading…' }</ code >< CopyBtn text = { tunnel ?. originUrl ?? '' } label = "Tunnel service URL" title = "Copy tunnel service URL" disabled = { ! tunnel ?. originUrl }/></ div ></ div >< p className = "xs muted" >{ tr ( "The token is used for this connection only and is not saved." )}</ p ></> :null )} 98{ tr ( step === last ?<>< p className = "small muted" >{ tr ( mode === 'quick' ?'Cloudflare will create your address when you connect.' :'Start the connector for the domain you configured.' )}</ p >< dl className = "tunnel-review" >< div >< dt >{ tr ( "Address" )}</ dt >< dd >{ tr ( mode === 'quick' ?'Free temporary URL' :hostname )}</ dd ></ div >< div >< dt >{ tr ( "Connector" )}</ dt >< dd >< Icon name = "check" size = { 14 }/>{ tr ( tunnel ?. connector ?. version ?"Installed · cloudflared {0}" :"Installed" , tunnel ?. connector ?. version ?[ tunnel . connector . version ] :undefined )}</ dd ></ div >< div >< dt >{ tr ( "Access" )}</ dt >< dd >< Icon name = "shield" size = { 14 }/>{ tr ( keyReady ?"API key required" :"No API key: anyone with the link can use it" )}</ dd ></ div ></ dl >< p className = "small muted" >{ tr ( mode === 'quick' ?'The address changes each time. Turn off streaming in your client; replies arrive all at once.' :'Use this hostname and your Strata API key in your client. Streaming is supported.' )}</ p >< p className = "xs muted" >{ tr ( "Only the inference API is shared. Your computer and Strata must stay on." )}</ p ></> :null )} 99</ section >)} 100{ tr ( error || tunnel ?. error ?< ErrorNotice message = { error || tunnel ?. error || 'Connection failed' } next = { tr ( "Check your internet connection, then retry." )}/> :null )} 101</ div > 102</ Dialog >; 103}