# All files / src/components/tools AgentToolCard.tsx

100% Statements 22/22
98.5% Branches 66/67
100% Functions 6/6
100% Lines 20/20

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
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219

    55x

    16x
    16x
    4x

    12x
    12x

    55x

    182x
    182x
    182x
    182x

    182x
    182x

    182x
    182x
    182x

    182x

    182x

    18x

    22x

     import { useState, type JSX } from "react";
    import { Link } from "react-router";
    import { parseDelegationArgs } from "@grackle-ai/common";
    import { sessionUrl } from "../../utils/navigation.js";
    import type { ToolCardProps } from "./ToolCardProps.js";
    import styles from "./toolCards.module.scss";
    import agentStyles from "./AgentToolCard.module.scss";

    /** Regex to parse Copilot read_agent structured result prefix. */
    const READ_AGENT_STATUS_PATTERN: RegExp =
      /^Agent\s+(completed|running|failed|error)\.\s*agent_id:\s*(\S+),?\s*([^\n]*)(?:\n\n([\s\S]*))?$/i;

    /** Parsed result from a Copilot read_agent poll. */
    interface ReadAgentResult {
      /** Agent lifecycle status. */
      status: string;
      /** The agent_id that was polled. */
      agentId: string;
      /** Metadata line (e.g. "elapsed: 6s, total_turns: 0, duration: 4s"). */
      metadata: string;
      /** The actual content after the status prefix. */
      content?: string;
    }

    /** Attempts to parse the structured prefix from a read_agent result. */
    function parseReadAgentResult(result: string): ReadAgentResult | undefined {
      const match = READ_AGENT_STATUS_PATTERN.exec(result);
      if (!match) {
        return undefined;
      }
      const rawContent: string | undefined = match[4] as string | undefined;
      return {
        status: match[1].toLowerCase(),
        agentId: match[2].replace(/,$/, ""),
        metadata: match[3].trim(),
        content: rawContent ? rawContent.trim() : undefined,
      };
    }

    /** Number of result lines shown when collapsed. */
    const PREVIEW_LINES: number = 5;

    /** Renders a subagent tool call (Claude Code Agent, Copilot task/read_agent). */
    export function AgentToolCard({
      tool,
      args,
      result,
      isError,
      childSessionId,
    }: ToolCardProps): JSX.Element {
      const [expanded, setExpanded] = useState(false);
      const [promptExpanded, setPromptExpanded] = useState(false);
      const info = parseDelegationArgs(tool, args);
      const inProgress = result === undefined;

      // For read_agent, try to parse the structured result
      const parsedPoll = info.isPoll && result ? parseReadAgentResult(result) : undefined;
      const displayResult = parsedPoll?.content ?? result;

      const resultLines = displayResult?.split("\n") ?? [];
      const hasMore = resultLines.length > PREVIEW_LINES;
      const visibleResult = expanded ? displayResult : resultLines.slice(0, PREVIEW_LINES).join("\n");

      // Header label
      const headerLabel = info.isPoll ? "Subagent" : "Agent";

      return (
        <div
          className={`${styles.card} ${isError ? styles.cardRed : styles.cardTeal} ${inProgress ? styles.inProgress : ""}`}
          data-testid="tool-card-agent"
        >
          {/* Header row */}
          <div className={styles.header}>
            <span className={styles.icon} style={{ color: "var(--accent-teal, #2dd4bf)" }}>
              &#9654;
            </span>
            <span className={styles.toolName} style={{ color: "var(--accent-teal, #2dd4bf)" }}>
              {headerLabel}
            </span>

            {info.agentType && (
              <span className={agentStyles.badgePill} data-testid="tool-card-agent-type">
                {info.agentType}
              </span>
            )}

            {info.model && (
              <span className={agentStyles.modelBadge} data-testid="tool-card-agent-model">
                {info.model}
              </span>
            )}

            {info.isBackground && (
              <span className={agentStyles.backgroundBadge} data-testid="tool-card-agent-background">
                <span
                  className={inProgress ? agentStyles.backgroundDotPulsing : agentStyles.backgroundDot}
                >
                  &#9679;
                </span>
                BG
              </span>
            )}

            {info.agentName && (
              <span className={styles.fileName} data-testid="tool-card-agent-name">
                {info.agentName}
              </span>
            )}

            {info.agentId && (
              <span className={styles.fileName} data-testid="tool-card-agent-id">
                {info.agentId}
              </span>
            )}

            <span className={styles.spacer} />

            {childSessionId && (
              <Link
                to={sessionUrl(childSessionId)}
                className={agentStyles.viewActivity}
                data-testid="tool-card-agent-view-activity"
              >
                View activity &#8594;
              </Link>
            )}

            {inProgress && !info.isBackground && (
              <span className={styles.exitPending} data-testid="tool-card-pending">
                &#9679;
              </span>
            )}
          </div>

          {/* Description */}
          {info.description && (
            <div className={agentStyles.description} data-testid="tool-card-agent-description">
              {info.isResume ? `Resuming: ${info.description}` : info.description}
            </div>
          )}

          {/* read_agent status line */}
          {parsedPoll && (
            <div className={agentStyles.statusLine} data-testid="tool-card-agent-status">
              <span
                className={
                  parsedPoll.status === "completed"
                    ? agentStyles.statusCompleted
                    : parsedPoll.status === "running"
                      ? agentStyles.statusRunning
                      : agentStyles.statusError
                }
              >
                {parsedPoll.status}
              </span>
              {parsedPoll.metadata && <span>{parsedPoll.metadata}</span>}
            </div>
          )}

          {/* Collapsible prompt */}
          {info.prompt && (
            <>
              <button
                type="button"
                className={agentStyles.promptToggle}
                onClick={() => {
                  setPromptExpanded((v) => !v);
                }}
                aria-expanded={promptExpanded}
                data-testid="tool-card-prompt-toggle"
              >
                <span className={`${styles.chevron} ${promptExpanded ? styles.chevronExpanded : ""}`}>
                  &#9656;
                </span>
                prompt
              </button>
              {promptExpanded && (
                <pre className={styles.pre} data-testid="tool-card-prompt">
                  {info.prompt}
                </pre>
              )}
            </>
          )}

          {/* Error result */}
          {isError && result && (
            <pre className={styles.pre} data-testid="tool-card-error">
              {result}
            </pre>
          )}

          {/* Normal result */}
          {!isError && !inProgress && displayResult && (
            <>
              <pre className={styles.pre} data-testid="tool-card-result">
                {visibleResult}
              </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 : ""}`}>
                    &#9656;
                  </span>
                  {expanded ? "collapse" : `${resultLines.length - PREVIEW_LINES} more lines`}
                </button>
              )}
            </>
          )}
        </div>
      );
    }