import { d as getRequestId, r as requireAdmin, g as getOpenCodeClient, e as errorResponse, k as jsonResponse, p as parseJsonBody, j as jsonBodyError } from './helpers-kzwksNq1.js';
import { u as unsetMainModel, s as setMainModel } from './config-CO8OSXtU.js';
import './src-CidJbygh.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
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';
import './state-fxN45gBp.js';
import './coercion-TNFJisCC.js';
import './http-D8YVIqJa.js';

//#region src/routes/admin/opencode/model/+server.ts
var GET = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const config = await getOpenCodeClient().getConfig();
	if (!config) return errorResponse(503, "opencode_unavailable", "OpenCode is not reachable", {}, requestId);
	return jsonResponse(200, {
		model: config.model ?? "",
		small_model: config.small_model ?? ""
	}, requestId);
};
/** Parse a "provider/model" string. Returns null if the input is empty or malformed. */
function parseProviderModel(raw) {
	if (typeof raw !== "string") return null;
	const trimmed = raw.trim();
	if (!trimmed) return null;
	const slash = trimmed.indexOf("/");
	if (slash <= 0 || slash === trimmed.length - 1) return null;
	return {
		provider: trimmed.slice(0, slash),
		model: trimmed.slice(slash + 1)
	};
}
var POST = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const result = await parseJsonBody(event.request);
	if ("error" in result) return jsonBodyError(result, requestId);
	const body = result.data;
	const hasModel = "model" in body;
	const hasSmallModel = "small_model" in body;
	if (!hasModel && !hasSmallModel) return errorResponse(400, "bad_request", "model or small_model is required", {}, requestId);
	try {
		if (hasModel) if (body.model === null || body.model === "") await unsetMainModel("model");
		else {
			const parsed = parseProviderModel(body.model);
			if (!parsed) return errorResponse(400, "bad_request", "model must be in \"provider/model\" format", {}, requestId);
			await setMainModel(parsed.provider, parsed.model, "model");
		}
		if (hasSmallModel) if (body.small_model === null || body.small_model === "") await unsetMainModel("small_model");
		else {
			const parsed = parseProviderModel(body.small_model);
			if (!parsed) return errorResponse(400, "bad_request", "small_model must be in \"provider/model\" format", {}, requestId);
			await setMainModel(parsed.provider, parsed.model, "small_model");
		}
	} catch (e) {
		console.warn("[opencode.model] Failed to persist model selection", e);
		return errorResponse(500, "internal_error", "Failed to persist model selection", {}, requestId);
	}
	return jsonResponse(200, { ok: true }, requestId);
};

export { GET, POST };
//# sourceMappingURL=_server.ts-QdNUZ6_w.js.map
