fix(coding-agent): fix standalone binary WASM loading on Linux, fixes #784

- Import photon-node from ESM entry point (photon_rs_bg.js) instead of CJS
  entry, allowing Bun to embed WASM in compiled binaries
- Add photon.d.ts for TypeScript support of ESM entry
- Add scripts/build-binaries.sh for local binary builds
- Simplify GitHub workflow to use the build script
- Add binaries/ to gitignore
This commit is contained in:
Mario Zechner 2026-01-16 20:56:18 +01:00
parent 0c33e0dee5
commit 5aa0689828
7 changed files with 177 additions and 87 deletions

View file

@ -1,4 +1,6 @@
import photon from "@silvia-odwyer/photon-node";
// 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 } from "@silvia-odwyer/photon-node/photon_rs_bg.js";
/**
* Convert image to PNG format for terminal display.
@ -14,7 +16,7 @@ export async function convertToPng(
}
try {
const image = photon.PhotonImage.new_from_byteslice(new Uint8Array(Buffer.from(base64Data, "base64")));
const image = PhotonImage.new_from_byteslice(new Uint8Array(Buffer.from(base64Data, "base64")));
try {
const pngBuffer = image.get_bytes();
return {

View file

@ -1,5 +1,7 @@
import type { ImageContent } from "@mariozechner/pi-ai";
import photon from "@silvia-odwyer/photon-node";
// 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";
export interface ImageResizeOptions {
maxWidth?: number; // Default: 2000
@ -53,9 +55,9 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
const opts = { ...DEFAULT_OPTIONS, ...options };
const inputBuffer = Buffer.from(img.data, "base64");
let image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;
let image: ReturnType<typeof PhotonImage.new_from_byteslice> | undefined;
try {
image = photon.PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));
image = PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));
const originalWidth = image.get_width();
const originalHeight = image.get_height();
@ -94,7 +96,7 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
height: number,
jpegQuality: number,
): { buffer: Uint8Array; mimeType: string } {
const resized = photon.resize(image!, width, height, photon.SamplingFilter.Lanczos3);
const resized = resize(image!, width, height, SamplingFilter.Lanczos3);
try {
const pngBuffer = resized.get_bytes();

View file

@ -0,0 +1,5 @@
// Type declarations for the ESM entry point of @silvia-odwyer/photon-node
// The ESM entry exports the same API but uses WASM imports that bundlers can embed
declare module "@silvia-odwyer/photon-node/photon_rs_bg.js" {
export * from "@silvia-odwyer/photon-node";
}

View file

@ -5,5 +5,5 @@
"rootDir": "./src"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist", "**/*.d.ts", "src/**/*.d.ts"]
"exclude": ["node_modules", "dist"]
}