mirror of
https://github.com/earendil-works/pi.git
synced 2026-06-18 15:54:04 +08:00
refactor(coding-agent): resize images from bytes
This commit is contained in:
@@ -50,13 +50,12 @@ export async function processFileArguments(fileArgs: string[], options?: Process
|
||||
if (mimeType) {
|
||||
// Handle image file
|
||||
const content = await readFile(absolutePath);
|
||||
const base64Content = content.toString("base64");
|
||||
|
||||
let attachment: ImageContent;
|
||||
let dimensionNote: string | undefined;
|
||||
|
||||
if (autoResizeImages) {
|
||||
const resized = await resizeImage({ type: "image", data: base64Content, mimeType });
|
||||
const resized = await resizeImage(content, mimeType);
|
||||
if (!resized) {
|
||||
text += `<file name="${absolutePath}">[Image omitted: could not be resized below the inline image size limit.]</file>\n`;
|
||||
continue;
|
||||
@@ -71,7 +70,7 @@ export async function processFileArguments(fileArgs: string[], options?: Process
|
||||
attachment = {
|
||||
type: "image",
|
||||
mimeType,
|
||||
data: base64Content,
|
||||
data: content.toString("base64"),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -250,10 +250,9 @@ export function createReadToolDefinition(
|
||||
if (mimeType) {
|
||||
// Read image as binary.
|
||||
const buffer = await ops.readFile(absolutePath);
|
||||
const base64 = buffer.toString("base64");
|
||||
if (autoResizeImages) {
|
||||
// Resize image if needed before sending it back to the model.
|
||||
const resized = await resizeImage({ type: "image", data: base64, mimeType });
|
||||
const resized = await resizeImage(buffer, mimeType);
|
||||
if (!resized) {
|
||||
let textNote = `Read image file [${mimeType}]\n[Image omitted: could not be resized below the inline image size limit.]`;
|
||||
if (nonVisionImageNote) textNote += `\n${nonVisionImageNote}`;
|
||||
@@ -273,7 +272,7 @@ export function createReadToolDefinition(
|
||||
if (nonVisionImageNote) textNote += `\n${nonVisionImageNote}`;
|
||||
content = [
|
||||
{ type: "text", text: textNote },
|
||||
{ type: "image", data: base64, mimeType },
|
||||
{ type: "image", data: buffer.toString("base64"), mimeType },
|
||||
];
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { ImageContent } from "@earendil-works/pi-ai";
|
||||
import { applyExifOrientation } from "./exif-orientation.js";
|
||||
import { loadPhoton } from "./photon.js";
|
||||
|
||||
@@ -57,10 +56,13 @@ function encodeCandidate(buffer: Uint8Array, mimeType: string): EncodedCandidate
|
||||
* 3. If still too large, try JPEG with decreasing quality
|
||||
* 4. If still too large, progressively reduce dimensions until 1x1
|
||||
*/
|
||||
export async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage | null> {
|
||||
export async function resizeImage(
|
||||
inputBytes: Uint8Array,
|
||||
mimeType: string,
|
||||
options?: ImageResizeOptions,
|
||||
): Promise<ResizedImage | null> {
|
||||
const opts = { ...DEFAULT_OPTIONS, ...options };
|
||||
const inputBuffer = Buffer.from(img.data, "base64");
|
||||
const inputBase64Size = Buffer.byteLength(img.data, "utf-8");
|
||||
const inputBase64Size = Math.ceil(inputBytes.byteLength / 3) * 4;
|
||||
|
||||
const photon = await loadPhoton();
|
||||
if (!photon) {
|
||||
@@ -69,20 +71,19 @@ export async function resizeImage(img: ImageContent, options?: ImageResizeOption
|
||||
|
||||
let image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;
|
||||
try {
|
||||
const inputBytes = new Uint8Array(inputBuffer);
|
||||
const rawImage = photon.PhotonImage.new_from_byteslice(inputBytes);
|
||||
image = applyExifOrientation(photon, rawImage, inputBytes);
|
||||
if (image !== rawImage) rawImage.free();
|
||||
|
||||
const originalWidth = image.get_width();
|
||||
const originalHeight = image.get_height();
|
||||
const format = img.mimeType?.split("/")[1] ?? "png";
|
||||
const format = mimeType.split("/")[1] ?? "png";
|
||||
|
||||
// Check if already within all limits (dimensions AND encoded size)
|
||||
if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && inputBase64Size < opts.maxBytes) {
|
||||
return {
|
||||
data: img.data,
|
||||
mimeType: img.mimeType ?? `image/${format}`,
|
||||
data: Buffer.from(inputBytes).toString("base64"),
|
||||
mimeType: mimeType || `image/${format}`,
|
||||
originalWidth,
|
||||
originalHeight,
|
||||
width: originalWidth,
|
||||
|
||||
Reference in New Issue
Block a user