feat(js): durable streaming for flows #3770
Open
+4,441
−140
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Genkit flows can be slow operations that stream intermediate progress. In scenarios with unreliable network conditions or client-side interruptions (e.g., page reloads), clients can lose their connection to the stream. Without a durability mechanism, the stream is lost, and the client cannot retrieve the in-progress or final results.
This PR introduces the concept of a
StreamManagerwhich is responsible for persisting the state of a stream. AStreamManagercan be provided when defining a flow handler.Three
StreamManagerimplementations are included:InMemoryStreamManager: Stores stream state in memory. This is useful for development and testing but is not truly durable as it will lose state if the process restarts.firestoreStreamManager: A durableStreamManagerthat uses Firebase Firestore to store stream state.rtdbStreamManager: A durableStreamManagerthat uses the Firebase Realtime Database to store stream state.To make a flow's streams durable, pass a
streamManagerinstance to the handler. This is supported in both the Express (expressHandler) and Next.js (appRoute) plugins. The newdurable-streamingsample app demonstrates this for Express:The sample app includes a
streamyThrowyflow that intentionally throws an error mid-stream to demonstrate how a durable stream manager can preserve the stream's state up to the point of failure.A client can then connect to a durable stream and resume it if necessary:
Beta Feature: This is to be released as a beta feature.
Checklist (if applicable):