okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1/** 2* The agent's own questions (the Harness's `ask_user_question` tool, #4244). The Harness raises the `user-questions/request` event with 3* `{ questions: [{ id, question, detail?, header?, options?: [{ label, description? }], multiSelect?, intent? } ], wait?: { callId } }` and wants 4* `{ answers: [{ id, selected: string[], custom? }] }` back (AskUserQuestionRequestEvent / AskUserQuestionAnswer of the pinned Harness). 5* This file only reads the first into something the page can draw, and checks the second before it goes out. 6*/ 7const text = ( v , max ) => ( typeof v === 'string' ?v . trim (). slice ( 0 , max ) :'' ); 8 9/** The questions of a request, bounded and plain; an empty list when the request carries nothing that can be shown. */ 10export function parseQuestions ( raw ) { 11const out = []; 12for ( const q of Array . isArray ( raw ) ?raw . slice ( 0 , 6 ) :[]) { 13const id = text ( q ?. id , 120 ), question = text ( q ?. question , 800 ); 14if ( ! id || ! question ) continue ; 15const options = ( Array . isArray ( q . options ) ?q . options . slice ( 0 , 12 ) :[]) 16. map ( o => ({ label :text ( o ?. label , 200 ), description :text ( o ?. description , 400 ) || undefined })). filter ( o => o . label ); 17out . push ({ id, question, detail :text ( q . detail , 800 ) || undefined , header :text ( q . header , 80 ) || undefined , options, multiSelect :q . multiSelect === true }); 18} 19return out ; 20} 21 22/** 23* The answer the person gave, checked against the questions that were asked: every question answered, only the offered labels (or the person's own 24* words), one choice for a single-select question. Throws a plain Error with a code on the first thing that does not fit. 25*/ 26export function buildAnswer ( questions , given ) { 27const byId = new Map (( Array . isArray ( given ) ?given :[]). map ( a => [ String ( a ?. id ?? '' ), a ])); 28const answers = []; 29for ( const q of questions ) { 30const a = byId . get ( q . id ); 31const labels = new Set ( q . options . map ( o => o . label )); 32const selected = ( Array . isArray ( a ?. selected ) ?a . selected :[]). map ( s => String ( s )). filter ( s => labels . has ( s )); 33const custom = text ( a ?. custom , 2000 ); 34if ( ! selected . length && ! custom ) throw Object . assign ( new Error ( 'Answer every question, or skip them.' ), { code :'question-incomplete' }); 35if ( ! q . multiSelect && selected . length > 1 ) throw Object . assign ( new Error ( 'Choose one answer for each question.' ), { code :'question-invalid' }); 36answers . push ({ id :q . id , selected :[ ...new Set ( selected )], ...( custom ?{ custom} :{}) }); 37} 38return { answers}; 39}