# All files / src/components/display EventRenderer.tsx

69.15% Statements 74/107
67.6% Branches 48/71
70% Functions 14/20
69.52% Lines 73/105

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
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485

    28x
    6x

    65x
    65x

    28x

    1x
    1x
    1x
    1x

    1x

    240x
    120x

    120x

    120x
    24x

    96x
    96x

    8x

    8x
    8x

    398x

    9x
    9x

    9x

    398x

    286x

    78x

    78x

    112x

    9x

    654x

    654x

    18x

    18x
    18x
    18x

    18x

    18x
    4x

    4x

    4x

    14x
    3x

    11x

    1x
    1x
    1x
    1x
    1x

    286x

    12x
    12x
    12x
    12x
    12x
    12x

    12x

    69x
    69x
    69x
    69x

    69x
    27x
    27x
    27x

    69x
    69x

    69x
    53x

    16x

    78x

    78x

    112x

     import {
      type ReactNode,
      useState,
      lazy,
      Suspense,
      type LazyExoticComponent,
      type ComponentType,
      type JSX,
    } from "react";
    import { ChevronDown, ChevronRight } from "lucide-react";
    import Markdown, { type Components, type ExtraProps } from "react-markdown";
    import rehypePrismPlus from "rehype-prism-plus/common";
    import remarkGfm from "remark-gfm";
    import type { SessionEvent } from "../../hooks/types.js";
    import { detectDelegation, delegationIdentityKey, deriveChildSessionId } from "@grackle-ai/common";
    import { formatTokens, formatCost } from "../../utils/format.js";
    import { ICON_SM } from "../../utils/iconSize.js";
    import { ToolCard } from "../tools/ToolCard.js";
    import type { McpAppWidgetProps } from "./McpAppWidget.js";
    import type { McpUiResourceCsp } from "@modelcontextprotocol/ext-apps/app-bridge";
    import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
    import { CopyButton } from "./CopyButton.js";
    import styles from "./EventRenderer.module.scss";

    // Lazy-loaded (and intentionally NOT re-exported from the package barrel) so the
    // heavy ext-apps AppBridge is code-split into an async chunk loaded only when a
    // widget actually renders — keeps the main chat bundle under the chunk-size cap.
    const McpAppWidget: LazyExoticComponent<ComponentType<McpAppWidgetProps>> = lazy(() =>
      import("./McpAppWidget.js").then((m) => ({ default: m.McpAppWidget })),
    );

    /** Props for the EventRenderer component. */
    interface Props {
      event: SessionEvent;
      /** Paired tool_use context, attached by SessionPanel when raw IDs match. */
      toolUseCtx?: { tool: string; args: unknown; detailedResult?: string };
      /** True when a tool_use completed but has no tool_result (e.g. Claude Code text-result pattern). */
      settled?: boolean;
      /** Sandbox proxy origin URL for rendering MCP Apps widget events (different origin than the app). */
      sandboxProxyUrl?: string;
      /** Open a file in the live-docs pane (#1396) — makes `file://` links and tool file paths clickable. */
      onOpenDocument?: (uri: string) => void;
    }

    /**
     * Compute the materialized child session id for a delegation tool call, using
     * the same shared derivation the server uses to create the child (#1075), so the
     * agent card can link to the subagent's activity view. Returns undefined for
     * non-delegation tools or when the parent session / tool-call id is unknown.
     */
    function delegationChildId(
      parentSessionId: string,
      toolCallId: string | undefined,
      tool: string,
      args: unknown,
    ): string | undefined {
      if (!parentSessionId || !toolCallId) {
        return undefined;
      }
      const info = detectDelegation(tool, args);
      Iif (!info) {
        return undefined;
      }
      return deriveChildSessionId(parentSessionId, delegationIdentityKey(info, toolCallId));
    }

    // --- Individual event type renderers ---

    /** Number of lines shown in the collapsed system context preview. */
    const SYSTEM_CONTEXT_PREVIEW_LINES: number = 3;

    /** Renders the system context (system prompt) as a collapsible left-bordered section. */
    function SystemContextEvent({ content }: { content: string }): JSX.Element {
      const [expanded, setExpanded] = useState(false);
      const lines = content.split("\n");
      const hasMore = lines.length > SYSTEM_CONTEXT_PREVIEW_LINES;
      const displayContent = expanded
        ? content
        : lines.slice(0, SYSTEM_CONTEXT_PREVIEW_LINES).join("\n");

      return (
        <div className={styles.systemContextEvent} data-testid="system-context-event">
          <button
            type="button"
            className={styles.systemContextHeader}
            onClick={() => {
              setExpanded((v) => !v);
            }}
            aria-expanded={expanded}
          >
            <span className={styles.systemContextBadge}>SYSTEM PROMPT</span>
            {hasMore && (
              <span className={styles.systemContextToggle} aria-hidden="true">
                {expanded ? <ChevronDown size={ICON_SM} /> : <ChevronRight size={ICON_SM} />}
              </span>
            )}
          </button>
          <pre className={styles.systemContextPre}>
            {displayContent}
            {!expanded && hasMore && <span className={styles.systemContextEllipsis}>{"\u2026"}</span>}
          </pre>
        </div>
      );
    }

    /** Renders a system-level event with timestamp. */
    function SystemEvent({ time, content }: { time: string; content: string }): JSX.Element {
      return (
        <div className={styles.systemEvent}>
          <span className={styles.systemTimestamp}>[{time}]</span> {content}
        </div>
      );
    }

    /** Recursively extracts plain text from React children (for code block copy). */
    export function extractText(node: ReactNode): string {
      if (typeof node === "string") {
        return node;
      }
      Iif (typeof node === "number") {
        return String(node);
      }
      if (Array.isArray(node)) {
        return node.map(extractText).join("");
      }
      if (node !== null && node !== undefined && typeof node === "object" && "props" in node) {
        return extractText((node as { props: { children?: ReactNode } }).props.children);
      }
      return "";
    }

    /** Props passed by react-markdown to component overrides. */
    interface PreProps extends React.HTMLAttributes<HTMLPreElement> {
      children?: ReactNode;
      /** AST node injected by react-markdown — must not be spread onto the DOM element. */
      node?: unknown;
    }

    /** Wraps markdown `<pre>` blocks with a CopyButton for code-only copy. */
    function CodeBlockWrapper({ children, node, ...preProps }: PreProps): JSX.Element {
      // node is destructured solely to exclude it from the DOM spread
      Iif (node === undefined) {
        /* intentionally unused */
      }
      const rawText = extractText(children);
      return (
        <div className={styles.codeBlockWrapper}>
          <pre {...preProps}>{children}</pre>
          <CopyButton
            text={rawText}
            data-testid="copy-code-block"
            className={styles.codeBlockCopyButton}
          />
        </div>
      );
    }

    /**
     * Renders a markdown string with GFM support and syntax-highlighted code blocks.
     *
     * Shared by both assistant text events and user input events so the two render
     * through an identical pipeline. When `onOpenDocument` is provided, `file://`
     * links open in the live-docs pane instead of navigating (#1396).
     */
    function MarkdownContent({
      content,
      onOpenDocument,
    }: {
      content: string;
      onOpenDocument?: (uri: string) => void;
    }): JSX.Element {
      const components: Components = {
        pre: CodeBlockWrapper,
        a(props: JSX.IntrinsicElements["a"] & ExtraProps) {
          const { href, children } = props;
          Iif (href && href.startsWith("file://") && onOpenDocument) {
            return (
              <a
                href={href}
                onClick={(e) => {
                  e.preventDefault();
                  onOpenDocument(href);
                }}
              >
                {children}
              </a>
            );
          }
          return (
            <a href={href} target="_blank" rel="noreferrer noopener">
              {children}
            </a>
          );
        },
      };
      return (
        <Markdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypePrismPlus]} components={components}>
          {content}
        </Markdown>
      );
    }

    /** Renders an assistant text output event with markdown formatting. */
    function TextEvent({
      content,
      onOpenDocument,
    }: {
      content: string;
      onOpenDocument?: (uri: string) => void;
    }): JSX.Element {
      return (
        <div className={styles.textEvent}>
          <MarkdownContent content={content} onOpenDocument={onOpenDocument} />
        </div>
      );
    }

    // ToolUseEvent and ToolResultEvent have been replaced by the ToolCard component
    // in packages/web/src/components/tools/. See ToolCard.tsx for the router and
    // individual card components (FileReadCard, FileEditCard, ShellCard, etc.).

    /** Renders an error event with red styling. */
    function ErrorEvent({ content }: { content: string }): JSX.Element {
      return <div className={styles.errorEvent}>Error: {content}</div>;
    }

    /** Renders a status change event with separator lines. */
    function StatusEvent({ content }: { content: string }): JSX.Element {
      return <div className={styles.statusEvent}>--- {content} ---</div>;
    }

    /** Renders a user input event as markdown, right-aligned to distinguish it from agent output. */
    function UserInputEvent({ content }: { content: string }): JSX.Element {
      return (
        <div className={styles.userInputEvent}>
          <div className={styles.userInputContent} data-testid="user-input-content">
            <MarkdownContent content={content} />
          </div>
        </div>
      );
    }

    /** Renders a signal event (e.g. SIGCHLD) as a left-bordered banner. */
    function SignalEvent({ content }: { content: string }): JSX.Element {
      return (
        <div className={styles.signalEvent} data-testid="signal-event">
          <span className={styles.signalBadge}>SIGNAL</span>
          <span className={styles.signalContent}>{content}</span>
        </div>
      );
    }

    /** Renders a usage event as a compact cost badge. */
    function UsageEvent({ content }: { content: string }): JSX.Element {
      let label = content;
      try {
        const data = JSON.parse(content) as Record<string, unknown>;
        const inTok = Number(data.input_tokens) || 0;
        const outTok = Number(data.output_tokens) || 0;
        const tokens = formatTokens(inTok + outTok);
        const cost = formatCost(Number(data.cost_millicents) || 0);
        label = `${tokens} tokens \u00b7 ${cost}`;
      } catch {
        /* show raw content if JSON fails */
      }
      return (
        <div className={styles.usageEvent} data-testid="usage-event">
          <span className={styles.usageBadge}>{label}</span>
        </div>
      );
    }

    /** Renders an unrecognized event type. */
    function DefaultEvent({ content }: { content: string }): JSX.Element {
      return <div className={styles.defaultEvent}>{content}</div>;
    }

    // --- Main component ---

    /** Renders a single session event, dispatching to the appropriate type-specific renderer. */
    export function EventRenderer({
      event,
      toolUseCtx,
      settled,
      sandboxProxyUrl,
      onOpenDocument,
    }: Props): JSX.Element {
      const time = new Date(event.timestamp).toLocaleTimeString();

      switch (event.eventType) {
        case "widget": {
          // MCP Apps widget event (pushed by the broker). Self-contained: HTML +
          // tool input/result. Renders in the cross-origin sandbox via McpAppWidget.
          Iif (!sandboxProxyUrl) {
            return <DefaultEvent content={event.content} />;
          }
          let payload: {
            html?: string;
            rendererKind?: string;
            // `allowInlineScripts`/`allowUnsafeEval` are Grackle extensions to the
            // upstream CSP type (agent-authored widgets #1239; React runtime #1268);
            // forwarded verbatim to the sandbox.
            csp?: McpUiResourceCsp & { allowInlineScripts?: boolean; allowUnsafeEval?: boolean };
            toolInput?: Record<string, unknown>;
            toolResult?: CallToolResult;
            // Resolved registry dependencies for composition (#1270), eval order.
            components?: Array<{ name: string; body: string }>;
          } = {};
          try {
            payload = JSON.parse(event.content) as typeof payload;
          } catch {
            /* malformed widget payload — fall back */
          }
          // Dispatch on rendererKind (default "mcp-app-html" for back-compat). This
          // switch is the seam for declarative/runtime renderers.
          const rendererKind: string = payload.rendererKind ?? "mcp-app-html";
          // GenUX React runtime (#1268): payload.html is JSX *source* (not a full
          // document). Render it via a bootstrap that loads the runtime bundle from the
          // sandbox origin; the source + props are delivered as tool input. The runtime
          // transpiles + renders the component against the Grackle component library.
          // An absolute runtime.js URL is required — the inner iframe is written via
          // doc.write (about:blank base), so a relative "/runtime.js" would not resolve.
          if (rendererKind === "grackle-react" && payload.html) {
            const sandboxOrigin: string = new URL(sandboxProxyUrl, window.location.href).origin;
            const bootstrap: string =
              `<!doctype html><html><head><meta charset="utf-8"></head>` +
              `<body><div id="grackle-root"></div>` +
              `<script type="module" src="${sandboxOrigin}/runtime.js"></script></body></html>`;
            return (
              <Suspense fallback={<DefaultEvent content="Loading widget..." />}>
                <McpAppWidget
                  widgetHtml={bootstrap}
                  sandboxProxyUrl={sandboxProxyUrl}
                  csp={payload.csp}
                  toolInput={{
                    source: payload.html,
                    props: payload.toolInput ?? {},
                    components: payload.components ?? [],
                  }}
                />
              </Suspense>
            );
          }
          if (rendererKind !== "mcp-app-html" || !payload.html) {
            return <DefaultEvent content={event.content} />;
          }
          return (
            <Suspense fallback={<DefaultEvent content="Loading widget..." />}>
              <McpAppWidget
                widgetHtml={payload.html}
                sandboxProxyUrl={sandboxProxyUrl}
                csp={payload.csp}
                toolInput={payload.toolInput}
                toolResult={payload.toolResult}
              />
            </Suspense>
          );
        }
        case "system": {
          // Detect system context events via the raw metadata marker
          if (event.raw) {
            try {
              const rawData = JSON.parse(event.raw) as Record<string, unknown>;
              if (rawData.systemContext === true) {
                return <SystemContextEvent content={event.content} />;
              }
            } catch {
              /* not JSON, render as normal system event */
            }
          }
          return <SystemEvent time={time} content={event.content} />;
        }
        case "text":
        case "output":
          return <TextEvent content={event.content} onOpenDocument={onOpenDocument} />;
        case "tool_use": {
          let tool = "";
          let args: unknown = {};
          try {
            const parsed = JSON.parse(event.content) as { tool?: string; args?: unknown };
            tool = parsed.tool || "";
            args = parsed.args;
          } catch {
            /* fallback to empty */
          }
          // When settled, pass empty result so the card shows as completed (no spinner)
          // rather than in-progress. This handles Claude Code which emits results as text.
          return (
            <ToolCard
              tool={tool}
              args={args}
              result={settled ? "" : undefined}
              childSessionId={delegationChildId(event.sessionId, event.toolCallId, tool, args)}
              onOpenDocument={onOpenDocument}
            />
          );
        }
        case "tool_result": {
          // When paired, toolUseCtx provides the tool name, args, and optional detailedResult.
          // When unpaired, fall back to a generic display.
          //
          // Error detection (#1362): prefer the first-class `toolError` field,
          // set by every runtime adapter from its native outcome signal and
          // carried across the AHP wire as `result.success`. Fall back to the
          // structured `content.is_ok === false` shape the reverse mapper writes
          // (HR8d #1355), then to the legacy `raw.is_error` path for pre-HR8d
          // JSONL replay.
          let isError = event.toolError === true;
          if (!isError) {
            try {
              const parsed = JSON.parse(event.content) as Record<string, unknown>;
              Iif (parsed.is_ok === false) {
                isError = true;
              }
            } catch {
              /* content not JSON — fall through to legacy raw check */
            }
          }
          if (!isError && event.raw) {
            try {
              const rawData = JSON.parse(event.raw) as Record<string, unknown>;
              isError = rawData.is_error === true;
            } catch {
              /* ignore */
            }
          }

          // Try to extract displayable content from JSON-wrapped results.
          // Guard with startsWith check to avoid throwing on plain text content.
          let resultContent = event.content;
          Iif (event.content.trimStart().startsWith("{")) {
            try {
              const parsed = JSON.parse(event.content) as Record<string, unknown>;
              Iif (typeof parsed.content === "string") {
                resultContent = parsed.content;
              }
            } catch {
              /* content looks like JSON but isn't — use as-is */
            }
          }

          if (toolUseCtx) {
            return (
              <ToolCard
                tool={toolUseCtx.tool}
                args={toolUseCtx.args}
                result={resultContent}
                isError={isError}
                detailedResult={toolUseCtx.detailedResult}
                childSessionId={delegationChildId(
                  event.sessionId,
                  event.toolCallId,
                  toolUseCtx.tool,
                  toolUseCtx.args,
                )}
                onOpenDocument={onOpenDocument}
              />
            );
          }
          // Unpaired tool_result — use generic card with fallback label
          return (
            <ToolCard
              tool="Tool output"
              args={undefined}
              result={resultContent}
              isError={isError}
              onOpenDocument={onOpenDocument}
            />
          );
        }
        case "error":
          return <ErrorEvent content={event.content} />;
        case "status":
          return <StatusEvent content={event.content} />;
        case "user_input":
          return <UserInputEvent content={event.content} />;
        case "signal":
          return <SignalEvent content={event.content} />;
        case "usage":
          return <UsageEvent content={event.content} />;
        default:
          return <DefaultEvent content={event.content} />;
      }
    }