Skip to content

Commit 87c000d

Browse files
committed
fix(tui): save clipboard pasted images to temp file for MCP tools (anomalyco#14673)
1 parent 988c989 commit 87c000d

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

  • packages/opencode/src/cli/cmd/tui/component/prompt

‎packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { BoxRenderable, TextareaRenderable, MouseEvent, PasteEvent, decodePasteB
22
import { createEffect, createMemo, onMount, createSignal, onCleanup, on, Show, Switch, Match } from "solid-js"
33
import "opentui-spinner/solid"
44
import path from "path"
5-
import { fileURLToPath } from "url"
5+
import { fileURLToPath, pathToFileURL } from "url"
6+
import fs from "fs/promises"
7+
import { tmpdir } from "os"
68
import { Filesystem } from "@/util/filesystem"
79
import { useLocal } from "@tui/context/local"
810
import { useTheme } from "@tui/context/theme"
@@ -789,14 +791,28 @@ export function Prompt(props: PromptProps) {
789791
typeId: promptPartTypeId,
790792
})
791793

794+
let url = `data:${file.mime};base64,${file.content}`
795+
let filepath = file.filepath
796+
797+
if (!filepath && file.mime.startsWith("image/")) {
798+
const ext = file.mime.split("/")[1] ?? "png"
799+
filepath = path.join(tmpdir(), `opencode-paste-${Date.now()}.${ext}`)
800+
try {
801+
await fs.writeFile(filepath, Buffer.from(file.content, "base64"))
802+
url = pathToFileURL(filepath).href
803+
} catch (err) {
804+
console.error("Failed to save pasted image:", err)
805+
}
806+
}
807+
792808
const part: Omit<FilePart, "id" | "messageID" | "sessionID"> = {
793809
type: "file" as const,
794810
mime: file.mime,
795811
filename: file.filename,
796-
url: `data:${file.mime};base64,${file.content}`,
812+
url,
797813
source: {
798814
type: "file",
799-
path: file.filepath ?? file.filename ?? "",
815+
path: filepath ?? file.filename ?? "",
800816
text: {
801817
start: extmarkStart,
802818
end: extmarkEnd,

0 commit comments

Comments
 (0)