fix(image): load Photon via runtime eval on compiled binaries; surface typed errors in omitted-image message

Merged
AL
6mo ago
+53-7

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)

  1. Real: Photon WASM fails to load in the Bun compiled binary on Windows. The patched photon_rs.js falls back to require("path").join(__dirname, "photon_rs_bg.wasm") when globalThis.__OPENCODE_PHOTON_WASM_PATH is unset at the right moment, and Bun's static bundling evaluates that require before the global is observable, so the wasm path never resolves under B:/~BUN/root/. image.normalize() then fails with PhotonUnavailableError.
  2. Hiding the real bug: the loader's catch { return null } swallowed the import error silently, and session/processor.ts hard-coded the omitted-image message to the size-failure wording regardless of which Image.Error tag 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), evaluate photon_rs.js via new Function(...) over await Bun.file(photonJs).text() after setting __OPENCODE_PHOTON_WASM_PATH. The patched module then reads from the embedded wasm asset. Falls back to plain await 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.findErrorOption and 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 typecheck from packages/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 (Read tool): image attaches, no omitted line.
    • 67-byte malformed PNG: [1 image omitted: could not decode image data.] (accurate DecodeError, not misleading size).
  • Confirmed against the installed bcode 0.1.5 binary 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-node in compiled binaries by evaluating the embedded photon_rs.js via new Function(...) after setting __OPENCODE_PHOTON_WASM_PATH. Falls back to dynamic import in dev/test.
    • Log Photon load failures with log.warn instead 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.