@@ -16,13 +16,7 @@ function validateGraph(graph: WorkflowGraph | undefined): graph is WorkflowGraph
1616
1717async function persistResult ( engine : WorkflowEngine , result : WorkflowRunResult ) {
1818 try {
19- // Backward compatibility: fall back to reading the private graph field if the engine
20- // instance doesn't yet expose getGraph (e.g., cached build).
21- const engineAny = engine as WorkflowEngine & { getGraph ?: ( ) => WorkflowGraph } ;
22- const workflow =
23- typeof engineAny . getGraph === 'function'
24- ? engineAny . getGraph ( )
25- : ( Reflect . get ( engine , 'graph' ) as WorkflowGraph | undefined ) ;
19+ const workflow = getEngineWorkflow ( engine ) ;
2620
2721 if ( ! workflow ) {
2822 throw new Error ( 'Workflow graph not available on engine instance' ) ;
@@ -44,6 +38,15 @@ async function persistResult(engine: WorkflowEngine, result: WorkflowRunResult)
4438 }
4539}
4640
41+ function getEngineWorkflow ( engine : WorkflowEngine ) : WorkflowGraph | undefined {
42+ // Backward compatibility: fall back to private graph if getGraph is unavailable.
43+ const engineAny = engine as WorkflowEngine & { getGraph ?: ( ) => WorkflowGraph } ;
44+ if ( typeof engineAny . getGraph === 'function' ) {
45+ return engineAny . getGraph ( ) ;
46+ }
47+ return Reflect . get ( engine , 'graph' ) as WorkflowGraph | undefined ;
48+ }
49+
4750export function createWorkflowRouter ( llm ?: WorkflowLLM ) : Router {
4851 const router = createRouter ( ) ;
4952
@@ -75,7 +78,8 @@ export function createWorkflowRouter(llm?: WorkflowLLM): Router {
7578 removeWorkflow ( runId ) ;
7679 }
7780
78- res . json ( result ) ;
81+ const workflow = getEngineWorkflow ( engine ) ?? graph ;
82+ res . json ( { ...result , workflow } ) ;
7983 } catch ( error ) {
8084 const message = error instanceof Error ? error . message : String ( error ) ;
8185 logger . error ( 'Failed to execute workflow' , message ) ;
@@ -129,7 +133,8 @@ export function createWorkflowRouter(llm?: WorkflowLLM): Router {
129133 removeWorkflow ( runId ) ;
130134 }
131135
132- sendEvent ( { type : 'done' , result } ) ;
136+ const workflow = getEngineWorkflow ( engine ) ?? graph ;
137+ sendEvent ( { type : 'done' , result : { ...result , workflow } } ) ;
133138 } catch ( error ) {
134139 const message = error instanceof Error ? error . message : String ( error ) ;
135140 logger . error ( 'Failed to execute workflow stream' , message ) ;
@@ -160,7 +165,8 @@ export function createWorkflowRouter(llm?: WorkflowLLM): Router {
160165 removeWorkflow ( runId ) ;
161166 }
162167
163- res . json ( result ) ;
168+ const workflow = getEngineWorkflow ( engine ) ;
169+ res . json ( workflow ? { ...result , workflow } : result ) ;
164170 } catch ( error ) {
165171 const message = error instanceof Error ? error . message : String ( error ) ;
166172 logger . error ( 'Failed to resume workflow' , message ) ;
@@ -201,7 +207,9 @@ export function createWorkflowRouter(llm?: WorkflowLLM): Router {
201207 // Check in-memory first — catches engines that are still running or paused
202208 const engine = getWorkflow ( runId ) ;
203209 if ( engine ) {
204- res . json ( engine . getResult ( ) ) ;
210+ const result = engine . getResult ( ) ;
211+ const workflow = getEngineWorkflow ( engine ) ;
212+ res . json ( workflow ? { ...result , workflow } : result ) ;
205213 return ;
206214 }
207215
@@ -221,6 +229,7 @@ export function createWorkflowRouter(llm?: WorkflowLLM): Router {
221229 state : record . state ?? { } ,
222230 waitingForInput : record . waitingForInput ?? false ,
223231 currentNodeId : record . currentNodeId ?? null ,
232+ workflow : record . workflow
224233 } ;
225234 res . json ( result ) ;
226235 } catch ( error ) {
0 commit comments