feat: switch to managed Paseo Sprite agents, remote hosts, and Fly token exchange

This commit is contained in:
nandi committed 2026-11-19 01:47:07 +00:00
1 parent 9188bfc277
commit 32c02cb3cd
14 files changed
+1860 -446

No files matched your search

+216 -84
View File
@@ -1,5 +1,11 @@
import { defineAgentTool, type AgentToolDefinition } from "@getpaseo/plugin/server";
import { z } from "zod";
import type { FlySpritesProviderRegistration } from "./provider";
import {
launchSpriteAgent,
listSpriteHostsWithAgents,
removeSpriteHostRegistration,
} from "./sprite-hosts";
import type { SpritesManager } from "./sprites-manager";
function textResult(text: string, structuredContent?: unknown, isError = false) {
@@ -14,22 +20,27 @@ function errorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefinition[] {
export function createSpriteAgentTools(
manager: SpritesManager,
_provider: FlySpritesProviderRegistration,
): AgentToolDefinition[] {
const spriteListTool = defineAgentTool({
name: "sprite_list",
title: "List Fly Sprites & Workers",
title: "List Fly Sprites & Managed Agents",
description:
"List remote Fly Sprites and currently tracked background workers spawned by this plugin.",
"List remote Fly Sprites, connected Sprite Paseo hosts, and managed agents running on those Sprite hosts.",
input: z.object({
includeRemote: z.boolean().optional().default(true),
}),
async execute(input) {
async execute(input, context) {
try {
const workers = manager.listWorkers();
const sprites = input.includeRemote ? await manager.listRemoteSprites() : [];
const [{ hosts, agents }, sprites] = await Promise.all([
listSpriteHostsWithAgents(context.paseo),
input.includeRemote ? manager.listRemoteSprites() : Promise.resolve([]),
]);
return textResult(
JSON.stringify({ sprites, workers }, null, 2),
{ sprites, workers },
JSON.stringify({ sprites, hosts, agents }, null, 2),
{ sprites, hosts, agents },
);
} catch (err) {
return textResult(`Failed to list Fly Sprites: ${errorMessage(err)}`, undefined, true);
@@ -44,7 +55,11 @@ export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefini
"Create or reuse a Fly Sprite microVM and optionally sync the current workspace directory into /workspace.",
input: z.object({
name: z.string().optional().describe("Optional sprite name. Generated automatically if omitted."),
syncWorkspace: z.boolean().optional().default(true).describe("Sync this agent's local workspace files into the Sprite."),
syncWorkspace: z
.boolean()
.optional()
.default(true)
.describe("Sync this agent's local workspace files into the Sprite."),
remoteDir: z.string().optional().describe("Destination directory inside the Sprite (defaults to /workspace)."),
region: z.string().optional(),
ramMB: z.number().int().positive().optional(),
@@ -79,33 +94,201 @@ export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefini
},
});
const spriteSpawnWorkerTool = defineAgentTool({
name: "sprite_spawn_worker",
title: "Spawn Worker on Fly Sprite",
const spriteSpawnAgentTool = defineAgentTool({
name: "sprite_spawn_agent",
title: "Spawn Managed Agent on Sprite Host",
description:
"Spawn an asynchronous background worker command inside a Fly Sprite. Returns a workerId immediately so you can launch multiple workers in parallel and inspect them later with sprite_worker_status.",
"Provision/connect a Fly Sprite as its own Paseo host and launch a managed Paseo agent directly under that Sprite host.",
input: z.object({
command: z.string().describe("Shell command to run inside the Sprite (via bash -lc)."),
spriteName: z.string().optional().describe("Target Sprite name; creates a new Sprite if omitted."),
cwd: z.string().optional().describe("Working directory inside the Sprite (defaults to /workspace)."),
syncWorkspace: z.boolean().optional().default(false).describe("Sync this agent's local workspace into the Sprite before starting."),
maxRunAfterDisconnect: z.string().optional().describe("Optional disconnect grace duration, e.g. '30m'."),
prompt: z.string().optional().describe("Initial prompt or task for the managed agent."),
title: z.string().optional().describe("Optional display title for the agent."),
spriteName: z.string().optional().describe("Target Fly Sprite name; provisions a new Sprite if omitted."),
cwd: z.string().optional().describe("Local workspace directory to sync into /workspace (defaults to caller's cwd)."),
remoteDir: z.string().optional().describe("Workspace directory inside the Sprite (defaults to /workspace)."),
provider: z.string().optional().describe("Optional provider or provider/model on the Sprite host (defaults to auto)."),
syncWorkspace: z.boolean().optional().default(true).describe("Sync the local workspace into the Sprite before launching."),
background: z.boolean().optional().default(false).describe("Hide from Paseo tabs/lists when true."),
waitForFinish: z.boolean().optional().default(false).describe("Wait for the initial turn to finish before returning."),
timeoutMs: z.number().int().positive().optional().default(300_000),
}),
async execute(input, context) {
try {
const worker = await manager.spawnWorker({
const launched = await launchSpriteAgent({
manager,
paseo: context.paseo,
spriteName: input.spriteName,
command: input.command,
cwd: input.cwd,
syncLocalPath: input.syncWorkspace ? context.agent.cwd : undefined,
maxRunAfterDisconnect: input.maxRunAfterDisconnect,
title: input.title,
prompt: input.prompt,
syncLocalPath: input.syncWorkspace ? (input.cwd?.trim() || context.agent.cwd) : undefined,
remoteDir: input.remoteDir,
provider: input.provider,
background: input.background,
});
let waitResult: unknown;
if (input.waitForFinish && input.prompt) {
const spritePaseo = await context.paseo.hosts.client(launched.serverId);
try {
waitResult = await spritePaseo.agents.ref(launched.agentId).waitForFinish(input.timeoutMs);
} finally {
await spritePaseo.dispose().catch(() => undefined);
}
}
return textResult(
`Spawned worker ${worker.workerId} on sprite ${worker.spriteName} (status=${worker.status}, cwd=${worker.cwd}).`,
worker,
`Managed agent ${launched.agentId} ready under Sprite host ${launched.spriteName} (${launched.serverId}, provider=${launched.provider}).`,
{
...launched,
waitResult,
},
);
} catch (err) {
return textResult(`Failed to spawn worker: ${errorMessage(err)}`, undefined, true);
return textResult(`Failed to spawn managed agent: ${errorMessage(err)}`, undefined, true);
}
},
});
const spriteAgentStatusTool = defineAgentTool({
name: "sprite_agent_status",
title: "Check Managed Sprite Agent Status",
description: "List managed agents across Sprite hosts or inspect one agent's state and latest turn output.",
input: z.object({
agentId: z.string().optional().describe("Agent ID returned by sprite_spawn_agent. Omit to list all Sprite-hosted agents."),
serverId: z.string().optional().describe("Optional Sprite host serverId if known."),
}),
async execute(input, context) {
try {
const { hosts, agents } = await listSpriteHostsWithAgents(context.paseo);
if (!input.agentId) {
return textResult(JSON.stringify({ hosts, agents }, null, 2), { hosts, agents });
}
const matchedServerId =
input.serverId?.trim() ||
agents.find((a) => a.agentId === input.agentId)?.serverId;
if (!matchedServerId) {
return textResult(`Unknown Sprite agent: ${input.agentId}`, undefined, true);
}
const spritePaseo = await context.paseo.hosts.client(matchedServerId);
try {
const ref = spritePaseo.agents.ref(input.agentId);
const refreshed = await ref.refresh();
if (!refreshed) {
return textResult(`Unknown agent: ${input.agentId}`, undefined, true);
}
const timeline = await ref.timeline
.refetch({ direction: "tail", limit: 1, wholeTurns: true })
.catch(() => null);
return textResult(
JSON.stringify(
{
serverId: matchedServerId,
agentId: refreshed.agent.id,
title: refreshed.agent.title ?? null,
status: refreshed.agent.status,
provider: refreshed.agent.provider,
model: refreshed.agent.model ?? null,
lastError: refreshed.agent.lastError ?? null,
latestEntries: timeline?.entries ?? [],
},
null,
2,
),
{
serverId: matchedServerId,
agent: refreshed.agent,
latestEntries: timeline?.entries ?? [],
},
);
} finally {
await spritePaseo.dispose().catch(() => undefined);
}
} catch (err) {
return textResult(`Failed to inspect agent: ${errorMessage(err)}`, undefined, true);
}
},
});
const spriteAgentSendTool = defineAgentTool({
name: "sprite_agent_send",
title: "Send Message to Managed Sprite Agent",
description: "Send a follow-up prompt or instruction to an existing managed agent on a Sprite host.",
input: z.object({
agentId: z.string().describe("Target managed agent ID."),
serverId: z.string().optional().describe("Sprite host serverId (resolved automatically if omitted)."),
prompt: z.string().describe("Message or instruction to send."),
waitForFinish: z.boolean().optional().default(false),
timeoutMs: z.number().int().positive().optional().default(300_000),
}),
async execute(input, context) {
try {
let serverId = input.serverId?.trim();
if (!serverId) {
const { agents } = await listSpriteHostsWithAgents(context.paseo);
serverId = agents.find((a) => a.agentId === input.agentId)?.serverId;
}
if (!serverId) {
return textResult(`Unknown Sprite agent: ${input.agentId}`, undefined, true);
}
const spritePaseo = await context.paseo.hosts.client(serverId);
try {
const ref = spritePaseo.agents.ref(input.agentId);
if (input.waitForFinish) {
const result = await ref.run(input.prompt, { timeoutMs: input.timeoutMs });
return textResult(`Agent ${input.agentId} finished turn.`, { serverId, agentId: input.agentId, result });
}
await ref.send(input.prompt);
return textResult(`Sent message to agent ${input.agentId}.`, { serverId, agentId: input.agentId, sent: true });
} finally {
await spritePaseo.dispose().catch(() => undefined);
}
} catch (err) {
return textResult(`Failed to message agent: ${errorMessage(err)}`, undefined, true);
}
},
});
const spriteAgentStopTool = defineAgentTool({
name: "sprite_agent_stop",
title: "Interrupt or Archive Managed Sprite Agent",
description: "Interrupt a running managed Sprite agent (via reload) or archive it.",
input: z.object({
agentId: z.string().describe("Target managed agent ID."),
serverId: z.string().optional().describe("Sprite host serverId (resolved automatically if omitted)."),
action: z.enum(["interrupt", "archive"]).optional().default("archive"),
}),
async execute(input, context) {
try {
let serverId = input.serverId?.trim();
if (!serverId) {
const { agents } = await listSpriteHostsWithAgents(context.paseo);
serverId = agents.find((a) => a.agentId === input.agentId)?.serverId;
}
if (!serverId) {
return textResult(`Unknown Sprite agent: ${input.agentId}`, undefined, true);
}
const spritePaseo = await context.paseo.hosts.client(serverId);
try {
const ref = spritePaseo.agents.ref(input.agentId);
if (input.action === "interrupt") {
await ref.reload();
return textResult(`Interrupted and reloaded agent ${input.agentId}.`, { serverId, agentId: input.agentId, action: "interrupt" });
}
const archived = await ref.archive();
return textResult(`Archived agent ${input.agentId} at ${archived.archivedAt}.`, {
serverId,
agentId: input.agentId,
action: "archive",
archivedAt: archived.archivedAt,
});
} finally {
await spritePaseo.dispose().catch(() => undefined);
}
} catch (err) {
return textResult(`Failed to ${input.action} agent: ${errorMessage(err)}`, undefined, true);
}
},
});
@@ -143,59 +326,6 @@ export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefini
},
});
const spriteWorkerStatusTool = defineAgentTool({
name: "sprite_worker_status",
title: "Check Fly Sprite Worker Status & Logs",
description: "Inspect the status and buffered output of a spawned Fly Sprite worker (or list all workers).",
input: z.object({
workerId: z.string().optional().describe("Worker ID returned by sprite_spawn_worker. Omit to list all workers."),
tailChars: z.number().int().positive().optional().default(8000),
}),
async execute(input) {
if (!input.workerId) {
const workers = manager.listWorkers();
return textResult(JSON.stringify(workers, null, 2), { workers });
}
const found = manager.getWorker(input.workerId);
if (!found) {
return textResult(`Unknown worker: ${input.workerId}`, undefined, true);
}
const output =
found.output.length > input.tailChars
? found.output.slice(-input.tailChars)
: found.output;
return textResult(
[
`workerId: ${found.summary.workerId}`,
`sprite: ${found.summary.spriteName}`,
`status: ${found.summary.status}`,
`exitCode: ${found.summary.exitCode ?? "null"}`,
found.summary.error ? `error: ${found.summary.error}` : "",
output ? `output:\n${output}` : "(no output yet)",
]
.filter(Boolean)
.join("\n"),
{ ...found.summary, output },
);
},
});
const spriteWorkerStopTool = defineAgentTool({
name: "sprite_worker_stop",
title: "Stop Fly Sprite Worker",
description: "Stop a running worker spawned with sprite_spawn_worker.",
input: z.object({
workerId: z.string(),
}),
async execute(input) {
const stopped = manager.stopWorker(input.workerId);
if (!stopped) {
return textResult(`Unknown worker: ${input.workerId}`, undefined, true);
}
return textResult(`Stopped worker ${input.workerId}.`, { workerId: input.workerId, stopped: true });
},
});
const spriteCheckpointTool = defineAgentTool({
name: "sprite_checkpoint",
title: "Manage Fly Sprite Checkpoints",
@@ -230,13 +360,14 @@ export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefini
const spriteDestroyTool = defineAgentTool({
name: "sprite_destroy",
title: "Delete Fly Sprite",
description: "Delete a Fly Sprite and stop any active tracked workers on it.",
description: "Delete a Fly Sprite microVM and remove its Paseo host registration.",
input: z.object({
spriteName: z.string(),
}),
async execute(input) {
async execute(input, context) {
try {
await manager.deleteSprite(input.spriteName);
await removeSpriteHostRegistration(context.paseo, input.spriteName).catch(() => undefined);
return textResult(`Deleted sprite ${input.spriteName}.`, { spriteName: input.spriteName, deleted: true });
} catch (err) {
return textResult(`Failed to delete sprite: ${errorMessage(err)}`, undefined, true);
@@ -247,10 +378,11 @@ export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefini
return [
spriteListTool,
spriteCreateTool,
spriteSpawnWorkerTool,
spriteSpawnAgentTool,
spriteAgentStatusTool,
spriteAgentSendTool,
spriteAgentStopTool,
spriteExecTool,
spriteWorkerStatusTool,
spriteWorkerStopTool,
spriteCheckpointTool,
spriteDestroyTool,
];