Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(ai): exclude the current chat from AI search results (#4164)

Eric Hayes3e5c57257e27
diff --git a/rust/cloud-storage/ai_tools/src/search/search_service/content.rs b/rust/cloud-storage/ai_tools/src/search/search_service/content.rs
index ed61faffe9..4ba4619005 100644
--- a/rust/cloud-storage/ai_tools/src/search/search_service/content.rs
+++ b/rust/cloud-storage/ai_tools/src/search/search_service/content.rs
@@ -1,3 +1,4 @@
+use super::context::SearchToolContext;
use super::types::{PAGE_SIZE, SearchToolResponse};
use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};
use async_trait::async_trait;
@@ -7,9 +8,7 @@ use models_search::{
unified::{UnifiedSearchIndex, UnifiedSearchRequest, entity_filters_from_include},
};
use schemars::JsonSchema;
-use search_service_client::SearchServiceClient;
use serde::Deserialize;
-use std::sync::Arc;
#[derive(Debug, JsonSchema, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
@@ -31,13 +30,13 @@ pub struct ContentSearch {
}
#[async_trait]
-impl AsyncTool<Arc<SearchServiceClient>> for ContentSearch {
+impl AsyncTool<SearchToolContext> for ContentSearch {
type Output = SearchToolResponse;
#[tracing::instrument(skip_all, fields(user_id=?(*request_context.user_id).as_ref()), err)]
async fn call(
&self,
- search_client: ServiceContext<Arc<SearchServiceClient>>,
+ search_context: ServiceContext<SearchToolContext>,
request_context: RequestContext,
) -> ToolResult<Self::Output> {
tracing::info!(self=?self, "Content search params");
@@ -65,7 +64,8 @@ impl AsyncTool<Arc<SearchServiceClient>> for ContentSearch {
collapse: None,
};
- let response = search_client
+ let response = search_context
+ .search_client
.search_unified(
(*request_context.user_id).as_ref(),
search_request,
@@ -78,9 +78,14 @@ impl AsyncTool<Arc<SearchServiceClient>> for ContentSearch {
internal_error: e,
})?;
- Ok(SearchToolResponse {
- results: response.results,
- })
+ // Drop the chat the agent is currently running inside so it never
+ // surfaces itself in its own search results.
+ let mut results = response.results;
+ if let Some(self_chat_id) = search_context.self_chat_id {
+ results.retain(|item| item.entity_id() != self_chat_id);
+ }
+
+ Ok(SearchToolResponse { results })
}
}
diff --git a/rust/cloud-storage/ai_tools/src/search/search_service/context.rs b/rust/cloud-storage/ai_tools/src/search/search_service/context.rs
new file mode 100644
index 0000000000..1e9c98b795
--- /dev/null
+++ b/rust/cloud-storage/ai_tools/src/search/search_service/context.rs
@@ -0,0 +1,37 @@
+use crate::tool_context::ToolServiceContext;
+use ai_usage::AiFeature;
+use axum::extract::FromRef;
+use search_service_client::SearchServiceClient;
+use std::sync::Arc;
+use uuid::Uuid;
+
+/// Context for the search tools.
+///
+/// Carries the search client plus, when the request is an interactive chat, the
+/// id of the chat the agent is running inside. The search tools use that id to
+/// drop the current chat from their results so the agent never "finds itself".
+#[derive(Clone)]
+pub struct SearchToolContext {
+ /// Client used to perform the unified search.
+ pub search_client: Arc<SearchServiceClient>,
+ /// Entity id of the chat this request belongs to, when the request is an
+ /// interactive chat session. `None` for every other feature, in which case
+ /// nothing is excluded.
+ pub self_chat_id: Option<Uuid>,
+}
+
+impl FromRef<ToolServiceContext> for SearchToolContext {
+ fn from_ref(ctx: &ToolServiceContext) -> Self {
+ // `usage_context.entity` is set to the chat id for the Chat feature
+ // (including subagents spawned from a chat, which inherit the feature).
+ // Only treat it as a chat to exclude when the feature is actually Chat;
+ // for other features the entity is some unrelated item.
+ let self_chat_id = matches!(ctx.usage_context.feature, AiFeature::Chat)
+ .then_some(ctx.usage_context.entity)
+ .flatten();
+ SearchToolContext {
+ search_client: ctx.search_service_client.clone(),
+ self_chat_id,
+ }
+ }
+}
diff --git a/rust/cloud-storage/ai_tools/src/search/search_service/mod.rs b/rust/cloud-storage/ai_tools/src/search/search_service/mod.rs
index 2f1548a588..efda66bb03 100644
--- a/rust/cloud-storage/ai_tools/src/search/search_service/mod.rs
+++ b/rust/cloud-storage/ai_tools/src/search/search_service/mod.rs
@@ -1,3 +1,4 @@
pub mod content;
+pub mod context;
pub mod name;
pub mod types;
diff --git a/rust/cloud-storage/ai_tools/src/search/search_service/name.rs b/rust/cloud-storage/ai_tools/src/search/search_service/name.rs
index f6b1a067ff..4028aeab9a 100644
--- a/rust/cloud-storage/ai_tools/src/search/search_service/name.rs
+++ b/rust/cloud-storage/ai_tools/src/search/search_service/name.rs
@@ -1,3 +1,4 @@
+use super::context::SearchToolContext;
use super::types::{PAGE_SIZE, SearchToolResponse};
use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};
use async_trait::async_trait;
@@ -7,9 +8,7 @@ use models_search::{
unified::{UnifiedSearchIndex, UnifiedSearchRequest, entity_filters_from_include},
};
use schemars::JsonSchema;
-use search_service_client::SearchServiceClient;
use serde::Deserialize;
-use std::sync::Arc;
#[derive(Debug, JsonSchema, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
@@ -31,13 +30,13 @@ pub struct NameSearch {
}
#[async_trait]
-impl AsyncTool<Arc<SearchServiceClient>> for NameSearch {
+impl AsyncTool<SearchToolContext> for NameSearch {
type Output = SearchToolResponse;
#[tracing::instrument(skip_all, fields(user_id=?(*request_context.user_id).as_ref()), err)]
async fn call(
&self,
- search_client: ServiceContext<Arc<SearchServiceClient>>,
+ search_context: ServiceContext<SearchToolContext>,
request_context: RequestContext,
) -> ToolResult<Self::Output> {
tracing::info!(self=?self, "Name search params");
@@ -65,7 +64,8 @@ impl AsyncTool<Arc<SearchServiceClient>> for NameSearch {
collapse: None,
};
- let response = search_client
+ let response = search_context
+ .search_client
.search_unified(
(*request_context.user_id).as_ref(),
search_request,
@@ -78,9 +78,14 @@ impl AsyncTool<Arc<SearchServiceClient>> for NameSearch {
internal_error: e,
})?;
- Ok(SearchToolResponse {
- results: response.results,
- })
+ // Drop the chat the agent is currently running inside so it never
+ // surfaces itself in its own search results.
+ let mut results = response.results;
+ if let Some(self_chat_id) = search_context.self_chat_id {
+ results.retain(|item| item.entity_id() != self_chat_id);
+ }
+
+ Ok(SearchToolResponse { results })
}
}