import { getLogger as getLogger$1 } from "../../../../logger.js";
import { AGENT_EXECUTION_MAX_GENERATION_STEPS, LLM_GENERATION_FIRST_CALL_TIMEOUT_MS_STREAMING, LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS } from "../../constants/execution-limits/index.js";
import { validateModel } from "../agent-types.js";
import { ModelFactory } from "@inkeep/agents-core";

//#region src/domains/run/agents/generation/model-config.ts
const logger = getLogger$1("Agent");
function getMaxGenerationSteps(config) {
	return config.stopWhen?.stepCountIs ?? AGENT_EXECUTION_MAX_GENERATION_STEPS;
}
function getPrimaryModel(config) {
	if (!config.models?.base) throw new Error("Base model configuration is required. Please configure models at the project level.");
	return {
		...config.models.base,
		model: validateModel(config.models.base.model, "Base")
	};
}
function getStructuredOutputModel(config) {
	if (!config.models) throw new Error("Model configuration is required. Please configure models at the project level.");
	const structuredConfig = config.models.structuredOutput;
	const baseConfig = config.models.base;
	if (structuredConfig) return {
		...structuredConfig,
		model: validateModel(structuredConfig.model, "Structured output")
	};
	if (!baseConfig) throw new Error("Base model configuration is required for structured output fallback. Please configure models at the project level.");
	return {
		...baseConfig,
		model: validateModel(baseConfig.model, "Base (fallback for structured output)")
	};
}
function getSummarizerModel(config) {
	if (!config.models) throw new Error("Model configuration is required. Please configure models at the project level.");
	const summarizerConfig = config.models.summarizer;
	const baseConfig = config.models.base;
	if (summarizerConfig) return {
		...summarizerConfig,
		model: validateModel(summarizerConfig.model, "Summarizer")
	};
	if (!baseConfig) throw new Error("Base model configuration is required for summarizer fallback. Please configure models at the project level.");
	return {
		...baseConfig,
		model: validateModel(baseConfig.model, "Base (fallback for summarizer)")
	};
}
function configureModelSettings(ctx) {
	const hasStructuredOutput = Boolean(ctx.config.dataComponents && ctx.config.dataComponents.length > 0);
	const primaryModelSettings = hasStructuredOutput ? getStructuredOutputModel(ctx.config) : getPrimaryModel(ctx.config);
	const modelSettings = ModelFactory.prepareGenerationConfig(primaryModelSettings);
	const configuredTimeout = modelSettings.maxDuration ? Math.min(modelSettings.maxDuration * 1e3, LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS) : LLM_GENERATION_FIRST_CALL_TIMEOUT_MS_STREAMING;
	const timeoutMs = Math.min(configuredTimeout, LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS);
	if (modelSettings.maxDuration && modelSettings.maxDuration * 1e3 > LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS) logger.warn({
		requestedTimeout: modelSettings.maxDuration * 1e3,
		appliedTimeout: timeoutMs,
		maxAllowed: LLM_GENERATION_MAX_ALLOWED_TIMEOUT_MS
	}, "Requested timeout exceeded maximum allowed, capping to 10 minutes");
	return {
		primaryModelSettings,
		modelSettings: {
			...modelSettings,
			maxDuration: timeoutMs / 1e3
		},
		hasStructuredOutput,
		timeoutMs
	};
}

//#endregion
export { configureModelSettings, getMaxGenerationSteps, getPrimaryModel, getStructuredOutputModel, getSummarizerModel };