import { t as sanitizeForLog } from "./ansi-BCVkJ8zW.js";
import { i as formatErrorMessage } from "./errors-Cq_Pdh35.js";
import { i as resolveAgentModelPrimaryValue } from "./model-input-DiS5KWpR.js";
import "./agent-scope-CZ-IyS6E.js";
import { a as resolveAgentDir, n as listAgentIds, o as resolveAgentWorkspaceDir, r as resolveAgentConfig } from "./agent-scope-config-CT5O0iC8.js";
import "./defaults-mDjiWzE5.js";
import { c as parseModelRef } from "./model-selection-normalize-DknR-OD2.js";
import { n as extractModelCompat } from "./provider-model-compat-CcFqbs95.js";
import { i as getPluginToolMeta } from "./tools-Cyfie2t2.js";
import { t as createNodmixCodingTools } from "./agent-tools-fN_yDRs4.js";
import { t as resolveModel } from "./model-BVoghDPc.js";
import { a as normalizeAgentRuntimeTools, t as filterRuntimeCompatibleTools } from "./tool-schema-projection-CsvPwf80.js";
//#region src/commands/doctor/shared/active-tool-schema-warnings.ts
function resolvePrimaryModelRef(cfg, agentModel) {
	return parseModelRef(resolveAgentModelPrimaryValue(agentModel) ?? resolveAgentModelPrimaryValue(cfg.agents?.defaults?.model) ?? "gpt-5.5", "openai", { allowPluginNormalization: false }) ?? {
		provider: "openai",
		model: "gpt-5.5"
	};
}
function resolveRuntimeModelContext(params) {
	const model = resolveModel(params.provider, params.modelId, params.agentDir, params.cfg, { workspaceDir: params.workspaceDir }).model;
	if (!model) return {};
	return {
		modelApi: model.api,
		model,
		modelCompat: extractModelCompat(model),
		...typeof model.contextWindow === "number" ? { modelContextWindowTokens: model.contextWindow } : {}
	};
}
function formatDiagnostic(params) {
	const plugin = params.pluginId ? ` from plugin "${params.pluginId}"` : "";
	return sanitizeForLog(`- agents.${params.agentId}: active tool "${params.diagnostic.toolName}"${plugin} has unsupported runtime input schema (${params.diagnostic.violations.join(", ")}). Nodmix will quarantine this tool at runtime; fix or disable the plugin, or remove the tool from active allowlists.`);
}
function collectActiveToolSchemaProjectionWarnings(params) {
	if (params.cfg.plugins?.enabled === false) return [];
	const env = params.env ?? process.env;
	const warnings = [];
	for (const agentId of listAgentIds(params.cfg)) {
		const agentConfig = resolveAgentConfig(params.cfg, agentId);
		const modelRef = resolvePrimaryModelRef(params.cfg, agentConfig?.model);
		const agentDir = resolveAgentDir(params.cfg, agentId, env);
		const workspaceDir = resolveAgentWorkspaceDir(params.cfg, agentId, env);
		let runtimeModelContext = {};
		try {
			runtimeModelContext = resolveRuntimeModelContext({
				cfg: params.cfg,
				agentDir,
				workspaceDir,
				provider: modelRef.provider,
				modelId: modelRef.model
			});
		} catch (error) {
			warnings.push(sanitizeForLog(`- agents.${agentId}: active tool schema validation could not resolve the runtime model context (${formatErrorMessage(error)}). Fix provider/model loading errors before relying on assistant tool startup.`));
		}
		let tools;
		try {
			tools = createNodmixCodingTools({
				agentId,
				agentDir,
				workspaceDir,
				config: params.cfg,
				modelProvider: modelRef.provider,
				modelId: modelRef.model,
				modelApi: runtimeModelContext.modelApi,
				modelCompat: runtimeModelContext.modelCompat,
				modelContextWindowTokens: runtimeModelContext.modelContextWindowTokens,
				allowGatewaySubagentBinding: true
			});
		} catch (error) {
			warnings.push(sanitizeForLog(`- agents.${agentId}: active tool schema validation could not load the runtime tool set (${formatErrorMessage(error)}). Fix plugin loading errors before relying on assistant tool startup.`));
			continue;
		}
		const rawToolsByName = new Map(tools.map((tool) => [tool.name, tool]));
		let normalizedTools;
		try {
			normalizedTools = normalizeAgentRuntimeTools({
				tools,
				provider: modelRef.provider,
				config: params.cfg,
				workspaceDir,
				env,
				modelId: modelRef.model,
				modelApi: runtimeModelContext.modelApi,
				model: runtimeModelContext.model
			});
		} catch (error) {
			warnings.push(sanitizeForLog(`- agents.${agentId}: active tool schema validation could not normalize the runtime tool set (${formatErrorMessage(error)}). Fix provider/plugin loading errors before relying on assistant tool startup.`));
			continue;
		}
		const projection = filterRuntimeCompatibleTools(normalizedTools);
		for (const diagnostic of projection.diagnostics) {
			const tool = normalizedTools[diagnostic.toolIndex];
			const rawTool = rawToolsByName.get(diagnostic.toolName);
			const pluginId = (tool ? getPluginToolMeta(tool)?.pluginId : void 0) ?? (rawTool ? getPluginToolMeta(rawTool)?.pluginId : void 0);
			warnings.push(formatDiagnostic({
				agentId,
				diagnostic,
				...pluginId ? { pluginId } : {}
			}));
		}
	}
	return warnings;
}
//#endregion
export { collectActiveToolSchemaProjectionWarnings as t };
