Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python
11.3 KB
Raw Blame
from __future__ import annotations

import asyncio
from unittest.mock import patch

from jiuwenswarm.agents.harness.common.recommendation.situation_report import (
    _format_skills_for_llm,
)
from jiuwenswarm.agents.harness.common.tools.skill_toolkits import SkillToolkit
from jiuwenswarm.server.runtime.skill.skill_manager import SkillManager


def test_uninstall_skill_removes_local_skill_without_plugin_record(tmp_path):
    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    source = tmp_path / "source-skill"
    source.mkdir()
    (source / "SKILL.md").write_text(
        "---\nname: local-only-skill\ndescription: local only\n---\nbody\n",
        encoding="utf-8",
    )

    imported = asyncio.run(manager.handle_skills_import_local({"path": str(source)}))
    assert imported["success"] is True
    assert manager.get_installed_plugins() == []
    assert (tmp_path / "workspace" / "skills" / "local-only-skill").is_dir()

    result = asyncio.run(toolkit.uninstall_skill("local-only-skill"))

    assert result["success"] is True
    assert result["removed"] is True
    assert not (tmp_path / "workspace" / "skills" / "local-only-skill").exists()
    assert manager.get_local_skills() == []


def test_uninstall_skill_matches_display_name_case_insensitively(tmp_path, monkeypatch):
    """UI/Agent 可能传入 Weather,内部规范名是 weather,应能成功卸载。"""
    import io
    import zipfile

    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    buf = io.BytesIO()
    with zipfile.ZipFile(buf, "w") as zf:
        zf.writestr(
            "weather/SKILL.md",
            "---\nname: weather\ndescription: Get weather\nversion: 1.0.0\n---\nbody\n",
        )
    zip_content = buf.getvalue()

    class _Resp:
        status_code = 200
        content = zip_content

        def raise_for_status(self):
            return None

    class _Client:
        async def __aenter__(self):
            return self

        async def __aexit__(self, *args):
            return False

        async def get(self, url, *, params, headers):
            return _Resp()

    monkeypatch.setattr(
        "jiuwenswarm.server.runtime.skill.skill_manager.httpx.AsyncClient",
        lambda timeout: _Client(),
    )
    asyncio.run(manager.handle_skills_clawhub_set_token({"token": "t"}))
    installed = asyncio.run(
        manager.handle_skills_clawhub_download(
            {"slug": "weather", "owner_handle": "steipete", "display_name": "Weather"}
        )
    )
    assert installed["success"] is True
    assert installed["skill"]["name"] == "weather"
    assert installed["skill"]["display_name"] == "Weather"

    # 用展示名大小写卸载
    result = asyncio.run(toolkit.uninstall_skill("Weather"))
    assert result["success"] is True
    assert result["removed"] is True
    assert result["name"] == "weather"
    assert not (tmp_path / "workspace" / "skills" / "weather").exists()


def test_find_installed_by_target_clawhub_owner_slug_interops_with_plain_slug(tmp_path):
    """新 origin=clawhub:owner/slug 时,纯 slug / 旧 origin 都应判已安装。"""
    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)
    manager._add_local_skill(
        {
            "name": "weather",
            "display_name": "Weather",
            "origin": "clawhub:steipete/weather",
            "source": "clawhub",
        }
    )

    by_owner = toolkit._find_installed_by_target("steipete/weather", "clawhub")
    by_slug = toolkit._find_installed_by_target("weather", "clawhub")
    assert by_owner is not None and by_owner["name"] == "weather"
    assert by_slug is not None and by_slug["name"] == "weather"

    # 旧版 origin 也应被 owner/slug 与纯 slug 命中
    manager._add_local_skill(
        {
            "name": "legacy-skill",
            "origin": "clawhub:legacy-skill",
            "source": "clawhub",
        }
    )
    assert toolkit._find_installed_by_target("alice/legacy-skill", "clawhub") is not None
    assert toolkit._find_installed_by_target("legacy-skill", "clawhub") is not None


def test_install_skill_clawhub_already_installed_when_origin_has_owner(tmp_path, monkeypatch):
    """Agent 用纯 slug 再装时,应对已有 clawhub:owner/slug 返回 already_installed。"""
    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)
    manager._add_local_skill(
        {
            "name": "weather",
            "display_name": "Weather",
            "origin": "clawhub:steipete/weather",
            "source": "clawhub",
        }
    )
    (tmp_path / "workspace" / "skills" / "weather").mkdir(parents=True)

    async def _should_not_download(_params):
        raise AssertionError("should skip download when already installed")

    monkeypatch.setattr(manager, "handle_skills_clawhub_download", _should_not_download)

    result = asyncio.run(toolkit.install_skill("weather", source="clawhub"))
    assert result["success"] is True
    assert result["already_installed"] is True
    assert result["name"] == "weather"


def test_search_builtin_skills_matches_name_and_description(tmp_path):
    builtin_dir = tmp_path / "builtin_skills"
    builtin_dir.mkdir()
    user_skills_dir = tmp_path / "user_skills"
    user_skills_dir.mkdir()

    skill_a = builtin_dir / "deep-research"
    skill_a.mkdir()
    (skill_a / "SKILL.md").write_text(
        "---\nname: openJiuwen-DeepSearch\ndescription: deep search and research report\n---\nbody\n",
        encoding="utf-8",
    )

    skill_b = builtin_dir / "ppt-helper"
    skill_b.mkdir()
    (skill_b / "SKILL.md").write_text(
        "---\nname: ppt-helper\ndescription: generate PPT slides\n---\nbody\n",
        encoding="utf-8",
    )

    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    with patch(
        "jiuwenswarm.common.utils.get_builtin_skills_dir",
        return_value=builtin_dir,
    ), patch(
        "jiuwenswarm.common.utils.get_agent_skills_dir",
        return_value=user_skills_dir,
    ):
        results = toolkit._search_builtin_skills("deep", set(), 10)

    assert len(results) == 1
    assert results[0]["name"] == "openJiuwen-DeepSearch"
    assert results[0]["source"] == "builtin"
    assert results[0]["identifier"] == "openJiuwen-DeepSearch"
    assert results[0]["is_builtin"] is True
    assert results[0]["is_builtin_source"] is True


def test_search_builtin_skills_skips_already_installed(tmp_path):
    builtin_dir = tmp_path / "builtin_skills"
    builtin_dir.mkdir()
    user_skills_dir = tmp_path / "user_skills"
    user_skills_dir.mkdir()

    skill_a = builtin_dir / "deep-research"
    skill_a.mkdir()
    (skill_a / "SKILL.md").write_text(
        "---\nname: openJiuwen-DeepSearch\ndescription: deep search\n---\nbody\n",
        encoding="utf-8",
    )

    installed_copy = user_skills_dir / "deep-research"
    installed_copy.mkdir()

    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    with patch(
        "jiuwenswarm.common.utils.get_builtin_skills_dir",
        return_value=builtin_dir,
    ), patch(
        "jiuwenswarm.common.utils.get_agent_skills_dir",
        return_value=user_skills_dir,
    ):
        results = toolkit._search_builtin_skills("deep", set(), 10)

    assert len(results) == 0


def test_search_skill_with_builtin_source(tmp_path):
    builtin_dir = tmp_path / "builtin_skills"
    builtin_dir.mkdir()
    user_skills_dir = tmp_path / "user_skills"
    user_skills_dir.mkdir()

    skill_a = builtin_dir / "deep-research"
    skill_a.mkdir()
    (skill_a / "SKILL.md").write_text(
        "---\nname: openJiuwen-DeepSearch\ndescription: deep search and report\n---\nbody\n",
        encoding="utf-8",
    )

    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    with patch(
        "jiuwenswarm.common.utils.get_builtin_skills_dir",
        return_value=builtin_dir,
    ), patch(
        "jiuwenswarm.common.utils.get_agent_skills_dir",
        return_value=user_skills_dir,
    ):
        result = asyncio.run(toolkit.search_skill("deep", source="builtin"))

    assert result["success"] is True
    assert result["source"] == "builtin"
    assert len(result["items"]) == 1
    assert result["items"][0]["name"] == "openJiuwen-DeepSearch"


def test_install_skill_builtin_source_routes_to_handle_skills_install_builtin(tmp_path):
    builtin_dir = tmp_path / "builtin_skills"
    builtin_dir.mkdir()
    user_skills_dir = tmp_path / "workspace" / "skills"
    user_skills_dir.mkdir(parents=True)

    skill_a = builtin_dir / "my-builtin-skill"
    skill_a.mkdir()
    (skill_a / "SKILL.md").write_text(
        "---\nname: my-builtin-skill\ndescription: a builtin skill\n---\nbody\n",
        encoding="utf-8",
    )

    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    with patch(
        "jiuwenswarm.server.runtime.skill.skill_manager.get_builtin_skills_dir",
        return_value=builtin_dir,
    ):
        result = asyncio.run(
            toolkit.install_skill("my-builtin-skill", source="builtin")
        )

    assert result["success"] is True
    assert result["source"] == "builtin"
    assert (user_skills_dir / "my-builtin-skill").is_dir()


def test_enterprise_skill_tools_do_not_expose_or_install_builtin_source(
    tmp_path,
    monkeypatch,
):
    monkeypatch.setenv("JIUWENSWARM_EDITION", "enterprise")
    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)
    tools = {tool.card.name: tool for tool in toolkit.get_tools()}
    search_sources = tools["search_skill"].card.input_params["properties"]["source"][
        "enum"
    ]
    install_sources = tools["install_skill"].card.input_params["properties"]["source"][
        "enum"
    ]
    result = asyncio.run(toolkit.install_skill("my-builtin-skill", "builtin"))

    assert "builtin" not in search_sources
    assert "builtin" not in install_sources
    assert result["success"] is False
    assert result["error_code"] == "builtin_not_available"


def test_skill_tool_descriptions_are_chinese(tmp_path):
    manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
    toolkit = SkillToolkit(manager)

    tools = {t.card.name: t.card.description for t in toolkit.get_tools()}

    assert "从 SkillNet、ClawHub、TeamSkillsHub 及内置目录搜索可安装技能。" in tools["search_skill"]
    assert "安装技能。" in tools["install_skill"]
    assert tools["uninstall_skill"] == "按名称卸载已安装的技能。"


def test_format_skills_for_llm_distinguishes_install_status():
    skills = [
        {"name": "installed-skill", "description": "already there", "installed": True, "source": "local"},
        {"name": "builtin-skill", "description": "builtin not installed", "installed": False, "source": "builtin"},
        {"name": "marketplace-skill", "description": "from marketplace", "installed": False, "source": "clawhub"},
    ]
    rendered = _format_skills_for_llm(skills)

    assert "- installed-skill | already there [已安装]" in rendered
    assert "- builtin-skill | builtin not installed [未安装·内置技能]" in rendered
    assert "- marketplace-skill | from marketplace [未安装]" in rendered