okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fcb0c2d0e
main
1/** 2* What the person chose in Settings -> Tools, and the single place that validates it. 3* 4* Saved (inside the host's settings file): which tool groups are off, which web search service to use (and its key, which never leaves 5* this file: the screen only learns whether one is saved), whether the browser tool is on, and the person's own tools. 6* 7* The profile builder reads `profileView()`; the screen reads `view()`. Custom command and instruction tools are served by the app's 8* own small tool server (custom-tools-mcp.mjs) from a JSON file this module writes, so they appear to the assistant as ordinary tools: 9* `mcp__strata-instructions__<name>` (look-only: it just returns the person's instructions) and `mcp__strata-commands__<name>` 10* (runs the person's command; asks first in Manual and is refused in Plan). A person's own tool server (a web address or a program) 11* is added as `mcp__custom-<name>__...` and asks first in Manual like any connected tool. 12*/ 13import { mkdirSync , renameSync , writeFileSync } from 'node:fs' ; 14import { join } from 'node:path' ; 15import { GROUP_IDS , TOOL_GROUPS } from './tools-catalog.mjs' ; 16import { SHELLS , clampTimeoutSec } from './custom-command-run.mjs' ; 17 18export const SEARCH_PROVIDERS = [ 'builtin' , 'exa' , 'perplexity' , 'deepseek' ]; 19const KINDS = [ 'instructions' , 'command' , 'server' ]; 20const NAME = / ^ [ A - Za - z ][ A - Za - z0 - 9 _- ]{ 0 , 39 } $ / ; 21const MAX_CUSTOM = 40 ; 22 23export const INSTRUCTIONS_SERVER = 'strata-instructions' ; 24export const COMMANDS_SERVER = 'strata-commands' ; 25 26const slug = value => String ( value ). toLowerCase (). replace ( / [ ^ a - z0 - 9 ] +/ g , '_' ). replace ( / ^_ +| _ + $ / g , '' ). slice ( 0 , 40 ) || 'tool' ; 27const clean = ( value , max ) => String ( value ?? '' ). replace ( / \0 / g , '' ). slice ( 0 , max ); 28 29function normalize ( raw ) { 30const r = raw && typeof raw === 'object' ?raw :{}; 31const search = r . search && typeof r . search === 'object' ?r . search :{}; 32return { 33off :[ ...new Set (( Array . isArray ( r . off ) ?r . off :[]). filter ( id => GROUP_IDS . includes ( id )))], 34search :{ provider :SEARCH_PROVIDERS . includes ( search . provider ) ?search . provider :'builtin' , key :typeof search . key === 'string' ?search . key :'' }, 35browser :{ enabled :r . browser ?. enabled === true }, 36// Manual mode asks once per chat before the first web search or page. This switch (off unless the person turned it on) skips that question. 37webWithoutAsking :r . webWithoutAsking === true , 38custom :( Array . isArray ( r . custom ) ?r . custom :[]). filter ( t => t && NAME . test ( t . name ?? '' ) && KINDS . includes ( t . kind )). slice ( 0 , MAX_CUSTOM ). map ( t => ({ 39id :String ( t . id ), name :String ( t . name ), about :clean ( t . about , 300 ), kind :t . kind , enabled :t . enabled !== false , 40instructions :clean ( t . instructions , 20000 ), command :clean ( t . command , 2000 ), address :clean ( t . address , 2000 ), 41args :( Array . isArray ( t . args ) ?t . args :[]). slice ( 0 , 50 ). map ( a => clean ( a , 1000 )), 42// a command tool's folder, shell and time allowed (custom-command-run.mjs); '' / 'auto' / 120 when not set 43cwd :clean ( t . cwd , 1000 ). trim (), shell :SHELLS . includes ( t . shell ) ?t . shell :'auto' , timeoutSec :clampTimeoutSec ( t . timeoutSec ), 44})), 45}; 46} 47 48function validateCustom ( body , existing ) { 49const name = String ( body ?. name ?? '' ). trim (); 50if ( ! NAME . test ( name )) throw new Error ( 'Give the tool a short name: letters, numbers, spaces or hyphens.' ); 51const about = clean ( body ?. about , 300 ). trim (); 52if ( ! about ) throw new Error ( 'Say what the tool does, in a sentence. The assistant reads this to decide when to use it.' ); 53const kind = body ?. kind ; 54if ( ! KINDS . includes ( kind )) throw new Error ( 'Choose how the tool works.' ); 55const out = { name, about, kind, instructions :'' , command :'' , address :'' , args :[], cwd :'' , shell :'auto' , timeoutSec :clampTimeoutSec ( undefined ) }; 56if ( kind === 'instructions' ) { 57out . instructions = clean ( body . instructions , 20000 ). trim (); 58if ( ! out . instructions ) throw new Error ( 'Write the instructions the assistant should follow when it uses this tool.' ); 59} else if ( kind === 'command' ) { 60out . command = clean ( body . command , 2000 ). trim (); 61if ( ! out . command ) throw new Error ( 'Enter the command to run.' ); 62if ( / [ \r\n ] / . test ( out . command )) throw new Error ( 'Put the command on one line.' ); 63// The edit form of Settings does not send these three: an edit keeps what the tool had. 64const before = existing . find ( t => t . id === body . id && t . kind === 'command' ); 65out . cwd = body . cwd !== undefined ?clean ( body . cwd , 1000 ). trim () :before ?. cwd ?? '' ; 66if ( / [ \r\n ] / . test ( out . cwd )) throw new Error ( 'Put the folder on one line.' ); 67if ( body . shell !== undefined && ! SHELLS . includes ( body . shell )) throw new Error ( 'Choose how the command runs: automatic, cmd, PowerShell, bash or sh.' ); 68out . shell = body . shell !== undefined ?body . shell :before ?. shell ?? 'auto' ; 69if ( body . timeoutSec !== undefined && ! ( Number ( body . timeoutSec ) >= 1 && Number ( body . timeoutSec ) <= 3600 )) throw new Error ( 'Give the time allowed in seconds, from 1 to 3600.' ); 70out . timeoutSec = body . timeoutSec !== undefined ?clampTimeoutSec ( body . timeoutSec ) :before ?. timeoutSec ?? clampTimeoutSec ( undefined ); 71} else { 72out . address = clean ( body . address , 2000 ). trim (); 73if ( ! out . address ) throw new Error ( 'Enter the tool server’s web address, or the program that starts it.' ); 74if ( / [ \r\n ] / . test ( out . address )) throw new Error ( 'Put the address or program on one line.' ); 75if ( / ^https ? :\/\/ / i . test ( out . address )) { 76let url ; try { url = new URL ( out . address ); } catch { throw new Error ( 'That web address is not valid.' ); } 77if ( url . username || url . password ) throw new Error ( 'Use a web address without a user name or password in it.' ); 78} 79out . args = ( Array . isArray ( body . args ) ?body . args :[]). slice ( 0 , 50 ). map ( a => clean ( a , 1000 )). filter ( Boolean ); 80} 81if ( existing . some ( t => t . name . toLowerCase () === name . toLowerCase () && t . id !== body . id )) throw new Error ( 'You already have a tool with that name.' ); 82return out ; 83} 84 85const idFor = ( name , existing ) => { 86const base = slug ( name ); 87let id = base , n = 2 ; 88while ( existing . some ( t => t . id === id )) id = ` ${ base } _ ${ n ++ } ` ; 89return id ; 90}; 91 92/** 93* @param { {read: () => object|undefined, write: (value: object) => void, dataDir: string, browser?: () => {found: boolean, name: string|null, path?: string|null, cli?: string|null}} } deps 94*/ 95export function createAgentTools ({ read, write, dataDir, browser= () => ({ found :false , name :null }) }) { 96const state = () => normalize ( read ()); 97const filePath = join ( dataDir , 'agent' , 'custom-tools.json' ); 98 99/** The file the app's own tool server reads: instruction tools and command tools that are on. */ 100function writeToolFile ( s ) { 101const instructions = {}, commands = {}; 102for ( const t of s . custom . filter ( t => t . enabled )) { 103const key = t . id ; 104if ( t . kind === 'instructions' ) instructions [ key ] = { description :t . about , text :t . instructions }; 105else if ( t . kind === 'command' ) commands [ key ] = { description :t . about , command :t . command , cwd :t . cwd , shell :t . shell , timeoutSec :t . timeoutSec }; 106} 107mkdirSync ( join ( dataDir , 'agent' ), { recursive :true }); 108const tmp = ` ${ filePath } .tmp` ; 109writeFileSync ( tmp , JSON . stringify ({ instructions, commands}, null , 1 )); 110renameSync ( tmp , filePath ); 111return { instructions :Object . keys ( instructions ). length , commands :Object . keys ( commands ). length }; 112} 113 114const save = s => { write ( s ); writeToolFile ( s ); }; 115 116function view () { 117const s = state (); 118const probe = browser (); 119return { 120groups :TOOL_GROUPS . map ( g => ({ id :g . id , name :g . name , about :g . about , area :g . area , asks :g . asks , enabled :! s . off . includes ( g . id ) })), 121search :{ provider :s . search . provider , keySet :!! s . search . key }, 122webWithoutAsking :s . webWithoutAsking , 123// `found` means the browser tool can run: a browser on this computer AND the tool server in the agent component. 124browser :{ enabled :s . browser . enabled , found :!! ( probe . found && probe . cli ), name :probe . name ?? null , ...( probe . found && ! probe . cli ?{ missing :'component' } :{}) }, 125custom :s . custom . map ( t => ({ 126id :t . id , name :t . name , about :t . about , kind :t . kind , enabled :t . enabled , 127summary :t . kind === 'instructions' ?t . instructions . slice ( 0 , 80 ) :t . kind === 'command' ?t . command :t . address , 128instructions :t . instructions , command :t . command , address :t . address , args :t . args , 129 ...( t . kind === 'command' ?{ cwd :t . cwd , shell :t . shell , timeoutSec :t . timeoutSec } :{}), 130})), 131}; 132} 133 134return { 135 view, 136 filePath, 137/** For the profile builder: plain values, secrets included. Never sent to the screen. */ 138profileView () { 139const s = state (); 140const custom = s . custom . filter ( t => t . enabled ); 141const servers = {}; 142for ( const t of custom . filter ( t => t . kind === 'server' )) { 143const name = `custom- ${ t . id . replace ( / _ / g , "-" )} ` . slice ( 0 , 32 ); 144servers [ name ] = / ^https ? :\/\/ / i . test ( t . address ) ?{ url :t . address } :{ command :t . address , args :t . args }; 145} 146return { 147off :new Set ( s . off ), 148search :s . search , 149webWithoutAsking :s . webWithoutAsking , 150browserEnabled :s . browser . enabled && !! ( browser (). found && browser (). cli ), 151browserPath :browser (). path ?? null , 152browserCli :browser (). cli ?? null , 153 servers, 154hasInstructionTools :custom . some ( t => t . kind === 'instructions' ), 155hasCommandTools :custom . some ( t => t . kind === 'command' ), 156toolFile :filePath , 157}; 158}, 159update ( change ) { 160const s = state (); 161if ( change ?. groups ) { 162for ( const [ id , on ] of Object . entries ( change . groups )) { 163if ( ! GROUP_IDS . includes ( id )) throw new Error ( 'That tool is not known.' ); 164s . off = on === false ?[ ...new Set ([ ...s . off , id ])] :s . off . filter ( x => x !== id ); 165} 166} 167if ( change ?. search ) { 168const provider = change . search . provider ; 169if ( provider !== undefined ) { 170if ( ! SEARCH_PROVIDERS . includes ( provider )) throw new Error ( 'Choose one of the listed search services.' ); 171if ( provider !== s . search . provider ) s . search = { provider, key :'' }; // a key belongs to its service 172} 173if ( change . search . key !== undefined ) { 174const key = String ( change . search . key ). trim (); 175if ( ! key || key . length > 500 || / [ \s\0 ] / . test ( key )) throw new Error ( 'That key does not look right. Paste it again without spaces.' ); 176s . search = { ...s . search , key}; 177} 178} 179if ( typeof change ?. webWithoutAsking === 'boolean' ) s . webWithoutAsking = change . webWithoutAsking ; 180if ( change ?. browser && typeof change . browser . enabled === 'boolean' ) s . browser = { enabled :change . browser . enabled }; 181if ( change ?. custom ) { 182for ( const [ id , on ] of Object . entries ( change . custom )) { 183const t = s . custom . find ( x => x . id === id ); 184if ( ! t ) throw new Error ( 'That tool is not in your list.' ); 185t . enabled = on !== false ; 186} 187} 188save ( s ); 189return view (); 190}, 191addCustom ( body ) { 192const s = state (); 193if ( s . custom . length >= MAX_CUSTOM ) throw new Error ( `You can have up to ${ MAX_CUSTOM } tools of your own.` ); 194const t = validateCustom ( body , s . custom ); 195s . custom . push ({ id :idFor ( t . name , s . custom ), enabled :true , ...t }); 196save ( s ); 197return view (); 198}, 199editCustom ( id , body ) { 200const s = state (); 201const at = s . custom . findIndex ( t => t . id === id ); 202if ( at < 0 ) throw new Error ( 'That tool is not in your list.' ); 203const t = validateCustom ({ ...body , id}, s . custom ); 204s . custom [ at ] = { ...s . custom [ at ], ...t }; 205save ( s ); 206return view (); 207}, 208removeCustom ( id ) { 209const s = state (); 210if ( ! s . custom . some ( t => t . id === id )) throw new Error ( 'That tool is not in your list.' ); 211s . custom = s . custom . filter ( t => t . id !== id ); 212save ( s ); 213return view (); 214}, 215/** Make sure the tool server's file exists (host start). */ 216ensureFile () { writeToolFile ( state ()); }, 217}; 218}