1- import { createWorkflow , workflowEvent } from "@llamaindex/workflow-core" ;
2- import { withDrawing } from "@llamaindex/workflow-viz" ;
1+ import { Workflow , workflowEvent } from "@llamaindex/workflow-core" ;
32
43//#region define workflow events
54const startEvent = workflowEvent < string > ( {
@@ -25,36 +24,35 @@ const stopEvent = workflowEvent<string>({
2524} ) ;
2625//#endregion
2726
28- //#region defines workflow
29- const workflow = withDrawing ( createWorkflow ( ) ) ;
27+ export function setupWorkflowEvents ( workflow : Workflow ) {
28+ workflow . handle ( [ startEvent ] , async ( ctx ) => {
29+ const { sendEvent, stream } = ctx ;
30+ // emit 3 different events, handled separately
3031
31- workflow . handle ( [ startEvent ] , async ( ctx ) => {
32- const { sendEvent, stream } = ctx ;
33- // emit 3 different events, handled separately
32+ sendEvent ( branchAEvent . with ( "Branch A" ) ) ;
33+ sendEvent ( branchBEvent . with ( "Branch B" ) ) ;
34+ sendEvent ( branchCEvent . with ( "Branch C" ) ) ;
3435
35- sendEvent ( branchAEvent . with ( "Branch A" ) ) ;
36- sendEvent ( branchBEvent . with ( "Branch B" ) ) ;
37- sendEvent ( branchCEvent . with ( "Branch C" ) ) ;
36+ const results = await stream . filter ( branchCompleteEvent ) . take ( 3 ) . toArray ( ) ;
3837
39- const results = await stream . filter ( branchCompleteEvent ) . take ( 3 ) . toArray ( ) ;
38+ return allCompleteEvent . with ( results . map ( ( e ) => e . data ) . join ( ", " ) ) ;
39+ } ) ;
4040
41- return allCompleteEvent . with ( results . map ( ( e ) => e . data ) . join ( ", " ) ) ;
42- } ) ;
43-
44- workflow . handle ( [ branchAEvent ] , ( _context1 , branchA ) => {
45- return branchCompleteEvent . with ( branchA . data ) ;
46- } ) ;
41+ workflow . handle ( [ branchAEvent ] , ( _context1 , branchA ) => {
42+ return branchCompleteEvent . with ( branchA . data ) ;
43+ } ) ;
4744
48- workflow . handle ( [ branchBEvent ] , ( _context2 , branchB ) => {
49- return branchCompleteEvent . with ( branchB . data ) ;
50- } ) ;
45+ workflow . handle ( [ branchBEvent ] , ( _context2 , branchB ) => {
46+ return branchCompleteEvent . with ( branchB . data ) ;
47+ } ) ;
5148
52- workflow . handle ( [ branchCEvent ] , ( _context3 , branchC ) => {
53- return branchCompleteEvent . with ( branchC . data ) ;
54- } ) ;
49+ workflow . handle ( [ branchCEvent ] , ( _context3 , branchC ) => {
50+ return branchCompleteEvent . with ( branchC . data ) ;
51+ } ) ;
5552
56- workflow . handle ( [ allCompleteEvent ] , ( _context4 , allComplete ) => {
57- return stopEvent . with ( allComplete . data ) ;
58- } ) ;
53+ workflow . handle ( [ allCompleteEvent ] , ( _context4 , allComplete ) => {
54+ return stopEvent . with ( allComplete . data ) ;
55+ } ) ;
5956
60- export { workflow } ;
57+ return workflow ;
58+ }
0 commit comments