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.

6.9 KB
Raw Blame
//! The daemon's client for the agent-harness service: open sessions, drive
//! them. Both calls act as the harness, and control calls additionally act
//! for the user whose mention they forward.

use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};
use agent_session::domain::model::AgentSessionId;
use agent_session::inbound::axum_router::{
    ControlRequest, CreateAgentSessionRequest, CreateAgentSessionResponse,
    ThreadSessionExistsResponse,
};
use macro_user_id::user_id::MacroUserIdStr;
use reqwest::StatusCode;
use trigger_context::TriggerContext;

use crate::config::{HarnessCredentials, MacroApi};

const HARNESS_TOKEN_HEADER: &str = "x-macro-harness-token";
const HARNESS_ACTING_USER_HEADER: &str = "x-macro-harness-for-macro-user-id";

/// A failure calling the agent-harness service.
#[derive(Debug, thiserror::Error)]
pub enum ApiError {
    /// The request could not be sent or the response not read.
    #[error(transparent)]
    Http(#[from] reqwest::Error),
    /// A bound agent already has a session for the thread.
    #[error("this agent already has a session for this thread")]
    ThreadSessionExists {
        /// The existing session, when the service could name it.
        session: Option<AgentSessionId>,
    },
    /// The service refused the request.
    #[error("the service answered {status}: {message}")]
    Refused {
        /// The status the service answered with.
        status: StatusCode,
        /// The response body, verbatim.
        message: String,
    },
}

/// Client for the agent-harness service, acting as one harness.
pub struct HarnessApi {
    http: reqwest::Client,
    base: String,
    token: String,
}

impl HarnessApi {
    /// Build a client from the daemon's config and paired credentials.
    pub fn new(config: &MacroApi, credentials: &HarnessCredentials) -> Self {
        Self {
            http: reqwest::Client::new(),
            base: config.api_url.trim_end_matches('/').to_owned(),
            token: credentials.token.clone(),
        }
    }

    /// Open an external session for one of this harness's bound agents,
    /// acting for the user who mentioned the agent.
    ///
    /// The acting-user header is what the service verifies the session's owner
    /// against (owner for a private harness, a team member for a team one), so
    /// it carries the sender the way the control calls do - the body's `owner`
    /// is not trusted for a harness caller.
    ///
    /// A thread routes to at most one of a bot's sessions, so a redelivered
    /// mention lands on [`ApiError::ThreadSessionExists`] carrying the
    /// session already serving it.
    pub async fn create_session(
        &self,
        request: &CreateAgentSessionRequest,
        acting_user: &MacroUserIdStr<'static>,
    ) -> Result<CreateAgentSessionResponse, ApiError> {
        // A trigger stream is not replayed. Creation can commit before reply
        // reservation fails, so retry the same identity before any prompt.
        let attempts = if request.id.is_some() && request.thread.is_some() {
            2
        } else {
            0
        };
        tokio_retry::RetryIf::start(
            tokio_retry::strategy::ExponentialBackoff::from_millis(2)
                .factor(100)
                .take(attempts),
            || self.create_session_once(request, acting_user),
            |error: &ApiError| match error {
                ApiError::Http(_) => true,
                ApiError::Refused { status, .. } => {
                    status.is_server_error() || *status == StatusCode::TOO_MANY_REQUESTS
                }
                ApiError::ThreadSessionExists { .. } => false,
            },
        )
        .await
    }

    async fn create_session_once(
        &self,
        request: &CreateAgentSessionRequest,
        acting_user: &MacroUserIdStr<'static>,
    ) -> Result<CreateAgentSessionResponse, ApiError> {
        let response = self
            .http
            .post(format!("{}/agent-sessions", self.base))
            .header(HARNESS_TOKEN_HEADER, &self.token)
            .header(HARNESS_ACTING_USER_HEADER, acting_user.as_ref())
            .json(request)
            .send()
            .await?;
        if response.status() == StatusCode::CONFLICT {
            let session = response
                .json::<ThreadSessionExistsResponse>()
                .await
                .ok()
                .and_then(|conflict| conflict.session_id);
            return Err(ApiError::ThreadSessionExists { session });
        }
        let response = refuse_errors(response).await?;
        Ok(response.json().await?)
    }

    /// Ask a session to do something, acting for the user who asked.
    ///
    /// Announcing the result into its channel is not this call's business:
    /// the harness service posts the chip from the trigger event it observed.
    pub async fn control(
        &self,
        session: AgentSessionId,
        actor: &MacroUserIdStr<'static>,
        action: AgentAction,
        context: Option<TriggerContext>,
    ) -> Result<(), ApiError> {
        self.control_with_id(session, actor, action, context, None)
            .await
    }

    async fn control_with_id(
        &self,
        session: AgentSessionId,
        actor: &MacroUserIdStr<'static>,
        action: AgentAction,
        context: Option<TriggerContext>,
        action_id: Option<AgentActionId>,
    ) -> Result<(), ApiError> {
        let response = self
            .http
            .post(format!("{}/agent-sessions/{session}/control", self.base))
            .header(HARNESS_TOKEN_HEADER, &self.token)
            .header(HARNESS_ACTING_USER_HEADER, actor.as_ref())
            .json(&ControlRequest {
                action_id,
                context,
                action,
            })
            .send()
            .await?;
        refuse_errors(response).await?;
        Ok(())
    }

    /// Deliver a prompt to a session, acting for the user who sent it, with
    /// why the agent was called as the trigger read it.
    pub async fn prompt(
        &self,
        session: AgentSessionId,
        sender: &MacroUserIdStr<'static>,
        text: &str,
        message_id: macro_uuid::Uuid,
        context: Option<TriggerContext>,
    ) -> Result<(), ApiError> {
        self.control_with_id(
            session,
            sender,
            AgentAction::prompt(text),
            context,
            Some(AgentActionId::from_uuid(message_id)),
        )
        .await
    }
}

async fn refuse_errors(response: reqwest::Response) -> Result<reqwest::Response, ApiError> {
    let status = response.status();
    if status.is_success() {
        return Ok(response);
    }
    let message = response.text().await.unwrap_or_default();
    if status == StatusCode::UNAUTHORIZED || status == StatusCode::FORBIDDEN {
        tracing::error!(
            %status,
            "the server refused this harness's credentials; press p to re-pair"
        );
    }
    Err(ApiError::Refused { status, message })
}

#[cfg(test)]
mod test;