repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/diff-review/session-state.ts

Raw
File outline
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
import { EXTENSION_ID } from "./constants.js";
import type { ReviewLiveEntry, ReviewRoundEntry, ReviewState } from "./types.js";

type WritableSessionManager = ExtensionContext["sessionManager"] & {
	appendCustomEntry(customType: string, data?: unknown): string;
};

export function appendReviewRound(ctx: ExtensionContext, data: ReviewRoundEntry): string {
	return (ctx.sessionManager as WritableSessionManager).appendCustomEntry(EXTENSION_ID, data);
}

export function appendLiveReview(ctx: ExtensionContext, data: ReviewLiveEntry): string {
	return (ctx.sessionManager as WritableSessionManager).appendCustomEntry(EXTENSION_ID, data);
}

export function restoreReviewState(ctx: ExtensionContext): ReviewState {
	const rounds: ReviewRoundEntry[] = [];
	let live: ReviewLiveEntry | undefined;
	for (const entry of ctx.sessionManager.getBranch()) {
		if (entry.type !== "custom" || entry.customType !== EXTENSION_ID) continue;
		const data = entry.data as ReviewRoundEntry | ReviewLiveEntry | undefined;
		if (data?.version !== 1) continue;
		if (data.kind === "round") rounds.push(data);
		if (data.kind === "live") live = data.status === "open" ? data : undefined;
	}
	return {
		round: Math.max(0, ...rounds.map((round) => round.round)),
		findings: rounds.flatMap((round) => round.findings),
		rounds,
		...(live && { live }),
	};
}

export function openFindings(state: ReviewState) {
	return state.findings.filter((finding) => finding.status === "open");
}