Add workflow management endpoints and enhance deployment logic

- Introduced new endpoints for creating workflows and versions in the custom_routes.py file.
- Updated the deployWorkflow function in index.js to include apiUrl in the request body and handle workflow versioning.
- Improved error handling for API requests and ensured required fields are validated before processing.
- Enhanced user feedback during deployment with updated success messages.

These changes streamline workflow management and improve the overall deployment process within the application.
This commit is contained in:
KarrixLee
2025-06-16 16:32:23 +08:00
parent c015b710fe
commit a91effe3c8
2 changed files with 152 additions and 40 deletions
+83
View File
@@ -2910,3 +2910,86 @@ async def auth_response_proxy(request):
return web.json_response(json_data, status=response.status)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
@server.PromptServer.instance.routes.post("/comfyui-deploy/workflow")
async def create_workflow_proxy(request):
data = await request.json()
name = data.get("name")
workflow_json = data.get("workflow_json")
workflow_api = data.get("workflow_api")
api_url = data.get("api_url", "https://api.comfydeploy.com")
auth_header = request.headers.get("Authorization")
if not auth_header:
return web.json_response(
{"error": "Authorization header is required"}, status=401
)
if not name or not workflow_json or not workflow_api:
return web.json_response(
{"error": "name, workflow_json, workflow_api are required"}, status=400
)
target_url = f"{api_url}/api/workflow"
request_body = {
"name": name,
"workflow_json": json.dumps(workflow_json),
"workflow_api": json.dumps(workflow_api),
}
try:
await ensure_client_session()
async with client_session.post(
target_url,
json=request_body,
headers={
"Content-Type": "application/json",
"Authorization": auth_header,
},
) as response:
json_data = await response.json()
return web.json_response(json_data, status=response.status)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
@server.PromptServer.instance.routes.post("/comfyui-deploy/workflow/version")
async def create_workflow_version_proxy(request):
data = await request.json()
workflow_id = data.get("workflow_id")
workflow = data.get("workflow")
workflow_api = data.get("workflow_api")
comment = data.get("comment", "")
api_url = data.get("api_url", "https://api.comfydeploy.com")
auth_header = request.headers.get("Authorization")
if not auth_header:
return web.json_response(
{"error": "Authorization header is required"}, status=401
)
target_url = f"{api_url}/api/workflow/{workflow_id}/version"
request_body = {
"workflow": workflow,
"workflow_api": workflow_api,
"comment": comment,
}
try:
await ensure_client_session()
async with client_session.post(
target_url,
json=request_body,
headers={
"Content-Type": "application/json",
"Authorization": auth_header,
},
) as response:
json_data = await response.json()
return web.json_response(json_data, status=response.status)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
+69 -40
View File
@@ -1112,9 +1112,9 @@ async function deployWorkflow() {
/** @type {LGraph} */
const graph = app.graph;
let { endpoint, apiKey, displayName } = getData();
let { endpoint, apiKey, apiUrl, displayName } = getData();
if (!endpoint || !apiKey || apiKey === "" || endpoint === "") {
if (!endpoint || !apiKey || apiKey === "" || endpoint === "" || !apiUrl) {
configDialog.show();
return;
}
@@ -1158,7 +1158,7 @@ async function deployWorkflow() {
<br><br>
<label>
<input id="include-deps" type="checkbox" checked>Include dependency</input>
<input id="include-deps" type="checkbox">Include dependency</input>
</label>
<br>
<label>
@@ -1172,13 +1172,65 @@ async function deployWorkflow() {
const includeDeps = document.getElementById("include-deps").checked;
const reuseHash = document.getElementById("reuse-hash").checked;
const prompt = await app.graphToPrompt();
let deps = undefined;
console.log(prompt);
if (workflow_id.trim() !== "") {
const text = await inputDialog.input("Save changes", "Comment");
if (!text) return;
try {
loadingDialog.showLoading("Saving changes");
const body = {
api_url: apiUrl,
workflow: prompt.workflow,
workflow_id: workflow_id,
workflow_api: prompt.output,
comment: text,
};
let data = await fetch("/comfyui-deploy/workflow/version", {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
},
});
if (data.status !== 200) {
throw new Error(await data.text());
} else {
data = await data.json();
console.log(data);
infoDialog.show(
`<span style="color:green;">New version created!</span> <br/> <br/> Workflow ID: ${data.workflow_id} <br/> Workflow Name: ${workflow_name} <br/> Workflow Version: ${data.version} <br/>`
);
deployMetaNode.widgets[2].value = data.version;
graph.change();
}
} catch (e) {
infoDialog.showError("Error", e.message);
return;
} finally {
loadingDialog.close();
return;
}
}
if (endpoint.endsWith("/")) {
endpoint = endpoint.slice(0, -1);
}
loadingDialog.showLoading("Generating snapshot");
const snapshot = await fetch("/snapshot/get_current").then((x) => x.json());
// console.log(snapshot);
console.log(snapshot);
loadingDialog.close();
if (!snapshot) {
@@ -1191,16 +1243,11 @@ async function deployWorkflow() {
const title = deploy.querySelector("#button-title");
const prompt = await app.graphToPrompt();
let deps = undefined;
console.log(prompt);
if (includeDeps) {
loadingDialog.showLoading("Fetching existing version");
const existing_workflow = await fetch(
endpoint + "/api/workflow/" + workflow_id,
apiUrl + "/api/workflow/" + workflow_id,
{
method: "GET",
headers: {
@@ -1214,6 +1261,8 @@ async function deployWorkflow() {
return {};
});
console.log("workflow", existing_workflow);
loadingDialog.close();
loadingDialog.showLoading("Generating dependency graph");
@@ -1297,6 +1346,7 @@ async function deployWorkflow() {
};
loadingDialog.close();
console.log(deps);
const depsOk = await confirmDialog.confirm(
"Check dependencies",
@@ -1319,9 +1369,8 @@ async function deployWorkflow() {
)}" />`
// createDynamicUIHtml(deps),
);
if (!depsOk) return;
console.log(deps);
if (!depsOk) return;
}
loadingDialog.showLoading("Deploying...");
@@ -1329,28 +1378,15 @@ async function deployWorkflow() {
title.innerText = "Deploying...";
title.style.color = "orange";
// console.log(prompt);
// TODO trim the ending / from endpoint is there is
if (endpoint.endsWith("/")) {
endpoint = endpoint.slice(0, -1);
}
// console.log(prompt.workflow);
const apiRoute = endpoint + "/api/workflow";
// const userId = apiKey
try {
const body = {
workflow_name,
workflow_id,
workflow: prompt.workflow,
name: workflow_name,
workflow_json: prompt.workflow,
workflow_api: prompt.output,
snapshot: snapshot,
dependencies: deps,
api_url: apiUrl,
};
console.log(body);
let data = await fetch(apiRoute, {
let data = await fetch("/comfyui-deploy/workflow", {
method: "POST",
body: JSON.stringify(body),
headers: {
@@ -1360,7 +1396,6 @@ async function deployWorkflow() {
});
console.log(data);
if (data.status !== 200) {
throw new Error(await data.text());
} else {
@@ -1372,22 +1407,16 @@ async function deployWorkflow() {
title.textContent = "Done";
title.style.color = "green";
const version = data.version || 1;
deployMetaNode.widgets[1].value = data.workflow_id;
deployMetaNode.widgets[2].value = data.version;
deployMetaNode.widgets[2].value = version;
graph.change();
infoDialog.show(
`<span style="color:green;">Deployed successfully!</span> <a style="color:white;" target="_blank" href=${endpoint}/workflows/${data.workflow_id}>-> View here</a> <br/> <br/> Workflow ID: ${data.workflow_id} <br/> Workflow Name: ${workflow_name} <br/> Workflow Version: ${data.version} <br/>`
`<span style="color:green;">Deployed successfully!</span> <a style="color:white;" target="_blank" href=${endpoint}/workflows/${data.workflow_id}>-> View here</a> <br/> <br/> Workflow ID: ${data.workflow_id} <br/> Workflow Name: ${workflow_name} <br/> Workflow Version: ${version} <br/>`
);
// // Refresh the workflows list in the sidebar
// const sidebarEl = document.querySelector(
// '.comfy-sidebar-tab[data-id="search"]',
// );
// if (sidebarEl) {
// refreshWorkflowsList(sidebarEl);
// }
setTimeout(() => {
title.textContent = "Deploy";
title.style.color = "white";