plugin_daemon.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 PluginNumTokensResponse(BaseModel):
  53. """
  54. Response for number of tokens.
  55. """
  56. num_tokens: int = Field(description="The number of tokens.")
  57. class PluginStringResultResponse(BaseModel):
  58. result: str = Field(description="The result of the string.")
  59. class PluginVoiceEntity(BaseModel):
  60. name: str = Field(description="The name of the voice.")
  61. value: str = Field(description="The value of the voice.")
  62. class PluginVoicesResponse(BaseModel):
  63. voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
  64. class PluginDaemonError(BaseModel):
  65. """
  66. Error from plugin daemon.
  67. """
  68. error_type: str
  69. message: str
  70. args: Optional[dict] = None
  71. class PluginDaemonInnerError(Exception):
  72. code: int
  73. message: str
  74. def __init__(self, code: int, message: str):
  75. self.code = code
  76. self.message = message
  77. class PluginInstallTaskStatus(str, Enum):
  78. Pending = "pending"
  79. Running = "running"
  80. Success = "success"
  81. Failed = "failed"
  82. class PluginInstallTaskPluginStatus(BaseModel):
  83. plugin_unique_identifier: str = Field(description="The plugin unique identifier of the install task.")
  84. plugin_id: str = Field(description="The plugin ID of the install task.")
  85. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  86. message: str = Field(description="The message of the install task.")
  87. icon: str = Field(description="The icon of the plugin.")
  88. labels: I18nObject = Field(description="The labels of the plugin.")
  89. class PluginInstallTask(BasePluginEntity):
  90. status: PluginInstallTaskStatus = Field(description="The status of the install task.")
  91. total_plugins: int = Field(description="The total number of plugins to be installed.")
  92. completed_plugins: int = Field(description="The number of plugins that have been installed.")
  93. plugins: list[PluginInstallTaskPluginStatus] = Field(description="The status of the plugins.")
  94. class PluginInstallTaskStartResponse(BaseModel):
  95. all_installed: bool = Field(description="Whether all plugins are installed.")
  96. task_id: str = Field(description="The ID of the install task.")
  97. class PluginUploadResponse(BaseModel):
  98. unique_identifier: str = Field(description="The unique identifier of the plugin.")
  99. manifest: PluginDeclaration