okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.5 KiB30 linesraw
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 {
8  const callId = typeof frame.callId === 'string' ? frame.callId : '';
9  const questions = (Array.isArray(frame.questions) ? frame.questions : []).flatMap((q): AgentQuestion[] => {
10    if (!q || typeof q !== 'object') return [];
11    const o = q as Record<string, unknown>;
12    if (typeof o.id !== 'string' || !o.id || typeof o.question !== 'string' || !o.question.trim()) return [];
13    const options = (Array.isArray(o.options) ? o.options : []).flatMap((x): { label: string; description?: string }[] => {
14      const p = x as Record<string, unknown> | null;
15      return p && typeof p.label === 'string' && p.label ? [{ label: p.label, ...(typeof p.description === 'string' && p.description ? { description: p.description } : {}) }] : [];
16    });
17    return [{ 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  });
19  return 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 {
24  return 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[] {
29  return 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}