[PR #15] [MERGED] feat: workflow middleware system #29

Closed
opened 2026-02-16 03:15:59 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/run-llama/workflows-ts/pull/15
Author: @himself65
Created: 4/3/2025
Status: Merged
Merged: 4/3/2025
Merged by: @himself65

Base: mainHead: store


📝 Commits (6)

📊 Changes

13 files changed (+323 additions, -53 deletions)

View changed files

📝 README.md (+57 -0)
📝 demo/workflows/llama-parse-workflow.ts (+4 -3)
📝 packages/fluere/package.json (+1 -1)
📝 packages/fluere/src/core/event.ts (+16 -14)
📝 packages/fluere/src/core/index.ts (+1 -1)
📝 packages/fluere/src/core/internal/context.ts (+15 -4)
📝 packages/fluere/src/core/internal/executor.ts (+41 -23)
📝 packages/fluere/src/core/workflow.ts (+2 -2)
packages/fluere/src/middleware/directed-graph.ts (+112 -0)
📝 packages/fluere/src/middleware/store.ts (+25 -4)
📝 packages/fluere/src/util/zod.ts (+1 -1)
packages/fluere/tests/with-directed-graph.spec.ts (+47 -0)
📝 packages/fluere/tsconfig.json (+1 -0)

📄 Description

Middleware

withStore

const workflow = withStore(
  () => ({
    pendingTasks: new Set<Promise<unknown>>(),
  }),
  createWorkflow(),
);

workflow.handle([startEvent], () => {
  workflow.getStore().pendingTasks.add(
    new Promise((resolve) => {
      setTimeout(() => {
        resolve();
      }, 100);
    }),
  );
});

const { getStore } = workflow.createContext();

withDirectedGraph

make first parameter of handler to be sendEvent and its type safe and runtime safe when you create a workflow with directedGraph.

// before:
workflow.handle([startEvent], (start) => {});
// after:
workflow.handle([startEvent], (sendEvent, start) => {});
const startEvent = workflowEvent<void, "start">();
const disallowedEvent = workflowEvent<void, "disallowed">({
  debugLabel: "disallowed",
});
const parseEvent = workflowEvent<string, "parse">();
const stopEvent = workflowEvent<number, "stop">();
const workflow = directedGraph(createWorkflow(), [
  [[startEvent], [stopEvent]],
  [[startEvent], [parseEvent]],
]);

workflow.handle([startEvent], (sendEvent, start) => {
  sendEvent(
    disallowedEvent.with(), // <-- ❌ Type Check Failed, Runtime Error
  );
  sendEvent(parseEvent.with("")); // <-- ✅
  sendEvent(stopEvent.with(1)); // <-- ✅
});

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/run-llama/workflows-ts/pull/15 **Author:** [@himself65](https://github.com/himself65) **Created:** 4/3/2025 **Status:** ✅ Merged **Merged:** 4/3/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `main` ← **Head:** `store` --- ### 📝 Commits (6) - [`7f99a6f`](https://github.com/run-llama/workflows-ts/commit/7f99a6fca2021a8968a970f5e18b86817c2c7974) feat: middleware system - [`c0f0f65`](https://github.com/run-llama/workflows-ts/commit/c0f0f65664172f604224e67a58e34b785436525b) fix: void - [`844cae0`](https://github.com/run-llama/workflows-ts/commit/844cae0cd342ee928dc31a3ae69528a6285fb7da) feat: `withDirectedGraph` - [`b35f9f3`](https://github.com/run-llama/workflows-ts/commit/b35f9f3788d3035596c252b2fca23adb5798f051) docs: update - [`73d1f8e`](https://github.com/run-llama/workflows-ts/commit/73d1f8e2a8b41e8dedbd1f6e455be5da4bbb1f29) test: update - [`01f0339`](https://github.com/run-llama/workflows-ts/commit/01f0339a7d4979efdb1400825d2591a1fdf89d86) 0.3.0-alpha.3 ### 📊 Changes **13 files changed** (+323 additions, -53 deletions) <details> <summary>View changed files</summary> 📝 `README.md` (+57 -0) 📝 `demo/workflows/llama-parse-workflow.ts` (+4 -3) 📝 `packages/fluere/package.json` (+1 -1) 📝 `packages/fluere/src/core/event.ts` (+16 -14) 📝 `packages/fluere/src/core/index.ts` (+1 -1) 📝 `packages/fluere/src/core/internal/context.ts` (+15 -4) 📝 `packages/fluere/src/core/internal/executor.ts` (+41 -23) 📝 `packages/fluere/src/core/workflow.ts` (+2 -2) ➕ `packages/fluere/src/middleware/directed-graph.ts` (+112 -0) 📝 `packages/fluere/src/middleware/store.ts` (+25 -4) 📝 `packages/fluere/src/util/zod.ts` (+1 -1) ➕ `packages/fluere/tests/with-directed-graph.spec.ts` (+47 -0) 📝 `packages/fluere/tsconfig.json` (+1 -0) </details> ### 📄 Description ## Middleware ### `withStore` ```ts const workflow = withStore( () => ({ pendingTasks: new Set<Promise<unknown>>(), }), createWorkflow(), ); workflow.handle([startEvent], () => { workflow.getStore().pendingTasks.add( new Promise((resolve) => { setTimeout(() => { resolve(); }, 100); }), ); }); const { getStore } = workflow.createContext(); ``` ### `withDirectedGraph` make first parameter of `handler` to be `sendEvent` and its type safe and runtime safe when you create a workflow with `directedGraph`. ```ts // before: workflow.handle([startEvent], (start) => {}); // after: workflow.handle([startEvent], (sendEvent, start) => {}); ``` ```ts const startEvent = workflowEvent<void, "start">(); const disallowedEvent = workflowEvent<void, "disallowed">({ debugLabel: "disallowed", }); const parseEvent = workflowEvent<string, "parse">(); const stopEvent = workflowEvent<number, "stop">(); const workflow = directedGraph(createWorkflow(), [ [[startEvent], [stopEvent]], [[startEvent], [parseEvent]], ]); workflow.handle([startEvent], (sendEvent, start) => { sendEvent( disallowedEvent.with(), // <-- ❌ Type Check Failed, Runtime Error ); sendEvent(parseEvent.with("")); // <-- ✅ sendEvent(stopEvent.with(1)); // <-- ✅ }); ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 03:15:59 -05:00
yindo closed this issue 2026-02-16 03:15:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: run-llama/workflows-ts#29