garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

.. /web/src/lib/treetalk.ts

raw

import type {Direction, Round, Tech, TreeState} from '@/lib/api'
import {goalsUnder, techsOf, wayOf} from '@/lib/tree'

/**
 * The research tree in words, for a voice conversation about it (discuss.ts,
 * `read_tree`): the goals, the techs on the way to one by state, the insights,
 * where the tree heads and the scout's last rounds. Ids are kept, as the voice
 * acts on them (`change_tree`, `scout_tree`).
 */
export function treeText(d: TreeState, goal: string): string {
    if (!d.goals.length) return 'The tree has no goals yet: a goal is the person\'s own sentence (goal_save), with the projects it is about; then the scout can fill in the techs on the way.'
    const g = d.goals.find((x) => x.id === goal) ?? d.goals.find((x) => !x.parent) ?? d.goals[0]!
    const goals = d.goals.map((x) => `- ${x.id} "${x.name}"${x.parent ? ` (under ${x.parent})` : ''}${x.id === g.id ? ' ← this one' : ''}`)
    const facts = [
        g.statement && `Statement: ${g.statement}`,
        g.who && `Who notices: ${g.who}`,
        g.signs && `Signs of progress: ${g.signs}`,
        g.never.length ? `Never: ${g.never.join('; ')}` : '',
        `Projects: ${g.repos.length ? g.repos.join(', ') : 'none yet (the scout needs at least one)'}`,
    ].filter(Boolean)
    const techs = techsOf(d, g.id, true)
    const name = (id: string) => d.techs.find((t) => t.id === id)?.name ?? id
    const line = (t: Tech) =>
        `- ${t.id} "${t.name}" [${t.size}]: ${t.what}${t.needs.length ? ` Needs: ${t.needs.map(name).join(', ')}.` : ''}${t.research ? ` Being built in ${t.research.session}.` : ''}${t.dropped ? ` Not this: ${t.dropped.why || 'no reason given'}.` : ''}`
    const byState = (s: string, title: string) => {
        const list = techs.filter((t) => (s === 'researching' ? !!t.research && t.state !== 'built' : t.state === s && !(s !== 'built' && t.research)))
        return list.length ? `${title} (${list.length}):\n${list.map(line).join('\n')}` : `${title}: none.`
    }
    // The way as the page numbers it, so "step 3" means the same out loud as on the screen.
    const way = wayOf(techs.filter((t) => t.state !== 'fog' && t.state !== 'dropped')).map(
        (x) => `${x.n}. ${x.tech.name} (${x.tech.id}): ${x.tech.state === 'researching' ? 'being built now' : x.after.length ? `after step ${x.after.join(', ')}` : 'can start now'}`,
    )
    const under = goalsUnder(d.goals, g.id)
    const insights = d.insights.filter((i) => under.includes(i.goal) && !i.dropped && !i.faded).map((i) => `- ${i.id}${i.about ? ` (on ${name(i.about)})` : ''}: ${i.text}${i.move === 'person' ? ' (the person\'s own)' : ''}`)
    const proposals = (d.proposals ?? []).filter((p) => under.includes(p.goal)).map((p) => `- ${p.id} "${p.name}": ${p.statement} (${p.techs.length} techs)`)
    const going = (d.directions ?? []).filter((x) => !x.cleared)
    const b = d.budget
    const dir = going.length
        ? [
              `${going.length} going, in this order (the first starts first when the budget is short)${b ? `; together at most ${b.at_once} at once, ${b.per_day} a day and ${b.per_project} at once in one project, ${b.going} at work now and ${b.today} started today` : ''}:`,
              ...going.map((x) => `- ${directionText(d, x)}`),
          ].join('\n')
        : 'No direction is set: nothing is researched by itself.'
    const rounds = d.rounds.filter((r) => under.includes(r.goal)).slice(-5).map(roundLine)
    const part = (title: string, rows: string[]) => (rows.length ? `${title}:\n${rows.join('\n')}` : `${title}: none.`)
    return [
        part('Goals', goals),
        `The goal ${g.id} "${g.name}":\n${facts.join('\n')}`,
        part('The way: what is left, numbered in the order it is built (the page shows the same numbers)', way),
        byState('built', 'Built'),
        byState('researching', 'Being built now'),
        byState('available', 'Available (what it needs is built)'),
        byState('locked', 'Locked (needs a tech not built yet)'),
        byState('fog', 'In the fog (a guess: Keep it or Not this)'),
        byState('dropped', 'Said "not this" to'),
        part('Insights', insights),
        part('Themes the scout proposed (proposal_accept makes one a goal)', proposals),
        `Direction: ${dir}`,
        part("The scout's latest rounds", rounds),
    ].join('\n\n')
}

/**
 * One tech in words, opened to talk it over (`open_tech`): its description, where it stands on the tree, the notes
 * on it, what its mockup shows (the voice cannot see the picture) and the remarks waiting for its next drawing.
 */
export function techText(d: TreeState, id: string): string {
    const t = d.techs.find((x) => x.id === id)
    if (!t) return `No tech ${id}.`
    const name = (x: string) => d.techs.find((y) => y.id === x)?.name ?? x
    const after = d.techs.filter((x) => x.needs.includes(t.id) && x.state !== 'dropped').map((x) => x.name)
    const notes = d.insights.filter((i) => i.about === t.id && !i.faded && !i.dropped).map((i) => `- ${i.id}: ${i.text}`)
    const remarks = (t.remarks ?? []).map((r) => `- ${r.id}: ${r.text}`)
    const shows = t.mockup_png
        ? t.mockup_shows
            ? `Its mockup (a ${t.mockup_frame ?? 'phone'} screen) shows: ${t.mockup_shows}`
            : 'It has a mockup, drawn before the scout said what its mockups show: ask them what they see on it, or have it drawn again.'
        : 'It has no mockup yet: the scout can draw one (scout_tree, move sketch).'
    return [
        `${t.id} "${t.name}" [${t.size}], ${t.state}${t.research ? `, being built in ${t.research.session}` : ''}.`,
        `What it is: ${t.what || '(not said)'}`,
        `Who notices: ${t.who || '(not said)'}`,
        `What they do differently: ${t.differently || '(not said)'}`,
        `What exists already: ${t.exists || '(not said)'}`,
        `Needs: ${t.needs.length ? t.needs.map(name).join(', ') : 'nothing'}. Leads to: ${after.length ? after.join(', ') : 'the goal'}.`,
        notes.length ? `Notes on it:\n${notes.join('\n')}` : '',
        shows,
        remarks.length ? `Remarks waiting for its next drawing:\n${remarks.join('\n')}` : 'No remarks wait for its next drawing.',
    ]
        .filter(Boolean)
        .join('\n')
}

function directionText(d: TreeState, x: Direction): string {
    const name = (id: string | null) => (id ? (d.techs.find((t) => t.id === id)?.name ?? id) : 'nothing yet')
    const state = x.reached ? 'reached' : x.paused ? `paused (${x.paused})` : 'on'
    const next = x.next ? ` Next: ${name(x.next.tech)} (${x.next.why}${x.next.after ? `, after ${name(x.next.after)}` : ''}${x.next.project ? `, in ${x.next.project}` : ''}).` : ''
    const last = x.runs.at(-1)
    const ran = last ? ` Last started: ${name(last.tech)} in ${last.session}${last.outcome ? `, ${last.outcome}` : ', at work'}.` : ''
    const scout = x.scout === false ? 'no scout' : `the scout sent when the way runs out${x.scout_every ? ` and every ${x.scout_every} landings` : ''}`
    return `${x.id}: heading to "${x.name}" (${x.target}), ${state}; at most ${x.at_once} at once and ${x.per_day} a day, ${scout}.${next}${ran}`
}

export function roundLine(r: Round): string {
    // A round at work says what it reads now and what it said last, so the voice can tell where the scout is.
    const now = r.live?.lines[0]
    const reading = r.state === 'asked' && r.live
        ? `, reading now: ${now ? `${now.kind} ${now.text} in ${now.repo}` : 'starting'}${r.live.thought ? `; it said last: "${r.live.thought}"` : ''} (${r.live.reads} files, ${r.live.searches} searches so far)`
        : ''
    const state = r.state === 'done' ? `done: ${r.summary ?? ''}` : r.state === 'failed' ? `failed: ${r.error ?? ''}` : r.state === 'queued' ? 'waiting its turn' : `going${reading}`
    return `- ${r.id} ${r.move} on ${r.goal}${r.focus ? ` (looking at: ${r.focus})` : ''}: ${state}`
}

/** What a finished round is worth telling: its summary, what it counted, and the guesses it brought. */
export function roundNews(d: TreeState, r: Round): string {
    if (r.state === 'failed') return `The scout's ${r.move} round ${r.id} failed: ${r.error ?? 'no reason given'}.`
    const c = r.counts
    const counted = c ? ` It added ${c.added} techs, confirmed ${c.confirmed}, found ${c.built} built, ${c.insights} insights${c.themes ? `, ${c.themes} themes` : ''}${c.mockups ? `, ${c.mockups} mockups` : ''}.` : ''
    const guesses = d.techs.filter((t) => t.state === 'fog' && t.at >= r.at && t.goals.includes(r.goal)).map((t) => `${t.id} "${t.name}": ${t.what}`)
    const themes = r.move === 'group' ? (d.proposals ?? []).filter((p) => p.goal === r.goal).map((p) => `${p.id} "${p.name}"`) : []
    return [
        `The scout's ${r.move} round ${r.id} is done: ${r.summary ?? ''}${counted}`,
        guesses.length ? `New guesses (kept once started, faded in a month if not):\n${guesses.map((x) => `- ${x}`).join('\n')}` : '',
        themes.length ? `Themes proposed: ${themes.join(', ')}.` : '',
    ]
        .filter(Boolean)
        .join('\n')
}