Skip to content
← 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.

FollowFork

Files

wake-request.ts

60 lines · 2.2 KB · TypeScript
/**
 * `@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,
		},
	}
}