Multiplayer agent harness for work.
Show image attachments as compact previews above message bubbles (#1510)
Regan Bell03673db62ef8
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.tsindex fe33165f7..9fda9fc3e 100644--- a/plugins/web-ui/src/chat.ts+++ b/plugins/web-ui/src/chat.ts@@ -139,7 +139,16 @@ import {harnessSupportsEffort,harnessSupportsFastMode,} from "./model-options";-import { browserRenderableImage, chipBadge, copyText, formatBytes, icon, relTime, waveLoader } from "./ui";+import {+ attachmentGallery,+ browserRenderableImage,+ chipBadge,+ copyText,+ formatBytes,+ icon,+ relTime,+ waveLoader,+} from "./ui";import { appState, renderSidebarTop, switchView, syncUrlFromState } from "./shell";import { contextsState, scopeTitle } from "./contexts";import { openProjectPage, scopeToolCount, sessionTopbarTpl, setScopedSession } from "./session-scope";@@ -1641,13 +1650,16 @@ export function createChatSurface(>${steered ? html`<div class="steer-label">↪ steered the running task</div>` : nothing}${speaker ? html`<div class="speaker-label">${speaker}</div>` : nothing}- <div class="message-bubble user-bubble ${deleted ? "deleted-bubble" : ""}">+ ${attachmentGallery(attachments, (attachment) => browserRenderableImage(attachment.mimeType), userAttachmentBadge)}+ <div+ class="message-bubble user-bubble ${deleted ? "deleted-bubble" : ""}"+ ?hidden=${!messageText(message).trim() && !edited && !deleted}+ ><div class="pin-content">${isReadOnlySlackView() ? slackWireBubble(messageText(message)) : markdown(messageText(message))}${edited || deleted ? html`<span class="revision-badge">(${deleted ? "deleted" : "edited"})</span>` : nothing}</div><button class="pin-toggle" type="button" hidden aria-expanded="false">Show more</button>- ${attachments.length ? html`<div class="message-files">${attachments.map(userAttachmentBadge)}</div>` : nothing}</div>${sendFailure@@ -2860,8 +2872,13 @@ export function createChatSurface(if (a.mimeType?.startsWith("image/")) {const dataUrl =a.content && (a.content.startsWith("data:") ? a.content : `data:${a.mimeType};base64,${a.content}`);- const download = !artifactHref || !browserRenderableImage(a.mimeType);- return chipBadge(FileImage, a.fileName, a.size, artifactHref ?? dataUrl ?? undefined, download);+ const href = artifactHref ?? localContentUrl(a) ?? dataUrl;+ if (href && browserRenderableImage(a.mimeType)) {+ return html`<a class="file-image" href=${href} target="_blank" rel="noreferrer" ${tip(a.fileName)}+ ><img src=${href} alt=${a.fileName} loading="lazy"+ /></a>`;+ }+ return chipBadge(FileImage, a.fileName, a.size, href || undefined, true);}if (inlineHtmlName(a.fileName, a.mimeType)) {let src = artifactHref;diff --git a/plugins/web-ui/src/composer.ts b/plugins/web-ui/src/composer.tsindex e349fa10d..e4b4d93bc 100644--- a/plugins/web-ui/src/composer.ts+++ b/plugins/web-ui/src/composer.ts@@ -4,8 +4,9 @@ import type { Agent, AgentMessage } from "@earendil-works/pi-agent-core";import { createFileDragState } from "./file-drag";import type { Attachment } from "@earendil-works/pi-web-ui";import { FolderDropError, folderToZipFile, isFolderReadError, splitDropItems, type DropEntryLike } from "./folder-drop";-import { html, nothing, type TemplateResult } from "lit";+import { html, nothing, render, type TemplateResult } from "lit";import { live } from "lit/directives/live.js";+import { repeat } from "lit/directives/repeat.js";import { ArrowUp, Box, CornerDownRight, FileText, Paperclip, Square, X } from "lucide";import {api,@@ -28,7 +29,7 @@ import {type QueuedRun,} from "./core-bridge";import { errMessage } from "../../chassis/src/errors";-import { fieldSelect, icon } from "./ui";+import { browserRenderableImage, fieldSelect, icon } from "./ui";import {defaultEffortForModel,defaultModelValue,@@ -519,9 +520,29 @@ export function createComposerSurface(ctx: ConvCtx): ComposerSurface {composerState.attachments.length? html`<div class="attachment-strip">- ${composerState.attachments.map(+ ${repeat(+ composerState.attachments,+ (a) => a.id,(a) => html`- <span class="file-chip">+ <span class=${browserRenderableImage(a.mimeType) ? "file-chip composer-image" : "file-chip"}>+ ${+ browserRenderableImage(a.mimeType)+ ? html`<button+ type="button"+ class="composer-image-open"+ aria-label=${`Preview ${a.fileName}`}+ @click=${() => openImagePreview(a)}+ >+ <img+ src=${a.content.startsWith("data:") ? a.content : `data:${a.mimeType};base64,${a.content}`}+ alt=${a.fileName}+ @error=${(event: Event) => {+ (event.currentTarget as HTMLImageElement).parentElement!.hidden = true;+ }}+ />+ </button>`+ : nothing+ }${pastedTextIds.has(a.id)? html`@@ -536,7 +557,11 @@ export function createComposerSurface(ctx: ConvCtx): ComposerSurface {<span>${pasteChipLabel(a.extractedText?.length ?? 0)}</span></button>`- : html`${icon(Paperclip, 14)}<span dir="auto">${a.fileName}</span>`+ : html`${browserRenderableImage(a.mimeType) ? nothing : icon(Paperclip, 14)}<span+ dir="auto"+ title=${a.fileName}+ >${a.fileName}</span+ >`}<buttontype="button"@@ -1660,6 +1685,43 @@ export function createComposerSurface(ctx: ConvCtx): ComposerSurface {ctx.chat.state.host?.querySelector<HTMLInputElement>(".file-input")?.click();}+ function openImagePreview(attachment: Attachment): void {+ const dialog = document.createElement("dialog");+ dialog.className = "project-dialog attachment-preview";+ dialog.setAttribute("aria-label", attachment.fileName);+ dialog.addEventListener(+ "close",+ () => {+ render(nothing, dialog);+ dialog.remove();+ },+ { once: true },+ );+ document.body.append(dialog);+ const src = attachment.content.startsWith("data:")+ ? attachment.content+ : `data:${attachment.mimeType};base64,${attachment.content}`;+ render(+ html`+ <div class="attachment-preview-head">+ <span dir="auto">${attachment.fileName}</span>+ <button type="button" class="btn compact" @click=${() => dialog.close()}>Close</button>+ </div>+ <div class="attachment-preview-body">+ <button+ type="button"+ aria-label="Toggle actual image size"+ @click=${(event: Event) => (event.currentTarget as HTMLElement).classList.toggle("actual-size")}+ >+ <img src=${src} alt=${attachment.fileName} />+ </button>+ </div>+ `,+ dialog,+ );+ dialog.showModal();+ }+function removeAttachment(id: string, agent: Agent): void {composerState.attachments = composerState.attachments.filter((a) => a.id !== id);pastedTextIds.delete(id);diff --git a/plugins/web-ui/src/shared-session.ts b/plugins/web-ui/src/shared-session.tsindex 9e3c07872..335a82564 100644--- a/plugins/web-ui/src/shared-session.ts+++ b/plugins/web-ui/src/shared-session.ts@@ -1,12 +1,12 @@import "./shell.css";import "@mariozechner/mini-lit/dist/ThemeToggle.js";import { html, render } from "lit";-import { Lock, ArrowUpRight, Check, Copy, File, FileImage } from "lucide";+import { Lock, ArrowUpRight, Check, Copy, File } from "lucide";import { createTranscriptViewport } from "./transcript-viewport";import { decorateTextCodeBlocks } from "./text-code";import { markdown } from "./message-markdown";import { installMarkdownSanitizer } from "./markdown-sanitize";-import { brandName, brandMark, chipBadge, icon, copyText } from "./ui";+import { brandName, brandMark, attachmentGallery, chipBadge, icon, copyText } from "./ui";interface SharedTranscript {createdAt: number;@@ -18,6 +18,10 @@ interface SharedTranscript {}>;}+function sharedInlineImage(mimeType: string): boolean {+ return /^image\/(png|jpeg|gif|webp|avif)$/.test(mimeType);+}+installMarkdownSanitizer({ shared: true });const transcript: SharedTranscript | null = JSON.parse(document.getElementById("shared-transcript")!.textContent!);const base = (import.meta as unknown as { env: { BASE_URL: string } }).env.BASE_URL;@@ -41,46 +45,39 @@ render(<div class="message-stack">${transcript- ? transcript.messages.map(- (message) => html`+ ? transcript.messages.map((message) => {+ const files = attachmentGallery(+ message.attachments ?? [],+ (file) => sharedInlineImage(file.mimetype),+ (file) => {+ const href = `${location.pathname}/files/${encodeURIComponent(file.id)}`;+ const inlineImage = sharedInlineImage(file.mimetype);+ if (inlineImage) {+ return html`<a class="file-image" href=${`${href}?inline=1`} target="_blank" rel="noreferrer"+ ><img src=${`${href}?inline=1`} alt=${file.name} loading="lazy"+ /></a>`;+ }+ return chipBadge(File, file.name, file.sizeBytes, href, true);+ },+ );+ return html`<article class=${`message-row ${message.role}-row`}>- <div class=${message.role === "user" ? "message-bubble user-bubble" : "assistant-body"}>+ ${message.role === "user" ? files : ""}+ <div+ class=${message.role === "user" ? "message-bubble user-bubble" : "assistant-body"}+ ?hidden=${message.role === "user" && !message.text.trim()}+ ><div class=${message.role === "user" ? "pin-content" : "shared-message-content"}>${markdown(message.text)}</div>${message.role === "user" ? html`<button class="pin-toggle" type="button" hidden aria-expanded="false">Show more</button>` : ""}- ${- message.attachments?.length- ? html`<div class="message-files">- ${message.attachments.map((file) => {- const href = `${location.pathname}/files/${encodeURIComponent(file.id)}`;- const inlineImage = /^image\/(png|jpeg|gif|webp|avif)$/.test(file.mimetype);- if (inlineImage && message.role !== "user") {- return html`<a- class="file-image"- href=${href}- download=${file.name}- rel="noreferrer"- ><img src=${`${href}?inline=1`} alt=${file.name} loading="lazy"- /></a>`;- }- return chipBadge(- inlineImage ? FileImage : File,- file.name,- file.sizeBytes,- inlineImage ? `${href}?inline=1` : href,- !inlineImage,- );- })}- </div>`- : ""- }+ ${message.role === "assistant" ? files : ""}${message.role === "assistant" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}${icon(Check, 13)}</button></div>` : ""}</div>${message.role === "user" ? html`<div class="message-meta"><button class="msg-copy" aria-label="Copy message" title="Copy" @click=${(e: Event) => void copyText(message.text, e.currentTarget as HTMLButtonElement)}>${icon(Copy, 13)}${icon(Check, 13)}</button></div>` : ""}</article>- `,- )+ `;+ }): html`<div class="empty-state"><h2>This link is unavailable</h2><p>The conversation is unavailable or you may not have access.</p>diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.cssindex d890aa31b..c76c7f6f3 100644--- a/plugins/web-ui/src/shell.css+++ b/plugins/web-ui/src/shell.css@@ -2069,6 +2069,94 @@ qm-markdown .tok-in {gap: 6px;margin-top: 9px;}+.attachment-strip {+ align-items: flex-start;+ max-height: 240px;+ overflow-y: auto;+ padding: 2px;+}+.file-chip.composer-image {+ position: relative;+ display: block;+ width: 80px;+ padding: 0;+ border: 0;+ background: transparent;+}+.composer-image > span {+ display: none;+}+.composer-image:has(.composer-image-open[hidden]) > span {+ display: block;+ padding: 6px 28px 6px 0;+}+.composer-image-open {+ width: 100%;+ border: 0;+ padding: 0;+ cursor: zoom-in;+ background: transparent;+}+.composer-image img {+ display: block;+ width: 100%;+ height: 80px;+ object-fit: cover;+ border-radius: 12px;+}+.composer-image-open[hidden] {+ display: none;+}+.composer-image .chip-x {+ position: absolute;+ top: 2px;+ right: 2px;+ width: 24px;+ height: 24px;+ border-radius: 50%;+ background: var(--background);+ color: var(--foreground);+}+.project-dialog.attachment-preview {+ width: min(960px, calc(100vw - 32px));+}+.attachment-preview-head {+ display: flex;+ align-items: center;+ justify-content: space-between;+ gap: 16px;+ margin-bottom: 12px;+}+.attachment-preview-head span {+ min-width: 0;+ overflow-wrap: anywhere;+}+.attachment-preview-body {+ overflow: auto;+ max-height: 70dvh;+}+.attachment-preview-body button {+ display: block;+ margin: auto;+ border: 0;+ padding: 0;+ background: var(--secondary);+ cursor: zoom-in;+}+.attachment-preview-body img {+ display: block;+ max-width: 100%;+ max-height: 70dvh;+ object-fit: contain;+}+.attachment-preview-body .actual-size {+ max-width: none;+ cursor: zoom-out;+}+.attachment-preview-body .actual-size img {+ max-width: none;+ max-height: none;+}.queued-strip,.chat-bottom-dock > .bg-activity {position: relative;@@ -2176,7 +2264,7 @@ qm-markdown .tok-in {display: inline-block;max-width: min(320px, 100%);- border: 1px solid var(--border);+ border: 0;border-radius: 8px;overflow: hidden;line-height: 0;@@ -2190,6 +2278,41 @@ qm-markdown .tok-in {height: auto;object-fit: contain;}+.attachment-gallery {+ flex-direction: column;+ max-width: 100%;+ gap: 10px;+}+.attachment-images,+.attachment-documents {+ display: flex;+ flex-wrap: wrap;+ gap: 10px;+ max-width: 100%;+}+.user-row > .attachment-gallery {+ align-items: flex-end;+ margin: 0 0 8px;+ pointer-events: auto;+}+.user-bubble[hidden] {+ display: none;+}+.user-row > .attachment-gallery > * {+ justify-content: flex-end;+}+.attachment-images > .file-image {+ flex: none;+}+.user-row > .message-files .file-image {+ width: 80px;+ border-radius: 12px;+}+.user-row > .message-files .file-image img {+ width: 100%;+ height: 80px;+ object-fit: cover;+}.file-html-unfurl {display: flex;flex-direction: column;diff --git a/plugins/web-ui/src/ui.ts b/plugins/web-ui/src/ui.tsindex 1135c6bd5..a0180f6ea 100644--- a/plugins/web-ui/src/ui.ts+++ b/plugins/web-ui/src/ui.ts@@ -331,6 +331,20 @@ const RENDERABLE_IMAGE_TYPES = new Set(["image/x-ms-bmp",]);+export function attachmentGallery<T>(+ attachments: readonly T[],+ isImage: (attachment: T) => boolean,+ renderAttachment: (attachment: T) => TemplateResult,+): TemplateResult | typeof nothing {+ if (!attachments.length) return nothing;+ const images = attachments.filter(isImage);+ const documents = attachments.filter((attachment) => !isImage(attachment));+ return html`<div class="message-files attachment-gallery">+ ${images.length ? html`<div class="attachment-images">${images.map(renderAttachment)}</div>` : nothing}+ ${documents.length ? html`<div class="attachment-documents">${documents.map(renderAttachment)}</div>` : nothing}+ </div>`;+}+export function browserRenderableImage(mimeType?: string): boolean {return RENDERABLE_IMAGE_TYPES.has((mimeType ?? "").split(";")[0]!.trim().toLowerCase());}diff --git a/plugins/web-ui/test/user-message-cap.test.ts b/plugins/web-ui/test/user-message-cap.test.tsindex 1ef0bb1e0..698b5265a 100644--- a/plugins/web-ui/test/user-message-cap.test.ts+++ b/plugins/web-ui/test/user-message-cap.test.ts@@ -25,25 +25,20 @@ test("the scroller is the size container the cap measures", () => {assert.doesNotMatch(chat, /--chat-viewport/);});-test("images a user attached render as chips, not inline, in the live chat", () => {+test("user images have inline previews before and after persistence", () => {const fn = chat.match(/function userAttachmentBadge\([\s\S]*?\n {2}\}/)?.[0] ?? "";- assert.match(fn, /startsWith\("image\/"\)/);- assert.match(- fn,- /return chipBadge\(FileImage, a\.fileName, a\.size, artifactHref \?\? dataUrl \?\? undefined, download\);/,- );- assert.doesNotMatch(fn, /<img/);- assert.match(fn, /const download = !artifactHref \|\| !browserRenderableImage\(a\.mimeType\);/);+ assert.match(fn, /artifactHref \?\? localContentUrl\(a\) \?\? dataUrl/);+ assert.match(fn, /href && browserRenderableImage\(a\.mimeType\)/);+ assert.match(fn, /<img src=\$\{href\} alt=\$\{a\.fileName\}/);+ assert.match(fn, /return chipBadge\(FileImage/);});-test("images a user attached render as chips that open inline on the share page", () => {- const files = shared.match(/message\.attachments\.map\(\(file\) => \{[\s\S]*?\n\s*\}\)\}/)?.[0] ?? "";- assert.match(files, /if \(inlineImage && message\.role !== "user"\) \{\s*return html`<a\s+class="file-image"/);- assert.equal(files.match(/<img/g)?.length, 1);- assert.match(- files,- /return chipBadge\(\s*inlineImage \? FileImage : File,\s*file\.name,\s*file\.sizeBytes,\s*inlineImage \? `\$\{href\}\?inline=1` : href,\s*!inlineImage,?\s*\);/,- );+test("shared images open the original through the inline endpoint", () => {+ assert.match(shared, /const inlineImage = sharedInlineImage\(file\.mimetype\)/);+ assert.match(shared, /if \(inlineImage\) \{/);+ assert.doesNotMatch(shared, /inlineImage && message.role/);+ assert.match(shared, /href=\$\{`\$\{href\}\?inline=1`\} target="_blank"/);+ assert.doesNotMatch(shared, /download=\$\{file.name\}/);});test("both transcript renderers provide an accessible control and an observable inner body", () => {