import { j as json } from './exports-D1quPX8S.js';
import { aI as readStackEnv, aK as readStackSecretEnv, b as annotateAddonProfileAvailability, a7 as getAddonProfiles, a6 as getAddonProfileSelection, ar as listEnabledAddonIds } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { r as requireAdmin, d as getRequestId, f as getUiLoginPassword } from './helpers-kzwksNq1.js';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import './utils-BSRjJDrZ.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
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/api/setup/current-config/+server.ts
function readAkmConfig(configDir) {
	const path = join(configDir, "akm", "config.json");
	if (!existsSync(path)) return {};
	try {
		return JSON.parse(readFileSync(path, "utf-8"));
	} catch {
		return {};
	}
}
/**
* Read model preferences persisted by importHostOpenCode into
* <configDir>/assistant/opencode.json. This is the on-disk source of truth
* after a host import; reusing it means wizard reruns restore the same
* preferences without re-detecting the (possibly absent) host install.
* Returns undefined if the file doesn't exist or contains no model keys.
*/
function readPersistedModelPreferences(configDir) {
	const path = join(configDir, "assistant", "opencode.json");
	if (!existsSync(path)) return void 0;
	try {
		const parsed = JSON.parse(readFileSync(path, "utf-8"));
		const prefs = {};
		if (typeof parsed.model === "string" && parsed.model) prefs.model = parsed.model;
		if (typeof parsed.small_model === "string" && parsed.small_model) prefs.small_model = parsed.small_model;
		return prefs.model || prefs.small_model ? prefs : void 0;
	} catch {
		return;
	}
}
function deriveBaseUrl(endpoint) {
	if (!endpoint) return "";
	return endpoint.replace(/\/chat\/completions\/?$/, "").replace(/\/embeddings\/?$/, "").replace(/\/+$/, "");
}
var GET = async (event) => {
	const authError = requireAdmin(event, getRequestId(event));
	if (authError) return authError;
	const state = getState();
	const configured = getUiLoginPassword();
	const env = readStackEnv(state.stackDir);
	const secretEnv = readStackSecretEnv(state.stackDir);
	const akm = readAkmConfig(state.configDir);
	const importedModelPreferences = readPersistedModelPreferences(state.configDir);
	const voiceProfiles = await annotateAddonProfileAvailability(getAddonProfiles(state.homeDir, "voice"));
	const selectedVoiceProfile = getAddonProfileSelection(state.stackDir, "voice");
	const ollamaProfiles = await annotateAddonProfileAvailability(getAddonProfiles(state.homeDir, "ollama"));
	const selectedOllamaProfile = getAddonProfileSelection(state.stackDir, "ollama");
	const hostHome = process.env.HOME ?? process.env.USERPROFILE ?? "";
	const hostAkm = !!hostHome && env.OP_AKM_STASH === `${hostHome}/akm` && env.OP_AKM_CONFIG === `${hostHome}/.config/akm`;
	const meta = (envKey) => ({
		envKey,
		present: Boolean(secretEnv[envKey])
	});
	const discord = {};
	for (const [field, envKey] of Object.entries({
		botToken: "DISCORD_BOT_TOKEN",
		applicationId: "DISCORD_APPLICATION_ID",
		registerCommands: "DISCORD_REGISTER_COMMANDS",
		allowedGuilds: "DISCORD_ALLOWED_GUILDS",
		allowedRoles: "DISCORD_ALLOWED_ROLES",
		allowedUsers: "DISCORD_ALLOWED_USERS",
		blockedUsers: "DISCORD_BLOCKED_USERS"
	})) if (secretEnv[envKey]) discord[field] = meta(envKey);
	const slack = {};
	for (const [field, envKey] of Object.entries({
		slackBotToken: "SLACK_BOT_TOKEN",
		slackAppToken: "SLACK_APP_TOKEN",
		allowedChannels: "SLACK_ALLOWED_CHANNELS",
		allowedUsers: "SLACK_ALLOWED_USERS",
		blockedUsers: "SLACK_BLOCKED_USERS"
	})) if (secretEnv[envKey]) slack[field] = meta(envKey);
	const portalCredentials = {};
	if (Object.keys(discord).length > 0) portalCredentials.discord = discord;
	if (Object.keys(slack).length > 0) portalCredentials.slack = slack;
	return json({
		ok: true,
		hasPassword: typeof configured === "string" && configured.length > 0,
		imageTag: env.OP_IMAGE_TAG ?? "",
		hostAkm,
		llm: akm.llm ? {
			provider: akm.llm.provider ?? "",
			model: akm.llm.model ?? "",
			baseUrl: deriveBaseUrl(akm.llm.endpoint)
		} : null,
		embedding: akm.embedding ? {
			provider: akm.embedding.provider ?? "",
			model: akm.embedding.model ?? "",
			dims: akm.embedding.dimension ?? 0,
			baseUrl: deriveBaseUrl(akm.embedding.endpoint)
		} : null,
		voice: {
			tts: {
				engine: env.OP_TTS_ENGINE ?? "",
				baseURL: env.OP_TTS_BASE_URL ?? "",
				model: env.OP_TTS_MODEL ?? "",
				voice: env.OP_TTS_VOICE ?? ""
			},
			stt: {
				engine: env.OP_STT_ENGINE ?? "",
				baseURL: env.OP_STT_BASE_URL ?? "",
				model: env.OP_STT_MODEL ?? "",
				language: env.OP_STT_LANGUAGE ?? ""
			},
			profiles: voiceProfiles,
			selectedProfile: selectedVoiceProfile
		},
		ollama: {
			profiles: ollamaProfiles,
			selectedProfile: selectedOllamaProfile
		},
		enabledAddons: listEnabledAddonIds(state.homeDir),
		portalCredentials,
		importedModelPreferences: importedModelPreferences ?? null
	});
};

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