Skip to content

The agent that grows with you

fix(desktop): paint OS-dropped images as thumbnails in sent messages

Brooklyn Nicholson8c180e3fa2f9
diff --git a/apps/desktop/src/components/assistant-ui/directive-blob-image.test.tsx b/apps/desktop/src/components/assistant-ui/directive-blob-image.test.tsx
new file mode 100644
index 00000000000..fa9f8437b2c
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/directive-blob-image.test.tsx
@@ -0,0 +1,79 @@
+// @vitest-environment jsdom
+import { cleanup, render } from '@testing-library/react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { DirectiveContent } from './directive-text'
+
+afterEach(cleanup)
+
+// The optimistic attachment ref for an OS-dropped image is a Markdown image
+// wrapping the renderer-local object URL. The sent-message surface has no
+// generic Markdown-image parser, so this exact producer form must be
+// recognized and painted as a thumbnail — raw Markdown and the blob URL
+// leaking into visible text is the bug being pinned here.
+describe('blob markdown image ref in a sent user message', () => {
+ it('paints the object URL as an image instead of raw Markdown', () => {
+ const { container } = render(
+ <DirectiveContent text="![upload_1.png](blob:file:///07aa165b-55f6-4167-96c0-68f45ce7de27)" />
+ )
+
+ const img = container.querySelector('span[data-slot="aui_directive-image"] img')
+ expect(img?.getAttribute('src')).toBe('blob:file:///07aa165b-55f6-4167-96c0-68f45ce7de27')
+ expect(img?.getAttribute('alt')).toBe('upload_1.png')
+ expect(container.textContent).not.toContain('![')
+ expect(container.textContent).not.toContain('blob:')
+ })
+
+ it('covers the https-origin blob form too', () => {
+ const { container } = render(<DirectiveContent text="see this ![shot](blob:https://desktop/preview-1) thanks" />)
+
+ const img = container.querySelector('span[data-slot="aui_directive-image"] img')
+ expect(img?.getAttribute('src')).toBe('blob:https://desktop/preview-1')
+ expect(container.textContent).not.toContain('blob:')
+ // surrounding prose survives
+ expect(container.textContent).toContain('see this')
+ expect(container.textContent).toContain('thanks')
+ })
+
+ it('paints a percent-encoded alt from a filename with brackets', () => {
+ // The producer percent-encodes the label, so `]` in a filename cannot end
+ // the alt span; the ref stays renderable instead of leaking into text.
+ const { container } = render(
+ <DirectiveContent text="![shot%5B1%5D.png](blob:file:///07aa165b-55f6-4167-96c0-68f45ce7de27)" />
+ )
+
+ const img = container.querySelector('span[data-slot="aui_directive-image"] img')
+ expect(img?.getAttribute('src')).toBe('blob:file:///07aa165b-55f6-4167-96c0-68f45ce7de27')
+ expect(img?.getAttribute('alt')).toBe('shot%5B1%5D.png')
+ expect(container.textContent).not.toContain('![')
+ expect(container.textContent).not.toContain('blob:')
+ })
+
+ it('keeps multiple dropped images as separate thumbnails', () => {
+ const { container } = render(
+ <DirectiveContent
+ text={
+ '![a.png](blob:file:///11111111-1111-1111-1111-111111111111) ' +
+ '![b.png](blob:file:///22222222-2222-2222-2222-222222222222)'
+ }
+ />
+ )
+
+ const srcs = Array.from(container.querySelectorAll('span[data-slot="aui_directive-image"] img')).map(el =>
+ el.getAttribute('src')
+ )
+
+ expect(srcs).toEqual([
+ 'blob:file:///11111111-1111-1111-1111-111111111111',
+ 'blob:file:///22222222-2222-2222-2222-222222222222'
+ ])
+ expect(container.textContent).not.toContain('![')
+ })
+
+ it('leaves ordinary markdown images as plain text', () => {
+ const { container } = render(<DirectiveContent text="![logo](https://example.com/logo.png)" />)
+
+ expect(container.querySelector('img')).toBeNull()
+ expect(container.textContent).toContain('![logo](https://example.com/logo.png)')
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/directive-text.tsx b/apps/desktop/src/components/assistant-ui/directive-text.tsx
index fadb18d7cad..c0b7554ee5d 100644
--- a/apps/desktop/src/components/assistant-ui/directive-text.tsx
+++ b/apps/desktop/src/components/assistant-ui/directive-text.tsx
@@ -138,6 +138,14 @@ const HERMES_DIRECTIVE_RE = referenceRe()
// something other than another slash.
const SLASH_SKILL_RE = /(?<=^|\s)\/([a-zA-Z][\w-]*)(?![\w-]*\/)/g
+// The optimistic attachment ref for an OS-dropped image is a Markdown image
+// wrapping a renderer-local object URL (`![alt](blob:file:///…)`) — a string
+// this same module's producer (optimisticAttachmentRef) serializes. Recognize
+// exactly that form so the ref renders as a thumbnail instead of leaking the
+// raw Markdown and the blob URL into visible message text. Only `blob:` URLs
+// qualify: a plain-http/data markdown image is foreign input and stays text.
+const BLOB_MARKDOWN_IMAGE_RE = /!\[([^\]\n]{0,512})\]\((blob:[^)\s]{1,2048})\)/g
+
const TRAILING_PUNCTUATION_RE = /[,.;!?]+$/
function unwrapRefValue(raw: string): string {
@@ -252,6 +260,13 @@ function parseDirectiveText(text: string): Unstable_DirectiveSegment[] {
type: 'skill',
label: match[1],
id: `/${match[1]}`
+ })),
+ ...Array.from(text.matchAll(BLOB_MARKDOWN_IMAGE_RE)).map(match => ({
+ start: match.index ?? 0,
+ end: (match.index ?? 0) + match[0].length,
+ type: 'image',
+ label: match[1] || 'image',
+ id: match[2]
}))
]
.filter(match => match.id)
@@ -401,7 +416,10 @@ export const DirectiveText: TextMessagePartComponent = ({ text }: TextMessagePar
* messages render after the backend embeds the data URL, so the UX is stable
* across initial send and refresh. */
const DirectiveImage: FC<{ id: string; label: string }> = ({ id, label }) => {
- const isUrl = /^(?:https?|data):/i.test(id)
+ // `blob:` joins the direct-URL set: the object URL is already renderer-local
+ // (the whole point of the OS-drop preview path), so painting it is free of
+ // the IPC read the path branch would issue.
+ const isUrl = /^(?:https?|data|blob):/i.test(id)
// `src` is the bounded thumbnail painted inline; `zoomSrc` is the full-
// resolution source the lightbox and download use. Keeping inline bounded is
// what lets the in-flight bubble render an `@image:<path>` ref without the
diff --git a/apps/desktop/src/lib/chat-runtime.test.ts b/apps/desktop/src/lib/chat-runtime.test.ts
index a353e0e1e58..9671918278e 100644
--- a/apps/desktop/src/lib/chat-runtime.test.ts
+++ b/apps/desktop/src/lib/chat-runtime.test.ts
@@ -99,6 +99,18 @@ describe('optimisticAttachmentRef', () => {
expect(ref).toBe(`![Lattice.png](${blobUrl})`)
})
+ it('percent-encodes a blob ref alt so brackets in a filename cannot break it', () => {
+ const blobUrl = 'blob:file:///07aa165b-55f6-4167-96c0-68f45ce7de27'
+
+ const ref = optimisticAttachmentRef(
+ attachment({ kind: 'image', label: 'shot[1].png', detail: '/tmp/shot[1].png', previewUrl: blobUrl })
+ )
+
+ // `]` in the raw label would end the alt span in the Markdown-image form
+ // the directive parser matches, leaking the raw expression into text.
+ expect(ref).toBe(`![shot%5B1%5D.png](${blobUrl})`)
+ })
+
it('passes non-image attachments straight through to attachmentDisplayText', () => {
expect(optimisticAttachmentRef(attachment({ kind: 'file', refText: '@file:src/a.ts', previewUrl: DATA_URL }))).toBe(
'@file:src/a.ts'
diff --git a/apps/desktop/src/lib/chat-runtime.ts b/apps/desktop/src/lib/chat-runtime.ts
index f56e684ebf1..8fac835cb38 100644
--- a/apps/desktop/src/lib/chat-runtime.ts
+++ b/apps/desktop/src/lib/chat-runtime.ts
@@ -238,7 +238,10 @@ export function optimisticAttachmentRef(attachment: ComposerAttachment): string
// markdown image keeps them out of the data-URL extract path while still
// rendering inline in the optimistic bubble (#63682).
if (attachment.previewUrl?.startsWith('blob:')) {
- const alt = attachment.label || 'image'
+ // Percent-encode the alt text: a filename with `]` or parens in it would
+ // otherwise break the Markdown-image form the directive parser matches
+ // below, and the raw expression would leak into visible text (#123368).
+ const alt = encodeURIComponent(attachment.label || 'image')
return `![${alt}](${attachment.previewUrl})`
}