Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/minimax/src/images.ts

Raw
import type {
	AssistantImages,
	ImageApi,
	ImageModel,
	ImagesContext,
	ImagesOptions,
	Usage,
} from "@earendil-works/pi-ai";
import type {
	ExtensionAPI,
	ExtensionContext,
} from "@earendil-works/pi-coding-agent";
import { credentials, region } from "./access.ts";
import { ORIGIN, object, request } from "./api.ts";
import { dbg } from "./debug.ts";
import { observeQuota, type QuotaLeft } from "./quota.ts";

export const IMAGE_PROVIDER = "minimax-media";
export const IMAGE_API = "minimax-mplan-images";
export const PRICE_SOURCE =
	"https://platform.minimax.io/docs/pricing/overview.md";

export function estimatedUsage(usd: number): Usage {
	// Zero counts mean no reported LLM tokens, never free service.
	return {
		input: 0,
		output: 0,
		cacheRead: 0,
		cacheWrite: 0,
		totalTokens: 0,
		cost: {
			input: 0,
			output: usd,
			cacheRead: 0,
			cacheWrite: 0,
			total: usd,
			totalFromProvider: false,
		},
	};
}

export async function generateImages(
	pi: ExtensionAPI,
	ctx: ExtensionContext,
	model: ImageModel<ImageApi>,
	context: ImagesContext,
	options: ImagesOptions = {},
): Promise<AssistantImages> {
	let requestedImages = 0;
	let key: string | undefined;
	const result: AssistantImages = {
		api: IMAGE_API,
		provider: IMAGE_PROVIDER,
		model: model.id,
		output: [],
		stopReason: "error",
		timestamp: Date.now(),
	};
	try {
		region(pi, ctx);
		if (model.id !== "image-01" || model.baseUrl !== ORIGIN)
			throw new Error("Unsupported MiniMax image model/endpoint.");
		const extra = options as ImagesOptions & Record<string, unknown>;
		const allowed = new Set([
			"signal",
			"apiKey",
			"headers",
			"env",
			"identity",
			"telemetry",
			"fetch",
			"timeoutMs",
			"maxRetries",
			"maxRetryDelayMs",
			"n",
			"width",
			"height",
			"seed",
			"metadata",
		]);
		if (
			Object.keys(extra).some((k) => !allowed.has(k)) ||
			(options.metadata && Object.keys(options.metadata).length)
		)
			throw new Error(
				"Unsupported MiniMax image option; accepts n, width, height, seed only. References/editing unverified.",
			);
		if (!context.input.length || context.input.some((b) => b.type !== "text"))
			throw new Error(
				"MiniMax reference-image generation unverified; text input only.",
			);
		const prompt = context.input
			.map((b) => (b.type === "text" ? b.text : ""))
			.join("\n");
		if (!prompt.trim() || prompt.length > 1500)
			throw new Error("MiniMax image prompt must contain 1–1500 characters.");
		const n = extra["n"] ?? 1,
			width = extra["width"] ?? 512,
			height = extra["height"] ?? 512,
			seed = extra["seed"];
		if (!Number.isInteger(n) || Number(n) < 1 || Number(n) > 9)
			throw new Error("MiniMax n must be integer 1–9.");
		for (const size of [width, height])
			if (
				!Number.isInteger(size) ||
				Number(size) < 512 ||
				Number(size) > 2048 ||
				Number(size) % 8
			)
				throw new Error(
					"MiniMax dimensions must be multiples of 8 within 512–2048.",
				);
		if (Number(width) * Number(height) > 2048 * 2048)
			throw new Error("MiniMax image dimensions too large.");
		if (
			seed !== undefined &&
			(!Number.isSafeInteger(seed) ||
				Number(seed) < 0 ||
				Number(seed) > 2147483647)
		)
			throw new Error("MiniMax seed must be integer 0–2147483647.");
		requestedImages = Number(n);
		key = await credentials(ctx, options.signal);
		const data = await request(
			key,
			"/v1/image_generation",
			{
				model: "image-01",
				prompt,
				n,
				width,
				height,
				...(seed === undefined ? {} : { seed }),
				response_format: "base64",
				prompt_optimizer: false,
			},
			options.signal,
			"POST",
			options.fetch,
			Math.min(options.timeoutMs ?? 120000, 120000),
		);
		const images = object(data["data"])["image_base64"];
		if (
			!Array.isArray(images) ||
			images.length < 1 ||
			images.length > Number(n)
		)
			throw new Error(
				"MiniMax incompatible image response: expected base64 images; no URL fallback.",
			);
		if (typeof data["id"] === "string") result.responseId = data["id"];
		for (const encoded of images) {
			if (
				typeof encoded !== "string" ||
				!encoded.length ||
				encoded.length > 12 * 1024 * 1024 ||
				!/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)
			)
				throw new Error("MiniMax invalid/oversized base64 image.");
			const bytes = Buffer.from(encoded, "base64");
			const mimeType = bytes.subarray(0, 3).equals(Buffer.from([255, 216, 255]))
				? "image/jpeg"
				: bytes
							.subarray(0, 8)
							.equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
					? "image/png"
					: undefined;
			if (!mimeType) throw new Error("MiniMax image format unrecognized.");
			result.output.push({ type: "image", data: encoded, mimeType });
		}
		result.stopReason = "stop";
		dbg?.("request.finish", { operation: "image", success: true });
	} catch (error) {
		result.stopReason = options.signal?.aborted ? "aborted" : "error";
		result.errorMessage =
			error instanceof Error
				? error.message
				: "MiniMax image operation failed.";
		dbg?.("request.finish", { operation: "image", success: false });
	}
	const count = result.output.filter((block) => block.type === "image").length;
	if (count > 0) {
		// Images returned means allowance was consumed, even on partial results.
		const quota: QuotaLeft | undefined = key
			? await observeQuota(pi, key, options.signal, options.fetch)
			: undefined;
		const partial = result.stopReason !== "stop" || count !== requestedImages;
		result.usage = estimatedUsage(count * 0.0035);
		result.output.push({
			type: "text",
			text: JSON.stringify({
				operation: "image",
				model: "image-01",
				actualImages: count,
				requestedImages,
				partial,
				accounting: {
					kind: "catalog-estimate",
					usd: count * 0.0035,
					unit: "image",
					rate: 0.0035,
					source: PRICE_SOURCE,
					observedAt: "2026-11-09",
					tokens: "not reported",
					providerCharge: "unknown",
					creditsConsumed: "unknown",
					...(partial ? { remainingConsumption: "unknown" } : {}),
				},
				...(quota ? { quota } : {}),
			}),
		});
	}
	return result;
}