feat(webui): add skills marketplace
This commit is contained in:
1 parent
129b74b4cf
commit
ba0ba4749d
23 files changed
+3042
-246
No files matched your search
+161
-7
@@ -2,10 +2,23 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shlex
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from nanobot.agent.skills import SkillsLoader
|
||||
from nanobot.config.loader import load_config, save_config
|
||||
|
||||
|
||||
class SkillManagementError(Exception):
|
||||
"""A safe skill-management error for the WebUI."""
|
||||
|
||||
def __init__(self, message: str, *, status: int = 400) -> None:
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.status = status
|
||||
|
||||
|
||||
def webui_skills_payload(
|
||||
@@ -14,12 +27,17 @@ def webui_skills_payload(
|
||||
disabled_skills: set[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return agent skills without leaking local filesystem paths."""
|
||||
loader = SkillsLoader(workspace_path, disabled_skills=disabled_skills)
|
||||
loader = SkillsLoader(workspace_path)
|
||||
entries = sorted(
|
||||
loader.list_skills(filter_unavailable=False),
|
||||
key=lambda entry: (entry.get("source") != "workspace", entry["name"]),
|
||||
)
|
||||
return {"skills": [_skill_payload(loader, entry) for entry in entries]}
|
||||
return {
|
||||
"skills": [
|
||||
_skill_payload(loader, entry, disabled_skills=disabled_skills)
|
||||
for entry in entries
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def webui_skill_detail_payload(
|
||||
@@ -29,26 +47,114 @@ def webui_skill_detail_payload(
|
||||
disabled_skills: set[str] | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Return a single skill's safe detail payload."""
|
||||
loader = SkillsLoader(workspace_path, disabled_skills=disabled_skills)
|
||||
loader = SkillsLoader(workspace_path)
|
||||
entries = loader.list_skills(filter_unavailable=False)
|
||||
entry = next((item for item in entries if item["name"] == name), None)
|
||||
if entry is None:
|
||||
return None
|
||||
metadata = loader.get_skill_metadata(name)
|
||||
return {
|
||||
**_skill_payload(loader, entry),
|
||||
**_skill_payload(
|
||||
loader,
|
||||
entry,
|
||||
metadata=metadata,
|
||||
disabled_skills=disabled_skills,
|
||||
),
|
||||
"requirements": loader.get_skill_requirements(name),
|
||||
"install_options": _install_options(metadata),
|
||||
"raw_markdown": loader.load_skill(name) or "",
|
||||
}
|
||||
|
||||
|
||||
def _skill_payload(loader: SkillsLoader, entry: dict[str, str]) -> dict[str, Any]:
|
||||
def set_webui_skill_enabled(
|
||||
workspace_path: Path,
|
||||
name: str,
|
||||
*,
|
||||
enabled: bool,
|
||||
disabled_skills: set[str],
|
||||
) -> dict[str, Any]:
|
||||
"""Persist and apply one skill's enabled state."""
|
||||
_require_skill_entry(workspace_path, name)
|
||||
config = load_config()
|
||||
next_disabled = set(config.agents.defaults.disabled_skills)
|
||||
if enabled:
|
||||
next_disabled.discard(name)
|
||||
else:
|
||||
next_disabled.add(name)
|
||||
if next_disabled != set(config.agents.defaults.disabled_skills):
|
||||
config.agents.defaults.disabled_skills = sorted(next_disabled)
|
||||
save_config(config)
|
||||
disabled_skills.clear()
|
||||
disabled_skills.update(next_disabled)
|
||||
return {"name": name, "enabled": enabled, "deleted": False}
|
||||
|
||||
|
||||
def delete_webui_skill(
|
||||
workspace_path: Path,
|
||||
name: str,
|
||||
*,
|
||||
disabled_skills: set[str],
|
||||
) -> dict[str, Any]:
|
||||
"""Delete one workspace skill and remove its disabled-state entry."""
|
||||
entry = _require_skill_entry(workspace_path, name)
|
||||
if entry.get("source") != "workspace":
|
||||
raise SkillManagementError("built-in skills cannot be deleted", status=403)
|
||||
|
||||
skills_root = (workspace_path.expanduser().resolve() / "skills").resolve()
|
||||
target = skills_root / name
|
||||
if target.parent != skills_root:
|
||||
raise SkillManagementError("invalid skill name")
|
||||
if target.is_symlink():
|
||||
target.unlink()
|
||||
elif target.is_dir():
|
||||
shutil.rmtree(target)
|
||||
else:
|
||||
raise SkillManagementError("skill directory was not found", status=404)
|
||||
|
||||
config = load_config()
|
||||
next_disabled = set(config.agents.defaults.disabled_skills)
|
||||
if name in next_disabled:
|
||||
next_disabled.remove(name)
|
||||
config.agents.defaults.disabled_skills = sorted(next_disabled)
|
||||
save_config(config)
|
||||
disabled_skills.clear()
|
||||
disabled_skills.update(next_disabled)
|
||||
return {"name": name, "enabled": False, "deleted": True}
|
||||
|
||||
|
||||
def _require_skill_entry(workspace_path: Path, name: str) -> dict[str, str]:
|
||||
if not name or "/" in name or "\\" in name:
|
||||
raise SkillManagementError("invalid skill name")
|
||||
entry = next(
|
||||
(
|
||||
item
|
||||
for item in SkillsLoader(workspace_path).list_skills(filter_unavailable=False)
|
||||
if item["name"] == name
|
||||
),
|
||||
None,
|
||||
)
|
||||
if entry is None:
|
||||
raise SkillManagementError("skill not found", status=404)
|
||||
return entry
|
||||
|
||||
|
||||
def _skill_payload(
|
||||
loader: SkillsLoader,
|
||||
entry: dict[str, str],
|
||||
*,
|
||||
metadata: dict[str, Any] | None = None,
|
||||
disabled_skills: set[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
name = entry["name"]
|
||||
metadata = loader.get_skill_metadata(name)
|
||||
metadata = metadata if metadata is not None else loader.get_skill_metadata(name)
|
||||
available, unavailable_reason = loader.get_skill_availability(name)
|
||||
source = entry.get("source", "unknown")
|
||||
return {
|
||||
"name": name,
|
||||
"description": _description(metadata, name),
|
||||
"source": entry.get("source", "unknown"),
|
||||
"source": source,
|
||||
"enabled": name not in (disabled_skills or set()),
|
||||
"deletable": source == "workspace",
|
||||
"available": available,
|
||||
"unavailable_reason": unavailable_reason,
|
||||
}
|
||||
@@ -59,3 +165,51 @@ def _description(metadata: dict[str, Any] | None, fallback: str) -> str:
|
||||
return fallback
|
||||
value = metadata.get("description")
|
||||
return value.strip() if isinstance(value, str) and value.strip() else fallback
|
||||
|
||||
|
||||
def _nanobot_metadata(metadata: dict[str, Any] | None) -> dict[str, Any]:
|
||||
if metadata is None:
|
||||
return {}
|
||||
raw = metadata.get("metadata")
|
||||
if isinstance(raw, str):
|
||||
try:
|
||||
raw = json.loads(raw)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
return {}
|
||||
if not isinstance(raw, dict):
|
||||
return {}
|
||||
payload = raw.get("nanobot", raw.get("openclaw", {}))
|
||||
return payload if isinstance(payload, dict) else {}
|
||||
|
||||
|
||||
def _install_options(metadata: dict[str, Any] | None) -> list[dict[str, str]]:
|
||||
"""Return safe, copyable setup commands declared by a skill."""
|
||||
install = _nanobot_metadata(metadata).get("install")
|
||||
if not isinstance(install, list):
|
||||
return []
|
||||
|
||||
options: list[dict[str, str]] = []
|
||||
for item in install:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
kind = item.get("kind")
|
||||
package = item.get("formula") if kind == "brew" else item.get("package")
|
||||
if not isinstance(package, str) or not package.strip():
|
||||
continue
|
||||
if kind == "brew":
|
||||
command = f"brew install {shlex.quote(package.strip())}"
|
||||
elif kind == "apt":
|
||||
command = f"sudo apt-get install -y {shlex.quote(package.strip())}"
|
||||
else:
|
||||
continue
|
||||
option_id = item.get("id")
|
||||
label = item.get("label")
|
||||
options.append(
|
||||
{
|
||||
"id": option_id if isinstance(option_id, str) else kind,
|
||||
"kind": kind,
|
||||
"label": label if isinstance(label, str) else f"Install with {kind}",
|
||||
"command": command,
|
||||
}
|
||||
)
|
||||
return options
|
||||
Reference in new issue
Block a user