Skip to content

fix: persist clipboard images as temp files with file:// URLs#22218

Open
dorukardahan wants to merge 2 commits intoanomalyco:devfrom
dorukardahan:fix/clipboard-image-file-url
Open

fix: persist clipboard images as temp files with file:// URLs#22218
dorukardahan wants to merge 2 commits intoanomalyco:devfrom
dorukardahan:fix/clipboard-image-file-url

Conversation

@dorukardahan
Copy link
Copy Markdown

@dorukardahan dorukardahan commented Apr 12, 2026

Issue for this PR

Closes #14673

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When an image is pasted from clipboard, OpenCode stored it as an inline data:image/...;base64,... URL. MCP tools (look_at, understand_image) expect a file path on disk, not a base64 string, so they couldn't analyze pasted images.

This PR:

  1. Saves clipboard images to temp files (os.tmpdir()/opencode-clipboard-<timestamp>.png) and keeps the file path
  2. Uses file:// URLs so MCP tools can access the file
  3. Converts file:// back to data: URLs before sending to AI SDK (which rejects file:// scheme)

Changes:

  • clipboard.ts — save temp file, return filepath in Content, unique filenames with timestamp
  • prompt/index.tsx — pass filepath through attachment flow, use file:// URL when filepath exists
  • prompt.ts — use part.url directly instead of re-reading the file
  • message-v2.ts — convert file://data: before AI SDK consumption (in both user message parts and tool output attachments)

How did you verify your code works?

  • All 23 message-v2 tests pass
  • All 45 prompt-related tests pass
  • tsgo --noEmit typecheck passes
  • CI: typecheck pass, unit (linux) pass, unit (windows) pass

Screenshots / recordings

No UI changes — purely internal data handling.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Note: There is an open PR #21633 addressing the same issue. This PR takes a more complete approach by also handling file://data: conversion in message-v2.ts (both user parts and tool output attachments), ensuring the AI SDK never sees a file:// URL it would reject.

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found

PR #21633: fix(tui): save clipboard-pasted images to temp files for MCP tool access

Reason: This PR appears to be addressing the same issue as PR #22218. Both are focused on saving clipboard-pasted images to temp files for MCP tool access. PR #21633 specifically mentions the same core functionality of persisting clipboard images for MCP tools.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 12, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@dorukardahan
Copy link
Copy Markdown
Author

Pushed an update: added error handling for the file:// → data: URL conversion in both locations (async path and sync callback). If a temp file has been deleted before conversion, it falls back to a text placeholder instead of crashing the message pipeline. All tests pass.

Clipboard images are now saved to temp files and referenced via file://
URLs instead of inline data: URLs. This allows MCP tools (look_at,
understand_image) to access the file path. Before sending to the AI
SDK, file:// URLs are converted back to data: URLs since the SDK
rejects file:// schemes.

Fixes anomalyco#14673
Add error handling for clipboard image temp files that may be deleted
before conversion. Uses async readFile in Promise.all path and try/catch
with text fallback in both conversion locations.
@dorukardahan dorukardahan force-pushed the fix/clipboard-image-file-url branch from 4567d68 to c4f7119 Compare April 13, 2026 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]:Pasted images use data: URL instead of file path, breaking MCP tools for non-vision models

1 participant