okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
22a3d7068
main
1/** 2* A quiet NDJSON answer stays open (#4878). A chat turn can say nothing for minutes (the model reads a long prompt, a tool 3* runs, an approval waits), and the desktop's web view gives up on a request that has been silent for 60 seconds ("Load failed") 4* while the turn goes on in the background. A beat frame, the same `{"type":"beat"}` the Time-skip stream uses and the chat 5* consumer ignores, goes out every 15 seconds. It changes nothing else: a client that goes away still cancels (or detaches 6* from) its turn through the response's own close, exactly as before, and the timer ends on finish, on close and on stop(). 7*/ 8export const BEAT_MS = 15_000 ; 9const BEAT_LINE = ` ${ JSON . stringify ({ type : 'beat' })} \n` ; 10 11/** The interval (a tester may shorten it: STRATA_STREAM_BEAT_MS); anything that is not a sensible number is the default. */ 12export function beatInterval ( env = process . env ) { 13const n = Number ( env . STRATA_STREAM_BEAT_MS ); 14return Number . isFinite ( n ) && n >= 20 && n <= 60_000 ?n :BEAT_MS ; 15} 16 17/** Start beating on `res`. Returns stop(), which is safe to call more than once. */ 18export function keepStreamAlive ( res , { ms= beatInterval () } = {}) { 19let timer = setInterval (() => { 20if ( res . writableEnded || res . destroyed ) { stop (); return ; } 21try { res . write ( BEAT_LINE ); } catch { stop (); } 22}, ms ); 23timer . unref ?.(); 24function stop () { 25if ( timer === null ) return ; 26clearInterval ( timer ); 27timer = null ; 28res . off ?.( 'finish' , stop ); 29res . off ?.( 'close' , stop ); 30res . off ?.( 'error' , stop ); 31} 32res . once ( 'finish' , stop ); 33res . once ( 'close' , stop ); 34res . once ( 'error' , stop ); 35return stop ; 36}