import { d as inspectProviderToolSchemasWithPlugin, g as normalizeProviderToolSchemasWithPlugin } from "./provider-runtime-BFL8aOfT.js";
import { t as log } from "./logger-BXbMtoV0.js";
//#region src/agents/embedded-agent-runner/tool-schema-runtime.ts
function buildProviderToolSchemaContext(params, provider) {
	return {
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env,
		provider,
		modelId: params.modelId,
		modelApi: params.modelApi,
		model: params.model,
		tools: params.tools
	};
}
/**
* Runs provider-owned tool-schema normalization without encoding provider
* families in the embedded runner.
*/
function normalizeProviderToolSchemas(params) {
	const provider = params.provider.trim();
	const pluginNormalized = normalizeProviderToolSchemasWithPlugin({
		provider,
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env,
		runtimeHandle: params.runtimeHandle,
		allowRuntimePluginLoad: params.allowRuntimePluginLoad,
		context: buildProviderToolSchemaContext(params, provider)
	});
	return Array.isArray(pluginNormalized) ? pluginNormalized : params.tools;
}
/**
* Logs provider-owned tool-schema diagnostics after normalization.
*/
function logProviderToolSchemaDiagnostics(params) {
	const provider = params.provider.trim();
	const diagnostics = inspectProviderToolSchemasWithPlugin({
		provider,
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env,
		runtimeHandle: params.runtimeHandle,
		allowRuntimePluginLoad: params.allowRuntimePluginLoad,
		context: buildProviderToolSchemaContext(params, provider)
	});
	if (!Array.isArray(diagnostics)) return;
	if (diagnostics.length === 0) return;
	const summary = summarizeProviderToolSchemaDiagnostics(diagnostics);
	log.warn(`provider tool schema diagnostics: ${diagnostics.length} ${diagnostics.length === 1 ? "tool" : "tools"} for ${params.provider}: ${summary}`, {
		provider: params.provider,
		toolCount: params.tools.length,
		diagnosticCount: diagnostics.length,
		tools: params.tools.map((tool, index) => `${index}:${tool.name}`),
		diagnostics: diagnostics.map((diagnostic) => ({
			index: diagnostic.toolIndex,
			tool: diagnostic.toolName,
			violations: diagnostic.violations.slice(0, 12),
			violationCount: diagnostic.violations.length
		}))
	});
}
function summarizeProviderToolSchemaDiagnostics(diagnostics) {
	const visible = diagnostics.slice(0, 6).map((diagnostic) => {
		const violationCount = diagnostic.violations.length;
		return `${diagnostic.toolName || "unknown"} (${violationCount} ${violationCount === 1 ? "violation" : "violations"})`;
	});
	const remaining = diagnostics.length - visible.length;
	return remaining > 0 ? `${visible.join(", ")}, +${remaining} more` : visible.join(", ");
}
//#endregion
//#region src/agents/runtime-plan/tools.ts
function runtimePlanToolContext(params) {
	return {
		workspaceDir: params.workspaceDir,
		modelApi: params.modelApi ?? void 0,
		model: params.model
	};
}
function normalizeAgentRuntimeTools(params) {
	const planContext = runtimePlanToolContext(params);
	return params.runtimePlan?.tools.normalize(params.tools, planContext) ?? normalizeProviderToolSchemas({
		tools: params.tools,
		provider: params.provider,
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env ?? process.env,
		modelId: params.modelId,
		modelApi: params.modelApi,
		model: params.model,
		runtimeHandle: params.runtimeHandle,
		allowRuntimePluginLoad: params.allowProviderRuntimePluginLoad
	});
}
function logAgentRuntimeToolDiagnostics(params) {
	const planContext = runtimePlanToolContext(params);
	if (params.runtimePlan) {
		params.runtimePlan.tools.logDiagnostics(params.tools, planContext);
		return;
	}
	logProviderToolSchemaDiagnostics({
		tools: params.tools,
		provider: params.provider,
		config: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env ?? process.env,
		modelId: params.modelId,
		modelApi: params.modelApi,
		model: params.model,
		runtimeHandle: params.runtimeHandle
	});
}
//#endregion
//#region src/agents/tool-schema-projection.ts
function isJsonValue(value) {
	if (value === null) return true;
	switch (typeof value) {
		case "boolean":
		case "number":
		case "string": return true;
		case "object":
			if (Array.isArray(value)) return value.every(isJsonValue);
			return Object.values(value).every(isJsonValue);
		default: return false;
	}
}
function isJsonObject(value) {
	return value !== null && typeof value === "object" && !Array.isArray(value);
}
function serializeToolInputSchema(value, path) {
	let text;
	try {
		text = JSON.stringify(value);
	} catch {
		return {
			schema: {},
			violations: [`${path} is not JSON-serializable`]
		};
	}
	if (!text) return {
		schema: {},
		violations: [`${path} is not JSON-serializable`]
	};
	const parsed = JSON.parse(text);
	if (!isJsonValue(parsed)) return {
		schema: {},
		violations: [`${path} is not a JSON value`]
	};
	return {
		schema: parsed,
		violations: []
	};
}
function findDynamicSchemaKeywordViolations(schema, path) {
	if (Array.isArray(schema)) return schema.flatMap((entry, index) => findDynamicSchemaKeywordViolations(entry, `${path}[${index}]`));
	if (!isJsonObject(schema)) return [];
	const violations = [];
	for (const key of ["$dynamicRef", "$dynamicAnchor"]) if (key in schema) violations.push(`${path}.${key}`);
	for (const [key, value] of Object.entries(schema)) {
		if (!value || typeof value !== "object") continue;
		if (schemaMapKeywords.has(key) && isJsonObject(value)) for (const [schemaName, childSchema] of Object.entries(value)) violations.push(...findDynamicSchemaKeywordViolations(childSchema, `${path}.${key}.${schemaName}`));
		else violations.push(...findDynamicSchemaKeywordViolations(value, `${path}.${key}`));
	}
	return violations;
}
const schemaMapKeywords = new Set([
	"$defs",
	"definitions",
	"dependencies",
	"dependentSchemas",
	"patternProperties",
	"properties"
]);
function projectRuntimeToolInputSchema(schema, path = "parameters") {
	const projection = serializeToolInputSchema(schema, path);
	const violations = [...projection.violations];
	if (!isJsonObject(projection.schema)) violations.push(`${path} must be a JSON object schema`);
	else if (projection.schema.type !== void 0 && projection.schema.type !== "object") violations.push(`${path}.type must be "object"`);
	violations.push(...findDynamicSchemaKeywordViolations(projection.schema, path));
	return {
		schema: projection.schema,
		violations
	};
}
function inspectRuntimeToolInputSchemas(tools) {
	return tools.flatMap((tool, toolIndex) => {
		const toolName = tool.name || `tool[${toolIndex}]`;
		const projection = projectRuntimeToolInputSchema(tool.parameters, `${toolName}.parameters`);
		if (projection.violations.length === 0) return [];
		return [{
			toolName,
			toolIndex,
			violations: projection.violations
		}];
	});
}
function filterRuntimeCompatibleTools(tools) {
	const diagnostics = inspectRuntimeToolInputSchemas(tools);
	if (diagnostics.length === 0) return {
		tools,
		diagnostics
	};
	const blockedIndexes = new Set(diagnostics.map((diagnostic) => diagnostic.toolIndex));
	return {
		tools: tools.filter((_tool, index) => !blockedIndexes.has(index)),
		diagnostics
	};
}
//#endregion
export { normalizeAgentRuntimeTools as a, logAgentRuntimeToolDiagnostics as i, inspectRuntimeToolInputSchemas as n, logProviderToolSchemaDiagnostics as o, projectRuntimeToolInputSchema as r, normalizeProviderToolSchemas as s, filterRuntimeCompatibleTools as t };
