Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(flows): share typed payload extraction across proposal lookups

Steven Enamakel47bf800bc91f
diff --git a/crates/openhuman-core/src/agent/orchestration/tools.rs b/crates/openhuman-core/src/agent/orchestration/tools.rs
index 4f4c87a692..cf6dc032c3 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools.rs
@@ -43,6 +43,8 @@ mod spawn_parallel_agents;
mod spawn_subagent;
#[path = "tools/spawn_worker_thread.rs"]
pub mod spawn_worker_thread;
+#[path = "tools/structured_payload.rs"]
+pub(crate) mod structured_payload;
#[path = "tools/steer_subagent.rs"]
mod steer_subagent;
#[path = "tools/subagent_abort_report.rs"]
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
index fe8421b4e1..c54eafd32a 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
@@ -533,15 +533,13 @@ fn durable_task_key_source(
pub(crate) fn extract_workflow_proposal_from_history(
history: &[TranscriptMessage],
) -> Option<serde_json::Value> {
- history
- .iter()
- .rev()
- .filter(|message| message.role == "tool")
- .find_map(|message| {
- let value: serde_json::Value = serde_json::from_str(message.content.trim()).ok()?;
- (value.get("type").and_then(|t| t.as_str()) == Some("workflow_proposal"))
- .then_some(value)
- })
+ crate::agent::orchestration::tools::structured_payload::extract_last_typed_payload(
+ history
+ .iter()
+ .filter(|message| message.role == "tool")
+ .map(|message| message.content.as_str()),
+ "workflow_proposal",
+ )
}
/// Durably surface a workflow proposal found in a finished child's history:
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs b/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs
new file mode 100644
index 0000000000..680f4d1500
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/tools/structured_payload.rs
@@ -0,0 +1,16 @@
+//! Shared projection of typed JSON payloads from product tool-result history.
+
+use serde_json::Value;
+
+/// Return the last tool-result JSON object whose `type` matches `expected_type`.
+/// Callers adapt their transcript representation to an iterator of result text.
+pub(crate) fn extract_last_typed_payload<'a>(
+ contents: impl IntoIterator<Item = &'a str>,
+ expected_type: &str,
+) -> Option<Value> {
+ contents
+ .into_iter()
+ .filter_map(|content| serde_json::from_str::<Value>(content.trim()).ok())
+ .filter(|value| value.get("type").and_then(Value::as_str) == Some(expected_type))
+ .last()
+}
diff --git a/crates/openhuman-core/src/flows/copilot_bus_tests.rs b/crates/openhuman-core/src/flows/copilot_bus_tests.rs
index c6abd75b59..a5a175922b 100644
--- a/crates/openhuman-core/src/flows/copilot_bus_tests.rs
+++ b/crates/openhuman-core/src/flows/copilot_bus_tests.rs
@@ -129,3 +129,47 @@ async fn concurrent_tool_callbacks_keep_each_explicit_profile_scope() {
assert!(bob_result.output().contains("Bob only"));
assert!(!bob_result.output().contains("Alice only"));
}
+
+#[tokio::test]
+async fn module_run_flow_uses_the_host_detached_runner_and_requires_a_saved_flow() {
+ let workspace = tempfile::tempdir().expect("temporary workspace");
+ let mut config = Config::default();
+ config.workspace_dir = workspace.path().join("workspace");
+ config.action_dir = config.workspace_dir.clone();
+ config.config_path = workspace.path().join("config.toml");
+ std::fs::create_dir_all(&config.workspace_dir).expect("workspace directory");
+ let catalog = CopilotToolCatalog::load(Arc::new(config.clone()))
+ .expect("module tool catalog loads");
+ let tool = catalog.tool("run_flow").expect("run_flow is catalogued");
+
+ let missing = tool
+ .execute(json!({ "flow_id": "not-saved" }))
+ .await
+ .expect("missing flow is a reported tool failure");
+ assert!(missing.is_error, "{}", missing.output());
+ assert!(missing.output().contains("not-saved"), "{}", missing.output());
+
+ let flow = crate::flows::ops::flows_create(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ "Detached Copilot run".into(),
+ json!({
+ "nodes": [{"id":"trigger","kind":"trigger","name":"Trigger","config":{}}],
+ "edges": []
+ }),
+ false,
+ )
+ .await
+ .expect("saved flow creates")
+ .value;
+ let result = tool
+ .execute(json!({ "flow_id": flow.id, "input": { "source": "copilot" } }))
+ .await
+ .expect("saved flow starts through the host callback");
+ assert!(!result.is_error, "{}", result.output());
+ let started: Value = serde_json::from_str(&result.output()).expect("run result JSON");
+ assert_eq!(started["type"], "workflow_run_started");
+ assert_eq!(started["status"], "running");
+ assert!(started["run_id"].as_str().is_some_and(|id| !id.is_empty()));
+ assert_eq!(started["flow_id"], flow.id);
+}
diff --git a/crates/openhuman-core/src/flows/ops/builder.rs b/crates/openhuman-core/src/flows/ops/builder.rs
index 3f3adc022f..ac7fc8af89 100644
--- a/crates/openhuman-core/src/flows/ops/builder.rs
+++ b/crates/openhuman-core/src/flows/ops/builder.rs
@@ -467,19 +467,16 @@ pub(super) fn extract_workflow_proposal(
history: &[tinytools_agent::dialect::TranscriptEntry],
) -> Option<Value> {
use tinytools_agent::dialect::TranscriptEntry;
- let mut latest = None;
- for message in history {
- if let TranscriptEntry::ToolResults(results) = message {
- for result in results {
- if let Ok(value) = serde_json::from_str::<Value>(&result.content) {
- if value.get("type").and_then(Value::as_str) == Some("workflow_proposal") {
- latest = Some(value);
- }
- }
- }
- }
- }
- latest
+ crate::agent::orchestration::tools::structured_payload::extract_last_typed_payload(
+ history.iter().flat_map(|message| match message {
+ TranscriptEntry::ToolResults(results) => results
+ .iter()
+ .map(|result| result.content.as_str())
+ .collect::<Vec<_>>(),
+ _ => Vec::new(),
+ }),
+ "workflow_proposal",
+ )
}
/// Keep the builder's first turn from auto-resuming another flow's session.