import { getLogger as getLogger$1 } from "../../../logger.js";
import { distillConversationHistory } from "../tools/distill-conversation-history-tool.js";
import { BaseCompressor, getModelAwareCompressionConfig } from "./BaseCompressor.js";
import { GENERATION_TYPES } from "@inkeep/agents-core";

//#region src/domains/run/compression/ConversationCompressor.ts
const logger = getLogger$1("ConversationCompressor");
/**
* Conversation-level compressor
* Compresses entire conversations for long-term storage or analysis
*/
var ConversationCompressor = class extends BaseCompressor {
	priorSummary;
	constructor(sessionId, conversationId, tenantId, projectId, agentId, options) {
		const compressionConfig = options?.config || getModelAwareCompressionConfig(options?.summarizerModel, .5);
		super(sessionId, conversationId, tenantId, projectId, agentId, compressionConfig, options?.summarizerModel, options?.baseModel);
		this.priorSummary = options?.priorSummary ?? null;
	}
	/**
	* 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).map((info) => info.artifactId)
		}, "CONVERSATION COMPRESSION: Compression completed successfully");
		return {
			artifactIds: Object.values(toolCallToArtifactMap).map((info) => info.artifactId),
			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({
			conversationId: this.conversationId,
			summarizerModel: this.summarizerModel,
			currentSummary: this.priorSummary,
			messageFormatter: (maxChars) => this.formatMessagesForDistillation(messages, toolCallToArtifactMap, maxChars),
			usageContext: {
				tenantId: this.tenantId,
				projectId: this.projectId,
				agentId: this.agentId,
				generationType: GENERATION_TYPES.CONVERSATION_COMPRESSION
			}
		});
	}
};

//#endregion
export { ConversationCompressor };