| 1 | import type { Connections, Consent } from "./types/mcp.ts"; |
| 2 | import type { Pool, Vdev, Disk, Dataset, Snapshot } from "./types/storage.ts"; |
| 3 | import type { Torrent, TorrentFile, ServerState } from "./types/seedbox.ts"; |
| 4 | import type { Video, Show, Job, Wall, Archive, Upscaler, Channels, ConfigFile, LibraryEntry, Ingest } from "./types/youtube.ts"; |
| 5 | import type { User, Group, Session, Credential } from "./types/users.ts"; |
| 6 | import type { Hono } from "hono"; |
| 7 | import type { Health } from "./types/model.ts"; |
| 8 | import type { Domain, Image, History, NewDomain } from "./types/vms.ts"; |
| 9 | import type { Me, ServiceSummary, ServiceDetail, ServiceDefinition, HostInfo, Issue, Series, LogLine, TraceSummary, Trace } from "./types/model.ts"; |
| 10 | import type { MapNode } from "./types/storage.index.ts"; |
| 11 | import type { ProgressNode, PaperCloverStats } from "./types/paperClover.ts"; |
| 12 | |
| 13 | type Endpoint<Output, Input = {}, Status extends number = 200, Format extends string = "json"> = { |
| 14 | input: Input; output: Output; status: Status; outputFormat: Format; |
| 15 | }; |
| 16 | |
| 17 | type FileEntry = { name: string; dir: boolean; size: number | null; alloc: number | null; items: number | null; modified: number; inode: number; dataset: string | null; download: string | null; }; |
| 18 | |
| 19 | type Explorer = { |
| 20 | "/list": { |
| 21 | $get: Endpoint<{ host: string; link: string | null; zip: string | null; entries: FileEntry[]; }, { query: { path?: string | undefined; }; }>; |
| 22 | }; |
| 23 | "/tree": { |
| 24 | $post: Endpoint<{ folders: { [x: string]: FileEntry[]; }; sizes: { [x: string]: { size: number; alloc: number; files: number; }; }; complete: boolean; updated: number | null; }, { json: { path: string; expanded: string[] | "all"; }; }>; |
| 25 | }; |
| 26 | "/match": { |
| 27 | $get: Endpoint<{ count: number; size: number | null; sample: string[]; rows: { [x: string]: number | "self"; }; }, { query: { path: string; pattern: string; }; }>; |
| 28 | }; |
| 29 | "/peek": { |
| 30 | $get: Endpoint<{ text: string; more: boolean; }, { query: { path: string; }; }>; |
| 31 | }; |
| 32 | "/ops": { |
| 33 | $get: Endpoint<{ id: string; label: string; at: number; undone: boolean; sample: string[]; count: number; }[]>; |
| 34 | }; |
| 35 | "/folder": { |
| 36 | $post: Endpoint<{ id: `${string}-${string}-${string}-${string}-${string}`; label: string; }, { json: { path: string; name: string; }; }>; |
| 37 | }; |
| 38 | "/rename": { |
| 39 | $post: Endpoint<{ id: `${string}-${string}-${string}-${string}-${string}`; label: string; }, { json: { renames: { path: string; name: string; }[]; }; }>; |
| 40 | }; |
| 41 | "/move": { |
| 42 | $post: Endpoint<{ id: `${string}-${string}-${string}-${string}-${string}`; label: string; }, { json: { paths: string[]; to: string; }; }>; |
| 43 | }; |
| 44 | "/delete": { |
| 45 | $post: Endpoint<{ id: `${string}-${string}-${string}-${string}-${string}`; label: string; }, { json: { paths: string[]; }; }>; |
| 46 | }; |
| 47 | "/bulk": { |
| 48 | $post: Endpoint<{ id: `${string}-${string}-${string}-${string}-${string}`; label: string; }, { json: { path: string; pattern: string; count: number; action: "move" | "delete"; to?: string | undefined; }; }>; |
| 49 | }; |
| 50 | "/ops/:id/undo": { |
| 51 | $post: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 52 | }; |
| 53 | }; |
| 54 | type ExplorerRoutes<Prefix extends string> = { [P in keyof Explorer as `${Prefix}${P}`]: Explorer[P] }; |
| 55 | |
| 56 | export type Api = Hono<{}, { |
| 57 | "/mcp": { $get: Endpoint<Connections>; }; |
| 58 | "/mcp/shale": { $post: Endpoint<{ redirect: string }, { json: { request?: string } }>; $delete: Endpoint<null, {}, 204>; }; |
| 59 | "/mcp/consent/:id": { |
| 60 | $get: Endpoint<Consent, { param: { id: string } }>; |
| 61 | $post: Endpoint<{ redirect: string }, { param: { id: string }; json: { resources: string[] } | { deny: true } }>; |
| 62 | }; |
| 63 | "/mcp/relay/pair": { $post: Endpoint<Connections["machines"][number], { json: { code: string } }>; }; |
| 64 | "/mcp/relay/machines/:id": { |
| 65 | $patch: Endpoint<Connections["machines"][number], { param: { id: string }; json: { name: string } }>; |
| 66 | $delete: Endpoint<null, { param: { id: string } }, 204>; |
| 67 | }; |
| 68 | "/mcp/relay/keys": { $post: Endpoint<{ key: string; id: string }, { json: { name: string; resources: string[]; write: boolean } }>; }; |
| 69 | "/mcp/connections/:id": { $delete: Endpoint<null, { param: { id: string } }, 204>; }; |
| 70 | |
| 71 | "/me": { |
| 72 | $get: Endpoint<Me>; |
| 73 | }; |
| 74 | "/launcher": { |
| 75 | $get: Endpoint<{ id: string; name: string; tagline: string | null; url: string; urls: Record<string, string>; icon: { light: string; dark: string; } | null; health: Health | null; }[]>; |
| 76 | }; |
| 77 | "/status": { |
| 78 | $get: Endpoint<{ load: number; issues: Issue[] | null }>; |
| 79 | }; |
| 80 | "/host": { |
| 81 | $get: Endpoint<HostInfo>; |
| 82 | }; |
| 83 | "/services": { |
| 84 | $get: Endpoint<ServiceSummary[]>; |
| 85 | }; |
| 86 | "/services/:id": { |
| 87 | $get: Endpoint<ServiceDetail, { param: { id: string; }; }>; |
| 88 | }; |
| 89 | "/services/:id/definition": { |
| 90 | $get: Endpoint<ServiceDefinition, { param: { id: string; }; }>; |
| 91 | }; |
| 92 | "/services/:id/:action": { |
| 93 | $post: Endpoint<null, { param: { id: string; action: "stop" | "restart" | "start"; }; }, 204, "body">; |
| 94 | }; |
| 95 | "/services/:id/restarts/acknowledge": { |
| 96 | $post: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 97 | }; |
| 98 | "/services/:id/secrets/:name": { |
| 99 | $get: Endpoint<{ value: string; }, { param: { name: string; } & { id: string; }; }>; |
| 100 | $put: Endpoint<null, { json: { value: string; }; } & { param: { name: string; } & { id: string; }; }, 204, "body">; |
| 101 | }; |
| 102 | "/services/:id/secrets/:name/rotate": { |
| 103 | $post: Endpoint<null, { param: { name: string; } & { id: string; }; }, 204, "body">; |
| 104 | }; |
| 105 | "/services/:id/logs": { |
| 106 | $get: Endpoint<LogLine[], { query: { q?: string | undefined; level?: "error" | "warn" | undefined; before?: string | string[] | undefined; after?: string | string[] | undefined; limit?: string | string[] | undefined; }; } & { param: { id: string; }; }>; |
| 107 | }; |
| 108 | "/services/:id/metrics/:name": { |
| 109 | $get: Endpoint<Series[], { param: { id: string; name: string; }; } & { query: { range?: string | string[] | undefined; }; }>; |
| 110 | }; |
| 111 | "/services/:id/traces": { |
| 112 | $get: Endpoint<TraceSummary[], { query: { q?: string | undefined; errors?: "1" | undefined; sort?: "newest" | "oldest" | "slowest" | "fastest" | undefined; limit?: string | string[] | undefined; }; } & { param: { id: string; }; }>; |
| 113 | }; |
| 114 | "/traces/:id": { |
| 115 | $get: Endpoint<Trace, { param: { id: string; }; }>; |
| 116 | }; |
| 117 | "/metrics/:metric": { |
| 118 | $get: Endpoint<Series[], { param: { metric: "host.cpu" | "host.memory" | "host.temperature" | "host.power" | "host.gpu" | "host.network" | "service.cpu" | "service.memory" | "vm.cpu" | "vm.memory"; }; } & { query: { range?: string | string[] | undefined; service?: string | undefined; }; }>; |
| 119 | }; |
| 120 | "/live": { |
| 121 | $get: Endpoint<{}, {}, 200, string>; |
| 122 | }; |
| 123 | "/icons/:id/:file": { |
| 124 | $get: Endpoint<Uint8Array, { param: { id: string; file: "icon.svg" | "icon-dark.svg" | "icon-light.svg"; }; }, 200, "body">; |
| 125 | }; |
| 126 | "/storage": { |
| 127 | $get: Endpoint<{ pool: Omit<Pool, "vdevs"> & { vdevs: (Omit<Vdev, "disks"> & { disks: (Disk & { issue: string | null })[] })[] }; datasets: (Dataset & { link: string | null; media: string | null })[]; space: { live: number; held: number; free: number } }>; |
| 128 | }; |
| 129 | "/storage/snapshots": { |
| 130 | $get: Endpoint<Snapshot[], { query: { dataset: string; }; }>; |
| 131 | }; |
| 132 | "/storage/reclaim": { |
| 133 | $get: Endpoint<{ bytes: number; }, { query: { dataset: string; from: string; to: string; }; }>; |
| 134 | }; |
| 135 | "/storage/removed": { |
| 136 | $get: Endpoint<{ count: number; largest: { path: string; size: number; }[]; }, { query: { dataset: string; snapshot: string; }; }>; |
| 137 | }; |
| 138 | "/storage/destroy": { |
| 139 | $post: Endpoint<null, { json: { dataset: string; from: string; to: string; }; }, 204, "body">; |
| 140 | }; |
| 141 | "/storage/files/map": { |
| 142 | $get: Endpoint<{ root: string; min: number; scanning: boolean; tree: MapNode | null }, { query: { width: string | string[]; height: string | string[]; path?: string | undefined; }; }>; |
| 143 | }; |
| 144 | "/storage/files/largest": { |
| 145 | $get: Endpoint<{ link: string | null; path: string; size: number; alloc: number; mtime: number; }[]>; |
| 146 | }; |
| 147 | "/media/refresh": { |
| 148 | $post: Endpoint<null, {}, 202, "body">; |
| 149 | }; |
| 150 | "/seedbox": { |
| 151 | $get: Endpoint<{state: ServerState; downloads: string | null; torrents: (Torrent & {folder: string | null; zip: string | null})[]}>; |
| 152 | }; |
| 153 | "/seedbox/history": { |
| 154 | $get: Endpoint<Series[]>; |
| 155 | }; |
| 156 | "/seedbox/torrents": { |
| 157 | $post: Endpoint<null, { json: { url: string; }; }, 204, "body">; |
| 158 | }; |
| 159 | "/seedbox/torrents/:hash/files": { |
| 160 | $get: Endpoint<(TorrentFile & {link: string | null})[], { param: { hash: string; }; }>; |
| 161 | }; |
| 162 | "/seedbox/torrents/:hash/:action": { |
| 163 | $post: Endpoint<null, { param: { hash: string; action: "stop" | "start" | "topPrio" | "increasePrio" | "decreasePrio" | "bottomPrio"; }; }, 204, "body">; |
| 164 | }; |
| 165 | "/seedbox/torrents/:hash": { |
| 166 | $delete: Endpoint<null, { param: { hash: string; }; } & { query: { files?: "0" | "1" | undefined; }; }, 204, "body">; |
| 167 | }; |
| 168 | "/youtube": { |
| 169 | $get: Endpoint<{pending:Video[]; shows:Show[]; jobs:Job[]; wall:Wall; archive:Archive; upscaler:Upscaler}>; |
| 170 | }; |
| 171 | "/youtube/channels": { |
| 172 | $get: Endpoint<Channels>; |
| 173 | $put: Endpoint<null, { json: Channels }, 204, "body">; |
| 174 | }; |
| 175 | "/youtube/configs": { |
| 176 | $get: Endpoint<ConfigFile[]>; |
| 177 | }; |
| 178 | "/youtube/configs/:name": { |
| 179 | $put: Endpoint<null, { json: { original: string; body: string; }; } & { param: { name: string; }; }, 204, "body">; |
| 180 | }; |
| 181 | "/youtube/library": { |
| 182 | $get: Endpoint<LibraryEntry[]>; |
| 183 | }; |
| 184 | "/youtube/library/rename": { |
| 185 | $post: Endpoint<null, { json: { path: string; title: string; season: number | null; episode: number | null; }; }, 204, "body">; |
| 186 | }; |
| 187 | "/youtube/pending": { |
| 188 | $post: Endpoint<null, { json: { urls: string[]; }; }, 204, "body">; |
| 189 | }; |
| 190 | "/youtube/pending/:key/ingest": { |
| 191 | $post: Endpoint<null, { json: Ingest; param: { key: string } }, 204, "body">; |
| 192 | }; |
| 193 | "/youtube/pending/:key/skip": { |
| 194 | $post: Endpoint<null, { param: { key: string; }; }, 204, "body">; |
| 195 | }; |
| 196 | "/youtube/jobs/:id/retry": { |
| 197 | $post: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 198 | }; |
| 199 | "/youtube/upscaler": { |
| 200 | $put: Endpoint<null, { json: { enabled: boolean; }; }, 204, "body">; |
| 201 | }; |
| 202 | "/paper-clover": { |
| 203 | $get: Endpoint<PaperCloverStats & { browse: string | null }>; |
| 204 | }; |
| 205 | "/paper-clover/activity": { |
| 206 | $get: Endpoint<ProgressNode[]>; |
| 207 | }; |
| 208 | "/paper-clover/scan": { |
| 209 | $post: Endpoint<null, { json: { path: string | null; }; }, 202, "body">; |
| 210 | }; |
| 211 | "/users": { |
| 212 | $get: Endpoint<{users:(User & {sessions:Session[]})[]; groups:Group[]}>; |
| 213 | $post: Endpoint<{ id: string; }, { json: { profile: { email: string; firstName: string; lastName: string; username: string; }; groups: string[]; setup: { kind: "email"; } | { kind: "password"; password: string; }; }; }, 201>; |
| 214 | }; |
| 215 | "/users/:id": { |
| 216 | $patch: Endpoint<null, { param: { id: string; }; } & { json: { username?: string | undefined; email?: string | undefined; firstName?: string | undefined; lastName?: string | undefined; enabled?: boolean | undefined; emailVerified?: boolean | undefined; requiredActions?: string[] | undefined; }; }, 204, "body">; |
| 217 | $delete: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 218 | }; |
| 219 | "/users/:id/groups/:group": { |
| 220 | $put: Endpoint<null, { param: { id: string; group: string; }; }, 204, "body">; |
| 221 | $delete: Endpoint<null, { param: { id: string; group: string; }; }, 204, "body">; |
| 222 | }; |
| 223 | "/users/:id/credentials": { |
| 224 | $get: Endpoint<Credential[], { param: { id: string; }; }>; |
| 225 | }; |
| 226 | "/users/:id/logout": { |
| 227 | $post: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 228 | }; |
| 229 | "/users/:id/actions-email": { |
| 230 | $post: Endpoint<null, { param: { id: string; }; }, 204, "body">; |
| 231 | }; |
| 232 | "/users/:id/password": { |
| 233 | $put: Endpoint<null, { param: { id: string; }; } & { json: { password: string; temporary: boolean; }; }, 204, "body">; |
| 234 | }; |
| 235 | "/deploys": { |
| 236 | $get: Endpoint<{ current: string | null; main: { release: string; commit: string; description: string } | null; recorded: boolean; history: { n: number; changed: string[] | null; redeploys: string[] | null; release: string; source: string; time: number; legacy: boolean; }[]; stages: { files: string | null; base: number | null; changed: string[] | null; behind: string[]; id: string; service: string; release: string | null; ready: boolean; overrides: string[]; created: number; clone: string | null; mount: string; hostname: string | null; health: Health | null; }[]; run: { id: string; title: string; target: string; code: number | null; } | null; }>; |
| 237 | }; |
| 238 | "/deploys/changes": { |
| 239 | $get: Endpoint<{ id: string; lines: [op: " " | "-" | "+", text: string][]; }[], { query: { to: string; from?: string | undefined; }; }>; |
| 240 | }; |
| 241 | "/deploys/stages/:id/runtime": { |
| 242 | $get: Endpoint<{ from: number; to: number | null; jobs: { id: string; allocations: { id: string; state: "pending" | "running" | "complete" | "failed" | "lost" | "unknown"; healthy: boolean | null; created: number; ended: number | null; tasks: { name: string; restarts: number; lastRestart: { t: number; reason: string; } | null; }[]; checks: { name: string; passing: boolean; output: string; }[]; }[]; cpu: { name: string; t: number[]; v: (number | null)[]; } | null; memory: { name: string; t: number[]; v: (number | null)[]; } | null; }[]; }, { param: { id: string; }; }>; |
| 243 | }; |
| 244 | "/deploys/history/:n/runtime": { |
| 245 | $get: Endpoint<{ from: number; to: number | null; jobs: { id: string; allocations: { id: string; state: "pending" | "running" | "complete" | "failed" | "lost" | "unknown"; healthy: boolean | null; created: number; ended: number | null; tasks: { name: string; restarts: number; lastRestart: { t: number; reason: string; } | null; }[]; checks: { name: string; passing: boolean; output: string; }[]; }[]; cpu: { name: string; t: number[]; v: (number | null)[]; } | null; memory: { name: string; t: number[]; v: (number | null)[]; } | null; }[]; }, { param: { n: string; }; }>; |
| 246 | }; |
| 247 | "/deploys/stages/:id/logs": { |
| 248 | $get: Endpoint<LogLine[], { param: { id: string }; query: { job?: string; after?: string | string[]; before?: string | string[]; limit?: string | string[] } }>; |
| 249 | }; |
| 250 | "/deploys/history/:n/logs": { |
| 251 | $get: Endpoint<LogLine[], { param: { n: string }; query: { job?: string; after?: string | string[]; before?: string | string[]; limit?: string | string[] } }>; |
| 252 | }; |
| 253 | "/deploys/stages/:id/output": { |
| 254 | $get: Endpoint<{ lines: string[] | null; }, { param: { id: string; }; }>; |
| 255 | }; |
| 256 | "/deploys/history/:n/output": { |
| 257 | $get: Endpoint<{ lines: string[] | null; }, { param: { n: string; }; }>; |
| 258 | }; |
| 259 | "/deploys/main/:release/deploy": { |
| 260 | $post: Endpoint<{ id: string; title: string; target: string; }, { param: { release: string; }; }>; |
| 261 | }; |
| 262 | "/deploys/stages/:id/destroy": { |
| 263 | $post: Endpoint<{ id: string; title: string; target: string; }, { param: { id: string; }; }>; |
| 264 | }; |
| 265 | "/deploys/history/:n/rollback": { |
| 266 | $post: Endpoint<{ id: string; title: string; target: string; }, { param: { n: string; }; }>; |
| 267 | }; |
| 268 | "/deploys/runs/:id": { |
| 269 | $get: Endpoint<{}, { param: { id: string; }; }, 200, string>; |
| 270 | }; |
| 271 | "/vms": { |
| 272 | $get: Endpoint<{ node: { cpus: number; memory: number }; domains: Domain[]; images: Image[] }>; |
| 273 | $post: Endpoint<null, { json: NewDomain }, 204, "body">; |
| 274 | }; |
| 275 | "/vms/history": { |
| 276 | $get: Endpoint<History, { query: { range?: string | string[]; name?: string } }>; |
| 277 | }; |
| 278 | "/vms/:name": { |
| 279 | $patch: Endpoint<null, { param: { name: string; }; } & { json: { autostart?: boolean | undefined; description?: string | undefined; }; }, 204, "body">; |
| 280 | $delete: Endpoint<null, { param: { name: string; }; } & { query: { disks?: "0" | "1" | undefined; }; }, 204, "body">; |
| 281 | }; |
| 282 | "/vms/:name/:action": { |
| 283 | $post: Endpoint<null, { param: { name: string; action: "start" | "destroy" | "shutdown" | "reboot" | "resume"; }; }, 204, "body">; |
| 284 | }; |
| 285 | "/account": { |
| 286 | $get: Endpoint<User & {picture: string | null; credentials:Credential[]; console: string | null}>; |
| 287 | $patch: Endpoint<null, { json: { firstName?: string | undefined; lastName?: string | undefined; }; }, 204, "body">; |
| 288 | }; |
| 289 | "/account/verify-email": { |
| 290 | $post: Endpoint<null, {}, 204, "body">; |
| 291 | }; |
| 292 | "/account/picture": { |
| 293 | $put: Endpoint<{ picture: string; }, { form: { picture: File; }; }>; |
| 294 | $delete: Endpoint<null, {}, 204, "body">; |
| 295 | }; |
| 296 | "/account/pictures/:id": { |
| 297 | $get: Endpoint<Uint8Array, { param: { id: string; }; }, 200, "body">; |
| 298 | }; |
| 299 | "/account/actions/:action": { |
| 300 | $get: Endpoint<undefined, { param: { action: string; }; }, 302, "redirect">; |
| 301 | }; |
| 302 | "/account/sign-out": { |
| 303 | $get: Endpoint<undefined, {}, 302, "redirect">; |
| 304 | }; |
| 305 | } & ExplorerRoutes<"/media"> & ExplorerRoutes<"/storage/files">>; |