# Conversation

A conversation with an agent, as a screen: the transcript and the parts in it. The transcript opens at its end and follows a reply while you are at the end. When you scroll up, it stops following. It anchors each of your messages near the top, so you read each reply from its start. A button takes you back to the latest message. Under this, it is a MessageScroller on the scroller of the pane. Your message sits on the right on a tint, with its attachments above it. The reply of the agent has no bubble. The tool calls between replies fold into one line. While they run, the line tells you what happens. When they are done, it tells you how much happened. The pictures that a turn made stay out of the fold. When the agent wants to spend something, it stops and asks at the composer, as a Confirmation in its notices. The transcript records only the answer. A failure that ends the turn is a notice with the one action that fixes it. Every word comes from the app, including the counts, because a copied file cannot localise.

## Files

### conversation.tsx

```tsx
"use client";

/**
 * A conversation with an agent: the transcript that holds it, and the parts of it that are not
 * words — the person's own message, the run of steps an agent takes between replies, and the
 * pictures a turn made. A question the agent stops to ask is not here: it waits at the composer,
 * as a `Confirmation` in its `notices`, and only the answer is recorded in the transcript.
 *
 * WHY A BLOCK. Every part here is an arrangement of package components with the words and the
 * pictures supplied by the app, and one piece of state (whether a run is open). That is the
 * definition of a source block. Following the live edge is the package's `MessageScroller`, and
 * `Conversation` is only where it is put together with the pane's own scroller.
 *
 * WHAT THE BEST CHATS AGREED ON, and so what this draws:
 *
 *  - **The person's words sit right, on a tint, with no edge**, and the reply has no bubble at
 *    all and takes the full measure (ChatGPT, Claude). Attachments sit above the bubble they
 *    came with, as thumbnails.
 *  - **A run of tool calls folds to one row.** Live, the row says what is happening now; done, it
 *    says how much happened, and opens to the list (ChatGPT's "Thought for", Perplexity's
 *    "Completed N steps", Cursor's grouped tool calls, Flora's "View steps"). A step that failed
 *    and was put right stays inside with the others; only a failure that stops the turn belongs
 *    in the conversation.
 *  - **Pictures are the result, so they stay out of the fold**: one at its own shape, several in a
 *    grid of squares (Midjourney's four, ChatGPT's sets). No captions; what they are called is the
 *    app's to show where the pictures live.
 *
 * REFUSED:
 *
 *  - **English.** The summary ("6 steps"), each step's words and every label are the caller's,
 *    because a copied file cannot localise and the counts are the app's grammar.
 *  - **A reply bubble.** `Reply` is text that keeps its line breaks and nothing more; a reply is
 *    read at the full measure with no box around it.
 *  - **Timing.** "Worked for 42 seconds" needs the app's clock, so it is a summary the app writes.
 *  - **A lightbox.** What opening a picture does (a dialog, the canvas, a download) is the app's.
 *
 * SIZE 3 IS THE DEFAULT, against the package's 2: a conversation is the
 * thing being read, and it was one step under the composer and the toolbar around it. The index
 * rides the part's own text and the lengths derived from it — the thumbnail's height, the
 * timeline's half-row, the corners — through `data-kb-size` in the stylesheet.
 */
import * as React from "react";
import {
  MessageScroller,
  MessageScrollerButton,
  MessageScrollerContent,
  MessageScrollerItem,
  ScrollArea,
  ShellScroll,
  Stack,
  Text,
  iconStroke,
  type Size,
} from "@kushagradhawan/kookie-ui-react";

import "./conversation.css";

export type ConversationProps = {
  /** The transcript's name, for the region a screen reader lands in: "Conversation". */
  "aria-label": string;
  /** The jump button's name, in your words: "Jump to the latest". */
  jumpLabel: string;
  /** The jump button's glyph. The package ships no icon set. */
  jumpIcon: React.ReactNode;
  /**
   * The pane the transcript fills is a Shell pane, so it scrolls with `ShellScroll` and spends the
   * reach the pane's floating bands publish. Otherwise it is a `ScrollArea`, for a card or a box
   * you have sized.
   */
  pane?: boolean;
  /** Shown in the middle of the transcript while it has no rows: usually an empty state. */
  empty?: React.ReactNode;
  /** One `Turn` per row, oldest first. */
  children?: React.ReactNode;
};

/**
 * The transcript: it opens at its end, follows what arrives while you are there, leaves you alone
 * once you scroll up, anchors each of your messages near the top so the reply is read from its
 * start, and offers a button back to the latest.
 */
export function Conversation({
  "aria-label": label,
  jumpLabel,
  jumpIcon,
  pane = false,
  empty,
  children,
}: ConversationProps) {
  const rows = React.Children.toArray(children);
  const Scroll = pane ? ShellScroll : ScrollArea;
  return (
    <MessageScroller>
      <Scroll fade aria-label={label} className="kb-conversation" data-kb-pane={pane || undefined}>
        <MessageScrollerContent className="kb-transcript" data-empty={rows.length === 0 || undefined}>
          {rows.length === 0 && empty ? <MessageScrollerItem>{empty}</MessageScrollerItem> : rows}
        </MessageScrollerContent>
        <MessageScrollerButton aria-label={jumpLabel}>{jumpIcon}</MessageScrollerButton>
      </Scroll>
    </MessageScroller>
  );
}

export type TurnProps = {
  /** A stable id, so the row can be jumped to. The message's own id. */
  id?: string;
  /**
   * Who is speaking. The person's turns anchor near the top when they arrive; the agent's follow
   * the live edge. Leave it out for a row that is neither (a thinking line, a decision).
   */
  from?: "person" | "agent";
  children: React.ReactNode;
};

/** One row of the transcript. */
export function Turn({ id, from, children }: TurnProps) {
  return (
    <MessageScrollerItem
      {...(id ? { messageId: id } : {})}
      scrollAnchor={from === "person"}
      className="kb-turn"
    >
      {children}
    </MessageScrollerItem>
  );
}

/** The agent's words: text in the reading ink, with the line breaks it wrote. */
export function Reply({ children, size = "3" }: { children: React.ReactNode; size?: Size }) {
  return (
    <Text size={size} className="kb-reply">
      {children}
    </Text>
  );
}

export type UserMessageProps = {
  /** The text's index, and the lengths that follow from it. A conversation reads at 3. */
  size?: Size;
  /** The person's words, as they typed them: line breaks are kept. */
  children: React.ReactNode;
  /** Thumbnails of what they attached, usually `img` elements. Sized here, owned by the app. */
  attachments?: React.ReactNode;
};

export function UserMessage({ children, attachments, size = "3" }: UserMessageProps) {
  return (
    <Stack className="kb-user-message" gap="2" align="end" data-kb-size={size}>
      {attachments ? <div className="kb-user-message-attachments">{attachments}</div> : null}
      <div className="kb-user-message-bubble">
        <Text size={size}>{children}</Text>
      </div>
    </Stack>
  );
}

export type StepsProps = {
  /** The text's index, and the lengths that follow from it. A conversation reads at 3. */
  size?: Size;

  /**
   * The row's words. Done, how much happened ("6 steps"); live, what is happening now ("Adding
   * nodes"). One prop for both, because the row is one line either way and only the app knows
   * which it is.
   */
  summary: React.ReactNode;
  /** The run is still going: the row turns its spinner on. */
  live?: boolean;
  /** One `Step` per tool call, oldest first. */
  children: React.ReactNode;
};

export function Steps({ summary, live = false, size = "3", children }: StepsProps) {
  const [open, setOpen] = React.useState(false);
  const listId = React.useId();
  return (
    <Stack className="kb-steps" gap="2" data-kb-size={size}>
      <button
        type="button"
        className="kb-steps-toggle"
        {...(live ? { "data-live": "" } : {})}
        aria-expanded={open}
        aria-controls={listId}
        onClick={() => setOpen((was) => !was)}
      >
        <Text size={size} emphasis="medium" className="kb-steps-line">
          {summary}
        </Text>
        {/* The disclosure chevron the accordion and the tree draw: into the reading direction
            closed, turned down open. */}
        <svg viewBox="0 0 16 16" fill="none" aria-hidden className="kb-steps-chevron" data-open={open || undefined}>
          <path
            d="M6 4l4 4-4 4"
            stroke="currentColor"
            strokeWidth={iconStroke}
            strokeLinecap="round"
            strokeLinejoin="round"
          />
        </svg>
      </button>
      {open ? (
        <div id={listId} className="kb-steps-list">
          {children}
        </div>
      ) : null}
    </Stack>
  );
}

export type StepProps = {
  /** The text's index, and the lengths that follow from it. A conversation reads at 3. */
  size?: Size;
  /** What the step did, in a few words. Longer than the line and it is cut; pass `title` too. */
  children: React.ReactNode;
  /** The step has been asked for and has not answered. */
  pending?: boolean;
  /** The whole line, for when the words are cut. */
  title?: string;
};

export function Step({ children, pending = false, title, size = "3" }: StepProps) {
  return (
    <div className="kb-step" data-kb-size={size} {...(pending ? { "data-pending": "" } : {})}>
      <Text size={size} emphasis="medium" className="kb-steps-line" title={title}>
        {children}
      </Text>
    </div>
  );
}

export type ThinkingProps = {
  /** What it is doing, in the app's words: "Thinking", "Reading the graph". */
  children: React.ReactNode;
  /** The text's index. A conversation reads at 3. */
  size?: Size;
};

/**
 * The model is working and has nothing to show yet: a line that sweeps until it does (AI Elements'
 * Reasoning header). A sweep rather than a spinner, because the line IS the news — a spinner beside
 * words says the same thing twice and pins a control's shape into the reading column.
 */
export function Thinking({ children, size = "3" }: ThinkingProps) {
  return (
    <Text size={size} emphasis="medium" className="kb-thinking" aria-live="polite">
      {children}
    </Text>
  );
}

export type PicturesProps = {
  /** The text's index, and the lengths that follow from it. A conversation reads at 3. */
  size?: Size;
  /** The pictures, usually `img` elements. One keeps its own shape; more are cropped square. */
  children: React.ReactNode;
};

export function Pictures({ children, size = "3" }: PicturesProps) {
  const single = React.Children.count(children) === 1;
  return (
    <div className="kb-pictures" data-kb-size={size} data-single={single || undefined}>
      {children}
    </div>
  );
}
```

### conversation.css

```css
/* ── The conversation's own half ────────────────────────────────────────────────────────────
   Every length resolves through a `--` token or is relative to the type, and every colour is a
   role the package already solved, so a copy of this file changes arrangement and nothing else. */

/* THE PART'S INDEX, AS LENGTHS. Every part states `data-kb-size` — the block's own attribute, not
   the system's `size` axis written onto the DOM — and the two lengths that have to
   follow the text — the row it occupies and the corner that wraps it — resolve here, so the same
   arrangement holds at any index. Default 3: a conversation is the thing being read. */
.kb-user-message,
.kb-steps,
.kb-step,
.kb-pictures {
  --kb-line: var(--line-height-3);
  --kb-radius: var(--radius-row-3);
}

[data-kb-size="1"].kb-user-message,
[data-kb-size="1"].kb-steps,
[data-kb-size="1"].kb-step,
[data-kb-size="1"].kb-pictures {
  --kb-line: var(--line-height-1);
  --kb-radius: var(--radius-row-1);
}

[data-kb-size="2"].kb-user-message,
[data-kb-size="2"].kb-steps,
[data-kb-size="2"].kb-step,
[data-kb-size="2"].kb-pictures {
  --kb-line: var(--line-height-2);
  --kb-radius: var(--radius-row-2);
}

[data-kb-size="4"].kb-user-message,
[data-kb-size="4"].kb-steps,
[data-kb-size="4"].kb-step,
[data-kb-size="4"].kb-pictures {
  --kb-line: var(--line-height-4);
  --kb-radius: var(--radius-row-4);
}

/* THE TRANSCRIPT. Turns stand apart and the blocks inside a turn sit closer (AI Elements: 32
   between messages, 16 within). In a Shell pane the content spends the reach the floating bands
   publish; anywhere else those names are unset and the pane's own padding is the edge. The end
   also clears the jump button, so at rest no line sits under it. */
.kb-transcript {
  flex: 1;
  gap: var(--layout-space-7);
  padding-block: var(--kui-pane-inset-block-start, 0px)
    calc(var(--kui-pane-inset-block-end, 0px) + var(--control-height-3));
}

/* The scroller's content box is as tall as the viewport, so an empty state can sit in its middle
   (the transcript's own `min-block-size: 100%` resolves against an automatic height and does
   nothing), and it never widens past it: ScrollArea's content box is `fit-content` so a wide
   thing can be scrolled to sideways, which in a conversation nothing should be — one unbroken
   word or a step's nowrap line would otherwise scroll the whole transcript. */
.kb-conversation > .kui-scroll-viewport > .kui-scroll-content {
  display: flex;
  flex-direction: column;
  min-block-size: 100%;
  min-inline-size: 0;
}

/* In a card or a box you sized, the transcript takes the room the composer leaves. */
.kb-conversation:not([data-kb-pane]) {
  flex: 1;
  min-block-size: 0;
}

/* Nothing yet: the one thing to read sits in the middle of the room it has. Auto margins rather
   than `justify-content`, because the rows are the transcript's and not the content box's. */
.kb-transcript[data-empty] > * {
  margin-block: auto;
}

/* In a pane the jump button rests one step above the floating footer. A sticky offset is measured
   inside the viewport's padding, which is the pane's padding moved inward, so it comes off twice. */
.kb-conversation[data-kb-pane] .kui-message-scroller-dock {
  inset-block-end: calc(var(--kui-pane-inset-block-end) - 2 * var(--kui-sf-p) + var(--layout-space-3));
}

/* THE AGENT'S WORDS keep the breaks it wrote, and a long unbroken word wraps rather than widening
   the transcript. */
.kb-reply {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* THE PERSON'S MESSAGE. A tint rather than a pane: an alpha fill reads the same on paper and on
   glass, where an opaque surface would punch a hole. The corner is a row's, so a one-line
   message is a capsule and a long one a soft box. The measure leaves the reply's side visibly
   open, which is how the two voices are told apart before a word is read. */
.kb-user-message-bubble {
  max-inline-size: 85%;
  padding-block: var(--layout-space-3);
  padding-inline: var(--layout-space-4);
  border-radius: var(--kb-radius);
  background-color: var(--neutral-a3);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.kb-user-message-attachments {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--layout-space-2);
  max-inline-size: 85%;
}

/* Four lines of the message tall and square: a thumbnail is recognised, not read. The fill is
   what a picture that has not loaded shows, and what a placeholder with no picture at all is. */
.kb-user-message-attachments > * {
  display: block;
  block-size: calc(var(--kb-line) * 4);
  inline-size: auto;
  aspect-ratio: 1;
  background-color: var(--neutral-a3);
  max-inline-size: 100%;
  object-fit: cover;
  border-radius: var(--kb-radius);
}

/* THE RUN. The row is words with a chevron, not a button's pill: it sits in the reply's column and
   its first letter lines up with the reply's. It carries no fill in any state; the words come up
   to the full ink under the pointer, which is how a disclosure says it can be pressed without
   claiming to be a control. */
.kb-steps-toggle {
  display: flex;
  align-items: center;
  gap: var(--layout-space-2);
  align-self: flex-start;
  min-inline-size: 0;
  max-inline-size: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: start;
  cursor: pointer;
}

.kb-steps-toggle:hover .kb-steps-line {
  color: var(--color-text);
}

/* Open, the row is the list's heading rather than a line of its own. */
.kb-steps-toggle[aria-expanded="true"] .kb-steps-line {
  color: var(--color-text);
}

.kb-steps-toggle:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring);
  outline-offset: var(--focus-ring-offset);
  border-radius: var(--radius-control-1);
}

.kb-steps-chevron {
  flex: none;
  inline-size: 1em;
  block-size: 1em;
  color: var(--color-text-muted);
}

.kb-steps-chevron[data-open] {
  rotate: 90deg;
}

.kb-steps-chevron:dir(rtl) {
  scale: -1 1;
}

/* THE LIST IS A TIMELINE (AI Elements' Chain of Thought): a line down the column with a mark on it
   per step, read top to bottom. The line is drawn behind the marks rather than as a border, so the
   first and last marks cap it instead of hanging off it. */
.kb-steps-list {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--layout-space-3);
  padding-inline-start: var(--layout-space-5);
}

/* The thread, from the first mark to the last: the row's own line-height is the panel's, so the
   half-row the line starts and ends inside is stated in the step's, which the marks are centred in
   too. Two hairlines wide, because one at the border colour disappears beside the marks. */
.kb-steps-list::before {
  content: "";
  position: absolute;
  inset-block: calc(var(--kb-line) / 2 - var(--border-width));
  inset-inline-start: calc((var(--layout-space-3) - 2 * var(--border-width)) / 2);
  inline-size: calc(2 * var(--border-width));
  /* The marks' own ink, thinned: at the border's colour the thread disappears beside them. It runs
     behind the marks rather than between them — a halo around each would break the line into
     dashes, which is what a thread is not. */
  background-color: color-mix(in oklab, var(--color-text-muted) 70%, transparent);
}

.kb-step {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--layout-space-2);
  min-inline-size: 0;
}

/* The mark: a dot on the line, in the ink of the words beside it, ringed in the panel's own colour
   so the line passes behind it rather than through it.
   ONLY ON THE LINE. A step standing on its own — a turn that took one — has no timeline to be a
   mark on, and a dot then hangs outside the text column, into the pane's padding. Centred by `inset-block: 0` plus auto margins rather than against a line-height,
   because the row's own line-height is the panel's and the words' is the step's. */
.kb-steps-list > .kb-step::before {
  content: "";
  position: absolute;
  inset-inline-start: calc(-1 * var(--layout-space-5));
  inset-block: 0;
  margin-block: auto;
  inline-size: var(--layout-space-3);
  block-size: var(--layout-space-3);
  border-radius: 50%;
  background-color: var(--color-text-muted);
}

/* The step being taken: a full dot in the reading ink, so the eye lands on it. */
.kb-steps-list > .kb-step[data-pending]::before {
  background-color: var(--color-text);
}

/* A LIVE LINE SWEEPS (AI Elements' Reasoning and Shimmer): one light band travels across the words
   while something is still happening and stops when it ends, which says "still happening" without a spinner in
   the reading column. The band is the words' own ink lifted, so it reads on paper and on glass. */
.kb-steps-toggle[data-live] .kb-steps-line,
.kb-thinking {
  color: transparent;
  background-image: linear-gradient(
    90deg,
    var(--color-text-muted) 0%,
    var(--color-text-muted) 40%,
    var(--color-text) 50%,
    var(--color-text-muted) 60%,
    var(--color-text-muted) 100%
  );
  background-size: 250% 100%;
  background-clip: text;
  animation: kb-shimmer 2s linear infinite;
}

@keyframes kb-shimmer {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -100% 0;
  }
}

/* Slowed rather than stopped: the sweep IS the news that something is happening. */
@media (prefers-reduced-motion: reduce) {
  .kb-steps-toggle[data-live] .kb-steps-line,
  .kb-thinking {
    animation-duration: 6s;
  }
}

/* A step is one line. What does not fit is cut, and the whole of it is the step's title.
   `min-inline-size: 0` is not enough on its own inside a scroller whose content box is
   `fit-content` (Kookie's ScrollArea): the line's longest word still widens the box and the whole
   panel scrolls sideways. The row is the wall. */
.kb-steps-line {
  min-inline-size: 0;
  max-inline-size: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

/* THE PICTURES. Several share one square so a set reads as a set; one keeps the shape it was made
   at, because a portrait cropped square is no longer the picture. Its height is bounded by the
   type, so a tall picture in a narrow column stays a glance and not a scroll. */
.kb-pictures {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--layout-space-2);
}

.kb-pictures > * {
  display: block;
  inline-size: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: var(--kb-radius);
  background-color: var(--neutral-a3);
}

.kb-pictures[data-single] {
  grid-template-columns: minmax(0, 1fr);
  justify-items: start;
}

.kb-pictures[data-single] > * {
  inline-size: auto;
  max-inline-size: 100%;
  max-block-size: 45ch;
  aspect-ratio: auto;
}
```
