- Add SpriteJobManager and job RPCs (start/get/list) for connect, launch, create, and sync operations - Time-box status RPC with 5s remote timeout and lazy remote fetching - Cache remote sprites, raw token, and host/agent lists to avoid redundant API calls - Harden sprite host exec timeouts, JSON parsing, and npm install with --no-audit --no-fund - UI: poll active jobs with phase updates in workers screen - Reduce token exchange and OpenBao request timeout to 8s
1128 lines
40 KiB
TypeScript
1128 lines
40 KiB
TypeScript
import {
|
|
type PluginSurfaceProps,
|
|
openExternalUrl,
|
|
usePaseo,
|
|
useRpc,
|
|
} from "@getpaseo/plugin/client";
|
|
import type { PaseoAgent, PaseoWorkspace } from "@getpaseo/client";
|
|
import { ScrollView, TextInput, copyText, useToast } from "@getpaseo/plugin/client/ui";
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
import { Pressable, Text, View } from "react-native";
|
|
import {
|
|
type CheckpointSummary,
|
|
type SpriteAgentSummary,
|
|
type SpriteHostSummary,
|
|
type SpriteJob,
|
|
type SpriteJobKind,
|
|
type SpriteSummary,
|
|
type TokenSource,
|
|
createCheckpointRpc,
|
|
destroySpriteRpc,
|
|
getSpriteJobRpc,
|
|
listCheckpointsRpc,
|
|
listSpriteJobsRpc,
|
|
restoreCheckpointRpc,
|
|
spriteAgentActionRpc,
|
|
startConnectHostRpc,
|
|
startCreateSpriteRpc,
|
|
startLaunchAgentRpc,
|
|
startSyncWorkspaceRpc,
|
|
statusRpc,
|
|
} from "../shared/contracts";
|
|
import { useOpenBaoAuth } from "./use-openbao-auth";
|
|
|
|
interface WorkersScreenProps extends PluginSurfaceProps {
|
|
workspaceName?: string;
|
|
workspaceDirectory?: string;
|
|
}
|
|
|
|
type SyncAction =
|
|
| { kind: "checkpoint"; spriteName: string }
|
|
| { kind: "restore"; spriteName: string; checkpointId: string }
|
|
| { kind: "destroy"; spriteName: string };
|
|
|
|
const PROVIDER_CHOICES = [
|
|
{ id: "auto", label: "Auto" },
|
|
{ id: "claude", label: "Claude" },
|
|
{ id: "codex", label: "Codex" },
|
|
{ id: "opencode", label: "OpenCode" },
|
|
{ id: "pi", label: "Pi" },
|
|
] as const;
|
|
|
|
function getWorkspacePath(ws: PaseoWorkspace): string {
|
|
return ws.workspaceDirectory || ws.projectRootPath;
|
|
}
|
|
|
|
function formatJobKind(kind: SpriteJobKind): string {
|
|
switch (kind) {
|
|
case "connect_host":
|
|
return "Connect Host";
|
|
case "launch_agent":
|
|
return "Launch Agent";
|
|
case "create_sprite":
|
|
return "Create Sprite";
|
|
case "sync_workspace":
|
|
return "Sync Workspace";
|
|
}
|
|
}
|
|
|
|
function formatElapsed(startedAt: string, nowMs: number): string {
|
|
const startMs = Date.parse(startedAt);
|
|
if (!Number.isFinite(startMs)) return "";
|
|
const seconds = Math.max(0, Math.floor((nowMs - startMs) / 1000));
|
|
if (seconds < 60) return `${seconds}s`;
|
|
const mins = Math.floor(seconds / 60);
|
|
const rem = seconds % 60;
|
|
return `${mins}m ${rem}s`;
|
|
}
|
|
|
|
export function FlySpritesWorkersScreen({
|
|
theme,
|
|
layout,
|
|
host,
|
|
navigation,
|
|
workspaceName,
|
|
workspaceDirectory,
|
|
}: WorkersScreenProps) {
|
|
const paseo = usePaseo();
|
|
const toast = useToast();
|
|
|
|
const fetchStatus = useRpc(statusRpc);
|
|
const startCreateSprite = useRpc(startCreateSpriteRpc);
|
|
const startConnectHost = useRpc(startConnectHostRpc);
|
|
const startLaunchAgent = useRpc(startLaunchAgentRpc);
|
|
const startSyncWorkspace = useRpc(startSyncWorkspaceRpc);
|
|
const getSpriteJob = useRpc(getSpriteJobRpc);
|
|
const listSpriteJobs = useRpc(listSpriteJobsRpc);
|
|
const spriteAgentAction = useRpc(spriteAgentActionRpc);
|
|
const destroySprite = useRpc(destroySpriteRpc);
|
|
const listCheckpoints = useRpc(listCheckpointsRpc);
|
|
const createCheckpoint = useRpc(createCheckpointRpc);
|
|
const restoreCheckpoint = useRpc(restoreCheckpointRpc);
|
|
|
|
const [configured, setConfigured] = useState(false);
|
|
const [tokenSource, setTokenSource] = useState<TokenSource>("none");
|
|
const [remoteError, setRemoteError] = useState<string | null>(null);
|
|
const [sprites, setSprites] = useState<SpriteSummary[]>([]);
|
|
const [spriteHosts, setSpriteHosts] = useState<SpriteHostSummary[]>([]);
|
|
const [spriteAgents, setSpriteAgents] = useState<SpriteAgentSummary[]>([]);
|
|
const [legacyAgents, setLegacyAgents] = useState<PaseoAgent[]>([]);
|
|
const [workspaces, setWorkspaces] = useState<PaseoWorkspace[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [remoteLoading, setRemoteLoading] = useState(false);
|
|
const [activeJobs, setActiveJobs] = useState<SpriteJob[]>([]);
|
|
const [startingKey, setStartingKey] = useState<string | null>(null);
|
|
const [syncAction, setSyncAction] = useState<SyncAction | null>(null);
|
|
const [agentBusyId, setAgentBusyId] = useState<string | null>(null);
|
|
const [nowTick, setNowTick] = useState(() => Date.now());
|
|
|
|
const handledJobIdsRef = useRef<Set<string>>(new Set());
|
|
const autoOpenAgentJobIdsRef = useRef<Set<string>>(new Set());
|
|
const formConnectJobIdRef = useRef<string | null>(null);
|
|
|
|
const [spriteNameInput, setSpriteNameInput] = useState("");
|
|
const [agentTitleInput, setAgentTitleInput] = useState("");
|
|
const [promptInput, setPromptInput] = useState("");
|
|
const [cwdInput, setCwdInput] = useState(workspaceDirectory ?? "");
|
|
const [providerInput, setProviderInput] = useState<string>("auto");
|
|
const [syncBeforeOpen, setSyncBeforeOpen] = useState(true);
|
|
|
|
const [checkpointsSprite, setCheckpointsSprite] = useState<string | null>(null);
|
|
const [checkpoints, setCheckpoints] = useState<CheckpointSummary[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (workspaceDirectory && !cwdInput) {
|
|
setCwdInput(workspaceDirectory);
|
|
setSyncBeforeOpen(true);
|
|
}
|
|
}, [workspaceDirectory, cwdInput]);
|
|
|
|
const refreshClusterState = useCallback(async () => {
|
|
const [agentsRes, workspacesRes] = await Promise.all([
|
|
paseo.agents.list().catch(() => null),
|
|
paseo.workspaces.list().catch(() => null),
|
|
]);
|
|
if (agentsRes) {
|
|
const flyAgents = agentsRes.entries
|
|
.map((entry) => entry.agent)
|
|
.filter((agent) => agent.provider === "fly-sprites" && !agent.archivedAt)
|
|
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
setLegacyAgents(flyAgents);
|
|
}
|
|
if (workspacesRes) {
|
|
const activeWorkspaces = workspacesRes.entries.filter(
|
|
(ws) => !ws.archivingAt && !ws.background && Boolean(getWorkspacePath(ws)),
|
|
);
|
|
setWorkspaces(activeWorkspaces);
|
|
setCwdInput((cur) => {
|
|
if (cur || workspaceDirectory || activeWorkspaces.length === 0) return cur;
|
|
return getWorkspacePath(activeWorkspaces[0]);
|
|
});
|
|
}
|
|
}, [paseo, workspaceDirectory]);
|
|
|
|
const refresh = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
// Fast path: local state first so screen paints immediately.
|
|
const statusRes = await fetchStatus({ includeRemote: false });
|
|
setConfigured(statusRes.configured);
|
|
setTokenSource(statusRes.tokenSource);
|
|
setSpriteHosts(statusRes.spriteHosts);
|
|
setSpriteAgents(statusRes.spriteAgents);
|
|
setLoading(false);
|
|
void refreshClusterState();
|
|
if (statusRes.configured) {
|
|
setRemoteLoading(true);
|
|
setRemoteError(null);
|
|
try {
|
|
const remote = await fetchStatus({ includeRemote: true });
|
|
setSprites(remote.sprites);
|
|
setRemoteError(remote.remoteError);
|
|
setSpriteHosts((prev) => (remote.spriteHosts.length ? remote.spriteHosts : prev));
|
|
setSpriteAgents((prev) => (remote.spriteAgents.length ? remote.spriteAgents : prev));
|
|
} catch {
|
|
setRemoteError("Remote Sprites unavailable; refresh again to retry.");
|
|
} finally {
|
|
setRemoteLoading(false);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
setLoading(false);
|
|
}
|
|
}, [fetchStatus, refreshClusterState, toast]);
|
|
|
|
useEffect(() => {
|
|
void refresh();
|
|
}, [refresh]);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
void (async () => {
|
|
try {
|
|
const res = await listSpriteJobs({});
|
|
if (cancelled) return;
|
|
const running = res.jobs.filter((job) => job.status === "running");
|
|
if (running.length > 0) {
|
|
setActiveJobs((prev) => {
|
|
const byId = new Map(prev.map((j) => [j.jobId, j]));
|
|
for (const job of running) {
|
|
byId.set(job.jobId, job);
|
|
}
|
|
return [...byId.values()];
|
|
});
|
|
}
|
|
} catch {
|
|
// Ignore if plugin server hasn't loaded jobs list yet.
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [listSpriteJobs]);
|
|
|
|
const handleJobFinished = useCallback(
|
|
(job: SpriteJob) => {
|
|
if (handledJobIdsRef.current.has(job.jobId)) return;
|
|
handledJobIdsRef.current.add(job.jobId);
|
|
if (formConnectJobIdRef.current === job.jobId) {
|
|
formConnectJobIdRef.current = null;
|
|
}
|
|
|
|
if (job.status === "failed") {
|
|
toast.error(job.error || `${formatJobKind(job.kind)} failed`);
|
|
void refresh();
|
|
return;
|
|
}
|
|
|
|
const result = job.result;
|
|
if (!result) {
|
|
void refresh();
|
|
return;
|
|
}
|
|
|
|
if (result.kind === "connect_host") {
|
|
toast.show(`Sprite host ${result.spriteName} connected`, { variant: "success" });
|
|
setSpriteNameInput(result.spriteName);
|
|
void refresh();
|
|
return;
|
|
}
|
|
|
|
if (result.kind === "launch_agent") {
|
|
toast.show(`Launched on ${result.spriteName} (${result.provider})`, { variant: "success" });
|
|
setSpriteNameInput(result.spriteName);
|
|
setPromptInput("");
|
|
setAgentTitleInput("");
|
|
void refresh();
|
|
if (autoOpenAgentJobIdsRef.current.has(job.jobId)) {
|
|
autoOpenAgentJobIdsRef.current.delete(job.jobId);
|
|
navigation?.openAgent({ agentId: result.agentId, serverId: result.serverId });
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (result.kind === "create_sprite") {
|
|
toast.show(`Sprite ${result.sprite.name} ready`, { variant: "success" });
|
|
setSpriteNameInput(result.sprite.name);
|
|
void refresh();
|
|
return;
|
|
}
|
|
|
|
if (result.kind === "sync_workspace") {
|
|
toast.show(`Synced ${result.fileCount} files to ${result.spriteName}:${result.remoteDir}`, {
|
|
variant: "success",
|
|
});
|
|
}
|
|
},
|
|
[navigation, refresh, toast],
|
|
);
|
|
|
|
const trackedJobIdsKey = activeJobs.map((j) => j.jobId).sort().join(",");
|
|
useEffect(() => {
|
|
if (!trackedJobIdsKey) return;
|
|
let cancelled = false;
|
|
const jobIds = trackedJobIdsKey.split(",").filter(Boolean);
|
|
|
|
const pollOnce = async () => {
|
|
if (cancelled) return;
|
|
setNowTick(Date.now());
|
|
const updates = await Promise.all(
|
|
jobIds.map(async (jobId) => {
|
|
try {
|
|
const res = await getSpriteJob({ jobId });
|
|
return { jobId, res };
|
|
} catch {
|
|
return { jobId, res: null };
|
|
}
|
|
}),
|
|
);
|
|
if (cancelled) return;
|
|
|
|
const finished: SpriteJob[] = [];
|
|
const dropped = new Set<string>();
|
|
const latestRunning = new Map<string, SpriteJob>();
|
|
|
|
for (const { jobId, res } of updates) {
|
|
if (!res) continue;
|
|
if (!res.ok) {
|
|
dropped.add(jobId);
|
|
continue;
|
|
}
|
|
if (res.job.status === "running") {
|
|
latestRunning.set(jobId, res.job);
|
|
} else {
|
|
finished.push(res.job);
|
|
dropped.add(jobId);
|
|
}
|
|
}
|
|
|
|
if (dropped.size > 0 || latestRunning.size > 0) {
|
|
setActiveJobs((prev) =>
|
|
prev
|
|
.filter((job) => !dropped.has(job.jobId))
|
|
.map((job) => latestRunning.get(job.jobId) ?? job),
|
|
);
|
|
}
|
|
|
|
for (const job of finished) {
|
|
handleJobFinished(job);
|
|
}
|
|
};
|
|
|
|
const timer = setInterval(() => {
|
|
void pollOnce();
|
|
}, 1000);
|
|
return () => {
|
|
cancelled = true;
|
|
clearInterval(timer);
|
|
};
|
|
}, [trackedJobIdsKey, getSpriteJob, handleJobFinished]);
|
|
|
|
const trackJob = useCallback(
|
|
(job: SpriteJob) => {
|
|
setNowTick(Date.now());
|
|
if (job.status !== "running") {
|
|
handleJobFinished(job);
|
|
return;
|
|
}
|
|
setActiveJobs((prev) => {
|
|
const idx = prev.findIndex((existing) => existing.jobId === job.jobId);
|
|
if (idx === -1) return [job, ...prev];
|
|
const next = [...prev];
|
|
next[idx] = job;
|
|
return next;
|
|
});
|
|
},
|
|
[handleJobFinished],
|
|
);
|
|
|
|
const openbao = useOpenBaoAuth({ onConnected: refresh });
|
|
const pendingFlow =
|
|
openbao.status?.pendingAuth?.status === "pending" ? openbao.status.pendingAuth : null;
|
|
|
|
const hostBySprite = useMemo(() => {
|
|
const map = new Map<string, SpriteHostSummary>();
|
|
for (const h of spriteHosts) {
|
|
map.set(h.spriteName, h);
|
|
}
|
|
return map;
|
|
}, [spriteHosts]);
|
|
|
|
const trimmedFormSprite = spriteNameInput.trim();
|
|
const isCreatingSprite =
|
|
startingKey === "create_sprite" || activeJobs.some((job) => job.kind === "create_sprite");
|
|
const isLaunchingAgent =
|
|
startingKey === "launch_agent" || activeJobs.some((job) => job.kind === "launch_agent");
|
|
const isConnectingFormHost =
|
|
startingKey === "connect_host:form" ||
|
|
activeJobs.some(
|
|
(job) =>
|
|
job.kind === "connect_host" &&
|
|
(job.jobId === formConnectJobIdRef.current ||
|
|
(trimmedFormSprite !== "" && job.spriteName === trimmedFormSprite)),
|
|
);
|
|
|
|
const handleCreateSprite = async () => {
|
|
setStartingKey("create_sprite");
|
|
try {
|
|
const res = await startCreateSprite({
|
|
name: spriteNameInput.trim() || undefined,
|
|
syncLocalPath: syncBeforeOpen && cwdInput.trim() ? cwdInput.trim() : undefined,
|
|
});
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
trackJob(res.job);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setStartingKey((cur) => (cur === "create_sprite" ? null : cur));
|
|
}
|
|
};
|
|
|
|
const handleConnectHost = async (name?: string, fromForm = false) => {
|
|
const trimmedName = name?.trim() || "";
|
|
const key = fromForm ? "connect_host:form" : `connect_host:${trimmedName}`;
|
|
setStartingKey(key);
|
|
try {
|
|
const res = await startConnectHost({
|
|
spriteName: trimmedName || undefined,
|
|
syncLocalPath: syncBeforeOpen && cwdInput.trim() ? cwdInput.trim() : undefined,
|
|
});
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
if (fromForm) {
|
|
formConnectJobIdRef.current = res.job.jobId;
|
|
}
|
|
trackJob(res.job);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setStartingKey((cur) => (cur === key ? null : cur));
|
|
}
|
|
};
|
|
|
|
const handleLaunchAgent = async () => {
|
|
const targetSprite = spriteNameInput.trim();
|
|
const initialPrompt = promptInput.trim();
|
|
const title = agentTitleInput.trim();
|
|
|
|
setStartingKey("launch_agent");
|
|
try {
|
|
const res = await startLaunchAgent({
|
|
spriteName: targetSprite || undefined,
|
|
title: title || undefined,
|
|
prompt: initialPrompt || undefined,
|
|
syncLocalPath: syncBeforeOpen && cwdInput.trim() ? cwdInput.trim() : undefined,
|
|
provider: providerInput || "auto",
|
|
});
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
await refresh();
|
|
return;
|
|
}
|
|
autoOpenAgentJobIdsRef.current.add(res.job.jobId);
|
|
trackJob(res.job);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setStartingKey((cur) => (cur === "launch_agent" ? null : cur));
|
|
}
|
|
};
|
|
|
|
const handleSpriteAgentAction = async (
|
|
agent: SpriteAgentSummary,
|
|
action: "interrupt" | "archive",
|
|
) => {
|
|
setAgentBusyId(agent.agentId);
|
|
try {
|
|
const res = await spriteAgentAction({
|
|
serverId: agent.serverId,
|
|
agentId: agent.agentId,
|
|
action,
|
|
});
|
|
if (!res.ok) {
|
|
toast.error(res.error ?? `Failed to ${action} agent`);
|
|
return;
|
|
}
|
|
toast.show(action === "interrupt" ? "Interrupted agent turn" : "Archived agent", {
|
|
variant: "info",
|
|
});
|
|
await refresh();
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setAgentBusyId(null);
|
|
}
|
|
};
|
|
|
|
const handleArchiveLegacyAgent = async (agentId: string) => {
|
|
setAgentBusyId(agentId);
|
|
try {
|
|
await paseo.agents.ref(agentId).archive();
|
|
toast.show("Archived legacy cluster agent", { variant: "info" });
|
|
setLegacyAgents((prev) => prev.filter((a) => a.id !== agentId));
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setAgentBusyId(null);
|
|
}
|
|
};
|
|
|
|
const handleSyncSprite = async (name: string) => {
|
|
const localPath = cwdInput.trim();
|
|
if (!localPath) {
|
|
toast.error("Select a workspace or enter a local folder path to sync.");
|
|
return;
|
|
}
|
|
const key = `sync_workspace:${name}`;
|
|
setStartingKey(key);
|
|
try {
|
|
const res = await startSyncWorkspace({ spriteName: name, localPath });
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
trackJob(res.job);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setStartingKey((cur) => (cur === key ? null : cur));
|
|
}
|
|
};
|
|
|
|
const handleCheckpoint = async (name: string) => {
|
|
setSyncAction({ kind: "checkpoint", spriteName: name });
|
|
try {
|
|
const res = await createCheckpoint({
|
|
spriteName: name,
|
|
comment: `paseo-${new Date().toISOString()}`,
|
|
});
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
toast.show(`Checkpointed ${name}`, { variant: "success" });
|
|
await handleLoadCheckpoints(name);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setSyncAction(null);
|
|
}
|
|
};
|
|
|
|
const handleLoadCheckpoints = async (name: string) => {
|
|
setCheckpointsSprite(name);
|
|
try {
|
|
const res = await listCheckpoints({ spriteName: name });
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
setCheckpoints(res.checkpoints);
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
}
|
|
};
|
|
|
|
const handleRestoreCheckpoint = async (name: string, checkpointId: string) => {
|
|
setSyncAction({ kind: "restore", spriteName: name, checkpointId });
|
|
try {
|
|
const res = await restoreCheckpoint({ spriteName: name, checkpointId });
|
|
if (!res.ok) {
|
|
toast.error(res.error);
|
|
return;
|
|
}
|
|
toast.show(`Restored ${name} to ${checkpointId}`, { variant: "success" });
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setSyncAction(null);
|
|
}
|
|
};
|
|
|
|
const handleDestroySprite = async (name: string) => {
|
|
setSyncAction({ kind: "destroy", spriteName: name });
|
|
try {
|
|
const res = await destroySprite({ spriteName: name });
|
|
if (!res.ok) {
|
|
toast.error(res.error ?? "Failed to delete sprite");
|
|
return;
|
|
}
|
|
toast.show(`Deleted sprite ${name}`, { variant: "info" });
|
|
if (checkpointsSprite === name) {
|
|
setCheckpointsSprite(null);
|
|
setCheckpoints([]);
|
|
}
|
|
await refresh();
|
|
} catch (error) {
|
|
toast.error(error instanceof Error ? error.message : String(error));
|
|
} finally {
|
|
setSyncAction(null);
|
|
}
|
|
};
|
|
|
|
const styles = useMemo(
|
|
() => ({
|
|
screen: { flex: 1, backgroundColor: theme.colors.surface0 },
|
|
body: { padding: layout.compact ? 16 : 24, gap: 14 },
|
|
headerRow: {
|
|
flexDirection: "row" as const,
|
|
justifyContent: "space-between" as const,
|
|
alignItems: "center" as const,
|
|
gap: 8,
|
|
},
|
|
title: { color: theme.colors.foreground, fontSize: 18, fontWeight: "600" as const },
|
|
sectionTitle: { color: theme.colors.foreground, fontSize: 15, fontWeight: "600" as const, marginTop: 6 },
|
|
muted: { color: theme.colors.foregroundMuted, fontSize: 12 },
|
|
accentText: { color: theme.colors.accent, fontSize: 12, fontWeight: "500" as const },
|
|
error: { color: theme.colors.statusDanger, fontSize: 12 },
|
|
card: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.border,
|
|
backgroundColor: theme.colors.surface1,
|
|
borderRadius: 10,
|
|
padding: 12,
|
|
gap: 8,
|
|
},
|
|
input: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.border,
|
|
backgroundColor: theme.colors.surface0,
|
|
color: theme.colors.foreground,
|
|
borderRadius: 8,
|
|
paddingHorizontal: 10,
|
|
paddingVertical: 8,
|
|
fontSize: 13,
|
|
},
|
|
row: { flexDirection: "row" as const, flexWrap: "wrap" as const, gap: 8, alignItems: "center" as const },
|
|
btnPrimary: {
|
|
backgroundColor: theme.colors.accent,
|
|
borderRadius: 8,
|
|
paddingHorizontal: 12,
|
|
paddingVertical: 8,
|
|
},
|
|
btnPrimaryText: { color: theme.colors.accentForeground, fontSize: 12, fontWeight: "600" as const },
|
|
btnSecondary: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.border,
|
|
backgroundColor: theme.colors.surface2,
|
|
borderRadius: 8,
|
|
paddingHorizontal: 10,
|
|
paddingVertical: 6,
|
|
},
|
|
btnSecondaryText: { color: theme.colors.foreground, fontSize: 12, fontWeight: "500" as const },
|
|
btnDanger: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.statusDanger,
|
|
borderRadius: 8,
|
|
paddingHorizontal: 10,
|
|
paddingVertical: 6,
|
|
},
|
|
btnDangerText: { color: theme.colors.statusDanger, fontSize: 12, fontWeight: "500" as const },
|
|
chip: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.border,
|
|
backgroundColor: theme.colors.surface0,
|
|
borderRadius: 999,
|
|
paddingHorizontal: 10,
|
|
paddingVertical: 4,
|
|
},
|
|
chipActive: {
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.accent,
|
|
backgroundColor: theme.colors.surface2,
|
|
borderRadius: 999,
|
|
paddingHorizontal: 10,
|
|
paddingVertical: 4,
|
|
},
|
|
chipText: { color: theme.colors.foregroundMuted, fontSize: 12 },
|
|
chipActiveText: { color: theme.colors.foreground, fontSize: 12, fontWeight: "600" as const },
|
|
monoBox: {
|
|
backgroundColor: theme.colors.surface0,
|
|
borderWidth: 1,
|
|
borderColor: theme.colors.border,
|
|
borderRadius: 8,
|
|
padding: 10,
|
|
},
|
|
}),
|
|
[theme, layout.compact],
|
|
);
|
|
|
|
const statusColor = (status: string) => {
|
|
if (status === "running" || status === "initializing" || status === "connecting") {
|
|
return theme.colors.accent;
|
|
}
|
|
if (status === "error" || status === "offline") return theme.colors.statusDanger;
|
|
if (status === "idle" || status === "online") return theme.colors.statusSuccess;
|
|
return theme.colors.foregroundMuted;
|
|
};
|
|
|
|
return (
|
|
<ScrollView style={styles.screen} contentContainerStyle={styles.body}>
|
|
<View style={styles.headerRow}>
|
|
<View>
|
|
<Text style={styles.title}>Fly Sprites Agents</Text>
|
|
<Text style={styles.muted}>
|
|
Host: {host.label} · Token: {tokenSource}
|
|
{workspaceName ? ` · Workspace: ${workspaceName}` : ""}
|
|
</Text>
|
|
</View>
|
|
<Pressable style={styles.btnSecondary} onPress={() => void refresh()} disabled={loading || remoteLoading}>
|
|
<Text style={styles.btnSecondaryText}>{loading || remoteLoading ? "Refreshing…" : "Refresh"}</Text>
|
|
</Pressable>
|
|
</View>
|
|
|
|
{!configured ? (
|
|
<View style={styles.card}>
|
|
<Text style={styles.error}>
|
|
No Fly Sprites API token loaded. Sign in with Zitadel to pull FLY_TOKEN from OpenBao, or configure a token in Settings → Fly Sprites Workers.
|
|
</Text>
|
|
{openbao.status?.lastError ? (
|
|
<Text style={styles.error}>OpenBao: {openbao.status.lastError}</Text>
|
|
) : null}
|
|
{pendingFlow ? (
|
|
<View style={{ gap: 6 }}>
|
|
<Text style={styles.muted}>
|
|
Waiting for Zitadel approval · Code: {pendingFlow.userCode}
|
|
</Text>
|
|
<View style={styles.row}>
|
|
<Pressable
|
|
style={styles.btnPrimary}
|
|
onPress={() =>
|
|
void openExternalUrl(
|
|
pendingFlow.verificationUriComplete || pendingFlow.verificationUri,
|
|
)
|
|
}
|
|
>
|
|
<Text style={styles.btnPrimaryText}>Open Zitadel Login</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void copyText(pendingFlow.userCode)}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Copy Code</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
) : (
|
|
<View style={styles.row}>
|
|
<Pressable
|
|
style={styles.btnPrimary}
|
|
onPress={() => void openbao.startLogin()}
|
|
disabled={openbao.busy}
|
|
>
|
|
<Text style={styles.btnPrimaryText}>
|
|
{openbao.busy
|
|
? "Starting…"
|
|
: openbao.status?.authenticated
|
|
? "Re-authenticate Zitadel"
|
|
: "Sign in with Zitadel"}
|
|
</Text>
|
|
</Pressable>
|
|
{openbao.status?.authenticated ? (
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void openbao.pullNow()}
|
|
disabled={openbao.busy}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Pull from OpenBao</Text>
|
|
</Pressable>
|
|
) : null}
|
|
</View>
|
|
)}
|
|
</View>
|
|
) : null}
|
|
|
|
{remoteError ? (
|
|
<View style={styles.card}>
|
|
<Text style={styles.error}>Remote API error: {remoteError}</Text>
|
|
<Pressable style={styles.btnSecondary} onPress={() => void refresh()}>
|
|
<Text style={styles.btnSecondaryText}>Retry</Text>
|
|
</Pressable>
|
|
</View>
|
|
) : null}
|
|
|
|
<View style={styles.card}>
|
|
<Text style={styles.sectionTitle}>Launch Agent on Sprite Host</Text>
|
|
<Text style={styles.muted}>
|
|
Provisions Paseo inside the Sprite and connects it as its own host so workspaces and agents appear under the Sprite host.
|
|
</Text>
|
|
<TextInput
|
|
style={styles.input}
|
|
value={spriteNameInput}
|
|
onChangeText={setSpriteNameInput}
|
|
placeholder="Target Sprite name (blank creates a new Sprite host)"
|
|
placeholderTextColor={theme.colors.foregroundMuted}
|
|
/>
|
|
<TextInput
|
|
style={styles.input}
|
|
value={agentTitleInput}
|
|
onChangeText={setAgentTitleInput}
|
|
placeholder="Agent title (optional)"
|
|
placeholderTextColor={theme.colors.foregroundMuted}
|
|
/>
|
|
<TextInput
|
|
style={styles.input}
|
|
value={promptInput}
|
|
onChangeText={setPromptInput}
|
|
placeholder="Initial prompt / task (optional; leave blank for interactive chat)"
|
|
placeholderTextColor={theme.colors.foregroundMuted}
|
|
multiline
|
|
/>
|
|
{workspaces.length > 0 && !workspaceDirectory ? (
|
|
<View style={styles.row}>
|
|
{workspaces.map((ws) => {
|
|
const wsPath = getWorkspacePath(ws);
|
|
const active = cwdInput === wsPath;
|
|
return (
|
|
<Pressable
|
|
key={ws.id}
|
|
style={active ? styles.chipActive : styles.chip}
|
|
onPress={() => setCwdInput(wsPath)}
|
|
>
|
|
<Text style={active ? styles.chipActiveText : styles.chipText}>
|
|
{ws.title || ws.name || wsPath}
|
|
</Text>
|
|
</Pressable>
|
|
);
|
|
})}
|
|
</View>
|
|
) : null}
|
|
<TextInput
|
|
style={styles.input}
|
|
value={cwdInput}
|
|
onChangeText={setCwdInput}
|
|
placeholder="Optional local workspace directory to sync to /workspace"
|
|
placeholderTextColor={theme.colors.foregroundMuted}
|
|
/>
|
|
<View style={styles.row}>
|
|
{PROVIDER_CHOICES.map((choice) => {
|
|
const active = providerInput === choice.id;
|
|
return (
|
|
<Pressable
|
|
key={choice.id}
|
|
style={active ? styles.chipActive : styles.chip}
|
|
onPress={() => setProviderInput(choice.id)}
|
|
>
|
|
<Text style={active ? styles.chipActiveText : styles.chipText}>
|
|
{choice.label}
|
|
</Text>
|
|
</Pressable>
|
|
);
|
|
})}
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => setSyncBeforeOpen((v) => !v)}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{syncBeforeOpen ? "Sync workspace: ON" : "Sync workspace: OFF"}
|
|
</Text>
|
|
</Pressable>
|
|
</View>
|
|
<View style={styles.row}>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleCreateSprite()}
|
|
disabled={isCreatingSprite || !configured}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isCreatingSprite ? "Creating…" : "Create Sprite"}
|
|
</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleConnectHost(spriteNameInput, true)}
|
|
disabled={isConnectingFormHost || !configured}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isConnectingFormHost ? "Connecting…" : "Connect Host"}
|
|
</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnPrimary}
|
|
onPress={() => void handleLaunchAgent()}
|
|
disabled={isLaunchingAgent || !configured}
|
|
>
|
|
<Text style={styles.btnPrimaryText}>
|
|
{isLaunchingAgent ? "Launching…" : "Launch Agent"}
|
|
</Text>
|
|
</Pressable>
|
|
</View>
|
|
{activeJobs.length > 0 ? (
|
|
<View style={styles.monoBox}>
|
|
{activeJobs.map((job) => (
|
|
<View key={job.jobId} style={styles.headerRow}>
|
|
<Text style={[styles.accentText, { flex: 1 }]}>
|
|
{formatJobKind(job.kind)}
|
|
{job.spriteName ? ` (${job.spriteName})` : ""}: {job.phase}
|
|
</Text>
|
|
<Text style={styles.muted}>{formatElapsed(job.startedAt, nowTick)}</Text>
|
|
</View>
|
|
))}
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
|
|
<Text style={styles.sectionTitle}>Sprite Host Agents ({spriteAgents.length})</Text>
|
|
{spriteAgents.length === 0 ? (
|
|
<View style={styles.card}>
|
|
<Text style={styles.muted}>
|
|
No agents on connected Sprite hosts yet. Launch one above to create it under its Sprite host.
|
|
</Text>
|
|
</View>
|
|
) : (
|
|
spriteAgents.map((agent) => {
|
|
const isBusy = agentBusyId === agent.agentId;
|
|
return (
|
|
<View key={`${agent.serverId}:${agent.agentId}`} style={styles.card}>
|
|
<View style={styles.headerRow}>
|
|
<Text style={{ color: theme.colors.foreground, fontWeight: "600", fontSize: 13, flex: 1 }}>
|
|
{agent.title || `Agent ${agent.agentId.slice(0, 8)}`}
|
|
</Text>
|
|
<Text style={{ color: statusColor(agent.status), fontSize: 12, fontWeight: "600" }}>
|
|
{agent.status}
|
|
{agent.pendingPermissionsCount > 0 ? " · permission" : ""}
|
|
</Text>
|
|
</View>
|
|
<Text style={styles.muted}>
|
|
Host: {agent.hostLabel} · {agent.provider}
|
|
{agent.model ? `/${agent.model}` : ""} · {agent.cwd}
|
|
</Text>
|
|
{agent.lastError ? <Text style={styles.error}>{agent.lastError}</Text> : null}
|
|
<View style={styles.row}>
|
|
{navigation ? (
|
|
<Pressable
|
|
style={styles.btnPrimary}
|
|
onPress={() =>
|
|
navigation.openAgent({ agentId: agent.agentId, serverId: agent.serverId })
|
|
}
|
|
>
|
|
<Text style={styles.btnPrimaryText}>Open</Text>
|
|
</Pressable>
|
|
) : null}
|
|
{agent.status === "running" ? (
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleSpriteAgentAction(agent, "interrupt")}
|
|
disabled={isBusy}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Interrupt</Text>
|
|
</Pressable>
|
|
) : null}
|
|
<Pressable
|
|
style={styles.btnDanger}
|
|
onPress={() => void handleSpriteAgentAction(agent, "archive")}
|
|
disabled={isBusy}
|
|
>
|
|
<Text style={styles.btnDangerText}>Archive</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
);
|
|
})
|
|
)}
|
|
|
|
{legacyAgents.length > 0 ? (
|
|
<>
|
|
<Text style={styles.sectionTitle}>Cluster-Hosted Fly Agents ({legacyAgents.length})</Text>
|
|
{legacyAgents.map((agent) => (
|
|
<View key={agent.id} style={styles.card}>
|
|
<View style={styles.headerRow}>
|
|
<Text style={{ color: theme.colors.foreground, fontWeight: "600", fontSize: 13, flex: 1 }}>
|
|
{agent.title || `Agent ${agent.id.slice(0, 8)}`}
|
|
</Text>
|
|
<Text style={{ color: statusColor(agent.status), fontSize: 12, fontWeight: "600" }}>
|
|
{agent.status}
|
|
</Text>
|
|
</View>
|
|
<Text style={styles.muted}>
|
|
Cluster host ({host.label}) · {agent.cwd}
|
|
</Text>
|
|
<View style={styles.row}>
|
|
{navigation ? (
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => navigation.openAgent({ agentId: agent.id, serverId: host.id })}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Open</Text>
|
|
</Pressable>
|
|
) : null}
|
|
<Pressable
|
|
style={styles.btnDanger}
|
|
onPress={() => void handleArchiveLegacyAgent(agent.id)}
|
|
disabled={agentBusyId === agent.id}
|
|
>
|
|
<Text style={styles.btnDangerText}>Archive</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
))}
|
|
</>
|
|
) : null}
|
|
|
|
<Text style={styles.sectionTitle}>Remote Fly Sprites ({sprites.length}){remoteLoading ? " · loading…" : ""}</Text>
|
|
{sprites.length === 0 ? (
|
|
<View style={styles.card}>
|
|
<Text style={styles.muted}>{remoteLoading ? "Loading remote Sprites…" : "No Fly Sprites found."}</Text>
|
|
</View>
|
|
) : (
|
|
sprites.map((sprite) => {
|
|
const connectedHost = hostBySprite.get(sprite.name);
|
|
const connectJob = activeJobs.find(
|
|
(job) => job.kind === "connect_host" && job.spriteName === sprite.name,
|
|
);
|
|
const syncJob = activeJobs.find(
|
|
(job) => job.kind === "sync_workspace" && job.spriteName === sprite.name,
|
|
);
|
|
const isConnectingSprite =
|
|
startingKey === `connect_host:${sprite.name}` || Boolean(connectJob);
|
|
const isSyncingSprite =
|
|
startingKey === `sync_workspace:${sprite.name}` || Boolean(syncJob);
|
|
const isCheckpointingSprite =
|
|
syncAction?.kind === "checkpoint" && syncAction.spriteName === sprite.name;
|
|
const isDeletingSprite =
|
|
syncAction?.kind === "destroy" && syncAction.spriteName === sprite.name;
|
|
|
|
return (
|
|
<View key={sprite.name} style={styles.card}>
|
|
<View style={styles.headerRow}>
|
|
<Text style={{ color: theme.colors.foreground, fontWeight: "600", fontSize: 13 }}>
|
|
{sprite.name}
|
|
</Text>
|
|
<Text style={styles.muted}>
|
|
VM: {sprite.status}
|
|
{connectedHost ? ` · Host: ${connectedHost.status}` : " · Host: not connected"}
|
|
</Text>
|
|
</View>
|
|
{connectedHost?.error ? (
|
|
<Text style={styles.error}>Host error: {connectedHost.error}</Text>
|
|
) : null}
|
|
{connectJob ? (
|
|
<Text style={styles.accentText}>
|
|
{connectJob.phase} ({formatElapsed(connectJob.startedAt, nowTick)})
|
|
</Text>
|
|
) : null}
|
|
{syncJob ? (
|
|
<Text style={styles.accentText}>
|
|
{syncJob.phase} ({formatElapsed(syncJob.startedAt, nowTick)})
|
|
</Text>
|
|
) : null}
|
|
{sprite.url ? <Text style={styles.muted}>{sprite.url}</Text> : null}
|
|
<View style={styles.row}>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => setSpriteNameInput(sprite.name)}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Select</Text>
|
|
</Pressable>
|
|
{!connectedHost || connectedHost.status !== "online" ? (
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleConnectHost(sprite.name, false)}
|
|
disabled={isConnectingSprite || isDeletingSprite}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isConnectingSprite ? "Connecting…" : "Connect Host"}
|
|
</Text>
|
|
</Pressable>
|
|
) : null}
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleSyncSprite(sprite.name)}
|
|
disabled={isSyncingSprite || isDeletingSprite}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isSyncingSprite ? "Syncing…" : "Sync Here"}
|
|
</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleCheckpoint(sprite.name)}
|
|
disabled={isCheckpointingSprite || isDeletingSprite}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isCheckpointingSprite ? "Checkpointing…" : "Checkpoint"}
|
|
</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleLoadCheckpoints(sprite.name)}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>Checkpoints</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={styles.btnDanger}
|
|
onPress={() => void handleDestroySprite(sprite.name)}
|
|
disabled={isDeletingSprite || isConnectingSprite}
|
|
>
|
|
<Text style={styles.btnDangerText}>
|
|
{isDeletingSprite ? "Deleting…" : "Delete"}
|
|
</Text>
|
|
</Pressable>
|
|
</View>
|
|
{checkpointsSprite === sprite.name ? (
|
|
<View style={styles.monoBox}>
|
|
<Text style={[styles.muted, { marginBottom: 4 }]}>
|
|
Checkpoints for {sprite.name} ({checkpoints.length})
|
|
</Text>
|
|
{checkpoints.length === 0 ? (
|
|
<Text style={styles.muted}>No checkpoints.</Text>
|
|
) : (
|
|
checkpoints.map((cp) => {
|
|
const isRestoring =
|
|
syncAction?.kind === "restore" &&
|
|
syncAction.spriteName === sprite.name &&
|
|
syncAction.checkpointId === cp.id;
|
|
return (
|
|
<View key={cp.id} style={[styles.headerRow, { paddingVertical: 4 }]}>
|
|
<Text style={styles.muted}>
|
|
{cp.id} {cp.comment ? `— ${cp.comment}` : ""}
|
|
</Text>
|
|
<Pressable
|
|
style={styles.btnSecondary}
|
|
onPress={() => void handleRestoreCheckpoint(sprite.name, cp.id)}
|
|
disabled={isRestoring || isDeletingSprite}
|
|
>
|
|
<Text style={styles.btnSecondaryText}>
|
|
{isRestoring ? "Restoring…" : "Restore"}
|
|
</Text>
|
|
</Pressable>
|
|
</View>
|
|
);
|
|
})
|
|
)}
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
);
|
|
})
|
|
)}
|
|
</ScrollView>
|
|
);
|
|
}
|