mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 20:03:05 +00:00
build(coding-agent): replaced wasm-vips with @silvia-odwyer/photon-node for image processing (#710)
- Replaced wasm-vips dependency with @silvia-odwyer/photon-node for image processing.
This commit is contained in:
parent
95859725b7
commit
6bf073f130
7 changed files with 148 additions and 225 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import { getVips } from "./vips.js";
|
||||
import photon from "@silvia-odwyer/photon-node";
|
||||
|
||||
/**
|
||||
* Convert image to PNG format for terminal display.
|
||||
|
|
@ -13,21 +13,17 @@ export async function convertToPng(
|
|||
return { data: base64Data, mimeType };
|
||||
}
|
||||
|
||||
const vips = await getVips();
|
||||
if (!vips) {
|
||||
// wasm-vips not available
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const buffer = Buffer.from(base64Data, "base64");
|
||||
const img = vips.Image.newFromBuffer(buffer);
|
||||
const pngBuffer = img.writeToBuffer(".png");
|
||||
img.delete();
|
||||
return {
|
||||
data: Buffer.from(pngBuffer).toString("base64"),
|
||||
mimeType: "image/png",
|
||||
};
|
||||
const image = photon.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;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { ImageContent } from "@mariozechner/pi-ai";
|
||||
import { getVips } from "./vips.js";
|
||||
import photon from "@silvia-odwyer/photon-node";
|
||||
|
||||
export interface ImageResizeOptions {
|
||||
maxWidth?: number; // Default: 2000
|
||||
|
|
@ -40,8 +40,8 @@ function pickSmaller(
|
|||
* Resize an image to fit within the specified max dimensions and file size.
|
||||
* Returns the original image if it already fits within the limits.
|
||||
*
|
||||
* Uses wasm-vips for image processing. If wasm-vips is not available (e.g., in some
|
||||
* environments), returns the original image unchanged.
|
||||
* Uses Photon (Rust/WASM) for image processing. If Photon is not available,
|
||||
* returns the original image unchanged.
|
||||
*
|
||||
* Strategy for staying under maxBytes:
|
||||
* 1. First resize to maxWidth/maxHeight
|
||||
|
|
@ -51,126 +51,74 @@ function pickSmaller(
|
|||
*/
|
||||
export async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage> {
|
||||
const opts = { ...DEFAULT_OPTIONS, ...options };
|
||||
const buffer = Buffer.from(img.data, "base64");
|
||||
const inputBuffer = Buffer.from(img.data, "base64");
|
||||
|
||||
const vipsOrNull = await getVips();
|
||||
if (!vipsOrNull) {
|
||||
// wasm-vips not available - return original image
|
||||
// We can't get dimensions without vips, so return 0s
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType,
|
||||
originalWidth: 0,
|
||||
originalHeight: 0,
|
||||
width: 0,
|
||||
height: 0,
|
||||
wasResized: false,
|
||||
};
|
||||
}
|
||||
// Capture non-null reference for use in nested functions
|
||||
const vips = vipsOrNull;
|
||||
|
||||
// Load image to get metadata
|
||||
let sourceImg: InstanceType<typeof vips.Image>;
|
||||
let image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;
|
||||
try {
|
||||
sourceImg = vips.Image.newFromBuffer(buffer);
|
||||
} catch {
|
||||
// Failed to load image
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType,
|
||||
originalWidth: 0,
|
||||
originalHeight: 0,
|
||||
width: 0,
|
||||
height: 0,
|
||||
wasResized: false,
|
||||
};
|
||||
}
|
||||
image = photon.PhotonImage.new_from_byteslice(new Uint8Array(inputBuffer));
|
||||
|
||||
const originalWidth = sourceImg.width;
|
||||
const originalHeight = sourceImg.height;
|
||||
|
||||
// Check if already within all limits (dimensions AND size)
|
||||
const originalSize = buffer.length;
|
||||
if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {
|
||||
sourceImg.delete();
|
||||
const originalWidth = image.get_width();
|
||||
const originalHeight = image.get_height();
|
||||
const format = img.mimeType?.split("/")[1] ?? "png";
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType ?? `image/${format}`,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: originalWidth,
|
||||
height: originalHeight,
|
||||
wasResized: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Calculate initial dimensions respecting max limits
|
||||
let targetWidth = originalWidth;
|
||||
let targetHeight = originalHeight;
|
||||
|
||||
if (targetWidth > opts.maxWidth) {
|
||||
targetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);
|
||||
targetWidth = opts.maxWidth;
|
||||
}
|
||||
if (targetHeight > opts.maxHeight) {
|
||||
targetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);
|
||||
targetHeight = opts.maxHeight;
|
||||
}
|
||||
|
||||
// Helper to resize and encode in both formats, returning the smaller one
|
||||
function tryBothFormats(
|
||||
width: number,
|
||||
height: number,
|
||||
jpegQuality: number,
|
||||
): { buffer: Uint8Array; mimeType: string } {
|
||||
// Load image fresh and resize using scale factor
|
||||
// (Using newFromBuffer + resize instead of thumbnailBuffer to avoid lazy re-read issues)
|
||||
const img = vips.Image.newFromBuffer(buffer);
|
||||
const scale = Math.min(width / img.width, height / img.height);
|
||||
const resized = scale < 1 ? img.resize(scale) : img;
|
||||
|
||||
const pngBuffer = resized.writeToBuffer(".png");
|
||||
const jpegBuffer = resized.writeToBuffer(".jpg", { Q: jpegQuality });
|
||||
|
||||
if (resized !== img) {
|
||||
resized.delete();
|
||||
// Check if already within all limits (dimensions AND size)
|
||||
const originalSize = inputBuffer.length;
|
||||
if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType ?? `image/${format}`,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: originalWidth,
|
||||
height: originalHeight,
|
||||
wasResized: false,
|
||||
};
|
||||
}
|
||||
img.delete();
|
||||
|
||||
return pickSmaller({ buffer: pngBuffer, mimeType: "image/png" }, { buffer: jpegBuffer, mimeType: "image/jpeg" });
|
||||
}
|
||||
// Calculate initial dimensions respecting max limits
|
||||
let targetWidth = originalWidth;
|
||||
let targetHeight = originalHeight;
|
||||
|
||||
// Clean up the source image
|
||||
sourceImg.delete();
|
||||
if (targetWidth > opts.maxWidth) {
|
||||
targetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);
|
||||
targetWidth = opts.maxWidth;
|
||||
}
|
||||
if (targetHeight > opts.maxHeight) {
|
||||
targetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);
|
||||
targetHeight = opts.maxHeight;
|
||||
}
|
||||
|
||||
// Try to produce an image under maxBytes
|
||||
const qualitySteps = [85, 70, 55, 40];
|
||||
const scaleSteps = [1.0, 0.75, 0.5, 0.35, 0.25];
|
||||
// Helper to resize and encode in both formats, returning the smaller one
|
||||
function tryBothFormats(
|
||||
width: number,
|
||||
height: number,
|
||||
jpegQuality: number,
|
||||
): { buffer: Uint8Array; mimeType: string } {
|
||||
const resized = photon.resize(image!, width, height, photon.SamplingFilter.Lanczos3);
|
||||
|
||||
let best: { buffer: Uint8Array; mimeType: string };
|
||||
let finalWidth = targetWidth;
|
||||
let finalHeight = targetHeight;
|
||||
try {
|
||||
const pngBuffer = resized.get_bytes();
|
||||
const jpegBuffer = resized.get_bytes_jpeg(jpegQuality);
|
||||
|
||||
// First attempt: resize to target dimensions, try both formats
|
||||
best = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);
|
||||
return pickSmaller(
|
||||
{ buffer: pngBuffer, mimeType: "image/png" },
|
||||
{ buffer: jpegBuffer, mimeType: "image/jpeg" },
|
||||
);
|
||||
} finally {
|
||||
resized.free();
|
||||
}
|
||||
}
|
||||
|
||||
if (best.buffer.length <= opts.maxBytes) {
|
||||
return {
|
||||
data: Buffer.from(best.buffer).toString("base64"),
|
||||
mimeType: best.mimeType,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
wasResized: true,
|
||||
};
|
||||
}
|
||||
// Try to produce an image under maxBytes
|
||||
const qualitySteps = [85, 70, 55, 40];
|
||||
const scaleSteps = [1.0, 0.75, 0.5, 0.35, 0.25];
|
||||
|
||||
// Still too large - try JPEG with decreasing quality (and compare to PNG each time)
|
||||
for (const quality of qualitySteps) {
|
||||
best = tryBothFormats(targetWidth, targetHeight, quality);
|
||||
let best: { buffer: Uint8Array; mimeType: string };
|
||||
let finalWidth = targetWidth;
|
||||
let finalHeight = targetHeight;
|
||||
|
||||
// First attempt: resize to target dimensions, try both formats
|
||||
best = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);
|
||||
|
||||
if (best.buffer.length <= opts.maxBytes) {
|
||||
return {
|
||||
|
|
@ -183,20 +131,10 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
|
|||
wasResized: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Still too large - reduce dimensions progressively
|
||||
for (const scale of scaleSteps) {
|
||||
finalWidth = Math.round(targetWidth * scale);
|
||||
finalHeight = Math.round(targetHeight * scale);
|
||||
|
||||
// Skip if dimensions are too small
|
||||
if (finalWidth < 100 || finalHeight < 100) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Still too large - try JPEG with decreasing quality
|
||||
for (const quality of qualitySteps) {
|
||||
best = tryBothFormats(finalWidth, finalHeight, quality);
|
||||
best = tryBothFormats(targetWidth, targetHeight, quality);
|
||||
|
||||
if (best.buffer.length <= opts.maxBytes) {
|
||||
return {
|
||||
|
|
@ -210,19 +148,59 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
|
|||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: return smallest version we produced even if over limit
|
||||
// (the API will reject it, but at least we tried everything)
|
||||
return {
|
||||
data: Buffer.from(best.buffer).toString("base64"),
|
||||
mimeType: best.mimeType,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
wasResized: true,
|
||||
};
|
||||
// Still too large - reduce dimensions progressively
|
||||
for (const scale of scaleSteps) {
|
||||
finalWidth = Math.round(targetWidth * scale);
|
||||
finalHeight = Math.round(targetHeight * scale);
|
||||
|
||||
if (finalWidth < 100 || finalHeight < 100) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (const quality of qualitySteps) {
|
||||
best = tryBothFormats(finalWidth, finalHeight, quality);
|
||||
|
||||
if (best.buffer.length <= opts.maxBytes) {
|
||||
return {
|
||||
data: Buffer.from(best.buffer).toString("base64"),
|
||||
mimeType: best.mimeType,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
wasResized: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: return smallest version we produced
|
||||
return {
|
||||
data: Buffer.from(best.buffer).toString("base64"),
|
||||
mimeType: best.mimeType,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: finalWidth,
|
||||
height: finalHeight,
|
||||
wasResized: true,
|
||||
};
|
||||
} catch {
|
||||
// Failed to load image
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType,
|
||||
originalWidth: 0,
|
||||
originalHeight: 0,
|
||||
width: 0,
|
||||
height: 0,
|
||||
wasResized: false,
|
||||
};
|
||||
} finally {
|
||||
if (image) {
|
||||
image.free();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
/**
|
||||
* Singleton wrapper for wasm-vips initialization.
|
||||
* wasm-vips requires async initialization, so we cache the instance.
|
||||
*/
|
||||
|
||||
import type Vips from "wasm-vips";
|
||||
|
||||
let vipsInstance: Awaited<ReturnType<typeof Vips>> | null = null;
|
||||
let vipsInitPromise: Promise<Awaited<ReturnType<typeof Vips>> | null> | null = null;
|
||||
|
||||
/**
|
||||
* Get the initialized wasm-vips instance.
|
||||
* Returns null if wasm-vips is not available or fails to initialize.
|
||||
*/
|
||||
export async function getVips(): Promise<Awaited<ReturnType<typeof Vips>> | null> {
|
||||
if (vipsInstance) {
|
||||
return vipsInstance;
|
||||
}
|
||||
|
||||
if (vipsInitPromise) {
|
||||
return vipsInitPromise;
|
||||
}
|
||||
|
||||
vipsInitPromise = (async () => {
|
||||
try {
|
||||
const VipsInit = (await import("wasm-vips")).default;
|
||||
vipsInstance = await VipsInit();
|
||||
return vipsInstance;
|
||||
} catch {
|
||||
// wasm-vips not available
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
const result = await vipsInitPromise;
|
||||
if (!result) {
|
||||
vipsInitPromise = null; // Allow retry on failure
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue