Files
pi/packages/coding-agent/src/utils/image-convert.ts
T
Mario Zechner 5aa0689828 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
2026-01-16 21:11:01 +01:00

34 lines
927 B
TypeScript

// 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.
* Kitty graphics protocol requires PNG format (f=100).
*/
export async function convertToPng(
base64Data: string,
mimeType: string,
): Promise<{ data: string; mimeType: string } | null> {
// Already PNG, no conversion needed
if (mimeType === "image/png") {
return { data: base64Data, mimeType };
}
try {
const image = PhotonImage.new_from_byteslice(new Uint8Array(Buffer.from(base64Data, "base64")));
try {
const pngBuffer = image.get_bytes();
return {
data: Buffer.from(pngBuffer).toString("base64"),
mimeType: "image/png",
};
} finally {
image.free();
}
} catch {
// Conversion failed
return null;
}
}