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.

7.7 KB
Raw Blame
//! Coding delegation tools. User identity always comes from the tool request.

use std::sync::Arc;

use ai_toolset::{
    AsyncTool, AsyncToolCollection, RequestContext, ServiceContext, ToolAnnotated, ToolAnnotations,
    ToolCallError, ToolResult,
};
use async_trait::async_trait;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::domain::coding_agents::{
    CodingAgent, CodingAgentError, CodingAgentService, DispatchCodingAgentRequest,
    DispatchedCodingAgent,
};
use crate::domain::updates::AgentUpdates;

#[cfg(test)]
mod test;

/// Shared coding-session capability used by non-coding agents.
#[derive(Clone)]
pub struct CodingAgentToolContext {
    /// Discovery, authorization, and dispatch remain in the domain service.
    pub service: Arc<dyn CodingAgentService>,
    /// Existing-session actions through the owning harness service.
    pub sessions: Arc<dyn crate::domain::tool_actions::SessionActions>,
}

/// Discover the user's available coding personas before selecting one.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(
    title = "ListCodingAgents",
    description = "Find the coding agents available to the current user. Call this before delegating coding work. Choose an agent using its name, description, instructions, runtime, and model, preferring the user's requested agent or the persona best suited to the repository and task. Returns only available coding agents. If none are available, explain that the user needs to connect or configure a coding agent."
)]
pub struct ListCodingAgents {}

/// Available coding personas and their task-selection context.
#[derive(Debug, Serialize, JsonSchema)]
pub struct ListCodingAgentsResponse {
    /// Personas the user can currently dispatch.
    pub agents: Vec<CodingAgent>,
}

/// Read the current user's coding sessions that need attention.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(
    title = "ListAgentUpdates",
    description = "List the current user's coding-agent sessions waiting for an answer or stalled for over 20 minutes. The user answers permissions and questions through the cards Macro renders from this tool's result. To send a coding agent instructions, use ManageAgentSession with a prompt. To recall the user's earlier decisions about these agents, search earlier agent conversations with ContentSearch (entity_types agent_sessions) and read them with ReadAgentSession."
)]
pub struct ListAgentUpdates {}

impl ToolAnnotated for ListAgentUpdates {
    const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Read agent updates");
}

#[async_trait]
impl AsyncTool<CodingAgentToolContext> for ListAgentUpdates {
    type Output = AgentUpdates;

    async fn call(
        &self,
        context: ServiceContext<CodingAgentToolContext>,
        request: RequestContext,
    ) -> ToolResult<Self::Output> {
        context
            .service
            .list_updates(request.user_id)
            .await
            .map_err(tool_error)
    }
}

impl ToolAnnotated for ListCodingAgents {
    const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("List coding agents");
}

#[async_trait]
impl AsyncTool<CodingAgentToolContext> for ListCodingAgents {
    type Output = ListCodingAgentsResponse;

    #[tracing::instrument(skip_all, err)]
    async fn call(
        &self,
        context: ServiceContext<CodingAgentToolContext>,
        request: RequestContext,
    ) -> ToolResult<Self::Output> {
        Ok(ListCodingAgentsResponse {
            agents: context
                .service
                .list(request.user_id)
                .await
                .map_err(tool_error)?,
        })
    }
}

/// Start the selected coding agent on one complete task.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(
    title = "DispatchCodingAgent",
    description = "Start a new coding agent session for a task using an agent returned by ListCodingAgents. Pass a self-contained task with the relevant repository, requirements, findings, and acceptance criteria; the coding agent does not inherit this conversation. Returns a live session reference after its first prompt is accepted, not completed code. Dispatch once per task and do not retry automatically after an uncertain failure."
)]
pub struct DispatchCodingAgent {
    /// Agent chosen from the current user's discovery result.
    #[schemars(description = "The id of the best-suited coding agent from ListCodingAgents")]
    pub agent_id: Uuid,
    /// Full task handed to the new session.
    #[schemars(
        description = "Self-contained coding task, including the repository, relevant context, requirements, and desired outcome"
    )]
    pub prompt: String,
}

impl ToolAnnotated for DispatchCodingAgent {
    const ANNOTATIONS: ToolAnnotations =
        ToolAnnotations::destructive("Dispatch coding agent").with_open_world();
}

#[async_trait]
impl AsyncTool<CodingAgentToolContext> for DispatchCodingAgent {
    type Output = DispatchedCodingAgent;

    #[tracing::instrument(skip_all, err)]
    async fn call(
        &self,
        context: ServiceContext<CodingAgentToolContext>,
        request: RequestContext,
    ) -> ToolResult<Self::Output> {
        context
            .service
            .dispatch(DispatchCodingAgentRequest {
                user_id: request.user_id,
                agent_id: self.agent_id,
                prompt: self.prompt.clone(),
            })
            .await
            .map_err(tool_error)
    }
}

fn tool_error(error: CodingAgentError) -> ToolCallError {
    ToolCallError {
        description: error.to_string(),
        internal_error: error.into(),
    }
}

/// Discovery and delegation for non-coding agent hosts.
pub fn coding_agent_toolset() -> AsyncToolCollection<CodingAgentToolContext> {
    AsyncToolCollection::new()
        .add_tool::<super::manage_agent_session::ManageAgentSession, CodingAgentToolContext>()
        .add_tool::<CreateAgentSession, CodingAgentToolContext>()
        .add_tool::<ListCodingAgents, CodingAgentToolContext>()
        .add_tool::<ListAgentUpdates, CodingAgentToolContext>()
        .add_tool::<DispatchCodingAgent, CodingAgentToolContext>()
}

/// Start a new conversation with an existing agent persona.
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[schemars(
    title = "CreateAgentSession",
    description = "Start a new session with a coding or chat agent from ListAgents. Provide the agent botId and a self-contained initial prompt. The new session does not inherit this conversation. Returns its session ID after the first prompt is accepted, not completed work. Use ManageAgentSession to continue an existing session. Create once; do not automatically retry after an uncertain failure."
)]
pub struct CreateAgentSession {
    /// Agent persona botId from ListAgents.
    pub agent_id: Uuid,
    /// Complete initial task and relevant context.
    pub prompt: String,
}
impl ToolAnnotated for CreateAgentSession {
    const ANNOTATIONS: ToolAnnotations =
        ToolAnnotations::destructive("Create agent session").with_open_world();
}
#[async_trait]
impl AsyncTool<CodingAgentToolContext> for CreateAgentSession {
    type Output = DispatchedCodingAgent;
    #[tracing::instrument(skip_all, err)]
    async fn call(
        &self,
        context: ServiceContext<CodingAgentToolContext>,
        request: RequestContext,
    ) -> ToolResult<Self::Output> {
        context
            .service
            .create(DispatchCodingAgentRequest {
                user_id: request.user_id,
                agent_id: self.agent_id,
                prompt: self.prompt.clone(),
            })
            .await
            .map_err(tool_error)
    }
}