import { aI as readStackEnv, az as patchSecretsEnvFile, b8 as writeFileAtomic } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, k as jsonResponse, w as withAdminBody, e as errorResponse } from './helpers-kzwksNq1.js';
import { existsSync, readFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
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/admin/assistant/+server.ts
var DEFAULT_PROJECT_NAME = "openpalm";
var DEFAULT_ASSISTANT_BIND_ADDRESS = "127.0.0.1";
var LAN_ASSISTANT_BIND_ADDRESS = "0.0.0.0";
var PROJECT_NAME_RE = /^[a-z0-9][a-z0-9_-]*$/;
function personaPath(configDir) {
	return join(configDir, "assistant", "persona.md");
}
function readPersona(configDir) {
	const path = personaPath(configDir);
	if (!existsSync(path)) return "";
	try {
		return readFileSync(path, "utf-8");
	} catch {
		return "";
	}
}
function normalizeProjectName(raw) {
	if (typeof raw !== "string") return null;
	const value = raw.trim() || DEFAULT_PROJECT_NAME;
	if (value.length > 63) return null;
	return PROJECT_NAME_RE.test(value) ? value : null;
}
var GET = async (event) => {
	const requestId = getRequestId(event);
	const denied = requireAdmin(event, requestId);
	if (denied) return denied;
	const state = getState();
	const env = readStackEnv(state.stackDir);
	return jsonResponse(200, {
		projectName: env.OP_PROJECT_NAME?.trim() || DEFAULT_PROJECT_NAME,
		lanExposureEnabled: (env.OP_ASSISTANT_BIND_ADDRESS?.trim() || DEFAULT_ASSISTANT_BIND_ADDRESS) === LAN_ASSISTANT_BIND_ADDRESS,
		stackEnvPath: "knowledge/env/stack.env",
		personaPath: "config/assistant/persona.md",
		personaContent: readPersona(state.configDir)
	}, requestId);
};
var PUT = async (event) => withAdminBody(event, async ({ requestId, body }) => {
	const projectName = normalizeProjectName(body.projectName);
	if (!projectName) return errorResponse(400, "bad_request", "projectName must be 1-63 chars of lowercase letters, numbers, dashes, or underscores.", {}, requestId);
	if (typeof body.personaContent !== "string") return errorResponse(400, "bad_request", "personaContent must be a string", {}, requestId);
	if (typeof body.lanExposureEnabled !== "boolean") return errorResponse(400, "bad_request", "lanExposureEnabled must be a boolean", {}, requestId);
	const state = getState();
	patchSecretsEnvFile(state.stackDir, {
		OP_PROJECT_NAME: projectName,
		OP_ASSISTANT_BIND_ADDRESS: body.lanExposureEnabled ? LAN_ASSISTANT_BIND_ADDRESS : DEFAULT_ASSISTANT_BIND_ADDRESS
	});
	const path = personaPath(state.configDir);
	mkdirSync(join(state.configDir, "assistant"), { recursive: true });
	const personaContent = body.personaContent.endsWith("\n") ? body.personaContent : `${body.personaContent}\n`;
	writeFileAtomic(path, personaContent, 420);
	return jsonResponse(200, {
		ok: true,
		projectName,
		lanExposureEnabled: body.lanExposureEnabled,
		stackEnvPath: "knowledge/env/stack.env",
		personaPath: "config/assistant/persona.md",
		personaContent
	}, requestId);
});

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