311 lines
7.2 KiB
TypeScript
311 lines
7.2 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 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 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),
|
|
workers: z.array(WorkerSummarySchema),
|
|
}),
|
|
});
|
|
|
|
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),
|
|
}),
|
|
});
|