import {dur, shortName} from '@/lib/format' import {speaker} from '@/lib/names' import type {WorkspaceEvent} from '@/lib/types' /** A name in a sentence, to be set apart from the words around it; `full` when it was cut short. */ export interface Named { name: string full: string } export type Part = string | Named const who = (a: unknown): Named => { const full = typeof a === 'string' && a ? speaker(a).title : 'someone' return {name: shortName(full, 48), full} } /** A sentence from a template: the names stay parts of their own, the rest is text. */ function t(strings: TemplateStringsArray, ...values: unknown[]): Part[] { const out: Part[] = [] const add = (x: Part) => { const last = out[out.length - 1] if (typeof x === 'string' && typeof last === 'string') out[out.length - 1] = last + x else if (x !== '') out.push(x) } strings.forEach((str, i) => { add(str) if (i < values.length) { const v = values[i] add(typeof v === 'object' && v != null && 'name' in v ? (v as Named) : String(v ?? '')) } }) return out } /** A sentence as plain text. */ export const text = (parts: Part[]) => parts.map((x) => (typeof x === 'string' ? x : x.name)).join('') const short = (sha: unknown) => String(sha ?? '').slice(0, 8) const secs = (s: unknown) => (typeof s === 'number' ? dur(s) : '') /** * A log line as a sentence a person reads, for the kinds that happen often; * null for the rest, which show their fields as they are. */ export function sentence(e: WorkspaceEvent): Part[] | null { const f = e as Record const node = f.node ? String(f.node) : '' switch (e.kind) { case 'claim': return t`${who(f.actor)} took ${node}${f.purpose ? ` for ${f.purpose}` : ''}` case 'release': return t`${who(f.actor)} gave ${node} back${f.held_s != null ? ` after ${secs(f.held_s)}` : ''}` case 'force_release': return t`${node} was taken back from ${who(f.holder ?? f.actor)}` case 'lease_idle': return t`${node} went back from ${who(f.actor)}: nothing ran on it for a while` case 'lease_lapsed': return t`${node} went back from ${who(f.actor)}: it stopped reporting` case 'queued': return t`${who(f.actor)} is waiting for a ${f.node ?? f.machine ?? 'any'} node${f.purpose ? ` (${f.purpose})` : ''}` case 'gave_up': return t`${who(f.actor)} stopped waiting for a node after ${secs(f.waited_s)}` case 'note': return t`${who(f.actor)} → ${who(f.to)}: ${f.text ?? ''}` case 'notes_taken': return t`${who(f.actor)} read ${Array.isArray(f.ids) ? f.ids.length : Array.isArray(f.notes) ? f.notes.length : 'its'} message${(Array.isArray(f.notes) && f.notes.length === 1) || (Array.isArray(f.ids) && f.ids.length === 1) ? '' : 's'}` case 'note_expired': return t`a message from ${who(f.from)} to ${who(f.to)} was thrown away unread (${f.reason === 'session_ended' ? 'the session ended' : 'too old'})` case 'decision_asked': return t`${who(f.actor)} asks: ${f.question ?? ''}` case 'decision_answered': return t`${who(f.actor)} answered ${f.id}: ${f.answer ?? ''}` case 'session_start': return t`${who(f.actor)} started` case 'session_end': return t`${who(f.actor)} ended` case 'session_label': return t`a session is called “${f.label ?? ''}”` case 'landing_queued': return t`${who(f.actor)} is waiting to land` case 'landing_granted': return t`${who(f.actor)} is landing` case 'landing_done': return f.outcome === 'landed' ? t`${who(f.actor)} landed ${short(f.sha)} on main` : t`${who(f.actor)}'s landing ended: ${f.outcome ?? '?'}` case 'pushed': return t`${String(f.ref ?? '').replace('refs/heads/', '')} moved to ${short(f.new)}` case 'verified': return t`${who(f.actor)} passed the gate on ${short(f.sha)}` case 'deploy_queued': return t`${f.target} deploy of ${short(f.sha)} asked for by ${who(f.actor)}` case 'deploy_built': return t`${f.target} built on ${node} in ${secs(f.seconds)}` case 'deploy_published': return t`${f.target} published (${short(f.sha)})` case 'deploy_failed': return t`${f.target} deploy failed${f.what ? `: ${f.what}` : ''}` case 'deploy_skipped': return t`${f.target} not built for ${short(f.sha)}: ${f.why ?? 'nothing it is built from changed'}` case 'deploy_announced': return t`${f.target} deploy of ${short(f.sha)} announced to the sessions at work` case 'deploy_deferred': return t`${f.target} deploy put off by ${who(f.actor)}${f.why ? `: ${f.why}` : ''}` case 'drained': return t`${node} taken out of the pool by ${who(f.actor)}: ${f.why ?? ''}` case 'undrained': return t`${node} back in the pool` case 'warm_start': return t`warming ${node}${f.why ? `: ${f.why}` : ''}` case 'warm_done': return t`${node} warmed in ${secs(f.seconds)}` case 'warm_failed': return t`warming ${node} failed: ${f.what ?? ''}` case 'intent': return t`${who(f.actor)} on ${node}: ${f.intent ?? ''}` case 'reserved': return t`${who(f.actor)} reserved ${f.namespace} ${f.number}${f.why ? ` — ${f.why}` : ''}` case 'todo_added': return t`left for the next ${f.for === 'any' ? '' : `${f.for} `}session: ${f.text ?? ''}` case 'todo_shown': return t`${who(f.actor)} picked up ${f.id}` case 'todo_done': return t`${f.id} done${f.note ? `: ${f.note}` : ''}` case 'hosted_status': return t`${who(f.actor)} is ${f.status === 'waiting' ? 'waiting for you' : String(f.status ?? '').replace('_', ' ')}` case 'deploy_step': return t`${f.deploy ? String(f.deploy).split('-').slice(2, -1).join('-') || 'a' : 'a'} deploy${f.ok === false ? ' failed at' : ':'} ${shortName(String(f.step ?? '').split(':')[0], 60)}` case 'deploy_build_start': return t`${f.target} build started on ${node}` case 'handover': return t`${node} handed over to ${who(f.actor)}${Array.isArray(f.killed) && f.killed.length ? `, ${f.killed.length} left-over process${f.killed.length === 1 ? '' : 'es'} stopped` : ''}` case 'hosted_started': return t`${who(f.actor)} started a session: ${f.title ?? ''}` case 'hosted_deleted': return t`${who(f.actor)} deleted session ${f.session ?? ''}` case 'alert': return t`warning: ${Array.isArray(f.problems) ? f.problems.join('; ') : ''}` case 'hub_started': return t`the hub started (${f.version ?? '?'})` case 'error': return [String(f.what ?? 'an error')] default: return null } }