okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3d9c21509
main
1import { useEffect , useSyncExternalStore } from 'react' ; 2import { api } from './client' ; 3 4/** A chat's goal as the host reports it (notes-split-agent3.md): the bar above the input draws it; the host owns it. */ 5export interface Goal { 6objective :string ; 7phase :'active' | 'paused' | 'blocked' | 'complete' ; 8round :number ; 9maxRounds :number ; 10blocked ?:string ; 11/** While a plan runs: which step the goal is on (notes-split-agent6.md). */ 12step ?:{ index :number ; total :number ; text :string }; 13} 14 15const goals = new Map < string , Goal >(); 16const listeners = new Set <() => void >(); 17const emit = () => listeners . forEach ( fn => fn ()); 18 19/** A goal object from a frame, an event or a route answer; anything that is not one means "no goal". */ 20export function readGoal ( raw :unknown ) :Goal | null { 21if ( ! raw || typeof raw !== 'object' ) return null ; 22const g = raw as Record < string , unknown >; 23const phase = g . phase ; 24if ( typeof g . objective !== 'string' || ! g . objective || ! [ 'active' , 'paused' , 'blocked' , 'complete' ]. includes ( String ( phase ))) return null ; 25return { 26objective :g . objective , phase :phase as Goal [ 'phase' ], 27round :Number ( g . round ) || 0 , maxRounds :Number ( g . maxRounds ) || 0 , 28 ...( typeof g . blocked === 'string' && g . blocked ?{ blocked :g . blocked } :{}), 29 ...readStep ( g . step ), 30}; 31} 32 33function readStep ( raw :unknown ) :{ step ?:Goal [ 'step' ] } { 34if ( ! raw || typeof raw !== 'object' ) return {}; 35const s = raw as Record < string , unknown >; 36const index = Number ( s . index ), total = Number ( s . total ); 37return index > 0 && total >= index ?{ step :{ index, total, text :typeof s . text === 'string' ?s . text :'' } } :{}; 38} 39 40export function applyGoalFrame ( chatId :string , raw :unknown ) { 41const goal = readGoal ( raw ); 42const before = goals . get ( chatId ); 43if ( ! goal ) { if ( goals . delete ( chatId )) emit (); return ; } 44if ( before && JSON . stringify ( before ) === JSON . stringify ( goal )) return ; 45goals . set ( chatId , goal ); 46emit (); 47} 48 49export const goalOf = ( chatId :string | null | undefined ) :Goal | null => ( chatId ?goals . get ( chatId ) ?? null :null ); 50 51export function useGoal ( chatId :string | null | undefined ) :Goal | null { 52const goal = useSyncExternalStore ( fn => { listeners . add ( fn ); return () => { listeners . delete ( fn ); }; }, () => goalOf ( chatId ), () => null ); 53// Page load or a switch to another chat: ask the host once. 54useEffect (() => { if ( chatId ) void loadGoal ( chatId ); }, [ chatId ]); 55return goal ; 56} 57 58export async function loadGoal ( chatId :string ) { 59try { applyGoalFrame ( chatId , ( await api . get <{ goal :unknown }>( `/api/goal?chatId= ${ encodeURIComponent ( chatId )} ` )). goal ); } 60catch { /* an older host has no goal route: no bar */ } 61} 62 63/** Pause, change or remove the goal. The host answers at once with the goal as it now is. */ 64export async function goalAction ( chatId :string , action :'pause' | 'edit' | 'clear' , text ?:string ) { 65const result = await api . post <{ goal :unknown }>( `/api/goal/ ${ action } ` , { chatId, ...( text !== undefined ?{ text} :{}) }); 66applyGoalFrame ( chatId , result . goal ); 67}