okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c5755ed83
main
1/** 2* An approval question in plain words: what the agent is about to do, as a sentence a person who never opens a terminal can answer. 3* 4* `askFor(name, args)` returns `{ id, params, why, detail }`: 5* - `id` + `params` name one fixed sentence the page translates (src/lib/ask.ts holds the English templates and `{0}` slots), 6* - `why` is an optional second fixed sentence id, `detail` a short plain line (a path) shown beneath it, 7* - the exact command or arguments stay on the card under "Details" for experts. 8* Anything not recognised falls back to a generic question, never to the raw command as the headline. 9*/ 10 11const KNOWN_FOLDERS = { documents :'Documents' , downloads :'Downloads' , desktop :'Desktop' , pictures :'Pictures' , music :'Music' , videos :'Videos' }; 12 13const clip = ( value , max = 120 ) => { const t = String ( value ?? '' ). replace ( / \s +/ g , ' ' ). trim (); return t . length > max ?` ${ t . slice ( 0 , max - 1 )} …` :t ; }; 14const unquote = text => String ( text ?? '' ). trim (). replace ( / ^ ([ '" ])( . * ) \1$ / , '$2' ); 15const baseName = path => { const t = unquote ( path ). replace ( / [ \\ / ] + $ / , '' ); return t . split ( / [ \\ / ] / ). pop () || t ; }; 16const hasExtension = path => / \. [ A - Za - z0 - 9 ]{ 1 , 8 } $ / . test ( baseName ( path )); 17const isExecutable = path => / \. ( exe | msi | bat | cmd | ps1 | com | scr | msix | appx | vbs | jar | sh ) $ / i . test ( unquote ( path )); 18 19/** Words of a command line, quotes kept together. */ 20function words ( text ) { 21const out = []; const re = / " ([ ^ " ] * ) " | ' ([ ^ ' ] * ) ' | ( \S + ) / g ; let m ; 22while (( m = re . exec ( text ))) out . push ( m [ 1 ] ?? m [ 2 ] ?? m [ 3 ]); 23return out ; 24} 25 26/** The first argument that is not a switch (-Name value pairs are skipped by the callers that need them). */ 27const firstPath = list => list . find ( w => ! /^- / . test ( w )) ?? '' ; 28 29function folderAsk ( target ) { 30const clean = unquote ( target ); 31const known = KNOWN_FOLDERS [ baseName ( clean ). toLowerCase ()]; 32if ( known ) return { id :'open-known-folder' , params :[ known ], why :'opens-window' , detail :clip ( clean , 160 ) }; 33if ( hasExtension ( clean )) return { id :'open-file' , params :[ baseName ( clean )], why :'opens-window' , detail :clip ( clean , 160 ) }; 34return { id :'open-folder' , params :[ baseName ( clean ) || clean ], why :'opens-window' , detail :clip ( clean , 160 ) }; 35} 36 37function shellAsk ( command ) { 38const text = String ( command ?? '' ). trim (); 39// Several steps in one command: say so rather than guessing at one of them. 40const steps = text . split ( / \s * (?: ; | && | \|\| ) \s *| \r ? \n / ). filter ( Boolean ); 41const first = steps [ 0 ] ?? '' ; 42const w = words ( first ); 43const verb = ( w [ 0 ] ?? '' ). toLowerCase (). replace ( / \.exe$ / , '' ); 44const rest = w . slice ( 1 ); 45if ( steps . length > 1 ) { 46const kinds = new Set ( steps . map ( s => ( words ( s )[ 0 ] ?? '' ). toLowerCase ())); 47if ( kinds . size > 1 ) return { id :'run-steps' , params :[ steps . length ], why :'run-steps-why' , detail :clip ( first , 100 ) }; 48} 49if ( verb === 'start-process' || verb === 'start' || verb === 'saps' ) { 50const pathAt = rest . findIndex (( x , i ) => ! /^- / . test ( x ) && ! ( i > 0 && / ^- ( argumentlist | args | workingdirectory | verb | wait ) $ / i . test ( rest [ i - 1 ]))); 51const nameFlag = rest . findIndex ( x => / ^- ( filepath | path ) $ / i . test ( x )); 52const target = nameFlag >= 0 ?rest [ nameFlag + 1 ] :rest [ pathAt ] ?? '' ; 53const lower = baseName ( target ). toLowerCase (); 54if ( / ^explorer ( \.exe ) ? $ / . test ( lower )) { 55const argAt = rest . findIndex ( x => / ^- ( argumentlist | args ) $ / i . test ( x )); 56const tail = argAt >= 0 ?rest . slice ( argAt + 1 ) :rest . filter (( x , i ) => i !== ( nameFlag >= 0 ?nameFlag + 1 :pathAt )); 57const folder = tail . find ( x => ! /^- / . test ( x ) && ! /^\/ ( select | e | root ) / i . test ( x )); 58return folder ?folderAsk ( folder ) :{ id :'open-explorer' , params :[], why :'opens-window' }; 59} 60if ( isExecutable ( target )) return { id :'run-program' , params :[ baseName ( target )], why :'run-program-why' , detail :clip ( unquote ( target ), 160 ) }; 61if ( target ) return folderAsk ( target ); 62} 63if ( verb === 'explorer' ) return rest . length ?folderAsk ( firstPath ( rest . filter ( x => ! /^\/ ( select | e | root ) / i . test ( x ))) || rest [ 0 ]) :{ id :'open-explorer' , params :[], why :'opens-window' }; 64if ( verb === 'invoke-item' || verb === 'ii' || verb === 'xdg-open' || verb === 'open' ) return folderAsk ( firstPath ( rest )); 65if ( verb === 'remove-item' || verb === 'rm' || verb === 'del' || verb === 'erase' || verb === 'rmdir' || verb === 'rd' ) { 66const target = firstPath ( rest . filter ( x => ! /^- ( recurse | force | r | rf | fr | f | path | literalpath ) $ / i . test ( x ))); 67return { id :'delete' , params :[ baseName ( target ) || 'these files' ], why :'delete-why' , detail :clip ( unquote ( target ), 160 ) }; 68} 69if ( verb === 'new-item' || verb === 'mkdir' || verb === 'md' || verb === 'ni' ) { 70const pathAt = rest . findIndex ( x => / ^-path$ / i . test ( x )); 71const target = pathAt >= 0 ?rest [ pathAt + 1 ] :firstPath ( rest ); 72const asFile = rest . some (( x , i ) => / ^- ( itemtype | type ) $ / i . test ( x ) && / file / i . test ( rest [ i + 1 ] ?? '' )) || ( verb === 'new-item' && hasExtension ( target )); 73return asFile ?{ id :'create-file' , params :[ baseName ( target )], why :'replaces-if-exists' , detail :clip ( unquote ( target ), 160 ) } :{ id :'create-folder' , params :[ baseName ( target ) || target ], detail :clip ( unquote ( target ), 160 ) }; 74} 75if ([ 'copy-item' , 'cp' , 'copy' ]. includes ( verb )) return { id :'copy' , params :[ baseName ( firstPath ( rest ))], detail :clip ( rest . filter ( x => ! /^- / . test ( x )). join ( ' → ' ), 160 ) }; 76if ([ 'move-item' , 'mv' , 'move' ]. includes ( verb )) return { id :'move' , params :[ baseName ( firstPath ( rest ))], detail :clip ( rest . filter ( x => ! /^- / . test ( x )). join ( ' → ' ), 160 ) }; 77if ([ 'rename-item' , 'ren' , 'rni' ]. includes ( verb )) return { id :'rename' , params :[ baseName ( firstPath ( rest ))], detail :clip ( rest . filter ( x => ! /^- / . test ( x )). join ( ' → ' ), 160 ) }; 78if ( / ( set-content | add-content | out-file | tee-object ) / i . test ( first ) || / > { 1 , 2 } \s * \S +/ . test ( first )) { 79const redirect = / > { 1 , 2 } \s * ( " [ ^ " ] + " | ' [ ^ ' ] + ' | \S + ) / . exec ( first )?.[ 1 ]; 80const all = words ( first ); 81const cmdAt = all . findIndex ( x => / ^ ( set-content | add-content | out-file | tee-object ) $ / i . test ( x )); 82const after = cmdAt >= 0 ?all . slice ( cmdAt + 1 ) :rest ; 83const pathAt = after . findIndex ( x => / ^- ( path | filepath | literalpath ) $ / i . test ( x )); 84const target = unquote ( redirect ?? ( pathAt >= 0 ?after [ pathAt + 1 ] :firstPath ( after ))); 85return { id :'write-file' , params :[ baseName ( target ) || 'a file' ], why :'replaces-if-exists' , detail :clip ( target , 160 ) }; 86} 87if ( verb === 'git' ) { 88const sub = ( rest [ 0 ] ?? '' ). toLowerCase (); 89if ([ 'commit' , 'push' , 'pull' , 'merge' , 'rebase' , 'reset' , 'checkout' , 'clone' , 'fetch' , 'stash' , 'switch' , 'tag' , 'branch' , 'add' , 'rm' , 'mv' , 'cherry-pick' , 'revert' ]. includes ( sub )) return { id :'git' , params :[ sub ], why :'changes-repo' }; 90} 91if ([ 'npm' , 'pnpm' , 'yarn' , 'pip' , 'pip3' , 'winget' , 'choco' , 'scoop' , 'apt' , 'apt-get' , 'brew' , 'cargo' , 'dotnet' , 'npx' ]. includes ( verb )) { 92const sub = ( rest [ 0 ] ?? '' ). toLowerCase (); 93if ( / ^ ( install | i | add | update | upgrade | remove | uninstall | restore | publish ) $ / . test ( sub )) return { id :'install' , params :[ verb ], why :'install-why' , detail :clip ( rest . slice ( 1 ). filter ( x => ! /^- / . test ( x )). join ( ' ' ), 120 ) }; 94} 95if ( isExecutable ( w [ 0 ] ?? '' ) || / ^ [ .& ] / . test ( first ) && isExecutable ( rest [ 0 ] ?? '' )) return { id :'run-program' , params :[ baseName ( isExecutable ( w [ 0 ]) ?w [ 0 ] :rest [ 0 ])], why :'run-program-why' }; 96if ([ 'python' , 'python3' , 'py' , 'node' , 'pwsh' , 'powershell' , 'bash' , 'sh' , 'cmd' ]. includes ( verb )) { 97const script = rest . find ( x => / \. ( py | js | mjs | ps1 | sh | bat | cmd ) $ / i . test ( x )); 98return script ?{ id :'run-script' , params :[ baseName ( script )], why :'run-program-why' } :{ id :'run-command' , params :[], why :'run-command-why' }; 99} 100return { id :'run-command' , params :[], why :'run-command-why' }; 101} 102 103/** @param { string } name tool name @param {object} args @param {string|null} [described] the model's own one-line description */ 104export function askFor ( name , args = {}, described = null ) { 105const path = args . file_path ?? args . path ?? args . filePath ?? '' ; 106switch ( name ) { 107case 'write' :return { id :'create-file' , params :[ baseName ( path ) || 'a file' ], why :'replaces-if-exists' , detail :clip ( path , 160 ) }; 108case 'edit' :return { id :'change-file' , params :[ baseName ( path ) || 'a file' ], detail :clip ( path , 160 ) }; 109case 'pwsh' :case 'bash' :{ 110const ask = shellAsk ( args . command ); 111// A command nothing above could read: the model's own one-line description is better than "Run a command" alone. 112if ( ask . id === 'run-command' && described ) return { ...ask , detail :clip ( described , 140 ) }; 113return ask ; 114} 115case 'job_kill' :return { id :'stop-task' , params :[] }; 116case 'web_fetch' :{ 117let host = String ( args . url ?? '' ); 118try { host = new URL ( host ). hostname ; } catch { /* keep the text */ } 119return { id :'open-page' , params :[ clip ( host , 80 )], detail :clip ( args . url , 160 ) }; 120} 121default : 122if ( String ( name ). startsWith ( 'mcp__' )) return { id :'use-tool' , params :[ String ( name ). split ( '__' ). slice ( 1 ). join ( ' / ' )], why :'use-tool-why' }; 123return { id :'use-other' , params :[ String ( name )] }; 124} 125}