@@ -76,6 +76,10 @@ export interface ExecuteStreamOptions {
7676 */
7777export function useExecutionStream ( ) {
7878 const abortControllerRef = useRef < AbortController | null > ( null )
79+ const currentExecutionRef = useRef < { workflowId : string ; executionId : string | null } > ( {
80+ workflowId : '' ,
81+ executionId : null ,
82+ } )
7983
8084 const execute = useCallback ( async ( options : ExecuteStreamOptions ) => {
8185 const { workflowId, callbacks = { } , ...payload } = options
@@ -89,6 +93,8 @@ export function useExecutionStream() {
8993 const abortController = new AbortController ( )
9094 abortControllerRef . current = abortController
9195
96+ currentExecutionRef . current = { workflowId, executionId : null }
97+
9298 try {
9399 const response = await fetch ( `/api/workflows/${ workflowId } /execute` , {
94100 method : 'POST' ,
@@ -108,6 +114,11 @@ export function useExecutionStream() {
108114 throw new Error ( 'No response body' )
109115 }
110116
117+ const executionId = response . headers . get ( 'X-Execution-Id' )
118+ if ( executionId ) {
119+ currentExecutionRef . current . executionId = executionId
120+ }
121+
111122 // Read SSE stream
112123 const reader = response . body . getReader ( )
113124 const decoder = new TextDecoder ( )
@@ -215,6 +226,7 @@ export function useExecutionStream() {
215226 throw error
216227 } finally {
217228 abortControllerRef . current = null
229+ currentExecutionRef . current = { workflowId : '' , executionId : null }
218230 }
219231 } , [ ] )
220232
@@ -223,6 +235,17 @@ export function useExecutionStream() {
223235 abortControllerRef . current . abort ( )
224236 abortControllerRef . current = null
225237 }
238+
239+ const { workflowId, executionId } = currentExecutionRef . current
240+ if ( workflowId && executionId ) {
241+ fetch ( `/api/workflows/${ workflowId } /execute/cancel` , {
242+ method : 'POST' ,
243+ headers : { 'Content-Type' : 'application/json' } ,
244+ body : JSON . stringify ( { executionId } ) ,
245+ } ) . catch ( ( ) => { } )
246+ }
247+
248+ currentExecutionRef . current = { workflowId : '' , executionId : null }
226249 } , [ ] )
227250
228251 return {
0 commit comments