okami.mom/strata

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

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

main
2.4 KiB39 linesraw
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) {
11  const out = [];
12  for (const q of Array.isArray(raw) ? raw.slice(0, 6) : []) {
13    const id = text(q?.id, 120), question = text(q?.question, 800);
14    if (!id || !question) continue;
15    const 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);
17    out.push({ id, question, detail: text(q.detail, 800) || undefined, header: text(q.header, 80) || undefined, options, multiSelect: q.multiSelect === true });
18  }
19  return 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) {
27  const byId = new Map((Array.isArray(given) ? given : []).map(a => [String(a?.id ?? ''), a]));
28  const answers = [];
29  for (const q of questions) {
30    const a = byId.get(q.id);
31    const labels = new Set(q.options.map(o => o.label));
32    const selected = (Array.isArray(a?.selected) ? a.selected : []).map(s => String(s)).filter(s => labels.has(s));
33    const custom = text(a?.custom, 2000);
34    if (!selected.length && !custom) throw Object.assign(new Error('Answer every question, or skip them.'), { code: 'question-incomplete' });
35    if (!q.multiSelect && selected.length > 1) throw Object.assign(new Error('Choose one answer for each question.'), { code: 'question-invalid' });
36    answers.push({ id: q.id, selected: [...new Set(selected)], ...(custom ? { custom } : {}) });
37  }
38  return { answers };
39}