import { g as getAllSubstrates } from './src-DG3l2-J-.js';
import { r as recordBrainLoadDuration } from './brain-D3P07VKO.js';
import './dual-scope-db-ATrwq0LH.js';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';

//#region src/routes/api/brain/chunks/+server.ts
/**
* Streaming Brain graph chunks endpoint — progressive-disclosure tier 1 and 2.
*
* GET /api/brain/chunks
*   → application/x-ndjson (one JSON object per line, newline-delimited)
*
* ## Protocol
*
* Each chunk is a JSON object on its own line.  Two event kinds are emitted:
*
* ```
* {"kind":"chunk","tier":1,"nodes":[...],"edges":[...],"counts":{...},"truncated":false}
* {"kind":"done","tier":1,"totalNodes":423,"elapsed":312}
* ```
*
* The client reads the stream line-by-line, appending nodes/edges to its
* in-memory graph on each `chunk` event, then marks the tier complete on
* `done`.
*
* ## Query parameters
*
* | Param        | Default | Notes                                              |
* |--------------|---------|---------------------------------------------------|
* | `tier`       | `1`     | 1 or 2.  Tier 0 is served via `/api/brain`.        |
* | `substrates` | all     | comma-separated filter                             |
* | `min_weight` | `0`     | quality/weight threshold                           |
*
* Tier limits are fixed: tier 1 = 1000 nodes, tier 2 = 5000 nodes.
*
* ## Client integration
*
* Agent E's page calls {@link streamRemainingNodes} (exported from
* `$lib/server/brain/streaming.ts`) from `onMount` after the tier-0
* data is in place.
*
* @task T990
*/
/** Node limits per tier (chunks endpoint handles tier 1 and 2 only). */
var TIER_LIMITS = {
	1: 1e3,
	2: 5e3
};
/** Valid substrate identifiers. */
var VALID_SUBSTRATES = new Set([
	"brain",
	"nexus",
	"tasks",
	"conduit",
	"agent-registry"
]);
var GET = ({ locals, url }) => {
	const t0 = performance.now();
	const tier = Number(url.searchParams.get("tier") ?? "1") === 2 ? 2 : 1;
	const substratesParam = url.searchParams.get("substrates");
	const substrates = substratesParam ? substratesParam.split(",").map((s) => s.trim()).filter((s) => VALID_SUBSTRATES.has(s)) : void 0;
	const minWeightParam = url.searchParams.get("min_weight");
	const minWeight = minWeightParam !== null ? Math.max(0, parseFloat(minWeightParam)) : 0;
	const limit = TIER_LIMITS[tier];
	const readable = new ReadableStream({ start(controller) {
		try {
			const graph = getAllSubstrates({
				limit,
				substrates,
				minWeight,
				projectCtx: locals.projectCtx
			});
			const elapsed = performance.now() - t0;
			recordBrainLoadDuration(tier, elapsed);
			const chunkLine = JSON.stringify({
				kind: "chunk",
				tier,
				nodes: graph.nodes,
				edges: graph.edges,
				counts: graph.counts,
				truncated: graph.truncated
			});
			controller.enqueue(new TextEncoder().encode(chunkLine + "\n"));
			const doneLine = JSON.stringify({
				kind: "done",
				tier,
				totalNodes: graph.nodes.length,
				elapsed: Math.round(elapsed)
			});
			controller.enqueue(new TextEncoder().encode(doneLine + "\n"));
			console.info(`[brain/chunks] tier=${tier} elapsed=${elapsed.toFixed(1)}ms nodes=${graph.nodes.length}`);
		} catch (err) {
			const errLine = JSON.stringify({
				kind: "error",
				message: String(err)
			});
			controller.enqueue(new TextEncoder().encode(errLine + "\n"));
		} finally {
			controller.close();
		}
	} });
	return new Response(readable, { headers: {
		"Content-Type": "application/x-ndjson",
		"Cache-Control": "no-store",
		"Transfer-Encoding": "chunked"
	} });
};

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