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

plan.ts

154 lines · 3.6 KB · TypeScript
/**
 * Plan model and storage. Plans are the factory's source of truth for what to
 * build and why. Separate storage key from the work-item ledger.
 */
import { loadJson, saveJson } from '../lib/storage.ts'
import { nowIso } from './work-item.ts'

export const PLANS_KEY = 'factory:plans:v1'

export const PLAN_STATUSES = ['exploring', 'proposed', 'decided', 'in-progress', 'shipped'] as const
export type PlanStatus = (typeof PLAN_STATUSES)[number]

export type PlanPriority = 0 | 1 | 2 | 3 | null

export type PlanEvent = {
	at: string
	from: PlanStatus | null
	to: PlanStatus
	reason: string
	actor: string
}

export type PlanLink = {
	kind: 'pr' | 'page' | 'decision' | 'issue' | 'other'
	url: string
	label?: string
	at: string
}

export type ProgressNote = {
	date: string
	text: string
	author?: string
}

export type PlanSource = {
	kind: 'github-project'
	itemId: string
	url: string
	contentId: string
	type: string
	importedAt: string
	extra?: Record<string, unknown>
}

export type Plan = {
	id: string
	title: string
	body: string
	status: PlanStatus
	priority: PlanPriority
	projectId: string
	linkedIssue?: { url: string; number: number; repo?: string; state?: string }
	links: PlanLink[]
	progressNotes: ProgressNote[]
	createdAt: string
	updatedAt: string
	statusChangedAt: string
	events: PlanEvent[]
	source?: PlanSource
	archived: boolean
	workItemIds: string[]
}

export type PlanStore = {
	version: 1
	plans: Record<string, Plan>
}

export function emptyPlanStore(): PlanStore {
	return { version: 1, plans: {} }
}

export function newPlanId(): string {
	return `plan_${crypto.randomUUID().replace(/-/g, '').slice(0, 16)}`
}

export async function loadPlanStore(): Promise<PlanStore> {
	return loadJson<PlanStore>(PLANS_KEY, emptyPlanStore())
}

export async function savePlanStore(store: PlanStore): Promise<void> {
	await saveJson(PLANS_KEY, store)
}

export function withPlanLink(links: PlanLink[], link: PlanLink): PlanLink[] {
	const list = links.slice()
	if (!list.some((l) => l.kind === link.kind && l.url === link.url)) list.push(link)
	return list
}

export function planStatusLabel(s: PlanStatus): string {
	const labels: Record<PlanStatus, string> = {
		exploring: 'Exploring',
		proposed: 'Proposed',
		decided: 'Decided',
		'in-progress': 'In progress',
		shipped: 'Shipped',
	}
	return labels[s] ?? s
}

export function createPlan(input: {
	title: string
	body?: string
	status?: PlanStatus
	priority?: PlanPriority
	projectId?: string
	linkedIssue?: Plan['linkedIssue']
	source?: PlanSource
	createdAt?: string
	archived?: boolean
}): Plan {
	const at = input.createdAt ?? nowIso()
	const status = input.status ?? 'exploring'
	return {
		id: newPlanId(),
		title: input.title,
		body: input.body ?? '',
		status,
		priority: input.priority ?? null,
		projectId: input.projectId ?? 'kody',
		linkedIssue: input.linkedIssue,
		links: [],
		progressNotes: [],
		createdAt: at,
		updatedAt: at,
		statusChangedAt: at,
		events: [{ at, from: null, to: status, reason: 'created', actor: 'human' }],
		source: input.source,
		archived: input.archived ?? false,
		workItemIds: [],
	}
}

export function updatePlanStatus(plan: Plan, to: PlanStatus, reason: string, actor = 'human'): Plan {
	if (plan.status === to) return plan
	const at = nowIso()
	return {
		...plan,
		status: to,
		updatedAt: at,
		statusChangedAt: at,
		events: [...plan.events, { at, from: plan.status, to, reason, actor }],
	}
}

export function addProgressNote(plan: Plan, note: { date: string; text: string; author?: string }): Plan {
	return {
		...plan,
		progressNotes: [...plan.progressNotes, note],
		updatedAt: nowIso(),
	}
}