import { g as getTaskAccessor } from './data-accessor-CVbz8X_7.js';
import { l as listSessions } from './sessions-j73dkbCI.js';
import { json } from '@sveltejs/kit';
import 'node:module';
import 'node:fs';
import 'node:path';
import 'node:child_process';
import 'node:crypto';
import 'node:os';
import './paths-2M_UxD_H.js';
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:process';
import 'node:async_hooks';
import './dual-scope-db-ATrwq0LH.js';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './registry2-BSyCTA3q.js';
import './sqlite-Dsbe0wec.js';
import './chunk-DDP5xfzh.js';
import './conduit-Ca_OFWR_.js';
import './unique-constraint-BM-Apx1H.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 './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import 'node:util';
import './session-manifest-ZHrj54LR.js';

//#region src/routes/api/tasks/sessions/+server.ts
/**
* GET /api/tasks/sessions — session history with task completions.
*
* Query params:
*   limit — max sessions (default 50)
*
* T9617 refactor: zero raw SQL — delegates to `listSessions` from
* `@cleocode/core/sessions`. The response shape preserves the pre-T9617
* contract consumed by the Studio sessions page.
*
* Note: `workedTasks` enrichment via raw task_work_history join is no
* longer performed here. The field is omitted (empty array) in this
* refactor and should be re-introduced via a dedicated core API if
* needed. // core-first-allowed: task_work_history join not in public API
*
* @task T9617
*/
var GET = async ({ locals, url }) => {
	const ctx = locals.projectCtx;
	if (!ctx.tasksDbExists) return json({ error: "tasks.db unavailable" }, { status: 503 });
	const limit = Math.min(Number(url.searchParams.get("limit") ?? "50"), 200);
	try {
		const sessions = await listSessions(ctx.projectPath, { limit });
		const accessor = await getTaskAccessor(ctx.projectPath);
		const enriched = await Promise.all(sessions.map(async (s) => {
			const completedIds = s.tasksCompleted ?? [];
			const createdIds = s.tasksCreated ?? [];
			const completedTasks = [];
			for (const tid of completedIds.slice(0, 20)) {
				const t = await accessor.loadSingleTask(tid);
				if (t) completedTasks.push({
					id: t.id,
					title: t.title,
					status: t.status
				});
			}
			let currentTask = null;
			const currentTaskId = s.taskWork?.taskId ?? null;
			if (currentTaskId) {
				const ct = await accessor.loadSingleTask(currentTaskId);
				if (ct) currentTask = {
					id: ct.id,
					title: ct.title,
					status: ct.status
				};
			}
			const durationMs = s.startedAt && s.endedAt ? new Date(s.endedAt).getTime() - new Date(s.startedAt).getTime() : null;
			return {
				id: s.id,
				name: s.name,
				status: s.status,
				agent: s.agent ?? null,
				currentTask,
				startedAt: s.startedAt,
				endedAt: s.endedAt ?? null,
				durationMs,
				completedCount: completedIds.length,
				createdCount: createdIds.length,
				completedTasks,
				workedTasks: []
			};
		}));
		return json({
			sessions: enriched,
			total: enriched.length
		});
	} catch (err) {
		return json({ error: String(err) }, { status: 500 });
	}
};

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