Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
feat(chat): preview staged screenshots before sending (#2128)
Maxf76509e14404
diff --git a/ui/src/components/ui/image-viewer-context.ts b/ui/src/components/ui/image-viewer-context.tsindex 684014876..b0067d937 100644--- a/ui/src/components/ui/image-viewer-context.ts+++ b/ui/src/components/ui/image-viewer-context.ts@@ -5,13 +5,17 @@ import * as React from 'react';// ``useImageViewer()`` returns null where no provider is mounted (e.g. the// agent-config editor preview), which makes the click a documented no-op.-// ``isolated`` opens the image on its own: the lightbox shows exactly that one-// src and never pages, whatever the session gallery happens to contain. A caller-// outside the transcript (the queue strip) needs that as a *stated* property —-// its URL not being in ``images`` today is an accident of when the message is-// sent, and would silently stop holding the moment the same file also appears in-// the transcript, or the queue flushes while the viewer is open.-export type ImageViewerOpenOptions = { isolated?: boolean };+// A caller outside the transcript opens its own images, and what it pages+// through has to be a *stated* property of the open call: which of its URLs the+// session gallery happens to hold is an accident of when a message is sent, and+// would change under an open viewer the moment the queue flushes.+//+// ``gallery`` names that set — the composer's staged attachments, in attachment+// order — and the viewer pages within exactly it, never the transcript's images.+// ``isolated`` is the one-image case of the same idea (the queue strip): shown on+// its own, never paging, whatever the session gallery contains. Setting both is a+// contradiction; ``isolated`` wins.+export type ImageViewerOpenOptions = { isolated?: boolean; gallery?: string[] };type ImageViewerContextValue = { open: (src: string, options?: ImageViewerOpenOptions) => void };diff --git a/ui/src/components/ui/image-viewer.test.tsx b/ui/src/components/ui/image-viewer.test.tsxindex f74e86daf..5ae597f65 100644--- a/ui/src/components/ui/image-viewer.test.tsx+++ b/ui/src/components/ui/image-viewer.test.tsx@@ -131,6 +131,68 @@ describe('ImageViewerProvider — paging belongs to the gallery, not to every im});});+// The other caller outside the transcript: the composer stages images that are+// not in the session gallery yet, and previewing one before sending must page+// through those and only those.+describe('ImageViewerProvider — a caller-named gallery is the one it pages', () => {+ const STAGED = ['/api/media/staged_1', '/api/media/staged_2'];++ const mountStaged = (images: string[] = GALLERY) =>+ render(+ <I18nextProvider i18n={i18n}>+ <ImageViewerProvider images={images}>+ <Opener src={STAGED[0]} options={{ gallery: STAGED }} />+ </ImageViewerProvider>+ </I18nextProvider>,+ );++ const shown = () => document.querySelector('[role="dialog"] img')?.getAttribute('src');++ it('pages within the named set instead of the session gallery', () => {+ mountStaged();+ fireEvent.click(screen.getByText('open'));++ expect(screen.getByText('1 / 2')).toBeTruthy();+ fireEvent.keyDown(window, { key: 'ArrowRight' });+ expect(shown()).toBe(STAGED[1]);+ // Wrapping stays in the named set rather than continuing into the gallery.+ fireEvent.keyDown(window, { key: 'ArrowRight' });+ expect(shown()).toBe(STAGED[0]);+ });++ it('keeps its set when the session gallery changes underneath it', () => {+ const view = mountStaged([]);+ fireEvent.click(screen.getByText('open'));++ view.rerender(+ <I18nextProvider i18n={i18n}>+ <ImageViewerProvider images={GALLERY}>+ <Opener src={STAGED[0]} options={{ gallery: STAGED }} />+ </ImageViewerProvider>+ </I18nextProvider>,+ );++ expect(screen.getByText('1 / 2')).toBeTruthy();+ fireEvent.keyDown(window, { key: 'ArrowRight' });+ expect(shown()).toBe(STAGED[1]);+ });++ it('shows a single staged image on its own', () => {+ render(+ <I18nextProvider i18n={i18n}>+ <ImageViewerProvider images={GALLERY}>+ <Opener src={STAGED[0]} options={{ gallery: [STAGED[0]] }} />+ </ImageViewerProvider>+ </I18nextProvider>,+ );+ fireEvent.click(screen.getByText('open'));++ expect(screen.getByRole('dialog')).toBeTruthy();+ expect(screen.queryByLabelText('Next')).toBeNull();+ expect(screen.queryByLabelText('Previous')).toBeNull();+ });+});+describe('ImageViewerProvider — retained route activity owns global keys', () => {const retained = (active: boolean, options?: ImageViewerOpenOptions) => (<I18nextProvider i18n={i18n}>diff --git a/ui/src/components/ui/image-viewer.tsx b/ui/src/components/ui/image-viewer.tsxindex bda4bee41..2dca26dcb 100644--- a/ui/src/components/ui/image-viewer.tsx+++ b/ui/src/components/ui/image-viewer.tsx@@ -11,9 +11,12 @@ import { ImageViewerContext, type ImageViewerOpenOptions } from './image-viewer-// A session-scoped image lightbox. ChatPage computes the ordered list of media-// proxy image URLs in the transcript and wraps the page in a provider; any chat// image (markdown inline image or a user attachment) opens it via ``open(src)``,-// and the lightbox pages left/right through the whole session's images. Used-// through the optional context so the shared Markdown renderer keeps working-// (no-op) where there's no provider (e.g. the agent-config editor preview).+// and the lightbox pages left/right through the whole session's images. A caller+// whose images are not the transcript's — the composer's staged attachments, the+// queue strip's single file — names its own set on the open call instead (see+// ``ImageViewerOpenOptions``). Used through the optional context so the shared+// Markdown renderer keeps working (no-op) where there's no provider (e.g. the+// agent-config editor preview).// Overlay controls sit on a dark backdrop, so the shared Button's themed// foreground/hover (tuned for app surfaces) would be invisible here — override@@ -33,16 +36,23 @@ export const ImageViewerProvider: React.FC<{ images: string[]; children: React.R// isn't in the list (shouldn't happen for our own clean proxy URLs, but be// safe) still shows exactly what was clicked — paging just turns off for it.//- // ``isolated`` is recorded at open time rather than re-derived per render, so- // an isolated view stays isolated for its whole life: the gallery growing to- // include this src (the queue flushing into the transcript, say) cannot hand- // it paging controls it was opened without.- const [view, setView] = React.useState<{ src: string; isolated: boolean } | null>(null);+ // The set this view pages through is recorded at open time rather than+ // re-derived per render, so it stays the set the opener named for the view's+ // whole life: the session gallery growing to include this src (the queue+ // flushing into the transcript, say) cannot hand a caller-owned view paging it+ // was opened without. ``null`` means the session gallery — the transcript's own+ // images, deliberately still live so streaming keeps extending it.+ const [view, setView] = React.useState<{ src: string; gallery: string[] | null } | null>(null);const src = view?.src ?? null;const open = React.useCallback((next: string, options?: ImageViewerOpenOptions) =>- setView({ src: next, isolated: options?.isolated === true }),+ setView({+ src: next,+ // One mechanism, three cases: an explicitly named set, the one-image set+ // ``isolated`` asks for, or the session gallery when the caller names none.+ gallery: options?.isolated === true ? [next] : options?.gallery ?? null,+ }),[],);const close = React.useCallback(() => setView(null), []);@@ -61,17 +71,22 @@ export const ImageViewerProvider: React.FC<{ images: string[]; children: React.R// close hit-test must also require the click to be inside the visible stage.const stageRef = React.useRef<HTMLDivElement>(null);- // An isolated view is deliberately not looked up in the gallery at all, so it- // reports no index — which turns off the arrows, the counter, and the keyboard- // paging below through the one condition they already share.- const index = src && !view?.isolated ? images.indexOf(src) : -1;- const pageable = index >= 0 && images.length > 1;+ // Everything about paging reads from this one set: the arrows, the counter and+ // the keyboard handler below share the single condition it produces. A+ // one-image set (what ``isolated`` opens as) is therefore not pageable by any+ // of them, without a second rule that could drift from the first.+ const gallery = view?.gallery ?? images;+ const index = src ? gallery.indexOf(src) : -1;+ const pageable = index >= 0 && gallery.length > 1;const step = React.useCallback((delta: number) => {- if (index < 0 || images.length === 0) return;- setView({ src: images[(index + delta + images.length) % images.length], isolated: false });+ if (!pageable) return;+ const next = gallery[(index + delta + gallery.length) % gallery.length];+ // Keep the view's own set: paging within a caller-owned gallery must not+ // quietly hand the next image back to the session gallery.+ setView((cur) => (cur ? { ...cur, src: next } : cur));},- [index, images],+ [index, gallery, pageable],);// The lightbox is a modal while its route surface is foreground: it OWNS@@ -254,7 +269,7 @@ export const ImageViewerProvider: React.FC<{ images: string[]; children: React.R</div>{pageable && (<span className="absolute bottom-4 left-1/2 z-10 -translate-x-1/2 rounded-full bg-white/10 px-3 py-1 font-mono text-[11px] text-white">- {index + 1} / {images.length}+ {index + 1} / {gallery.length}</span>)}</div>diff --git a/ui/src/components/workbench/Composer.attachmentPreview.test.tsx b/ui/src/components/workbench/Composer.attachmentPreview.test.tsxnew file mode 100644index 000000000..83158ddc1--- /dev/null+++ b/ui/src/components/workbench/Composer.attachmentPreview.test.tsx@@ -0,0 +1,219 @@+/* @vitest-environment jsdom */++// Inspecting a screenshot BEFORE it is sent (#2100). The composer's chips are+// the only place a staged image exists, so the thumbnail is the trigger — and+// what it opens is the staged set alone: the transcript's gallery sits in the+// same provider and must never page into a pre-send preview.++import { createInstance } from 'i18next';+import { createRef, type ReactElement } from 'react';+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';+import userEvent from '@testing-library/user-event';+import { I18nextProvider, initReactI18next } from 'react-i18next';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++vi.hoisted(() => {+ Object.defineProperty(window, 'matchMedia', {+ configurable: true,+ value: vi.fn().mockReturnValue({ matches: false }),+ });+});++import { ToastProvider } from '../../context/ToastProvider';+import en from '../../i18n/en.json';+import { ImageViewerProvider } from '../ui/image-viewer';++const uploadWorkbenchAttachment = vi.hoisted(() => vi.fn());++vi.mock('../../lib/workbenchUpload', async (importOriginal) => ({+ ...await importOriginal<typeof import('../../lib/workbenchUpload')>(),+ uploadWorkbenchAttachment,+}));++import { Composer, type ComposerAttachment, type ComposerHandle } from './Composer';++const i18n = createInstance();+void i18n.use(initReactI18next).init({+ lng: 'en',+ fallbackLng: 'en',+ resources: { en: { translation: en } },+ interpolation: { escapeValue: false },+});++// An image already in the transcript. The chat page feeds these to the provider,+// so every assertion about "only the staged ones" has something to exclude.+const TRANSCRIPT = ['/api/media/transcript_1'];++const previewLabel = (name: string) => en.chat.compose.previewAttachment.replace('{{name}}', name);+const mediaUrl = (name: string) => `/api/media/${name}`;+const imageFile = (name: string) => new File(['png-bytes'], name, { type: 'image/png' });++const dialogImage = () => screen.getByRole('dialog').querySelector('img')?.getAttribute('src');++const onSend = vi.fn();+const ref = createRef<ComposerHandle>();++const providers = (ui: ReactElement) => (+ <I18nextProvider i18n={i18n}>+ <ToastProvider>+ <ImageViewerProvider images={TRANSCRIPT}>{ui}</ImageViewerProvider>+ </ToastProvider>+ </I18nextProvider>+);++const renderComposer = () => render(providers(+ <Composer ref={ref} sessionId="ses-1" onSend={onSend} />,+));++// Stage files through the same entry point the picker, paste and drop all use,+// and wait for the uploads the test's mock resolves.+const stage = async (...names: string[]) => {+ act(() => ref.current?.addFiles(names.map((name) => (+ name.endsWith('.png') ? imageFile(name) : new File(['text'], name, { type: 'text/plain' })+ ))));+ await waitFor(() => expect(uploadWorkbenchAttachment).toHaveBeenCalledTimes(names.length));+};++// Uploads run concurrently, so a test that reasons about the whole staged set+// waits for every image to reach the state that puts it in that set.+const stageImages = async (...names: string[]) => {+ await stage(...names);+ for (const name of names.filter((n) => n.endsWith('.png'))) {+ await screen.findByLabelText(previewLabel(name));+ }+};++beforeEach(() => {+ onSend.mockReset();+ uploadWorkbenchAttachment.mockReset();+ uploadWorkbenchAttachment.mockImplementation(async (_sid: string, file: File) => ({+ token: `tok-${file.name}`,+ name: file.name,+ mime: file.type,+ size: file.size,+ kind: file.type.startsWith('image/') ? 'image' : 'file',+ url: mediaUrl(file.name),+ }));+ // The lightbox's zoom stage observes its own box; jsdom reports no layout.+ vi.stubGlobal('ResizeObserver', class { observe() {} unobserve() {} disconnect() {} });+});+afterEach(() => {+ cleanup();+ vi.unstubAllGlobals();+});++describe('Composer attachment preview', () => {+ it('opens an uploaded screenshot at full size and closes again, leaving the chip staged', async () => {+ renderComposer();+ await stage('shot-a.png');++ fireEvent.click(await screen.findByLabelText(previewLabel('shot-a.png')));+ expect(dialogImage()).toBe(mediaUrl('shot-a.png'));+ // The viewer's own affordances, inherited rather than re-implemented here.+ expect(screen.getByLabelText(en.chat.viewer.zoomIn)).toBeTruthy();+ expect(screen.getByLabelText(en.chat.media.download)).toBeTruthy();++ fireEvent.click(screen.getByLabelText(en.chat.viewer.close));++ expect(screen.queryByRole('dialog')).toBeNull();+ expect(screen.getByText('shot-a.png')).toBeTruthy();+ expect(screen.getByLabelText(previewLabel('shot-a.png'))).toBeTruthy();+ expect(uploadWorkbenchAttachment).toHaveBeenCalledTimes(1);+ });++ it('pages through the staged images in attachment order and never the transcript', async () => {+ renderComposer();+ await stageImages('shot-a.png', 'notes.txt', 'shot-b.png');++ fireEvent.click(screen.getByLabelText(previewLabel('shot-a.png')));+ // Two staged images, not three attachments and not the transcript's image.+ expect(screen.getByText('1 / 2')).toBeTruthy();++ fireEvent.click(screen.getByLabelText(en.chat.viewer.next));+ expect(dialogImage()).toBe(mediaUrl('shot-b.png'));++ // Wrapping stays inside the staged set rather than falling into the gallery.+ fireEvent.click(screen.getByLabelText(en.chat.viewer.next));+ expect(dialogImage()).toBe(mediaUrl('shot-a.png'));+ expect(screen.getByRole('dialog').innerHTML).not.toContain(TRANSCRIPT[0]);+ });++ it('opens the image the thumbnail belongs to, whichever chip is clicked', async () => {+ renderComposer();+ await stageImages('shot-a.png', 'shot-b.png');++ fireEvent.click(screen.getByLabelText(previewLabel('shot-b.png')));++ expect(dialogImage()).toBe(mediaUrl('shot-b.png'));+ expect(screen.getByText('2 / 2')).toBeTruthy();+ });++ it('offers no preview while an upload is in flight', async () => {+ uploadWorkbenchAttachment.mockImplementation(() => new Promise(() => {}));+ renderComposer();+ await stage('pending.png');++ expect(screen.getByText('pending.png')).toBeTruthy();+ expect(screen.queryByLabelText(previewLabel('pending.png'))).toBeNull();+ expect(screen.getByLabelText(en.chat.compose.removeAttachment)).toBeTruthy();+ });++ it('offers no preview for a failed upload and keeps retry and remove', async () => {+ uploadWorkbenchAttachment.mockRejectedValue(new TypeError('Failed to fetch'));+ renderComposer();+ await stage('broken.png');++ await screen.findByLabelText(en.chat.compose.retryAttachment);+ expect(screen.queryByLabelText(previewLabel('broken.png'))).toBeNull();+ expect(screen.getByLabelText(en.chat.compose.removeAttachment)).toBeTruthy();+ });++ it('leaves a non-image attachment with no preview action', async () => {+ renderComposer();+ await stage('notes.txt');++ await waitFor(() => expect(screen.getByText('notes.txt')).toBeTruthy());+ expect(screen.queryByLabelText(previewLabel('notes.txt'))).toBeNull();+ expect(screen.getByLabelText(en.chat.compose.removeAttachment)).toBeTruthy();+ });++ it('opens and closes from the keyboard without sending or losing the draft', async () => {+ const user = userEvent.setup();+ renderComposer();+ await stage('shot-a.png');++ const textbox = screen.getByRole('textbox') as HTMLTextAreaElement;+ fireEvent.change(textbox, { target: { value: 'ship this crop' } });++ const trigger = await screen.findByLabelText(previewLabel('shot-a.png'));+ (trigger as HTMLButtonElement).focus();+ await user.keyboard('{Enter}');+ expect(dialogImage()).toBe(mediaUrl('shot-a.png'));++ // Escape belongs to the open viewer alone — it must not reach the composer.+ fireEvent.keyDown(window, { key: 'Escape' });++ expect(screen.queryByRole('dialog')).toBeNull();+ expect(textbox.value).toBe('ship this crop');+ expect(screen.getByText('shot-a.png')).toBeTruthy();+ expect(onSend).not.toHaveBeenCalled();+ });++ it('sends exactly the attachments it staged after a preview', async () => {+ renderComposer();+ await stageImages('shot-a.png', 'shot-b.png');++ fireEvent.change(screen.getByRole('textbox'), { target: { value: 'look' } });+ fireEvent.click(screen.getByLabelText(previewLabel('shot-a.png')));+ fireEvent.click(screen.getByLabelText(en.chat.viewer.next));+ fireEvent.click(screen.getByLabelText(en.chat.viewer.close));+ fireEvent.click(screen.getByLabelText(en.chat.compose.send));++ await waitFor(() => expect(onSend).toHaveBeenCalledTimes(1));+ const [text, attachments] = onSend.mock.calls[0] as [string, ComposerAttachment[]];+ expect(text).toBe('look');+ expect(attachments.map((a) => a.url)).toEqual([mediaUrl('shot-a.png'), mediaUrl('shot-b.png')]);+ expect(attachments.map((a) => a.token)).toEqual(['tok-shot-a.png', 'tok-shot-b.png']);+ expect(attachments.every((a) => a.status === 'ready')).toBe(true);+ });+});diff --git a/ui/src/components/workbench/Composer.tsx b/ui/src/components/workbench/Composer.tsxindex 521d8d01a..b2fde2bf7 100644--- a/ui/src/components/workbench/Composer.tsx+++ b/ui/src/components/workbench/Composer.tsx@@ -61,6 +61,7 @@ import {workbenchUploadErrorTranslationKey,} from '../../lib/workbenchUpload';import { Button } from '../ui/button';+import { useImageViewer } from '../ui/image-viewer-context';import { inForegroundSurface } from './chatShortcuts';import { useRouteSurfaceWindowEvent } from '../../lib/routeSurfaceActivity';import {@@ -106,6 +107,55 @@ const newLocalId = () => (?? `${Date.now()}-${Math.random().toString(36).slice(2, 14)}`);+// The leading slot of a staged-attachment chip: the spinner while the file+// uploads, the paperclip for anything that is not a rendered image, and the+// thumbnail once an image upload has its media URL.+//+// That last case is also the preview trigger (#2100): a screenshot has to be+// inspectable at full size BEFORE it is sent, and the thumbnail is the thing on+// screen that means "this image". It is a real button so the keyboard reaches+// it, named after the file so two staged screenshots are told apart, and it+// opens the shared lightbox over the staged images alone — the composer's own+// set, never the transcript's.+//+// Only a ``ready`` chip previews. ``uploading`` and ``error`` have no media URL+// to show, and the home composer's ``staged`` chips carry a local blob URL the+// viewer's download action cannot serve — they keep the plain thumbnail they+// render today. Where no lightbox is mounted at all the trigger is not rendered+// either, rather than a focusable control that would do nothing.+const AttachmentThumbnail = ({ att, gallery }: { att: ComposerAttachment; gallery: string[] }) => {+ const { t } = useTranslation();+ const imageViewer = useImageViewer();+ if (att.status === 'uploading') {+ return (+ <span className="grid size-7 place-items-center rounded text-muted">+ <Loader2 className="size-4 animate-spin" />+ </span>+ );+ }+ if (att.kind !== 'image' || !att.url) {+ return (+ <span className="grid size-7 place-items-center rounded bg-cyan/15 text-cyan-ink">+ <Paperclip className="size-3.5" />+ </span>+ );+ }+ const thumbnail = <img src={att.url} alt="" className="size-7 rounded object-cover" />;+ if (att.status !== 'ready' || !imageViewer) return thumbnail;+ return (+ <Button+ type="button"+ variant="ghost"+ size="icon"+ onClick={() => imageViewer.open(att.url, { gallery })}+ aria-label={t('chat.compose.previewAttachment', { name: att.name })}+ className="size-7 shrink-0 cursor-zoom-in overflow-hidden rounded p-0 hover:bg-transparent"+ >+ {thumbnail}+ </Button>+ );+};+type VoiceSegment = VoiceTranscriptionSegment & {task: Promise<void>;transcriptionStarted?: boolean;@@ -1236,6 +1286,12 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compoconst trimmed = value.trim();const readyAttachments = attachments.filter((a) => a.status === 'ready' || a.status === 'staged');const uploading = attachments.some((a) => a.status === 'uploading');+ // What a thumbnail preview pages through: the uploaded images staged right+ // here, in attachment order. Derived from the same list the chips render, so+ // the viewer's order is the row's order by construction.+ const stagedImageUrls = attachments+ .filter((a) => a.status === 'ready' && a.kind === 'image' && a.url)+ .map((a) => a.url);// The mention path doesn't mirror its text into ``value`` (see onChange), so// its "is there text?" signal comes from ``hasText``; the textarea path reads// ``value`` directly.@@ -1788,17 +1844,7 @@ export const Composer = forwardRef<ComposerHandle, ComposerProps>(function Compokey={att.localId}className="flex items-center gap-2 rounded-lg border border-border bg-surface-2 py-1 pl-1.5 pr-1 text-[12px]">- {att.status === 'uploading' ? (- <span className="grid size-7 place-items-center rounded text-muted">- <Loader2 className="size-4 animate-spin" />- </span>- ) : att.kind === 'image' && att.url ? (- <img src={att.url} alt="" className="size-7 rounded object-cover" />- ) : (- <span className="grid size-7 place-items-center rounded bg-cyan/15 text-cyan-ink">- <Paperclip className="size-3.5" />- </span>- )}+ <AttachmentThumbnail att={att} gallery={stagedImageUrls} /><span className={clsx('max-w-[160px] truncate', att.status === 'error' ? 'text-pink-ink' : 'text-foreground')}>{att.name}</span>diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.jsonindex afc86c284..bc4c77f19 100644--- a/ui/src/i18n/en.json+++ b/ui/src/i18n/en.json@@ -4255,6 +4255,7 @@"attachmentFailed": "The file could not be uploaded. Try again.","retryAttachment": "Retry upload","removeAttachment": "Remove",+ "previewAttachment": "Preview {{name}}","dropOverlay": "Drop files to attach"},"media": {diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.jsonindex d034e074a..9a73d799f 100644--- a/ui/src/i18n/zh.json+++ b/ui/src/i18n/zh.json@@ -4255,6 +4255,7 @@"attachmentFailed": "文件上传失败,请重试。","retryAttachment": "重试上传","removeAttachment": "移除",+ "previewAttachment": "预览 {{name}}","dropOverlay": "拖放文件以添加"},"media": {