import { j as json } from './exports-D1quPX8S.js';
import { a as PROVIDER_KEY_MAP } from './provider-constants-DPrj3yBX.js';
import { am as isSetupComplete, aZ as resolveStackDir, Q as detectGpu, S as detectLocalProviders, R as detectHostOpenCode, aN as recommendSetup, j as authJsonPath } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { r as requireAdmin, d as getRequestId, g as getOpenCodeClient } from './helpers-kzwksNq1.js';
import { existsSync, readFileSync } from 'node:fs';
import './utils-BSRjJDrZ.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
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/api/setup/recommend/+server.ts
/** Local providers are detected separately as host providers — exclude from "cloud". */
var LOCAL_PROVIDER_IDS = new Set([
	"ollama",
	"lmstudio",
	"model-runner",
	"openai-compatible"
]);
/** Providers that have credentials stored in OP_HOME auth.json (API key or OAuth). */
function authJsonConnected() {
	try {
		const path = authJsonPath(getState());
		if (!existsSync(path)) return [];
		const data = JSON.parse(readFileSync(path, "utf-8"));
		return Object.keys(data ?? {});
	} catch {
		return [];
	}
}
/** Fallback: providers whose API-key env var is present in the process env. */
function envKeyConnected() {
	const out = [];
	for (const [provider, envVar] of Object.entries(PROVIDER_KEY_MAP)) if (process.env[envVar]) out.push(provider);
	return out;
}
/** Cloud providers = OpenCode connected[] ∪ auth.json credentials, minus locals.
*  Falls back to env-key detection when OpenCode is unavailable. */
async function detectCloudProviders() {
	let connected = [];
	try {
		const client = getOpenCodeClient();
		if (await client.isAvailable()) {
			const catalog = await client.proxy("/provider");
			const raw = catalog.ok ? catalog.data : {};
			connected = Array.from(new Set([...raw.connected ?? [], ...authJsonConnected()]));
		} else connected = Array.from(new Set([...authJsonConnected(), ...envKeyConnected()]));
	} catch {
		connected = Array.from(new Set([...authJsonConnected(), ...envKeyConnected()]));
	}
	return connected.filter((id) => !LOCAL_PROVIDER_IDS.has(id));
}
var GET = async (event) => {
	if (isSetupComplete(resolveStackDir())) {
		const authError = requireAdmin(event, getRequestId(event));
		if (authError) return authError;
	}
	const [cloudProviders, gpu, localDetections, hostOpenCode] = await Promise.all([
		detectCloudProviders(),
		detectGpu(),
		detectLocalProviders(),
		Promise.resolve(detectHostOpenCode())
	]);
	const hostProviders = localDetections.filter((p) => p.available).map((p) => ({
		provider: p.provider,
		url: p.url
	}));
	return json({
		ok: true,
		recommendation: recommendSetup({
			cloudProviders,
			hostProviders,
			gpu,
			hostCredentialCount: hostOpenCode.credentialCount
		}),
		gpu,
		cloudProviders,
		hostProviders
	});
};

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