import { localDateKey } from './clock.ts' import { BUCKET_LABELS, DIGEST_BUCKETS, PRIORITIES, type DigestBucket, type QueueItem, } from './types.ts' /** * Which digest bucket an item lands in. Checked in this order: * 1. `urgent-resolved`: priority urgent AND kind done / fyi / kent-request * (something urgent happened overnight and is already handled). * 2. `deadline-today`: kind is not done AND `deadline` falls on the digest's * local (America/Denver) date or earlier. * 3. `decision`: kind needs-decision (a bot stopped and is waiting on Kent). * 4. `fyi`: everything else: done (finished work), fyi, and legacy kent-request. */ export function bucketFor(item: QueueItem, todayKey: string): DigestBucket { const resolvedKind = item.kind === 'done' || item.kind === 'fyi' || item.kind === 'kent-request' if (item.priority === 'urgent' && resolvedKind) return 'urgent-resolved' if (item.kind !== 'done' && item.deadline && localDateKey(new Date(item.deadline)) <= todayKey) { return 'deadline-today' } if (item.kind === 'needs-decision') return 'decision' return 'fyi' } const priorityRank = (item: QueueItem) => { const rank = (PRIORITIES as ReadonlyArray).indexOf(item.priority) return rank === -1 ? PRIORITIES.length : rank } function compareWithinBucket(a: QueueItem, b: QueueItem) { const byPriority = priorityRank(a) - priorityRank(b) if (byPriority) return byPriority const da = a.deadline ? Date.parse(a.deadline) : Infinity const db = b.deadline ? Date.parse(b.deadline) : Infinity if (da !== db) return da - db return a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0 } export type DigestGroup = { bucket: DigestBucket; label: string; items: QueueItem[] } /** Group and order items: urgent-resolved, deadline-today, decision, fyi. */ export function orderDigest(items: QueueItem[], now: Date): DigestGroup[] { const todayKey = localDateKey(now) const groups = new Map(DIGEST_BUCKETS.map((b) => [b, []])) for (const item of items) groups.get(bucketFor(item, todayKey))!.push(item) return DIGEST_BUCKETS.map((bucket) => ({ bucket, label: BUCKET_LABELS[bucket], items: groups.get(bucket)!.sort(compareWithinBucket), })).filter((group) => group.items.length > 0) } export function digestCounts(items: QueueItem[]) { return { total: items.length, done: items.filter((i) => i.kind === 'done').length, needsDecision: items.filter((i) => i.kind === 'needs-decision').length, fyi: items.filter((i) => i.kind === 'fyi' || i.kind === 'kent-request').length, } } function line(item: QueueItem, numbered?: number) { const lead = numbered ? `${numbered}. ` : '- ' const due = item.deadline ? ` (due ${new Date(item.deadline).toLocaleString('en-US', { timeZone: 'America/Denver', hour: 'numeric', minute: '2-digit', month: 'short', day: 'numeric', })} MT)` : '' const link = item.link ? ` <${item.link}>` : '' return `${lead}${item.bot}: ${item.title}${due}${link}` } /** * Plain-text digest: a report of finished work and open decisions, not a * backlog to kick off. Kept under `maxChars` (Discord's 2000 limit). */ export function formatDigestText(groups: DigestGroup[], maxChars = 1900): string { const items = groups.flatMap((g) => g.items) if (items.length === 0) return 'Quiet night. Nothing needs you.' const counts = digestCounts(items) const decisionsOpen = groups .filter((g) => g.bucket === 'decision' || g.bucket === 'deadline-today') .reduce((n, g) => n + g.items.length, 0) const lines: string[] = [ `Morning. Overnight: ${counts.done} done, ${decisionsOpen} need${decisionsOpen === 1 ? 's' : ''} you.`, ] for (const group of groups) { lines.push('', `${group.label}:`) const numbered = group.bucket === 'decision' || group.bucket === 'deadline-today' group.items.forEach((item, index) => lines.push(line(item, numbered ? index + 1 : undefined))) } let out = '' let shown = 0 for (const next of lines) { const candidate = out ? out + '\n' + next : next if (candidate.length > maxChars - 60) break out = candidate if (/^(\d+\.|-) /.test(next)) shown += 1 } if (shown < items.length) { out += `\n\n…and ${items.length - shown} more. Full list: quiet-hours list.` } return out }