您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

task_entities.py 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. from collections.abc import Mapping, Sequence
  2. from enum import Enum
  3. from typing import Any, Optional
  4. from pydantic import BaseModel, ConfigDict, Field
  5. from core.model_runtime.entities.llm_entities import LLMResult, LLMUsage
  6. from core.model_runtime.utils.encoders import jsonable_encoder
  7. from core.rag.entities.citation_metadata import RetrievalSourceMetadata
  8. from core.workflow.entities import AgentNodeStrategyInit
  9. from core.workflow.enums import WorkflowNodeExecutionMetadataKey, WorkflowNodeExecutionStatus
  10. class AnnotationReplyAccount(BaseModel):
  11. id: str
  12. name: str
  13. class AnnotationReply(BaseModel):
  14. id: str
  15. account: AnnotationReplyAccount
  16. class TaskStateMetadata(BaseModel):
  17. annotation_reply: AnnotationReply | None = None
  18. retriever_resources: Sequence[RetrievalSourceMetadata] = Field(default_factory=list)
  19. usage: LLMUsage | None = None
  20. class TaskState(BaseModel):
  21. """
  22. TaskState entity
  23. """
  24. metadata: TaskStateMetadata = Field(default_factory=TaskStateMetadata)
  25. class EasyUITaskState(TaskState):
  26. """
  27. EasyUITaskState entity
  28. """
  29. llm_result: LLMResult
  30. class WorkflowTaskState(TaskState):
  31. """
  32. WorkflowTaskState entity
  33. """
  34. answer: str = ""
  35. class StreamEvent(Enum):
  36. """
  37. Stream event
  38. """
  39. PING = "ping"
  40. ERROR = "error"
  41. MESSAGE = "message"
  42. MESSAGE_END = "message_end"
  43. TTS_MESSAGE = "tts_message"
  44. TTS_MESSAGE_END = "tts_message_end"
  45. MESSAGE_FILE = "message_file"
  46. MESSAGE_REPLACE = "message_replace"
  47. AGENT_THOUGHT = "agent_thought"
  48. AGENT_MESSAGE = "agent_message"
  49. WORKFLOW_STARTED = "workflow_started"
  50. WORKFLOW_FINISHED = "workflow_finished"
  51. NODE_STARTED = "node_started"
  52. NODE_FINISHED = "node_finished"
  53. NODE_RETRY = "node_retry"
  54. ITERATION_STARTED = "iteration_started"
  55. ITERATION_NEXT = "iteration_next"
  56. ITERATION_COMPLETED = "iteration_completed"
  57. LOOP_STARTED = "loop_started"
  58. LOOP_NEXT = "loop_next"
  59. LOOP_COMPLETED = "loop_completed"
  60. TEXT_CHUNK = "text_chunk"
  61. TEXT_REPLACE = "text_replace"
  62. AGENT_LOG = "agent_log"
  63. class StreamResponse(BaseModel):
  64. """
  65. StreamResponse entity
  66. """
  67. event: StreamEvent
  68. task_id: str
  69. def to_dict(self):
  70. return jsonable_encoder(self)
  71. class ErrorStreamResponse(StreamResponse):
  72. """
  73. ErrorStreamResponse entity
  74. """
  75. event: StreamEvent = StreamEvent.ERROR
  76. err: Exception
  77. model_config = ConfigDict(arbitrary_types_allowed=True)
  78. class MessageStreamResponse(StreamResponse):
  79. """
  80. MessageStreamResponse entity
  81. """
  82. event: StreamEvent = StreamEvent.MESSAGE
  83. id: str
  84. answer: str
  85. from_variable_selector: Optional[list[str]] = None
  86. class MessageAudioStreamResponse(StreamResponse):
  87. """
  88. MessageStreamResponse entity
  89. """
  90. event: StreamEvent = StreamEvent.TTS_MESSAGE
  91. audio: str
  92. class MessageAudioEndStreamResponse(StreamResponse):
  93. """
  94. MessageStreamResponse entity
  95. """
  96. event: StreamEvent = StreamEvent.TTS_MESSAGE_END
  97. audio: str
  98. class MessageEndStreamResponse(StreamResponse):
  99. """
  100. MessageEndStreamResponse entity
  101. """
  102. event: StreamEvent = StreamEvent.MESSAGE_END
  103. id: str
  104. metadata: dict = Field(default_factory=dict)
  105. files: Optional[Sequence[Mapping[str, Any]]] = None
  106. class MessageFileStreamResponse(StreamResponse):
  107. """
  108. MessageFileStreamResponse entity
  109. """
  110. event: StreamEvent = StreamEvent.MESSAGE_FILE
  111. id: str
  112. type: str
  113. belongs_to: str
  114. url: str
  115. class MessageReplaceStreamResponse(StreamResponse):
  116. """
  117. MessageReplaceStreamResponse entity
  118. """
  119. event: StreamEvent = StreamEvent.MESSAGE_REPLACE
  120. answer: str
  121. reason: str
  122. class AgentThoughtStreamResponse(StreamResponse):
  123. """
  124. AgentThoughtStreamResponse entity
  125. """
  126. event: StreamEvent = StreamEvent.AGENT_THOUGHT
  127. id: str
  128. position: int
  129. thought: Optional[str] = None
  130. observation: Optional[str] = None
  131. tool: Optional[str] = None
  132. tool_labels: Optional[dict] = None
  133. tool_input: Optional[str] = None
  134. message_files: Optional[list[str]] = None
  135. class AgentMessageStreamResponse(StreamResponse):
  136. """
  137. AgentMessageStreamResponse entity
  138. """
  139. event: StreamEvent = StreamEvent.AGENT_MESSAGE
  140. id: str
  141. answer: str
  142. class WorkflowStartStreamResponse(StreamResponse):
  143. """
  144. WorkflowStartStreamResponse entity
  145. """
  146. class Data(BaseModel):
  147. """
  148. Data entity
  149. """
  150. id: str
  151. workflow_id: str
  152. inputs: Mapping[str, Any]
  153. created_at: int
  154. event: StreamEvent = StreamEvent.WORKFLOW_STARTED
  155. workflow_run_id: str
  156. data: Data
  157. class WorkflowFinishStreamResponse(StreamResponse):
  158. """
  159. WorkflowFinishStreamResponse entity
  160. """
  161. class Data(BaseModel):
  162. """
  163. Data entity
  164. """
  165. id: str
  166. workflow_id: str
  167. status: str
  168. outputs: Optional[Mapping[str, Any]] = None
  169. error: Optional[str] = None
  170. elapsed_time: float
  171. total_tokens: int
  172. total_steps: int
  173. created_by: Optional[dict] = None
  174. created_at: int
  175. finished_at: int
  176. exceptions_count: Optional[int] = 0
  177. files: Optional[Sequence[Mapping[str, Any]]] = []
  178. event: StreamEvent = StreamEvent.WORKFLOW_FINISHED
  179. workflow_run_id: str
  180. data: Data
  181. class NodeStartStreamResponse(StreamResponse):
  182. """
  183. NodeStartStreamResponse entity
  184. """
  185. class Data(BaseModel):
  186. """
  187. Data entity
  188. """
  189. id: str
  190. node_id: str
  191. node_type: str
  192. title: str
  193. index: int
  194. predecessor_node_id: Optional[str] = None
  195. inputs: Optional[Mapping[str, Any]] = None
  196. inputs_truncated: bool = False
  197. created_at: int
  198. extras: dict = Field(default_factory=dict)
  199. parallel_id: Optional[str] = None
  200. parallel_start_node_id: Optional[str] = None
  201. parent_parallel_id: Optional[str] = None
  202. parent_parallel_start_node_id: Optional[str] = None
  203. iteration_id: Optional[str] = None
  204. loop_id: Optional[str] = None
  205. parallel_run_id: Optional[str] = None
  206. agent_strategy: Optional[AgentNodeStrategyInit] = None
  207. event: StreamEvent = StreamEvent.NODE_STARTED
  208. workflow_run_id: str
  209. data: Data
  210. def to_ignore_detail_dict(self):
  211. return {
  212. "event": self.event.value,
  213. "task_id": self.task_id,
  214. "workflow_run_id": self.workflow_run_id,
  215. "data": {
  216. "id": self.data.id,
  217. "node_id": self.data.node_id,
  218. "node_type": self.data.node_type,
  219. "title": self.data.title,
  220. "index": self.data.index,
  221. "predecessor_node_id": self.data.predecessor_node_id,
  222. "inputs": None,
  223. "created_at": self.data.created_at,
  224. "extras": {},
  225. "parallel_id": self.data.parallel_id,
  226. "parallel_start_node_id": self.data.parallel_start_node_id,
  227. "parent_parallel_id": self.data.parent_parallel_id,
  228. "parent_parallel_start_node_id": self.data.parent_parallel_start_node_id,
  229. "iteration_id": self.data.iteration_id,
  230. "loop_id": self.data.loop_id,
  231. },
  232. }
  233. class NodeFinishStreamResponse(StreamResponse):
  234. """
  235. NodeFinishStreamResponse entity
  236. """
  237. class Data(BaseModel):
  238. """
  239. Data entity
  240. """
  241. id: str
  242. node_id: str
  243. node_type: str
  244. title: str
  245. index: int
  246. predecessor_node_id: Optional[str] = None
  247. inputs: Optional[Mapping[str, Any]] = None
  248. inputs_truncated: bool = False
  249. process_data: Optional[Mapping[str, Any]] = None
  250. process_data_truncated: bool = False
  251. outputs: Optional[Mapping[str, Any]] = None
  252. outputs_truncated: bool = True
  253. status: str
  254. error: Optional[str] = None
  255. elapsed_time: float
  256. execution_metadata: Optional[Mapping[WorkflowNodeExecutionMetadataKey, Any]] = None
  257. created_at: int
  258. finished_at: int
  259. files: Optional[Sequence[Mapping[str, Any]]] = []
  260. parallel_id: Optional[str] = None
  261. parallel_start_node_id: Optional[str] = None
  262. parent_parallel_id: Optional[str] = None
  263. parent_parallel_start_node_id: Optional[str] = None
  264. iteration_id: Optional[str] = None
  265. loop_id: Optional[str] = None
  266. event: StreamEvent = StreamEvent.NODE_FINISHED
  267. workflow_run_id: str
  268. data: Data
  269. def to_ignore_detail_dict(self):
  270. return {
  271. "event": self.event.value,
  272. "task_id": self.task_id,
  273. "workflow_run_id": self.workflow_run_id,
  274. "data": {
  275. "id": self.data.id,
  276. "node_id": self.data.node_id,
  277. "node_type": self.data.node_type,
  278. "title": self.data.title,
  279. "index": self.data.index,
  280. "predecessor_node_id": self.data.predecessor_node_id,
  281. "inputs": None,
  282. "process_data": None,
  283. "outputs": None,
  284. "status": self.data.status,
  285. "error": None,
  286. "elapsed_time": self.data.elapsed_time,
  287. "execution_metadata": None,
  288. "created_at": self.data.created_at,
  289. "finished_at": self.data.finished_at,
  290. "files": [],
  291. "parallel_id": self.data.parallel_id,
  292. "parallel_start_node_id": self.data.parallel_start_node_id,
  293. "parent_parallel_id": self.data.parent_parallel_id,
  294. "parent_parallel_start_node_id": self.data.parent_parallel_start_node_id,
  295. "iteration_id": self.data.iteration_id,
  296. "loop_id": self.data.loop_id,
  297. },
  298. }
  299. class NodeRetryStreamResponse(StreamResponse):
  300. """
  301. NodeFinishStreamResponse entity
  302. """
  303. class Data(BaseModel):
  304. """
  305. Data entity
  306. """
  307. id: str
  308. node_id: str
  309. node_type: str
  310. title: str
  311. index: int
  312. predecessor_node_id: Optional[str] = None
  313. inputs: Optional[Mapping[str, Any]] = None
  314. inputs_truncated: bool = False
  315. process_data: Optional[Mapping[str, Any]] = None
  316. process_data_truncated: bool = False
  317. outputs: Optional[Mapping[str, Any]] = None
  318. outputs_truncated: bool = False
  319. status: str
  320. error: Optional[str] = None
  321. elapsed_time: float
  322. execution_metadata: Optional[Mapping[WorkflowNodeExecutionMetadataKey, Any]] = None
  323. created_at: int
  324. finished_at: int
  325. files: Optional[Sequence[Mapping[str, Any]]] = []
  326. parallel_id: Optional[str] = None
  327. parallel_start_node_id: Optional[str] = None
  328. parent_parallel_id: Optional[str] = None
  329. parent_parallel_start_node_id: Optional[str] = None
  330. iteration_id: Optional[str] = None
  331. loop_id: Optional[str] = None
  332. retry_index: int = 0
  333. event: StreamEvent = StreamEvent.NODE_RETRY
  334. workflow_run_id: str
  335. data: Data
  336. def to_ignore_detail_dict(self):
  337. return {
  338. "event": self.event.value,
  339. "task_id": self.task_id,
  340. "workflow_run_id": self.workflow_run_id,
  341. "data": {
  342. "id": self.data.id,
  343. "node_id": self.data.node_id,
  344. "node_type": self.data.node_type,
  345. "title": self.data.title,
  346. "index": self.data.index,
  347. "predecessor_node_id": self.data.predecessor_node_id,
  348. "inputs": None,
  349. "process_data": None,
  350. "outputs": None,
  351. "status": self.data.status,
  352. "error": None,
  353. "elapsed_time": self.data.elapsed_time,
  354. "execution_metadata": None,
  355. "created_at": self.data.created_at,
  356. "finished_at": self.data.finished_at,
  357. "files": [],
  358. "parallel_id": self.data.parallel_id,
  359. "parallel_start_node_id": self.data.parallel_start_node_id,
  360. "parent_parallel_id": self.data.parent_parallel_id,
  361. "parent_parallel_start_node_id": self.data.parent_parallel_start_node_id,
  362. "iteration_id": self.data.iteration_id,
  363. "loop_id": self.data.loop_id,
  364. "retry_index": self.data.retry_index,
  365. },
  366. }
  367. class IterationNodeStartStreamResponse(StreamResponse):
  368. """
  369. NodeStartStreamResponse entity
  370. """
  371. class Data(BaseModel):
  372. """
  373. Data entity
  374. """
  375. id: str
  376. node_id: str
  377. node_type: str
  378. title: str
  379. created_at: int
  380. extras: dict = Field(default_factory=dict)
  381. metadata: Mapping = {}
  382. inputs: Mapping = {}
  383. inputs_truncated: bool = False
  384. event: StreamEvent = StreamEvent.ITERATION_STARTED
  385. workflow_run_id: str
  386. data: Data
  387. class IterationNodeNextStreamResponse(StreamResponse):
  388. """
  389. NodeStartStreamResponse entity
  390. """
  391. class Data(BaseModel):
  392. """
  393. Data entity
  394. """
  395. id: str
  396. node_id: str
  397. node_type: str
  398. title: str
  399. index: int
  400. created_at: int
  401. extras: dict = Field(default_factory=dict)
  402. event: StreamEvent = StreamEvent.ITERATION_NEXT
  403. workflow_run_id: str
  404. data: Data
  405. class IterationNodeCompletedStreamResponse(StreamResponse):
  406. """
  407. NodeCompletedStreamResponse entity
  408. """
  409. class Data(BaseModel):
  410. """
  411. Data entity
  412. """
  413. id: str
  414. node_id: str
  415. node_type: str
  416. title: str
  417. outputs: Optional[Mapping] = None
  418. outputs_truncated: bool = False
  419. created_at: int
  420. extras: Optional[dict] = None
  421. inputs: Optional[Mapping] = None
  422. inputs_truncated: bool = False
  423. status: WorkflowNodeExecutionStatus
  424. error: Optional[str] = None
  425. elapsed_time: float
  426. total_tokens: int
  427. execution_metadata: Optional[Mapping] = None
  428. finished_at: int
  429. steps: int
  430. event: StreamEvent = StreamEvent.ITERATION_COMPLETED
  431. workflow_run_id: str
  432. data: Data
  433. class LoopNodeStartStreamResponse(StreamResponse):
  434. """
  435. NodeStartStreamResponse entity
  436. """
  437. class Data(BaseModel):
  438. """
  439. Data entity
  440. """
  441. id: str
  442. node_id: str
  443. node_type: str
  444. title: str
  445. created_at: int
  446. extras: dict = Field(default_factory=dict)
  447. metadata: Mapping = {}
  448. inputs: Mapping = {}
  449. inputs_truncated: bool = False
  450. parallel_id: Optional[str] = None
  451. parallel_start_node_id: Optional[str] = None
  452. event: StreamEvent = StreamEvent.LOOP_STARTED
  453. workflow_run_id: str
  454. data: Data
  455. class LoopNodeNextStreamResponse(StreamResponse):
  456. """
  457. NodeStartStreamResponse entity
  458. """
  459. class Data(BaseModel):
  460. """
  461. Data entity
  462. """
  463. id: str
  464. node_id: str
  465. node_type: str
  466. title: str
  467. index: int
  468. created_at: int
  469. pre_loop_output: Optional[Any] = None
  470. extras: dict = Field(default_factory=dict)
  471. parallel_id: Optional[str] = None
  472. parallel_start_node_id: Optional[str] = None
  473. parallel_mode_run_id: Optional[str] = None
  474. event: StreamEvent = StreamEvent.LOOP_NEXT
  475. workflow_run_id: str
  476. data: Data
  477. class LoopNodeCompletedStreamResponse(StreamResponse):
  478. """
  479. NodeCompletedStreamResponse entity
  480. """
  481. class Data(BaseModel):
  482. """
  483. Data entity
  484. """
  485. id: str
  486. node_id: str
  487. node_type: str
  488. title: str
  489. outputs: Optional[Mapping] = None
  490. outputs_truncated: bool = False
  491. created_at: int
  492. extras: Optional[dict] = None
  493. inputs: Optional[Mapping] = None
  494. inputs_truncated: bool = False
  495. status: WorkflowNodeExecutionStatus
  496. error: Optional[str] = None
  497. elapsed_time: float
  498. total_tokens: int
  499. execution_metadata: Optional[Mapping] = None
  500. finished_at: int
  501. steps: int
  502. parallel_id: Optional[str] = None
  503. parallel_start_node_id: Optional[str] = None
  504. event: StreamEvent = StreamEvent.LOOP_COMPLETED
  505. workflow_run_id: str
  506. data: Data
  507. class TextChunkStreamResponse(StreamResponse):
  508. """
  509. TextChunkStreamResponse entity
  510. """
  511. class Data(BaseModel):
  512. """
  513. Data entity
  514. """
  515. text: str
  516. from_variable_selector: Optional[list[str]] = None
  517. event: StreamEvent = StreamEvent.TEXT_CHUNK
  518. data: Data
  519. class TextReplaceStreamResponse(StreamResponse):
  520. """
  521. TextReplaceStreamResponse entity
  522. """
  523. class Data(BaseModel):
  524. """
  525. Data entity
  526. """
  527. text: str
  528. event: StreamEvent = StreamEvent.TEXT_REPLACE
  529. data: Data
  530. class PingStreamResponse(StreamResponse):
  531. """
  532. PingStreamResponse entity
  533. """
  534. event: StreamEvent = StreamEvent.PING
  535. class AppStreamResponse(BaseModel):
  536. """
  537. AppStreamResponse entity
  538. """
  539. stream_response: StreamResponse
  540. class ChatbotAppStreamResponse(AppStreamResponse):
  541. """
  542. ChatbotAppStreamResponse entity
  543. """
  544. conversation_id: str
  545. message_id: str
  546. created_at: int
  547. class CompletionAppStreamResponse(AppStreamResponse):
  548. """
  549. CompletionAppStreamResponse entity
  550. """
  551. message_id: str
  552. created_at: int
  553. class WorkflowAppStreamResponse(AppStreamResponse):
  554. """
  555. WorkflowAppStreamResponse entity
  556. """
  557. workflow_run_id: Optional[str] = None
  558. class AppBlockingResponse(BaseModel):
  559. """
  560. AppBlockingResponse entity
  561. """
  562. task_id: str
  563. def to_dict(self):
  564. return jsonable_encoder(self)
  565. class ChatbotAppBlockingResponse(AppBlockingResponse):
  566. """
  567. ChatbotAppBlockingResponse entity
  568. """
  569. class Data(BaseModel):
  570. """
  571. Data entity
  572. """
  573. id: str
  574. mode: str
  575. conversation_id: str
  576. message_id: str
  577. answer: str
  578. metadata: dict = Field(default_factory=dict)
  579. created_at: int
  580. data: Data
  581. class CompletionAppBlockingResponse(AppBlockingResponse):
  582. """
  583. CompletionAppBlockingResponse entity
  584. """
  585. class Data(BaseModel):
  586. """
  587. Data entity
  588. """
  589. id: str
  590. mode: str
  591. message_id: str
  592. answer: str
  593. metadata: dict = Field(default_factory=dict)
  594. created_at: int
  595. data: Data
  596. class WorkflowAppBlockingResponse(AppBlockingResponse):
  597. """
  598. WorkflowAppBlockingResponse entity
  599. """
  600. class Data(BaseModel):
  601. """
  602. Data entity
  603. """
  604. id: str
  605. workflow_id: str
  606. status: str
  607. outputs: Optional[Mapping[str, Any]] = None
  608. error: Optional[str] = None
  609. elapsed_time: float
  610. total_tokens: int
  611. total_steps: int
  612. created_at: int
  613. finished_at: int
  614. workflow_run_id: str
  615. data: Data
  616. class AgentLogStreamResponse(StreamResponse):
  617. """
  618. AgentLogStreamResponse entity
  619. """
  620. class Data(BaseModel):
  621. """
  622. Data entity
  623. """
  624. node_execution_id: str
  625. id: str
  626. label: str
  627. parent_id: str | None
  628. error: str | None
  629. status: str
  630. data: Mapping[str, Any]
  631. metadata: Optional[Mapping[str, Any]] = None
  632. node_id: str
  633. event: StreamEvent = StreamEvent.AGENT_LOG
  634. data: Data