Replace forever-on heartbeat billing with a finite lease system: callers request a window of Sprite time and renew as needed; on expiry the heartbeat session stops and the Sprite auto-pauses. - SpriteLeaseStore (server/sprite-lease-store.ts): refcounted leases with TTL clamping ([60s, 8h], default 15min) persisted to sprite-leases.json - SpriteHostKeepalive reimplemented on leases; legacy keepalive-hosts manifest seeds one final window then clears - Provider: session.open acquires, session.prompt renews, session.close releases - sprite_* tools and RPCs acquire/renew/release via sprites.lease.acquire/renew/release/list - destroy/archive release leases before deleting Sprites - Settings: defaultLeaseMinutes (1-480, default 30), schema v2
662 lines
15 KiB
TypeScript
662 lines
15 KiB
TypeScript
import { defineRpc, defineSubscription } from "@getpaseo/plugin";
|
|
import { z } from "zod";
|
|
|
|
export const SpriteSummarySchema = z.object({
|
|
name: z.string(),
|
|
id: z.string().optional(),
|
|
status: z.string(),
|
|
organization: z.string().optional(),
|
|
primaryRegion: z.string().optional(),
|
|
url: z.string().optional(),
|
|
createdAt: z.string().optional(),
|
|
updatedAt: z.string().optional(),
|
|
});
|
|
|
|
export type SpriteSummary = z.output<typeof SpriteSummarySchema>;
|
|
|
|
export const WorkerStatusSchema = z.enum(["starting", "running", "completed", "failed", "stopped"]);
|
|
|
|
export const WorkerSummarySchema = z.object({
|
|
workerId: z.string(),
|
|
spriteName: z.string(),
|
|
command: z.string(),
|
|
cwd: z.string(),
|
|
status: WorkerStatusSchema,
|
|
exitCode: z.number().nullable(),
|
|
error: z.string().nullable(),
|
|
startedAt: z.string(),
|
|
endedAt: z.string().nullable(),
|
|
outputTail: z.string(),
|
|
outputBytes: z.number(),
|
|
});
|
|
|
|
export type WorkerSummary = z.output<typeof WorkerSummarySchema>;
|
|
|
|
export const CheckpointSummarySchema = z.object({
|
|
id: z.string(),
|
|
comment: z.string().optional(),
|
|
createdAt: z.string(),
|
|
isAuto: z.boolean().optional(),
|
|
});
|
|
|
|
export type CheckpointSummary = z.output<typeof CheckpointSummarySchema>;
|
|
|
|
export const SpriteHostSummarySchema = z.object({
|
|
registrationId: z.string(),
|
|
serverId: z.string().nullable(),
|
|
label: z.string(),
|
|
spriteName: z.string(),
|
|
status: z.enum(["error", "connecting", "online", "offline", "disabled"]),
|
|
error: z.string().nullable(),
|
|
});
|
|
|
|
export type SpriteHostSummary = z.output<typeof SpriteHostSummarySchema>;
|
|
|
|
export const SpriteAgentSummarySchema = z.object({
|
|
agentId: z.string(),
|
|
serverId: z.string(),
|
|
hostLabel: z.string(),
|
|
spriteName: z.string(),
|
|
title: z.string().nullable(),
|
|
status: z.string(),
|
|
provider: z.string(),
|
|
model: z.string().nullable(),
|
|
cwd: z.string(),
|
|
lastError: z.string().nullable(),
|
|
pendingPermissionsCount: z.number(),
|
|
updatedAt: z.string(),
|
|
});
|
|
|
|
export type SpriteAgentSummary = z.output<typeof SpriteAgentSummarySchema>;
|
|
|
|
export const TokenSourceSchema = z.enum(["settings", "openbao", "env", "none"]);
|
|
export type TokenSource = z.output<typeof TokenSourceSchema>;
|
|
|
|
export const OpenBaoPendingAuthSchema = z.object({
|
|
flowId: z.string(),
|
|
userCode: z.string(),
|
|
verificationUri: z.string(),
|
|
verificationUriComplete: z.string(),
|
|
expiresAt: z.string(),
|
|
status: z.enum(["pending", "complete", "error"]),
|
|
error: z.string().nullable(),
|
|
});
|
|
|
|
export type OpenBaoPendingAuth = z.output<typeof OpenBaoPendingAuthSchema>;
|
|
|
|
export const OpenBaoStatusSchema = z.object({
|
|
enabled: z.boolean(),
|
|
authenticated: z.boolean(),
|
|
displayName: z.string().nullable(),
|
|
policies: z.array(z.string()),
|
|
tokenPresent: z.boolean(),
|
|
lastPulledAt: z.string().nullable(),
|
|
lastError: z.string().nullable(),
|
|
pendingAuth: OpenBaoPendingAuthSchema.nullable(),
|
|
});
|
|
|
|
export type OpenBaoStatus = z.output<typeof OpenBaoStatusSchema>;
|
|
|
|
export const SpriteJobKindSchema = z.enum([
|
|
"connect_host",
|
|
"launch_agent",
|
|
"create_sprite",
|
|
"sync_workspace",
|
|
]);
|
|
|
|
export type SpriteJobKind = z.output<typeof SpriteJobKindSchema>;
|
|
|
|
export const SpriteJobStatusSchema = z.enum(["running", "succeeded", "failed"]);
|
|
|
|
export type SpriteJobStatus = z.output<typeof SpriteJobStatusSchema>;
|
|
|
|
export const SpriteJobResultSchema = z.discriminatedUnion("kind", [
|
|
z.object({
|
|
kind: z.literal("connect_host"),
|
|
spriteName: z.string(),
|
|
serverId: z.string(),
|
|
registrationId: z.string(),
|
|
remoteDir: z.string(),
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
kind: z.literal("launch_agent"),
|
|
agentId: z.string(),
|
|
serverId: z.string(),
|
|
spriteName: z.string(),
|
|
provider: z.string(),
|
|
remoteDir: z.string(),
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
kind: z.literal("create_sprite"),
|
|
sprite: SpriteSummarySchema,
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
kind: z.literal("sync_workspace"),
|
|
spriteName: z.string(),
|
|
fileCount: z.number(),
|
|
bytes: z.number(),
|
|
remoteDir: z.string(),
|
|
}),
|
|
]);
|
|
|
|
export type SpriteJobResult = z.output<typeof SpriteJobResultSchema>;
|
|
|
|
export const SpriteJobSchema = z.object({
|
|
jobId: z.string(),
|
|
kind: SpriteJobKindSchema,
|
|
spriteName: z.string().nullable(),
|
|
status: SpriteJobStatusSchema,
|
|
phase: z.string(),
|
|
startedAt: z.string(),
|
|
updatedAt: z.string(),
|
|
finishedAt: z.string().nullable(),
|
|
error: z.string().nullable(),
|
|
result: SpriteJobResultSchema.nullable(),
|
|
});
|
|
|
|
export type SpriteJob = z.output<typeof SpriteJobSchema>;
|
|
|
|
export const testConnectionRpc = defineRpc({
|
|
name: "sprites.test",
|
|
input: z.object({}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
spriteCount: z.number(),
|
|
source: z.enum(["settings", "openbao", "env"]),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const statusRpc = defineRpc({
|
|
name: "sprites.status",
|
|
input: z.object({
|
|
includeRemote: z.boolean().optional(),
|
|
}),
|
|
output: z.object({
|
|
configured: z.boolean(),
|
|
tokenSource: TokenSourceSchema,
|
|
remoteError: z.string().nullable(),
|
|
sprites: z.array(SpriteSummarySchema),
|
|
spriteHosts: z.array(SpriteHostSummarySchema),
|
|
spriteAgents: z.array(SpriteAgentSummarySchema),
|
|
agentSprites: z.record(z.string(), z.string()),
|
|
leases: z.array(
|
|
z.object({
|
|
spriteName: z.string(),
|
|
leaseId: z.string(),
|
|
holder: z.object({ kind: z.string(), id: z.string() }),
|
|
reason: z.string(),
|
|
createdAtMs: z.number(),
|
|
updatedAtMs: z.number(),
|
|
expiresAtMs: z.number(),
|
|
}),
|
|
),
|
|
}),
|
|
});
|
|
|
|
export const ensureSpriteHostRpc = defineRpc({
|
|
name: "sprites.host.ensure",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
spriteName: z.string(),
|
|
serverId: z.string(),
|
|
registrationId: z.string(),
|
|
remoteDir: z.string(),
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const startConnectHostRpc = defineRpc({
|
|
name: "sprites.host.connect.start",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
job: SpriteJobSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const launchSpriteAgentRpc = defineRpc({
|
|
name: "sprites.agent.launch",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
title: z.string().optional(),
|
|
prompt: z.string().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
provider: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
agentId: z.string(),
|
|
serverId: z.string(),
|
|
spriteName: z.string(),
|
|
provider: z.string(),
|
|
remoteDir: z.string(),
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const startLaunchAgentRpc = defineRpc({
|
|
name: "sprites.agent.launch.start",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
title: z.string().optional(),
|
|
prompt: z.string().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
provider: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
job: SpriteJobSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const startCreateSpriteRpc = defineRpc({
|
|
name: "sprites.create.start",
|
|
input: z.object({
|
|
name: z.string().optional(),
|
|
region: z.string().optional(),
|
|
ramMB: z.number().int().positive().optional(),
|
|
cpus: z.number().int().positive().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
job: SpriteJobSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const startSyncWorkspaceRpc = defineRpc({
|
|
name: "sprites.sync.start",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
localPath: z.string(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
job: SpriteJobSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const getSpriteJobRpc = defineRpc({
|
|
name: "sprites.job.get",
|
|
input: z.object({
|
|
jobId: z.string(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
job: SpriteJobSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const listSpriteJobsRpc = defineRpc({
|
|
name: "sprites.jobs.list",
|
|
input: z.object({}),
|
|
output: z.object({
|
|
jobs: z.array(SpriteJobSchema),
|
|
}),
|
|
});
|
|
|
|
export const spriteAgentActionRpc = defineRpc({
|
|
name: "sprites.agent.action",
|
|
input: z.object({
|
|
serverId: z.string(),
|
|
agentId: z.string(),
|
|
action: z.enum(["interrupt", "archive"]),
|
|
}),
|
|
output: z.object({
|
|
ok: z.boolean(),
|
|
error: z.string().optional(),
|
|
}),
|
|
});
|
|
|
|
export const openbaoStatusRpc = defineRpc({
|
|
name: "openbao.status",
|
|
input: z.object({}),
|
|
output: OpenBaoStatusSchema,
|
|
});
|
|
|
|
export const openbaoAuthStartRpc = defineRpc({
|
|
name: "openbao.auth.start",
|
|
input: z.object({}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
pendingAuth: OpenBaoPendingAuthSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const openbaoPullRpc = defineRpc({
|
|
name: "openbao.pull",
|
|
input: z.object({}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
status: OpenBaoStatusSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const openbaoLogoutRpc = defineRpc({
|
|
name: "openbao.logout",
|
|
input: z.object({}),
|
|
output: z.object({
|
|
ok: z.literal(true),
|
|
}),
|
|
});
|
|
|
|
export const createSpriteRpc = defineRpc({
|
|
name: "sprites.create",
|
|
input: z.object({
|
|
name: z.string().optional(),
|
|
region: z.string().optional(),
|
|
ramMB: z.number().int().positive().optional(),
|
|
cpus: z.number().int().positive().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
sprite: SpriteSummarySchema,
|
|
syncedFiles: z.number().optional(),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const syncWorkspaceRpc = defineRpc({
|
|
name: "sprites.sync",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
localPath: z.string(),
|
|
remoteDir: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
fileCount: z.number(),
|
|
bytes: z.number(),
|
|
remoteDir: z.string(),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const spawnWorkerRpc = defineRpc({
|
|
name: "sprites.worker.spawn",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
command: z.string(),
|
|
cwd: z.string().optional(),
|
|
syncLocalPath: z.string().optional(),
|
|
maxRunAfterDisconnect: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
worker: WorkerSummarySchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const stopWorkerRpc = defineRpc({
|
|
name: "sprites.worker.stop",
|
|
input: z.object({
|
|
workerId: z.string(),
|
|
}),
|
|
output: z.object({
|
|
ok: z.boolean(),
|
|
error: z.string().optional(),
|
|
}),
|
|
});
|
|
|
|
export const workerLogsRpc = defineRpc({
|
|
name: "sprites.worker.logs",
|
|
input: z.object({
|
|
workerId: z.string(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
worker: WorkerSummarySchema,
|
|
output: z.string(),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const destroySpriteRpc = defineRpc({
|
|
name: "sprites.destroy",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
}),
|
|
output: z.object({
|
|
ok: z.boolean(),
|
|
error: z.string().optional(),
|
|
}),
|
|
});
|
|
|
|
export const listCheckpointsRpc = defineRpc({
|
|
name: "sprites.checkpoint.list",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
checkpoints: z.array(CheckpointSummarySchema),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const createCheckpointRpc = defineRpc({
|
|
name: "sprites.checkpoint.create",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
comment: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
messages: z.array(z.string()),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const restoreCheckpointRpc = defineRpc({
|
|
name: "sprites.checkpoint.restore",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
checkpointId: z.string(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
messages: z.array(z.string()),
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const watchWorkersSub = defineSubscription({
|
|
name: "sprites.watch",
|
|
input: z.object({}),
|
|
event: z.object({
|
|
workers: z.array(WorkerSummarySchema),
|
|
}),
|
|
});
|
|
|
|
// --- Sprite leases (finite keep-alive windows) ---
|
|
|
|
export const LeaseHolderSchema = z.object({
|
|
kind: z.string(),
|
|
id: z.string(),
|
|
});
|
|
|
|
export const SpriteLeaseSchema = z.object({
|
|
spriteName: z.string(),
|
|
leaseId: z.string(),
|
|
holder: LeaseHolderSchema,
|
|
reason: z.string(),
|
|
createdAtMs: z.number(),
|
|
updatedAtMs: z.number(),
|
|
expiresAtMs: z.number(),
|
|
});
|
|
|
|
export type SpriteLease = z.output<typeof SpriteLeaseSchema>;
|
|
|
|
export const acquireLeaseRpc = defineRpc({
|
|
name: "sprites.lease.acquire",
|
|
input: z.object({
|
|
spriteName: z.string(),
|
|
holder: LeaseHolderSchema,
|
|
ttlMs: z.number().int().positive().optional(),
|
|
reason: z.string().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
lease: SpriteLeaseSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const renewLeaseRpc = defineRpc({
|
|
name: "sprites.lease.renew",
|
|
input: z.object({
|
|
leaseId: z.string(),
|
|
ttlMs: z.number().int().positive().optional(),
|
|
}),
|
|
output: z.discriminatedUnion("ok", [
|
|
z.object({
|
|
ok: z.literal(true),
|
|
lease: SpriteLeaseSchema,
|
|
}),
|
|
z.object({
|
|
ok: z.literal(false),
|
|
error: z.string(),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
export const releaseLeaseRpc = defineRpc({
|
|
name: "sprites.lease.release",
|
|
input: z.object({
|
|
leaseId: z.string(),
|
|
}),
|
|
output: z.object({
|
|
ok: z.boolean(),
|
|
}),
|
|
});
|
|
|
|
export const listLeasesRpc = defineRpc({
|
|
name: "sprites.lease.list",
|
|
input: z.object({
|
|
spriteName: z.string().optional(),
|
|
}),
|
|
output: z.object({
|
|
leases: z.array(SpriteLeaseSchema),
|
|
}),
|
|
});
|
|
|