feat(frontend): migrate render jobs off Supabase to V2 render orchestrator; drop all Supabase lib files

- render-jobs.ts: replace Supabase client with V2 gateway calls
  POST /v1/renders (saved_project_id + quality + resolution + frame_rate)
  GET  /v1/renders/:id/progress for status polling
  GET  /v1/renders/:id + /v1/exports/:id/download-url for completed output URL
  triggerRenderWorker is now a no-op (V2 dispatches internally)
- render/route.ts: add getAccessToken() guard, pass token to createRenderJob
- render/[jobId]/status/route.ts: add getAccessToken() guard, pass token to getRenderJob
- Delete src/lib/supabase/, src/lib/supabase.ts — no remaining consumers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-01 08:41:58 +03:30
parent 9555044485
commit a076c4911f
9 changed files with 153 additions and 172 deletions
+138 -49
View File
@@ -1,5 +1,27 @@
import { createAdminClient } from "@/lib/supabase/admin";
import type { RenderRequest, RenderJobStatus } from "@/lib/render-schemas";
import { gatewayUrl } from "@/lib/api/gateway";
import type { RenderRequest } from "@/lib/render-schemas";
// ── V2 render service types ──────────────────────────────────────────────────
type V2RenderStep =
| "Queued"
| "Preparing"
| "TemplateCache"
| "JsxGen"
| "Music"
| "Rendering"
| "Validating"
| "Repairing"
| "Optimisation"
| "Video"
| "Mixing"
| "Final"
| "Uploading"
| "Done"
| "Failed"
| "Cancelled";
export type RenderJobStatus = "queued" | "processing" | "completed" | "failed";
export interface RenderJobRow {
id: string;
@@ -8,69 +30,136 @@ export interface RenderJobRow {
progress: number;
progress_message: string | null;
output_url: string | null;
scenes: RenderRequest["scenes"];
settings: RenderRequest["settings"];
error_message: string | null;
created_at: string;
updated_at: string;
}
// ── Helpers ──────────────────────────────────────────────────────────────────
/** Map frontend resolution string to V2 quality enum. */
function mapQuality(resolution: string): string {
if (resolution === "4K") return "Full";
if (resolution === "720p") return "Medium";
return "High"; // 1080p default
}
/** Map V2 step → legacy status for frontend compatibility. */
function stepToStatus(step: V2RenderStep): RenderJobStatus {
if (step === "Done") return "completed";
if (step === "Failed" || step === "Cancelled") return "failed";
if (step === "Queued") return "queued";
return "processing";
}
function authHeaders(token: string): Record<string, string> {
return {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
};
}
// ── Public API ───────────────────────────────────────────────────────────────
/**
* Create a render job in the V2 render orchestrator.
* `payload.projectId` must be the UUID of a V2 saved project.
*/
export async function createRenderJob(
payload: RenderRequest
payload: RenderRequest,
token: string
): Promise<{ jobId: string } | { error: string }> {
const supabase = createAdminClient();
const res = await fetch(gatewayUrl("/v1/renders"), {
method: "POST",
cache: "no-store",
headers: authHeaders(token),
body: JSON.stringify({
saved_project_id: payload.projectId,
quality: mapQuality(payload.settings.resolution),
resolution: payload.settings.resolution,
frame_rate: payload.settings.fps,
}),
});
const { data, error } = await supabase
.from("render_jobs")
.insert({
project_id: payload.projectId,
status: "queued",
progress: 0,
progress_message: "Queued for rendering",
scenes: payload.scenes,
settings: payload.settings,
})
.select("id")
.single();
if (error || !data) {
return { error: error?.message ?? "Failed to create render job" };
if (!res.ok) {
const err = (await res.json().catch(() => null)) as {
message?: string;
} | null;
return { error: err?.message ?? "Failed to create render job" };
}
const data = (await res.json().catch(() => null)) as { id?: string } | null;
if (!data?.id) return { error: "No job ID returned from render service" };
return { jobId: data.id };
}
/**
* Fetch the current render job status from V2.
* If the job is completed, also resolves the presigned export download URL.
*/
export async function getRenderJob(
jobId: string
jobId: string,
token: string
): Promise<RenderJobRow | null> {
const supabase = createAdminClient();
// 1. Poll progress endpoint
const progressRes = await fetch(gatewayUrl(`/v1/renders/${jobId}/progress`), {
cache: "no-store",
headers: authHeaders(token),
});
if (!progressRes.ok) return null;
const { data, error } = await supabase
.from("render_jobs")
.select("*")
.eq("id", jobId)
.maybeSingle();
const progress = (await progressRes.json().catch(() => null)) as {
step?: V2RenderStep;
progress?: number;
message?: string;
} | null;
if (!progress) return null;
if (error || !data) return null;
return data as RenderJobRow;
}
const step = progress.step ?? "Queued";
const status = stepToStatus(step);
export async function triggerRenderWorker(jobId: string): Promise<void> {
const workerUrl = process.env.RENDER_WORKER_URL;
if (!workerUrl) return;
const secret = process.env.RENDER_WORKER_SECRET;
try {
await fetch(`${workerUrl.replace(/\/$/, "")}/process`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(secret ? { Authorization: `Bearer ${secret}` } : {}),
},
body: JSON.stringify({ jobId }),
// 2. If done, resolve the export download URL via MinIO presigned link
let outputUrl: string | null = null;
if (status === "completed") {
const jobRes = await fetch(gatewayUrl(`/v1/renders/${jobId}`), {
cache: "no-store",
headers: authHeaders(token),
});
} catch {
// Worker may be offline; job stays queued for retry/poll
if (jobRes.ok) {
const job = (await jobRes.json().catch(() => null)) as {
export_id?: string | null;
} | null;
if (job?.export_id) {
const urlRes = await fetch(
gatewayUrl(`/v1/exports/${job.export_id}/download-url`),
{ cache: "no-store", headers: authHeaders(token) }
);
if (urlRes.ok) {
const urlData = (await urlRes.json().catch(() => null)) as {
url?: string;
} | null;
outputUrl = urlData?.url ?? null;
}
}
}
}
return {
id: jobId,
project_id: "",
status,
progress: progress.progress ?? 0,
progress_message: step,
output_url: outputUrl,
error_message:
status === "failed" ? (progress.message ?? "Render failed") : null,
};
}
/**
* No-op in V2 — the render orchestrator dispatches jobs to node agents
* automatically. Kept for API compatibility.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function triggerRenderWorker(_jobId: string): Promise<void> {
// V2 render service handles dispatch internally.
}