task_entities.py 8.4 KB

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