queue_entities.py 13 KB

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