Files
dify/api/controllers/trigger/trigger.py
T
9547c33067 feat: add end-user authentication and connections (SNP-911) (#47362)
Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-11-30 03:18:09 +00:00

68 lines
3.1 KiB
Python

import logging
import re
from http import HTTPStatus
from flask import Response, jsonify, request
from controllers.common.access_response import plugin_endpoint_not_found_response
from controllers.trigger import bp
from core.plugin.entities.plugin_daemon import PluginDaemonInnerError
from extensions.ext_application_services import application_services
from services.trigger.endpoint_service import (
TriggerEndpointEventNotFoundError,
TriggerEndpointLoadError,
TriggerEndpointProviderNotFoundError,
)
logger = logging.getLogger(__name__)
UUID_PATTERN = r"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"
UUID_MATCHER = re.compile(UUID_PATTERN)
def _invalid_dispatch_response() -> tuple[Response, int]:
"""Return the client error without publishing plugin diagnostics."""
return (
jsonify({"error": "Endpoint processing failed", "message": "Invalid webhook request or plugin response."}),
HTTPStatus.BAD_REQUEST,
)
@bp.route("/plugin/<string:endpoint_id>", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"])
def trigger_endpoint(endpoint_id: str) -> Response | tuple[Response, int]:
"""Dispatch a valid endpoint ID; known provider rejections return a redacted 400."""
# endpoint_id must be UUID
if not UUID_MATCHER.match(endpoint_id):
return plugin_endpoint_not_found_response()
try:
response = application_services().triggers.endpoints.process(endpoint_id, request)
if response is None:
response = application_services().triggers.builders.process_builder_validation_endpoint(
endpoint_id, request
)
if response is None:
logger.info("Endpoint not found for %s", endpoint_id)
return plugin_endpoint_not_found_response()
return response
except TriggerEndpointProviderNotFoundError:
return Response(status=HTTPStatus.NOT_FOUND, response="Trigger provider not found")
except TriggerEndpointLoadError:
logger.exception("Failed to load trigger subscription for endpoint %s", endpoint_id)
return Response(status=HTTPStatus.INTERNAL_SERVER_ERROR, response="Failed to get subscription by endpoint")
except TriggerEndpointEventNotFoundError:
return (
jsonify({"error": "Endpoint processing failed", "message": "Plugin returned an undeclared trigger event."}),
HTTPStatus.BAD_REQUEST,
)
except PluginDaemonInnerError as error:
logger.warning("Webhook dispatch failed for endpoint %s with plugin code %s", endpoint_id, error.code)
if error.code > 0:
return _invalid_dispatch_response()
return jsonify({"error": "Internal server error"}), HTTPStatus.INTERNAL_SERVER_ERROR
except ValueError:
logger.exception("Invalid webhook request or plugin response for endpoint %s", endpoint_id)
return _invalid_dispatch_response()
except Exception:
logger.exception("Webhook processing failed for %s", endpoint_id)
return jsonify({"error": "Internal server error"}), HTTPStatus.INTERNAL_SERVER_ERROR