import { j as json } from './exports-D1quPX8S.js';
import { ay as parseEnvFile, ar as listEnabledAddonIds, aU as resolveEffectivePlatformImageTag, a4 as formatForDisplay, ai as isComparableSemver, aW as resolveLatestImageTagForCurrentMajor, aV as resolveLatestImageTag, aq as listDockerImageTags, aX as resolveLatestNpmVersion, P as PLATFORM_VERSION } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, e as errorResponse } from './helpers-kzwksNq1.js';
import { i as invalidateVersionCache, w as withCache } from './version-cache-CSnmLqxQ.js';
import { existsSync } from 'node:fs';
import './utils-BSRjJDrZ.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';

//#region src/routes/admin/versions/+server.ts
var PORTAL_ADDON_IDS = [
	"api",
	"chat",
	"discord",
	"slack",
	"gateway"
];
var GET = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	if (event.url.searchParams.get("refresh") === "1") invalidateVersionCache();
	const state = getState();
	if (!state.stackDir) return errorResponse(503, "not_initialized", "Stack directory not configured", {}, requestId);
	const stackEnvPath = `${state.stashDir}/env/stack.env`;
	const envVars = existsSync(stackEnvPath) ? parseEnvFile(stackEnvPath) : {};
	const imageTag = envVars.OP_IMAGE_TAG ?? "latest";
	const enabledAddons = listEnabledAddonIds(state.homeDir);
	const portalEnabled = enabledAddons.some((a) => PORTAL_ADDON_IDS.includes(a));
	const baseServices = [{
		id: "assistant",
		label: "Assistant",
		version: envVars.OP_ASSISTANT_IMAGE_TAG ?? imageTag
	}];
	if (portalEnabled) {
		baseServices.push({
			id: "guardian",
			label: "Guardian",
			version: resolveEffectivePlatformImageTag(envVars, "guardian")
		});
		baseServices.push({
			id: "portal",
			label: "Chat (Discord/Slack)",
			version: resolveEffectivePlatformImageTag(envVars, "portal")
		});
	}
	if (enabledAddons.includes("voice")) baseServices.push({
		id: "voice",
		label: "Voice",
		version: envVars.OP_VOICE_IMAGE_TAG ?? imageTag
	});
	const inElectron = process.env.OP_INSIDE_ELECTRON === "1";
	const electronVersion = process.env.OP_ELECTRON_VERSION ?? null;
	const electronLatestVersion = process.env.OP_ELECTRON_LATEST_VERSION ?? null;
	const electronLatestUrl = process.env.OP_ELECTRON_LATEST_URL ?? null;
	const harnessContractVersion = process.env.OP_HARNESS_CONTRACT_VERSION ? Number(process.env.OP_HARNESS_CONTRACT_VERSION) : null;
	const platformVersion = formatForDisplay(PLATFORM_VERSION);
	const namespace = (envVars.OP_IMAGE_NAMESPACE ?? process.env.OP_IMAGE_NAMESPACE ?? "openpalm").trim().toLowerCase();
	const latestImageTags = {};
	const unitTags = {};
	const services = [];
	for (const s of baseServices) {
		const current = s.version;
		const latestVersion = await withCache(`docker:latest:${s.id}`, async () => {
			return formatForDisplay(isComparableSemver(current) ? await resolveLatestImageTagForCurrentMajor(namespace, s.id, current) : await resolveLatestImageTag(namespace, s.id));
		}) ?? null;
		if (latestVersion) latestImageTags[s.id] = latestVersion;
		const tags = await withCache(`docker:tags:${s.id}`, async () => {
			return listDockerImageTags(namespace, s.id, { sameMajorAs: isComparableSemver(current) ? current : void 0 });
		});
		unitTags[s.id] = tags ?? [];
		services.push({
			...s,
			latestVersion
		});
	}
	const latestImageTag = latestImageTags["assistant"] ?? null;
	const platformLatest = await withCache("npm:@openpalm/lib", async () => {
		return resolveLatestNpmVersion("@openpalm/lib", { distTag: "latest" });
	});
	return json({
		imageTag,
		services,
		inElectron,
		electronVersion,
		electronLatestVersion,
		electronLatestUrl,
		electronUpdateAvailable: !!electronLatestVersion,
		harnessVersion: electronVersion,
		harnessContractVersion,
		harnessUpdateAvailable: !!electronLatestVersion,
		platformVersion,
		platformLatest: platformLatest ?? null,
		latestImageTag,
		latestImageTags,
		unitTags
	});
};

export { GET };
//# sourceMappingURL=_server.ts-CLGLoOX4.js.map
