fix: handle file upload status correctly, add serverless machine type

This commit is contained in:
BennyKok
2023-12-24 18:53:32 +08:00
parent dc5ae7a7b1
commit 850d8473ad
18 changed files with 1716 additions and 207 deletions
+41 -18
View File
@@ -7,6 +7,7 @@ import { ComfyAPI_Run } from "@/types/ComfyAPI_Run";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import "server-only";
import { v4 } from "uuid";
export const createRun = withServerPromise(
async (
@@ -37,8 +38,6 @@ export const createRun = withServerPromise(
throw new Error("Workflow version not found");
}
const comfyui_endpoint = `${machine.endpoint}/comfyui-deploy/run`;
const workflow_api = workflow_version_data.workflow_api;
// Replace the inputs
@@ -52,33 +51,57 @@ export const createRun = withServerPromise(
}
}
const body = {
let prompt_id: string | undefined = undefined;
const shareData = {
workflow_api: workflow_api,
status_endpoint: `${origin}/api/update-run`,
file_upload_endpoint: `${origin}/api/file-upload`,
};
// console.log(body);
const bodyJson = JSON.stringify(body);
// console.log(bodyJson);
// Sending to comfyui
const _result = await fetch(comfyui_endpoint, {
method: "POST",
body: bodyJson,
cache: "no-store",
});
if (!_result.ok) {
throw new Error(`Error creating run, ${_result.statusText}`);
switch (machine.type) {
case "runpod-serverless":
prompt_id = v4();
const data = {
input: {
...shareData,
prompt_id: prompt_id,
},
};
console.log(data);
const __result = await fetch(`${machine.endpoint}/run`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${machine.auth_token}`,
},
body: JSON.stringify(data),
cache: "no-store",
});
console.log(__result);
if (!__result.ok)
throw new Error(`Error creating run, ${__result.statusText}`);
console.log(data, __result);
break;
case "classic":
const body = shareData;
const comfyui_endpoint = `${machine.endpoint}/comfyui-deploy/run`;
const _result = await fetch(comfyui_endpoint, {
method: "POST",
body: JSON.stringify(body),
cache: "no-store",
});
if (!_result.ok)
throw new Error(`Error creating run, ${_result.statusText}`);
const result = await ComfyAPI_Run.parseAsync(await _result.json());
prompt_id = result.prompt_id;
break;
}
const result = await ComfyAPI_Run.parseAsync(await _result.json());
// Add to our db
const workflow_run = await db
.insert(workflowRunsTable)
.values({
id: result.prompt_id,
id: prompt_id,
workflow_id: workflow_version_data.workflow_id,
workflow_version_id: workflow_version_data.id,
workflow_inputs: inputs,