import { n as assertOkOrThrowHttpError } from "./provider-http-errors-B-4ePZb-.js";
import { c as postJsonRequest } from "./shared-CxpgWZfA.js";
import { r as isProviderApiKeyConfigured } from "./provider-auth-D_i8TBze.js";
import "./provider-http-BMg8yvY2.js";
import { d as resolveVydraRequestContext, f as resolveVydraResponseJobId, l as resolveCompletedVydraPayload, n as DEFAULT_VYDRA_IMAGE_MODEL, o as downloadVydraAsset, p as resolveVydraResponseStatus, s as extractVydraResultUrls, u as resolveVydraGeneratedMediaMaxBytes } from "./shared-VCDXnte8.js";
//#region extensions/vydra/image-generation-provider.ts
function buildVydraImageGenerationProvider() {
	return {
		id: "vydra",
		label: "Vydra",
		defaultModel: DEFAULT_VYDRA_IMAGE_MODEL,
		models: [DEFAULT_VYDRA_IMAGE_MODEL],
		isConfigured: ({ agentDir }) => isProviderApiKeyConfigured({
			provider: "vydra",
			agentDir
		}),
		capabilities: {
			generate: {
				maxCount: 1,
				supportsSize: false,
				supportsAspectRatio: false,
				supportsResolution: false
			},
			edit: {
				enabled: false,
				maxCount: 1,
				maxInputImages: 0,
				supportsSize: false,
				supportsAspectRatio: false,
				supportsResolution: false
			}
		},
		async generateImage(req) {
			if ((req.inputImages?.length ?? 0) > 0) throw new Error("Vydra image generation currently supports text-to-image only in the bundled plugin.");
			if ((req.count ?? 1) > 1) throw new Error("Vydra image generation supports at most one image per request.");
			const { fetchFn, baseUrl, allowPrivateNetwork, headers, dispatcherPolicy } = await resolveVydraRequestContext({
				cfg: req.cfg,
				agentDir: req.agentDir,
				authStore: req.authStore,
				capability: "image"
			});
			const model = req.model?.trim() || "grok-imagine";
			const { response, release } = await postJsonRequest({
				url: `${baseUrl}/models/${model}`,
				headers,
				body: {
					prompt: req.prompt,
					model: "text-to-image"
				},
				timeoutMs: req.timeoutMs,
				fetchFn,
				allowPrivateNetwork,
				ssrfPolicy: req.ssrfPolicy,
				dispatcherPolicy
			});
			try {
				await assertOkOrThrowHttpError(response, "Vydra image generation failed");
				const submitted = await response.json();
				const completedPayload = await resolveCompletedVydraPayload({
					submitted,
					baseUrl,
					headers,
					timeoutMs: req.timeoutMs,
					fetchFn,
					kind: "image",
					missingJobIdMessage: "Vydra image generation response missing job id"
				});
				const imageUrl = extractVydraResultUrls(completedPayload, "image")[0];
				if (!imageUrl) throw new Error("Vydra image generation completed without an image URL");
				const image = await downloadVydraAsset({
					url: imageUrl,
					kind: "image",
					timeoutMs: req.timeoutMs,
					fetchFn,
					maxBytes: resolveVydraGeneratedMediaMaxBytes({
						cfg: req.cfg,
						kind: "image"
					})
				});
				return {
					images: [{
						buffer: image.buffer,
						mimeType: image.mimeType,
						fileName: image.fileName
					}],
					model,
					metadata: {
						jobId: resolveVydraResponseJobId(completedPayload) ?? resolveVydraResponseJobId(submitted),
						imageUrl,
						status: resolveVydraResponseStatus(completedPayload) ?? "completed"
					}
				};
			} finally {
				await release();
			}
		}
	};
}
//#endregion
export { buildVydraImageGenerationProvider as t };
