const require_rolldown_runtime = require("./rolldown-runtime-D_mwlA32.cjs");
let path = require("path");
path = require_rolldown_runtime.__toESM(path, 1);
//#region src/providers/openai/codexApiKeyGating.ts
/**
* Whether the provider routes inference through a non-OpenAI Codex model provider
* (e.g. `amazon-bedrock`). When it does, the model id belongs to that provider's namespace and
* an ambient OpenAI/Codex key is unrelated to the active backend, so promptfoo's OpenAI model
* allowlist no longer applies and the key must not be injected.
*/
function usesCustomModelProvider(config) {
	const cliConfigProvider = config.cli_config?.model_provider;
	const provider = config.model_provider ?? (typeof cliConfigProvider === "string" ? cliConfigProvider : void 0);
	const normalized = typeof provider === "string" ? provider.trim().toLowerCase() : "";
	return normalized.length > 0 && normalized !== "openai";
}
/**
* Whether the resolved API key should be presented to the Codex CLI/SDK across every channel
* (the spawned process env, the SDK constructor `apiKey`, and `account/login`). Single source
* of truth so the channels can never disagree: inject only when the key belongs to the active
* backend — the default OpenAI provider, or a custom `model_provider` where the user set
* `config.apiKey` explicitly.
*/
function shouldInjectApiKey(config, apiKey) {
	return Boolean(apiKey) && (!usesCustomModelProvider(config) || Boolean(config.apiKey));
}
/**
* Apply the resolved Codex/OpenAI API key to the spawned Codex CLI environment.
*
* Inject it only when {@link shouldInjectApiKey} allows (the key belongs to the active
* backend). When routing to a non-OpenAI provider without an explicit key, an ambient
* OPENAI_API_KEY / CODEX_API_KEY is unrelated to the backend; never inject one, and also strip
* any inherited via `inherit_process_env` — unless the user passed it explicitly through
* cli_env.
*/
function applyApiKeyToCliEnv(sortedEnv, config, apiKey) {
	if (apiKey && shouldInjectApiKey(config, apiKey)) {
		sortedEnv.OPENAI_API_KEY = apiKey;
		sortedEnv.CODEX_API_KEY = apiKey;
		return;
	}
	if (usesCustomModelProvider(config)) {
		for (const key of ["OPENAI_API_KEY", "CODEX_API_KEY"]) if (!(key in (config.cli_env ?? {}))) delete sortedEnv[key];
	}
}
//#endregion
//#region src/providers/openai/codexSkillMetadata.ts
function getCodexSkillRootPrefixes(options) {
	const prefixes = /* @__PURE__ */ new Set();
	const addPrefix = (candidate) => {
		if (!candidate) return;
		const normalized = candidate.replace(/\\/g, "/").replace(/\/+$/g, "");
		if (normalized) prefixes.add(normalized);
	};
	addPrefix(options.codexHome);
	addPrefix("/etc/codex");
	if (options.workingDir) {
		const resolvedWorkingDir = path.default.resolve(options.workingDir).replace(/\\/g, "/");
		addPrefix(path.default.posix.join(resolvedWorkingDir, ".agents"));
		if (options.gitRepositoryRoot) addPrefix(path.default.posix.join(options.gitRepositoryRoot.replace(/\\/g, "/"), ".agents"));
	}
	if (options.homeDir) addPrefix(path.default.posix.join(options.homeDir.replace(/\\/g, "/"), ".codex"));
	return Array.from(prefixes);
}
function extractCodexSkillPathCandidates(text, skillRootPrefixes = []) {
	const matches = /* @__PURE__ */ new Map();
	for (const rawToken of text.split(/\s+/)) {
		const token = rawToken.replace(/^[`"'([{<]+|[`"',;:)\]}>]+$/g, "").trim();
		if (!token) continue;
		const normalizedPath = token.replace(/\\/g, "/");
		const repoMatch = normalizedPath.match(/^\.agents\/skills\/([^/\s]+)\/SKILL\.md$/);
		if (repoMatch) {
			if (isValidCodexSkillName(repoMatch[1])) matches.set(normalizedPath, {
				name: repoMatch[1],
				path: normalizedPath
			});
			continue;
		}
		const matchingRoot = skillRootPrefixes.find((prefix) => normalizedPath.startsWith(`${prefix}/skills/`));
		if (!matchingRoot) continue;
		const customRootMatch = normalizedPath.slice(matchingRoot.length + 1).match(/^skills\/([^/\s]+)\/SKILL\.md$/);
		if (customRootMatch && isValidCodexSkillName(customRootMatch[1])) matches.set(normalizedPath, {
			name: customRootMatch[1],
			path: normalizedPath
		});
	}
	return Array.from(matches.values());
}
function buildCodexSkillMetadata(items, skillRootPrefixes, itemAdapter) {
	if (!Array.isArray(items) || items.length === 0) return;
	const attemptedSkillCalls = extractCodexSkillCallsFromItems(items, skillRootPrefixes, itemAdapter);
	const skillCalls = extractCodexSkillCallsFromItems(items, skillRootPrefixes, itemAdapter, { requireSuccessfulCommand: true });
	if (skillCalls.length === 0 && attemptedSkillCalls.length === 0) return;
	return {
		attemptedSkillCalls,
		skillCalls
	};
}
function getCodexSkillMetadataFields(skillMetadata) {
	if (!skillMetadata) return {};
	return {
		...skillMetadata.skillCalls.length > 0 ? { skillCalls: skillMetadata.skillCalls } : {},
		...skillMetadata.attemptedSkillCalls.length > skillMetadata.skillCalls.length ? { attemptedSkillCalls: skillMetadata.attemptedSkillCalls } : {}
	};
}
function extractCodexSkillCallsFromItems(items, skillRootPrefixes, itemAdapter, options = {}) {
	const skillCalls = /* @__PURE__ */ new Map();
	for (const item of items) {
		if (options.requireSuccessfulCommand && !itemAdapter.isSuccessfulCommand(item)) continue;
		const command = itemAdapter.getCommand(item);
		if (!command) continue;
		for (const skillPath of extractCodexSkillPathCandidates(command, skillRootPrefixes)) skillCalls.set(skillPath.path, skillPath);
	}
	return Array.from(skillCalls.values()).map((skillCall) => ({
		name: skillCall.name,
		path: skillCall.path,
		source: "heuristic"
	}));
}
function isValidCodexSkillName(name) {
	return /^[A-Za-z0-9._:-]+$/.test(name);
}
//#endregion
Object.defineProperty(exports, "applyApiKeyToCliEnv", {
	enumerable: true,
	get: function() {
		return applyApiKeyToCliEnv;
	}
});
Object.defineProperty(exports, "buildCodexSkillMetadata", {
	enumerable: true,
	get: function() {
		return buildCodexSkillMetadata;
	}
});
Object.defineProperty(exports, "extractCodexSkillPathCandidates", {
	enumerable: true,
	get: function() {
		return extractCodexSkillPathCandidates;
	}
});
Object.defineProperty(exports, "getCodexSkillMetadataFields", {
	enumerable: true,
	get: function() {
		return getCodexSkillMetadataFields;
	}
});
Object.defineProperty(exports, "getCodexSkillRootPrefixes", {
	enumerable: true,
	get: function() {
		return getCodexSkillRootPrefixes;
	}
});
Object.defineProperty(exports, "shouldInjectApiKey", {
	enumerable: true,
	get: function() {
		return shouldInjectApiKey;
	}
});
Object.defineProperty(exports, "usesCustomModelProvider", {
	enumerable: true,
	get: function() {
		return usesCustomModelProvider;
	}
});

//# sourceMappingURL=codexSkillMetadata-CH3SNHBT.cjs.map