浏览代码

feat: download pkg from marketplace (#9184)

Junyan Qin 8 月之前
父节点
当前提交
b58f8dd7b4

+ 4 - 1
api/.env.example

@@ -297,4 +297,7 @@ PLUGIN_API_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi+vRjI/+Xb
 PLUGIN_API_URL=http://127.0.0.1:5002
 PLUGIN_REMOTE_INSTALL_PORT=5003
 PLUGIN_REMOTE_INSTALL_HOST=localhost
-INNER_API_KEY=QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1
+INNER_API_KEY=QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1
+
+# Marketplace configuration
+MARKETPLACE_API_URL=https://marketplace.dify.ai

+ 11 - 0
api/configs/feature/__init__.py

@@ -137,6 +137,16 @@ class PluginConfig(BaseSettings):
         default=5003,
     )
 
+class MarketplaceConfig(BaseSettings):
+    """
+    Configuration for marketplace
+    """
+
+    MARKETPLACE_API_URL: HttpUrl = Field(
+        description="Marketplace API URL",
+        default="https://marketplace.dify.ai",
+    )
+
 
 class EndpointConfig(BaseSettings):
     """
@@ -636,6 +646,7 @@ class FeatureConfig(
     BillingConfig,
     CodeExecutionSandboxConfig,
     PluginConfig,
+    MarketplaceConfig,
     DataSetConfig,
     EndpointConfig,
     FileAccessConfig,

+ 13 - 0
api/core/helper/marketplace.py

@@ -0,0 +1,13 @@
+from yarl import URL
+
+from configs import dify_config
+from core.helper.download import download_with_size_limit
+
+
+def get_plugin_pkg_url(plugin_unique_identifier: str):
+    return (URL(str(dify_config.MARKETPLACE_API_URL)) / "api/v1/plugins/download").with_query(unique_identifier=plugin_unique_identifier)
+
+
+def download_plugin_pkg(plugin_unique_identifier: str):
+    url = str(get_plugin_pkg_url(plugin_unique_identifier))
+    return download_with_size_limit(url, 15 * 1024 * 1024)

+ 2 - 1
api/services/plugin/plugin_service.py

@@ -2,6 +2,7 @@ from collections.abc import Generator
 from mimetypes import guess_type
 
 from core.helper.download import download_with_size_limit
+from core.helper.marketplace import download_plugin_pkg
 from core.plugin.entities.plugin import PluginEntity, PluginInstallationSource
 from core.plugin.entities.plugin_daemon import InstallPluginMessage, PluginDaemonInnerError
 from core.plugin.manager.asset import PluginAssetManager
@@ -83,7 +84,7 @@ class PluginService:
         """
         manager = PluginInstallationManager()
 
-        pkg = b""
+        pkg = download_plugin_pkg(plugin_unique_identifier)
 
         try:
             yield from manager.install_from_pkg(

+ 4 - 1
api/tests/integration_tests/.env.example

@@ -87,4 +87,7 @@ ZHINAO_API_KEY=
 # Plugin configuration
 PLUGIN_API_KEY=
 PLUGIN_API_URL=
-INNER_API_KEY=
+INNER_API_KEY=
+
+# Marketplace configuration
+MARKETPLACE_API_URL=

+ 7 - 0
api/tests/unit_tests/core/helper/test_marketplace.py

@@ -0,0 +1,7 @@
+from core.helper.marketplace import download_plugin_pkg
+
+
+def test_download_plugin_pkg():
+    pkg = download_plugin_pkg("yeuoly/google:0.0.1@4ff79ee644987e5b744d9c5b7a735d459fe66f26b28724326a7834d7e459e708")
+    assert pkg is not None
+    assert len(pkg) > 0