decocms
Version:
Deco CMS — Self-hostable MCP Gateway for managing AI connections and tools
10 lines (9 loc) • 6.76 kB
JavaScript
import{r as m,j as c,h as I,c as k,bb as v}from"./index-DoEtSiIe.js";import{l as S,c as L,F as $}from"./editor-UaRoONxx.js";import{d as K}from"./delay-CnY5UFtP.js";L();async function D(e){const t=e.getModel();if(!t)throw new Error("[Monaco] No model found in editor");const n=t.getValue(),i=n.replace(/export default (async )?function/,"export default $1function __default")+`
type __ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: __ExpandRecursively<A>) => __ExpandRecursively<R>
: T extends object
? T extends infer O ? { [K in keyof O]: __ExpandRecursively<O[K]> } : never
: T;
type __InferredOutput = __ExpandRecursively<Awaited<ReturnType<typeof __default>>>;
declare const __outputValue: __InferredOutput;
`;t.setValue(i);const r=t.findMatches("__outputValue",!1,!1,!1,null,!1);if(!r||r.length===0)return t.setValue(n),null;const l=r[0];if(!l)return t.setValue(n),null;const f={lineNumber:l.range.startLineNumber,column:l.range.startColumn+1};try{const y=await(await S.init()).languages.typescript.getTypeScriptWorker();if(!y)return t.setValue(n),null;const _=await y(t.uri);await K(100);const x=t.getOffsetAt(f),h=await _.getQuickInfoAtPosition(t.uri.toString(),x);if(t.setValue(n),h){const T=h.displayParts.map(O=>O.text).join("").replace(/^const __outputValue:\s*/,"");return g(T)}else return null}catch(o){return t.setValue(n),console.error("Error getting return type:",o),null}}function g(e){if(e=e.trim(),e==="string")return{type:"string"};if(e==="number")return{type:"number"};if(e==="boolean")return{type:"boolean"};if(e==="null")return{type:"null"};if(e==="undefined")return{type:"null"};if(e==="unknown"||e==="any")return{};if(e==="never")return{not:{}};if(e.endsWith("[]")){const s=e.slice(0,-2);return{type:"array",items:g(s)}}const t=e.match(/^Array<(.+)>$/);if(t&&t[1])return{type:"array",items:g(t[1])};const n=e.match(/^Record<(.+),\s*(.+)>$/);if(n&&n[2])return{type:"object",additionalProperties:g(n[2].trim())};if(e.includes("|")){const s=A(e);if(s.length>1)return s.every(r=>/^["']/.test(r.trim()))?{type:"string",enum:s.map(r=>r.trim().replace(/^["']|["']$/g,""))}:{anyOf:s.map(r=>g(r.trim()))}}return/^["'].*["']$/.test(e)?{type:"string",const:e.slice(1,-1)}:/^-?\d+(\.\d+)?$/.test(e)?{type:"number",const:parseFloat(e)}:e.startsWith("{")&&e.endsWith("}")?z(e):{description:`TypeScript type: ${e}`}}function A(e){const t=[];let n=0,s="";for(let i=0;i<e.length;i++){const r=e[i];if(r==="{"||r==="<"||r==="("||r==="[")n++;else if(r==="}"||r===">"||r===")"||r==="]")n--;else if(r==="|"&&n===0){t.push(s.trim()),s="";continue}s+=r}return s.trim()&&t.push(s.trim()),t}function z(e){const t=e.slice(1,-1).trim();if(!t)return{type:"object",properties:{}};const n={},s=[];let i=0,r="";for(let f=0;f<t.length;f++){const o=t[f];if(o==="{"||o==="<"||o==="("||o==="[")i++;else if(o==="}"||o===">"||o===")"||o==="]")i--;else if(o===";"&&i===0){r.trim()&&P(r.trim(),n,s),r="";continue}r+=o}r.trim()&&P(r.trim(),n,s);const l={type:"object",properties:n};return s.length>0&&(l.required=s),l}function P(e,t,n){const s=e.match(/^(\w+)(\?)?:\s*(.+)$/s);if(s){const i=s[1],r=s[2],l=s[3];i&&l&&(t[i]=g(l.trim()),r||n.push(i))}}class W extends m.Component{constructor(t){super(t),this.state={hasError:!1,mountKey:0}}static getDerivedStateFromError(t){if(t.message?.includes("InstantiationService has been disposed"))return{hasError:!0};throw t}componentDidCatch(t){t.message?.includes("InstantiationService has been disposed")&&this.setState(n=>({hasError:!1,mountKey:n.mountKey+1}))}render(){return this.state.hasError?c.jsx("div",{className:"flex items-center justify-center h-full w-full bg-white dark:bg-[#1e1e1e] text-muted-foreground",children:c.jsx(I,{size:"sm"})}):m.cloneElement(this.props.children,{mountKey:this.state.mountKey})}}let w=null;const J=async()=>{if(w)return w;const[e,t,n]=await Promise.all([v(()=>import("./standalone-BioU5qLH.js"),[]),v(()=>import("./typescript-Ca_eWjgL.js"),[]),v(()=>import("./estree-CRgMSIiv.js"),[])]);return w={format:e.format,plugins:[t.default,n.default]},w},F={parser:"typescript",semi:!0,singleQuote:!1,tabWidth:2,trailingComma:"es5",printWidth:80},M={fontSize:13,scrollBeyondLastLine:!1,automaticLayout:!0,lineNumbersMinChars:2,stickyScroll:{enabled:!1},wordWrap:"on",folding:!0,bracketPairColorization:{enabled:!0},suggestOnTriggerCharacters:!0,quickSuggestions:{other:!0,comments:!1,strings:!0},minimap:{enabled:!1},parameterHints:{enabled:!0},inlineSuggest:{enabled:!0},autoClosingBrackets:"always",autoClosingQuotes:"always",autoSurround:"languageDefined",padding:{top:12,bottom:12},scrollbar:{vertical:"auto",horizontal:"auto",verticalScrollbarSize:8,horizontalScrollbarSize:8}},q=c.jsx("div",{className:"flex items-center justify-center h-full w-full text-muted-foreground",children:c.jsx(I,{size:"sm"})});function B({code:e,onChange:t,onSave:n,readOnly:s=!1,height:i=300,language:r="typescript",disableDiagnostics:l=!1,autoFocus:f=!1,mountKey:o=0}){const y=m.useRef(null),_=m.useRef(n);_.current=n;const x=m.useRef(r);x.current=r;const h=m.useId(),j=`${h}-${o}`,T=r==="typescript"?`file:///workflow-${h.replace(/:/g,"-")}-${o}.tsx`:void 0,C=s?{...M,readOnly:!0}:M,O=async u=>{const a=u.getModel();if(!a){console.warn("No model found");return}const p=a.getValue(),E=x.current;if(E==="json"){try{const d=JSON.parse(p),R=JSON.stringify(d,null,2);if(R!==p){const b=a.getFullModelRange();u.executeEdits("json-format",[{range:b,text:R}])}}catch(d){console.error("JSON formatting failed:",d)}return}if(E==="typescript")try{const{format:d,plugins:R}=await J(),b=await d(p,{...F,plugins:R});if(b!==p){const V=a.getFullModelRange();u.executeEdits("prettier",[{range:V,text:b}])}}catch(d){console.error("Prettier formatting failed:",d)}},N=async(u,a)=>{y.current=u,f&&u.focus(),r==="typescript"&&(a.languages.typescript.typescriptDefaults.setCompilerOptions({target:a.languages.typescript.ScriptTarget.ESNext,module:a.languages.typescript.ModuleKind.ESNext,moduleResolution:a.languages.typescript.ModuleResolutionKind.NodeJs,allowNonTsExtensions:!0,allowJs:!0,strict:!1,noEmit:!0,esModuleInterop:!0,jsx:a.languages.typescript.JsxEmit.React,allowSyntheticDefaultImports:!0}),a.languages.typescript.typescriptDefaults.setDiagnosticsOptions({noSemanticValidation:l,noSyntaxValidation:l})),u.addCommand(a.KeyMod.CtrlCmd|a.KeyCode.KeyS,async()=>{await O(u);const p=await D(u),E=u.getValue();_.current?.(E,p)})};return c.jsx("div",{className:"h-full",children:c.jsx($,{height:i,language:r,theme:document.documentElement.classList.contains("dark")?"vs-dark":"light",value:e,path:T,onChange:t,onMount:N,loading:q,options:C},j)})}function G(e){const t=k.c(2);let n;return t[0]!==e?(n=c.jsx(W,{children:c.jsx(B,{...e})}),t[0]=e,t[1]=n):n=t[1],n}export{G as MonacoCodeEditor};