okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ea2d565e6
main
1import { bindingFor , bindingEqual , eventBinding , type Binding } from './shortcut-bindings' ; 2/** Shared action descriptors for keyboard shortcuts, palette and chat commands. */ 3import type { IconName } from '../components/ui' ; 4import type { Route } from './router' ; 5import { 6exportConversation , getState , go , newSession , pushToast , 7selectSession , 8restartEngine , setFocus , setNavOpen , setPalette , setShortcuts , setTheme , 9toggleFocus , toggleRail , unloadModel , 10} from './store' ; 11import { recentChats } from './recent-chats' ; 12 13export const MOD = / Mac|iPhone|iPad / . test ( navigator . platform ) ?'⌘' :'Ctrl' ; 14interface AppCommand { 15id :string ; 16label :string ; 17icon :IconName ; 18group :'Session' | 'Engine' | 'Navigation' | 'Model' | 'Chat' ; 19hint ?:string ; 20keywords ?:string ; 21key ?:string ; 22binding ?:Binding | null ; 23shift ?:boolean ; 24alt ?:boolean ; 25slash ?:string []; 26palette ?:boolean ; 27danger ?:boolean ; 28available ?:() => boolean ; 29unavailable ?:string ; 30run :() => void ; 31} 32export const DESTINATIONS :{ route :Route ; label :string ; icon :IconName ; keywords :string ; key :string ; slash ?:string }[] = [ 33{ route :'chat' , label :'Chat' , icon :'chat' , keywords :'conversation prompt ask' , key :'1' }, 34{ route :'monitor' , label :'Monitor' , icon :'activity' , keywords :'stats performance speed hardware' , key :'2' , slash :'monitor' }, 35{ route :'serving' , label :'Serving' , icon :'link' , keywords :'api server endpoint openai anthropic port' , key :'3' , slash :'serve' }, 36{ route :'models' , label :'Models' , icon :'models' , keywords :'download weights gguf choose tune engine parameters context sampling' , key :'4' , slash :'model' }, 37{ route :'tasks' , label :'Tasks' , icon :'clock' , keywords :'schedule recurring loop automation' , key :'6' , slash :'tasks' }, 38{ route :'skills' , label :'Skills' , icon :'skills' , keywords :'instructions prompts abilities' , key :'7' , slash :'skills' }, 39{ route :'settings' , label :'Settings' , icon :'settings' , keywords :'preferences theme appearance text size' , key :'8' }, 40]; 41export const APP_COMMANDS :AppCommand [] = [ 42{ id :'new' , label :'New chat' , hint :'Start a new conversation; keep this one' , group :'Session' , key :'n' , icon :'new-chat' , slash :[ 'new' ], run :() => { const s = newSession (); go ( 'chat' , s . id ); } }, 43{ id :'palette' , label :'Command palette' , group :'Session' , key :'k' , icon :'search' , palette :false , run :() => setPalette ( ! getState (). paletteOpen ) }, 44{ id :'sidebar' , label :'Show or hide sidebar' , group :'Session' , key :'b' , icon :'panel' , run :() => window . innerWidth < 1280 ?setNavOpen ( ! getState (). navOpen ) :toggleRail () }, 45{ id :'settings' , label :'Settings' , group :'Session' , key :',' , icon :'settings' , palette :false , run :() => go ( 'settings' ) }, 46{ id :'shortcuts' , label :'Keyboard shortcuts' , keywords :'keys bindings help' , group :'Session' , key :'/' , icon :'bolt' , run :() => setShortcuts ( ! getState (). shortcutsOpen ) }, 47{ id :'dictation' , label :'Start or stop dictation' , hint :'Speak into the message box without sending' , keywords :'speech microphone voice transcribe' , group :'Session' , key :'d' , shift :true , icon :'mic' , available :() => window . location . hash . startsWith ( '#/chat' ) && ! getState (). streaming , unavailable :'Open a chat and wait for the current response to finish.' , run :() => window . dispatchEvent ( new Event ( 'strata:dictation' )) }, 48{ id :'restart' , label :'Restart engine' , hint :'Reloads the model into memory' , keywords :'reboot refresh' , group :'Engine' , key :'r' , shift :true , icon :'refresh' , danger :true , run :() => void restartEngine () }, 49{ id :'unload' , label :'Unload model' , hint :'Frees model memory' , keywords :'eject stop vram' , group :'Engine' , key :'u' , shift :true , icon :'stop' , danger :true , available :() => !! getState (). library ?. loaded , unavailable :'No model is loaded.' , run :() => void unloadModel () }, 50{ id :'go:tuning' , label :'Model settings' , group :'Navigation' , icon :'engine' , key :'5' , alt :true , palette :false , slash :[ 'tune' ], run :() => go ( 'models' ) }, 51{ id :'monitor' , label :'Open Monitor' , group :'Engine' , key :'m' , shift :true , icon :'activity' , palette :false , run :() => go ( 'monitor' ) }, 52{ id :'focus' , label :'Focus on the conversation' , group :'Session' , key :'f' , shift :true , icon :'chat' , run :() => { go ( 'chat' , getState (). activeSession ?? undefined ); toggleFocus (); } }, 53{ id :'export' , label :'Export conversation' , hint :'Download the active conversation as Markdown' , keywords :'save download markdown' , group :'Session' , icon :'download' , slash :[ 'export' ], available :() => getState (). sessions . some ( s => s . id === getState (). activeSession && s . messages . length > 0 ), unavailable :'Open a conversation with messages first.' , run :() => exportConversation () }, 54{ id :'setup' , label :'Run setup' , hint :'Check computer, pick a version and load' , keywords :'wizard onboarding install resume' , group :'Engine' , icon :'bolt' , slash :[ 'setup' ], run :() => go ( 'setup' ) }, 55]; 56 57/** All model version changes happen on the Model screen. */ 58export function getCommands () :AppCommand [] { 59const state = getState (); 60const dark = state . theme === 'dark' || ( state . theme === 'system' && matchMedia ( '(prefers-color-scheme: dark)' ). matches ); 61const commands :AppCommand [] = [ 62 ...APP_COMMANDS , 63 ...Array . from ({ length :6 }, ( _ , index ) => ({ id :`chat:slot: ${ index + 1 } ` , label :`Chat ${ index + 1 } ` , icon :'chat' as const , group :'Chat' as const , key :String ( index + 1 ), hint :recentChats ( state . sessions , state . activeSession )[ index ]?. title , available :() => !! recentChats ( getState (). sessions , getState (). activeSession )[ index ], unavailable :'No saved chat in this position.' , run :() => { const session = recentChats ( getState (). sessions , getState (). activeSession )[ index ]; if ( session ) { selectSession ( session . id ); go ( 'chat' , session . id ); } } })), 64 ...DESTINATIONS . map ( d => ({ id :`go: ${ d . route } ` , label :d . label , icon :d . icon , keywords :d . keywords , key :d . key , alt :true , group :'Navigation' as const , slash :d . slash ?[ d . slash ] :undefined , run :() => go ( d . route ) })), 65{ id :'theme' , label :dark ?'Switch to light theme' :'Switch to dark theme' , keywords :'appearance colours' , icon :dark ?'sun' :'moon' , group :'Session' , run :() => setTheme ( dark ?'light' :'dark' ) }, 66 67]; 68return commands . map ( c => ({ ...c , binding :bindingFor ( c . id , c . key ?{ key :c . key , mod :true , shift :!! c . shift , alt :!! c . alt } :null )})); 69} 70export function runCommand ( command :AppCommand | string ) { 71const action = typeof command === 'string' ?getCommands (). find ( c => c . id === command ) :command ; 72if ( ! action ) return ; 73if ( action . available && ! action . available ()) { pushToast ( 'error' , action . unavailable ?? 'This action is unavailable.' ); return ; } 74action . run (); 75} 76export function getSlashCommands () { 77return getCommands (). filter ( c => ! c . available || c . available ()). flatMap ( c => ( c . slash ?? []). map ( alias => ({ 78id :` ${ c . id } : ${ alias } ` , label :`/ ${ alias } ` , hint :c . hint ?? `Open ${ c . label } ` , icon :c . icon , run :() => runCommand ( c ), 79}))); 80} 81export function handleAppShortcut ( e :KeyboardEvent ) { 82if ( e . defaultPrevented || e . isComposing || getState (). shortcutsOpen ) return ; 83const meta = e . metaKey || e . ctrlKey ; 84const typing = ( e . target as HTMLElement )?. matches ( 'input, textarea, [contenteditable="true"]' ); 85if ( ! meta ) { 86if ( e . key === '?' && ! typing ) { e . preventDefault (); setShortcuts ( true ); } 87if ( e . key === 'Escape' ) { 88const state = getState (); 89if ( state . paletteOpen ) setPalette ( false ); 90else if ( state . shortcutsOpen ) setShortcuts ( false ); 91else if ( state . navOpen ) setNavOpen ( false ); 92else if ( state . focus ) setFocus ( false ); 93} 94return ; 95} 96const command = getCommands (). find ( c => bindingEqual ( c . binding ?? null , eventBinding ( e ))); 97if ( command ) { e . preventDefault (); runCommand ( command ); } 98}