fix(coding-agent): use lazy-loaded photon wrapper for Node.js compatibility

Previous commit broke Node.js/tsx by using the ESM entry point which
doesn't work with Node. This creates a wrapper module that:
- Uses require() for lazy loading (works in both Node and Bun)
- Gracefully handles load failures (returns original image)
- Works in Node.js, tsx, and Bun compiled binaries
This commit is contained in:
Mario Zechner 2026-01-16 21:18:13 +01:00
parent 5aa0689828
commit 75628e0cfd
5 changed files with 86 additions and 16 deletions

View file

@ -1,7 +1,5 @@
import type { ImageContent } from "@mariozechner/pi-ai";
// Use ESM entry point so Bun can embed the WASM in compiled binaries
// (the CJS entry uses fs.readFileSync which breaks in standalone binaries)
import { PhotonImage, resize, SamplingFilter } from "@silvia-odwyer/photon-node/photon_rs_bg.js";
import { getPhoton } from "./photon.js";
export interface ImageResizeOptions {
maxWidth?: number; // Default: 2000
@ -55,9 +53,23 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
const opts = { ...DEFAULT_OPTIONS, ...options };
const inputBuffer = Buffer.from(img.data, "base64");
let image: ReturnType<typeof PhotonImage.new_from_byteslice> | undefined;
const photon = getPhoton();
if (!photon) {
// Photon not available, return original image
return {
data: img.data,
mimeType: img.mimeType,
originalWidth: 0,
originalHeight: 0,
width: 0,
height: 0,
wasResized: false,
};
}
let image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;
try {
image = PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));
image = photon.PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));
const originalWidth = image.get_width();
const originalHeight = image.get_height();
@ -96,7 +108,7 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
height: number,
jpegQuality: number,
): { buffer: Uint8Array; mimeType: string } {
const resized = resize(image!, width, height, SamplingFilter.Lanczos3);
const resized = photon!.resize(image!, width, height, photon!.SamplingFilter.Lanczos3);
try {
const pngBuffer = resized.get_bytes();