feat: add category for plugins

pull/12372/head
Yeuoly 2 years ago
parent 276701e1b7
commit a81293cf5a
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61

@ -133,9 +133,7 @@ class PluginInstallFromPkgApi(Resource):
response = PluginService.install_from_local_pkg(tenant_id, args["plugin_unique_identifier"]) response = PluginService.install_from_local_pkg(tenant_id, args["plugin_unique_identifier"])
return { return response.model_dump()
"task_id": response,
}
class PluginInstallFromGithubApi(Resource): class PluginInstallFromGithubApi(Resource):
@ -160,9 +158,7 @@ class PluginInstallFromGithubApi(Resource):
tenant_id, args["repo"], args["version"], args["package"], args["plugin_unique_identifier"] tenant_id, args["repo"], args["version"], args["package"], args["plugin_unique_identifier"]
) )
return { return response.model_dump()
"task_id": response,
}
class PluginInstallFromMarketplaceApi(Resource): class PluginInstallFromMarketplaceApi(Resource):
@ -182,9 +178,7 @@ class PluginInstallFromMarketplaceApi(Resource):
response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifier"]) response = PluginService.install_from_marketplace_pkg(tenant_id, args["plugin_unique_identifier"])
return { return response.model_dump()
"task_id": response,
}
class PluginFetchManifestApi(Resource): class PluginFetchManifestApi(Resource):
@ -200,7 +194,9 @@ class PluginFetchManifestApi(Resource):
tenant_id = user.current_tenant_id tenant_id = user.current_tenant_id
return {"manifest": PluginService.fetch_plugin_manifest(tenant_id, args["plugin_unique_identifier"])} return jsonable_encoder(
{"manifest": PluginService.fetch_plugin_manifest(tenant_id, args["plugin_unique_identifier"]).model_dump()}
)
class PluginFetchInstallTasksApi(Resource): class PluginFetchInstallTasksApi(Resource):

@ -3,7 +3,7 @@ from collections.abc import Mapping
from enum import Enum from enum import Enum
from typing import Any, Optional from typing import Any, Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field, model_validator
from core.model_runtime.entities.provider_entities import ProviderEntity from core.model_runtime.entities.provider_entities import ProviderEntity
from core.plugin.entities.base import BasePluginEntity from core.plugin.entities.base import BasePluginEntity
@ -54,6 +54,12 @@ class PluginResourceRequirements(BaseModel):
permission: Optional[Permission] permission: Optional[Permission]
class PluginCategory(str, Enum):
Tool = "tool"
Model = "model"
Extension = "extension"
class PluginDeclaration(BaseModel): class PluginDeclaration(BaseModel):
class Plugins(BaseModel): class Plugins(BaseModel):
tools: Optional[list[str]] = Field(default_factory=list) tools: Optional[list[str]] = Field(default_factory=list)
@ -65,6 +71,7 @@ class PluginDeclaration(BaseModel):
name: str = Field(..., pattern=r"^[a-z0-9_-]{1,128}$") name: str = Field(..., pattern=r"^[a-z0-9_-]{1,128}$")
icon: str icon: str
label: I18nObject label: I18nObject
category: PluginCategory
created_at: datetime.datetime created_at: datetime.datetime
resource: PluginResourceRequirements resource: PluginResourceRequirements
plugins: Plugins plugins: Plugins
@ -72,6 +79,18 @@ class PluginDeclaration(BaseModel):
model: Optional[ProviderEntity] = None model: Optional[ProviderEntity] = None
endpoint: Optional[EndpointProviderDeclaration] = None endpoint: Optional[EndpointProviderDeclaration] = None
@model_validator(mode="before")
@classmethod
def validate_category(cls, values: dict) -> dict:
# auto detect category
if values.get("tool"):
values["category"] = PluginCategory.Tool
elif values.get("model"):
values["category"] = PluginCategory.Model
else:
values["category"] = PluginCategory.Extension
return values
class PluginEntity(BasePluginEntity): class PluginEntity(BasePluginEntity):
name: str name: str

@ -128,3 +128,8 @@ class PluginInstallTask(BasePluginEntity):
total_plugins: int = Field(description="The total number of plugins to be installed.") total_plugins: int = Field(description="The total number of plugins to be installed.")
completed_plugins: int = Field(description="The number of plugins that have been installed.") completed_plugins: int = Field(description="The number of plugins that have been installed.")
plugins: list[PluginInstallTaskPluginStatus] = Field(description="The status of the plugins.") plugins: list[PluginInstallTaskPluginStatus] = Field(description="The status of the plugins.")
class PluginInstallTaskStartResponse(BasePluginEntity):
all_installed: bool = Field(description="Whether all plugins are installed.")
task_id: str = Field(description="The ID of the install task.")

@ -1,7 +1,9 @@
from collections.abc import Sequence from collections.abc import Sequence
from pydantic import BaseModel
from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource from core.plugin.entities.plugin import PluginDeclaration, PluginEntity, PluginInstallationSource
from core.plugin.entities.plugin_daemon import PluginInstallTask from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginInstallTaskStartResponse
from core.plugin.manager.base import BasePluginManager from core.plugin.manager.base import BasePluginManager
@ -53,15 +55,15 @@ class PluginInstallationManager(BasePluginManager):
def install_from_identifiers( def install_from_identifiers(
self, tenant_id: str, identifiers: Sequence[str], source: PluginInstallationSource, meta: dict self, tenant_id: str, identifiers: Sequence[str], source: PluginInstallationSource, meta: dict
) -> str: ) -> PluginInstallTaskStartResponse:
""" """
Install a plugin from an identifier. Install a plugin from an identifier.
""" """
# exception will be raised if the request failed # exception will be raised if the request failed
return self._request_with_plugin_daemon_response( return self._request_with_plugin_daemon_response(
"POST", "POST",
f"plugin/{tenant_id}/management/install/identifier", f"plugin/{tenant_id}/management/install/identifiers",
str, PluginInstallTaskStartResponse,
data={ data={
"plugin_unique_identifiers": identifiers, "plugin_unique_identifiers": identifiers,
"source": source, "source": source,
@ -94,11 +96,16 @@ class PluginInstallationManager(BasePluginManager):
""" """
Fetch a plugin manifest. Fetch a plugin manifest.
""" """
class PluginDeclarationResponse(BaseModel):
declaration: PluginDeclaration
return self._request_with_plugin_daemon_response( return self._request_with_plugin_daemon_response(
"GET", "GET",
f"plugin/{tenant_id}/management/fetch/identifier", f"plugin/{tenant_id}/management/fetch/manifest",
PluginDeclaration, PluginDeclarationResponse,
) params={"plugin_unique_identifier": plugin_unique_identifier},
).declaration
def uninstall(self, tenant_id: str, plugin_installation_id: str) -> bool: def uninstall(self, tenant_id: str, plugin_installation_id: str) -> bool:
""" """

@ -87,7 +87,7 @@ class PluginService:
) )
@staticmethod @staticmethod
def install_from_local_pkg(tenant_id: str, plugin_unique_identifier: str) -> str: def install_from_local_pkg(tenant_id: str, plugin_unique_identifier: str):
manager = PluginInstallationManager() manager = PluginInstallationManager()
return manager.install_from_identifiers( return manager.install_from_identifiers(
tenant_id, tenant_id,
@ -97,9 +97,7 @@ class PluginService:
) )
@staticmethod @staticmethod
def install_from_github( def install_from_github(tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str):
tenant_id: str, plugin_unique_identifier: str, repo: str, version: str, package: str
) -> str:
""" """
Install plugin from github release package files, Install plugin from github release package files,
returns plugin_unique_identifier returns plugin_unique_identifier
@ -117,7 +115,7 @@ class PluginService:
) )
@staticmethod @staticmethod
def install_from_marketplace_pkg(tenant_id: str, plugin_unique_identifier: str) -> str: def install_from_marketplace_pkg(tenant_id: str, plugin_unique_identifier: str):
""" """
Install plugin from marketplace package files, Install plugin from marketplace package files,
returns installation task id returns installation task id

Loading…
Cancel
Save