import { getLogger } from "../../../logger.js";
import { BaseCompressor, getModelAwareCompressionConfig } from "./BaseCompressor.js";

//#region src/domains/run/services/MidGenerationCompressor.ts
const logger = getLogger("MidGenerationCompressor");
/**
* Mid-generation compressor
* Compresses context when generate() steps get too large with manual compression support
*/
var MidGenerationCompressor = class extends BaseCompressor {
	shouldCompress = false;
	lastProcessedMessageIndex = 0;
	constructor(sessionId, conversationId, tenantId, projectId, config, summarizerModel, baseModel) {
		const compressionConfig = config || getModelAwareCompressionConfig(summarizerModel);
		super(sessionId, conversationId, tenantId, projectId, compressionConfig, summarizerModel, baseModel);
	}
	/**
	* Get compression type for this compressor
	*/
	getCompressionType() {
		return "mid_generation";
	}
	/**
	* Manual compression request from LLM tool
	*/
	requestManualCompression(reason) {
		this.shouldCompress = true;
		logger.info({
			sessionId: this.sessionId,
			reason: reason || "Manual request from LLM"
		}, "Manual compression requested");
	}
	/**
	* Check if compression is needed (either automatic or manual)
	* Supports manual compression requests unique to mid-generation
	*/
	isCompressionNeeded(messages) {
		if (this.shouldCompress) return true;
		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,
			manualRequest: this.shouldCompress
		}, "Checking mid-generation compression criteria");
		return needsCompression;
	}
	/**
	* Perform mid-generation compression with incremental processing
	* Uses base class functionality with mid-generation specific logic
	*/
	async compress(messages) {
		const contextSizeBefore = this.calculateContextSize(messages);
		logger.info({
			sessionId: this.sessionId,
			messageCount: messages.length,
			contextSize: contextSizeBefore
		}, "MID-GENERATION COMPRESSION: Starting compression");
		const toolCallToArtifactMap = await this.saveToolResultsAsArtifacts(messages, this.lastProcessedMessageIndex);
		const summary = await this.createConversationSummary(messages, toolCallToArtifactMap);
		const contextSizeAfter = this.estimateTokens(JSON.stringify(summary));
		this.recordCompressionEvent({
			reason: this.shouldCompress ? "manual" : "automatic",
			messageCount: messages.length,
			artifactCount: Object.keys(toolCallToArtifactMap).length,
			contextSizeBefore,
			contextSizeAfter,
			compressionType: this.getCompressionType()
		});
		this.shouldCompress = false;
		this.lastProcessedMessageIndex = messages.length;
		logger.info({
			sessionId: this.sessionId,
			artifactsCreated: Object.keys(toolCallToArtifactMap).length,
			messageCount: messages.length,
			contextSizeBefore,
			contextSizeAfter,
			compressionRatio: contextSizeAfter / contextSizeBefore,
			artifactIds: Object.values(toolCallToArtifactMap)
		}, "MID-GENERATION COMPRESSION: Compression completed successfully");
		return {
			artifactIds: Object.values(toolCallToArtifactMap),
			summary
		};
	}
	/**
	* Get current state for debugging
	*/
	getState() {
		return {
			...super.getState(),
			shouldCompress: this.shouldCompress,
			lastProcessedMessageIndex: this.lastProcessedMessageIndex
		};
	}
};

//#endregion
export { MidGenerationCompressor };