Files
dify-plugin-sdks/python/dify_plugin/interfaces/trigger/__init__.py
T
Yeuoly 5415dbafa9 feat: enhance plugin functionality with dynamic parameter options and trigger interface
- Added dynamic parameter fetching capabilities to the Plugin class.
- Introduced TriggerProvider and Trigger classes for managing trigger functionalities.
- Updated Tool interface to enforce implementation of credential validation and OAuth methods.
- Enhanced error messages for NotImplementedError in ToolProvider and Trigger classes.
- Removed deprecated tools and their configurations from the codebase.
2025-06-25 15:03:28 +08:00

100 lines
3.3 KiB
Python

from abc import ABC, abstractmethod
from collections.abc import Mapping
from typing import Any, final
from werkzeug import Request
from dify_plugin.core.runtime import Session
from dify_plugin.entities import ParameterOption
from dify_plugin.entities.trigger import TriggerResponse, TriggerRuntime
class TriggerProvider:
"""
The provider of a trigger
"""
def validate_credentials(self, credentials: dict):
return self._validate_credentials(credentials)
def _validate_credentials(self, credentials: dict):
raise NotImplementedError(
"This plugin should implement `_validate_credentials` method to enable credentials validation"
)
def oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str:
return self._oauth_get_authorization_url(system_credentials)
def _oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str:
raise NotImplementedError("This plugin should implement `_oauth_get_authorization_url` method to enable oauth")
def oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]:
return self._oauth_get_credentials(system_credentials, request)
def _oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]:
raise NotImplementedError("This plugin should implement `_oauth_get_credentials` method to enable oauth")
class Trigger(ABC):
"""
The trigger interface
"""
runtime: TriggerRuntime
session: Session
@final
def __init__(
self,
runtime: TriggerRuntime,
session: Session,
):
"""
Initialize the trigger
NOTE:
- This method has been marked as final, DO NOT OVERRIDE IT.
"""
self.runtime = runtime
self.session = session
############################################################
# Methods that can be implemented by plugin #
############################################################
@abstractmethod
def _trigger(self, request: Request, values: Mapping, parameters: Mapping) -> TriggerResponse:
"""
Trigger the trigger with the given request.
To be implemented by subclasses.
"""
def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]:
"""
Fetch the parameter options of the trigger.
To be implemented by subclasses.
Also, it's optional to implement, that's why it's not an abstract method.
"""
raise NotImplementedError(
"This plugin should implement `_fetch_parameter_options` method to enable dynamic select parameter"
)
############################################################
# For executor use only #
############################################################
def trigger(self, request: Request, values: Mapping, parameters: Mapping) -> TriggerResponse:
"""
Trigger the trigger with the given request.
"""
return self._trigger(request, values, parameters)
def fetch_parameter_options(self, parameter: str) -> list[ParameterOption]:
"""
Fetch the parameter options of the trigger.
"""
return self._fetch_parameter_options(parameter)