store.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { useContext } from 'react'
  2. import {
  3. useStore as useZustandStore,
  4. } from 'zustand'
  5. import { createStore } from 'zustand/vanilla'
  6. import { debounce } from 'lodash-es'
  7. import type { Viewport } from 'reactflow'
  8. import type {
  9. HelpLineHorizontalPosition,
  10. HelpLineVerticalPosition,
  11. } from './help-line/types'
  12. import type { VariableAssignerNodeType } from './nodes/variable-assigner/types'
  13. import type {
  14. ConversationVariable,
  15. Edge,
  16. EnvironmentVariable,
  17. HistoryWorkflowData,
  18. Node,
  19. RunFile,
  20. ToolWithProvider,
  21. WorkflowRunningData,
  22. } from './types'
  23. import { WorkflowContext } from './context'
  24. import type { NodeTracing } from '@/types/workflow'
  25. // #TODO chatVar#
  26. // const MOCK_DATA = [
  27. // {
  28. // id: 'fjlaksdjflkjg-dfjlajfl0dnfkafjk-djfdkafj-djfak',
  29. // name: 'chat_history',
  30. // value_type: 'array[message]',
  31. // value: [],
  32. // description: 'The chat history of the conversation',
  33. // },
  34. // {
  35. // id: 'fljdaklfjl-dfjlafj0-dklajglje-eknglh',
  36. // name: 'order_id',
  37. // value: '123456',
  38. // value_type: 'string',
  39. // description: '',
  40. // },
  41. // ]
  42. type PreviewRunningData = WorkflowRunningData & {
  43. resultTabActive?: boolean
  44. resultText?: string
  45. }
  46. type Shape = {
  47. appId: string
  48. panelWidth: number
  49. showSingleRunPanel: boolean
  50. setShowSingleRunPanel: (showSingleRunPanel: boolean) => void
  51. workflowRunningData?: PreviewRunningData
  52. setWorkflowRunningData: (workflowData: PreviewRunningData) => void
  53. historyWorkflowData?: HistoryWorkflowData
  54. setHistoryWorkflowData: (historyWorkflowData?: HistoryWorkflowData) => void
  55. showRunHistory: boolean
  56. setShowRunHistory: (showRunHistory: boolean) => void
  57. showFeaturesPanel: boolean
  58. setShowFeaturesPanel: (showFeaturesPanel: boolean) => void
  59. helpLineHorizontal?: HelpLineHorizontalPosition
  60. setHelpLineHorizontal: (helpLineHorizontal?: HelpLineHorizontalPosition) => void
  61. helpLineVertical?: HelpLineVerticalPosition
  62. setHelpLineVertical: (helpLineVertical?: HelpLineVerticalPosition) => void
  63. draftUpdatedAt: number
  64. setDraftUpdatedAt: (draftUpdatedAt: number) => void
  65. publishedAt: number
  66. setPublishedAt: (publishedAt: number) => void
  67. showInputsPanel: boolean
  68. setShowInputsPanel: (showInputsPanel: boolean) => void
  69. inputs: Record<string, string>
  70. setInputs: (inputs: Record<string, string>) => void
  71. toolPublished: boolean
  72. setToolPublished: (toolPublished: boolean) => void
  73. files: RunFile[]
  74. setFiles: (files: RunFile[]) => void
  75. backupDraft?: {
  76. nodes: Node[]
  77. edges: Edge[]
  78. viewport: Viewport
  79. features: Record<string, any>
  80. environmentVariables: EnvironmentVariable[]
  81. }
  82. setBackupDraft: (backupDraft?: Shape['backupDraft']) => void
  83. notInitialWorkflow: boolean
  84. setNotInitialWorkflow: (notInitialWorkflow: boolean) => void
  85. nodesDefaultConfigs: Record<string, any>
  86. setNodesDefaultConfigs: (nodesDefaultConfigs: Record<string, any>) => void
  87. nodeAnimation: boolean
  88. setNodeAnimation: (nodeAnimation: boolean) => void
  89. isRestoring: boolean
  90. setIsRestoring: (isRestoring: boolean) => void
  91. debouncedSyncWorkflowDraft: (fn: () => void) => void
  92. buildInTools: ToolWithProvider[]
  93. setBuildInTools: (tools: ToolWithProvider[]) => void
  94. customTools: ToolWithProvider[]
  95. setCustomTools: (tools: ToolWithProvider[]) => void
  96. workflowTools: ToolWithProvider[]
  97. setWorkflowTools: (tools: ToolWithProvider[]) => void
  98. clipboardElements: Node[]
  99. setClipboardElements: (clipboardElements: Node[]) => void
  100. showDebugAndPreviewPanel: boolean
  101. setShowDebugAndPreviewPanel: (showDebugAndPreviewPanel: boolean) => void
  102. showEnvPanel: boolean
  103. setShowEnvPanel: (showEnvPanel: boolean) => void
  104. environmentVariables: EnvironmentVariable[]
  105. setEnvironmentVariables: (environmentVariables: EnvironmentVariable[]) => void
  106. envSecrets: Record<string, string>
  107. setEnvSecrets: (envSecrets: Record<string, string>) => void
  108. showChatVariablePanel: boolean
  109. setShowChatVariablePanel: (showChatVariablePanel: boolean) => void
  110. showGlobalVariablePanel: boolean
  111. setShowGlobalVariablePanel: (showGlobalVariablePanel: boolean) => void
  112. conversationVariables: ConversationVariable[]
  113. setConversationVariables: (conversationVariables: ConversationVariable[]) => void
  114. selection: null | { x1: number; y1: number; x2: number; y2: number }
  115. setSelection: (selection: Shape['selection']) => void
  116. bundleNodeSize: { width: number; height: number } | null
  117. setBundleNodeSize: (bundleNodeSize: Shape['bundleNodeSize']) => void
  118. controlMode: 'pointer' | 'hand'
  119. setControlMode: (controlMode: Shape['controlMode']) => void
  120. candidateNode?: Node
  121. setCandidateNode: (candidateNode?: Node) => void
  122. panelMenu?: {
  123. top: number
  124. left: number
  125. }
  126. setPanelMenu: (panelMenu: Shape['panelMenu']) => void
  127. nodeMenu?: {
  128. top: number
  129. left: number
  130. nodeId: string
  131. }
  132. setNodeMenu: (nodeMenu: Shape['nodeMenu']) => void
  133. mousePosition: { pageX: number; pageY: number; elementX: number; elementY: number }
  134. setMousePosition: (mousePosition: Shape['mousePosition']) => void
  135. syncWorkflowDraftHash: string
  136. setSyncWorkflowDraftHash: (hash: string) => void
  137. showConfirm?: { title: string; desc?: string; onConfirm: () => void }
  138. setShowConfirm: (showConfirm: Shape['showConfirm']) => void
  139. showAssignVariablePopup?: {
  140. nodeId: string
  141. nodeData: Node['data']
  142. variableAssignerNodeId: string
  143. variableAssignerNodeData: VariableAssignerNodeType
  144. variableAssignerNodeHandleId: string
  145. parentNode?: Node
  146. x: number
  147. y: number
  148. }
  149. setShowAssignVariablePopup: (showAssignVariablePopup: Shape['showAssignVariablePopup']) => void
  150. hoveringAssignVariableGroupId?: string
  151. setHoveringAssignVariableGroupId: (hoveringAssignVariableGroupId?: string) => void
  152. connectingNodePayload?: { nodeId: string; nodeType: string; handleType: string; handleId: string | null }
  153. setConnectingNodePayload: (startConnectingPayload?: Shape['connectingNodePayload']) => void
  154. enteringNodePayload?: {
  155. nodeId: string
  156. nodeData: VariableAssignerNodeType
  157. }
  158. setEnteringNodePayload: (enteringNodePayload?: Shape['enteringNodePayload']) => void
  159. isSyncingWorkflowDraft: boolean
  160. setIsSyncingWorkflowDraft: (isSyncingWorkflowDraft: boolean) => void
  161. controlPromptEditorRerenderKey: number
  162. setControlPromptEditorRerenderKey: (controlPromptEditorRerenderKey: number) => void
  163. showImportDSLModal: boolean
  164. setShowImportDSLModal: (showImportDSLModal: boolean) => void
  165. showTips: string
  166. setShowTips: (showTips: string) => void
  167. iterTimes: number
  168. setIterTimes: (iterTimes: number) => void
  169. iterParallelLogMap: Map<string, Map<string, NodeTracing[]>>
  170. setIterParallelLogMap: (iterParallelLogMap: Map<string, Map<string, NodeTracing[]>>) => void
  171. }
  172. export const createWorkflowStore = () => {
  173. const hideAllPanel = {
  174. showDebugAndPreviewPanel: false,
  175. showEnvPanel: false,
  176. showChatVariablePanel: false,
  177. showGlobalVariablePanel: false,
  178. }
  179. return createStore<Shape>(set => ({
  180. appId: '',
  181. panelWidth: localStorage.getItem('workflow-node-panel-width') ? parseFloat(localStorage.getItem('workflow-node-panel-width')!) : 420,
  182. showSingleRunPanel: false,
  183. setShowSingleRunPanel: showSingleRunPanel => set(() => ({ showSingleRunPanel })),
  184. workflowRunningData: undefined,
  185. setWorkflowRunningData: workflowRunningData => set(() => ({ workflowRunningData })),
  186. historyWorkflowData: undefined,
  187. setHistoryWorkflowData: historyWorkflowData => set(() => ({ historyWorkflowData })),
  188. showRunHistory: false,
  189. setShowRunHistory: showRunHistory => set(() => ({ showRunHistory })),
  190. showFeaturesPanel: false,
  191. setShowFeaturesPanel: showFeaturesPanel => set(() => ({ showFeaturesPanel })),
  192. helpLineHorizontal: undefined,
  193. setHelpLineHorizontal: helpLineHorizontal => set(() => ({ helpLineHorizontal })),
  194. helpLineVertical: undefined,
  195. setHelpLineVertical: helpLineVertical => set(() => ({ helpLineVertical })),
  196. draftUpdatedAt: 0,
  197. setDraftUpdatedAt: draftUpdatedAt => set(() => ({ draftUpdatedAt: draftUpdatedAt ? draftUpdatedAt * 1000 : 0 })),
  198. publishedAt: 0,
  199. setPublishedAt: publishedAt => set(() => ({ publishedAt: publishedAt ? publishedAt * 1000 : 0 })),
  200. showInputsPanel: false,
  201. setShowInputsPanel: showInputsPanel => set(() => ({ showInputsPanel })),
  202. inputs: {},
  203. setInputs: inputs => set(() => ({ inputs })),
  204. toolPublished: false,
  205. setToolPublished: toolPublished => set(() => ({ toolPublished })),
  206. files: [],
  207. setFiles: files => set(() => ({ files })),
  208. backupDraft: undefined,
  209. setBackupDraft: backupDraft => set(() => ({ backupDraft })),
  210. notInitialWorkflow: false,
  211. setNotInitialWorkflow: notInitialWorkflow => set(() => ({ notInitialWorkflow })),
  212. nodesDefaultConfigs: {},
  213. setNodesDefaultConfigs: nodesDefaultConfigs => set(() => ({ nodesDefaultConfigs })),
  214. nodeAnimation: false,
  215. setNodeAnimation: nodeAnimation => set(() => ({ nodeAnimation })),
  216. isRestoring: false,
  217. setIsRestoring: isRestoring => set(() => ({ isRestoring })),
  218. debouncedSyncWorkflowDraft: debounce((syncWorkflowDraft) => {
  219. syncWorkflowDraft()
  220. }, 5000),
  221. buildInTools: [],
  222. setBuildInTools: buildInTools => set(() => ({ buildInTools })),
  223. customTools: [],
  224. setCustomTools: customTools => set(() => ({ customTools })),
  225. workflowTools: [],
  226. setWorkflowTools: workflowTools => set(() => ({ workflowTools })),
  227. clipboardElements: [],
  228. setClipboardElements: clipboardElements => set(() => ({ clipboardElements })),
  229. showDebugAndPreviewPanel: false,
  230. setShowDebugAndPreviewPanel: showDebugAndPreviewPanel => set(() => ({ showDebugAndPreviewPanel })),
  231. showEnvPanel: false,
  232. setShowEnvPanel: showEnvPanel => set(() => ({ showEnvPanel })),
  233. environmentVariables: [],
  234. setEnvironmentVariables: environmentVariables => set(() => ({ environmentVariables })),
  235. envSecrets: {},
  236. setEnvSecrets: envSecrets => set(() => ({ envSecrets })),
  237. showChatVariablePanel: false,
  238. setShowChatVariablePanel: showChatVariablePanel => set(() => ({ showChatVariablePanel })),
  239. showGlobalVariablePanel: false,
  240. setShowGlobalVariablePanel: showGlobalVariablePanel => set(() => {
  241. if (showGlobalVariablePanel)
  242. return { ...hideAllPanel, showGlobalVariablePanel: true }
  243. else
  244. return { showGlobalVariablePanel: false }
  245. }),
  246. conversationVariables: [],
  247. setConversationVariables: conversationVariables => set(() => ({ conversationVariables })),
  248. selection: null,
  249. setSelection: selection => set(() => ({ selection })),
  250. bundleNodeSize: null,
  251. setBundleNodeSize: bundleNodeSize => set(() => ({ bundleNodeSize })),
  252. controlMode: localStorage.getItem('workflow-operation-mode') === 'pointer' ? 'pointer' : 'hand',
  253. setControlMode: (controlMode) => {
  254. set(() => ({ controlMode }))
  255. localStorage.setItem('workflow-operation-mode', controlMode)
  256. },
  257. candidateNode: undefined,
  258. setCandidateNode: candidateNode => set(() => ({ candidateNode })),
  259. panelMenu: undefined,
  260. setPanelMenu: panelMenu => set(() => ({ panelMenu })),
  261. nodeMenu: undefined,
  262. setNodeMenu: nodeMenu => set(() => ({ nodeMenu })),
  263. mousePosition: { pageX: 0, pageY: 0, elementX: 0, elementY: 0 },
  264. setMousePosition: mousePosition => set(() => ({ mousePosition })),
  265. syncWorkflowDraftHash: '',
  266. setSyncWorkflowDraftHash: syncWorkflowDraftHash => set(() => ({ syncWorkflowDraftHash })),
  267. showConfirm: undefined,
  268. setShowConfirm: showConfirm => set(() => ({ showConfirm })),
  269. showAssignVariablePopup: undefined,
  270. setShowAssignVariablePopup: showAssignVariablePopup => set(() => ({ showAssignVariablePopup })),
  271. hoveringAssignVariableGroupId: undefined,
  272. setHoveringAssignVariableGroupId: hoveringAssignVariableGroupId => set(() => ({ hoveringAssignVariableGroupId })),
  273. connectingNodePayload: undefined,
  274. setConnectingNodePayload: connectingNodePayload => set(() => ({ connectingNodePayload })),
  275. enteringNodePayload: undefined,
  276. setEnteringNodePayload: enteringNodePayload => set(() => ({ enteringNodePayload })),
  277. isSyncingWorkflowDraft: false,
  278. setIsSyncingWorkflowDraft: isSyncingWorkflowDraft => set(() => ({ isSyncingWorkflowDraft })),
  279. controlPromptEditorRerenderKey: 0,
  280. setControlPromptEditorRerenderKey: controlPromptEditorRerenderKey => set(() => ({ controlPromptEditorRerenderKey })),
  281. showImportDSLModal: false,
  282. setShowImportDSLModal: showImportDSLModal => set(() => ({ showImportDSLModal })),
  283. showTips: '',
  284. setShowTips: showTips => set(() => ({ showTips })),
  285. iterTimes: 1,
  286. setIterTimes: iterTimes => set(() => ({ iterTimes })),
  287. iterParallelLogMap: new Map<string, Map<string, NodeTracing[]>>(),
  288. setIterParallelLogMap: iterParallelLogMap => set(() => ({ iterParallelLogMap })),
  289. }))
  290. }
  291. export function useStore<T>(selector: (state: Shape) => T): T {
  292. const store = useContext(WorkflowContext)
  293. if (!store)
  294. throw new Error('Missing WorkflowContext.Provider in the tree')
  295. return useZustandStore(store, selector)
  296. }
  297. export const useWorkflowStore = () => {
  298. return useContext(WorkflowContext)!
  299. }