chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,118 @@
|
||||
from types import ModuleType
|
||||
|
||||
from flask import Blueprint
|
||||
from flask_restx import Namespace
|
||||
|
||||
from controllers.common.errors import register_credential_refresh_error_handler
|
||||
from libs.external_api import ExternalApi
|
||||
|
||||
bp = Blueprint("inner_api", __name__, url_prefix="/inner/api")
|
||||
|
||||
api = ExternalApi(
|
||||
bp,
|
||||
version="1.0",
|
||||
title="Inner API",
|
||||
description="Internal APIs for enterprise features, billing, knowledge retrieval, and plugin communication",
|
||||
authorizations={
|
||||
"InnerApiKey": {
|
||||
"type": "apiKey",
|
||||
"in": "header",
|
||||
"name": "X-Inner-Api-Key",
|
||||
"description": "INNER_API_KEY for enterprise callers; INNER_API_KEY_FOR_PLUGIN for plugin/agent callers.",
|
||||
}
|
||||
},
|
||||
security="InnerApiKey",
|
||||
)
|
||||
register_credential_refresh_error_handler(api)
|
||||
|
||||
# Create namespace
|
||||
inner_api_ns = Namespace("inner_api", description="Internal API operations", path="/")
|
||||
|
||||
|
||||
def _register_controllers() -> tuple[ModuleType, ...]:
|
||||
"""Load route modules after their namespace exists."""
|
||||
|
||||
from . import datasource_credentials, mail, runtime_credentials
|
||||
from .agent import files, knowledge, llm, runtime_usage, tools, tracing
|
||||
from .app import app, dsl, file_grants
|
||||
from .knowledge import retrieval
|
||||
from .knowledge_fs import background, storage, texts, vectors
|
||||
from .plugin import agent_config, plugin, skills
|
||||
from .workspace import workspace
|
||||
|
||||
return (
|
||||
datasource_credentials,
|
||||
mail,
|
||||
runtime_credentials,
|
||||
files,
|
||||
knowledge,
|
||||
llm,
|
||||
runtime_usage,
|
||||
tools,
|
||||
tracing,
|
||||
app,
|
||||
dsl,
|
||||
file_grants,
|
||||
retrieval,
|
||||
background,
|
||||
storage,
|
||||
texts,
|
||||
vectors,
|
||||
agent_config,
|
||||
plugin,
|
||||
skills,
|
||||
workspace,
|
||||
)
|
||||
|
||||
|
||||
(
|
||||
_datasource_credentials,
|
||||
_mail,
|
||||
_runtime_credentials,
|
||||
_agent_files,
|
||||
_agent_knowledge,
|
||||
_agent_llm,
|
||||
_agent_runtime_usage,
|
||||
_agent_tools,
|
||||
_agent_tracing,
|
||||
_app_app,
|
||||
_app_dsl,
|
||||
_app_file_grants,
|
||||
_knowledge_retrieval,
|
||||
_knowledge_fs_background,
|
||||
_knowledge_fs_storage,
|
||||
_knowledge_fs_texts,
|
||||
_knowledge_fs_vectors,
|
||||
_agent_config,
|
||||
_plugin,
|
||||
_skills,
|
||||
_workspace,
|
||||
) = _register_controllers()
|
||||
|
||||
api.add_namespace(inner_api_ns)
|
||||
|
||||
__all__ = [
|
||||
"_agent_config",
|
||||
"_agent_files",
|
||||
"_agent_knowledge",
|
||||
"_agent_llm",
|
||||
"_agent_runtime_usage",
|
||||
"_agent_tools",
|
||||
"_app_app",
|
||||
"_app_dsl",
|
||||
"_app_file_grants",
|
||||
"_datasource_credentials",
|
||||
"_knowledge_fs_background",
|
||||
"_knowledge_fs_storage",
|
||||
"_knowledge_fs_texts",
|
||||
"_knowledge_fs_vectors",
|
||||
"_knowledge_retrieval",
|
||||
"_mail",
|
||||
"_plugin",
|
||||
"_runtime_credentials",
|
||||
"_skills",
|
||||
"_workspace",
|
||||
"api",
|
||||
"bp",
|
||||
"inner_api_ns",
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
"""Agent backend inner API controllers."""
|
||||
@@ -0,0 +1,207 @@
|
||||
"""Agent-owned inner endpoints for CLI file URL allocation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from configs import dify_config
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
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.plugin.wraps import get_user
|
||||
from controllers.inner_api.wraps import plugin_inner_api_only
|
||||
from core.plugin.entities.request import RequestDownloadFileMapping, RequestRequestUploadFile
|
||||
from core.tools.signature import bind_file_uri, get_signed_file_uri_for_plugin
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from libs.exception import BaseHTTPException
|
||||
from services.file_request_service import FileRequestService
|
||||
|
||||
|
||||
class AgentFileRequestHttpError(BaseHTTPException):
|
||||
error_code = "agent_file_request_failed"
|
||||
description = "Agent file request failed."
|
||||
code = 500
|
||||
|
||||
def __init__(self, *, error_code: str, description: str, status_code: int) -> None:
|
||||
self.error_code = error_code
|
||||
self.description = description
|
||||
self.code = status_code
|
||||
super().__init__(description)
|
||||
|
||||
|
||||
class AgentFileUploadRequestPayload(RequestRequestUploadFile):
|
||||
tenant_id: str
|
||||
user_id: str
|
||||
user_from: Literal["account", "end-user"] | None = None
|
||||
max_size: int = Field(ge=0, description="Maximum upload size in bytes")
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class AgentFileDownloadRequestPayload(BaseModel):
|
||||
tenant_id: str
|
||||
user_id: str
|
||||
user_from: Literal["account", "end-user"]
|
||||
invoke_from: Literal[
|
||||
"service-api",
|
||||
"openapi",
|
||||
"web-app",
|
||||
"trigger",
|
||||
"explore",
|
||||
"debugger",
|
||||
"published",
|
||||
"validation",
|
||||
]
|
||||
file: RequestDownloadFileMapping
|
||||
for_frontend: bool = True
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class AgentFileUploadRequestResponse(ResponseModel):
|
||||
upload_uri: str
|
||||
|
||||
|
||||
class AgentFileDownloadRequestResponse(ResponseModel):
|
||||
filename: str
|
||||
mime_type: str | None = None
|
||||
size: int
|
||||
download_uri: str
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, AgentFileUploadRequestPayload, AgentFileDownloadRequestPayload)
|
||||
register_response_schema_models(
|
||||
inner_api_ns,
|
||||
AgentFileUploadRequestResponse,
|
||||
AgentFileDownloadRequestResponse,
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/files/upload-request")
|
||||
class AgentFileUploadRequestApi(Resource):
|
||||
"""Allocate an origin-free signed upload URI for the Agent CLI."""
|
||||
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_file_upload_request")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentFileUploadRequestPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200,
|
||||
"Upload URI allocated",
|
||||
inner_api_ns.models[AgentFileUploadRequestResponse.__name__],
|
||||
)
|
||||
@with_session(write=False)
|
||||
def post(self, session: Session) -> dict[str, object]:
|
||||
try:
|
||||
payload = AgentFileUploadRequestPayload.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="invalid_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
tenant = application_services().workspaces.management.get(payload.tenant_id)
|
||||
if tenant is None:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="tenant_not_found",
|
||||
description="tenant not found",
|
||||
status_code=404,
|
||||
)
|
||||
try:
|
||||
if payload.user_from == "account":
|
||||
if not application_services().workspaces.members.get_role(tenant.id, payload.user_id):
|
||||
raise ValueError("account not found")
|
||||
owner_id = payload.user_id
|
||||
else:
|
||||
owner_id = get_user(tenant.id, payload.user_id).id
|
||||
upload_uri = get_signed_file_uri_for_plugin(
|
||||
filename=payload.filename,
|
||||
mimetype=payload.mimetype,
|
||||
tenant_id=tenant.id,
|
||||
user_id=owner_id,
|
||||
conversation_id=payload.conversation_id,
|
||||
user_from=payload.user_from,
|
||||
max_size=payload.max_size,
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="user_not_found",
|
||||
description=str(exc),
|
||||
status_code=404,
|
||||
) from exc
|
||||
|
||||
return AgentFileUploadRequestResponse(upload_uri=upload_uri).model_dump(mode="json")
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/files/download-request")
|
||||
class AgentFileDownloadRequestApi(Resource):
|
||||
"""Allocate a transfer URI or frontend URL for one Agent CLI file."""
|
||||
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_file_download_request")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentFileDownloadRequestPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200,
|
||||
"Download URI allocated",
|
||||
inner_api_ns.models[AgentFileDownloadRequestResponse.__name__],
|
||||
)
|
||||
@with_session(write=False)
|
||||
def post(self, session: Session) -> dict[str, object]:
|
||||
try:
|
||||
payload = AgentFileDownloadRequestPayload.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="invalid_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
if application_services().workspaces.management.get(payload.tenant_id) is None:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="tenant_not_found",
|
||||
description="tenant not found",
|
||||
status_code=404,
|
||||
)
|
||||
try:
|
||||
result = FileRequestService().request_download(
|
||||
tenant_id=payload.tenant_id,
|
||||
user_id=payload.user_id,
|
||||
user_from=payload.user_from,
|
||||
invoke_from=payload.invoke_from,
|
||||
file_mapping=payload.file.model_dump(mode="python", exclude_none=True),
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise AgentFileRequestHttpError(
|
||||
error_code="file_not_accessible",
|
||||
description=str(exc),
|
||||
status_code=404,
|
||||
) from exc
|
||||
|
||||
download_uri = result.download_uri
|
||||
if payload.for_frontend:
|
||||
download_uri = bind_file_uri(download_uri, dify_config.FILES_URL)
|
||||
|
||||
return AgentFileDownloadRequestResponse(
|
||||
filename=result.filename,
|
||||
mime_type=result.mime_type,
|
||||
size=result.size,
|
||||
download_uri=download_uri,
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AgentFileDownloadRequestApi",
|
||||
"AgentFileDownloadRequestPayload",
|
||||
"AgentFileDownloadRequestResponse",
|
||||
"AgentFileUploadRequestApi",
|
||||
"AgentFileUploadRequestPayload",
|
||||
"AgentFileUploadRequestResponse",
|
||||
]
|
||||
@@ -0,0 +1,102 @@
|
||||
"""Private authorization bridge for run-scoped, read-only Agent knowledge CLI."""
|
||||
|
||||
from dify_agent.protocol.knowledge_fs import KnowledgeFsError, KnowledgeFsPreparedRequest, KnowledgeFsPrepareRequest
|
||||
from dify_agent.protocol.knowledge_investigation import KnowledgeInvestigationPayload
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
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 fields.base import ResponseModel
|
||||
from services.agent.knowledge_gateway import AgentKnowledgeGateway
|
||||
from services.agent_config_service import AgentConfigServiceError
|
||||
from services.knowledge_fs.app_admission_service import KnowledgeFSAppAdmissionError
|
||||
from services.knowledge_fs.product_authorization import KnowledgeFSProductNotFoundError
|
||||
from services.knowledge_fs.product_remote import KnowledgeFSOperationUnavailableError
|
||||
from services.knowledge_fs.query_images import KnowledgeFSQueryImageError
|
||||
from services.knowledge_fs_capability import KnowledgeFSCapabilityConfigurationError, KnowledgeFSCapabilityPolicyError
|
||||
|
||||
register_schema_models(
|
||||
inner_api_ns, KnowledgeFsPrepareRequest, KnowledgeFsPreparedRequest, KnowledgeInvestigationPayload
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/knowledge/prepare")
|
||||
class AgentKnowledgePrepareApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_knowledge_prepare")
|
||||
@inner_api_ns.expect(inner_api_ns.models[KnowledgeFsPrepareRequest.__name__])
|
||||
@inner_api_ns.response(200, "Private prepared command", inner_api_ns.models[KnowledgeFsPreparedRequest.__name__])
|
||||
def post(self):
|
||||
try:
|
||||
payload = KnowledgeFsPrepareRequest.model_validate(request.get_json(silent=True) or {})
|
||||
return AgentKnowledgeGateway().prepare(payload).model_dump(mode="json", exclude_none=True)
|
||||
except ValidationError:
|
||||
return {
|
||||
"code": "KNOWLEDGE_INVALID_REQUEST",
|
||||
"message": "Invalid knowledge command or execution context.",
|
||||
}, 400
|
||||
except KnowledgeFsError as exc:
|
||||
return {"code": exc.code, "message": exc.message, "status": exc.status_code}, exc.status_code
|
||||
except (
|
||||
KnowledgeFSAppAdmissionError,
|
||||
KnowledgeFSProductNotFoundError,
|
||||
AgentConfigServiceError,
|
||||
KnowledgeFSCapabilityPolicyError,
|
||||
PermissionError,
|
||||
):
|
||||
return {"code": "KNOWLEDGE_ACCESS_DENIED", "message": "Knowledge access is unavailable or revoked."}, 403
|
||||
except (KnowledgeFSOperationUnavailableError, KnowledgeFSCapabilityConfigurationError):
|
||||
return {"code": "KNOWLEDGE_UNAVAILABLE", "message": "KnowledgeFS capability is unavailable."}, 503
|
||||
except (KnowledgeFSQueryImageError, ValueError):
|
||||
return {
|
||||
"code": "KNOWLEDGE_INVALID_REQUEST",
|
||||
"message": "Query image or knowledge reference is unavailable.",
|
||||
}, 400
|
||||
|
||||
|
||||
class KnowledgeInvestigationAcceptedResponse(ResponseModel):
|
||||
accepted: bool
|
||||
|
||||
|
||||
register_response_schema_models(inner_api_ns, KnowledgeInvestigationAcceptedResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/knowledge/investigations")
|
||||
class AgentKnowledgeInvestigationApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_knowledge_investigation")
|
||||
@inner_api_ns.response(
|
||||
202, "Investigation queued", inner_api_ns.models[KnowledgeInvestigationAcceptedResponse.__name__]
|
||||
)
|
||||
@inner_api_ns.expect(inner_api_ns.models[KnowledgeInvestigationPayload.__name__])
|
||||
def post(self):
|
||||
try:
|
||||
payload = KnowledgeInvestigationPayload.model_validate(request.get_json(silent=True) or {})
|
||||
AgentKnowledgeGateway().authorize_investigation(payload)
|
||||
from tasks.knowledge_fs_agent_investigation_tasks import capture_agent_knowledge_investigation_task
|
||||
|
||||
# Per-space jobs allow one unavailable knowledge base to fail independently.
|
||||
# An uncertain broker acknowledgement can be retried with the same investigation ID.
|
||||
for space_id in sorted({str(a.control_space_id) for a in payload.attempts}):
|
||||
capture_agent_knowledge_investigation_task.delay(
|
||||
report=payload.model_dump(mode="json"),
|
||||
control_space_id=space_id,
|
||||
)
|
||||
return KnowledgeInvestigationAcceptedResponse(accepted=True).model_dump(mode="json"), 202
|
||||
except ValidationError:
|
||||
return {"code": "KNOWLEDGE_INVALID_REQUEST", "message": "Invalid investigation."}, 400
|
||||
except KnowledgeFsError as exc:
|
||||
return {"code": exc.code, "message": exc.message, "status": exc.status_code}, exc.status_code
|
||||
except (
|
||||
AgentConfigServiceError,
|
||||
KnowledgeFSAppAdmissionError,
|
||||
KnowledgeFSCapabilityPolicyError,
|
||||
PermissionError,
|
||||
):
|
||||
return {"code": "KNOWLEDGE_ACCESS_DENIED", "message": "Knowledge access is unavailable or revoked."}, 403
|
||||
@@ -0,0 +1,119 @@
|
||||
"""Trusted streaming LLM gateway for dify-agent runs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from collections.abc import Generator
|
||||
|
||||
from flask import Response, stream_with_context
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
|
||||
from controllers.common.schema import register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import agent_inner_api_only
|
||||
from core.errors.error import QuotaExceededError
|
||||
from libs.exception import BaseHTTPException
|
||||
from services.agent_llm_inner_service import AgentLLMInnerService, AgentLLMInnerServiceError
|
||||
from services.entities.agent_llm_inner import AgentLLMInvokeRequest
|
||||
|
||||
|
||||
class AgentLLMInvokeHttpError(BaseHTTPException):
|
||||
error_code = "agent_llm_invoke_failed"
|
||||
description = "Agent LLM invocation failed."
|
||||
code = 500
|
||||
|
||||
def __init__(self, *, error_code: str, description: str, status_code: int) -> None:
|
||||
self.error_code = error_code
|
||||
self.description = description
|
||||
self.code = status_code
|
||||
super().__init__(description)
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, AgentLLMInvokeRequest)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/llm/invoke")
|
||||
class AgentLLMInvokeApi(Resource):
|
||||
"""Resolve one dify-agent model request before proxying it."""
|
||||
|
||||
@agent_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_llm_invoke")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentLLMInvokeRequest.__name__])
|
||||
@inner_api_ns.produces(["text/event-stream"])
|
||||
def post(self) -> Response:
|
||||
try:
|
||||
payload = AgentLLMInvokeRequest.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise AgentLLMInvokeHttpError(
|
||||
error_code="invalid_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
service = AgentLLMInnerService()
|
||||
try:
|
||||
prepared = service.prepare(payload)
|
||||
except AgentLLMInnerServiceError as exc:
|
||||
raise AgentLLMInvokeHttpError(
|
||||
error_code=exc.error_code,
|
||||
description=exc.description,
|
||||
status_code=exc.status_code,
|
||||
) from exc
|
||||
except QuotaExceededError as exc:
|
||||
raise AgentLLMInvokeHttpError(
|
||||
error_code="agent_llm_quota_exceeded",
|
||||
description=str(exc) or "Insufficient Message Credits.",
|
||||
status_code=429,
|
||||
) from exc
|
||||
except ValueError as exc:
|
||||
raise AgentLLMInvokeHttpError(
|
||||
error_code="invalid_model_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
def generate() -> Generator[str]:
|
||||
try:
|
||||
for chunk in service.invoke(prepared):
|
||||
# Agent owns its history. Do not reserialize opaque request
|
||||
# messages through the runtime's typed PromptMessage field.
|
||||
envelope = {
|
||||
"code": 0,
|
||||
"message": "",
|
||||
"data": chunk.model_dump(mode="json", exclude={"prompt_messages"}),
|
||||
}
|
||||
yield f"data: {json.dumps(envelope, ensure_ascii=False, separators=(',', ':'))}\n\n"
|
||||
except GeneratorExit:
|
||||
raise
|
||||
except QuotaExceededError as exc:
|
||||
error = {
|
||||
"error_type": "AgentLLMQuotaExceededError",
|
||||
"message": str(exc) or "Insufficient hosted model quota remaining.",
|
||||
}
|
||||
envelope = {
|
||||
"code": -429,
|
||||
"message": json.dumps(error, ensure_ascii=False, separators=(",", ":")),
|
||||
"data": None,
|
||||
}
|
||||
yield f"data: {json.dumps(envelope, ensure_ascii=False, separators=(',', ':'))}\n\n"
|
||||
except Exception as exc:
|
||||
error = {
|
||||
"error_type": type(exc).__name__,
|
||||
"message": str(exc) or "Agent LLM invocation failed.",
|
||||
}
|
||||
envelope = {
|
||||
"code": -500,
|
||||
"message": json.dumps(error, ensure_ascii=False, separators=(",", ":")),
|
||||
"data": None,
|
||||
}
|
||||
yield f"data: {json.dumps(envelope, ensure_ascii=False, separators=(',', ':'))}\n\n"
|
||||
|
||||
return Response(
|
||||
stream_with_context(generate()), # pyrefly: ignore[no-matching-overload]
|
||||
content_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||
)
|
||||
|
||||
|
||||
__all__ = ["AgentLLMInvokeApi", "AgentLLMInvokeHttpError"]
|
||||
@@ -0,0 +1,100 @@
|
||||
"""Trusted ingestion for independently persisted E2B execution usage."""
|
||||
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
||||
|
||||
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import agent_inner_api_only
|
||||
from fields.base import ResponseModel
|
||||
from libs.exception import BaseHTTPException
|
||||
from libs.helper import dump_response
|
||||
from services.agent.runtime_usage_service import SandboxUsageError, SandboxUsageEvent, SandboxUsageService
|
||||
|
||||
|
||||
class SandboxUsageHttpError(BaseHTTPException):
|
||||
error_code = "sandbox_usage_invalid_request"
|
||||
description = "Invalid sandbox usage request."
|
||||
code = 400
|
||||
|
||||
def __init__(self, error: SandboxUsageError | None = None) -> None:
|
||||
if error is not None:
|
||||
self.error_code = error.code
|
||||
self.description = error.code
|
||||
self.code = error.status_code
|
||||
super().__init__(self.description)
|
||||
|
||||
|
||||
class SandboxUsagePayload(BaseModel):
|
||||
project_id: str = Field(min_length=1, max_length=128)
|
||||
events: list[SandboxUsageEvent] = Field(min_length=1, max_length=100)
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class SandboxUsageQuery(BaseModel):
|
||||
project_id: str = Field(min_length=1, max_length=128)
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
|
||||
class SandboxUsageResponse(ResponseModel):
|
||||
accepted: int
|
||||
duplicates: int
|
||||
conflicts: int
|
||||
ignored: int
|
||||
|
||||
|
||||
class SandboxUsageDiagnosticsResponse(ResponseModel):
|
||||
unresolved_events: int = 0
|
||||
conflict_events: int = 0
|
||||
open_executions: int = 0
|
||||
unattributed_executions: int = 0
|
||||
|
||||
|
||||
class SandboxUsageStateResponse(ResponseModel):
|
||||
enabled: bool
|
||||
project_id: str
|
||||
started_at: str | None
|
||||
checkpoint_at: str | None
|
||||
full_scan_at: str | None
|
||||
diagnostics: SandboxUsageDiagnosticsResponse
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, SandboxUsagePayload, SandboxUsageQuery)
|
||||
register_response_schema_models(inner_api_ns, SandboxUsageResponse, SandboxUsageStateResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/sandbox-usage/events")
|
||||
class SandboxUsageEventsApi(Resource):
|
||||
@agent_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_sandbox_usage_events")
|
||||
@inner_api_ns.expect(inner_api_ns.models[SandboxUsagePayload.__name__])
|
||||
@inner_api_ns.response(200, "Durably committed", inner_api_ns.models[SandboxUsageResponse.__name__])
|
||||
def post(self):
|
||||
request.max_content_length = 1024 * 1024
|
||||
try:
|
||||
payload = SandboxUsagePayload.model_validate(inner_api_ns.payload or {})
|
||||
result = SandboxUsageService.ingest(project_id=payload.project_id, events=payload.events)
|
||||
except ValidationError as exc:
|
||||
raise SandboxUsageHttpError() from exc
|
||||
except SandboxUsageError as exc:
|
||||
raise SandboxUsageHttpError(exc) from exc
|
||||
return dump_response(SandboxUsageResponse, result)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/sandbox-usage/state")
|
||||
class SandboxUsageStateApi(Resource):
|
||||
@agent_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_sandbox_usage_state", params=query_params_from_model(SandboxUsageQuery))
|
||||
@inner_api_ns.response(
|
||||
200, "Persisted activation and completed scans", inner_api_ns.models[SandboxUsageStateResponse.__name__]
|
||||
)
|
||||
def get(self):
|
||||
try:
|
||||
query = SandboxUsageQuery.model_validate(request.args.to_dict(flat=True))
|
||||
result = SandboxUsageService.get_state(project_id=query.project_id)
|
||||
except ValidationError as exc:
|
||||
raise SandboxUsageHttpError() from exc
|
||||
except SandboxUsageError as exc:
|
||||
raise SandboxUsageHttpError(exc) from exc
|
||||
return dump_response(SandboxUsageStateResponse, result)
|
||||
@@ -0,0 +1,70 @@
|
||||
"""Inner API endpoint for Agent core tool invocation."""
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.console.app.wraps import with_session
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import agent_inner_api_only
|
||||
from libs.exception import BaseHTTPException
|
||||
from services.agent_tool_inner_service import AgentToolInnerService
|
||||
from services.entities.agent_tool_inner import AgentToolInvokeRequest, AgentToolInvokeResponse
|
||||
from services.errors.agent_tool_inner import AgentToolInnerServiceError
|
||||
|
||||
|
||||
class AgentToolInvokeHttpError(BaseHTTPException):
|
||||
error_code = "agent_tool_invoke_failed"
|
||||
description = "Agent tool invocation failed."
|
||||
code = 500
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
error_code: str | None = None,
|
||||
description: str | None = None,
|
||||
status_code: int | None = None,
|
||||
) -> None:
|
||||
if error_code is not None:
|
||||
self.error_code = error_code
|
||||
if description is not None:
|
||||
self.description = description
|
||||
if status_code is not None:
|
||||
self.code = status_code
|
||||
super().__init__(self.description)
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, AgentToolInvokeRequest)
|
||||
register_response_schema_models(inner_api_ns, AgentToolInvokeResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/tools/invoke")
|
||||
class AgentToolInvokeApi(Resource):
|
||||
"""Invoke one Agent tool through the API-owned core tool runtime path."""
|
||||
|
||||
@agent_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_tool_invoke")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentToolInvokeRequest.__name__])
|
||||
@inner_api_ns.response(200, "Tool invoked successfully", inner_api_ns.models[AgentToolInvokeResponse.__name__])
|
||||
@with_session
|
||||
def post(self, session: Session) -> dict[str, object]:
|
||||
try:
|
||||
payload = AgentToolInvokeRequest.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise AgentToolInvokeHttpError(
|
||||
error_code="invalid_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
try:
|
||||
response = AgentToolInnerService().invoke(session=session, request=payload)
|
||||
except AgentToolInnerServiceError as exc:
|
||||
raise AgentToolInvokeHttpError(
|
||||
error_code=exc.error_code,
|
||||
description=exc.description,
|
||||
status_code=exc.status_code,
|
||||
) from exc
|
||||
|
||||
return response.model_dump(mode="json")
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Authenticated, idempotent ingestion of Agent observations."""
|
||||
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from dify_agent.protocol.tracing import AgentTraceBatch
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from werkzeug.exceptions import BadRequest, Conflict, NotFound, RequestEntityTooLarge
|
||||
|
||||
from configs import dify_config
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import agent_inner_api_only
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from services.tracing.errors import (
|
||||
TraceCollectionConflictError,
|
||||
TraceCollectionError,
|
||||
TraceCollectionLimitError,
|
||||
TraceCollectionNotFoundError,
|
||||
)
|
||||
|
||||
|
||||
class AgentTraceBatchResponse(ResponseModel):
|
||||
result: Literal["success"]
|
||||
batch_id: UUID
|
||||
accepted_spans: int
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, AgentTraceBatch)
|
||||
register_response_schema_models(inner_api_ns, AgentTraceBatchResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent/tracing/batches")
|
||||
class AgentTraceBatchesApi(Resource):
|
||||
@agent_inner_api_only
|
||||
@inner_api_ns.doc("inner_agent_trace_batches")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentTraceBatch.__name__])
|
||||
@inner_api_ns.response(200, "Durably accepted", inner_api_ns.models[AgentTraceBatchResponse.__name__])
|
||||
def post(self):
|
||||
request.max_content_length = dify_config.OPS_AGENT_TRACE_BATCH_MAX_BYTES
|
||||
try:
|
||||
payload = AgentTraceBatch.model_validate(inner_api_ns.payload or {})
|
||||
accepted = application_services().tracing.agent_traces.ingest(payload)
|
||||
except ValidationError as error:
|
||||
raise BadRequest("Invalid Agent trace batch") from error
|
||||
except TraceCollectionNotFoundError as error:
|
||||
raise NotFound(error.code) from error
|
||||
except TraceCollectionConflictError as error:
|
||||
raise Conflict(error.code) from error
|
||||
except TraceCollectionLimitError as error:
|
||||
raise RequestEntityTooLarge(error.code) from error
|
||||
except TraceCollectionError as error:
|
||||
raise BadRequest(error.code) from error
|
||||
return AgentTraceBatchResponse(result="success", batch_id=payload.batch_id, accepted_spans=accepted).model_dump(
|
||||
mode="json"
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
"""Inner API endpoints for the enterprise admin app list and app detail.
|
||||
|
||||
Called by the enterprise admin-api service. The list covers Studio-visible apps
|
||||
across all workspaces with their publish state and invocation count; enterprise
|
||||
joins its own data onto it by app id. The detail adds what only CE owns: the
|
||||
app's published version, access points, and invocations.
|
||||
"""
|
||||
|
||||
from http import HTTPStatus
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, Field, ValidationError, field_validator
|
||||
from werkzeug.exceptions import BadRequest
|
||||
|
||||
from controllers.common.fields import SimpleMessageResponse
|
||||
from controllers.common.schema import (
|
||||
query_params_from_model,
|
||||
query_params_from_request,
|
||||
register_response_schema_models,
|
||||
)
|
||||
from controllers.console.wraps import setup_required
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import enterprise_inner_api_only
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from libs.helper import to_timestamp
|
||||
from models.model import AppMode
|
||||
from services.app.enterprise_app_service import (
|
||||
EnterpriseAppApi,
|
||||
EnterpriseAppCreator,
|
||||
EnterpriseAppListParams,
|
||||
EnterpriseAppMCPServer,
|
||||
EnterpriseAppPublishedVersion,
|
||||
EnterpriseAppSite,
|
||||
EnterpriseAppTrigger,
|
||||
)
|
||||
|
||||
EnterpriseAppListMode = Literal["completion", "workflow", "chat", "advanced-chat", "agent-chat", "agent"]
|
||||
APP_LIST_QUERY_ARRAY_FIELDS = ("workspace_ids", "modes")
|
||||
|
||||
|
||||
class EnterpriseAppListQuery(BaseModel):
|
||||
page: int = Field(default=1, ge=1, le=99999, description="Page number (1-99999)")
|
||||
limit: int = Field(default=20, ge=1, le=100, description="Page size (1-100)")
|
||||
workspace_ids: list[UUID] = Field(default_factory=list, description="Filter by workspace IDs (repeatable)")
|
||||
modes: list[EnterpriseAppListMode] = Field(default_factory=list, description="Filter by app modes (repeatable)")
|
||||
name: str | None = Field(default=None, max_length=255, description="Filter by app name substring")
|
||||
sort_by: Literal["created_at", "name"] = Field(default="created_at", description="Sort field")
|
||||
sort_order: Literal["asc", "desc"] = Field(default="desc", description="Sort direction")
|
||||
|
||||
@field_validator("name", mode="before")
|
||||
@classmethod
|
||||
def blank_name_as_none(cls, value: object) -> object:
|
||||
return value.strip() or None if isinstance(value, str) else value
|
||||
|
||||
|
||||
class EnterpriseAppCreatorResponse(ResponseModel):
|
||||
id: str
|
||||
name: str
|
||||
email: str
|
||||
|
||||
|
||||
class EnterpriseAppResponse(ResponseModel):
|
||||
id: str
|
||||
name: str
|
||||
description: str
|
||||
mode: str
|
||||
icon_type: str | None
|
||||
icon: str | None
|
||||
icon_background: str | None
|
||||
workspace_id: str
|
||||
workspace_name: str | None
|
||||
created_by: EnterpriseAppCreatorResponse | None
|
||||
created_at: int
|
||||
updated_at: int
|
||||
|
||||
|
||||
_INVOCATION_COUNT_DESCRIPTION = (
|
||||
"Invocations in the last 24h: published workflow runs (app-run, webhook, schedule, plugin) for "
|
||||
"workflow and advanced-chat apps, non-debugger messages for other apps"
|
||||
)
|
||||
|
||||
|
||||
_TRIGGER_ENTRY_ACCESS_DESCRIPTION = (
|
||||
"Null when the published workflow has a trigger entry, since trigger-entry workflows cannot be run through "
|
||||
"Web App, Service API, or MCP"
|
||||
)
|
||||
|
||||
|
||||
class EnterpriseAppListItemResponse(EnterpriseAppResponse):
|
||||
published: bool = Field(description="Whether the app detail reports a published_version")
|
||||
invocation_count_24h: int = Field(description=_INVOCATION_COUNT_DESCRIPTION)
|
||||
|
||||
|
||||
class EnterpriseAppPaginationResponse(ResponseModel):
|
||||
page: int
|
||||
limit: int
|
||||
total: int
|
||||
has_more: bool
|
||||
data: list[EnterpriseAppListItemResponse]
|
||||
|
||||
|
||||
class EnterpriseAppPublishedVersionResponse(ResponseModel):
|
||||
id: str
|
||||
version: str | None = Field(description="Workflow version string; null for Agent config snapshots")
|
||||
version_number: int | None = Field(
|
||||
description="The #N shown in Studio version history; null for unnumbered legacy workflows"
|
||||
)
|
||||
marked_name: str | None = Field(description="Workflow version name; null for Agent config snapshots")
|
||||
marked_comment: str | None = Field(description="Workflow version comment or Agent snapshot version note")
|
||||
created_by: EnterpriseAppCreatorResponse | None
|
||||
published_at: int
|
||||
|
||||
|
||||
class EnterpriseAppSiteResponse(ResponseModel):
|
||||
enabled: bool
|
||||
code: str | None
|
||||
app_base_url: str | None
|
||||
|
||||
|
||||
class EnterpriseAppApiResponse(ResponseModel):
|
||||
enabled: bool
|
||||
base_url: str | None
|
||||
api_key_count: int
|
||||
|
||||
|
||||
class EnterpriseAppMCPServerResponse(ResponseModel):
|
||||
status: str
|
||||
server_code: str
|
||||
|
||||
|
||||
class EnterpriseAppTriggerResponse(ResponseModel):
|
||||
id: str
|
||||
node_id: str | None
|
||||
title: str
|
||||
trigger_type: str
|
||||
provider_name: str | None
|
||||
status: str
|
||||
webhook_url: str | None
|
||||
|
||||
|
||||
class EnterpriseAppDetailResponse(EnterpriseAppResponse):
|
||||
published_version: EnterpriseAppPublishedVersionResponse | None = Field(
|
||||
description=(
|
||||
"Version the app serves, unaffected by later unpublished draft edits; null when never published, "
|
||||
"and always for chat, completion, and agent-chat apps"
|
||||
)
|
||||
)
|
||||
published_version_count: int = Field(
|
||||
description="Number of entries in the app's Studio version history; 0 for apps without versions"
|
||||
)
|
||||
site: EnterpriseAppSiteResponse | None = Field(description=_TRIGGER_ENTRY_ACCESS_DESCRIPTION)
|
||||
api: EnterpriseAppApiResponse | None = Field(description=_TRIGGER_ENTRY_ACCESS_DESCRIPTION)
|
||||
mcp_server: EnterpriseAppMCPServerResponse | None = Field(
|
||||
description=f"{_TRIGGER_ENTRY_ACCESS_DESCRIPTION}, or when no MCP server was ever created"
|
||||
)
|
||||
triggers: list[EnterpriseAppTriggerResponse]
|
||||
last_invoked_at: int | None = Field(
|
||||
description="Latest invocation counted like invocation_count_24h, without the 24h limit"
|
||||
)
|
||||
invocation_count_24h: int = Field(description=_INVOCATION_COUNT_DESCRIPTION)
|
||||
failed_invocation_count_24h: int = Field(
|
||||
description=(
|
||||
"Invocations counted in invocation_count_24h that ended failed or partial-succeeded (workflow runs) "
|
||||
"or in error (messages); running and stopped runs are not failures"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
register_response_schema_models(inner_api_ns, EnterpriseAppPaginationResponse, EnterpriseAppDetailResponse)
|
||||
|
||||
|
||||
def _creator_response(creator: EnterpriseAppCreator | None) -> EnterpriseAppCreatorResponse | None:
|
||||
if creator is None:
|
||||
return None
|
||||
return EnterpriseAppCreatorResponse(id=creator.id, name=creator.name, email=creator.email)
|
||||
|
||||
|
||||
def _published_version_response(
|
||||
version: EnterpriseAppPublishedVersion | None,
|
||||
) -> EnterpriseAppPublishedVersionResponse | None:
|
||||
if version is None:
|
||||
return None
|
||||
return EnterpriseAppPublishedVersionResponse(
|
||||
id=version.id,
|
||||
version=version.version,
|
||||
version_number=version.version_number,
|
||||
marked_name=version.marked_name,
|
||||
marked_comment=version.marked_comment,
|
||||
created_by=_creator_response(version.created_by),
|
||||
published_at=to_timestamp(version.published_at),
|
||||
)
|
||||
|
||||
|
||||
def _site_response(site: EnterpriseAppSite | None) -> EnterpriseAppSiteResponse | None:
|
||||
if site is None:
|
||||
return None
|
||||
return EnterpriseAppSiteResponse(enabled=site.enabled, code=site.code, app_base_url=site.app_base_url)
|
||||
|
||||
|
||||
def _api_response(api: EnterpriseAppApi | None) -> EnterpriseAppApiResponse | None:
|
||||
if api is None:
|
||||
return None
|
||||
return EnterpriseAppApiResponse(enabled=api.enabled, base_url=api.base_url, api_key_count=api.api_key_count)
|
||||
|
||||
|
||||
def _mcp_server_response(server: EnterpriseAppMCPServer | None) -> EnterpriseAppMCPServerResponse | None:
|
||||
if server is None:
|
||||
return None
|
||||
return EnterpriseAppMCPServerResponse(status=server.status, server_code=server.server_code)
|
||||
|
||||
|
||||
def _trigger_response(trigger: EnterpriseAppTrigger) -> EnterpriseAppTriggerResponse:
|
||||
return EnterpriseAppTriggerResponse(
|
||||
id=trigger.id,
|
||||
node_id=trigger.node_id,
|
||||
title=trigger.title,
|
||||
trigger_type=trigger.trigger_type,
|
||||
provider_name=trigger.provider_name,
|
||||
status=trigger.status,
|
||||
webhook_url=trigger.webhook_url,
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/apps")
|
||||
class EnterpriseAppList(Resource):
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@inner_api_ns.doc(
|
||||
"enterprise_app_list",
|
||||
params=query_params_from_model(EnterpriseAppListQuery),
|
||||
responses={HTTPStatus.BAD_REQUEST: "Invalid query parameters"},
|
||||
)
|
||||
@inner_api_ns.response(HTTPStatus.OK, "Success", inner_api_ns.models[EnterpriseAppPaginationResponse.__name__])
|
||||
def get(self):
|
||||
"""List Studio-visible apps across all workspaces."""
|
||||
try:
|
||||
query = query_params_from_request(EnterpriseAppListQuery, list_fields=APP_LIST_QUERY_ARRAY_FIELDS)
|
||||
except ValidationError as exc:
|
||||
raise BadRequest(str(exc)) from exc
|
||||
|
||||
result = application_services().apps.enterprise.list_apps(
|
||||
EnterpriseAppListParams(
|
||||
page=query.page,
|
||||
limit=query.limit,
|
||||
workspace_ids=[str(workspace_id) for workspace_id in query.workspace_ids],
|
||||
modes=[AppMode(mode) for mode in query.modes],
|
||||
name=query.name,
|
||||
sort_by=query.sort_by,
|
||||
sort_order=query.sort_order,
|
||||
)
|
||||
)
|
||||
return EnterpriseAppPaginationResponse(
|
||||
page=result.page,
|
||||
limit=result.per_page,
|
||||
total=result.total,
|
||||
has_more=result.has_next,
|
||||
data=[
|
||||
EnterpriseAppListItemResponse(
|
||||
id=item.app.id,
|
||||
name=item.app.name,
|
||||
description=item.app.description,
|
||||
mode=item.app.mode,
|
||||
icon_type=item.app.icon_type,
|
||||
icon=item.app.icon,
|
||||
icon_background=item.app.icon_background,
|
||||
workspace_id=item.app.workspace_id,
|
||||
workspace_name=item.app.workspace_name,
|
||||
created_by=_creator_response(item.app.created_by),
|
||||
created_at=to_timestamp(item.app.created_at),
|
||||
updated_at=to_timestamp(item.app.updated_at),
|
||||
published=item.published,
|
||||
invocation_count_24h=item.invocation_count,
|
||||
)
|
||||
for item in result.items
|
||||
],
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/apps/<uuid:app_id>")
|
||||
class EnterpriseAppDetail(Resource):
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@inner_api_ns.doc("enterprise_app_detail", responses={HTTPStatus.NOT_FOUND: "App not found"})
|
||||
@inner_api_ns.response(HTTPStatus.OK, "Success", inner_api_ns.models[EnterpriseAppDetailResponse.__name__])
|
||||
def get(self, app_id: UUID):
|
||||
"""Get a Studio-visible app with its published version, access points, and invocations."""
|
||||
detail = application_services().apps.enterprise.get_app(str(app_id))
|
||||
if detail is None:
|
||||
return SimpleMessageResponse(message="app not found").model_dump(mode="json"), HTTPStatus.NOT_FOUND
|
||||
|
||||
app = detail.app
|
||||
return EnterpriseAppDetailResponse(
|
||||
id=app.id,
|
||||
name=app.name,
|
||||
description=app.description,
|
||||
mode=app.mode,
|
||||
icon_type=app.icon_type,
|
||||
icon=app.icon,
|
||||
icon_background=app.icon_background,
|
||||
workspace_id=app.workspace_id,
|
||||
workspace_name=app.workspace_name,
|
||||
created_by=_creator_response(app.created_by),
|
||||
created_at=to_timestamp(app.created_at),
|
||||
updated_at=to_timestamp(app.updated_at),
|
||||
published_version=_published_version_response(detail.published_version),
|
||||
published_version_count=detail.published_version_count,
|
||||
site=_site_response(detail.site),
|
||||
api=_api_response(detail.api),
|
||||
mcp_server=_mcp_server_response(detail.mcp_server),
|
||||
triggers=[_trigger_response(trigger) for trigger in detail.triggers],
|
||||
last_invoked_at=to_timestamp(detail.last_invoked_at),
|
||||
invocation_count_24h=detail.invocation_count,
|
||||
failed_invocation_count_24h=detail.failed_invocation_count,
|
||||
).model_dump(mode="json")
|
||||
@@ -0,0 +1,179 @@
|
||||
"""Inner API endpoints for app DSL import/export.
|
||||
|
||||
Called by the enterprise admin-api service. Import requires ``creator_email``
|
||||
to attribute the created app; workspace/membership validation is done by the
|
||||
Go admin-api caller.
|
||||
"""
|
||||
|
||||
from http import HTTPStatus
|
||||
from uuid import UUID
|
||||
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, Field, RootModel, ValidationError, field_validator
|
||||
from werkzeug.exceptions import Forbidden
|
||||
|
||||
from controllers.common.fields import SimpleMessageResponse
|
||||
from controllers.common.schema import (
|
||||
query_params_from_model,
|
||||
register_response_schema_model,
|
||||
register_response_schema_models,
|
||||
register_schema_model,
|
||||
)
|
||||
from controllers.console.wraps import setup_required, validate_request
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import enterprise_inner_api_only
|
||||
from core.logging.context import get_request_id, get_trace_id
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.app_fields import AppExportResponse
|
||||
from fields.base import ResponseModel
|
||||
from libs.helper import dump_response
|
||||
from services.entities.app_entities import AppExportOptions
|
||||
from services.entities.dsl_entities import AppImportParams, Import, ImportMode, ImportStatus
|
||||
from services.errors.app import AppDiscoveryNotFoundError, IsDraftWorkflowError, WorkflowNotFoundError
|
||||
from services.errors.base import NoPermissionError
|
||||
|
||||
|
||||
class InnerAppDSLImportPayload(BaseModel):
|
||||
yaml_content: str = Field(description="YAML DSL content")
|
||||
creator_email: str = Field(description="Email of the workspace member who will own the imported app")
|
||||
name: str | None = Field(default=None, description="Override app name from DSL")
|
||||
description: str | None = Field(default=None, description="Override app description from DSL")
|
||||
|
||||
|
||||
class EnterpriseAppDSLExportQuery(BaseModel):
|
||||
include_secret: bool = Field(default=False, description="Whether to include secret values in the exported DSL")
|
||||
workflow_id: UUID | None = Field(default=None, description="Published workflow version ID to export")
|
||||
|
||||
@field_validator("include_secret", mode="before")
|
||||
@classmethod
|
||||
def parse_include_secret(cls, value: object) -> bool:
|
||||
if isinstance(value, str):
|
||||
return value.lower() == "true"
|
||||
return bool(value)
|
||||
|
||||
|
||||
class EnterpriseAppDSLWorkflowErrorResponse(ResponseModel):
|
||||
code: str
|
||||
message: str
|
||||
status: int
|
||||
|
||||
|
||||
class EnterpriseAppDSLExportNotFoundResponse(RootModel[SimpleMessageResponse | EnterpriseAppDSLWorkflowErrorResponse]):
|
||||
"""Export 404: either an app-level message or a workflow-version error."""
|
||||
|
||||
|
||||
register_schema_model(inner_api_ns, InnerAppDSLImportPayload)
|
||||
register_response_schema_models(inner_api_ns, Import, AppExportResponse, EnterpriseAppDSLWorkflowErrorResponse)
|
||||
register_response_schema_model(inner_api_ns, SimpleMessageResponse)
|
||||
register_response_schema_model(inner_api_ns, EnterpriseAppDSLExportNotFoundResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/workspaces/<string:workspace_id>/dsl/import")
|
||||
class EnterpriseAppDSLImport(Resource):
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@inner_api_ns.doc("enterprise_app_dsl_import")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerAppDSLImportPayload.__name__])
|
||||
@inner_api_ns.response(HTTPStatus.OK, "Import completed", inner_api_ns.models[Import.__name__])
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.ACCEPTED,
|
||||
"Import pending (DSL version mismatch requires confirmation)",
|
||||
inner_api_ns.models[Import.__name__],
|
||||
)
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.BAD_REQUEST, "Import failed (business error)", inner_api_ns.models[Import.__name__]
|
||||
)
|
||||
@inner_api_ns.response(HTTPStatus.FORBIDDEN, "Import permission denied or app quota exceeded")
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.NOT_FOUND,
|
||||
"Creator account not found or inactive",
|
||||
inner_api_ns.models[SimpleMessageResponse.__name__],
|
||||
)
|
||||
def post(self, workspace_id: str):
|
||||
"""Import a DSL into a workspace on behalf of a specified creator."""
|
||||
args = validate_request(InnerAppDSLImportPayload)
|
||||
try:
|
||||
result = application_services().apps.imports.import_as_creator(
|
||||
workspace_id=workspace_id,
|
||||
creator_email=args.creator_email,
|
||||
params=AppImportParams(
|
||||
mode=ImportMode.YAML_CONTENT,
|
||||
yaml_content=args.yaml_content,
|
||||
name=args.name,
|
||||
description=args.description,
|
||||
),
|
||||
request_id=get_request_id(),
|
||||
trace_id=get_trace_id(),
|
||||
)
|
||||
except NoPermissionError as error:
|
||||
raise Forbidden(str(error)) from error
|
||||
if result is None:
|
||||
return dump_response(
|
||||
SimpleMessageResponse, {"message": f"account '{args.creator_email}' not found or inactive"}
|
||||
), HTTPStatus.NOT_FOUND
|
||||
|
||||
if result.status == ImportStatus.FAILED:
|
||||
return dump_response(Import, result), HTTPStatus.BAD_REQUEST
|
||||
if result.status == ImportStatus.PENDING:
|
||||
return dump_response(Import, result), HTTPStatus.ACCEPTED
|
||||
return dump_response(Import, result), HTTPStatus.OK
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/apps/<string:app_id>/dsl")
|
||||
class EnterpriseAppDSLExport(Resource):
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@inner_api_ns.doc(
|
||||
"enterprise_app_dsl_export",
|
||||
params=query_params_from_model(EnterpriseAppDSLExportQuery),
|
||||
)
|
||||
@inner_api_ns.response(200, "Export successful", inner_api_ns.models[AppExportResponse.__name__])
|
||||
@inner_api_ns.response(
|
||||
400,
|
||||
"Invalid workflow ID or unpublished workflow version",
|
||||
inner_api_ns.models[EnterpriseAppDSLWorkflowErrorResponse.__name__],
|
||||
)
|
||||
@inner_api_ns.response(
|
||||
404,
|
||||
"App or workflow version not found",
|
||||
inner_api_ns.models[EnterpriseAppDSLExportNotFoundResponse.__name__],
|
||||
)
|
||||
def get(self, app_id: str):
|
||||
"""Export an app's DSL as YAML."""
|
||||
try:
|
||||
query = EnterpriseAppDSLExportQuery.model_validate(request.args.to_dict(flat=True))
|
||||
except ValidationError:
|
||||
return dump_response(
|
||||
EnterpriseAppDSLWorkflowErrorResponse,
|
||||
{
|
||||
"code": "invalid_workflow_id",
|
||||
"message": "workflow_id must be a valid UUID",
|
||||
"status": HTTPStatus.BAD_REQUEST,
|
||||
},
|
||||
), HTTPStatus.BAD_REQUEST
|
||||
|
||||
workflow_id = str(query.workflow_id) if query.workflow_id else None
|
||||
|
||||
try:
|
||||
data = application_services().apps.exports.export_for_inner(
|
||||
app_id, AppExportOptions(include_secret=query.include_secret, workflow_id=workflow_id)
|
||||
)
|
||||
except AppDiscoveryNotFoundError:
|
||||
return dump_response(SimpleMessageResponse, {"message": "app not found"}), HTTPStatus.NOT_FOUND
|
||||
except WorkflowNotFoundError as exc:
|
||||
if not workflow_id:
|
||||
raise
|
||||
return dump_response(
|
||||
EnterpriseAppDSLWorkflowErrorResponse,
|
||||
{"code": "workflow_version_not_found", "message": str(exc), "status": HTTPStatus.NOT_FOUND},
|
||||
), HTTPStatus.NOT_FOUND
|
||||
except IsDraftWorkflowError as exc:
|
||||
if not workflow_id:
|
||||
raise
|
||||
return dump_response(
|
||||
EnterpriseAppDSLWorkflowErrorResponse,
|
||||
{"code": "workflow_version_not_published", "message": str(exc), "status": HTTPStatus.BAD_REQUEST},
|
||||
), HTTPStatus.BAD_REQUEST
|
||||
|
||||
return dump_response(AppExportResponse, {"data": data}), HTTPStatus.OK
|
||||
@@ -0,0 +1,205 @@
|
||||
"""Mint AppDeploy file grants for the enterprise control plane.
|
||||
|
||||
This is the only endpoint that asserts an AppDeploy identity: the application
|
||||
service upserts the subject's ``EndUser`` row, validates the files the caller
|
||||
claims to reference, and signs a short-lived grant.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, Field, ValidationError
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.console.wraps import setup_required
|
||||
from controllers.files.wraps import GrantedFileNotFoundError
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import enterprise_inner_api_only
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from fields.file_grant_fields import ResolvedFileResponse
|
||||
from libs.exception import BaseHTTPException
|
||||
from libs.external_api import error_responses
|
||||
from services.entities.file_grant_entities import FileGrantMintRequest, FileGrantScope, FileKind, FileRef
|
||||
from services.file_grant_service import (
|
||||
AppNotFoundError,
|
||||
EndUserNotFoundError,
|
||||
TooManyFileRefsError,
|
||||
)
|
||||
from services.file_grant_service import (
|
||||
GrantedFileNotFoundError as ServiceGrantedFileNotFoundError,
|
||||
)
|
||||
from services.file_grant_service import (
|
||||
GrantTtlTooLongError as ServiceGrantTtlTooLongError,
|
||||
)
|
||||
from services.file_grant_service import (
|
||||
InvalidGrantRequestError as ServiceInvalidGrantRequestError,
|
||||
)
|
||||
from services.file_grant_service import (
|
||||
InvalidSubjectError as ServiceInvalidSubjectError,
|
||||
)
|
||||
|
||||
|
||||
class InvalidGrantRequestError(BaseHTTPException):
|
||||
error_code = "invalid_request"
|
||||
description = "The file grant request is malformed."
|
||||
code = 400
|
||||
|
||||
|
||||
class GrantTtlTooLongError(BaseHTTPException):
|
||||
error_code = "grant_ttl_too_long"
|
||||
description = "The requested file grant lifetime exceeds its allowed window."
|
||||
code = 400
|
||||
|
||||
|
||||
class InvalidSubjectError(BaseHTTPException):
|
||||
error_code = "invalid_subject"
|
||||
description = "The subject is empty or contains a NUL byte."
|
||||
code = 400
|
||||
|
||||
|
||||
class GrantAppNotFoundError(BaseHTTPException):
|
||||
error_code = "app_not_found"
|
||||
description = "App not found."
|
||||
code = 404
|
||||
|
||||
|
||||
class FileGrantFileRef(BaseModel):
|
||||
id: str
|
||||
kind: FileKind
|
||||
|
||||
|
||||
class FileGrantMintPayload(BaseModel):
|
||||
tenant_id: str
|
||||
app_id: str
|
||||
subject: str
|
||||
is_anonymous: bool = False
|
||||
scopes: list[FileGrantScope]
|
||||
ttl_seconds: int = Field(gt=0)
|
||||
# Recorded by the enterprise caller's own audit log; it never enters the
|
||||
# grant because a rotated key must not strand the files it uploaded.
|
||||
actor_key_digest: str | None = None
|
||||
file_ids: list[FileGrantFileRef] = Field(default_factory=list)
|
||||
optional_file_ids: list[FileGrantFileRef] = Field(default_factory=list)
|
||||
run_deadline: int | None = None
|
||||
|
||||
|
||||
class FileGrantLimits(ResponseModel):
|
||||
file_size_limit: int
|
||||
image_file_size_limit: int
|
||||
audio_file_size_limit: int
|
||||
video_file_size_limit: int
|
||||
workflow_file_upload_limit: int
|
||||
batch_count_limit: int
|
||||
|
||||
|
||||
class FileGrantFileMetadata(ResponseModel):
|
||||
id: str
|
||||
kind: FileKind
|
||||
name: str
|
||||
size: int
|
||||
extension: str
|
||||
mime_type: str | None = None
|
||||
|
||||
|
||||
class FileGrantMintResponse(ResponseModel):
|
||||
grant: str
|
||||
expires_at: int
|
||||
limits: FileGrantLimits
|
||||
files: list[FileGrantFileMetadata]
|
||||
optional_files: list[ResolvedFileResponse]
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, FileGrantMintPayload)
|
||||
register_response_schema_models(inner_api_ns, FileGrantMintResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/file-grants")
|
||||
class EnterpriseFileGrantApi(Resource):
|
||||
"""Assert one AppDeploy subject and sign a grant for it."""
|
||||
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@error_responses(
|
||||
InvalidGrantRequestError,
|
||||
GrantTtlTooLongError,
|
||||
InvalidSubjectError,
|
||||
GrantAppNotFoundError,
|
||||
GrantedFileNotFoundError,
|
||||
)
|
||||
@inner_api_ns.doc("enterprise_mint_file_grant")
|
||||
@inner_api_ns.expect(inner_api_ns.models[FileGrantMintPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200,
|
||||
"File grant minted",
|
||||
inner_api_ns.models[FileGrantMintResponse.__name__],
|
||||
)
|
||||
def post(self):
|
||||
try:
|
||||
payload = FileGrantMintPayload.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise InvalidGrantRequestError(str(exc)) from exc
|
||||
|
||||
try:
|
||||
result = application_services().file_grants.mint(
|
||||
FileGrantMintRequest(
|
||||
tenant_id=payload.tenant_id,
|
||||
app_id=payload.app_id,
|
||||
subject=payload.subject,
|
||||
is_anonymous=payload.is_anonymous,
|
||||
scopes=tuple(payload.scopes),
|
||||
ttl_seconds=payload.ttl_seconds,
|
||||
file_refs=tuple(FileRef(id=ref.id, kind=ref.kind) for ref in payload.file_ids),
|
||||
optional_file_refs=tuple(FileRef(id=ref.id, kind=ref.kind) for ref in payload.optional_file_ids),
|
||||
run_deadline=payload.run_deadline,
|
||||
)
|
||||
)
|
||||
except AppNotFoundError as exc:
|
||||
raise GrantAppNotFoundError() from exc
|
||||
except ServiceGrantTtlTooLongError as exc:
|
||||
raise GrantTtlTooLongError() from exc
|
||||
except ServiceInvalidSubjectError as exc:
|
||||
raise InvalidSubjectError() from exc
|
||||
except (ServiceInvalidGrantRequestError, TooManyFileRefsError) as exc:
|
||||
raise InvalidGrantRequestError(str(exc)) from exc
|
||||
except (EndUserNotFoundError, ServiceGrantedFileNotFoundError) as exc:
|
||||
raise GrantedFileNotFoundError() from exc
|
||||
|
||||
return FileGrantMintResponse(
|
||||
grant=result.grant,
|
||||
expires_at=result.expires_at,
|
||||
limits=FileGrantLimits(
|
||||
file_size_limit=result.limits.file_size_limit,
|
||||
image_file_size_limit=result.limits.image_file_size_limit,
|
||||
audio_file_size_limit=result.limits.audio_file_size_limit,
|
||||
video_file_size_limit=result.limits.video_file_size_limit,
|
||||
workflow_file_upload_limit=result.limits.workflow_file_upload_limit,
|
||||
batch_count_limit=result.limits.batch_count_limit,
|
||||
),
|
||||
files=[
|
||||
FileGrantFileMetadata(
|
||||
id=file.id,
|
||||
kind=file.kind,
|
||||
name=file.name,
|
||||
size=file.size,
|
||||
extension=file.extension,
|
||||
mime_type=file.mime_type,
|
||||
)
|
||||
for file in result.files
|
||||
],
|
||||
optional_files=[
|
||||
ResolvedFileResponse.from_resolved(ref.id, access)
|
||||
for ref, access in zip(
|
||||
payload.optional_file_ids,
|
||||
result.optional_files,
|
||||
strict=True,
|
||||
)
|
||||
],
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"EnterpriseFileGrantApi",
|
||||
"FileGrantMintPayload",
|
||||
"FileGrantMintResponse",
|
||||
]
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Trusted endpoint for resolving a workspace datasource default to a concrete id."""
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_model
|
||||
from controllers.console.wraps import model_validate
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import plugin_inner_api_only
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from services.data_source.credential_metadata_service import DatasourceCredentialMetadataService
|
||||
|
||||
|
||||
class InnerDatasourceDefaultCredentialPayload(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
tenant_id: str = Field(min_length=1, max_length=512, description="Workspace id.")
|
||||
plugin_id: str = Field(min_length=1, max_length=512, description="Datasource plugin id.")
|
||||
provider: str = Field(min_length=1, max_length=256, description="Datasource provider name.")
|
||||
|
||||
|
||||
class InnerDatasourceCredentialMetadataResponse(ResponseModel):
|
||||
id: str
|
||||
name: str
|
||||
type: str
|
||||
is_default: bool
|
||||
|
||||
|
||||
class InnerDatasourceDefaultCredentialResponse(ResponseModel):
|
||||
credential: InnerDatasourceCredentialMetadataResponse | None
|
||||
|
||||
|
||||
register_schema_model(inner_api_ns, InnerDatasourceDefaultCredentialPayload)
|
||||
register_response_schema_models(
|
||||
inner_api_ns,
|
||||
InnerDatasourceCredentialMetadataResponse,
|
||||
InnerDatasourceDefaultCredentialResponse,
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/datasource-credentials/default")
|
||||
class InnerDatasourceDefaultCredentialApi(Resource):
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc(
|
||||
"resolve_default_datasource_credential",
|
||||
description="Resolve the current workspace default datasource credential to a concrete id.",
|
||||
responses={
|
||||
200: "Default datasource credential resolved.",
|
||||
404: "Invalid plugin inner API key.",
|
||||
},
|
||||
)
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerDatasourceDefaultCredentialPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200,
|
||||
"Default datasource credential resolved.",
|
||||
inner_api_ns.models[InnerDatasourceDefaultCredentialResponse.__name__],
|
||||
)
|
||||
@model_validate(InnerDatasourceDefaultCredentialPayload)
|
||||
def post(self, payload: InnerDatasourceDefaultCredentialPayload):
|
||||
credential = DatasourceCredentialMetadataService(
|
||||
credential_query=application_services().credential_queries
|
||||
).resolve_default(
|
||||
tenant_id=payload.tenant_id,
|
||||
plugin_id=payload.plugin_id,
|
||||
provider=payload.provider,
|
||||
)
|
||||
return {
|
||||
"credential": (
|
||||
{
|
||||
"id": credential.id,
|
||||
"name": credential.name,
|
||||
"type": credential.type,
|
||||
"is_default": credential.is_default,
|
||||
}
|
||||
if credential is not None
|
||||
else None
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
"""Inner knowledge retrieval endpoints."""
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Plugin inner API endpoint for tenant-scoped knowledge retrieval.
|
||||
|
||||
This controller is a thin HTTP wrapper around
|
||||
``services.knowledge_retrieval_inner_service.InnerKnowledgeRetrievalService``.
|
||||
It uses the plugin inner API key because dify-agent calls this endpoint through
|
||||
the same trusted Dify API bridge as other agent/plugin inner calls; tenant-scoped
|
||||
app/dataset validation remains in the service layer.
|
||||
"""
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.console.app.wraps import with_session
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import plugin_inner_api_only
|
||||
from core.workflow.nodes.knowledge_retrieval import exc as retrieval_exc
|
||||
from libs.exception import BaseHTTPException
|
||||
from services.entities.knowledge_retrieval_inner import InnerKnowledgeRetrieveRequest, InnerKnowledgeRetrieveResponse
|
||||
from services.errors.knowledge_retrieval import ExternalKnowledgeRetrievalError, InnerKnowledgeRetrievalServiceError
|
||||
from services.knowledge_retrieval_inner_service import InnerKnowledgeRetrievalService
|
||||
|
||||
|
||||
class InnerKnowledgeRetrievalHttpError(BaseHTTPException):
|
||||
error_code = "knowledge_retrieve_failed"
|
||||
description = "Knowledge retrieval failed."
|
||||
code = 500
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
error_code: str | None = None,
|
||||
description: str | None = None,
|
||||
status_code: int | None = None,
|
||||
) -> None:
|
||||
if error_code is not None:
|
||||
self.error_code = error_code
|
||||
if description is not None:
|
||||
self.description = description
|
||||
if status_code is not None:
|
||||
self.code = status_code
|
||||
super().__init__(self.description)
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, InnerKnowledgeRetrieveRequest)
|
||||
register_response_schema_models(inner_api_ns, InnerKnowledgeRetrieveResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge/retrieve")
|
||||
class InnerKnowledgeRetrieveApi(Resource):
|
||||
"""Retrieve knowledge from one or more datasets within the caller tenant."""
|
||||
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("inner_knowledge_retrieve")
|
||||
@inner_api_ns.doc(description="Retrieve knowledge for trusted internal callers")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerKnowledgeRetrieveRequest.__name__])
|
||||
@inner_api_ns.response(
|
||||
200,
|
||||
"Knowledge retrieved successfully",
|
||||
inner_api_ns.models[InnerKnowledgeRetrieveResponse.__name__],
|
||||
)
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
400: "Invalid request body",
|
||||
403: "Caller tenant does not own the requested resource",
|
||||
404: "Invalid plugin inner API key, app not found, or dataset not found",
|
||||
422: "Invalid retrieval configuration",
|
||||
429: "Knowledge retrieval rate limited",
|
||||
502: "External knowledge retrieval failed",
|
||||
500: "Unexpected knowledge retrieval failure",
|
||||
}
|
||||
)
|
||||
@with_session
|
||||
def post(self, session: Session) -> dict[str, object]:
|
||||
"""Validate the payload, run retrieval, and return workflow-style sources."""
|
||||
try:
|
||||
payload = InnerKnowledgeRetrieveRequest.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as exc:
|
||||
raise InnerKnowledgeRetrievalHttpError(
|
||||
error_code="invalid_request",
|
||||
description=str(exc),
|
||||
status_code=400,
|
||||
) from exc
|
||||
|
||||
try:
|
||||
response = InnerKnowledgeRetrievalService().retrieve(payload, session=session)
|
||||
except InnerKnowledgeRetrievalServiceError as exc:
|
||||
raise InnerKnowledgeRetrievalHttpError(
|
||||
error_code=exc.error_code,
|
||||
description=exc.description,
|
||||
status_code=exc.status_code,
|
||||
) from exc
|
||||
except retrieval_exc.RateLimitExceededError as exc:
|
||||
raise InnerKnowledgeRetrievalHttpError(
|
||||
error_code="knowledge_rate_limited",
|
||||
description=str(exc),
|
||||
status_code=429,
|
||||
) from exc
|
||||
except ExternalKnowledgeRetrievalError as exc:
|
||||
raise InnerKnowledgeRetrievalHttpError(
|
||||
error_code="external_knowledge_failed",
|
||||
description=str(exc),
|
||||
status_code=502,
|
||||
) from exc
|
||||
except ValueError as exc:
|
||||
raise InnerKnowledgeRetrievalHttpError(
|
||||
error_code="retrieval_config_invalid",
|
||||
description=str(exc),
|
||||
status_code=422,
|
||||
) from exc
|
||||
|
||||
return response.model_dump(mode="json", by_alias=True)
|
||||
@@ -0,0 +1 @@
|
||||
"""Trusted KnowledgeFS inner API endpoints."""
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Authenticated broker publication only; never accepts a command or document body."""
|
||||
|
||||
from http import HTTPStatus
|
||||
|
||||
from flask import Response, request
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from werkzeug.exceptions import BadRequest, RequestEntityTooLarge, ServiceUnavailable
|
||||
|
||||
from controllers.common.schema import register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import knowledge_fs_inner_api_only
|
||||
from services.knowledge_fs.background_contract import BackgroundJobPayload
|
||||
from services.knowledge_fs.background_publisher import check_background_publisher_ready, publish_background_job
|
||||
|
||||
register_schema_models(inner_api_ns, BackgroundJobPayload)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/background/jobs")
|
||||
class KnowledgeFSBackgroundJobApi(Resource):
|
||||
@inner_api_ns.expect(inner_api_ns.models[BackgroundJobPayload.__name__])
|
||||
@inner_api_ns.response(202, "Accepted by the Celery broker")
|
||||
@knowledge_fs_inner_api_only
|
||||
def post(self) -> Response:
|
||||
request.max_content_length = 16_384
|
||||
if request.content_length is not None and request.content_length > 16_384:
|
||||
raise RequestEntityTooLarge("KnowledgeFS background locator is too large")
|
||||
try:
|
||||
job = BackgroundJobPayload.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as error:
|
||||
raise BadRequest("Invalid KnowledgeFS background locator") from error
|
||||
try:
|
||||
publish_background_job(job)
|
||||
except ValueError as error:
|
||||
raise BadRequest("Invalid KnowledgeFS background schedule") from error
|
||||
except Exception as error:
|
||||
# A failed/uncertain publish must leave the durable outbox eligible for redelivery.
|
||||
raise ServiceUnavailable("KnowledgeFS Celery publication is unavailable") from error
|
||||
return Response(status=HTTPStatus.ACCEPTED)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/background/health")
|
||||
class KnowledgeFSBackgroundHealthApi(Resource):
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> Response:
|
||||
try:
|
||||
check_background_publisher_ready()
|
||||
except Exception as error:
|
||||
raise ServiceUnavailable("KnowledgeFS Celery publication is unavailable") from error
|
||||
return Response(status=HTTPStatus.NO_CONTENT)
|
||||
@@ -0,0 +1,416 @@
|
||||
"""Trusted KnowledgeFS gateway to Dify's configured object-storage backend."""
|
||||
|
||||
import json
|
||||
from base64 import b64decode
|
||||
from binascii import Error as BinasciiError
|
||||
from http import HTTPStatus
|
||||
from typing import NoReturn
|
||||
|
||||
from flask import Response, request
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError
|
||||
from pydantic.alias_generators import to_camel
|
||||
|
||||
from controllers.common.schema import query_params_from_model, register_response_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import knowledge_fs_inner_api_only
|
||||
from fields.base import ResponseModel
|
||||
from libs.exception import BaseHTTPException
|
||||
from libs.helper import dump_response
|
||||
from services.knowledge_fs.object_storage import (
|
||||
KNOWLEDGE_FS_OBJECT_MAX_BYTES,
|
||||
KnowledgeFSObjectList,
|
||||
KnowledgeFSObjectMetadata,
|
||||
KnowledgeFSObjectStorageChecksumError,
|
||||
KnowledgeFSObjectStorageCorruptError,
|
||||
KnowledgeFSObjectStorageError,
|
||||
KnowledgeFSObjectStorageInvalidInputError,
|
||||
KnowledgeFSObjectStorageService,
|
||||
KnowledgeFSObjectStorageTooLargeError,
|
||||
KnowledgeFSObjectStorageUnavailableError,
|
||||
)
|
||||
from services.knowledge_fs.query_images import KnowledgeFSQueryImageError, load_query_image
|
||||
from services.knowledge_fs.remote_images import (
|
||||
KnowledgeFSRemoteImageError,
|
||||
load_remote_image,
|
||||
)
|
||||
|
||||
_METADATA_HEADER = "X-Knowledge-FS-Metadata"
|
||||
_CHECKSUM_HEADER = "X-Knowledge-FS-Checksum-Sha256"
|
||||
_CONTENT_TYPE_HEADER = "X-Knowledge-FS-Content-Type"
|
||||
_MAX_ENCODED_METADATA_BYTES = 128 * 1024
|
||||
_metadata_adapter = TypeAdapter(dict[str, str])
|
||||
|
||||
|
||||
class KnowledgeFSObjectStorageHttpError(BaseHTTPException):
|
||||
"""Safe HTTP representation of a KnowledgeFS storage boundary error."""
|
||||
|
||||
error_code = "knowledge_fs_object_storage_failed"
|
||||
description = "KnowledgeFS object storage request failed."
|
||||
code = HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
|
||||
def __init__(self, *, error_code: str, description: str, status_code: HTTPStatus) -> None:
|
||||
self.error_code = error_code
|
||||
self.description = description
|
||||
self.code = status_code
|
||||
super().__init__(description)
|
||||
|
||||
|
||||
class _CamelCaseResponse(ResponseModel):
|
||||
model_config = ConfigDict(alias_generator=to_camel)
|
||||
|
||||
|
||||
class KnowledgeFSObjectQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
key: str = Field(description="Logical KnowledgeFS object key")
|
||||
|
||||
|
||||
class KnowledgeFSObjectListQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
prefix: str = Field(default="", description="Logical object-key prefix")
|
||||
cursor: str | None = Field(default=None, description="Exclusive lexical key cursor")
|
||||
limit: int = Field(default=100, ge=1, le=100, description="Maximum objects to return")
|
||||
|
||||
|
||||
class KnowledgeFSObjectMetadataResponse(_CamelCaseResponse):
|
||||
checksum_sha256_base64: str
|
||||
content_type: str | None = None
|
||||
key: str
|
||||
metadata: dict[str, str]
|
||||
size_bytes: int
|
||||
|
||||
|
||||
class KnowledgeFSObjectListResponse(_CamelCaseResponse):
|
||||
objects: list[KnowledgeFSObjectMetadataResponse]
|
||||
next_cursor: str | None = None
|
||||
|
||||
|
||||
class KnowledgeFSObjectHealthResponse(ResponseModel):
|
||||
ok: bool
|
||||
|
||||
|
||||
class KnowledgeFSQueryImageQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", alias_generator=to_camel, populate_by_name=True)
|
||||
|
||||
upload_file_id: str = Field(min_length=1)
|
||||
tenant_id: str = Field(min_length=1)
|
||||
subject_id: str = Field(min_length=1)
|
||||
|
||||
|
||||
class KnowledgeFSRemoteImageQuery(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
url: str = Field(min_length=1, max_length=8_192)
|
||||
|
||||
|
||||
register_response_schema_models(
|
||||
inner_api_ns,
|
||||
KnowledgeFSObjectMetadataResponse,
|
||||
KnowledgeFSObjectListResponse,
|
||||
KnowledgeFSObjectHealthResponse,
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/query-image")
|
||||
class KnowledgeFSQueryImageApi(Resource):
|
||||
"""Resolve one actor-owned or workflow-granted Dify file for a bounded KFS query run."""
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSQueryImageQuery))
|
||||
@inner_api_ns.produces(["image/gif", "image/jpeg", "image/png", "image/webp"])
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> Response:
|
||||
try:
|
||||
query = KnowledgeFSQueryImageQuery.model_validate(request.args.to_dict(flat=True))
|
||||
access_grant = request.headers.get("X-Knowledge-FS-Query-Image-Grant")
|
||||
if access_grant is not None and (not access_grant or len(access_grant) > 2_048):
|
||||
raise KnowledgeFSQueryImageError("QUERY_IMAGE_GRANT_INVALID", "Workflow query image grant is invalid")
|
||||
account_id = None if access_grant else _query_image_account_id(query.subject_id)
|
||||
image = load_query_image(
|
||||
tenant_id=query.tenant_id,
|
||||
subject_id=query.subject_id,
|
||||
account_id=account_id,
|
||||
access_grant=access_grant,
|
||||
upload_file_id=query.upload_file_id,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSQueryImageError as exc:
|
||||
status = (
|
||||
HTTPStatus.NOT_FOUND
|
||||
if exc.code == "QUERY_IMAGE_NOT_FOUND"
|
||||
else HTTPStatus.REQUEST_ENTITY_TOO_LARGE
|
||||
if exc.code in {"QUERY_IMAGE_TOO_LARGE", "QUERY_IMAGE_TOTAL_TOO_LARGE"}
|
||||
else HTTPStatus.BAD_REQUEST
|
||||
)
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code=exc.code.lower(),
|
||||
description=str(exc),
|
||||
status_code=status,
|
||||
) from exc
|
||||
|
||||
response = Response(image.body, content_type=image.mime_type)
|
||||
response.content_length = image.byte_size
|
||||
response.headers["X-Query-Image-Sha256"] = image.sha256
|
||||
response.headers["X-Query-Image-Upload-File-Id"] = image.upload_file_id
|
||||
return response
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/remote-image")
|
||||
class KnowledgeFSRemoteImageApi(Resource):
|
||||
"""Resolve one bounded document image through Dify's SSRF-protected network client."""
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSRemoteImageQuery))
|
||||
@inner_api_ns.produces(["image/gif", "image/jpeg", "image/png", "image/webp"])
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> Response:
|
||||
try:
|
||||
query = KnowledgeFSRemoteImageQuery.model_validate(request.args.to_dict(flat=True))
|
||||
image = load_remote_image(query.url)
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSRemoteImageError as exc:
|
||||
raise _remote_image_http_error(exc) from exc
|
||||
|
||||
response = Response(image.body, content_type=image.mime_type)
|
||||
response.content_length = image.byte_size
|
||||
response.headers["X-Knowledge-FS-Remote-Image-Sha256"] = image.sha256
|
||||
return response
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/storage/object")
|
||||
class KnowledgeFSObjectApi(Resource):
|
||||
"""Read, write, or delete one logical KnowledgeFS object."""
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSObjectQuery))
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.OK,
|
||||
"Object stored",
|
||||
inner_api_ns.models[KnowledgeFSObjectMetadataResponse.__name__],
|
||||
)
|
||||
@knowledge_fs_inner_api_only
|
||||
def put(self) -> dict[str, object]:
|
||||
try:
|
||||
query = KnowledgeFSObjectQuery.model_validate(request.args.to_dict(flat=True))
|
||||
metadata = _decode_metadata_header(request.headers.get(_METADATA_HEADER))
|
||||
body = request.stream.read(KNOWLEDGE_FS_OBJECT_MAX_BYTES + 1)
|
||||
if len(body) > KNOWLEDGE_FS_OBJECT_MAX_BYTES:
|
||||
raise KnowledgeFSObjectStorageTooLargeError(f"object exceeds max bytes {KNOWLEDGE_FS_OBJECT_MAX_BYTES}")
|
||||
result = KnowledgeFSObjectStorageService().put_object(
|
||||
body=body,
|
||||
checksum_sha256_base64=request.headers.get(_CHECKSUM_HEADER),
|
||||
content_type=request.headers.get(_CONTENT_TYPE_HEADER),
|
||||
key=query.key,
|
||||
metadata=metadata,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSObjectStorageError as exc:
|
||||
_raise_http_error(exc)
|
||||
return _metadata_response(result)
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSObjectQuery))
|
||||
@inner_api_ns.produces(["application/octet-stream"])
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> Response:
|
||||
try:
|
||||
query = KnowledgeFSObjectQuery.model_validate(request.args.to_dict(flat=True))
|
||||
service = KnowledgeFSObjectStorageService()
|
||||
metadata = service.head_object(key=query.key)
|
||||
if metadata is None:
|
||||
raise _not_found_error()
|
||||
body = service.load_stream(key=query.key)
|
||||
if body is None:
|
||||
raise _not_found_error()
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSObjectStorageError as exc:
|
||||
_raise_http_error(exc)
|
||||
|
||||
response = Response(
|
||||
body,
|
||||
content_type=metadata.content_type or "application/octet-stream",
|
||||
)
|
||||
response.content_length = metadata.size_bytes
|
||||
response.headers[_CHECKSUM_HEADER] = metadata.checksum_sha256_base64
|
||||
return response
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSObjectQuery))
|
||||
@inner_api_ns.response(HTTPStatus.NO_CONTENT, "Object deleted")
|
||||
@knowledge_fs_inner_api_only
|
||||
def delete(self) -> tuple[str, int]:
|
||||
try:
|
||||
query = KnowledgeFSObjectQuery.model_validate(request.args.to_dict(flat=True))
|
||||
KnowledgeFSObjectStorageService().delete_object(key=query.key)
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSObjectStorageError as exc:
|
||||
_raise_http_error(exc)
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/storage/object/metadata")
|
||||
class KnowledgeFSObjectMetadataApi(Resource):
|
||||
"""Read portable metadata for one logical KnowledgeFS object."""
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSObjectQuery))
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.OK,
|
||||
"Object metadata",
|
||||
inner_api_ns.models[KnowledgeFSObjectMetadataResponse.__name__],
|
||||
)
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> dict[str, object]:
|
||||
try:
|
||||
query = KnowledgeFSObjectQuery.model_validate(request.args.to_dict(flat=True))
|
||||
result = KnowledgeFSObjectStorageService().head_object(key=query.key)
|
||||
if result is None:
|
||||
raise _not_found_error()
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSObjectStorageError as exc:
|
||||
_raise_http_error(exc)
|
||||
return _metadata_response(result)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/storage/objects")
|
||||
class KnowledgeFSObjectListApi(Resource):
|
||||
"""List logical KnowledgeFS objects with bounded keyset pagination."""
|
||||
|
||||
@inner_api_ns.doc(params=query_params_from_model(KnowledgeFSObjectListQuery))
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.OK,
|
||||
"Object page",
|
||||
inner_api_ns.models[KnowledgeFSObjectListResponse.__name__],
|
||||
)
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> dict[str, object]:
|
||||
try:
|
||||
query = KnowledgeFSObjectListQuery.model_validate(request.args.to_dict(flat=True))
|
||||
result = KnowledgeFSObjectStorageService().list_objects(
|
||||
cursor=query.cursor,
|
||||
limit=query.limit,
|
||||
prefix=query.prefix,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
raise _invalid_request_error() from exc
|
||||
except KnowledgeFSObjectStorageError as exc:
|
||||
_raise_http_error(exc)
|
||||
return _list_response(result)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/storage/health")
|
||||
class KnowledgeFSObjectHealthApi(Resource):
|
||||
"""Report whether Dify storage satisfies KnowledgeFS portable requirements."""
|
||||
|
||||
@inner_api_ns.response(
|
||||
HTTPStatus.OK,
|
||||
"Storage available",
|
||||
inner_api_ns.models[KnowledgeFSObjectHealthResponse.__name__],
|
||||
)
|
||||
@inner_api_ns.response(HTTPStatus.SERVICE_UNAVAILABLE, "Storage unavailable")
|
||||
@knowledge_fs_inner_api_only
|
||||
def get(self) -> dict[str, bool] | tuple[dict[str, bool], int]:
|
||||
if KnowledgeFSObjectStorageService().health():
|
||||
return {"ok": True}
|
||||
return {"ok": False}, HTTPStatus.SERVICE_UNAVAILABLE
|
||||
|
||||
|
||||
def _decode_metadata_header(value: str | None) -> dict[str, str]:
|
||||
if value is None:
|
||||
return {}
|
||||
if len(value.encode()) > _MAX_ENCODED_METADATA_BYTES:
|
||||
raise KnowledgeFSObjectStorageInvalidInputError("object metadata header is too large")
|
||||
try:
|
||||
padding = "=" * (-len(value) % 4)
|
||||
decoded = b64decode(value + padding, altchars=b"-_", validate=True)
|
||||
return _metadata_adapter.validate_json(decoded)
|
||||
except (BinasciiError, UnicodeEncodeError, ValidationError, json.JSONDecodeError) as exc:
|
||||
raise KnowledgeFSObjectStorageInvalidInputError("object metadata header is invalid") from exc
|
||||
|
||||
|
||||
def _query_image_account_id(subject_id: str) -> str:
|
||||
prefix = "dify-account:"
|
||||
account_id = subject_id[len(prefix) :].strip() if subject_id.startswith(prefix) else subject_id.strip()
|
||||
if not account_id:
|
||||
raise KnowledgeFSQueryImageError("QUERY_IMAGE_SUBJECT_INVALID", "Query image subject is invalid")
|
||||
return account_id
|
||||
|
||||
|
||||
def _metadata_response(metadata: KnowledgeFSObjectMetadata) -> dict[str, object]:
|
||||
return dump_response(KnowledgeFSObjectMetadataResponse, metadata)
|
||||
|
||||
|
||||
def _list_response(result: KnowledgeFSObjectList) -> dict[str, object]:
|
||||
return dump_response(KnowledgeFSObjectListResponse, result)
|
||||
|
||||
|
||||
def _raise_http_error(error: KnowledgeFSObjectStorageError) -> NoReturn:
|
||||
if isinstance(error, KnowledgeFSObjectStorageTooLargeError):
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_too_large",
|
||||
description="KnowledgeFS object exceeds the configured size limit.",
|
||||
status_code=HTTPStatus.REQUEST_ENTITY_TOO_LARGE,
|
||||
) from error
|
||||
if isinstance(error, KnowledgeFSObjectStorageChecksumError):
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_checksum_mismatch",
|
||||
description="KnowledgeFS object checksum does not match the request body.",
|
||||
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
|
||||
) from error
|
||||
if isinstance(error, KnowledgeFSObjectStorageInvalidInputError):
|
||||
raise _invalid_request_error() from error
|
||||
if isinstance(error, KnowledgeFSObjectStorageCorruptError):
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_corrupt",
|
||||
description="KnowledgeFS object metadata is inconsistent.",
|
||||
status_code=HTTPStatus.BAD_GATEWAY,
|
||||
) from error
|
||||
if isinstance(error, KnowledgeFSObjectStorageUnavailableError):
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_storage_unavailable",
|
||||
description="Dify object storage is unavailable for KnowledgeFS.",
|
||||
status_code=HTTPStatus.SERVICE_UNAVAILABLE,
|
||||
) from error
|
||||
raise KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_storage_failed",
|
||||
description="KnowledgeFS object storage request failed.",
|
||||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||
) from error
|
||||
|
||||
|
||||
def _invalid_request_error() -> KnowledgeFSObjectStorageHttpError:
|
||||
return KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_storage_invalid_request",
|
||||
description="KnowledgeFS object storage request is invalid.",
|
||||
status_code=HTTPStatus.BAD_REQUEST,
|
||||
)
|
||||
|
||||
|
||||
def _not_found_error() -> KnowledgeFSObjectStorageHttpError:
|
||||
return KnowledgeFSObjectStorageHttpError(
|
||||
error_code="knowledge_fs_object_not_found",
|
||||
description="KnowledgeFS object was not found.",
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
)
|
||||
|
||||
|
||||
def _remote_image_http_error(error: KnowledgeFSRemoteImageError) -> KnowledgeFSObjectStorageHttpError:
|
||||
status_by_code = {
|
||||
"REMOTE_IMAGE_BLOCKED": HTTPStatus.FORBIDDEN,
|
||||
"REMOTE_IMAGE_CONTENT_UNSUPPORTED": HTTPStatus.UNSUPPORTED_MEDIA_TYPE,
|
||||
"REMOTE_IMAGE_EMPTY": HTTPStatus.UNPROCESSABLE_ENTITY,
|
||||
"REMOTE_IMAGE_HOST_NOT_FOUND": HTTPStatus.NOT_FOUND,
|
||||
"REMOTE_IMAGE_NOT_FOUND": HTTPStatus.NOT_FOUND,
|
||||
"REMOTE_IMAGE_RATE_LIMITED": HTTPStatus.TOO_MANY_REQUESTS,
|
||||
"REMOTE_IMAGE_REQUEST_REJECTED": HTTPStatus.UNPROCESSABLE_ENTITY,
|
||||
"REMOTE_IMAGE_TOO_LARGE": HTTPStatus.REQUEST_ENTITY_TOO_LARGE,
|
||||
"REMOTE_IMAGE_UPSTREAM_UNAVAILABLE": HTTPStatus.BAD_GATEWAY,
|
||||
"REMOTE_IMAGE_URL_INVALID": HTTPStatus.BAD_REQUEST,
|
||||
}
|
||||
return KnowledgeFSObjectStorageHttpError(
|
||||
error_code=error.code.lower(),
|
||||
description=str(error),
|
||||
status_code=status_by_code.get(error.code, HTTPStatus.BAD_GATEWAY),
|
||||
)
|
||||
@@ -0,0 +1,87 @@
|
||||
"""Authenticated full-text bridge; provider configuration never comes from callers."""
|
||||
|
||||
import re
|
||||
from contextlib import nullcontext
|
||||
from uuid import uuid4
|
||||
|
||||
from flask import Response, after_this_request, request
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from werkzeug.exceptions import BadRequest, ServiceUnavailable
|
||||
|
||||
from configs import dify_config
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.knowledge_fs.vectors import VectorMatchResponse
|
||||
from controllers.inner_api.wraps import knowledge_fs_inner_api_only
|
||||
from fields.base import ResponseModel
|
||||
from libs.helper import dump_response
|
||||
from services.knowledge_fs.text_search_deadline import TextSearchDeadline
|
||||
from services.knowledge_fs.text_store import (
|
||||
TextIndexPendingError,
|
||||
TextPayload,
|
||||
TextPoint,
|
||||
configured_text_client,
|
||||
execute_text_request,
|
||||
)
|
||||
from services.knowledge_fs.text_store_diagnostics import text_backend_diagnostics, text_backend_stage
|
||||
from services.tidb_binding_service import TidbBindingPendingError
|
||||
|
||||
|
||||
class TextResponse(ResponseModel):
|
||||
points: list[TextPoint]
|
||||
matches: list[VectorMatchResponse]
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, TextPayload)
|
||||
register_response_schema_models(inner_api_ns, TextResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/texts")
|
||||
class KnowledgeFSTextApi(Resource):
|
||||
@inner_api_ns.expect(inner_api_ns.models[TextPayload.__name__])
|
||||
@inner_api_ns.response(200, "Full-text operation completed", inner_api_ns.models[TextResponse.__name__])
|
||||
@knowledge_fs_inner_api_only
|
||||
def post(self) -> dict[str, object]:
|
||||
request.max_content_length = 32 * 1024 * 1024
|
||||
try:
|
||||
payload = TextPayload.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as error:
|
||||
raise BadRequest("Invalid KnowledgeFS full-text operation") from error
|
||||
deadline_ms = None
|
||||
if payload.operation == "search":
|
||||
header = request.headers.get("X-Knowledge-FS-Deadline-Ms")
|
||||
if header is not None:
|
||||
if not re.fullmatch(r"[0-9]{1,16}", header) or not 0 < int(header) <= 2**53 - 1:
|
||||
raise BadRequest("Invalid KnowledgeFS full-text deadline")
|
||||
deadline_ms = int(header)
|
||||
request_id = request.headers.get("X-Knowledge-FS-Request-Id", "")
|
||||
if not re.fullmatch(r"[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}", request_id):
|
||||
request_id = str(uuid4())
|
||||
|
||||
@after_this_request
|
||||
def attach_request_id(response: Response) -> Response:
|
||||
response.headers["X-Knowledge-FS-Request-Id"] = request_id
|
||||
return response
|
||||
|
||||
with text_backend_diagnostics(request_id, payload, dify_config.VECTOR_STORE or "unconfigured") as diagnostics:
|
||||
try:
|
||||
with text_backend_stage("deadline"):
|
||||
deadline = TextSearchDeadline.from_epoch_ms(deadline_ms) if payload.operation == "search" else None
|
||||
with deadline.enforce() if deadline else nullcontext():
|
||||
with (
|
||||
text_backend_stage("configure"),
|
||||
configured_text_client(
|
||||
payload.scope.tenant_id,
|
||||
allow_create=payload.operation == "upsert",
|
||||
**({"deadline": deadline} if deadline else {}),
|
||||
) as client,
|
||||
):
|
||||
result = execute_text_request(client, payload)
|
||||
except (TidbBindingPendingError, TextIndexPendingError) as error:
|
||||
diagnostics.log_failure(error, outcome="pending")
|
||||
raise ServiceUnavailable("KnowledgeFS full-text index is being initialized", retry_after=5) from None
|
||||
except Exception as error:
|
||||
diagnostics.log_failure(error, outcome="failed")
|
||||
raise ServiceUnavailable("KnowledgeFS full-text backend is unavailable") from None
|
||||
return dump_response(TextResponse, result)
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Authenticated vector bridge; only the server's configured backend is reachable."""
|
||||
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import ValidationError
|
||||
from werkzeug.exceptions import BadRequest, ServiceUnavailable
|
||||
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import knowledge_fs_inner_api_only
|
||||
from fields.base import ResponseModel
|
||||
from libs.exception import BaseHTTPException
|
||||
from libs.helper import dump_response
|
||||
from services.knowledge_fs.vector_store import (
|
||||
VectorCollectionPendingError,
|
||||
VectorPoint,
|
||||
VectorRequest,
|
||||
admit_vector_request,
|
||||
configured_vector_client,
|
||||
execute_vector_request,
|
||||
)
|
||||
from services.tidb_binding_service import TidbBindingPendingError
|
||||
from services.vector_space_admission_service import VectorSpaceAdmissionError, VectorSpaceAdmissionUnavailableError
|
||||
|
||||
|
||||
class KnowledgeFSVectorQuotaExceededError(BaseHTTPException):
|
||||
error_code = "vector_space_quota_exceeded"
|
||||
code = 413
|
||||
|
||||
|
||||
class KnowledgeFSVectorQuotaUnavailableError(BaseHTTPException):
|
||||
error_code = "vector_space_quota_unavailable"
|
||||
code = 503
|
||||
|
||||
|
||||
class VectorMatchResponse(ResponseModel):
|
||||
id: str
|
||||
score: float
|
||||
|
||||
|
||||
class VectorResponse(ResponseModel):
|
||||
points: list[VectorPoint]
|
||||
matches: list[VectorMatchResponse]
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, VectorRequest)
|
||||
register_response_schema_models(inner_api_ns, VectorResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/knowledge-fs/vectors")
|
||||
class KnowledgeFSVectorApi(Resource):
|
||||
@inner_api_ns.expect(inner_api_ns.models[VectorRequest.__name__])
|
||||
@inner_api_ns.response(200, "Vector operation completed", inner_api_ns.models[VectorResponse.__name__])
|
||||
@knowledge_fs_inner_api_only
|
||||
def post(self) -> dict[str, object]:
|
||||
request.max_content_length = 32 * 1024 * 1024
|
||||
try:
|
||||
payload = VectorRequest.model_validate(inner_api_ns.payload or {})
|
||||
except ValidationError as error:
|
||||
raise BadRequest("Invalid KnowledgeFS vector operation") from error
|
||||
try:
|
||||
admit_vector_request(payload)
|
||||
with configured_vector_client(
|
||||
payload.scope.tenant_id, allow_create=payload.operation == "upsert"
|
||||
) as client:
|
||||
result = execute_vector_request(client, payload, check_admission=False)
|
||||
except (TidbBindingPendingError, VectorCollectionPendingError):
|
||||
raise ServiceUnavailable("KnowledgeFS vector backend is being provisioned", retry_after=5) from None
|
||||
except VectorSpaceAdmissionUnavailableError as error:
|
||||
raise KnowledgeFSVectorQuotaUnavailableError(str(error)) from None
|
||||
except VectorSpaceAdmissionError as error:
|
||||
raise KnowledgeFSVectorQuotaExceededError(str(error)) from None
|
||||
except Exception:
|
||||
# Native SDK exceptions can contain endpoint credentials or vectors.
|
||||
raise ServiceUnavailable("KnowledgeFS vector backend is unavailable") from None
|
||||
return dump_response(VectorResponse, result)
|
||||
@@ -0,0 +1,86 @@
|
||||
from typing import Any, override
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from controllers.common.fields import SimpleMessageResponse
|
||||
from controllers.common.schema import register_response_schema_model, register_schema_model
|
||||
from controllers.console.wraps import model_validate, setup_required
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import inner_api_only
|
||||
from extensions.ext_application_services import application_services
|
||||
from services.entities.mail_entities import InnerMailMessage
|
||||
|
||||
|
||||
class InnerMailPayload(BaseModel):
|
||||
to: list[str] = Field(description="Recipient email addresses", min_length=1)
|
||||
subject: str
|
||||
body: str
|
||||
substitutions: dict[str, Any] | None = None
|
||||
|
||||
|
||||
register_schema_model(inner_api_ns, InnerMailPayload)
|
||||
register_response_schema_model(inner_api_ns, SimpleMessageResponse)
|
||||
|
||||
|
||||
class BaseMail(Resource):
|
||||
"""Shared logic for sending an inner email."""
|
||||
|
||||
@inner_api_ns.doc("send_inner_mail")
|
||||
@inner_api_ns.doc(description="Send internal email")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerMailPayload.__name__])
|
||||
@model_validate(InnerMailPayload)
|
||||
def post(self, args: InnerMailPayload):
|
||||
application_services().inner_mail.send(
|
||||
InnerMailMessage(
|
||||
recipients=tuple(args.to),
|
||||
subject=args.subject,
|
||||
body=args.body,
|
||||
substitutions=args.substitutions,
|
||||
)
|
||||
)
|
||||
return SimpleMessageResponse(message="success").model_dump(mode="json"), 200
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/mail")
|
||||
class EnterpriseMail(BaseMail):
|
||||
@inner_api_ns.doc("send_enterprise_mail")
|
||||
@inner_api_ns.doc(description="Send internal email for enterprise features")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerMailPayload.__name__])
|
||||
@inner_api_ns.response(200, "Email sent successfully", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
@inner_api_only
|
||||
@setup_required
|
||||
@override
|
||||
def post(self):
|
||||
"""Send internal email for enterprise features.
|
||||
|
||||
This endpoint allows sending internal emails for enterprise-specific
|
||||
notifications and communications.
|
||||
|
||||
Returns:
|
||||
dict: Success message with status code 200
|
||||
"""
|
||||
return super().post()
|
||||
|
||||
|
||||
@inner_api_ns.route("/billing/mail")
|
||||
class BillingMail(BaseMail):
|
||||
@inner_api_ns.doc("send_billing_mail")
|
||||
@inner_api_ns.doc(description="Send internal email for billing notifications")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerMailPayload.__name__])
|
||||
@inner_api_ns.doc(
|
||||
responses={200: "Email sent successfully", 401: "Unauthorized - invalid API key", 404: "Service not available"}
|
||||
)
|
||||
@inner_api_only
|
||||
@setup_required
|
||||
@override
|
||||
def post(self):
|
||||
"""Send internal email for billing notifications.
|
||||
|
||||
This endpoint allows sending internal emails for billing-related
|
||||
notifications and alerts.
|
||||
|
||||
Returns:
|
||||
dict: Success message with status code 200
|
||||
"""
|
||||
return super().post()
|
||||
Whitespace-only changes.
@@ -0,0 +1,303 @@
|
||||
"""Inner API for Agent Soul-backed config assets.
|
||||
|
||||
These endpoints are called by the dify-agent server with the inner API key.
|
||||
They resolve the requested Agent config version directly from Agent Soul JSON
|
||||
and authorize downloads with the existing Config target, tenant, and source
|
||||
ownership semantics. Download requests return metadata plus a short-lived,
|
||||
origin-free ``/files/*`` URI, never file bytes; the Sandbox fetches those bytes
|
||||
directly from the Dify API data plane.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from flask import request
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, ValidationError, model_validator
|
||||
|
||||
from controllers.common.schema import query_params_from_model, register_response_schema_models, register_schema_models
|
||||
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 fields.base import ResponseModel
|
||||
from libs.exception import RelayedHTTPError
|
||||
from libs.helper import dump_response
|
||||
from models.agent_config_entities import validate_config_name, validate_config_skill_name
|
||||
from services.agent_config_service import (
|
||||
AgentConfigService,
|
||||
AgentConfigServiceError,
|
||||
AgentConfigVersionKind,
|
||||
ConfigPushPayload,
|
||||
)
|
||||
|
||||
|
||||
class _ConfigTargetQuery(BaseModel):
|
||||
tenant_id: str
|
||||
user_id: str | None = None
|
||||
config_version_id: str
|
||||
config_version_kind: AgentConfigVersionKind
|
||||
|
||||
|
||||
class _ConfigMutationRequest(BaseModel):
|
||||
tenant_id: str
|
||||
user_id: str
|
||||
config_version_id: str
|
||||
config_version_kind: AgentConfigVersionKind
|
||||
|
||||
|
||||
class _ConfigDownloadSource(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
kind: Literal["file", "skill"]
|
||||
name: str
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_name(self) -> _ConfigDownloadSource:
|
||||
self.name = validate_config_skill_name(self.name) if self.kind == "skill" else validate_config_name(self.name)
|
||||
return self
|
||||
|
||||
|
||||
class _ConfigDownloadRequest(_ConfigTargetQuery):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
config: _ConfigDownloadSource
|
||||
|
||||
|
||||
class _ConfigPushRequest(_ConfigMutationRequest):
|
||||
files: list[dict] = []
|
||||
skills: list[dict] = []
|
||||
env_text: str | None = None
|
||||
note: str | None = None
|
||||
|
||||
def to_payload(self) -> ConfigPushPayload:
|
||||
return ConfigPushPayload.model_validate(
|
||||
{
|
||||
"files": self.files,
|
||||
"skills": self.skills,
|
||||
"env_text": self.env_text,
|
||||
"note": self.note,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class _ConfigEnvUpdateRequest(_ConfigMutationRequest):
|
||||
env_text: str
|
||||
|
||||
|
||||
class _ConfigNoteUpdateRequest(_ConfigMutationRequest):
|
||||
note: str
|
||||
|
||||
|
||||
class AgentConfigPromptUpdatePayload(_ConfigMutationRequest):
|
||||
"""Replace the raw system prompt; missing/null text is never a clear request."""
|
||||
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
prompt: str
|
||||
|
||||
|
||||
class AgentConfigPromptResponse(ResponseModel):
|
||||
prompt: str
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, AgentConfigPromptUpdatePayload)
|
||||
register_response_schema_models(inner_api_ns, AgentConfigPromptResponse)
|
||||
|
||||
|
||||
def _target_query_from_request() -> _ConfigTargetQuery:
|
||||
return _ConfigTargetQuery.model_validate(
|
||||
{
|
||||
"tenant_id": request.args.get("tenant_id"),
|
||||
"user_id": request.args.get("user_id"),
|
||||
"config_version_id": request.args.get("config_version_id"),
|
||||
"config_version_kind": request.args.get("config_version_kind"),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _error_response(exc: AgentConfigServiceError) -> tuple[dict[str, str], int]:
|
||||
return {"code": exc.code, "message": exc.message}, exc.status_code
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/manifest")
|
||||
class AgentConfigManifestApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_manifest")
|
||||
def get(self, agent_id: str):
|
||||
try:
|
||||
query = _target_query_from_request()
|
||||
return AgentConfigService().manifest(
|
||||
tenant_id=query.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=query.user_id,
|
||||
config_version_id=query.config_version_id,
|
||||
config_version_kind=query.config_version_kind,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/download-request")
|
||||
class AgentConfigDownloadRequestApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_download_request")
|
||||
def post(self, agent_id: str):
|
||||
try:
|
||||
body = _ConfigDownloadRequest.model_validate(request.get_json(silent=True) or {})
|
||||
result = AgentConfigService().request_download(
|
||||
tenant_id=body.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=body.user_id,
|
||||
config_version_id=body.config_version_id,
|
||||
config_version_kind=body.config_version_kind,
|
||||
kind=body.config.kind,
|
||||
name=body.config.name,
|
||||
)
|
||||
return {
|
||||
"filename": result.filename,
|
||||
"mime_type": result.mime_type,
|
||||
"size": result.size,
|
||||
"download_uri": result.download_uri,
|
||||
}
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/skills/<string:name>/inspect")
|
||||
class AgentConfigSkillInspectApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_skill_inspect")
|
||||
def get(self, agent_id: str, name: str):
|
||||
try:
|
||||
query = _target_query_from_request()
|
||||
return AgentConfigService().inspect_skill(
|
||||
tenant_id=query.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=query.user_id,
|
||||
config_version_id=query.config_version_id,
|
||||
config_version_kind=query.config_version_kind,
|
||||
name=name,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/push")
|
||||
class AgentConfigPushApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_push")
|
||||
def post(self, agent_id: str):
|
||||
try:
|
||||
body = _ConfigPushRequest.model_validate(request.get_json(silent=True) or {})
|
||||
return AgentConfigService().push(
|
||||
tenant_id=body.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=body.user_id,
|
||||
config_version_id=body.config_version_id,
|
||||
config_version_kind=body.config_version_kind,
|
||||
payload=body.to_payload(),
|
||||
)
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/env")
|
||||
class AgentConfigEnvApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_env")
|
||||
def patch(self, agent_id: str):
|
||||
try:
|
||||
body = _ConfigEnvUpdateRequest.model_validate(request.get_json(silent=True) or {})
|
||||
return AgentConfigService().update_env(
|
||||
tenant_id=body.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=body.user_id,
|
||||
config_version_id=body.config_version_id,
|
||||
config_version_kind=body.config_version_kind,
|
||||
env_text=body.env_text,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/note")
|
||||
class AgentConfigNoteApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_note")
|
||||
def put(self, agent_id: str):
|
||||
try:
|
||||
body = _ConfigNoteUpdateRequest.model_validate(request.get_json(silent=True) or {})
|
||||
return AgentConfigService().update_note(
|
||||
tenant_id=body.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=body.user_id,
|
||||
config_version_id=body.config_version_id,
|
||||
config_version_kind=body.config_version_kind,
|
||||
note=body.note,
|
||||
)
|
||||
except ValidationError as exc:
|
||||
return {"code": "invalid_request", "message": str(exc)}, 400
|
||||
except AgentConfigServiceError as exc:
|
||||
return _error_response(exc)
|
||||
|
||||
|
||||
@inner_api_ns.route("/agent-config/<string:agent_id>/prompt")
|
||||
class AgentConfigPromptApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_prompt_get", params=query_params_from_model(_ConfigTargetQuery))
|
||||
@inner_api_ns.response(200, "Raw system prompt", inner_api_ns.models[AgentConfigPromptResponse.__name__])
|
||||
def get(self, agent_id: str):
|
||||
try:
|
||||
query = _target_query_from_request()
|
||||
result = AgentConfigService().get_prompt(
|
||||
tenant_id=query.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=query.user_id,
|
||||
config_version_id=query.config_version_id,
|
||||
config_version_kind=query.config_version_kind,
|
||||
)
|
||||
return dump_response(AgentConfigPromptResponse, result)
|
||||
except ValidationError as exc:
|
||||
raise RelayedHTTPError(status=400, error_code="invalid_request", message=str(exc)) from exc
|
||||
except AgentConfigServiceError as exc:
|
||||
raise RelayedHTTPError(status=exc.status_code, error_code=exc.code, message=exc.message) from exc
|
||||
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@inner_api_ns.doc("agent_config_prompt_put")
|
||||
@inner_api_ns.expect(inner_api_ns.models[AgentConfigPromptUpdatePayload.__name__])
|
||||
@inner_api_ns.response(200, "Saved system prompt", inner_api_ns.models[AgentConfigPromptResponse.__name__])
|
||||
def put(self, agent_id: str):
|
||||
try:
|
||||
body = AgentConfigPromptUpdatePayload.model_validate(request.get_json(silent=True) or {})
|
||||
result = AgentConfigService().update_prompt(
|
||||
tenant_id=body.tenant_id,
|
||||
agent_id=agent_id,
|
||||
user_id=body.user_id,
|
||||
config_version_id=body.config_version_id,
|
||||
config_version_kind=body.config_version_kind,
|
||||
prompt=body.prompt,
|
||||
)
|
||||
return dump_response(AgentConfigPromptResponse, result)
|
||||
except ValidationError as exc:
|
||||
raise RelayedHTTPError(status=400, error_code="invalid_request", message=str(exc)) from exc
|
||||
except AgentConfigServiceError as exc:
|
||||
raise RelayedHTTPError(status=exc.status_code, error_code=exc.code, message=exc.message) from exc
|
||||
@@ -0,0 +1,660 @@
|
||||
import re
|
||||
|
||||
from flask_restx import Resource
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from configs import dify_config
|
||||
from controllers.console.app.wraps import with_session
|
||||
from controllers.console.wraps import setup_required
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.plugin.wraps import get_user_tenant, plugin_data
|
||||
from controllers.inner_api.wraps import plugin_inner_api_only
|
||||
from core.plugin.backwards_invocation.app import PluginAppBackwardsInvocation
|
||||
from core.plugin.backwards_invocation.base import BaseBackwardsInvocationResponse
|
||||
from core.plugin.backwards_invocation.encrypt import PluginEncrypter
|
||||
from core.plugin.backwards_invocation.model import PluginModelBackwardsInvocation
|
||||
from core.plugin.backwards_invocation.node import PluginNodeBackwardsInvocation
|
||||
from core.plugin.backwards_invocation.tool import PluginToolBackwardsInvocation
|
||||
from core.plugin.entities.request import (
|
||||
RequestFetchAppInfo,
|
||||
RequestInvokeApp,
|
||||
RequestInvokeDatasource,
|
||||
RequestInvokeEncrypt,
|
||||
RequestInvokeLLM,
|
||||
RequestInvokeLLMWithStructuredOutput,
|
||||
RequestInvokeModeration,
|
||||
RequestInvokeMultimodalEmbedding,
|
||||
RequestInvokeParameterExtractorNode,
|
||||
RequestInvokeQuestionClassifierNode,
|
||||
RequestInvokeRerank,
|
||||
RequestInvokeSpeech2Text,
|
||||
RequestInvokeSummary,
|
||||
RequestInvokeTextEmbedding,
|
||||
RequestInvokeTool,
|
||||
RequestInvokeTTS,
|
||||
RequestListModels,
|
||||
RequestRequestDownloadFile,
|
||||
RequestRequestUploadFile,
|
||||
)
|
||||
from core.tools.entities.tool_entities import ToolProviderType
|
||||
from core.tools.signature import bind_file_uri, get_signed_file_uri_for_plugin
|
||||
from extensions.ext_application_services import application_services
|
||||
from extensions.ext_database import db
|
||||
from graphon.model_runtime.errors.invoke import (
|
||||
InvokeConnectionError,
|
||||
InvokeRateLimitError,
|
||||
InvokeServerUnavailableError,
|
||||
)
|
||||
from graphon.model_runtime.utils.encoders import jsonable_encoder
|
||||
from libs.helper import length_prefixed_response
|
||||
from models import Account, Tenant
|
||||
from models.model import EndUser
|
||||
from services.file_request_service import FileRequestService
|
||||
from services.plugin.datasource_backwards_invocation import PluginDatasourceBackwardsInvocation
|
||||
from services.workflow_run_agg import WorkflowRunAgg
|
||||
|
||||
|
||||
def _model_invocation_error_response(error: Exception) -> dict[str, object]:
|
||||
"""Return a backwards-compatible envelope with safe retry classification."""
|
||||
|
||||
message = str(error)
|
||||
normalized = message.lower()
|
||||
if (
|
||||
isinstance(error, InvokeRateLimitError)
|
||||
or "resource_exhausted" in normalized
|
||||
or "rate limit" in normalized
|
||||
or re.search(r"\b429\b", normalized)
|
||||
):
|
||||
error_code = "rate_limited"
|
||||
retryable = True
|
||||
status = 429
|
||||
elif "timed out" in normalized or "timeout" in normalized:
|
||||
error_code = "timeout"
|
||||
retryable = True
|
||||
status = 504
|
||||
elif (
|
||||
isinstance(error, InvokeConnectionError | InvokeServerUnavailableError)
|
||||
or "connection error" in normalized
|
||||
or "temporarily unavailable" in normalized
|
||||
):
|
||||
error_code = "unavailable"
|
||||
retryable = True
|
||||
status = 503
|
||||
else:
|
||||
error_code = "invocation_failed"
|
||||
retryable = False
|
||||
status = None
|
||||
|
||||
response: dict[str, object] = {
|
||||
"data": None,
|
||||
"error": message,
|
||||
"error_code": error_code,
|
||||
"retryable": retryable,
|
||||
}
|
||||
if status is not None:
|
||||
response["status"] = status
|
||||
return response
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/llm")
|
||||
class PluginInvokeLLMApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeLLM)
|
||||
@inner_api_ns.doc("plugin_invoke_llm")
|
||||
@inner_api_ns.doc(description="Invoke LLM models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "LLM invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeLLM):
|
||||
def generator():
|
||||
response = PluginModelBackwardsInvocation.invoke_llm(user_model.id, tenant_model, payload)
|
||||
return PluginModelBackwardsInvocation.convert_to_event_stream(response)
|
||||
|
||||
return length_prefixed_response(0xF, generator())
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/llm/structured-output")
|
||||
class PluginInvokeLLMWithStructuredOutputApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeLLMWithStructuredOutput)
|
||||
@inner_api_ns.doc("plugin_invoke_llm_structured")
|
||||
@inner_api_ns.doc(description="Invoke LLM models with structured output through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "LLM structured output invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeLLMWithStructuredOutput):
|
||||
def generator():
|
||||
response = PluginModelBackwardsInvocation.invoke_llm_with_structured_output(
|
||||
user_model.id, tenant_model, payload
|
||||
)
|
||||
return PluginModelBackwardsInvocation.convert_to_event_stream(response)
|
||||
|
||||
return length_prefixed_response(0xF, generator())
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/text-embedding")
|
||||
class PluginInvokeTextEmbeddingApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeTextEmbedding)
|
||||
@inner_api_ns.doc("plugin_invoke_text_embedding")
|
||||
@inner_api_ns.doc(description="Invoke text embedding models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Text embedding successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTextEmbedding):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.invoke_text_embedding(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return _model_invocation_error_response(e)
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/multimodal-embedding")
|
||||
class PluginInvokeMultimodalEmbeddingApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeMultimodalEmbedding)
|
||||
@inner_api_ns.doc("plugin_invoke_multimodal_embedding")
|
||||
@inner_api_ns.doc(description="Invoke multimodal embedding models through Dify model management")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Multimodal embedding successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeMultimodalEmbedding):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.invoke_multimodal_embedding(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/rerank")
|
||||
class PluginInvokeRerankApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeRerank)
|
||||
@inner_api_ns.doc("plugin_invoke_rerank")
|
||||
@inner_api_ns.doc(description="Invoke rerank models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={200: "Rerank successful", 401: "Unauthorized - invalid API key", 404: "Service not available"}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeRerank):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.invoke_rerank(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/model-catalog")
|
||||
class PluginModelCatalogApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestListModels)
|
||||
@inner_api_ns.doc("plugin_model_catalog")
|
||||
@inner_api_ns.doc(description="List tenant-active models managed by Dify")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Model catalog lookup successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestListModels):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.list_models(
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=user_model.id,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/datasource")
|
||||
class PluginInvokeDatasourceApi(Resource):
|
||||
"""Invoke an installed datasource with credentials resolved inside Dify."""
|
||||
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeDatasource)
|
||||
@inner_api_ns.doc("plugin_invoke_datasource")
|
||||
@inner_api_ns.doc(description="Invoke datasource plugins through Dify credential management")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Datasource invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Datasource provider, datasource, or credential not found",
|
||||
}
|
||||
)
|
||||
def post(
|
||||
self,
|
||||
user_model: Account | EndUser,
|
||||
tenant_model: Tenant,
|
||||
payload: RequestInvokeDatasource,
|
||||
):
|
||||
response = PluginDatasourceBackwardsInvocation.invoke(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
return length_prefixed_response(
|
||||
0xF,
|
||||
PluginDatasourceBackwardsInvocation.convert_to_event_stream(response),
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/tts")
|
||||
class PluginInvokeTTSApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeTTS)
|
||||
@inner_api_ns.doc("plugin_invoke_tts")
|
||||
@inner_api_ns.doc(description="Invoke text-to-speech models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "TTS invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTTS):
|
||||
def generator():
|
||||
response = PluginModelBackwardsInvocation.invoke_tts(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
return PluginModelBackwardsInvocation.convert_to_event_stream(response)
|
||||
|
||||
return length_prefixed_response(0xF, generator())
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/speech2text")
|
||||
class PluginInvokeSpeech2TextApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeSpeech2Text)
|
||||
@inner_api_ns.doc("plugin_invoke_speech2text")
|
||||
@inner_api_ns.doc(description="Invoke speech-to-text models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={200: "Speech2Text successful", 401: "Unauthorized - invalid API key", 404: "Service not available"}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSpeech2Text):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.invoke_speech2text(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/moderation")
|
||||
class PluginInvokeModerationApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeModeration)
|
||||
@inner_api_ns.doc("plugin_invoke_moderation")
|
||||
@inner_api_ns.doc(description="Invoke moderation models through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={200: "Moderation successful", 401: "Unauthorized - invalid API key", 404: "Service not available"}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeModeration):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginModelBackwardsInvocation.invoke_moderation(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/tool")
|
||||
class PluginInvokeToolApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeTool)
|
||||
@inner_api_ns.doc("plugin_invoke_tool")
|
||||
@inner_api_ns.doc(description="Invoke tools through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Tool invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
@with_session
|
||||
def post(self, session: Session, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeTool):
|
||||
def generator():
|
||||
return PluginToolBackwardsInvocation.convert_to_event_stream(
|
||||
PluginToolBackwardsInvocation.invoke_tool(
|
||||
execution_driver=WorkflowRunAgg.run,
|
||||
session=session,
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=user_model.id,
|
||||
tool_type=ToolProviderType.value_of(payload.tool_type),
|
||||
provider=payload.provider,
|
||||
tool_name=payload.tool,
|
||||
tool_parameters=payload.tool_parameters,
|
||||
credential_id=payload.credential_id,
|
||||
),
|
||||
)
|
||||
|
||||
return length_prefixed_response(0xF, generator())
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/parameter-extractor")
|
||||
class PluginInvokeParameterExtractorNodeApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeParameterExtractorNode)
|
||||
@inner_api_ns.doc("plugin_invoke_parameter_extractor")
|
||||
@inner_api_ns.doc(description="Invoke parameter extractor node through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Parameter extraction successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeParameterExtractorNode):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginNodeBackwardsInvocation.invoke_parameter_extractor(
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=user_model.id,
|
||||
parameters=payload.parameters,
|
||||
model_config=payload.model,
|
||||
instruction=payload.instruction,
|
||||
query=payload.query,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/question-classifier")
|
||||
class PluginInvokeQuestionClassifierNodeApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeQuestionClassifierNode)
|
||||
@inner_api_ns.doc("plugin_invoke_question_classifier")
|
||||
@inner_api_ns.doc(description="Invoke question classifier node through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Question classification successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeQuestionClassifierNode):
|
||||
try:
|
||||
return jsonable_encoder(
|
||||
BaseBackwardsInvocationResponse(
|
||||
data=PluginNodeBackwardsInvocation.invoke_question_classifier(
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=user_model.id,
|
||||
query=payload.query,
|
||||
model_config=payload.model,
|
||||
classes=payload.classes,
|
||||
instruction=payload.instruction,
|
||||
)
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
return jsonable_encoder(BaseBackwardsInvocationResponse(error=str(e)))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/app")
|
||||
class PluginInvokeAppApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeApp)
|
||||
@inner_api_ns.doc("plugin_invoke_app")
|
||||
@inner_api_ns.doc(description="Invoke application through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "App invocation successful (streaming response)",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
@with_session
|
||||
def post(self, session: Session, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeApp):
|
||||
response = PluginAppBackwardsInvocation.invoke_app(
|
||||
execution_driver=WorkflowRunAgg.run,
|
||||
session=session,
|
||||
app_id=payload.app_id,
|
||||
user_id=user_model.id,
|
||||
tenant_id=tenant_model.id,
|
||||
conversation_id=payload.conversation_id,
|
||||
query=payload.query,
|
||||
stream=payload.response_mode == "streaming",
|
||||
inputs=payload.inputs,
|
||||
files=payload.files,
|
||||
end_users=application_services().app_scoped_end_users.commands,
|
||||
)
|
||||
|
||||
return length_prefixed_response(0xF, PluginAppBackwardsInvocation.convert_to_event_stream(response))
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/encrypt")
|
||||
class PluginInvokeEncryptApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeEncrypt)
|
||||
@inner_api_ns.doc("plugin_invoke_encrypt")
|
||||
@inner_api_ns.doc(description="Encrypt or decrypt data through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Encryption/decryption successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeEncrypt):
|
||||
"""
|
||||
encrypt or decrypt data
|
||||
"""
|
||||
try:
|
||||
return BaseBackwardsInvocationResponse(
|
||||
data=PluginEncrypter.invoke_encrypt(tenant_model, payload)
|
||||
).model_dump()
|
||||
except Exception as e:
|
||||
return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
|
||||
|
||||
|
||||
@inner_api_ns.route("/invoke/summary")
|
||||
class PluginInvokeSummaryApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestInvokeSummary)
|
||||
@inner_api_ns.doc("plugin_invoke_summary")
|
||||
@inner_api_ns.doc(description="Invoke summary functionality through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Summary generation successful",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestInvokeSummary):
|
||||
try:
|
||||
return BaseBackwardsInvocationResponse(
|
||||
data={
|
||||
"summary": PluginModelBackwardsInvocation.invoke_summary(
|
||||
user_id=user_model.id,
|
||||
tenant=tenant_model,
|
||||
payload=payload,
|
||||
)
|
||||
}
|
||||
).model_dump()
|
||||
except Exception as e:
|
||||
return BaseBackwardsInvocationResponse(error=str(e)).model_dump()
|
||||
|
||||
|
||||
@inner_api_ns.route("/upload/file/request")
|
||||
class PluginUploadFileRequestApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestRequestUploadFile)
|
||||
@inner_api_ns.doc("plugin_upload_file_request")
|
||||
@inner_api_ns.doc(description="Request signed URL for file upload through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Signed URL generated successfully",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestRequestUploadFile):
|
||||
# generate signed url
|
||||
uri = get_signed_file_uri_for_plugin(
|
||||
filename=payload.filename,
|
||||
mimetype=payload.mimetype,
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=user_model.id,
|
||||
conversation_id=payload.conversation_id,
|
||||
)
|
||||
url = bind_file_uri(uri, dify_config.INTERNAL_FILES_URL or dify_config.FILES_URL)
|
||||
return BaseBackwardsInvocationResponse(data={"url": url}).model_dump()
|
||||
|
||||
|
||||
@inner_api_ns.route("/download/file/request")
|
||||
class PluginDownloadFileRequestApi(Resource):
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestRequestDownloadFile)
|
||||
@inner_api_ns.doc("plugin_download_file_request")
|
||||
@inner_api_ns.doc(description="Request signed URL for file download through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "Signed URL generated successfully",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, payload: RequestRequestDownloadFile):
|
||||
"""Adapt a shared file request result to the Plugin backward contract.
|
||||
|
||||
``FileRequestService`` rebuilds the caller's ``FileAccessScope`` and
|
||||
resolves one origin-free signed URI. This controller binds that URI to
|
||||
the Plugin-selected external or internal files base URL, then returns
|
||||
the existing backward-invocation envelope.
|
||||
"""
|
||||
tenant_model = db.session.get(Tenant, payload.tenant_id)
|
||||
if tenant_model is None:
|
||||
raise ValueError("tenant not found")
|
||||
|
||||
result = FileRequestService().request_download(
|
||||
tenant_id=tenant_model.id,
|
||||
user_id=payload.user_id,
|
||||
user_from=payload.user_from,
|
||||
invoke_from=payload.invoke_from,
|
||||
file_mapping=payload.file.model_dump(mode="python", exclude_none=True),
|
||||
)
|
||||
base_url = (
|
||||
dify_config.FILES_URL if payload.for_external else (dify_config.INTERNAL_FILES_URL or dify_config.FILES_URL)
|
||||
)
|
||||
return BaseBackwardsInvocationResponse(
|
||||
data={
|
||||
"filename": result.filename,
|
||||
"mime_type": result.mime_type,
|
||||
"size": result.size,
|
||||
"download_url": bind_file_uri(result.download_uri, base_url),
|
||||
}
|
||||
).model_dump()
|
||||
|
||||
|
||||
@inner_api_ns.route("/fetch/app/info")
|
||||
class PluginFetchAppInfoApi(Resource):
|
||||
@get_user_tenant
|
||||
@setup_required
|
||||
@plugin_inner_api_only
|
||||
@plugin_data(payload_type=RequestFetchAppInfo)
|
||||
@inner_api_ns.doc("plugin_fetch_app_info")
|
||||
@inner_api_ns.doc(description="Fetch application information through plugin interface")
|
||||
@inner_api_ns.doc(
|
||||
responses={
|
||||
200: "App information retrieved successfully",
|
||||
401: "Unauthorized - invalid API key",
|
||||
404: "Service not available",
|
||||
}
|
||||
)
|
||||
def post(self, user_model: Account | EndUser, tenant_model: Tenant, payload: RequestFetchAppInfo):
|
||||
return BaseBackwardsInvocationResponse(
|
||||
data=PluginAppBackwardsInvocation.fetch_app_info(payload.app_id, tenant_model.id)
|
||||
).model_dump()
|
||||
@@ -0,0 +1,59 @@
|
||||
"""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"]
|
||||
@@ -0,0 +1,157 @@
|
||||
from collections.abc import Callable
|
||||
from functools import wraps
|
||||
from uuid import UUID
|
||||
|
||||
from flask import current_app, request
|
||||
from flask_login import user_logged_in
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from extensions.ext_database import db
|
||||
from libs.login import current_user
|
||||
from models.account import Tenant
|
||||
from models.enums import DEFAULT_END_USER_SESSION_ID, EndUserType
|
||||
from models.model import EndUser
|
||||
|
||||
|
||||
class TenantUserPayload(BaseModel):
|
||||
tenant_id: str
|
||||
user_id: str
|
||||
|
||||
|
||||
def get_user(tenant_id: str, user_id: str | None) -> EndUser:
|
||||
"""
|
||||
Get current user.
|
||||
|
||||
NOTE: user_id is not trusted, it could be maliciously set to any value.
|
||||
As a result, it could only be considered as an end user id. Even when a
|
||||
concrete end-user ID is supplied, lookups must stay tenant-scoped so one
|
||||
tenant cannot bind another tenant's user record into the plugin request
|
||||
context.
|
||||
"""
|
||||
if not user_id:
|
||||
user_id = DEFAULT_END_USER_SESSION_ID
|
||||
is_anonymous = user_id == DEFAULT_END_USER_SESSION_ID
|
||||
try:
|
||||
with sessionmaker(db.engine, expire_on_commit=False).begin() as session:
|
||||
user_model = None
|
||||
|
||||
if is_anonymous:
|
||||
user_model = session.scalar(
|
||||
select(EndUser)
|
||||
.where(
|
||||
EndUser.session_id == user_id,
|
||||
EndUser.tenant_id == tenant_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
else:
|
||||
# Try id first (preserves the original "explicit end-user
|
||||
# id → that specific user" semantics for callers that pass
|
||||
# a known EndUser.id). Fall back to session_id so daemon-
|
||||
# supplied session UUIDs dedup against the row created on
|
||||
# the first Reverse Invocation call — without this, an
|
||||
# id-only lookup never matched (create writes user_id to
|
||||
# session_id, id is auto-generated) and a fresh EndUser
|
||||
# was created per call, breaking multi-turn chat
|
||||
# continuation (see #36736).
|
||||
if _is_uuid(user_id):
|
||||
user_model = session.scalar(
|
||||
select(EndUser)
|
||||
.where(
|
||||
EndUser.id == user_id,
|
||||
EndUser.tenant_id == tenant_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
if user_model is None:
|
||||
user_model = session.scalar(
|
||||
select(EndUser)
|
||||
.where(
|
||||
EndUser.session_id == user_id,
|
||||
EndUser.tenant_id == tenant_id,
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
|
||||
if not user_model:
|
||||
user_model = EndUser(
|
||||
tenant_id=tenant_id,
|
||||
type=EndUserType.SERVICE_API,
|
||||
_is_anonymous=is_anonymous,
|
||||
session_id=user_id,
|
||||
)
|
||||
session.add(user_model)
|
||||
session.flush()
|
||||
session.refresh(user_model)
|
||||
|
||||
except Exception as e:
|
||||
raise ValueError("user not found") from e
|
||||
|
||||
return user_model
|
||||
|
||||
|
||||
def _is_uuid(value: str) -> bool:
|
||||
try:
|
||||
UUID(value)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_user_tenant[**P, R](view_func: Callable[P, R]) -> Callable[P, R]:
|
||||
@wraps(view_func)
|
||||
def decorated_view(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
payload = TenantUserPayload.model_validate(request.get_json(silent=True) or {})
|
||||
|
||||
user_id = payload.user_id
|
||||
tenant_id = payload.tenant_id
|
||||
|
||||
if not tenant_id:
|
||||
raise ValueError("tenant_id is required")
|
||||
|
||||
if not user_id:
|
||||
user_id = DEFAULT_END_USER_SESSION_ID
|
||||
|
||||
tenant_model = db.session.get(Tenant, tenant_id)
|
||||
|
||||
if not tenant_model:
|
||||
raise ValueError("tenant not found")
|
||||
|
||||
kwargs["tenant_model"] = tenant_model
|
||||
|
||||
user = get_user(tenant_id, user_id)
|
||||
kwargs["user_model"] = user
|
||||
|
||||
current_app.login_manager._update_request_context_with_user(user) # type: ignore
|
||||
user_logged_in.send(current_app._get_current_object(), user=current_user) # type: ignore
|
||||
|
||||
return view_func(*args, **kwargs)
|
||||
|
||||
return decorated_view
|
||||
|
||||
|
||||
def plugin_data[**P, R](
|
||||
*,
|
||||
payload_type: type[BaseModel],
|
||||
) -> Callable[[Callable[P, R]], Callable[P, R]]:
|
||||
def decorator(view_func: Callable[P, R]) -> Callable[P, R]:
|
||||
@wraps(view_func)
|
||||
def decorated_view(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
try:
|
||||
data = request.get_json()
|
||||
except Exception as e:
|
||||
raise ValueError("invalid json") from e
|
||||
|
||||
try:
|
||||
payload = payload_type.model_validate(data)
|
||||
except Exception as e:
|
||||
raise ValueError(f"invalid payload: {str(e)}") from e
|
||||
|
||||
kwargs["payload"] = payload
|
||||
return view_func(*args, **kwargs)
|
||||
|
||||
return decorated_view
|
||||
|
||||
return decorator
|
||||
@@ -0,0 +1,222 @@
|
||||
"""Inner API endpoints for runtime credential resolution.
|
||||
|
||||
Called by Enterprise while resolving AppRunner runtime artifacts. The endpoint
|
||||
returns decrypted model and tool credentials for in-memory runtime use only.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from json import JSONDecodeError
|
||||
from typing import Any
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from controllers.common.fields import SimpleMessageResponse
|
||||
from controllers.common.schema import (
|
||||
register_response_schema_models,
|
||||
register_schema_model,
|
||||
)
|
||||
from controllers.console.wraps import model_validate, setup_required
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import enterprise_inner_api_only
|
||||
from core.helper import encrypter
|
||||
from core.helper.provider_cache import ToolProviderCredentialsCache
|
||||
from core.helper.provider_encryption import create_provider_encrypter
|
||||
from core.plugin.impl.model_runtime_factory import create_plugin_provider_manager
|
||||
from core.tools.tool_manager import ToolManager
|
||||
from extensions.ext_database import db
|
||||
from fields.base import ResponseModel
|
||||
from models.provider import ProviderCredential
|
||||
from models.tools import BuiltinToolProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_KIND_MODEL = "model"
|
||||
_KIND_TOOL = "tool"
|
||||
|
||||
# (body, status) pair returned by a resolver helper when resolution fails.
|
||||
ResolveError = tuple[dict[str, str], int]
|
||||
|
||||
|
||||
class InnerRuntimeCredentialResolveItem(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
credential_id: str = Field(description="Credential id")
|
||||
provider: str = Field(description="Runtime provider identifier, for example langgenius/openai/openai")
|
||||
kind: str = Field(description="Credential kind, either 'model' or 'tool'")
|
||||
|
||||
|
||||
class InnerRuntimeCredentialsResolvePayload(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
tenant_id: str = Field(description="Workspace id")
|
||||
credentials: list[InnerRuntimeCredentialResolveItem] = Field(default_factory=list)
|
||||
|
||||
|
||||
class InnerRuntimeCredentialResponse(ResponseModel):
|
||||
credential_id: str
|
||||
provider: str
|
||||
kind: str
|
||||
values: dict[str, Any]
|
||||
|
||||
|
||||
class InnerRuntimeCredentialsResolveResponse(ResponseModel):
|
||||
credentials: list[InnerRuntimeCredentialResponse] = Field(default_factory=list)
|
||||
|
||||
|
||||
register_schema_model(inner_api_ns, InnerRuntimeCredentialsResolvePayload)
|
||||
register_response_schema_models(inner_api_ns, InnerRuntimeCredentialResponse, InnerRuntimeCredentialsResolveResponse)
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/credentials/resolve")
|
||||
class EnterpriseRuntimeCredentialsResolve(Resource):
|
||||
@setup_required
|
||||
@enterprise_inner_api_only
|
||||
@inner_api_ns.doc("enterprise_runtime_credentials_resolve")
|
||||
@inner_api_ns.expect(inner_api_ns.models[InnerRuntimeCredentialsResolvePayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200, "Credentials resolved", inner_api_ns.models[InnerRuntimeCredentialsResolveResponse.__name__]
|
||||
)
|
||||
@inner_api_ns.response(
|
||||
400, "Invalid request or credential config", inner_api_ns.models[SimpleMessageResponse.__name__]
|
||||
)
|
||||
@inner_api_ns.response(404, "Provider or credential not found", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
@model_validate(InnerRuntimeCredentialsResolvePayload)
|
||||
def post(self, args: InnerRuntimeCredentialsResolvePayload):
|
||||
if not args.credentials:
|
||||
return InnerRuntimeCredentialsResolveResponse(credentials=[]).model_dump(mode="json"), 200
|
||||
|
||||
# Model resolution shares one provider configuration set; build it lazily
|
||||
# so a tool-only request never pays for the plugin daemon round trip.
|
||||
model_configurations = None
|
||||
|
||||
resolved: list[InnerRuntimeCredentialResponse] = []
|
||||
for item in args.credentials:
|
||||
if item.kind == _KIND_MODEL:
|
||||
if model_configurations is None:
|
||||
provider_manager = create_plugin_provider_manager(tenant_id=args.tenant_id)
|
||||
model_configurations = provider_manager.get_configurations(args.tenant_id)
|
||||
values, error = _resolve_model(args.tenant_id, model_configurations, item)
|
||||
elif item.kind == _KIND_TOOL:
|
||||
values, error = _resolve_tool(args.tenant_id, item)
|
||||
else:
|
||||
return {"message": f"unsupported credential kind '{item.kind}'"}, 400
|
||||
|
||||
if values is None:
|
||||
return error
|
||||
resolved.append(
|
||||
InnerRuntimeCredentialResponse(
|
||||
credential_id=item.credential_id,
|
||||
kind=item.kind,
|
||||
provider=item.provider,
|
||||
values=values,
|
||||
)
|
||||
)
|
||||
|
||||
return InnerRuntimeCredentialsResolveResponse(credentials=resolved).model_dump(mode="json"), 200
|
||||
|
||||
|
||||
def _resolve_model(
|
||||
tenant_id: str, provider_configurations: Any, item: InnerRuntimeCredentialResolveItem
|
||||
) -> tuple[dict[str, Any] | None, ResolveError | None]:
|
||||
provider_configuration = provider_configurations.get(item.provider)
|
||||
if provider_configuration is None:
|
||||
return None, ({"message": f"provider '{item.provider}' not found"}, 404)
|
||||
|
||||
provider_schema = provider_configuration.provider.provider_credential_schema
|
||||
secret_variables = provider_configuration.extract_secret_variables(
|
||||
provider_schema.credential_form_schemas if provider_schema else []
|
||||
)
|
||||
|
||||
with Session(db.engine) as session:
|
||||
stmt = select(ProviderCredential).where(
|
||||
ProviderCredential.id == item.credential_id,
|
||||
ProviderCredential.tenant_id == tenant_id,
|
||||
ProviderCredential.provider_name.in_(provider_configuration._get_provider_names()),
|
||||
)
|
||||
credential = session.execute(stmt).scalar_one_or_none()
|
||||
|
||||
if credential is None or not credential.encrypted_config:
|
||||
return None, ({"message": f"credential '{item.credential_id}' not found"}, 404)
|
||||
|
||||
try:
|
||||
values = json.loads(credential.encrypted_config)
|
||||
except JSONDecodeError:
|
||||
return None, ({"message": f"credential '{item.credential_id}' has invalid config"}, 400)
|
||||
if not isinstance(values, dict):
|
||||
return None, ({"message": f"credential '{item.credential_id}' has invalid config"}, 400)
|
||||
|
||||
for key in secret_variables:
|
||||
value = values.get(key)
|
||||
if value is None:
|
||||
continue
|
||||
try:
|
||||
values[key] = encrypter.decrypt_token(tenant_id=tenant_id, token=value)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"failed to resolve runtime model credential",
|
||||
extra={
|
||||
"credential_id": item.credential_id,
|
||||
"provider": item.provider,
|
||||
"tenant_id": tenant_id,
|
||||
"error": type(exc).__name__,
|
||||
},
|
||||
)
|
||||
return None, ({"message": f"credential '{item.credential_id}' decrypt failed"}, 400)
|
||||
|
||||
return values, None
|
||||
|
||||
|
||||
def _resolve_tool(
|
||||
tenant_id: str, item: InnerRuntimeCredentialResolveItem
|
||||
) -> tuple[dict[str, Any] | None, ResolveError | None]:
|
||||
try:
|
||||
provider_controller = ToolManager.get_builtin_provider(item.provider, tenant_id)
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"failed to load runtime tool provider",
|
||||
extra={"provider": item.provider, "tenant_id": tenant_id, "error": type(exc).__name__},
|
||||
)
|
||||
return None, ({"message": f"tool provider '{item.provider}' not found"}, 404)
|
||||
|
||||
with Session(db.engine) as session:
|
||||
stmt = select(BuiltinToolProvider).where(
|
||||
BuiltinToolProvider.id == item.credential_id,
|
||||
BuiltinToolProvider.provider == item.provider,
|
||||
BuiltinToolProvider.tenant_id == tenant_id,
|
||||
)
|
||||
builtin_provider = session.execute(stmt).scalar_one_or_none()
|
||||
|
||||
if builtin_provider is None:
|
||||
return None, ({"message": f"credential '{item.credential_id}' not found"}, 404)
|
||||
|
||||
try:
|
||||
# Tool credentials are stored as a single encrypted dict; the secret
|
||||
# fields are decided by the schema bound to this credential type.
|
||||
provider_encrypter, _ = create_provider_encrypter(
|
||||
tenant_id=tenant_id,
|
||||
config=[
|
||||
schema.to_basic_provider_config()
|
||||
for schema in provider_controller.get_credentials_schema_by_type(builtin_provider.credential_type)
|
||||
],
|
||||
cache=ToolProviderCredentialsCache(
|
||||
tenant_id=tenant_id, provider=item.provider, credential_id=builtin_provider.id
|
||||
),
|
||||
)
|
||||
values = dict(provider_encrypter.decrypt(builtin_provider.credentials))
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"failed to resolve runtime tool credential",
|
||||
extra={
|
||||
"credential_id": item.credential_id,
|
||||
"provider": item.provider,
|
||||
"tenant_id": tenant_id,
|
||||
"error": type(exc).__name__,
|
||||
},
|
||||
)
|
||||
return None, ({"message": f"credential '{item.credential_id}' decrypt failed"}, 400)
|
||||
|
||||
return values, None
|
||||
Whitespace-only changes.
@@ -0,0 +1,202 @@
|
||||
from datetime import datetime
|
||||
from typing import Any
|
||||
from uuid import UUID
|
||||
|
||||
from flask_restx import Resource
|
||||
from pydantic import BaseModel
|
||||
|
||||
from controllers.common.errors import InvalidArgumentError
|
||||
from controllers.common.fields import SimpleMessageResponse
|
||||
from controllers.common.schema import register_response_schema_models, register_schema_models
|
||||
from controllers.console.wraps import setup_required, validate_request
|
||||
from controllers.inner_api import inner_api_ns
|
||||
from controllers.inner_api.wraps import inner_api_only
|
||||
from enums.account import TenantAccountRole
|
||||
from extensions.ext_application_services import application_services
|
||||
from fields.base import ResponseModel
|
||||
from services.account_errors import AccountNotFoundError
|
||||
from services.errors.workspace import (
|
||||
InvalidWorkspaceMemberRoleError,
|
||||
WorkspaceNotFoundError,
|
||||
WorkspaceOwnerNotFoundError,
|
||||
WorkspacesLimitExceededError,
|
||||
)
|
||||
|
||||
|
||||
class WorkspaceCreatePayload(BaseModel):
|
||||
name: str
|
||||
owner_email: str
|
||||
|
||||
|
||||
class WorkspaceOwnerlessPayload(BaseModel):
|
||||
name: str
|
||||
|
||||
|
||||
class WorkspaceMemberPayload(BaseModel):
|
||||
workspace_id: str
|
||||
account_id: str
|
||||
email: str
|
||||
role: str = TenantAccountRole.NORMAL.value
|
||||
current: bool = False
|
||||
operator_account_id: str | None = None
|
||||
|
||||
|
||||
register_schema_models(inner_api_ns, WorkspaceCreatePayload, WorkspaceOwnerlessPayload, WorkspaceMemberPayload)
|
||||
|
||||
|
||||
class WorkspaceResponse(ResponseModel):
|
||||
id: str
|
||||
name: str
|
||||
plan: str
|
||||
status: str
|
||||
created_at: str | None = None
|
||||
updated_at: str | None = None
|
||||
|
||||
|
||||
class WorkspaceOwnerlessTenantResponse(WorkspaceResponse):
|
||||
encrypt_public_key: str | None = None
|
||||
custom_config: dict[str, Any]
|
||||
|
||||
|
||||
class WorkspaceCreateResponse(ResponseModel):
|
||||
message: str
|
||||
tenant: WorkspaceResponse
|
||||
|
||||
|
||||
class WorkspaceOwnerlessCreateResponse(ResponseModel):
|
||||
message: str
|
||||
tenant: WorkspaceOwnerlessTenantResponse
|
||||
|
||||
|
||||
class WorkspaceMemberInfoResponse(ResponseModel):
|
||||
workspace_id: str
|
||||
account_id: str
|
||||
role: str
|
||||
|
||||
|
||||
class WorkspaceMemberResponse(ResponseModel):
|
||||
message: str
|
||||
member: WorkspaceMemberInfoResponse
|
||||
|
||||
|
||||
def _iso_utc(value: datetime | None) -> str | None:
|
||||
return value.isoformat() + "Z" if value else None
|
||||
|
||||
|
||||
register_response_schema_models(
|
||||
inner_api_ns,
|
||||
WorkspaceResponse,
|
||||
WorkspaceOwnerlessTenantResponse,
|
||||
WorkspaceCreateResponse,
|
||||
WorkspaceOwnerlessCreateResponse,
|
||||
WorkspaceMemberResponse,
|
||||
SimpleMessageResponse,
|
||||
)
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/workspace")
|
||||
class EnterpriseWorkspace(Resource):
|
||||
@setup_required
|
||||
@inner_api_only
|
||||
@inner_api_ns.doc("create_enterprise_workspace")
|
||||
@inner_api_ns.doc(description="Create a new enterprise workspace with owner assignment")
|
||||
@inner_api_ns.expect(inner_api_ns.models[WorkspaceCreatePayload.__name__])
|
||||
@inner_api_ns.response(200, "Workspace created successfully", inner_api_ns.models[WorkspaceCreateResponse.__name__])
|
||||
@inner_api_ns.response(404, "Owner account not found", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
def post(self):
|
||||
args = validate_request(WorkspaceCreatePayload)
|
||||
try:
|
||||
tenant = application_services().workspaces.provisioning.create(name=args.name, owner_email=args.owner_email)
|
||||
except WorkspaceOwnerNotFoundError:
|
||||
return {"message": "owner account not found."}, 404
|
||||
except WorkspacesLimitExceededError as exc:
|
||||
raise InvalidArgumentError(str(exc)) from exc
|
||||
return WorkspaceCreateResponse(
|
||||
message="enterprise workspace created.",
|
||||
tenant=WorkspaceResponse(
|
||||
id=tenant.id,
|
||||
name=tenant.name,
|
||||
plan=tenant.plan,
|
||||
status=tenant.status,
|
||||
created_at=_iso_utc(tenant.created_at),
|
||||
updated_at=_iso_utc(tenant.updated_at),
|
||||
),
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/workspace/ownerless")
|
||||
class EnterpriseWorkspaceNoOwnerEmail(Resource):
|
||||
@setup_required
|
||||
@inner_api_only
|
||||
@inner_api_ns.doc("create_enterprise_workspace_ownerless")
|
||||
@inner_api_ns.doc(description="Create a new enterprise workspace without initial owner assignment")
|
||||
@inner_api_ns.expect(inner_api_ns.models[WorkspaceOwnerlessPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200, "Workspace created successfully", inner_api_ns.models[WorkspaceOwnerlessCreateResponse.__name__]
|
||||
)
|
||||
def post(self):
|
||||
args = validate_request(WorkspaceOwnerlessPayload)
|
||||
tenant = application_services().workspaces.provisioning.create(name=args.name)
|
||||
return WorkspaceOwnerlessCreateResponse(
|
||||
message="enterprise workspace created.",
|
||||
tenant=WorkspaceOwnerlessTenantResponse(
|
||||
id=tenant.id,
|
||||
name=tenant.name,
|
||||
plan=tenant.plan,
|
||||
status=tenant.status,
|
||||
created_at=_iso_utc(tenant.created_at),
|
||||
updated_at=_iso_utc(tenant.updated_at),
|
||||
encrypt_public_key=tenant.encrypt_public_key,
|
||||
custom_config=tenant.custom_config,
|
||||
),
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/workspace/member")
|
||||
class EnterpriseWorkspaceMember(Resource):
|
||||
@setup_required
|
||||
@inner_api_only
|
||||
@inner_api_ns.doc("join_enterprise_workspace_member")
|
||||
@inner_api_ns.doc(description="Add an existing account to an enterprise workspace")
|
||||
@inner_api_ns.expect(inner_api_ns.models[WorkspaceMemberPayload.__name__])
|
||||
@inner_api_ns.response(
|
||||
200, "Workspace member joined successfully", inner_api_ns.models[WorkspaceMemberResponse.__name__]
|
||||
)
|
||||
@inner_api_ns.response(400, "Invalid workspace member role", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
@inner_api_ns.response(404, "Workspace or account not found", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
def post(self):
|
||||
args = validate_request(WorkspaceMemberPayload)
|
||||
try:
|
||||
member = application_services().workspaces.provisioning.join_member(
|
||||
workspace_id=args.workspace_id,
|
||||
account_id=args.account_id,
|
||||
email=args.email,
|
||||
role=args.role,
|
||||
operator_account_id=args.operator_account_id,
|
||||
)
|
||||
except InvalidWorkspaceMemberRoleError as exc:
|
||||
return {"message": str(exc)}, 400
|
||||
except WorkspaceNotFoundError:
|
||||
return {"message": "workspace not found."}, 404
|
||||
except AccountNotFoundError:
|
||||
return {"message": "account not found."}, 404
|
||||
return WorkspaceMemberResponse(
|
||||
message="enterprise workspace member joined.",
|
||||
member=WorkspaceMemberInfoResponse(
|
||||
workspace_id=member.workspace_id,
|
||||
account_id=member.account_id,
|
||||
role=member.role,
|
||||
),
|
||||
).model_dump(mode="json")
|
||||
|
||||
|
||||
@inner_api_ns.route("/enterprise/workspace/<uuid:workspace_id>/resources")
|
||||
class EnterpriseWorkspaceResourceDeletion(Resource):
|
||||
@setup_required
|
||||
@inner_api_only
|
||||
@inner_api_ns.doc("delete_enterprise_workspace_resources")
|
||||
@inner_api_ns.doc(description="Schedule App, Dataset and Agent cleanup; retain the workspace and memberships")
|
||||
@inner_api_ns.response(202, "Cleanup accepted", inner_api_ns.models[SimpleMessageResponse.__name__])
|
||||
def delete(self, workspace_id: UUID):
|
||||
application_services().workspaces.resource_cleanup.cleanup(str(workspace_id))
|
||||
return SimpleMessageResponse(message="workspace resource cleanup accepted.").model_dump(mode="json"), 202
|
||||
@@ -0,0 +1,118 @@
|
||||
from base64 import b64encode
|
||||
from collections.abc import Callable
|
||||
from functools import wraps
|
||||
from hashlib import sha1
|
||||
from hmac import new as hmac_new
|
||||
|
||||
from flask import abort, request
|
||||
|
||||
from configs import dify_config
|
||||
from core.db.session_factory import session_factory
|
||||
from libs.exception import BaseHTTPException
|
||||
from models.model import EndUser
|
||||
from services.knowledge_fs.feature_service import get_knowledge_fs_features
|
||||
|
||||
|
||||
class InnerApiUnauthorizedError(BaseHTTPException):
|
||||
error_code = "inner_api_unauthorized"
|
||||
description = "Unauthorized."
|
||||
code = 401
|
||||
|
||||
|
||||
def inner_api_only[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
"""Restrict access to callers authenticated with the shared inner API key."""
|
||||
|
||||
@wraps(view)
|
||||
def decorated(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
if not dify_config.INNER_API:
|
||||
abort(404)
|
||||
|
||||
inner_api_key = request.headers.get("X-Inner-Api-Key")
|
||||
if not inner_api_key or inner_api_key != dify_config.INNER_API_KEY:
|
||||
raise InnerApiUnauthorizedError()
|
||||
|
||||
return view(*args, **kwargs)
|
||||
|
||||
return decorated
|
||||
|
||||
|
||||
def enterprise_inner_api_only[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
return inner_api_only(view)
|
||||
|
||||
|
||||
def enterprise_inner_api_user_auth[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
"""Inject an EndUser for valid inner API HMAC auth, otherwise pass the request through unchanged."""
|
||||
|
||||
@wraps(view)
|
||||
def decorated(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
if not dify_config.INNER_API:
|
||||
return view(*args, **kwargs)
|
||||
|
||||
# get header 'X-Inner-Api-Key'
|
||||
authorization = request.headers.get("Authorization")
|
||||
if not authorization:
|
||||
return view(*args, **kwargs)
|
||||
|
||||
parts = authorization.split(":")
|
||||
if len(parts) != 2:
|
||||
return view(*args, **kwargs)
|
||||
|
||||
user_id, token = parts
|
||||
if " " in user_id:
|
||||
user_id = user_id.split(" ")[1]
|
||||
|
||||
inner_api_key = request.headers.get("X-Inner-Api-Key", "")
|
||||
|
||||
data_to_sign = f"DIFY {user_id}"
|
||||
|
||||
signature = hmac_new(inner_api_key.encode("utf-8"), data_to_sign.encode("utf-8"), sha1)
|
||||
signature_base64 = b64encode(signature.digest()).decode("utf-8")
|
||||
|
||||
if signature_base64 != token:
|
||||
return view(*args, **kwargs)
|
||||
|
||||
with session_factory.create_session() as session:
|
||||
kwargs["user"] = session.get(EndUser, user_id)
|
||||
return view(*args, **kwargs)
|
||||
|
||||
return decorated
|
||||
|
||||
|
||||
def plugin_inner_api_only[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
@wraps(view)
|
||||
def decorated(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
if not dify_config.PLUGIN_DAEMON_KEY:
|
||||
abort(404)
|
||||
|
||||
# get header 'X-Inner-Api-Key'
|
||||
inner_api_key = request.headers.get("X-Inner-Api-Key")
|
||||
if not inner_api_key or inner_api_key != dify_config.INNER_API_KEY_FOR_PLUGIN:
|
||||
abort(404)
|
||||
|
||||
return view(*args, **kwargs)
|
||||
|
||||
return decorated
|
||||
|
||||
|
||||
def agent_inner_api_only[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
"""Temporary alias for agent-backend inner API callers.
|
||||
|
||||
Agent tool and knowledge calls currently share the same trusted
|
||||
`dify-agent -> Dify API` transport credentials as the plugin inner bridge.
|
||||
Keep the wrapper name agent-specific so the controller surface does not grow
|
||||
more plugin-only semantics while auth settings stay shared.
|
||||
"""
|
||||
|
||||
return plugin_inner_api_only(view)
|
||||
|
||||
|
||||
def knowledge_fs_inner_api_only[**P, R](view: Callable[P, R]) -> Callable[P, R]:
|
||||
"""Authenticate the trusted KnowledgeFS process while the product is available."""
|
||||
|
||||
@wraps(view)
|
||||
def enabled_view(*args: P.args, **kwargs: P.kwargs) -> R:
|
||||
if not get_knowledge_fs_features().enabled:
|
||||
abort(404)
|
||||
return view(*args, **kwargs)
|
||||
|
||||
return plugin_inner_api_only(enabled_view)
|
||||
Reference in new issue
Block a user