venice-dev-tools
Version:
unOfficial SDK for the Venice AI API
198 lines (194 loc) • 6.96 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
VeniceNode: () => VeniceNode,
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
// src/venice-node.ts
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var os = __toESM(require("os"));
var crypto = __toESM(require("crypto"));
var import_core = require("@venice-dev-tools/core");
var VeniceNode = class extends import_core.VeniceAI {
/**
* Create a new Node.js Venice AI client.
*
* @param config - Configuration options for the client.
*/
constructor(config = {}) {
if (!config.apiKey) {
config.apiKey = process.env.VENICE_API_KEY;
}
super(config);
}
/**
* Get the current API key.
*
* @returns The current API key.
* @throws Error with a user-friendly message if no API key is set.
*/
getApiKey() {
const apiKey = super.getApiKey();
if (!apiKey) {
const error = new Error(
"No API key found. Please provide an API key using one of these methods:\n1. Use the --api-key or -k option: venice -k YOUR_API_KEY ...\n2. Set the VENICE_API_KEY environment variable\n3. Save your API key using: venice set-key YOUR_API_KEY"
);
error.name = "VeniceAuthError";
throw error;
}
return apiKey;
}
/**
* Load configuration from a JSON file.
*
* @param filePath - Path to the configuration file.
* @returns This client instance.
*/
loadConfigFromFile(filePath) {
try {
const configData = JSON.parse(fs.readFileSync(filePath, "utf8"));
if (configData.apiKey) {
this.setApiKey(configData.apiKey);
}
return this;
} catch (error) {
throw new Error(`Failed to load configuration: ${error.message}`);
}
}
/**
* Save an image from a base64 string to a file.
*
* @param base64Data - The base64 image data (can include or exclude data URI prefix).
* @param outputPath - Path where the image should be saved.
* @returns The absolute path to the saved file.
*/
saveImageToFile(base64Data, outputPath) {
const base64Image = base64Data.replace(/^data:image\/\w+;base64,/, "");
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputPath, Buffer.from(base64Image, "base64"));
return path.resolve(outputPath);
}
/**
* Generate an image and save it directly to a file.
*
* @param options - The image generation options with additional save options.
* @param outputPath - Path where the image should be saved.
* @returns The image generation response and the saved file path.
*/
async generateImageToFile(options, outputPath) {
const response = await this.images.generate(options);
if (response.data && response.data.url) {
const filePath = this.saveImageToFile(response.data.url, outputPath);
return { response, filePath };
}
throw new Error("No images were generated");
}
/**
* Load an image file as a base64 string.
*
* @param filePath - Path to the image file.
* @param includeDataUri - Whether to include the data URI prefix.
* @returns The base64 encoded image string.
*/
loadImageAsBase64(filePath, includeDataUri = false) {
const fileBuffer = fs.readFileSync(filePath);
const base64Data = fileBuffer.toString("base64");
if (includeDataUri) {
const extension = path.extname(filePath).toLowerCase();
let mimeType = "image/png";
if (extension === ".jpg" || extension === ".jpeg") {
mimeType = "image/jpeg";
} else if (extension === ".webp") {
mimeType = "image/webp";
} else if (extension === ".gif") {
mimeType = "image/gif";
}
return `data:${mimeType};base64,${base64Data}`;
}
return base64Data;
}
/**
* Read an image file as a Buffer.
*
* @param filePath - Path to the image file.
* @returns The image buffer.
*/
readImageFile(filePath) {
return fs.readFileSync(filePath);
}
/**
* Create a temporary file with a unique name.
*
* @param extension - File extension (without the dot).
* @param prefix - Optional file name prefix.
* @returns The path to the temporary file.
*/
createTempFilePath(extension = "png", prefix = "venice-") {
const tempDir = os.tmpdir();
const fileName = `${prefix}${crypto.randomBytes(16).toString("hex")}.${extension}`;
return path.join(tempDir, fileName);
}
/**
* Save the API key to a configuration file.
*
* @param apiKey - The API key to save.
* @param filePath - Path to the configuration file.
*/
saveApiKey(apiKey, filePath) {
const configDir = path.dirname(filePath);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
let config = {};
if (fs.existsSync(filePath)) {
try {
config = JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch (error) {
}
}
config = { ...config, apiKey };
fs.writeFileSync(filePath, JSON.stringify(config, null, 2));
}
};
// src/index.ts
__reExport(index_exports, require("@venice-dev-tools/core"), module.exports);
var index_default = VeniceNode;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
VeniceNode,
...require("@venice-dev-tools/core")
});