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

notify.ts

49 lines · 2.2 KB · TypeScript
/**
 * Push triggers: new cards and needs-attention notifications, under the
 * project's notification rules (`pushEnabled`, `pushLabels`,
 * `pushNeedsAttention`). Never throws and never blocks the caller's write.
 */
import { loadRules } from '../notifications/store.ts'
import { resolveRules, shouldPushCard, type Notification } from '../notifications/types.ts'
import { sendPush, type PushSendResult } from './send.ts'

/** Deep link to one card in the factory app (`/c/<id>`, which opens it in the work view; `/cards/<id>` also works). */
export function cardPath(cardId: string): string {
	return `/c/${encodeURIComponent(cardId)}`
}

export async function pushForNewCard(card: { id: string; projectId: string; title: string; body: string; labels: string[]; needsKent: boolean; priority: number | null }): Promise<PushSendResult | { skipped: string }> {
	try {
		const rule = resolveRules(await loadRules(), card.projectId)
		if (!shouldPushCard(rule, card)) return { skipped: 'rules' }
		const tag = card.labels.find((l) => /^(sentry|signal|outage|dr)$/i.test(l)) ?? (card.needsKent ? 'needs-you' : 'card')
		return await sendPush({
			kind: 'card',
			title: `${card.needsKent ? 'Needs you: ' : ''}${typeof card.priority === 'number' ? `P${card.priority} ` : ''}${card.title}`,
			body: `${card.projectId} · ${tag}${card.body ? ` · ${card.body}` : ''}`,
			path: cardPath(card.id),
			tag: `card-${card.id}`,
			cardId: card.id,
		})
	} catch (error) {
		return { skipped: error instanceof Error ? error.message.slice(0, 200) : 'push failed' }
	}
}

export async function pushForNotification(n: Notification): Promise<PushSendResult | { skipped: string }> {
	try {
		if (n.category !== 'needs-kent') return { skipped: 'category' }
		const rule = resolveRules(await loadRules(), n.projectId)
		if (!rule.pushEnabled || !rule.pushNeedsAttention) return { skipped: 'rules' }
		return await sendPush({
			kind: 'notification',
			title: `Needs you: ${n.title}`,
			body: n.body,
			path: n.itemId ? cardPath(n.itemId) : '/notifications',
			tag: `notif-${n.id}`,
			...(n.itemId ? { cardId: n.itemId } : {}),
		})
	} catch (error) {
		return { skipped: error instanceof Error ? error.message.slice(0, 200) : 'push failed' }
	}
}