Skip to content

The agent that grows with you

fix(desktop): collect artifacts referenced with #media: hrefs

brooklyn!d909c697f00b
diff --git a/apps/desktop/src/app/artifacts/artifact-utils.ts b/apps/desktop/src/app/artifacts/artifact-utils.ts
index 0d39c0f2baf..ef822a16ac3 100644
--- a/apps/desktop/src/app/artifacts/artifact-utils.ts
+++ b/apps/desktop/src/app/artifacts/artifact-utils.ts
@@ -1,5 +1,5 @@
import { mediaTagValues } from '@/lib/chat-messages/parts'
-import { isArtifactFilePath, mediaExternalUrl, resolveMediaDisplaySrc } from '@/lib/media'
+import { isArtifactFilePath, mediaExternalUrl, mediaPathFromMarkdownHref, resolveMediaDisplaySrc } from '@/lib/media'
import type { SessionInfo, SessionMessage } from '@/types/hermes'
export type ArtifactKind = 'image' | 'file' | 'link'
@@ -84,6 +84,12 @@ function normalizeValue(value: string): string {
return value.trim().replace(/[),.;]+$/, '')
}
+// Chat renders file refs as `[label](#media:<encoded path>)`. Decode before
+// classification so the Artifacts page keeps the path, not the href.
+function decodeMediaHrefValue(value: string): string {
+ return mediaPathFromMarkdownHref(value) ?? value
+}
+
function unquoteMediaValue(value: string): string {
let trimmed = value.trim()
const quote = trimmed[0]
@@ -288,7 +294,7 @@ function collectArtifactsFromText(text: string, pushValue: PushValue): void {
continue
}
- const value = match[2] || ''
+ const value = decodeMediaHrefValue(match[2] || '')
if (looksLikeArtifact(value)) {
pushValue(value)
@@ -388,7 +394,7 @@ function collectArtifactsFromMessage(message: SessionMessage, pushValue: PushVal
collectMediaValues(value, pushValue)
- const normalized = normalizeValue(value)
+ const normalized = normalizeValue(decodeMediaHrefValue(value))
if (normalized && looksLikeArtifact(normalized)) {
pushValue(normalized)
@@ -407,7 +413,7 @@ export function collectArtifactsForSession(session: SessionInfo, messages: Sessi
}
collectArtifactsFromMessage(message, (candidate, explicit = false) => {
- const value = normalizeValue(candidate)
+ const value = normalizeValue(decodeMediaHrefValue(candidate))
if (!value || !looksLikeArtifact(value, explicit)) {
return
diff --git a/apps/desktop/src/app/artifacts/index.test.ts b/apps/desktop/src/app/artifacts/index.test.ts
index bf1d25643ee..b321b171d96 100644
--- a/apps/desktop/src/app/artifacts/index.test.ts
+++ b/apps/desktop/src/app/artifacts/index.test.ts
@@ -394,6 +394,91 @@ ${payload}
path: '/api/fs/read-data-url?path=%2FUsers%2Fme%2F.hermes%2Fskills%2Fwork-esab%2Freferences%2Fimages%2Fmanual-step03.jpeg'
})
})
+
+ it('collects images referenced with a #media: markdown href and decodes the path', () => {
+ const artifacts = collectArtifactsForSession(makeSession(), [
+ {
+ content: '[Image: report](#media:C%3A%5CUsers%5CMorten%5CMy%20Report.png)',
+ role: 'assistant',
+ timestamp: 2000
+ }
+ ])
+
+ expect(artifacts).toHaveLength(1)
+ expect(artifacts[0]).toMatchObject({
+ kind: 'image',
+ value: 'C:\\Users\\Morten\\My Report.png'
+ })
+ })
+
+ it('collects #media: hrefs with percent-encoded POSIX paths', () => {
+ const artifacts = collectArtifactsForSession(makeSession(), [
+ {
+ content: '[Audio: clip](#media:%2Ftmp%2Fgenerated%2Fmy%20clip.mp3)',
+ role: 'assistant',
+ timestamp: 2000
+ }
+ ])
+
+ expect(artifacts).toHaveLength(1)
+ expect(artifacts[0]).toMatchObject({
+ kind: 'file',
+ value: '/tmp/generated/my clip.mp3'
+ })
+ })
+
+ it('collects image markdown whose href is a #media: link', () => {
+ const artifacts = collectArtifactsForSession(makeSession(), [
+ {
+ content: '![cat](#media:%2Ftmp%2Fgenerated%2Fcat.png)',
+ role: 'assistant',
+ timestamp: 2000
+ }
+ ])
+
+ expect(artifacts).toHaveLength(1)
+ expect(artifacts[0]).toMatchObject({
+ kind: 'image',
+ value: '/tmp/generated/cat.png'
+ })
+ })
+
+ it('still collects legacy MEDIA paths and plain URLs beside #media: hrefs', () => {
+ const artifacts = collectArtifactsForSession(makeSession(), [
+ {
+ content: [
+ '[Image: report](#media:C%3A%5CUsers%5CMorten%5CMy%20Report.png)',
+ 'Old: **MEDIA: /tmp/generated/demo.png**',
+ 'Link: [docs](https://example.com/docs)'
+ ].join('\n\n'),
+ role: 'assistant',
+ timestamp: 2000
+ }
+ ])
+
+ expect(artifacts.map(artifact => artifact.value)).toEqual([
+ '/tmp/generated/demo.png',
+ 'C:\\Users\\Morten\\My Report.png',
+ 'https://example.com/docs'
+ ])
+ })
+
+ it('collects #media: hrefs stored on explicit tool artifact keys', () => {
+ const artifacts = collectArtifactsForSession(makeSession(), [
+ {
+ content: JSON.stringify({ output_file: '#media:%2Ftmp%2Fgenerated%2Ftool.png' }),
+ role: 'tool',
+ timestamp: 2000,
+ tool_name: 'image_generate'
+ }
+ ])
+
+ expect(artifacts).toHaveLength(1)
+ expect(artifacts[0]).toMatchObject({
+ kind: 'image',
+ value: '/tmp/generated/tool.png'
+ })
+ })
})
describe('loadArtifactsForSessions', () => {