import { getLogger as getLogger$1 } from "../../../../logger.js";
import { isTextDocumentMimeType } from "../../utils/text-document-attachments.js";
import { createDefaultConversationHistoryConfig, formatMessagesAsConversationHistory, getConversationHistoryWithCompression } from "../../data/conversations.js";
import { getPrimaryModel, getSummarizerModel } from "./model-config.js";
import { resolveTextAttachmentBlock } from "../../services/blob-storage/text-attachment-resolver.js";
import { calculateBreakdownTotal, estimateTokens } from "@inkeep/agents-core";
import { isOfficeDocumentMimeType, normalizeMimeType } from "@inkeep/agents-core/constants/allowed-file-formats";

//#region src/domains/run/agents/generation/conversation-history.ts
const PDF_MEDIA_TYPE = "application/pdf";
const logger = getLogger$1("conversation-history");
function mapFileToAiSdkContentPart(fileValue, mimeType, metadata) {
	if (mimeType.startsWith("image/")) return {
		type: "image",
		image: fileValue,
		...metadata?.detail && { experimental_providerMetadata: { openai: { imageDetail: metadata.detail } } }
	};
	if (mimeType === PDF_MEDIA_TYPE || isOfficeDocumentMimeType(mimeType)) return {
		type: "file",
		data: fileValue,
		mediaType: mimeType,
		...metadata?.filename ? { filename: metadata.filename } : {}
	};
	return null;
}
async function buildConversationHistory(ctx, contextId, taskId, userMessage, streamRequestId, initialContextBreakdown) {
	let conversationHistory = "";
	const historyConfig = ctx.config.conversationHistoryConfig ?? createDefaultConversationHistoryConfig();
	if (historyConfig && historyConfig.mode !== "none") {
		if (historyConfig.mode === "full") {
			const filters = {
				delegationId: ctx.delegationId,
				isDelegated: ctx.isDelegatedAgent
			};
			conversationHistory = await formatMessagesAsConversationHistory(await getConversationHistoryWithCompression({
				tenantId: ctx.config.tenantId,
				projectId: ctx.config.projectId,
				conversationId: contextId,
				currentMessage: userMessage,
				options: historyConfig,
				filters,
				summarizerModel: getSummarizerModel(ctx.config),
				baseModel: getPrimaryModel(ctx.config),
				streamRequestId,
				fullContextSize: initialContextBreakdown.total
			}));
		} else if (historyConfig.mode === "scoped") conversationHistory = await formatMessagesAsConversationHistory(await getConversationHistoryWithCompression({
			tenantId: ctx.config.tenantId,
			projectId: ctx.config.projectId,
			conversationId: contextId,
			currentMessage: userMessage,
			options: historyConfig,
			filters: {
				subAgentId: ctx.config.id,
				taskId,
				delegationId: ctx.delegationId,
				isDelegated: ctx.isDelegatedAgent
			},
			summarizerModel: getSummarizerModel(ctx.config),
			baseModel: getPrimaryModel(ctx.config),
			streamRequestId,
			fullContextSize: initialContextBreakdown.total
		}));
	}
	const conversationHistoryTokens = estimateTokens(conversationHistory);
	const updatedContextBreakdown = {
		components: {
			...initialContextBreakdown.components,
			conversationHistory: conversationHistoryTokens
		},
		total: initialContextBreakdown.total
	};
	calculateBreakdownTotal(updatedContextBreakdown);
	return {
		conversationHistory,
		contextBreakdown: updatedContextBreakdown
	};
}
async function buildTextAttachmentPart(part) {
	return {
		type: "text",
		text: await resolveTextAttachmentBlock(part, { throwIfUnresolvable: true })
	};
}
async function buildInitialMessages(systemPrompt, conversationHistory, userMessage, fileParts, artifactsMessage = null) {
	const messages = [];
	messages.push({
		role: "system",
		content: systemPrompt
	});
	if (conversationHistory.trim() !== "") messages.push({
		role: "user",
		content: conversationHistory
	});
	if (artifactsMessage && artifactsMessage.trim() !== "") messages.push({
		role: "user",
		content: artifactsMessage
	});
	const userContent = await buildUserMessageContent(userMessage, fileParts);
	messages.push({
		role: "user",
		content: userContent
	});
	return messages;
}
async function buildUserMessageContent(text, fileParts) {
	if (!fileParts || fileParts.length === 0) return text;
	const content = [{
		type: "text",
		text
	}];
	for (const part of fileParts) {
		const file = part.file;
		const fileValue = "uri" in file && file.uri ? new URL(file.uri) : `data:${file.mimeType || ""};base64,${file.bytes}`;
		const mimeType = normalizeMimeType(file.mimeType ?? "");
		if (isTextDocumentMimeType(mimeType)) {
			content.push(await buildTextAttachmentPart(part));
			continue;
		}
		const mappedPart = mapFileToAiSdkContentPart(fileValue, mimeType, {
			detail: typeof part.metadata?.detail === "string" ? part.metadata.detail : void 0,
			filename: typeof part.metadata?.filename === "string" ? part.metadata.filename : void 0
		});
		if (mappedPart) content.push(mappedPart);
		else logger.warn({
			mimeType,
			source: "user-message"
		}, "Dropping unsupported file content part");
	}
	return content;
}

//#endregion
export { buildConversationHistory, buildInitialMessages, buildUserMessageContent };