import { getLogger as getLogger$1 } from "../../../logger.js";
import { estimateTokens } from "@inkeep/agents-core";

//#region src/domains/run/artifacts/artifact-utils.ts
const logger = getLogger$1("artifact-utils");
/**
* Generate human-readable structure information for data
*/
function generateStructureInfo(data) {
	try {
		if (Array.isArray(data)) return `Array with ${data.length} items`;
		if (typeof data === "object" && data !== null) {
			const keys = Object.keys(data);
			return `Object with ${keys.length} keys: ${keys.slice(0, 5).join(", ")}${keys.length > 5 ? ", ..." : ""}`;
		}
		if (typeof data === "string") {
			const lines = data.split("\n").length;
			return `String: ${data.length} characters, ${lines} lines`;
		}
		return `${typeof data} value`;
	} catch {
		return "Unknown structure";
	}
}
/**
* Formats the retrieval-blocked reason string for an oversized artifact.
*/
function formatOversizedRetrievalReason(originalTokenSize, contextWindowSize) {
	return `Tool result is ~${Math.floor(originalTokenSize / 1e3)}K tokens, which exceeds safe context limits (max safe: ${Math.floor(contextWindowSize * .3 / 1e3)}K tokens).`;
}
/**
* Detect if artifact data is oversized (>30% of context window)
* Returns metadata for artifact saving
*/
function detectOversizedArtifact(data, contextWindowSize, context) {
	const result = {
		isOversized: false,
		originalTokenSize: 0,
		contextWindowSize,
		retrievalBlocked: false
	};
	if (!contextWindowSize) return result;
	result.originalTokenSize = estimateTokens(typeof data === "string" ? data : JSON.stringify(data));
	const maxSafeSize = Math.floor(contextWindowSize * .3);
	result.isOversized = result.originalTokenSize > maxSafeSize;
	result.retrievalBlocked = result.isOversized;
	if (result.isOversized) {
		logger.warn({
			...context,
			tokenSize: result.originalTokenSize,
			maxSafeSize,
			contextWindowSize
		}, "Artifact data exceeds safe context size - marking as oversized");
		result.oversizedWarning = `⚠️ OVERSIZED ARTIFACT - DO NOT RETRY: Result is ~${Math.floor(result.originalTokenSize / 1e3)}K tokens, exceeds safe context limits. The tool call succeeded and data was saved to database, but the result is too large to include in conversation context. DO NOT call this tool again with the same arguments - you already have the data, it just cannot be retrieved into context. If you need this data, ask the user to break it into smaller chunks or use a different approach.`;
		result.structureInfo = generateStructureInfo(data);
	}
	return result;
}

//#endregion
export { detectOversizedArtifact, formatOversizedRetrievalReason, generateStructureInfo };