import{a$ as Ve,r as m,be as V,N as Be,eq as be,e as t,P as B,bV as lt,w as qe,Q as Te,x as Ye,Z as ct,G as Q,b_ as dt,dZ as pt,as as Pe,d_ as ft,M as ht,T as ut,B as W,aR as mt,e0 as gt,e1 as yt,A as ee,J as Ze,n as wt,e2 as Ct,U as xt,cL as vt,u as we,ah as bt,af as Ce,e8 as kt,eR as Nt,a6 as Oe,e6 as jt,W as xe,e9 as He,a5 as ie,ea as At,Y as St,eb as Dt,F as Mt,cN as Fe,eS as Et,ed as It,ee as Ft,K as $t}from"./index-_6SmYgWK.js";import{C as Tt,I as Bt,a as Rt,b as _t,c as zt,B as Lt}from"./style-Djw-NCXs.js";import{A as Pt,I as Ot,S as Ht,a as Wt,E as Vt}from"./EditNodeDialog-DcOiZAxi.js";import{d as qt,e as ke,k as Yt,z as Zt,P as Ut,h as Ue,l as Gt,p as Jt,m as Xt,s as Ge,n as Kt,o as Qt,g as eo,q as to,u as oo,b as no,R as ao,c as so}from"./DocStoreInputHandler-CaPW_OoW.js";import{u as io,C as ro,A as lo,I as co,a as po}from"./usePrompt-B4r_asdX.js";import{I as fo}from"./IconExclamationCircle-kP-S1YaN.js";import{i as ho}from"./index-BupL1YuJ.js";import"./NodeInfoDialog-BSQc04od.js";import"./Table-DcZ35doG.js";import"./config-DpuVAZLd.js";import"./ExpandMore-DfVpxsz-.js";import"./AssistantDialog-CjH6GuIO.js";import"./v4-CtRu48qb.js";import"./string-BbYD24iH.js";import"./vectorstore-khdhkL5P.js";import"./IconDeviceFloppy-WiT0BtR9.js";import"./llamaindex-B_JSDULd.js";import"./_baseAssignValue-YwoXCWsZ.js";import"./StatsCard-DjLJlF2a.js";/**
 * @license @tabler/icons-react v3.31.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var uo=Ve("outline","checkbox","IconCheckbox",[["path",{d:"M9 11l3 3l8 -8",key:"svg-0"}],["path",{d:"M20 12v6a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h9",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.31.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var mo=Ve("outline","checklist","IconChecklist",[["path",{d:"M9.615 20h-2.615a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h8a2 2 0 0 1 2 2v8",key:"svg-0"}],["path",{d:"M14 19l2 2l4 -4",key:"svg-1"}],["path",{d:"M9 8h4",key:"svg-2"}],["path",{d:"M9 12h2",key:"svg-3"}]]);const Je=({id:e,x:o,y:a,width:p,height:f,style:r,color:s,strokeColor:d,strokeWidth:i,className:l,borderRadius:h,shapeRendering:x,onClick:C,selected:g})=>{const{background:b,backgroundColor:y}=r||{},M=s||b||y;return V.createElement("rect",{className:Ue(["react-flow__minimap-node",{selected:g},l]),x:o,y:a,rx:h,ry:h,width:p,height:f,fill:M,stroke:d,strokeWidth:i,shapeRendering:x,onClick:C?j=>C(j,e):void 0})};Je.displayName="MiniMapNode";var go=m.memo(Je);const yo=e=>e.nodeOrigin,wo=e=>e.getNodes().filter(o=>!o.hidden&&o.width&&o.height),$e=e=>e instanceof Function?e:()=>e;function Co({nodeStrokeColor:e="transparent",nodeColor:o="#e2e2e2",nodeClassName:a="",nodeBorderRadius:p=5,nodeStrokeWidth:f=2,nodeComponent:r=go,onClick:s}){const d=ke(wo,Ge),i=ke(yo),l=$e(o),h=$e(e),x=$e(a),C=typeof window>"u"||window.chrome?"crispEdges":"geometricPrecision";return V.createElement(V.Fragment,null,d.map(g=>{const{x:b,y}=Xt(g,i).positionAbsolute;return V.createElement(r,{key:g.id,x:b,y,width:g.width,height:g.height,style:g.style,selected:g.selected,className:x(g),color:l(g),borderRadius:p,strokeColor:h(g),strokeWidth:f,shapeRendering:C,onClick:s,id:g.id})}))}var xo=m.memo(Co);const vo=200,bo=150,ko=e=>{const o=e.getNodes(),a={x:-e.transform[0]/e.transform[2],y:-e.transform[1]/e.transform[2],width:e.width/e.transform[2],height:e.height/e.transform[2]};return{viewBB:a,boundingRect:o.length>0?Kt(Qt(o,e.nodeOrigin),a):a,rfId:e.rfId}},No="react-flow__minimap-desc";function Xe({style:e,className:o,nodeStrokeColor:a="transparent",nodeColor:p="#e2e2e2",nodeClassName:f="",nodeBorderRadius:r=5,nodeStrokeWidth:s=2,nodeComponent:d,maskColor:i="rgb(240, 240, 240, 0.6)",maskStrokeColor:l="none",maskStrokeWidth:h=1,position:x="bottom-right",onClick:C,onNodeClick:g,pannable:b=!1,zoomable:y=!1,ariaLabel:M="React Flow mini map",inversePan:j=!1,zoomStep:w=10,offsetScale:E=5}){const S=qt(),P=m.useRef(null),{boundingRect:$,viewBB:T,rfId:je}=ke(ko,Ge),re=(e==null?void 0:e.width)??vo,te=(e==null?void 0:e.height)??bo,Ae=$.width/re,oe=$.height/te,ne=Math.max(Ae,oe),le=ne*re,ue=ne*te,R=E*ne,ae=$.x-(le-$.width)/2-R,me=$.y-(ue-$.height)/2-R,J=le+R*2,ge=ue+R*2,ce=`${No}-${je}`,q=m.useRef(0);q.current=ne,m.useEffect(()=>{if(P.current){const I=Yt(P.current),Y=z=>{const{transform:U,d3Selection:X,d3Zoom:G}=S.getState();if(z.sourceEvent.type!=="wheel"||!X||!G)return;const de=-z.sourceEvent.deltaY*(z.sourceEvent.deltaMode===1?.05:z.sourceEvent.deltaMode?1:.002)*w,pe=U[2]*Math.pow(2,de);G.scaleTo(X,pe)},Z=z=>{const{transform:U,d3Selection:X,d3Zoom:G,translateExtent:de,width:pe,height:De}=S.getState();if(z.sourceEvent.type!=="mousemove"||!X||!G)return;const ye=q.current*Math.max(1,U[2])*(j?-1:1),fe={x:U[0]-z.sourceEvent.movementX*ye,y:U[1]-z.sourceEvent.movementY*ye},he=[[0,0],[pe,De]],se=Gt.translate(fe.x,fe.y).scale(U[2]),Me=G.constrain()(se,he,de);G.transform(X,Me)},Se=Zt().on("zoom",b?Z:null).on("zoom.wheel",y?Y:null);return I.call(Se),()=>{I.on("zoom",null)}}},[b,y,j,w]);const O=C?I=>{const Y=Jt(I);C(I,{x:Y[0],y:Y[1]})}:void 0,H=g?(I,Y)=>{const Z=S.getState().nodeInternals.get(Y);g(I,Z)}:void 0;return V.createElement(Ut,{position:x,style:e,className:Ue(["react-flow__minimap",o]),"data-testid":"rf__minimap"},V.createElement("svg",{width:re,height:te,viewBox:`${ae} ${me} ${J} ${ge}`,role:"img","aria-labelledby":ce,ref:P,onClick:O},M&&V.createElement("title",{id:ce},M),V.createElement(xo,{onClick:H,nodeColor:p,nodeStrokeColor:a,nodeBorderRadius:r,nodeClassName:f,nodeStrokeWidth:s,nodeComponent:d}),V.createElement("path",{className:"react-flow__minimap-mask",d:`M${ae-R},${me-R}h${J+R*2}v${ge+R*2}h${-J-R*2}z
        M${T.x},${T.y}h${T.width}v${T.height}h${-T.width}z`,fill:i,fillRule:"evenodd",stroke:l,strokeWidth:h,pointerEvents:"none"})))}Xe.displayName="MiniMap";var jo=m.memo(Xe);const Re=({root:e,particles:o,decay:a,rotate:p,lifetime:f,fps:r,updateParticle:s,onFinish:d})=>{const i=f;let l=0,h=0;const x=1e3/r,C=g=>{h||(h=g);const b=g-h;if(b>=x&&(h=g-b%x,o.forEach(y=>s(y,l/i,a,p)),l+=1,l>=i)){o.forEach(y=>{y.element.parentNode===e&&e.removeChild(y.element)}),d();return}window.requestAnimationFrame(C)};window.requestAnimationFrame(C)},{PI:ve}=Math,We=e=>e*(ve/180),Ne=(e,o)=>{const a=Math.ceil(e),p=Math.floor(o);return Math.floor(Math.random()*(p-a))+a},_e=(e,o,a,p)=>{const f=We(e),r=We(o),{random:s}=Math;return{x:0,y:0,z:0,height:0,wobble:s()*10,velocity:a*.5+s()*a,angle2D:-f+(.5*r-s()*r),angle3D:-(ve/4)+s()*(ve/2),tiltAngle:s()*ve,differentiator:p}},Ao=e=>{const o=document.getElementById(e);return o||console.error(`Element with an ID of ${e} could not be found. Please provide a valid ID.`),o},So=["#A45BF1","#25C6F6","#72F753","#F76C88","#F5F770"],Ke=[-.6,-.3,0,.3,.6],Do=(e,o,a,p,f,r)=>Array.from({length:o}).map((s,d)=>{const i=document.createElement("span");return i.style["background-color"]=r[d%r.length],i.style.width=`${a}px`,i.style.height=`${a}px`,i.style.position=f,i.style.zIndex=`${p}`,e.appendChild(i),{element:i,differentiator:Ne(0,Ke.length)}}),Mo=(e,o,a)=>{const{x:p,y:f,tiltAngle:r,wobble:s,angle2D:d,angle3D:i,velocity:l,differentiator:h}=e.physics;e.physics.x+=Math.cos(d)*l,e.physics.y+=Math.sin(d)*l,e.physics.z+=Math.sin(i)*l,e.physics.wobble+=.05,e.physics.velocity*=a,e.physics.y+=3.5,e.physics.tiltAngle+=.15;const x=p+(Ke[h]*o*s*s+20*(h%2?Math.sin(s/4):Math.cos(s/4))),C=f+5*Math.sin(s);e.element.style.transform=`translate3d(${x}px, ${C}px, 0) rotate3d(1, 1, 1, ${h%2?r:-1*r}rad)`,e.element.style.scale=`${1-.2*o}`,o>.5&&(e.element.style.opacity=`${2-2*o}`)},Eo=(e,o,a)=>{const p=a||{},{elementCount:f=50,elementSize:r=8,colors:s=So,angle:d=90,spread:i=45,decay:l=.94,lifetime:h=200,startVelocity:x=35,zIndex:C=0,position:g="fixed",fps:b=60,onAnimationComplete:y}=p,M=Do(e,f,r,C,g,s).map(({element:j,differentiator:w})=>({element:j,physics:_e(d,i,x,w)}));Re({root:e,particles:M,decay:l,lifetime:h,fps:b,updateParticle:Mo,onFinish:()=>{typeof y=="function"&&y(),o()}})},Io=["🤓","😊","🥳"],Qe=[-.6,-.3,0,.3,.6],Fo=(e,o,a,p,f,r)=>Array.from({length:o}).map((s,d)=>{const i=document.createElement("span");return i.innerHTML=r[d%r.length],i.style.fontSize=`${a+Ne(0,4)}px`,i.style.position=f,i.style.zIndex=`${p}`,e.appendChild(i),{element:i,differentiator:Ne(0,Qe.length)}}),$o=(e,o,a,p)=>{const{x:f,y:r,tiltAngle:s,angle2D:d,velocity:i,differentiator:l,wobble:h}=e.physics;e.physics.x+=Math.cos(d)*i,e.physics.y+=Math.sin(d)*i,e.physics.wobble+=0,e.physics.velocity*=a,e.physics.y+=5,e.physics.tiltAngle+=.05;const x=`translate3d(${f+(Qe[l]*o*h*h+20*Math.sin(h/4))}px, ${r}px, 0)`,C=p?`rotate3d(0, 0, 1, ${l%2?s:-1*s}rad)`:"";e.element.style.transform=[x,C].filter(Boolean).join(" "),o>.5&&(e.element.style.opacity=`${2-2*o}`)},To=(e,o,a)=>{const p=a||{},{elementCount:f=20,elementSize:r=25,emoji:s=Io,angle:d=90,spread:i=45,decay:l=.94,lifetime:h=200,startVelocity:x=35,zIndex:C=0,position:g="fixed",rotate:b=!0,fps:y=60,onAnimationComplete:M}=p,j=Fo(e,f,r,C,g,s).map(({element:w,differentiator:E})=>({element:w,physics:_e(d,i,x,E)}));Re({root:e,particles:j,decay:l,rotate:b,lifetime:h,fps:y,updateParticle:$o,onFinish:()=>{typeof M=="function"&&M(),o()}})};var Bo=`<svg width="100%" viewBox="0 0 976 3040" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M489.693 1269.49C489.773 1378.79 410.493 1466.8 389.107 1571.11C384.147 1595.33 382.893 1620.12 386.187 1644.64C389.44 1668.83 399.28 1691.16 409.04 1713.29C419.253 1736.44 429.28 1760.11 429.453 1785.83C429.64 1812.27 416.307 1836.8 408.933 1861.59C396 1905.15 409.613 1948.88 425.213 1989.73C442.04 2033.83 461.387 2077.87 456.6 2126.17C453.92 2153.15 444.147 2179.13 434.2 2204.13C424.173 2229.35 413.72 2254.28 405.507 2280.17C389.213 2331.51 379.493 2385.83 378.267 2439.72C377.693 2464.89 380.333 2490.25 388.667 2514.11C397.053 2538.09 412.6 2558.75 424.733 2580.84C449.907 2626.64 458.453 2681 451.053 2732.52C443.853 2782.56 414.227 2825.99 408.893 2876.35C404.44 2918.39 416.733 2961.63 440.08 2996.53C450.373 3011.91 463.267 3025.44 477.867 3036.75C480.773 3039 484.88 3034.95 481.947 3032.68C446.147 3004.96 423.24 2964.2 415.907 2919.71C412.107 2896.61 413.627 2872.95 419.427 2850.35C426.053 2824.51 438.067 2800.36 446.667 2775.17C462.44 2729 463.147 2678.51 450.92 2631.45C444.733 2607.64 434.453 2585.4 421.893 2564.32C407.933 2540.91 394.307 2518.68 388.32 2491.71C382.493 2465.45 383.413 2438 385.56 2411.37C387.853 2382.85 392.467 2354.56 398.827 2326.67C405 2299.61 413.253 2273.05 423.107 2247.12C432.533 2222.27 443.533 2198.05 451.867 2172.8C459.707 2149.04 464.387 2124.29 463 2099.23C461.773 2077.19 455.64 2055.65 448.467 2034.89C433.587 1991.8 411.307 1949.77 409.413 1903.37C408.36 1877.76 417.12 1854.67 425.587 1830.97C433.68 1808.33 437.44 1786.41 432.787 1762.6C428.307 1739.63 417.333 1718.23 408.12 1696.91C397.267 1671.77 390.413 1646.41 390.013 1618.89C388.387 1509.52 469.707 1420.85 490.813 1316.72C493.973 1301.19 495.467 1285.33 495.453 1269.49C495.453 1265.77 489.693 1265.77 489.693 1269.49" stroke="#7A7E82" stroke-width="4"/>
<path d="M555.653 1298.41H423.52C415.173 1298.41 408.413 1305.17 408.413 1313.52C408.413 1321.85 415.173 1328.63 423.52 1328.63H555.653C564 1328.63 570.76 1321.85 570.76 1313.52C570.76 1305.17 564 1298.41 555.653 1298.41Z" fill="currentColor"/>
<path d="M555.653 1298.41H423.52C415.173 1298.41 408.413 1305.17 408.413 1313.52C408.413 1321.85 415.173 1328.63 423.52 1328.63H555.653C564 1328.63 570.76 1321.85 570.76 1313.52C570.76 1305.17 564 1298.41 555.653 1298.41Z" fill="black" fill-opacity="0.1"/>
<path d="M975.133 528.947C975.133 820.667 756.973 1255.43 487.853 1255.43C218.733 1255.43 0.559814 820.667 0.559814 528.947C0.559814 237.227 218.733 0.733359 487.853 0.733359C756.973 0.733359 975.133 237.227 975.133 528.947Z" fill="currentColor"/>
<path d="M549.04 1298.41C531.293 1285.91 519.893 1269.95 512.587 1254.2C504.387 1255.01 496.147 1255.43 487.853 1255.43C481.04 1255.43 474.28 1255.15 467.546 1254.6C460.226 1270.21 448.867 1286.01 431.253 1298.41H549.04Z" fill="currentColor"/>
<path d="M211.413 569.213C193.787 569.213 175.947 564.4 160.92 555.24C128.133 535.293 108.133 498.56 101.147 460.827C97.0133 438.533 96.9468 415.827 99.1068 393.214C120.627 308.28 168.453 234.48 233.467 183.733C246.947 173.213 261.16 163.693 276.027 155.267C280.347 152.813 284.72 150.467 289.147 148.2C291.707 147.373 294.347 146.92 297.12 146.92C301.347 146.92 305.907 147.974 311.067 150.414C332.773 160.64 334.36 192.213 323.693 213.72C313.04 235.226 294.187 251.8 283.413 273.253C262 315.88 276.48 366.707 287.347 413.147C298.213 459.6 302.427 515.373 267.8 548.187C252.707 562.48 232.227 569.213 211.413 569.213" fill="white" fill-opacity="0.2"/>
<path d="M785.133 110.4C863.72 204.187 911.613 328.747 911.613 465.414C911.613 757.147 693.44 1191.89 424.32 1191.89C350.973 1191.89 281.413 1159.6 219.013 1106.71C296.067 1196.16 388.48 1255.43 487.853 1255.43C756.973 1255.43 975.133 820.667 975.133 528.947C975.133 358.547 900.693 206.987 785.133 110.4Z" fill="black" fill-opacity="0.1"/>
</svg>`;const Ro=["#A45BF1","#25C6F6","#72F753","#F76C88","#F5F770"],et=[-.6,-.3,0,.3,.6],_o=(e,o,a)=>{o.parentNode===a&&a.removeChild(o)},zo=(e,o,a,p,f,r)=>Array.from({length:o}).map((s,d)=>{const i=document.createElement("span");return i.innerHTML=Bo,i.style.width=`${a}px`,i.style.position=f,i.style.color=r[d%r.length],i.style.zIndex=`${p}`,i.addEventListener("click",l=>{_o(l,i,e)},{once:!0}),e.appendChild(i),{element:i,differentiator:Ne(0,et.length)}}),Lo=(e,o,a)=>{const{x:p,y:f,tiltAngle:r,angle2D:s,velocity:d,differentiator:i,wobble:l}=e.physics;e.physics.x+=Math.cos(s)*.5*d,e.physics.y+=Math.sin(s)*.5*d,e.physics.wobble+=0,e.physics.velocity*=a,e.physics.tiltAngle+=.1;const h=p+(et[i]*o*l*l+20*Math.sin(l));e.element.style.transform=`translate3d(${h}px, ${f}px, 0) rotate3d(0, 0, 1, ${i%2?Math.sin(r/4)/3:Math.cos(r/4)/3}rad)`,e.element.style.scale=`${1-.2*o}`,o>.5&&(e.element.style.opacity=`${2-2*o}`)},Po=(e,o,a)=>{const p=a||{},{angle:f=90,decay:r=.999,spread:s=50,startVelocity:d=3,elementCount:i=10,elementSize:l=20,lifetime:h=600,zIndex:x=0,position:C="fixed",colors:g=Ro,fps:b=60,onAnimationComplete:y}=p,M=zo(e,i,l,x,C,g).map(({element:j,differentiator:w})=>({element:j,physics:_e(f,s,d,w)}));Re({root:e,particles:M,decay:r,lifetime:h,fps:b,updateParticle:Lo,onFinish:()=>{typeof y=="function"&&y(),o()}})},Oo=(e,o,a)=>{const[p,f]=m.useState(!1),r=()=>{f(!1)};return{reward:m.useCallback(()=>{const s=Ao(e);if(s)switch(f(!0),o){case"confetti":Eo(s,r,a);break;case"emoji":To(s,r,a);break;case"balloons":Po(s,r,a);break;default:console.error(`${o} is not a valid react-rewards type.`)}},[a,e,o]),isAnimating:p}};function tt({transform:e,isHumanInput:o,label:a,color:p}){return t.jsx("div",{style:{position:"absolute",background:"transparent",left:o?20:10,paddingTop:1,color:p,fontSize:"0.5rem",fontWeight:700,transform:e,zIndex:1e3},className:"nodrag nopan",children:a})}tt.propTypes={transform:B.string,isHumanInput:B.bool,label:B.string,color:B.string};const ot=({fromX:e,fromY:o,toX:a,toY:p,fromPosition:f,toPosition:r})=>{var g;const[s]=eo({sourceX:e,sourceY:o,sourcePosition:f,targetX:a,targetY:p,targetPosition:r}),{connectionHandleId:d}=ke(),i=Be(),l=(d||"").split("_")[0]||"",h=l==="humanInputAgentflow"||l==="conditionAgentflow"||l==="conditionAgentAgentflow",x=()=>{let b;if(l==="conditionAgentflow"||l==="conditionAgentAgentflow"){const y=d.split("-").pop();b=(isNaN(y)?0:y).toString()}if(l==="humanInputAgentflow"){const y=d.split("-").pop();b=(isNaN(y)?0:y).toString(),b=b==="0"?"proceed":"reject"}return b},C=((g=be.find(b=>b.name===(d||"").split("_")[0]||""))==null?void 0:g.color)??i.palette.primary.main;return t.jsxs("g",{children:[t.jsx("path",{fill:"none",stroke:C,strokeWidth:1.5,className:"animated",d:s}),t.jsxs("g",{transform:`translate(${a-10}, ${p-10}) scale(0.8)`,children:[t.jsx("path",{stroke:"none",d:"M0 0h24v24H0z",fill:"none"}),t.jsx("path",{d:"M12 2c5.523 0 10 4.477 10 10a10 10 0 0 1 -20 0c0 -5.523 4.477 -10 10 -10m-.293 6.293a1 1 0 0 0 -1.414 0l-.083 .094a1 1 0 0 0 .083 1.32l2.292 2.293l-2.292 2.293a1 1 0 0 0 1.414 1.414l3 -3a1 1 0 0 0 0 -1.414z",fill:C})]}),h&&t.jsx(to,{children:t.jsx(tt,{color:C,isHumanInput:l==="humanInputAgentflow",label:x(),transform:`translate(-50%, 0%) translate(${e}px,${o}px)`})})]})};ot.propTypes={fromX:B.number,fromY:B.number,toX:B.number,toY:B.number,fromPosition:B.any,toPosition:B.any};const Ho=m.memo(ot),Wo="/assets/validate_empty-BoEkQrnY.svg",Vo=e=>lt.get(`/validation/${e}`),qo={checkValidation:Vo},nt=({chatflowid:e,hidden:o})=>{const a=Be(),p=qe(),f=Te(w=>w.customization);Ye();const r=(...w)=>p(Ze(...w)),[s,d]=m.useState(!1),[i,l]=m.useState([]),[h,x]=m.useState(!1),C=m.useRef(null),g=m.useRef(s),b=w=>{C.current&&C.current.contains(w.target)||d(!1)},y=()=>{d(w=>!w)},M=async()=>{if(e)try{x(!0);const w=await qo.checkValidation(e);l(w.data),w.data.length===0&&r({message:"No issues found in your flow!",options:{key:new Date().getTime()+Math.random(),variant:"success",autoHideDuration:3e3}})}catch(w){console.error(w),r({message:w.message||"Failed to validate flow",options:{key:new Date().getTime()+Math.random(),variant:"error",autoHideDuration:3e3}})}finally{x(!1)}};m.useEffect(()=>{g.current===!0&&s===!1&&C.current.focus(),g.current=s},[s,e]);const j=w=>{const E=w.name,S=be.find(P=>P.name===E);return S?t.jsx(W,{sx:{width:"28px",height:"28px",borderRadius:"4px",backgroundColor:S.color,display:"flex",alignItems:"center",justifyContent:"center",color:"white"},children:t.jsx(S.icon,{size:16})}):t.jsx(W,{sx:{width:"28px",height:"28px",borderRadius:"4px",backgroundColor:w.type==="LLM"?"#4747d1":"#f97316",display:"flex",alignItems:"center",justifyContent:"center",color:"white"},children:w.type==="LLM"?t.jsx("span",{children:"ℓ"}):t.jsx(ho,{size:16})})};return t.jsxs(t.Fragment,{children:[!o&&t.jsx(ct,{sx:{position:"absolute",right:80,top:20},ref:C,size:"small",color:"teal","aria-label":"validation",title:"Validate Nodes",onClick:y,children:s?t.jsx(Q,{}):t.jsx(mo,{})}),t.jsx(dt,{placement:"bottom-end",open:s&&!o,anchorEl:C.current,role:void 0,transition:!0,disablePortal:!0,popperOptions:{modifiers:[{name:"offset",options:{offset:[80,14]}}]},sx:{zIndex:1e3},children:({TransitionProps:w})=>t.jsx(pt,{in:s,...w,children:t.jsx(Pe,{children:t.jsx(ft,{onClickAway:b,children:t.jsxs(ht,{elevation:16,border:!1,content:!1,sx:{p:2,width:"400px",maxWidth:"100%"},boxShadow:!0,shadow:a.shadows[16],children:[t.jsxs(ut,{variant:"h4",sx:{mt:1,mb:2},children:["Checklist (",i.length,")"]}),t.jsx(W,{sx:{maxHeight:"60vh",overflowY:"auto",pr:1,mr:-1},children:i.length>0?i.map((E,S)=>t.jsxs(Pe,{elevation:0,sx:{p:2,mb:2,backgroundColor:f.isDarkMode?a.palette.background.paper:a.palette.background.neutral,borderRadius:"8px",border:`1px solid ${mt("#FFB938",f.isDarkMode?.3:.5)}`},children:[t.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",marginBottom:"8px"},children:[j(E),t.jsx("div",{style:{fontWeight:500},children:E.label||E.name})]}),t.jsx(W,{sx:{mt:2}}),E.issues.map((P,$)=>t.jsxs(W,{sx:{pt:2,px:2,pb:$===E.issues.length-1?2:1,backgroundColor:f.isDarkMode?gt("#FFB938",.85):yt("#FFB938",.9),display:"flex",alignItems:"center",gap:2},children:[t.jsx(fo,{color:"#FFB938",size:20,style:{minWidth:"20px",width:"20px",height:"20px",flexShrink:0}}),t.jsx("span",{children:P})]},$))]},S)):t.jsx(W,{sx:{p:2,height:"auto",display:"flex",justifyContent:"center",alignItems:"center"},children:t.jsx("img",{style:{objectFit:"cover",height:"15vh",width:"auto"},src:Wo,alt:"validate_empty"})})}),t.jsx(W,{sx:{display:"flex",justifyContent:"center",mt:2,mb:1},children:t.jsx(ee,{variant:"contained",color:"teal",onClick:M,disabled:h,startIcon:h?null:t.jsx(uo,{size:18}),sx:{color:"white",minWidth:"120px"},children:h?"Validating...":"Validate Flow"})})]})})})})})]})};nt.propTypes={chatflowid:B.string,hidden:B.bool};const Yo=m.memo(nt),Zo={agentFlow:Wt,stickyNote:Ht,iteration:Ot},Uo={agentFlow:Pt},mn=()=>{const e=Be(),o=wt(),a=Te(n=>n.customization),{state:p}=Ct(),f=p?p.templateFlowData:"",r=document.location.pathname.toString().split("/"),s=r[r.length-1]==="canvas"||r[r.length-1]==="agentcanvas"?"":r[r.length-1],d=r.includes("agentcanvas")?"Agent":"Chatflow",{confirm:i}=xt(),l=qe(),h=Te(n=>n.canvas),[x,C]=m.useState(h),[g,b]=m.useState(null),{reactFlowInstance:y,setReactFlowInstance:M}=m.useContext(vt);Ye();const j=(...n)=>l(Ze(...n)),w=(...n)=>l($t(...n)),[E,S,P]=oo(),[$,T,je]=no(),[re,te]=m.useState(null),[Ae,oe]=m.useState(!1),[ne,le]=m.useState(!1),[ue,R]=m.useState({}),[ae,me]=m.useState(!1),[J,ge]=m.useState(!0),ce=m.useRef(null),q=we(bt.getAllNodes),O=we(Ce.createNewChatflow),H=we(Ce.updateChatflow),I=we(Ce.getSpecificChatflow),Y=n=>{var D,_;if(!Et(n,y))return;const c=n.sourceHandle.split("_")[0],u=n.targetHandle.split("_")[0],v=((D=be.find(F=>F.name===u))==null?void 0:D.color)??e.palette.primary.main,k=((_=be.find(F=>F.name===c))==null?void 0:_.color)??e.palette.primary.main;let N;if(c==="conditionAgentflow"||c==="conditionAgentAgentflow"){const F=n.sourceHandle.split("-").pop();N=(isNaN(F)?0:F).toString()}c==="humanInputAgentflow"&&(N=n.sourceHandle.split("-").pop(),N=N==="0"?"proceed":"reject");const L=y.getNodes().find(F=>F.id===n.source),A=y.getNodes().find(F=>F.id===n.target),Ee=(L==null?void 0:L.parentNode)&&(A==null?void 0:A.parentNode)&&L.parentNode===A.parentNode,K={...n,data:{...n.data,sourceColor:k,targetColor:v,edgeLabel:N,isHumanInput:c==="humanInputAgentflow"},...Ee&&{zIndex:9999},type:"agentFlow",id:`${n.source}-${n.sourceHandle}-${n.target}-${n.targetHandle}`};T(F=>so(K,F))},Z=n=>{try{const c=JSON.parse(n),u=c.nodes||[];S(u),T(c.edges||[]),setTimeout(()=>se(),0)}catch(c){console.error(c)}},Se=async()=>{const n={title:"Delete",description:`Delete ${d} ${g.name}?`,confirmButtonName:"Delete",cancelButtonName:"Cancel"};if(await i(n))try{await Ce.deleteChatflow(g.id),localStorage.removeItem(`${g.id}_INTERNAL`),o("/agentflows")}catch(u){j({message:typeof u.response.data=="object"?u.response.data.message:u.response.data,options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:v=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(v),children:t.jsx(Q,{})})}})}},z=n=>{if(y){const c=y.getNodes().map(k=>{const N=ie.cloneDeep(k.data);return Object.prototype.hasOwnProperty.call(N.inputs,Fe)&&(N.credential=N.inputs[Fe],N.inputs=ie.omit(N.inputs,[Fe])),k.data={...N,selected:!1,status:void 0},k}),u=y.toObject();u.nodes=c;const v=JSON.stringify(u);if(g.id){const k={name:n,flowData:v};H.request(g.id,k)}else{const k={name:n,deployed:!1,isPublic:!1,flowData:v,type:"AGENTFLOW"};O.request(k)}}},U=m.useCallback((n,c)=>{te(c),S(u=>u.map(v=>(v.id===c.id?v.data={...v.data,selected:!0}:v.data={...v.data,selected:!1},v)))}),X=m.useCallback((n,c)=>{if(!(!c||!c.data)&&c.data.name!=="stickyNoteAgentflow"){const u={data:c.data,inputParams:c.data.inputParams.filter(v=>!v.hidden)};R(u),le(!0)}}),G=m.useCallback(n=>{n.preventDefault(),n.dataTransfer.dropEffect="move"},[]),de=m.useCallback(n=>{n.preventDefault();const c=ce.current.getBoundingClientRect();let u=n.dataTransfer.getData("application/reactflow");if(typeof u>"u"||!u)return;u=JSON.parse(u);const v=y.project({x:n.clientX-c.left-100,y:n.clientY-c.top-50}),k=y.getNodes();if(u.name==="startAgentflow"&&k.find(D=>D.data.name==="startAgentflow")){j({message:"Only one start node is allowed",options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:D=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(D),children:t.jsx(Q,{})})}});return}const N=kt(u,y.getNodes()),L=Nt(u,k),A={id:N,position:v,data:{...Oe(u,N,!0),label:L}};u.type==="Iteration"?A.type="iteration":u.type==="StickyNote"?A.type="stickyNote":A.type="agentFlow";const Ee=k.filter(D=>D.type==="iteration");let K=null;for(const D of Ee){const _=D.width||300,F=D.height||250,ze=D.position.x,it=ze+_,Le=D.position.y,rt=Le+F;if(v.x>=ze&&v.x<=it&&v.y>=Le&&v.y<=rt){if(K=D,u.name==="iterationAgentflow"){j({message:"Nested iteration node is not supported yet",options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:Ie=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(Ie),children:t.jsx(Q,{})})}});return}if(u.name==="humanInputAgentflow"){j({message:"Human input node is not supported inside Iteration node",options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:Ie=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(Ie),children:t.jsx(Q,{})})}});return}break}}K&&(A.parentNode=K.id,A.extent="parent",A.position={x:v.x-K.position.x,y:v.y-K.position.y}),te(A),S(D=>(D??[]).concat(A).map(_=>(_.id===A.id?_.data={..._.data,selected:!0}:_.data={..._.data,selected:!1},_))),setTimeout(()=>se(),0)},[y]),pe=()=>{const n=h.componentNodes,c=ie.cloneDeep(E),u=ie.cloneDeep($);let v=[];for(let k=0;k<c.length;k++){const N=c[k],L=n.find(A=>A.name===N.data.name);if(L&&L.version>N.data.version){const A=ie.cloneDeep(L);c[k].data=It(A,N.data,!0),v.push(...Ft(c[k].data,u))}}S(c),T(u.filter(k=>!v.includes(k))),se(),oe(!1)},{reward:De}=Oo("canvasConfetti","confetti",{elementCount:150,spread:80,lifetime:300,startVelocity:40,zIndex:1e4,decay:.92,position:"fixed"}),ye=()=>{setTimeout(()=>{De()},50)},fe=()=>{l({type:He}),j({message:`${d} saved`,options:{key:new Date().getTime()+Math.random(),variant:"success",action:n=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(n),children:t.jsx(Q,{})})}})},he=n=>{j({message:n,options:{key:new Date().getTime()+Math.random(),variant:"error",persist:!0,action:c=>t.jsx(ee,{style:{color:"white"},onClick:()=>w(c),children:t.jsx(Q,{})})}})},se=()=>{l({type:jt})},Me=n=>{const c=h.componentNodes;for(let u=0;u<n.length;u++){const v=n[u],k=c.find(N=>N.name===v.data.name);if(k&&k.version>v.data.version){oe(!0);return}}oe(!1)};m.useEffect(()=>{if(I.data){const n=I.data,c=n.flowData?JSON.parse(n.flowData):[];S(c.nodes||[]),T(c.edges||[]),l({type:xe,chatflow:n})}else I.error&&he(`Failed to retrieve ${d}: ${I.error.response.data.message}`)},[I.data,I.error]),m.useEffect(()=>{if(O.data){const n=O.data;l({type:xe,chatflow:n}),fe(),window.history.replaceState(p,null,`/v2/agentcanvas/${n.id}`)}else O.error&&he(`Failed to save ${d}: ${O.error.response.data.message}`)},[O.data,O.error]),m.useEffect(()=>{H.data?(l({type:xe,chatflow:H.data}),fe()):H.error&&he(`Failed to save ${d}: ${H.error.response.data.message}`)},[H.data,H.error]),m.useEffect(()=>{if(b(x.chatflow),x.chatflow){const n=x.chatflow.flowData?JSON.parse(x.chatflow.flowData):[];Me(n.nodes||[])}},[x.chatflow]),m.useEffect(()=>(oe(!1),s?I.request(s):(localStorage.getItem("duplicatedFlowData")?(Z(localStorage.getItem("duplicatedFlowData")),setTimeout(()=>localStorage.removeItem("duplicatedFlowData"),0)):(S([]),T([])),l({type:xe,chatflow:{name:`Untitled ${d}`}})),q.request(),()=>{setTimeout(()=>l({type:He}),0)}),[]),m.useEffect(()=>{C(h)},[h]),m.useEffect(()=>{function n(c){const u=c.clipboardData.getData("text");u.includes('{"nodes":[')&&u.includes('],"edges":[')&&Z(u)}return window.addEventListener("paste",n),()=>{window.removeEventListener("paste",n)}},[]),m.useEffect(()=>{f&&f.includes('"nodes":[')&&f.includes('],"edges":[')&&Z(f)},[f]),io("You have unsaved changes! Do you want to navigate away?",x.isDirty);const[at,st]=m.useState(!1);return m.useEffect(()=>{if(!s&&!localStorage.getItem("duplicatedFlowData")&&q.data&&E.length===0){const n=q.data.find(c=>c.name==="startAgentflow");if(n){const c=ie.cloneDeep(n);c.position={x:100,y:100};const u={id:"startAgentflow_0",type:"agentFlow",position:{x:100,y:100},data:{...Oe(c,"startAgentflow_0",!0),label:"Start"}};S([u]),T([])}}},[q.data,s]),t.jsxs(t.Fragment,{children:[t.jsx("span",{id:"canvasConfetti",style:{position:"fixed",top:"50%",left:"50%",transform:"translate(-50%, -50%)",width:"0",height:"0",zIndex:9999,pointerEvents:"none",background:"transparent"}}),t.jsxs(W,{children:[t.jsx(At,{enableColorOnDark:!0,position:"fixed",color:"inherit",elevation:1,sx:{bgcolor:e.palette.background.default},children:t.jsx(St,{children:t.jsx(ro,{chatflow:g,handleSaveFlow:z,handleDeleteFlow:Se,handleLoadFlow:Z,isAgentCanvas:!0,isAgentflowV2:!0})})}),t.jsx(W,{sx:{pt:"70px",height:"100vh",width:"100%"},children:t.jsx("div",{className:"reactflow-parent-wrapper",children:t.jsx("div",{className:"reactflow-wrapper",ref:ce,children:t.jsxs(ao,{nodes:E,edges:$,onNodesChange:P,onNodeClick:U,onNodeDoubleClick:X,onEdgesChange:je,onDrop:de,onDragOver:G,onNodeDragStop:se,nodeTypes:Zo,edgeTypes:Uo,onConnect:Y,onInit:M,fitView:!0,deleteKeyCode:h.canvasDialogShow?null:["Delete"],minZoom:.5,snapGrid:[25,25],snapToGrid:ae,connectionLineComponent:Ho,children:[t.jsxs(Tt,{className:a.isDarkMode?"dark-mode-controls":"",style:{display:"flex",flexDirection:"row",left:"50%",transform:"translate(-50%, -50%)"},children:[t.jsx("button",{className:"react-flow__controls-button react-flow__controls-interactive",onClick:()=>{me(!ae)},title:"toggle snapping","aria-label":"toggle snapping",children:ae?t.jsx(Bt,{}):t.jsx(Rt,{})}),t.jsx("button",{className:"react-flow__controls-button react-flow__controls-interactive",onClick:()=>{ge(!J)},title:"toggle background","aria-label":"toggle background",children:J?t.jsx(_t,{}):t.jsx(zt,{})})]}),t.jsx(jo,{nodeStrokeWidth:3,nodeColor:a.isDarkMode?"#2d2d2d":"#e2e2e2",nodeStrokeColor:a.isDarkMode?"#525252":"#fff",maskColor:a.isDarkMode?"rgb(45, 45, 45, 0.6)":"rgb(240, 240, 240, 0.6)",style:{backgroundColor:a.isDarkMode?e.palette.background.default:"#fff"}}),J&&t.jsx(Lt,{color:"#aaa",gap:16}),t.jsx(lo,{isAgentCanvas:!0,isAgentflowv2:!0,nodesData:q.data,node:re,onFlowGenerated:ye}),t.jsx(Vt,{show:ne,dialogProps:ue,onCancel:()=>le(!1)}),Ae&&t.jsx(Dt,{sx:{left:60,top:20,color:"white",background:"orange","&:hover":{background:"orange",backgroundImage:"linear-gradient(rgb(0 0 0/10%) 0 0)"}},size:"small","aria-label":"sync",title:"Sync Nodes",onClick:()=>pe(),children:t.jsx(co,{})}),t.jsx(po,{isAgentCanvas:!0,chatflowid:s,onOpenChange:st}),!at&&t.jsx(Yo,{isAgentCanvas:!0,chatflowid:s})]})})})}),t.jsx(Mt,{})]})]})};export{mn as default};
