import { n as getCurrentPluginMetadataSnapshot } from "./current-plugin-metadata-snapshot-DUwUqs5q.js";
import { c as normalizeProviderId, r as findNormalizedProviderKey, s as normalizeModelRef } from "./model-ref-shared-DflvAb3G.js";
import { t as modelKey } from "./model-key-CMdQNkZf.js";
import { _ as isDefaultAgentRuntimeId, y as normalizeOptionalAgentRuntimeId } from "./openai-routing-WLjNjydC.js";
import { c as loadManifestMetadataSnapshot, n as isManifestPluginAvailableForControlPlane } from "./manifest-contract-eligibility-B5yM4KNC.js";
import { i as resolveSessionRuntimeOverrideForProvider, t as resolveCompatibleAgentRuntimeForProvider } from "./session-runtime-compat-C3pxHI7H.js";
import "./model-selection-CtjI2iP7.js";
import { t as RUNTIME_MODEL_VISIBILITY_NORMALIZATION } from "./model-visibility-policy-BXFoTvfL.js";
//#region src/auto-reply/reply/directive-handling.model-runtime.ts
/** Resolves and applies explicit runtime selections attached to `/model`. */
/** Validates a requested runtime against the provider selected by the same directive. */
function resolveModelRuntimeDirective(params) {
	const rawRuntime = params.rawRuntime?.trim();
	if (!rawRuntime) {
		if (params.sessionEntry?.agentRuntimeOverride?.trim() && !resolveSessionRuntimeOverrideForProvider({
			provider: params.provider,
			entry: params.sessionEntry,
			cfg: params.cfg
		})) return { kind: "clear" };
		return { kind: "unchanged" };
	}
	const runtime = normalizeOptionalAgentRuntimeId(rawRuntime);
	if (isDefaultAgentRuntimeId(runtime)) return { kind: "clear" };
	const provider = normalizeProviderId(params.provider);
	const compatibleRuntime = resolveCompatibleAgentRuntimeForProvider({
		provider,
		runtime,
		cfg: params.cfg
	});
	if (compatibleRuntime) return {
		kind: "set",
		runtime: compatibleRuntime
	};
	return {
		kind: "invalid",
		runtime: rawRuntime,
		errorText: `Runtime "${rawRuntime}" is not supported for ${provider || params.provider}.`
	};
}
/** Applies a validated runtime choice without disturbing existing pins when no choice was given. */
function applyModelRuntimeDirective(entry, resolution) {
	if (resolution.kind === "clear") {
		const updated = entry.agentRuntimeOverride !== void 0;
		delete entry.agentRuntimeOverride;
		return { updated };
	}
	if (resolution.kind === "set") {
		const updated = entry.agentRuntimeOverride !== resolution.runtime;
		entry.agentRuntimeOverride = resolution.runtime;
		return { updated };
	}
	return { updated: false };
}
//#endregion
//#region src/auto-reply/reply/model-runtime-normalization.ts
/** Carries the Gateway-owned metadata snapshot through one model-selection run. */
function resolveRuntimeNormalization(cfg) {
	return {
		...RUNTIME_MODEL_VISIBILITY_NORMALIZATION,
		manifestPlugins: getCurrentPluginMetadataSnapshot({
			config: cfg,
			allowWorkspaceScopedSnapshot: true
		})?.plugins
	};
}
function normalizeRuntimeRef(provider, model, normalization = RUNTIME_MODEL_VISIBILITY_NORMALIZATION) {
	return normalizeModelRef(provider, model, normalization);
}
function findSelectedCatalogEntry(params) {
	const selectedKey = modelKey(normalizeProviderId(params.provider), params.model);
	return params.catalog?.find((entry) => modelKey(entry.provider, entry.id) === selectedKey);
}
/** Provider identity comes from authored routes or prepared/plugin metadata, not model inventory. */
function isKnownModelSelectionProvider(params) {
	const provider = normalizeProviderId(params.provider);
	if (findNormalizedProviderKey(params.cfg.models?.providers, provider) || params.catalog.some((entry) => normalizeProviderId(entry.provider) === provider)) return true;
	const snapshot = loadManifestMetadataSnapshot({ config: params.cfg });
	return snapshot.plugins.some((plugin) => plugin.providers.some((id) => normalizeProviderId(id) === provider) && isManifestPluginAvailableForControlPlane({
		snapshot,
		plugin,
		config: params.cfg
	}));
}
/** Prepare runtime and capabilities for the selected route before any session mutation. */
async function prepareModelSelectionRuntime(params) {
	const runtime = resolveModelRuntimeDirective(params);
	if (runtime.kind === "invalid") return {
		status: "rejected",
		reason: "invalid-runtime",
		message: runtime.errorText
	};
	const selected = findSelectedCatalogEntry(params);
	if (!isKnownModelSelectionProvider(params)) return {
		status: "rejected",
		reason: "unknown-provider",
		message: `Unknown provider "${params.provider}". Use /models to list providers.`
	};
	if (selected?.reasoning !== void 0) return {
		status: "ready",
		runtime,
		catalog: [...params.catalog]
	};
	const { loadProviderScopedThinkingCatalog } = await import("./agents/model-catalog.runtime.js");
	const catalog = await loadProviderScopedThinkingCatalog({
		config: params.cfg,
		agentId: params.agentId,
		provider: params.provider,
		model: params.model
	});
	const resolved = findSelectedCatalogEntry({
		...params,
		catalog
	});
	return {
		status: "ready",
		runtime,
		catalog: resolved ? [resolved, ...params.catalog.filter((entry) => entry !== selected)] : [...params.catalog]
	};
}
function mergePreparedConfiguredCatalog(params) {
	if (!params.prepared?.length) return params.configured;
	const preparedByKey = new Map(params.prepared.map((entry) => [modelKey(entry.provider, entry.id), entry]));
	return params.configured.map((entry) => {
		const prepared = preparedByKey.get(modelKey(entry.provider, entry.id));
		return prepared ? {
			...entry,
			...prepared
		} : entry;
	});
}
//#endregion
export { prepareModelSelectionRuntime as a, resolveModelRuntimeDirective as c, normalizeRuntimeRef as i, isKnownModelSelectionProvider as n, resolveRuntimeNormalization as o, mergePreparedConfiguredCatalog as r, applyModelRuntimeDirective as s, findSelectedCatalogEntry as t };
