import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
//#region src/agent/session/tracker.ts
init_logger();
const log = createLogger("SessionTracker");
var SessionTracker = class {
	sessionUsage = /* @__PURE__ */ new Map();
	sessionLastActivity = /* @__PURE__ */ new Map();
	sessionModels = /* @__PURE__ */ new Map();
	cleanupInterval;
	maxSessions;
	sessionTtlMs;
	cleanupIntervalMs;
	constructor(config = {}) {
		this.maxSessions = config.maxSessions ?? 1e3;
		this.sessionTtlMs = config.sessionTtlMs ?? 1800 * 1e3;
		this.cleanupIntervalMs = config.cleanupIntervalMs ?? 300 * 1e3;
		this.startCleanupInterval();
	}
	/**
	* Start periodic cleanup of inactive sessions
	*/
	startCleanupInterval() {
		this.cleanupInterval = setInterval(() => {
			this.cleanupInactiveSessions();
		}, this.cleanupIntervalMs);
	}
	/**
	* Cleanup inactive sessions to prevent memory leak
	*/
	cleanupInactiveSessions() {
		const now = Date.now();
		const sessionsToDelete = [];
		let deletedCount = 0;
		for (const [sessionKey, lastActivity] of this.sessionLastActivity) if (now - lastActivity > this.sessionTtlMs) sessionsToDelete.push(sessionKey);
		for (const sessionKey of sessionsToDelete) {
			this.sessionUsage.delete(sessionKey);
			this.sessionLastActivity.delete(sessionKey);
			this.sessionModels.delete(sessionKey);
			deletedCount++;
		}
		if (this.sessionUsage.size > this.maxSessions) {
			const toRemove = Array.from(this.sessionLastActivity.entries()).sort((a, b) => a[1] - b[1]).slice(0, this.sessionUsage.size - this.maxSessions);
			for (const [sessionKey] of toRemove) {
				this.sessionUsage.delete(sessionKey);
				this.sessionLastActivity.delete(sessionKey);
				this.sessionModels.delete(sessionKey);
				deletedCount++;
			}
		}
		if (deletedCount > 0) log.info({
			deleted: deletedCount,
			remaining: this.sessionUsage.size
		}, "Cleaned up inactive sessions");
	}
	/**
	* Update last activity timestamp for a session
	*/
	touchSession(sessionKey) {
		this.sessionLastActivity.set(sessionKey, Date.now());
	}
	/**
	* Get usage for a session
	*/
	getUsage(sessionKey) {
		return this.sessionUsage.get(sessionKey);
	}
	/**
	* Update usage for a session
	*/
	updateUsage(sessionKey, usage) {
		const current = this.sessionUsage.get(sessionKey) ?? {
			prompt: 0,
			completion: 0,
			total: 0
		};
		if (usage.prompt !== void 0) current.prompt += usage.prompt;
		if (usage.completion !== void 0) current.completion += usage.completion;
		if (usage.total !== void 0) current.total += usage.total;
		this.sessionUsage.set(sessionKey, current);
		this.touchSession(sessionKey);
	}
	/**
	* Set model for a session
	*/
	setModel(sessionKey, modelId) {
		this.sessionModels.set(sessionKey, modelId);
		this.touchSession(sessionKey);
	}
	/**
	* Get model for a session
	*/
	getModel(sessionKey) {
		return this.sessionModels.get(sessionKey);
	}
	/**
	* Get all session keys
	*/
	getSessionKeys() {
		return Array.from(this.sessionUsage.keys());
	}
	/**
	* Get total session count
	*/
	getSessionCount() {
		return this.sessionUsage.size;
	}
	/**
	* Delete a session
	*/
	deleteSession(sessionKey) {
		const hadUsage = this.sessionUsage.delete(sessionKey);
		this.sessionLastActivity.delete(sessionKey);
		this.sessionModels.delete(sessionKey);
		return hadUsage;
	}
	/**
	* Dispose resources (call on shutdown)
	*/
	dispose() {
		if (this.cleanupInterval) {
			clearInterval(this.cleanupInterval);
			this.cleanupInterval = void 0;
		}
	}
};
//#endregion
export { SessionTracker };

//# sourceMappingURL=tracker.js.map