From d4bf575d0a87641bc64e0ab6eccfdcb8f9323d41 Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Fri, 20 Sep 2024 13:55:09 +0800 Subject: [PATCH] impl: basic plugin manager --- api/configs/feature/__init__.py | 2 +- api/core/plugin/manager/asset.py | 5 +++ api/core/plugin/manager/base.py | 48 +++++++++++++++++++++++++++++ api/core/plugin/manager/endpoint.py | 5 +++ api/core/plugin/manager/model.py | 5 +++ api/core/plugin/manager/tool.py | 5 +++ 6 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 api/core/plugin/manager/asset.py create mode 100644 api/core/plugin/manager/base.py create mode 100644 api/core/plugin/manager/endpoint.py create mode 100644 api/core/plugin/manager/model.py create mode 100644 api/core/plugin/manager/tool.py diff --git a/api/configs/feature/__init__.py b/api/configs/feature/__init__.py index 30b97749ee..660ca5a2a0 100644 --- a/api/configs/feature/__init__.py +++ b/api/configs/feature/__init__.py @@ -115,7 +115,7 @@ class PluginConfig(BaseSettings): """ Plugin configs """ - PLUGIN_API_URL: str = Field( + PLUGIN_API_URL: HttpUrl = Field( description='Plugin API URL', default='http://plugin:5002', ) diff --git a/api/core/plugin/manager/asset.py b/api/core/plugin/manager/asset.py new file mode 100644 index 0000000000..f55ff748ca --- /dev/null +++ b/api/core/plugin/manager/asset.py @@ -0,0 +1,5 @@ +from core.plugin.manager.base import BasePluginManager + + +class PluginAssetManager(BasePluginManager): + pass \ No newline at end of file diff --git a/api/core/plugin/manager/base.py b/api/core/plugin/manager/base.py new file mode 100644 index 0000000000..5f1e5cd1d2 --- /dev/null +++ b/api/core/plugin/manager/base.py @@ -0,0 +1,48 @@ +import json +from collections.abc import Generator +from typing import TypeVar + +import requests +from pydantic import BaseModel +from yarl import URL + +from configs import dify_config + +plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL +plugin_daemon_inner_api_key = dify_config.INNER_API_KEY_FOR_PLUGIN + +T = TypeVar("T", bound=(BaseModel | dict)) + + +class BasePluginManager: + def _request(self, method: str, path: str, headers: dict, data: bytes, stream: bool = False) -> requests.Response: + """ + Make a request to the plugin daemon inner API. + """ + url = URL(str(plugin_daemon_inner_api_baseurl)) / path + headers["X-Api-Key"] = plugin_daemon_inner_api_key + response = requests.request(method=method, url=str(url), headers=headers, data=data, stream=stream) + return response + + def _stream_request(self, method: str, path: str, headers: dict, data: bytes) -> Generator[bytes, None, None]: + """ + Make a stream request to the plugin daemon inner API + """ + response = self._request(method, path, headers, data, stream=True) + yield from response.iter_lines() + + def _stream_request_with_model( + self, method: str, path: str, headers: dict, data: bytes, type: type[T] + ) -> Generator[T, None, None]: + """ + Make a stream request to the plugin daemon inner API and yield the response as a model. + """ + for line in self._stream_request(method, path, headers, data): + yield type(**json.loads(line)) + + def _request_with_model(self, method: str, path: str, headers: dict, data: bytes, type: type[T]) -> T: + """ + Make a request to the plugin daemon inner API and return the response as a model. + """ + response = self._request(method, path, headers, data) + return type(**response.json()) diff --git a/api/core/plugin/manager/endpoint.py b/api/core/plugin/manager/endpoint.py new file mode 100644 index 0000000000..a3f49903fd --- /dev/null +++ b/api/core/plugin/manager/endpoint.py @@ -0,0 +1,5 @@ +from core.plugin.manager.base import BasePluginManager + + +class PluginEndpointManager(BasePluginManager): + pass \ No newline at end of file diff --git a/api/core/plugin/manager/model.py b/api/core/plugin/manager/model.py new file mode 100644 index 0000000000..f03dbfd1e3 --- /dev/null +++ b/api/core/plugin/manager/model.py @@ -0,0 +1,5 @@ +from core.plugin.manager.base import BasePluginManager + + +class PluginModelManager(BasePluginManager): + pass \ No newline at end of file diff --git a/api/core/plugin/manager/tool.py b/api/core/plugin/manager/tool.py new file mode 100644 index 0000000000..83517f1caf --- /dev/null +++ b/api/core/plugin/manager/tool.py @@ -0,0 +1,5 @@ +from core.plugin.manager.base import BasePluginManager + + +class PluginToolManager(BasePluginManager): + pass \ No newline at end of file