add generation cancellation middleware and refactor chat/message workflows to support HTTP client disconnections
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
/** Cancel generation when its HTTP client disconnects, for JSON and streamed replies. */
|
||||
export function generationCancellation(req, res, next) {
|
||||
const controller = new AbortController();
|
||||
const onClose = () => {
|
||||
if (!res.writableEnded) controller.abort();
|
||||
res.removeListener("finish", onFinish);
|
||||
};
|
||||
const onFinish = () => res.removeListener("close", onClose);
|
||||
res.once("close", onClose);
|
||||
res.once("finish", onFinish);
|
||||
req.generationSignal = controller.signal;
|
||||
next();
|
||||
}
|
||||
Reference in New Issue
Block a user