Files
PaperClipAI/ui/src/components/task-chat/task-chat-attachments.ts
T
DottaandPaperclip 87d05e194b feat(work-products): add rich cards and run artifact inventory (#12717)
## Thinking Path

> - Paperclip is the control plane for AI-agent companies.
> - Agent outputs must remain visible after a run and easy to inspect
from a task.
> - The thread and artifact inventory need one consistent rich-card
vocabulary.
> - Run uploads also need durable artifact registration and
producing-run context.
> - Reviewers need deterministic examples for each rich-card kind and
state.
> - This pull request adds the shared presentation, registration,
inventory, and Storybook review coverage.
> - The benefit is a complete output path that reviewers can inspect
without seeded data.

## Linked Issues or Issue Description

**What existing behavior does this improve?**

This change improves work-product presentation in task threads and the
task Artifacts tab.

**Subsystem affected**

The change affects shared work-product contracts, the runner diff path,
server attachment and work-product services, GitHub metadata refresh,
the React board UI, and Storybook.

**Current behavior**

The thread used generic cards. Some files uploaded by a run existed only
as message attachments. The Artifacts tab showed a flat list without run
context or filters. Storybook showed only one resting card per kind.

**Proposed behavior**

The thread uses rich cards for supported work-product types. Each
run-produced file registers one attachment-backed artifact work product.
The Artifacts tab groups outputs by run and supports filters. Storybook
shows every kind and requested state, PR lifecycle states, stats
variants, truncation, mobile layout, and message-tail media.

**Reason and benefit**

Users can identify outputs quickly. Reviewers can inspect all card
permutations without creating task data.

**Breaking changes**

None. The metadata fields and automatic artifact registration are
additive. Existing attachments and work products keep their current
behavior.

## What Changed

- Added a shared rich work-product card with kind-specific content and a
compact inventory variant.
- Added pull-request and commit diff metadata plus bounded GitHub state
refresh.
- Added media strips and typed file chips to message-tail attachments.
- Registered each run-produced attachment as an artifact work product in
the same server transaction.
- Grouped task artifacts by run with agent and timestamp headings.
- Added type and run filters, image thumbnails, compact cards, and a
company Artifacts link.
- Added a Storybook kind-by-state matrix with stats variants for all
eight visual kinds.
- Added PR open, draft, merged, and closed examples, long-title
truncation, an exact 375-pixel viewport, and message-tail overflow
coverage.
- Closed reconciled runtime work products when the linked runtime stops
or disappears, so the card shows `Stopped` instead of `Unhealthy`.

### Screenshots

Before: one resting card per kind.

![Previous rich-card
inventory](https://pages.paperclip.ing/rich-work-product-storybook-20260902/before-inventory.png)

After: the kind and state matrix.

![Rich-card kind and state
matrix](https://pages.paperclip.ing/rich-work-product-storybook-20260902/after-kind-state-matrix.png)

After: message-tail media at 375 pixels.

![Message-tail thumbnails and typed
chips](https://pages.paperclip.ing/rich-work-product-storybook-20260902/after-message-tail.png)

[Open the Storybook evidence
viewer](https://pages.paperclip.ing/rich-work-product-storybook-20260902/).

The earlier artifact inventory comparison remains available in the
[artifact inventory
viewer](https://pages.paperclip.ing/rich-artifacts-inventory-proof-20260902/).

## Verification

- `pnpm --filter @paperclipai/ui typecheck` passed.
- `pnpm check:token-gates` passed.
- `pnpm build-storybook` passed.
- `pnpm exec vitest run
server/src/__tests__/work-product-runtime-reconciliation.test.ts` passed
with 5 tests.
- Chromium visual checks passed at desktop and 375-pixel widths.
- All 30 latest-head GitHub checks passed. One unrelated annotation test
was flaky and passed on its single retry.
- Greptile passed at 5/5 with zero unresolved threads.

## Risks

- Low risk. The Storybook change adds review fixtures only. The runtime
fix changes read-time reconciliation without database writes.
- The matrix is intentionally large so every permutation stays visible
in one review surface.

> I checked `ROADMAP.md`. This work does not duplicate planned core
work.

## Model Used

- OpenAI Codex with GPT-5 and GPT-5.6-sol across this pull request.
Reasoning, tool use, and code execution were enabled. The context-window
size is not exposed.

## 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 public branch name describes the change and contains no
internal task id
- [x] I have run tests locally and the changed-path tests 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 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-09-02 15:27:54 -05:00

278 lines
9.4 KiB
TypeScript

/**
* Attachment-chip helpers for the task-chat redesign (PAP-351): map a filename
* to a kind icon + label for the shadcn base/attachment chips, and extract the
* non-image file references the composer writes into message bodies
* ("[name](/api/attachments/<id>/content)") so posted bubbles can render them
* as the same chips instead of bare links.
*/
import {
FileArchive,
FileAudio,
FileCode,
FileSpreadsheet,
FileText,
FileVideo,
File as FileIcon,
} from "lucide-react";
import type { LucideIcon } from "lucide-react";
import type { IssueAttachment } from "@paperclipai/shared";
export interface FileKind {
icon: LucideIcon;
/** Short kind label for the chip description, e.g. "PDF", "ZIP". */
label: string;
}
const IMAGE_EXTENSIONS = new Set([
"png",
"jpg",
"jpeg",
"gif",
"webp",
"svg",
"avif",
"bmp",
"ico",
"heic",
]);
const KIND_BY_EXTENSION: Record<string, FileKind> = {
pdf: { icon: FileText, label: "PDF" },
doc: { icon: FileText, label: "Doc" },
docx: { icon: FileText, label: "Doc" },
txt: { icon: FileText, label: "Text" },
md: { icon: FileText, label: "Markdown" },
rtf: { icon: FileText, label: "Text" },
csv: { icon: FileSpreadsheet, label: "CSV" },
tsv: { icon: FileSpreadsheet, label: "TSV" },
xls: { icon: FileSpreadsheet, label: "Sheet" },
xlsx: { icon: FileSpreadsheet, label: "Sheet" },
zip: { icon: FileArchive, label: "ZIP" },
gz: { icon: FileArchive, label: "Archive" },
tar: { icon: FileArchive, label: "Archive" },
tgz: { icon: FileArchive, label: "Archive" },
rar: { icon: FileArchive, label: "Archive" },
"7z": { icon: FileArchive, label: "Archive" },
mp3: { icon: FileAudio, label: "Audio" },
wav: { icon: FileAudio, label: "Audio" },
m4a: { icon: FileAudio, label: "Audio" },
ogg: { icon: FileAudio, label: "Audio" },
mp4: { icon: FileVideo, label: "Video" },
mov: { icon: FileVideo, label: "Video" },
webm: { icon: FileVideo, label: "Video" },
json: { icon: FileCode, label: "JSON" },
yaml: { icon: FileCode, label: "YAML" },
yml: { icon: FileCode, label: "YAML" },
xml: { icon: FileCode, label: "XML" },
html: { icon: FileCode, label: "HTML" },
css: { icon: FileCode, label: "CSS" },
js: { icon: FileCode, label: "Code" },
jsx: { icon: FileCode, label: "Code" },
ts: { icon: FileCode, label: "Code" },
tsx: { icon: FileCode, label: "Code" },
py: { icon: FileCode, label: "Code" },
rb: { icon: FileCode, label: "Code" },
go: { icon: FileCode, label: "Code" },
rs: { icon: FileCode, label: "Code" },
sh: { icon: FileCode, label: "Code" },
sql: { icon: FileCode, label: "SQL" },
log: { icon: FileText, label: "Log" },
patch: { icon: FileCode, label: "Patch" },
diff: { icon: FileCode, label: "Patch" },
};
function extensionOf(name: string): string {
const match = /\.([a-z0-9]+)$/i.exec(name.trim());
return match ? match[1].toLowerCase() : "";
}
export function isImageFilename(name: string): boolean {
return IMAGE_EXTENSIONS.has(extensionOf(name));
}
type AttachmentRecord = Pick<
IssueAttachment,
| "id"
| "contentPath"
| "openPath"
| "downloadPath"
| "contentType"
| "byteSize"
| "originalFilename"
>;
function normalizedContentType(contentType: string | undefined): string {
return (contentType ?? "").toLowerCase().split(";")[0]?.trim() ?? "";
}
/** MIME type is authoritative; filenames remain a fallback for legacy refs. */
export function isImageAttachment(ref: AttachmentRef): boolean {
const contentType = normalizedContentType(ref.contentType);
if (contentType.startsWith("image/")) return true;
if (contentType && contentType !== "application/octet-stream") return false;
return isImageFilename(ref.name) || isImageFilename(ref.url.split("?")[0]);
}
/** Kind icon + short label for a filename; unknown extensions get File/"File". */
export function fileKindForName(name: string): FileKind {
return KIND_BY_EXTENSION[extensionOf(name)] ?? { icon: FileIcon, label: "File" };
}
export function fileKindForAttachment(ref: AttachmentRef): FileKind {
const byName = fileKindForName(ref.name);
if (byName.label !== "File") return byName;
const contentType = normalizedContentType(ref.contentType);
if (contentType === "application/pdf") return { icon: FileText, label: "PDF" };
if (contentType === "application/json" || contentType.endsWith("+json")) {
return { icon: FileCode, label: "JSON" };
}
if (contentType === "text/csv" || contentType === "application/csv") {
return { icon: FileSpreadsheet, label: "CSV" };
}
if (contentType.startsWith("text/")) return { icon: FileText, label: "Text" };
if (contentType.startsWith("audio/")) return { icon: FileAudio, label: "Audio" };
if (contentType.startsWith("video/")) return { icon: FileVideo, label: "Video" };
if (contentType.includes("zip") || contentType.includes("archive")) {
return { icon: FileArchive, label: "Archive" };
}
return byName;
}
/** "2.4 MB"-style size for chip descriptions (same tiers as IssueChatThread). */
export function formatFileSize(bytes: number | undefined): string {
if (bytes === undefined || !Number.isFinite(bytes) || bytes <= 0) return "";
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
export interface AttachmentRef {
name: string;
url: string;
id?: string;
contentType?: string;
byteSize?: number;
openPath?: string;
downloadPath?: string;
}
function attachmentIdFromUrl(url: string): string | null {
return /\/api\/(?:attachments|assets)\/([^/?]+)\/content/.exec(url)?.[1] ?? null;
}
export function hydrateAttachmentRefs(
refs: AttachmentRef[],
attachments: AttachmentRecord[],
): AttachmentRef[] {
const byId = new Map(attachments.map((attachment) => [attachment.id, attachment]));
const byPath = new Map(
attachments.flatMap((attachment) => [
[attachment.contentPath, attachment] as const,
...(attachment.openPath ? [[attachment.openPath, attachment] as const] : []),
]),
);
return refs.map((ref) => {
const cleanUrl = ref.url.split("?")[0];
const record =
byPath.get(cleanUrl) ??
byId.get(attachmentIdFromUrl(ref.url) ?? "");
if (!record) return ref;
return {
...ref,
id: record.id,
name: record.originalFilename?.trim() || ref.name,
contentType: record.contentType,
byteSize: record.byteSize,
openPath: record.openPath,
downloadPath: record.downloadPath,
};
});
}
/**
* Markdown links (not `![]` embeds) pointing at attachment/asset content. The
* label admits backslash-escaped characters — the composer escapes `[`/`]` in
* filenames when inserting references.
*/
const ATTACHMENT_LINK_RE =
/(?<!!)\[((?:\\.|[^\]\\])+)\]\((\/api\/(?:attachments|assets)\/[^()\s]+\/content(?:\?[^()\s]*)?)\)/g;
/** Markdown image embeds (`![name](url)`), same escaped-label grammar. */
const IMAGE_EMBED_RE = /!\[((?:\\.|[^\]\\])*)\]\(([^()\s]+)\)/g;
function isStandaloneImageEmbedLine(line: string): boolean {
return (
line.replace(IMAGE_EMBED_RE, "").trim().length === 0 && line.trim().length > 0
);
}
/**
* Images on standalone lines, in document order, deduped by URL. Images woven
* into prose stay inline so they are not also duplicated in the media strip.
* Feeds the bubble's lightbox: the refs become the gallery items and the
* clicked <img> src picks the initial index.
*/
export function extractImageRefs(body: string): AttachmentRef[] {
const refs: AttachmentRef[] = [];
const seen = new Set<string>();
for (const line of body.split("\n")) {
if (!isStandaloneImageEmbedLine(line)) continue;
for (const match of line.matchAll(IMAGE_EMBED_RE)) {
const [, name, url] = match;
if (seen.has(url)) continue;
seen.add(url);
refs.push({ name: name.replace(/\\([[\]])/g, "$1"), url });
}
}
return refs;
}
/** Remove standalone image-embed lines after promoting them to the media strip. */
export function stripStandaloneImageEmbeds(body: string): string {
return body
.split("\n")
.filter((line) => !isStandaloneImageEmbedLine(line))
.join("\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
}
export interface ExtractedAttachmentRefs {
refs: AttachmentRef[];
/** Body with lines that were nothing but extracted links removed. */
text: string;
}
/**
* Pull non-image attachment references out of a message body. Every matching
* link becomes a chip; a line is stripped from the rendered body only when it
* contains nothing but extracted links (the shape the composer inserts), so
* links woven into prose keep their sentence.
*/
export function extractAttachmentRefs(body: string): ExtractedAttachmentRefs {
const refs: AttachmentRef[] = [];
const seen = new Set<string>();
for (const match of body.matchAll(ATTACHMENT_LINK_RE)) {
const [, name, url] = match;
if (seen.has(url)) continue;
seen.add(url);
refs.push({ name: name.replace(/\\([[\]])/g, "$1"), url });
}
if (refs.length === 0) return { refs, text: body };
const kept = body.split("\n").filter((line) => {
const withoutLinks = line.replace(ATTACHMENT_LINK_RE, (full, name: string, url: string) => {
return seen.has(url) ? "" : full;
});
return withoutLinks.trim().length > 0 || line.trim().length === 0;
});
// Collapse the blank run left behind where a link-only line was removed.
const text = kept
.join("\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
return { refs, text };
}