plugin_daemon.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from datetime import datetime
  2. from enum import StrEnum
  3. from typing import Generic, Optional, TypeVar
  4. from pydantic import BaseModel, ConfigDict, Field
  5. from core.agent.plugin_entities import AgentProviderEntityWithPlugin
  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(StrEnum):
  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 PluginAgentProviderEntity(BaseModel):
  36. provider: str
  37. plugin_unique_identifier: str
  38. plugin_id: str
  39. declaration: AgentProviderEntityWithPlugin
  40. class PluginBasicBooleanResponse(BaseModel):
  41. """
  42. Basic boolean response from plugin daemon.
  43. """
  44. result: bool
  45. credentials: dict | None = None
  46. class PluginModelSchemaEntity(BaseModel):
  47. model_schema: AIModelEntity = Field(description="The model schema.")
  48. # pydantic configs
  49. model_config = ConfigDict(protected_namespaces=())
  50. class PluginModelProviderEntity(BaseModel):
  51. id: str = Field(description="ID")
  52. created_at: datetime = Field(description="The created at time of the model provider.")
  53. updated_at: datetime = Field(description="The updated at time of the model provider.")
  54. provider: str = Field(description="The provider of the model.")
  55. tenant_id: str = Field(description="The tenant ID.")
  56. plugin_unique_identifier: str = Field(description="The plugin unique identifier.")
  57. plugin_id: str = Field(description="The plugin ID.")
  58. declaration: ProviderEntity = Field(description="The declaration of the model provider.")
  59. class PluginTextEmbeddingNumTokensResponse(BaseModel):
  60. """
  61. Response for number of tokens.
  62. """
  63. num_tokens: list[int] = Field(description="The number of tokens.")
  64. class PluginLLMNumTokensResponse(BaseModel):
  65. """
  66. Response for number of tokens.
  67. """
  68. num_tokens: int = Field(description="The number of tokens.")
  69. class PluginStringResultResponse(BaseModel):
  70. result: str = Field(description="The result of the string.")
  71. class PluginVoiceEntity(BaseModel):
  72. name: str = Field(description="The name of the voice.")
  73. value: str = Field(description="The value of the voice.")
  74. class PluginVoicesResponse(BaseModel):
  75. voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
  76. class PluginDaemonError(BaseModel):
  77. """
  78. Error from plugin daemon.
  79. """
  80. error_type: str
  81. message: str
  82. class PluginDaemonInnerError(Exception):
  83. code: int
  84. message: str
  85. def __init__(self, code: int, message: str):
  86. self.code = code
  87. self.message = message
  88. class PluginInstallTaskStatus(StrEnum):
  89. Pending = "pending"
  90. Running = "running"
  91. Success = "success"
  92. Failed = "failed"
  93. class PluginInstallTaskPluginStatus(BaseModel):
  94. plugin_unique_identifier: str = Field(description="The plugin unique identifier of the install task.")
  95. plugin_id: str = Field(description="The plugin ID of the install task.")
  96. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  97. message: str = Field(description="The message of the install task.")
  98. icon: str = Field(description="The icon of the plugin.")
  99. labels: I18nObject = Field(description="The labels of the plugin.")
  100. class PluginInstallTask(BasePluginEntity):
  101. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  102. total_plugins: int = Field(description="The total number of plugins to be installed.")
  103. completed_plugins: int = Field(description="The number of plugins that have been installed.")
  104. plugins: list[PluginInstallTaskPluginStatus] = Field(description="The status of the plugins.")
  105. class PluginInstallTaskStartResponse(BaseModel):
  106. all_installed: bool = Field(description="Whether all plugins are installed.")
  107. task_id: str = Field(description="The ID of the install task.")
  108. class PluginUploadResponse(BaseModel):
  109. unique_identifier: str = Field(description="The unique identifier of the plugin.")
  110. manifest: PluginDeclaration