import { retireSessionMcpRuntimeForSessionKey } from "../../agent/mcp/bundle-mcp-runtime.js";
import "../../agent/mcp/bundle-mcp-tools.js";
import { getDistinctSessionChatIds } from "./session-chat-ids.js";
import { performSessionReset } from "../session-reset-service.js";
//#region src/gateway/service/sessions-api.ts
var GatewaySessionsApi = class {
	opts;
	constructor(opts) {
		this.opts = opts;
	}
	listSessions(query) {
		return this.opts.sessionIndex.listSessions(query);
	}
	/** Subagent sessions have keys starting with `subagent:`. */
	listSubagents(query) {
		return this.opts.sessionIndex.listSubagents(query);
	}
	getSession(key, options) {
		return this.opts.sessionIndex.getSession(key, options);
	}
	/** Read-only: in-flight webchat agent run for this session key, if any. */
	getActiveRun(sessionKey) {
		const key = sessionKey.trim();
		if (!key) return { active: false };
		const runId = this.opts.getActiveWebchatRunId(key)?.trim();
		if (!runId) return { active: false };
		return {
			active: true,
			runId
		};
	}
	getMessagePage(key, options) {
		return this.opts.sessionIndex.getSessionMessagePage(key, options);
	}
	patch(key, body) {
		return this.opts.sessionIndex.patchSession(key, body);
	}
	async getAgentConfig(sessionKey) {
		return this.opts.getAgentService().sessionInspector.agentConfig(sessionKey);
	}
	/** Resolved markdown workspace for a session (after hydration / mkdir). */
	getEffectiveWorkspacePath(sessionKey) {
		return this.opts.getAgentService().getEffectiveWorkspacePathForSession(sessionKey);
	}
	patchAgentConfig(sessionKey, body) {
		return this.opts.getAgentService().sessionConfig.patch(sessionKey, body);
	}
	listCompactionCheckpoints(key) {
		return this.opts.sessionIndex.listCompactionCheckpoints(key);
	}
	getCompactionCheckpoint(key, checkpointId) {
		return this.opts.sessionIndex.getCompactionCheckpointDetail(key, checkpointId);
	}
	async restoreCompactionCheckpoint(key, checkpointId) {
		await this.opts.sessionIndex.restoreCompactionCheckpoint(key, checkpointId);
		this.opts.getAgentService().evictSessionAgent(key);
	}
	async runCompaction(key, options) {
		const result = await this.opts.getAgentService().sessionInspector.compact(key, options);
		if (result.compacted) this.opts.sessionIndex.appendTranscriptContextEntry(key, {
			text: "Session transcript compacted",
			data: {
				firstKeptIndex: result.firstKeptIndex,
				tokensBefore: result.tokensBefore,
				tokensAfter: result.tokensAfter,
				summaryPreview: result.summary.slice(0, 500)
			}
		}).catch(() => {});
		return result;
	}
	async delete(key) {
		const result = await this.opts.sessionIndex.deleteSession(key);
		if (result) {
			this.opts.getAgentService().evictSessionAgent(key);
			await retireSessionMcpRuntimeForSessionKey({
				sessionKey: key,
				reason: "session-delete"
			});
		}
		return { deleted: result };
	}
	/** Reset transcript in place (archive + new session id); preserves session key and overrides. */
	reset(key) {
		return performSessionReset(key, {
			sessionIndex: this.opts.sessionIndex,
			getAgentService: this.opts.getAgentService
		});
	}
	deleteMany(keys) {
		return this.opts.sessionIndex.deleteSessions(keys);
	}
	async rename(key, name) {
		await this.opts.sessionIndex.renameSession(key, name);
		return { renamed: true };
	}
	async tag(key, tags) {
		await this.opts.sessionIndex.tagSession(key, tags);
		return { tagged: true };
	}
	async untag(key, tags) {
		await this.opts.sessionIndex.untagSession(key, tags);
		return { untagged: true };
	}
	async archive(key) {
		await this.opts.sessionIndex.archiveSession(key);
		return { archived: true };
	}
	async unarchive(key) {
		await this.opts.sessionIndex.unarchiveSession(key);
		return { unarchived: true };
	}
	async pin(key) {
		await this.opts.sessionIndex.pinSession(key);
		return { pinned: true };
	}
	async unpin(key) {
		await this.opts.sessionIndex.unpinSession(key);
		return { unpinned: true };
	}
	search(query) {
		return this.opts.sessionIndex.searchSessions(query);
	}
	searchIn(key, keyword) {
		return this.opts.sessionIndex.searchInSession(key, keyword);
	}
	async export(key, format) {
		return { content: await this.opts.sessionIndex.exportSession(key, format) };
	}
	stats() {
		return this.opts.sessionIndex.getStats();
	}
	/**
	* Distinct chat-id pairs from sessions, grouped by channel. Used by cron job
	* configuration UI to seed the "send to existing chat" picker.
	*/
	chatIds(channel) {
		return getDistinctSessionChatIds(this.opts.sessionIndex, channel);
	}
};
//#endregion
export { GatewaySessionsApi };

//# sourceMappingURL=sessions-api.js.map