okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a1cc285fa
main
1import { tr } from '../lib/i18n' ; 2/** Quiet skill rows; all editing lives in one focused dialog. */ 3import { useState } from 'react' ; 4import { Btn , Dialog , Empty , Icon , IconBtn , Select , ErrorNotice } from '../components/ui' ; 5import { Screen } from '../components/layout' ; 6import { api } from '../lib/client' ; 7import { pageIsOnThisComputer , postWithFolderFallback } from '../lib/folder-pick' ; 8import { editSkill , loadSkills , toggleSkill , useStore } from '../lib/store' ; 9import type { Skill } from '../lib/types' ; 10export function SkillsScreen () { 11const skills = useStore ( s => s . skills ); 12const workspaces = useStore ( s => s . workspaces ); 13const workspace = useStore ( s => s . activeWorkspace ); 14const [ query , setQuery ] = useState ( '' ); 15const [ pending , setPending ] = useState < string | null >( null ); 16const [ error , setError ] = useState ( '' ); 17const [ tab , setTab ] = useState < 'details' | 'instructions' >( 'details' ); 18const [ editing , setEditing ] = useState < Skill | null >( null ); 19const [ triggers , setTriggers ] = useState ( '' ); 20const visible = skills . filter ( s => ` ${ s . source === 'bundled' ? tr ( s . name ) : s . name } ${ s . source === 'bundled' ? tr ( s . description ) : s . description } ${ s . triggers . join ( ' ' )} ` . toLowerCase (). includes ( query . toLowerCase ())); 21async function action ( id :string , fn :() => Promise < void >) { 22setPending ( id ); 23setError ( '' ); 24try { 25await fn (); 26} 27catch ( e ) { 28setError ( e instanceof Error ?e . message :String ( e )); 29} 30finally { 31setPending ( null ); 32} 33} 34async function add () { 35// The folder window opens on the computer that runs Strata. From a page on another computer there is nothing to see, so say so instead of waiting. 36if ( ! pageIsOnThisComputer ( window . location . hostname )) 37throw new Error ( tr ( "Adding a skill opens a folder window on the computer that runs Strata. Open Strata on that computer to add one." )); 38const result = await postWithFolderFallback <{ 39cancelled ?:boolean ; 40added ?:string ; 41}>( '/api/skills/add' ) as { 42cancelled ?:boolean ; 43added ?:string ; 44}; 45if ( ! result . cancelled ) 46await loadSkills (); 47} 48/** A skill written here: a name, what it is for, and the instructions. Saved as a folder the assistant reads; it is there for the next message. */ 49function openNew () { setTab ( 'details' ); setEditing ({ id :'' , name :'' , description :'' , body :'' , triggers :[], enabled :true , source :'created' , onlyIn :null , sizeKb :0 , useCount :0 , lastUsedAt :null } as unknown as Skill ); setTriggers ( '' ); setError ( '' ); } 50function openEditor ( skill :Skill ) { setTab ( 'details' ); setEditing ({ ...skill }); setTriggers ( skill . triggers . join ( ', ' )); setError ( '' ); } 51function closeEditor () { 52if ( ! pending ) { 53setEditing ( null ); 54setError ( '' ); 55} 56} 57async function save () { 58if ( ! editing ) 59return ; 60if ( ! editing . id ) { 61await api . post ( '/api/skills' , { name :editing . name . trim (), description :editing . description . trim (), body :editing . body ?? '' , enabled :editing . enabled , triggers :triggers . split ( ',' ). map ( t => t . trim ()). filter ( Boolean ) }); 62await loadSkills (); 63setEditing ( null ); 64return ; 65} 66await editSkill ( editing . id , { name :editing . name . trim (), description :editing . description . trim (), body :editing . body , triggers :triggers . split ( ',' ). map ( t => t . trim ()). filter ( Boolean ), enabled :editing . enabled , onlyIn :editing . onlyIn ?? null }); 67setEditing ( null ); 68} 69return < Screen title = { tr ( "Skills" )} titleDetail = { tr ( "Manage your skills library" )} actions = {<>< Btn icon = "plus" variant = "primary" disabled = { !! pending } onClick = { openNew }>{ tr ( "New skill" )}</ Btn >< Btn icon = "folder-plus" disabled = { !! pending } onClick = {() => void action ( 'add' , add )}>{ tr ( "Add skill" )}</ Btn >< input className = "st-input skills-header-search" type = "search" aria-label = { tr ( "Search skills" )} placeholder = { tr ( "Search skills…" )} value = { query } onChange = { e => setQuery ( e . target . value )}/>< IconBtn name = "refresh" title = { tr ( "Refresh skills" )} disabled = { !! pending } onClick = {() => void action ( 'refresh' , loadSkills )}/></>}> 70< div className = "skills-page" >{ tr ( error && ! editing ?< ErrorNotice message = { error } action = { tr ( "Retry" )} onRetry = {() => void action ( 'refresh' , loadSkills )}/> :null )} 71{ tr (([ 'bundled' , 'added' ] as const ). map ( group => { const rows = visible . filter ( s => group === 'bundled' ?s . source === 'bundled' :s . source !== 'bundled' ); return rows . length ?< section className = "skill-group" aria-label = { tr ( group === 'bundled' ?'Built-in skills' :'Added skills' )} key = { group }>{ tr ( group === 'added' ?< h3 className = "h4" >{ tr ( "Added skills" )}</ h3 > :null )}< div >{ tr ( rows . map ( s => < article className = "skill-row" key = { s . id }>< span className = "skill-symbol" >< Icon name = "skills" size = { 26 }/></ span >< div className = "skill-copy" >< h3 className = "h4" >{ s . source === 'bundled' ?tr ( s . name ) :s . name }</ h3 >< p className = "small muted" >{ s . source === 'bundled' ?tr ( s . description ) :s . description }</ p >{ tr ( s . onlyIn ?< span className = "xs muted" >{ tr ( s . onlyIn === workspace ?'This workspace' :`Only in ${ workspaces . find ( w => w . id === s . onlyIn )?. name ?? 'another workspace' } ` )}</ span > :null )}</ div >< div className = "skill-row-actions" >< IconBtn name = "pencil" title = { tr ( "Edit {0}" , [ s . name ])} disabled = { !! pending } onClick = {() => openEditor ( s )}/>< button className = "st-toggle" role = "switch" aria-checked = { s . enabled } aria-label = { tr ( "Enable {0}" , [ s . name ])} disabled = { !! pending } onClick = {() => void action ( s . id , () => toggleSkill ( s . id , ! s . enabled ))}/></ div ></ article >))}</ div ></ section > :null ; }))} 72{ tr ( ! visible . length ?< Empty icon = "skills" title = { tr ( skills . length ?'No matching skills' :'No skills yet' )} body = { skills . length ?'Try another search.' :'Add a folder containing SKILL.md.' } action = { tr (< Btn variant = { skills . length ?undefined :'primary' } onClick = {() => skills . length ?setQuery ( '' ) :void action ( 'add' , add )}>{ tr ( skills . length ?'Clear search' :'Add skill' )}</ Btn >)}/> :null )} 73 74</ div > 75< Dialog open = { !! editing } title = { tr ( editing && ! editing . id ?'New skill' :'Edit skill' )} onClose = { closeEditor } actions = {<>< Btn disabled = { !! pending } onClick = { closeEditor }>{ tr ( "Cancel" )}</ Btn >< Btn variant = "primary" disabled = { !! pending || ! editing ?. name . trim () || ! editing ?. body ?. trim ()} onClick = {() => void action ( editing ! . id , save )}>{ tr ( pending ?'Saving…' :editing && ! editing . id ?'Save skill' :'Save changes' )}</ Btn ></>}> 76{ tr ( editing ?<>< div className = "editor-tabs" role = "tablist" aria-label = { tr ( "Skill editor sections" )}>{ tr (([ 'details' , 'instructions' ] as const ). map ( t => < button key = { t } id = { `skill-tab- ${ t } ` } role = "tab" aria-selected = { tab === t } aria-controls = { `skill-panel- ${ t } ` } tabIndex = { tab === t ?0 :- 1 } onClick = {() => setTab ( t )} onKeyDown = { e => { 77if ([ 'ArrowLeft' , 'ArrowRight' ]. includes ( e . key )) { 78e . preventDefault (); 79const next = tab === 'details' ?'instructions' :'details' ; 80setTab ( next ); 81document . getElementById ( `skill-tab- ${ next } ` )?. focus (); 82} 83}}>{ tr ( t === 'details' ?'Details' :'Instructions' )}</ button >))}</ div >< div className = "form-stack settings-tab-panel" role = "tabpanel" id = { `skill-panel- ${ tab } ` } aria-labelledby = { `skill-tab- ${ tab } ` }> 84{ tr ( tab === 'details' ?<>< div className = "skill-detail-grid skill-detail-grid--name" >< label className = "input-field" >< span >{ tr ( "Name" )}</ span >< input className = "st-input" maxLength = { 120 } value = { editing . name } onChange = { e => setEditing ({ ...editing , name :e . target . value })}/></ label >< div className = "input-field skill-enable-field" >< span >{ tr ( "Enabled" )}</ span >< button className = "st-toggle" role = "switch" aria-label = { tr ( "Skill enabled" )} aria-checked = { editing . enabled } onClick = {() => setEditing ({ ...editing , enabled :! editing . enabled })}/></ div ></ div >< label className = "input-field" >< span >{ tr ( "Description" )}</ span >< input className = "st-input" maxLength = { 500 } value = { editing . description } onChange = { e => setEditing ({ ...editing , description :e . target . value })}/></ label > 85< div className = "skill-detail-grid" >< div className = "input-field" >< span id = "skill-scope-label" >{ tr ( "Available in" )}</ span >< Select ariaLabel = { tr ( "Skill workspace scope" )} value = { editing . onlyIn ?? 'all' } options = {[{ value :'all' , label :'All workspaces' }, ...workspaces . map ( w => ({ value :w . id , label :w . name }))]} onChange = { value => setEditing ({ ...editing , onlyIn :value === 'all' ?null :value })}/></ div > 86< label className = "input-field" >< span >{ tr ( "Trigger phrases" )}</ span >< input className = "st-input" value = { triggers } onChange = { e => setTriggers ( e . target . value )} placeholder = { tr ( "review this, check my code" )}/>< small className = "muted" >{ tr ( "Separate phrases with commas." )}</ small ></ label ></ div > 87</> :< label className = "input-field" >< span className = "sr-only" >{ tr ( "Instructions" )}</ span >< textarea className = "st-input skill-instructions" rows = { 10 } maxLength = { 64000 } value = { editing . body ?? '' } onChange = { e => setEditing ({ ...editing , body :e . target . value })}/></ label >)} 88{ tr ( editing . id ?< p className = "xs muted" >{ tr ( "Changes are saved in Strata. The original skill file stays intact." )}</ p > :null )}{ tr ( error ?< ErrorNotice message = { error } action = { tr ( "Retry" )} onRetry = {() => void action ( 'refresh' , loadSkills )}/> :null )}</ div ></> :null )} 89</ Dialog > 90</ Screen >; 91}