import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { resolveStateDir } from "../../config/paths-state.js";
import { init_paths, resolveBundledSkillsDir } from "../../config/paths.js";
import { createSkillConfigManager, isSkillEnabled } from "./config.js";
import { formatSkillsForPrompt, selectSkillsVisibleInPrompt } from "./format-skills-prompt.js";
import { createSkillLoader } from "./index.js";
//#region src/agent/skills/skill-manager.ts
/**
* Skill Manager - Manages skills loading and expansion
*
* Handles skill initialization, reloading, and command expansion.
*/
init_paths();
init_logger();
const log = createLogger("SkillManager");
var SkillManager = class {
	skillPrompt = "";
	skills = [];
	skillLoader = createSkillLoader();
	workspace;
	bundledSkillsDir;
	constructor(workspace, bundledSkillsDir) {
		this.workspace = workspace;
		this.bundledSkillsDir = bundledSkillsDir || resolveBundledSkillsDir();
		this.initialize();
	}
	/**
	* Initialize skills from workspace and bundled directories
	*/
	initialize() {
		const result = this.skillLoader.init(this.workspace, this.bundledSkillsDir);
		this.skillPrompt = result.prompt;
		this.skills = result.skills;
		for (const diag of result.diagnostics) if (diag.type === "collision") log.warn({
			skill: diag.skillName,
			message: diag.message
		}, "Skill collision");
		else if (diag.type === "warning") log.warn({
			skill: diag.skillName,
			message: diag.message
		}, "Skill warning");
		else if (diag.type === "error") log.error({
			skill: diag.skillName,
			message: diag.message
		}, "Skill error");
		else log.info({
			skill: diag.skillName,
			message: diag.message
		}, "Skill info");
		log.debug({ count: result.skills.length }, "Skills loaded");
	}
	/**
	* Recompute skill XML prompt from current skills.json (no disk rescan).
	*/
	refreshPromptFromConfig() {
		this.skillLoader.refreshPromptFromConfig();
		this.skillPrompt = this.skillLoader.getPrompt();
	}
	/**
	* Reload skills from disk
	*/
	reload() {
		log.info("Reloading skills...");
		const result = this.skillLoader.reload();
		this.skillPrompt = result.prompt;
		this.skills = result.skills;
		for (const diag of result.diagnostics) if (diag.type === "collision") log.warn({
			skill: diag.skillName,
			message: diag.message
		}, "Skill collision");
		else if (diag.type === "warning") log.warn({
			skill: diag.skillName,
			message: diag.message
		}, "Skill warning");
		else if (diag.type === "error") log.error({
			skill: diag.skillName,
			message: diag.message
		}, "Skill error");
		log.info({ count: result.skills.length }, "Skills reloaded");
	}
	/**
	* Get the skill prompt to append to system prompt
	*/
	getPrompt() {
		return this.skillPrompt;
	}
	/**
	* `<available_skills>` XML with optional per-agent allowlist and/or tool-based gating.
	* When both arguments are omitted, returns the cached prompt from disk load (no tool gating).
	*/
	getPromptForSkillAllowlist(allowlist, registeredToolNames) {
		if (allowlist === void 0 && registeredToolNames === void 0) return this.getPrompt();
		const skillsConfig = createSkillConfigManager(resolveStateDir()).load();
		return formatSkillsForPrompt(this.getSkills(), skillsConfig, {
			skillAllowlist: allowlist,
			registeredToolNames
		});
	}
	/** Skills visible for prompts and skills_list given the same indexing options as {@link getPromptForSkillAllowlist}. */
	selectSkillsForAgentIndexing(options) {
		const skillsConfig = createSkillConfigManager(resolveStateDir()).load();
		return selectSkillsVisibleInPrompt(this.getSkills(), skillsConfig, options);
	}
	/**
	* Get all loaded skills
	*/
	getSkills() {
		return [...this.skills];
	}
	/**
	* Get skill names
	*/
	getSkillNames() {
		return this.skills.map((s) => s.name);
	}
	/** Names of skills included in the agent system prompt (`<available_skills>`). */
	getSkillNamesForPrompt() {
		const skillsConfig = createSkillConfigManager(resolveStateDir()).load();
		return this.getSkills().filter((s) => !s.disableModelInvocation && isSkillEnabled(s, skillsConfig)).map((s) => s.name);
	}
	/**
	* Skills that are enabled for model discovery (respects skills.json and requirement checks).
	*/
	getEnabledSkills() {
		const skillsConfig = createSkillConfigManager(resolveStateDir()).load();
		return this.getSkills().filter((s) => !s.disableModelInvocation && isSkillEnabled(s, skillsConfig));
	}
	/**
	* Like {@link getEnabledSkills} plus optional allowlist and tool gating (matches `<available_skills>`).
	*/
	getEnabledSkillsForAgentSession(options) {
		return this.selectSkillsForAgentIndexing(options);
	}
	/**
	* Find a skill by name
	*/
	findSkill(name) {
		return this.skills.find((s) => s.name === name);
	}
	/**
	* Check if a skill exists
	*/
	hasSkill(name) {
		return this.skills.some((s) => s.name === name);
	}
	/**
	* Expand a skill command into a full skill block
	*/
	expandCommand(text) {
		if (!text.startsWith("/skill:")) return text;
		const { skillName, args } = this.parseSkillCommand(text);
		const skill = this.findSkill(skillName);
		if (!skill) {
			log.warn({ skillName }, "Skill not found for expansion");
			return text;
		}
		return this.buildSkillBlock(skill, args);
	}
	/**
	* Parse a /skill: command
	*/
	parseSkillCommand(text) {
		const withoutPrefix = text.slice(7);
		const spaceIndex = withoutPrefix.indexOf(" ");
		if (spaceIndex === -1) return { skillName: withoutPrefix.trim() };
		return {
			skillName: withoutPrefix.slice(0, spaceIndex).trim(),
			args: withoutPrefix.slice(spaceIndex + 1).trim()
		};
	}
	/**
	* Build a skill block for inclusion in the prompt
	*/
	buildSkillBlock(skill, args) {
		let block = `\n\n## Skill: ${skill.name}\n\n`;
		if (skill.description) block += `${skill.description}\n\n`;
		if (skill.content) block += `${skill.content}\n\n`;
		if (args) block += `**Arguments**: ${args}\n\n`;
		return block;
	}
};
//#endregion
export { SkillManager };

//# sourceMappingURL=skill-manager.js.map