Skip to content

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

refactor(profiles): migrate profile permission hook to generic tool hook

Steven Enamakel1804ac5135c8
diff --git a/crates/openhuman-core/src/profiles/capabilities.rs b/crates/openhuman-core/src/profiles/capabilities.rs
index 672024879c..62b2891966 100644
--- a/crates/openhuman-core/src/profiles/capabilities.rs
+++ b/crates/openhuman-core/src/profiles/capabilities.rs
@@ -4,7 +4,8 @@
//! call additionally passes the profile lease fence and the host permission
//! callback. These adapters never install process-wide user capabilities.
-use crate::agent::hooks::{ToolHook, ToolHookContext, ToolHookDecision};
+use tinyagents_hooks::tool::{ToolHook, ToolHookContext, ToolHookDecision};
+use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::agent::{HostTools, HostTurnTools, TurnContext};
use crate::config::Config;
use crate::core::runtime::{ContextOverlay, CoreContext, DispatchCtx};
@@ -17,7 +18,7 @@ use tinytools::PermissionLevel;
pub struct ProfileCapabilities {
host: Option<HostTools>,
names: Option<HashSet<String>>,
- permission: Option<Arc<dyn ToolHook>>,
+ permission: Option<Arc<dyn ToolHook<OpenHumanRunContext>>>,
channels: HashMap<String, PermissionLevel>,
groups: Vec<super::tools::SaasToolGroup>,
/// The shared engine, resolved only under the forced profile memory root.
@@ -31,7 +32,7 @@ impl ProfileCapabilities {
pub fn new(
host: Option<HostTools>,
names: Option<HashSet<String>>,
- permission: Option<Arc<dyn ToolHook>>,
+ permission: Option<Arc<dyn ToolHook<OpenHumanRunContext>>>,
channels: HashMap<String, PermissionLevel>,
groups: Vec<super::tools::SaasToolGroup>,
memory_engine: Option<Arc<dyn tinymemory_api::MemoryEngine>>,
@@ -237,7 +238,7 @@ impl ProfileCapabilities {
self: &Arc<Self>,
profile: &super::ProfileId,
fence: Arc<crate::storage::fence::LeaseFence>,
- ) -> Arc<dyn ToolHook> {
+ ) -> Arc<dyn ToolHook<OpenHumanRunContext>> {
Arc::new(ProfilePermission {
capabilities: Arc::clone(self),
profile: profile.as_str().to_owned(),
@@ -253,39 +254,28 @@ struct ProfilePermission {
}
#[async_trait::async_trait]
-impl ToolHook for ProfilePermission {
+impl ToolHook<OpenHumanRunContext> for ProfilePermission {
fn name(&self) -> &str {
"profile.can_use_tool"
}
- async fn before_tool(&self, _: &ToolHookContext) -> anyhow::Result<()> {
- Ok(())
- }
- async fn after_tool(&self, _: &ToolHookContext) -> anyhow::Result<()> {
- Ok(())
- }
- async fn before_tool_decision(&self, context: &ToolHookContext) -> ToolHookDecision {
- // A caller that raised the call without a turn context: the calling
- // task's own, as every call before the context was passed explicitly.
- self.before_tool_decision_in(&DispatchCtx::from_weak(None), context)
- .await
- }
- async fn before_tool_decision_in(
+ async fn before_tool_decision(
&self,
- dispatch: &DispatchCtx,
+ host: &OpenHumanRunContext,
context: &ToolHookContext,
) -> ToolHookDecision {
+ let dispatch = host.dispatch_ctx();
let tenant = dispatch.tenant().ok().and_then(|tenant| tenant.profile);
if tenant.as_deref() != Some(self.profile.as_str())
|| self.fence.check_local(super::lease::now_ms()).is_err()
- || !self.capabilities.admits(dispatch, &context.tool_name)
+ || !self.capabilities.admits(&dispatch, &context.tool_name)
{
- return ToolHookDecision::Deny(
+ return ToolHookDecision::deny(
"tool is outside the hosted profile capability boundary".into(),
);
}
match &self.capabilities.permission {
Some(permission) => {
- let binding = super::provider_settings::ProfileProviderSettings::in_ctx(dispatch);
+ let binding = super::provider_settings::ProfileProviderSettings::in_ctx(&dispatch);
let canonical = binding
.as_ref()
.and_then(|binding| binding.canonical_role_name(&context.tool_name));
@@ -293,30 +283,28 @@ impl ToolHook for ProfilePermission {
let mut rewritten = false;
if let Some(canonical) = canonical.filter(|name| *name != context.tool_name) {
if !self.capabilities.admits_capability(canonical) {
- return ToolHookDecision::Deny(
+ return ToolHookDecision::deny(
"canonical search capability is denied".into(),
);
}
let mut original = context.clone();
original.tool_name = canonical.to_owned();
- match permission.before_tool_decision(&original).await {
- ToolHookDecision::Proceed => {}
- ToolHookDecision::ProceedWith(arguments) => {
- advertised.arguments = arguments;
- rewritten = true;
- }
- refusal => return refusal,
+ let decision = permission.before_tool_decision(host, &original).await;
+ if decision.is_deny() || decision.is_ask() {
+ return decision;
}
- }
- match permission.before_tool_decision(&advertised).await {
- ToolHookDecision::Proceed if rewritten => {
- ToolHookDecision::ProceedWith(advertised.arguments)
+ if let Some(arguments) = decision.updated_input {
+ advertised.arguments = arguments;
+ rewritten = true;
}
- decision => decision,
+ }
+ let mut decision = permission.before_tool_decision(host, &advertised).await;
+ if rewritten && !decision.is_deny() && !decision.is_ask() {
+ decision.updated_input = Some(advertised.arguments);
}
}
None => {
- ToolHookDecision::Deny("this profile has no host tool permission callback".into())
+ ToolHookDecision::deny("this profile has no host tool permission callback")
}
}
}