feat: add fly-sprites-workers Paseo plugin
This commit is contained in:
commit
0c4027ebe5
15 files changed
+7446
No files matched your search
@@ -0,0 +1,257 @@
|
||||
import { defineAgentTool, type AgentToolDefinition } from "@getpaseo/plugin/server";
|
||||
import { z } from "zod";
|
||||
import type { SpritesManager } from "./sprites-manager";
|
||||
|
||||
function textResult(text: string, structuredContent?: unknown, isError = false) {
|
||||
return {
|
||||
text,
|
||||
structuredContent,
|
||||
...(isError ? { isError: true } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function errorMessage(err: unknown): string {
|
||||
return err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
|
||||
export function createSpriteAgentTools(manager: SpritesManager): AgentToolDefinition[] {
|
||||
const spriteListTool = defineAgentTool({
|
||||
name: "sprite_list",
|
||||
title: "List Fly Sprites & Workers",
|
||||
description:
|
||||
"List remote Fly Sprites and currently tracked background workers spawned by this plugin.",
|
||||
input: z.object({
|
||||
includeRemote: z.boolean().optional().default(true),
|
||||
}),
|
||||
async execute(input) {
|
||||
try {
|
||||
const workers = manager.listWorkers();
|
||||
const sprites = input.includeRemote ? await manager.listRemoteSprites() : [];
|
||||
return textResult(
|
||||
JSON.stringify({ sprites, workers }, null, 2),
|
||||
{ sprites, workers },
|
||||
);
|
||||
} catch (err) {
|
||||
return textResult(`Failed to list Fly Sprites: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const spriteCreateTool = defineAgentTool({
|
||||
name: "sprite_create",
|
||||
title: "Create Fly Sprite",
|
||||
description:
|
||||
"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."),
|
||||
remoteDir: z.string().optional().describe("Destination directory inside the Sprite (defaults to /workspace)."),
|
||||
region: z.string().optional(),
|
||||
ramMB: z.number().int().positive().optional(),
|
||||
cpus: z.number().int().positive().optional(),
|
||||
}),
|
||||
async execute(input, context) {
|
||||
try {
|
||||
const result = await manager.ensureSprite({
|
||||
name: input.name,
|
||||
config: {
|
||||
region: input.region,
|
||||
ramMB: input.ramMB,
|
||||
cpus: input.cpus,
|
||||
},
|
||||
syncLocalPath: input.syncWorkspace ? context.agent.cwd : undefined,
|
||||
remoteDir: input.remoteDir,
|
||||
});
|
||||
return textResult(
|
||||
`Sprite ${result.sprite.name} ready (${result.created ? "created" : "existing"}, remoteDir=${result.remoteDir}${
|
||||
result.syncedFiles !== undefined ? `, syncedFiles=${result.syncedFiles}` : ""
|
||||
}).`,
|
||||
{
|
||||
spriteName: result.sprite.name,
|
||||
created: result.created,
|
||||
remoteDir: result.remoteDir,
|
||||
syncedFiles: result.syncedFiles,
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
return textResult(`Failed to create sprite: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const spriteSpawnWorkerTool = defineAgentTool({
|
||||
name: "sprite_spawn_worker",
|
||||
title: "Spawn Worker on Fly Sprite",
|
||||
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.",
|
||||
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'."),
|
||||
}),
|
||||
async execute(input, context) {
|
||||
try {
|
||||
const worker = await manager.spawnWorker({
|
||||
spriteName: input.spriteName,
|
||||
command: input.command,
|
||||
cwd: input.cwd,
|
||||
syncLocalPath: input.syncWorkspace ? context.agent.cwd : undefined,
|
||||
maxRunAfterDisconnect: input.maxRunAfterDisconnect,
|
||||
});
|
||||
return textResult(
|
||||
`Spawned worker ${worker.workerId} on sprite ${worker.spriteName} (status=${worker.status}, cwd=${worker.cwd}).`,
|
||||
worker,
|
||||
);
|
||||
} catch (err) {
|
||||
return textResult(`Failed to spawn worker: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const spriteExecTool = defineAgentTool({
|
||||
name: "sprite_exec",
|
||||
title: "Execute Command on Fly Sprite",
|
||||
description:
|
||||
"Run a synchronous shell command inside an existing Fly Sprite and return stdout, stderr, and exitCode.",
|
||||
input: z.object({
|
||||
spriteName: z.string().describe("Target Fly Sprite name."),
|
||||
command: z.string().describe("Shell command to execute via bash -lc."),
|
||||
cwd: z.string().optional().describe("Working directory inside the Sprite."),
|
||||
timeoutMs: z.number().int().positive().optional().default(120_000),
|
||||
}),
|
||||
async execute(input) {
|
||||
try {
|
||||
const res = await manager.execCommand({
|
||||
spriteName: input.spriteName,
|
||||
command: input.command,
|
||||
cwd: input.cwd,
|
||||
timeoutMs: input.timeoutMs,
|
||||
});
|
||||
const summary = [
|
||||
`exitCode: ${res.exitCode}`,
|
||||
res.stdout ? `stdout:\n${res.stdout}` : "",
|
||||
res.stderr ? `stderr:\n${res.stderr}` : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join("\n\n");
|
||||
return textResult(summary, res, res.exitCode !== 0);
|
||||
} catch (err) {
|
||||
return textResult(`Exec failed: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
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",
|
||||
description: "Create, list, or restore copy-on-write checkpoints on a Fly Sprite.",
|
||||
input: z.object({
|
||||
action: z.enum(["create", "list", "restore"]),
|
||||
spriteName: z.string(),
|
||||
comment: z.string().optional().describe("Comment when action='create'."),
|
||||
checkpointId: z.string().optional().describe("Checkpoint ID when action='restore'."),
|
||||
}),
|
||||
async execute(input) {
|
||||
try {
|
||||
if (input.action === "list") {
|
||||
const checkpoints = await manager.listCheckpoints(input.spriteName);
|
||||
return textResult(JSON.stringify(checkpoints, null, 2), { checkpoints });
|
||||
}
|
||||
if (input.action === "create") {
|
||||
const messages = await manager.createCheckpoint(input.spriteName, input.comment);
|
||||
return textResult(messages.join("\n") || "Checkpoint created.", { messages });
|
||||
}
|
||||
if (!input.checkpointId) {
|
||||
return textResult("checkpointId is required for action='restore'.", undefined, true);
|
||||
}
|
||||
const messages = await manager.restoreCheckpoint(input.spriteName, input.checkpointId);
|
||||
return textResult(messages.join("\n") || `Restored ${input.checkpointId}.`, { messages });
|
||||
} catch (err) {
|
||||
return textResult(`Checkpoint ${input.action} failed: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const spriteDestroyTool = defineAgentTool({
|
||||
name: "sprite_destroy",
|
||||
title: "Delete Fly Sprite",
|
||||
description: "Delete a Fly Sprite and stop any active tracked workers on it.",
|
||||
input: z.object({
|
||||
spriteName: z.string(),
|
||||
}),
|
||||
async execute(input) {
|
||||
try {
|
||||
await manager.deleteSprite(input.spriteName);
|
||||
return textResult(`Deleted sprite ${input.spriteName}.`, { spriteName: input.spriteName, deleted: true });
|
||||
} catch (err) {
|
||||
return textResult(`Failed to delete sprite: ${errorMessage(err)}`, undefined, true);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return [
|
||||
spriteListTool,
|
||||
spriteCreateTool,
|
||||
spriteSpawnWorkerTool,
|
||||
spriteExecTool,
|
||||
spriteWorkerStatusTool,
|
||||
spriteWorkerStopTool,
|
||||
spriteCheckpointTool,
|
||||
spriteDestroyTool,
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user