import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CIXf7egm.js";
import { c as isRecord } from "./record-coerce-DItp3I4t.js";
import { t as pruneMapToMaxSize } from "./map-size-DAGm21RM.js";
import { S as hashJson } from "./discovery-DUKpMd1M.js";
import { a as getPluginCache, c as getPluginMetadataSnapshotCache, f as withPluginCache, i as createPluginCache, r as bindPluginMetadataSnapshotCache } from "./plugin-cache-DMPn6vOK.js";
import { r as normalizeProviderId } from "./provider-id-DMd-TDFp.js";
import { n as MANIFEST_KEY } from "./legacy-names-NIXaj2oi.js";
import { o as measureDiagnosticsTimelineSpanSync, r as getActiveDiagnosticsTimelineSpan } from "./diagnostics-timeline-D3ILzYcZ.js";
import { p as resolveInstalledPluginIndexPolicyHash } from "./installed-plugin-index-eRMeMKev.js";
import { r as official_external_provider_catalog_default } from "./official-external-plugin-bundled-catalogs-7QIXEfNP.js";
import { n as resolveActivePluginInstallRoots } from "./install-root-context-E5pO3Vk6.js";
import { t as loadBundledPluginManifestRegistry } from "./manifest-registry-7OpYeIgh.js";
import { a as loadPluginRegistrySnapshotWithMetadata } from "./plugin-registry-snapshot-CUJ0SXDb.js";
import { n as resolveInstalledManifestRegistryIndexFingerprint, t as loadPluginManifestRegistryForInstalledIndex } from "./manifest-registry-installed-DQXyKPc5.js";
import { d as serializePluginIdScope, i as isCurrentPluginMetadataSnapshotRuntimeGeneration, n as getCurrentPluginMetadataSnapshot, p as resolvePluginControlPlaneFingerprint, u as normalizePluginIdScope } from "./current-plugin-metadata-snapshot-DUwUqs5q.js";
import { r as registerPluginMetadataSnapshotReaders, t as adoptCurrentPluginMetadataSnapshotIfAbsentRuntime } from "./plugin-metadata-snapshot.runtime.js";
//#region src/plugins/official-external-provider-endpoints.ts
/**
* Provider endpoint metadata for officially externalized provider plugins.
*
* Endpoint classification (SSRF, attribution, payload-compat policy) keys off
* base URLs and must keep working when the owning plugin is not installed:
* dist packages exclude externalized plugins, so their manifests are invisible
* to bundled discovery. Only the repo-bundled catalog JSON feeds this table;
* hosted marketplace feeds must never influence endpoint classification.
* Kept separate from official-external-plugin-catalog.ts so provider
* transports do not pull the ClawHub install/marketplace module graph.
*/
/**
* Lists manifest-shaped catalog metadata blocks that declare provider endpoints.
*
* The catalog mirrors manifests faithfully, including endpoint classes core
* does not (yet) recognize (e.g. deepinfra-native, gmi-native). The endpoint
* reader filters unknown classes exactly as it does for installed manifests,
* so they stay inert instead of complicating the mirror contract.
*/
function listOfficialExternalProviderEndpointManifests() {
	const entries = official_external_provider_catalog_default.entries;
	if (!Array.isArray(entries)) return [];
	const manifests = [];
	for (const entry of entries) {
		if (!isRecord(entry)) continue;
		const manifest = entry[MANIFEST_KEY];
		if (isRecord(manifest) && Array.isArray(manifest.providerEndpoints)) manifests.push(manifest);
	}
	return manifests;
}
//#endregion
//#region src/plugins/plugin-metadata-provider-facts.ts
const PROVIDER_ENDPOINT_CLASSES = new Set("anthropic-public cerebras-native chutes-native deepseek-native github-copilot-native groq-native meta-native mistral-public minimax-native moonshot-native modelstudio-native nvidia-native openai-public openai opencode-native azure-openai openrouter xai-native xiaomi-native zai-native google-generative-ai google-vertex".split(" "));
function normalizeProviderHosts(value) {
	return Array.isArray(value) ? value.filter((entry) => typeof entry === "string").map((entry) => entry.trim().toLowerCase()).filter(Boolean) : [];
}
function normalizePluginProviderBaseUrl(value) {
	const trimmed = normalizeOptionalString(value);
	const schemeless = trimmed && /^[a-z0-9.[\]-]+(?::\d+)?(?:[/?#].*)?$/i.test(trimmed);
	const url = trimmed ? URL.parse(schemeless ? `https://${trimmed}` : trimmed) : null;
	if (!url || url.protocol !== "http:" && url.protocol !== "https:") return;
	url.hash = "";
	url.search = "";
	return normalizeOptionalLowercaseString(url.toString().replace(/\/+$/, ""));
}
function prepareProviderEndpoints(value) {
	if (!Array.isArray(value)) return [];
	return value.filter(isRecord).filter((endpoint) => {
		const endpointClass = normalizeOptionalString(endpoint.endpointClass);
		return endpointClass ? PROVIDER_ENDPOINT_CLASSES.has(endpointClass) : false;
	}).map((endpoint) => {
		const endpointClass = normalizeOptionalString(endpoint.endpointClass);
		const googleVertexRegion = normalizeOptionalString(endpoint.googleVertexRegion);
		const googleVertexRegionHostSuffix = normalizeOptionalString(endpoint.googleVertexRegionHostSuffix)?.toLowerCase();
		return Object.assign({
			endpointClass,
			hosts: normalizeProviderHosts(endpoint.hosts),
			hostSuffixes: normalizeProviderHosts(endpoint.hostSuffixes),
			baseUrls: normalizeProviderHosts(endpoint.baseUrls).map(normalizePluginProviderBaseUrl).filter((baseUrl) => baseUrl !== void 0)
		}, googleVertexRegion ? { googleVertexRegion } : {}, googleVertexRegionHostSuffix ? { googleVertexRegionHostSuffix } : {});
	});
}
function buildPluginMetadataProviderFacts(plugins) {
	const providerEndpoints = plugins.flatMap((plugin) => prepareProviderEndpoints(plugin.providerEndpoints));
	const providerRequests = /* @__PURE__ */ new Map();
	for (const plugin of plugins) {
		const requests = isRecord(plugin.providerRequest?.providers) ? plugin.providerRequest.providers : {};
		for (const [rawProvider, request] of Object.entries(requests)) {
			if (!isRecord(request)) continue;
			const provider = normalizeLowercaseStringOrEmpty(rawProvider);
			if (!provider) continue;
			const supportsStreamingUsage = isRecord(request.openAICompletions) ? request.openAICompletions.supportsStreamingUsage : void 0;
			providerRequests.set(provider, {
				...normalizeOptionalString(request.family) ? { family: normalizeOptionalString(request.family) } : {},
				...normalizeOptionalString(request.compatibilityFamily) === "moonshot" ? { compatibilityFamily: "moonshot" } : {},
				...typeof supportsStreamingUsage === "boolean" ? { openAICompletions: { supportsStreamingUsage } } : {}
			});
		}
	}
	for (const manifest of listOfficialExternalProviderEndpointManifests()) providerEndpoints.push(...prepareProviderEndpoints(manifest.providerEndpoints));
	return {
		providerEndpoints,
		providerRequests
	};
}
//#endregion
//#region src/plugins/plugin-registry-id-normalizer.ts
function normalizePluginRegistryAlias(value) {
	return value.trim();
}
function normalizePluginRegistryAliasKey(value) {
	return normalizePluginRegistryAlias(value).toLowerCase();
}
function collectObjectKeys(value) {
	return value ? Object.keys(value) : [];
}
function listPluginRegistryNormalizerAliases(plugin) {
	return [
		plugin.id,
		...plugin.providers ?? [],
		...plugin.channels ?? [],
		...plugin.setup?.providers?.map((provider) => provider.id) ?? [],
		...plugin.cliBackends ?? [],
		...plugin.setup?.cliBackends ?? [],
		...collectObjectKeys(plugin.modelCatalog?.providers),
		...collectObjectKeys(plugin.modelCatalog?.aliases),
		...collectObjectKeys(plugin.providerAuthAliases),
		...plugin.legacyPluginIds ?? []
	];
}
/** Creates a normalizer that maps provider/channel/catalog aliases back to plugin ids. */
function createPluginRegistryIdNormalizer(index, options = {}) {
	const aliases = /* @__PURE__ */ new Map();
	for (const plugin of index.plugins) {
		if (!plugin.pluginId) continue;
		const pluginId = normalizePluginRegistryAlias(plugin.pluginId);
		if (pluginId) aliases.set(normalizePluginRegistryAliasKey(pluginId), plugin.pluginId);
	}
	const registry = options.lookUpTable?.manifestRegistry ?? options.manifestRegistry ?? loadPluginManifestRegistryForInstalledIndex({
		index,
		includeDisabled: true
	});
	for (const plugin of [...registry.plugins].toSorted((left, right) => left.id.localeCompare(right.id))) {
		const pluginId = normalizePluginRegistryAlias(plugin.id);
		if (!pluginId) continue;
		aliases.set(normalizePluginRegistryAliasKey(pluginId), plugin.id);
		for (const alias of listPluginRegistryNormalizerAliases(plugin)) {
			const normalizedAlias = normalizePluginRegistryAlias(alias);
			const normalizedAliasKey = normalizePluginRegistryAliasKey(alias);
			if (normalizedAlias && !aliases.has(normalizedAliasKey)) aliases.set(normalizedAliasKey, pluginId);
		}
	}
	return (pluginId) => {
		const trimmed = normalizePluginRegistryAlias(pluginId);
		return aliases.get(normalizePluginRegistryAliasKey(trimmed)) ?? trimmed;
	};
}
//#endregion
//#region src/plugins/plugin-metadata-snapshot.ts
const PLUGIN_METADATA_ENV_KEYS = [
	"APPDATA",
	"HOME",
	"OPENCLAW_BUNDLED_PLUGINS_DIR",
	"OPENCLAW_COMPATIBILITY_HOST_VERSION",
	"OPENCLAW_CONFIG_PATH",
	"OPENCLAW_DISABLE_BUNDLED_PLUGINS",
	"OPENCLAW_DISABLE_BUNDLED_SOURCE_OVERLAYS",
	"OPENCLAW_HOME",
	"OPENCLAW_NIX_MODE",
	"OPENCLAW_STATE_DIR",
	"USERPROFILE",
	"XDG_CONFIG_HOME"
];
const MAX_PLUGIN_METADATA_PROJECTIONS = 64;
function pickPluginMetadataEnv(env) {
	return Object.fromEntries(PLUGIN_METADATA_ENV_KEYS.flatMap((key) => {
		const value = env[key];
		return value === void 0 ? [] : [[key, value]];
	}));
}
function resolvePluginMetadataEnvFingerprint(env) {
	return hashJson({
		env: pickPluginMetadataEnv(env),
		installRoots: resolveActivePluginInstallRoots(env)
	});
}
function throwReadonlyPluginMetadataMutation() {
	throw new TypeError("Plugin metadata snapshots are immutable");
}
function freezeSnapshotValue(value, seen = /* @__PURE__ */ new WeakSet()) {
	if (!value || typeof value !== "object") return value;
	if (seen.has(value)) return value;
	seen.add(value);
	if (value instanceof Map) {
		for (const [key, entry] of value) {
			freezeSnapshotValue(key, seen);
			freezeSnapshotValue(entry, seen);
		}
		Object.defineProperties(value, {
			clear: { value: throwReadonlyPluginMetadataMutation },
			delete: { value: throwReadonlyPluginMetadataMutation },
			set: { value: throwReadonlyPluginMetadataMutation }
		});
		return Object.freeze(value);
	}
	if (value instanceof Set) {
		for (const entry of value) freezeSnapshotValue(entry, seen);
		Object.defineProperties(value, {
			add: { value: throwReadonlyPluginMetadataMutation },
			clear: { value: throwReadonlyPluginMetadataMutation },
			delete: { value: throwReadonlyPluginMetadataMutation }
		});
		return Object.freeze(value);
	}
	for (const entry of Object.values(value)) freezeSnapshotValue(entry, seen);
	return Object.freeze(value);
}
function indexesMatch(left, right) {
	if (!left || !right) return true;
	return resolveInstalledManifestRegistryIndexFingerprint(left) === resolveInstalledManifestRegistryIndexFingerprint(right);
}
/** Restores process-local behavior and immutability after a snapshot crosses a worker boundary. */
function restorePluginMetadataSnapshot(snapshot) {
	const restored = freezeSnapshotValue({
		...snapshot,
		normalizePluginId: createPluginRegistryIdNormalizer(snapshot.index, { manifestRegistry: snapshot.manifestRegistry })
	});
	bindPluginMetadataSnapshotCache(restored);
	return restored;
}
function isPluginMetadataSnapshotCompatible(params) {
	const env = params.env ?? process.env;
	if (isCurrentPluginMetadataSnapshotRuntimeGeneration(params.snapshot)) return true;
	const requestedPluginIds = normalizePluginIdScope(params.pluginIds);
	const snapshotPluginIds = normalizePluginIdScope(params.snapshot.pluginIds);
	return (snapshotPluginIds === void 0 || params.allowScopedSnapshot === true || requestedPluginIds !== void 0 && serializePluginIdScope(snapshotPluginIds) === serializePluginIdScope(requestedPluginIds)) && params.snapshot.policyHash === resolveInstalledPluginIndexPolicyHash(params.config) && (!params.snapshot.configFingerprint || params.snapshot.configFingerprint === resolvePluginControlPlaneFingerprint({
		config: params.config,
		env,
		index: params.index ?? params.snapshot.index,
		policyHash: params.snapshot.policyHash,
		workspaceDir: params.workspaceDir
	})) && (params.snapshot.workspaceDir ?? "") === (params.workspaceDir ?? "") && indexesMatch(params.snapshot.index, params.index);
}
function appendOwner(owners, ownedId, pluginId) {
	const existing = owners.get(ownedId);
	if (existing) {
		if (existing.includes(pluginId)) return;
		existing.push(pluginId);
		return;
	}
	owners.set(ownedId, [pluginId]);
}
function freezeOwnerMap(owners) {
	return new Map([...owners.entries()].map(([ownedId, pluginIds]) => [ownedId, Object.freeze([...pluginIds])]));
}
function buildPluginMetadataOwnerMaps(plugins) {
	const channels = /* @__PURE__ */ new Map();
	const channelConfigs = /* @__PURE__ */ new Map();
	const providers = /* @__PURE__ */ new Map();
	const modelCatalogProviders = /* @__PURE__ */ new Map();
	const cliBackends = /* @__PURE__ */ new Map();
	const setupProviders = /* @__PURE__ */ new Map();
	const commandAliases = /* @__PURE__ */ new Map();
	const contracts = /* @__PURE__ */ new Map();
	for (const plugin of plugins) {
		for (const channelId of plugin.channels ?? []) appendOwner(channels, channelId, plugin.id);
		for (const channelId of Object.keys(plugin.channelConfigs ?? {})) appendOwner(channelConfigs, channelId, plugin.id);
		for (const providerId of plugin.providers ?? []) appendOwner(providers, providerId, plugin.id);
		for (const [rawAlias, target] of Object.entries(plugin.providerAuthAliases ?? {})) {
			const alias = normalizeProviderId(rawAlias);
			const targetProvider = normalizeProviderId(target);
			if (alias && targetProvider && (plugin.providers ?? []).some((providerId) => normalizeProviderId(providerId) === targetProvider)) appendOwner(providers, alias, plugin.id);
		}
		for (const providerId of Object.keys(plugin.modelCatalog?.providers ?? {})) appendOwner(modelCatalogProviders, providerId, plugin.id);
		for (const providerId of Object.keys(plugin.modelCatalog?.aliases ?? {})) appendOwner(modelCatalogProviders, providerId, plugin.id);
		for (const cliBackendId of plugin.cliBackends ?? []) appendOwner(cliBackends, cliBackendId, plugin.id);
		for (const cliBackendId of plugin.setup?.cliBackends ?? []) appendOwner(cliBackends, cliBackendId, plugin.id);
		for (const setupProvider of plugin.setup?.providers ?? []) appendOwner(setupProviders, setupProvider.id, plugin.id);
		for (const commandAlias of plugin.commandAliases ?? []) appendOwner(commandAliases, commandAlias.name, plugin.id);
		for (const [contract, values] of Object.entries(plugin.contracts ?? {})) if (Array.isArray(values) && values.length > 0) appendOwner(contracts, contract, plugin.id);
	}
	return {
		channels: freezeOwnerMap(channels),
		channelConfigs: freezeOwnerMap(channelConfigs),
		providers: freezeOwnerMap(providers),
		modelCatalogProviders: freezeOwnerMap(modelCatalogProviders),
		cliBackends: freezeOwnerMap(cliBackends),
		setupProviders: freezeOwnerMap(setupProviders),
		commandAliases: freezeOwnerMap(commandAliases),
		contracts: freezeOwnerMap(contracts),
		...buildPluginMetadataProviderFacts(plugins)
	};
}
function listPluginOriginsFromMetadataSnapshot(snapshot) {
	return new Map(snapshot.plugins.map((record) => [record.id, record.origin]));
}
/** Rebuilds every manifest-derived snapshot fact from one authoritative registry. */
function rebasePluginMetadataSnapshotManifestRegistry(snapshot, manifestRegistry) {
	const plugins = manifestRegistry.plugins;
	const rebased = {
		...snapshot,
		manifestRegistry,
		plugins,
		diagnostics: manifestRegistry.diagnostics,
		byPluginId: new Map(plugins.map((plugin) => [plugin.id, plugin])),
		normalizePluginId: snapshot.index ? createPluginRegistryIdNormalizer(snapshot.index, { manifestRegistry }) : snapshot.normalizePluginId,
		owners: buildPluginMetadataOwnerMaps(plugins),
		...snapshot.metrics ? { metrics: {
			...snapshot.metrics,
			manifestPluginCount: plugins.length
		} } : {}
	};
	bindPluginMetadataSnapshotCache(rebased, getPluginMetadataSnapshotCache(snapshot));
	return rebased;
}
function projectPluginMetadataSnapshot(snapshot, pluginIds) {
	const selectedIds = normalizePluginIdScope(pluginIds);
	if (selectedIds === void 0) return snapshot;
	const key = serializePluginIdScope(selectedIds);
	if (key === serializePluginIdScope(snapshot.pluginIds)) return snapshot;
	const cache = getPluginMetadataSnapshotCache(snapshot);
	let selections = cache.metadata.projections.get(snapshot);
	if (!selections) {
		selections = /* @__PURE__ */ new Map();
		cache.metadata.projections.set(snapshot, selections);
	}
	const cached = selections.get(key);
	if (cached) return cached;
	const selected = new Set(selectedIds);
	const projected = freezeSnapshotValue({
		...rebasePluginMetadataSnapshotManifestRegistry(snapshot, {
			plugins: snapshot.plugins.filter((plugin) => selected.has(plugin.id)),
			diagnostics: snapshot.manifestRegistry.diagnostics
		}),
		pluginIds: selectedIds
	});
	bindPluginMetadataSnapshotCache(projected, cache);
	cache.metadata.projectionSources.set(projected, snapshot);
	selections.set(key, projected);
	pruneMapToMaxSize(selections, MAX_PLUGIN_METADATA_PROJECTIONS);
	return projected;
}
/** Uses semantic inputs only: checking freshness here would defeat first-access reuse. */
function resolvePluginMetadataSnapshotCacheKey(params) {
	return hashJson({
		env: resolvePluginMetadataEnvFingerprint(params.env ?? process.env),
		policy: resolveInstalledPluginIndexPolicyHash(params.config),
		loadPaths: params.config?.plugins?.load?.paths,
		workspaceDir: params.workspaceDir,
		stateDir: params.stateDir,
		index: params.index ? resolveInstalledManifestRegistryIndexFingerprint(params.index) : void 0,
		preferPersisted: params.preferPersisted !== false
	});
}
function loadPluginMetadataSnapshot(params) {
	if (params.allowCurrent === false && getPluginCache().kind !== "operation") return withPluginCache(createPluginCache(), () => loadPluginMetadataSnapshot(params));
	if (params.allowCurrent !== false && params.stateDir === void 0 && params.preferPersisted !== false) {
		const current = getCurrentPluginMetadataSnapshot({
			config: params.config,
			env: params.env,
			workspaceDir: params.workspaceDir,
			allowWorkspaceScopedSnapshot: true
		});
		if (current && (isCurrentPluginMetadataSnapshotRuntimeGeneration(current) || isPluginMetadataSnapshotCompatible({
			snapshot: current,
			config: params.config,
			env: params.env,
			workspaceDir: params.workspaceDir,
			index: params.index
		}))) return projectPluginMetadataSnapshot(current, params.pluginIds ?? params.pluginIdScope?.resolve({ index: current.index }));
	}
	const cache = getPluginCache();
	const key = resolvePluginMetadataSnapshotCacheKey(params);
	const cached = cache.metadata.snapshots.get(key);
	if (cached) return projectPluginMetadataSnapshot(cached, params.pluginIds ?? params.pluginIdScope?.resolve({ index: cached.index }));
	const activeTimelineSpan = getActiveDiagnosticsTimelineSpan();
	const snapshot = measureDiagnosticsTimelineSpanSync("plugins.metadata.scan", () => loadPluginMetadataSnapshotImpl(params), {
		phase: activeTimelineSpan?.phase ?? "startup",
		config: params.config,
		env: params.env,
		attributes: {
			hasWorkspaceDir: params.workspaceDir !== void 0,
			hasInstalledIndex: params.index !== void 0
		}
	});
	const frozen = measureDiagnosticsTimelineSpanSync("plugins.metadata.freeze", () => restorePluginMetadataSnapshot(snapshot), {
		phase: activeTimelineSpan?.phase ?? "startup",
		config: params.config,
		env: params.env,
		attributes: {
			indexPluginCount: snapshot.index.plugins.length,
			manifestPluginCount: snapshot.plugins.length
		}
	});
	cache.metadata.snapshots.set(key, frozen);
	return projectPluginMetadataSnapshot(frozen, params.pluginIds ?? params.pluginIdScope?.resolve({ index: frozen.index }));
}
/** Promotes a planning-scoped graph to the complete process-lifecycle metadata snapshot. */
function completePluginMetadataSnapshot(params) {
	if (!params.snapshot || params.snapshot.pluginIds === void 0 && params.snapshot.bundledManifestRegistry) return params.snapshot;
	const snapshot = params.snapshot;
	const cache = getPluginMetadataSnapshotCache(snapshot);
	const cached = cache.metadata.completions.get(snapshot);
	if (cached) return cached;
	const source = cache.metadata.projectionSources.get(snapshot);
	if (source) {
		const completed = completePluginMetadataSnapshot({
			...params,
			snapshot: source
		});
		if (completed) cache.metadata.completions.set(snapshot, completed);
		return completed;
	}
	return withPluginCache(cache, () => {
		const completed = completePluginMetadataSnapshotImpl({
			...params,
			snapshot
		});
		cache.metadata.completions.set(snapshot, completed);
		return completed;
	});
}
function completePluginMetadataSnapshotImpl(params) {
	const workspaceDir = params.workspaceDir ?? params.snapshot.workspaceDir;
	const manifestStartedAt = performance.now();
	const manifestRegistry = params.snapshot.pluginIds === void 0 ? params.snapshot.manifestRegistry : loadPluginManifestRegistryForInstalledIndex({
		index: params.snapshot.index,
		config: params.config,
		env: params.env ?? process.env,
		...workspaceDir ? { workspaceDir } : {},
		includeDisabled: true
	});
	const bundledManifestRegistry = params.snapshot.bundledManifestRegistry ?? loadBundledPluginManifestRegistry({ env: params.env });
	const manifestRegistryMs = performance.now() - manifestStartedAt;
	const completed = rebasePluginMetadataSnapshotManifestRegistry(params.snapshot, manifestRegistry);
	const { pluginIds: _pluginIds, ...unscoped } = completed;
	return restorePluginMetadataSnapshot({
		...unscoped,
		bundledManifestRegistry,
		configFingerprint: resolvePluginControlPlaneFingerprint({
			config: params.config,
			env: params.env,
			index: completed.index,
			policyHash: completed.policyHash,
			workspaceDir
		}),
		metrics: {
			...completed.metrics,
			manifestRegistryMs,
			totalMs: completed.metrics.totalMs + manifestRegistryMs
		}
	});
}
function resolvePluginMetadataSnapshot(params) {
	if (params.allowCurrent !== false && params.stateDir === void 0 && params.preferPersisted !== false) {
		const current = getCurrentPluginMetadataSnapshot({
			config: params.config,
			env: params.env,
			...params.config === void 0 ? { requireDefaultDiscoveryContext: true } : {},
			...params.pluginIds !== void 0 ? { pluginIds: params.pluginIds } : {},
			...params.pluginIdScope !== void 0 ? { pluginIdScope: params.pluginIdScope } : {},
			...params.workspaceDir !== void 0 ? { workspaceDir: params.workspaceDir } : {},
			...params.allowWorkspaceScopedCurrent === true ? { allowWorkspaceScopedSnapshot: true } : {}
		});
		if (!current) {
			const snapshot = loadPluginMetadataSnapshot(params);
			if (params.index === void 0 && params.workspaceDir === void 0 && params.pluginIds === void 0 && params.pluginIdScope === void 0 && snapshot.workspaceDir === void 0 && snapshot.pluginIds === void 0) adoptCurrentPluginMetadataSnapshotIfAbsentRuntime(snapshot, params);
			return snapshot;
		}
		if (isCurrentPluginMetadataSnapshotRuntimeGeneration(current)) return projectPluginMetadataSnapshot(current, params.pluginIds ?? params.pluginIdScope?.resolve({ index: current.index }));
		if (!params.index) return current;
		if (isPluginMetadataSnapshotCompatible({
			snapshot: current,
			config: params.config,
			env: params.env,
			allowScopedSnapshot: params.pluginIds !== void 0 || params.pluginIdScope !== void 0,
			workspaceDir: params.workspaceDir ?? (params.allowWorkspaceScopedCurrent === true ? current.workspaceDir : void 0),
			index: params.index
		})) return current;
	}
	return loadPluginMetadataSnapshot(params);
}
function loadPluginMetadataSnapshotImpl(params) {
	const totalStartedAt = performance.now();
	const registryStartedAt = performance.now();
	const registryResult = loadPluginRegistrySnapshotWithMetadata({
		config: params.config,
		workspaceDir: params.workspaceDir,
		...params.stateDir ? { stateDir: params.stateDir } : {},
		env: params.env,
		...params.preferPersisted !== void 0 ? { preferPersisted: params.preferPersisted } : {},
		...params.allowCurrent !== void 0 ? { allowCurrent: params.allowCurrent } : {},
		...params.index ? { index: params.index } : {}
	});
	const registrySnapshotMs = performance.now() - registryStartedAt;
	const index = structuredClone(registryResult.snapshot);
	index.diagnostics ??= [];
	const manifestStartedAt = performance.now();
	const manifestRegistry = loadPluginManifestRegistryForInstalledIndex({
		index,
		...registryResult.manifestRegistry ? { manifestRegistry: registryResult.manifestRegistry } : {},
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env,
		includeDisabled: true
	});
	const manifestRegistryMs = performance.now() - manifestStartedAt;
	const byPluginId = new Map(manifestRegistry.plugins.map((plugin) => [plugin.id, plugin]));
	const ownerMapsStartedAt = performance.now();
	const owners = buildPluginMetadataOwnerMaps(manifestRegistry.plugins);
	const ownerMapsMs = performance.now() - ownerMapsStartedAt;
	const totalMs = performance.now() - totalStartedAt;
	return {
		policyHash: index.policyHash,
		registrySource: registryResult.source,
		configFingerprint: resolvePluginControlPlaneFingerprint({
			config: params.config,
			env: params.env,
			index,
			policyHash: index.policyHash,
			workspaceDir: params.workspaceDir
		}),
		...params.workspaceDir ? { workspaceDir: params.workspaceDir } : {},
		index,
		registryIndex: index,
		registryDiagnostics: registryResult.diagnostics,
		manifestRegistry,
		plugins: manifestRegistry.plugins,
		diagnostics: manifestRegistry.diagnostics,
		byPluginId,
		owners,
		metrics: {
			registrySnapshotMs,
			manifestRegistryMs,
			ownerMapsMs,
			totalMs,
			indexPluginCount: index.plugins.length,
			manifestPluginCount: manifestRegistry.plugins.length
		},
		discovery: registryResult.discovery
	};
}
registerPluginMetadataSnapshotReaders({ resolvePluginMetadataSnapshot });
//#endregion
export { projectPluginMetadataSnapshot as a, resolvePluginMetadataSnapshot as c, createPluginRegistryIdNormalizer as d, normalizePluginProviderBaseUrl as f, loadPluginMetadataSnapshot as i, resolvePluginMetadataSnapshotCacheKey as l, isPluginMetadataSnapshotCompatible as n, rebasePluginMetadataSnapshotManifestRegistry as o, listPluginOriginsFromMetadataSnapshot as r, resolvePluginMetadataEnvFingerprint as s, completePluginMetadataSnapshot as t, restorePluginMetadataSnapshot as u };
