/** @jsxRuntime automatic */ /** @jsxImportSource @alchemy.run/sigil */ import * as Clock from "effect/Clock"; import * as Effect from "effect/Effect"; import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Scope from "effect/Scope"; import type { ApplyEvent } from "../../../Events.ts"; import { Interaction } from "../../../Interaction.ts"; import type { Plan } from "../../../Plan.ts"; import { type PlanStatusSession, Cli } from "../../../Report.ts"; import { formatElapsed } from "../../Format.ts"; import { approvePlanScreen } from "./ApprovePlan.tsx"; import { Plan as PlanComponent, PlanTree } from "./PlanView.tsx"; /** * UI choices that outlive a single apply session. `alchemy dev` opens a new * session (and a new plan widget) on every hot reload; without this the * widget would reappear after the user hid it with `p`. */ interface SessionPreferences { planExpanded: boolean; } export const sigilCli = () => Layer.effect( Cli, Effect.map(Interaction, (cli) => { const preferences: SessionPreferences = { planExpanded: true }; return Cli.of({ startPlanningSession: (label, detail, title) => startPlanningSession(cli, label, detail, title), approvePlan: (plan, options) => approvePlan(cli, plan, options), displayPlan: (plan, options) => displayPlan(cli, plan, options), startApplySession: (plan, options) => startApplySession(cli, plan, options, preferences), }); }), ); const approvePlan = Effect.fn(function*
( cli: Interaction["Service"], plan: P, options?: import("../../../Report.ts").PlanDisplayOptions, ) { // A dismissed screen means "declined"; NonInteractiveTerminal propagates // typed to the command-level handlers (a friendly line, not a raw cause). return yield* cli.prompt .screen(approvePlanScreen(plan, options?.detailed)) .pipe(Effect.catchTag("TerminalCancelled", () => Effect.succeed(false))); }); const displayPlan = Effect.fn(function*
(
cli: Interaction["Service"],
plan: P,
options?: import("../../../Report.ts").PlanDisplayOptions,
) {
const tree = new PlanTree(plan, {
detailed: options?.detailed,
mode: "review",
label: "Plan",
viewport: "full",
});
yield* cli.output.print( (
cli: Interaction["Service"],
plan: P,
options: import("../../../Report.ts").PlanDisplayOptions | undefined,
preferences: SessionPreferences,
) {
// Detailed applies render their YAML diffs inline in the progress tree.
// One-shot operations persist the final render; dev keeps its live block
// mounted so logs remain static above the collapsible plan and keybar.
const labels = plan.destroy
? {
active: "Destroying stack",
success: "Stack destroyed",
failure: "Destroy failed",
}
: plan.defaultMode === "local"
? {
active: "Starting dev stack",
success: "Dev stack ready",
failure: "Dev startup failed",
}
: {
active: "Deploying stack",
success: "Stack deployed",
failure: "Deploy failed",
};
const progress = new PlanTree(plan, {
detailed: options?.detailed,
mode: "apply",
label: labels.active,
titleDetail: options?.stage,
busy: true,
expanded: preferences.planExpanded,
});
// Remember the collapse toggle as it happens, so the next hot-reload
// generation opens the way the user left this one.
progress.subscribe(() => {
preferences.planExpanded = progress.snapshot().expanded;
});
// The session outlives this effect — the caller settles it via `done` on
// every exit path (Apply.ts's onExit). live.open is Scope-bound, so give
// it a manually managed scope. One-shot operations close it in `done`;
// dev retains it after `done` and tears it down through `close` when the
// generation is interrupted (a reload or Ctrl+C), so a replaced
// generation never leaves its widget behind.
const scope = yield* Scope.make();
const live = yield* cli
.live(