index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="workflow">
  3. <TeleportContainer />
  4. <div class="workflow-chart">
  5. <workflowChart
  6. :ID="ID"
  7. :data="state.workflowData"
  8. ref="ref_workflow"
  9. @save="onSave"
  10. />
  11. </div>
  12. <div class="operations">
  13. <div>
  14. <div
  15. class="__hover-bg"
  16. @click="WorkflowStore.$patch((s: any) => (s.envVars.show = true))"
  17. >
  18. <SvgIcon name="env" />
  19. </div>
  20. </div>
  21. <a-button type="primary" @click="onSave">保存配置</a-button>
  22. </div>
  23. <div class="panel">
  24. <workflowPanel />
  25. <envVarsPanel />
  26. </div>
  27. </div>
  28. </template>
  29. <script setup lang="ts">
  30. import {
  31. getCurrentInstance,
  32. onMounted,
  33. provide,
  34. reactive,
  35. ref,
  36. watch,
  37. } from 'vue'
  38. import workflowChart from './chart/index.vue'
  39. import workflowPanel from './chart/panel-index.vue'
  40. import envVarsPanel from './instance/component/vars/evn-index.vue'
  41. import { getTeleport } from '@antv/x6-vue-shape'
  42. import { useAppStore, useDictionaryStore, useWorkflowStore } from '@/stores'
  43. import {
  44. workflowDraftDetail,
  45. workflowDraftSave,
  46. } from '@/api/modules/workflow/chart'
  47. import { ElLoading, ElMessage } from 'element-plus'
  48. import { debounce } from 'lodash'
  49. import { handleNodeSubmit } from '@/views/workflow/handle'
  50. const DictionaryStore = useDictionaryStore()
  51. const AppStore = useAppStore()
  52. const TeleportContainer = getTeleport()
  53. const WorkflowStore = useWorkflowStore()
  54. const emit = defineEmits(['autoSave'])
  55. const props = defineProps({
  56. ID: {},
  57. })
  58. const { proxy }: any = getCurrentInstance()
  59. const state: any = reactive({
  60. workflowData: null,
  61. uniqueHash: '',
  62. })
  63. const ref_workflow = ref()
  64. const autoSave = debounce(() => {
  65. onSave()
  66. }, 5000)
  67. watch(
  68. () => WorkflowStore.autoSaveFlag,
  69. () => {
  70. autoSave()
  71. },
  72. )
  73. const onSave = () => {
  74. const data = ref_workflow.value.toJSON()
  75. const offset = WorkflowStore.graph.translate()
  76. const res: any = {
  77. conversationVariables: [],
  78. environmentVariables: WorkflowStore.envVars.vars,
  79. hash: state.uniqueHash,
  80. graph: {
  81. nodes: [],
  82. edges: [],
  83. viewport: {
  84. zoom: WorkflowStore.graph.zoom(),
  85. x: offset.tx,
  86. y: offset.ty,
  87. },
  88. },
  89. }
  90. JSON.parse(JSON.stringify(data.cells)).forEach((cell: any) => {
  91. if (cell.shape === 'workflow-node') {
  92. const node: any = {
  93. id: cell.id,
  94. type: cell.data.workflowData.type,
  95. position: cell.position,
  96. data: handleNodeSubmit(cell.data.workflowData),
  97. }
  98. delete node.data.edgeSource
  99. delete node.data.inVars
  100. res.graph.nodes.push(node)
  101. } else if (cell.shape === 'edge') {
  102. const edge: any = {
  103. target: cell.target.cell,
  104. source: cell.source.cell,
  105. }
  106. if (!cell.source.port.includes('end')) {
  107. edge.sourceHandle = cell.source.port
  108. }
  109. res.graph.edges.push(edge)
  110. }
  111. })
  112. const loading = ElLoading.service({
  113. text: '自动保存中……',
  114. background: 'rgba(0, 0,0, 0.3)',
  115. })
  116. workflowDraftSave(props.ID, res)
  117. .then(({ data }: any) => {
  118. loading.close()
  119. state.uniqueHash = data.uniqueHash
  120. emit('autoSave', data.updateTime)
  121. })
  122. .catch(() => {})
  123. .finally(() => {})
  124. }
  125. const initData = () => {
  126. workflowDraftDetail(props.ID)
  127. .then(({ data }: any) => {
  128. emit('autoSave', data.updateTime)
  129. state.uniqueHash = data.uniqueHash
  130. WorkflowStore.$patch(
  131. (s: any) => (s.envVars.vars = data.environmentVariables || []),
  132. )
  133. if (data.graph.viewport) {
  134. const graph = {
  135. viewport: data.graph.viewport,
  136. nodes: formatNodes(data.graph.nodes),
  137. edges: formatEdges(data.graph.edges),
  138. }
  139. state.workflowData = graph
  140. } else {
  141. state.workflowData = {
  142. nodes: [],
  143. edges: [],
  144. }
  145. }
  146. // state.workflowData = {
  147. // nodes: [],
  148. // edges: [],
  149. // }
  150. })
  151. .catch(() => {})
  152. .finally(() => {})
  153. // const d = data0
  154. // WorkflowStore.$patch((s: any) => (s.envVars.vars = d.envVars || []))
  155. // state.workflowData = d.graph
  156. }
  157. const formatNodes = (arr) => {
  158. return arr.map((node: any) => {
  159. const obj = {
  160. id: node.id,
  161. x: node.position.x,
  162. y: node.position.y,
  163. data: node.data,
  164. }
  165. return obj
  166. })
  167. }
  168. const formatEdges = (arr) => {
  169. return arr.map((edge: any) => {
  170. const obj = {
  171. source: edge.source,
  172. target: edge.target,
  173. port: edge.sourceHandle || null,
  174. }
  175. return obj
  176. })
  177. }
  178. onMounted(() => {
  179. initData()
  180. initDictionary()
  181. })
  182. const initDictionary = () => {
  183. DictionaryStore.initKnowledges(AppStore.tenantInfo?.id)
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .workflow {
  188. width: 100%;
  189. height: 100%;
  190. display: flex;
  191. align-items: center;
  192. justify-content: center;
  193. position: relative;
  194. position: relative;
  195. .workflow-chart {
  196. width: 100%;
  197. height: 100%;
  198. z-index: 1;
  199. }
  200. .operations {
  201. position: absolute;
  202. top: 10px;
  203. right: 10px;
  204. z-index: 2;
  205. display: flex;
  206. align-items: center;
  207. gap: 10px;
  208. > div {
  209. display: flex;
  210. align-items: center;
  211. background-color: rgba(255, 255, 255, 0.95);
  212. box-shadow: 0 0px 8px rgba(0, 0, 0, 0.1);
  213. border-radius: 6px;
  214. padding: 4px;
  215. > div {
  216. min-width: 24px;
  217. height: 24px;
  218. display: flex;
  219. align-items: center;
  220. justify-content: center;
  221. }
  222. }
  223. }
  224. .panel {
  225. z-index: 2;
  226. position: absolute;
  227. right: 0;
  228. bottom: 0;
  229. display: flex;
  230. gap: 10px;
  231. height: calc(100% - 10px - 32px - 10px);
  232. }
  233. }
  234. </style>