use-one-step-run.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. import { useCallback, useEffect, useRef, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { unionBy } from 'lodash-es'
  4. import produce from 'immer'
  5. import {
  6. useIsChatMode,
  7. useNodeDataUpdate,
  8. useWorkflow,
  9. } from '@/app/components/workflow/hooks'
  10. import { getNodeInfoById, isConversationVar, isENV, isSystemVar, toNodeOutputVars } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  11. import type { CommonNodeType, InputVar, ValueSelector, Var, Variable } from '@/app/components/workflow/types'
  12. import { BlockEnum, InputVarType, NodeRunningStatus, VarType } from '@/app/components/workflow/types'
  13. import { useStore as useAppStore } from '@/app/components/app/store'
  14. import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
  15. import { fetchNodeInspectVars, getIterationSingleNodeRunUrl, getLoopSingleNodeRunUrl, singleNodeRun } from '@/service/workflow'
  16. import Toast from '@/app/components/base/toast'
  17. import LLMDefault from '@/app/components/workflow/nodes/llm/default'
  18. import KnowledgeRetrievalDefault from '@/app/components/workflow/nodes/knowledge-retrieval/default'
  19. import IfElseDefault from '@/app/components/workflow/nodes/if-else/default'
  20. import CodeDefault from '@/app/components/workflow/nodes/code/default'
  21. import TemplateTransformDefault from '@/app/components/workflow/nodes/template-transform/default'
  22. import QuestionClassifyDefault from '@/app/components/workflow/nodes/question-classifier/default'
  23. import HTTPDefault from '@/app/components/workflow/nodes/http/default'
  24. import ToolDefault from '@/app/components/workflow/nodes/tool/default'
  25. import VariableAssigner from '@/app/components/workflow/nodes/variable-assigner/default'
  26. import Assigner from '@/app/components/workflow/nodes/assigner/default'
  27. import ParameterExtractorDefault from '@/app/components/workflow/nodes/parameter-extractor/default'
  28. import IterationDefault from '@/app/components/workflow/nodes/iteration/default'
  29. import DocumentExtractorDefault from '@/app/components/workflow/nodes/document-extractor/default'
  30. import LoopDefault from '@/app/components/workflow/nodes/loop/default'
  31. import { ssePost } from '@/service/base'
  32. import { noop } from 'lodash-es'
  33. import { getInputVars as doGetInputVars } from '@/app/components/base/prompt-editor/constants'
  34. import type { NodeRunResult, NodeTracing } from '@/types/workflow'
  35. const { checkValid: checkLLMValid } = LLMDefault
  36. const { checkValid: checkKnowledgeRetrievalValid } = KnowledgeRetrievalDefault
  37. const { checkValid: checkIfElseValid } = IfElseDefault
  38. const { checkValid: checkCodeValid } = CodeDefault
  39. const { checkValid: checkTemplateTransformValid } = TemplateTransformDefault
  40. const { checkValid: checkQuestionClassifyValid } = QuestionClassifyDefault
  41. const { checkValid: checkHttpValid } = HTTPDefault
  42. const { checkValid: checkToolValid } = ToolDefault
  43. const { checkValid: checkVariableAssignerValid } = VariableAssigner
  44. const { checkValid: checkAssignerValid } = Assigner
  45. const { checkValid: checkParameterExtractorValid } = ParameterExtractorDefault
  46. const { checkValid: checkIterationValid } = IterationDefault
  47. const { checkValid: checkDocumentExtractorValid } = DocumentExtractorDefault
  48. const { checkValid: checkLoopValid } = LoopDefault
  49. import {
  50. useStoreApi,
  51. } from 'reactflow'
  52. import { useInvalidLastRun } from '@/service/use-workflow'
  53. import useInspectVarsCrud from '../../../hooks/use-inspect-vars-crud'
  54. // eslint-disable-next-line ts/no-unsafe-function-type
  55. const checkValidFns: Record<BlockEnum, Function> = {
  56. [BlockEnum.LLM]: checkLLMValid,
  57. [BlockEnum.KnowledgeRetrieval]: checkKnowledgeRetrievalValid,
  58. [BlockEnum.IfElse]: checkIfElseValid,
  59. [BlockEnum.Code]: checkCodeValid,
  60. [BlockEnum.TemplateTransform]: checkTemplateTransformValid,
  61. [BlockEnum.QuestionClassifier]: checkQuestionClassifyValid,
  62. [BlockEnum.HttpRequest]: checkHttpValid,
  63. [BlockEnum.Tool]: checkToolValid,
  64. [BlockEnum.VariableAssigner]: checkAssignerValid,
  65. [BlockEnum.VariableAggregator]: checkVariableAssignerValid,
  66. [BlockEnum.ParameterExtractor]: checkParameterExtractorValid,
  67. [BlockEnum.Iteration]: checkIterationValid,
  68. [BlockEnum.DocExtractor]: checkDocumentExtractorValid,
  69. [BlockEnum.Loop]: checkLoopValid,
  70. } as any
  71. export type Params<T> = {
  72. id: string
  73. data: CommonNodeType<T>
  74. defaultRunInputData: Record<string, any>
  75. moreDataForCheckValid?: any
  76. iteratorInputKey?: string
  77. loopInputKey?: string
  78. isRunAfterSingleRun: boolean
  79. isPaused: boolean
  80. }
  81. const varTypeToInputVarType = (type: VarType, {
  82. isSelect,
  83. isParagraph,
  84. }: {
  85. isSelect: boolean
  86. isParagraph: boolean
  87. }) => {
  88. if (isSelect)
  89. return InputVarType.select
  90. if (isParagraph)
  91. return InputVarType.paragraph
  92. if (type === VarType.number)
  93. return InputVarType.number
  94. if (type === VarType.boolean)
  95. return InputVarType.checkbox
  96. if ([VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(type))
  97. return InputVarType.json
  98. if (type === VarType.file)
  99. return InputVarType.singleFile
  100. if (type === VarType.arrayFile)
  101. return InputVarType.multiFiles
  102. return InputVarType.textInput
  103. }
  104. const useOneStepRun = <T>({
  105. id,
  106. data,
  107. defaultRunInputData,
  108. moreDataForCheckValid,
  109. iteratorInputKey,
  110. loopInputKey,
  111. isRunAfterSingleRun,
  112. isPaused,
  113. }: Params<T>) => {
  114. const { t } = useTranslation()
  115. const { getBeforeNodesInSameBranch, getBeforeNodesInSameBranchIncludeParent } = useWorkflow() as any
  116. const conversationVariables = useStore(s => s.conversationVariables)
  117. const isChatMode = useIsChatMode()
  118. const isIteration = data.type === BlockEnum.Iteration
  119. const isLoop = data.type === BlockEnum.Loop
  120. const isStartNode = data.type === BlockEnum.Start
  121. const availableNodes = getBeforeNodesInSameBranch(id)
  122. const availableNodesIncludeParent = getBeforeNodesInSameBranchIncludeParent(id)
  123. const allOutputVars = toNodeOutputVars(availableNodes, isChatMode, undefined, undefined, conversationVariables)
  124. const getVar = (valueSelector: ValueSelector): Var | undefined => {
  125. const isSystem = valueSelector[0] === 'sys'
  126. const targetVar = allOutputVars.find(item => isSystem ? !!item.isStartNode : item.nodeId === valueSelector[0])
  127. if (!targetVar)
  128. return undefined
  129. if (isSystem)
  130. return targetVar.vars.find(item => item.variable.split('.')[1] === valueSelector[1])
  131. let curr: any = targetVar.vars
  132. for (let i = 1; i < valueSelector.length; i++) {
  133. const key = valueSelector[i]
  134. const isLast = i === valueSelector.length - 1
  135. if (Array.isArray(curr))
  136. curr = curr.find((v: any) => v.variable.replace('conversation.', '') === key)
  137. if (isLast)
  138. return curr
  139. else if (curr?.type === VarType.object || curr?.type === VarType.file)
  140. curr = curr.children
  141. }
  142. return undefined
  143. }
  144. const checkValid = checkValidFns[data.type]
  145. const appId = useAppStore.getState().appDetail?.id
  146. const [runInputData, setRunInputData] = useState<Record<string, any>>(defaultRunInputData || {})
  147. const runInputDataRef = useRef(runInputData)
  148. const handleSetRunInputData = useCallback((data: Record<string, any>) => {
  149. runInputDataRef.current = data
  150. setRunInputData(data)
  151. }, [])
  152. const iterationTimes = iteratorInputKey ? runInputData[iteratorInputKey]?.length : 0
  153. const loopTimes = loopInputKey ? runInputData[loopInputKey]?.length : 0
  154. const store = useStoreApi()
  155. const workflowStore = useWorkflowStore()
  156. const {
  157. setShowSingleRunPanel,
  158. } = workflowStore.getState()
  159. const invalidLastRun = useInvalidLastRun(appId!, id)
  160. const [runResult, doSetRunResult] = useState<NodeRunResult | null>(null)
  161. const {
  162. appendNodeInspectVars,
  163. invalidateSysVarValues,
  164. invalidateConversationVarValues,
  165. } = useInspectVarsCrud()
  166. const runningStatus = data._singleRunningStatus || NodeRunningStatus.NotStart
  167. const isPausedRef = useRef(isPaused)
  168. useEffect(() => {
  169. isPausedRef.current = isPaused
  170. }, [isPaused])
  171. const setRunResult = useCallback(async (data: NodeRunResult | null) => {
  172. const isPaused = isPausedRef.current
  173. // The backend don't support pause the single run, so the frontend handle the pause state.
  174. if (isPaused)
  175. return
  176. const canRunLastRun = !isRunAfterSingleRun || runningStatus === NodeRunningStatus.Succeeded
  177. if (!canRunLastRun) {
  178. doSetRunResult(data)
  179. return
  180. }
  181. // run fail may also update the inspect vars when the node set the error default output.
  182. const vars = await fetchNodeInspectVars(appId!, id)
  183. const { getNodes } = store.getState()
  184. const nodes = getNodes()
  185. appendNodeInspectVars(id, vars, nodes)
  186. if (data?.status === NodeRunningStatus.Succeeded) {
  187. invalidLastRun()
  188. if (isStartNode)
  189. invalidateSysVarValues()
  190. invalidateConversationVarValues() // loop, iteration, variable assigner node can update the conversation variables, but to simple the logic(some nodes may also can update in the future), all nodes refresh.
  191. }
  192. }, [isRunAfterSingleRun, runningStatus, appId, id, store, appendNodeInspectVars, invalidLastRun, isStartNode, invalidateSysVarValues, invalidateConversationVarValues])
  193. const { handleNodeDataUpdate }: { handleNodeDataUpdate: (data: any) => void } = useNodeDataUpdate()
  194. const setNodeRunning = () => {
  195. handleNodeDataUpdate({
  196. id,
  197. data: {
  198. ...data,
  199. _singleRunningStatus: NodeRunningStatus.Running,
  200. },
  201. })
  202. }
  203. const checkValidWrap = () => {
  204. if (!checkValid)
  205. return { isValid: true, errorMessage: '' }
  206. const res = checkValid(data, t, moreDataForCheckValid)
  207. if (!res.isValid) {
  208. handleNodeDataUpdate({
  209. id,
  210. data: {
  211. ...data,
  212. _isSingleRun: false,
  213. },
  214. })
  215. Toast.notify({
  216. type: 'error',
  217. message: res.errorMessage,
  218. })
  219. }
  220. return res
  221. }
  222. const [canShowSingleRun, setCanShowSingleRun] = useState(false)
  223. const isShowSingleRun = data._isSingleRun && canShowSingleRun
  224. const [iterationRunResult, setIterationRunResult] = useState<NodeTracing[]>([])
  225. const [loopRunResult, setLoopRunResult] = useState<NodeTracing[]>([])
  226. useEffect(() => {
  227. if (!checkValid) {
  228. setCanShowSingleRun(true)
  229. return
  230. }
  231. if (data._isSingleRun) {
  232. const { isValid } = checkValidWrap()
  233. setCanShowSingleRun(isValid)
  234. }
  235. }, [data._isSingleRun])
  236. useEffect(() => {
  237. setShowSingleRunPanel(!!isShowSingleRun)
  238. }, [isShowSingleRun, setShowSingleRunPanel])
  239. const hideSingleRun = () => {
  240. handleNodeDataUpdate({
  241. id,
  242. data: {
  243. ...data,
  244. _isSingleRun: false,
  245. },
  246. })
  247. }
  248. const showSingleRun = () => {
  249. handleNodeDataUpdate({
  250. id,
  251. data: {
  252. ...data,
  253. _isSingleRun: true,
  254. },
  255. })
  256. }
  257. const isCompleted = runningStatus === NodeRunningStatus.Succeeded || runningStatus === NodeRunningStatus.Failed
  258. const handleRun = async (submitData: Record<string, any>) => {
  259. handleNodeDataUpdate({
  260. id,
  261. data: {
  262. ...data,
  263. _isSingleRun: false,
  264. _singleRunningStatus: NodeRunningStatus.Running,
  265. },
  266. })
  267. let res: any
  268. let hasError = false
  269. try {
  270. if (!isIteration && !isLoop) {
  271. const isStartNode = data.type === BlockEnum.Start
  272. const postData: Record<string, any> = {}
  273. if (isStartNode) {
  274. const { '#sys.query#': query, '#sys.files#': files, ...inputs } = submitData
  275. if (isChatMode)
  276. postData.conversation_id = ''
  277. postData.inputs = inputs
  278. postData.query = query
  279. postData.files = files || []
  280. }
  281. else {
  282. postData.inputs = submitData
  283. }
  284. res = await singleNodeRun(appId!, id, postData) as any
  285. }
  286. else if (isIteration) {
  287. setIterationRunResult([])
  288. let _iterationResult: NodeTracing[] = []
  289. let _runResult: any = null
  290. ssePost(
  291. getIterationSingleNodeRunUrl(isChatMode, appId!, id),
  292. { body: { inputs: submitData } },
  293. {
  294. onWorkflowStarted: noop,
  295. onWorkflowFinished: (params) => {
  296. if (isPausedRef.current)
  297. return
  298. handleNodeDataUpdate({
  299. id,
  300. data: {
  301. ...data,
  302. _isSingleRun: false,
  303. _singleRunningStatus: NodeRunningStatus.Succeeded,
  304. },
  305. })
  306. const { data: iterationData } = params
  307. _runResult.created_by = iterationData.created_by.name
  308. setRunResult(_runResult)
  309. },
  310. onIterationStart: (params) => {
  311. const newIterationRunResult = produce(_iterationResult, (draft) => {
  312. draft.push({
  313. ...params.data,
  314. status: NodeRunningStatus.Running,
  315. })
  316. })
  317. _iterationResult = newIterationRunResult
  318. setIterationRunResult(newIterationRunResult)
  319. },
  320. onIterationNext: () => {
  321. // iteration next trigger time is triggered one more time than iterationTimes
  322. if (_iterationResult.length >= iterationTimes!)
  323. return _iterationResult.length >= iterationTimes!
  324. },
  325. onIterationFinish: (params) => {
  326. _runResult = params.data
  327. setRunResult(_runResult)
  328. const iterationRunResult = _iterationResult
  329. const currentIndex = iterationRunResult.findIndex(trace => trace.id === params.data.id)
  330. const newIterationRunResult = produce(iterationRunResult, (draft) => {
  331. if (currentIndex > -1) {
  332. draft[currentIndex] = {
  333. ...draft[currentIndex],
  334. ...data,
  335. }
  336. }
  337. })
  338. _iterationResult = newIterationRunResult
  339. setIterationRunResult(newIterationRunResult)
  340. },
  341. onNodeStarted: (params) => {
  342. const newIterationRunResult = produce(_iterationResult, (draft) => {
  343. draft.push({
  344. ...params.data,
  345. status: NodeRunningStatus.Running,
  346. })
  347. })
  348. _iterationResult = newIterationRunResult
  349. setIterationRunResult(newIterationRunResult)
  350. },
  351. onNodeFinished: (params) => {
  352. const iterationRunResult = _iterationResult
  353. const { data } = params
  354. const currentIndex = iterationRunResult.findIndex(trace => trace.id === data.id)
  355. const newIterationRunResult = produce(iterationRunResult, (draft) => {
  356. if (currentIndex > -1) {
  357. draft[currentIndex] = {
  358. ...draft[currentIndex],
  359. ...data,
  360. }
  361. }
  362. })
  363. _iterationResult = newIterationRunResult
  364. setIterationRunResult(newIterationRunResult)
  365. },
  366. onNodeRetry: (params) => {
  367. const newIterationRunResult = produce(_iterationResult, (draft) => {
  368. draft.push(params.data)
  369. })
  370. _iterationResult = newIterationRunResult
  371. setIterationRunResult(newIterationRunResult)
  372. },
  373. onError: () => {
  374. if (isPausedRef.current)
  375. return
  376. handleNodeDataUpdate({
  377. id,
  378. data: {
  379. ...data,
  380. _isSingleRun: false,
  381. _singleRunningStatus: NodeRunningStatus.Failed,
  382. },
  383. })
  384. },
  385. },
  386. )
  387. }
  388. else if (isLoop) {
  389. setLoopRunResult([])
  390. let _loopResult: NodeTracing[] = []
  391. let _runResult: any = null
  392. ssePost(
  393. getLoopSingleNodeRunUrl(isChatMode, appId!, id),
  394. { body: { inputs: submitData } },
  395. {
  396. onWorkflowStarted: noop,
  397. onWorkflowFinished: (params) => {
  398. if (isPausedRef.current)
  399. return
  400. handleNodeDataUpdate({
  401. id,
  402. data: {
  403. ...data,
  404. _isSingleRun: false,
  405. _singleRunningStatus: NodeRunningStatus.Succeeded,
  406. },
  407. })
  408. const { data: loopData } = params
  409. _runResult.created_by = loopData.created_by.name
  410. setRunResult(_runResult)
  411. },
  412. onLoopStart: (params) => {
  413. const newLoopRunResult = produce(_loopResult, (draft) => {
  414. draft.push({
  415. ...params.data,
  416. status: NodeRunningStatus.Running,
  417. })
  418. })
  419. _loopResult = newLoopRunResult
  420. setLoopRunResult(newLoopRunResult)
  421. },
  422. onLoopNext: () => {
  423. // loop next trigger time is triggered one more time than loopTimes
  424. if (_loopResult.length >= loopTimes!)
  425. return _loopResult.length >= loopTimes!
  426. },
  427. onLoopFinish: (params) => {
  428. _runResult = params.data
  429. setRunResult(_runResult)
  430. const loopRunResult = _loopResult
  431. const currentIndex = loopRunResult.findIndex(trace => trace.id === params.data.id)
  432. const newLoopRunResult = produce(loopRunResult, (draft) => {
  433. if (currentIndex > -1) {
  434. draft[currentIndex] = {
  435. ...draft[currentIndex],
  436. ...data,
  437. }
  438. }
  439. })
  440. _loopResult = newLoopRunResult
  441. setLoopRunResult(newLoopRunResult)
  442. },
  443. onNodeStarted: (params) => {
  444. const newLoopRunResult = produce(_loopResult, (draft) => {
  445. draft.push({
  446. ...params.data,
  447. status: NodeRunningStatus.Running,
  448. })
  449. })
  450. _loopResult = newLoopRunResult
  451. setLoopRunResult(newLoopRunResult)
  452. },
  453. onNodeFinished: (params) => {
  454. const loopRunResult = _loopResult
  455. const { data } = params
  456. const currentIndex = loopRunResult.findIndex(trace => trace.id === data.id)
  457. const newLoopRunResult = produce(loopRunResult, (draft) => {
  458. if (currentIndex > -1) {
  459. draft[currentIndex] = {
  460. ...draft[currentIndex],
  461. ...data,
  462. }
  463. }
  464. })
  465. _loopResult = newLoopRunResult
  466. setLoopRunResult(newLoopRunResult)
  467. },
  468. onNodeRetry: (params) => {
  469. const newLoopRunResult = produce(_loopResult, (draft) => {
  470. draft.push(params.data)
  471. })
  472. _loopResult = newLoopRunResult
  473. setLoopRunResult(newLoopRunResult)
  474. },
  475. onError: () => {
  476. if (isPausedRef.current)
  477. return
  478. handleNodeDataUpdate({
  479. id,
  480. data: {
  481. ...data,
  482. _isSingleRun: false,
  483. _singleRunningStatus: NodeRunningStatus.Failed,
  484. },
  485. })
  486. },
  487. },
  488. )
  489. }
  490. if (res && res.error)
  491. throw new Error(res.error)
  492. }
  493. catch (e: any) {
  494. console.error(e)
  495. hasError = true
  496. invalidLastRun()
  497. if (!isIteration && !isLoop) {
  498. if (isPausedRef.current)
  499. return
  500. handleNodeDataUpdate({
  501. id,
  502. data: {
  503. ...data,
  504. _isSingleRun: false,
  505. _singleRunningStatus: NodeRunningStatus.Failed,
  506. },
  507. })
  508. return false
  509. }
  510. }
  511. finally {
  512. if (!isPausedRef.current && !isIteration && !isLoop && res) {
  513. setRunResult({
  514. ...res,
  515. total_tokens: res.execution_metadata?.total_tokens || 0,
  516. created_by: res.created_by_account?.name || '',
  517. })
  518. }
  519. }
  520. if (isPausedRef.current)
  521. return
  522. if (!isIteration && !isLoop && !hasError) {
  523. if (isPausedRef.current)
  524. return
  525. handleNodeDataUpdate({
  526. id,
  527. data: {
  528. ...data,
  529. _isSingleRun: false,
  530. _singleRunningStatus: NodeRunningStatus.Succeeded,
  531. },
  532. })
  533. }
  534. }
  535. const handleStop = () => {
  536. handleNodeDataUpdate({
  537. id,
  538. data: {
  539. ...data,
  540. _singleRunningStatus: NodeRunningStatus.NotStart,
  541. },
  542. })
  543. }
  544. const toVarInputs = (variables: Variable[]): InputVar[] => {
  545. if (!variables)
  546. return []
  547. const varInputs = variables.filter(item => !isENV(item.value_selector)).map((item) => {
  548. const originalVar = getVar(item.value_selector)
  549. if (!originalVar) {
  550. return {
  551. label: item.label || item.variable,
  552. variable: item.variable,
  553. type: InputVarType.textInput,
  554. required: true,
  555. value_selector: item.value_selector,
  556. }
  557. }
  558. return {
  559. label: (typeof item.label === 'object' ? item.label.variable : item.label) || item.variable,
  560. variable: item.variable,
  561. type: varTypeToInputVarType(originalVar.type, {
  562. isSelect: !!originalVar.isSelect,
  563. isParagraph: !!originalVar.isParagraph,
  564. }),
  565. required: item.required !== false,
  566. options: originalVar.options,
  567. }
  568. })
  569. return varInputs
  570. }
  571. const getInputVars = (textList: string[]) => {
  572. const valueSelectors: ValueSelector[] = []
  573. textList.forEach((text) => {
  574. valueSelectors.push(...doGetInputVars(text))
  575. })
  576. const variables = unionBy(valueSelectors, item => item.join('.')).map((item) => {
  577. const varInfo = getNodeInfoById(availableNodesIncludeParent, item[0])?.data
  578. return {
  579. label: {
  580. nodeType: varInfo?.type,
  581. nodeName: varInfo?.title || availableNodesIncludeParent[0]?.data.title, // default start node title
  582. variable: isSystemVar(item) ? item.join('.') : item[item.length - 1],
  583. isChatVar: isConversationVar(item),
  584. },
  585. variable: `#${item.join('.')}#`,
  586. value_selector: item,
  587. }
  588. })
  589. const varInputs = toVarInputs(variables)
  590. return varInputs
  591. }
  592. const varSelectorsToVarInputs = (valueSelectors: ValueSelector[] | string[]): InputVar[] => {
  593. return valueSelectors.filter(item => !!item).map((item) => {
  594. return getInputVars([`{{#${typeof item === 'string' ? item : item.join('.')}#}}`])[0]
  595. })
  596. }
  597. return {
  598. appId,
  599. isShowSingleRun,
  600. hideSingleRun,
  601. showSingleRun,
  602. toVarInputs,
  603. varSelectorsToVarInputs,
  604. getInputVars,
  605. runningStatus,
  606. isCompleted,
  607. handleRun,
  608. handleStop,
  609. runInputData,
  610. runInputDataRef,
  611. setRunInputData: handleSetRunInputData,
  612. runResult,
  613. iterationRunResult,
  614. loopRunResult,
  615. setNodeRunning,
  616. checkValid: checkValidWrap,
  617. }
  618. }
  619. export default useOneStepRun