"""load_skill 工具: agent 主动加载某个 skill 的完整 SKILL.md。

这是渐进披露的中间层 (Activation): 启动时只看到 name+description,
认为自己要做某类任务时,调 load_skill 拿到完整指引。
"""
from __future__ import annotations

from pathlib import Path
from typing import Optional

from core.skills import SkillRegistry

from .base import Tool


class LoadSkillTool(Tool):
    name = "load_skill"
    description = (
        "Load full instructions for a skill. Call this when the current task matches "
        "a skill's domain (e.g. writing PPT, coding, research proposal). "
        "Returns the SKILL.md body, which may reference further files in skills/<name>/references."
    )
    parameters = {
        "type": "object",
        "properties": {
            "name": {
                "type": "string",
                "description": "Skill name as listed in the system prompt's discovery block",
            }
        },
        "required": ["name"],
    }

    def __init__(
        self,
        registry: SkillRegistry,
        base_dir: Optional[Path] = None,
        user_root: Optional[Path] = None,
    ) -> None:
        super().__init__(base_dir, user_root=user_root)
        self.registry = registry

    def execute(self, name: str) -> str:
        skill = self.registry.get(name)
        if skill is None:
            available = ", ".join(self.registry.skills.keys()) or "(none)"
            return f"[Error] skill '{name}' not found. Available: {available}"
        body = skill.full_content()
        # docker backend 下 fs/shell/run_python 都在容器里跑,skill 目录按来源 bind 到
        # 不同挂载点(内置 → /sandbox/skills:ro,用户 → /workspace/.skills);registry
        # 据 skill.source 给容器内路径,否则 LLM 拿 host 绝对路径在沙盒里 read 不到
        # references。host backend → None,退回 skill_dir 原 host 绝对路径。
        dir_str = self.registry.container_dir(skill) or str(skill.skill_dir)
        header = f"[skill={skill.name}, dir={dir_str}]\n"
        return header + body
