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.

11.7 KB
Raw Blame
from types import ModuleType
from typing import override

from flask import Blueprint
from flask_restx import Namespace

from controllers.common.access_response import register_auth_error_response

# Register response/query models BEFORE importing controller modules so that
# @openapi_ns.response / @openapi_ns.expect decorators can resolve model names.
from controllers.common.fields import BinaryFileResponse, EventStreamResponse, SimpleResultResponse
from controllers.common.schema import register_enum_models, register_response_schema_models, register_schema_models
from controllers.openapi._catalog import attach_catalog
from controllers.openapi._errors import ErrorBody, OpenApiErrorCode, OpenApiErrorFormatter
from controllers.openapi._models import (
    AccountPayload,
    AccountResponse,
    AdvancedChatNodeRunPayload,
    AdvancedChatRunPayload,
    AdvancedChatWebApp,
    AdvancedChatWebAppPatch,
    AgentAppInfo,
    AgentAppInfoPatch,
    AgentServiceApi,
    AgentWebApp,
    AppDescribeInfo,
    AppDescribeQuery,
    AppDescribeResponse,
    AppDslExportQuery,
    AppDslExportResponse,
    AppDslImportPayload,
    AppDslImportResponse,
    AppInfo,
    AppInfoPatch,
    AppListQuery,
    AppListResponse,
    AppListRow,
    AppSettingsInfo,
    ChatAppInfo,
    ChatAppInfoPatch,
    ChatRunPayload,
    ChatWebApp,
    ChatWebAppPatch,
    CheckDependenciesResponse,
    CompletionRunPayload,
    CreateAppPayload,
    CreatedAppResponse,
    CredentialFormField,
    CredentialRef,
    CredentialWriteResponse,
    CustomModelRow,
    DeviceCodeRequest,
    DeviceCodeResponse,
    DeviceFlowErrorResponse,
    DeviceLookupQuery,
    DeviceLookupResponse,
    DeviceMutateRequest,
    DeviceMutateResponse,
    DevicePollRequest,
    DeviceTokenResponse,
    DraftWorkflowRunPayload,
    DslCheckPayload,
    DslCheckResponse,
    DslIssueRow,
    EnvVariableListResponse,
    EnvVariableSetPayload,
    FileUploadPayload,
    FormSubmitResponse,
    HealthResponse,
    Hint,
    HumanInputFormDefinitionResponse,
    KnowledgeBaseListQuery,
    KnowledgeBaseListResponse,
    KnowledgeBaseRow,
    KnowledgeRetrievalFragment,
    LlmModelBlock,
    MarketplacePluginListResponse,
    MarketplacePluginQuery,
    MarketplacePluginRow,
    MemberActionResponse,
    MemberInvitePayload,
    MemberInviteResponse,
    MemberListQuery,
    MemberListResponse,
    MemberResponse,
    MemberRoleUpdatePayload,
    MessageMetadata,
    ModelCredentialCreatePayload,
    ModelCredentialUpdatePayload,
    ModelListQuery,
    ModelListResponse,
    ModelProviderDetailResponse,
    ModelProviderRow,
    ModelRef,
    ModelRow,
    MultipleRetrievalFragment,
    NodeRunPayload,
    NodeTypeDetailResponse,
    NodeTypeListResponse,
    NodeTypeRow,
    OpenApiFormSubmitPayload,
    PermittedExternalAppsListQuery,
    PermittedExternalAppsListResponse,
    PluginDeleteResponse,
    PluginInstallPayload,
    PluginListQuery,
    PluginListResponse,
    PluginProvides,
    PluginRow,
    PluginTaskItem,
    PluginTaskResponse,
    PluginTaskStartResponse,
    ProviderCredentialCreatePayload,
    ProviderCredentialUpdatePayload,
    PublishPayload,
    PublishResponse,
    ReleaseCheckResponse,
    RestoreResponse,
    RevokeResponse,
    RunListQuery,
    RunListResponse,
    ServerVersionResponse,
    ServiceApi,
    ServiceApiPatch,
    SessionListQuery,
    SessionListResponse,
    SessionRow,
    SkillDescribeResponse,
    SkillExportQuery,
    SkillImportPayload,
    SkillImportResponse,
    SkillListQuery,
    SkillListResponse,
    SkillPublishPayload,
    SkillPublishResponse,
    SkillRow,
    SkillVersionListQuery,
    SkillVersionListResponse,
    SkillVersionRow,
    TaskStopResponse,
    ToolCredentialCreatePayload,
    ToolCredentialUpdatePayload,
    ToolListQuery,
    ToolListResponse,
    ToolProviderDetailResponse,
    ToolProviderRow,
    UsageInfo,
    VersionListQuery,
    VersionListResponse,
    WebApp,
    WebAppAccess,
    WebAppAccessPayload,
    WebAppPatch,
    WebAppToken,
    WorkflowRunData,
    WorkflowRunPayload,
    WorkflowWebApp,
    WorkflowWebAppPatch,
    WorkspaceDetailResponse,
    WorkspaceListQuery,
    WorkspaceListResponse,
    WorkspacePayload,
    WorkspaceSummaryResponse,
)
from controllers.openapi._upload import describe_multipart_bodies
from fields.file_fields import FileResponse
from fields.workflow_run_fields import (
    WorkflowRunDetailResponse,
    WorkflowRunNodeExecutionListResponse,
    WorkflowRunNodeExecutionResponse,
)
from libs.device_flow_security import attach_anti_framing
from libs.external_api import ErrorSpec, ExternalApi
from services.entities.dsl_entities import CheckDependenciesResult, Import

bp = Blueprint("openapi", __name__, url_prefix="/openapi/v1")
attach_anti_framing(bp)
register_auth_error_response(bp)
attach_catalog(bp)


class _OpenApi(ExternalApi):
    """The surface's document also names the multipart form its file-bearing bodies accept."""

    error_response_model = ErrorBody

    @override
    def finish_document(self, document: dict[str, object]) -> dict[str, object]:
        return super().finish_document(describe_multipart_bodies(document))

    @override
    def error_code_for(self, spec: ErrorSpec) -> str:
        return OpenApiErrorFormatter.code_for(spec["status"], spec["error_code"])

    @override
    def implicit_error_codes(self, status: int) -> set[str]:
        return {OpenApiErrorFormatter.code_for(status, None)}


api = _OpenApi(
    bp,
    version="1.0",
    title="OpenAPI",
    description="User-scoped programmatic API (bearer auth)",
    error_body_formatter=OpenApiErrorFormatter(),
)

openapi_ns = Namespace("openapi", description="User-scoped operations", path="/")

register_schema_models(
    openapi_ns,
    AdvancedChatNodeRunPayload,
    AdvancedChatRunPayload,
    AgentAppInfoPatch,
    AppDescribeQuery,
    AppInfoPatch,
    ChatAppInfoPatch,
    ServiceApiPatch,
    AdvancedChatWebAppPatch,
    ChatWebAppPatch,
    WebAppAccessPayload,
    WebAppPatch,
    WorkflowWebAppPatch,
    AppDslImportPayload,
    DslCheckPayload,
    AppDslExportQuery,
    AppListQuery,
    ChatRunPayload,
    CompletionRunPayload,
    CreateAppPayload,
    DeviceCodeRequest,
    DevicePollRequest,
    DeviceLookupQuery,
    DeviceMutateRequest,
    DraftWorkflowRunPayload,
    FileUploadPayload,
    MemberInvitePayload,
    MemberListQuery,
    MemberRoleUpdatePayload,
    NodeRunPayload,
    OpenApiFormSubmitPayload,
    EnvVariableSetPayload,
    MarketplacePluginQuery,
    PermittedExternalAppsListQuery,
    PluginInstallPayload,
    PluginListQuery,
    ModelListQuery,
    KnowledgeBaseListQuery,
    ToolListQuery,
    ProviderCredentialCreatePayload,
    ProviderCredentialUpdatePayload,
    ModelRef,
    ModelCredentialCreatePayload,
    ModelCredentialUpdatePayload,
    ToolCredentialCreatePayload,
    ToolCredentialUpdatePayload,
    PublishPayload,
    RunListQuery,
    SessionListQuery,
    SkillExportQuery,
    SkillImportPayload,
    SkillListQuery,
    SkillPublishPayload,
    SkillVersionListQuery,
    VersionListQuery,
    WorkflowRunPayload,
    WorkspaceListQuery,
)
register_response_schema_models(
    openapi_ns,
    BinaryFileResponse,
    ErrorBody,
    EventStreamResponse,
    SimpleResultResponse,
    UsageInfo,
    MessageMetadata,
    Hint,
    AppListRow,
    AppListResponse,
    AppInfo,
    AppDescribeInfo,
    AppDescribeResponse,
    AppDslExportResponse,
    Import,
    AppDslImportResponse,
    DslIssueRow,
    DslCheckResponse,
    ReleaseCheckResponse,
    CheckDependenciesResult,
    CheckDependenciesResponse,
    WorkflowRunData,
    AccountPayload,
    WorkspacePayload,
    AccountResponse,
    SessionRow,
    SessionListResponse,
    PermittedExternalAppsListResponse,
    RevokeResponse,
    WorkspaceSummaryResponse,
    WorkspaceListResponse,
    WorkspaceDetailResponse,
    MemberResponse,
    MemberListResponse,
    MemberInviteResponse,
    MemberActionResponse,
    TaskStopResponse,
    FormSubmitResponse,
    HumanInputFormDefinitionResponse,
    DeviceCodeResponse,
    DeviceTokenResponse,
    DeviceLookupResponse,
    DeviceMutateResponse,
    DeviceFlowErrorResponse,
    FileResponse,
    ServerVersionResponse,
    HealthResponse,
    RunListResponse,
    PublishResponse,
    VersionListResponse,
    RestoreResponse,
    EnvVariableListResponse,
    WorkflowRunDetailResponse,
    WorkflowRunNodeExecutionListResponse,
    WorkflowRunNodeExecutionResponse,
    CreatedAppResponse,
    NodeTypeRow,
    NodeTypeListResponse,
    NodeTypeDetailResponse,
    AppSettingsInfo,
    ChatAppInfo,
    AgentAppInfo,
    ServiceApi,
    AgentServiceApi,
    AdvancedChatWebApp,
    AgentWebApp,
    ChatWebApp,
    WebApp,
    WebAppAccess,
    WebAppToken,
    WorkflowWebApp,
    MarketplacePluginRow,
    MarketplacePluginListResponse,
    PluginProvides,
    PluginRow,
    PluginListResponse,
    PluginTaskStartResponse,
    PluginTaskItem,
    PluginTaskResponse,
    PluginDeleteResponse,
    CredentialFormField,
    CredentialRef,
    CredentialWriteResponse,
    CustomModelRow,
    ModelListResponse,
    ToolListResponse,
    ModelProviderDetailResponse,
    ModelProviderRow,
    ModelRow,
    KnowledgeBaseListResponse,
    KnowledgeBaseRow,
    KnowledgeRetrievalFragment,
    LlmModelBlock,
    MultipleRetrievalFragment,
    SkillRow,
    SkillImportResponse,
    SkillListResponse,
    SkillVersionRow,
    SkillDescribeResponse,
    SkillVersionListResponse,
    SkillPublishResponse,
    ToolProviderDetailResponse,
    ToolProviderRow,
)
# Standalone definition for contract codegen; ErrorBody.code stays an open
# string on the wire so old clients keep parsing future codes.
register_enum_models(openapi_ns, OpenApiErrorCode)


def _register_controllers() -> tuple[ModuleType, ...]:
    """Load route modules after their namespaces and schema models exist."""

    from . import (
        account,
        app_create,
        app_dsl,
        app_info,
        app_release,
        app_run,
        app_webapp,
        app_workflow,
        apps,
        apps_permitted_external,
        files,
        human_input_form,
        index,
        knowledge_bases,
        knowledge_fs,
        model_providers,
        node_types,
        oauth_device,
        oauth_device_sso,
        plugins,
        skills,
        tool_providers,
        workflow_events,
        workspaces,
    )

    return (
        account,
        app_create,
        app_dsl,
        app_info,
        app_release,
        app_run,
        app_webapp,
        app_workflow,
        apps,
        apps_permitted_external,
        files,
        human_input_form,
        index,
        knowledge_bases,
        knowledge_fs,
        model_providers,
        node_types,
        oauth_device,
        oauth_device_sso,
        plugins,
        skills,
        tool_providers,
        workflow_events,
        workspaces,
    )


(
    account,
    app_create,
    app_dsl,
    app_info,
    app_release,
    app_run,
    app_webapp,
    app_workflow,
    apps,
    apps_permitted_external,
    files,
    human_input_form,
    index,
    knowledge_bases,
    knowledge_fs,
    model_providers,
    node_types,
    oauth_device,
    oauth_device_sso,
    plugins,
    skills,
    tool_providers,
    workflow_events,
    workspaces,
) = _register_controllers()

# Request models are imported from _models.py and registered above.

__all__ = [
    "account",
    "app_create",
    "app_dsl",
    "app_info",
    "app_release",
    "app_run",
    "app_webapp",
    "app_workflow",
    "apps",
    "apps_permitted_external",
    "files",
    "human_input_form",
    "index",
    "knowledge_bases",
    "knowledge_fs",
    "model_providers",
    "node_types",
    "oauth_device",
    "oauth_device_sso",
    "plugins",
    "skills",
    "tool_providers",
    "workflow_events",
    "workspaces",
]

api.add_namespace(openapi_ns)