The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(flows): split schema handlers into a module directory
Steven Enamakela0212ceeab66
diff --git a/crates/openhuman-core/src/flows/schemas.rs b/crates/openhuman-core/src/flows/schemas.rsindex 4b5cf1ff59..5d8a0a8eee 100644--- a/crates/openhuman-core/src/flows/schemas.rs+++ b/crates/openhuman-core/src/flows/schemas.rs@@ -352,6 +352,8 @@ pub fn all_registered_controllers() -> Vec<RegisteredController> {mod registered;+mod handlers;+use handlers::*;mod builder_schemas;mod definition_schemas;mod draft_schemas;@@ -393,3 +395,7 @@ pub fn schemas(function: &str) -> ControllerSchema {}++#[cfg(test)]+#[path = "schemas_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/flows/schemas/handlers/builder.rs b/crates/openhuman-core/src/flows/schemas/handlers/builder.rsnew file mode 100644index 0000000000..a8835528e2--- /dev/null+++ b/crates/openhuman-core/src/flows/schemas/handlers/builder.rs@@ -0,0 +1,160 @@+use super::super::*;++pub(super) fn handle_build(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ // Optional streaming target: when the copilot passes its chat `thread_id`+ // the builder turn streams live text/tool/proposal events into that+ // thread (Phase B). Read + strip the transport-only keys before the rest+ // of the object is deserialized into the structured BuilderRequest.+ let stream = read_flow_stream_target(¶ms);+ // Deserialize the remaining param object into the structured BuilderRequest+ // (mode/instruction/graph/flow_id/run_id/error/failing_node_ids). The+ // stream keys are ignored (BuilderRequest doesn't declare them).+ let req: crate::flows::agents::workflow_builder::builder_prompt::BuilderRequest =+ serde_json::from_value(Value::Object(params))+ .map_err(|e| format!("invalid flows.build params: {e}"))?;+ to_json(ops::flows_build(&ctx, &config, req, stream).await?)+ })+}++pub(super) fn handle_build_cancel(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let thread_id = read_required::<String>(¶ms, "thread_id")?;+ let request_id = params+ .get("request_id")+ .and_then(Value::as_str)+ .map(str::to_string);+ to_json(ops::flows_build_cancel(thread_id.trim(), request_id.as_deref()).await?)+ })+}++pub(super) fn handle_discover(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ // Optional streaming target for the Flow Scout run (Phase B) — same+ // `thread_id`/`request_id` convention as `flows.build`.+ let stream = read_flow_stream_target(¶ms);+ to_json(ops::flows_discover(&ctx, &config, stream).await?)+ })+}++/// Read the optional `thread_id` / `request_id` streaming params shared by+/// `flows.build` and `flows.discover` into an [`ops::FlowStreamTarget`].+/// Returns `None` (headless run) when no usable `thread_id` is present; a+/// missing `request_id` is filled with a fresh uuid inside `from_params`.+pub(super) fn read_flow_stream_target(params: &Map<String, Value>) -> Option<ops::FlowStreamTarget> {+ let thread_id = params+ .get("thread_id")+ .and_then(Value::as_str)+ .map(str::to_string);+ let request_id = params+ .get("request_id")+ .and_then(Value::as_str)+ .map(str::to_string);+ ops::FlowStreamTarget::from_params(thread_id, request_id)+}++pub(super) fn handle_list_suggestions(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let status = params+ .get("status")+ .and_then(Value::as_str)+ .map(str::trim)+ .filter(|s| !s.is_empty())+ .map(tinyflows_catalog::SuggestionStatus::from_str_lossy);+ to_json(ops::flows_list_suggestions(&config, status).await?)+ })+}++pub(super) fn handle_dismiss_suggestion(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_dismiss_suggestion(&config, id.trim()).await?)+ })+}++pub(super) fn handle_mark_suggestion_built(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_mark_suggestion_built(&config, id.trim()).await?)+ })+}++pub(super) fn handle_required_connections(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let graph = read_required::<Value>(¶ms, "graph")?;+ to_json(ops::flows_required_connections(&ctx, &config, graph).await?)+ })+}++pub(super) fn handle_approval_manifest(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = params+ .get("id")+ .and_then(Value::as_str)+ .filter(|s| !s.trim().is_empty())+ .map(str::to_string);+ let graph = params.get("graph").filter(|v| !v.is_null()).cloned();+ to_json(ops::flows_approval_manifest(&config, id.as_deref(), graph).await?)+ })+}++pub(super) fn handle_search_tool_catalog(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let query = read_required::<String>(¶ms, "query")?;+ let toolkit = params+ .get("toolkit")+ .and_then(Value::as_str)+ .filter(|s| !s.is_empty());+ let limit = params+ .get("limit")+ .and_then(Value::as_u64)+ .map(|n| n as usize)+ .unwrap_or(25);+ to_json(ops::flows_search_tool_catalog(&config, query.trim(), toolkit, limit).await?)+ })+}++pub(super) fn handle_get_tool_contract(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let slug = read_required::<String>(¶ms, "slug")?;+ to_json(ops::flows_get_tool_contract(&config, slug.trim()).await?)+ })+}++pub(super) fn handle_get_history(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let limit = params+ .get("limit")+ .and_then(Value::as_u64)+ .map(|n| n as usize)+ .unwrap_or(20);+ to_json(ops::flows_get_history(&config, id.trim(), limit)?)+ })+}++pub(super) fn handle_rollback(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let revision_id = read_required::<String>(¶ms, "revision_id")?;+ let expected_version = params+ .get("expected_version")+ .and_then(Value::as_str)+ .filter(|s| !s.is_empty())+ .map(str::to_string);+ to_json(+ ops::flows_rollback(&config, id.trim(), revision_id.trim(), expected_version).await?,+ )+ })+}diff --git a/crates/openhuman-core/src/flows/schemas/handlers/definition.rs b/crates/openhuman-core/src/flows/schemas/handlers/definition.rsnew file mode 100644index 0000000000..58c81fbfb9--- /dev/null+++ b/crates/openhuman-core/src/flows/schemas/handlers/definition.rs@@ -0,0 +1,138 @@+use super::super::*;++pub(super) fn handle_create(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let name = read_required::<String>(¶ms, "name")?;+ let graph = read_required::<Value>(¶ms, "graph")?;+ let require_approval = params+ .get("require_approval")+ .and_then(Value::as_bool)+ .unwrap_or(false);+ // Opt-in strict mode (F3): run the same author hard-gates an agent save+ // must pass, before persisting. Default off — the human canvas save+ // path stays permissive.+ if params+ .get("strict")+ .and_then(Value::as_bool)+ .unwrap_or(false)+ {+ ops::strict_gate(&ctx, &config, &graph).await?;+ }+ to_json(ops::flows_create(&config, name, graph, require_approval).await?)+ })+}++pub(super) fn handle_validate(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ // No config load: validation is pure (no persistence, no workspace).+ let graph = read_required::<Value>(¶ms, "graph")?;+ to_json(ops::flows_validate(graph))+ })+}++pub(super) fn handle_import(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ // No config load: import is pure (no persistence, no workspace).+ let graph = read_required::<Value>(¶ms, "graph")?;+ let format = params+ .get("format")+ .filter(|v| !v.is_null())+ .map(|v| serde_json::from_value::<String>(v.clone()))+ .transpose()+ .map_err(|e| format!("invalid 'format': {e}"))?;+ to_json(ops::flows_import(graph, format)?)+ })+}++pub(super) fn handle_duplicate(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_duplicate(&config, id.trim()).await?)+ })+}++pub(super) fn handle_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_get(&config, id.trim()).await?)+ })+}++pub(super) fn handle_list(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ to_json(ops::flows_list(&config).await?)+ })+}++pub(super) fn handle_list_connections(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ to_json(ops::flows_list_connections(&ctx, &config).await?)+ })+}++pub(super) fn handle_update(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let name = params+ .get("name")+ .filter(|v| !v.is_null())+ .map(|v| serde_json::from_value(v.clone()))+ .transpose()+ .map_err(|e| format!("invalid 'name': {e}"))?;+ let graph = params.get("graph").filter(|v| !v.is_null()).cloned();+ let require_approval = params.get("require_approval").and_then(Value::as_bool);+ let expected_version = params+ .get("expected_version")+ .and_then(Value::as_str)+ .filter(|s| !s.is_empty())+ .map(str::to_string);+ // Opt-in strict mode (F3): when a new graph is supplied, run the same+ // author hard-gates an agent save must pass, before persisting.+ if params+ .get("strict")+ .and_then(Value::as_bool)+ .unwrap_or(false)+ {+ if let Some(graph_json) = graph.as_ref() {+ ops::strict_gate(&ctx, &config, graph_json).await?;+ }+ }+ to_json(+ ops::flows_update(+ &config,+ id.trim(),+ name,+ graph,+ require_approval,+ expected_version,+ )+ .await?,+ )+ })+}++pub(super) fn handle_delete(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_delete(&config, id.trim()).await?)+ })+}++pub(super) fn handle_set_enabled(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let enabled = params+ .get("enabled")+ .and_then(Value::as_bool)+ .ok_or_else(|| "missing required param 'enabled'".to_string())?;+ to_json(ops::flows_set_enabled(&config, id.trim(), enabled).await?)+ })+}diff --git a/crates/openhuman-core/src/flows/schemas/handlers/draft.rs b/crates/openhuman-core/src/flows/schemas/handlers/draft.rsnew file mode 100644index 0000000000..26581def5e--- /dev/null+++ b/crates/openhuman-core/src/flows/schemas/handlers/draft.rs@@ -0,0 +1,123 @@+use super::super::*;++pub(super) fn handle_draft_create(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let name = read_required::<String>(¶ms, "name")?;+ let graph = read_required::<Value>(¶ms, "graph")?;+ let flow_id = params+ .get("flow_id")+ .and_then(Value::as_str)+ .filter(|s| !s.is_empty())+ .map(str::to_string);+ let origin = params+ .get("origin")+ .and_then(Value::as_str)+ .and_then(|s| serde_json::from_value(Value::String(s.to_string())).ok())+ .unwrap_or(tinyflows_catalog::DraftOrigin::Canvas);+ to_json(ops::flows_draft_create(+ &config, flow_id, name, graph, origin,+ )?)+ })+}++pub(super) fn handle_draft_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_draft_get(&config, id.trim())?)+ })+}++pub(super) fn handle_draft_update(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let name = params+ .get("name")+ .filter(|v| !v.is_null())+ .map(|v| serde_json::from_value(v.clone()))+ .transpose()+ .map_err(|e| format!("invalid 'name': {e}"))?;+ let graph = params.get("graph").filter(|v| !v.is_null()).cloned();+ // A present `flow_id` (even null) re-links the draft; absent leaves it.+ let flow_id = parse_draft_update_flow_id(¶ms)?;+ to_json(ops::flows_draft_update(+ &config,+ id.trim(),+ name,+ graph,+ flow_id,+ )?)+ })+}++pub(super) fn handle_draft_list(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ to_json(ops::flows_draft_list(&config)?)+ })+}++pub(super) fn handle_draft_delete(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_draft_delete(&config, id.trim())?)+ })+}++pub(super) fn handle_draft_promote(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let require_approval = params.get("require_approval").and_then(Value::as_bool);+ to_json(ops::flows_draft_promote(&config, id.trim(), require_approval).await?)+ })+}++fn read_required<T: DeserializeOwned>(params: &Map<String, Value>, key: &str) -> Result<T, String> {+ let value = params+ .get(key)+ .cloned()+ .ok_or_else(|| format!("missing required param '{key}'"))?;+ serde_json::from_value(value).map_err(|e| format!("invalid '{key}': {e}"))+}++fn to_json<T: serde::Serialize>(outcome: Outcome<T>) -> Result<Value, String> {+ outcome.into_cli_compatible_json()+}++/// Parses `draft_update`'s `flow_id` param (R-m7). The outer `Option`+/// mirrors `ops::flows_draft_update`'s "present vs absent" contract — absent+/// leaves the draft's existing link untouched; the inner `Option` is the new+/// link (`None` unlinks).+///+/// A present-but-non-string `flow_id` (a number, or an object from a buggy+/// client) is REJECTED rather than silently coerced into `Some(None)` via+/// `Value::as_str()` returning `None` on a type mismatch — that shape used+/// to be indistinguishable from an explicit `flow_id: null` unlink, and+/// `update_draft` treats `Some(None)` as exactly that: unlinking the draft+/// from its flow. A later `draft_promote` then creates a brand-new flow+/// instead of updating the one the caller actually meant.+pub(super) fn parse_draft_update_flow_id(+ params: &Map<String, Value>,+) -> Result<Option<Option<String>>, String> {+ match params.get("flow_id") {+ None => Ok(None),+ Some(Value::Null) => Ok(Some(None)),+ Some(Value::String(s)) => {+ let s = s.trim();+ Ok(Some(if s.is_empty() {+ None+ } else {+ Some(s.to_string())+ }))+ }+ Some(other) => Err(format!(+ "invalid 'flow_id': expected a string or null, got {other}"+ )),+ }+}++#[cfg(test)]diff --git a/crates/openhuman-core/src/flows/schemas/handlers/mod.rs b/crates/openhuman-core/src/flows/schemas/handlers/mod.rsnew file mode 100644index 0000000000..58dc933cf7--- /dev/null+++ b/crates/openhuman-core/src/flows/schemas/handlers/mod.rs@@ -0,0 +1,9 @@+mod definition;+mod run;+mod builder;+mod draft;++pub(super) use builder::*;+pub(super) use definition::*;+pub(super) use draft::*;+pub(super) use run::*;diff --git a/crates/openhuman-core/src/flows/schemas/handlers/run.rs b/crates/openhuman-core/src/flows/schemas/handlers/run.rsnew file mode 100644index 0000000000..8f6ca7a2a8--- /dev/null+++ b/crates/openhuman-core/src/flows/schemas/handlers/run.rs@@ -0,0 +1,149 @@+use super::super::*;++pub(super) fn handle_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let input = params.get("input").cloned().unwrap_or(Value::Null);+ let inputs = read_declared_inputs(¶ms)?;+ to_json(+ ops::flows_run(+ &ctx,+ &config,+ id.trim(),+ input,+ inputs,+ tinyflows_catalog::FlowRunTrigger::Rpc,+ )+ .await?,+ )+ })+}++pub(super) fn handle_run_detached(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let input = params.get("input").cloned().unwrap_or(Value::Null);+ let inputs = read_declared_inputs(¶ms)?;+ to_json(+ ops::flows_run_detached(+ &ctx,+ &config,+ id.trim(),+ input,+ inputs,+ tinyflows_catalog::FlowRunTrigger::Rpc,+ )+ .await?,+ )+ })+}++/// Reads the optional `inputs` param — values for the flow's declared workflow+/// inputs, keyed by name.+///+/// Absent or `null` means "supplied nothing", which is valid for a flow whose+/// inputs are all optional or defaulted. A present-but-non-object value is a+/// caller error rejected here, before it reaches `ops`, so the message names the+/// parameter rather than surfacing as a confusing per-input complaint.+pub(super) fn read_declared_inputs(params: &Map<String, Value>) -> Result<Map<String, Value>, String> {+ match params.get("inputs") {+ None | Some(Value::Null) => Ok(Map::new()),+ Some(Value::Object(map)) => Ok(map.clone()),+ Some(other) => Err(format!(+ "param 'inputs' must be an object keyed by declared input name, got {}",+ match other {+ Value::Array(_) => "an array",+ Value::String(_) => "a string",+ Value::Number(_) => "a number",+ Value::Bool(_) => "a boolean",+ _ => "a non-object",+ }+ )),+ }+}++pub(super) fn handle_resume(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let thread_id = read_required::<String>(¶ms, "thread_id")?;+ let approvals: Vec<String> = params+ .get("approvals")+ .filter(|v| !v.is_null())+ .cloned()+ .map(serde_json::from_value)+ .transpose()+ .map_err(|e| format!("invalid 'approvals': {e}"))?+ .unwrap_or_default();+ let rejections: Vec<String> = params+ .get("rejections")+ .filter(|v| !v.is_null())+ .cloned()+ .map(serde_json::from_value)+ .transpose()+ .map_err(|e| format!("invalid 'rejections': {e}"))?+ .unwrap_or_default();+ to_json(+ ops::flows_resume(+ &ctx,+ &config,+ id.trim(),+ thread_id.trim(),+ approvals,+ rejections,+ )+ .await?,+ )+ })+}++pub(super) fn handle_cancel_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let run_id = read_required::<String>(¶ms, "run_id")?;+ to_json(ops::flows_cancel_run(&config, run_id.trim()).await?)+ })+}++pub(super) fn handle_list_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ let limit = params+ .get("limit")+ .and_then(Value::as_u64)+ .and_then(|n| usize::try_from(n).ok())+ .unwrap_or(20);+ to_json(ops::flows_list_runs(&config, id.trim(), limit).await?)+ })+}++pub(super) fn handle_list_all_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let limit = params+ .get("limit")+ .and_then(Value::as_u64)+ .and_then(|n| usize::try_from(n).ok())+ .unwrap_or(100);+ to_json(ops::flows_list_all_runs(&config, limit).await?)+ })+}++pub(super) fn handle_get_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let run_id = read_required::<String>(¶ms, "run_id")?;+ to_json(ops::flows_get_run(&config, run_id.trim()).await?)+ })+}++pub(super) fn handle_prune_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {+ Box::pin(async move {+ let config = ctx.load_config().await?;+ let id = read_required::<String>(¶ms, "id")?;+ to_json(ops::flows_prune_runs(&config, id.trim()).await?)+ })+}diff --git a/crates/openhuman-core/src/flows/schemas_handlers.rs b/crates/openhuman-core/src/flows/schemas_handlers.rsdeleted file mode 100644index 4ce35b47de..0000000000--- a/crates/openhuman-core/src/flows/schemas_handlers.rs+++ /dev/null@@ -1,568 +0,0 @@-use crate::core::runtime::DispatchCtx;-fn handle_create(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let name = read_required::<String>(¶ms, "name")?;- let graph = read_required::<Value>(¶ms, "graph")?;- let require_approval = params- .get("require_approval")- .and_then(Value::as_bool)- .unwrap_or(false);- // Opt-in strict mode (F3): run the same author hard-gates an agent save- // must pass, before persisting. Default off — the human canvas save- // path stays permissive.- if params- .get("strict")- .and_then(Value::as_bool)- .unwrap_or(false)- {- ops::strict_gate(&ctx, &config, &graph).await?;- }- to_json(ops::flows_create(&config, name, graph, require_approval).await?)- })-}--fn handle_validate(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- // No config load: validation is pure (no persistence, no workspace).- let graph = read_required::<Value>(¶ms, "graph")?;- to_json(ops::flows_validate(graph))- })-}--fn handle_import(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- // No config load: import is pure (no persistence, no workspace).- let graph = read_required::<Value>(¶ms, "graph")?;- let format = params- .get("format")- .filter(|v| !v.is_null())- .map(|v| serde_json::from_value::<String>(v.clone()))- .transpose()- .map_err(|e| format!("invalid 'format': {e}"))?;- to_json(ops::flows_import(graph, format)?)- })-}--fn handle_duplicate(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_duplicate(&config, id.trim()).await?)- })-}--fn handle_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_get(&config, id.trim()).await?)- })-}--fn handle_list(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- to_json(ops::flows_list(&config).await?)- })-}--fn handle_list_connections(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- to_json(ops::flows_list_connections(&ctx, &config).await?)- })-}--fn handle_update(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let name = params- .get("name")- .filter(|v| !v.is_null())- .map(|v| serde_json::from_value(v.clone()))- .transpose()- .map_err(|e| format!("invalid 'name': {e}"))?;- let graph = params.get("graph").filter(|v| !v.is_null()).cloned();- let require_approval = params.get("require_approval").and_then(Value::as_bool);- let expected_version = params- .get("expected_version")- .and_then(Value::as_str)- .filter(|s| !s.is_empty())- .map(str::to_string);- // Opt-in strict mode (F3): when a new graph is supplied, run the same- // author hard-gates an agent save must pass, before persisting.- if params- .get("strict")- .and_then(Value::as_bool)- .unwrap_or(false)- {- if let Some(graph_json) = graph.as_ref() {- ops::strict_gate(&ctx, &config, graph_json).await?;- }- }- to_json(- ops::flows_update(- &config,- id.trim(),- name,- graph,- require_approval,- expected_version,- )- .await?,- )- })-}--fn handle_delete(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_delete(&config, id.trim()).await?)- })-}--fn handle_set_enabled(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let enabled = params- .get("enabled")- .and_then(Value::as_bool)- .ok_or_else(|| "missing required param 'enabled'".to_string())?;- to_json(ops::flows_set_enabled(&config, id.trim(), enabled).await?)- })-}--fn handle_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let input = params.get("input").cloned().unwrap_or(Value::Null);- let inputs = read_declared_inputs(¶ms)?;- to_json(- ops::flows_run(- &ctx,- &config,- id.trim(),- input,- inputs,- tinyflows_catalog::FlowRunTrigger::Rpc,- )- .await?,- )- })-}--fn handle_run_detached(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let input = params.get("input").cloned().unwrap_or(Value::Null);- let inputs = read_declared_inputs(¶ms)?;- to_json(- ops::flows_run_detached(- &ctx,- &config,- id.trim(),- input,- inputs,- tinyflows_catalog::FlowRunTrigger::Rpc,- )- .await?,- )- })-}--/// Reads the optional `inputs` param — values for the flow's declared workflow-/// inputs, keyed by name.-///-/// Absent or `null` means "supplied nothing", which is valid for a flow whose-/// inputs are all optional or defaulted. A present-but-non-object value is a-/// caller error rejected here, before it reaches `ops`, so the message names the-/// parameter rather than surfacing as a confusing per-input complaint.-fn read_declared_inputs(params: &Map<String, Value>) -> Result<Map<String, Value>, String> {- match params.get("inputs") {- None | Some(Value::Null) => Ok(Map::new()),- Some(Value::Object(map)) => Ok(map.clone()),- Some(other) => Err(format!(- "param 'inputs' must be an object keyed by declared input name, got {}",- match other {- Value::Array(_) => "an array",- Value::String(_) => "a string",- Value::Number(_) => "a number",- Value::Bool(_) => "a boolean",- _ => "a non-object",- }- )),- }-}--fn handle_resume(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let thread_id = read_required::<String>(¶ms, "thread_id")?;- let approvals: Vec<String> = params- .get("approvals")- .filter(|v| !v.is_null())- .cloned()- .map(serde_json::from_value)- .transpose()- .map_err(|e| format!("invalid 'approvals': {e}"))?- .unwrap_or_default();- let rejections: Vec<String> = params- .get("rejections")- .filter(|v| !v.is_null())- .cloned()- .map(serde_json::from_value)- .transpose()- .map_err(|e| format!("invalid 'rejections': {e}"))?- .unwrap_or_default();- to_json(- ops::flows_resume(- &ctx,- &config,- id.trim(),- thread_id.trim(),- approvals,- rejections,- )- .await?,- )- })-}--fn handle_cancel_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let run_id = read_required::<String>(¶ms, "run_id")?;- to_json(ops::flows_cancel_run(&config, run_id.trim()).await?)- })-}--fn handle_list_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let limit = params- .get("limit")- .and_then(Value::as_u64)- .and_then(|n| usize::try_from(n).ok())- .unwrap_or(20);- to_json(ops::flows_list_runs(&config, id.trim(), limit).await?)- })-}--fn handle_list_all_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let limit = params- .get("limit")- .and_then(Value::as_u64)- .and_then(|n| usize::try_from(n).ok())- .unwrap_or(100);- to_json(ops::flows_list_all_runs(&config, limit).await?)- })-}--fn handle_get_run(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let run_id = read_required::<String>(¶ms, "run_id")?;- to_json(ops::flows_get_run(&config, run_id.trim()).await?)- })-}--fn handle_prune_runs(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_prune_runs(&config, id.trim()).await?)- })-}--fn handle_build(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- // Optional streaming target: when the copilot passes its chat `thread_id`- // the builder turn streams live text/tool/proposal events into that- // thread (Phase B). Read + strip the transport-only keys before the rest- // of the object is deserialized into the structured BuilderRequest.- let stream = read_flow_stream_target(¶ms);- // Deserialize the remaining param object into the structured BuilderRequest- // (mode/instruction/graph/flow_id/run_id/error/failing_node_ids). The- // stream keys are ignored (BuilderRequest doesn't declare them).- let req: crate::flows::agents::workflow_builder::builder_prompt::BuilderRequest =- serde_json::from_value(Value::Object(params))- .map_err(|e| format!("invalid flows.build params: {e}"))?;- to_json(ops::flows_build(&ctx, &config, req, stream).await?)- })-}--fn handle_build_cancel(_ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let thread_id = read_required::<String>(¶ms, "thread_id")?;- let request_id = params- .get("request_id")- .and_then(Value::as_str)- .map(str::to_string);- to_json(ops::flows_build_cancel(thread_id.trim(), request_id.as_deref()).await?)- })-}--fn handle_discover(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- // Optional streaming target for the Flow Scout run (Phase B) — same- // `thread_id`/`request_id` convention as `flows.build`.- let stream = read_flow_stream_target(¶ms);- to_json(ops::flows_discover(&ctx, &config, stream).await?)- })-}--/// Read the optional `thread_id` / `request_id` streaming params shared by-/// `flows.build` and `flows.discover` into an [`ops::FlowStreamTarget`].-/// Returns `None` (headless run) when no usable `thread_id` is present; a-/// missing `request_id` is filled with a fresh uuid inside `from_params`.-fn read_flow_stream_target(params: &Map<String, Value>) -> Option<ops::FlowStreamTarget> {- let thread_id = params- .get("thread_id")- .and_then(Value::as_str)- .map(str::to_string);- let request_id = params- .get("request_id")- .and_then(Value::as_str)- .map(str::to_string);- ops::FlowStreamTarget::from_params(thread_id, request_id)-}--fn handle_list_suggestions(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let status = params- .get("status")- .and_then(Value::as_str)- .map(str::trim)- .filter(|s| !s.is_empty())- .map(tinyflows_catalog::SuggestionStatus::from_str_lossy);- to_json(ops::flows_list_suggestions(&config, status).await?)- })-}--fn handle_dismiss_suggestion(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_dismiss_suggestion(&config, id.trim()).await?)- })-}--fn handle_mark_suggestion_built(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_mark_suggestion_built(&config, id.trim()).await?)- })-}--fn handle_required_connections(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let graph = read_required::<Value>(¶ms, "graph")?;- to_json(ops::flows_required_connections(&ctx, &config, graph).await?)- })-}--fn handle_approval_manifest(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = params- .get("id")- .and_then(Value::as_str)- .filter(|s| !s.trim().is_empty())- .map(str::to_string);- let graph = params.get("graph").filter(|v| !v.is_null()).cloned();- to_json(ops::flows_approval_manifest(&config, id.as_deref(), graph).await?)- })-}--fn handle_search_tool_catalog(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let query = read_required::<String>(¶ms, "query")?;- let toolkit = params- .get("toolkit")- .and_then(Value::as_str)- .filter(|s| !s.is_empty());- let limit = params- .get("limit")- .and_then(Value::as_u64)- .map(|n| n as usize)- .unwrap_or(25);- to_json(ops::flows_search_tool_catalog(&config, query.trim(), toolkit, limit).await?)- })-}--fn handle_get_tool_contract(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let slug = read_required::<String>(¶ms, "slug")?;- to_json(ops::flows_get_tool_contract(&config, slug.trim()).await?)- })-}--fn handle_get_history(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let limit = params- .get("limit")- .and_then(Value::as_u64)- .map(|n| n as usize)- .unwrap_or(20);- to_json(ops::flows_get_history(&config, id.trim(), limit)?)- })-}--fn handle_rollback(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let revision_id = read_required::<String>(¶ms, "revision_id")?;- let expected_version = params- .get("expected_version")- .and_then(Value::as_str)- .filter(|s| !s.is_empty())- .map(str::to_string);- to_json(- ops::flows_rollback(&config, id.trim(), revision_id.trim(), expected_version).await?,- )- })-}--fn handle_draft_create(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let name = read_required::<String>(¶ms, "name")?;- let graph = read_required::<Value>(¶ms, "graph")?;- let flow_id = params- .get("flow_id")- .and_then(Value::as_str)- .filter(|s| !s.is_empty())- .map(str::to_string);- let origin = params- .get("origin")- .and_then(Value::as_str)- .and_then(|s| serde_json::from_value(Value::String(s.to_string())).ok())- .unwrap_or(tinyflows_catalog::DraftOrigin::Canvas);- to_json(ops::flows_draft_create(- &config, flow_id, name, graph, origin,- )?)- })-}--fn handle_draft_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_draft_get(&config, id.trim())?)- })-}--fn handle_draft_update(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let name = params- .get("name")- .filter(|v| !v.is_null())- .map(|v| serde_json::from_value(v.clone()))- .transpose()- .map_err(|e| format!("invalid 'name': {e}"))?;- let graph = params.get("graph").filter(|v| !v.is_null()).cloned();- // A present `flow_id` (even null) re-links the draft; absent leaves it.- let flow_id = parse_draft_update_flow_id(¶ms)?;- to_json(ops::flows_draft_update(- &config,- id.trim(),- name,- graph,- flow_id,- )?)- })-}--fn handle_draft_list(ctx: DispatchCtx, _params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- to_json(ops::flows_draft_list(&config)?)- })-}--fn handle_draft_delete(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- to_json(ops::flows_draft_delete(&config, id.trim())?)- })-}--fn handle_draft_promote(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {- Box::pin(async move {- let config = ctx.load_config().await?;- let id = read_required::<String>(¶ms, "id")?;- let require_approval = params.get("require_approval").and_then(Value::as_bool);- to_json(ops::flows_draft_promote(&config, id.trim(), require_approval).await?)- })-}--fn read_required<T: DeserializeOwned>(params: &Map<String, Value>, key: &str) -> Result<T, String> {- let value = params- .get(key)- .cloned()- .ok_or_else(|| format!("missing required param '{key}'"))?;- serde_json::from_value(value).map_err(|e| format!("invalid '{key}': {e}"))-}--fn to_json<T: serde::Serialize>(outcome: Outcome<T>) -> Result<Value, String> {- outcome.into_cli_compatible_json()-}--/// Parses `draft_update`'s `flow_id` param (R-m7). The outer `Option`-/// mirrors `ops::flows_draft_update`'s "present vs absent" contract — absent-/// leaves the draft's existing link untouched; the inner `Option` is the new-/// link (`None` unlinks).-///-/// A present-but-non-string `flow_id` (a number, or an object from a buggy-/// client) is REJECTED rather than silently coerced into `Some(None)` via-/// `Value::as_str()` returning `None` on a type mismatch — that shape used-/// to be indistinguishable from an explicit `flow_id: null` unlink, and-/// `update_draft` treats `Some(None)` as exactly that: unlinking the draft-/// from its flow. A later `draft_promote` then creates a brand-new flow-/// instead of updating the one the caller actually meant.-fn parse_draft_update_flow_id(- params: &Map<String, Value>,-) -> Result<Option<Option<String>>, String> {- match params.get("flow_id") {- None => Ok(None),- Some(Value::Null) => Ok(Some(None)),- Some(Value::String(s)) => {- let s = s.trim();- Ok(Some(if s.is_empty() {- None- } else {- Some(s.to_string())- }))- }- Some(other) => Err(format!(- "invalid 'flow_id': expected a string or null, got {other}"- )),- }-}--#[cfg(test)]-#[path = "schemas_tests.rs"]-mod tests;