mirror of
https://github.com/run-llama/workflows-ts.git
synced 2026-07-21 14:15:24 -04:00
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { createWorkflow, workflowEvent, getContext } from "@llama-flow/core";
|
|
import { pipeline } from "node:stream/promises";
|
|
import { collect } from "@llama-flow/core/stream/consumer";
|
|
|
|
//#region define workflow events
|
|
const startEvent = workflowEvent<string>();
|
|
const branchAEvent = workflowEvent<string>();
|
|
const branchBEvent = workflowEvent<string>();
|
|
const branchCEvent = workflowEvent<string>();
|
|
const branchCompleteEvent = workflowEvent<string>();
|
|
const allCompleteEvent = workflowEvent<string>();
|
|
const stopEvent = workflowEvent<string>();
|
|
//#endregion
|
|
|
|
//#region defines workflow
|
|
const workflow = createWorkflow();
|
|
workflow.handle([startEvent], async () => {
|
|
// emit 3 different events, handled separately
|
|
const { sendEvent, stream } = getContext();
|
|
sendEvent(branchAEvent.with("Branch A"));
|
|
sendEvent(branchBEvent.with("Branch B"));
|
|
sendEvent(branchCEvent.with("Branch C"));
|
|
|
|
const results = await stream.filter(branchCompleteEvent).take(3).toArray();
|
|
|
|
return allCompleteEvent.with(results.map((e) => e.data).join(", "));
|
|
});
|
|
|
|
workflow.handle([branchAEvent], (branchA) => {
|
|
return branchCompleteEvent.with(branchA.data);
|
|
});
|
|
|
|
workflow.handle([branchBEvent], (branchB) => {
|
|
return branchCompleteEvent.with(branchB.data);
|
|
});
|
|
|
|
workflow.handle([branchCEvent], (branchC) => {
|
|
return branchCompleteEvent.with(branchC.data);
|
|
});
|
|
|
|
workflow.handle([allCompleteEvent], (allComplete) => {
|
|
return stopEvent.with(allComplete.data);
|
|
});
|
|
|
|
//#endregion
|
|
|
|
const { stream, sendEvent } = workflow.createContext();
|
|
sendEvent(startEvent.with("initial data"));
|
|
|
|
const result = await pipeline(stream, async function (source) {
|
|
for await (const event of source) {
|
|
if (stopEvent.include(event)) {
|
|
return `Result: ${event.data}`;
|
|
}
|
|
}
|
|
});
|
|
|
|
console.log(result); // Result: Branch A, Branch B, Branch C
|