495 lines
18 KiB
TypeScript
495 lines
18 KiB
TypeScript
import { openExternalUrl, type PluginSurfaceProps, useRpc, useSettings } from "@getpaseo/plugin/client";
|
|
import {
|
|
ScrollView,
|
|
SettingsAction,
|
|
SettingsCard,
|
|
SettingsInput,
|
|
SettingsRow,
|
|
SettingsSection,
|
|
SettingsSelect,
|
|
SettingsSwitch,
|
|
useToast,
|
|
} from "@getpaseo/plugin/client/ui";
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { Text, View } from "react-native";
|
|
import { testConnectionRpc } from "../shared/contracts";
|
|
import {
|
|
defaultFlySpritesSettings,
|
|
flySpritesSettings,
|
|
type FlySpritesSettings,
|
|
} from "../shared/settings";
|
|
import { useOpenBaoAuth } from "./use-openbao-auth";
|
|
|
|
const MODE_OPTIONS: readonly { label: string; value: FlySpritesSettings["defaultProviderMode"] }[] = [
|
|
{ label: "Remote Shell Worker (bash -lc)", value: "shell" },
|
|
{ label: "Remote ACP Agent (over WebSocket stdio)", value: "acp" },
|
|
];
|
|
|
|
export function FlySpritesSettingsScreen({ theme, layout, host }: PluginSurfaceProps) {
|
|
const settings = useSettings(flySpritesSettings);
|
|
const testConnection = useRpc(testConnectionRpc);
|
|
const toast = useToast();
|
|
const openbao = useOpenBaoAuth();
|
|
|
|
const [draft, setDraft] = useState<FlySpritesSettings>(defaultFlySpritesSettings);
|
|
const [testing, setTesting] = useState(false);
|
|
const [statusMessage, setStatusMessage] = useState<string | null>(null);
|
|
|
|
const readyRevision = settings.status === "ready" ? settings.revision : null;
|
|
const readyValues = settings.status === "ready" ? settings.values : null;
|
|
|
|
useEffect(() => {
|
|
if (readyValues) {
|
|
setDraft(readyValues);
|
|
}
|
|
}, [readyRevision, readyValues]);
|
|
|
|
const styles = useMemo(
|
|
() => ({
|
|
screen: { flex: 1, backgroundColor: theme.colors.surface0 },
|
|
body: { padding: layout.compact ? 16 : 24, gap: 16 },
|
|
muted: { color: theme.colors.foregroundMuted, fontSize: 13 },
|
|
error: { color: theme.colors.statusDanger, fontSize: 13 },
|
|
success: { color: theme.colors.statusSuccess, fontSize: 13 },
|
|
}),
|
|
[theme, layout.compact],
|
|
);
|
|
|
|
if (settings.status === "loading") {
|
|
return (
|
|
<View style={[styles.screen, styles.body]}>
|
|
<Text style={styles.muted}>Loading Fly Sprites settings for {host.label}…</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (settings.status === "error") {
|
|
return (
|
|
<View style={[styles.screen, styles.body]}>
|
|
<Text style={styles.error}>Could not load settings: {settings.error}</Text>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const isInvalid = settings.status === "invalid";
|
|
const revision = settings.revision;
|
|
const isDirty =
|
|
settings.status === "ready" &&
|
|
JSON.stringify(draft) !== JSON.stringify(settings.values);
|
|
|
|
async function saveDraft(next: FlySpritesSettings) {
|
|
setStatusMessage(null);
|
|
const ok = await settings.save(next, revision);
|
|
if (ok) {
|
|
setStatusMessage("Saved.");
|
|
}
|
|
}
|
|
|
|
async function handleReset() {
|
|
setStatusMessage(null);
|
|
const ok = await settings.reset();
|
|
if (ok) {
|
|
setDraft(defaultFlySpritesSettings);
|
|
setStatusMessage("Reset to defaults.");
|
|
}
|
|
}
|
|
|
|
async function handleTest() {
|
|
setTesting(true);
|
|
setStatusMessage(null);
|
|
try {
|
|
if (isDirty) {
|
|
await settings.save(draft, revision);
|
|
}
|
|
const res = await testConnection({});
|
|
if (res.ok) {
|
|
const msg = `Connected via ${res.source} token (${res.spriteCount} sprites).`;
|
|
setStatusMessage(msg);
|
|
toast.show(msg, { variant: "success" });
|
|
} else {
|
|
setStatusMessage(res.error);
|
|
toast.error(res.error);
|
|
}
|
|
} catch (error) {
|
|
const msg = error instanceof Error ? error.message : String(error);
|
|
setStatusMessage(msg);
|
|
toast.error(msg);
|
|
} finally {
|
|
setTesting(false);
|
|
}
|
|
}
|
|
|
|
async function handleStartZitadel() {
|
|
if (isDirty) {
|
|
await settings.save(draft, revision);
|
|
}
|
|
await openbao.startLogin();
|
|
}
|
|
|
|
async function handlePullOpenBao() {
|
|
if (isDirty) {
|
|
await settings.save(draft, revision);
|
|
}
|
|
await openbao.pullNow();
|
|
}
|
|
|
|
const baoStatus = openbao.status;
|
|
const pendingFlow = baoStatus?.pendingAuth?.status === "pending" ? baoStatus.pendingAuth : null;
|
|
const baoSummary = baoStatus?.authenticated
|
|
? `Signed in as ${baoStatus.displayName ?? "Zitadel user"}${
|
|
baoStatus.tokenPresent ? " · Token loaded" : " · Token not loaded"
|
|
}`
|
|
: "Not signed in";
|
|
|
|
return (
|
|
<ScrollView style={styles.screen} contentContainerStyle={styles.body}>
|
|
<SettingsSection
|
|
title="Fly Sprites authentication"
|
|
info={
|
|
<Text style={styles.muted}>
|
|
Host settings for {host.label}. Leave Sprites API token blank to pull from OpenBao or host env.
|
|
</Text>
|
|
}
|
|
>
|
|
{isInvalid ? (
|
|
<SettingsCard>
|
|
<SettingsRow label="Invalid saved settings" error={settings.error} />
|
|
</SettingsCard>
|
|
) : null}
|
|
|
|
<SettingsCard key={`auth-${revision}`}>
|
|
<SettingsInput
|
|
label="Sprites API token"
|
|
hint="Optional direct override. Leave blank to pull FLY_TOKEN from OpenBao."
|
|
initialValue={draft.token}
|
|
placeholder="Leave blank to use OpenBao or SPRITES_TOKEN"
|
|
secureTextEntry
|
|
disabled={settings.saving}
|
|
onChangeText={(token) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, token }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="API base URL"
|
|
hint="Default: https://api.sprites.dev"
|
|
initialValue={draft.baseUrl}
|
|
placeholder="https://api.sprites.dev"
|
|
disabled={settings.saving}
|
|
onChangeText={(baseUrl) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, baseUrl }));
|
|
}}
|
|
/>
|
|
<SettingsAction
|
|
label="Test Fly Sprites connection"
|
|
hint="Verifies token access against the Sprites API."
|
|
actionLabel={testing ? "Testing…" : "Test connection"}
|
|
disabled={testing || settings.saving}
|
|
onPress={() => void handleTest()}
|
|
/>
|
|
</SettingsCard>
|
|
</SettingsSection>
|
|
|
|
<SettingsSection
|
|
title="OpenBao + Zitadel OIDC"
|
|
info={
|
|
<Text style={styles.muted}>
|
|
Pulls {draft.openbaoSecretKey || "FLY_TOKEN"} from {draft.openbaoKvMount || "secret"}/
|
|
{draft.openbaoSecretPath || "paseo"} via Zitadel Device Code auth.
|
|
</Text>
|
|
}
|
|
>
|
|
<SettingsCard key={`openbao-${revision}`}>
|
|
<SettingsSwitch
|
|
label="Pull token from OpenBao"
|
|
hint="Uses your Zitadel OpenBao session when direct token is blank."
|
|
value={draft.openbaoEnabled}
|
|
disabled={settings.saving}
|
|
onValueChange={(openbaoEnabled) => {
|
|
const next = { ...draft, openbaoEnabled };
|
|
setDraft(next);
|
|
void saveDraft(next);
|
|
}}
|
|
/>
|
|
<SettingsRow
|
|
label="OpenBao session"
|
|
hint={baoStatus?.lastError ?? baoSummary}
|
|
error={baoStatus?.lastError ?? undefined}
|
|
/>
|
|
{pendingFlow ? (
|
|
<SettingsAction
|
|
label={`Waiting for Zitadel approval (Code: ${pendingFlow.userCode})`}
|
|
hint={pendingFlow.verificationUriComplete || pendingFlow.verificationUri}
|
|
actionLabel="Open Zitadel"
|
|
onPress={() =>
|
|
void openExternalUrl(pendingFlow.verificationUriComplete || pendingFlow.verificationUri)
|
|
}
|
|
/>
|
|
) : null}
|
|
<SettingsAction
|
|
label="Zitadel OIDC sign-in"
|
|
hint="Opens Zitadel Device Code authorization in your browser and exchanges JWT with OpenBao."
|
|
actionLabel={
|
|
openbao.busy
|
|
? "Working…"
|
|
: baoStatus?.authenticated
|
|
? "Re-authenticate"
|
|
: "Sign in with Zitadel"
|
|
}
|
|
disabled={openbao.busy || settings.saving || !draft.openbaoEnabled}
|
|
onPress={() => void handleStartZitadel()}
|
|
/>
|
|
{baoStatus?.authenticated ? (
|
|
<>
|
|
<SettingsAction
|
|
label="Refresh token from OpenBao"
|
|
hint={`Reads ${draft.openbaoKvMount}/${draft.openbaoSecretPath} (${draft.openbaoSecretKey}).`}
|
|
actionLabel="Pull now"
|
|
disabled={openbao.busy || settings.saving || !draft.openbaoEnabled}
|
|
onPress={() => void handlePullOpenBao()}
|
|
/>
|
|
<SettingsAction
|
|
label="Sign out of OpenBao"
|
|
hint="Clears saved OpenBao token for this host."
|
|
actionLabel="Sign out"
|
|
disabled={openbao.busy}
|
|
onPress={() => void openbao.logout()}
|
|
/>
|
|
</>
|
|
) : null}
|
|
<SettingsInput
|
|
label="OpenBao URL"
|
|
initialValue={draft.openbaoAddr}
|
|
placeholder="https://bao.site.latha.org"
|
|
disabled={settings.saving}
|
|
onChangeText={(openbaoAddr) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, openbaoAddr }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="OpenBao KV mount"
|
|
initialValue={draft.openbaoKvMount}
|
|
placeholder="secret"
|
|
disabled={settings.saving}
|
|
onChangeText={(openbaoKvMount) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, openbaoKvMount }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="OpenBao secret path"
|
|
initialValue={draft.openbaoSecretPath}
|
|
placeholder="paseo"
|
|
disabled={settings.saving}
|
|
onChangeText={(openbaoSecretPath) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, openbaoSecretPath }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="OpenBao secret key"
|
|
initialValue={draft.openbaoSecretKey}
|
|
placeholder="FLY_TOKEN"
|
|
disabled={settings.saving}
|
|
onChangeText={(openbaoSecretKey) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, openbaoSecretKey }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="OpenBao auth mount & role"
|
|
hint={`Mount: ${draft.openbaoAuthMount} · Role: ${draft.openbaoRole}`}
|
|
initialValue={`${draft.openbaoAuthMount}:${draft.openbaoRole}`}
|
|
placeholder="jwt:admin"
|
|
disabled={settings.saving}
|
|
onChangeText={(val) => {
|
|
const [mount, role] = val.split(":");
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({
|
|
...prev,
|
|
openbaoAuthMount: (mount ?? "jwt").trim() || "jwt",
|
|
openbaoRole: (role ?? "admin").trim() || "admin",
|
|
}));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Zitadel issuer URL"
|
|
initialValue={draft.zitadelIssuer}
|
|
placeholder="https://auth.site.latha.org"
|
|
disabled={settings.saving}
|
|
onChangeText={(zitadelIssuer) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, zitadelIssuer }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Zitadel client ID"
|
|
initialValue={draft.zitadelClientId}
|
|
placeholder="400222693598691513"
|
|
disabled={settings.saving}
|
|
onChangeText={(zitadelClientId) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, zitadelClientId }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Zitadel scope"
|
|
initialValue={draft.zitadelScope}
|
|
placeholder="openid profile email"
|
|
disabled={settings.saving}
|
|
onChangeText={(zitadelScope) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, zitadelScope }));
|
|
}}
|
|
/>
|
|
</SettingsCard>
|
|
</SettingsSection>
|
|
|
|
<SettingsSection title="Sprite defaults & provider">
|
|
<SettingsCard key={`defaults-${revision}`}>
|
|
<SettingsSelect
|
|
label="Default provider mode"
|
|
hint="How the Fly Sprites provider executes prompts when creating a new agent."
|
|
value={draft.defaultProviderMode}
|
|
disabled={settings.saving}
|
|
options={MODE_OPTIONS}
|
|
onValueChange={(defaultProviderMode) => {
|
|
const next = { ...draft, defaultProviderMode };
|
|
setDraft(next);
|
|
void saveDraft(next);
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Remote ACP command"
|
|
hint="Executed inside the Sprite when provider mode is ACP."
|
|
initialValue={draft.acpCommand}
|
|
placeholder="claude-code-acp"
|
|
disabled={settings.saving}
|
|
onChangeText={(acpCommand) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, acpCommand }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Sprite name prefix"
|
|
hint="Prefix for auto-generated worker sprites."
|
|
initialValue={draft.spritePrefix}
|
|
placeholder="paseo-worker-"
|
|
disabled={settings.saving}
|
|
onChangeText={(spritePrefix) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, spritePrefix }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Default remote directory"
|
|
hint="Working directory inside Sprites."
|
|
initialValue={draft.defaultRemoteCwd}
|
|
placeholder="/workspace"
|
|
disabled={settings.saving}
|
|
onChangeText={(defaultRemoteCwd) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, defaultRemoteCwd }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Default region"
|
|
hint="Optional Fly region slug (blank uses API default)."
|
|
initialValue={draft.defaultRegion}
|
|
placeholder="iad"
|
|
disabled={settings.saving}
|
|
onChangeText={(defaultRegion) => {
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, defaultRegion }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Default RAM (MB)"
|
|
hint="0 uses Fly Sprites default sizing."
|
|
initialValue={String(draft.defaultRamMB)}
|
|
placeholder="0"
|
|
disabled={settings.saving}
|
|
onChangeText={(val) => {
|
|
const parsed = Number.parseInt(val.trim() || "0", 10);
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, defaultRamMB: Number.isFinite(parsed) && parsed >= 0 ? parsed : 0 }));
|
|
}}
|
|
/>
|
|
<SettingsInput
|
|
label="Default CPUs"
|
|
hint="0 uses Fly Sprites default sizing."
|
|
initialValue={String(draft.defaultCpus)}
|
|
placeholder="0"
|
|
disabled={settings.saving}
|
|
onChangeText={(val) => {
|
|
const parsed = Number.parseInt(val.trim() || "0", 10);
|
|
setStatusMessage(null);
|
|
setDraft((prev) => ({ ...prev, defaultCpus: Number.isFinite(parsed) && parsed >= 0 ? parsed : 0 }));
|
|
}}
|
|
/>
|
|
</SettingsCard>
|
|
</SettingsSection>
|
|
|
|
<SettingsSection title="Integration behavior">
|
|
<SettingsCard key={`behavior-${revision}`}>
|
|
<SettingsSwitch
|
|
label="Inject sprite_* tools into Paseo agents"
|
|
hint="Allows local agents to spawn parallel Fly Sprite workers and checkpoints."
|
|
value={draft.injectAgentTools}
|
|
disabled={settings.saving}
|
|
onValueChange={(injectAgentTools) => {
|
|
const next = { ...draft, injectAgentTools };
|
|
setDraft(next);
|
|
void saveDraft(next);
|
|
}}
|
|
/>
|
|
<SettingsSwitch
|
|
label="Sync local workspace on provider open"
|
|
hint="Uploads workspace files to /workspace when a Fly Sprites session starts."
|
|
value={draft.syncWorkspaceOnCreate}
|
|
disabled={settings.saving}
|
|
onValueChange={(syncWorkspaceOnCreate) => {
|
|
const next = { ...draft, syncWorkspaceOnCreate };
|
|
setDraft(next);
|
|
void saveDraft(next);
|
|
}}
|
|
/>
|
|
<SettingsSwitch
|
|
label="Delete ephemeral sprites on archive"
|
|
hint="Deletes the Sprite created by a Fly Sprites agent when that agent is archived."
|
|
value={draft.autoCleanupOnArchive}
|
|
disabled={settings.saving}
|
|
onValueChange={(autoCleanupOnArchive) => {
|
|
const next = { ...draft, autoCleanupOnArchive };
|
|
setDraft(next);
|
|
void saveDraft(next);
|
|
}}
|
|
/>
|
|
</SettingsCard>
|
|
|
|
<SettingsCard>
|
|
<SettingsAction
|
|
label="Save settings"
|
|
hint={isDirty ? "You have unsaved text or numeric changes." : "All text changes saved."}
|
|
actionLabel={settings.saving ? "Saving…" : "Save"}
|
|
disabled={settings.saving || (!isDirty && !isInvalid)}
|
|
onPress={() => void saveDraft(draft)}
|
|
/>
|
|
<SettingsAction
|
|
label="Reset settings"
|
|
hint="Restore default Fly Sprites configuration."
|
|
actionLabel="Reset"
|
|
disabled={settings.saving}
|
|
onPress={() => void handleReset()}
|
|
/>
|
|
</SettingsCard>
|
|
|
|
{settings.saveError ? <Text style={styles.error}>{settings.saveError}</Text> : null}
|
|
{statusMessage ? <Text style={styles.success}>{statusMessage}</Text> : null}
|
|
</SettingsSection>
|
|
</ScrollView>
|
|
);
|
|
}
|