import { b as asRecord } from './coercion-TNFJisCC.js';
import { o as opencodeFetch } from './http-D8YVIqJa.js';
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';

//#region src/lib/server/opencode/config.ts
/**
* OpenCode config read/write + small mutation helpers.
*
* Reads/writes `OP_HOME/config/assistant/opencode.json` directly because the
* container mount is read-only and OpenCode's PATCH /config does not persist
* to disk. We still call PATCH afterwards (best-effort) so the live process
* picks up the change without a restart.
*/
function configPath() {
	return join(process.env.OP_HOME ?? "", "config", "assistant", "opencode.json");
}
async function getCurrentConfig() {
	try {
		return JSON.parse(readFileSync(configPath(), "utf-8"));
	} catch {
		return opencodeFetch("/config");
	}
}
async function patchConfig(config) {
	let existing = {};
	try {
		existing = JSON.parse(readFileSync(configPath(), "utf-8"));
	} catch {}
	const merged = {
		...existing,
		...config
	};
	if (config.provider) merged.provider = {
		...existing.provider ?? {},
		...config.provider
	};
	writeFileSync(configPath(), JSON.stringify(merged, null, 2) + "\n");
	await opencodeFetch("/config", {
		method: "PATCH",
		body: JSON.stringify(config)
	}).catch(() => {});
	return merged;
}
function normalizeProviderConfig(providerConfig) {
	const normalized = providerConfig ? { ...providerConfig } : {};
	const options = asRecord(normalized.options);
	if (options && Object.keys(options).length === 0) delete normalized.options;
	return Object.keys(normalized).length > 0 ? normalized : void 0;
}
function setProviderEnabled(config, providerId, enabled) {
	const disabled = new Set(config.disabled_providers ?? []);
	const allowlist = config.enabled_providers ? new Set(config.enabled_providers) : void 0;
	if (enabled) {
		disabled.delete(providerId);
		allowlist?.add(providerId);
	} else {
		disabled.add(providerId);
		allowlist?.delete(providerId);
	}
	config.disabled_providers = Array.from(disabled).sort();
	if (allowlist) config.enabled_providers = Array.from(allowlist).sort();
	return config;
}
/** Save non-credential connection options (baseURL, headers, timeout, etc.) for a provider. */
async function setProviderOptions(providerId, options) {
	const config = await getCurrentConfig();
	const providerConfig = { ...config.provider ?? {} };
	const current = asRecord(providerConfig[providerId]) ?? {};
	const nextOptions = { ...asRecord(current.options) ?? {} };
	delete nextOptions.apiKey;
	if (options.baseURL) nextOptions.baseURL = options.baseURL;
	else delete nextOptions.baseURL;
	if (options.enterpriseUrl) nextOptions.enterpriseUrl = options.enterpriseUrl;
	else delete nextOptions.enterpriseUrl;
	if (options.timeout !== void 0 && options.timeout > 0) nextOptions.timeout = options.timeout;
	else delete nextOptions.timeout;
	if (options.setCacheKey === true) nextOptions.setCacheKey = true;
	else delete nextOptions.setCacheKey;
	if (options.headers && Object.keys(options.headers).length > 0) nextOptions.headers = options.headers;
	else delete nextOptions.headers;
	const nextEntry = normalizeProviderConfig({
		...current,
		options: nextOptions
	});
	if (nextEntry) providerConfig[providerId] = nextEntry;
	else delete providerConfig[providerId];
	config.provider = providerConfig;
	await patchConfig(config);
}
/** Register a provider entry (local-detected or fully custom) in opencode.json. */
async function registerProvider(providerId, entry, overwrite = false) {
	const config = await getCurrentConfig();
	const providerConfig = { ...config.provider ?? {} };
	if (providerConfig[providerId] && !overwrite) return { alreadyExists: true };
	providerConfig[providerId] = {
		...asRecord(providerConfig[providerId]),
		...entry.npm !== void 0 ? { npm: entry.npm } : {},
		...entry.name !== void 0 ? { name: entry.name } : {},
		...entry.options !== void 0 ? { options: entry.options } : {},
		...entry.models !== void 0 ? { models: entry.models } : {}
	};
	config.provider = providerConfig;
	await patchConfig(config);
	return { alreadyExists: false };
}
/** Set the main model (or small model) in opencode.json. */
async function setMainModel(providerId, modelId, target) {
	const config = await getCurrentConfig();
	config[target] = `${providerId}/${modelId}`;
	await patchConfig(config);
}
/** Clear the main model (or small model) in opencode.json. */
async function unsetMainModel(target) {
	const config = await getCurrentConfig();
	delete config[target];
	await patchConfig(config);
}

export { setProviderEnabled as a, setProviderOptions as b, getCurrentConfig as g, patchConfig as p, registerProvider as r, setMainModel as s, unsetMainModel as u };
//# sourceMappingURL=config-CO8OSXtU.js.map
