import{j as e,H as v,P as w,u as E,N as H,R as O,a as G,i as P,B,C as L,b as I}from"./reactflow-vendor-DYfacRG4.js";import{r as c}from"./react-vendor-CMZ2fpGT.js";import{c as A,u as V,b as J}from"./index-9ErzGo47.js";import{c as j,u as k,a as C,g as m,F as N,S as X,G as $,f as W,b as F,e as R}from"./shallow-D0wo54Jg.js";import{S as b}from"./Stack-1B6MSRlq.js";import"./react-syntax-highlighter-Ca70WVaL.js";/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Y=j("outline","arrow-back-up","IconArrowBackUp",[["path",{d:"M9 14l-4 -4l4 -4",key:"svg-0"}],["path",{d:"M5 10h11a4 4 0 1 1 0 8h-1",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var K=j("outline","arrow-forward-up","IconArrowForwardUp",[["path",{d:"M15 14l4 -4l-4 -4",key:"svg-0"}],["path",{d:"M19 10h-11a4 4 0 1 0 0 8h1",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var q=j("outline","arrows-diagonal-2","IconArrowsDiagonal2",[["path",{d:"M16 20l4 0l0 -4",key:"svg-0"}],["path",{d:"M14 14l6 6",key:"svg-1"}],["path",{d:"M8 4l-4 0l0 4",key:"svg-2"}],["path",{d:"M4 4l6 6",key:"svg-3"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Q=j("outline","circle-check","IconCircleCheck",[["path",{d:"M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0",key:"svg-0"}],["path",{d:"M9 12l2 2l4 -4",key:"svg-1"}]]);/**
 * @license @tabler/icons-react v3.18.0 - MIT
 *
 * This source code is licensed under the MIT license.
 * See the LICENSE file in the root directory of this source tree.
 */var Z=j("outline","xbox-x","IconXboxX",[["path",{d:"M12 21a9 9 0 0 0 9 -9a9 9 0 0 0 -9 -9a9 9 0 0 0 -9 9a9 9 0 0 0 9 9z",key:"svg-0"}],["path",{d:"M9 8l6 8",key:"svg-1"}],["path",{d:"M15 8l-6 8",key:"svg-2"}]]);const z=c.forwardRef(({className:n,...o},s)=>e.jsx("textarea",{className:A("flex min-h-[20px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",n),ref:s,...o}));z.displayName="Textarea";const M=c.forwardRef(({className:n,type:o,...s},r)=>e.jsx("input",{type:o,className:A("flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",n),ref:r,...s}));M.displayName="Input";function ee({input:n,nodeID:o}){const{updateValues:s,values:r}=k(C(t=>({values:t.values,updateValues:t.updateValues})));return e.jsx(M,{type:"number",value:r[m("input",o,n.id)]??"",onChange:t=>{s({[m("input",o,n.id)]:t.target.value===""?void 0:Number(t.target.value)})},className:"w-full border-zinc-700 [appearance:textfield] [&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none"})}function ne({input:n,nodeID:o,isConnected:s}){const r=n.type==="int"||n.type==="float";return e.jsxs("div",{className:`flex relative grow w-full ${r?"h-10":""}`,children:[e.jsx(v,{type:"target",position:w.Left,id:n.id}),e.jsxs(N,{className:"gap-2 ml-2 w-full",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("span",{className:"text-sm font-semibold",children:n.name}),e.jsx("span",{className:"text-[10px] text-zinc-500 italic",children:n.type})]}),!s&&e.jsx(se,{input:n,nodeID:o})]})]},"input"+n.id)}function se({input:n,nodeID:o}){const{updateValues:s,values:r}=k(C(l=>({values:l.values,updateValues:l.updateValues}))),t=c.useRef(null),p=E();if(c.useEffect(()=>{if(n.type!=="str")return;const l=t.current;if(!l)return;const u=()=>{l.style.height="auto",l.style.height=`${l.scrollHeight}px`,p(o)};return u(),l.addEventListener("input",u),()=>{l.removeEventListener("input",u)}},[n.type,o,p,r[m("input",o,n.id)]]),n.type==="int"||n.type==="float")return e.jsx(ee,{input:n,nodeID:o});if(n.type==="str")return e.jsx(z,{ref:t,value:r[m("input",o,n.id)]??"",className:"border-zinc-700 py-1 flex-1 flex-grow nodrag nopan resize-none",style:{height:"auto",overflow:"hidden"},onChange:l=>{s({[m("input",o,n.id)]:l.target.value})}})}function oe({children:n}){const o=c.useRef(null),[s,r]=c.useState(100);return c.useEffect(()=>{var t;(t=o.current)!=null&&t.offsetHeight&&r(o.current.offsetHeight)},[]),e.jsxs("div",{className:"group h-full",children:[e.jsx(H,{className:"!w-3 !h-3 !translate-x-[-30%] !translate-y-[-30%] !bg-transparent !border-none group",minWidth:150,minHeight:s,children:e.jsx(q,{size:10,className:"hidden group-hover:block absolute bottom-0 right-0"})}),e.jsx("div",{className:"h-full",ref:o,children:n})]})}var y=(n=>(n.RUNNING="RUNNING",n.SUCCESS="SUCCESS",n.FAIL="FAIL",n))(y||{});function te({output:n,nodeID:o,status:s}){const r=s==null?void 0:s.results;return r?e.jsxs(b,{className:"text-left m-2 gap-[1px]",children:[e.jsxs(N,{className:"gap-1 items-center mb-1",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground ",children:"Outputs"}),(s==null?void 0:s.status)===y.RUNNING&&e.jsx(X,{className:"w-4 h-4"}),(s==null?void 0:s.status)===y.SUCCESS&&e.jsx(Q,{className:"w-4 h-4 text-green-500"}),(s==null?void 0:s.status)===y.FAIL&&e.jsx(Z,{className:"w-4 h-4 text-red-500"})]}),r==null?void 0:r.map((t,p)=>e.jsx(b,{children:e.jsx("div",{className:"text-sm p-1 rounded-md border border-zinc-600 bg-zinc-700",children:typeof t=="object"?e.jsx("pre",{className:"whitespace-pre-wrap",children:JSON.stringify(t,null,2)}):e.jsx("pre",{className:"whitespace-pre-wrap",children:t})})},p)),(s==null?void 0:s.error)&&e.jsx(b,{children:e.jsx("div",{className:"text-sm p-1 rounded-md border bg-destructive",children:e.jsx("pre",{className:"whitespace-pre-wrap break-all",children:s==null?void 0:s.error})})})]}):null}function re({data:n,id:o}){var l,u;const{selectedNodeIDs:s,edges:r,status:t}=k(C(i=>{var h,x;return{selectedNodeIDs:i.selectedNodeIDs,edges:i.edges,status:(x=(h=i.job_status)==null?void 0:h.nodes)==null?void 0:x[o]}})),p=E();return c.useEffect(()=>{p(o)},[t,r,o,p]),e.jsx(oe,{children:e.jsxs("div",{className:"h-full flex flex-col min-w-[120px]",children:[e.jsxs("div",{className:"h-4 flex justify-end",children:[n.import_error&&e.jsxs("p",{className:"text-[10px] font-bold px-2",children:[e.jsx("span",{className:"text-red-500",children:" ❌ Import Failed: "}),e.jsx("span",{children:n.import_error})]}),e.jsxs("p",{className:"text-[10px] text-muted-foreground italic px-2 w-fit text-right",children:["#",o]})]}),e.jsxs(b,{className:`flex-1 bg-zinc-800 rounded-md ${s.includes(o)?"outline outline-[0.8px] outline-zinc-300":(t==null?void 0:t.status)==="RUNNING"?"outline outline-2 outline-purple-500":""}`,children:[e.jsxs(N,{className:`relative py-1 ${n.import_error?"bg-red-500":"bg-gray-700"} rounded-t-md justify-between`,children:[e.jsx(v,{type:"target",position:w.Left,id:"top"}),e.jsx(N,{className:"justify-between  flex-grow",children:e.jsx("p",{className:"px-2 font-semibold",children:n.name})}),e.jsx(v,{type:"source",position:w.Right,id:"top"})]}),e.jsxs(N,{className:"flex justify-between py-2 h-full",children:[e.jsx("div",{className:"flex flex-col gap-1 h-full w-full pr-2",children:(l=n.input)==null?void 0:l.map(i=>{const h=r.some(x=>x.target===o&&x.targetHandle===i.id);return e.jsx(ne,{input:i,nodeID:o,isConnected:h},i.id)})}),e.jsx(b,{className:"flex flex-col justify-center ml-2 gap-2",children:(u=n.output)==null?void 0:u.map(i=>e.jsxs("div",{className:"relative flex items-center",children:[e.jsx("p",{className:"text-[10px] text-zinc-500 italic mr-[5px]",children:i.type}),e.jsx(v,{type:"source",position:w.Right,id:i.id})]},"output"+i.id))})]}),e.jsx(te,{nodeID:o,output:n.output??null,status:t??null})]})]})})}const ae=n=>({nodes:n.nodes,edges:n.edges,onNodesChange:n.onNodesChange,onEdgesChange:n.onEdgesChange,setJobStatus:n.setJobStatus,onConnect:n.onConnect,addNode:n.addNode,loadGraph:n.loadGraph,setSelectedNodeIDs:n.setSelectedNodeIDs}),le={astFunction:re};function ie(){const{theme:n}=V(),{screenToFlowPosition:o}=G(),{dropingNode:s,setDropingNode:r}=J(),{nodes:t,edges:p,onNodesChange:l,onEdgesChange:u,onConnect:i,addNode:h,loadGraph:x,setSelectedNodeIDs:S,setJobStatus:U}=k(C(ae));c.useEffect(()=>{const a=sessionStorage.getItem($);a&&(x(JSON.parse(a)),W("/refresh_node_def",{method:"POST",body:a}).then(f=>f.json()).then(f=>{x(f)})),console.log("connecting to websocket",F.wsUrl);const d=new WebSocket(F.wsUrl);return d.onopen=()=>{console.log("websocket connected")},d.onmessage=f=>{const g=JSON.parse(f.data);window.dispatchEvent(new CustomEvent("message",{detail:g})),console.log("message",g),g.type==="job_status"&&g.data&&U(g.data)},()=>{d.close()}},[]);const _=c.useCallback(a=>{a.preventDefault(),a.dataTransfer.dropEffect="move"},[]),D=c.useCallback(()=>{let a=1;for(;a<t.length+1;){if(t.every(d=>d.id!==a.toString()))return a.toString();a++}return a.toString()},[t]),T=c.useCallback(a=>{if(a.preventDefault(),!s)return;const d=o({x:a.clientX,y:a.clientY}),f={id:D(),data:s,position:d,type:"astFunction"};h(f),r(null)},[o,s]);return e.jsxs(P,{nodes:t,nodeTypes:le,edges:p,colorMode:n,onNodesChange:l,onEdgesChange:u,onDrop:T,onDragOver:_,onConnect:i,fitView:!0,fitViewOptions:{padding:.2},defaultEdgeOptions:{},onNodeClick:(a,d)=>{console.log("node click #"+d.id),S([d.id])},onPaneClick:()=>{console.log("pane click"),S([])},children:[e.jsx(B,{}),e.jsxs(L,{orientation:"horizontal",children:[e.jsx(I,{onClick:R.undo,children:e.jsx(Y,{size:16,className:"!fill-[none] !max-w-4 !max-h-4"})}),e.jsx(I,{onClick:R.redo,children:e.jsx(K,{size:16,className:"!fill-[none] !max-w-4 !max-h-4"})})]})]})}function he(){return e.jsx(O,{children:e.jsx(ie,{})})}export{ie as FlowCanvas,he as default};
