//#region src/cognitive/evolution/skill-reviewer.ts
const FALLBACK_RESULT = {
	approved: false,
	confidence: 0,
	notes: "Review failed"
};
function clamp01(value) {
	const n = typeof value === "number" ? value : Number(value);
	if (!Number.isFinite(n)) return 0;
	return Math.min(1, Math.max(0, n));
}
function buildReviewPrompt(draft, taskSummary) {
	return [
		"You are reviewing a skill for an AI assistant. Given ONLY this skill content and its claimed purpose, will it work correctly?",
		"",
		`Skill purpose: ${taskSummary}`,
		"",
		"Skill content:",
		`Name: ${draft.name}`,
		`Description: ${draft.description}`,
		`Triggers: ${draft.triggerPhrases.join(", ")}`,
		"Body:",
		draft.bodyMarkdown,
		"",
		"Respond as JSON: {\"approved\": true/false, \"confidence\": 0.0-1.0, \"notes\": \"...\"}"
	].join("\n");
}
function parseJsonLoose(text) {
	const trimmed = text.trim();
	const start = trimmed.indexOf("{");
	const end = trimmed.lastIndexOf("}");
	if (start === -1 || end === -1 || end <= start) throw new Error("No JSON object found in LLM response");
	return JSON.parse(trimmed.slice(start, end + 1));
}
async function reviewSkill(draft, taskSummary, deps) {
	let raw;
	try {
		raw = parseJsonLoose(await deps.generateText(buildReviewPrompt(draft, taskSummary)));
	} catch {
		return { ...FALLBACK_RESULT };
	}
	const notes = typeof raw.notes === "string" && raw.notes.trim() ? raw.notes : "";
	return {
		approved: raw.approved === true,
		confidence: clamp01(raw.confidence),
		notes
	};
}
//#endregion
export { reviewSkill };
