var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { tool } from "langchain";
import { z } from "zod";
import { listSkillManifests, loadSkillMarkdown, } from "../skills/registry.js";
const fetchSkillSchema = z.object({
    skillName: z
        .string()
        .describe("Name of the custom AdminForth skill to load, for example fetch_data or mutate_data."),
});
function serializeSkillManifests(skillManifests) {
    return skillManifests.map((skill) => ({
        name: skill.name,
        description: skill.description,
    }));
}
export function createFetchSkillTool(customComponentsDir) {
    return __awaiter(this, void 0, void 0, function* () {
        const availableSkills = yield listSkillManifests(customComponentsDir);
        const availableSkillNames = availableSkills.map((skill) => skill.name);
        return tool((_a) => __awaiter(this, [_a], void 0, function* ({ skillName }) {
            try {
                const skillMarkdown = yield loadSkillMarkdown(skillName, customComponentsDir);
                if (!skillMarkdown) {
                    return [
                        `Skill "${skillName}" not found.`,
                        "Available skills:",
                        JSON.stringify(serializeSkillManifests(availableSkills), null, 2),
                    ].join("\n");
                }
                return skillMarkdown;
            }
            catch (error) {
                return `Failed to load skill "${skillName}": ${error instanceof Error ? error.message : String(error)}`;
            }
        }), {
            name: "fetch_skill",
            description: `Fetch the raw SKILL.md content for a custom AdminForth skill by name.${availableSkillNames.length > 0
                ? ` Available skills: ${availableSkillNames.join(", ")}.`
                : ""}`,
            schema: fetchSkillSchema,
        });
    });
}
