Skip to content

Commit 9296e12

Browse files
author
yida
committed
fix(tui): paste local images as file:// attachments
Keep the short basename in FileAttachment.name for display and provider filename metadata. Send the filesystem path as a file:// URI so admission retains source.uri and existing toLLMMessages location text can surface it. Clipboard bitmaps stay data: URIs named "clipboard" and do not get a fake Attached file path note. Fixes anomalyco#41454
1 parent 81200db commit 9296e12

6 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { Slot } from "../../plugin/render"
6161
import type { SessionInbox } from "@opencode/schema/session-inbox"
6262
import {
6363
deduplicatePromptImages,
64+
isPromptImageUri,
6465
preserveMentionlessPromptAttachments,
6566
promptAttachmentLabel,
6667
} from "../../prompt/attachment"
@@ -392,7 +393,7 @@ export function Prompt(props: PromptProps) {
392393
}
393394

394395
const imageAttachments = createMemo(() =>
395-
(deduplicatePromptImages(store.prompt.files) ?? []).filter((file) => file.uri.startsWith("data:image/")),
396+
(deduplicatePromptImages(store.prompt.files) ?? []).filter((file) => isPromptImageUri(file.uri)),
396397
)
397398
const imagePreviewHeight = createMemo(() => Math.max(4, Math.min(8, Math.floor(dimensions().height / 4))))
398399
const imagePreviewWidth = createMemo(() => imagePreviewHeight() * 2)

‎packages/tui/src/component/prompt/local-attachment.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from "node:path"
2+
import { pathToFileURL } from "node:url"
23

34
// Bound filesystem work per terminal paste; the byte budget also bounds staged data.
45
const MAX_PASTED_FILEPATHS = 32
@@ -50,9 +51,11 @@ export async function resolvePastedAttachments(text: string, platform: string) {
5051
if (item.attachment.type === "text") {
5152
return { type: "text" as const, content: item.attachment.content, filename }
5253
}
54+
// Keep the short display name, and pass the path as a file:// URI so
55+
// admission retains source.uri and toLLMMessages can expose the location.
5356
return {
5457
type: "file" as const,
55-
uri: `data:${item.attachment.mime};base64,${Buffer.from(item.attachment.content).toString("base64")}`,
58+
uri: pathToFileURL(item.filepath).href,
5659
filename,
5760
}
5861
})

‎packages/tui/src/mini/footer.prompt.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from "@opentui/core"
1818
import { useRenderer, useTerminalDimensions } from "@opentui/solid"
1919
import { normalizePromptContent } from "../prompt/content"
20-
import { deduplicatePromptImages, promptAttachmentLabel } from "../prompt/attachment"
20+
import { deduplicatePromptImages, isPromptImageUri, promptAttachmentLabel } from "../prompt/attachment"
2121
import { resolvePastedAttachments } from "../component/prompt/local-attachment"
2222
import { createTuiClipboard, type OwnedClipboardService } from "../clipboard"
2323
import type { ClipboardService } from "../context/clipboard"
@@ -393,7 +393,7 @@ export function createPromptState(input: PromptInput): PromptState {
393393
),
394394
)
395395
const images = createMemo(() =>
396-
(deduplicatePromptImages(attachments()) ?? []).filter((file) => file.uri.startsWith("data:image/")),
396+
(deduplicatePromptImages(attachments()) ?? []).filter((file) => isPromptImageUri(file.uri)),
397397
)
398398
let clipboard: OwnedClipboardService | undefined
399399
let pasteQueue: Promise<void> | undefined

‎packages/tui/src/prompt/attachment.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ type ProjectedFile = Readonly<{
1414
function attachmentKind(uri: string) {
1515
if (uri.startsWith("data:image/")) return "Image"
1616
if (uri.startsWith("data:application/pdf;")) return "PDF"
17+
if (uri.startsWith("file:")) {
18+
const pathname = URL.parse(uri)?.pathname ?? uri
19+
if (/\.(avif|gif|jpe?g|png|webp)$/i.test(pathname)) return "Image"
20+
if (/\.pdf$/i.test(pathname)) return "PDF"
21+
}
1722
return undefined
1823
}
1924

25+
export function isPromptImageUri(uri: string) {
26+
return attachmentKind(uri) === "Image"
27+
}
28+
2029
function attachmentMetadata(file: PromptFileIdentity) {
2130
return JSON.stringify([file.name ?? null, file.description ?? null])
2231
}

‎packages/tui/test/prompt/attachment.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ describe("prompt attachments", () => {
6464
expect(promptAttachmentLabel(files, { uri: "data:application/pdf;base64,BBB" })).toBe("[PDF 1]")
6565
})
6666

67+
test("labels local file:// image and PDF attachments", () => {
68+
expect(promptAttachmentLabel([], { uri: "file:///tmp/photo.png", name: "photo.png" })).toBe("[Image 1]")
69+
expect(promptAttachmentLabel([], { uri: "file:///tmp/doc.pdf", name: "doc.pdf" })).toBe("[PDF 1]")
70+
})
71+
6772
test("does not reuse a label when attachment metadata differs", () => {
6873
const uri = "data:image/png;base64,AAA"
6974
const files = [{ uri, name: "one.png", mention: { start: 0, end: 9, text: "[Image 1]" } }]

‎packages/tui/test/prompt/local-attachment.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe("prompt local attachments", () => {
9797

9898
for (const input of [file, `'${file}'`, pathToFileURL(file).href]) {
9999
expect(await resolvePastedAttachments(input, process.platform)).toEqual([
100-
{ type: "file", uri: "data:image/png;base64,AQID", filename: "one image.png" },
100+
{ type: "file", uri: pathToFileURL(file).href, filename: "one image.png" },
101101
])
102102
}
103103
})
@@ -113,8 +113,8 @@ describe("prompt local attachments", () => {
113113
`# dropped files\r\n${pathToFileURL(image).href}\r\n${pathToFileURL(pdf).href}`,
114114
]) {
115115
expect(await resolvePastedAttachments(input, process.platform)).toEqual([
116-
{ type: "file", uri: "data:image/png;base64,AQID", filename: "one image.png" },
117-
{ type: "file", uri: "data:application/pdf;base64,BAUG", filename: "two file.pdf" },
116+
{ type: "file", uri: pathToFileURL(image).href, filename: "one image.png" },
117+
{ type: "file", uri: pathToFileURL(pdf).href, filename: "two file.pdf" },
118118
])
119119
}
120120
})
@@ -159,7 +159,7 @@ describe("prompt local attachments", () => {
159159
])
160160

161161
expect(await resolvePastedAttachments(`${image} ${svg}`, process.platform)).toMatchObject([
162-
{ type: "file", filename: "image.png" },
162+
{ type: "file", uri: pathToFileURL(image).href, filename: "image.png" },
163163
{ type: "text", content, filename: "image.svg" },
164164
])
165165
await Bun.write(svg, content + " ")

0 commit comments

Comments
 (0)