import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Badge, Card, FieldSelect, FieldText} from '@garage44/mithril/components' import type {Tone} from '@garage44/mithril/components' import {api} from '@/lib/api' import {age, when} from '@/lib/format' import {markdown} from '@/lib/markdown' import {notes, search} from '@/lib/memory' import type {MemoryNote} from '@/lib/memory' import {speaker} from '@/lib/names' import {bind, local} from '@/lib/reactive' const TONE: Record = {user: 'accent', feedback: 'warning', project: 'info', reference: 'neutral'} /** Notes drawn at a time, before "more". */ const PAGE = 60 /** One note: its name, where it holds and what it says it is; opened, its text. */ function note(n: MemoryNote, open: boolean, toggle: () => void, gone: string[] = []): m.Children { return (
  • {open ? (
    {markdown(n.body)}

    {n.file} · {n.set} · changed {when(n.ts)} by {speaker(n.by).title}

    ) : null}
  • ) } /** * What agents remember, as the hub keeps it for every machine (memory.rs): * the workspace's notes and each repository's, searchable as you type. */ export class Memory extends MithrilComponent { s = local({ all: [] as MemoryNote[], query: '', set: '' as string | null, open: '', shown: PAGE, error: '', loaded: false, /** `set/file` → the files it names that are gone. */ stale: {} as Record, onlyStale: false, }) oninit() { api.memory() .then((r) => { this.s.all = notes(r.sets) this.s.stale = r.stale ?? {} }) .catch((e) => (this.s.error = (e as Error).message)) .finally(() => (this.s.loaded = true)) } view() { const {all, query, set, open} = this.s if (!this.s.loaded) return

    Loading…

    if (this.s.error) return

    {this.s.error}

    const sets = [...new Set(all.map((n) => n.set))].sort((a, b) => (a === 'workspace' ? -1 : b === 'workspace' ? 1 : a.localeCompare(b))) const stale = this.s.stale const isStale = (n: MemoryNote) => (stale[`${n.set}/${n.file}`] ?? []).length > 0 const staleCount = all.filter(isStale).length const found = search((set ? all.filter((n) => n.set === set) : all).filter((n) => !this.s.onlyStale || isStale(n)), query) const more = found.length - this.s.shown return (
    {staleCount ? ( {staleCount} note{staleCount === 1 ? '' : 's'} may be out of date: {staleCount === 1 ? 'it names' : 'they name'} a file its repository no longer has.{' '} ) : null}
    ({value: x, label: x === 'workspace' ? 'Workspace (every repository)' : x}))} />
    {found.length ? (
      {found.slice(0, this.s.shown).map((n) => note(n, open === `${n.set}/${n.file}`, () => (this.s.open = open === `${n.set}/${n.file}` ? '' : `${n.set}/${n.file}`), stale[`${n.set}/${n.file}`]))}
    ) : (

    {all.length ? 'No note has all of those words.' : 'No memory yet: sessions send theirs with `midas memory sync`, which the hooks run.'}

    )} {more > 0 ? (

    ) : null}
    ) } }