Interrupt Comfy gens on cancel

52497ea96ddec110091d7f3f50d72457e032f3db

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +15 -0Ignore whitespace
src/endpoints/stable-diffusion.js+15 -0
@@ -569,6 +569,17 @@ comfy.post('/generate', jsonParser, async (request, response) => {
569 const url = new URL(request.body.url);569 const url = new URL(request.body.url);
570 url.pathname = '/prompt';570 url.pathname = '/prompt';
571571
572 const controller = new AbortController();
573 request.socket.removeAllListeners('close');
574 request.socket.on('close', function () {
575 if (!response.writableEnded && !item) {
576 const interruptUrl = new URL(request.body.url);
577 interruptUrl.pathname = '/interrupt';
578 fetch(interruptUrl, { method: 'POST', headers: { 'Authorization': getBasicAuthHeader(request.body.auth) } });
579 }
580 controller.abort();
581 });
582
572 const promptResult = await fetch(url, {583 const promptResult = await fetch(url, {
573 method: 'POST',584 method: 'POST',
574 body: request.body.prompt,585 body: request.body.prompt,
@@ -594,6 +605,9 @@ comfy.post('/generate', jsonParser, async (request, response) => {
594 }605 }
595 await delay(100);606 await delay(100);
596 }607 }
608 if (item.status.status_str === 'error') {
609 throw new Error('ComfyUI generation did not succeed.');
610 }
597 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];611 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];
598 const imgUrl = new URL(request.body.url);612 const imgUrl = new URL(request.body.url);
599 imgUrl.pathname = '/view';613 imgUrl.pathname = '/view';
@@ -605,6 +619,7 @@ comfy.post('/generate', jsonParser, async (request, response) => {
605 const imgBuffer = await imgResponse.buffer();619 const imgBuffer = await imgResponse.buffer();
606 return response.send(imgBuffer.toString('base64'));620 return response.send(imgBuffer.toString('base64'));
607 } catch (error) {621 } catch (error) {
622 console.log(error);
608 return response.sendStatus(500);623 return response.sendStatus(500);
609 }624 }
610});625});