okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
19ea30f6a
main
1/** 2* Skills on disk, for the app and for the agent. 3* 4* The app keeps the skills a person writes in its own folder (`<data>/skills/<id>/SKILL.md`). The agent (the Harness) finds skills by scanning 5* folders of `<name>/SKILL.md` files whose frontmatter has a kebab-case `name` and a `description`. So the app also writes the whole current 6* list (bundled, written, added, edited, switched on or off) into one folder the Harness is told to scan (`materializeSkills`), every time 7* anything changes. The Harness watches that folder, so a skill saved in the app is there for the next message without a restart. 8*/ 9import { existsSync , mkdirSync , readFileSync , readdirSync , rmSync , writeFileSync } from 'node:fs' ; 10import { join } from 'node:path' ; 11 12/** The kebab-case name the Harness requires, from what a person typed ("Directory opener" -> "directory-opener"). */ 13export function skillSlug ( name ) { 14return String ( name ?? '' ). toLowerCase (). normalize ( 'NFKD' ). replace ( / [ ̀ - ͯ ] / g , '' ). replace ( / [ ^ a - z0 - 9 ] +/ g , '-' ). replace ( / ^- +| - + $ / g , '' ). slice ( 0 , 60 ); 15} 16 17const yamlText = value => JSON . stringify ( String ( value ?? '' ). replace ( / \s +/ g , ' ' ). trim ()); 18 19/** `SKILL.md` text: the frontmatter the Harness reads, then the instructions. */ 20export function skillFileText ({ name, title= null , description, body, hidden= false }) { 21return `---\nname: ${ skillSlug ( name )} \n ${ title ? `title: ${ yamlText ( title )} \n` : '' } description: ${ yamlText ( description || name )} \n ${ hidden ? 'disable-model-invocation: true\n' : '' } ---\n\n ${ String ( body ?? '' ). trim ()} \n` ; 22} 23 24/** Name, description and instructions out of a SKILL.md (frontmatter optional). */ 25export function parseSkillFile ( text ) { 26const raw = String ( text ?? '' ). replace ( / ^ / , '' ). replace ( / \r\n / g , '\n' ); 27const m = / ^---\n ([ \s\S ] * ? ) \n---\n ?/ . exec ( raw ); 28if ( ! m ) return { name :null , title :null , description :null , body :raw . trim () }; 29const field = key => { 30const line = new RegExp ( `^ ${ key } :\\s*(.*)$` , 'mi' ). exec ( m [ 1 ])?.[ 1 ]?. trim (); 31if ( line === undefined ) return null ; 32try { if ( / ^ [ "' ] / . test ( line )) return typeof JSON . parse ( line . replace ( / ^' ( . * ) '$ / , '"$1"' )) === 'string' ?JSON . parse ( line . replace ( / ^' ( . * ) '$ / , '"$1"' )) :line ; } catch { /* plain scalar */ } 33return line ; 34}; 35return { name :field ( 'name' ), title :field ( 'title' ), description :field ( 'description' ), body :raw . slice ( m [ 0 ]. length ). trim () }; 36} 37 38/** Where a skill written in the app lives, and writing it there. */ 39export function writeCreatedSkill ( root , { name, description, body}) { 40const id = skillSlug ( name ); 41if ( ! id ) throw new Error ( 'Give the skill a name with letters or numbers in it.' ); 42const dir = join ( root , id ); 43if ( existsSync ( dir )) throw new Error ( `You already have a skill called " ${ id } ". Choose another name, or edit the one you have.` ); 44mkdirSync ( dir , { recursive :true }); 45writeFileSync ( join ( dir , 'SKILL.md' ), skillFileText ({ name :id , title :String ( name ). trim (), description, body})); 46return id ; 47} 48 49/** 50* Make `root` hold exactly the skills the agent may use: one folder each, a SKILL.md with frontmatter. A skill switched off is written 51* with `disable-model-invocation` (the agent does not offer it); folders of skills that are gone are removed. Only this folder is touched. 52* @param { string } root 53* @param { Array<{id: string, name: string, description?: string, body?: string, enabled?: boolean}> } skills 54*/ 55export function materializeSkills ( root , skills ) { 56mkdirSync ( root , { recursive :true }); 57const keep = new Set (); 58for ( const s of skills ) { 59const slug = skillSlug ( s . id . replace ( / ^ (?: ws | my ) - / , '' ) || s . name ); 60if ( ! slug || ! s . body ?. trim ()) continue ; 61let unique = slug , n = 2 ; 62while ( keep . has ( unique )) unique = ` ${ slug } - ${ n ++ } ` ; 63keep . add ( unique ); 64const dir = join ( root , unique ); 65mkdirSync ( dir , { recursive :true }); 66const text = skillFileText ({ name :unique , description :` ${ s . name } . ${ s . description ?? '' } ` . trim (), body :s . body , hidden :s . enabled === false }); 67const file = join ( dir , 'SKILL.md' ); 68// Only a changed skill is rewritten: the agent watches this folder and treats every write as a change to its list. 69let same = false ; try { same = readFileSync ( file , 'utf8' ) === text ; } catch { /* not there yet */ } 70if ( ! same ) writeFileSync ( file , text ); 71} 72for ( const entry of readdirSync ( root , { withFileTypes :true })) if ( entry . isDirectory () && ! keep . has ( entry . name )) rmSync ( join ( root , entry . name ), { recursive :true , force :true }); 73return [ ...keep ]; 74}