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.

9.8 KB
Raw Blame
//! The Claude Code harness, `@agentclientprotocol/claude-agent-acp`.
//!
//! Writes its own keys under `_meta.claudeCode`, read as [`ClaudeCodeMeta`]:
//!
//! - `toolName` - the harness's name for the tool (`Bash`, `Read`, `Write`,
//!   or `mcp__<server>__<tool>` for an MCP tool).
//! - `subagent: true` - on the `Agent` tool call that delegates work.
//! - `parentToolUseId` - on a call the subagent made, naming the `Agent`
//!   call it belongs to. Present on the opening frame; not on every patch.
//! - `toolResponse` - the tool's own result object, richer than the text
//!   blocks `rawOutput` later carries. For `Agent` it is an
//!   [`AgentResponse`]: the subagent's answer, id, model, timings, token
//!   count and per-tool statistics. For `AskUserQuestion` it carries
//!   `answers`, the answer the adapter settled on per question.
//!
//! `AskUserQuestion` is asked through `elicitation/create` (adapter ≥ 0.64,
//! gated on the client advertising `elicitation.form`): one form per call,
//! `toolCallId` naming the tool call, and per question a `question_N` select
//! plus a `question_N_custom` free-text companion marked with the shared
//! [`generic::MARKER`]. Custom text wins over the choice when both are sent.

use agent_client_protocol::schema::v1::{ContentBlock, Meta};
use lazy_regex::regex_is_match;
use nom::bytes::complete::{tag, take_until, take_while1};
use nom::character::complete::{char, multispace0};
use nom::multi::many0;
use nom::sequence::{delimited, preceded};
use nom::{IResult, Parser};
use serde::Deserialize;
use serde_json::Value;

use super::{HarnessReader, ToolFrame, generic, has_namespace, namespaced, raw};
use crate::domain::model::{MessagePart, SubagentResult, ToolName, ToolStats, ToolUseId};

/// The `_meta` namespace Claude Code writes under.
pub const NAMESPACE: &str = "claudeCode";

/// What the adapter appends to a question's name for its "Other" companion.
const CUSTOM_ANSWER_SUFFIX: &str = "_custom";

/// Reader for Claude Code's conventions.
pub struct ClaudeCode;

/// `_meta.claudeCode`, as far as the fold reads it.
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ClaudeCodeMeta {
    tool_name: Option<String>,
    #[serde(default)]
    subagent: bool,
    parent_tool_use_id: Option<String>,
    tool_response: Option<Value>,
    origin: Option<String>,
}

fn meta_of(meta: Option<&Meta>) -> ClaudeCodeMeta {
    namespaced(meta, NAMESPACE).unwrap_or_default()
}

/// The `Agent` tool's `toolResponse`.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AgentResponse {
    status: Option<String>,
    error: Option<String>,
    #[serde(default)]
    content: Vec<ContentBlock>,
    agent_id: Option<String>,
    resolved_model: Option<String>,
    total_duration_ms: Option<u32>,
    total_tokens: Option<u32>,
    total_tool_use_count: Option<u32>,
    tool_stats: Option<AgentToolStats>,
}

/// The `Agent` tool's `toolStats`, in Claude Code's names.
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
struct AgentToolStats {
    #[serde(default)]
    read_count: u32,
    #[serde(default)]
    search_count: u32,
    #[serde(default)]
    bash_count: u32,
    #[serde(default)]
    edit_file_count: u32,
    #[serde(default)]
    lines_added: u32,
    #[serde(default)]
    lines_removed: u32,
    #[serde(default)]
    other_tool_count: u32,
}

impl From<AgentToolStats> for ToolStats {
    fn from(stats: AgentToolStats) -> Self {
        Self {
            reads: stats.read_count,
            searches: stats.search_count,
            commands: stats.bash_count,
            edits: stats.edit_file_count,
            lines_added: stats.lines_added,
            lines_removed: stats.lines_removed,
            other: stats.other_tool_count,
        }
    }
}

impl From<AgentResponse> for SubagentResult {
    fn from(response: AgentResponse) -> Self {
        let text = response
            .content
            .iter()
            .filter_map(|block| match block {
                ContentBlock::Text(text) => Some(text.text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n");
        let failed = response.status.as_deref() == Some("failed");
        Self {
            text: (!text.is_empty()).then_some(text),
            error: failed.then(|| {
                response
                    .error
                    .unwrap_or_else(|| "subagent failed".to_owned())
            }),
            agent_id: response.agent_id,
            model: response.resolved_model,
            duration_ms: response.total_duration_ms,
            tokens: response.total_tokens,
            tool_uses: response.total_tool_use_count,
            stats: response.tool_stats.map(ToolStats::from),
        }
    }
}

impl HarnessReader for ClaudeCode {
    fn announces(&self, name: &str) -> bool {
        regex_is_match!(r"(?i)claude", name)
    }

    fn wrote(&self, frame: &ToolFrame<'_>) -> bool {
        has_namespace(frame.meta, NAMESPACE)
    }

    fn reported_tool_name(&self, frame: &ToolFrame<'_>) -> Option<ToolName> {
        tool_name(frame.meta).map(|name| name.parse().unwrap_or_else(|never| match never {}))
    }

    fn is_subagent(&self, name: &ToolName, frame: &ToolFrame<'_>) -> bool {
        meta_of(frame.meta).subagent || generic::is_subagent(name, frame)
    }

    fn parent_tool_call(&self, frame: &ToolFrame<'_>) -> Option<ToolUseId> {
        meta_of(frame.meta).parent_tool_use_id.map(ToolUseId)
    }

    fn subagent_result(&self, frame: &ToolFrame<'_>) -> Option<SubagentResult> {
        if let Some(response) = meta_of(frame.meta).tool_response {
            // The response object is what the harness says about the run;
            // one that does not read as an `AgentResponse` still says nothing
            // the generic path would not.
            if let Ok(response) = serde_json::from_value::<AgentResponse>(response) {
                return Some(response.into());
            }
        }
        // The final frame copies the answer into `rawOutput` as content
        // blocks, the second of which is an "agentId: ... <usage>" boilerplate
        // the response object already said better. Keep the first.
        let first_text = raw::<Vec<ContentBlock>>(frame.raw_output).and_then(|blocks| {
            blocks.into_iter().find_map(|block| match block {
                ContentBlock::Text(text) => Some(text.text),
                _ => None,
            })
        });
        match first_text {
            Some(text) => Some(SubagentResult {
                text: Some(text),
                ..SubagentResult::default()
            }),
            None => generic::subagent_result(frame),
        }
    }

    fn custom_answer_for(&self, name: &str, property: &Value) -> Option<String> {
        generic::custom_answer_for(name, property)
            .or_else(|| generic::custom_answer_by_suffix(name, property, CUSTOM_ANSWER_SUFFIX))
    }

    /// `_meta.claudeCode.toolResponse.answers`: an object keyed by question
    /// text, holding the answer the adapter took for each.
    fn reported_elicitation_answer(&self, frame: &ToolFrame<'_>) -> Option<Value> {
        meta_of(frame.meta).tool_response?.get("answers").cloned()
    }
}

/// The harness's own name for the tool behind a `tool_call`, verbatim.
///
/// Reads `_meta.claudeCode.toolName`.
#[must_use]
pub fn tool_name(meta: Option<&Meta>) -> Option<String> {
    meta_of(meta).tool_name
}

/// Whether a user chunk is a task report the CLI itself injected. Only the
/// `_meta.claudeCode.origin` tag `claude_fold` copies from the transcript
/// marks one, never its text: a person can type the same XML.
#[must_use]
pub fn is_task_notification(meta: Option<&Meta>) -> bool {
    meta_of(meta).origin.as_deref() == Some("task-notification")
}

/// The reports in a [`is_task_notification`] chunk, one part per
/// `<task-notification>` block, and never none.
#[must_use]
pub fn task_notifications(text: &str) -> Vec<MessagePart> {
    let blocks = many0(preceded(
        take_until(TASK_NOTIFICATION_OPEN),
        task_notification,
    ))
    .parse(text)
    .map(|(_, blocks)| blocks)
    .unwrap_or_default();
    // Tagged but unparseable is still the CLI's: show that it reported.
    if blocks.is_empty() {
        return vec![MessagePart::TaskNotification {
            status: None,
            summary: String::new(),
            event: None,
        }];
    }
    blocks
        .into_iter()
        .map(|fields| {
            let field = |name: &str| {
                fields
                    .iter()
                    .find(|(field, _)| *field == name)
                    .map(|(_, value)| html_escape::decode_html_entities(value.trim()).into_owned())
            };
            MessagePart::TaskNotification {
                status: field("status").and_then(|status| status.parse().ok()),
                summary: field("summary").unwrap_or_default(),
                event: field("event"),
            }
        })
        .collect()
}

const TASK_NOTIFICATION_OPEN: &str = "<task-notification>";

/// One `<task-notification>` block, as its `<name>value</name>` fields.
fn task_notification(input: &str) -> IResult<&str, Vec<(&str, &str)>> {
    delimited(
        tag(TASK_NOTIFICATION_OPEN),
        many0(preceded(multispace0, field)),
        (multispace0, tag("</task-notification>")),
    )
    .parse(input)
}

/// `<name>value</name>`, the value raw: a subagent's `<result>` is prose
/// that may hold markup of its own.
fn field(input: &str) -> IResult<&str, (&str, &str)> {
    let (input, name) = delimited(
        char('<'),
        take_while1(|c: char| c.is_ascii_alphanumeric() || c == '-'),
        char('>'),
    )
    .parse(input)?;
    let close = format!("</{name}>");
    let (input, value) = take_until(close.as_str()).parse(input)?;
    let (input, _) = tag(close.as_str()).parse(input)?;
    Ok((input, (name, value)))
}