workflow.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import type { Viewport } from 'reactflow'
  2. import type { BlockEnum, CommonNodeType, ConversationVariable, Edge, EnvironmentVariable, InputVar, Node, ValueSelector, VarType, Variable } from '@/app/components/workflow/types'
  3. import type { TransferMethod } from '@/types/app'
  4. import type { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types'
  5. import type { BeforeRunFormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form'
  6. import type { SpecialResultPanelProps } from '@/app/components/workflow/run/special-result-panel'
  7. import type { MutableRefObject } from 'react'
  8. export type AgentLogItem = {
  9. node_execution_id: string,
  10. id: string,
  11. node_id: string,
  12. parent_id?: string,
  13. label: string,
  14. data: object, // debug data
  15. error?: string,
  16. status: string,
  17. metadata?: {
  18. elapsed_time?: number
  19. provider?: string
  20. icon?: string
  21. },
  22. }
  23. export type AgentLogItemWithChildren = AgentLogItem & {
  24. hasCircle?: boolean
  25. children: AgentLogItemWithChildren[]
  26. }
  27. export type NodeTracing = {
  28. id: string
  29. index: number
  30. predecessor_node_id: string
  31. node_id: string
  32. iteration_id?: string
  33. loop_id?: string
  34. node_type: BlockEnum
  35. title: string
  36. inputs: any
  37. process_data: any
  38. outputs?: Record<string, any>
  39. status: string
  40. parallel_run_id?: string
  41. error?: string
  42. elapsed_time: number
  43. execution_metadata?: {
  44. total_tokens: number
  45. total_price: number
  46. currency: string
  47. iteration_id?: string
  48. iteration_index?: number
  49. loop_id?: string
  50. loop_index?: number
  51. parallel_id?: string
  52. parallel_start_node_id?: string
  53. parent_parallel_id?: string
  54. parent_parallel_start_node_id?: string
  55. parallel_mode_run_id?: string
  56. iteration_duration_map?: IterationDurationMap
  57. loop_duration_map?: LoopDurationMap
  58. error_strategy?: ErrorHandleTypeEnum
  59. agent_log?: AgentLogItem[]
  60. tool_info?: {
  61. agent_strategy?: string
  62. icon?: string
  63. }
  64. loop_variable_map?: Record<string, any>
  65. }
  66. metadata: {
  67. iterator_length: number
  68. iterator_index: number
  69. loop_length: number
  70. loop_index: number
  71. }
  72. created_at: number
  73. created_by: {
  74. id: string
  75. name: string
  76. email: string
  77. }
  78. iterDurationMap?: IterationDurationMap
  79. loopDurationMap?: LoopDurationMap
  80. finished_at: number
  81. extras?: any
  82. expand?: boolean // for UI
  83. details?: NodeTracing[][] // iteration or loop detail
  84. retryDetail?: NodeTracing[] // retry detail
  85. retry_index?: number
  86. parallelDetail?: { // parallel detail. if is in parallel, this field will be set
  87. isParallelStartNode?: boolean
  88. parallelTitle?: string
  89. branchTitle?: string
  90. children?: NodeTracing[]
  91. }
  92. parallel_id?: string
  93. parallel_start_node_id?: string
  94. parent_parallel_id?: string
  95. parent_parallel_start_node_id?: string
  96. agentLog?: AgentLogItemWithChildren[] // agent log
  97. }
  98. export type FetchWorkflowDraftResponse = {
  99. id: string
  100. graph: {
  101. nodes: Node[]
  102. edges: Edge[]
  103. viewport?: Viewport
  104. }
  105. features?: any
  106. created_at: number
  107. created_by: {
  108. id: string
  109. name: string
  110. email: string
  111. }
  112. hash: string
  113. updated_at: number
  114. updated_by: {
  115. id: string
  116. name: string
  117. email: string
  118. },
  119. tool_published: boolean
  120. environment_variables?: EnvironmentVariable[]
  121. conversation_variables?: ConversationVariable[]
  122. version: string
  123. marked_name: string
  124. marked_comment: string
  125. }
  126. export type VersionHistory = FetchWorkflowDraftResponse
  127. export type FetchWorkflowDraftPageParams = {
  128. appId: string
  129. initialPage: number
  130. limit: number
  131. userId?: string
  132. namedOnly?: boolean
  133. }
  134. export type FetchWorkflowDraftPageResponse = {
  135. items: VersionHistory[]
  136. has_more: boolean
  137. page: number
  138. }
  139. export type NodeTracingListResponse = {
  140. data: NodeTracing[]
  141. }
  142. export type WorkflowStartedResponse = {
  143. task_id: string
  144. workflow_run_id: string
  145. event: string
  146. data: {
  147. id: string
  148. workflow_id: string
  149. created_at: number
  150. }
  151. }
  152. export type WorkflowFinishedResponse = {
  153. task_id: string
  154. workflow_run_id: string
  155. event: string
  156. data: {
  157. id: string
  158. workflow_id: string
  159. status: string
  160. outputs: any
  161. error: string
  162. elapsed_time: number
  163. total_tokens: number
  164. total_steps: number
  165. created_at: number
  166. created_by: {
  167. id: string
  168. name: string
  169. email: string
  170. }
  171. finished_at: number
  172. files?: FileResponse[]
  173. }
  174. }
  175. export type NodeStartedResponse = {
  176. task_id: string
  177. workflow_run_id: string
  178. event: string
  179. data: NodeTracing
  180. }
  181. export type FileResponse = {
  182. related_id: string
  183. extension: string
  184. filename: string
  185. size: number
  186. mime_type: string
  187. transfer_method: TransferMethod
  188. type: string
  189. url: string
  190. upload_file_id: string
  191. remote_url: string
  192. }
  193. export type NodeFinishedResponse = {
  194. task_id: string
  195. workflow_run_id: string
  196. event: string
  197. data: NodeTracing
  198. }
  199. export type IterationStartedResponse = {
  200. task_id: string
  201. workflow_run_id: string
  202. event: string
  203. data: NodeTracing
  204. }
  205. export type IterationNextResponse = {
  206. task_id: string
  207. workflow_run_id: string
  208. event: string
  209. data: NodeTracing
  210. }
  211. export type IterationFinishedResponse = {
  212. task_id: string
  213. workflow_run_id: string
  214. event: string
  215. data: NodeTracing
  216. }
  217. export type LoopStartedResponse = {
  218. task_id: string
  219. workflow_run_id: string
  220. event: string
  221. data: NodeTracing
  222. }
  223. export type LoopNextResponse = {
  224. task_id: string
  225. workflow_run_id: string
  226. event: string
  227. data: NodeTracing
  228. }
  229. export type LoopFinishedResponse = {
  230. task_id: string
  231. workflow_run_id: string
  232. event: string
  233. data: NodeTracing
  234. }
  235. export type ParallelBranchStartedResponse = {
  236. task_id: string
  237. workflow_run_id: string
  238. event: string
  239. data: NodeTracing
  240. }
  241. export type ParallelBranchFinishedResponse = {
  242. task_id: string
  243. workflow_run_id: string
  244. event: string
  245. data: NodeTracing
  246. }
  247. export type TextChunkResponse = {
  248. task_id: string
  249. workflow_run_id: string
  250. event: string
  251. data: {
  252. text: string
  253. }
  254. }
  255. export type TextReplaceResponse = {
  256. task_id: string
  257. workflow_run_id: string
  258. event: string
  259. data: {
  260. text: string
  261. }
  262. }
  263. export type AgentLogResponse = {
  264. task_id: string
  265. event: string
  266. data: AgentLogItemWithChildren
  267. }
  268. export type WorkflowRunHistory = {
  269. id: string
  270. version: string
  271. conversation_id?: string
  272. message_id?: string
  273. graph: {
  274. nodes: Node[]
  275. edges: Edge[]
  276. viewport?: Viewport
  277. }
  278. inputs: Record<string, string>
  279. status: string
  280. outputs: Record<string, any>
  281. error?: string
  282. elapsed_time: number
  283. total_tokens: number
  284. total_steps: number
  285. created_at: number
  286. finished_at: number
  287. created_by_account: {
  288. id: string
  289. name: string
  290. email: string
  291. }
  292. }
  293. export type WorkflowRunHistoryResponse = {
  294. data: WorkflowRunHistory[]
  295. }
  296. export type ChatRunHistoryResponse = {
  297. data: WorkflowRunHistory[]
  298. }
  299. export type NodesDefaultConfigsResponse = {
  300. type: string
  301. config: any
  302. }[]
  303. export type ConversationVariableResponse = {
  304. data: (ConversationVariable & { updated_at: number; created_at: number })[]
  305. has_more: boolean
  306. limit: number
  307. total: number
  308. page: number
  309. }
  310. export type IterationDurationMap = Record<string, number>
  311. export type LoopDurationMap = Record<string, number>
  312. export type LoopVariableMap = Record<string, any>
  313. export type WorkflowConfigResponse = {
  314. parallel_depth_limit: number
  315. }
  316. export type PublishWorkflowParams = {
  317. title: string
  318. releaseNotes: string
  319. }
  320. export type UpdateWorkflowParams = {
  321. workflowId: string
  322. title: string
  323. releaseNotes: string
  324. }
  325. export type PanelExposedType = {
  326. singleRunParams: Pick<BeforeRunFormProps, 'forms'> & Partial<SpecialResultPanelProps>
  327. }
  328. export type PanelProps = {
  329. getInputVars: (textList: string[]) => InputVar[]
  330. toVarInputs: (variables: Variable[]) => InputVar[]
  331. runInputData: Record<string, any>
  332. runInputDataRef: MutableRefObject<Record<string, any>>
  333. setRunInputData: (data: Record<string, any>) => void
  334. runResult: any
  335. }
  336. export type NodeRunResult = NodeTracing
  337. // Var Inspect
  338. export enum VarInInspectType {
  339. conversation = 'conversation',
  340. environment = 'env',
  341. node = 'node',
  342. system = 'sys',
  343. }
  344. export type VarInInspect = {
  345. id: string
  346. type: VarInInspectType
  347. name: string
  348. description: string
  349. selector: ValueSelector // can get node id from selector[0]
  350. value_type: VarType
  351. value: any
  352. edited: boolean
  353. visible: boolean
  354. }
  355. export type NodeWithVar = {
  356. nodeId: string
  357. nodePayload: CommonNodeType
  358. nodeType: BlockEnum
  359. title: string
  360. vars: VarInInspect[]
  361. isSingRunRunning?: boolean
  362. isValueFetched?: boolean
  363. }