import { g as getActiveEndpoint } from './endpoints-UdQGgKsR.js';
import { d as getRequestId, r as requireAdmin } from './helpers-kzwksNq1.js';
import './state-fxN45gBp.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';

//#region src/routes/proxy/assistant/[...path]/+server.ts
/**
* Proxy route: forward /proxy/assistant/[...path] → assistant OpenCode server.
*
* Auth: requires the operator's `op_session` cookie (cookie-only since
* Phase 2 of the auth/proxy refactor — the `x-admin-token` header fallback
* was removed).
* Forwards the full request body and method unchanged.
* The target URL and per-endpoint Basic-auth password are resolved per-request
* from the active endpoint store, so switching endpoints in the UI takes
* effect immediately without restarting the server.
*
* Streaming: the upstream response body is forwarded as-is (no buffering) so
* SSE responses (text/event-stream) pass through chunk-by-chunk. We do not
* impose a fixed timeout on the upstream fetch — OpenCode SSE streams can run
* for minutes. Instead the per-request AbortController is wired to the client
* disconnect signal (`event.request.signal`); when the browser aborts (tab
* close, navigation away), we propagate the abort to upstream.
*/
function buildForwardHeaders(incomingContentType, username, password) {
	const headers = {};
	if (incomingContentType) headers["content-type"] = incomingContentType;
	if (password) headers["authorization"] = `Basic ${btoa(`${username || "openpalm"}:${password}`)}`;
	return headers;
}
function buildResponseHeaders(upstream, requestId, endpointId, endpointLabel) {
	const headers = new Headers();
	headers.set("content-type", upstream.headers.get("content-type") ?? "application/json");
	const cacheControl = upstream.headers.get("cache-control");
	if (cacheControl) headers.set("cache-control", cacheControl);
	const transferEncoding = upstream.headers.get("transfer-encoding");
	if (transferEncoding) headers.set("transfer-encoding", transferEncoding);
	headers.set("x-request-id", requestId);
	headers.set("x-endpoint-id", endpointId);
	headers.set("x-endpoint-label", encodeURIComponent(endpointLabel));
	return headers;
}
var handler = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const endpoint = getActiveEndpoint();
	const { path } = event.params;
	const targetUrl = `${endpoint.url}/${path}${event.url.search}`;
	const method = event.request.method;
	const contentType = event.request.headers.get("content-type");
	const body = method !== "GET" && method !== "HEAD" ? await event.request.arrayBuffer() : void 0;
	const controller = new AbortController();
	const onClientAbort = () => controller.abort();
	event.request.signal.addEventListener("abort", onClientAbort, { once: true });
	try {
		const upstream = await fetch(targetUrl, {
			method,
			headers: buildForwardHeaders(contentType, endpoint.username, endpoint.password),
			body,
			signal: controller.signal
		});
		return new Response(upstream.body, {
			status: upstream.status,
			headers: buildResponseHeaders(upstream, requestId, endpoint.id, endpoint.label)
		});
	} catch (e) {
		event.request.signal.removeEventListener("abort", onClientAbort);
		console.warn("[proxy/assistant] Upstream request failed:", e);
		return new Response(JSON.stringify({
			error: "endpoint_unreachable",
			message: `Assistant endpoint "${endpoint.label}" is not reachable`,
			endpointId: endpoint.id,
			endpointLabel: endpoint.label,
			url: endpoint.url
		}), {
			status: 503,
			headers: {
				"content-type": "application/json",
				"x-request-id": requestId
			}
		});
	}
};
var GET = handler;
var POST = handler;
var PUT = handler;
var DELETE = handler;

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