/** * The linear view of a session's active path. Every client reads the artifact * text, the working copy, and the comments straight off the record, so the * record keeps them materialized; this module recomputes them from the path * whenever a tip moves. Comments the path does not reach are shelved, never * dropped, so a later move can show them again. */ import { derivePath, followBranch, MAIN_BRANCH, pathOf, type SessionHistory } from "./history"; import type { Annotation, ShareLink, SharePolicy, Thread, TextCut } from "./types"; export interface PathView { content: string; workingCopy: string | undefined; textCuts: TextCut[] | undefined; annotations: Annotation[]; shelvedAnnotations: Annotation[]; } /** * Fold the active path over every comment the session holds, on or off the * path. Comments on the path whose objects are gone stay gone. */ export function viewOfPath(history: SessionHistory, known: Annotation[]): PathView { const derived = derivePath(history); const agentHead = pathOf(history) .filter((entry) => entry.type === "revision" && entry.by === "agent") .at(-1); const content = agentHead?.type === "revision" ? agentHead.content : derived.head.content; const byId = new Map(known.map((annotation) => [annotation.id, annotation])); const annotations: Annotation[] = []; for (const id of derived.annotationIds) { const annotation = byId.get(id); if (annotation) { annotations.push(annotation); byId.delete(id); } } return { content, workingCopy: derived.head.content === content ? undefined : derived.head.content, textCuts: derived.head.by === "reviewer" ? derived.head.textCuts : undefined, annotations, shelvedAnnotations: [...byId.values()], }; } /** Write a path view back into a record, in place; absent fields are removed. */ export function applyPathView(session: Thread, view: PathView): void { session.artifact = { ...session.artifact, content: view.content }; session.annotations = view.annotations; if (view.textCuts?.length) session.textCuts = view.textCuts; else delete session.textCuts; if (view.workingCopy === undefined) delete session.workingCopy; else session.workingCopy = view.workingCopy; if (view.shelvedAnnotations.length === 0) delete session.shelvedAnnotations; else session.shelvedAnnotations = view.shelvedAnnotations; } /** * The record as a share carries it: the followed branch's path and what it * shows, with no other branch, no shelved comment, and no working copy of the * owner's. A followed branch that no longer exists falls back to main. A record * without a history travels as it is. */ export function viewFollowing(session: Thread, branch?: string): Thread { const asked = branch ?? session.shares?.at(-1)?.shareBranch ?? MAIN_BRANCH; // a branch that no longer exists falls back to main, matching the daemon const followed = session.history && session.history.tips[asked] === undefined ? MAIN_BRANCH : asked; const shared: Thread = { ...session }; if (!session.history) return shared; const history = followBranch(session.history, followed); const known = [...session.annotations, ...(session.shelvedAnnotations ?? [])]; shared.history = history; applyPathView(shared, viewOfPath(history, known)); delete shared.shelvedAnnotations; return shared; } /** Publish one link's branch and policy, without carrying other links; the gateway assigns its final ID and owner. */ export function sharePublicationView( session: Thread, publication: SharePolicy & Pick, ): Thread { const shared = viewFollowing(session, publication.shareBranch ?? session.history?.branch); const link: ShareLink = { id: session.id, requireAuth: publication.requireAuth, allowlist: publication.requireAuth ? publication.allowlist : [], shareBranch: shared.history?.branch ?? publication.shareBranch ?? MAIN_BRANCH, }; if (publication.agentEnabled) link.agentEnabled = true; return { ...shared, shares: [link] }; }