char-slop/berry
multipurpose self-modifiable agentic harness
git clone https://git.t4t.associates/char-slop/berry
230f21e
main
1let calls = 0 ; 2let cancelled = 0 ; 3let roots :unknown ; 4const output = Deno . stdout . writable . getWriter (); 5const encoder = new TextEncoder (); 6const send = async ( value :unknown ) => { 7const bytes = encoder . encode ( JSON . stringify ( value ) + "\n" ); 8const split = Math . floor ( bytes . length / 2 ); 9await output . write ( bytes . slice ( 0 , split )); 10await output . write ( bytes . slice ( split )); 11}; 12if ( Deno . args . includes ( "stubborn" )) Deno . addSignalListener ( "SIGTERM" , () => {}); 13if ( Deno . args . some (( arg ) => [ "descendant" , "escaped" , "quiet-child" ]. includes ( arg ))) { 14const escaped = Deno . args . includes ( "escaped" ); 15const quiet = Deno . args . includes ( "quiet-child" ); 16const child = new Deno . Command ( escaped ?"setsid" :Deno . execPath (), { 17args :[ 18 ...( escaped ?[ Deno . execPath ()] :[]), 19"eval" , 20"Deno.addSignalListener('SIGTERM', () => {}); Deno.writeTextFileSync('descendant-ready', ''); setInterval(() => {}, 60000)" , 21], 22stdin :"null" , 23stdout :escaped || quiet ?"null" :"inherit" , 24stderr :quiet ?"null" :"inherit" , 25}). spawn (); 26await Deno . writeTextFile ( "descendant" , String ( child . pid )); 27while ( ! ( await Deno . stat ( "descendant-ready" ). then (() => true , () => false ))) { 28await new Promise (( resolve ) => setTimeout ( resolve , 5 )); 29} 30} 31let buffered = "" ; 32for await ( const chunk of Deno . stdin . readable ) { 33buffered += new TextDecoder (). decode ( chunk ); 34let newline :number ; 35while (( newline = buffered . indexOf ( "\n" )) >= 0 ) { 36const line = buffered . slice ( 0 , newline ); 37buffered = buffered . slice ( newline + 1 ); 38const message = JSON . parse ( line ); 39if ( message . id === "roots" ) { 40roots = message . result . roots ; 41continue ; 42} 43if ( message . method === "notifications/initialized" ) { 44await send ({ jsonrpc :"2.0" , id :"roots" , method :"roots/list" }); 45continue ; 46} 47if ( message . method === "notifications/cancelled" ) { 48cancelled ++ ; 49continue ; 50} 51if ( message . id === undefined ) continue ; 52let result :unknown ; 53if ( message . method === "initialize" ) { 54result = { 55protocolVersion :message . params . protocolVersion , 56capabilities :{ tools :{} }, 57serverInfo :{ name :"fixture" , version :"1" }, 58instructions :"Use echo to inspect the environment." , 59}; 60} else if ( message . method === "tools/list" ) { 61result = { 62tools :[{ 63name :"echo" , 64description :"Echo arguments: café, 猫" , 65inputSchema :{ type :"object" , properties :{ fail :{ type :"boolean" } } }, 66outputSchema :{ type :"object" , properties :{ count :{ type :"integer" } } }, 67annotations :{ readOnlyHint :true }, 68}], 69}; 70} else if ( message . method === "tools/call" ) { 71calls ++ ; 72if ( message . params . name === "crash" ) Deno . exit ( 7 ); 73if ( message . params . name === "wait" ) { 74await Deno . writeTextFile ( "waiting" , "" ); 75continue ; 76} 77if ( message . params . _meta ?. progressToken !== undefined ) { 78await send ({ 79jsonrpc :"2.0" , 80method :"notifications/progress" , 81params :{ progressToken :message . params . _meta . progressToken , progress :1 , total :1 }, 82}); 83} 84result = { 85content :[{ type :"text" , text :"café, 猫" }], 86structuredContent :{ 87count :calls , 88 cancelled, 89pid :Deno . pid , 90cwd :Deno . cwd (), 91token :Deno . env . get ( "TOKEN" ) ?? "" , 92 roots, 93arguments :message . params . arguments , 94}, 95isError :message . params . arguments . fail === true , 96}; 97} else result = {}; 98await send ({ jsonrpc :"2.0" , id :message . id , result}); 99} 100} 101if ( Deno . args . includes ( "stubborn" )) setInterval (() => {}, 60_000 );