feat(demo): add sample from laurie (#51)

This commit is contained in:
Alex Yang
2025-04-19 14:45:13 -07:00
committed by GitHub
parent febeab189d
commit 680dcaf035
2 changed files with 71 additions and 2 deletions
+69
View File
@@ -0,0 +1,69 @@
import { createWorkflow, workflowEvent, getContext } from "@llama-flow/core";
import { pipeline } from "node:stream/promises";
import { collect } from "@llama-flow/core/stream/consumer";
import { until } from "@llama-flow/core/stream/until";
import { filter } from "@llama-flow/core/stream/filter";
//#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"));
let condition = 0;
const results = await collect(
until(
filter(stream, (ev) => branchCompleteEvent.include(ev)),
() => {
condition++;
return condition === 3;
},
),
);
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
+2 -2
View File
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"target": "esnext",
"module": "esnext",
"esModuleInterop": true,
"moduleResolution": "node16",
"types": ["DOM", "DOM.AsyncIterable", "DOM.Iterable", "esnext"],