dify/api/core/plugin/manager/endpoint.py

117 lines
3.7 KiB
Python
Raw Normal View History

from core.plugin.entities.endpoint import EndpointEntityWithInstance
2024-09-20 13:55:09 +08:00
from core.plugin.manager.base import BasePluginManager
class PluginEndpointManager(BasePluginManager):
2024-10-08 23:48:38 +08:00
def create_endpoint(
self, tenant_id: str, user_id: str, plugin_unique_identifier: str, name: str, settings: dict
) -> bool:
2024-09-26 12:49:00 +08:00
"""
Create an endpoint for the given plugin.
Errors will be raised if any error occurs.
"""
2024-10-08 23:48:38 +08:00
return self._request_with_plugin_daemon_response(
2024-09-26 12:49:00 +08:00
"POST",
f"plugin/{tenant_id}/endpoint/setup",
2024-10-08 23:48:38 +08:00
bool,
2024-09-26 12:49:00 +08:00
headers={
"Content-Type": "application/json",
},
data={
"user_id": user_id,
"plugin_unique_identifier": plugin_unique_identifier,
"settings": settings,
2024-09-30 16:57:09 +08:00
"name": name,
2024-09-26 12:49:00 +08:00
},
)
2024-10-08 23:48:38 +08:00
def list_endpoints(self, tenant_id: str, user_id: str, page: int, page_size: int):
2024-09-26 12:49:00 +08:00
"""
List all endpoints for the given tenant and user.
"""
return self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/endpoint/list",
list[EndpointEntityWithInstance],
2024-10-08 23:48:38 +08:00
params={"page": page, "page_size": page_size},
2024-09-26 12:49:00 +08:00
)
def list_endpoints_for_single_plugin(self, tenant_id: str, user_id: str, plugin_id: str, page: int, page_size: int):
"""
List all endpoints for the given tenant, user and plugin.
"""
return self._request_with_plugin_daemon_response(
"GET",
f"plugin/{tenant_id}/endpoint/list/plugin",
list[EndpointEntityWithInstance],
params={"plugin_id": plugin_id, "page": page, "page_size": page_size},
)
2024-09-30 16:57:09 +08:00
def update_endpoint(self, tenant_id: str, user_id: str, endpoint_id: str, name: str, settings: dict):
2024-09-26 12:49:00 +08:00
"""
Update the settings of the given endpoint.
"""
2024-10-08 23:48:38 +08:00
return self._request_with_plugin_daemon_response(
2024-09-26 12:49:00 +08:00
"POST",
f"plugin/{tenant_id}/endpoint/update",
2024-10-08 23:48:38 +08:00
bool,
2024-09-26 12:49:00 +08:00
data={
2024-09-26 14:20:05 +08:00
"user_id": user_id,
2024-09-26 12:49:00 +08:00
"endpoint_id": endpoint_id,
2024-09-30 16:57:09 +08:00
"name": name,
2024-09-26 12:49:00 +08:00
"settings": settings,
},
2024-10-08 23:48:38 +08:00
headers={
"Content-Type": "application/json",
},
2024-09-26 12:49:00 +08:00
)
def delete_endpoint(self, tenant_id: str, user_id: str, endpoint_id: str):
"""
Delete the given endpoint.
"""
2024-10-08 23:48:38 +08:00
return self._request_with_plugin_daemon_response(
"POST",
2024-09-26 12:49:00 +08:00
f"plugin/{tenant_id}/endpoint/remove",
2024-10-08 23:48:38 +08:00
bool,
2024-09-26 12:49:00 +08:00
data={
"endpoint_id": endpoint_id,
},
2024-10-08 23:48:38 +08:00
headers={
"Content-Type": "application/json",
},
2024-09-26 12:49:00 +08:00
)
def enable_endpoint(self, tenant_id: str, user_id: str, endpoint_id: str):
"""
Enable the given endpoint.
"""
2024-10-08 23:48:38 +08:00
return self._request_with_plugin_daemon_response(
2024-09-26 12:49:00 +08:00
"POST",
f"plugin/{tenant_id}/endpoint/enable",
2024-10-08 23:48:38 +08:00
bool,
2024-09-26 12:49:00 +08:00
data={
"endpoint_id": endpoint_id,
},
2024-10-08 23:48:38 +08:00
headers={
"Content-Type": "application/json",
},
2024-09-26 12:49:00 +08:00
)
def disable_endpoint(self, tenant_id: str, user_id: str, endpoint_id: str):
"""
Disable the given endpoint.
"""
2024-10-08 23:48:38 +08:00
return self._request_with_plugin_daemon_response(
2024-09-26 12:49:00 +08:00
"POST",
f"plugin/{tenant_id}/endpoint/disable",
2024-10-08 23:48:38 +08:00
bool,
2024-09-26 12:49:00 +08:00
data={
"endpoint_id": endpoint_id,
},
2024-10-08 23:48:38 +08:00
headers={
"Content-Type": "application/json",
},
2024-09-26 12:49:00 +08:00
)