queue_entities.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. from datetime import datetime
  2. from enum import Enum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, field_validator
  5. from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk
  6. from core.workflow.entities.node_entities import NodeRunMetadataKey
  7. from core.workflow.graph_engine.entities.graph_runtime_state import GraphRuntimeState
  8. from core.workflow.nodes import NodeType
  9. from core.workflow.nodes.base import BaseNodeData
  10. class QueueEvent(str, Enum):
  11. """
  12. QueueEvent enum
  13. """
  14. LLM_CHUNK = "llm_chunk"
  15. TEXT_CHUNK = "text_chunk"
  16. AGENT_MESSAGE = "agent_message"
  17. MESSAGE_REPLACE = "message_replace"
  18. MESSAGE_END = "message_end"
  19. ADVANCED_CHAT_MESSAGE_END = "advanced_chat_message_end"
  20. WORKFLOW_STARTED = "workflow_started"
  21. WORKFLOW_SUCCEEDED = "workflow_succeeded"
  22. WORKFLOW_FAILED = "workflow_failed"
  23. ITERATION_START = "iteration_start"
  24. ITERATION_NEXT = "iteration_next"
  25. ITERATION_COMPLETED = "iteration_completed"
  26. NODE_STARTED = "node_started"
  27. NODE_SUCCEEDED = "node_succeeded"
  28. NODE_FAILED = "node_failed"
  29. RETRIEVER_RESOURCES = "retriever_resources"
  30. ANNOTATION_REPLY = "annotation_reply"
  31. AGENT_THOUGHT = "agent_thought"
  32. MESSAGE_FILE = "message_file"
  33. PARALLEL_BRANCH_RUN_STARTED = "parallel_branch_run_started"
  34. PARALLEL_BRANCH_RUN_SUCCEEDED = "parallel_branch_run_succeeded"
  35. PARALLEL_BRANCH_RUN_FAILED = "parallel_branch_run_failed"
  36. ERROR = "error"
  37. PING = "ping"
  38. STOP = "stop"
  39. class AppQueueEvent(BaseModel):
  40. """
  41. QueueEvent abstract entity
  42. """
  43. event: QueueEvent
  44. class QueueLLMChunkEvent(AppQueueEvent):
  45. """
  46. QueueLLMChunkEvent entity
  47. Only for basic mode apps
  48. """
  49. event: QueueEvent = QueueEvent.LLM_CHUNK
  50. chunk: LLMResultChunk
  51. class QueueIterationStartEvent(AppQueueEvent):
  52. """
  53. QueueIterationStartEvent entity
  54. """
  55. event: QueueEvent = QueueEvent.ITERATION_START
  56. node_execution_id: str
  57. node_id: str
  58. node_type: NodeType
  59. node_data: BaseNodeData
  60. parallel_id: Optional[str] = None
  61. """parallel id if node is in parallel"""
  62. parallel_start_node_id: Optional[str] = None
  63. """parallel start node id if node is in parallel"""
  64. parent_parallel_id: Optional[str] = None
  65. """parent parallel id if node is in parallel"""
  66. parent_parallel_start_node_id: Optional[str] = None
  67. """parent parallel start node id if node is in parallel"""
  68. start_at: datetime
  69. node_run_index: int
  70. inputs: Optional[dict[str, Any]] = None
  71. predecessor_node_id: Optional[str] = None
  72. metadata: Optional[dict[str, Any]] = None
  73. class QueueIterationNextEvent(AppQueueEvent):
  74. """
  75. QueueIterationNextEvent entity
  76. """
  77. event: QueueEvent = QueueEvent.ITERATION_NEXT
  78. index: int
  79. node_execution_id: str
  80. node_id: str
  81. node_type: NodeType
  82. node_data: BaseNodeData
  83. parallel_id: Optional[str] = None
  84. """parallel id if node is in parallel"""
  85. parallel_start_node_id: Optional[str] = None
  86. """parallel start node id if node is in parallel"""
  87. parent_parallel_id: Optional[str] = None
  88. """parent parallel id if node is in parallel"""
  89. parent_parallel_start_node_id: Optional[str] = None
  90. """parent parallel start node id if node is in parallel"""
  91. node_run_index: int
  92. output: Optional[Any] = None # output for the current iteration
  93. @field_validator("output", mode="before")
  94. @classmethod
  95. def set_output(cls, v):
  96. """
  97. Set output
  98. """
  99. if v is None:
  100. return None
  101. if isinstance(v, int | float | str | bool | dict | list):
  102. return v
  103. raise ValueError("output must be a valid type")
  104. class QueueIterationCompletedEvent(AppQueueEvent):
  105. """
  106. QueueIterationCompletedEvent entity
  107. """
  108. event: QueueEvent = QueueEvent.ITERATION_COMPLETED
  109. node_execution_id: str
  110. node_id: str
  111. node_type: NodeType
  112. node_data: BaseNodeData
  113. parallel_id: Optional[str] = None
  114. """parallel id if node is in parallel"""
  115. parallel_start_node_id: Optional[str] = None
  116. """parallel start node id if node is in parallel"""
  117. parent_parallel_id: Optional[str] = None
  118. """parent parallel id if node is in parallel"""
  119. parent_parallel_start_node_id: Optional[str] = None
  120. """parent parallel start node id if node is in parallel"""
  121. start_at: datetime
  122. node_run_index: int
  123. inputs: Optional[dict[str, Any]] = None
  124. outputs: Optional[dict[str, Any]] = None
  125. metadata: Optional[dict[str, Any]] = None
  126. steps: int = 0
  127. error: Optional[str] = None
  128. class QueueTextChunkEvent(AppQueueEvent):
  129. """
  130. QueueTextChunkEvent entity
  131. """
  132. event: QueueEvent = QueueEvent.TEXT_CHUNK
  133. text: str
  134. from_variable_selector: Optional[list[str]] = None
  135. """from variable selector"""
  136. in_iteration_id: Optional[str] = None
  137. """iteration id if node is in iteration"""
  138. class QueueAgentMessageEvent(AppQueueEvent):
  139. """
  140. QueueMessageEvent entity
  141. """
  142. event: QueueEvent = QueueEvent.AGENT_MESSAGE
  143. chunk: LLMResultChunk
  144. class QueueMessageReplaceEvent(AppQueueEvent):
  145. """
  146. QueueMessageReplaceEvent entity
  147. """
  148. event: QueueEvent = QueueEvent.MESSAGE_REPLACE
  149. text: str
  150. class QueueRetrieverResourcesEvent(AppQueueEvent):
  151. """
  152. QueueRetrieverResourcesEvent entity
  153. """
  154. event: QueueEvent = QueueEvent.RETRIEVER_RESOURCES
  155. retriever_resources: list[dict]
  156. in_iteration_id: Optional[str] = None
  157. """iteration id if node is in iteration"""
  158. class QueueAnnotationReplyEvent(AppQueueEvent):
  159. """
  160. QueueAnnotationReplyEvent entity
  161. """
  162. event: QueueEvent = QueueEvent.ANNOTATION_REPLY
  163. message_annotation_id: str
  164. class QueueMessageEndEvent(AppQueueEvent):
  165. """
  166. QueueMessageEndEvent entity
  167. """
  168. event: QueueEvent = QueueEvent.MESSAGE_END
  169. llm_result: Optional[LLMResult] = None
  170. class QueueAdvancedChatMessageEndEvent(AppQueueEvent):
  171. """
  172. QueueAdvancedChatMessageEndEvent entity
  173. """
  174. event: QueueEvent = QueueEvent.ADVANCED_CHAT_MESSAGE_END
  175. class QueueWorkflowStartedEvent(AppQueueEvent):
  176. """
  177. QueueWorkflowStartedEvent entity
  178. """
  179. event: QueueEvent = QueueEvent.WORKFLOW_STARTED
  180. graph_runtime_state: GraphRuntimeState
  181. class QueueWorkflowSucceededEvent(AppQueueEvent):
  182. """
  183. QueueWorkflowSucceededEvent entity
  184. """
  185. event: QueueEvent = QueueEvent.WORKFLOW_SUCCEEDED
  186. outputs: Optional[dict[str, Any]] = None
  187. class QueueWorkflowFailedEvent(AppQueueEvent):
  188. """
  189. QueueWorkflowFailedEvent entity
  190. """
  191. event: QueueEvent = QueueEvent.WORKFLOW_FAILED
  192. error: str
  193. class QueueNodeStartedEvent(AppQueueEvent):
  194. """
  195. QueueNodeStartedEvent entity
  196. """
  197. event: QueueEvent = QueueEvent.NODE_STARTED
  198. node_execution_id: str
  199. node_id: str
  200. node_type: NodeType
  201. node_data: BaseNodeData
  202. node_run_index: int = 1
  203. predecessor_node_id: Optional[str] = None
  204. parallel_id: Optional[str] = None
  205. """parallel id if node is in parallel"""
  206. parallel_start_node_id: Optional[str] = None
  207. """parallel start node id if node is in parallel"""
  208. parent_parallel_id: Optional[str] = None
  209. """parent parallel id if node is in parallel"""
  210. parent_parallel_start_node_id: Optional[str] = None
  211. """parent parallel start node id if node is in parallel"""
  212. in_iteration_id: Optional[str] = None
  213. """iteration id if node is in iteration"""
  214. start_at: datetime
  215. class QueueNodeSucceededEvent(AppQueueEvent):
  216. """
  217. QueueNodeSucceededEvent entity
  218. """
  219. event: QueueEvent = QueueEvent.NODE_SUCCEEDED
  220. node_execution_id: str
  221. node_id: str
  222. node_type: NodeType
  223. node_data: BaseNodeData
  224. parallel_id: Optional[str] = None
  225. """parallel id if node is in parallel"""
  226. parallel_start_node_id: Optional[str] = None
  227. """parallel start node id if node is in parallel"""
  228. parent_parallel_id: Optional[str] = None
  229. """parent parallel id if node is in parallel"""
  230. parent_parallel_start_node_id: Optional[str] = None
  231. """parent parallel start node id if node is in parallel"""
  232. in_iteration_id: Optional[str] = None
  233. """iteration id if node is in iteration"""
  234. start_at: datetime
  235. inputs: Optional[dict[str, Any]] = None
  236. process_data: Optional[dict[str, Any]] = None
  237. outputs: Optional[dict[str, Any]] = None
  238. execution_metadata: Optional[dict[NodeRunMetadataKey, Any]] = None
  239. error: Optional[str] = None
  240. class QueueNodeFailedEvent(AppQueueEvent):
  241. """
  242. QueueNodeFailedEvent entity
  243. """
  244. event: QueueEvent = QueueEvent.NODE_FAILED
  245. node_execution_id: str
  246. node_id: str
  247. node_type: NodeType
  248. node_data: BaseNodeData
  249. parallel_id: Optional[str] = None
  250. """parallel id if node is in parallel"""
  251. parallel_start_node_id: Optional[str] = None
  252. """parallel start node id if node is in parallel"""
  253. parent_parallel_id: Optional[str] = None
  254. """parent parallel id if node is in parallel"""
  255. parent_parallel_start_node_id: Optional[str] = None
  256. """parent parallel start node id if node is in parallel"""
  257. in_iteration_id: Optional[str] = None
  258. """iteration id if node is in iteration"""
  259. start_at: datetime
  260. inputs: Optional[dict[str, Any]] = None
  261. process_data: Optional[dict[str, Any]] = None
  262. outputs: Optional[dict[str, Any]] = None
  263. error: str
  264. class QueueAgentThoughtEvent(AppQueueEvent):
  265. """
  266. QueueAgentThoughtEvent entity
  267. """
  268. event: QueueEvent = QueueEvent.AGENT_THOUGHT
  269. agent_thought_id: str
  270. class QueueMessageFileEvent(AppQueueEvent):
  271. """
  272. QueueAgentThoughtEvent entity
  273. """
  274. event: QueueEvent = QueueEvent.MESSAGE_FILE
  275. message_file_id: str
  276. class QueueErrorEvent(AppQueueEvent):
  277. """
  278. QueueErrorEvent entity
  279. """
  280. event: QueueEvent = QueueEvent.ERROR
  281. error: Any = None
  282. class QueuePingEvent(AppQueueEvent):
  283. """
  284. QueuePingEvent entity
  285. """
  286. event: QueueEvent = QueueEvent.PING
  287. class QueueStopEvent(AppQueueEvent):
  288. """
  289. QueueStopEvent entity
  290. """
  291. class StopBy(Enum):
  292. """
  293. Stop by enum
  294. """
  295. USER_MANUAL = "user-manual"
  296. ANNOTATION_REPLY = "annotation-reply"
  297. OUTPUT_MODERATION = "output-moderation"
  298. INPUT_MODERATION = "input-moderation"
  299. event: QueueEvent = QueueEvent.STOP
  300. stopped_by: StopBy
  301. def get_stop_reason(self) -> str:
  302. """
  303. To stop reason
  304. """
  305. reason_mapping = {
  306. QueueStopEvent.StopBy.USER_MANUAL: "Stopped by user.",
  307. QueueStopEvent.StopBy.ANNOTATION_REPLY: "Stopped by annotation reply.",
  308. QueueStopEvent.StopBy.OUTPUT_MODERATION: "Stopped by output moderation.",
  309. QueueStopEvent.StopBy.INPUT_MODERATION: "Stopped by input moderation.",
  310. }
  311. return reason_mapping.get(self.stopped_by, "Stopped by unknown reason.")
  312. class QueueMessage(BaseModel):
  313. """
  314. QueueMessage abstract entity
  315. """
  316. task_id: str
  317. app_mode: str
  318. event: AppQueueEvent
  319. class MessageQueueMessage(QueueMessage):
  320. """
  321. MessageQueueMessage entity
  322. """
  323. message_id: str
  324. conversation_id: str
  325. class WorkflowQueueMessage(QueueMessage):
  326. """
  327. WorkflowQueueMessage entity
  328. """
  329. pass
  330. class QueueParallelBranchRunStartedEvent(AppQueueEvent):
  331. """
  332. QueueParallelBranchRunStartedEvent entity
  333. """
  334. event: QueueEvent = QueueEvent.PARALLEL_BRANCH_RUN_STARTED
  335. parallel_id: str
  336. parallel_start_node_id: str
  337. parent_parallel_id: Optional[str] = None
  338. """parent parallel id if node is in parallel"""
  339. parent_parallel_start_node_id: Optional[str] = None
  340. """parent parallel start node id if node is in parallel"""
  341. in_iteration_id: Optional[str] = None
  342. """iteration id if node is in iteration"""
  343. class QueueParallelBranchRunSucceededEvent(AppQueueEvent):
  344. """
  345. QueueParallelBranchRunSucceededEvent entity
  346. """
  347. event: QueueEvent = QueueEvent.PARALLEL_BRANCH_RUN_SUCCEEDED
  348. parallel_id: str
  349. parallel_start_node_id: str
  350. parent_parallel_id: Optional[str] = None
  351. """parent parallel id if node is in parallel"""
  352. parent_parallel_start_node_id: Optional[str] = None
  353. """parent parallel start node id if node is in parallel"""
  354. in_iteration_id: Optional[str] = None
  355. """iteration id if node is in iteration"""
  356. class QueueParallelBranchRunFailedEvent(AppQueueEvent):
  357. """
  358. QueueParallelBranchRunFailedEvent entity
  359. """
  360. event: QueueEvent = QueueEvent.PARALLEL_BRANCH_RUN_FAILED
  361. parallel_id: str
  362. parallel_start_node_id: str
  363. parent_parallel_id: Optional[str] = None
  364. """parent parallel id if node is in parallel"""
  365. parent_parallel_start_node_id: Optional[str] = None
  366. """parent parallel start node id if node is in parallel"""
  367. in_iteration_id: Optional[str] = None
  368. """iteration id if node is in iteration"""
  369. error: str