naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
83 lines • 3.45 kB
JavaScript
import OpenAI from "openai";
import path from "path";
import sharp from "sharp";
import stringArgv from "string-argv";
import { genImgCmd } from "../../command/commandDefs.js";
import * as pathService from "../../services/runtime/pathService.js";
export function createGenImg({ globalConfig }, { agentConfig }, costTracker, output, getImageModel) {
/** genimg "<description>" <filepath>: Generate an image with the description and save it to the file path */
async function handleCommand(args) {
// genimg sholdn't even be presented as an available command unless it is defined in the config
if (!agentConfig().imageModel) {
throw "Agent config: Error, 'imageModel' is not defined";
}
const argv = stringArgv(args);
// args already has the command name stripped, so argv[0] is the description
const description = argv[0];
const filepath = argv[1] || "";
if (!description) {
throw "Invalid parameters: Description in quotes and fully qualified filepath with desired image extension are required";
}
if (!filepath) {
throw "Error: Filepath is required";
}
// Validate path is fully qualified (Unix or Windows)
if (!path.isAbsolute(filepath)) {
throw "Error: Filepath must be fully qualified";
}
pathService.ensureFileDirExists(filepath);
const imageModelName = agentConfig().imageModel;
if (!imageModelName) {
throw "Error: imageModel is not defined in agent config";
}
output.commentAndLog(`Generating image with ${imageModelName}...`);
const model = getImageModel(imageModelName);
const apiKey = model.apiKeyVar
? globalConfig().variableMap[model.apiKeyVar]
: undefined;
if (!apiKey) {
throw `Error, set ${model.apiKeyVar} variable`;
}
const openai = new OpenAI({
apiKey,
baseURL: model.baseUrl,
});
const isDalle = model.versionName.startsWith("dall-e");
const response = await openai.images.generate({
prompt: description,
model: model.versionName,
size: model.size,
quality: model.quality,
...(isDalle ? { response_format: "b64_json" } : {}),
});
// save to filepath
if (!response.data || response.data.length === 0) {
throw "Error: No image data returned from OpenAI";
}
const base64Image = response.data[0].b64_json;
if (!base64Image) {
throw 'Error: "b64_json" not found in response';
}
// Convert the base64 string to a buffer
const imageBuffer = Buffer.from(base64Image, "base64");
// Use sharp to convert the buffer and save it as a JPG file
const fileExtension = path.extname(filepath).substring(1);
await sharp(imageBuffer)
/*.resize(512, 512, {
fit: "inside",
})*/
.toFormat(fileExtension)
.toFile(filepath);
// Record the cost
costTracker.recordCost(model.cost, "genimg", model.key);
return "1024x1024 Image generated and saved to " + filepath;
}
const registrableCommand = {
command: genImgCmd,
handleCommand,
};
return {
...registrableCommand,
};
}
//# sourceMappingURL=genImg.js.map