Fix loading images in opencode by handling data:png in URL construction#1845
Merged
Conversation
jlu-figma
marked this pull request as draft
July 23, 2026 21:29
jlu-figma
marked this pull request as ready for review
July 24, 2026 00:08
Contributor
Author
|
@NathanFlurry can i get a look at this? Would love to use agent-os but this is a blocker |
The fallback `NativeURL` implementation used when a native WHATWG `URL`
is unavailable (e.g. the secure-exec bootstrap replaces `globalThis.URL`
with a `__secureExecBootstrapStub`, so `canUseNativeUrlImplementation`
returns false) only parsed hierarchical `scheme://host` URLs and a
special case for `file:`. For any other scheme it fell through to:
const match = full.match(/^(\w+:)\/\/([^/:?#]+)(:\d+)?(.*)$/);
An opaque URL such as `data:image/png;base64,...` or `mailto:a@b.com`
has no `//authority`, so `match` was null and every field defaulted to
empty, producing `href === "///"`, `protocol === ""` and `pathname
=== "/"`.
This breaks any guest code that round-trips a `data:` URL through
`new URL(...)`. Concretely, when OpenCode runs inside AgentOS and its
`read` tool returns an image as a `data:image/png;base64,...`
attachment, the AI SDK's `convertToLanguageModelPrompt` /
`validateDownloadUrl` call `new URL(dataUrl)` and check
`parsed.protocol === "data:"`. The corrupted URL fails that check and
raises `AI_DownloadError: Invalid URL: ///`, so no image ever reaches
the model turn.
Add a branch for opaque (non-hierarchical) URLs — any `scheme:` not
followed by `//` — before the hierarchical regex. It sets the protocol,
treats the remainder (minus query/hash) as an opaque pathname, reports
`origin === "null"`, and keeps `href`/`searchParams` in sync, matching
Node's WHATWG `URL` behavior. `file:` and `scheme://host` URLs are
unaffected (the negative lookahead `(?!\/\/)` lets them fall through).
Adds a `data:`/`mailto:` regression case to the `url` builtin
conformance test, which diffs guest V8 output against host Node.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The `searchParams`->`href` sync wiring was duplicated across the `file:`, opaque, and hierarchical branches of the fallback `NativeURL` constructor, each with its own inline `["append","delete","set","sort"]` list. Factor it into a single `bindSearchParams(buildHref)` helper driven by a shared `SEARCH_PARAM_SYNC_METHOD_NAMES` constant, so each branch only supplies its own `href` builder. No behavior change; the `url` builtin conformance test (guest V8 vs host Node) still passes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
NathanFlurry
force-pushed
the
jlu/new_fix
branch
from
July 25, 2026 00:07
f27804e to
944ff0a
Compare
This was referenced Jul 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
I was trying to use opencode with AgentOS as the filesystem backend, but found that my agent would crash whenever it tried to load images.
Turns out:
data:image/png...)NativeURLparsing through some pathNativeURLtries to match eitherfile:orfull.match(/^([a-zA-Z][a-zA-Z\d+\-.]*:)(?!\/\/)/);(which isstuff likescheme://host/`, I believe)data:don't get processed and so nothing gets turned to the agent during image reading.The change here is to add an opaque URL branch before the final backup regex above.
Testing
AGENTOS_BUILTIN_CONFORMANCE_CASE=url cargo test -p agentos-native-sidecar --test builtin_conformance __builtin_conformance_case_runner -- --exact— passes (guest fallback output matches host Node for thedata:/mailto:cases and all pre-existing cases).Alternative appraoches considerd