Skip to content

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

feat(search): add proxy support for search requests

Steven Enamakel0018a264c5db
diff --git a/crates/openhuman-core/src/config/ops/search.rs b/crates/openhuman-core/src/config/ops/search.rs
index df3ff4e416..70de133919 100644
--- a/crates/openhuman-core/src/config/ops/search.rs
+++ b/crates/openhuman-core/src/config/ops/search.rs
@@ -295,7 +295,7 @@ pub async fn apply_search_settings(
apply_search_patch(config, update)?;
config.save().await.map_err(|e| e.to_string())?;
#[cfg(feature = "modules")]
- crate::modules::search::refresh_loaded(config).await?;
+ crate::modules::search::refresh_loaded(&crate::core::runtime::DispatchCtx::ambient(), config).await?;
tracing::debug!(
enabled = crate::search::is_active(config),
providers = ?config.search.enabled_provider_names(),
diff --git a/crates/openhuman-core/src/modules/search/proxy.rs b/crates/openhuman-core/src/modules/search/proxy.rs
index 56f4eac5db..efec7ff010 100644
--- a/crates/openhuman-core/src/modules/search/proxy.rs
+++ b/crates/openhuman-core/src/modules/search/proxy.rs
@@ -171,9 +171,10 @@ where
/// it is one agent's in-memory config, its keys exist nowhere else, and a
/// reload of the shared `config_path` would drop them. Any other config is
/// reloaded so a settings or login change is honoured on the next call.
-async fn current_config(config: &Config) -> Result<Config, String> {
+async fn current_config(ctx: &DispatchCtx, config: &Config) -> Result<Config, String> {
if config.profile_providers.is_some() {
- let context = crate::core::runtime::CoreContext::scoped()
+ let context = ctx
+ .core()
.ok_or_else(|| "profile search requires its authenticated context".to_owned())?;
let current = context
.embedder_config()
@@ -193,12 +194,12 @@ async fn current_config(config: &Config) -> Result<Config, String> {
/// Refresh a loaded module after a settings or credential change. An unloaded
/// module gets the current configuration during its first initialization.
-pub async fn refresh_loaded(config: &Config) -> Result<(), String> {
+pub async fn refresh_loaded(ctx: &DispatchCtx, config: &Config) -> Result<(), String> {
if matches!(
crate::modules::ops::state_of(MODULE_ID),
crate::modules::types::ModuleState::Ready
) {
- let current = current_config(config).await?;
+ let current = current_config(ctx, config).await?;
with_module_lock(|| async {
let _ = proxy(&current).await?;
Ok(())
@@ -208,8 +209,8 @@ pub async fn refresh_loaded(config: &Config) -> Result<(), String> {
Ok(())
}
-pub async fn list_tools(config: &Config) -> Result<ListToolsResponse, String> {
- let current = current_config(config).await?;
+pub async fn list_tools(ctx: &DispatchCtx, config: &Config) -> Result<ListToolsResponse, String> {
+ let current = current_config(ctx, config).await?;
with_module_lock(|| async {
let mut listed: ListToolsResponse = proxy(&current)
.await?
@@ -218,11 +219,11 @@ pub async fn list_tools(config: &Config) -> Result<ListToolsResponse, String> {
.map_err(|error| format!("search ListTools failed: {error}"))?;
if let Some(binding) = &current.profile_providers {
binding.present(&mut listed.tools);
- let capabilities = crate::profiles::capabilities::ProfileCapabilities::current();
+ let capabilities = crate::profiles::capabilities::ProfileCapabilities::in_ctx(ctx);
listed.tools.retain(|tool| {
capabilities
.as_ref()
- .is_some_and(|capabilities| capabilities.admits(&tool.name))
+ .is_some_and(|capabilities| capabilities.admits(ctx, &tool.name))
});
}
Ok(listed)
@@ -232,14 +233,15 @@ pub async fn list_tools(config: &Config) -> Result<ListToolsResponse, String> {
/// The presentation and route boundary is checked before any module load or call.
fn canonical_request(
+ ctx: &DispatchCtx,
config: &Config,
mut request: ExecuteToolRequest,
) -> Result<ExecuteToolRequest, String> {
let Some(binding) = &config.profile_providers else {
return Ok(request);
};
- if !crate::profiles::capabilities::ProfileCapabilities::current()
- .is_some_and(|capabilities| capabilities.admits(&request.name))
+ if !crate::profiles::capabilities::ProfileCapabilities::in_ctx(ctx)
+ .is_some_and(|capabilities| capabilities.admits(ctx, &request.name))
{
return Err("search tool is outside the profile capability boundary".into());
}
@@ -293,11 +295,12 @@ fn canonical_request(
}
pub async fn execute_tool(
+ ctx: &DispatchCtx,
config: &Config,
request: ExecuteToolRequest,
) -> Result<ExecuteToolResponse, String> {
- let current = current_config(config).await?;
- let request = canonical_request(&current, request)?;
+ let current = current_config(ctx, config).await?;
+ let request = canonical_request(ctx, &current, request)?;
let tool = request.name.clone();
with_module_lock(|| async {
let proxy = proxy(&current).await?;
diff --git a/crates/openhuman-core/src/search/bus.rs b/crates/openhuman-core/src/search/bus.rs
index 9f0891830e..33c7024bfa 100644
--- a/crates/openhuman-core/src/search/bus.rs
+++ b/crates/openhuman-core/src/search/bus.rs
@@ -29,14 +29,15 @@ impl EventHandler<DomainEvent> for CredentialRefreshSubscriber {
let DomainEvent::CredentialChanged { kind } = event else {
return;
};
- let config = match DispatchCtx::ambient().load_config().await {
+ let ctx = DispatchCtx::ambient();
+ let config = match ctx.load_config().await {
Ok(config) => config,
Err(error) => {
tracing::warn!(kind = %kind, %error, "[search][bus] config unavailable; skipping refresh");
return;
}
};
- match crate::modules::search::refresh_loaded(&config).await {
+ match crate::modules::search::refresh_loaded(&ctx, &config).await {
Ok(()) => {
tracing::debug!(kind = %kind, "[search][bus] module refreshed after credential change")
}
diff --git a/crates/openhuman-core/src/search/tools.rs b/crates/openhuman-core/src/search/tools.rs
index 51a4da7740..d12dec2cc0 100644
--- a/crates/openhuman-core/src/search/tools.rs
+++ b/crates/openhuman-core/src/search/tools.rs
@@ -246,7 +246,8 @@ impl Tool for TinySearchTool {
args: Value,
options: ToolCallOptions,
) -> anyhow::Result<ToolResult> {
- let config = self.live_config(&DispatchCtx::ambient()).await?;
+ let ctx = DispatchCtx::ambient();
+ let config = self.live_config(&ctx).await?;
// Re-read per call, so a key added or a provider switched mid-session
// clears an earlier refusal instead of outliving it.
let signature = self.provider_signature(&config);
@@ -278,7 +279,7 @@ impl Tool for TinySearchTool {
if let Some(message) = local_only_search_block(&request.name) {
return Ok(ToolResult::error(message));
}
- match crate::modules::search::execute_tool(&config, request).await {
+ match crate::modules::search::execute_tool(&ctx, &config, request).await {
Ok(response) => {
tracing::debug!(
tool = %self.spec.name,
diff --git a/crates/openhuman-core/src/tinymemes/search.rs b/crates/openhuman-core/src/tinymemes/search.rs
index 0b83d847dc..57325637ea 100644
--- a/crates/openhuman-core/src/tinymemes/search.rs
+++ b/crates/openhuman-core/src/tinymemes/search.rs
@@ -45,7 +45,11 @@ impl OpenHumanSearch {
name: TOOL.to_string(),
arguments: json!({ "query": query, "max_results": max_results.clamp(1, 20) }),
};
- let response = crate::modules::search::execute_tool(&self.config, request)
+ let response = crate::modules::search::execute_tool(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &self.config,
+ request,
+ )
.await
.map_err(|e| crate::search::tools::user_facing_error(&e))?;
Ok(response