UNPKG

decocms

Version:

Deco CMS โ€” Self-hostable MCP Gateway for managing AI connections and tools

2 lines (1 loc) โ€ข 24.9 kB
import{b as Re,a6 as Xe,a as Ze,a5 as dn,aI as un,aH as pn,j as e,I as Q,B as Y,D as en,v as nn,w as tn,x as on,aW as mn,y as gn,t as X,c as hn,r as ue,au as xn,aw as fn,T as jn,K as an,m as vn,h as we,E as A,a_ as Ge,q as Cn,d as Dn,gs as yn}from"./index-ztdGKvBm.js";import{C as _n}from"./collection-search-DkVgkcUC.js";import{g as Nn,a as bn,e as wn}from"./extract-connection-data-DjYTV7t7.js";import{u as sn,g as Sn,C as Oe}from"./connection-card-CTJwLo8p.js";import{D as kn,a as In,e as Tn,b as An,c as Pn,f as Mn,g as Fn}from"./drawer-CROtV2u4.js";import{u as Ln}from"./use-mobile-C_hdQY5I.js";import{F as B,b as O,c as E,d as z,f as $,a as Qe}from"./form-DlZ0KMAb.js";import{S as Vn,b as Bn,a as On,d as En,c as ge}from"./select-CxXZwsSr.js";import{T as zn}from"./textarea-viSdm6BE.js";import{a as cn}from"./use-connection-CcG4QE1K.js";import{n as Se}from"./LinkBroken01-DfUhihLM.js";import{a6 as Ee,aL as ze}from"./agent-icon-BYWtutu9.js";import{d as $n}from"./UserSquare-CafFh5_d.js";import{s as Hn}from"./XClose-CmTuu4UN.js";import{c as Un}from"./schema-D_OZwHJO.js";import{i as Ne,a as Ye,E as Kn,p as Rn,b as Xn,c as Wn}from"./connection-form-helpers-BjSA2uCL.js";import{a as $e}from"./authenticate-and-persist-oauth-87e1Ix6y.js";import{g as qn}from"./connection-slug-BNBBglpK.js";import{C as Gn}from"./collection-tabs-Cju2BOrS.js";import{u as Je}from"./use-infinite-scroll-DdrDrAAo.js";import{g as ke,a as Qn}from"./metadata-D72DunRR.js";import{B as be}from"./badge-DcUertOZ.js";import{c as He,T as Ue,a as Ke}from"./tooltip-Bm0P1gm_.js";import{u as Yn}from"./useSuspenseInfiniteQuery-Pk4rU2Ft.js";import{u as Jn}from"./use-mcp-client-2MG3LveS.js";import{s as Zn}from"./Plus-DWySsEmB.js";function et({open:l,onOpenChange:n,onCreated:J}){const i=Re(),{org:f}=Xe(),{data:h}=Ze.useSession(),{stdioEnabled:H}=dn(),U=Ln(),P=cn(),b=sn("").items,a=un({resolver:pn(Un),defaultValues:{title:"",description:null,icon:null,ui_type:"HTTP",connection_url:"",connection_token:null,npx_package:"",stdio_command:"",stdio_args:"",stdio_cwd:"",env_vars:[]}}),p=a.watch("ui_type"),w=a.watch("connection_url"),K=a.watch("npx_package"),r=Ne({uiType:p,connectionUrl:w??"",npxPackage:K??""})??Ye({uiType:p,connectionUrl:w??"",registryItems:b}),j=t=>{const d=t.trim();if(!d)return;const g=!!a.formState.dirtyFields.title,N=!!a.formState.dirtyFields.description,L=!!a.formState.dirtyFields.env_vars,k=c=>{if(c&&(!g&&!a.getValues("title").trim()&&c.title&&a.setValue("title",c.title,{shouldDirty:!1}),!N&&!(a.getValues("description")??"").trim()&&c.description&&a.setValue("description",c.description,{shouldDirty:!1}),!L&&c.envVarKeys?.length)){const y=a.getValues("env_vars")??[],ee=new Set(y.map(m=>m.key)),_=c.envVarKeys.filter(m=>!ee.has(m));_.length>0&&a.setValue("env_vars",[...y,..._.map(m=>({key:m,value:""}))],{shouldDirty:!0})}},s=Rn(d);if(s&&H){a.setValue("ui_type","NPX",{shouldDirty:!0}),a.setValue("npx_package",s.packageName,{shouldDirty:!0}),a.setValue("connection_url","",{shouldDirty:!0}),a.setValue("connection_token",null,{shouldDirty:!0}),k(Ne({uiType:"NPX",npxPackage:s.packageName}));return}if(d.startsWith("http://")||d.startsWith("https://")){const c=p==="HTTP"||p==="SSE"||p==="Websocket"?p:"HTTP";a.setValue("ui_type",c,{shouldDirty:!0}),a.setValue("connection_url",d,{shouldDirty:!0}),k(Ne({uiType:c,connectionUrl:d})??Ye({uiType:c,connectionUrl:d,registryItems:b}));return}p==="NPX"&&k(Ne({uiType:"NPX",npxPackage:d}))},S=t=>{t||a.reset(),n(t)},Z=async t=>{let d,g=null,N=null,L=null;t.ui_type==="NPX"?(d="STDIO",g="",L=Xn(t.npx_package||"",t.env_vars||[])):t.ui_type==="STDIO"?(d="STDIO",g="",L=Wn(t.stdio_command||"",t.stdio_args||"",t.stdio_cwd,t.env_vars||[])):(d=t.ui_type,g=t.connection_url||"",N=t.connection_token||null);const k=Nn("conn");try{await P.create.mutateAsync({id:k,title:t.title,description:t.description||null,connection_type:d,connection_url:g,connection_token:N,created_at:new Date().toISOString(),updated_at:new Date().toISOString(),created_by:h?.user?.id||"system",organization_id:f.id,icon:t.icon??null,app_name:null,app_id:null,connection_headers:L,oauth_config:null,configuration_state:null,metadata:null,tools:null,bindings:null}),a.reset(),n(!1),J?.(k)}catch{X.error(i("connections.createConnectionDialog.failedToCreateConnection"))}},x=i("connections.createConnectionDialog.title"),M=i("connections.createConnectionDialog.description"),D=a.formState.isSubmitting?i("connections.createConnectionDialog.saving"):i("connections.createConnectionDialog.createButton"),v=e.jsxs("div",{className:"grid gap-4",children:[e.jsx(B,{control:a.control,name:"ui_type",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.typeLabel")}),e.jsxs(Vn,{value:t.value,onValueChange:t.onChange,children:[e.jsx(z,{children:e.jsx(Bn,{children:e.jsx(On,{})})}),e.jsxs(En,{children:[e.jsx(ge,{value:"HTTP",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx(Ee,{className:"w-4 h-4"}),i("connections.createConnectionDialog.httpType")]})}),e.jsx(ge,{value:"SSE",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx(Ee,{className:"w-4 h-4"}),i("connections.createConnectionDialog.sseType")]})}),e.jsx(ge,{value:"Websocket",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx(Ee,{className:"w-4 h-4"}),i("connections.createConnectionDialog.websocketType")]})}),H&&e.jsxs(e.Fragment,{children:[e.jsx(ge,{value:"NPX",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx(Se,{className:"w-4 h-4"}),i("connections.createConnectionDialog.npxPackageType")]})}),e.jsx(ge,{value:"STDIO",children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx($n,{className:"w-4 h-4"}),i("connections.createConnectionDialog.customCommandType")]})})]})]})]}),e.jsx($,{})]})}),p==="NPX"&&e.jsx(B,{control:a.control,name:"npx_package",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.npmPackageLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.npmPackagePlaceholder"),...t,value:t.value??"",onPaste:d=>{const g=d.clipboardData.getData("text");g&&(d.preventDefault(),a.setValue("npx_package",g.trim(),{shouldDirty:!0}),j(g))},onBlur:d=>{j(d.target.value),t.onBlur()}})}),e.jsx($,{})]})}),p==="STDIO"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4 items-start",children:[e.jsx(B,{control:a.control,name:"stdio_command",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.commandLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.commandPlaceholder"),...t,value:t.value??""})}),e.jsx($,{})]})}),e.jsx(B,{control:a.control,name:"stdio_args",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.argumentsLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.argumentsPlaceholder"),...t,value:t.value??""})}),e.jsx($,{})]})})]}),e.jsx(B,{control:a.control,name:"stdio_cwd",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.workingDirectoryLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.workingDirectoryPlaceholder"),...t,value:t.value??""})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:i("connections.createConnectionDialog.workingDirectoryDescription")}),e.jsx($,{})]})})]}),(p==="NPX"||p==="STDIO")&&e.jsx(B,{control:a.control,name:"env_vars",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.environmentVariablesLabel")}),e.jsx(z,{children:e.jsx(Kn,{value:t.value??[],onChange:t.onChange})}),e.jsx($,{})]})}),p!=="NPX"&&p!=="STDIO"&&e.jsxs(e.Fragment,{children:[e.jsx(B,{control:a.control,name:"connection_url",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.urlLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.urlPlaceholder"),...t,value:t.value??"",onPaste:d=>{const g=d.clipboardData.getData("text");g&&(d.preventDefault(),a.setValue("connection_url",g.trim(),{shouldDirty:!0}),j(g))},onBlur:d=>{j(d.target.value),t.onBlur()}})}),e.jsx($,{})]})}),e.jsx(B,{control:a.control,name:"connection_token",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:r?.token?.label??i("connections.createConnectionDialog.tokenLabelDefault")}),e.jsx(z,{children:e.jsx(Q,{type:"password",placeholder:r?.token?.placeholder??i("connections.createConnectionDialog.tokenPlaceholderDefault"),className:"ph-no-capture",...t,value:t.value??""})}),r?.token?.helperText&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:[r.token.helperText,r.id==="github"&&e.jsxs(e.Fragment,{children:[" ","ยท"," ",e.jsx("a",{className:"text-foreground underline underline-offset-4 hover:text-foreground/80",href:"https://github.com/settings/personal-access-tokens",target:"_blank",rel:"noreferrer",children:i("connections.createConnectionDialog.openGitHubPatSettings")})]})]}),e.jsx($,{})]})})]}),e.jsx(B,{control:a.control,name:"title",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.nameLabel")}),e.jsx(z,{children:e.jsx(Q,{placeholder:i("connections.createConnectionDialog.namePlaceholder"),...t})}),e.jsx($,{})]})}),e.jsx(B,{control:a.control,name:"description",render:({field:t})=>e.jsxs(O,{children:[e.jsx(E,{children:i("connections.createConnectionDialog.descriptionLabel")}),e.jsx(z,{children:e.jsx(zn,{placeholder:i("connections.createConnectionDialog.descriptionPlaceholder"),rows:3,...t,value:t.value??""})}),e.jsx($,{})]})})]});return U?e.jsx(kn,{open:l,onOpenChange:S,children:e.jsxs(In,{className:"max-h-[90vh]",children:[e.jsx(Tn,{className:"pb-2",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 text-left",children:[e.jsx(An,{children:x}),e.jsx(Pn,{className:"mt-1",children:M})]}),e.jsx(Mn,{asChild:!0,children:e.jsx(Y,{variant:"ghost",size:"icon",className:"shrink-0 -mt-1","aria-label":i("connections.createConnectionDialog.closeButton"),children:e.jsx(Hn,{size:16})})})]})}),e.jsx(Qe,{...a,children:e.jsxs("form",{onSubmit:a.handleSubmit(Z),children:[e.jsx("div",{className:"overflow-y-auto px-4 pb-4",children:v}),e.jsx(Fn,{children:e.jsx(Y,{type:"submit",disabled:a.formState.isSubmitting,className:"w-full",children:D})})]})})]})}):e.jsx(en,{open:l,onOpenChange:S,children:e.jsxs(nn,{className:"sm:max-w-[525px]",children:[e.jsxs(tn,{children:[e.jsx(on,{children:x}),e.jsx(mn,{children:M})]}),e.jsx(Qe,{...a,children:e.jsxs("form",{onSubmit:a.handleSubmit(Z),children:[e.jsx("div",{className:"py-4",children:v}),e.jsxs(gn,{children:[e.jsx(Y,{type:"button",variant:"outline",onClick:()=>S(!1),children:i("connections.createConnectionDialog.cancelButton")}),e.jsx(Y,{type:"submit",disabled:a.formState.isSubmitting,className:"min-w-40",children:D})]})]})})]})})}function nt(l){const n=hn.c(71),{mode:J,agentId:i,addedConnectionIds:f,onAdd:h,onCloneAndAdd:H,onConnectAndAdd:U,connectingItemId:P,search:C,onCreateConnection:b,onBrowseNavigate:a,defaultTab:p}=l,w=J===void 0?"add":J,K=p===void 0?"connected":p,r=Re(),{org:j}=Xe(),S=ue.useDeferredValue(C),Z=C!==S;let x;n[0]!==S?(x=S.trim().toLowerCase(),n[0]=S,n[1]=x):x=n[1];const M=x;let D;n[2]!==K?(D=o=>o??K,n[2]=K,n[3]=D):D=n[3];const[v,t]=xn(fn.connectionsTab(j.slug)+(K==="all"?":home-modal":":agent-modal"),D);let d;n[4]!==v||n[5]!==t?(d=o=>{o!==v&&A("connections_dialog_tab_changed",{to_tab:o}),t(o)},n[4]=v,n[5]=t,n[6]=d):d=n[6];const g=d;let N;n[7]!==j.id||n[8]!==j.slug?(N={connectionId:jn,orgId:j.id,orgSlug:j.slug},n[7]=j.id,n[8]=j.slug,n[9]=N):N=n[9];const L=Jn(N),k=S?.trim().split(/\s+/).filter(Boolean)??[],s=k.length>0?{operator:"or",conditions:k.flatMap(lt)}:void 0,c={...s&&{where:s},orderBy:[{field:["updated_at"],direction:"asc"}],limit:100,offset:0},y=JSON.stringify(c),{data:ee,fetchNextPage:_,hasNextPage:m,isFetchingNextPage:I}=Yn({queryKey:an.collectionListInfinite(L,j.id,"","CONNECTIONS",y),queryFn:async o=>{const{pageParam:u}=o,R=u===void 0?0:u;return(await L.callTool({name:"COLLECTION_CONNECTIONS_LIST",arguments:{...s&&{where:s},orderBy:[{field:["updated_at"],direction:"asc"}],limit:100,offset:R}})).structuredContent},initialPageParam:0,getNextPageParam:ct,staleTime:3e4}),he=ee?.pages.flatMap(st)??[],xe=Sn(he),rn=new Set(he.filter(it).map(at)),W=sn(S),Ie=Je(W.loadMore,W.hasMore,W.isLoadingMore),Te=Je(_,m??!1,I),F=v==="all"||!!M,We=F?W.items.filter(o=>{const u=bn(o);if(u&&rn.has(u))return!1;if(!M)return!0;const R=ke(o._meta),q=[o.title,o.description,o.name,o.server?.title,o.server?.description,o.server?.name,R?.friendly_name,R?.friendlyName].filter(Boolean).join(" ").toLowerCase();return k.some(V=>q.includes(V.toLowerCase()))}):[],pe=We.filter(ot),me=We.filter(tt);let fe;n[10]!==f?(fe=o=>o.some(u=>f.has(u.id)),n[10]=f,n[11]=fe):fe=n[11];const Ae=fe;let je;n[12]!==f||n[13]!==i||n[14]!==P||n[15]!==Ae||n[16]!==w||n[17]!==h||n[18]!==a||n[19]!==H||n[20]!==r?(je=(o,u,R,q,V)=>{const _e=Ae(V),de=V.find(G=>!f.has(G.id)),T=V[0];if(w==="browse"){const G=qn(T);return e.jsx(Oe,{connection:{title:u,icon:R,description:V.length>1?`${V.length} instances`:q??void 0},fallbackIcon:e.jsx(Se,{}),headerActionsAlwaysVisible:!0,headerActions:e.jsxs(be,{variant:"secondary",className:"gap-1 font-normal",children:[e.jsx(Ge,{size:11})," ",r("virtualMcp.connectionDialogContent.connected")]}),onClick:()=>{A("connection_browse_clicked",{app_name:T.app_name??null,connection_id:T.id,instances_count:V.length}),a?.(G)}},o)}return e.jsx(Oe,{connection:{title:u,icon:R,description:q},fallbackIcon:e.jsx(Se,{}),headerActionsAlwaysVisible:!0,headerActions:e.jsxs("div",{className:"flex items-center gap-1.5",children:[_e&&e.jsxs(be,{variant:"secondary",className:"gap-1 font-normal",children:[e.jsx(Ge,{size:11})," ",r("virtualMcp.connectionDialogContent.added")]}),e.jsx(Y,{variant:"outline",size:"sm",disabled:P!==null,onClick:G=>{G.stopPropagation(),de?(A("connection_add_clicked",{action:"use_existing",app_name:T.app_name??null,connection_id:de.id}),i&&A("agent_connection_attached",{agent_id:i,connection_id:de.id,app_name:T.app_name??null,mode:"existing"}),h(de.id)):(A("connection_add_clicked",{action:"clone",app_name:T.app_name??null,base_connection_id:T.id}),H(T))},children:r("virtualMcp.connectionDialogContent.add")})]})},o)},n[12]=f,n[13]=i,n[14]=P,n[15]=Ae,n[16]=w,n[17]=h,n[18]=a,n[19]=H,n[20]=r,n[21]=je):je=n[21];const ve=je;let Ce;n[22]!==P||n[23]!==w||n[24]!==U||n[25]!==r?(Ce=o=>{const u=ke(o._meta),R=u?.friendlyName||u?.friendly_name||o.server?.title||o.title||o.server?.name||o.name||o.id||"",q=o.server?.description||o.description||null,V=o.server?.icons?.[0]?.src||Qn(o.server?.repository)||null,_e=u?.official===!0,de=u?.verified===!0,T=u?.owner==="deco";return e.jsx(Oe,{connection:{title:R,description:q,icon:V},fallbackIcon:e.jsx(Se,{}),headerActionsAlwaysVisible:!0,headerActions:e.jsxs("div",{className:"flex items-center gap-1.5",children:[T&&e.jsxs(He,{children:[e.jsx(Ue,{asChild:!0,children:e.jsx("span",{className:"inline-flex items-center justify-center size-5 rounded-md bg-muted shrink-0",children:e.jsx("img",{src:"/logos/deco logo.svg",alt:r("virtualMcp.connectionDialogContent.madeByDeco"),className:"size-3"})})}),e.jsx(Ke,{children:r("virtualMcp.connectionDialogContent.builtByDeco")})]}),!T&&_e&&e.jsxs(He,{children:[e.jsx(Ue,{asChild:!0,children:e.jsx(be,{variant:"outline",size:"icon",children:e.jsx(ze,{})})}),e.jsx(Ke,{children:r("virtualMcp.connectionDialogContent.builtByOfficial")})]}),!T&&!_e&&de&&e.jsxs(He,{children:[e.jsx(Ue,{asChild:!0,children:e.jsx(be,{variant:"outline",size:"icon",children:e.jsx(ze,{})})}),e.jsx(Ke,{children:r("virtualMcp.connectionDialogContent.verifiedByDeco")})]}),e.jsx(Y,{variant:"outline",size:"sm",disabled:P!==null,onClick:G=>{G.stopPropagation(),A("connection_add_clicked",{action:"connect_new",registry_item_id:o.id,app_name:u?.friendlyName||o.server?.name||o.name||null}),U(o)},children:P===o.id?e.jsx(we,{className:"size-3.5"}):r(w==="browse"?"virtualMcp.connectionDialogContent.connect":"virtualMcp.connectionDialogContent.add")})]})},`catalog-${o.id}`)},n[22]=P,n[23]=w,n[24]=U,n[25]=r,n[26]=Ce):Ce=n[26];const qe=Ce;let ne;n[27]!==v||n[28]!==g||n[29]!==b||n[30]!==M||n[31]!==r?(ne=!M&&e.jsxs("div",{className:"flex items-center justify-between px-5 py-3 border-b border-border shrink-0",children:[e.jsx(Gn,{tabs:[{id:"all",label:r("virtualMcp.connectionDialogContent.tabAll")},{id:"connected",label:r("virtualMcp.connectionDialogContent.tabConnected")}],activeTab:v,onTabChange:o=>g(o)}),e.jsxs(Y,{variant:"outline",size:"sm",onClick:()=>{A("connections_dialog_custom_clicked"),b()},children:[e.jsx(Zn,{size:12}),r("virtualMcp.connectionDialogContent.customConnection")]})]}),n[27]=v,n[28]=g,n[29]=b,n[30]=M,n[31]=r,n[32]=ne):ne=n[32];const Pe=Z&&"opacity-50 pointer-events-none";let te;n[33]!==Pe?(te=vn("flex-1 overflow-auto p-5 transition-opacity duration-150",Pe),n[33]=Pe,n[34]=te):te=n[34];const ln="grid grid-cols-[repeat(auto-fill,minmax(260px,1fr))] gap-4";let De;n[35]!==ve?(De=o=>{if(o.type==="group")return ve(o.key,o.title,o.icon,null,o.connections);const u=o.connection;return ve(u.id,u.title,u.icon,u.description??null,[u])},n[35]=ve,n[36]=De):De=n[36];const Me=xe.map(De);let oe;n[37]!==Te?(oe=e.jsx("div",{ref:Te,className:"col-span-full h-1"}),n[37]=Te,n[38]=oe):oe=n[38];let ae;n[39]!==I?(ae=I&&e.jsx("div",{className:"col-span-full flex justify-center py-6",children:e.jsx(we,{className:"size-6 text-muted-foreground"})}),n[39]=I,n[40]=ae):ae=n[40];const Fe=F&&pe.length>0&&e.jsxs("div",{className:"col-span-full flex items-center gap-2 mt-2",children:[e.jsx(ze,{size:13,className:"text-muted-foreground shrink-0"}),e.jsx("span",{className:"text-sm font-medium text-muted-foreground whitespace-nowrap",children:r("virtualMcp.connectionDialogContent.sectionVerified")}),e.jsx("div",{className:"flex-1 h-px bg-border"})]}),Le=F&&pe.map(qe),Ve=F&&me.length>0&&e.jsxs("div",{className:"col-span-full flex items-center gap-2 mt-2",children:[e.jsx("span",{className:"text-sm font-medium text-muted-foreground whitespace-nowrap",children:r("virtualMcp.connectionDialogContent.sectionAllConnections")}),e.jsx("div",{className:"flex-1 h-px bg-border"})]}),Be=F&&me.map(qe);let ie;n[41]!==Ie||n[42]!==F?(ie=F&&e.jsx("div",{ref:Ie,className:"col-span-full h-1"}),n[41]=Ie,n[42]=F,n[43]=ie):ie=n[43];let se;n[44]!==W.isLoadingMore||n[45]!==F?(se=F&&W.isLoadingMore&&e.jsx("div",{className:"col-span-full flex justify-center py-6",children:e.jsx(we,{className:"size-6 text-muted-foreground"})}),n[44]=W.isLoadingMore,n[45]=F,n[46]=se):se=n[46];let ce;n[47]!==Me||n[48]!==oe||n[49]!==ae||n[50]!==Fe||n[51]!==Le||n[52]!==Ve||n[53]!==Be||n[54]!==ie||n[55]!==se?(ce=e.jsxs("div",{className:ln,children:[Me,oe,ae,Fe,Le,Ve,Be,ie,se]}),n[47]=Me,n[48]=oe,n[49]=ae,n[50]=Fe,n[51]=Le,n[52]=Ve,n[53]=Be,n[54]=ie,n[55]=se,n[56]=ce):ce=n[56];let re;n[57]!==v||n[58]!==xe.length||n[59]!==me||n[60]!==C||n[61]!==r||n[62]!==pe?(re=xe.length===0&&pe.length===0&&me.length===0&&e.jsx("div",{className:"flex items-center justify-center h-48 text-sm text-muted-foreground",children:C?r("virtualMcp.connectionDialogContent.emptyStateSearchNoMatch",{search:C}):r(v==="connected"?"virtualMcp.connectionDialogContent.emptyStateNoConnectionsYet":"virtualMcp.connectionDialogContent.emptyStateNoConnectionsAvailable")}),n[57]=v,n[58]=xe.length,n[59]=me,n[60]=C,n[61]=r,n[62]=pe,n[63]=re):re=n[63];let le;n[64]!==te||n[65]!==ce||n[66]!==re?(le=e.jsxs("div",{className:te,children:[ce,re]}),n[64]=te,n[65]=ce,n[66]=re,n[67]=le):le=n[67];let ye;return n[68]!==ne||n[69]!==le?(ye=e.jsxs(e.Fragment,{children:[ne,le]}),n[68]=ne,n[69]=le,n[70]=ye):ye=n[70],ye}function tt(l){return!l.verified&&!ke(l._meta)?.verified&&!l.meta?.verified}function ot(l){return l.verified||ke(l._meta)?.verified||l.meta?.verified}function at(l){return l.app_name}function it(l){return l.app_name}function st(l){return l?.items??[]}function ct(l,n){if(l?.hasMore)return n.reduce(rt,0)}function rt(l,n){return l+(n?.items?.length??0)}function lt(l){return[{field:["title"],operator:"contains",value:l},{field:["description"],operator:"contains",value:l}]}function Vt({open:l,onOpenChange:n,defaultTab:J,initialSearch:i="",...f}){const h=Re(),H=f.mode??"add",U="agentId"in f?f.agentId:void 0,P="addedConnectionIds"in f?f.addedConnectionIds??new Set:new Set,C="onAdd"in f&&f.onAdd?f.onAdd:s=>{},b=(s,c,y)=>{U&&A("agent_connection_attached",{agent_id:U,connection_id:s,app_name:c,mode:y})},[a,p]=ue.useState(null),[w,K]=ue.useState(i),[r,j]=ue.useState(!1),[S,Z]=ue.useState(l);l!==S&&(Z(l),l&&K(i));const{org:x}=Xe(),{data:M}=Ze.useSession(),D=cn(),v=Cn(),t=Dn(),d=s=>{n(!1),t({to:"/$org/settings/connections/$appSlug",params:{org:x.slug,appSlug:s}})},g=()=>{yn(v,x.id)},N=async(s,c)=>{A("connection_oauth_succeeded",{connection_id:s,flow:c});const y=new URL(`/api/${x.slug}/mcp/${s}`,window.location.origin);await v.invalidateQueries({queryKey:an.isMCPAuthenticated(y.href,null)}),g()},L=async s=>{p(s.app_name??s.id);try{const y=`${s.title.replace(/\s*\(\d+\)\s*$/,"")} (${Date.now().toString(36).slice(-4)})`,_=(await D.create.mutateAsync({title:y,description:s.description??null,connection_type:s.connection_type,connection_url:s.connection_url??null,connection_token:null,icon:s.icon??null,app_name:s.app_name??null,app_id:s.app_id??null,connection_headers:s.connection_headers??null})).id,m=await $e({connectionId:_,orgId:x.id,orgSlug:x.slug,persistFallback:I=>D.update.mutateAsync({id:_,data:{connection_token:I}}).then(()=>{})});if(m.ran&&!m.ok){A("connection_oauth_failed",{connection_id:_,flow:"clone",error:m.error??"no_token"}),X.error(h("virtualMcp.addConnectionDialog.authenticationFailedError",{error:m.error??"no token received"})),await D.delete.mutateAsync(_);return}m.ran&&await N(_,"clone"),b(_,s.app_name??null,"clone"),C(_)}catch(c){console.error("Failed to add connection:",c),X.error(h("virtualMcp.addConnectionDialog.failedToAddConnection"))}finally{p(null)}},k=async s=>{if(!(!x||!M?.user?.id)){p(s.id);try{const c=wn(s,x.id,M.user.id,{remoteIndex:0}),y=c.connection_type==="STDIO",ee=!!c.connection_url,_=y&&c.connection_headers&&typeof c.connection_headers=="object"&&"command"in c.connection_headers;if(!ee&&!_){X.error(h("virtualMcp.addConnectionDialog.mcpServerConnectionError")),p(null);return}const{id:m}=await D.create.mutateAsync(c),I=await $e({connectionId:m,orgId:x.id,orgSlug:x.slug,persistFallback:he=>D.update.mutateAsync({id:m,data:{connection_token:he}}).then(()=>{})});if(I.ran&&!I.ok){A("connection_oauth_failed",{connection_id:m,flow:"connect_new",error:I.error??"no_token"}),X.warning(h("virtualMcp.addConnectionDialog.signInFailedTitle"),{description:h("virtualMcp.addConnectionDialog.signInFailedDescription",{error:I.error??"no token received"})}),b(m,c.app_name??null,"new"),C(m);return}I.ran?(await N(m,"connect_new"),X.success(h("virtualMcp.addConnectionDialog.connectedAuthenticated"))):X.success(h("virtualMcp.addConnectionDialog.connected")),b(m,c.app_name??null,"new"),C(m)}catch(c){console.error("Failed to connect:",c),X.error(h("virtualMcp.addConnectionDialog.failedToConnect"))}finally{p(null)}}};return e.jsxs(en,{open:l,onOpenChange:n,children:[e.jsxs(nn,{className:"sm:max-w-5xl h-[85vh] max-h-[85vh] flex flex-col p-0 gap-0 overflow-hidden w-[95vw]",children:[e.jsx(tn,{className:"px-6 pt-5 pb-0 shrink-0",children:e.jsx(on,{className:"text-base font-semibold",children:h(H==="browse"?"virtualMcp.addConnectionDialog.connectionsTitle":"virtualMcp.addConnectionDialog.addConnectionTitle")})}),e.jsx("div",{className:"pt-3 shrink-0",children:e.jsx(_n,{value:w,onChange:K,placeholder:h("virtualMcp.addConnectionDialog.searchConnectionsPlaceholder")})}),e.jsx(ue.Suspense,{fallback:e.jsx("div",{className:"flex-1 flex items-center justify-center",children:e.jsx(we,{className:"size-6 text-muted-foreground"})}),children:e.jsx(nt,{mode:H,agentId:U,addedConnectionIds:P,onAdd:C,onCloneAndAdd:L,onConnectAndAdd:k,connectingItemId:a,search:w,onCreateConnection:()=>j(!0),onBrowseNavigate:d,defaultTab:J})})]}),e.jsx(et,{open:r,onOpenChange:j,onCreated:async s=>{j(!1);const c=await $e({connectionId:s,orgId:x.id,orgSlug:x.slug,persistFallback:y=>D.update.mutateAsync({id:s,data:{connection_token:y}}).then(()=>{})});if(c.ran&&!c.ok){A("connection_oauth_failed",{connection_id:s,flow:"custom_create",error:c.error??"no_token"}),X.warning(h("virtualMcp.addConnectionDialog.signInFailedTitle"),{description:h("virtualMcp.addConnectionDialog.signInFailedDescription",{error:c.error??"no token received"})}),b(s,null,"custom"),C(s),n(!1);return}c.ran&&await N(s,"custom_create"),b(s,null,"custom"),C(s),n(!1)}})]})}export{Vt as A};