Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom } from "effect/reactivity";
4
5import { environmentThreadShells } from "~/state/threads";
6import {
7 AlertDialog,
8 AlertDialogClose,
9 AlertDialogDescription,
10 AlertDialogFooter,
11 AlertDialogHeader,
12 AlertDialogPopup,
13 AlertDialogTitle,
14} from "../ui/alert-dialog";
15import { Button } from "../ui/button";
16import type { RestartingMachine } from "./updateEverything.logic";
17
18/** A thread an agent is working on now. A restart stops these; queued threads wait. */
19const isRunning = (status: string) =>
20 status === "preparing" || status === "starting" || status === "running";
21
22/** Running threads across the environments in a newline-joined key. */
23const runningThreadCountAtom = Atom.family((environmentIds: string) =>
24 Atom.make((get) =>
25 environmentIds
26 .split("\n")
27 .filter((id) => id !== "")
28 .reduce(
29 (count, id) =>
30 count +
31 get(environmentThreadShells.environmentThreadsAtom(EnvironmentId.make(id))).filter(
32 (thread) => isRunning(thread.status),
33 ).length,
34 0,
35 ),
36 ).pipe(Atom.withLabel(`update-everything-running-threads:${environmentIds}`)),
37);
38
39interface UpdateEverythingDialogProps {
40 readonly open: boolean;
41 readonly onOpenChange: (open: boolean) => void;
42 readonly summary: string;
43 readonly lines: ReadonlyArray<string>;
44 readonly restarts: ReadonlyArray<RestartingMachine>;
45 readonly onConfirm: () => void;
46}
47
48/**
49 * Confirms an "update everything" run. It counts the running threads that the
50 * restarts stop, and how many can resume afterwards because "Continue threads
51 * after restarts" is on for their server. Recovery still skips some, such as a
52 * thread waiting on the user, so the text says "can resume", not "will".
53 */
54export function UpdateEverythingDialog({
55 open,
56 onOpenChange,
57 ...content
58}: UpdateEverythingDialogProps) {
59 return (
60 <AlertDialog open={open} onOpenChange={onOpenChange}>
61 <AlertDialogPopup>
62 {/* The popup unmounts when closed, so the thread count is read only while open. */}
63 <UpdateEverythingDialogContent {...content} />
64 </AlertDialogPopup>
65 </AlertDialog>
66 );
67}
68
69function UpdateEverythingDialogContent({
70 summary,
71 lines,
72 restarts,
73 onConfirm,
74}: Omit<UpdateEverythingDialogProps, "open" | "onOpenChange">) {
75 const stopped = useAtomValue(
76 runningThreadCountAtom(
77 restarts
78 .filter((machine) => !machine.continuesThreads)
79 .map((machine) => machine.environmentId)
80 .join("\n"),
81 ),
82 );
83 const continuing = useAtomValue(
84 runningThreadCountAtom(
85 restarts
86 .filter((machine) => machine.continuesThreads)
87 .map((machine) => machine.environmentId)
88 .join("\n"),
89 ),
90 );
91 const running = stopped + continuing;
92 const threads = `${running} running ${running === 1 ? "thread" : "threads"}`;
93
94 return (
95 <>
96 <AlertDialogHeader>
97 <AlertDialogTitle>{summary}?</AlertDialogTitle>
98 <AlertDialogDescription className="whitespace-pre-line">
99 {lines.join("\n")}
100 </AlertDialogDescription>
101 {running > 0 ? (
102 <p className="pt-2 text-sm">
103 {stopped === 0
104 ? `${threads} will be interrupted and can resume after the restart.`
105 : continuing === 0
106 ? `${threads} will be interrupted.`
107 : `${threads} will be interrupted. ${continuing} can resume after the restart.`}
108 </p>
109 ) : null}
110 </AlertDialogHeader>
111 <AlertDialogFooter>
112 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
113 <Button onClick={onConfirm}>Update</Button>
114 </AlertDialogFooter>
115 </>
116 );
117}