okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1/** The agent's own questions (#4244): what the host sends, and the answer that goes back. */ 2export interface AgentQuestion { id :string ; question :string ; detail ?:string ; header ?:string ; options :{ label :string ; description ?:string }[]; multiSelect :boolean } 3export interface AgentAsk { callId :string ; questions :AgentQuestion [] } 4export interface AgentAnswer { id :string ; selected :string []; custom ?:string } 5 6/** Reads the frame of the host defensively: a question that cannot be shown is left out. */ 7export function readAsk ( frame :{ callId ?:unknown ; questions ?:unknown }) :AgentAsk | null { 8const callId = typeof frame . callId === 'string' ?frame . callId :'' ; 9const questions = ( Array . isArray ( frame . questions ) ?frame . questions :[]). flatMap (( q ) :AgentQuestion [] => { 10if ( ! q || typeof q !== 'object' ) return []; 11const o = q as Record < string , unknown >; 12if ( typeof o . id !== 'string' || ! o . id || typeof o . question !== 'string' || ! o . question . trim ()) return []; 13const options = ( Array . isArray ( o . options ) ?o . options :[]). flatMap (( x ) :{ label :string ; description ?:string }[] => { 14const p = x as Record < string , unknown > | null ; 15return p && typeof p . label === 'string' && p . label ?[{ label :p . label , ...( typeof p . description === 'string' && p . description ?{ description :p . description } :{}) }] :[]; 16}); 17return [{ id :o . id , question :o . question , ...( typeof o . detail === 'string' && o . detail ?{ detail :o . detail } :{}), ...( typeof o . header === 'string' && o . header ?{ header :o . header } :{}), options, multiSelect :o . multiSelect === true }]; 18}); 19return callId && questions . length ?{ callId, questions} :null ; 20} 21 22/** Whether every question has a choice or the person's own words, so the answer can be sent. */ 23export function answerComplete ( questions :readonly AgentQuestion [], answers :Record < string , { selected :string []; custom :string }>) :boolean { 24return questions . every ( q => { const a = answers [ q . id ]; return !! a && ( a . selected . length > 0 || a . custom . trim (). length > 0 ); }); 25} 26 27/** The answer in the shape the host takes: the offered labels the person ticked, and their own words when they typed some. */ 28export function toAnswers ( questions :readonly AgentQuestion [], answers :Record < string , { selected :string []; custom :string }>) :AgentAnswer [] { 29return questions . map ( q => { const a = answers [ q . id ] ?? { selected :[], custom :'' }; const custom = a . custom . trim (); return { id :q . id , selected :a . selected , ...( custom ?{ custom} :{}) }; }); 30}