import { t as generateSkillDraft } from "./skill-draft-generator-C8zPLkD-.js";
//#region src/cognitive/evolution/engine.ts
var EvolutionEngine = class {
	constructor(store, draftGenerator) {
		this.store = store;
		this.draftGenerator = draftGenerator;
	}
	async generate(candidate) {
		if (this.draftGenerator) return this.draftGenerator(candidate);
		return generateSkillDraft(candidate);
	}
	async checkBeforeGenerate(candidate, lifecycle, existingSkills, deps) {
		if (!lifecycle) return { shouldCreate: true };
		const result = deps?.generateText && existingSkills ? await lifecycle.checkSemanticDuplicate(candidate.taskSummary, candidate.taskSummary, existingSkills, deps) : await lifecycle.checkDuplicate(candidate.domain, candidate.taskSummary);
		if (result.duplicate) return {
			shouldCreate: false,
			existingSkill: result.existingName
		};
		return { shouldCreate: true };
	}
	async patchSkill(patch, deps) {
		const rawContent = await deps.writer.readRawSkill(patch.name);
		if (rawContent === null) return {
			ok: false,
			error: `Skill not found: ${patch.name}`
		};
		if (patch.replacements && patch.replacements.length > 0 && !deps.generateText) {
			let updated = rawContent;
			for (const { oldText, newText } of patch.replacements) {
				if (!updated.includes(oldText)) return {
					ok: false,
					error: `Text not found in skill: "${oldText}"`
				};
				updated = updated.replace(oldText, newText);
			}
			return {
				ok: true,
				updatedPath: await deps.writer.updateSkill(patch.name, updated)
			};
		}
		if (!deps.generateText) return {
			ok: false,
			error: "LLM text generation required but not provided"
		};
		const replacementsSection = patch.replacements ? `\nSpecific text replacements:\n${patch.replacements.map((r) => `- Replace "${r.oldText}" with "${r.newText}"`).join("\n")}\n` : "";
		const prompt = `You are a skill patching assistant. Update the following SKILL.md based on the user's instructions.

Current SKILL.md content:
---
${rawContent}
---

Instructions for changes:
${patch.instructions}${replacementsSection}

Return ONLY the complete updated SKILL.md content with YAML frontmatter preserved. Do not include any explanation outside the SKILL.md content.`;
		try {
			const updatedContent = await deps.generateText(prompt);
			return {
				ok: true,
				updatedPath: await deps.writer.updateSkill(patch.name, updatedContent)
			};
		} catch (err) {
			return {
				ok: false,
				error: `Failed to patch skill: ${String(err)}`
			};
		}
	}
};
//#endregion
export { EvolutionEngine };
