Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

2.0 KB
Raw Blame
"""Inner API for published workspace Skills.

These endpoints are called by trusted runtime services. They expose only
published Skill artifacts, never draft files or editable metadata.
"""

from __future__ import annotations

import io

from flask import request, send_file
from flask_restx import Resource
from pydantic import BaseModel, ValidationError
from sqlalchemy.orm import Session

from controllers.common.session import with_session
from controllers.console.wraps import setup_required
from controllers.inner_api import inner_api_ns
from controllers.inner_api.wraps import plugin_inner_api_only
from services.skill_management_service import SkillManagementService, SkillManagementServiceError


class _SkillTargetQuery(BaseModel):
    tenant_id: str


def _target_query_from_request() -> _SkillTargetQuery:
    return _SkillTargetQuery.model_validate({"tenant_id": request.args.get("tenant_id")})


def _error_response(exc: SkillManagementServiceError) -> tuple[dict[str, str], int]:
    return {"code": exc.code, "message": exc.message}, exc.status_code


@inner_api_ns.route("/skills/<string:skill_id>/pull")
class PublishedSkillPullApi(Resource):
    @setup_required
    @plugin_inner_api_only
    @inner_api_ns.doc("published_skill_pull")
    @with_session(write=False)
    def get(self, session: Session, skill_id: str):
        try:
            query = _target_query_from_request()
            result = SkillManagementService(session=session).pull_published_archive(
                tenant_id=query.tenant_id, skill_id=skill_id
            )
            return send_file(
                io.BytesIO(result.payload),
                mimetype=result.mime_type,
                as_attachment=True,
                download_name=result.filename,
            )
        except ValidationError as exc:
            return {"code": "invalid_request", "message": str(exc)}, 400
        except SkillManagementServiceError as exc:
            return _error_response(exc)


__all__ = ["PublishedSkillPullApi"]