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.

16.7 KB
Raw Blame
//! Tool calls: opening a part for one and patching it as updates arrive.

use std::path::PathBuf;

use crate::domain::error::FoldError;
use crate::domain::harness::{
    self, HarnessReader, ToolFrame, ToolShape, command_from_raw_input, file_edit_from_raw_input,
};
use crate::domain::model::{
    AnsiText, FileDiff, MessagePart, ToolDetail, ToolName, ToolUseId, UserToolOutcome,
};
use agent_client_protocol::schema::v1::{
    ToolCall, ToolCallContent, ToolCallLocation, ToolCallUpdate, ToolKind,
};

use super::convert::tool_kind_name;
use super::state::{Changed, FoldState, ToolPath};
use super::subagent::{patch_subagent_detail, subagent_detail};

impl FoldState {
    /// Handle a `tool_call`: add a new tool part.
    pub(super) fn open_tool_call(&mut self, call: ToolCall) -> Option<Changed> {
        let id = ToolUseId(call.tool_call_id.0.to_string());
        let reader = self.reader();
        let frame = ToolFrame::of_call(&call);

        // A question already asked on this call's behalf - its
        // `elicitation/create` is a request the agent sends directly, and can
        // overtake the `tool_call` notification on the wire - takes the call
        // in now, the way an open call is absorbed when the question follows
        // it. Either order shows one row.
        if let Some(changed) = self.absorb_late_tool_call(&id, &frame) {
            return Some(changed);
        }

        let name = harness::tool_name(reader, &frame);

        // Whose shape the call is in is decided here, once; a patch finds the
        // detail it opened with and reads into that.
        let detail = match harness::tool_shape(reader, &name, &frame) {
            ToolShape::Harness => tool_detail(reader, &frame),
            ToolShape::Macro(_) => macro_detail(reader, &frame),
            ToolShape::UserTool(tool) => user_tool_detail(reader, tool, &frame),
            ToolShape::View => view_detail(reader, &frame),
            ToolShape::Subagent => subagent_detail(reader, &name, &frame),
        };
        let tool = MessagePart::ToolUse {
            id: id.clone(),
            name,
            status: call.status.into(),
            detail,
        };

        // A repeated open for the same id patches in place rather than
        // duplicating the row. A subagent's children were pushed by their own
        // frames, which a re-announcement of the parent does not carry, so
        // they are kept - unless the re-announcement carries the child's
        // whole transcript itself, which is then the newer copy.
        if let Some(at) = self.tool_positions.get(&id).cloned() {
            let message = at.message;
            if let Some(existing @ MessagePart::ToolUse { .. }) = self.part_at_mut(&at) {
                let mut tool = tool;
                if let (Some(kept), Some(children)) = (existing.children_mut(), tool.children_mut())
                    && children.is_empty()
                {
                    children.append(kept);
                }
                *existing = tool;
            }
            return Some(Changed::updated(message));
        }

        // A call the harness attributes to a subagent nests under it.
        let (changed, at) = match self.parent_path(reader, &frame, &id) {
            Some(parent) => self.push_child_part(&parent, tool)?,
            None => {
                let (changed, position) = self.push_agent_part(tool)?;
                (
                    changed,
                    ToolPath {
                        message: changed.message,
                        path: vec![position],
                    },
                )
            }
        };
        self.tool_positions.insert(id, at);
        Some(changed)
    }

    /// Handle a `tool_call_update`: patch an existing tool part.
    ///
    /// Only fields the update actually carries are written, since
    /// `ToolCallUpdateFields` is entirely optional and a typical call is
    /// patched several times - the recordings average about four updates per
    /// call.
    pub(super) fn patch_tool_call(&mut self, update: ToolCallUpdate) -> Option<Changed> {
        let id = ToolUseId(update.tool_call_id.0.to_string());
        let reader = self.reader();

        let Some(at) = self.tool_positions.get(&id).cloned() else {
            self.warn(FoldError::PatchBeforeOpen { tool_call: id });
            return None;
        };
        let message = at.message;

        // A call a question absorbed (see `request_elicitation`) is patched
        // as the question: the only thing a later update can add is the
        // harness's own reading of the answer.
        if matches!(self.part_at_mut(&at), Some(MessagePart::Elicitation { .. })) {
            return self.patch_absorbed_elicitation(&at, &ToolFrame::of_update(&update));
        }

        let Some(MessagePart::ToolUse {
            name,
            status,
            detail,
            ..
        }) = self.part_at_mut(&at)
        else {
            return None;
        };

        let frame = ToolFrame::of_update(&update);
        if let Some(new_status) = frame.status {
            *status = new_status;
        }
        // Readers see the status the call is in once this update lands, so
        // "has it finished" is answered the same on a patch that carries a
        // status and on one that follows it.
        let frame = frame.with_status(*status);

        // A harness-supplied name outranks any ACP title, so a title alone
        // only fills a name nothing better has set.
        if let Some(found) = reader.reported_tool_name(&frame) {
            *name = found;
        } else if let Some(title) = frame.title
            && name.is_empty()
        {
            *name = title.parse().unwrap_or_else(|never| match never {});
        }

        match detail {
            ToolDetail::Macro { .. } | ToolDetail::UserTool { .. } => {
                patch_macro_detail(reader, name, detail, &frame);
            }
            ToolDetail::View { .. } => {
                if frame.raw_output.is_some() {
                    *detail = view_detail(reader, &frame);
                }
            }
            ToolDetail::Subagent { .. } => patch_subagent_detail(reader, name, detail, &frame),
            _ => patch_detail(reader, detail, &frame),
        }

        Some(Changed::updated(message))
    }
}

/// Build a [`ToolDetail`] from a tool call's opening frame.
pub(super) fn tool_detail(reader: &dyn HarnessReader, frame: &ToolFrame<'_>) -> ToolDetail {
    let content = frame.content.unwrap_or_default();
    let locations = frame.locations.unwrap_or_default();
    match frame.kind.unwrap_or_default() {
        ToolKind::Execute => ToolDetail::Terminal {
            command: command_from_raw_input(frame.raw_input),
            output: reader.terminal_output(frame).map(AnsiText),
            exit_code: reader.terminal_exit_code(frame),
        },
        ToolKind::Edit => ToolDetail::Edit {
            diffs: edit_diffs(content, frame.raw_input),
        },
        ToolKind::Read => ToolDetail::Read {
            paths: location_paths(locations),
        },
        ToolKind::Delete => ToolDetail::Delete {
            paths: location_paths(locations),
        },
        ToolKind::Move => ToolDetail::Move {
            paths: location_paths(locations),
        },
        ToolKind::Search => ToolDetail::Search {
            paths: location_paths(locations),
            output: frame.content_text(),
        },
        ToolKind::Fetch => ToolDetail::Fetch {
            output: frame.content_text(),
        },
        ToolKind::Think => ToolDetail::Think {
            output: frame.content_text(),
        },
        other => {
            let (result, error) = tool_output(reader, frame);
            ToolDetail::Other {
                kind: tool_kind_name(other).to_owned(),
                output: frame.content_text(),
                input: reader.tool_input(frame),
                result,
                error,
            }
        }
    }
}

/// A frame's `rawOutput` as the tool's own result and the wrapper's error,
/// through the harness's reading. `(None, None)` when the frame carries no
/// output; a result the wrapper reports as empty is `None` too, so a reader
/// never shows a `null` response for a call that reported nothing.
fn tool_output(
    reader: &dyn HarnessReader,
    frame: &ToolFrame<'_>,
) -> (Option<serde_json::Value>, Option<String>) {
    match frame.raw_output.map(|raw| reader.unwrap_tool_output(raw)) {
        None => (None, None),
        Some((serde_json::Value::Null, error)) => (None, error),
        Some((value, error)) => (Some(value), error),
    }
}

/// Build the detail for a Macro tool from its opening frame: its input as
/// given, its output out of the harness's wrapper.
pub(super) fn macro_detail(reader: &dyn HarnessReader, frame: &ToolFrame<'_>) -> ToolDetail {
    let (output, error) = match frame.raw_output.map(|raw| reader.unwrap_tool_output(raw)) {
        None => (None, None),
        Some((value, error)) => (Some(value), error),
    };
    ToolDetail::Macro {
        input: reader.tool_input(frame).unwrap_or(serde_json::Value::Null),
        output,
        error,
    }
}

/// Build the detail for a view tool from a frame: the view its result
/// carries, decoded. A view tool's arguments are never read: they are the
/// model's, streamed undecoded, and the server decodes the view into the
/// result it returns.
pub(super) fn view_detail(reader: &dyn HarnessReader, frame: &ToolFrame<'_>) -> ToolDetail {
    /// What a view tool returns.
    #[derive(serde::Deserialize)]
    struct ViewResult {
        view: dynamic_ui::View,
    }

    let Some(raw) = frame.raw_output else {
        return ToolDetail::View {
            view: None,
            error: None,
        };
    };
    let (value, error) = reader.unwrap_tool_output(raw);
    ToolDetail::View {
        view: serde_json::from_value::<ViewResult>(value)
            .ok()
            .map(|result| result.view),
        error,
    }
}

/// Build the detail for a Macro user tool from its opening frame.
///
/// A user tool starts [`UserToolOutcome::Pending`] whether or not the frame
/// carried output: the backend's `"PendingUserExecution"` reads as pending
/// too, and a frame with nothing yet is a call still being made.
pub(super) fn user_tool_detail(
    reader: &dyn HarnessReader,
    tool: &str,
    frame: &ToolFrame<'_>,
) -> ToolDetail {
    ToolDetail::UserTool {
        input: reader.tool_input(frame).unwrap_or(serde_json::Value::Null),
        outcome: frame.raw_output.map_or(UserToolOutcome::Pending, |raw| {
            harness::user_tool_outcome(reader, tool, raw)
        }),
    }
}

/// Write an update's raw input and output into a Macro tool's detail.
///
/// Both replace rather than merge: a harness sends the arguments whole each
/// time, and so does the session API that later records what the user did
/// with a user tool.
pub(super) fn patch_macro_detail(
    reader: &dyn HarnessReader,
    name: &ToolName,
    detail: &mut ToolDetail,
    frame: &ToolFrame<'_>,
) {
    match detail {
        ToolDetail::Macro {
            input,
            output,
            error,
        } => {
            if let Some(found) = reader.tool_input(frame) {
                *input = found;
            }
            if let Some(raw) = frame.raw_output {
                let (value, failure) = reader.unwrap_tool_output(raw);
                *output = Some(value);
                *error = failure;
            }
        }
        ToolDetail::UserTool { input, outcome } => {
            if let Some(found) = reader.tool_input(frame) {
                *input = found;
            }
            if let Some(raw) = frame.raw_output {
                // The detail says this is a user tool, so the name's short
                // form is the tool's: `SendEmail` whether the harness wrote it
                // bare or as `mcp__macro__SendEmail`.
                *outcome = harness::user_tool_outcome(reader, name.display(), raw);
            }
        }
        _ => {}
    }
}

/// Write an update's fields into an existing detail, leaving what it does not
/// carry untouched.
pub(super) fn patch_detail(
    reader: &dyn HarnessReader,
    detail: &mut ToolDetail,
    frame: &ToolFrame<'_>,
) {
    match detail {
        ToolDetail::Terminal {
            command,
            output,
            exit_code,
        } => {
            if let Some(found) = command_from_raw_input(frame.raw_input) {
                *command = Some(found);
            }
            // Each update carries the output accumulated so far, so replace.
            if let Some(found) = reader.terminal_output(frame) {
                *output = Some(AnsiText(found));
            }
            if let Some(found) = reader.terminal_exit_code(frame) {
                *exit_code = Some(found);
            }
        }
        ToolDetail::Edit { diffs: existing } => {
            if let Some(content) = frame.content {
                let found = diffs(content);
                if !found.is_empty() {
                    *existing = found;
                }
            }
            // A call that never reports a diff block (Claude Code's `Write`)
            // may still deliver its raw input on a later update.
            if existing.is_empty()
                && let Some(found) = synthesized_edit_diff(frame.raw_input)
            {
                *existing = vec![found];
            }
        }
        ToolDetail::Read { paths } | ToolDetail::Delete { paths } | ToolDetail::Move { paths } => {
            if let Some(found) = frame.locations.map(location_paths)
                && !found.is_empty()
            {
                *paths = found;
            }
        }
        ToolDetail::Search { paths, output } => {
            if let Some(found) = frame.locations.map(location_paths)
                && !found.is_empty()
            {
                *paths = found;
            }
            if let Some(found) = frame.content_text() {
                *output = Some(found);
            }
        }
        ToolDetail::Fetch { output } | ToolDetail::Think { output } => {
            if let Some(found) = frame.content_text() {
                *output = Some(found);
            }
        }
        ToolDetail::Other {
            input,
            output,
            result,
            error,
            ..
        } => {
            if let Some(found) = reader.tool_input(frame) {
                *input = Some(found);
            }
            if let Some(found) = frame.content_text() {
                *output = Some(found);
            }
            // A harness sends the result whole each time, so both replace:
            // an update that carries the outcome carries all of it.
            if frame.raw_output.is_some() {
                let (found, failure) = tool_output(reader, frame);
                *result = found;
                *error = failure;
            }
        }
        // Patched by `patch_macro_detail` / `patch_subagent_detail`, which
        // have the name and the harness's result shapes to hand.
        ToolDetail::Macro { .. }
        | ToolDetail::View { .. }
        | ToolDetail::UserTool { .. }
        | ToolDetail::Subagent { .. } => {}
    }
}

/// An edit call's diffs: the reported diff blocks, or — for calls that never
/// report one, like Claude Code's `Write` — a whole-file diff synthesized
/// from the raw input.
pub(super) fn edit_diffs(
    content: &[ToolCallContent],
    raw_input: Option<&serde_json::Value>,
) -> Vec<FileDiff> {
    let found = diffs(content);
    if !found.is_empty() {
        return found;
    }
    synthesized_edit_diff(raw_input).into_iter().collect()
}

/// A whole-file diff from `{filePath, content}` raw input. The prior contents
/// are not on the wire, so the file reads as new.
pub(super) fn synthesized_edit_diff(raw_input: Option<&serde_json::Value>) -> Option<FileDiff> {
    let (path, content) = file_edit_from_raw_input(raw_input)?;
    Some(FileDiff {
        path: path.into(),
        old_text: None,
        new_text: content,
    })
}

/// The diffs among a tool call's content blocks.
pub(super) fn diffs(content: &[ToolCallContent]) -> Vec<FileDiff> {
    content
        .iter()
        .filter_map(|block| match block {
            ToolCallContent::Diff(diff) => Some(FileDiff {
                path: diff.path.clone(),
                old_text: diff.old_text.clone(),
                new_text: diff.new_text.clone(),
            }),
            _ => None,
        })
        .collect()
}

/// The paths among a tool call's reported locations.
///
/// The one source this fold trusts for "what path did this call touch" -
/// `locations` is ACP's own field, meant for exactly this, unlike `rawInput`,
/// whose keys are a harness's own convention with no fixed shape to read.
pub(super) fn location_paths(locations: &[ToolCallLocation]) -> Vec<PathBuf> {
    locations
        .iter()
        .map(|location| location.path.clone())
        .collect()
}