plugin_daemon.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import enum
  2. from datetime import datetime
  3. from enum import Enum
  4. from typing import Generic, Optional, TypeVar
  5. from pydantic import BaseModel, ConfigDict, Field
  6. from core.model_runtime.entities.model_entities import AIModelEntity
  7. from core.model_runtime.entities.provider_entities import ProviderEntity
  8. from core.plugin.entities.base import BasePluginEntity
  9. from core.plugin.entities.plugin import PluginDeclaration
  10. from core.tools.entities.common_entities import I18nObject
  11. from core.tools.entities.tool_entities import ToolProviderEntityWithPlugin
  12. T = TypeVar("T", bound=(BaseModel | dict | list | bool | str))
  13. class PluginDaemonBasicResponse(BaseModel, Generic[T]):
  14. """
  15. Basic response from plugin daemon.
  16. """
  17. code: int
  18. message: str
  19. data: Optional[T]
  20. class InstallPluginMessage(BaseModel):
  21. """
  22. Message for installing a plugin.
  23. """
  24. class Event(Enum):
  25. Info = "info"
  26. Done = "done"
  27. Error = "error"
  28. event: Event
  29. data: str
  30. class PluginToolProviderEntity(BaseModel):
  31. provider: str
  32. plugin_unique_identifier: str
  33. plugin_id: str
  34. declaration: ToolProviderEntityWithPlugin
  35. class PluginBasicBooleanResponse(BaseModel):
  36. """
  37. Basic boolean response from plugin daemon.
  38. """
  39. result: bool
  40. class PluginModelSchemaEntity(BaseModel):
  41. model_schema: AIModelEntity = Field(description="The model schema.")
  42. # pydantic configs
  43. model_config = ConfigDict(protected_namespaces=())
  44. class PluginModelProviderEntity(BaseModel):
  45. id: str = Field(description="ID")
  46. created_at: datetime = Field(description="The created at time of the model provider.")
  47. updated_at: datetime = Field(description="The updated at time of the model provider.")
  48. provider: str = Field(description="The provider of the model.")
  49. tenant_id: str = Field(description="The tenant ID.")
  50. plugin_unique_identifier: str = Field(description="The plugin unique identifier.")
  51. plugin_id: str = Field(description="The plugin ID.")
  52. declaration: ProviderEntity = Field(description="The declaration of the model provider.")
  53. class PluginTextEmbeddingNumTokensResponse(BaseModel):
  54. """
  55. Response for number of tokens.
  56. """
  57. num_tokens: list[int] = Field(description="The number of tokens.")
  58. class PluginLLMNumTokensResponse(BaseModel):
  59. """
  60. Response for number of tokens.
  61. """
  62. num_tokens: int = Field(description="The number of tokens.")
  63. class PluginStringResultResponse(BaseModel):
  64. result: str = Field(description="The result of the string.")
  65. class PluginVoiceEntity(BaseModel):
  66. name: str = Field(description="The name of the voice.")
  67. value: str = Field(description="The value of the voice.")
  68. class PluginVoicesResponse(BaseModel):
  69. voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
  70. class PluginDaemonError(BaseModel):
  71. """
  72. Error from plugin daemon.
  73. """
  74. error_type: str
  75. message: str
  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(enum.StrEnum):
  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