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.

5.0 KB
Raw Blame
/**
 * Where a message's parts fold for display: consecutive ordinary tool calls
 * and their thinking blocks share a group, everything else stays visible.
 * The group starts with the first call so receiving another call does not
 * replace the first call's host in the middle of its animation.
 * Subagents and user tools stay outside groups so delegated work and user
 * interactions remain visible regardless of the surrounding tool traffic.
 * Tools with their own answer or session view always break the run, including
 * while their arguments are still arriving.
 *
 * A thought at the tail of a run stays out of the group only when the run
 * closes the message: that row is the current (or last) reasoning, and
 * burying it in "Called N tools" is what made a live Cursor turn look
 * finished. Mid-message, a trailing thought belongs to its run — prose
 * follows it, so nothing live is being hidden.
 *
 * Segments are half-open index ranges into the parts array, so a grouped call
 * keeps its position for the tool render context.
 */

import type { MessagePart } from '@service-agent-fold/generated/types';

export type PartSegment = {
  kind: 'part' | 'tools';
  start: number;
  /** Exclusive. A `part` segment spans exactly one index. */
  end: number;
};

/**
 * Macro tools whose call renders the answer itself rather than a card about
 * it. `GenerateImage` shows the generated
 * image document. `DispatchCodingAgent` shows the dispatched session's Magic Chip.
 *
 * Folding one of these into a group would put the answer behind a closed
 * caret, indented inside a row of muted tool chips — so they break the run
 * and stand on their own, like a paragraph of the reply.
 */
const SELF_RENDERING_TOOLS: ReadonlySet<string> = new Set([
  'DispatchCodingAgent',
  'GenerateImage',
]);

/**
 * Whether a part is a tool call that renders its own view (see
 * {@link SELF_RENDERING_TOOLS}). Only Macro's own tools do: the fold names
 * them, and the chat component library is what renders them.
 */
export function rendersOwnView(part: MessagePart | undefined): boolean {
  if (part?.kind !== 'tool_use') return false;
  if (
    isCodeExecution(part) ||
    presentedReviewId(part) ||
    part.detail.kind === 'view'
  )
    return true;
  const macroTool =
    part.detail.kind === 'macro' ||
    (part.detail.kind === 'other' &&
      part.name.kind === 'mcp' &&
      part.name.server === 'macro');
  if (!macroTool) return false;
  // The tool's own name, without the MCP server namespace the fold already
  // separated out (mirrors `toolLabel` in `component/parts/shared.ts`).
  const name = part.name.kind === 'mcp' ? part.name.tool : part.name.name;
  return SELF_RENDERING_TOOLS.has(name);
}

/** Whether a part may be folded into a collapsed run with its neighbours. */
function isGroupable(part: MessagePart | undefined): boolean {
  return (
    part?.kind === 'thought' ||
    (part?.kind === 'tool_use' &&
      part.detail.kind !== 'subagent' &&
      part.detail.kind !== 'user_tool' &&
      !rendersOwnView(part))
  );
}

export function segmentParts(parts: readonly MessagePart[]): PartSegment[] {
  const segments: PartSegment[] = [];
  let start = 0;
  while (start < parts.length) {
    if (!isGroupable(parts[start])) {
      segments.push({ kind: 'part', start, end: start + 1 });
      start += 1;
      continue;
    }

    let end = start;
    let lastCallEnd = start;
    while (isGroupable(parts[end])) {
      if (parts[end].kind === 'tool_use') lastCallEnd = end + 1;
      end += 1;
    }
    if (lastCallEnd > start) {
      const groupEnd = end === parts.length ? lastCallEnd : end;
      segments.push({ kind: 'tools', start, end: groupEnd });
      start = groupEnd;
    }
    // At the message tail, every thought after the last call remains visible.
    // Thought-only runs never become groups: a group needs an actual call.
    while (start < end) {
      segments.push({ kind: 'part', start, end: start + 1 });
      start += 1;
    }
  }
  return segments;
}

/** Only Macro's session-authenticated internal server may select this view. */
export function isCodeExecution(part: MessagePart): boolean {
  return (
    part.kind === 'tool_use' &&
    part.name.kind === 'mcp' &&
    part.name.server === 'macro_internal' &&
    part.name.tool === 'ExecuteCode'
  );
}

/** Only the server-authored presentation call selects a persisted review card.
 * The normal PresentReview invocation still renders its ordinary receipt.
 */
export function presentedReviewId(part: MessagePart): string | undefined {
  if (
    part.kind !== 'tool_use' ||
    part.name.kind !== 'mcp' ||
    part.name.server !== 'macro_internal' ||
    part.name.tool !== 'PresentReview' ||
    !('input' in part.detail)
  )
    return;
  const input = part.detail.input;
  if (
    !input ||
    typeof input !== 'object' ||
    !('reviewId' in input) ||
    typeof input.reviewId !== 'string' ||
    !/^[\da-f]{8}(?:-[\da-f]{4}){3}-[\da-f]{12}$/i.test(input.reviewId)
  )
    return;
  return part.id === `review_${input.reviewId}` ? input.reviewId : undefined;
}