Restored to '84d7de2da64f8f2fd1e2d64959817c64c8fb7c04'

Replit-Restored-To: 84d7de2da6
This commit is contained in:
sebastjanartic
2026-02-28 20:36:50 +00:00
parent 802282187e
commit c441cc1594
76 changed files with 4644 additions and 212 deletions
@@ -0,0 +1,59 @@
import fs from "node:fs";
import OpenAI, { toFile } from "openai";
import { Buffer } from "node:buffer";
export const openai = new OpenAI({
apiKey: process.env.AI_INTEGRATIONS_OPENAI_API_KEY,
baseURL: process.env.AI_INTEGRATIONS_OPENAI_BASE_URL,
});
/**
* Generate an image and return as Buffer.
* Uses gpt-image-1 model via Replit AI Integrations.
*/
export async function generateImageBuffer(
prompt: string,
size: "1024x1024" | "512x512" | "256x256" = "1024x1024"
): Promise<Buffer> {
const response = await openai.images.generate({
model: "gpt-image-1",
prompt,
size,
});
const base64 = response.data[0]?.b64_json ?? "";
return Buffer.from(base64, "base64");
}
/**
* Edit/combine multiple images into a composite.
* Uses gpt-image-1 model via Replit AI Integrations.
*/
export async function editImages(
imageFiles: string[],
prompt: string,
outputPath?: string
): Promise<Buffer> {
const images = await Promise.all(
imageFiles.map((file) =>
toFile(fs.createReadStream(file), file, {
type: "image/png",
})
)
);
const response = await openai.images.edit({
model: "gpt-image-1",
image: images,
prompt,
});
const imageBase64 = response.data[0]?.b64_json ?? "";
const imageBytes = Buffer.from(imageBase64, "base64");
if (outputPath) {
fs.writeFileSync(outputPath, imageBytes);
}
return imageBytes;
}