AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
refactor(backend): move the copilot-skills routes into their own feature module (#14471)
Reinier van der Leerd47d92cef34f
diff --git a/autogpt_platform/backend/backend/api/features/skills/__init__.py b/autogpt_platform/backend/backend/api/features/skills/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/autogpt_platform/backend/backend/api/features/skills/model.py b/autogpt_platform/backend/backend/api/features/skills/model.pynew file mode 100644index 0000000000..874926535e--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/skills/model.py@@ -0,0 +1,48 @@+from pydantic import BaseModel+++class CopilotSkillInfo(BaseModel):+ """User-distilled copilot skill metadata for the library UI.++ Defaults (built-in agent-building / MCP-tool guides) are intentionally+ excluded — they cannot be edited or deleted, so surfacing them in the+ user-facing list would add noise without affordances.+ """++ name: str+ description: str+ triggers: list[str] = []+++class CopilotSkillFile(BaseModel):+ """One file of a skill package, by its path relative to the skill folder+ (``scripts/run.py``) — the path the SKILL.md body references it by."""++ path: str+ size_bytes: int+ is_executable: bool = False+++class CopilotSkillDetail(BaseModel):+ """Full SKILL.md content surfaced to the library expand-to-view UI."""++ name: str+ description: str+ triggers: list[str] = []+ body: str+ version: str | None = None+ is_default: bool = False+ # The package's other files (references/, scripts/, assets/). Empty for+ # built-in defaults, which ship as one on-disk markdown file.+ files: list[CopilotSkillFile] = []+++class UploadCopilotSkillRequest(BaseModel):+ """Body for the library UI's "upload skill" action.++ Carries the raw ``SKILL.md`` text (YAML frontmatter + markdown body) the+ user picked from disk; the server parses + validates it so the upload and+ the copilot's ``store_skill`` tool share one source of truth.+ """++ content: strdiff --git a/autogpt_platform/backend/backend/api/features/skills/routes.py b/autogpt_platform/backend/backend/api/features/skills/routes.pynew file mode 100644index 0000000000..634dfeab91--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/skills/routes.py@@ -0,0 +1,375 @@+import logging+from typing import Annotated++from autogpt_libs.auth import get_user_id, requires_user+from fastapi import (+ APIRouter,+ HTTPException,+ Path,+ Query,+ Response,+ Security,+ UploadFile,+)+from fastapi.concurrency import run_in_threadpool+from starlette.status import HTTP_404_NOT_FOUND++from backend.api.features.experts import experts_db+from backend.api.features.skill_zip import (+ MAX_ZIP_BYTES,+ package_from_zip,+ zip_from_package,+)+from backend.api.features.skills.model import (+ CopilotSkillDetail,+ CopilotSkillFile,+ CopilotSkillInfo,+ UploadCopilotSkillRequest,+)+from backend.api.features.store.exceptions import VirusDetectedError, VirusScanError+from backend.copilot.tools.skills import (+ BuiltInSkillError,+ ParsedSkill,+ SkillFile,+ SkillLimitError,+ SkillNotFoundError,+ SkillPackageError,+ delete_user_skill,+ get_default_skill_with_body,+ list_user_skill_files,+ list_user_skills,+ parse_skill_markdown,+ read_user_skill_package,+ read_user_skill_with_body,+ skill_folder,+ store_user_skill,+)++logger = logging.getLogger(__name__)++router = APIRouter(dependencies=[Security(requires_user)])+++async def _require_skill_owner(user_id: str, expert_id: str | None) -> None:+ """A skill owner named on a REST call must be one of the caller's active+ experts; personal Otto (``None``) needs no check."""+ if expert_id is None:+ return+ if not await experts_db.owns_private_active_expert(user_id, expert_id):+ raise HTTPException(+ status_code=HTTP_404_NOT_FOUND, detail=f"Expert '{expert_id}' not found"+ )+++ path="",+ summary="List user-distilled copilot skills",+ operation_id="listCopilotSkills",+)+async def list_copilot_skills(+ user_id: Annotated[str, Security(get_user_id)],+ expert_id: str | None = Query(+ default=None,+ description="List this expert's own skills instead of personal Otto's.",+ ),+) -> list[CopilotSkillInfo]:+ """Return the skills owned by personal Otto, or by one expert.++ Reuses :func:`backend.copilot.tools.skills.list_user_skills` so the+ library UI sees the exact same set the copilot ``<available_skills>``+ block surfaces, minus the built-in defaults (which are read-only and+ handled separately by the copilot runtime).+ """+ await _require_skill_owner(user_id, expert_id)+ skills = await list_user_skills(user_id, expert_id)+ return [+ CopilotSkillInfo(+ name=s.name,+ description=s.description,+ triggers=list(s.triggers),+ )+ for s in skills+ ]+++ path="",+ summary="Upload a copilot skill from a SKILL.md file",+ operation_id="uploadCopilotSkill",+ status_code=201,+ responses={+ 400: {"description": "Malformed SKILL.md or validation error"},+ 409: {"description": "Per-user skill limit reached"},+ },+)+async def upload_copilot_skill(+ user_id: Annotated[str, Security(get_user_id)],+ body: UploadCopilotSkillRequest,+ expert_id: str | None = Query(+ default=None, description="Store the skill as this expert's own."+ ),+) -> CopilotSkillInfo:+ """Create a user-distilled skill from an uploaded ``SKILL.md`` file.++ Parses the canonical frontmatter + body, then reuses+ :func:`backend.copilot.tools.skills.store_user_skill` so an uploaded skill+ is validated, capped, and persisted exactly like one the copilot distils+ via ``store_skill``. Malformed files return 400, the per-user cap returns+ 409, and an existing slug is overwritten (upsert).+ """+ await _require_skill_owner(user_id, expert_id)+ parsed = parse_skill_markdown(body.content)+ if parsed is None:+ raise HTTPException(+ status_code=400,+ detail=(+ "File is not a valid SKILL.md — expected YAML frontmatter with "+ "'name' and 'description' followed by a markdown body."+ ),+ )+ stored = await _store_uploaded_skill(user_id, parsed, expert_id=expert_id)+ return CopilotSkillInfo(+ name=stored.name,+ description=stored.description,+ triggers=list(stored.triggers),+ )+++ path="/package",+ summary="Upload a copilot skill as a zipped package",+ operation_id="uploadCopilotSkillPackage",+ status_code=201,+ responses={+ 400: {"description": "Unreadable archive, or a malformed SKILL.md or path"},+ 409: {"description": "Per-user skill limit reached"},+ 413: {"description": "Archive, file or file count over the package limit"},+ },+)+async def upload_copilot_skill_package(+ user_id: Annotated[str, Security(get_user_id)],+ file: UploadFile,+ expert_id: str | None = Query(+ default=None, description="Store the skill as this expert's own."+ ),+) -> CopilotSkillInfo:+ """Create a skill from a zipped package — a root ``SKILL.md`` plus the+ files beside it.++ The zip is transport only: it is unpacked here and stored as the skill's+ folder, so the model reaches ``scripts/`` and ``references/`` by path+ exactly as it does for a package copied from another skill.+ """+ await _require_skill_owner(user_id, expert_id)+ data = await _read_upload(file, MAX_ZIP_BYTES)+ try:+ package = await run_in_threadpool(package_from_zip, data)+ except SkillPackageError as exc:+ raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))+ parsed = parse_skill_markdown(package.skill_md)+ if parsed is None:+ raise HTTPException(+ status_code=400,+ detail=(+ "The archive's SKILL.md is not valid — expected YAML "+ "frontmatter with 'name' and 'description' followed by a "+ "markdown body."+ ),+ )+ stored = await _store_uploaded_skill(+ user_id, parsed, expert_id=expert_id, files=package.files+ )+ return CopilotSkillInfo(+ name=stored.name,+ description=stored.description,+ triggers=list(stored.triggers),+ )+++async def _store_uploaded_skill(+ user_id: str,+ parsed: ParsedSkill,+ *,+ expert_id: str | None,+ files: list[SkillFile] | None = None,+) -> ParsedSkill:+ """Persist a parsed upload, mapping each refusal to its status: 409 at the+ per-user cap, 413 over a package limit, 400 for anything malformed."""+ try:+ return await store_user_skill(+ user_id,+ expert_id=expert_id,+ name=parsed.name,+ description=parsed.description,+ body=parsed.body,+ triggers=list(parsed.triggers),+ version=parsed.version,+ extra=parsed.extra,+ files=files,+ )+ except SkillLimitError as exc:+ raise HTTPException(status_code=409, detail=str(exc))+ except (VirusDetectedError, VirusScanError) as exc:+ logger.warning(f"[skills] virus scan rejected an uploaded skill: {exc}")+ raise HTTPException(+ status_code=400, detail="Skill content rejected by virus scan"+ )+ except SkillPackageError as exc:+ raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc))+++async def _read_upload(file: UploadFile, max_bytes: int) -> bytes:+ """Read an upload with an early abort, so a body over the cap is refused+ without ever being held whole."""+ chunks: list[bytes] = []+ total = 0+ while chunk := await file.read(64 * 1024):+ total += len(chunk)+ if total > max_bytes:+ raise HTTPException(+ status_code=413,+ detail=f"Archive is larger than the {max_bytes}-byte limit",+ )+ chunks.append(chunk)+ return b"".join(chunks)+++ path="/{name}",+ summary="Read a single copilot skill with its full SKILL.md body",+ operation_id="readCopilotSkill",+ responses={404: {"description": "Skill not found"}},+)+async def read_copilot_skill(+ user_id: Annotated[str, Security(get_user_id)],+ name: str = Path(..., description="Slug of the skill to read"),+ expert_id: str | None = Query(+ default=None, description="Read this expert's own copy of the skill."+ ),+) -> CopilotSkillDetail:+ """Return full SKILL.md content (name, description, triggers, body)+ for the library UI's expand-to-view dialog.++ Built-in default skills are returned with ``is_default=True`` so the+ UI can hide destructive affordances; missing user skills return 404.+ """+ slug = name.strip().lower()+ try:+ default = get_default_skill_with_body(slug)+ except OSError:+ # Don't leak the on-disk path; operators trace via server logs.+ logger.exception("[skills] failed to load default skill body for %s", slug)+ raise HTTPException(+ status_code=500,+ detail="Failed to load default skill body",+ )+ if default is not None:+ return CopilotSkillDetail(+ name=default.name,+ description=default.description,+ triggers=list(default.triggers),+ body=default.body,+ is_default=True,+ )++ await _require_skill_owner(user_id, expert_id)+ parsed = await read_user_skill_with_body(user_id, slug, expert_id=expert_id)+ if parsed is None:+ raise HTTPException(+ status_code=HTTP_404_NOT_FOUND, detail=f"Skill '{slug}' not found"+ )+ prefix = f"{skill_folder(expert_id)}/{slug}/"+ return CopilotSkillDetail(+ name=parsed.name,+ description=parsed.description,+ triggers=list(parsed.triggers),+ body=parsed.body,+ version=parsed.version,+ is_default=False,+ files=[+ CopilotSkillFile(+ path=f.path.removeprefix(prefix),+ size_bytes=f.size_bytes,+ is_executable=f.is_executable,+ )+ for f in await list_user_skill_files(user_id, slug, expert_id=expert_id)+ ],+ )+++ path="/{name}/package",+ summary="Download a copilot skill as a zipped package",+ operation_id="downloadCopilotSkillPackage",+ response_class=Response,+ responses={+ 200: {+ "content": {+ "application/zip": {"schema": {"type": "string", "format": "binary"}}+ },+ "description": "The skill folder as a zip archive",+ },+ 404: {"description": "Skill not found"},+ },+)+async def download_copilot_skill_package(+ user_id: Annotated[str, Security(get_user_id)],+ name: str = Path(..., description="Slug of the skill to download"),+ expert_id: str | None = Query(+ default=None, description="Download this expert's own copy of the skill."+ ),+) -> Response:+ """Return the skill's whole folder as a zip — ``SKILL.md`` at the root,+ siblings at their relative paths, executable bits preserved — so a+ download re-uploads to the same tree.++ Built-in defaults are single on-disk files and are not downloadable here;+ GET ``/skills/{name}`` serves their body.+ """+ await _require_skill_owner(user_id, expert_id)+ slug = name.strip().lower()+ try:+ package = await read_user_skill_package(user_id, slug, expert_id=expert_id)+ except SkillPackageError as exc:+ raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))+ if package is None:+ raise HTTPException(+ status_code=HTTP_404_NOT_FOUND, detail=f"Skill '{slug}' not found"+ )+ return Response(+ content=await run_in_threadpool(zip_from_package, package),+ media_type="application/zip",+ headers={"Content-Disposition": f'attachment; filename="{slug}.zip"'},+ )+++ path="/{name}",+ summary="Delete a user-distilled copilot skill",+ operation_id="deleteCopilotSkill",+)+async def delete_copilot_skill(+ user_id: Annotated[str, Security(get_user_id)],+ name: str = Path(..., description="Slug of the skill to delete"),+ expert_id: str | None = Query(+ default=None, description="Delete this expert's own copy of the skill."+ ),+) -> dict[str, str]:+ """Delete a user-distilled skill by slug.++ Built-in defaults are not user-deletable — attempting to delete one+ returns 400. Missing skills return 404 so the UI can reconcile a+ stale list.+ """+ await _require_skill_owner(user_id, expert_id)+ try:+ slug = await delete_user_skill(user_id, name, expert_id=expert_id)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc))+ except BuiltInSkillError as exc:+ raise HTTPException(status_code=400, detail=str(exc))+ except SkillNotFoundError as exc:+ raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(exc))+ return {"name": slug}diff --git a/autogpt_platform/backend/backend/api/features/skills/routes_test.py b/autogpt_platform/backend/backend/api/features/skills/routes_test.pynew file mode 100644index 0000000000..fe0286edc8--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/skills/routes_test.py@@ -0,0 +1,692 @@+"""Tests for the copilot-skills routes."""++from unittest.mock import AsyncMock++import fastapi+import fastapi.testclient+import pytest+import pytest_mock+from fastapi.routing import APIRoute++from backend.api.features.skill_zip import package_from_zip, zip_from_package+from backend.api.features.skills.routes import router+from backend.api.features.store.exceptions import VirusDetectedError+from backend.api.rest_api import app as real_app+from backend.api.rest_api import handle_internal_http_error+from backend.copilot.tools.skills import (+ MAX_PACKAGE_FILES,+ BuiltInSkillError,+ ParsedSkill,+ SkillFile,+ SkillLimitError,+ SkillNotFoundError,+ SkillPackage,+ parse_skill_markdown,+ render_skill_markdown,+)++# _FakeWorkspaceManager/_patch_skills_path: the skills layer's own test owns the+# in-memory workspace these round-trip tests need; a second copy here would+# drift from the real manager's surface.+from backend.copilot.tools.skills_test import ( # noqa: E402+ _FakeWorkspaceManager,+ _patch_skills_path,+)+from backend.util.exceptions import ConflictError++app = fastapi.FastAPI()+app.include_router(router, prefix="/skills")+# ConflictError is mapped app-wide, never on the route, so without this a+# conflict reads here as an unhandled error rather than the 409 a client gets.+app.add_exception_handler(ConflictError, handle_internal_http_error(409))+client = fastapi.testclient.TestClient(app)++[email protected](autouse=True)+def setup_app_auth(mock_jwt_user):+ from autogpt_libs.auth.jwt_utils import get_jwt_payload++ app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]+ yield+ app.dependency_overrides.clear()+++EXPECTED_OPERATIONS = {+ ("get", "/api/skills"): "listCopilotSkills",+ ("post", "/api/skills"): "uploadCopilotSkill",+ ("post", "/api/skills/package"): "uploadCopilotSkillPackage",+ ("delete", "/api/skills/{name}"): "deleteCopilotSkill",+ ("get", "/api/skills/{name}"): "readCopilotSkill",+ ("get", "/api/skills/{name}/package"): "downloadCopilotSkillPackage",+}+++ "method,path,operation_id",+ [(m, p, oid) for (m, p), oid in EXPECTED_OPERATIONS.items()],+)+def test_skill_operation_is_published(method: str, path: str, operation_id: str):+ """The mounted surface is contract: the generated frontend client is built from it."""+ operation = real_app.openapi()["paths"][path][method]+ assert operation["operationId"] == operation_id+ assert operation["tags"] == ["v1", "skills"]+++def test_skill_surface_has_no_other_operations():+ published = {+ (method, path)+ for path, operations in real_app.openapi()["paths"].items()+ if path.startswith("/api/skills")+ for method in operations+ }+ assert published == set(EXPECTED_OPERATIONS)++[email protected]("path", sorted({p for _, p in EXPECTED_OPERATIONS}))+def test_skill_path_is_served_by_this_module(path: str):+ handlers = {+ route.endpoint.__module__+ for route in real_app.routes+ if isinstance(route, APIRoute) and route.path == path+ }+ assert handlers == {"backend.api.features.skills.routes"}+++def test_list_copilot_skills_returns_user_skills(+ mocker: pytest_mock.MockFixture,+) -> None:+ """GET /skills returns user-distilled skills (defaults are excluded+ because the UI hides them).+ """++ mocker.patch(+ "backend.api.features.skills.routes.list_user_skills",+ AsyncMock(+ return_value=[+ ParsedSkill(+ name="oauth_flow",+ description="OAuth handshake recipe",+ body="...",+ triggers=("auth", "oauth"),+ ),+ ParsedSkill(+ name="zzz_cleanup",+ description="Cleanup recipe",+ body="...",+ ),+ ]+ ),+ )++ response = client.get("/skills")+ assert response.status_code == 200+ body = response.json()+ assert [s["name"] for s in body] == ["oauth_flow", "zzz_cleanup"]+ assert body[0]["triggers"] == ["auth", "oauth"]+ assert body[1]["triggers"] == []+++def test_delete_copilot_skill_returns_name_on_success(+ mocker: pytest_mock.MockFixture,+) -> None:+ """DELETE /skills/{name} returns the slug and forwards the user_id."""+ delete_mock = AsyncMock(return_value="my_skill")+ mocker.patch("backend.api.features.skills.routes.delete_user_skill", delete_mock)++ response = client.delete("/skills/my_skill")+ assert response.status_code == 200+ assert response.json() == {"name": "my_skill"}+ delete_mock.assert_awaited_once()+++def test_delete_copilot_skill_rejects_builtin(+ mocker: pytest_mock.MockFixture,+) -> None:+ """Built-in defaults must not be user-deletable via the REST endpoint."""++ mocker.patch(+ "backend.api.features.skills.routes.delete_user_skill",+ AsyncMock(side_effect=BuiltInSkillError("built-in")),+ )++ response = client.delete("/skills/agent_building_guide")+ assert response.status_code == 400+++def test_delete_copilot_skill_returns_404_when_missing(+ mocker: pytest_mock.MockFixture,+) -> None:+ """Missing skills surface as 404 so the UI can reconcile its list."""++ mocker.patch(+ "backend.api.features.skills.routes.delete_user_skill",+ AsyncMock(side_effect=SkillNotFoundError("gone")),+ )++ response = client.delete("/skills/missing")+ assert response.status_code == 404+++def test_read_copilot_skill_returns_user_body(+ mocker: pytest_mock.MockFixture,+) -> None:+ """GET /skills/{name} returns the full SKILL.md body for a user skill."""++ mocker.patch(+ "backend.api.features.skills.routes.read_user_skill_with_body",+ AsyncMock(+ return_value=ParsedSkill(+ name="oauth_flow",+ description="OAuth handshake recipe",+ body="# OAuth flow\n\nStep 1: ...",+ triggers=("auth",),+ version="1",+ )+ ),+ )++ response = client.get("/skills/oauth_flow")+ assert response.status_code == 200+ body = response.json()+ assert body["name"] == "oauth_flow"+ assert body["body"].startswith("# OAuth flow")+ assert body["triggers"] == ["auth"]+ assert body["version"] == "1"+ assert body["is_default"] is False+++def test_read_copilot_skill_returns_default_with_body(+ mocker: pytest_mock.MockFixture,+) -> None:+ """A built-in default name returns is_default=True and a non-empty body."""++ mocker.patch(+ "backend.api.features.skills.routes.get_default_skill_with_body",+ return_value=ParsedSkill(+ name="agent_building_guide",+ description="default desc",+ body="# Default body\n",+ triggers=("create_agent",),+ ),+ )++ response = client.get("/skills/agent_building_guide")+ assert response.status_code == 200+ body = response.json()+ assert body["name"] == "agent_building_guide"+ assert body["is_default"] is True+ assert body["body"].startswith("# Default body")+++def test_read_copilot_skill_returns_404_when_missing(+ mocker: pytest_mock.MockFixture,+) -> None:+ """A user-skill slug that has no SKILL.md surfaces as 404."""+ mocker.patch(+ "backend.api.features.skills.routes.read_user_skill_with_body",+ AsyncMock(return_value=None),+ )++ response = client.get("/skills/missing")+ assert response.status_code == 404+++_VALID_SKILL_MD = (+ "---\n"+ "name: oauth_flow\n"+ "description: OAuth handshake recipe\n"+ "triggers:\n"+ " - auth\n"+ "---\n\n"+ "# OAuth flow\n\nStep 1: redirect to /authorize\n"+)++# Storing a skill re-renders its frontmatter, so the canonical form is what a+# download hands back and what an upload -> download round trip can compare.+_PARSED_VALID_SKILL = parse_skill_markdown(_VALID_SKILL_MD)+assert _PARSED_VALID_SKILL is not None+_CANONICAL_SKILL_MD = render_skill_markdown(_PARSED_VALID_SKILL)+++def test_upload_copilot_skill_creates_skill(+ mocker: pytest_mock.MockFixture,+) -> None:+ """POST /skills parses the SKILL.md and persists it via store_user_skill."""+ store_mock = AsyncMock(+ return_value=ParsedSkill(+ name="oauth_flow",+ description="OAuth handshake recipe",+ body="# OAuth flow",+ triggers=("auth",),+ )+ )+ mocker.patch("backend.api.features.skills.routes.store_user_skill", store_mock)++ response = client.post("/skills", json={"content": _VALID_SKILL_MD})+ assert response.status_code == 201+ body = response.json()+ assert body["name"] == "oauth_flow"+ assert body["triggers"] == ["auth"]+ store_mock.assert_awaited_once()+ assert store_mock.await_args.kwargs["name"] == "oauth_flow"+++def test_upload_copilot_skill_rejects_malformed_markdown() -> None:+ """A file without valid frontmatter returns 400 before touching storage."""+ response = client.post(+ "/skills", json={"content": "just some text, no frontmatter"}+ )+ assert response.status_code == 400+++def test_upload_copilot_skill_returns_409_when_at_cap(+ mocker: pytest_mock.MockFixture,+) -> None:+ """The per-user cap surfaces as 409 so the UI can prompt a delete."""+ mocker.patch(+ "backend.api.features.skills.routes.store_user_skill",+ AsyncMock(side_effect=SkillLimitError("Skill limit reached (50).")),+ )++ response = client.post("/skills", json={"content": _VALID_SKILL_MD})+ assert response.status_code == 409+++def test_upload_copilot_skill_returns_400_on_validation_error(+ mocker: pytest_mock.MockFixture,+) -> None:+ """A validation failure from store_user_skill maps to 400."""+ mocker.patch(+ "backend.api.features.skills.routes.store_user_skill",+ AsyncMock(side_effect=ValueError("name must be a slug")),+ )++ response = client.post("/skills", json={"content": _VALID_SKILL_MD})+ assert response.status_code == 400+++def test_upload_copilot_skill_returns_400_on_virus_detection(+ mocker: pytest_mock.MockFixture,+) -> None:+ """A virus-scan rejection surfaces as a 400 client error, not a 500."""+ mocker.patch(+ "backend.api.features.skills.routes.store_user_skill",+ AsyncMock(side_effect=VirusDetectedError("nasty")),+ )++ response = client.post("/skills", json={"content": _VALID_SKILL_MD})+ assert response.status_code == 400+ assert "virus scan" in response.json()["detail"]+++_EXPERT_SKILL_ROUTES = {+ "list": lambda expert_id: client.get("/skills", params=_owner(expert_id)),+ "upload": lambda expert_id: client.post(+ "/skills", params=_owner(expert_id), json={"content": _VALID_SKILL_MD}+ ),+ "read": lambda expert_id: client.get(+ "/skills/oauth_flow", params=_owner(expert_id)+ ),+ "upload_package": lambda expert_id: client.post(+ "/skills/package",+ params=_owner(expert_id),+ files={"file": ("pkg.zip", _package_zip(), "application/zip")},+ ),+ "download_package": lambda expert_id: client.get(+ "/skills/oauth_flow/package", params=_owner(expert_id)+ ),+ "delete": lambda expert_id: client.delete(+ "/skills/oauth_flow", params=_owner(expert_id)+ ),+}++_SKILL_LAYER = {+ "list_user_skills": [],+ "store_user_skill": ParsedSkill(name="oauth_flow", description="d", body="b"),+ "read_user_skill_with_body": ParsedSkill(+ name="oauth_flow", description="d", body="b"+ ),+ "delete_user_skill": "oauth_flow",+ "read_user_skill_package": SkillPackage(skill_md=_CANONICAL_SKILL_MD),+}++[email protected]("route", list(_EXPERT_SKILL_ROUTES))+def test_expert_skill_routes_refuse_an_expert_the_caller_does_not_own(+ route: str,+ mocker: pytest_mock.MockFixture,+ test_user_id: str,+) -> None:+ """Every /skills route naming an expert goes through the same owner gate,+ and a refusal is a 404 that never reaches the skills layer."""+ owns = mocker.patch(+ "backend.api.features.skills.routes.experts_db.owns_private_active_expert",+ AsyncMock(return_value=False),+ )+ downstream = {+ name: mocker.patch(f"backend.api.features.skills.routes.{name}", AsyncMock())+ for name in _SKILL_LAYER+ }++ response = _EXPERT_SKILL_ROUTES[route]("expert-not-mine")++ assert response.status_code == 404+ assert response.json()["detail"] == "Expert 'expert-not-mine' not found"+ owns.assert_awaited_once_with(test_user_id, "expert-not-mine")+ for mock in downstream.values():+ mock.assert_not_awaited()+++ "route,downstream",+ [+ ("list", "list_user_skills"),+ ("upload", "store_user_skill"),+ ("read", "read_user_skill_with_body"),+ ("delete", "delete_user_skill"),+ ],+)+def test_expert_skill_routes_forward_an_owned_expert(+ route: str,+ downstream: str,+ mocker: pytest_mock.MockFixture,+) -> None:+ """An owned PRIVATE expert passes the gate and its id reaches the skills+ layer, so the call reads that expert's folder and not Otto's."""+ mocker.patch(+ "backend.api.features.skills.routes.experts_db.owns_private_active_expert",+ AsyncMock(return_value=True),+ )+ mocker.patch(+ "backend.api.features.skills.routes.list_user_skill_files",+ AsyncMock(return_value=[]),+ )+ target = mocker.patch(+ f"backend.api.features.skills.routes.{downstream}",+ AsyncMock(return_value=_SKILL_LAYER[downstream]),+ )++ response = _EXPERT_SKILL_ROUTES[route]("expert-mine")++ assert response.status_code in (200, 201)+ assert _forwarded_expert_id(target) == "expert-mine"++[email protected]("route", list(_EXPERT_SKILL_ROUTES))+def test_personal_autopilot_skill_routes_skip_the_expert_gate(+ route: str,+ mocker: pytest_mock.MockFixture,+) -> None:+ """No expert named means personal Otto, which owns its own folder and+ must never be refused by the expert gate."""+ owns = mocker.patch(+ "backend.api.features.skills.routes.experts_db.owns_private_active_expert",+ AsyncMock(return_value=False),+ )+ mocker.patch(+ "backend.api.features.skills.routes.list_user_skill_files",+ AsyncMock(return_value=[]),+ )+ for name, value in _SKILL_LAYER.items():+ mocker.patch(+ f"backend.api.features.skills.routes.{name}", AsyncMock(return_value=value)+ )++ response = _EXPERT_SKILL_ROUTES[route](None)++ assert response.status_code in (200, 201)+ owns.assert_not_awaited()+++# ``expert_id`` omitted entirely, not sent empty: an empty query value is a+# str, which would take the expert branch of the gate rather than the+# personal-Otto one.+def _owner(expert_id: str | None) -> dict[str, str]:+ return {} if expert_id is None else {"expert_id": expert_id}+++def _forwarded_expert_id(mock: AsyncMock) -> str | None:+ """``list_user_skills`` takes the owner positionally, the rest by keyword."""+ call = mock.await_args+ if call is None:+ return None+ if "expert_id" in call.kwargs:+ return call.kwargs["expert_id"]+ return call.args[1] if len(call.args) > 1 else None+++def test_upload_copilot_skill_keeps_frontmatter_the_platform_ignores(+ mocker: pytest_mock.MockFixture,+) -> None:+ """An uploaded package's licence line is the author's, not ours — storing+ the file without it rewrites what they uploaded."""+ store_mock = AsyncMock(+ return_value=ParsedSkill(name="oauth_flow", description="d", body="b")+ )+ mocker.patch("backend.api.features.skills.routes.store_user_skill", store_mock)++ response = client.post(+ "/skills",+ json={"content": _VALID_SKILL_MD.replace("---\n\n", "license: MIT\n---\n\n")},+ )+ assert response.status_code == 201+ assert store_mock.await_args.kwargs["extra"] == {"license": "MIT"}+++def _package_zip(files: list[SkillFile] | None = None) -> bytes:+ """A valid package archive: the canonical SKILL.md plus *files*."""+ return zip_from_package(+ SkillPackage(skill_md=_CANONICAL_SKILL_MD, files=files or [])+ )+++_PACKAGE_FILES = [+ SkillFile(relative_path="references/providers.md", content=b"# Providers\n"),+ SkillFile(relative_path="references/errors.md", content=b"# Errors\n"),+ SkillFile(relative_path="assets/logo.svg", content=b"<svg/>"),+ SkillFile(relative_path="assets/nested/icon.svg", content=b"<svg id=1/>"),+ SkillFile(+ relative_path="scripts/exchange_code.py",+ content=b"#!/usr/bin/env python3\nprint(1)\n",+ is_executable=True,+ ),+ SkillFile(relative_path="scripts/helpers.py", content=b"X = 1\n"),+]+++def test_skill_package_round_trips_through_upload_and_download() -> None:+ """The tree a user uploads is the tree they get back — same paths, same+ bytes, and a script that arrived executable is still executable."""+ with _patch_skills_path(_FakeWorkspaceManager()):+ created = client.post(+ "/skills/package",+ files={+ "file": ("pkg.zip", _package_zip(_PACKAGE_FILES), "application/zip")+ },+ )+ assert created.status_code == 201, created.text+ assert created.json()["name"] == "oauth_flow"++ assert [s["name"] for s in client.get("/skills").json()] == ["oauth_flow"]++ detail = client.get("/skills/oauth_flow")+ assert detail.status_code == 200+ assert {f["path"]: f["is_executable"] for f in detail.json()["files"]} == {+ f.relative_path: f.is_executable for f in _PACKAGE_FILES+ }++ downloaded = client.get("/skills/oauth_flow/package")++ assert downloaded.status_code == 200+ assert downloaded.headers["content-type"] == "application/zip"+ assert "oauth_flow.zip" in downloaded.headers["content-disposition"]+ restored = package_from_zip(downloaded.content)+ assert restored.skill_md == _CANONICAL_SKILL_MD+ assert sorted(+ (f.relative_path, f.content, f.is_executable) for f in restored.files+ ) == sorted((f.relative_path, f.content, f.is_executable) for f in _PACKAGE_FILES)+++def test_upload_package_over_the_files_cap_is_413_and_writes_nothing() -> None:+ manager = _FakeWorkspaceManager()+ oversized = [+ SkillFile(relative_path=f"f{i}.txt", content=b"x")+ for i in range(MAX_PACKAGE_FILES + 1)+ ]++ with _patch_skills_path(manager):+ response = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", _package_zip(oversized), "application/zip")},+ )++ assert response.status_code == 413+ detail = response.json()["detail"]+ assert "files" in detail and str(MAX_PACKAGE_FILES) in detail+ assert manager.files == {}+++def test_upload_package_with_an_escaping_member_is_400_and_writes_nothing() -> None:+ """Zip slip is refused before the first write, so a rejected archive+ leaves the workspace exactly as it was."""+ manager = _FakeWorkspaceManager()+ escaping = [SkillFile(relative_path="../../etc/passwd", content=b"x")]++ with _patch_skills_path(manager):+ response = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", _package_zip(escaping), "application/zip")},+ )++ assert response.status_code == 400+ assert manager.files == {}+++def test_upload_package_refuses_an_oversized_body_before_parsing(+ mocker: pytest_mock.MockFixture,+) -> None:+ """The body cap aborts mid-read, so a 20 MiB archive never lands in+ memory whole."""+ mocker.patch("backend.api.features.skills.routes.MAX_ZIP_BYTES", 64)+ parse = mocker.patch("backend.api.features.skills.routes.package_from_zip")++ response = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", b"x" * 4096, "application/zip")},+ )++ assert response.status_code == 413+ parse.assert_not_called()+++def test_upload_package_rejects_an_archive_whose_skill_md_is_malformed() -> None:+ response = client.post(+ "/skills/package",+ files={+ "file": (+ "pkg.zip",+ zip_from_package(SkillPackage(skill_md="no frontmatter here")),+ "application/zip",+ )+ },+ )+ assert response.status_code == 400+++def test_upload_package_rejects_something_that_is_not_a_zip() -> None:+ response = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", _VALID_SKILL_MD.encode(), "application/zip")},+ )+ assert response.status_code == 400+++def test_download_refuses_rather_than_serving_a_package_it_cannot_read_whole() -> None:+ """A download is a backup: a sibling that will not read must fail the+ request, not quietly produce an archive with the file missing."""++ class _OneUnreadableSibling(_FakeWorkspaceManager):+ async def read_file(self, path: str) -> bytes:+ if path.endswith("references/providers.md"):+ raise RuntimeError("storage unavailable")+ return await super().read_file(path)++ with _patch_skills_path(_OneUnreadableSibling()):+ created = client.post(+ "/skills/package",+ files={+ "file": ("pkg.zip", _package_zip(_PACKAGE_FILES), "application/zip")+ },+ )+ assert created.status_code == 201, created.text++ with pytest.raises(RuntimeError, match="storage unavailable"):+ client.get("/skills/oauth_flow/package")+++def test_download_does_not_report_a_storage_failure_as_a_missing_skill() -> None:+ """404 means the user has no such skill. A storage failure reading the+ SKILL.md is ours, and saying "not found" would send them looking for a+ skill they still have."""++ class _UnreadableRoot(_FakeWorkspaceManager):+ async def read_file(self, path: str) -> bytes:+ if path.endswith("/SKILL.md"):+ raise RuntimeError("storage unavailable")+ return await super().read_file(path)++ with _patch_skills_path(_UnreadableRoot()):+ created = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", _package_zip(), "application/zip")},+ )+ assert created.status_code == 201, created.text++ with pytest.raises(RuntimeError, match="storage unavailable"):+ client.get("/skills/oauth_flow/package")+++def test_download_refuses_a_stored_package_over_the_files_cap() -> None:+ """Only a legacy or hand-made folder can be over the cap, and truncating it+ to the cap would hand back an archive that silently is not the skill."""+ manager = _FakeWorkspaceManager()+ with _patch_skills_path(manager):+ created = client.post(+ "/skills/package",+ files={"file": ("pkg.zip", _package_zip(), "application/zip")},+ )+ assert created.status_code == 201, created.text+ # Seed the folder past the cap the way only a pre-cap write could have.+ for i in range(MAX_PACKAGE_FILES + 1):+ manager.files[f"/skills/oauth_flow/f{i}.txt"] = b"x"++ response = client.get("/skills/oauth_flow/package")++ assert response.status_code == 413+ assert str(MAX_PACKAGE_FILES) in response.json()["detail"]+++def test_download_of_a_package_being_rewritten_is_409_not_a_bad_request(+ mocker: pytest_mock.MockFixture,+) -> None:+ """A tree that will not hold still is a conflict the caller can retry, not+ a malformed package: 400 or 413 would blame the user for their own+ concurrent edit."""+ mocker.patch(+ "backend.api.features.skills.routes.read_user_skill_package",+ AsyncMock(side_effect=ConflictError("changed while it was read")),+ )++ response = client.get("/skills/oauth_flow/package")++ assert response.status_code == 409, response.text+++def test_download_package_returns_404_for_a_missing_skill(+ mocker: pytest_mock.MockFixture,+) -> None:+ mocker.patch(+ "backend.api.features.skills.routes.read_user_skill_package",+ AsyncMock(return_value=None),+ )+ assert client.get("/skills/missing/package").status_code == 404diff --git a/autogpt_platform/backend/backend/api/features/v1.py b/autogpt_platform/backend/backend/api/features/v1.pyindex 22b82fc5d8..f1062108a2 100644--- a/autogpt_platform/backend/backend/api/features/v1.py+++ b/autogpt_platform/backend/backend/api/features/v1.py@@ -44,12 +44,6 @@ from backend.api.features.executions.activity_gate import (hide_activity_summary_if_disabled,)from backend.api.features.experts import experts_db-from backend.api.features.skill_zip import (- MAX_ZIP_BYTES,- package_from_zip,- zip_from_package,-)-from backend.api.features.store.exceptions import VirusDetectedError, VirusScanErrorfrom backend.api.features.workspace.routes import create_file_download_responsefrom backend.api.model import (CreateGraph,@@ -62,23 +56,6 @@ from backend.api.model import ()from backend.blocks import get_block, get_blocksfrom backend.copilot.rate_limit import enforce_payment_paywall, get_tier_multipliers-from backend.copilot.tools.skills import (- BuiltInSkillError,- ParsedSkill,- SkillFile,- SkillLimitError,- SkillNotFoundError,- SkillPackageError,- delete_user_skill,- get_default_skill_with_body,- list_user_skill_files,- list_user_skills,- parse_skill_markdown,- read_user_skill_package,- read_user_skill_with_body,- skill_folder,- store_user_skill,-)from backend.data import execution as execution_dbfrom backend.data import graph as graph_dbfrom backend.data.block import BlockInput, CompletedBlockOutput@@ -2739,392 +2716,3 @@ async def delete_graph_execution_schedule(detail=f"Schedule #{schedule_id} not found",)return {"id": schedule_id}---########################################################-##################### COPILOT SKILLS #####################-########################################################---class CopilotSkillInfo(BaseModel):- """User-distilled copilot skill metadata for the library UI.-- Defaults (built-in agent-building / MCP-tool guides) are intentionally- excluded — they cannot be edited or deleted, so surfacing them in the- user-facing list would add noise without affordances.- """-- name: str- description: str- triggers: list[str] = []---class CopilotSkillFile(BaseModel):- """One file of a skill package, by its path relative to the skill folder- (``scripts/run.py``) — the path the SKILL.md body references it by."""-- path: str- size_bytes: int- is_executable: bool = False---class CopilotSkillDetail(BaseModel):- """Full SKILL.md content surfaced to the library expand-to-view UI."""-- name: str- description: str- triggers: list[str] = []- body: str- version: str | None = None- is_default: bool = False- # The package's other files (references/, scripts/, assets/). Empty for- # built-in defaults, which ship as one on-disk markdown file.- files: list[CopilotSkillFile] = []---class UploadCopilotSkillRequest(BaseModel):- """Body for the library UI's "upload skill" action.-- Carries the raw ``SKILL.md`` text (YAML frontmatter + markdown body) the- user picked from disk; the server parses + validates it so the upload and- the copilot's ``store_skill`` tool share one source of truth.- """-- content: str---async def _require_skill_owner(user_id: str, expert_id: str | None) -> None:- """A skill owner named on a REST call must be one of the caller's active- experts; personal Otto (``None``) needs no check."""- if expert_id is None:- return- if not await experts_db.owns_private_active_expert(user_id, expert_id):- raise HTTPException(- status_code=HTTP_404_NOT_FOUND, detail=f"Expert '{expert_id}' not found"- )---@v1_router.get(- path="/skills",- summary="List user-distilled copilot skills",- operation_id="listCopilotSkills",- tags=["skills"],- dependencies=[Security(requires_user)],-)-async def list_copilot_skills(- user_id: Annotated[str, Security(get_user_id)],- expert_id: str | None = Query(- default=None,- description="List this expert's own skills instead of personal Otto's.",- ),-) -> list[CopilotSkillInfo]:- """Return the skills owned by personal Otto, or by one expert.-- Reuses :func:`backend.copilot.tools.skills.list_user_skills` so the- library UI sees the exact same set the copilot ``<available_skills>``- block surfaces, minus the built-in defaults (which are read-only and- handled separately by the copilot runtime).- """- await _require_skill_owner(user_id, expert_id)- skills = await list_user_skills(user_id, expert_id)- return [- CopilotSkillInfo(- name=s.name,- description=s.description,- triggers=list(s.triggers),- )- for s in skills- ]---@v1_router.post(- path="/skills",- summary="Upload a copilot skill from a SKILL.md file",- operation_id="uploadCopilotSkill",- tags=["skills"],- status_code=201,- responses={- 400: {"description": "Malformed SKILL.md or validation error"},- 409: {"description": "Per-user skill limit reached"},- },- dependencies=[Security(requires_user)],-)-async def upload_copilot_skill(- user_id: Annotated[str, Security(get_user_id)],- body: UploadCopilotSkillRequest,- expert_id: str | None = Query(- default=None, description="Store the skill as this expert's own."- ),-) -> CopilotSkillInfo:- """Create a user-distilled skill from an uploaded ``SKILL.md`` file.-- Parses the canonical frontmatter + body, then reuses- :func:`backend.copilot.tools.skills.store_user_skill` so an uploaded skill- is validated, capped, and persisted exactly like one the copilot distils- via ``store_skill``. Malformed files return 400, the per-user cap returns- 409, and an existing slug is overwritten (upsert).- """- await _require_skill_owner(user_id, expert_id)- parsed = parse_skill_markdown(body.content)- if parsed is None:- raise HTTPException(- status_code=400,- detail=(- "File is not a valid SKILL.md — expected YAML frontmatter with "- "'name' and 'description' followed by a markdown body."- ),- )- stored = await _store_uploaded_skill(user_id, parsed, expert_id=expert_id)- return CopilotSkillInfo(- name=stored.name,- description=stored.description,- triggers=list(stored.triggers),- )---@v1_router.post(- path="/skills/package",- summary="Upload a copilot skill as a zipped package",- operation_id="uploadCopilotSkillPackage",- tags=["skills"],- status_code=201,- responses={- 400: {"description": "Unreadable archive, or a malformed SKILL.md or path"},- 409: {"description": "Per-user skill limit reached"},- 413: {"description": "Archive, file or file count over the package limit"},- },- dependencies=[Security(requires_user)],-)-async def upload_copilot_skill_package(- user_id: Annotated[str, Security(get_user_id)],- file: UploadFile,- expert_id: str | None = Query(- default=None, description="Store the skill as this expert's own."- ),-) -> CopilotSkillInfo:- """Create a skill from a zipped package — a root ``SKILL.md`` plus the- files beside it.-- The zip is transport only: it is unpacked here and stored as the skill's- folder, so the model reaches ``scripts/`` and ``references/`` by path- exactly as it does for a package copied from another skill.- """- await _require_skill_owner(user_id, expert_id)- data = await _read_upload(file, MAX_ZIP_BYTES)- try:- package = await run_in_threadpool(package_from_zip, data)- except SkillPackageError as exc:- raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))- parsed = parse_skill_markdown(package.skill_md)- if parsed is None:- raise HTTPException(- status_code=400,- detail=(- "The archive's SKILL.md is not valid — expected YAML "- "frontmatter with 'name' and 'description' followed by a "- "markdown body."- ),- )- stored = await _store_uploaded_skill(- user_id, parsed, expert_id=expert_id, files=package.files- )- return CopilotSkillInfo(- name=stored.name,- description=stored.description,- triggers=list(stored.triggers),- )---async def _store_uploaded_skill(- user_id: str,- parsed: ParsedSkill,- *,- expert_id: str | None,- files: list[SkillFile] | None = None,-) -> ParsedSkill:- """Persist a parsed upload, mapping each refusal to its status: 409 at the- per-user cap, 413 over a package limit, 400 for anything malformed."""- try:- return await store_user_skill(- user_id,- expert_id=expert_id,- name=parsed.name,- description=parsed.description,- body=parsed.body,- triggers=list(parsed.triggers),- version=parsed.version,- extra=parsed.extra,- files=files,- )- except SkillLimitError as exc:- raise HTTPException(status_code=409, detail=str(exc))- except (VirusDetectedError, VirusScanError) as exc:- logger.warning(f"[skills] virus scan rejected an uploaded skill: {exc}")- raise HTTPException(- status_code=400, detail="Skill content rejected by virus scan"- )- except SkillPackageError as exc:- raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))---async def _read_upload(file: UploadFile, max_bytes: int) -> bytes:- """Read an upload with an early abort, so a body over the cap is refused- without ever being held whole."""- chunks: list[bytes] = []- total = 0- while chunk := await file.read(64 * 1024):- total += len(chunk)- if total > max_bytes:- raise HTTPException(- status_code=413,- detail=f"Archive is larger than the {max_bytes}-byte limit",- )- chunks.append(chunk)- return b"".join(chunks)---@v1_router.get(- path="/skills/{name}",- summary="Read a single copilot skill with its full SKILL.md body",- operation_id="readCopilotSkill",- tags=["skills"],- responses={404: {"description": "Skill not found"}},- dependencies=[Security(requires_user)],-)-async def read_copilot_skill(- user_id: Annotated[str, Security(get_user_id)],- name: str = Path(..., description="Slug of the skill to read"),- expert_id: str | None = Query(- default=None, description="Read this expert's own copy of the skill."- ),-) -> CopilotSkillDetail:- """Return full SKILL.md content (name, description, triggers, body)- for the library UI's expand-to-view dialog.-- Built-in default skills are returned with ``is_default=True`` so the- UI can hide destructive affordances; missing user skills return 404.- """- slug = name.strip().lower()- try:- default = get_default_skill_with_body(slug)- except OSError:- # Don't leak the on-disk path; operators trace via server logs.- logger.exception("[skills] failed to load default skill body for %s", slug)- raise HTTPException(- status_code=500,- detail="Failed to load default skill body",- )- if default is not None:- return CopilotSkillDetail(- name=default.name,- description=default.description,- triggers=list(default.triggers),- body=default.body,- is_default=True,- )-- await _require_skill_owner(user_id, expert_id)- parsed = await read_user_skill_with_body(user_id, slug, expert_id=expert_id)- if parsed is None:- raise HTTPException(- status_code=HTTP_404_NOT_FOUND, detail=f"Skill '{slug}' not found"- )- prefix = f"{skill_folder(expert_id)}/{slug}/"- return CopilotSkillDetail(- name=parsed.name,- description=parsed.description,- triggers=list(parsed.triggers),- body=parsed.body,- version=parsed.version,- is_default=False,- files=[- CopilotSkillFile(- path=f.path.removeprefix(prefix),- size_bytes=f.size_bytes,- is_executable=f.is_executable,- )- for f in await list_user_skill_files(user_id, slug, expert_id=expert_id)- ],- )---@v1_router.get(- path="/skills/{name}/package",- summary="Download a copilot skill as a zipped package",- operation_id="downloadCopilotSkillPackage",- tags=["skills"],- response_class=Response,- responses={- 200: {- "content": {- "application/zip": {"schema": {"type": "string", "format": "binary"}}- },- "description": "The skill folder as a zip archive",- },- 404: {"description": "Skill not found"},- },- dependencies=[Security(requires_user)],-)-async def download_copilot_skill_package(- user_id: Annotated[str, Security(get_user_id)],- name: str = Path(..., description="Slug of the skill to download"),- expert_id: str | None = Query(- default=None, description="Download this expert's own copy of the skill."- ),-) -> Response:- """Return the skill's whole folder as a zip — ``SKILL.md`` at the root,- siblings at their relative paths, executable bits preserved — so a- download re-uploads to the same tree.-- Built-in defaults are single on-disk files and are not downloadable here;- GET ``/skills/{name}`` serves their body.- """- await _require_skill_owner(user_id, expert_id)- slug = name.strip().lower()- try:- package = await read_user_skill_package(user_id, slug, expert_id=expert_id)- except SkillPackageError as exc:- raise HTTPException(status_code=413 if exc.over_limit else 400, detail=str(exc))- if package is None:- raise HTTPException(- status_code=HTTP_404_NOT_FOUND, detail=f"Skill '{slug}' not found"- )- return Response(- content=await run_in_threadpool(zip_from_package, package),- media_type="application/zip",- headers={"Content-Disposition": f'attachment; filename="{slug}.zip"'},- )---@v1_router.delete(- path="/skills/{name}",- summary="Delete a user-distilled copilot skill",- operation_id="deleteCopilotSkill",- tags=["skills"],- dependencies=[Security(requires_user)],-)-async def delete_copilot_skill(- user_id: Annotated[str, Security(get_user_id)],- name: str = Path(..., description="Slug of the skill to delete"),- expert_id: str | None = Query(- default=None, description="Delete this expert's own copy of the skill."- ),-) -> dict[str, str]:- """Delete a user-distilled skill by slug.-- Built-in defaults are not user-deletable — attempting to delete one- returns 400. Missing skills return 404 so the UI can reconcile a- stale list.- """- await _require_skill_owner(user_id, expert_id)- try:- slug = await delete_user_skill(user_id, name, expert_id=expert_id)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- except BuiltInSkillError as exc:- raise HTTPException(status_code=400, detail=str(exc))- except SkillNotFoundError as exc:- raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=str(exc))- return {"name": slug}diff --git a/autogpt_platform/backend/backend/api/features/v1_test.py b/autogpt_platform/backend/backend/api/features/v1_test.pyindex 6a96875f3c..92bde90c58 100644--- a/autogpt_platform/backend/backend/api/features/v1_test.py+++ b/autogpt_platform/backend/backend/api/features/v1_test.py@@ -12,24 +12,7 @@ from autogpt_libs.auth.models import RequestContextfrom fastapi import HTTPException, UploadFilefrom pytest_snapshot.plugin import Snapshot-from backend.api.features.skill_zip import package_from_zip, zip_from_package-from backend.api.features.store.exceptions import VirusDetectedErrorfrom backend.api.rest_api import handle_internal_http_error-from backend.copilot.tools.skills import (- MAX_PACKAGE_FILES,- BuiltInSkillError,- ParsedSkill,- SkillFile,- SkillLimitError,- SkillNotFoundError,- SkillPackage,- parse_skill_markdown,- render_skill_markdown,-)--# The skills layer's own test owns the in-memory workspace these round-trip-# tests need; a second copy here would drift from the real manager's surface.-from backend.copilot.tools.skills_test import _FakeWorkspaceManager, _patch_skills_pathfrom backend.data.credit import AutoTopUpConfigfrom backend.data.graph import GraphModelfrom backend.integrations.webhooks.graph_lifecycle_hooks import GraphActivationError@@ -1413,605 +1396,3 @@ def test_list_copilot_turn_schedules_filters_to_copilot_kind(mock_client.get_execution_schedules.assert_awaited_once_with(user_id=test_user_id, kind="copilot_turn")---def test_list_copilot_skills_returns_user_skills(- mocker: pytest_mock.MockFixture,- test_user_id: str,-) -> None:- """GET /skills returns user-distilled skills (defaults are excluded- because the UI hides them).- """-- mocker.patch(- "backend.api.features.v1.list_user_skills",- AsyncMock(- return_value=[- ParsedSkill(- name="oauth_flow",- description="OAuth handshake recipe",- body="...",- triggers=("auth", "oauth"),- ),- ParsedSkill(- name="zzz_cleanup",- description="Cleanup recipe",- body="...",- ),- ]- ),- )-- response = client.get("/skills")- assert response.status_code == 200- body = response.json()- assert [s["name"] for s in body] == ["oauth_flow", "zzz_cleanup"]- assert body[0]["triggers"] == ["auth", "oauth"]- assert body[1]["triggers"] == []---def test_delete_copilot_skill_returns_name_on_success(- mocker: pytest_mock.MockFixture,-) -> None:- """DELETE /skills/{name} returns the slug and forwards the user_id."""- delete_mock = AsyncMock(return_value="my_skill")- mocker.patch("backend.api.features.v1.delete_user_skill", delete_mock)-- response = client.delete("/skills/my_skill")- assert response.status_code == 200- assert response.json() == {"name": "my_skill"}- delete_mock.assert_awaited_once()---def test_delete_copilot_skill_rejects_builtin(- mocker: pytest_mock.MockFixture,-) -> None:- """Built-in defaults must not be user-deletable via the REST endpoint."""-- mocker.patch(- "backend.api.features.v1.delete_user_skill",- AsyncMock(side_effect=BuiltInSkillError("built-in")),- )-- response = client.delete("/skills/agent_building_guide")- assert response.status_code == 400---def test_delete_copilot_skill_returns_404_when_missing(- mocker: pytest_mock.MockFixture,-) -> None:- """Missing skills surface as 404 so the UI can reconcile its list."""-- mocker.patch(- "backend.api.features.v1.delete_user_skill",- AsyncMock(side_effect=SkillNotFoundError("gone")),- )-- response = client.delete("/skills/missing")- assert response.status_code == 404---def test_read_copilot_skill_returns_user_body(- mocker: pytest_mock.MockFixture,-) -> None:- """GET /skills/{name} returns the full SKILL.md body for a user skill."""-- mocker.patch(- "backend.api.features.v1.read_user_skill_with_body",- AsyncMock(- return_value=ParsedSkill(- name="oauth_flow",- description="OAuth handshake recipe",- body="# OAuth flow\n\nStep 1: ...",- triggers=("auth",),- version="1",- )- ),- )-- response = client.get("/skills/oauth_flow")- assert response.status_code == 200- body = response.json()- assert body["name"] == "oauth_flow"- assert body["body"].startswith("# OAuth flow")- assert body["triggers"] == ["auth"]- assert body["version"] == "1"- assert body["is_default"] is False---def test_read_copilot_skill_returns_default_with_body(- mocker: pytest_mock.MockFixture,-) -> None:- """A built-in default name returns is_default=True and a non-empty body."""-- mocker.patch(- "backend.api.features.v1.get_default_skill_with_body",- return_value=ParsedSkill(- name="agent_building_guide",- description="default desc",- body="# Default body\n",- triggers=("create_agent",),- ),- )-- response = client.get("/skills/agent_building_guide")- assert response.status_code == 200- body = response.json()- assert body["name"] == "agent_building_guide"- assert body["is_default"] is True- assert body["body"].startswith("# Default body")---def test_read_copilot_skill_returns_404_when_missing(- mocker: pytest_mock.MockFixture,-) -> None:- """A user-skill slug that has no SKILL.md surfaces as 404."""- mocker.patch(- "backend.api.features.v1.read_user_skill_with_body",- AsyncMock(return_value=None),- )-- response = client.get("/skills/missing")- assert response.status_code == 404---_VALID_SKILL_MD = (- "---\n"- "name: oauth_flow\n"- "description: OAuth handshake recipe\n"- "triggers:\n"- " - auth\n"- "---\n\n"- "# OAuth flow\n\nStep 1: redirect to /authorize\n"-)---def test_upload_copilot_skill_creates_skill(- mocker: pytest_mock.MockFixture,-) -> None:- """POST /skills parses the SKILL.md and persists it via store_user_skill."""- store_mock = AsyncMock(- return_value=ParsedSkill(- name="oauth_flow",- description="OAuth handshake recipe",- body="# OAuth flow",- triggers=("auth",),- )- )- mocker.patch("backend.api.features.v1.store_user_skill", store_mock)-- response = client.post("/skills", json={"content": _VALID_SKILL_MD})- assert response.status_code == 201- body = response.json()- assert body["name"] == "oauth_flow"- assert body["triggers"] == ["auth"]- store_mock.assert_awaited_once()- assert store_mock.await_args.kwargs["name"] == "oauth_flow"---def test_upload_copilot_skill_keeps_frontmatter_the_platform_ignores(- mocker: pytest_mock.MockFixture,-) -> None:- """An uploaded package's licence line is the author's, not ours — storing- the file without it rewrites what they uploaded."""- store_mock = AsyncMock(- return_value=ParsedSkill(name="oauth_flow", description="d", body="b")- )- mocker.patch("backend.api.features.v1.store_user_skill", store_mock)-- response = client.post(- "/skills",- json={"content": _VALID_SKILL_MD.replace("---\n\n", "license: MIT\n---\n\n")},- )- assert response.status_code == 201- assert store_mock.await_args.kwargs["extra"] == {"license": "MIT"}---def test_upload_copilot_skill_rejects_malformed_markdown() -> None:- """A file without valid frontmatter returns 400 before touching storage."""- response = client.post(- "/skills", json={"content": "just some text, no frontmatter"}- )- assert response.status_code == 400---def test_upload_copilot_skill_returns_409_when_at_cap(- mocker: pytest_mock.MockFixture,-) -> None:- """The per-user cap surfaces as 409 so the UI can prompt a delete."""- mocker.patch(- "backend.api.features.v1.store_user_skill",- AsyncMock(side_effect=SkillLimitError("Skill limit reached (50).")),- )-- response = client.post("/skills", json={"content": _VALID_SKILL_MD})- assert response.status_code == 409---def test_upload_copilot_skill_returns_400_on_validation_error(- mocker: pytest_mock.MockFixture,-) -> None:- """A validation failure from store_user_skill maps to 400."""- mocker.patch(- "backend.api.features.v1.store_user_skill",- AsyncMock(side_effect=ValueError("name must be a slug")),- )-- response = client.post("/skills", json={"content": _VALID_SKILL_MD})- assert response.status_code == 400---def test_upload_copilot_skill_returns_400_on_virus_detection(- mocker: pytest_mock.MockFixture,-) -> None:- """A virus-scan rejection surfaces as a 400 client error, not a 500."""- mocker.patch(- "backend.api.features.v1.store_user_skill",- AsyncMock(side_effect=VirusDetectedError("nasty")),- )-- response = client.post("/skills", json={"content": _VALID_SKILL_MD})- assert response.status_code == 400- assert "virus scan" in response.json()["detail"]---# Storing a skill re-renders its frontmatter, so the canonical form is what a-# download hands back and what an upload→download round trip can compare.-_PARSED_VALID_SKILL = parse_skill_markdown(_VALID_SKILL_MD)-assert _PARSED_VALID_SKILL is not None-_CANONICAL_SKILL_MD = render_skill_markdown(_PARSED_VALID_SKILL)---def _package_zip(files: list[SkillFile] | None = None) -> bytes:- """A valid package archive: the canonical SKILL.md plus *files*."""- return zip_from_package(- SkillPackage(skill_md=_CANONICAL_SKILL_MD, files=files or [])- )---_PACKAGE_FILES = [- SkillFile(relative_path="references/providers.md", content=b"# Providers\n"),- SkillFile(relative_path="references/errors.md", content=b"# Errors\n"),- SkillFile(relative_path="assets/logo.svg", content=b"<svg/>"),- SkillFile(relative_path="assets/nested/icon.svg", content=b"<svg id=1/>"),- SkillFile(- relative_path="scripts/exchange_code.py",- content=b"#!/usr/bin/env python3\nprint(1)\n",- is_executable=True,- ),- SkillFile(relative_path="scripts/helpers.py", content=b"X = 1\n"),-]---def test_skill_package_round_trips_through_upload_and_download() -> None:- """The tree a user uploads is the tree they get back — same paths, same- bytes, and a script that arrived executable is still executable."""- with _patch_skills_path(_FakeWorkspaceManager()):- created = client.post(- "/skills/package",- files={- "file": ("pkg.zip", _package_zip(_PACKAGE_FILES), "application/zip")- },- )- assert created.status_code == 201, created.text- assert created.json()["name"] == "oauth_flow"-- assert [s["name"] for s in client.get("/skills").json()] == ["oauth_flow"]-- detail = client.get("/skills/oauth_flow")- assert detail.status_code == 200- assert {f["path"]: f["is_executable"] for f in detail.json()["files"]} == {- f.relative_path: f.is_executable for f in _PACKAGE_FILES- }-- downloaded = client.get("/skills/oauth_flow/package")-- assert downloaded.status_code == 200- assert downloaded.headers["content-type"] == "application/zip"- assert "oauth_flow.zip" in downloaded.headers["content-disposition"]- restored = package_from_zip(downloaded.content)- assert restored.skill_md == _CANONICAL_SKILL_MD- assert sorted(- (f.relative_path, f.content, f.is_executable) for f in restored.files- ) == sorted((f.relative_path, f.content, f.is_executable) for f in _PACKAGE_FILES)---def test_upload_package_over_the_files_cap_is_413_and_writes_nothing() -> None:- manager = _FakeWorkspaceManager()- oversized = [- SkillFile(relative_path=f"f{i}.txt", content=b"x")- for i in range(MAX_PACKAGE_FILES + 1)- ]-- with _patch_skills_path(manager):- response = client.post(- "/skills/package",- files={"file": ("pkg.zip", _package_zip(oversized), "application/zip")},- )-- assert response.status_code == 413- detail = response.json()["detail"]- assert "files" in detail and str(MAX_PACKAGE_FILES) in detail- assert manager.files == {}---def test_upload_package_with_an_escaping_member_is_400_and_writes_nothing() -> None:- """Zip slip is refused before the first write, so a rejected archive- leaves the workspace exactly as it was."""- manager = _FakeWorkspaceManager()- escaping = [SkillFile(relative_path="../../etc/passwd", content=b"x")]-- with _patch_skills_path(manager):- response = client.post(- "/skills/package",- files={"file": ("pkg.zip", _package_zip(escaping), "application/zip")},- )-- assert response.status_code == 400- assert manager.files == {}---def test_upload_package_refuses_an_oversized_body_before_parsing(- mocker: pytest_mock.MockFixture,-) -> None:- """The body cap aborts mid-read, so a 20 MiB archive never lands in- memory whole."""- mocker.patch("backend.api.features.v1.MAX_ZIP_BYTES", 64)- parse = mocker.patch("backend.api.features.v1.package_from_zip")-- response = client.post(- "/skills/package",- files={"file": ("pkg.zip", b"x" * 4096, "application/zip")},- )-- assert response.status_code == 413- parse.assert_not_called()---def test_upload_package_rejects_an_archive_whose_skill_md_is_malformed() -> None:- response = client.post(- "/skills/package",- files={- "file": (- "pkg.zip",- zip_from_package(SkillPackage(skill_md="no frontmatter here")),- "application/zip",- )- },- )- assert response.status_code == 400---def test_upload_package_rejects_something_that_is_not_a_zip() -> None:- response = client.post(- "/skills/package",- files={"file": ("pkg.zip", _VALID_SKILL_MD.encode(), "application/zip")},- )- assert response.status_code == 400---def test_download_refuses_rather_than_serving_a_package_it_cannot_read_whole() -> None:- """A download is a backup: a sibling that will not read must fail the- request, not quietly produce an archive with the file missing."""-- class _OneUnreadableSibling(_FakeWorkspaceManager):- async def read_file(self, path: str) -> bytes:- if path.endswith("references/providers.md"):- raise RuntimeError("storage unavailable")- return await super().read_file(path)-- with _patch_skills_path(_OneUnreadableSibling()):- created = client.post(- "/skills/package",- files={- "file": ("pkg.zip", _package_zip(_PACKAGE_FILES), "application/zip")- },- )- assert created.status_code == 201, created.text-- with pytest.raises(RuntimeError, match="storage unavailable"):- client.get("/skills/oauth_flow/package")---def test_download_does_not_report_a_storage_failure_as_a_missing_skill() -> None:- """404 means the user has no such skill. A storage failure reading the- SKILL.md is ours, and saying "not found" would send them looking for a- skill they still have."""-- class _UnreadableRoot(_FakeWorkspaceManager):- async def read_file(self, path: str) -> bytes:- if path.endswith("/SKILL.md"):- raise RuntimeError("storage unavailable")- return await super().read_file(path)-- with _patch_skills_path(_UnreadableRoot()):- created = client.post(- "/skills/package",- files={"file": ("pkg.zip", _package_zip(), "application/zip")},- )- assert created.status_code == 201, created.text-- with pytest.raises(RuntimeError, match="storage unavailable"):- client.get("/skills/oauth_flow/package")---def test_download_refuses_a_stored_package_over_the_files_cap() -> None:- """Only a legacy or hand-made folder can be over the cap, and truncating it- to the cap would hand back an archive that silently is not the skill."""- manager = _FakeWorkspaceManager()- with _patch_skills_path(manager):- created = client.post(- "/skills/package",- files={"file": ("pkg.zip", _package_zip(), "application/zip")},- )- assert created.status_code == 201, created.text- # Seed the folder past the cap the way only a pre-cap write could have.- for i in range(MAX_PACKAGE_FILES + 1):- manager.files[f"/skills/oauth_flow/f{i}.txt"] = b"x"-- response = client.get("/skills/oauth_flow/package")-- assert response.status_code == 413- assert str(MAX_PACKAGE_FILES) in response.json()["detail"]---def test_download_of_a_package_being_rewritten_is_409_not_a_bad_request(- mocker: pytest_mock.MockFixture,-) -> None:- """A tree that will not hold still is a conflict the caller can retry, not- a malformed package: 400 or 413 would blame the user for their own- concurrent edit."""- mocker.patch(- "backend.api.features.v1.read_user_skill_package",- AsyncMock(side_effect=ConflictError("changed while it was read")),- )-- response = client.get("/skills/oauth_flow/package")-- assert response.status_code == 409, response.text---def test_download_package_returns_404_for_a_missing_skill(- mocker: pytest_mock.MockFixture,-) -> None:- mocker.patch(- "backend.api.features.v1.read_user_skill_package",- AsyncMock(return_value=None),- )- assert client.get("/skills/missing/package").status_code == 404---_EXPERT_SKILL_ROUTES = {- "list": lambda expert_id: client.get("/skills", params=_owner(expert_id)),- "upload": lambda expert_id: client.post(- "/skills", params=_owner(expert_id), json={"content": _VALID_SKILL_MD}- ),- "read": lambda expert_id: client.get(- "/skills/oauth_flow", params=_owner(expert_id)- ),- "upload_package": lambda expert_id: client.post(- "/skills/package",- params=_owner(expert_id),- files={"file": ("pkg.zip", _package_zip(), "application/zip")},- ),- "download_package": lambda expert_id: client.get(- "/skills/oauth_flow/package", params=_owner(expert_id)- ),- "delete": lambda expert_id: client.delete(- "/skills/oauth_flow", params=_owner(expert_id)- ),-}--_SKILL_LAYER = {- "list_user_skills": [],- "store_user_skill": ParsedSkill(name="oauth_flow", description="d", body="b"),- "read_user_skill_with_body": ParsedSkill(- name="oauth_flow", description="d", body="b"- ),- "delete_user_skill": "oauth_flow",- "read_user_skill_package": SkillPackage(skill_md=_CANONICAL_SKILL_MD),-}--[email protected]("route", list(_EXPERT_SKILL_ROUTES))-def test_expert_skill_routes_refuse_an_expert_the_caller_does_not_own(- route: str,- mocker: pytest_mock.MockFixture,- test_user_id: str,-) -> None:- """Every /skills route naming an expert goes through the same owner gate,- and a refusal is a 404 that never reaches the skills layer."""- owns = mocker.patch(- "backend.api.features.v1.experts_db.owns_private_active_expert",- AsyncMock(return_value=False),- )- downstream = {- name: mocker.patch(f"backend.api.features.v1.{name}", AsyncMock())- for name in _SKILL_LAYER- }-- response = _EXPERT_SKILL_ROUTES[route]("expert-not-mine")-- assert response.status_code == 404- assert response.json()["detail"] == "Expert 'expert-not-mine' not found"- owns.assert_awaited_once_with(test_user_id, "expert-not-mine")- for mock in downstream.values():- mock.assert_not_awaited()--- "route,downstream",- [- ("list", "list_user_skills"),- ("upload", "store_user_skill"),- ("read", "read_user_skill_with_body"),- ("delete", "delete_user_skill"),- ("upload_package", "store_user_skill"),- ("download_package", "read_user_skill_package"),- ],-)-def test_expert_skill_routes_forward_an_owned_expert(- route: str,- downstream: str,- mocker: pytest_mock.MockFixture,-) -> None:- """An owned PRIVATE expert passes the gate and its id reaches the skills- layer, so the call reads that expert's folder and not Otto's."""- mocker.patch(- "backend.api.features.v1.experts_db.owns_private_active_expert",- AsyncMock(return_value=True),- )- mocker.patch(- "backend.api.features.v1.list_user_skill_files",- AsyncMock(return_value=[]),- )- target = mocker.patch(- f"backend.api.features.v1.{downstream}",- AsyncMock(return_value=_SKILL_LAYER[downstream]),- )-- response = _EXPERT_SKILL_ROUTES[route]("expert-mine")-- assert response.status_code in (200, 201)- assert _forwarded_expert_id(target) == "expert-mine"--[email protected]("route", list(_EXPERT_SKILL_ROUTES))-def test_personal_autopilot_skill_routes_skip_the_expert_gate(- route: str,- mocker: pytest_mock.MockFixture,-) -> None:- """No expert named means personal Otto, which owns its own folder and- must never be refused by the expert gate."""- owns = mocker.patch(- "backend.api.features.v1.experts_db.owns_private_active_expert",- AsyncMock(return_value=False),- )- mocker.patch(- "backend.api.features.v1.list_user_skill_files",- AsyncMock(return_value=[]),- )- for name, value in _SKILL_LAYER.items():- mocker.patch(f"backend.api.features.v1.{name}", AsyncMock(return_value=value))-- response = _EXPERT_SKILL_ROUTES[route](None)-- assert response.status_code in (200, 201)- owns.assert_not_awaited()---# ``expert_id`` omitted entirely, not sent empty: an empty query value is a-# str, which would take the expert branch of the gate rather than the-# personal-Otto one.-def _owner(expert_id: str | None) -> dict[str, str]:- return {} if expert_id is None else {"expert_id": expert_id}---def _forwarded_expert_id(mock: AsyncMock) -> str | None:- """``list_user_skills`` takes the owner positionally, the rest by keyword."""- call = mock.await_args- if call is None:- return None- if "expert_id" in call.kwargs:- return call.kwargs["expert_id"]- return call.args[1] if len(call.args) > 1 else Nonediff --git a/autogpt_platform/backend/backend/api/rest_api.py b/autogpt_platform/backend/backend/api/rest_api.pyindex aef1d57cd2..7287dfc2e1 100644--- a/autogpt_platform/backend/backend/api/rest_api.py+++ b/autogpt_platform/backend/backend/api/rest_api.py@@ -55,6 +55,7 @@ import backend.api.features.platform_linking.routesimport backend.api.features.postmark.postmarkimport backend.api.features.push.routes as push_routesimport backend.api.features.search.routes as search_routes+import backend.api.features.skills.routes as skills_routesimport backend.api.features.store.modelimport backend.api.features.store.routesimport backend.api.features.store.skill_routes@@ -391,6 +392,11 @@ app.include_router(tags=["v1", "api-keys"],prefix="/api/api-keys",)+app.include_router(+ skills_routes.router,+ tags=["v1", "skills"],+ prefix="/api/skills",+)app.include_router(auth_email_routes.auth_email_router,prefix="/api/auth/email",