okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
19ea30f6a
main
1/** 2* Skills, end to end: a skill written in the app is saved as a folder, written out for the agent, listed in the agent's skill catalog, 3* loadable by name through the `skill` tool, and a skill added or switched off later reaches the NEXT message without a restart. 4*/ 5import test from 'node:test' ; 6import assert from 'node:assert/strict' ; 7import { existsSync , mkdtempSync , mkdirSync , readFileSync , rmSync } from 'node:fs' ; 8import { tmpdir } from 'node:os' ; 9import { join } from 'node:path' ; 10import { startScriptedWorker } from './helpers/scripted-worker.mjs' ; 11import { createAgentBridge } from '../server/agent/agent-bridge.mjs' ; 12import { resolveHarness } from '../server/agent/harness-runtime.mjs' ; 13import { buildProfile } from '../server/agent/profile.mjs' ; 14import { materializeSkills , parseSkillFile , skillFileText , skillSlug , writeCreatedSkill } from '../server/skill-files.mjs' ; 15 16const skip = ! process . env . STRATA_HARNESS_DIR || ! resolveHarness ({ env :process . env }). ok ?'STRATA_HARNESS_DIR is not set to a built Harness' :false ; 17const cleanup = dir => { try { rmSync ( dir , { recursive :true , force :true , maxRetries :3 }); } catch { /* best effort */ } }; 18const INSTRUCTIONS = 'Open the Downloads folder, then open the Documents folder, using the command line. Say which folders you opened.' ; 19 20test ( 'a skill name becomes the kebab-case name the agent requires, and its file carries what the agent reads' , () => { 21assert . equal ( skillSlug ( 'Directory opener' ), 'directory-opener' ); 22assert . equal ( skillSlug ( ' Écrire des notes!! ' ), 'ecrire-des-notes' ); 23assert . equal ( skillSlug ( '???' ), '' ); 24const text = skillFileText ({ name :'directory-opener' , title :'Directory opener' , description :'Opens "folders"\nfor you' , body :INSTRUCTIONS }); 25const parsed = parseSkillFile ( text ); 26assert . deepEqual ([ parsed . name , parsed . title , parsed . description , parsed . body ], [ 'directory-opener' , 'Directory opener' , 'Opens "folders" for you' , INSTRUCTIONS ]); 27assert . match ( skillFileText ({ name :'x' , description :'d' , body :'b' , hidden :true }), / disable-model-invocation: true / ); 28assert . deepEqual ( parseSkillFile ( 'just text' ), { name :null , title :null , description :null , body :'just text' }); 29}); 30 31test ( 'writing a skill refuses a duplicate and an empty name, and materializing makes the agent folder match the list exactly' , () => { 32const dir = mkdtempSync ( join ( tmpdir (), 'strata-skill-files-' )); 33try { 34const mine = join ( dir , 'mine' ); 35assert . equal ( writeCreatedSkill ( mine , { name :'Directory opener' , description :'Opens folders' , body :INSTRUCTIONS }), 'directory-opener' ); 36assert . throws (() => writeCreatedSkill ( mine , { name :'directory opener' , body :'x' }), / already have a skill / ); 37assert . throws (() => writeCreatedSkill ( mine , { name :'!!!' , body :'x' }), / name with letters / ); 38const live = join ( dir , 'live' ); 39const list = [{ id :'my-directory-opener' , name :'Directory opener' , description :'Opens folders' , body :INSTRUCTIONS , enabled :true }, 40{ id :'commit-messages' , name :'Commit messages' , description :'Writes commits' , body :'Write them well.' , enabled :false }, 41{ id :'ws-empty' , name :'Empty' , body :'' , enabled :true }]; 42assert . deepEqual ( materializeSkills ( live , list ). sort (), [ 'commit-messages' , 'directory-opener' ]); 43assert . match ( readFileSync ( join ( live , 'commit-messages' , 'SKILL.md' ), 'utf8' ), / disable-model-invocation: true / ); 44assert . ok ( ! existsSync ( join ( live , 'empty' )), 'a skill with no instructions is not offered' ); 45mkdirSync ( join ( live , 'old-one' )); 46materializeSkills ( live , list . slice ( 0 , 1 )); 47assert . ok ( ! existsSync ( join ( live , 'old-one' )) && ! existsSync ( join ( live , 'commit-messages' )), 'removed skills disappear from the agent folder' ); 48} finally { cleanup ( dir ); } 49}); 50 51test ( 'the agent profile tells the skills plugin to scan the app skill folder' , () => { 52const dir = '/data/agent/skills-live' ; 53const entries = JSON . parse ( buildProfile ({ local :[], connections :[], skillsDir :dir }). patch . split ( '\n' ). slice ( 1 ). join ( '\n' )); 54const plugins = entries . find ( e => e . id === 'preset-standard' ). config . plugins ; 55assert . deepEqual ( plugins . find ( p => p . id === 'skill-filesystem' ). config , { customSkillDirs :[ dir ] }); 56assert . ok ( plugins . some ( p => p . id === 'tool-skill' ), 'the skill loader tool is in the preset' ); 57}); 58 59test ( 'real Harness: a skill saved in the app is in the catalog, loads by name, and one added later is seen without a restart' , { skip}, async ( t ) => { 60const worker = await startScriptedWorker ({}); 61const dir = mkdtempSync ( join ( tmpdir (), 'strata-skills-' )); 62const ws = join ( dir , 'ws' ); mkdirSync ( ws , { recursive :true }); 63const live = join ( dir , 'skills-live' ); 64const list = [{ id :'my-directory-opener' , name :'Directory opener' , description :'Opens the Downloads and Documents folders' , body :INSTRUCTIONS , enabled :true }]; 65materializeSkills ( live , list ); 66const local = [{ id :'inst-1' , label :'Scripted' , baseUrl :worker . url , modelId :'scripted-model' , context :32768 , slots :2 }]; 67const bridge = createAgentBridge ({ dataDir :dir , localInstances :() => local , connections :() => [], skillsDir :() => live , env :process . env , startTimeoutMs :90_000 }); 68t . after ( async () => { await bridge . close (); await worker . close (); cleanup ( dir ); }); 69const run = async ( chatId , message ) => { 70const frames = []; 71await bridge . runTurn ({ chatId, message, workspacePath :ws , mode :'bypass' , onFrame :f => frames . push ( f ) }); 72return frames . filter ( f => f . type === 'agent-tool' ). map ( f => f . tool ); 73}; 74await run ( 's1' , 'SCRIPT:say hello' ); 75const first = worker . requests . at ( - 1 ); 76assert . ok ( first . toolNames . includes ( 'skill' ), 'the agent has the skill loader' ); 77assert . match ( first . raw , / directory-opener / , 'the skill is in what the model is told' ); 78const pid = bridge . status (). runtime . pid ; 79const cards = await run ( 's1' , `SCRIPT:call skill ${ JSON . stringify ({ name : 'directory-opener' })} ` ); 80assert . equal ( cards . at ( - 1 ). state , 'done' ); 81assert . equal ( cards . at ( - 1 ). verb , 'Loaded your skill' , 'the card says it in the person own words' ); 82assert . match ( cards . at ( - 1 ). result , / Open the Downloads folder, then open the Documents folder / , 'loading it returns the instructions the person wrote' ); 83// saved later, while the agent runs 84list . push ({ id :'my-release-notes' , name :'Release notes' , description :'Writes release notes' , body :'Start with one line.' , enabled :true }); 85materializeSkills ( live , list ); 86let seen = false ; 87for ( let i = 0 ; i < 20 && ! seen ; i ++ ) { await new Promise ( r => setTimeout ( r , 500 )); await run ( 's2' , 'SCRIPT:say hi' ); seen = / release-notes / . test ( worker . requests . at ( - 1 ). raw ); } 88assert . ok ( seen , 'a skill saved while the agent runs reaches a later message' ); 89assert . equal ( bridge . status (). runtime . pid , pid , 'without restarting the agent' ); 90});