okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
74e25e165
main
1import { tr } from '../lib/i18n' ; 2/** 3* Settings -> Tools: what the assistant may use in chats, in plain words. 4* 5* Every row is a switch in the existing Field pattern. Nothing here is a new component: switches, selects, dialogs and buttons are 6* the ones the other Settings panels use. The host owns the truth (GET/PUT /api/agent/tools); a change applies from the next message. 7*/ 8import { useEffect , useState } from 'react' ; 9import { api } from '../lib/client' ; 10import { pushToast } from '../lib/store' ; 11import { Btn , Dialog , ErrorNotice , Field , Icon , Select } from './ui' ; 12 13export interface ToolGroup { 14id :string ; 15name :string ; 16about :string ; 17area :'computer' | 'web' | 'together' ; 18enabled :boolean ; 19/** Manual mode asks before this tool changes anything. */ 20asks :boolean ; 21} 22export interface CustomTool { 23id :string ; 24name :string ; 25about :string ; 26kind :'instructions' | 'command' | 'server' ; 27enabled :boolean ; 28/** Shown in the list: the command, the server address or the first words of the instructions. */ 29summary :string ; 30instructions ?:string ; 31command ?:string ; 32address ?:string ; 33args ?:string []; 34} 35export interface ToolsState { 36groups :ToolGroup []; 37search :{ provider :'builtin' | 'exa' | 'perplexity' | 'deepseek' ; keySet :boolean }; 38/** Skip the once-per-chat question before the first web search or page in Manual mode. Off unless the person turned it on. */ 39webWithoutAsking :boolean ; 40browser :{ enabled :boolean ; found :boolean ; name :string | null ; missing ?:'component' }; 41custom :CustomTool []; 42} 43 44const AREAS :{ id :ToolGroup [ 'area' ]; title :string }[] = [ 45{ id :'computer' , title :'On this computer' }, 46{ id :'web' , title :'On the web' }, 47{ id :'together' , title :'Working on bigger jobs' }, 48]; 49const SEARCH_CHOICES = [ 50{ value :'builtin' , label :'DuckDuckGo (no key)' }, 51{ value :'exa' , label :'Exa (your own key)' }, 52{ value :'perplexity' , label :'Perplexity (your own key)' }, 53{ value :'deepseek' , label :'DeepSeek (your own key)' }, 54]; 55const KIND_CHOICES = [ 56{ value :'instructions' , label :'Follow my instructions' }, 57{ value :'command' , label :'Run a command' }, 58{ value :'server' , label :'Connect a tool server (MCP)' }, 59]; 60const KIND_NAMES :Record < CustomTool [ 'kind' ], string > = { instructions :'Instructions' , command :'Command' , server :'Tool server' }; 61 62interface Draft { 63id ?:string ; 64name :string ; 65about :string ; 66kind :CustomTool [ 'kind' ]; 67instructions :string ; 68command :string ; 69address :string ; 70args :string ; 71} 72const blank = () :Draft => ({ name :'' , about :'' , kind :'instructions' , instructions :'' , command :'' , address :'' , args :'' }); 73 74export function SettingsTools ({ preview} :{ preview ?:ToolsState }) { 75const [ state , setState ] = useState < ToolsState | null >( preview ?? null ); 76const [ error , setError ] = useState ( '' ); 77const [ key , setKey ] = useState ( '' ); 78const [ draft , setDraft ] = useState < Draft | null >( null ); 79const [ formError , setFormError ] = useState ( '' ); 80const [ remove , setRemove ] = useState < CustomTool | null >( null ); 81const [ busy , setBusy ] = useState ( false ); 82 83async function load () { 84try { setState ( await api . get < ToolsState >( '/api/agent/tools' )); setError ( '' ); } 85catch ( e ) { setError ( e instanceof Error ?e . message :'The tool list could not be loaded.' ); } 86} 87useEffect (() => { if ( ! preview ) void load (); }, []); 88 89async function save ( change :Record < string , unknown >) { 90if ( preview ) return ; 91setBusy ( true ); 92try { setState ( await api . put < ToolsState >( '/api/agent/tools' , change )); setError ( '' ); } 93catch ( e ) { setError ( e instanceof Error ?e . message :'The change could not be saved.' ); void load (); } 94finally { setBusy ( false ); } 95} 96const toggle = ( id :string , on :boolean ) => { 97setState ( s => s && { ...s , groups :s . groups . map ( g => g . id === id ?{ ...g , enabled :on } :g ) }); 98void save ({ groups :{ [ id ] :on } }); 99}; 100 101async function submit () { 102if ( ! draft ) return ; 103if ( ! /^[A-Za-z][A-Za-z0-9 _-]{0,39}$ / . test ( draft . name . trim ())) { setFormError ( 'Give the tool a short name: letters, numbers, spaces or hyphens.' ); return ; } 104if ( ! draft . about . trim ()) { setFormError ( 'Say what the tool does, in a sentence. The assistant reads this to decide when to use it.' ); return ; } 105if ( draft . kind === 'instructions' && ! draft . instructions . trim ()) { setFormError ( 'Write the instructions the assistant should follow when it uses this tool.' ); return ; } 106if ( draft . kind === 'command' && ! draft . command . trim ()) { setFormError ( 'Enter the command to run.' ); return ; } 107if ( draft . kind === 'server' && ! draft . address . trim ()) { setFormError ( 'Enter the tool server’s web address, or the program that starts it.' ); return ; } 108setBusy ( true ); 109try { 110const body = { name :draft . name . trim (), about :draft . about . trim (), kind :draft . kind , instructions :draft . instructions , command :draft . command . trim (), address :draft . address . trim (), args :draft . args . split ( '\n' ). map ( a => a . trim ()). filter ( Boolean ) }; 111setState ( draft . id ?await api . patch < ToolsState >( `/api/agent/tools/custom/ ${ encodeURIComponent ( draft . id )} ` , body ) :await api . post < ToolsState >( '/api/agent/tools/custom' , body )); 112setDraft ( null ); setFormError ( '' ); 113} catch ( e ) { setFormError ( e instanceof Error ?e . message :'The tool could not be saved.' ); } 114finally { setBusy ( false ); } 115} 116 117if ( ! state ) return < div className = "stack stack--lg settings-models" >{ error ?< ErrorNotice message = { tr ( error )} onRetry = {() => void load ()} /> :< p className = "small muted" >{ tr ( 'Loading the tool list…' )}</ p >}</ div >; 118const keyed = state . search . provider !== 'builtin' ; 119 120return < div className = "stack stack--lg settings-tools" > 121< div >< h2 className = "h3" >{ tr ( 'Tools' )}</ h2 >< p className = "small muted settings-models__intro" >{ tr ( 'Choose what the assistant may use in your chats. A change applies from your next message. In Manual mode, anything that changes your computer still asks you first.' )}</ p ></ div > 122{ error ?< ErrorNotice message = { tr ( error )} onRetry = {() => void load ()} /> :null } 123{ AREAS . map ( area => < section className = "settings-section" key = { area . id }> 124< h2 className = "h4" style = {{ marginBottom :10 }}>{ tr ( area . title )}</ h2 > 125< div className = "st-card st-card--flush" > 126{ state . groups . filter ( g => g . area === area . id ). map ( g => < Field key = { g . id } label = { tr ( g . name )} hint = { tr ( g . about )}> 127< button className = "st-toggle" role = "switch" aria-checked = { g . enabled } aria-label = { tr ( g . name )} disabled = { busy } onClick = {() => toggle ( g . id , ! g . enabled )} /> 128</ Field >)} 129</ div > 130</ section >)} 131 132< section className = "settings-section" > 133< h2 className = "h4" style = {{ marginBottom :10 }}>{ tr ( 'Web search' )}</ h2 > 134< div className = "st-card st-card--flush" > 135< Field label = { tr ( 'Where results come from' )} hint = { tr ( keyed ?'Searches use your own account with this service.' :'Results come from DuckDuckGo, or from Bing when DuckDuckGo is busy. You do not need an account or a key.' )}> 136< Select ariaLabel = { tr ( 'Where results come from' )} value = { state . search . provider } options = { SEARCH_CHOICES } onChange = { value => { setKey ( '' ); void save ({ search :{ provider :value } }); }} /> 137</ Field > 138{ keyed ?< Field label = { tr ( 'Your key' )} hint = { tr ( state . search . keySet ?'A key is saved on this computer. Enter a new one to replace it.' :'Saved on this computer only. Strata never shows it again.' )}> 139< div className = "row" > 140< input className = "st-input" style = {{ width :190 }} type = "password" aria-label = { tr ( 'Your key' )} autoComplete = "new-password" spellCheck = { false } placeholder = { tr ( state . search . keySet ?'Leave blank to keep the saved key' :'Paste your key' )} value = { key } onChange = { e => setKey ( e . target . value )} /> 141< Btn size = "sm" disabled = { busy || ! key . trim ()} onClick = {() => { void save ({ search :{ provider :state . search . provider , key :key . trim () } }); setKey ( '' ); pushToast ( 'success' , tr ( 'Key saved' )); }}>{ tr ( 'Save key' )}</ Btn > 142</ div > 143</ Field > :null } 144< Field label = { tr ( 'Let the assistant search the web without asking' )} hint = { tr ( 'In Manual mode the assistant asks once per chat before it searches the web or opens a web page. Turn this on to skip that question.' )}> 145< button className = "st-toggle" role = "switch" aria-checked = { state . webWithoutAsking } aria-label = { tr ( 'Let the assistant search the web without asking' )} disabled = { busy } onClick = {() => void save ({ webWithoutAsking :! state . webWithoutAsking })} /> 146</ Field > 147</ div > 148</ section > 149 150< section className = "settings-section" > 151< h2 className = "h4" style = {{ marginBottom :10 }}>{ tr ( 'Web browser' )}</ h2 > 152< div className = "st-card st-card--flush" > 153< Field label = { tr ( 'Use a web browser' )} hint = { state . browser . found ?tr ( 'The assistant can open pages, click and type in {0}. In Manual mode every action asks first.' , [ state . browser . name ?? tr ( 'your browser' )]) :tr ( state . browser . missing === 'component' ?'The browser tool is not part of this installation.' :'Chrome or Edge was not found on this computer. Install one, then come back and turn this on.' )}> 154< button className = "st-toggle" role = "switch" aria-checked = { state . browser . enabled && state . browser . found } aria-label = { tr ( 'Use a web browser' )} disabled = { busy || ! state . browser . found } onClick = {() => void save ({ browser :{ enabled :! state . browser . enabled } })} /> 155</ Field > 156</ div > 157</ section > 158 159< section className = "settings-section" > 160< h2 className = "h4" style = {{ marginBottom :10 }}>{ tr ( 'Your own tools' )}</ h2 > 161< p className = "small muted" style = {{ marginBottom :10 }}>{ tr ( 'Teach the assistant something new: give it instructions to follow, a command to run, or connect a tool server. Commands and servers ask first in Manual mode.' )}</ p > 162{ state . custom . length ?< div className = "st-card st-card--flush" > 163{ state . custom . map ( t => < Field key = { t . id } label = { tr ( t . name )} hint = {<>{ tr ( t . about )} < span className = "muted" > · { tr ( KIND_NAMES [ t . kind ])}</ span ></>}> 164< div className = "row" > 165< Btn size = "sm" disabled = { busy } onClick = {() => { setFormError ( '' ); setDraft ({ id :t . id , name :t . name , about :t . about , kind :t . kind , instructions :t . instructions ?? '' , command :t . command ?? '' , address :t . address ?? '' , args :( t . args ?? []). join ( '\n' ) }); }}>{ tr ( 'Edit' )}</ Btn > 166< Btn size = "sm" disabled = { busy } onClick = {() => setRemove ( t )}>{ tr ( 'Remove' )}</ Btn > 167< button className = "st-toggle" role = "switch" aria-checked = { t . enabled } aria-label = { tr ( t . name )} disabled = { busy } onClick = {() => void save ({ custom :{ [ t . id ] :! t . enabled } })} /> 168</ div > 169</ Field >)} 170</ div > :< div className = "provider-empty" >< Icon name = "tool" size = { 26 } />< strong >{ tr ( 'No tools of your own yet' )}</ strong >< p className = "small muted" >{ tr ( 'For example: “Write release notes in our house style”, or a command that checks your website is up.' )}</ p ></ div >} 171< div style = {{ marginTop :12 }}>< Btn icon = "plus" block onClick = {() => { setFormError ( '' ); setDraft ( blank ()); }}>{ tr ( 'Add a tool' )}</ Btn ></ div > 172</ section > 173 174< Dialog open = { draft !== null } title = { tr ( draft ?. id ?'Edit tool' :'Add a tool' )} compact onClose = {() => setDraft ( null )} actions = {<>< Btn onClick = {() => setDraft ( null )}>{ tr ( 'Cancel' )}</ Btn >< Btn variant = "primary" disabled = { busy } onClick = {() => void submit ()}>{ tr ( 'Save tool' )}</ Btn ></>}> 175{ draft ?< div className = "stack" > 176< label className = "input-field" >< span >{ tr ( 'Name' )}</ span >< input className = "st-input" aria-label = { tr ( 'Name' )} value = { draft . name } placeholder = { tr ( 'For example: Release notes' )} spellCheck = { false } onChange = { e => setDraft ({ ...draft , name :e . target . value })} /></ label > 177< label className = "input-field" >< span >{ tr ( 'What it does' )}</ span >< input className = "st-input" aria-label = { tr ( 'What it does' )} value = { draft . about } placeholder = { tr ( 'One sentence. The assistant reads it to decide when to use the tool.' )} onChange = { e => setDraft ({ ...draft , about :e . target . value })} /></ label > 178< div className = "input-field" >< span >{ tr ( 'How it works' )}</ span >< Select ariaLabel = { tr ( 'How it works' )} value = { draft . kind } options = { KIND_CHOICES } onChange = { value => setDraft ({ ...draft , kind :value as Draft [ 'kind' ] })} /></ div > 179{ draft . kind === 'instructions' ?< label className = "input-field" >< span >{ tr ( 'Instructions' )}</ span >< textarea className = "st-input" rows = { 6 } aria-label = { tr ( 'Instructions' )} value = { draft . instructions } placeholder = { tr ( 'Write what the assistant should do when it uses this tool.' )} onChange = { e => setDraft ({ ...draft , instructions :e . target . value })} /></ label > :null } 180{ draft . kind === 'command' ?<> 181< label className = "input-field" >< span >{ tr ( 'Command to run' )}</ span >< input className = "st-input" aria-label = { tr ( 'Command to run' )} value = { draft . command } placeholder = { tr ( 'For example: curl -I https://example.com' )} spellCheck = { false } onChange = { e => setDraft ({ ...draft , command :e . target . value })} /></ label > 182< p className = "small muted" >{ tr ( 'The assistant can pass text to the command. The command reads it from the STRATA_INPUT setting. Its printed output goes back to the assistant.' )}</ p > 183</> :null } 184{ draft . kind === 'server' ?<> 185< label className = "input-field" >< span >{ tr ( 'Web address or program' )}</ span >< input className = "st-input" aria-label = { tr ( 'Web address or program' )} value = { draft . address } placeholder = { tr ( 'https://… or the program that starts the server' )} spellCheck = { false } onChange = { e => setDraft ({ ...draft , address :e . target . value })} /></ label > 186< label className = "input-field" >< span >{ tr ( 'Extra settings for the program ' )}< span className = "muted" >{ tr ( '(optional, one per line)' )}</ span ></ span >< textarea className = "st-input" rows = { 3 } aria-label = { tr ( 'Extra settings for the program' )} value = { draft . args } spellCheck = { false } onChange = { e => setDraft ({ ...draft , args :e . target . value })} /></ label > 187</> :null } 188{ formError ?< p className = "small" role = "alert" style = {{ color :'var(--danger, #b3261e)' }}>{ tr ( formError )}</ p > :null } 189</ div > :null } 190</ Dialog > 191< Dialog open = { remove !== null } title = { tr ( 'Remove this tool?' )} compact onClose = {() => setRemove ( null )} actions = {<>< Btn onClick = {() => setRemove ( null )}>{ tr ( 'Keep it' )}</ Btn >< Btn variant = "primary" onClick = {() => { const id = remove ! . id ; setRemove ( null ); void ( async () => { try { setState ( await api . del < ToolsState >( `/api/agent/tools/custom/ ${ encodeURIComponent ( id )} ` )); } catch ( e ) { setError ( e instanceof Error ?e . message :'The tool could not be removed.' ); } })(); }}>{ tr ( 'Remove' )}</ Btn ></>}> 192< p >{ remove ?tr ( '“{0}” will no longer be available to the assistant.' , [ remove . name ]) :null }</ p > 193</ Dialog > 194</ div >; 195}