import { DOWNLOAD_TERMS } from "./consent";
import { useSettings, type PluginSurfaceProps } from "@getpaseo/plugin/client";
import {
SettingsAction,
SettingsCard,
SettingsInput,
SettingsRow,
SettingsSection,
SettingsSelect,
SettingsSwitch,
} from "@getpaseo/plugin/client/ui";
import { useState } from "react";
import { Text, View } from "react-native";
import { AGENTS, AGENT_IDS, type AgentId } from "../shared/agents";
import { sandboxSettings, type IsolationMode, type SandboxSettings } from "../shared/settings";
import { STARSHIP_DOWNLOAD_MB, STARSHIP_VERSION } from "../shared/starship";
import { invalidEnvNames, invalidMounts, parseList, parsePortList } from "../shared/spec";
import { useStarshipStatus, useTemplates } from "./queries";
interface Draft {
enabledAgents: AgentId[];
allowImageDownloads: boolean;
routeClaudeAgents: boolean;
gsdPath: boolean;
isolation: IsolationMode;
memoryPercent: string;
memoryFloorGiB: string;
cpus: string;
dockerDiskGiB: string;
idleHoldHours: string;
githubHttps: boolean;
starship: boolean;
psiPercent: string;
availablePercent: string;
diskAvailablePercent: string;
template: string;
mounts: string;
claudeShared: string;
envFile: string;
envNames: string;
networkAllow: string;
forwardPorts: string;
excludedPaths: string;
}
function starshipHint(status: { version: string | null; installing: boolean } | undefined): string {
const about =
`The Sandbox terminal's prompt, from Starship ${STARSHIP_VERSION}. Turning it on downloads it from GitHub ` +
`(about ${STARSHIP_DOWNLOAD_MB} MB), checked against its pinned SHA-256, into the plugin's folder every sandbox ` +
"mounts; it runs only in the VMs. Your ~/.config/starship.toml is used when you have one, otherwise a setup " +
"without Nerd Font icons.";
if (status?.installing) return `${about} Downloading…`;
return status?.version ? `${about} Installed: Starship ${status.version}.` : about;
}
function toDraft(values: SandboxSettings): Draft {
return {
enabledAgents: values.enabledAgents,
allowImageDownloads: values.allowImageDownloads,
routeClaudeAgents: values.routeClaudeAgents,
gsdPath: values.gsdPath,
isolation: values.isolation,
memoryPercent: String(values.memoryPercent),
memoryFloorGiB: String(values.memoryFloorGiB),
cpus: String(values.cpus),
dockerDiskGiB: String(values.dockerDiskGiB),
idleHoldHours: String(values.idleHoldHours),
githubHttps: values.githubHttps,
starship: values.starship,
psiPercent: String(values.memoryAlert.psiPercent),
availablePercent: String(values.memoryAlert.availablePercent),
diskAvailablePercent: String(values.diskAlert.availablePercent),
template: values.template,
// The host's input is single-line: lists are comma-separated.
mounts: values.mounts.join(", "),
claudeShared: values.claudeShared.join(", "),
envFile: values.envFile,
envNames: values.envNames.join(", "),
networkAllow: values.networkAllow.join(", "),
forwardPorts: values.forwardPorts.join(", "),
excludedPaths: values.excludedPaths.join(", "),
};
}
function fromDraft(draft: Draft, base: SandboxSettings): SandboxSettings | string {
const memoryPercent = Number(draft.memoryPercent);
const memoryFloorGiB = Number(draft.memoryFloorGiB);
const cpus = Number(draft.cpus);
if (!(memoryPercent >= 1 && memoryPercent <= 90)) return "Memory percent must be between 1 and 90.";
if (!(memoryFloorGiB >= 1)) return "Memory floor must be at least 1 GiB.";
if (!(Number.isInteger(cpus) && cpus >= 0)) return "CPUs must be a whole number (0 = all).";
const dockerDiskGiB = Number(draft.dockerDiskGiB);
if (!(Number.isInteger(dockerDiskGiB) && dockerDiskGiB >= 0)) return "Docker disk must be a whole number of GiB (0 = sbx's).";
const idleHoldHours = Number(draft.idleHoldHours);
if (!(draft.idleHoldHours.trim() !== "" && idleHoldHours >= 0)) return "Idle agents' VMs: hours must be 0 or more (0 = until archived).";
const psiPercent = Number(draft.psiPercent);
const availablePercent = Number(draft.availablePercent);
if (!(psiPercent >= 1 && psiPercent <= 100)) return "The pressure threshold must be between 1 and 100.";
if (!(availablePercent >= 1 && availablePercent <= 90)) return "The available RAM threshold must be between 1 and 90.";
const diskAvailablePercent = Number(draft.diskAvailablePercent);
if (!(diskAvailablePercent >= 1 && diskAvailablePercent <= 90)) return "The free disk threshold must be between 1 and 90.";
const badMounts = invalidMounts(parseList(draft.mounts));
if (badMounts.length > 0) return `Mounts must be absolute or ~/ paths: ${badMounts.join(", ")}`;
const badNames = invalidEnvNames(parseList(draft.envNames));
if (badNames.length > 0) return `Not variables you can forward: ${badNames.join(", ")}`;
return {
...base,
enabledAgents: draft.enabledAgents,
allowImageDownloads: draft.allowImageDownloads,
routeClaudeAgents: draft.routeClaudeAgents,
gsdPath: draft.gsdPath,
isolation: draft.isolation,
memoryPercent,
memoryFloorGiB,
cpus,
dockerDiskGiB,
idleHoldHours,
githubHttps: draft.githubHttps,
starship: draft.starship,
memoryAlert: { psiPercent, availablePercent },
diskAlert: { availablePercent: diskAvailablePercent },
template: draft.template,
mounts: parseList(draft.mounts),
claudeShared: parseList(draft.claudeShared),
envFile: draft.envFile.trim(),
envNames: parseList(draft.envNames),
networkAllow: parseList(draft.networkAllow),
forwardPorts: parsePortList(draft.forwardPorts),
excludedPaths: parseList(draft.excludedPaths),
};
}
export function SettingsScreen({ theme }: PluginSurfaceProps) {
const settings = useSettings(sandboxSettings);
if (settings.status === "loading") return null;
if (settings.status !== "ready") {
return (
{settings.error}
{settings.status === "invalid" ? (
void settings.reset()} />
) : null}
);
}
return ;
}
type ReadySettings = Extract>, { status: "ready" }>;
function SettingsForm({ settings, theme }: { settings: ReadySettings; theme: PluginSurfaceProps["theme"] }) {
const [draft, setDraft] = useState(() => toDraft(settings.values));
const templates = useTemplates();
const starship = useStarshipStatus(draft.starship);
const templateOptions = [
{ label: "sbx image", value: "" },
...(templates.data?.templates ?? [])
.filter((template) => !template.base)
.map((template) => ({ label: template.ref.replace(/^docker\.io\/library\//, ""), value: template.ref })),
];
if (draft.template && !templateOptions.some((option) => option.value === draft.template)) {
templateOptions.push({ label: `${draft.template} (missing)`, value: draft.template });
}
const [error, setError] = useState(null);
const update = (key: Key) => (value: Draft[Key]) =>
setDraft((current) => ({ ...current, [key]: value }));
async function save() {
const next = fromDraft(draft, settings.values);
if (typeof next === "string") {
setError(next);
return;
}
setError(null);
await settings.save(next, settings.revision);
}
return (
{AGENT_IDS.map((agent) => (
setDraft((current) => ({
...current,
enabledAgents: on
? AGENT_IDS.filter((id) => id === agent || current.enabledAgents.includes(id))
: current.enabledAgents.filter((id) => id !== agent),
}))
}
/>
))}
void save()}
/>
{error ? {error} : null}
);
}