All files / src/components/tools GenericToolCard.tsx

12.82% Statements 5/39
4% Branches 2/50
20% Functions 1/5
13.15% Lines 5/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                10x 10x 6x   4x                                                                           2x                                                                                                                                                                
import { useState, type JSX } from "react";
import { ChevronRight, Cog } from "lucide-react";
import type { ToolCardProps } from "./ToolCardProps.js";
import { ICON_SM, ICON_MD } from "../../utils/iconSize.js";
import styles from "./toolCards.module.scss";
 
/** Formats an MCP tool name for display: `mcp__server__tool` → `server / tool`. */
export function formatToolName(tool: string): { display: string; isMcp: boolean } {
  const mcpMatch = /^mcp__([^_]+(?:_[^_]+)*)__(.+)$/.exec(tool);
  if (mcpMatch) {
    return { display: `${mcpMatch[1]} / ${mcpMatch[2]}`, isMcp: true };
  }
  return { display: tool, isMcp: false };
}
 
/** Extracts a one-line human-readable summary of tool arguments. */
function argsPreview(args: unknown): string {
  if (args === null || args === undefined) {
    return "";
  }
  if (typeof args !== "object") {
    return String(args);
  }
  const a = args as Record<string, unknown>;
  // Common patterns
  if (typeof a.command === "string") {
    return a.command;
  }
  if (typeof a.file_path === "string") {
    return a.file_path;
  }
  if (typeof a.path === "string") {
    return a.path;
  }
  if (typeof a.query === "string") {
    return a.query;
  }
  if (typeof a.url === "string") {
    return a.url;
  }
  // Fallback
  try {
    const json = JSON.stringify(args);
    return json.length > 120 ? `${json.slice(0, 120)}\u2026` : json;
  } catch {
    return "";
  }
}
 
/** Number of result lines shown when collapsed. */
const PREVIEW_LINES: number = 5;
 
/** Renders a generic/unknown tool call with formatted args and result. */
export function GenericToolCard({ tool, args, result, isError }: ToolCardProps): JSX.Element {
  const [expanded, setExpanded] = useState(false);
  const { display } = formatToolName(tool);
  const preview = argsPreview(args);
  const inProgress = result === undefined;
 
  const resultLines = result?.split("\n") ?? [];
  const hasMore = resultLines.length > PREVIEW_LINES;
  const displayResult = expanded ? result : resultLines.slice(0, PREVIEW_LINES).join("\n");
 
  // Format args as pretty JSON for expanded view
  let argsFormatted = "";
  if (args !== null && args !== undefined) {
    try {
      argsFormatted = JSON.stringify(args, null, 2);
    } catch {
      argsFormatted = String(args);
    }
  }
 
  return (
    <div
      className={`${styles.card} ${isError ? styles.cardRed : styles.cardBlue} ${inProgress ? styles.inProgress : ""}`}
      data-testid="tool-card-generic"
    >
      <div className={styles.header}>
        <span className={styles.icon}>
          <Cog size={ICON_MD} aria-hidden="true" />
        </span>
        <span className={styles.toolName} style={{ color: "var(--accent-blue)" }}>
          {display}
        </span>
        {preview && <span className={styles.fileName}>{preview}</span>}
      </div>
 
      {/* Show formatted args when no result yet */}
      {inProgress && argsFormatted && (
        <pre className={styles.pre} data-testid="tool-card-args">
          {argsFormatted}
        </pre>
      )}
 
      {isError && result && (
        <pre className={styles.pre} data-testid="tool-card-error">
          {result}
        </pre>
      )}
 
      {!isError && !inProgress && result && (
        <>
          <pre className={styles.pre} data-testid="tool-card-result">
            {displayResult}
          </pre>
          {hasMore && (
            <button
              type="button"
              className={styles.bodyToggle}
              onClick={() => {
                setExpanded((v) => !v);
              }}
              aria-expanded={expanded}
              data-testid="tool-card-toggle"
            >
              <span
                className={`${styles.chevron} ${expanded ? styles.chevronExpanded : ""}`}
                aria-hidden="true"
              >
                <ChevronRight size={ICON_SM} />
              </span>
              {expanded ? "collapse" : `${resultLines.length - PREVIEW_LINES} more lines`}
            </button>
          )}
        </>
      )}
    </div>
  );
}