okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f9bfe8bc1
main
1/** 2* Drive the host over HTTP (no browser) against a real model and print what the page would receive. 3* 4* STRATA_URL=http://127.0.0.1:8899 node scripts/agent3-http.mjs <command> [args] 5* 6* connect <baseUrl> <modelId> save an API connection to a model server (once per STRATA_HOME) 7* stream "<message>" [mode] one chat turn; prints every frame compactly with its time (status, goal, tools, deltas counted) 8* goal "<objective>" /goal run, then the goal bar calls: state, pause, edit, clear, resume 9* timeskip "<request>" [stopAfterS] a Time-skip job; polls status (phase + activity); with stopAfterS, presses Stop that many seconds in 10* 11* Environment: STRATA_URL (required), CHAT (chat id, default a fresh one), WORKSPACE_ID. 12*/ 13const BASE = process . env . STRATA_URL ; 14if ( ! BASE ) { console . error ( 'STRATA_URL is required' ); process . exit ( 2 ); } 15const [ command , ...rest ] = process . argv . slice ( 2 ); 16const t0 = Date . now (); 17const stamp = () => ` ${ String ( Math . round (( Date . now () - t0 ) / 1000 )). padStart ( 4 )} s` ; 18const j = async ( path , body , method = body ?'POST' :'GET' ) => { 19const res = await fetch ( BASE + path , { method, headers :{ 'content-type' :'application/json' }, ...( body ?{ body :JSON . stringify ( body ) } :{}) }); 20const text = await res . text (); 21let data ; try { data = JSON . parse ( text ); } catch { data = { raw :text }; } 22if ( ! res . ok ) throw new Error ( ` ${ path } -> ${ res . status } ${ data . error ?? text . slice ( 0 , 200 )} ` ); 23return data ; 24}; 25const sleep = ms => new Promise ( r => setTimeout ( r , ms )); 26 27async function model () { 28const providers = await j ( '/api/model-providers' ); 29const c = providers . connections . at ( - 1 ); 30if ( ! c ) throw new Error ( 'no API connection saved: run "connect" first' ); 31return { connectionId :c . id , modelId :c . models [ 0 ]. id }; 32} 33 34/** One chat stream; returns the frames. `print` shows a compact line per frame, deltas and reasoning as counters. */ 35async function stream ( body , { quiet= false } = {}) { 36const res = await fetch ( BASE + '/api/chat/stream' , { method :'POST' , headers :{ 'content-type' :'application/json' }, body :JSON . stringify ( body ) }); 37if ( ! res . ok ) throw new Error ( `stream -> ${ res . status } ${ await res . text ()} ` ); 38const frames = []; let buffer = '' , text = 0 , reasoning = 0 , lastLine = '' ; 39const flushCounters = () => { if ( text || reasoning ) { console . log ( ` ${ stamp ()} ... ${ reasoning } reasoning chars, ${ text } answer chars` ); text = 0 ; reasoning = 0 ; } }; 40const reader = res . body . getReader (); const dec = new TextDecoder (); 41for (;;) { 42const { value, done} = await reader . read (); 43if ( value ) { 44buffer += dec . decode ( value , { stream :true }); 45const lines = buffer . split ( '\n' ); buffer = lines . pop () ?? '' ; 46for ( const line of lines . filter ( Boolean )) { 47const f = JSON . parse ( line ); f . _at = Date . now (); frames . push ( f ); 48if ( f . type === 'delta' ) { text += ( f . text ?? '' ). length ; continue ; } 49if ( f . type === 'reasoning' ) { reasoning += ( f . text ?? '' ). length ; continue ; } 50if ( f . type === 'stats' ) continue ; 51flushCounters (); 52let out = f . type ; 53if ( f . type === 'status' ) out = `status[ ${ f . seq } ] ${ f . phase } / ${ f . code } : ${ f . text } ${ f . tokens !== undefined ? ` (tokens ${ f . tokens } )` : '' } + ${ f . elapsedMs } ms` ; 54else if ( f . type === 'goal' ) out = `goal ${ JSON . stringify ( f . goal )} ` ; 55else if ( f . type === 'agent-tool' ) out = `tool ${ f . tool . state } ${ f . tool . verb } ${ f . tool . target ?? '' } ` ; 56else if ( f . type === 'done' ) out = `done ${ f . finishReason } tokensOut= ${ f . tokensOut } ` ; 57else if ( f . type === 'error' ) out = `ERROR ${ f . message } ` ; 58if ( ! quiet && out !== lastLine ) console . log ( ` ${ stamp ()} ${ out } ` ); 59lastLine = out ; 60} 61} 62if ( done ) break ; 63} 64flushCounters (); 65return frames ; 66} 67 68if ( command === 'connect' ) { 69const [ baseUrl , modelId ] = rest ; 70await j ( '/api/model-providers' , { kind :'custom' , name :'Cloud model' , baseUrl, protocol :'openai-chat' , apiKey :'' , models :[{ id :modelId , name :'Test model' }] }); 71console . log ( 'saved' ); 72} else if ( command === 'stream' ) { 73const [ message , mode = 'bypass' ] = rest ; 74const chat = process . env . CHAT ?? `c- ${ Date . now ()} ` ; 75await stream ({ sessionId :chat , message, mode, remoteModel :await model (), workspaceId :process . env . WORKSPACE_ID , ...( process . env . PLAIN ?{ agent :false } :{}), ...( process . env . THINK ?{ thinking :process . env . THINK } :{}) }); 76} else if ( command === 'goal' ) { 77const [ objective ] = rest ; 78const chat = process . env . CHAT ?? `g- ${ Date . now ()} ` ; 79const common = { sessionId :chat , mode :'bypass' , remoteModel :await model () }; 80console . log ( ` ${ stamp ()} -- /goal run` ); 81const frames = await stream ({ ...common , message :`/goal ${ objective } ` }); 82const last = frames . filter ( f => f . type === 'goal' ). at ( - 1 ); 83console . log ( ` ${ stamp ()} -- goal bar: state` , JSON . stringify (( await j ( `/api/goal?chatId= ${ chat } ` )). goal )); 84console . log ( ` ${ stamp ()} -- pause` , JSON . stringify ( await j ( '/api/goal/pause' , { chatId :chat }))); 85console . log ( ` ${ stamp ()} -- edit` , JSON . stringify ( await j ( '/api/goal/edit' , { chatId :chat , text :` ${ objective } (edited)` }))); 86console . log ( ` ${ stamp ()} -- resume (stream)` ); 87await stream ({ ...common , goalAction :'resume' }); 88console . log ( ` ${ stamp ()} -- clear` , JSON . stringify ( await j ( '/api/goal/clear' , { chatId :chat }))); 89console . log ( ` ${ stamp ()} -- state after clear` , JSON . stringify ( await j ( `/api/goal?chatId= ${ chat } ` ))); 90void last ; 91} else if ( command === 'goalbar' ) { 92// Pause from the goal bar while a round runs, then resume through the chat stream. 93const [ objective , pauseAfter = '6' ] = rest ; 94const chat = process . env . CHAT ?? `gb- ${ Date . now ()} ` ; 95const common = { sessionId :chat , mode :'bypass' , remoteModel :await model () }; 96const running = stream ({ ...common , message :`/goal ${ objective } ` }); 97await sleep ( Number ( pauseAfter ) * 1000 ); 98console . log ( ` ${ stamp ()} -- pause from the bar` , JSON . stringify ( await j ( '/api/goal/pause' , { chatId :chat }))); 99await running ; 100console . log ( ` ${ stamp ()} -- state after the run` , JSON . stringify (( await j ( `/api/goal?chatId= ${ chat } ` )). goal )); 101console . log ( ` ${ stamp ()} -- resume (stream, no message)` ); 102await stream ({ ...common , goalAction :'resume' }); 103console . log ( ` ${ stamp ()} -- state after resume` , JSON . stringify (( await j ( `/api/goal?chatId= ${ chat } ` )). goal )); 104console . log ( ` ${ stamp ()} -- remove` , JSON . stringify ( await j ( '/api/goal/clear' , { chatId :chat }))); 105} else if ( command === 'timeskip' ) { 106const [ request , stopAfter ] = rest ; 107const chat = process . env . CHAT ?? `t- ${ Date . now ()} ` ; 108const started = await j ( '/api/timeskip/start' , { prompt :request , sessionId :chat , attempts :1 , workspaceId :process . env . WORKSPACE_ID , remoteModel :await model () }); 109const id = started . run . id ; 110console . log ( ` ${ stamp ()} started ${ id } activity= ${ JSON . stringify ( started . run . activity )} ` ); 111let last = '' , stopped = false ; 112for (;;) { 113const { run} = await j ( `/api/timeskip/status?id= ${ id } ` ); 114const line = ` ${ run . phase } ${ run . activity ? ` | ${ run . activity . text } ${ run . activity . args ?. step ? ` | assistant: ${ run . activity . args . step } ` : '' } ` : '' } ` ; 115if ( line !== last ) { console . log ( ` ${ stamp ()} ${ line } ` ); last = line ; } 116if ( stopAfter && ! stopped && ( Date . now () - t0 ) / 1000 >= Number ( stopAfter )) { 117console . log ( ` ${ stamp ()} -- pressing Stop (the chat Stop route)` ); 118console . log ( ` ${ stamp ()} ` , JSON . stringify ( await j ( '/api/chat/stop' , { sessionId :chat }))); 119stopped = true ; 120} 121if ([ 'completed' , 'cancelled' , 'failed' , 'interrupted' ]. includes ( run . phase )) { 122console . log ( ` ${ stamp ()} END ${ run . phase } ${ run . error ? ` error= ${ run . error } ` : '' } needsReview= ${ run . needsUserReview } ` ); 123try { const files = await j ( `/api/timeskip/files?id= ${ id } ` ); console . log ( ` ${ stamp ()} files: ${ files . files . map ( f => ` ${ f . status } ${ f . path } ` ). join ( ', ' ) || '(none)' } ` ); } catch ( e ) { console . log ( ` ${ stamp ()} files: ${ e . message } ` ); } 124break ; 125} 126await sleep ( 4000 ); 127} 128} else { 129console . error ( 'commands: connect | stream | goal | timeskip' ); process . exit ( 2 ); 130}