import { b as getNexusDb } from './connections2-D22b0xbS.js';
import { json } from '@sveltejs/kit';
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';
import './dist4-CiQ5cizO.js';
import './adapters-D2gEs_5L.js';
import './registry2-BSyCTA3q.js';
import './data-accessor-CVbz8X_7.js';
import './tasks-CnwUAUXf.js';
import './conduit-Ca_OFWR_.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import 'node:fs/promises';
import 'jsonc-parser';
import '@cleocode/cant';
import 'node:util';
import 'node:buffer';
import 'child_process';
import './branch-lock-qP1EkZ7p.js';
import './src2-DK8DKufK.js';
import 'llmtxt/identity';
import './sessions-j73dkbCI.js';
import './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import './session-manifest-ZHrj54LR.js';
import './storage-B9UA3BHR.js';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './data-safety-central-vmfu_Y4J.js';
import './sqlite-data-accessor-C59wZ0zp.js';
import './cross-db-cleanup-C_FLyd-6.js';
import './query-ops-BDMaTdYw.js';
import 'node:net';
import './src3-CMSGYxDI.js';
import './local-model-fit2-J1xCIhC_.js';
import './credential-pool-B04qO8at.js';
import './catalog-model-resolver2-Cexk2TN0.js';
import './role-resolver-DF9BQ_U6.js';
import './identity2-DeAB5RXE.js';
import './role-executor-C4-3bmgC.js';
import 'node:stream';
import 'crypto';
import 'os';
import 'node:http2';
import 'fs/promises';
import './models-catalog-DG3aPok8.js';
import './catalog-resolver2-c0qfF34B.js';
import './providers-CmIcBfg-.js';
import './brain-maintenance-bSR6GwAG.js';
import './graph-auto-populate2-DYsUNGSL.js';
import './temporal-supersession-BZ46j30w.js';
import './typed-query-l1D_QElt.js';
import './llm-backend-resolver-BPbvttS4.js';
import '@ai-sdk/provider-utils';
import './observer-reflector2-D8nxuglX.js';
import './learnings2-2m2p7lzT.js';
import './memory-accessor2-BzlgmY-Z.js';
import './quality-scoring-CCPuRQmJ.js';
import './patterns2-DYKfsbwN.js';
import './graph-memory-bridge2-C7Y32Mk4.js';
import './sigil2-DftKm7Um.js';
import './services-Dlf3Gefq.js';
import './audit4-S87oN-BC.js';
import 'process';
import './cleo-home-BCqJ5rg1.js';

//#region src/routes/api/nexus/+server.ts
/**
* GET /api/nexus
*
* Returns the macro-view payload (communities + cross-community edges)
* consumed by `/code`.  Replaces the previous communities-only payload.
*
* Shape:
* {
*   communities: Array<{ id, label, size, color, topKind, rawLabel }>,
*   edges:       Array<{ source, target, weight, dominantType }>,
*   totalNodes:  number,
*   totalRelations: number,
* }
*
* `dominantType` preserves the most-common {@link NexusRelationType}
* across each cross-community pair so the renderer can style edges by
* their semantic kind instead of a hardcoded `'cross-community'`
* placeholder.
*
* Legacy consumers that only need the communities array continue to
* work by reading `response.communities`.
*
* @task T990
* @wave 1B
*/
/**
* 12-hue semantic palette — references the installed tokens.css
* variables so server-side macro colouring remains theme-aware once
* the client probes its computed style.  Kept as CSS expressions so
* the client can run them through `getComputedStyle` at render time.
*/
var CLUSTER_CSS_CYCLE = [
	"var(--info)",
	"var(--accent)",
	"var(--success)",
	"var(--warning)",
	"var(--danger)",
	"var(--priority-critical)",
	"color-mix(in srgb, var(--info) 60%, var(--accent) 40%)",
	"color-mix(in srgb, var(--success) 50%, var(--info) 50%)",
	"color-mix(in srgb, var(--warning) 60%, var(--danger) 40%)",
	"color-mix(in srgb, var(--accent) 70%, white 30%)",
	"color-mix(in srgb, var(--info) 40%, var(--priority-critical) 60%)",
	"color-mix(in srgb, var(--success) 40%, var(--warning) 60%)"
];
function cycleColor(idx) {
	return CLUSTER_CSS_CYCLE[idx % CLUSTER_CSS_CYCLE.length] ?? "var(--accent)";
}
var GET = ({ url }) => {
	const db = getNexusDb();
	if (!db) return json({ error: "nexus.db not available" }, { status: 503 });
	const onlyCommunities = url.searchParams.get("only") === "communities";
	const totalNodes = db.prepare("SELECT COUNT(*) AS cnt FROM nexus_nodes").get().cnt;
	const totalRelations = db.prepare("SELECT COUNT(*) AS cnt FROM nexus_relations").get().cnt;
	const communities = db.prepare(`SELECT n1.community_id AS community_id,
              COUNT(*) AS size,
              (SELECT kind FROM nexus_nodes n2
               WHERE n2.community_id = n1.community_id
               GROUP BY kind ORDER BY COUNT(*) DESC LIMIT 1) AS top_kind,
              (SELECT cn.label FROM nexus_nodes cn
               WHERE cn.id = n1.community_id
               LIMIT 1) AS community_label
       FROM nexus_nodes n1
       WHERE n1.community_id IS NOT NULL
       GROUP BY n1.community_id
       ORDER BY size DESC`).all().map((row, idx) => {
		const rawLabel = (row.community_label ?? "").trim();
		const clusterNum = row.community_id.replace("comm_", "");
		const name = rawLabel && rawLabel !== row.community_id ? `${rawLabel} (${row.size})` : `Cluster ${clusterNum} (${row.size})`;
		return {
			id: row.community_id,
			name,
			rawLabel: rawLabel && rawLabel !== row.community_id ? rawLabel : `Cluster ${clusterNum}`,
			size: row.size,
			color: cycleColor(idx),
			topKind: row.top_kind ?? "function"
		};
	});
	let edges = [];
	if (!onlyCommunities) {
		const edgeRows = db.prepare(`SELECT s.community_id AS src_comm,
                t.community_id AS tgt_comm,
                r.type        AS rel_type,
                COUNT(*)      AS weight
         FROM nexus_relations r
         JOIN nexus_nodes s ON r.source_id = s.id
         JOIN nexus_nodes t ON r.target_id = t.id
         WHERE s.community_id IS NOT NULL
           AND t.community_id IS NOT NULL
           AND s.community_id != t.community_id
         GROUP BY src_comm, tgt_comm, rel_type`).all();
		const accum = /* @__PURE__ */ new Map();
		for (const row of edgeRows) {
			const key = `${row.src_comm}::${row.tgt_comm}`;
			const cur = accum.get(key) ?? {
				weight: 0,
				typeCounts: /* @__PURE__ */ new Map()
			};
			cur.weight += row.weight;
			cur.typeCounts.set(row.rel_type, (cur.typeCounts.get(row.rel_type) ?? 0) + row.weight);
			accum.set(key, cur);
		}
		edges = [...accum.entries()].map(([key, value]) => {
			const [src, tgt] = key.split("::");
			let dominantType = "calls";
			let bestCount = 0;
			for (const [t, c] of value.typeCounts) if (c > bestCount) {
				bestCount = c;
				dominantType = t;
			}
			return {
				source: src,
				target: tgt,
				weight: value.weight,
				dominantType
			};
		}).sort((a, b) => b.weight - a.weight).slice(0, 600);
	}
	return json({
		communities,
		edges,
		totalNodes,
		totalRelations
	});
};

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