char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
a4e772a
main
1import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context" ; 2import { getOrThrow } from "@earendil-works/pi-durable/env" ; 3import type { Environment } from "@berry/api" ; 4import { localEnvironment } from "@berry/api/environment.ts" ; 5import { rootedEnvironment } from "@berry/api/testing.ts" ; 6 7export function targetEnvironment ( 8root :string , 9options :Parameters < typeof rootedEnvironment >[ 1 ] = {}, 10) :Environment { 11const env = rootedEnvironment ( root , options ); 12const exec = env . exec ; 13env . exec = ( command , options , context ) => 14exec ( 15typeof command === "string" 16 ?command 17 :command . map (( arg ) => arg . startsWith ( "/" ) ?root + arg :arg ), 18options , 19context , 20); 21return env ; 22} 23 24export function hostEnvironment ( home :string , variables :Record < string , string > = {}) :Environment { 25const env = localEnvironment ( home ); 26Object . defineProperties ( env , { home :{ value :home }, variables :{ value :variables } }); 27return env ; 28} 29 30export async function activeDirectory ( env :Environment , prompt :string ) :Promise < string > { 31for ( const match of prompt . matchAll ( / "(?:[^"\\]|\\.)*" / g )) { 32const directory :string = JSON . parse ( match [ 0 ]); 33const file = getOrThrow ( await env . joinPath ([ directory , "SKILL.md" ], BACKGROUND_CONTEXT )); 34const info = await env . fileInfo ( file , BACKGROUND_CONTEXT ); 35if ( info . ok && info . value . kind === "file" ) return directory ; 36} 37throw new Error ( "The prompt has no readable skill directory" ); 38} 39 40export async function writeSkill ( 41env :Environment , 42collection :string , 43name :string , 44body :string , 45description = `Use ${ name } for its workflow.` , 46) :Promise < string > { 47const path = getOrThrow ( await env . joinPath ([ collection , name , "SKILL.md" ], BACKGROUND_CONTEXT )); 48getOrThrow ( 49await env . writeFile ( 50path , 51`---\nname: ${ name } \ndescription: ${ JSON . stringify ( description )} \n---\n ${ body } ` , 52BACKGROUND_CONTEXT , 53), 54); 55return path ; 56}