plugin_daemon.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from datetime import datetime
  2. from enum import Enum
  3. from typing import Generic, Optional, TypeVar
  4. from pydantic import BaseModel, ConfigDict, Field
  5. from core.model_runtime.entities.model_entities import AIModelEntity
  6. from core.model_runtime.entities.provider_entities import ProviderEntity
  7. from core.plugin.entities.base import BasePluginEntity
  8. from core.plugin.entities.plugin import PluginDeclaration
  9. from core.tools.entities.common_entities import I18nObject
  10. from core.tools.entities.tool_entities import ToolProviderEntityWithPlugin
  11. T = TypeVar("T", bound=(BaseModel | dict | list | bool | str))
  12. class PluginDaemonBasicResponse(BaseModel, Generic[T]):
  13. """
  14. Basic response from plugin daemon.
  15. """
  16. code: int
  17. message: str
  18. data: Optional[T]
  19. class InstallPluginMessage(BaseModel):
  20. """
  21. Message for installing a plugin.
  22. """
  23. class Event(Enum):
  24. Info = "info"
  25. Done = "done"
  26. Error = "error"
  27. event: Event
  28. data: str
  29. class PluginToolProviderEntity(BaseModel):
  30. provider: str
  31. plugin_unique_identifier: str
  32. plugin_id: str
  33. declaration: ToolProviderEntityWithPlugin
  34. class PluginBasicBooleanResponse(BaseModel):
  35. """
  36. Basic boolean response from plugin daemon.
  37. """
  38. result: bool
  39. class PluginModelSchemaEntity(BaseModel):
  40. model_schema: AIModelEntity = Field(description="The model schema.")
  41. # pydantic configs
  42. model_config = ConfigDict(protected_namespaces=())
  43. class PluginModelProviderEntity(BaseModel):
  44. id: str = Field(description="ID")
  45. created_at: datetime = Field(description="The created at time of the model provider.")
  46. updated_at: datetime = Field(description="The updated at time of the model provider.")
  47. provider: str = Field(description="The provider of the model.")
  48. tenant_id: str = Field(description="The tenant ID.")
  49. plugin_unique_identifier: str = Field(description="The plugin unique identifier.")
  50. plugin_id: str = Field(description="The plugin ID.")
  51. declaration: ProviderEntity = Field(description="The declaration of the model provider.")
  52. class PluginTextEmbeddingNumTokensResponse(BaseModel):
  53. """
  54. Response for number of tokens.
  55. """
  56. num_tokens: list[int] = Field(description="The number of tokens.")
  57. class PluginLLMNumTokensResponse(BaseModel):
  58. """
  59. Response for number of tokens.
  60. """
  61. num_tokens: int = Field(description="The number of tokens.")
  62. class PluginStringResultResponse(BaseModel):
  63. result: str = Field(description="The result of the string.")
  64. class PluginVoiceEntity(BaseModel):
  65. name: str = Field(description="The name of the voice.")
  66. value: str = Field(description="The value of the voice.")
  67. class PluginVoicesResponse(BaseModel):
  68. voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
  69. class PluginDaemonError(BaseModel):
  70. """
  71. Error from plugin daemon.
  72. """
  73. error_type: str
  74. message: str
  75. args: Optional[dict] = None
  76. class PluginDaemonInnerError(Exception):
  77. code: int
  78. message: str
  79. def __init__(self, code: int, message: str):
  80. self.code = code
  81. self.message = message
  82. class PluginInstallTaskStatus(str, Enum):
  83. Pending = "pending"
  84. Running = "running"
  85. Success = "success"
  86. Failed = "failed"
  87. class PluginInstallTaskPluginStatus(BaseModel):
  88. plugin_unique_identifier: str = Field(description="The plugin unique identifier of the install task.")
  89. plugin_id: str = Field(description="The plugin ID of the install task.")
  90. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  91. message: str = Field(description="The message of the install task.")
  92. icon: str = Field(description="The icon of the plugin.")
  93. labels: I18nObject = Field(description="The labels of the plugin.")
  94. class PluginInstallTask(BasePluginEntity):
  95. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  96. total_plugins: int = Field(description="The total number of plugins to be installed.")
  97. completed_plugins: int = Field(description="The number of plugins that have been installed.")
  98. plugins: list[PluginInstallTaskPluginStatus] = Field(description="The status of the plugins.")
  99. class PluginInstallTaskStartResponse(BaseModel):
  100. all_installed: bool = Field(description="Whether all plugins are installed.")
  101. task_id: str = Field(description="The ID of the install task.")
  102. class PluginUploadResponse(BaseModel):
  103. unique_identifier: str = Field(description="The unique identifier of the plugin.")
  104. manifest: PluginDeclaration