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/memory/quality/+server.ts
/**
* Memory quality distribution API endpoint.
* GET /api/memory/quality → quality distribution stats across all brain tables.
*
* @remarks T9616 follow-up: This route still uses raw SQL because the CORE
* public API (`getMemoryQualityReport`) returns a different shape (3 aggregate
* buckets per memory tier) rather than the per-table 5-range quality buckets
* and per-count stats consumed by the Studio quality dashboard. A future task
* should extend `getMemoryQualityReport` (or add a `getTableQualityStats` op)
* in `packages/core/src/memory/public-api.ts` to cover this shape, then
* migrate this handler to use it.
*/
/** Build quality buckets for a table. */
function buildBuckets(db, table) {
	return [
		{
			range: "0.0–0.2",
			min: 0,
			max: .2
		},
		{
			range: "0.2–0.4",
			min: .2,
			max: .4
		},
		{
			range: "0.4–0.6",
			min: .4,
			max: .6
		},
		{
			range: "0.6–0.8",
			min: .6,
			max: .8
		},
		{
			range: "0.8–1.0",
			min: .8,
			max: 1
		}
	].map(({ range, min, max }) => {
		const isLast = max === 1;
		return {
			range,
			min,
			max,
			count: db.prepare(`SELECT COUNT(*) as cnt FROM ${table}
         WHERE quality_score >= ? AND quality_score ${isLast ? "<=" : "<"} ?`).get(min, max).cnt
		};
	});
}
var GET = ({ locals }) => {
	const db = getBrainDb(locals.projectCtx);
	if (!db) {
		const empty = {
			buckets: [],
			verified_count: 0,
			prune_count: 0,
			invalidated_count: 0
		};
		return json({
			observations: {
				...empty,
				tiers: [],
				types: []
			},
			decisions: empty,
			patterns: empty,
			learnings: empty
		});
	}
	try {
		const obsBuckets = buildBuckets(db, "brain_observations");
		const obsTiers = db.prepare(`SELECT COALESCE(memory_tier, 'unknown') as tier, COUNT(*) as count
         FROM brain_observations GROUP BY memory_tier ORDER BY count DESC`).all();
		const obsTypes = db.prepare(`SELECT COALESCE(memory_type, 'unknown') as memory_type, COUNT(*) as count
         FROM brain_observations GROUP BY memory_type ORDER BY count DESC`).all();
		const obsVerified = db.prepare("SELECT COUNT(*) as cnt FROM brain_observations WHERE verified = 1").get().cnt;
		const obsPrune = db.prepare("SELECT COUNT(*) as cnt FROM brain_observations WHERE prune_candidate = 1").get().cnt;
		const obsInvalidated = db.prepare("SELECT COUNT(*) as cnt FROM brain_observations WHERE invalid_at IS NOT NULL").get().cnt;
		const decBuckets = buildBuckets(db, "brain_decisions");
		const decVerified = db.prepare("SELECT COUNT(*) as cnt FROM brain_decisions WHERE verified = 1").get().cnt;
		const decPrune = db.prepare("SELECT COUNT(*) as cnt FROM brain_decisions WHERE prune_candidate = 1").get().cnt;
		const patBuckets = buildBuckets(db, "brain_patterns");
		const patVerified = db.prepare("SELECT COUNT(*) as cnt FROM brain_patterns WHERE verified = 1").get().cnt;
		const learnBuckets = buildBuckets(db, "brain_learnings");
		const learnVerified = db.prepare("SELECT COUNT(*) as cnt FROM brain_learnings WHERE verified = 1").get().cnt;
		return json({
			observations: {
				buckets: obsBuckets,
				tiers: obsTiers,
				types: obsTypes,
				verified_count: obsVerified,
				prune_count: obsPrune,
				invalidated_count: obsInvalidated
			},
			decisions: {
				buckets: decBuckets,
				verified_count: decVerified,
				prune_count: decPrune,
				invalidated_count: 0
			},
			patterns: {
				buckets: patBuckets,
				verified_count: patVerified,
				prune_count: 0,
				invalidated_count: 0
			},
			learnings: {
				buckets: learnBuckets,
				verified_count: learnVerified,
				prune_count: 0,
				invalidated_count: 0
			}
		});
	} catch {
		const empty = {
			buckets: [],
			verified_count: 0,
			prune_count: 0,
			invalidated_count: 0
		};
		return json({
			observations: {
				...empty,
				tiers: [],
				types: []
			},
			decisions: empty,
			patterns: empty,
			learnings: empty
		});
	}
};

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