import { a as getBrainDb } 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/v1/graph/+server.ts
/**
* Universal Semantic Graph API — v1 endpoint.
*
* GET /api/v1/graph
*   → { nodes, edges, totalNodes, totalEdges }
*
* Returns brain_page_nodes of the sentience layer types:
*   task | decision | observation | symbol | conduit_message | llmtxt
*
* Only edges where **both** endpoints are present in the returned node set
* are included, matching the T945 AC requirement.
*
* ## Query parameters
*
* | Param   | Default | Notes                                      |
* |---------|---------|-------------------------------------------|
* | `limit` | `500`   | Max nodes returned; capped at 5000.        |
*
* ## Response shape
*
* ```json
* {
*   "nodes": [{ "id", "type", "label", "qualityScore", "metadataJson", "createdAt" }],
*   "edges": [{ "fromId", "toId", "edgeType", "weight", "createdAt" }],
*   "totalNodes": 42,
*   "totalEdges": 17
* }
* ```
*
* The `type` field maps `msg` (internal) to `conduit_message` (API-facing).
*
* @task T945
* @epic T1056
*/
/**
* Internal node types that form the sentience layer.
* `msg` is the internal type for CONDUIT messages.
*/
var SENTIENCE_INTERNAL_TYPES = [
	"task",
	"decision",
	"observation",
	"symbol",
	"msg",
	"llmtxt"
];
/**
* Map internal `msg` type to the API-facing `conduit_message` string.
* All other types are passed through unchanged.
*/
function toApiType(internalType) {
	if (internalType === "msg") return "conduit_message";
	return internalType;
}
/** Parse and clamp the `limit` query parameter. */
function parseLimit(raw) {
	const n = raw ? Number.parseInt(raw, 10) : 500;
	if (!Number.isFinite(n) || n <= 0) return 500;
	return Math.min(n, 5e3);
}
/**
* GET /api/v1/graph
*
* Returns the universal semantic graph nodes and edges for the active project.
* Returns an empty graph (200 OK) when brain.db is unavailable.
*
* @task T945
*/
var GET = ({ locals, url }) => {
	const limit = parseLimit(url.searchParams.get("limit"));
	const emptyResponse = {
		nodes: [],
		edges: [],
		totalNodes: 0,
		totalEdges: 0
	};
	const db = getBrainDb(locals.projectCtx);
	if (!db) return json(emptyResponse);
	try {
		const placeholders = SENTIENCE_INTERNAL_TYPES.map(() => "?").join(", ");
		const totalNodes = db.prepare(`SELECT COUNT(*) AS cnt FROM brain_page_nodes WHERE node_type IN (${placeholders})`).get(...SENTIENCE_INTERNAL_TYPES)?.cnt ?? 0;
		const rawNodes = db.prepare(`SELECT id, node_type, label, quality_score, metadata_json, created_at
         FROM brain_page_nodes
         WHERE node_type IN (${placeholders})
         ORDER BY quality_score DESC, last_activity_at DESC
         LIMIT ?`).all(...SENTIENCE_INTERNAL_TYPES, limit);
		const nodeIds = new Set(rawNodes.map((n) => n.id));
		const nodes = rawNodes.map((n) => ({
			id: n.id,
			type: toApiType(n.node_type),
			label: n.label,
			qualityScore: n.quality_score,
			metadataJson: n.metadata_json,
			createdAt: n.created_at
		}));
		const allEdges = db.prepare(`SELECT from_id, to_id, edge_type, weight, created_at FROM brain_page_edges`).all();
		const totalEdges = allEdges.length;
		return json({
			nodes,
			edges: allEdges.filter((e) => nodeIds.has(e.from_id) && nodeIds.has(e.to_id)).map((e) => ({
				fromId: e.from_id,
				toId: e.to_id,
				edgeType: e.edge_type,
				weight: e.weight,
				createdAt: e.created_at
			})),
			totalNodes,
			totalEdges
		});
	} catch {
		return json(emptyResponse);
	} finally {
		try {
			db.close();
		} catch {}
	}
};

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