/** * `@kody/factory.wake.requested`: the factory names a role, and a bot when a * specific bot was asked for. `@kody/wake-relay` maps the bot (or the role's * route) to credentials and posts. The factory holds no wake credentials. */ import { WAKE_PROMPT_BUDGET, clipToBytes, eventKey } from './emit.ts' export const WAKE_ROLES = ['package-triage', 'platform', 'outage', 'dr', 'notification'] as const export type WakeRole = (typeof WAKE_ROLES)[number] export const WAKE_SOURCES = ['health', 'signal', 'notification', 'friction', 'self-test', 'outage', 'dr', 'card'] as const export type WakeSource = (typeof WAKE_SOURCES)[number] /** Role for a request that names a bot but no role. */ export function roleForBot(bot: string): WakeRole { const name = bot.trim().toLowerCase() if (name === 'cole') return 'platform' return 'package-triage' } export type WakeRequestInput = { source: WakeSource /** Who the wake is for, by role. Defaults from `bot`. */ role?: WakeRole /** A specific bot. Omit to let the relay route by role. */ bot?: string prompt: string reason: string /** Stable id of the thing the wake is about (fingerprint, signal id, notification id, friction title, card key). */ ref: string cardId?: string | null priority?: number | null /** `true` for a dry run: the relay logs and never posts. */ dryRun?: boolean /** Override the clock (tests). */ now?: number } export function buildWakeRequest(input: WakeRequestInput) { const at = new Date(input.now ?? Date.now()).toISOString() const requestId = eventKey(input.source, input.ref, at) const bot = typeof input.bot === 'string' ? input.bot.trim().toLowerCase() : '' const role: WakeRole = input.role && (WAKE_ROLES as readonly string[]).includes(input.role) ? input.role : roleForBot(bot) const dryRun = input.dryRun === true return { requestId, payload: { requestId, role, ...(bot ? { bot } : {}), source: input.source, reason: clipToBytes(input.reason, 400), ref: clipToBytes(input.ref, 200), ...(input.cardId ? { cardId: input.cardId } : {}), ...(typeof input.priority === 'number' ? { priority: input.priority } : {}), prompt: clipToBytes(input.prompt, WAKE_PROMPT_BUDGET), dryRun, shadow: dryRun, }, } }