Skip to content
← Public packages

@kentcdodds/quiet-hours

Quiet hours for Kent's bots: 10 PM to 8 AM MT (DST-aware) clock, shared overnight queue, override, and the 8 AM digest of finished work and open decisions.

FollowFork

Files

mirror.ts

46 lines · 1.7 KB · TypeScript
import { executeExport, WAITING_ON_ME_CREATE_CODE } from './cross-package.ts'
import type { Priority, QueueItem } from './types.ts'

const URGENCY: Record<Priority, 'critical' | 'high' | 'medium' | 'low'> = {
	urgent: 'critical',
	high: 'high',
	normal: 'medium',
	low: 'low',
}

/** Pure: the `@kentcdodds/waiting-on-me/create` input for a needs-decision item. */
export function waitingOnMeInput(item: QueueItem) {
	const nativeId = `quiet-hours:${item.id}`
	return {
		nativeId,
		title: `${item.bot}: ${item.title}`,
		summary: item.detail || item.title,
		whyWaiting: `${item.bot} stopped and is waiting on Kent's decision (queued during quiet hours).`,
		actionUrl: item.link ?? '',
		actionLabel: 'Decide',
		urgency: URGENCY[item.priority] ?? 'medium',
		deadline: item.deadline,
		sourceMetadata: { quietHoursItemId: item.id, bot: item.bot, kind: item.kind },
		sourceCreatedAt: item.createdAt,
		actor: 'agent' as const,
		idempotencyKey: nativeId,
	}
}

/**
 * Mirror a needs-decision item into Waiting on Me so the decision outlives
 * the 8 AM digest. Never throws: failures are returned and recorded on the
 * queue item so queueing itself always succeeds.
 */
export async function mirrorToWaitingOnMe(item: QueueItem): Promise<NonNullable<QueueItem['waitingOnMe']>> {
	if (item.kind !== 'needs-decision') return { ok: false, skipped: 'not a needs-decision item' }
	if (!item.link) return { ok: false, skipped: 'no link (Waiting on Me requires an action URL)' }
	try {
		const result = (await executeExport(WAITING_ON_ME_CREATE_CODE, waitingOnMeInput(item))) as
			| { item?: { id?: string } }
			| null
		return { ok: true, id: result?.item?.id ?? null }
	} catch (error) {
		return { ok: false, error: error instanceof Error ? error.message : String(error) }
	}
}