Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.8.0 - Comfy Events Via Webhook #20

Merged
merged 21 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
prompt.failed event
shawnrushefsky committed Jan 16, 2025
commit aecb42479c7531562ab014636c4f66a464923d4e
137 changes: 86 additions & 51 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -282,69 +282,104 @@ server.after(() => {
/**
* Send the prompt to ComfyUI, and return a 202 response to the user.
*/
runPromptAndGetOutputs(prompt, app.log).then(
/**
* This function does not block returning the 202 response to the user.
*/
async (outputs: Record<string, Buffer>) => {
for (const originalFilename in outputs) {
let filename = originalFilename;
let fileBuffer = outputs[filename];
if (convert_output) {
try {
fileBuffer = await convertImageBuffer(
fileBuffer,
convert_output
);

/**
* If the user has provided an output format, we need to update the filename
*/
filename = originalFilename.replace(
/\.[^/.]+$/,
`.${convert_output.format}`
);
} catch (e: any) {
app.log.warn(`Failed to convert image: ${e.message}`);
runPromptAndGetOutputs(prompt, app.log)
.then(
/**
* This function does not block returning the 202 response to the user.
*/
async (outputs: Record<string, Buffer>) => {
for (const originalFilename in outputs) {
let filename = originalFilename;
let fileBuffer = outputs[filename];
if (convert_output) {
try {
fileBuffer = await convertImageBuffer(
fileBuffer,
convert_output
);

/**
* If the user has provided an output format, we need to update the filename
*/
filename = originalFilename.replace(
/\.[^/.]+$/,
`.${convert_output.format}`
);
} catch (e: any) {
app.log.warn(`Failed to convert image: ${e.message}`);
}
}
const base64File = fileBuffer.toString("base64");
app.log.info(
`Sending image ${filename} to webhook: ${webhook}`
);
fetch(webhook, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
event: "output.complete",
image: base64File,
id,
filename,
prompt,
}),
})
.catch((e: any) => {
app.log.error(
`Failed to send image to webhook: ${e.message}`
);
})
.then(async (resp) => {
if (!resp) {
app.log.error("No response from webhook");
} else if (!resp.ok) {
app.log.error(
`Failed to send image ${filename}: ${await resp.text()}`
);
} else {
app.log.info(`Sent image ${filename}`);
}
});

// Remove the file after sending
fsPromises.unlink(
path.join(config.outputDir, originalFilename)
);
}
const base64File = fileBuffer.toString("base64");
app.log.info(`Sending image ${filename} to webhook: ${webhook}`);
fetch(webhook, {
}
)
.catch(async (e: any) => {
/**
* Send a webhook reporting that the generation failed.
*/
app.log.error(`Failed to generate images: ${e.message}`);
try {
const resp = await fetch(webhook, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
event: "output.complete",
image: base64File,
event: "prompt.failed",
id,
filename,
prompt,
error: e.message,
}),
})
.catch((e: any) => {
app.log.error(
`Failed to send image to webhook: ${e.message}`
);
})
.then(async (resp) => {
if (!resp) {
app.log.error("No response from webhook");
} else if (!resp.ok) {
app.log.error(
`Failed to send image ${filename}: ${await resp.text()}`
);
} else {
app.log.info(`Sent image ${filename}`);
}
});
});

// Remove the file after sending
fsPromises.unlink(path.join(config.outputDir, originalFilename));
if (!resp.ok) {
app.log.error(
`Failed to send failure message to webhook: ${await resp.text()}`
);
}
} catch (e: any) {
app.log.error(
`Failed to send failure message to webhook: ${e.message}`
);
}
}
);
});
return reply.code(202).send({ status: "ok", id, webhook, prompt });
} else {
/**