Skip to content

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

refactor(composio): improve execute handler structure

Steven Enamakel029166d69e47
diff --git a/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools.rs b/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools.rs
index b7f5cf1c14..49ecd613d7 100644
--- a/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools.rs
+++ b/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools.rs
@@ -3,7 +3,8 @@
use crate::core::runtime::DispatchCtx;
use serde_json::{Map, Value};
-use crate::core::all::ControllerFuture;
+use crate::config::Config;
+use crate::core::all::{ControllerFuture, ControllerTask};
use crate::integrations::composio::ops;
use super::util::{read_optional, read_required_non_empty, to_json};
@@ -20,9 +21,52 @@ pub(super) fn handle_list_tools(ctx: DispatchCtx, params: Map<String, Value>) ->
pub(super) fn handle_execute(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let config = ctx.load_config().await?;
- let tool = read_required_non_empty(&params, "tool")?;
- let arguments = read_optional::<Value>(&params, "arguments")?;
- let connection_id = read_optional::<String>(&params, "connection_id")?;
- to_json(ops::composio_execute(&config, &tool, arguments, connection_id.as_deref()).await?)
+ let parsed = parse_execute(params)?;
+ run_execute(ctx, config, parsed).await
})
}
+
+#[derive(Debug, PartialEq)]
+pub(super) struct ExecuteParams {
+ tool: String,
+ arguments: Option<Value>,
+ connection_id: Option<String>,
+}
+
+/// Preserve the RPC readers' validation and error precedence for execute.
+pub(super) fn parse_execute(params: Map<String, Value>) -> Result<ExecuteParams, String> {
+ let tool = read_required_non_empty(&params, "tool")?;
+ let arguments = read_optional::<Value>(&params, "arguments")?;
+ let connection_id = read_optional::<String>(&params, "connection_id")?;
+ Ok(ExecuteParams {
+ tool,
+ arguments,
+ connection_id,
+ })
+}
+
+pub(super) fn load_execute_config(ctx: DispatchCtx) -> ControllerTask<Config> {
+ Box::pin(async move { ctx.load_config().await })
+}
+
+pub(super) fn run_execute(
+ _ctx: DispatchCtx,
+ config: Config,
+ params: ExecuteParams,
+) -> ControllerFuture {
+ Box::pin(async move {
+ to_json(
+ ops::composio_execute(
+ &config,
+ &params.tool,
+ params.arguments,
+ params.connection_id.as_deref(),
+ )
+ .await?,
+ )
+ })
+}
+
+#[cfg(test)]
+#[path = "handlers_tools_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools_tests.rs b/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools_tests.rs
new file mode 100644
index 0000000000..3ef616536a
--- /dev/null
+++ b/crates/openhuman-core/src/integrations/composio/schemas/handlers_tools_tests.rs
@@ -0,0 +1,82 @@
+use super::*;
+use serde_json::json;
+
+#[test]
+fn execute_parser_trims_tool_preserves_optional_values_and_ignores_unknown_keys() {
+ let params = Map::from_iter([
+ ("tool".into(), json!(" GMAIL_SEND_EMAIL ")),
+ ("arguments".into(), json!(42)),
+ ("connection_id".into(), json!("")),
+ ("unrecognized".into(), json!(true)),
+ ]);
+
+ assert_eq!(
+ parse_execute(params).unwrap(),
+ ExecuteParams {
+ tool: "GMAIL_SEND_EMAIL".into(),
+ arguments: Some(json!(42)),
+ connection_id: Some(String::new()),
+ }
+ );
+}
+
+#[test]
+fn execute_parser_defaults_missing_and_null_optional_fields_to_none() {
+ let missing = Map::from_iter([("tool".into(), json!("GMAIL_SEND_EMAIL"))]);
+ assert_eq!(
+ parse_execute(missing).unwrap(),
+ ExecuteParams {
+ tool: "GMAIL_SEND_EMAIL".into(),
+ arguments: None,
+ connection_id: None,
+ }
+ );
+
+ let nulls = Map::from_iter([
+ ("tool".into(), json!("GMAIL_SEND_EMAIL")),
+ ("arguments".into(), Value::Null),
+ ("connection_id".into(), Value::Null),
+ ]);
+ assert_eq!(
+ parse_execute(nulls).unwrap(),
+ ExecuteParams {
+ tool: "GMAIL_SEND_EMAIL".into(),
+ arguments: None,
+ connection_id: None,
+ }
+ );
+}
+
+#[test]
+fn execute_parser_preserves_tool_first_errors_and_connection_type_error() {
+ let missing_tool = Map::from_iter([
+ ("arguments".into(), json!("bad-but-json")),
+ ("connection_id".into(), json!(7)),
+ ]);
+ assert_eq!(
+ parse_execute(missing_tool).unwrap_err(),
+ "missing required param 'tool'"
+ );
+
+ let invalid_tool = Map::from_iter([
+ ("tool".into(), json!(42)),
+ ("connection_id".into(), json!(7)),
+ ]);
+ assert!(parse_execute(invalid_tool)
+ .unwrap_err()
+ .starts_with("invalid 'tool':"));
+
+ let empty_tool = Map::from_iter([
+ ("tool".into(), json!(" ")),
+ ("connection_id".into(), json!(7)),
+ ]);
+ assert_eq!(parse_execute(empty_tool).unwrap_err(), "'tool' must not be empty");
+
+ let invalid_connection = Map::from_iter([
+ ("tool".into(), json!("GMAIL_SEND_EMAIL")),
+ ("connection_id".into(), json!(7)),
+ ]);
+ assert!(parse_execute(invalid_connection)
+ .unwrap_err()
+ .starts_with("invalid 'connection_id':"));
+}
diff --git a/crates/openhuman-core/src/integrations/composio/schemas/registry.rs b/crates/openhuman-core/src/integrations/composio/schemas/registry.rs
index 0851449308..1170cbcc28 100644
--- a/crates/openhuman-core/src/integrations/composio/schemas/registry.rs
+++ b/crates/openhuman-core/src/integrations/composio/schemas/registry.rs
@@ -3,7 +3,7 @@
//! [`super::definitions::schemas`] entry to its handler and register it
//! with the shared controller registry.
-use crate::core::all::RegisteredController;
+use crate::core::all::{BoundRegisteredController, ControllerEntry, RegisteredController};
use crate::core::ControllerSchema;
use super::definitions::schemas;
@@ -16,7 +16,9 @@ use super::handlers_identity::{
handle_get_user_profile, handle_get_user_scopes, handle_refresh_all_identities,
handle_set_user_scopes,
};
-use super::handlers_tools::{handle_execute, handle_list_tools};
+use super::handlers_tools::{
+ handle_execute, handle_list_tools, load_execute_config, parse_execute, run_execute,
+};
use super::handlers_triggers::{
handle_create_trigger, handle_disable_trigger, handle_enable_trigger,
handle_list_available_triggers, handle_list_github_repos, handle_list_trigger_history,
@@ -142,3 +144,24 @@ pub fn all_registered_controllers() -> Vec<RegisteredController> {
},
]
}
+
+/// Runtime entries preserve the legacy catalog while binding execute's actual
+/// runtime parser to the same parser used by schema-example validation.
+pub(crate) fn all_controller_entries() -> Vec<ControllerEntry> {
+ let execute = BoundRegisteredController::after_gate(
+ schemas("execute"),
+ load_execute_config,
+ parse_execute,
+ run_execute,
+ );
+ all_registered_controllers()
+ .into_iter()
+ .map(|controller| {
+ if controller.schema.method_name() == "composio.execute" {
+ ControllerEntry::bound(execute.clone())
+ } else {
+ ControllerEntry::legacy(controller)
+ }
+ })
+ .collect()
+}