Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/grok-bot

Wake a named Grok Bot from Kody by posting a prompt to that bot's own Cursor webhook.

FollowFork

Files

quiet-stamp.ts

38 lines · 1.3 KB · TypeScript
import quietStatus from 'kody:@kentcdodds/quiet-hours/status'

/** How long a wake waits on quiet-hours before sending without a stamp. */
export const QUIET_STAMP_TIMEOUT_MS = 1_500

export type QuietStamp = { quiet: true; quietUntil: string }

type StatusFn = (input: { bot?: string }) => Promise<{ quiet?: unknown; until?: unknown } | null | undefined>

/**
 * `{ quiet: true, quietUntil }` when the wake goes out during quiet hours
 * (10 PM to 8 AM MT via @kentcdodds/quiet-hours `status`), else null.
 * Excluded bots (Lilly, Imogen) are never stamped. Fails open: a quiet-hours
 * error or timeout means no stamp, and the wake still goes out.
 */
export async function quietStamp(
	bot: string,
	deps: { status?: StatusFn; timeoutMs?: number } = {},
): Promise<QuietStamp | null> {
	const status = deps.status ?? ((input) => quietStatus(input))
	let timer: ReturnType<typeof setTimeout> | undefined
	try {
		const result = await Promise.race([
			status({ bot }),
			new Promise<null>((resolve) => {
				timer = setTimeout(() => resolve(null), deps.timeoutMs ?? QUIET_STAMP_TIMEOUT_MS)
			}),
		])
		if (result?.quiet === true && typeof result.until === 'string') {
			return { quiet: true, quietUntil: result.until }
		}
		return null
	} catch {
		return null
	} finally {
		if (timer) clearTimeout(timer)
	}
}