Uh oh!
There was an error while loading. Please reload this page.
feat: Pass through data URLs directly and gracefully handle unknown U… - #12592
feat: Pass through data URLs directly and gracefully handle unknown U…#12592yxshee wants to merge 3 commits into
Conversation
…RL protocols in prompt part processing.
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate FoundPR #12549: fix(desktop): windows clipboard images come thru as data not file This PR is directly related to the current PR #12592. Both address the same issue: handling clipboard images on Windows that come through as |
There was a problem hiding this comment.
Pull request overview
Fixes a crash when handling clipboard-pasted images (e.g., Windows 11 data:image/png;base64,...) by preventing data: URLs from falling through to file: handling and by adding a safe fallback for previously-unhandled URL protocols in SessionPrompt.
Changes:
- Pass through non-
text/plaindata:URL file parts (e.g., images/PDFs) without attemptingfileURLToPath(). - Add a
defaultbranch in the URL protocol switch to avoid unhandled protocols causing failures/dropped parts. - Add warning log for unknown protocols (with URL prefix logging).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ] | ||
| default: | ||
| // Handle unknown protocols gracefully to prevent crashes | ||
| log.warn("unsupported URL protocol", { protocol: url.protocol, url: part.url.slice(0, 100) }) |
There was a problem hiding this comment.
The default-case log includes a prefix of the full URL (part.url.slice(0, 100)), which can leak sensitive user data (e.g., signed URLs/tokens in query strings). Consider logging only the protocol plus a redacted/sanitized identifier (e.g., hostname for http(s), or just url.protocol/mime/length), and avoid logging the URL itself at warn level.
| log.warn("unsupported URL protocol",{protocol: url.protocol,url: part.url.slice(0,100)}) | |
| log.warn("unsupported URL protocol",{ | |
| protocol: url.protocol, | |
| mime: part.mime, | |
| filename: part.filename, | |
| }) |
| default: | ||
| // Handle unknown protocols gracefully to prevent crashes | ||
| log.warn("unsupported URL protocol", { protocol: url.protocol, url: part.url.slice(0, 100) }) | ||
| return [ |
There was a problem hiding this comment.
The default branch labels any non-data:/file: URL as "unsupported" and emits a warn. The codebase can create http: URLs for image parts (see packages/opencode/src/acp/agent.ts where part.uri.startsWith("http:") becomes a file part), and those may be valid to pass through to providers. Consider adding explicit case "http:":/"https:": to pass through without warning, or change the log message/level to reflect "unhandled" rather than "unsupported" to avoid noisy/misleading logs.
| // For non-text data URLs (images, PDFs from clipboard), pass through as-is | ||
| log.info("data url", { mime: part.mime }) | ||
| return [ | ||
| { | ||
| ...part, | ||
| id: part.id ?? Identifier.ascending("part"), | ||
| messageID: info.id, | ||
| sessionID: input.sessionID, | ||
| }, | ||
| ] |
There was a problem hiding this comment.
This change introduces new behavior for non-text data: URLs and unknown protocols. There are existing SessionPrompt tests under packages/opencode/test/session/; please add coverage for (1) data:image/png;base64,... parts to ensure they no longer crash/are preserved, and (2) a non-file:/data: URL (e.g., http:) to ensure it’s handled consistently (and not dropped).
When pasting images from clipboard on Windows 11, the app crashed with: TypeError: The URL must be of scheme file (ERR_INVALID_URL_SCHEME) Root cause: The data: case only handled text/plain and broke, causing non-text data URLs to fall through to file: case which called fileURLToPath() on a data: URL. Fix: - Return non-text data URLs directly instead of breaking - Add default case to prevent fall-through for unknown protocols
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR. |
Summary
Fixes#12547
Error:
TypeError: The URL must be of scheme file (ERR_INVALID_URL_SCHEME)Root Cause
In prompt.ts, the switch statement at line 946 handles URL protocols:
case "data:"only processestext/plainmime types, thenbreaksimage/png), the condition fails and falls through tocase "file:"fileURLToPath()is called on adata:URL → crashChanges
packages/opencode/src/session/prompt.ts
data:URLs (images, PDFs) - returns them as-is for the modelWhy This Happens on Windows
Windows clipboard paste encodes images as
data:image/png;base64,...URLs viaFileReader.readAsDataURL(). The server-side switch statement wasn't handling this case properly.Testing
Checklist