Files
PaperClipAI/ui/src/components/task-chat/TaskChatTurn.tsx
T
DottaandPaperclip a0ac81e114 fix(ui): show task image results in compact thumbnail grids (#19626)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Task feeds show agent messages, documents, and files in time order.
> - Each image currently uses a full feed card.
> - A turn with seven screenshots makes the conversation hard to scan.
> - This pull request groups adjacent images from the same run in a
compact grid.
> - Users can scan the results and open each image in the existing
gallery.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

Image results in task history and the live task feed.

**Current behavior**

Each image uses a separate full-width card. Native image upload receipt
messages can also separate related screenshots.

**Proposed behavior**

Adjacent images from one run share a wrapping thumbnail grid. New image
results join the grid. Documents, video, and authored messages keep
their positions. The grid opens the existing gallery.

**Reason and benefit**

Users can see a batch of screenshots without scrolling past one large
card for each file.

**Breaking changes**

The feed layout changes. The stored comments, files, APIs, and
permissions do not change.

Related public work: Refs #17228. Refs #8785. Refs #7667. These changes
supplied the artifact cards and media gallery that this pull request
reuses.

## What Changed

- Add one image grouping function for settled history, turn history, and
live results. Group only images with the same known run. Keep the first
row key and each image key when results arrive.
- Add a token-based grid that reuses the media artifact cards and image
gallery.
- Add an optional typed attachment-preparation marker to file
registration and chat-file reuse. Preserve the marker through comment
normalization. Use it with matching image, author, and run links to hide
generated receipts. Keep the exact historical sentence as a fallback for
old receipts.
- Add nine production component stories with seven sample-account
screenshots. Cover mobile, Classic, mixed results, single images,
missing images, streaming, gallery navigation, and the complete feed
projection.
- Add grouping and production-feed regressions. Add 18 browser cases
across light and dark themes.
- Improve contrast for the shared fallback label and gallery counter
after the accessibility checks found low contrast.
- Match alternate original-image paths when opening the gallery. Keep a
separate Open original link beside Download in the gallery.
- Record the feed layout in DESIGN.md.

## Verification

- Final qualification at `09b12381ce85bee6593050bd8a2fa18cbe2c45a6`:
[required CI
passed](https://github.com/paperclipai/paperclip/actions/runs/42317080212/job/128237094513),
including typecheck, build, all test lanes, E2E, and verify. The
snapshot has 58 successful check runs, three conditional skips, one
neutral advisory job, and a successful legacy Snyk status. No pending or
failed checks remain. Greptile and Paperclip Review both score this
exact head 5/5; all review threads are resolved.

- Current-head local checks: 544 focused UI tests passed across 11
files, with one existing skip. UI/shared typechecks and token gates
pass. All 18 focused Storybook browser cases passed after the CSS merge,
with retries disabled.
- Two database integration cases verify file registration and chat-file
reuse receipt markers. All 52 issue-validator tests and six upstream
default-model tests passed.
- Regressions failed before the fixes for unknown-run grouping, receipt
wording, and authored progress text with an image. The repair uses a
typed receipt marker and requires matching author, attachment, image,
and run links before hiding a generated receipt. All five review
findings are resolved.
- Browser checks cover loaded images, wrapping columns, overflow,
missing-image fallback, streaming stability, keyboard gallery
navigation, original-file links, and downloads. Fixtures use production
renderers. These checks do not prove deployed persistence or provider
behavior and do not compare visual baselines.
- Local limits: the preflight helper could not finish because Cargo was
absent. The earlier low-memory server typecheck and dependency-free
selector did not replace final-head CI. The now-passing final CI
supplies broad typecheck, build, server, Runner, and E2E coverage.
- Prior independent CI failures remain recorded: one Slack timer case
passed a single diagnosed rerun; its exact timer cause was not proved. A
later chat shard lost its self-hosted runner without a test assertion
failure. The current revision passes that shard and all applicable
gates. Master conflict resolution retained both image-grid and patch CSS
rules.

## Risks

- Images without a run ID remain separate. Messages and other resource
types end each batch.
- Receipt suppression requires a matching preparation marker or the
exact legacy sentence, plus matching attachment, image work product,
run, and author. Tests preserve human messages and native progress text
with attachments. The optional marker uses existing JSON metadata and
needs no database migration.
- Shared gallery and fallback text now have stronger contrast.

## Model Used

OpenAI GPT-6, through Codex. The agent used reasoning, repository tools,
code execution, and browser tests. The runtime did not expose a more
specific model ID or context window size.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-11-06 18:42:22 -06:00

300 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { groupImageResources } from "./image-resources";
import { useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
import { TaskChatRunHealthBanner } from "./TaskChatRunHealthBanner";
import { useStreamlinedTaskChatPresentation } from "./presentation-mode";
import { Check, ChevronRight, X } from "lucide-react";
import { MarkdownBody } from "@/components/MarkdownBody";
import type {
TaskChatTurnItem,
TaskChatTurnChildItem,
} from "./task-chat-model";
import { TaskChatStatusPill } from "./TaskChatStatusPill";
import { TaskChatAgentIdentity } from "./TaskChatBubble";
interface TaskChatTurnProps {
item: TaskChatTurnItem;
renderChild: (child: TaskChatTurnChildItem) => ReactNode;
/**
* When the settled turn is attached to its reply bubble (round 9), the
* bubble's timestamp leads the summary line — "2:34 PM · ✓ Worked · 38s" —
* always visible, in the slot the hover-only timestamp used to occupy.
*/
timestampPrefix?: string;
/**
* Content rendered on the header row (PAP-413: the copy/👍/👎 action
* cluster). The inspection caret and timestamp stay left; actions sit at the
* stable right edge, outside the expandable fold.
*/
leading?: ReactNode;
}
/** Metric segments after the label: "38s · 3 tools · +34 −3 · 12.3k tokens". */
export function turnSummaryMetrics(
summary: TaskChatTurnItem["summary"],
): string {
const parts: string[] = [];
if (summary.durationLabel) parts.push(summary.durationLabel);
if (summary.toolCount > 0)
parts.push(
`${summary.toolCount} tool${summary.toolCount === 1 ? "" : "s"}`,
);
if (summary.added > 0 || summary.removed > 0)
parts.push(`+${summary.added} −${summary.removed}`);
if (summary.tokensLabel) parts.push(summary.tokensLabel);
return parts.join(" · ");
}
/** "✓ Worked · 38s · 3 tools · +34 −3 · 12.3k tokens" (parts omitted when unknown). */
export function turnSummaryText(summary: TaskChatTurnItem["summary"]): string {
const metrics = turnSummaryMetrics(summary);
const label = summary.failed ? "Stopped" : "Worked";
return metrics ? `${label} · ${metrics}` : label;
}
/**
* One agent turn's activity behind a single expandable header row. Paperclip
* Runner turns opt into the chronological-timeline branch below instead.
*
* While the turn is live and carries `liveStatus`, that status line (whimsy or
* taxonomy gerund + elapsed + tokens) IS the turn: one parent row, collapsed
* by default, owning all activity (PAP-354). Expanding nests the chronological
* tool/thinking history underneath — each row keeping its own expand — and new
* rows append live while open. On settle the header morphs in place into the
* "✓ Worked · …" summary, preserving the expand state, so the running line
* reads as being replaced by the summary in the same slot.
*
* Both directions of the fold reuse the .tc-turn-fold grid-rows motion
* (--motion-turn-fold, zeroed under prefers-reduced-motion). The fold class
* only transitions on state CHANGE, so first paint in the folded state (e.g. a
* turn that loads already settled) never animates. A live turn without
* `liveStatus` (harness fixtures) renders its children expanded with no header
* and folds when it settles.
*/
export function TaskChatTurn({
item,
renderChild,
timestampPrefix,
leading,
}: TaskChatTurnProps) {
const streamlined = useStreamlinedTaskChatPresentation();
const parentRow = !item.settled && item.liveStatus != null;
// The new Paperclip Runner task surface owns one durable chronological
// timeline. The Worked/Stopped row is its stable header, so it stays directly
// below the preceding human bubble and above commentary, activity phases,
// request receipts, and plan artifacts. The classic task interface continues
// to use the run-wide fold below.
if (item.standaloneHeader) {
return (
<div
data-testid="task-chat-turn"
data-settled={item.settled ? "true" : "false"}
>
{item.agentName || !item.historical ? <div
className="flex min-h-8 w-full min-w-0 items-center gap-2 pb-1 pt-1.5 text-sm text-muted-foreground"
data-testid="task-chat-turn-summary"
data-turn-position="identity"
>
{item.agentName ? (
<TaskChatAgentIdentity
agentName={item.agentName}
agentIcon={item.agentIcon} agent={item.agent}
/>
) : null}
{!item.historical ? <span className="min-w-0 truncate">
{item.continuedAfterSteering ? "Continued after steering · " : ""}
{item.summary.durationLabel
? `${item.summary.failed ? "Stopped" : "Worked"} for ${item.summary.durationLabel}`
: item.summary.failed
? "Stopped"
: "Worked"}
</span> : null}
</div> : null}
<TaskChatRunHealthBanner failures={item.mcpStartupFailures ?? []} />
{item.items.length > 0 ? (
<div
className="flex min-w-0 flex-col gap-2 py-1"
data-testid="task-chat-turn-timeline"
>
{groupImageResources(item.items).map((child) => (
<div
className="min-w-0"
key={child.id}
data-testid="task-chat-turn-timeline-row"
data-timeline-row-id={child.id}
data-thread-anchor={child.id}
>
{renderChild(child)}
</div>
))}
</div>
) : null}
{item.finalResponse ? (
<div className="w-full" data-testid="task-chat-final-response">
<div
className="break-words px-1 py-2 text-sm text-foreground"
data-testid="task-chat-agent-bubble"
>
<MarkdownBody softBreaks linkIssueReferences>
{item.finalResponse.text}
</MarkdownBody>
</div>
</div>
) : null}
</div>
);
}
const persistentItems = item.settled
? item.items.filter(
(child) =>
child.kind === "protocol" && child.surface === "runtime_request",
)
: [];
const persistentItemIds = new Set(persistentItems.map((child) => child.id));
const foldedItems =
persistentItems.length > 0
? item.items.filter((child) => !persistentItemIds.has(child.id))
: item.items;
// Parent-row live turns and settled turns start as their one-line header;
// only the headerless legacy live turn starts expanded.
const [open, setOpen] = useState(
() => !item.settled && item.liveStatus == null,
);
// Historical folds can contain thousands of tool/reasoning rows. Mount them
// on first inspection, then retain them for closing motion and child state.
const [historyMounted, setHistoryMounted] = useState(open);
if (open && !historyMounted) setHistoryMounted(true);
const [prevSettled, setPrevSettled] = useState(item.settled);
const [wasParentRow, setWasParentRow] = useState(parentRow);
// Render-adjusted state (not effects) so transitions land in the same
// commit — no flash of the wrong fold state.
if (parentRow && !wasParentRow) setWasParentRow(true);
// Live → settled while mounted: a parent-row turn keeps its expand state
// (the header morphs in place); a headerless expanded turn folds (animated
// via CSS unless reduced motion).
if (item.settled !== prevSettled) {
setPrevSettled(item.settled);
if (item.settled && !wasParentRow) setOpen(false);
}
const expandable = foldedItems.length > 0;
const folded = (item.settled || parentRow) && !open;
const SummaryIcon = item.summary.failed ? X : Check;
const showSettledHeader = item.settled && (!item.historical || expandable);
const header = showSettledHeader ? (
<button
type="button"
onClick={() => setOpen((o) => !o)}
aria-expanded={open}
className={cn(
"group flex w-full items-center gap-2 px-1 text-muted-foreground transition-colors hover:text-foreground",
item.standaloneHeader
? "border-b border-border/70 py-2 text-sm"
: "py-0.5 text-xs",
streamlined && "min-w-0",
)}
data-testid="task-chat-turn-summary"
>
{timestampPrefix ? (
<>
<span className="text-(length:--text-micro)">{timestampPrefix}</span>
<span aria-hidden className="text-(length:--text-micro)">
·
</span>
</>
) : null}
{!item.standaloneHeader && !item.historical ? (
<SummaryIcon className="h-3.5 w-3.5 shrink-0" />
) : null}
<span>
{item.historical ? "Activity" : item.standaloneHeader && item.summary.durationLabel
? `${item.summary.failed ? "Stopped" : "Worked"} for ${item.summary.durationLabel}`
: item.summary.failed
? "Stopped"
: "Worked"}
</span>
{!item.standaloneHeader && turnSummaryMetrics(item.summary) ? (
// Time/tools/tokens is demoted, not deleted (PAP-502): it stays in the
// DOM (and the accessible tree) but fades in only on hover/focus so the
// settled line reads as "2:34 PM · ✓ Worked" at rest. Revealed too when
// the fold is open, so the metrics don't vanish while you read below.
<span
className="tc-turn-metrics"
data-visible={open ? "true" : "false"}
>
<span className="min-w-0 overflow-hidden whitespace-nowrap font-mono text-(length:--text-micro)">
{turnSummaryMetrics(item.summary)}
</span>
</span>
) : null}
<ChevronRight
className={cn(
"h-3 w-3 shrink-0 transition-transform",
item.standaloneHeader && "ml-auto",
open ? "rotate-90" : null,
)}
aria-hidden
/>
</button>
) : parentRow ? (
// The pill renders the expand button itself, wrapped around only the
// gerund status line — the interstitial row above stays outside the
// hover/click target (PAP-376). Without activity there is no
// chevron/button.
<TaskChatStatusPill
item={item.liveStatus!}
chevronOpen={expandable ? open : undefined}
onToggle={expandable ? () => setOpen((o) => !o) : undefined}
/>
) : null;
return (
<div
data-testid="task-chat-turn"
data-settled={item.settled ? "true" : "false"}
>
{leading ? (
streamlined ? (
<div className="flex w-full items-center justify-between gap-2">
<div className="min-w-0">{header}</div>
<div className="shrink-0">{leading}</div>
</div>
) : (
<div className="flex items-center gap-1">
{leading}
{header}
</div>
)
) : (
header
)}
<TaskChatRunHealthBanner failures={item.mcpStartupFailures ?? []} />
<div
className="tc-turn-fold"
data-folded={folded ? "true" : "false"}
aria-hidden={folded}
>
<div>
<div className="flex flex-col gap-2 pt-1">
{historyMounted && groupImageResources(foldedItems).map((child) => (
<div key={child.id}>{renderChild(child)}</div>
))}
</div>
</div>
</div>
{persistentItems.length > 0 ? (
<div
className="flex flex-col gap-2 pt-2"
data-testid="task-chat-turn-persistent-history"
>
{persistentItems.map((child) => (
<div key={child.id}>{renderChild(child)}</div>
))}
</div>
) : null}
</div>
);
}