task_entities.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. from enum import Enum
  2. from typing import Optional
  3. from pydantic import BaseModel
  4. from core.model_runtime.entities.llm_entities import LLMResult, LLMUsage
  5. from core.model_runtime.utils.encoders import jsonable_encoder
  6. from core.workflow.entities.node_entities import NodeType
  7. from core.workflow.nodes.answer.entities import GenerateRouteChunk
  8. class WorkflowStreamGenerateNodes(BaseModel):
  9. """
  10. WorkflowStreamGenerateNodes entity
  11. """
  12. end_node_id: str
  13. stream_node_ids: list[str]
  14. class ChatflowStreamGenerateRoute(BaseModel):
  15. """
  16. ChatflowStreamGenerateRoute entity
  17. """
  18. answer_node_id: str
  19. generate_route: list[GenerateRouteChunk]
  20. current_route_position: int = 0
  21. class NodeExecutionInfo(BaseModel):
  22. """
  23. NodeExecutionInfo entity
  24. """
  25. workflow_node_execution_id: str
  26. node_type: NodeType
  27. start_at: float
  28. class TaskState(BaseModel):
  29. """
  30. TaskState entity
  31. """
  32. metadata: dict = {}
  33. class EasyUITaskState(TaskState):
  34. """
  35. EasyUITaskState entity
  36. """
  37. llm_result: LLMResult
  38. class WorkflowTaskState(TaskState):
  39. """
  40. WorkflowTaskState entity
  41. """
  42. answer: str = ""
  43. workflow_run_id: Optional[str] = None
  44. start_at: Optional[float] = None
  45. total_tokens: int = 0
  46. total_steps: int = 0
  47. ran_node_execution_infos: dict[str, NodeExecutionInfo] = {}
  48. latest_node_execution_info: Optional[NodeExecutionInfo] = None
  49. current_stream_generate_state: Optional[WorkflowStreamGenerateNodes] = None
  50. class AdvancedChatTaskState(WorkflowTaskState):
  51. """
  52. AdvancedChatTaskState entity
  53. """
  54. usage: LLMUsage
  55. current_stream_generate_state: Optional[ChatflowStreamGenerateRoute] = None
  56. class StreamEvent(Enum):
  57. """
  58. Stream event
  59. """
  60. PING = "ping"
  61. ERROR = "error"
  62. MESSAGE = "message"
  63. MESSAGE_END = "message_end"
  64. MESSAGE_FILE = "message_file"
  65. MESSAGE_REPLACE = "message_replace"
  66. AGENT_THOUGHT = "agent_thought"
  67. AGENT_MESSAGE = "agent_message"
  68. WORKFLOW_STARTED = "workflow_started"
  69. WORKFLOW_FINISHED = "workflow_finished"
  70. NODE_STARTED = "node_started"
  71. NODE_FINISHED = "node_finished"
  72. TEXT_CHUNK = "text_chunk"
  73. TEXT_REPLACE = "text_replace"
  74. class StreamResponse(BaseModel):
  75. """
  76. StreamResponse entity
  77. """
  78. event: StreamEvent
  79. task_id: str
  80. def to_dict(self) -> dict:
  81. return jsonable_encoder(self)
  82. class ErrorStreamResponse(StreamResponse):
  83. """
  84. ErrorStreamResponse entity
  85. """
  86. event: StreamEvent = StreamEvent.ERROR
  87. err: Exception
  88. class Config:
  89. arbitrary_types_allowed = True
  90. class MessageStreamResponse(StreamResponse):
  91. """
  92. MessageStreamResponse entity
  93. """
  94. event: StreamEvent = StreamEvent.MESSAGE
  95. id: str
  96. answer: str
  97. class MessageEndStreamResponse(StreamResponse):
  98. """
  99. MessageEndStreamResponse entity
  100. """
  101. event: StreamEvent = StreamEvent.MESSAGE_END
  102. id: str
  103. metadata: dict = {}
  104. class MessageFileStreamResponse(StreamResponse):
  105. """
  106. MessageFileStreamResponse entity
  107. """
  108. event: StreamEvent = StreamEvent.MESSAGE_FILE
  109. id: str
  110. type: str
  111. belongs_to: str
  112. url: str
  113. class MessageReplaceStreamResponse(StreamResponse):
  114. """
  115. MessageReplaceStreamResponse entity
  116. """
  117. event: StreamEvent = StreamEvent.MESSAGE_REPLACE
  118. answer: str
  119. class AgentThoughtStreamResponse(StreamResponse):
  120. """
  121. AgentThoughtStreamResponse entity
  122. """
  123. event: StreamEvent = StreamEvent.AGENT_THOUGHT
  124. id: str
  125. position: int
  126. thought: Optional[str] = None
  127. observation: Optional[str] = None
  128. tool: Optional[str] = None
  129. tool_labels: Optional[dict] = None
  130. tool_input: Optional[str] = None
  131. message_files: Optional[list[str]] = None
  132. class AgentMessageStreamResponse(StreamResponse):
  133. """
  134. AgentMessageStreamResponse entity
  135. """
  136. event: StreamEvent = StreamEvent.AGENT_MESSAGE
  137. id: str
  138. answer: str
  139. class WorkflowStartStreamResponse(StreamResponse):
  140. """
  141. WorkflowStartStreamResponse entity
  142. """
  143. class Data(BaseModel):
  144. """
  145. Data entity
  146. """
  147. id: str
  148. workflow_id: str
  149. sequence_number: int
  150. inputs: dict
  151. created_at: int
  152. event: StreamEvent = StreamEvent.WORKFLOW_STARTED
  153. workflow_run_id: str
  154. data: Data
  155. class WorkflowFinishStreamResponse(StreamResponse):
  156. """
  157. WorkflowFinishStreamResponse entity
  158. """
  159. class Data(BaseModel):
  160. """
  161. Data entity
  162. """
  163. id: str
  164. workflow_id: str
  165. sequence_number: int
  166. status: str
  167. outputs: Optional[dict] = None
  168. error: Optional[str] = None
  169. elapsed_time: float
  170. total_tokens: int
  171. total_steps: int
  172. created_by: Optional[dict] = None
  173. created_at: int
  174. finished_at: int
  175. files: Optional[list[dict]] = []
  176. event: StreamEvent = StreamEvent.WORKFLOW_FINISHED
  177. workflow_run_id: str
  178. data: Data
  179. class NodeStartStreamResponse(StreamResponse):
  180. """
  181. NodeStartStreamResponse entity
  182. """
  183. class Data(BaseModel):
  184. """
  185. Data entity
  186. """
  187. id: str
  188. node_id: str
  189. node_type: str
  190. title: str
  191. index: int
  192. predecessor_node_id: Optional[str] = None
  193. inputs: Optional[dict] = None
  194. created_at: int
  195. extras: dict = {}
  196. event: StreamEvent = StreamEvent.NODE_STARTED
  197. workflow_run_id: str
  198. data: Data
  199. def to_ignore_detail_dict(self):
  200. return {
  201. "event": self.event.value,
  202. "task_id": self.task_id,
  203. "workflow_run_id": self.workflow_run_id,
  204. "data": {
  205. "id": self.data.id,
  206. "node_id": self.data.node_id,
  207. "node_type": self.data.node_type,
  208. "title": self.data.title,
  209. "index": self.data.index,
  210. "predecessor_node_id": self.data.predecessor_node_id,
  211. "inputs": None,
  212. "created_at": self.data.created_at,
  213. "extras": {}
  214. }
  215. }
  216. class NodeFinishStreamResponse(StreamResponse):
  217. """
  218. NodeFinishStreamResponse entity
  219. """
  220. class Data(BaseModel):
  221. """
  222. Data entity
  223. """
  224. id: str
  225. node_id: str
  226. node_type: str
  227. title: str
  228. index: int
  229. predecessor_node_id: Optional[str] = None
  230. inputs: Optional[dict] = None
  231. process_data: Optional[dict] = None
  232. outputs: Optional[dict] = None
  233. status: str
  234. error: Optional[str] = None
  235. elapsed_time: float
  236. execution_metadata: Optional[dict] = None
  237. created_at: int
  238. finished_at: int
  239. files: Optional[list[dict]] = []
  240. event: StreamEvent = StreamEvent.NODE_FINISHED
  241. workflow_run_id: str
  242. data: Data
  243. def to_ignore_detail_dict(self):
  244. return {
  245. "event": self.event.value,
  246. "task_id": self.task_id,
  247. "workflow_run_id": self.workflow_run_id,
  248. "data": {
  249. "id": self.data.id,
  250. "node_id": self.data.node_id,
  251. "node_type": self.data.node_type,
  252. "title": self.data.title,
  253. "index": self.data.index,
  254. "predecessor_node_id": self.data.predecessor_node_id,
  255. "inputs": None,
  256. "process_data": None,
  257. "outputs": None,
  258. "status": self.data.status,
  259. "error": None,
  260. "elapsed_time": self.data.elapsed_time,
  261. "execution_metadata": None,
  262. "created_at": self.data.created_at,
  263. "finished_at": self.data.finished_at,
  264. "files": []
  265. }
  266. }
  267. class TextChunkStreamResponse(StreamResponse):
  268. """
  269. TextChunkStreamResponse entity
  270. """
  271. class Data(BaseModel):
  272. """
  273. Data entity
  274. """
  275. text: str
  276. event: StreamEvent = StreamEvent.TEXT_CHUNK
  277. data: Data
  278. class TextReplaceStreamResponse(StreamResponse):
  279. """
  280. TextReplaceStreamResponse entity
  281. """
  282. class Data(BaseModel):
  283. """
  284. Data entity
  285. """
  286. text: str
  287. event: StreamEvent = StreamEvent.TEXT_REPLACE
  288. data: Data
  289. class PingStreamResponse(StreamResponse):
  290. """
  291. PingStreamResponse entity
  292. """
  293. event: StreamEvent = StreamEvent.PING
  294. class AppStreamResponse(BaseModel):
  295. """
  296. AppStreamResponse entity
  297. """
  298. stream_response: StreamResponse
  299. class ChatbotAppStreamResponse(AppStreamResponse):
  300. """
  301. ChatbotAppStreamResponse entity
  302. """
  303. conversation_id: str
  304. message_id: str
  305. created_at: int
  306. class CompletionAppStreamResponse(AppStreamResponse):
  307. """
  308. CompletionAppStreamResponse entity
  309. """
  310. message_id: str
  311. created_at: int
  312. class WorkflowAppStreamResponse(AppStreamResponse):
  313. """
  314. WorkflowAppStreamResponse entity
  315. """
  316. workflow_run_id: str
  317. class AppBlockingResponse(BaseModel):
  318. """
  319. AppBlockingResponse entity
  320. """
  321. task_id: str
  322. def to_dict(self) -> dict:
  323. return jsonable_encoder(self)
  324. class ChatbotAppBlockingResponse(AppBlockingResponse):
  325. """
  326. ChatbotAppBlockingResponse entity
  327. """
  328. class Data(BaseModel):
  329. """
  330. Data entity
  331. """
  332. id: str
  333. mode: str
  334. conversation_id: str
  335. message_id: str
  336. answer: str
  337. metadata: dict = {}
  338. created_at: int
  339. data: Data
  340. class CompletionAppBlockingResponse(AppBlockingResponse):
  341. """
  342. CompletionAppBlockingResponse entity
  343. """
  344. class Data(BaseModel):
  345. """
  346. Data entity
  347. """
  348. id: str
  349. mode: str
  350. message_id: str
  351. answer: str
  352. metadata: dict = {}
  353. created_at: int
  354. data: Data
  355. class WorkflowAppBlockingResponse(AppBlockingResponse):
  356. """
  357. WorkflowAppBlockingResponse entity
  358. """
  359. class Data(BaseModel):
  360. """
  361. Data entity
  362. """
  363. id: str
  364. workflow_id: str
  365. status: str
  366. outputs: Optional[dict] = None
  367. error: Optional[str] = None
  368. elapsed_time: float
  369. total_tokens: int
  370. total_steps: int
  371. created_at: int
  372. finished_at: int
  373. workflow_run_id: str
  374. data: Data