fix(image): load Photon via runtime eval on compiled binaries; surface typed errors in omitted-image message
Merged
AL
6mo ago+53+53+01234567890123456789-7-7-0123456789
Closes #68.
Symptom
On the Windows v0.1.5 release binary, every browser_execute screenshot and every read of an image file returns
[1 image omitted: could not be resized below the inline image size limit.]
even for sub-200-byte PNGs that cannot physically exceed the 4.5 MB ceiling. The misleading message hid a real Photon load failure and burned hours of user + agent debugging.
Root cause (two stacked bugs)
- Real: Photon WASM fails to load in the Bun compiled binary on Windows. The patched
photon_rs.jsfalls back torequire("path").join(__dirname, "photon_rs_bg.wasm")whenglobalThis.__OPENCODE_PHOTON_WASM_PATHis unset at the right moment, and Bun's static bundling evaluates thatrequirebefore the global is observable, so the wasm path never resolves underB:/~BUN/root/.image.normalize()then fails withPhotonUnavailableError. - Hiding the real bug: the loader's
catch { return null }swallowed the import error silently, andsession/processor.tshard-coded the omitted-image message to the size-failure wording regardless of whichImage.Errortag fired.
Fix
packages/opencode/src/image/image.ts
- Log the swallowed Photon import error via
log.warn("photon image processor unavailable", { error, stack }). - When
with: { type: "file" }yields an asset path (compiled binary), evaluatephoton_rs.jsvianew Function(...)overawait Bun.file(photonJs).text()after setting__OPENCODE_PHOTON_WASM_PATH. The patched module then reads from the embedded wasm asset. Falls back to plainawait import("@silvia-odwyer/photon-node")in dev/test where the import returns the module record directly.
packages/opencode/src/session/processor.ts
- Extract the typed error per failure via
Cause.findErrorOptionand switch on_tag:ImagePhotonUnavailableError-> "image processor unavailable in this runtime"ImageInvalidDataUrlError-> "attachment URL malformed"ImageDecodeError-> "could not decode image data"ImageSizeError-> keep the original "could not be resized below the inline image size limit"
- Group failures by reason so a single tool call dropping multiple images for different reasons emits one line per distinct reason.
Verification
bun typecheckfrompackages/opencode/: clean.bun test test/image/image.test.ts: 3 pass, 0 fail (existing tests unchanged).- End-to-end on a freshly built Windows
bcode.exe:- 192-byte real PNG (
Readtool): image attaches, no omitted line. - 67-byte malformed PNG:
[1 image omitted: could not decode image data.](accurateDecodeError, not misleading size).
- 192-byte real PNG (
- Confirmed against the installed
bcode 0.1.5binary reproducing the original symptom before the fix.
Summary by cubic
Fixes Photon loading in Windows compiled binaries and shows accurate reasons when images are omitted. Screenshots and file reads now work on the Windows build instead of always being dropped.
- Bug Fixes
- Load
@silvia-odwyer/photon-nodein compiled binaries by evaluating the embeddedphoton_rs.jsvianew Function(...)after setting__OPENCODE_PHOTON_WASM_PATH. Falls back to dynamic import in dev/test. - Log Photon load failures with
log.warninstead of swallowing the error. - Show reason-specific omitted-image messages (processor unavailable, malformed URL, decode error, or size limit). Group multiple failures by reason to keep output concise.
- Load