Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

use-fetch-workflow-inspect-vars.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import type { NodeWithVar, VarInInspect } from '@/types/workflow'
  2. import { useStore, useWorkflowStore } from '@/app/components/workflow/store'
  3. import { useStoreApi } from 'reactflow'
  4. import type { ToolWithProvider } from '@/app/components/workflow/types'
  5. import type { Node } from '@/app/components/workflow/types'
  6. import { fetchAllInspectVars } from '@/service/workflow'
  7. import { useInvalidateConversationVarValues, useInvalidateSysVarValues } from '@/service/use-workflow'
  8. import { useNodesInteractionsWithoutSync } from '@/app/components/workflow/hooks/use-nodes-interactions-without-sync'
  9. import type { FlowType } from '@/types/common'
  10. import useMatchSchemaType, { getMatchedSchemaType } from '../nodes/_base/components/variable/use-match-schema-type'
  11. import { toNodeOutputVars } from '../nodes/_base/components/variable/utils'
  12. import type { SchemaTypeDefinition } from '@/service/use-common'
  13. import { useCallback } from 'react'
  14. type Params = {
  15. flowType: FlowType
  16. flowId: string
  17. }
  18. export const useSetWorkflowVarsWithValue = ({
  19. flowType,
  20. flowId,
  21. }: Params) => {
  22. const workflowStore = useWorkflowStore()
  23. const store = useStoreApi()
  24. const invalidateConversationVarValues = useInvalidateConversationVarValues(flowType, flowId)
  25. const invalidateSysVarValues = useInvalidateSysVarValues(flowType, flowId)
  26. const { handleCancelAllNodeSuccessStatus } = useNodesInteractionsWithoutSync()
  27. const { schemaTypeDefinitions } = useMatchSchemaType()
  28. const buildInTools = useStore(s => s.buildInTools)
  29. const customTools = useStore(s => s.customTools)
  30. const workflowTools = useStore(s => s.workflowTools)
  31. const mcpTools = useStore(s => s.mcpTools)
  32. const dataSourceList = useStore(s => s.dataSourceList)
  33. const allPluginInfoList = {
  34. buildInTools,
  35. customTools,
  36. workflowTools,
  37. mcpTools,
  38. dataSourceList: dataSourceList ?? [],
  39. }
  40. const setInspectVarsToStore = (inspectVars: VarInInspect[], passedInAllPluginInfoList?: Record<string, ToolWithProvider[]>, passedInSchemaTypeDefinitions?: SchemaTypeDefinition[]) => {
  41. const { setNodesWithInspectVars } = workflowStore.getState()
  42. const { getNodes } = store.getState()
  43. const nodeArr = getNodes()
  44. const allNodesOutputVars = toNodeOutputVars(nodeArr, false, () => true, [], [], [], passedInAllPluginInfoList || allPluginInfoList, passedInSchemaTypeDefinitions || schemaTypeDefinitions)
  45. const nodesKeyValue: Record<string, Node> = {}
  46. nodeArr.forEach((node) => {
  47. nodesKeyValue[node.id] = node
  48. })
  49. const withValueNodeIds: Record<string, boolean> = {}
  50. inspectVars.forEach((varItem) => {
  51. const nodeId = varItem.selector[0]
  52. const node = nodesKeyValue[nodeId]
  53. if (!node)
  54. return
  55. withValueNodeIds[nodeId] = true
  56. })
  57. const withValueNodes = Object.keys(withValueNodeIds).map((nodeId) => {
  58. return nodesKeyValue[nodeId]
  59. })
  60. const res: NodeWithVar[] = withValueNodes.map((node) => {
  61. const nodeId = node.id
  62. const varsUnderTheNode = inspectVars.filter((varItem) => {
  63. return varItem.selector[0] === nodeId
  64. })
  65. const nodeVar = allNodesOutputVars.find(item => item.nodeId === nodeId)
  66. const nodeWithVar = {
  67. nodeId,
  68. nodePayload: node.data,
  69. nodeType: node.data.type,
  70. title: node.data.title,
  71. vars: varsUnderTheNode.map((item) => {
  72. const schemaType = nodeVar ? nodeVar.vars.find(v => v.variable === item.name)?.schemaType : ''
  73. return {
  74. ...item,
  75. schemaType,
  76. }
  77. }),
  78. isSingRunRunning: false,
  79. isValueFetched: false,
  80. }
  81. return nodeWithVar
  82. })
  83. setNodesWithInspectVars(res)
  84. }
  85. const fetchInspectVars = useCallback(async (params: {
  86. passInVars?: boolean,
  87. vars?: VarInInspect[],
  88. passedInAllPluginInfoList?: Record<string, ToolWithProvider[]>,
  89. passedInSchemaTypeDefinitions?: SchemaTypeDefinition[]
  90. }) => {
  91. const { passInVars, vars, passedInAllPluginInfoList, passedInSchemaTypeDefinitions } = params
  92. invalidateConversationVarValues()
  93. invalidateSysVarValues()
  94. const data = passInVars ? vars! : await fetchAllInspectVars(flowType, flowId)
  95. setInspectVarsToStore(data, passedInAllPluginInfoList, passedInSchemaTypeDefinitions)
  96. handleCancelAllNodeSuccessStatus() // to make sure clear node output show the unset status
  97. }, [invalidateConversationVarValues, invalidateSysVarValues, flowType, flowId, setInspectVarsToStore, handleCancelAllNodeSuccessStatus, schemaTypeDefinitions, getMatchedSchemaType])
  98. return {
  99. fetchInspectVars,
  100. }
  101. }