Skip to content

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

refactor(flows): extract native tool invocation into shared flow backend

Steven Enamakela6837a108241
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs b/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
index a40f3dffcf..ab84d1a6a6 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/tools/native.rs
@@ -1,22 +1,12 @@
-//! The `oh:` namespace — native OpenHuman tools.
-//!
-//! A "Tool" node in a flow, as opposed to an "App action" node. The slug names
-//! a tool in the same agent registry the assistant itself uses, so a flow
-//! reaches the full toolset (search, media generation, file, shell, …) rather
-//! than a flow-specific subset.
-//!
-//! Composio's curation gate does not apply here — an `oh:` slug is not a
-//! Composio action — but the autonomy-tier and approval gates do, and for the
-//! same reason they apply to the assistant: a flow is another way to reach the
-//! same effects.
+//! Thin TinyFlows registration and error adapter for native OpenHuman tools.
use async_trait::async_trait;
use serde_json::Value;
+use tinybox_bus::CommandClass;
use tinyflows::error::{EngineError, Result};
use super::{ToolBackend, ToolCallCtx, NATIVE_TOOL_PREFIX};
-/// Dispatches `oh:<tool_name>` to the agent tool registry.
pub(crate) struct NativeToolBackend;
#[async_trait]
@@ -36,91 +26,93 @@ impl ToolBackend for NativeToolBackend {
args: Value,
_conn: Option<&str>,
) -> Result<Value> {
- let tool_name = slug.strip_prefix(NATIVE_TOOL_PREFIX).unwrap_or(slug).trim();
- if tool_name.is_empty() {
- return Err(EngineError::Capability(
- "tool_call node: native tool slug is empty (expected `oh:<tool_name>`)".to_string(),
- ));
- }
-
- let security = std::sync::Arc::new(
- super::super::current_tier_policy(ctx.dispatch, ctx.config, ctx.security).await?,
- );
- let class = crate::tools::native_ops::classify_tool_call_with_security(
+ crate::tools::native_ops::flow_backend::invoke(
ctx.config,
- security.clone(),
- tool_name,
- &args,
+ slug,
+ args,
+ &NativeFlowPolicyHooks { ctx },
)
- .map_err(EngineError::Capability)?;
- let tier_decision = super::super::enforce_node_tier_gate(&security, class, "tool_call")?;
- let summary = crate::security::approval::summarize_action(tool_name, &args);
- let redacted = crate::security::approval::redact_args(&args);
+ .await
+ .map_err(EngineError::Capability)
+ }
+}
+
+struct NativeFlowPolicyHooks<'a, 'ctx> {
+ ctx: &'a ToolCallCtx<'ctx>,
+}
+
+#[async_trait]
+impl crate::tools::native_ops::flow_backend::FlowPolicyHooks
+ for NativeFlowPolicyHooks<'_, '_>
+{
+ async fn resolve_policy(
+ &self,
+ config: &crate::config::Config,
+ ) -> std::result::Result<std::sync::Arc<crate::security::SecurityPolicy>, String> {
+ super::super::current_tier_policy(self.ctx.dispatch, config, self.ctx.security)
+ .await
+ .map(std::sync::Arc::new)
+ .map_err(|error| match error {
+ EngineError::Capability(body) => body,
+ other => other.to_string(),
+ })
+ }
+
+ async fn authorize(
+ &self,
+ policy: &crate::security::SecurityPolicy,
+ class: CommandClass,
+ tool_name: &str,
+ args: &Value,
+ ) -> std::result::Result<Option<String>, String> {
+ let tier_decision = super::super::enforce_node_tier_gate(policy, class, "tool_call")
+ .map_err(|error| match error {
+ EngineError::Capability(body) => body,
+ other => other.to_string(),
+ })?;
+ let summary = crate::security::approval::summarize_action(tool_name, args);
+ let redacted = crate::security::approval::redact_args(args);
let (outcome, audit_id) = super::super::gate_call_for_tier(
- ctx.dispatch,
- ctx.approval,
+ self.ctx.dispatch,
+ self.ctx.approval,
tier_decision,
tool_name,
&summary,
redacted,
)
.await;
- if let crate::security::approval::GateOutcome::Deny { reason } = outcome {
- return Err(EngineError::Capability(reason));
+ match outcome {
+ crate::security::approval::GateOutcome::Deny { reason } => Err(reason),
+ crate::security::approval::GateOutcome::Allow => Ok(audit_id),
}
+ }
+
+ fn record_execution(
+ &self,
+ audit_id: Option<&str>,
+ result: &std::result::Result<Value, String>,
+ ) {
+ let Some(id) = audit_id else {
+ return;
+ };
+ let Some(gate) = crate::security::approval::ApprovalGate::try_global() else {
+ return;
+ };
+ let execution = if result.is_ok() {
+ crate::security::approval::ExecutionOutcome::Success
+ } else {
+ crate::security::approval::ExecutionOutcome::Failure
+ };
+ let error = result
+ .as_ref()
+ .err()
+ .map(|body| EngineError::Capability(body.clone()).to_string());
tracing::debug!(
target: "flows",
- %tool_name,
- ?class,
- ?tier_decision,
- "[flows] tool_call: dispatching NATIVE OpenHuman tool"
+ audit_id = %id,
+ success = result.is_ok(),
+ "[flows] recording native tool execution outcome on the approval audit trail"
);
- let exec_result = crate::tools::native_ops::execute_tool_with_security(
- ctx.config, security, tool_name, args, false,
- )
- .await
- .map_err(EngineError::Capability)
- .and_then(|outcome| {
- crate::tools::native_ops::flow_backend::reject_failed_native_tool_result(
- slug,
- &outcome.result,
- )
- .map_err(EngineError::Capability)
- .map(|_| crate::tools::native_ops::flow_backend::native_tool_payload(&outcome.result))
- });
-
- // E-m1: close out the approval audit row (unlike the http/code/
- // Composio paths, this used to discard the request id and never
- // call `record_execution`, leaving every prompted-and-approved
- // native `oh:` tool_call's audit trail stuck open with no
- // Success/Failure terminal outcome).
- if let Some(id) = &audit_id {
- if let Some(gate) = crate::security::approval::ApprovalGate::try_global() {
- let exec = if exec_result.is_ok() {
- crate::security::approval::ExecutionOutcome::Success
- } else {
- crate::security::approval::ExecutionOutcome::Failure
- };
- tracing::debug!(
- target: "flows",
- %tool_name,
- audit_id = %id,
- success = exec_result.is_ok(),
- "[flows] tool_call: recording native tool execution outcome on the \
- approval audit trail"
- );
- gate.record_execution(
- id,
- exec,
- exec_result
- .as_ref()
- .err()
- .map(ToString::to_string)
- .as_deref(),
- );
- }
- }
-
- exec_result
+ gate.record_execution(id, execution, error.as_deref());
}
}
diff --git a/crates/openhuman-core/src/tools/native_ops/flow_backend.rs b/crates/openhuman-core/src/tools/native_ops/flow_backend.rs
index 34242a0c7b..a190d913b3 100644
--- a/crates/openhuman-core/src/tools/native_ops/flow_backend.rs
+++ b/crates/openhuman-core/src/tools/native_ops/flow_backend.rs
@@ -2,9 +2,59 @@
//! TinyFlows. Tool execution itself remains in [`super`], which owns the
//! native registry and its policy snapshot.
+use std::sync::Arc;
+
+use async_trait::async_trait;
use serde_json::{json, Value};
+use tinybox_bus::CommandClass;
use tinytools::ToolResult;
+use crate::{config::Config, security::SecurityPolicy};
+
+/// Flow-specific policy and approval operations supplied by the host adapter.
+#[async_trait]
+pub(crate) trait FlowPolicyHooks: Send + Sync {
+ async fn resolve_policy(&self, config: &Config) -> Result<Arc<SecurityPolicy>, String>;
+
+ async fn authorize(
+ &self,
+ policy: &SecurityPolicy,
+ class: CommandClass,
+ tool_name: &str,
+ args: &Value,
+ ) -> Result<Option<String>, String>;
+
+ fn record_execution(&self, audit_id: Option<&str>, result: &Result<Value, String>);
+}
+
+/// Invokes a native tool for a flow while keeping TinyFlows policy operations
+/// behind neutral host hooks.
+pub(crate) async fn invoke(
+ config: &Config,
+ slug: &str,
+ args: Value,
+ hooks: &dyn FlowPolicyHooks,
+) -> Result<Value, String> {
+ let tool_name = slug.strip_prefix("oh:").unwrap_or(slug).trim();
+ if tool_name.is_empty() {
+ return Err("tool_call node: native tool slug is empty (expected `oh:<tool_name>`)".into());
+ }
+
+ let policy = hooks.resolve_policy(config).await?;
+ let class = super::classify_tool_call_with_security(config, policy.clone(), tool_name, &args)?;
+ let audit_id = hooks.authorize(&policy, class, tool_name, &args).await?;
+ tracing::debug!(target: "flows", %tool_name, ?class, "[flows] dispatching NATIVE OpenHuman tool");
+ let result = super::execute_tool_with_security(config, policy, tool_name, args, false)
+ .await
+ .map_err(|error| format!("{error}"))
+ .and_then(|outcome| {
+ reject_failed_native_tool_result(slug, &outcome.result)
+ .map(|()| native_tool_payload(&outcome.result))
+ });
+ hooks.record_execution(audit_id.as_deref(), &result);
+ result
+}
+
/// Turns a failed native tool result into the flow-facing error body.
pub(crate) fn reject_failed_native_tool_result(
slug: &str,