call-ai
Version:
Lightweight library for making AI API calls with streaming support
100 lines • 4.72 kB
JavaScript
import { callAiFetch, joinUrlParts } from "./utils.js";
import { callAiEnv } from "./env.js";
import { PACKAGE_VERSION } from "./version.js";
export async function imageGen(prompt, options = {}) {
const { model = "gpt-image-1", apiKey = "VIBES_DIY", debug = false, size = "1024x1024" } = options;
if (debug) {
console.log(`[imageGen:${PACKAGE_VERSION}] Generating image with prompt: ${prompt.substring(0, 50)}...`);
console.log(`[imageGen:${PACKAGE_VERSION}] Using model: ${model}`);
}
const customOrigin = options.imgUrl || callAiEnv.CALLAI_IMG_URL;
try {
if (!options.images || options.images.length === 0) {
const origin = customOrigin || callAiEnv.def.CALLAI_CHAT_URL;
const generateEndpoint = joinUrlParts(origin, "/api/openai-image/generate");
const response = await callAiFetch(options)(generateEndpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model,
prompt,
size,
...(options.quality && { quality: options.quality }),
...(options.style && { style: options.style }),
}),
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(`Image generation failed: ${response.status} ${response.statusText} - ${errorData}`);
}
const responseText = await response.text();
if (debug) {
console.log(`[imageGen:${PACKAGE_VERSION}] Raw response:`, responseText.substring(0, 500) + "...");
}
try {
const result = JSON.parse(responseText);
return result;
}
catch (parseError) {
if (debug) {
console.error(`[imageGen:${PACKAGE_VERSION}] JSON Parse Error:`, parseError);
console.error(`[imageGen:${PACKAGE_VERSION}] Response text length:`, responseText.length);
console.error(`[imageGen:${PACKAGE_VERSION}] Response sample:`, responseText.substring(0, 1000));
}
throw new Error(`Failed to parse JSON response: ${parseError instanceof Error ? parseError.message : "Unknown error"}`);
}
}
else {
const formData = new FormData();
formData.append("model", model);
formData.append("prompt", prompt);
options.images.forEach((image, index) => {
formData.append(`image_${index}`, image);
});
formData.append("size", size);
if (options.quality)
formData.append("quality", options.quality);
if (options.style)
formData.append("style", options.style);
const origin = customOrigin || callAiEnv.def.CALLAI_CHAT_URL;
const editEndpoint = joinUrlParts(origin, "/api/openai-image/edit");
const response = await callAiFetch(options)(editEndpoint, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
},
body: formData,
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(`Image editing failed: ${response.status} ${response.statusText} - ${errorData}`);
}
const responseText = await response.text();
if (debug) {
console.log(`[imageGen:${PACKAGE_VERSION}] Raw response:`, responseText.substring(0, 500) + "...");
}
try {
const result = JSON.parse(responseText);
return result;
}
catch (parseError) {
if (debug) {
console.error(`[imageGen:${PACKAGE_VERSION}] JSON Parse Error:`, parseError);
console.error(`[imageGen:${PACKAGE_VERSION}] Response text length:`, responseText.length);
console.error(`[imageGen:${PACKAGE_VERSION}] Response sample:`, responseText.substring(0, 1000));
}
throw new Error(`Failed to parse JSON response: ${parseError instanceof Error ? parseError.message : "Unknown error"}`);
}
}
}
catch (error) {
if (debug) {
console.error(`[imageGen:${PACKAGE_VERSION}] Error:`, error);
}
throw error;
}
}
//# sourceMappingURL=image.js.map