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.

3.3 KB
Raw Blame
/**
 * Shared vocabulary for the per-part components, mirroring the chat block's
 * tool-handler split (`@core/component/AI/component/tool/handler.tsx`): each
 * detail kind renders in its own file, and the dispatcher passes the
 * row-level facts every card shares.
 */

import type {
  MessagePart,
  ToolName,
} from '@service-agent-fold/generated/types';
import type { ToolName as MacroToolName } from '@service-cognition/generated/tools/tool';
import type { JSX } from 'solid-js';
import { match } from 'ts-pattern';
import type { ToolStatus } from '../../ui';

export type ToolUsePart = Extract<MessagePart, { kind: 'tool_use' }>;

/**
 * Where a part sits in its transcript, for the chat components a Macro tool
 * renders with: they key per-call state by chat, message, and part, and the
 * agent block's equivalents are the session, the turn's message, and the
 * part's index.
 */
export type ToolCallContext = {
  /** The agent session id, standing in for the chat block's chat id. */
  sessionId: string;
  /** A stable per-turn id, standing in for the chat block's message id. */
  messageId: string;
  /** The part's index within its message. */
  partIndex: number;
  /** The turn is still in flight — the chat block's `isStreaming`. */
  inFlight: boolean;
  /** Whether a later part of the same turn calls this tool. */
  followedBy: (name: MacroToolName) => boolean;
};

/** Row-level facts common to every tool card, derived once by the dispatcher. */
export type ToolCallCommon = {
  /** The ACP tool call id. */
  id: string;
  label: string;
  status: ToolStatus;
  /** The chat block's failed treatment: faded row, quiet trailing label. */
  muted: boolean;
  /** The turn ended while the call was still running (see `settledToolStatus`). */
  stopped: boolean;
  trailing: JSX.Element | undefined;
};

/**
 * The short name to show for a tool: its own name, without the MCP server
 * namespace the fold already separated out.
 */
export function toolLabel(name: ToolName): string {
  return name.kind === 'mcp' ? name.tool : name.name;
}

/** Whether a part after `index` calls the tool named `name`. */
export function toolUsedAfter(
  parts: readonly MessagePart[],
  index: number
): (name: MacroToolName) => boolean {
  return (name) =>
    parts
      .slice(index + 1)
      .some(
        (part) => part.kind === 'tool_use' && toolLabel(part.name) === name
      );
}

/** Subtitle for a call that touched paths: the path, or how many. */
export function pathsSubtitle(paths: string[]): string | undefined {
  if (paths.length === 0) return undefined;
  if (paths.length === 1) return paths[0];
  return `${paths.length} files`;
}

/**
 * What a call touched, in a word — the subtitle its card would show, for the
 * one-line summary a collapsed group gives its latest call.
 */
export function toolCallDetail(part: ToolUsePart): string | undefined {
  return match(part.detail)
    .with({ kind: 'terminal' }, (detail) => detail.command ?? undefined)
    .with({ kind: 'edit' }, (detail) =>
      pathsSubtitle(detail.diffs.map((diff) => diff.path))
    )
    .with(
      { kind: 'read' },
      { kind: 'delete' },
      { kind: 'move' },
      { kind: 'search' },
      (detail) => pathsSubtitle(detail.paths)
    )
    .with({ kind: 'subagent' }, (detail) => detail.title)
    .otherwise(() => undefined);
}