โ Public packages
@kody/kody-factory
Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.
Files
/**
* Server-rendered Report pages: a list (filter by source, noteworthy or quiet)
* and one report with its findings and the cards it filed. Pure (no
* kody:runtime). Read-only pages; they work without JavaScript.
*/
import { esc, icon, renderShell } from './ui.ts'
import { renderMarkdown } from '../lib/markdown.ts'
import type { FactoryReport, FindingSeverity } from '../reports/model.ts'
import type { WorkItemState } from '../core/work-item.ts'
const TZ = 'America/Denver'
function fmtTime(iso: string | null | undefined): string {
const t = iso ? Date.parse(iso) : Number.NaN
if (!Number.isFinite(t)) return 'unknown'
return new Intl.DateTimeFormat('en-US', { timeZone: TZ, month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }).format(new Date(t))
}
const SEVERITY: Record<FindingSeverity, [string, string]> = {
info: ['Info', 'off'],
notice: ['Notice', 'st-triaged'],
warning: ['Warning', 'lvl-warn'],
critical: ['Critical', 'lvl-bad'],
}
const STATE_LABEL: Record<string, string> = {
intake: 'New',
triaged: 'Triaged',
planned: 'Planned',
running: 'Running',
verifying: 'Verifying',
shipped: 'Shipped',
'needs-kent': 'Needs you',
dropped: 'Dropped',
missing: 'Removed',
}
function stateChip(state: string): string {
return `<span class="chip st-${esc(state)}">${esc(STATE_LABEL[state] ?? state)}</span>`
}
function periodText(r: FactoryReport): string {
if (!r.period) return ''
if (r.period.label) return r.period.label
if (r.period.start && r.period.end) return `${r.period.start} to ${r.period.end}`
return r.period.start ?? r.period.end ?? ''
}
function countsChips(r: FactoryReport): string {
const created = r.cards.filter((c) => c.relation === 'created').length
const existing = r.cards.length - created
const needsYou = r.cards.filter((c) => c.needsKent).length
const flagged = r.findings.filter((f) => f.flagged).length
const chips: string[] = []
if (!r.noteworthy) chips.push('<span class="chip off">Quiet</span>')
if (created) chips.push(`<span class="chip st-intake">${created} new card${created === 1 ? '' : 's'}</span>`)
if (existing) chips.push(`<span class="chip off">${existing} still open</span>`)
if (needsYou) chips.push(`<span class="chip st-needs-kent">${needsYou} need${needsYou === 1 ? 's' : ''} you</span>`)
if (flagged) chips.push(`<span class="chip lvl-warn">${flagged} flagged</span>`)
return `<div class="chip-row">${chips.join('')}</div>`
}
function reportRow(bp: string, r: FactoryReport): string {
const period = periodText(r)
return `<li class="frow"><div class="frow-main"><a class="row-title" href="${bp}/reports/${esc(r.id)}">${esc(r.title)}</a>
<div class="row-meta"><span>${esc(r.source)}</span>${r.projectId ? `<span>${esc(r.projectId)}</span>` : ''}${period ? `<span>${esc(period)}</span>` : ''}<span>${esc(fmtTime(r.createdAt))}</span><span>${r.findings.length} finding${r.findings.length === 1 ? '' : 's'}</span></div>
</div>${countsChips(r)}</li>`
}
export type ReportsPageOptions = {
basePath: string
reports: FactoryReport[]
total: number
sources: string[]
sourceFilter: string
showFilter: '' | 'noteworthy' | 'quiet'
}
export function renderReportsPage(opts: ReportsPageOptions): string {
const bp = esc(opts.basePath)
const tab = (value: ReportsPageOptions['showFilter'], label: string) => {
const params = new URLSearchParams()
if (value) params.set('show', value)
if (opts.sourceFilter) params.set('source', opts.sourceFilter)
const qs = params.toString()
return `<a href="${bp}/reports${qs ? `?${qs}` : ''}"${opts.showFilter === value ? ' aria-current="true"' : ''}>${esc(label)}</a>`
}
const sourceOptions = ['', ...opts.sources]
.map((s) => `<option value="${esc(s)}"${s === opts.sourceFilter ? ' selected' : ''}>${esc(s || 'All sources')}</option>`)
.join('')
const list = opts.reports.length
? `<ul class="list flist">${opts.reports.map((r) => reportRow(bp, r)).join('')}</ul>`
: `<div class="card empty"><div class="bubble">${icon('inbox')}</div><p>No reports yet. Audits and weekly reviews post here, with or without cards.</p></div>`
const main = `
<header class="page-head"><div><h1>Reports</h1><p>What audits and scheduled reviews found. A report can need no work, or file one or many cards that link back to it.</p></div></header>
<section aria-labelledby="reports-h" class="stack">
<h2 id="reports-h" class="sr">Report list</h2>
<form class="toolbar" method="get" action="${bp}/reports" role="search">
<label><span class="sr">Source</span><select name="source">${sourceOptions}</select></label>
${opts.showFilter ? `<input type="hidden" name="show" value="${esc(opts.showFilter)}"/>` : ''}
<button type="submit" class="btn">Filter</button>
<nav class="tabs" aria-label="Filter reports">${tab('', 'All')}${tab('noteworthy', 'Has something')}${tab('quiet', 'Quiet')}</nav>
</form>
<p class="muted small" role="status">${opts.reports.length} of ${opts.total} reports shown</p>
${list}
</section>`
return renderShell({ basePath: opts.basePath, view: 'reports', title: 'Reports', main })
}
export type ReportDetailOptions = {
basePath: string
report: FactoryReport
/** Live state of each card, by id. Missing ids were removed. */
cardStates: Record<string, { state: WorkItemState; needsKent: boolean } | undefined>
}
export function renderReportDetail(opts: ReportDetailOptions): string {
const bp = esc(opts.basePath)
const r = opts.report
const period = periodText(r)
const cardFor = new Map(r.cards.map((c) => [c.cardId, c]))
const cardLink = (cardId: string, label?: string) => `<a class="link" href="${bp}/cards/${esc(cardId)}">${esc(label ?? 'Open card')}</a>`
const cardRows = r.cards.length
? `<ul class="flist">${r.cards
.map((c) => {
const live = opts.cardStates[c.cardId]
const state = live?.state ?? 'missing'
return `<li class="frow"><div class="frow-main">${live ? `<a class="row-title" href="${bp}/cards/${esc(c.cardId)}">${esc(c.title)}</a>` : `<span class="row-title">${esc(c.title)}</span>`}
<div class="row-meta"><span>${c.relation === 'created' ? 'Filed by this report' : 'Already open, seen again'}</span>${(live?.needsKent ?? c.needsKent) ? '<span>Decision for you</span>' : '<span>Work card</span>'}<code class="small">${esc(c.cardId)}</code></div>
</div><div class="chip-row">${stateChip(state)}</div></li>`
})
.join('')}</ul>`
: `<p class="muted">No cards. Nothing here needs work.</p>`
const findingRows = r.findings.length
? `<ul class="flist">${r.findings
.map((f) => {
const [label, cls] = SEVERITY[f.severity] ?? SEVERITY.info
const card = f.cardId ? cardFor.get(f.cardId) : undefined
return `<li class="frow"><div class="frow-main"><b class="row-title">${esc(f.title)}</b>
${f.detail ? `<div class="md frow-sum">${renderMarkdown(f.detail)}</div>` : ''}
${card ? `<div class="row-meta">${cardLink(card.cardId, card.relation === 'created' ? 'Card filed' : 'Open card')}</div>` : ''}
</div><div class="chip-row">${f.flagged ? '<span class="chip lvl-warn">Flagged</span>' : ''}<span class="chip ${cls}">${esc(label)}</span></div></li>`
})
.join('')}</ul>`
: `<p class="muted">No findings.</p>`
const metrics = r.metrics && Object.keys(r.metrics).length
? `<section class="card stack" aria-labelledby="metrics-h"><div class="card-head"><h2 id="metrics-h">Numbers</h2></div><ul class="kv">${Object.entries(r.metrics)
.map(([k, v]) => `<li><div><b>${esc(k)}</b></div><span>${esc(typeof v === 'number' ? v.toLocaleString('en-US') : v)}</span></li>`)
.join('')}</ul></section>`
: ''
const links = r.links.length
? `<div class="chip-row">${r.links.map((l) => `<a class="btn small" href="${esc(l.url)}" target="_blank" rel="noopener noreferrer">${icon('external')}${esc(l.label ?? new URL(l.url).hostname)}</a>`).join('')}</div>`
: ''
const main = `
<header class="page-head">
<div>
<nav class="breadcrumb"><a href="${bp}/reports">Reports</a> / ${esc(r.source)}</nav>
<h1>${esc(r.title)}</h1>
<p>${esc([r.source, r.projectId, period, fmtTime(r.createdAt)].filter(Boolean).join(' ยท '))}</p>
</div>
${countsChips(r)}
</header>
<section class="card stack" aria-labelledby="summary-h"><div class="card-head"><h2 id="summary-h">Summary</h2></div><div class="md">${renderMarkdown(r.summary)}</div>${links}</section>
<div class="grid2">
<section class="card stack" aria-labelledby="cards-h"><div class="card-head"><h2 id="cards-h">Cards (${r.cards.length})</h2></div>${cardRows}</section>
${metrics || `<section class="card stack" aria-labelledby="push-h"><div class="card-head"><h2 id="push-h">Notification</h2></div><p class="muted">${r.push?.sent ? 'Sent a push for this report.' : r.noteworthy ? 'No push sent.' : 'Quiet report: recorded without a push.'}</p></section>`}
</div>
<section class="card stack" aria-labelledby="findings-h"><div class="card-head"><h2 id="findings-h">Findings (${r.findings.length})</h2></div>${findingRows}</section>
<p class="muted small">Report <code>${esc(r.id)}</code></p>`
return renderShell({ basePath: opts.basePath, view: 'reports', title: r.title, main })
}
export function renderReportMissing(basePath: string): string {
const bp = esc(basePath)
return renderShell({
basePath,
view: 'reports',
title: 'Report not found',
main: `<header class="page-head"><div><nav class="breadcrumb"><a href="${bp}/reports">Reports</a></nav><h1>Report not found</h1><p>It may have been removed, or only the newest reports are kept.</p></div></header>`,
})
}