import { resolveAuthProfileForProvider } from "../../../src/providers/auth-runtime/resolve-auth.js";
import { isProviderApiKeyConfigured } from "../../../src/providers/auth-runtime/is-configured.js";
import "../../../src/providers/auth-runtime/index.js";
import { createOpenAiCompatibleImageProvider } from "../../../src/agent/image/generation/openai-compatible-image-provider.js";
//#region extensions/openai/src/image-generation-provider.ts
/**
* Bundled OpenAI image-generation provider.
*
* Migrated from src/agent/image/generation/openai-generate.ts as part of the
* Step 3 plugin-isation. Step 4 adds:
*   - **Azure OpenAI** routing (when `cfg.providers.openai.azure.{resource,
*     deployment, apiVersion}` is set, or env `AZURE_OPENAI_*`): swaps base
*     URL + uses `api-key` header instead of `Authorization: Bearer`.
*   - **OAuth** support (Codex / future): when the active auth profile is
*     `mode: 'oauth'`, the OAuth access token is sent as a Bearer token.
*     Refresh is NOT performed here — vendors call
*     `refreshOAuthProfile` + `store.save` ahead of time.
*/
const OPENAI_DEFAULT_BASE_URL = "https://api.openai.com/v1";
/** Image models exposed by this extension (OpenAI Images API + compatible hosts). */
const OPENAI_IMAGE_MODELS = [
	"gpt-image-1",
	"dall-e-3",
	"dall-e-2"
];
const OPENAI_DEFAULT_IMAGE_MODEL = OPENAI_IMAGE_MODELS[0];
const OPENAI_IMAGE_UI = {
	baseUrlPresets: [{
		value: OPENAI_DEFAULT_BASE_URL,
		label: "OpenAI API (default)"
	}],
	baseUrlPresetKind: "openai"
};
const OPENAI_CAPABILITIES = {
	generate: {
		maxCount: 4,
		supportsSize: true
	},
	edit: {
		enabled: true,
		maxInputImages: 1,
		supportsSize: true
	},
	geometry: { sizes: [
		"1024x1024",
		"1024x1536",
		"1536x1024"
	] },
	output: {
		qualities: [
			"low",
			"medium",
			"high",
			"auto"
		],
		formats: [
			"png",
			"jpeg",
			"webp"
		],
		backgrounds: [
			"transparent",
			"opaque",
			"auto"
		]
	}
};
function resolveAzureSettings(req) {
	const fromCfg = req.cfg?.providers?.openai?.azure;
	const resource = pickString(fromCfg?.resource) ?? pickEnv("AZURE_OPENAI_RESOURCE");
	const deployment = pickString(fromCfg?.deployment) ?? pickEnv("AZURE_OPENAI_DEPLOYMENT") ?? req.model;
	const apiVersion = pickString(fromCfg?.apiVersion) ?? pickEnv("AZURE_OPENAI_API_VERSION") ?? "2024-08-01-preview";
	if (!resource || !deployment) return void 0;
	return {
		resource,
		deployment,
		apiVersion
	};
}
function pickString(v) {
	return typeof v === "string" && v.trim().length > 0 ? v.trim() : void 0;
}
function pickEnv(name) {
	return pickString(process.env[name]);
}
function resolveOpenAiBaseUrl(req) {
	const cfg = req.cfg;
	const fromCfg = pickString(cfg?.providers?.openai?.baseUrl);
	if (fromCfg) return fromCfg.replace(/\/+$/, "");
	const fromEnv = pickEnv("OPENAI_BASE_URL");
	if (fromEnv) return fromEnv.replace(/\/+$/, "");
	return OPENAI_DEFAULT_BASE_URL;
}
function buildAzureEndpoint(azure) {
	const baseUrl = `https://${azure.resource}.openai.azure.com/openai`;
	const query = `?api-version=${encodeURIComponent(azure.apiVersion)}`;
	return {
		baseUrl,
		generationsPath: `/deployments/${encodeURIComponent(azure.deployment)}/images/generations${query}`,
		editsPath: `/deployments/${encodeURIComponent(azure.deployment)}/images/edits${query}`,
		authorization: {
			kind: "header",
			headerName: "api-key"
		}
	};
}
function buildOpenAIImageGenerationProvider() {
	return createOpenAiCompatibleImageProvider({
		id: "openai",
		label: "OpenAI",
		defaultModel: OPENAI_DEFAULT_IMAGE_MODEL,
		models: [...OPENAI_IMAGE_MODELS],
		capabilities: OPENAI_CAPABILITIES,
		ui: OPENAI_IMAGE_UI,
		isConfigured: (ctx) => isProviderApiKeyConfigured({
			providerId: "openai",
			cfg: ctx.cfg
		}),
		resolveApiKey: (req) => {
			return resolveAuthProfileForProvider({
				providerId: "openai",
				cfg: req.cfg,
				store: req.authStore
			}).apiKey ?? null;
		},
		resolveEndpoint: (req) => {
			const azure = resolveAzureSettings(req);
			if (azure) return buildAzureEndpoint(azure);
			return { baseUrl: resolveOpenAiBaseUrl(req) };
		},
		defaultTimeoutMs: 12e4,
		buildGenerateRequestBody: (req, base) => {
			const m = (req.model || "").toLowerCase();
			if (m === "dall-e-2" || m === "dall-e-3") {
				const cleaned = { ...base };
				delete cleaned.output_format;
				delete cleaned.background;
				delete cleaned.output_compression;
				delete cleaned.moderation;
				if (m === "dall-e-3" && (cleaned.n === void 0 || Number(cleaned.n) > 1)) cleaned.n = 1;
				if (m === "dall-e-2" && cleaned.quality !== void 0) delete cleaned.quality;
				return cleaned;
			}
			return base;
		}
	});
}
//#endregion
export { OPENAI_DEFAULT_IMAGE_MODEL, OPENAI_IMAGE_MODELS, buildOpenAIImageGenerationProvider };

//# sourceMappingURL=image-generation-provider.js.map