@@ -734,40 +734,41 @@ const eventRouteHookWrapperSource = (
734734 ' execute: async (dispatch) => {' ,
735735 ' const context = await agent();' ,
736736 ' const id = ++sequence;' ,
737- ' return new Promise((resolvePromise, rejectPromise) => { ' ,
738- ' const abort = () => { worker.postMessage({ id, type: "cancel" } ); rejectPromise(new DOMException("Agent render was aborted ", "AbortError") ); };' ,
739- ' const cleanup = () => { dispatch.signal.removeEventListener("abort", abort); worker.off("error", onError ); worker.off("exit", onExit ); worker.off("message", onMessage) ; };' ,
740- ' const reject = (error) => { cleanup(); rejectPromise (error); };' ,
741- ' const onError = (error ) => { reject(error ); };' ,
742- ' const onExit = (code) => { reject (new Error(`Generated hook Flight worker exited with code ${String(code)}.`)); };' ,
743- ' const onMessage = (message) => {' ,
744- ' if (message.id !== id) return;' ,
745- ' if (message.type === "progress") { Promise.resolve(dispatch.progress?.report(message.update)).catch(reject ); return; }' ,
746- ' if (message.type === "error ") { reject(new Error( message.message) ); return; }' ,
747- ' if (message.type !== "complete ") return;' ,
748- ' cleanup() ;' ,
749- ' resolvePromise(new ReadableStream({ start(controller) { controller.enqueue(message.bytes); controller.close(); } }) );' ,
750- ' } ;' ,
751- ' worker.on("error", onError) ;' ,
752- ' worker.on("exit ", onExit); ' ,
753- ' worker.on("message ", onMessage );' ,
754- ' dispatch.signal.addEventListener("abort ", abort, { once: true } );' ,
755- ' if (dispatch.signal.aborted) { abort(); return; } ' ,
756- ' worker.postMessage({ ' ,
757- ' actor: context.actor, ' ,
758- ' artifactEpoch: flightArtifactEpoch ,' ,
759- ' host: context.host ,' ,
760- ' id ,' ,
761- ' invocation: dispatch.invocation ,' ,
762- ' lineage: context.lineage ,' ,
763- ' plugin : context.plugin ,' ,
764- ' requestInvocation : context.invocation ,' ,
765- ' session : context.session ,' ,
766- ' terminal : context.terminal ,' ,
767- ' type: "render" ,' ,
768- ' workspace: context.workspace ,' ,
769- ' }); ' ,
737+ ' let controller; ' ,
738+ ' const cleanup = () => { dispatch.signal.removeEventListener("abort", abort); worker.off("error", fail ); worker.off("exit ", onExit); worker.off("message", onMessage ); };' ,
739+ ' const stream = new ReadableStream({ cancel() { worker.postMessage({ id, type: "cancel" } ); cleanup( ); }, start(opened) { controller = opened ; } }) ;' ,
740+ ' const fail = (error) => { cleanup(); controller.error (error); };' ,
741+ ' const abort = () => { worker.postMessage({ id, type: "cancel" }); fail(new DOMException("Agent render was aborted", "AbortError") ); };' ,
742+ ' const onExit = (code) => { fail (new Error(`Generated hook Flight worker exited with code ${String(code)}.`)); };' ,
743+ ' const onMessage = (message) => {' ,
744+ ' if (message.id !== id) return;' ,
745+ ' if (message.type === "progress") { Promise.resolve(dispatch.progress?.report(message.update)).catch(fail ); return; }' ,
746+ ' if (message.type === "chunk ") { controller.enqueue( message.bytes ); return; }' ,
747+ ' if (message.type === "error ") { fail(new Error(message.message)); return; } ' ,
748+ ' if (message.type !== "end") return ;' ,
749+ ' cleanup( );' ,
750+ ' controller.close() ;' ,
751+ ' } ;' ,
752+ ' if (dispatch.signal.aborted) { controller.error(new DOMException("Agent render was aborted ", "AbortError")); return stream; } ' ,
753+ ' worker.on("error ", fail );' ,
754+ ' worker.on("exit ", onExit );' ,
755+ ' worker.on("message", onMessage); ' ,
756+ ' dispatch.signal.addEventListener("abort", abort, { once: true }); ' ,
757+ ' worker.postMessage({ ' ,
758+ ' actor: context.actor ,' ,
759+ ' artifactEpoch: flightArtifactEpoch ,' ,
760+ ' host: context.host ,' ,
761+ ' id ,' ,
762+ ' invocation: dispatch.invocation ,' ,
763+ ' lineage : context.lineage ,' ,
764+ ' plugin : context.plugin ,' ,
765+ ' requestInvocation : context.invocation ,' ,
766+ ' session : context.session ,' ,
767+ ' terminal: context.terminal ,' ,
768+ ' type: "render" ,' ,
769+ ' workspace: context.workspace, ' ,
770770 ' });' ,
771+ ' return stream;' ,
771772 ' },' ,
772773 ' });' ,
773774 ' try {' ,
0 commit comments