2024-09-20 13:55:09 +08:00
|
|
|
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
|
2024-09-20 14:43:01 +08:00
|
|
|
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL
|
2024-09-20 15:08:39 +08:00
|
|
|
plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY
|
2024-09-20 13:55:09 +08:00
|
|
|
|
2024-09-23 13:09:46 +08:00
|
|
|
T = TypeVar("T", bound=(BaseModel | dict | list | bool))
|
2024-09-20 13:55:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BasePluginManager:
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request(
|
2024-09-20 21:35:19 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
2024-09-23 13:09:46 +08:00
|
|
|
params: dict | None = None,
|
2024-09-20 21:35:19 +08:00
|
|
|
stream: bool = False,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> requests.Response:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API.
|
|
|
|
"""
|
|
|
|
url = URL(str(plugin_daemon_inner_api_baseurl)) / path
|
2024-09-20 14:43:01 +08:00
|
|
|
headers = headers or {}
|
2024-09-20 13:55:09 +08:00
|
|
|
headers["X-Api-Key"] = plugin_daemon_inner_api_key
|
2024-09-23 13:09:46 +08:00
|
|
|
response = requests.request(
|
|
|
|
method=method, url=str(url), headers=headers, data=data, params=params, stream=stream
|
|
|
|
)
|
2024-09-20 13:55:09 +08:00
|
|
|
return response
|
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _stream_request(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
params: dict | None = None,
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> Generator[bytes, None, None]:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API
|
|
|
|
"""
|
2024-09-23 13:09:46 +08:00
|
|
|
response = self._request(method, path, headers, data, params, stream=True)
|
2024-09-20 13:55:09 +08:00
|
|
|
yield from response.iter_lines()
|
|
|
|
|
|
|
|
def _stream_request_with_model(
|
2024-09-20 14:43:01 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
2024-09-20 21:35:19 +08:00
|
|
|
data: bytes | dict | None = None,
|
2024-09-23 13:09:46 +08:00
|
|
|
params: dict | None = None,
|
2024-09-20 13:55:09 +08:00
|
|
|
) -> Generator[T, None, None]:
|
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API and yield the response as a model.
|
|
|
|
"""
|
2024-09-23 13:09:46 +08:00
|
|
|
for line in self._stream_request(method, path, params, headers, data):
|
2024-09-20 13:55:09 +08:00
|
|
|
yield type(**json.loads(line))
|
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request_with_model(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | None = None,
|
|
|
|
params: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> T:
|
2024-09-20 13:55:09 +08:00
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API and return the response as a model.
|
|
|
|
"""
|
2024-09-23 13:09:46 +08:00
|
|
|
response = self._request(method, path, headers, data, params)
|
2024-09-20 13:55:09 +08:00
|
|
|
return type(**response.json())
|
2024-09-20 14:43:01 +08:00
|
|
|
|
|
|
|
def _request_with_plugin_daemon_response(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
|
|
|
params: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> T:
|
|
|
|
"""
|
|
|
|
Make a request to the plugin daemon inner API and return the response as a model.
|
|
|
|
"""
|
2024-09-23 13:09:46 +08:00
|
|
|
response = self._request(method, path, headers, data, params)
|
2024-09-23 18:06:16 +08:00
|
|
|
json_response = response.json()
|
|
|
|
for provider in json_response.get("data", []):
|
|
|
|
declaration = provider.get("declaration", {}) or {}
|
|
|
|
provider_name = declaration.get("identity", {}).get("name")
|
|
|
|
for tool in declaration.get("tools", []):
|
|
|
|
tool["identity"]["provider"] = provider_name
|
|
|
|
|
|
|
|
rep = PluginDaemonBasicResponse[type](**json_response)
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.code != 0:
|
2024-09-20 15:08:39 +08:00
|
|
|
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.data is None:
|
2024-09-20 15:08:39 +08:00
|
|
|
raise ValueError("got empty data from plugin daemon")
|
2024-09-20 21:35:19 +08:00
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
return rep.data
|
2024-09-20 21:35:19 +08:00
|
|
|
|
2024-09-20 14:43:01 +08:00
|
|
|
def _request_with_plugin_daemon_response_stream(
|
2024-09-23 13:09:46 +08:00
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
path: str,
|
|
|
|
type: type[T],
|
|
|
|
headers: dict | None = None,
|
|
|
|
data: bytes | dict | None = None,
|
|
|
|
params: dict | None = None,
|
2024-09-20 14:43:01 +08:00
|
|
|
) -> Generator[T, None, None]:
|
|
|
|
"""
|
|
|
|
Make a stream request to the plugin daemon inner API and yield the response as a model.
|
|
|
|
"""
|
2024-09-23 13:09:46 +08:00
|
|
|
for line in self._stream_request(method, path, params, headers, data):
|
2024-09-20 14:43:01 +08:00
|
|
|
line_data = json.loads(line)
|
|
|
|
rep = PluginDaemonBasicResponse[type](**line_data)
|
|
|
|
if rep.code != 0:
|
2024-09-20 21:35:19 +08:00
|
|
|
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
|
2024-09-20 14:43:01 +08:00
|
|
|
if rep.data is None:
|
2024-09-20 21:35:19 +08:00
|
|
|
raise ValueError("got empty data from plugin daemon")
|
|
|
|
yield rep.data
|