import { BOAT_CLOUD_REQUIRED_SCOPES, BOAT_CLOUD_SETUP_STEPS, type BoatCloudEnvironmentAccess, type BoatCloudMachine, type BoatCloudSetup, type BoatCloudSetupStep, type BoatCloudStatus, type EnvironmentId, } from "@t3tools/contracts"; import { CheckIcon, CircleDotIcon, CircleIcon, XIcon } from "lucide-react"; import { type FormEvent, useState } from "react"; import { useBoatCloudKeyHolderStates, usePrimaryBoatCloudStatus } from "../../boatCloud"; import { type EnvironmentPresentation, useEnvironments } from "../../state/environments"; import { serverEnvironment } from "../../state/server"; import { useAtomCommand } from "../../state/use-atom-command"; import { BoatIcon } from "../Icons"; import { Badge } from "../ui/badge"; import { Button } from "../ui/button"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { Switch } from "../ui/switch"; import { Text } from "../ui/text"; import { SecretInput } from "./SecretInput"; import { SettingsNote, SettingsPageContainer, SettingsRow, SettingsRowTile, SettingsSection, } from "./settingsLayout"; import { searchableSetting } from "./settingsSearch"; const STEP_LABELS: Record = { "create-vm": "Create a VM", "install-server": "Install T3 Code server", "copy-skills": "Copy this machine's skills", "save-snapshot": "Save the T3 Code snapshot", }; const MACHINE_STATE: Record< BoatCloudMachine["state"], { readonly label: string; readonly variant: "success" | "info" | "unavailable" | "warning" } > = { running: { label: "Running", variant: "success" }, starting: { label: "Starting", variant: "info" }, stopped: { label: "Stopped", variant: "unavailable" }, gone: { label: "Saved copy", variant: "warning" }, }; const ACCESS_LABELS: Record = { passGithub: "GitHub access", passSandboxCredentials: "Boat credentials", passSecrets: "Secrets", passAgentsCredentials: "Agent sign-ins", }; const ACCESS_KEYS = Object.keys(ACCESS_LABELS) as ReadonlyArray; type StepState = "done" | "active" | "failed" | "pending"; function stepState(setup: BoatCloudSetup, index: number): StepState { if (setup.status === "ready") return "done"; if (setup.step === null || setup.status === "idle") return "pending"; const current = BOAT_CLOUD_SETUP_STEPS.indexOf(setup.step); if (index < current) return "done"; if (index > current) return "pending"; return setup.status === "failed" ? "failed" : "active"; } // Static glyphs: setup takes minutes, and a spinner would repaint the whole time. function StepIcon({ state }: { readonly state: StepState }) { if (state === "done") return ; if (state === "failed") return ; if (state === "active") return ; return ; } /** Machines that hand out VMs: every connected one except the VMs themselves. */ function useKeyHolders(vmEnvironmentIds: ReadonlySet) { const { environments } = useEnvironments(); return environments.filter( (environment) => environment.connection.phase === "connected" && !environment.boatCloud && !vmEnvironmentIds.has(environment.environmentId), ); } function KeyHolderRow({ environment, state, }: { readonly environment: EnvironmentPresentation; readonly state: BoatCloudStatus | "unsupported" | null | undefined; }) { return ( Needs update ) : state?.connected ? ( Enabled ) : ( Not enabled ) } /> ); } function ApiKeyForm({ holders, setupEnvironmentId, }: { readonly holders: ReadonlyArray; readonly setupEnvironmentId: EnvironmentId | null; }) { const [apiKey, setApiKey] = useState(""); const [busy, setBusy] = useState(false); const connect = useAtomCommand(serverEnvironment.connectBoatCloud, "Connect Boat"); const runSetup = useAtomCommand(serverEnvironment.runBoatCloudSetup, "Set up Boat cloud"); const onSubmit = async (event: FormEvent) => { event.preventDefault(); const key = apiKey.trim(); if (!key || busy) return; setBusy(true); // Every machine gets the key, so a thread started on any of them can use Boat cloud. const results = await Promise.all( holders.map(async (holder) => ({ environmentId: holder.environmentId, result: await connect({ environmentId: holder.environmentId, input: { apiKey: key } }), })), ); setBusy(false); if (results.some(({ result }) => result._tag === "Success")) setApiKey(""); // Only this machine builds the snapshot, so connected machines never race to save it. const own = results.find(({ environmentId }) => environmentId === setupEnvironmentId); if (own?.result._tag === "Success" && own.result.value.setup.status === "idle") { void runSetup({ environmentId: own.environmentId, input: {} }); } }; return (
setApiKey(event.target.value)} /> ); } /** The Boat environment new VMs start on, and what it passes to them. Every key holder gets the same choice. */ function EnvironmentSection({ status, holders, }: { readonly status: BoatCloudStatus; readonly holders: ReadonlyArray; }) { const configure = useAtomCommand( serverEnvironment.configureBoatCloudEnvironment, "Change the Boat environment", ); const [busy, setBusy] = useState(false); const selected = status.environments.find( (environment) => environment.name === status.environment, ); const apply = async (environmentId: string, access?: BoatCloudEnvironmentAccess) => { setBusy(true); await Promise.all( holders.map((holder) => configure({ environmentId: holder.environmentId, input: { environmentId, ...(access ? { access } : {}) }, }), ), ); setBusy(false); }; return ( {status.environmentsError !== null ? ( {status.environmentsError} ) : ( <> { if (typeof value === "string" && value !== selected?.id) void apply(value); }} > {selected?.name ?? "Choose"} {status.environments.map((environment) => ( {environment.name} {environment.isDefault ? " (default)" : ""} ))} } /> {selected ? ACCESS_KEYS.map((key) => ( void apply(selected.id, { passGithub: selected.passGithub, passSandboxCredentials: selected.passSandboxCredentials, passSecrets: selected.passSecrets, passAgentsCredentials: selected.passAgentsCredentials, [key]: Boolean(checked), }) } /> } /> )) : null} New VMs start on this environment and keep it. Running VMs keep theirs. )} ); } export function BoatCloudSettingsPanel() { const { holderEnvironmentId, status } = usePrimaryBoatCloudStatus(); const vmEnvironmentIds = new Set( (status?.machines ?? []).flatMap((machine) => machine.environmentId === null ? [] : [machine.environmentId], ), ); const holderStates = useBoatCloudKeyHolderStates(); const machines = useKeyHolders(vmEnvironmentIds); // A machine whose T3 Code predates Boat cloud cannot take the key until it updates. const holders = machines.filter((machine) => { const state = holderStates.get(machine.environmentId); return state !== undefined && state !== "unsupported"; }); const missingKey = holders.filter((holder) => { const state = holderStates.get(holder.environmentId); return state !== "unsupported" && state?.connected === false; }); const runSetup = useAtomCommand(serverEnvironment.runBoatCloudSetup, "Set up Boat cloud"); const disconnect = useAtomCommand(serverEnvironment.disconnectBoatCloud, "Disconnect Boat"); const connected = status?.connected === true; const setup = status?.setup; return ( } > {status.account ?? "Connected"} ) : ( "API key" ) } description={connected ? "Connected" : undefined} leading={ } control={ connected && holderEnvironmentId !== null ? ( ) : status ? ( ) : null } /> {status && !connected ? ( The key needs{" "} {BOAT_CLOUD_REQUIRED_SCOPES.map((scope, index) => ( {scope} {index < BOAT_CLOUD_REQUIRED_SCOPES.length - 1 ? ", " : "."} ))} ) : null} {connected ? : null} {connected && setup ? ( { if (holderEnvironmentId === null) return; void runSetup({ environmentId: holderEnvironmentId, input: {} }); }} > {setup.status === "running" ? "Setting up" : setup.status === "ready" ? "Rebuild snapshot" : setup.status === "failed" ? "Retry" : "Set up"} } > {BOAT_CLOUD_SETUP_STEPS.map((step, index) => { const state = stepState(setup, index); return ( } description={state === "failed" ? (setup.error ?? undefined) : undefined} /> ); })} ) : null} {connected ? ( {machines.map((machine) => ( ))} {missingKey.length > 0 ? ( } /> ) : null} ) : null} {connected && status.machines.length > 0 ? ( {status.machines.map((machine) => { const state = MACHINE_STATE[machine.state]; return ( 0 ? machine.repositories .map((repository) => repository.split("/").slice(1).join("/")) .join(", ") : undefined } control={{state.label}} /> ); })} ) : null} ); }