chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

@@ -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"
)