| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <!DOCTYPE html>
- <html lang="zh">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>龙虾引擎 - 工作流可视化编辑器</title>
- <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
- <script src="https://unpkg.com/@vue-flow/core@latest/dist/vue-flow.umd.js"></script>
- <script src="https://unpkg.com/@vue-flow/background@latest/dist/vue-flow-background.umd.js"></script>
- <script src="https://unpkg.com/@vue-flow/minimap@latest/dist/vue-flow-minimap.umd.js"></script>
- <script src="https://unpkg.com/@vue-flow/controls@latest/dist/vue-flow-controls.umd.js"></script>
- <link href="https://unpkg.com/@vue-flow/core@latest/dist/style.css" rel="stylesheet">
- <link href="https://unpkg.com/@vue-flow/minimap@latest/dist/style.css" rel="stylesheet">
- <link href="https://unpkg.com/@vue-flow/controls@latest/dist/style.css" rel="stylesheet">
- <style>
- *{margin:0;padding:0;box-sizing:border-box}
- body{font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;height:100vh;overflow:hidden}
- #app{display:flex;height:100vh}
- .sidebar{width:220px;background:#1a1a2e;color:#fff;padding:12px;overflow-y:auto;flex-shrink:0}
- .sidebar h3{font-size:14px;margin:8px 0;color:#e94560;border-bottom:1px solid #333;padding-bottom:4px}
- .node-item{background:#16213e;margin:4px 0;padding:8px;border-radius:6px;cursor:grab;font-size:12px;border:1px solid #0f3460;transition:all .2s}
- .node-item:hover{background:#0f3460;border-color:#e94560;transform:translateX(2px)}
- .node-item .type-tag{display:inline-block;background:#e94560;color:#fff;padding:1px 6px;border-radius:3px;font-size:10px;margin-right:6px}
- .main{flex:1;position:relative}
- .toolbar{position:absolute;top:10px;left:10px;z-index:10;display:flex;gap:8px}
- .toolbar button{padding:6px 14px;border:none;border-radius:4px;cursor:pointer;font-size:12px}
- .btn-primary{background:#e94560;color:#fff}
- .btn-secondary{background:#0f3460;color:#fff}
- .btn-success{background:#22c55e;color:#fff}
- .prop-panel{width:320px;background:#f8f9fa;border-left:2px solid #dee2e6;padding:12px;overflow-y:auto;flex-shrink:0;font-size:13px}
- .prop-panel h3{color:#1a1a2e;margin-bottom:10px}
- .prop-panel label{display:block;margin:6px 0 2px;color:#555;font-weight:600}
- .prop-panel input,.prop-panel textarea,.prop-panel select{width:100%;padding:6px;border:1px solid #ccc;border-radius:4px;font-size:12px;margin-bottom:6px}
- .prop-panel textarea{min-height:60px;font-family:monospace}
- .vue-flow__node{font-size:12px;min-width:120px;text-align:center}
- .flow-node{padding:8px 12px;border-radius:8px;border:2px solid;background:#fff;box-shadow:0 2px 6px rgba(0,0,0,.1)}
- .flow-node.start{border-color:#22c55e}
- .flow-node.ai{border-color:#8b5cf6}
- .flow-node.msg{border-color:#3b82f6}
- .flow-node.cond{border-color:#f59e0b}
- .flow-node.end{border-color:#ef4444}
- .flow-node.default{border-color:#6b7280}
- .node-name{font-weight:700}
- .node-desc{font-size:10px;color:#888;margin-top:2px}
- .modal-overlay{position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,.5);display:flex;align-items:center;justify-content:center;z-index:1000}
- .modal{background:#fff;border-radius:12px;padding:24px;width:500px;max-height:80vh;overflow-y:auto}
- .modal h4{margin-bottom:12px}
- .modal textarea{width:100%;min-height:120px;margin:8px 0;font-family:monospace;font-size:12px;padding:8px}
- .modal .actions{display:flex;gap:8px;justify-content:flex-end;margin-top:12px}
- </style>
- </head>
- <body>
- <div id="app">
- <div class="sidebar">
- <h3>🦞 龙虾引擎</h3>
- <div v-for="(group,gname) in nodeGroups" :key="gname">
- <h3>{{gname}}</h3>
- <div class="node-item" v-for="nt in group" :key="nt.type"
- @dragstart="onDragStart($event,nt)" draggable="true">
- <span class="type-tag">{{nt.type}}</span>{{nt.name}}
- </div>
- </div>
- <div style="margin-top:12px;border-top:1px solid #333;padding-top:8px">
- <button class="btn-primary" style="width:100%;padding:8px;margin:4px 0" @click="showGenerate=true">🤖 AI生成工作流</button>
- <button class="btn-secondary" style="width:100%;padding:8px;margin:4px 0" @click="exportJson">📋 导出JSON</button>
- <button class="btn-success" style="width:100%;padding:8px;margin:4px 0" @click="saveWorkflow">💾 保存到服务器</button>
- <button class="btn-secondary" style="width:100%;padding:8px;margin:4px 0" @click="showLoad=true">📂 加载工作流</button>
- </div>
- </div>
- <div class="main" @drop="onDrop" @dragover.prevent>
- <div class="toolbar">
- <button class="btn-primary" @click="fitView()">🎯 适应视图</button>
- <button class="btn-secondary" @click="undo()">↩ 撤销</button>
- </div>
- <vue-flow ref="flowRef" v-model="elements" :node-types="nodeTypes"
- :default-edge-options="{animated:true,style:{stroke:'#64748b',strokeWidth:2}}"
- @node-click="onNodeClick" @connect="onConnect" style="height:100%;background:#f1f5f9">
- <template #node-start="props"><div class="flow-node start"><div class="node-name">🚀 {{props.data.label}}</div></div></template>
- <template #node-ai="props"><div class="flow-node ai"><div class="node-name">🧠 {{props.data.label}}</div></div></template>
- <template #node-msg="props"><div class="flow-node msg"><div class="node-name">💬 {{props.data.label}}</div></div></template>
- <template #node-cond="props"><div class="flow-node cond"><div class="node-name">🔀 {{props.data.label}}</div></div></template>
- <template #node-end="props"><div class="flow-node end"><div class="node-name">🏁 {{props.data.label}}</div></div></template>
- <template #node-default="props"><div class="flow-node default"><div class="node-name">📌 {{props.data.label}}</div></div></template>
- <vue-flow-background />
- <vue-flow-minimap />
- <vue-flow-controls />
- </vue-flow>
- </div>
- <div class="prop-panel" v-if="selectedNode">
- <h3>节点属性</h3>
- <label>节点编码</label><input v-model="selectedNode.data.nodeCode">
- <label>节点名称</label><input v-model="selectedNode.data.label">
- <label>节点类型</label>
- <select v-model.number="selectedNode.data.nodeType" @change="onTypeChange">
- <option v-for="nt in allNodes" :value="nt.type" :key="nt.type">{{nt.type}}-{{nt.name}}</option>
- </select>
- <label>下一节点</label><input v-model="selectedNode.data.nextNodeCode" placeholder="next_node_code">
- <label>话术模板</label><textarea v-model="selectedNode.data.messageTemplate" placeholder="支持 ${变量}"></textarea>
- <label>条件表达式</label><textarea v-model="selectedNode.data.conditionExpr" placeholder='{"field":"intent","op":"eq","value":"purchase"}'></textarea>
- <label>节点配置(JSON)</label><textarea v-model="selectedNode.data.nodeConfig" placeholder='{"collectFields":["1","2"]}'></textarea>
- <label>最大轮次</label><input v-model.number="selectedNode.data.maxRounds" type="number" min="0">
- <button class="btn-primary" style="width:100%;margin-top:8px;padding:8px" @click="selectedNode=null">关闭</button>
- </div>
- </div>
- <div class="modal-overlay" v-if="showGenerate">
- <div class="modal">
- <h4>🤖 AI生成工作流</h4>
- <label>需求描述</label>
- <textarea v-model="genReq" placeholder="例如:我需要一个旅游行业的客户跟进流程,包括意图识别、信息收集、发送方案、条件判断、创建跟进任务..."></textarea>
- <label>行业类型</label>
- <select v-model="genIndustry">
- <option value="travel">旅游</option><option value="medical">医美</option>
- <option value="education">教育</option><option value="insurance">保险</option>
- <option value="general">通用</option>
- </select>
- <label>已有工作流JSON(可选-迭代优化)</label>
- <textarea v-model="genExisting" placeholder="粘贴已有工作流JSON进行迭代优化...留空则全新生成"></textarea>
- <label>迭代指令(可选)</label>
- <input v-model="genInstruction" placeholder="如:增加关怀节点、优化话术、调整条件">
- <div class="actions">
- <button class="btn-secondary" @click="showGenerate=false">取消</button>
- <button class="btn-primary" @click="aiGenerate()">生成</button>
- </div>
- <div v-if="genResult" style="margin-top:12px;background:#f0fdf4;padding:10px;border-radius:6px;font-size:12px">
- <b>评分:{{genResult.score}}</b> {{genResult.details}}
- </div>
- </div>
- </div>
- <div class="modal-overlay" v-if="showLoad">
- <div class="modal">
- <h4>📂 加载工作流</h4>
- <textarea v-model="loadJson" placeholder="粘贴工作流JSON..."></textarea>
- <div class="actions">
- <button class="btn-secondary" @click="showLoad=false">取消</button>
- <button class="btn-primary" @click="importJson()">加载</button>
- </div>
- </div>
- </div>
- </div>
- <script>
- const {createApp,ref,computed,reactive,onMounted} = Vue
- const nodeGroups = {
- '流程控制':[{type:1,name:'开始',icon:'🚀'},{type:99,name:'结束',icon:'🏁'},{type:4,name:'等待',icon:'⏳'}],
- 'AI智能':[{type:2,name:'AI处理',icon:'🧠'},{type:11,name:'知识检索',icon:'🔍'}],
- '交互触达':[{type:3,name:'发送消息',icon:'💬'},{type:7,name:'信息收集',icon:'📋'},{type:10,name:'HTTP调用',icon:'🌐'}],
- '逻辑控制':[{type:5,name:'条件判断',icon:'🔀'},{type:13,name:'循环迭代',icon:'🔄'},{type:16,name:'变量赋值',icon:'📝'}],
- '业务操作':[{type:6,name:'创建任务',icon:'✅'},{type:8,name:'转人工',icon:'👤'},{type:9,name:'标签操作',icon:'🏷'}],
- '扩展能力':[{type:12,name:'代码执行',icon:'⚡'},{type:14,name:'数据库查询',icon:'🗄'},{type:15,name:'子流程',icon:'📦'}]
- }
- const allNodes = Object.values(nodeGroups).flat()
- const typeToCategory = {}
- allNodes.forEach(n => {
- if (n.type === 1) typeToCategory[n.type] = 'start'
- else if (n.type === 99) typeToCategory[n.type] = 'end'
- else if (n.type === 2 || n.type === 11) typeToCategory[n.type] = 'ai'
- else if (n.type === 3 || n.type === 7 || n.type === 10) typeToCategory[n.type] = 'msg'
- else if (n.type === 5 || n.type === 13 || n.type === 16) typeToCategory[n.type] = 'cond'
- else typeToCategory[n.type] = 'default'
- })
- const app = createApp({
- setup(){
- const flowRef = ref(null)
- const selectedNode = ref(null)
- const elements = ref([
- {id:'1',type:'start',position:{x:300,y:50},data:{label:'开始',nodeCode:'START',nodeType:1,nextNodeCode:'MSG_1',messageTemplate:'',conditionExpr:'',nodeConfig:'{}',maxRounds:0}},
- {id:'2',type:'msg',position:{x:300,y:180},data:{label:'欢迎消息',nodeCode:'MSG_1',nodeType:3,nextNodeCode:'END',messageTemplate:'您好${customerName},欢迎咨询!',conditionExpr:'',nodeConfig:'{}',maxRounds:0}},
- {id:'3',type:'end',position:{x:300,y:310},data:{label:'结束',nodeCode:'END',nodeType:99,nextNodeCode:'',messageTemplate:'',conditionExpr:'',nodeConfig:'{}',maxRounds:0}},
- {id:'e1-2',source:'1',target:'2',animated:true},
- ])
- const showGenerate = ref(false), showLoad = ref(false)
- const genReq = ref(''), genIndustry = ref('travel'), genExisting = ref(''), genInstruction = ref('')
- const genResult = ref(null), loadJson = ref('')
- let nodeIdCounter = elements.value.length
- const nodeTypes = {
- start:{template:'#node-start'}, ai:{template:'#node-ai'}, msg:{template:'#node-msg'},
- cond:{template:'#node-cond'}, end:{template:'#node-end'}, default:{template:'#node-default'}
- }
- function onDragStart(ev,nt){
- ev.dataTransfer.setData('application/json',JSON.stringify(nt))
- ev.dataTransfer.effectAllowed = 'move'
- }
- function onDrop(ev){
- const nt = JSON.parse(ev.dataTransfer.getData('application/json'))
- const pos = flowRef.value?.screenToFlowCoordinate?.({x:ev.clientX,y:ev.clientY}) || {x:ev.offsetX,y:ev.offsetY}
- nodeIdCounter++
- const cat = typeToCategory[nt.type] || 'default'
- elements.value.push({
- id:String(nodeIdCounter), type:cat, position:pos,
- data:{label:nt.name,nodeCode:nt.name.toUpperCase()+'_'+nodeIdCounter,nodeType:nt.type,
- nextNodeCode:'',messageTemplate:'',conditionExpr:'',nodeConfig:'{}',maxRounds:0}
- })
- }
- function onNodeClick({node}){ selectedNode.value = node }
- function onConnect(conn){
- elements.value.push({id:'e'+conn.source+'-'+conn.target,source:conn.source,target:conn.target,animated:true})
- }
- function onTypeChange(){
- if(selectedNode.value){
- selectedNode.value.type = typeToCategory[selectedNode.value.data.nodeType] || 'default'
- }
- }
- function fitView(){ flowRef.value?.fitView() }
- function undo(){ elements.value.pop() }
- function exportJson(){
- const nodes = elements.value.filter(e=>e.type&&e.data).map(e=>({nodeCode:e.data.nodeCode,nodeName:e.data.label,nodeType:e.data.nodeType,sortNo:parseInt(e.id),nextNodeCode:e.data.nextNodeCode,messageTemplate:e.data.messageTemplate||'',conditionExpr:e.data.conditionExpr||'',nodeConfig:e.data.nodeConfig||'{}',maxRounds:e.data.maxRounds||0}))
- const edges = elements.value.filter(e=>e.source&&e.target).map((e,i)=>({edgeKey:'EDGE_'+i,sourceNodeCode:elements.value.find(n=>n.id===e.source)?.data?.nodeCode||'',targetNodeCode:elements.value.find(n=>n.id===e.target)?.data?.nodeCode||'',edgeLabel:''}))
- const json = JSON.stringify({templateName:'工作流模板',description:'',nodes,edges},null,2)
- navigator.clipboard.writeText(json).then(()=>alert('已复制到剪贴板'))
- console.log(json)
- }
- async function saveWorkflow(){
- exportJson()
- const token = localStorage.getItem('company_token') || ''
- try{
- const r = await fetch('http://localhost:8006/workflow/lobster/template/save',{
- method:'POST',headers:{'Content-Type':'application/json','Authorization':'Bearer '+token},
- body:JSON.stringify(JSON.parse(await navigator.clipboard.readText()))
- })
- const d = await r.json()
- alert(d.code===200?'保存成功!':'保存失败: '+d.msg)
- }catch(e){alert('请先登录系统获取token,或在控制台复制JSON手动保存')}
- }
- async function aiGenerate(){
- genResult.value = null
- genStatus.value = 'submitting'
- genProgress.value = '正在提交AI生成任务...'
-
- const body = {requirement:genReq.value,industryType:genIndustry.value,modelConfig:{modelA:'doubao-lite',modelB:'doubao-lite',modelC:'doubao-lite'}}
- if(genExisting.value){
- body.existingWorkflow = genExisting.value
- body.modifyInstruction = genInstruction.value||'优化工作流结构'
- }
- try{
- /* 第一步: 提交异步任务, 立即返回recordId */
- const submitRes = await fetch('/workflow/ai-generator/generate',{
- method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)
- })
- const submitData = await submitRes.json()
- if(submitData.code!==200||!submitData.data||!submitData.data.recordId){
- genStatus.value = 'error'
- genProgress.value = '提交失败'
- alert('AI生成任务提交失败')
- return
- }
- const recordId = submitData.data.recordId
- genProgress.value = 'AI正在生成工作流(模型A生成中)...'
-
- /* 第二步: 轮询任务状态, 每3秒查询一次, 最多轮询60次(3分钟) */
- for(let i=0;i<60;i++){
- await new Promise(r=>setTimeout(r,3000))
- const pollRes = await fetch('/workflow/ai-generator/result/'+recordId+'/detail')
- const pollData = await pollRes.json()
- if(pollData.code===200&&pollData.data){
- const result = pollData.data
- if(result.status==='completed'||result.status==='done'){
- genResult.value = {score:result.qualityScore||'85',details:result.suggestions||''}
- elements.value = parseWorkflowJson(result.workflowJson || result.nodes || result)
- genStatus.value = 'done'
- genProgress.value = 'AI生成完成! (得分:'+(result.qualityScore||85)+')'
- return
- }
- if(result.status==='failed'){
- genStatus.value = 'error'
- genProgress.value = '生成失败: '+(result.errorMsg||'未知错误')
- return
- }
- /* 进度更新 */
- if(i<5) genProgress.value = 'AI正在生成工作流(模型A生成中)...'
- else if(i<10) genProgress.value = '模型B优化中...'
- else genProgress.value = '模型C质检评分中...'
- }
- }
- genStatus.value = 'error'
- genProgress.value = '超时(3分钟),请重试'
- }catch(e){
- genStatus.value = 'error'
- genProgress.value = '请求失败: '+e.message
- alert('AI生成请求失败: '+e.message)
- }
- }
- function importJson(){
- try{
- elements.value = parseWorkflowJson(JSON.parse(loadJson.value))
- showLoad.value = false
- }catch(e){alert('JSON格式错误: '+e.message)}
- }
- function parseWorkflowJson(wf){
- if(typeof wf==='string') wf = JSON.parse(wf)
- const nodes = (wf.nodes||[]).map((n,i)=>({id:String(i+1),type:typeToCategory[n.nodeType]||'default',position:{x:300,y:50+i*130},data:{label:n.nodeName||n.name||'',nodeCode:n.nodeCode||'',nodeType:n.nodeType||3,nextNodeCode:n.nextNodeCode||'',messageTemplate:n.messageTemplate||'',conditionExpr:n.conditionExpr||'',nodeConfig:typeof n.nodeConfig==='string'?n.nodeConfig:JSON.stringify(n.nodeConfig),maxRounds:n.maxRounds||0}}))
- const edgeMap = {}
- nodes.forEach(n=>{if(n.data.nextNodeCode) edgeMap[n.data.nodeCode]=n.data.nextNodeCode})
- const edges = (wf.edges||[]).map((e,i)=>({id:'e'+i,source:nodes.find(n=>n.data.nodeCode===e.sourceNodeCode)?.id||'',target:nodes.find(n=>n.data.nodeCode===e.targetNodeCode)?.id||'',animated:true}))
- nodeIdCounter = nodes.length
- return [...nodes,...edges]
- }
- return {nodeGroups,allNodes,flowRef,elements,selectedNode,nodeTypes,
- showGenerate,showLoad,genReq,genIndustry,genExisting,genInstruction,genResult,loadJson,
- onDragStart,onDrop,onNodeClick,onConnect,onTypeChange,fitView,undo,exportJson,saveWorkflow,aiGenerate,importJson}
- }
- })
- app.component('vue-flow',VueFlow.default)
- app.component('vue-flow-background',VueFlowBackground.default)
- app.component('vue-flow-minimap',VueFlowMinimap.default)
- app.component('vue-flow-controls',VueFlowControls.default)
- app.mount('#app')
- </script>
- </body>
- </html>
|