import { getLogger } from "../../../logger.js";
import { distillConversationHistory } from "../tools/distill-conversation-history-tool.js";
import { BaseCompressor, getModelAwareCompressionConfig } from "./BaseCompressor.js";

//#region src/domains/run/services/ConversationCompressor.ts
const logger = getLogger("ConversationCompressor");
/**
* Conversation-level compressor
* Compresses entire conversations for long-term storage or analysis
*/
var ConversationCompressor = class extends BaseCompressor {
	constructor(sessionId, conversationId, tenantId, projectId, config, summarizerModel, baseModel) {
		const compressionConfig = config || getModelAwareCompressionConfig(summarizerModel, .5);
		super(sessionId, conversationId, tenantId, projectId, compressionConfig, summarizerModel, baseModel);
	}
	/**
	* Get compression type for this compressor
	*/
	getCompressionType() {
		return "conversation_level";
	}
	/**
	* Check if compression is needed based on total context size exceeding conversation limits
	*/
	isCompressionNeeded(messages) {
		const contextSize = this.calculateContextSize(messages);
		const remaining = this.config.hardLimit - contextSize;
		const needsCompression = remaining <= this.config.safetyBuffer;
		logger.debug({
			sessionId: this.sessionId,
			contextSize,
			hardLimit: this.config.hardLimit,
			safetyBuffer: this.config.safetyBuffer,
			remaining,
			needsCompression
		}, "Checking conversation compression criteria");
		return needsCompression;
	}
	/**
	* Perform conversation-level compression
	* Unlike mid-generation, this compresses ALL messages in the conversation
	*/
	async compress(messages) {
		const contextSizeBefore = this.calculateContextSize(messages);
		logger.info({
			sessionId: this.sessionId,
			messageCount: messages.length,
			contextSize: contextSizeBefore
		}, "CONVERSATION COMPRESSION: Starting compression");
		const toolCallToArtifactMap = await this.saveToolResultsAsArtifacts(messages, 0);
		const summary = await this.createConversationSummary(messages, toolCallToArtifactMap);
		const contextSizeAfter = this.estimateTokens(JSON.stringify(summary));
		this.recordCompressionEvent({
			reason: "automatic",
			messageCount: messages.length,
			artifactCount: Object.keys(toolCallToArtifactMap).length,
			contextSizeBefore,
			contextSizeAfter,
			compressionType: this.getCompressionType()
		});
		logger.info({
			sessionId: this.sessionId,
			artifactsCreated: Object.keys(toolCallToArtifactMap).length,
			messageCount: messages.length,
			contextSizeBefore,
			contextSizeAfter,
			compressionRatio: contextSizeAfter / contextSizeBefore,
			artifactIds: Object.values(toolCallToArtifactMap)
		}, "CONVERSATION COMPRESSION: Compression completed successfully");
		return {
			artifactIds: Object.values(toolCallToArtifactMap),
			summary
		};
	}
	/**
	* Override createConversationSummary for conversation-level compression
	* Uses specialized conversation history distillation instead of the base implementation
	*/
	async createConversationSummary(messages, toolCallToArtifactMap) {
		if (!this.summarizerModel) throw new Error("Summarizer model is required for conversation history compression");
		return await distillConversationHistory({
			messages,
			conversationId: this.conversationId,
			summarizerModel: this.summarizerModel,
			toolCallToArtifactMap
		});
	}
};

//#endregion
export { ConversationCompressor };