Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! Thin AI adapter for connector discovery.
use crate::domain::{
ports::{
ConnectionStore, ConnectorCapabilities, ConnectorDirectory, ConnectorOverlays,
OverlayConnections,
},
service::discovery::{ConnectorDiscovery, DiscoveryRequest, DiscoveryResult},
};
use ai_toolset::{
AsyncTool, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations, ToolCallError,
ToolResult,
};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
/// Dependencies for the discovery tool.
pub struct ConnectorToolContext<D, S, N> {
/// Domain service, constructed by the host.
pub service: Arc<ConnectorDiscovery<D, S, N>>,
}
impl<D, S, N> Clone for ConnectorToolContext<D, S, N> {
fn clone(&self) -> Self {
Self {
service: self.service.clone(),
}
}
}
/// Discover apps when the current tools cannot perform the user's task.
#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(
title = "DiscoverConnectors",
description = "Find an integration when an external app is missing or a workflow requires an unconnected account, including native import tools that need a connector. Use SearchTools for available actions. Search by app name, then inspect a returned app_slug to see its actual tools and connection status. Only recommend connecting if an advertised tool supports the task; an app's name alone is not evidence. Inspection does not load or execute tools. If suitable and unconnected, include the returned connect_markup in your reply and explain what connecting enables. After connection, use SearchTools again in the same agent session to load and call the new tools."
)]
pub struct DiscoverConnectors {
/// Search for an app, or inspect a catalog result's actual capabilities.
pub request: DiscoveryRequest,
}
/// Discovery metadata and optional markup for a user-controlled auth flow.
#[derive(Debug, Serialize, JsonSchema, utoipa::ToSchema)]
pub struct DiscoverConnectorsResponse {
/// Catalog or capability results.
pub result: DiscoveryResult,
/// Include verbatim only if an advertised tool supports the requested task.
pub connect_markup: Option<String>,
}
impl ToolAnnotated for DiscoverConnectors {
const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Discover connectors");
}
#[async_trait]
impl<D, S, N> AsyncTool<ConnectorToolContext<D, S, N>> for DiscoverConnectors
where
D: ConnectorDirectory + ConnectorCapabilities,
S: ConnectionStore + ConnectorOverlays,
N: OverlayConnections,
{
type Output = DiscoverConnectorsResponse;
#[tracing::instrument(skip_all, err)]
async fn call(
&self,
context: ServiceContext<ConnectorToolContext<D, S, N>>,
request: RequestContext,
) -> ToolResult<Self::Output> {
let result = context
.service
.discover(&request.user_id, &self.request)
.await
.map_err(|error| ToolCallError {
description: error.to_string(),
internal_error: error,
})?;
Ok(DiscoverConnectorsResponse::from(result))
}
}
impl From<DiscoveryResult> for DiscoverConnectorsResponse {
/// Offer connect markup only for an unconnected app whose inspected tools
/// could do something.
fn from(result: DiscoveryResult) -> Self {
let connect_markup = match &result {
DiscoveryResult::Inspect {
app,
connected: false,
tools,
..
} if !tools.is_empty() => {
let payload = serde_json::json!({"appSlug": app.app_slug, "name": app.name});
Some(format!("<m-connect-app>{payload}</m-connect-app>"))
}
_ => None,
};
Self {
result,
connect_markup,
}
}
}