import { uiError } from "./errors"; export type UiSeedMethod = "active-branch" | "whole-tree"; export type UiSeedResult = { path: string; method: UiSeedMethod; warning?: string; }; export type UiSeedSessionManager = { getLeafId(): string | null; getSessionFile(): string | undefined; createBranchedSession(leafId: string): string | undefined; }; export type UiSeedOptions = { cwd: string; sessionManager: UiSeedSessionManager; allowWholeTreeFallback?: boolean; forkWholeTree?: (sourcePath: string, cwd: string) => string | undefined; }; export const WHOLE_TREE_WARNING = "Whole-tree session fork includes abandoned branches; active LLM context remains branch-clean."; export function seedUiSession(options: UiSeedOptions): UiSeedResult { const leafId = options.sessionManager.getLeafId(); if (!leafId) { throw uiError( "session_seed_failed", "Cannot seed UI session: no active leaf", { action: "Send at least one message in the terminal session, then run /ui again.", retryable: true, }, ); } const branched = options.sessionManager.createBranchedSession(leafId); if (branched) return { path: branched, method: "active-branch" }; if (options.allowWholeTreeFallback) { const source = options.sessionManager.getSessionFile(); const forked = source ? options.forkWholeTree?.(source, options.cwd) : undefined; if (forked) { return { path: forked, method: "whole-tree", warning: WHOLE_TREE_WARNING, }; } } throw uiError("session_seed_failed", "Cannot seed UI session", { action: "Use a persistent terminal session with an active branch, then run /ui again.", retryable: true, }); }