#!/usr/bin/env node
import "./chunk-CzxHAdEc.js";
import { o as logger_default, w as getEnvString } from "./logger-CjS9Mbik.js";
import { s as REQUEST_TIMEOUT_MS } from "./fetch-Brl8CXgW.js";
import "./cloud-CJaIuNs9.js";
import { a as fetchWithCache } from "./cache-B1ROlJSH.js";

//#region src/providers/hyperbolic/image.ts
const HYPERBOLIC_IMAGE_MODELS = [
	{
		id: "Flux.1-dev",
		aliases: [
			"flux-dev",
			"flux.1-dev",
			"FLUX.1-dev"
		],
		cost: .025
	},
	{
		id: "SDXL1.0-base",
		aliases: ["sdxl", "sdxl-base"],
		cost: .01
	},
	{
		id: "SD1.5",
		aliases: ["stable-diffusion-1.5", "sd-1.5"],
		cost: .005
	},
	{
		id: "SD2",
		aliases: ["stable-diffusion-2", "sd-2"],
		cost: .008
	},
	{
		id: "SSD",
		aliases: ["segmind-sd-1b"],
		cost: .005
	},
	{
		id: "SDXL-turbo",
		aliases: ["sdxl-turbo"],
		cost: .008
	},
	{
		id: "SDXL-ControlNet",
		aliases: ["sdxl-controlnet"],
		cost: .015
	},
	{
		id: "SD1.5-ControlNet",
		aliases: ["sd1.5-controlnet"],
		cost: .008
	}
];
function formatHyperbolicImageOutput(imageData, _prompt, responseFormat) {
	if (responseFormat === "b64_json") return JSON.stringify({ data: [{ b64_json: imageData }] });
	else {
		let mimeType = "image/jpeg";
		if (imageData.startsWith("/9j/")) mimeType = "image/jpeg";
		else if (imageData.startsWith("iVBORw0KGgo")) mimeType = "image/png";
		else if (imageData.startsWith("UklGR")) mimeType = "image/webp";
		return `data:${mimeType};base64,${imageData}`;
	}
}
var HyperbolicImageProvider = class {
	modelName;
	config;
	env;
	constructor(modelName, options = {}) {
		this.modelName = modelName;
		this.config = options.config || {};
		this.env = options.env;
	}
	getApiKey() {
		if (this.config?.apiKey) return this.config.apiKey;
		return this.env?.HYPERBOLIC_API_KEY || getEnvString("HYPERBOLIC_API_KEY");
	}
	getApiUrl() {
		return this.config?.apiBaseUrl || "https://api.hyperbolic.xyz/v1";
	}
	id() {
		return `hyperbolic:image:${this.modelName}`;
	}
	toString() {
		return `[Hyperbolic Image Provider ${this.modelName}]`;
	}
	getApiModelName() {
		return HYPERBOLIC_IMAGE_MODELS.find((m) => m.id === this.modelName || m.aliases && m.aliases.includes(this.modelName))?.id || this.modelName;
	}
	calculateImageCost() {
		return HYPERBOLIC_IMAGE_MODELS.find((m) => m.id === this.modelName || m.aliases && m.aliases.includes(this.modelName))?.cost || .01;
	}
	async callApi(prompt, context, _callApiOptions) {
		const apiKey = this.getApiKey();
		if (!apiKey) throw new Error("Hyperbolic API key is not set. Set the HYPERBOLIC_API_KEY environment variable or add `apiKey` to the provider config.");
		const config = {
			...this.config,
			...context?.prompt?.config
		};
		const modelName = config.model_name || this.getApiModelName();
		const endpoint = "/image/generation";
		const responseFormat = config.response_format || "url";
		const body = {
			model_name: modelName,
			prompt,
			height: config.height || 1024,
			width: config.width || 1024,
			backend: config.backend || "auto"
		};
		if (config.prompt_2) body.prompt_2 = config.prompt_2;
		if (config.negative_prompt) body.negative_prompt = config.negative_prompt;
		if (config.negative_prompt_2) body.negative_prompt_2 = config.negative_prompt_2;
		if (config.image) body.image = config.image;
		if (config.strength !== void 0) body.strength = config.strength;
		if (config.seed !== void 0) body.seed = config.seed;
		if (config.cfg_scale !== void 0) body.cfg_scale = config.cfg_scale;
		if (config.sampler) body.sampler = config.sampler;
		if (config.steps !== void 0) body.steps = config.steps;
		if (config.style_preset) body.style_preset = config.style_preset;
		if (config.enable_refiner !== void 0) body.enable_refiner = config.enable_refiner;
		if (config.controlnet_name) body.controlnet_name = config.controlnet_name;
		if (config.controlnet_image) body.controlnet_image = config.controlnet_image;
		if (config.loras) body.loras = config.loras;
		const headers = {
			"Content-Type": "application/json",
			Authorization: `Bearer ${apiKey}`
		};
		let data, status, statusText, latencyMs;
		let cached = false;
		try {
			({data, cached, status, statusText, latencyMs} = await fetchWithCache(`${this.getApiUrl()}${endpoint}`, {
				method: "POST",
				headers,
				body: JSON.stringify(body)
			}, REQUEST_TIMEOUT_MS));
			if (status < 200 || status >= 300) return { error: `API error: ${status} ${statusText}\n${typeof data === "string" ? data : JSON.stringify(data)}` };
		} catch (err) {
			logger_default.error(`API call error: ${String(err)}`);
			return { error: `API call error: ${String(err)}` };
		}
		if (data.error) return { error: typeof data.error === "string" ? data.error : JSON.stringify(data.error) };
		try {
			if (!data.images || !Array.isArray(data.images) || data.images.length === 0) return { error: "No images returned from API" };
			const imageData = data.images[0].image;
			const cost = cached ? 0 : this.calculateImageCost();
			return {
				output: formatHyperbolicImageOutput(imageData, prompt, responseFormat),
				cached,
				latencyMs,
				cost,
				...responseFormat === "b64_json" ? {
					isBase64: true,
					format: "json"
				} : {}
			};
		} catch (err) {
			return { error: `API error: ${String(err)}: ${JSON.stringify(data)}` };
		}
	}
};
function createHyperbolicImageProvider(providerPath, options = {}) {
	return new HyperbolicImageProvider(providerPath.split(":").slice(2).join(":") || "SDXL1.0-base", options);
}

//#endregion
export { createHyperbolicImageProvider };
//# sourceMappingURL=image-CqkZU55T.js.map