// Force strict mode and setup for ESM
"use strict";
import {
  buildSkillLlmContent,
  escapeXml,
  registerSkillHooks
} from "./chunk-G7YTSRES.js";
import "./chunk-4AOCVI6J.js";
import {
  SkillLaunchEvent,
  logSkillLaunch
} from "./chunk-7QVG36DW.js";
import "./chunk-UWCTAVOD.js";
import "./chunk-OFEVLU4C.js";
import "./chunk-PR4T27R7.js";
import "./chunk-MAY32HXD.js";
import "./chunk-D5NTAHYL.js";
import "./chunk-T4VD6OJ4.js";
import "./chunk-RDYWTWEM.js";
import "./chunk-YJLGXDQJ.js";
import {
  BaseDeclarativeTool,
  BaseToolInvocation,
  ToolDisplayNames,
  ToolNames
} from "./chunk-PVVL5Q3W.js";
import {
  createDebugLogger
} from "./chunk-XP27SJMH.js";
import "./chunk-ZERZSAZL.js";
import "./chunk-QN5NZ3UQ.js";
import "./chunk-BR4QREVK.js";
import "./chunk-QWSRH265.js";
import {
  init_esbuild_shims
} from "./chunk-A4BMJM77.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/core/src/tools/skill.ts
init_esbuild_shims();
import path from "path";
var debugLogger = createDebugLogger("SKILL");
var SkillTool = class _SkillTool extends BaseDeclarativeTool {
  constructor(config) {
    const initialSchema = {
      type: "object",
      properties: {
        skill: {
          type: "string",
          description: 'The skill name (no arguments). E.g., "pdf" or "xlsx"'
        }
      },
      required: ["skill"],
      additionalProperties: false,
      $schema: "http://json-schema.org/draft-07/schema#"
    };
    super(
      _SkillTool.Name,
      ToolDisplayNames.SKILL,
      "Execute a skill within the main conversation. Loading available skills...",
      // Initial description
      "read" /* Read */,
      initialSchema,
      false,
      // isOutputMarkdown
      false
      // canUpdateOutput
    );
    this.config = config;
    const skillManager = config.getSkillManager();
    if (!skillManager) {
      throw new Error("SkillManager not available");
    }
    this.skillManager = skillManager;
    this.removeChangeListener = this.skillManager.addChangeListener(
      () => this.refreshSkills()
    );
    this.refreshSkills();
  }
  static {
    __name(this, "SkillTool");
  }
  static Name = ToolNames.SKILL;
  skillManager;
  availableSkills = [];
  // Conditional skills (with `paths:`) that exist on disk but have not yet
  // been activated by a matching tool invocation. Tracked separately so
  // validateToolParams can give a distinct error message when the model
  // names one of these: "gated by paths:, access a matching file first"
  // instead of the generic "not found".
  pendingConditionalSkillNames = /* @__PURE__ */ new Set();
  modelInvocableCommands = [];
  loadedSkillNames = /* @__PURE__ */ new Set();
  // Cleanup function returned by `addChangeListener`. Stored so per-agent
  // SkillTool instances (subagents share the parent's SkillManager) can
  // detach their listener at teardown — without this the SkillManager
  // accumulates listeners across subagent lifetimes, and each path
  // activation would serialize through every stale listener's
  // refreshSkills / setTools round-trip.
  removeChangeListener;
  /**
   * Asynchronously initializes the tool by loading available skills
   * and updating the description and schema.
   */
  async refreshSkills() {
    try {
      const allSkills = await this.skillManager.listSkills();
      this.availableSkills = allSkills.filter(
        (s) => !s.disableModelInvocation && this.skillManager.isSkillActive(s)
      );
      this.pendingConditionalSkillNames = new Set(
        allSkills.filter(
          (s) => !s.disableModelInvocation && s.paths && s.paths.length > 0 && !this.skillManager.isSkillActive(s)
        ).map((s) => s.name)
      );
      const provider = this.config.getModelInvocableCommandsProvider();
      const allCommands = provider ? provider() : [];
      const fileBasedSkillNames = new Set(
        allSkills.filter((s) => !s.disableModelInvocation).map((s) => s.name)
      );
      this.modelInvocableCommands = allCommands.filter(
        (cmd) => !fileBasedSkillNames.has(cmd.name)
      );
      this.updateDescriptionAndSchema();
    } catch (error) {
      debugLogger.warn("Failed to load skills for Skills tool:", error);
      this.availableSkills = [];
      this.pendingConditionalSkillNames = /* @__PURE__ */ new Set();
      this.modelInvocableCommands = [];
      this.updateDescriptionAndSchema();
    } finally {
      const geminiClient = this.config.getGeminiClient();
      if (geminiClient) {
        await geminiClient.setTools();
      }
    }
  }
  /**
   * Updates the tool's description and schema based on available skills and
   * model-invocable commands (e.g. bundled skills, file commands, MCP prompts).
   */
  updateDescriptionAndSchema() {
    const allSkillEntries = [];
    for (const skill of this.availableSkills) {
      const descText = `${escapeXml(skill.description)}${skill.whenToUse ? ` \u2014 ${escapeXml(skill.whenToUse)}` : ""} (${skill.level})`;
      allSkillEntries.push(`<skill>
<name>
${escapeXml(skill.name)}
</name>
<description>
${descText}
</description>
<location>
${skill.level}
</location>
</skill>`);
    }
    for (const cmd of this.modelInvocableCommands) {
      allSkillEntries.push(`<skill>
<name>
${escapeXml(cmd.name)}
</name>
<description>
${escapeXml(cmd.description)}
</description>
</skill>`);
    }
    let skillDescriptions = "";
    if (allSkillEntries.length === 0) {
      skillDescriptions = "No skills are currently configured. Skills can be created by adding directories with SKILL.md files to .qwen/skills/ or ~/.qwen/skills/.";
    } else {
      skillDescriptions = allSkillEntries.join("\n");
    }
    const baseDescription = `Execute a skill within the main conversation

<skills_instructions>
When users ask you to perform tasks, check if any of the available skills below can help complete the task more effectively. Skills provide specialized capabilities and domain knowledge.

How to invoke:
- Use this tool with the skill name only (no arguments)
- Examples:
  - \`skill: "pdf"\` - invoke the pdf skill
  - \`skill: "xlsx"\` - invoke the xlsx skill
  - \`skill: "ms-office-suite:pdf"\` - invoke using fully qualified name

Important:
- When a skill is relevant, you must invoke this tool IMMEDIATELY as your first action
- NEVER just announce or mention a skill in your text response without actually calling this tool
- This is a BLOCKING REQUIREMENT: invoke the relevant Skill tool BEFORE generating any other response about the task
- Only use skills listed in <available_skills> below
- Do not invoke a skill that is already running
- Do not use this tool for built-in CLI commands (like /help, /clear, etc.)
- When executing scripts or loading referenced files, ALWAYS resolve absolute paths from skill's base directory. Examples:
  - \`bash scripts/init.sh\` -> \`bash /path/to/skill/scripts/init.sh\`
  - \`python scripts/helper.py\` -> \`python /path/to/skill/scripts/helper.py\`
  - \`reference.md\` -> \`/path/to/skill/reference.md\`
</skills_instructions>

<available_skills>
${skillDescriptions}
</available_skills>`;
    this.description = baseDescription;
  }
  validateToolParams(params) {
    if (!params.skill || typeof params.skill !== "string" || params.skill.trim() === "") {
      return 'Parameter "skill" must be a non-empty string.';
    }
    const skillExists = this.availableSkills.some(
      (skill) => skill.name === params.skill
    );
    if (skillExists) return null;
    const commandExists = this.modelInvocableCommands.some(
      (cmd) => cmd.name === params.skill
    );
    if (commandExists) return null;
    if (this.pendingConditionalSkillNames.has(params.skill)) {
      return `Skill "${params.skill}" is gated by path-based activation (paths: frontmatter) and is not yet available. Access a file matching its paths patterns first to activate it.`;
    }
    const availableNames = [
      ...this.availableSkills.map((s) => s.name),
      ...this.modelInvocableCommands.map((c) => c.name)
    ];
    if (availableNames.length === 0) {
      return `Skill "${params.skill}" not found. No skills are currently available.`;
    }
    return `Skill "${params.skill}" not found. Available skills: ${availableNames.join(", ")}`;
  }
  createInvocation(params) {
    return new SkillToolInvocation(
      this.config,
      this.skillManager,
      params,
      (name) => this.loadedSkillNames.add(name),
      this.config.getModelInvocableCommandsExecutor()
    );
  }
  toAutoClassifierInput(params) {
    return { skill: params.skill };
  }
  getAvailableSkillNames() {
    return this.availableSkills.map((skill) => skill.name);
  }
  /**
   * Returns the set of skill names that have been successfully loaded
   * (invoked) during the current session. Used by /context to attribute
   * loaded skill body tokens separately from the tool-definition cost.
   */
  getLoadedSkillNames() {
    return this.loadedSkillNames;
  }
  /**
   * Clears the loaded-skills tracking. Should be called when the session
   * is reset (e.g. /clear) so that stale body-token data is not shown.
   */
  clearLoadedSkills() {
    this.loadedSkillNames.clear();
  }
  /**
   * Detach the change listener from SkillManager. Tool registries call
   * this on teardown (mirroring AgentTool's pattern). Per-subagent
   * SkillTool instances share the parent's SkillManager via
   * `InProcessBackend.createPerAgentConfig`, so without dispose the
   * SkillManager would accumulate one stale listener per subagent
   * lifetime — and `notifyChangeListeners` is now `await`-ed
   * sequentially, so each path activation would serialize through every
   * accumulated listener's refreshSkills + setTools round-trip.
   */
  dispose() {
    this.removeChangeListener();
  }
};
var SkillToolInvocation = class extends BaseToolInvocation {
  constructor(config, skillManager, params, onSkillLoaded, commandExecutor = null) {
    super(params);
    this.config = config;
    this.skillManager = skillManager;
    this.onSkillLoaded = onSkillLoaded;
    this.commandExecutor = commandExecutor;
  }
  static {
    __name(this, "SkillToolInvocation");
  }
  getDescription() {
    return `Use skill: "${this.params.skill}"`;
  }
  /**
   * Skills load user-defined code that runs with the agent's tool
   * access — they're a privileged sink. In AUTO mode the classifier
   * needs to inspect the skill name and any inline args before the
   * skill loads, but the scheduler short-circuits at L4 when
   * `finalPermission === 'allow'`. The L3 default must be `'ask'` so
   * the classifier projection added in this PR can be reached.
   */
  async getDefaultPermission() {
    return "ask";
  }
  async execute(_signal, _updateOutput) {
    try {
      const skill = await this.skillManager.loadSkillForRuntime(
        this.params.skill
      );
      if (!skill) {
        if (this.commandExecutor) {
          const content = await this.commandExecutor(this.params.skill);
          if (content !== null) {
            logSkillLaunch(
              this.config,
              new SkillLaunchEvent(this.params.skill, true)
            );
            this.onSkillLoaded(this.params.skill);
            return {
              llmContent: [{ text: content }],
              returnDisplay: `Executed command: ${this.params.skill}`
            };
          }
        }
        logSkillLaunch(
          this.config,
          new SkillLaunchEvent(this.params.skill, false)
        );
        const parseErrors = this.skillManager.getParseErrors();
        const errorMessages = [];
        for (const [filePath, error] of parseErrors) {
          if (filePath.includes(this.params.skill)) {
            errorMessages.push(`Parse error at ${filePath}: ${error.message}`);
          }
        }
        const errorDetail = errorMessages.length > 0 ? `
Errors:
${errorMessages.join("\n")}` : "";
        return {
          llmContent: `Skill "${this.params.skill}" not found.${errorDetail}`,
          returnDisplay: `Skill "${this.params.skill}" not found.${errorDetail}`
        };
      }
      logSkillLaunch(
        this.config,
        new SkillLaunchEvent(this.params.skill, true)
      );
      this.onSkillLoaded(this.params.skill);
      debugLogger.debug("Skill hooks check:", {
        hasHooks: !!skill.hooks,
        hooksKeys: skill.hooks ? Object.keys(skill.hooks) : [],
        skillName: skill.name
      });
      if (skill.hooks) {
        const hookSystem = this.config.getHookSystem();
        const sessionId = this.config.getSessionId();
        debugLogger.debug("Hook system and session:", {
          hasHookSystem: !!hookSystem,
          sessionId
        });
        if (hookSystem && sessionId) {
          const sessionHooksManager = hookSystem.getSessionHooksManager();
          const hookCount = registerSkillHooks(
            sessionHooksManager,
            sessionId,
            skill
          );
          if (hookCount > 0) {
            debugLogger.info(
              `Registered ${hookCount} hooks from skill "${this.params.skill}"`
            );
          } else {
            debugLogger.warn(
              `No hooks registered from skill "${this.params.skill}"`
            );
          }
        }
      } else {
        debugLogger.warn(
          `Skill "${this.params.skill}" has no hooks to register`
        );
      }
      const baseDir = path.dirname(skill.filePath);
      const llmContent = buildSkillLlmContent(baseDir, skill.body);
      return {
        llmContent: [{ text: llmContent }],
        returnDisplay: skill.description,
        modelOverride: skill.model
      };
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      debugLogger.error(`[SkillsTool] Error using skill: ${errorMessage}`);
      logSkillLaunch(
        this.config,
        new SkillLaunchEvent(this.params.skill, false)
      );
      return {
        llmContent: `Failed to load skill "${this.params.skill}": ${errorMessage}`,
        returnDisplay: `Failed to load skill "${this.params.skill}": ${errorMessage}`
      };
    }
  }
};
export {
  SkillTool,
  buildSkillLlmContent
};
/**
 * @license
 * Copyright 2025 Qwen
 * SPDX-License-Identifier: Apache-2.0
 */
