Skip to content

Streaming support #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ Thumbs.db

# Project specific
target
TODO.md
TODO.md

*.wav
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions crates/stepflow-analysis/src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ pub struct DependencyTracker {
completed: BitSet,
}

impl Clone for DependencyTracker {
fn clone(&self) -> Self {
Self {
dependencies: self.dependencies.clone(),
blocking: self.blocking.clone(),
completed: self.completed.clone(),
}
}
}

impl DependencyTracker {
pub fn new(dependencies: Arc<Dependencies>) -> Self {
let blocking = dependencies
Expand Down
10 changes: 10 additions & 0 deletions crates/stepflow-builtins/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ impl BuiltinComponent for EvalComponent {
let result_value = match nested_result {
FlowResult::Success { result } => result.as_ref().clone(),
FlowResult::Skipped => serde_json::Value::Null,
FlowResult::Streaming { stream_id, metadata, chunk, chunk_index, is_final } => {
// For streaming results, return the metadata and chunk info
serde_json::json!({
"stream_id": stream_id,
"metadata": metadata.as_ref(),
"chunk": chunk,
"chunk_index": chunk_index,
"is_final": is_final
})
}
FlowResult::Failed { error } => {
// Propagate the failure from the nested workflow
return Ok(FlowResult::Failed { error });
Expand Down
28 changes: 28 additions & 0 deletions crates/stepflow-core/src/flow_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ impl FlowError {
pub enum FlowResult {
/// The step execution was successful.
Success { result: ValueRef },
/// The step is streaming data.
Streaming {
/// Stream identifier
stream_id: String,
/// Metadata about the stream
metadata: ValueRef,
/// Base64 encoded chunk data
chunk: String,
/// Chunk index
chunk_index: usize,
/// Whether this is the final chunk
is_final: bool,
},
/// The step was skipped.
Skipped,
/// The step failed with the given error.
Expand All @@ -57,6 +70,12 @@ pub enum FlowResult {

impl From<serde_json::Value> for FlowResult {
fn from(value: serde_json::Value) -> Self {
// First try to deserialize as a proper FlowResult
if let Ok(flow_result) = serde_json::from_value::<FlowResult>(value.clone()) {
return flow_result;
}

// If that fails, wrap in Success as fallback
let result = ValueRef::new(value);
Self::Success { result }
}
Expand All @@ -70,6 +89,15 @@ impl FlowResult {
}
}

pub fn streaming(&self) -> Option<(String, ValueRef, String, usize, bool)> {
match self {
Self::Streaming { stream_id, metadata, chunk, chunk_index, is_final } => {
Some((stream_id.clone(), metadata.clone(), chunk.clone(), *chunk_index, *is_final))
}
_ => None,
}
}

pub fn skipped(&self) -> bool {
matches!(self, Self::Skipped)
}
Expand Down
8 changes: 7 additions & 1 deletion crates/stepflow-core/src/workflow/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::schema::SchemaRef;
use schemars::JsonSchema;

/// A step in a workflow that executes a component with specific arguments.
#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, JsonSchema, utoipa::ToSchema)]
#[derive(Clone, serde::Serialize, serde::Deserialize, Debug, PartialEq, JsonSchema, utoipa::ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Step {
/// Optional identifier for the step
Expand All @@ -28,6 +28,10 @@ pub struct Step {
/// Arguments to pass to the component for this step
#[serde(default, skip_serializing_if = "ValueRef::is_null")]
pub input: ValueRef,

/// Whether this step is a streaming step (doesn't persist results)
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub streaming: bool,
}

#[derive(
Expand Down Expand Up @@ -134,6 +138,7 @@ mod tests {
default_value: Some(ValueRef::from("fallback")),
},
input: serde_json::Value::Null.into(),
streaming: false,
};

let yaml = serde_yaml_ng::to_string(&step).unwrap();
Expand All @@ -152,6 +157,7 @@ mod tests {
skip_if: None,
on_error: ErrorAction::Fail,
input: serde_json::Value::Null.into(),
streaming: false,
};

let yaml = serde_yaml_ng::to_string(&step).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/stepflow-execution/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub enum ExecutionError {
ExecutionNotFound(Uuid),
#[error("workflow '{0}' not found")]
WorkflowNotFound(FlowHash),
#[error("streaming operation failed")]
StreamingError,
}

pub type Result<T, E = error_stack::Report<ExecutionError>> = std::result::Result<T, E>;
Loading
Loading