@membranehq/cli 1.4.11 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,7 +32,52 @@ membrane push
32
32
 
33
33
  See the Configuration section below for alternative ways to configure credentials.
34
34
 
35
- ## Commands
35
+ ## Authentication
36
+
37
+ Use `membrane login` to authenticate via browser-based OAuth. This stores your access token, refresh token, and the API URI locally.
38
+
39
+ ```bash
40
+ # Login to the default Membrane API
41
+ membrane login
42
+
43
+ # Login to a self-hosted instance
44
+ membrane login --apiUri http://localhost:9000
45
+
46
+ # Check current login status, API URI, and project defaults
47
+ membrane status
48
+
49
+ # Log out and clear stored tokens
50
+ membrane logout
51
+ ```
52
+
53
+ The `--apiUri` flag sets the API endpoint for this and all subsequent commands. Once logged in, the CLI remembers the API URI so you don't need to specify it again.
54
+
55
+ During login you select a default workspace and tenant. These defaults are saved in `.membrane/config.json` (project-level) and used by other commands like `connect`.
56
+
57
+ ## Connecting External Apps
58
+
59
+ Use `membrane connect` to create a connection to a third-party service via browser-based OAuth. Requires prior authentication via `membrane login`.
60
+
61
+ ```bash
62
+ # Connect using an integration key (uses defaults from login)
63
+ membrane connect --integrationKey hubspot
64
+
65
+ # Specify workspace and tenant explicitly
66
+ membrane connect --integrationKey hubspot --workspaceKey my-workspace --tenantKey my-customer
67
+
68
+ # Connect using a connector ID
69
+ membrane connect --connectorId 123
70
+
71
+ # Reconnect an existing connection
72
+ membrane connect --connectionId abc
73
+
74
+ # Print the connection URL instead of opening a browser
75
+ membrane connect --integrationKey hubspot --no-browser
76
+ ```
77
+
78
+ ## Synchronizing Workspace
79
+
80
+ Use `membrane init` to configure workspace credentials, then `membrane pull` and `membrane push` to synchronize workspace elements between local files and the remote workspace.
36
81
 
37
82
  ```bash
38
83
  # Initialize workspace configuration
@@ -44,12 +89,9 @@ membrane pull [--force]
44
89
  # Push workspace elements to remote
45
90
  membrane push [--force]
46
91
 
47
- # Get help
48
- membrane --help
49
- membrane <command> --help
92
+ # Show what would change on push without applying anything
93
+ membrane diff
50
94
 
51
- # Check version
52
- membrane --version
53
95
  ```
54
96
 
55
97
  ## Configuration
@@ -117,6 +159,13 @@ When you **push**, the CLI:
117
159
 
118
160
  Changes are applied in dependency order (integrations before integration-level elements, parents before children) to maintain referential integrity. Conflicts occur when an element exists in only one location; use `--force` to resolve by preferring the source.
119
161
 
162
+ When you **diff**, the CLI:
163
+
164
+ - Compares current local state with the remote workspace
165
+ - Displays a unified diff of all changes
166
+
167
+ Useful for previewing what `push` would do before actually applying changes.
168
+
120
169
  **Example:**
121
170
 
122
171
  ```bash
package/dist/index.js CHANGED
@@ -1,32 +1,37 @@
1
1
  #!/usr/bin/env node
2
- var Fn=Object.defineProperty;var c=(r,e)=>Fn(r,"name",{value:e,configurable:!0});import*as v from"node:fs";import{mkdirSync as Ln,writeFileSync as jn,readFileSync as Un}from"node:fs";import*as E from"node:path";import{join as _e,dirname as _n}from"node:path";import{URL as Kn,fileURLToPath as qn}from"node:url";import w from"chalk";import{Command as Bn}from"commander";import{WorkspaceElementType as x,MembraneConfigLoader as Wn,MembraneAxiosInstance as Jn,MembraneClient as jt,extractMembraneErrorData as z,WorkspaceElementSpecs as Z,WorkspaceSyncEventType as zn,ConnectorFileUpdateType as Ke,CONSOLE_ACCOUNT_API_TOKEN_PATH as Hn,MEMBRANE_CLI_CLIENT_ID as qe,OAUTH_SCOPE_PLATFORM_USER as Gn,NotFoundError as Vn,getConnectorSpecPath as Yn,getConnectorVersionPath as Ut,CONNECTOR_VERSION_DEVELOPMENT as Be,getMembraneElementPath as Zn,parseMembraneElementPath as ae,compareWorkspaceExports as Xn,WorkspaceElementChangeType as X,setValueAtLocator as We,getDataCollectionCreateFields as Je,excludeWriteOnlyFieldsFromSchema as _t,valueToSchema as Ie,getRequiredFieldsFromSchema as Qn,getValueAtLocator as er,walkSchema as Kt,makeDataLocationPath as qt,getDataCollectionUpdateFields as ze}from"@membranehq/sdk";import He from"lodash/camelCase.js";import Ge from"lodash/upperFirst.js";import tr from"code-block-writer";import nr from"lodash/uniqBy.js";import ce,{createContext as Ve,useState as N,useEffect as fe,useContext as Ye,useId as rr,useRef as Ze,useLayoutEffect as ke,useMemo as or}from"react";import{Box as g,Text as y,useInput as le,render as Xe,Newline as ir}from"ink";import{jsx as u,jsxs as b,Fragment as $e}from"react/jsx-runtime";import Bt from"swr/immutable";import A,{existsSync as Qe,mkdirSync as Wt,statSync as sr,chmodSync as ar,readFileSync as cr,writeFileSync as lr}from"fs";import et from"os";import k from"path";import ur from"conf";import we,{isAxiosError as dr}from"axios";import tt from"jsonwebtoken";import nt from"semver";import{AsyncLocalStorage as hr}from"async_hooks";import{toSentenceCase as pr}from"js-convert-case";import fr from"lodash/isEqual.js";import H from"js-yaml";import*as mr from"node:crypto";import{createHash as gr}from"node:crypto";import yr from"chokidar";import wr from"yaml";import{EventEmitter as Cr}from"events";import{EventSource as Sr}from"eventsource";import rt from"archiver";import Jt from"form-data";import Pe from"unzipper";import*as br from"node:os";import"node:events";import zt from"ink-text-input";import{SWRConfig as vr}from"swr";import Ae from"ink-spinner";import{TextInput as Er,Select as Tr,Spinner as xr}from"@inkjs/ui";import{randomBytes as Ht,createHash as Ir}from"crypto";import{createServer as kr}from"http";import ot from"ora";import{FastMCP as $r}from"fastmcp";import{z as R}from"zod";import Gt from"jszip";import{minimatch as Pr}from"minimatch";import{exec as Ar}from"node:child_process";import it from"lodash/merge.js";import Rr from"@anthropic-ai/sdk";import{faker as Ce}from"@faker-js/faker";import Dr from"lodash/template.js";import Or from"lodash/templateSettings.js";function ue(r){if(r.type){if(Array.isArray(r.type))return{anyOf:r.type.map(t=>ue({...r,type:t}))};if(r.type==="array"){const t={type:"array",items:r.items?ue(r.items):{type:"object"}};return r.title&&(t.title=r.title),r.description&&(t.description=r.description),r.minItems!==void 0&&(t.minItems=r.minItems),r.maxItems!==void 0&&(t.maxItems=r.maxItems),r.uniqueItems!==void 0&&(t.uniqueItems=r.uniqueItems),t}}const e={};if(r.type&&!Array.isArray(r.type)&&(e.type=r.type),r.title&&(e.title=r.title),r.description&&(e.description=r.description),r.format&&(e.format=r.format),r.enum&&(e.enum=r.enum),r.default!==void 0&&(e.default=r.default),r.minimum!==void 0&&(e.minimum=r.minimum),r.maximum!==void 0&&(e.maximum=r.maximum),r.minLength!==void 0&&(e.minLength=r.minLength),r.maxLength!==void 0&&(e.maxLength=r.maxLength),r.maxItems!==void 0&&(e.maxItems=r.maxItems),r.readOnly!==void 0&&(e.readOnly=r.readOnly),r.writeOnly!==void 0&&(e.writeOnly=r.writeOnly),r.examples&&r.examples.length>0&&(e.example=r.examples[0]),r.properties){e.properties={};for(const[t,n]of Object.entries(r.properties))e.properties[t]=ue(n)}return r.anyOf&&(e.anyOf=r.anyOf.map(ue)),r.additionalProperties!==void 0&&(typeof r.additionalProperties=="boolean"?e.additionalProperties=r.additionalProperties:e.additionalProperties=ue(r.additionalProperties)),r.required&&(e.required=r.required),e}c(ue,"convertDataSchemaToOpenAPI");function Nr(r){const{membraneInterfaces:{actions:e}}=r,t=Mr(e);return JSON.stringify({openapi:"3.0.0",info:{title:"Membrane Actions API",version:"1.0.0",description:"Generated OpenAPI specification for Membrane actions"},paths:{},components:{schemas:t}},null,2)}c(Nr,"generateOpenAPIContent");function Mr(r){const e={};return r.forEach(t=>{const n=Ge(He(t.key)),o={type:"object",properties:{id:{type:"string",description:"Request ID"},action:{type:"string",enum:[t.key]}},required:["id","action"]};if(t?.inputSchema){const a=ue(t.inputSchema);a.properties&&(o.properties={...o.properties,...a.properties}),a.required&&(o.required=[...o.required||[],...a.required])}e[`${n}Request`]=o;const i={type:"object",properties:{id:{type:"string",description:"Response ID"},success:{type:"boolean"},data:{type:"object"}},required:["id","success"]},s=t?.customOutputSchema||t?.outputSchema;if(s){const a=ue(s);i.properties={...i.properties,data:a}}e[`${n}Response`]=i}),e}c(Mr,"generateOpenAPISchemas");function Fr(r){const{membraneInterfaces:{actions:e}}=r,t=nr(e,"key"),n=new tr({indentNumberOfSpaces:2,newLine:`
3
- `});return n.writeLine("// Generated TypeScript definitions for Membrane actions"),n.newLine(),n.writeLine("import type {"),n.writeLine(" ActionAccessor,"),n.writeLine(" ConnectionLevelActionAccessor,"),n.writeLine('} from "@membranehq/sdk";'),n.newLine(),Lr(n,t),n.toString()}c(Fr,"generateTypeScriptContent");function Lr(r,e){r.writeLine('declare module "@membranehq/sdk" {'),r.indent(()=>{e.forEach(t=>{const n=at(t),o=t?.inputSchema;Re(r,{interfaceName:n,properties:o?.properties,required:o?.required||[]});const i=ct(t),s=t?.customOutputSchema||t?.outputSchema;Re(r,{interfaceName:i,properties:s?.properties,required:s?.required||[],fallbackContent:"result: unknown"}),r.newLine()}),Re(r,{interfaceName:"MembraneClient",methods:e.map(t=>{const n=at(t),o=ct(t);return{name:"action",parameters:`selector: "${t.key}"`,returnType:`ActionAccessor<${n}, ${o}>`}})}),r.newLine(),Re(r,{interfaceName:"ConnectionAccessor",methods:e.map(t=>{const n=at(t),o=ct(t);return{name:"action",parameters:`selector: "${t.key}"`,returnType:`ConnectionLevelActionAccessor<${n}, ${o}>`}})})}),r.writeLine("}")}c(Lr,"generateModuleAugmentation");function Re(r,e){const{interfaceName:t,properties:n,required:o=[],methods:i,fallbackContent:s}=e;r.writeLine(`interface ${t} {`),r.indent(()=>{n&&Object.keys(n).length>0&&jr(r,n,o),i&&i.length>0&&(n&&Object.keys(n).length>0&&r.newLine(),i.forEach((a,l)=>{r.writeLine(`${a.name}(${a.parameters}): ${a.returnType};`),l<i.length-1&&r.newLine()})),s&&(!n||Object.keys(n).length===0)&&(!i||i.length===0)&&r.writeLine(s)}),r.writeLine("}")}c(Re,"generateTypeScriptInterface");function jr(r,e,t){Object.entries(e).forEach(([n,o])=>{const s=t.includes(n)?"":"?",a=st(o);r.writeLine(`readonly ${n}${s}: ${a}`)})}c(jr,"generateTypeScriptProperties");function st(r){return r.type==="string"?r.enum?r.enum.map(e=>`'${e}'`).join(" | "):"string":r.type==="number"||r.type==="integer"?"number":r.type==="boolean"?"boolean":r.type==="array"?`${r.items?st(r.items):"unknown"}[]`:r.type==="object"?r.properties?`{ ${Object.entries(r.properties).map(([t,n])=>{const i=(r.required||[]).includes(t)?"":"?",s=st(n);return`readonly ${t}${i}: ${s}`}).join("; ")} }`:"Record<string, unknown>":"unknown"}c(st,"convertJsonSchemaToTypeScript");function at(r){return`${Ge(He(r.key))}Input`}c(at,"getInputTypeName");function ct(r){return`${Ge(He(r.key))}Output`}c(ct,"getOutputTypeName");async function lt(r){const{out:e}=r;await v.promises.mkdir(e,{recursive:!0});const t=Ur(r);for(const[n,o]of Object.entries(t)){const i=E.join(e,n);await v.promises.writeFile(i,o,"utf-8")}}c(lt,"generateCode");function Ur(r){switch(r.target){case"openapi":return{"openapi.json":Nr(r)};case"typescript":return{"generated.d.ts":Fr(r)};default:throw new Error(`Unsupported target: ${r.target}`)}}c(Ur,"generateContent");const ut=c(()=>[w.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),w.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),w.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),w.yellow("\u2502 The codegen command is experimental and subject to rapid changes.\u2502"),w.yellow("\u2502 Features, APIs, and file structures may change without notice. \u2502"),w.yellow("\u2502 Use in production environments is not recommended. \u2502"),w.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
4
- `),"createExperimentalWarning$2");function _r(r,e){r.command("codegen").description("\u26A0\uFE0F EXPERIMENTAL: Generate code for easy integration within projects - This feature is experimental and will be changing rapidly. Use at your own risk.").requiredOption("--actions <actions>",'Comma-separated list of action names (e.g., "list-files,get-file-by-id")').requiredOption("--out <path>","Output directory for generated files").requiredOption("--target <type>","Target format: openapi or typescript").requiredOption("--schemasOnly","Generate only schemas without implementation").addHelpText("after",ut()).addHelpText("after",["",w.bold("Examples:"),` ${w.gray("\u25B8")} ${w.cyan("membrane codegen --actions list-files,get-file-by-id --out src/generated --target openapi --schemasOnly")}`,` ${w.gray("\u25B8")} ${w.cyan("membrane install --actions delete-file --out src/generated --target typescript --schemasOnly")}`,"",ut()].join(`
5
- `)).action(async t=>{try{console.warn(ut()),console.error(w.cyan("\u{1F527} Membrane Codegen")),console.error("Status: Loading membrane interfaces...");const n=await Kr(e,t),o={out:t.out,target:t.target,schemasOnly:t.schemasOnly,membraneInterfaces:n};console.error(`Output: ${o.out}`),console.error(`Target: ${o.target}`),console.error(`Schemas Only: ${o.schemasOnly?"Yes":"No"}`),console.error(`Loaded ${n.actions.length} membrane interfaces`),console.error("Status: Generating code..."),await lt(o),console.error(w.green("\u2705 Code generation completed successfully!")),process.exit(0)}catch(n){console.error(w.red("\u274C Code generation failed:")),console.error(n instanceof Error?n.message:"Unknown error occurred"),process.exit(1)}})}c(_r,"setupCodegenCommand");async function Kr(r,e){return await r.fetchElements(),{actions:[...r.getSyncedElementsByType(x.Action).map(t=>t.data)].filter(t=>e.actions.includes(t.key||""))}}c(Kr,"loadMembraneInterfaces");class ye{static{c(this,"ConfigLoader")}static instance=null;sdkLoader;constructor(){this.sdkLoader=new Wn}static getInstance(){return ye.instance||(ye.instance=new ye),ye.instance}loadConfig(e){e&&e!==this.sdkLoader.getCwd()&&this.sdkLoader.setCwd(e);try{return this.sdkLoader.loadConfig({validate:!0})}catch(t){if(t.message?.includes("Failed to parse"))throw t;return null}}clearCache(){this.sdkLoader.clearCache()}updateConfig(e){return this.sdkLoader.updateConfig(e)}saveToFile(e){return this.sdkLoader.saveToFile(e)}isCacheDefined(){return this.sdkLoader.hasValidConfig()}}const de=ye.getInstance();function Se(r){return de.loadConfig(r)}c(Se,"readProjectConfig");const qr="membrane",Br="membrane.config.yml",De="https://api.getmembrane.com",Wr="https://console.getmembrane.com";function dt(r){const t=Se()?.consoleUri||Wr;return r?new Kn(r,t).toString():t}c(dt,"getConsoleUrl");function ht(){const r=process.cwd(),e=E.join(r,qr),t=E.join(r,Br),n=e,o=E.join(n,"workspace.yaml");return{membraneDirPath:e,configPath:t,payloadDirPath:n,workspaceDataFilePath:o}}c(ht,"getPaths");function Vt(){return ht().membraneDirPath}c(Vt,"getBasePath");const Oe=k.join(et.homedir(),".membrane");Qe(Oe)?(sr(Oe).mode&511)!==448&&ar(Oe,448):Wt(Oe,{mode:448});const Jr={pat:{type:"string"},workspace:{type:"object"},accessToken:{type:"string"},refreshToken:{type:"string"},expiresAt:{type:"number"},apiUri:{type:"string"}},U=new ur({schema:Jr,configName:"credentials",cwd:k.join(et.homedir(),".membrane"),configFileMode:384}),zr=c(r=>{U.set("pat",r)},"setPat"),Yt=c(()=>U.get("pat"),"getPat"),Hr=c(()=>{U.delete("pat")},"clearPat"),Zt=c((r,e,t)=>{U.set("accessToken",r),U.set("refreshToken",e),t!=null?U.set("expiresAt",Date.now()+t*1e3):U.delete("expiresAt")},"setTokens"),Gr=c(()=>U.get("accessToken"),"getAccessToken"),Ne=c(()=>U.get("refreshToken"),"getRefreshToken"),Vr=c(()=>{const r=U.get("expiresAt");return r?Date.now()>=r-6e4:!0},"isTokenExpired"),Yr=c(()=>{U.delete("accessToken"),U.delete("refreshToken"),U.delete("expiresAt"),U.delete("apiUri")},"clearTokens"),Me=c(()=>U.get("apiUri"),"getApiUri"),Zr=c(r=>{U.set("apiUri",r)},"setApiUri"),Xr=c(()=>!!(U.get("accessToken")||U.get("pat")),"isAuthenticated");class pt{static{c(this,"AccountApiClient")}constructor(e=De){this.apiBaseUrl=e}refreshPromise=null;async refreshAccessToken(){const e=Ne();if(!e)throw new Error("No refresh token available. Please run `membrane login`.");const t=await fetch(`${this.apiBaseUrl}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"refresh_token",refresh_token:e})});if(!t.ok)throw new Error("Failed to refresh access token. Please run `membrane login` again.");const n=await t.json(),o=n.refresh_token??e;return Zt(n.access_token,o,n.expires_in),n.access_token}async getAuthToken(){const e=Gr();if(e&&!Vr())return e;if(Ne())return this.refreshAccessToken();const t=Yt();if(t)return t;throw new Error("Not authenticated. Run `membrane login` or set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET.")}async request(e,t={}){const n=t.headers?t.headers instanceof Headers?new Headers(t.headers):new Headers(t.headers):new Headers;if(!n.has("Authorization")){const s=await this.getAuthToken();n.set("Authorization",`Bearer ${s}`)}t.body&&!n.has("Content-Type")&&n.set("Content-Type","application/json");let o=await fetch(`${this.apiBaseUrl}${e}`,{...t,headers:n});if(o.status===401&&Ne()){const s=await(this.refreshPromise??=this.refreshAccessToken().finally(()=>{this.refreshPromise=null}));n.set("Authorization",`Bearer ${s}`),o=await fetch(`${this.apiBaseUrl}${e}`,{...t,headers:n})}if(!o.ok){const s=await o.text();throw new Error(`API request failed: ${o.status} ${o.statusText} - ${s}`)}if(o.status===204)return{};const i=await o.text();return JSON.parse(i)}get(e){return this.request(e,{method:"GET"})}post(e,t){return this.request(e,{method:"POST",body:JSON.stringify(t)})}put(e,t){return this.request(e,{method:"PUT",body:JSON.stringify(t)})}patch(e,t){return this.request(e,{method:"PATCH",body:JSON.stringify(t)})}delete(e){return this.request(e,{method:"DELETE"})}}new pt(Me());const Qr=c(r=>{const e=new pt(Me());return t=>e.get(t)},"createAccountApiFetcher"),eo="membrane",to=["**/node_modules/**","**/.git/**","**/.DS_Store","**/Thumbs.db","**/*.tmp","**/*.swp","**/*.swo","**/*.log","**/*.lock","**/.cache/**","**/.next/**","**/.vscode/**","**/.idea/**","**/.logs/**"];function q(r){return E.join(r,eo)}c(q,"getMembraneDir");const Xt=_e(q(process.cwd()),".logs");class no{static{c(this,"WorkspaceLogger")}_logs;verboseMode=!1;notificationHandler=null;constructor(){this._logs=[]}setNotificationHandler(e){this.notificationHandler=e}setVerboseMode(e){this.verboseMode=e}getVerboseMode(){return this.verboseMode}get logs(){return[...this._logs]}get latestLogs(){return this._logs.slice(this._logs.length-5)}log(e,t="info"){const n={timestamp:new Date().toISOString(),message:e,type:t};this._logs.push(n),this._logs=this._logs.slice(0,1e3),(t!=="debug"||this.verboseMode)&&this.notificationHandler&&this.notificationHandler.addLog(n)}info(e){this.log(e,"info")}success(e){this.log(e,"success")}warning(e){this.log(e,"warning")}error(e){this.log(e,"error")}debug(e){this.log(e,"debug")}clear(){this._logs=[]}saveLogsToFile(e){if(this._logs.length===0)return null;try{const t=new Date().toISOString().replace(/[:.]/g,"_"),n=e?`-${e}`:"",o=`${t}${n}.txt`;Ln(Xt,{recursive:!0});const i=_e(Xt,o),s=this._logs.map(a=>`[${a.timestamp}] ${a.type?.toUpperCase()||"INFO"}: ${a.message}`).join(`
6
- `);return jn(i,s,"utf-8"),i}catch(t){return console.error("Failed to save logs:",t),null}}}const m=new no,Qt=new hr;class en{static{c(this,"RequestLogger")}constructor(e=Jn){this.axiosInstance=e}interceptorsConfigured=!1;setup(){if(this.interceptorsConfigured)return;const e=c(t=>(this.logError(t),Promise.reject(t)),"errorHandler");this.axiosInstance.interceptors.request.use(t=>{const n=Qt.getStore();t.metadata={startTime:Date.now(),skipErrorLog:n};const{method:o,url:i}=this.getRequestDetails(t);return m.debug(`[Request]: ${o} ${i}`),t},e),this.axiosInstance.interceptors.response.use(t=>{const{method:n,url:o}=this.getRequestDetails(t.config),i=this.getDuration(t.config),{status:s,statusText:a}=t;return m.debug(`[Response]: ${n} ${o} - ${s} ${a} (${i}ms)`),t},e),this.interceptorsConfigured=!0}static withSkipErrorLog(e){return Qt.run(!0,e)}getRequestDetails(e){const t=e?.method?.toUpperCase()??"UNKNOWN";if(!e?.url)return{method:t,url:"unknown"};if(e.url.startsWith("http"))return{method:t,url:e.url};const o=`${e.baseURL??""}/${e.url}`.replace(/\/+/g,"/").replace(/:\//,"://");return{method:t,url:o}}getDuration(e){return Date.now()-(e?.metadata?.startTime??Date.now())}logError(e){if(e.config?.metadata?.skipErrorLog)return;const{method:t,url:n}=this.getRequestDetails(e.config),o=this.getDuration(e.config);if(e.response){const{status:i,statusText:s}=e.response;m.error(`[Response]: ${t} ${n} - ${i} ${s} (${o}ms)`),e.response.data&&m.error(`[Response Data]: ${JSON.stringify(e.response.data,null,2)}`)}}}function ro(r,e){return new Proxy(r,{get(t,n){const o=t[n];return typeof o=="function"?(...i)=>(e.lastCall={method:String(n),args:i},o.apply(t,i)):o}})}c(ro,"createTrackedClient");class oo{static{c(this,"MembraneAPIManager")}client=null;currentConfig=null;tokenExpiry=0;requestTimes=[];maxRequestsPerSecond=80;windowSizeMs=1e3;semaphore=0;maxConcurrentRequests;maxRetries;retryDelay;requestTimeout;enableJitter;semaphoreQueue=[];rateLimitMutex=Promise.resolve();requestLogger=null;constructor(e={}){this.init(e)}init(e={}){this.windowSizeMs=e.windowSizeMs??1e3,this.maxRequestsPerSecond=e.maxRequestsPerSecond??80,this.maxConcurrentRequests=e.maxConcurrentRequests??Math.max(1,Math.min(Math.floor(this.maxRequestsPerSecond/4),20)),this.maxRetries=e.maxRetries??3,this.retryDelay=e.retryDelay??1e3,this.requestTimeout=e.requestTimeout??12e4,this.enableJitter=e.enableJitter!==!1,this.requestLogger=new en,this.requestLogger.setup()}async withClient(e,t=!0,n=process.cwd()){const o={lastCall:null},i=c(async()=>{try{return await this.withRetry(async()=>{const s=await this.getClient(n),a=ro(s,o);await this.acquireSemaphore();try{return await this.waitIfNeeded(),this.recordRequest(),await this.withTimeout(e(a),this.requestTimeout)}finally{this.releaseSemaphore()}},"API request")}catch(s){if(t){const a=o.lastCall?`Request: client.${o.lastCall.method}(${o.lastCall.args.map(l=>JSON.stringify(l)).join(", ")})`:null;throw s?.isMembraneError?(console.error(`
7
- Membrane API Error:`),a&&console.error(a),console.error(JSON.stringify(z(s),null,2))):dr(s)?(console.error(`
2
+ var nr=Object.defineProperty;var c=(r,e)=>nr(r,"name",{value:e,configurable:!0});import*as v from"node:fs";import{mkdirSync as rr,writeFileSync as or,readFileSync as ir}from"node:fs";import*as x from"node:path";import{join as Ge,dirname as sr}from"node:path";import{URL as ar,fileURLToPath as cr}from"node:url";import m from"chalk";import{Command as lr}from"commander";import{NotFoundError as ur,MembraneConfigLoader as dr,MembraneAxiosInstance as hr,MembraneClient as Gt,extractMembraneErrorData as z,parseMembraneElementPath as ue,getConnectorSpecPath as pr,getConnectorVersionPath as Vt,CONNECTOR_VERSION_DEVELOPMENT as Ve,WorkspaceElementType as I,getMembraneElementPath as fr,compareWorkspaceExports as Yt,WorkspaceElementSpecs as Z,WorkspaceSyncEventType as mr,ConnectorFileUpdateType as Ye,CONSOLE_ACCOUNT_API_TOKEN_PATH as gr,MEMBRANE_CLI_CLIENT_ID as Ze,OAUTH_SCOPE_PLATFORM_USER as yr,WorkspaceElementChangeType as X,setValueAtLocator as Xe,getDataCollectionCreateFields as Qe,excludeWriteOnlyFieldsFromSchema as Zt,valueToSchema as Ne,getRequiredFieldsFromSchema as wr,getValueAtLocator as Cr,walkSchema as Xt,makeDataLocationPath as Qt,getDataCollectionUpdateFields as et}from"@membranehq/sdk";import xe from"ora";import Ie,{isAxiosError as Sr}from"axios";import en from"form-data";import A,{existsSync as Me,mkdirSync as tn,statSync as br,chmodSync as vr,readFileSync as nn,writeFileSync as Er}from"fs";import tt from"os";import k from"path";import Tr from"conf";import nt from"jsonwebtoken";import rt from"semver";import{AsyncLocalStorage as xr}from"async_hooks";import ot from"archiver";import H from"js-yaml";import rn from"jszip";import{minimatch as Ir}from"minimatch";import it from"lodash/camelCase.js";import st from"lodash/upperFirst.js";import kr from"code-block-writer";import $r from"lodash/uniqBy.js";import de,{createContext as at,useState as N,useEffect as we,useContext as ct,useId as Pr,useRef as lt,useLayoutEffect as Fe,useMemo as Ar}from"react";import{Box as y,Text as w,useInput as he,render as ut,Newline as Rr}from"ink";import{jsx as u,jsxs as b,Fragment as Le}from"react/jsx-runtime";import on from"swr/immutable";import{toSentenceCase as Or}from"js-convert-case";import Dr from"lodash/isEqual.js";import*as Nr from"node:crypto";import{createHash as Mr}from"node:crypto";import Fr from"chokidar";import Lr from"yaml";import{EventEmitter as jr}from"events";import{EventSource as Ur}from"eventsource";import je from"unzipper";import*as Kr from"node:os";import"node:events";import sn from"ink-text-input";import{SWRConfig as _r}from"swr";import{randomUUID as qr,randomBytes as an,createHash as Br}from"crypto";import{createServer as cn}from"http";import Ue from"ink-spinner";import{TextInput as Wr,Select as Jr,Spinner as zr}from"@inkjs/ui";import{FastMCP as Hr}from"fastmcp";import{z as D}from"zod";import{exec as Gr}from"node:child_process";import dt from"lodash/merge.js";import Vr from"@anthropic-ai/sdk";import{faker as ke}from"@faker-js/faker";import Yr from"lodash/template.js";import Zr from"lodash/templateSettings.js";const Xr="membrane",Qr=["**/node_modules/**","**/.git/**","**/.DS_Store","**/Thumbs.db","**/*.tmp","**/*.swp","**/*.swo","**/*.log","**/*.lock","**/.cache/**","**/.next/**","**/.vscode/**","**/.idea/**","**/.logs/**"];function q(r){return x.join(r,Xr)}c(q,"getMembraneDir");const ln=Ge(q(process.cwd()),".logs");class eo{static{c(this,"WorkspaceLogger")}_logs;verboseMode=!1;notificationHandler=null;constructor(){this._logs=[]}setNotificationHandler(e){this.notificationHandler=e}setVerboseMode(e){this.verboseMode=e}getVerboseMode(){return this.verboseMode}get logs(){return[...this._logs]}get latestLogs(){return this._logs.slice(this._logs.length-5)}log(e,t="info"){const n={timestamp:new Date().toISOString(),message:e,type:t};this._logs.push(n),this._logs=this._logs.slice(0,1e3),(t!=="debug"||this.verboseMode)&&this.notificationHandler&&this.notificationHandler.addLog(n)}info(e){this.log(e,"info")}success(e){this.log(e,"success")}warning(e){this.log(e,"warning")}error(e){this.log(e,"error")}debug(e){this.log(e,"debug")}clear(){this._logs=[]}saveLogsToFile(e){if(this._logs.length===0)return null;try{const t=new Date().toISOString().replace(/[:.]/g,"_"),n=e?`-${e}`:"",o=`${t}${n}.txt`;rr(ln,{recursive:!0});const i=Ge(ln,o),s=this._logs.map(a=>`[${a.timestamp}] ${a.type?.toUpperCase()||"INFO"}: ${a.message}`).join(`
3
+ `);return or(i,s,"utf-8"),i}catch(t){return console.error("Failed to save logs:",t),null}}}const g=new eo;async function ht(r,e,t={}){const{jobId:n,accessKey:o}=await e(),i=`${n}:${o}`;g.debug(`[background-job] Started job ${n}`);const s=Date.now(),{pollIntervalMs:a=1e3,timeoutMs:l=3e5,maxRetries:d=3}=t;let h=0;for(;;){if(Date.now()-s>l)throw new Error(`Background job ${n} timed out after ${l}ms`);await new Promise(C=>setTimeout(C,a));let p;try{p=await r.get(`background-jobs/${i}`),h=0}catch(C){if(C instanceof ur&&h<d){h++,g.debug(`[background-job] Job ${n} not found, retrying (${h}/${d})`);continue}throw C}if(p.status==="completed"){if(g.debug(`[background-job] Completed job ${n}`),!p.result)throw new Error(`Background job ${n} completed but returned no result`);return p.result}if(p.status==="failed"){const C=p.error?.message||"Unknown error";throw g.error(`[background-job] Failed job ${n}: ${C}`),p.error?.stack&&g.error(`Stacktrace: ${p.error.stack}`),new Error(`Background job ${n} failed: ${C}`)}g.debug(`[background-job] Polling job ${n} (status: ${p.status})`)}}c(ht,"pollBackgroundJob");class Ke extends Error{static{c(this,"ApiError")}statusCode;errorType;errorKey;data;constructor(e,t){const n=to(t);if(n){const{message:o,type:i,key:s,...a}=n,l=typeof o=="string"?o:`API request failed (${e})`;super(l),this.errorType=typeof i=="string"?i:void 0,this.errorKey=typeof s=="string"?s:void 0,this.data=Object.keys(a).length>0?a:void 0}else super(t||`API request failed (${e})`);this.statusCode=e,this.name="ApiError"}format(){const e=[this.message];return this.data&&Object.keys(this.data).length>0&&e.push(JSON.stringify(this.data,null,2)),e.join(`
4
+ `)}}function to(r){try{const e=JSON.parse(r);return typeof e=="object"&&e!==null?e:null}catch{return null}}c(to,"tryParseJson");class Ee{static{c(this,"ConfigLoader")}static instance=null;sdkLoader;constructor(){this.sdkLoader=new dr}static getInstance(){return Ee.instance||(Ee.instance=new Ee),Ee.instance}loadConfig(e){e&&e!==this.sdkLoader.getCwd()&&this.sdkLoader.setCwd(e);try{return this.sdkLoader.loadConfig({validate:!0})}catch(t){if(t.message?.includes("Failed to parse"))throw t;return null}}clearCache(){this.sdkLoader.clearCache()}updateConfig(e){return this.sdkLoader.updateConfig(e)}saveToFile(e){return this.sdkLoader.saveToFile(e)}isCacheDefined(){return this.sdkLoader.hasValidConfig()}}const pe=Ee.getInstance();function $e(r){return pe.loadConfig(r)}c($e,"readProjectConfig");const no="membrane",ro="membrane.config.yml",ne="https://api.getmembrane.com",oo="https://console.getmembrane.com";function pt(r){const t=$e()?.consoleUri||oo;return r?new ar(r,t).toString():t}c(pt,"getConsoleUrl");function ft(){const r=process.cwd(),e=x.join(r,no),t=x.join(r,ro),n=e,o=x.join(n,"workspace.yaml");return{membraneDirPath:e,configPath:t,payloadDirPath:n,workspaceDataFilePath:o}}c(ft,"getPaths");function un(){return ft().membraneDirPath}c(un,"getBasePath");const _e=k.join(tt.homedir(),".membrane");Me(_e)?(br(_e).mode&511)!==448&&vr(_e,448):tn(_e,{mode:448});const io={pat:{type:"string"},workspace:{type:"object"},accessToken:{type:"string"},refreshToken:{type:"string"},expiresAt:{type:"number"},apiUri:{type:"string"}},K=new Tr({schema:io,configName:"credentials",cwd:k.join(tt.homedir(),".membrane"),configFileMode:384}),so=c(r=>{K.set("pat",r)},"setPat"),dn=c(()=>K.get("pat"),"getPat"),ao=c(()=>{K.delete("pat")},"clearPat"),hn=c((r,e,t)=>{K.set("accessToken",r),K.set("refreshToken",e),t!=null?K.set("expiresAt",Date.now()+t*1e3):K.delete("expiresAt")},"setTokens"),co=c(()=>K.get("accessToken"),"getAccessToken"),Ce=c(()=>K.get("refreshToken"),"getRefreshToken"),mt=c(()=>{const r=K.get("expiresAt");return r?Date.now()>=r-6e4:!0},"isTokenExpired"),lo=c(()=>{K.delete("accessToken"),K.delete("refreshToken"),K.delete("expiresAt"),K.delete("apiUri")},"clearTokens"),Se=c(()=>K.get("apiUri"),"getApiUri"),uo=c(r=>{K.set("apiUri",r)},"setApiUri"),gt=c(()=>!!(K.get("accessToken")||K.get("pat")),"isAuthenticated");class qe{static{c(this,"AccountApiClient")}refreshPromise=null;explicitBaseUrl;constructor(e){this.explicitBaseUrl=e}get apiBaseUrl(){return this.explicitBaseUrl||Se()||ne}async refreshAccessToken(){const e=Ce();if(!e)throw new Error("No refresh token available. Please run `membrane login`.");const t=await fetch(`${this.apiBaseUrl}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"refresh_token",refresh_token:e})});if(!t.ok)throw new Error("Failed to refresh access token. Please run `membrane login` again.");const n=await t.json(),o=n.refresh_token??e;return hn(n.access_token,o,n.expires_in),n.access_token}async getAuthToken(){const e=co();if(e&&!mt())return e;if(Ce())return this.refreshAccessToken();const t=dn();if(t)return t;throw new Error("Not authenticated. Run `membrane login` or set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET.")}async request(e,t={}){const n=t.headers?t.headers instanceof Headers?new Headers(t.headers):new Headers(t.headers):new Headers;if(!n.has("Authorization")){const s=await this.getAuthToken();n.set("Authorization",`Bearer ${s}`)}t.body&&!n.has("Content-Type")&&n.set("Content-Type","application/json");let o=await fetch(`${this.apiBaseUrl}${e}`,{...t,headers:n});if(o.status===401&&Ce()){const s=await(this.refreshPromise??=this.refreshAccessToken().finally(()=>{this.refreshPromise=null}));n.set("Authorization",`Bearer ${s}`),o=await fetch(`${this.apiBaseUrl}${e}`,{...t,headers:n})}if(!o.ok){const s=await o.text();throw new Ke(o.status,s)}if(o.status===204)return{};const i=await o.text();return JSON.parse(i)}get(e){return this.request(e,{method:"GET"})}post(e,t){return this.request(e,{method:"POST",body:JSON.stringify(t)})}put(e,t){return this.request(e,{method:"PUT",body:JSON.stringify(t)})}patch(e,t){return this.request(e,{method:"PATCH",body:JSON.stringify(t)})}delete(e){return this.request(e,{method:"DELETE"})}}const ho=c(r=>{const e=new qe(r);return t=>e.get(t)},"createAccountApiFetcher"),pn=new xr;class fn{static{c(this,"RequestLogger")}constructor(e=hr){this.axiosInstance=e}interceptorsConfigured=!1;setup(){if(this.interceptorsConfigured)return;const e=c(t=>(this.logError(t),Promise.reject(t)),"errorHandler");this.axiosInstance.interceptors.request.use(t=>{const n=pn.getStore();t.metadata={startTime:Date.now(),skipErrorLog:n};const{method:o,url:i}=this.getRequestDetails(t);return g.debug(`[Request]: ${o} ${i}`),t},e),this.axiosInstance.interceptors.response.use(t=>{const{method:n,url:o}=this.getRequestDetails(t.config),i=this.getDuration(t.config),{status:s,statusText:a}=t;return g.debug(`[Response]: ${n} ${o} - ${s} ${a} (${i}ms)`),t},e),this.interceptorsConfigured=!0}static withSkipErrorLog(e){return pn.run(!0,e)}getRequestDetails(e){const t=e?.method?.toUpperCase()??"UNKNOWN";if(!e?.url)return{method:t,url:"unknown"};if(e.url.startsWith("http"))return{method:t,url:e.url};const o=`${e.baseURL??""}/${e.url}`.replace(/\/+/g,"/").replace(/:\//,"://");return{method:t,url:o}}getDuration(e){return Date.now()-(e?.metadata?.startTime??Date.now())}logError(e){if(e.config?.metadata?.skipErrorLog)return;const{method:t,url:n}=this.getRequestDetails(e.config),o=this.getDuration(e.config);if(e.response){const{status:i,statusText:s}=e.response;g.error(`[Response]: ${t} ${n} - ${i} ${s} (${o}ms)`),e.response.data&&g.error(`[Response Data]: ${JSON.stringify(e.response.data,null,2)}`)}}}function po(r,e){return new Proxy(r,{get(t,n){const o=t[n];return typeof o=="function"?(...i)=>(e.lastCall={method:String(n),args:i},o.apply(t,i)):o}})}c(po,"createTrackedClient");class fo{static{c(this,"MembraneAPIManager")}client=null;currentConfig=null;tokenExpiry=0;requestTimes=[];maxRequestsPerSecond=80;windowSizeMs=1e3;semaphore=0;maxConcurrentRequests;maxRetries;retryDelay;requestTimeout;enableJitter;semaphoreQueue=[];rateLimitMutex=Promise.resolve();requestLogger=null;constructor(e={}){this.init(e)}init(e={}){this.windowSizeMs=e.windowSizeMs??1e3,this.maxRequestsPerSecond=e.maxRequestsPerSecond??80,this.maxConcurrentRequests=e.maxConcurrentRequests??Math.max(1,Math.min(Math.floor(this.maxRequestsPerSecond/4),20)),this.maxRetries=e.maxRetries??3,this.retryDelay=e.retryDelay??1e3,this.requestTimeout=e.requestTimeout??12e4,this.enableJitter=e.enableJitter!==!1,this.requestLogger=new fn,this.requestLogger.setup()}async withClient(e,t=!0,n=process.cwd()){const o={lastCall:null},i=c(async()=>{try{return await this.withRetry(async()=>{const s=await this.getClient(n),a=po(s,o);await this.acquireSemaphore();try{return await this.waitIfNeeded(),this.recordRequest(),await this.withTimeout(e(a),this.requestTimeout)}finally{this.releaseSemaphore()}},"API request")}catch(s){if(t){const a=o.lastCall?`Request: client.${o.lastCall.method}(${o.lastCall.args.map(l=>JSON.stringify(l)).join(", ")})`:null;throw s?.isMembraneError?(console.error(`
5
+ Membrane API Error:`),a&&console.error(a),console.error(JSON.stringify(z(s),null,2))):Sr(s)?(console.error(`
8
6
  HTTP Error:`),a&&console.error(a),console.error(JSON.stringify({message:s.message,status:s.response?.status,statusText:s.response?.statusText,responseData:s.response?.data},null,2))):(console.error(`
9
- Unexpected Error:`),a&&console.error(a),console.error(s)),s}return}},"executeRequest");return t?i():en.withSkipErrorLog(i)}async generateAccessToken(e,t){return tt.sign({name:"Membrane Agent",isAdmin:!0,exp:Math.floor(Date.now()/1e3)+3600,iss:e},t,{algorithm:"HS512"})}isTokenValid(){return Date.now()<this.tokenExpiry}getCurrentConfig(e){const t=Se(e);return!t?.workspaceKey||!t?.workspaceSecret?null:{workspaceKey:t.workspaceKey,workspaceSecret:t.workspaceSecret,apiUri:t.apiUri}}async createClient(e){const t=await this.generateAccessToken(e.workspaceKey,e.workspaceSecret);return this.tokenExpiry=Date.now()+36e5,this.currentConfig=e,new jt({token:t,apiUri:e.apiUri})}async getClient(e=process.cwd()){const t=this.getCurrentConfig(e);if(!t)throw new Error("Unable to create MembraneClient: No workspace configuration found.");const n=!this.currentConfig||this.currentConfig.workspaceKey!==t.workspaceKey||this.currentConfig.workspaceSecret!==t.workspaceSecret||this.currentConfig.apiUri!==t.apiUri,o=!this.isTokenValid()||this.tokenExpiry-Date.now()<3e5;return(!this.client||n||o)&&(this.client=await this.createClient(t)),this.client}clearClient(){this.client=null,this.currentConfig=null,this.tokenExpiry=0,this.requestTimes=[],this.semaphore=0,this.semaphoreQueue=[],this.rateLimitMutex=Promise.resolve()}getCurrentConfigInfo(){return this.currentConfig}async waitIfNeeded(){this.rateLimitMutex=this.rateLimitMutex.then(async()=>{for(this.cleanOldRequests();this.requestTimes.length>=this.maxRequestsPerSecond;){const t=this.requestTimes[0]+this.windowSizeMs-Date.now()+10;if(t>0)await new Promise(n=>setTimeout(n,t)),this.cleanOldRequests();else break}}),await this.rateLimitMutex}recordRequest(){this.requestTimes.push(Date.now())}cleanOldRequests(){const e=Date.now()-this.windowSizeMs;this.requestTimes=this.requestTimes.filter(t=>t>e)}getRequestCount(){return this.cleanOldRequests(),this.requestTimes.length}async acquireSemaphore(){if(this.semaphore<this.maxConcurrentRequests){this.semaphore++;return}return new Promise(e=>{this.semaphoreQueue.push(e)})}releaseSemaphore(){if(this.semaphore--,this.semaphoreQueue.length>0){const e=this.semaphoreQueue.shift();this.semaphore++,e()}}async withRetry(e,t="operation"){let n;for(let o=0;o<=this.maxRetries;o++)try{return await e()}catch(i){if(n=i,!this.isRetryableError(i)||o===this.maxRetries)throw i;const s=new Date().toISOString(),a=o+2;m.debug(`[${s}] Retrying ${t} (attempt ${a}/${this.maxRetries+1})`);let l=this.retryDelay*Math.pow(2,o);if(this.enableJitter){const d=l*.25*(Math.random()*2-1);l=Math.max(0,l+d)}await new Promise(d=>setTimeout(d,l))}throw n||new Error("Unknown error occurred during retry attempts")}isRetryableError(e){if(!e)return!1;const t=(e.message||e.toString()||"").toLowerCase(),n=e.code;return n&&["econnrefused","enotfound","econnreset","etimedout","ehostunreach","enetunreach"].includes(n.toLowerCase())?!0:["econnrefused","connection refused","network error","timeout","socket hang up","request timeout","dns lookup failed"].some(s=>t.includes(s))}async withTimeout(e,t){let n;const o=new Promise((i,s)=>{n=setTimeout(()=>s(new Error(`Request timeout after ${t}ms`)),t)});try{const i=await Promise.race([e,o]);return n&&clearTimeout(n),i}catch(i){throw n&&clearTimeout(n),i}}async getVersion(){try{return(await this.withClient(t=>t.get("version"),!1))?.version??null}catch{return null}}async isVersionAtLeast(e){const t=await this.getVersion(),n=t?nt.coerce(t):null,o=nt.coerce(e);return!!(n&&o&&nt.gte(n,o))}}const $=new oo;async function tn(r){const e=await $.withClient(t=>t.get("org-workspace-id"),!0,r);if(!e)throw new Error("Failed to get workspace ID");return e.id}c(tn,"getWorkspaceId");const Q={UPDATE:"update",DELETE:"delete",CREATE:"create"},B={INCOMING:"incoming",OUTGOING:"outgoing"},V={[x.Integration]:{element:"integration",elements:"integrations",exportable:!1,exportCleanup:c(r=>({id:r.id,key:r.key,name:r.name,connectorId:r.connectorId,baseUri:r.baseUri,connectorVersion:r.connectorVersion}),"exportCleanup")},[x.Connector]:{element:"connector",elements:"connectors",exportable:!1},[x.Action]:{element:"action",elements:"actions",integrationSpecific:!0,exportCleanup:c(r=>(delete r.integration,r),"exportCleanup")},[x.AppDataSchema]:{element:"appDataSchema",elements:"appDataSchemas"},[x.AppEventType]:{element:"appEventType",elements:"appEventTypes"},[x.DataLinkTable]:{element:"dataLinkTable",elements:"dataLinkTables"},[x.DataSource]:{element:"dataSource",elements:"dataSources",parentKey:"universalDataSourceId",integrationSpecific:!0},[x.FieldMapping]:{element:"fieldMapping",elements:"fieldMappings",integrationSpecific:!0,parentKey:"universalFieldMappingId",exportCleanup:c(r=>(delete r.dataSourceId,r),"exportCleanup")},[x.Flow]:{element:"flow",elements:"flows",integrationSpecific:!0,parentKey:"universalFlowId"},[x.Package]:{element:"package",elements:"packages",integrationSpecific:!0}},io=["id","workspaceId","integrationId","createdAt","updatedAt","revision","archivedAt","baseUri","state","appliedToIntegrations","dependencies"],nn=[x.Action,x.FieldMapping,x.Flow,x.DataSource,x.Package];class F{static{c(this,"Element")}type;key;integrationKey;data;constructor(e,t,n,o){if(!o)throw new Error("Element must always contain data");if(!t)throw new Error("Element must have a key");this.type=e,this.key=t,this.data=o,this.integrationKey=n||F.extractIntegrationKey(o)}get id(){return F.makeId(this.type,this.key,this.integrationKey)}get dirPath(){const e=Z[this.type].apiPath;if(this.integrationKey){const t=Z[x.Integration].apiPath;return E.join(t,this.integrationKey,e,this.key)}return E.join(e,this.key)}get path(){return E.join(this.dirPath,"spec.yaml")}get relativePath(){return E.relative(q(process.cwd()),this.path)}get absolutePath(){return E.resolve(E.join(q(process.cwd()),this.path))}isEqual(e){if(this.id!==e.id)return!1;const t=this.clean(),n=e.clean();return fr(t,n)}hasParent(){if(this.data?.parentUuid)return!0;const e=this.getParentKey();return!!e&&!!this.data?.[e]}clean(){const e={...this.data};return io.forEach(t=>{delete e[t]}),Object.keys(e).forEach(t=>{t.match(/universal.*Revision/)&&delete e[t]}),e}getParentKey(){let e="parentId";return V?.[this.type]?.parentKey&&(e=V?.[this.type]?.parentKey),e}static new(e,t,n,o){return new F(e,t,n,o)}static fromData(e,t){if(!t?.key)throw new Error(`Element missing key: ${JSON.stringify(t)}`);const n=this.extractIntegrationKey(t);return new F(e,t.key,n,t)}static fromPathAndData(e,t){const n=F.parsePath(e);if(!t)return;const o=t?.key||n?.key;return n?F.new(n.type,o,n.integrationKey,t):void 0}static fromElement(e){return new F(e.type,e.key,e.integrationKey,{...e.data})}static idFromPath(e){const t=F.parsePath(e);if(t)return F.makeId(t.type,t.key,t.integrationKey)}static makeId(e,t,n){return e===x.Integration?`${e}:${t}`:F.isIntegrationSpecific(e)?`${n||"universal"}:${e}:${t}`:`${e}:${t}`}static parsePath(e){const t=this.getRelativePath(e);if(!this.isElementFile(t))return;const n=Object.values(Z).map(l=>l.apiPath).join("|"),o=new RegExp(`^(?<elementType>${n})/(?<elementKey>[^/]+)/spec\\.ya?ml$`),i=t.match(o);if(i?.groups){const{elementType:l,elementKey:d}=i.groups,p=this.getElementTypeFromPath(l);if(p)return{type:p,key:d}}const s=new RegExp(`^integrations/(?<integrationKey>[^/]+)/(?<elementType>${n})/(?<elementKey>[^/]+)/spec\\.ya?ml$`),a=t.match(s);if(a?.groups){const{integrationKey:l,elementType:d,elementKey:p}=a.groups,f=this.getElementTypeFromPath(d);if(f)return{type:f,key:p,integrationKey:l}}}static extractIntegrationKey(e){return e?.integrationKey||e?.integration?.key||e?.integration_key||void 0}static isElementFile(e){return e.endsWith(".yml")||e.endsWith(".yaml")}static getElementTypeFromPath(e){return Object.values(x).find(t=>Z[t].apiPath===e)}static getRelativePath(e){return E.relative(q(process.cwd()),e)}static isIntegrationSpecific(e){return nn.includes(e)}static canBeIntegrationSpecific(e){return nn.includes(e)}}class rn{static{c(this,"BaseWorkspaceElementsRepository")}constructor(e){this.cache=e}connectorsMapping;sourceCache;setConnectorsMapping(e){this.connectorsMapping=e}setSourceCache(e){this.sourceCache=e}async getElements(){const e=[];try{const t=await this.getIntegrations();e.push(...t);const n=[x.Integration,x.Connector],o=Object.keys(V),i=await Promise.all(o.filter(s=>!n.includes(s)).map(async s=>this.getElementsByType(s,t)));for(const s of i)e.push(...s)}catch(t){throw m.error(`Failed to get elements: ${t}`),t}return e}async putElement(e){let t=this.cache.get(e.id);return t&&!e.data.id&&t.data.id&&(e.data.id=t.data.id),!t&&e.data.id&&(t=await this.getByInternalId(e.data.id,e.type,!1)),e.data.id&&t?t=await this.updateElement(e,t,{elements:this.cache.getAll(),connectorsMapping:this.connectorsMapping,sourceElements:this.sourceCache?.getAll()}):t=await this.createElement(e,{elements:this.cache.getAll(),connectorsMapping:this.connectorsMapping,sourceElements:this.sourceCache?.getAll()}),t}async getElementsByType(e,t){return[]}async createElement(e,t){return e}async updateElement(e,t,n){return t}async getElement(e){}async getIntegrations(){return[]}async getByInternalId(e,t,n=!0){}async deleteElement(e,t){}}function ft(r,e=!0){if(v.existsSync(r))try{const t=v.readFileSync(r,"utf8");return H.load(t)||void 0}catch(t){if(!e)return;if(t instanceof Error){const n=E.relative(process.cwd(),r);throw new Error(`Failed to parse YAML file "${n}": ${t.message}`)}throw t}}c(ft,"readYaml$1");function so(r,e,t){try{const n=H.dump(e,t);v.writeFileSync(r,n,"utf8")}catch(n){if(n instanceof Error){const o=E.relative(process.cwd(),r);throw new Error(`Failed to write YAML file "${o}": ${n.message}`)}throw n}}c(so,"writeYaml$1");class ao extends rn{static{c(this,"LocalWorkspaceElementsRepository")}basePath;constructor(e){super(e),this.basePath=q(process.cwd())}async getElementsByType(e,t){const n=[],o=E.join(this.basePath,Z[e].apiPath),i=await this.readElementsInDir(o);n.push(...i);for(const s of t){const a=E.join(this.basePath,Z[x.Integration].apiPath,s.key,Z[e].apiPath),l=await this.readElementsInDir(a);n.push(...l)}return n.length>0&&m.debug(`[local] Fetched ${n.length} ${e}${n.length!==1?"s":""}`),n}async getElement(e){return this.readElement(e.path)}async createElement(e){return this.updateElement(e,e)}async updateElement(e,t){if(!e.data)throw new Error("Element must have data to write");const n=E.join(this.basePath,t.dirPath),o=E.join(this.basePath,t.path);return v.existsSync(n)||v.mkdirSync(n,{recursive:!0}),so(o,e.data),m.debug(`[local] Written ${t.relativePath}`),t}async deleteElement(e){const t=E.join(this.basePath,e.path),n=E.join(this.basePath,e.dirPath);v.existsSync(t)&&v.rmSync(t,{force:!0}),this.pruneEmptyDir(n),m.debug(`[local] Deleted ${e.relativePath}`)}async getIntegrations(){const e=E.join(this.basePath,Z[x.Integration].apiPath);return this.readElementsInDir(e)}async readElement(e){const t=ft(e);if(t)return F.fromPathAndData(e,t)}async readElementsInDir(e){const t=[];if(!v.existsSync(e))return t;const n=v.readdirSync(e);if(n.length===0)return t;const o=n.map(async s=>{const a=E.join(e,s,"spec.yaml");return this.readElement(a)});return(await Promise.all(o)).filter(s=>s!=null)}pruneEmptyDir(e){try{if(!e.startsWith(this.basePath)||e===this.basePath||!v.existsSync(e)||v.readdirSync(e).length>0)return;v.rmdirSync(e),this.pruneEmptyDir(E.dirname(e))}catch(t){console.warn(`Failed to prune empty directory ${e}:`,t)}}}class co extends rn{static{c(this,"RemoteWorkspaceElementsRepository")}async getElementsByType(e,t){const n=F.canBeIntegrationSpecific(e),o=await this.findAll(e,n?{layer:"universal"}:{});if(!F.canBeIntegrationSpecific(e))return o;for(const i of t){const s=i.key,a=i.data.id,l=await this.findAll(e,a?{integrationId:a}:{integrationKey:s},s);o.push(...l)}return o.length>0&&m.debug(`[remote] Fetched ${o.length} ${e}${o.length!==1?"s":""}`),o}async getElement(e){const t=await $.withClient(n=>n[V[e.type].element](e.data.id).get());return F.fromData(e.type,t)}async createElement(e,t){const n=e.clean();if(this.transformElementForCreate(e,n,t),e.hasParent()&&n.integrationId&&!n.isCustomized){const s=e.getParentKey(),a=new Set(["uuid","integrationId",s,"meta","integrationUuid","parentUuid"]);Object.keys(n).forEach(l=>{a.has(l)||delete n[l]})}const o=await $.withClient(s=>s[V[e.type].elements].create(n)),i=F.fromData(e.type,o);return m.debug(`[remote] Created ${i.id}`),i}async updateElement(e,t,n){if(!e.data.id)throw new Error("Element must have an id to update");t.data.archivedAt&&await $.withClient(a=>a[V[e.type].element](t.data.id).restore());const o=e.clean();if(this.transformElementForUpdate(e,o,n),e.hasParent()&&o.integrationId&&!o.isCustomized)return m.debug(`[remote] Skipped update for ${t.id} (non-customized)`),t;const i=await $.withClient(a=>a[V[e.type].element](t.data.id).put(o)),s=F.fromData(t.type,i);return m.debug(`[remote] Updated ${t.id}`),s}async deleteElement(e){if(!e.data.id)throw new Error("Element must have an id to delete");await $.withClient(t=>t[V[e.type].element](e.data.id).archive()),m.debug(`[remote] Deleted ${e.id}`)}async getIntegrations(){const e=await $.withClient(n=>n.integrations.findAll());if(!e)return[];const t=e.map(n=>F.fromData(x.Integration,n));return t.length>0&&m.debug(`[remote] Fetched ${t.length} integrations`),t}async getByInternalId(e,t){let n;if(n=await $.withClient(o=>o[V[t].element](e).get(),!1),!!n)return F.fromData(t,n)}transformElementForCreate(e,t,n){if(e.integrationKey){const o=n.elements.find(i=>i.type===x.Integration&&i.key===e.integrationKey);if(o)t.integrationId=o.data.id;else throw new Error(`Dependency integration ${e.integrationKey} not found for ${e.id}`)}if(e.type===x.Integration){const o=n.connectorsMapping?.[t.connectorId];o&&(t.connectorId=o)}this.transformPackageDependencies(e,t,n),this.transformParentDependency(e,t,n)}transformElementForUpdate(e,t,n){if(e.type===x.Integration){const o=n.connectorsMapping?.[t.connectorId];o&&(t.connectorId=o)}if(e.integrationKey){t.integrationKey=e.integrationKey;const o=n.elements.find(i=>i.type===x.Integration&&i.key===e.integrationKey);if(o)t.integrationId=o.data.id;else throw new Error(`Dependency integration ${e.integrationKey} not found for ${e.id}`)}this.transformPackageDependencies(e,t,n),this.transformParentDependency(e,t,n)}transformPackageDependencies(e,t,n){if(e.type!==x.Package||!t.elements)return;const o=t.elements.map(i=>{const a=(n.sourceElements||n.elements).find(d=>d.data.id===i.id&&d.type===i.type);if(!a)throw new Error(`Package element ${i.type} with id ${i.id} not found in source workspace for package ${e.id}`);const l=n.elements.find(d=>d.type===i.type&&d.key===a.key&&d.integrationKey===a.integrationKey);if(!l)throw new Error(`Package element ${i.type} with key ${a.key} not found in target workspace for package ${e.id}`);return{id:l.data.id,type:i.type}});t.elements=o}transformParentDependency(e,t,n){if(!e.hasParent())return;const o=e.getParentKey();if(!o)return;const i=n.elements.find(s=>s.type===e.type&&s.key===e.key&&!s.hasParent());if(i)t[o]=i.data.id;else throw new Error(`Parent ${e.getParentKey()} not found for ${e.id}`)}async findAll(e,t={},n){const o=await $.withClient(i=>i[V[e].elements].findAll(t));return o?o.filter(i=>i.key).map(i=>(n&&!i.integrationKey&&(i.integrationKey=n),F.fromData(e,i))):[]}}class mt extends Cr{static{c(this,"TypedEventEmitter")}on(e,t){return super.on(e,t)}emit(e,...t){return super.emit(e,...t)}off(e,t){return super.off(e,t)}once(e,t){return super.once(e,t)}}const W={Updated:"updated",Deleted:"deleted",Stopped:"stopped"},lo={ignored:to,persistent:!0,ignoreInitial:!0,followSymlinks:!1,depth:20,awaitWriteFinish:{stabilityThreshold:500,pollInterval:100},ignorePermissionErrors:!0,atomic:!0,usePolling:!1,alwaysStat:!1,interval:1e3,binaryInterval:300};class uo extends mt{static{c(this,"LocalElementWatcher")}constructor(e){super(),this.options=e,this.membraneDir=q(this.options.cwd),this.lockTimeoutMs=this.options.lockTimeoutMs??1e3}isWatching=!1;watcher;membraneDir;contentCache={};ignoredPaths=new Set;lockTimeoutMs;async start(){this.isWatching||(v.existsSync(this.membraneDir)||v.mkdirSync(this.membraneDir,{recursive:!0}),this.initializeContentCache(),this.watcher=yr.watch(this.membraneDir,lo),this.watcher.on("add",e=>this.handleFileSystemEvent(W.Updated,e)).on("change",e=>this.handleFileSystemEvent(W.Updated,e)).on("unlink",e=>this.handleFileSystemEvent(W.Deleted,e)).on("ready",()=>this.isWatching=!0))}async stop(){!this.isWatching||!this.watcher||(await this.watcher.close(),this.isWatching=!1,this.watcher=void 0,this.clearCache(),this.clearAllLocks(),this.emit(W.Stopped))}getCwd(){return this.options.cwd}async executeWithPathLock(e,t){const n=E.resolve(e);this.ignoredPaths.add(n);try{await t()}finally{setTimeout(()=>{this.ignoredPaths.delete(n)},this.lockTimeoutMs)}}isPathLocked(e){const t=E.resolve(e);if(this.ignoredPaths.has(t))return!0;for(const n of this.ignoredPaths)if(t.startsWith(n+E.sep))return!0;return!1}clearAllLocks(){this.ignoredPaths.clear()}handleFileSystemEvent(e,t){const n=E.relative(this.membraneDir,t);if(this.isPathLocked(t))return;if(e===W.Deleted){this.removeFromCache(n);const a={filePath:t,relativePath:n,data:void 0};m.info(`[local] ${e}: ${a.relativePath}`),this.emit(e,a);return}const o=this.readFileContent(t);if(!this.hasContentChanged(n,o))return;const s=this.processFileEvent(t,o);this.updateCache(n,o),s&&(m.info(`[local] ${e}: ${s.relativePath}`),this.emit(e,s))}readFileContent(e){return v.readFileSync(e,"utf8")}processFileEvent(e,t){const n=E.relative(this.membraneDir,e);let o;try{o=t?wr.parse(t):void 0}catch{o=void 0}return{filePath:e,relativePath:n,data:o}}hasContentChanged(e,t){if(!t)return this.contentCache[e]!==void 0;const n=this.getContentHash(t);return this.contentCache[e]!==n}getContentHash(e){return mr.createHash("sha256").update(e).digest("hex")}updateCache(e,t){if(!t){delete this.contentCache[e];return}this.contentCache[e]=this.getContentHash(t)}removeFromCache(e){delete this.contentCache[e]}clearCache(){Object.keys(this.contentCache).forEach(e=>{delete this.contentCache[e]})}initializeContentCache(){v.existsSync(this.membraneDir)&&this.scanDirectoryForCache(this.membraneDir)}scanDirectoryForCache(e){const t=v.readdirSync(e,{withFileTypes:!0});for(const n of t){const o=E.join(e,n.name);if(n.isDirectory())this.scanDirectoryForCache(o);else if(n.isFile())try{const i=v.readFileSync(o,"utf8"),s=E.relative(this.membraneDir,o);this.updateCache(s,i)}catch{}}}}var gt=(r=>(r.Updated="updated",r.ConnectorFileUpdated="connector-file-updated",r.Connected="connected",r.Disconnected="disconnected",r.Error="error",r))(gt||{});const ho={debounceMs:2e3,reconnectIntervalMs:5e3,maxReconnectAttempts:1/0,maxBackoffMs:6e4,connectionTimeoutMs:3e5};class po extends mt{static{c(this,"RemoteElementWatcher")}constructor(e=ho){super(),this.config=e}eventSource;debounceTimeouts=new Map;reconnectAttempts=0;reconnectTimeout;connectionTimeout;isStarted=!1;isConnected=!1;async start(){this.isStarted||(this.isStarted=!0,await this.connect())}async stop(){this.isStarted&&(this.isStarted=!1,this.isConnected=!1,this.clearReconnectTimeout(),this.clearConnectionTimeout(),this.clearAllDebounceTimeouts(),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0))}async connect(){try{m.debug("[remote-events] Connecting to server"),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0);const e=await $.getClient(process.cwd()),t=await e.getToken();if(!t)throw new Error("No auth token available");const n=`${e.apiUri}/sse/workspace?token=${encodeURIComponent(t)}`;m.debug("[remote-events] Subscribing to workspace events"),this.eventSource=new Sr(n),this.setupEventSourceHandlers()}catch(e){m.debug(`[remote-events] Failed to connect: ${e}`),this.emit("error",{error:e}),this.scheduleReconnect()}}setupEventSourceHandlers(){this.eventSource&&(this.eventSource.onopen=()=>{m.debug("[remote-events] Connected to server"),this.reconnectAttempts=0,this.isConnected=!0,this.resetConnectionTimeout(),this.emit("connected",{})},this.eventSource.onmessage=e=>{try{this.resetConnectionTimeout();const t=JSON.parse(e.data);this.handleElementUpdate(t)}catch(t){m.debug(`[remote-events] Failed to parse workspace event: ${t}`)}},this.eventSource.onerror=e=>{m.debug(`[remote-events] Connection error: ${JSON.stringify(e,null,2)}`),this.isConnected=!1,this.clearConnectionTimeout(),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.emit("disconnected",{}),this.isStarted&&this.scheduleReconnect()})}handleElementUpdate(e){if(e.type!==zn.ElementUpdate)return;const{elementId:t,elementType:n,data:o={}}=e;if(!(!t||!n)){if(n===x.Connector){const{filePath:i,eventType:s,newPath:a}=o;return m.debug(`[remote-watcher] Received connector event - elementId: ${t}, filePath: ${i}, eventType: ${s}`),this.scheduleConnectorFileUpdate(t,i,s,a)}return this.scheduleElementUpdate(t,n)}}scheduleElementUpdate(e,t){const n=this.debounceTimeouts.get(e);n&&clearTimeout(n);const o=setTimeout(()=>{this.debounceTimeouts.delete(e),this.emit("updated",{elementId:e,elementType:t})},this.config.debounceMs);this.debounceTimeouts.set(e,o)}scheduleConnectorFileUpdate(e,t,n,o){const i=JSON.stringify({connectorId:e,filePath:t}),s=this.debounceTimeouts.get(i);s&&clearTimeout(s);const a=setTimeout(()=>{this.debounceTimeouts.delete(i),this.emit("connector-file-updated",{connectorId:e,filePath:t,eventType:n,newPath:o})},this.config.debounceMs);this.debounceTimeouts.set(i,a)}scheduleReconnect(){if(!this.isStarted||this.reconnectAttempts>=this.config.maxReconnectAttempts){this.reconnectAttempts>=this.config.maxReconnectAttempts&&(m.error("[remote-events] Max reconnection attempts reached. Connection will not be retried."),this.emit("error",{error:new Error("Max reconnection attempts reached")}));return}this.reconnectTimeout&&clearTimeout(this.reconnectTimeout),this.reconnectAttempts++;const e=this.config.reconnectIntervalMs*Math.pow(2,this.reconnectAttempts-1),t=Math.random()*.3*e,n=Math.min(e+t,this.config.maxBackoffMs);m.debug(`[remote-events] Reconnecting in ${Math.round(n)}ms (attempt ${this.reconnectAttempts})`),this.reconnectTimeout=setTimeout(()=>{this.connect()},n)}clearReconnectTimeout(){this.reconnectTimeout&&(clearTimeout(this.reconnectTimeout),this.reconnectTimeout=void 0)}resetConnectionTimeout(){this.clearConnectionTimeout(),this.connectionTimeout=setTimeout(()=>{this.isConnected&&this.isStarted&&(m.debug("[remote-events] Connection timeout detected, reconnecting..."),this.isConnected=!1,this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.scheduleReconnect())},this.config.connectionTimeoutMs)}clearConnectionTimeout(){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=void 0)}clearAllDebounceTimeouts(){this.debounceTimeouts.forEach(e=>clearTimeout(e)),this.debounceTimeouts.clear()}}class on{static{c(this,"ElementsCache")}elements=new Map;typeIndex=new Map;internalIdIndex=new Map;constructor(e){e&&this.addAll(e)}add(e){const t=e.id;this.elements.set(t,e),this.typeIndex.has(e.type)||this.typeIndex.set(e.type,new Set),this.typeIndex.get(e.type).add(t),e.data?.id&&this.internalIdIndex.set(e.data.id,t)}remove(e){const t=e.id,n=this.elements.delete(t);if(n){const o=this.typeIndex.get(e.type);o&&(o.delete(t),o.size===0&&this.typeIndex.delete(e.type)),e.data?.id&&this.internalIdIndex.delete(e.data.id)}return n}put(e){this.elements.has(e.id)&&this.remove(e),this.add(e)}get(e){return this.elements.get(e)}getByInternalId(e){const t=this.internalIdIndex.get(e);if(t)return this.elements.get(t)}getElementsByType(e){const t=this.typeIndex.get(e);return t?Array.from(t).map(n=>this.elements.get(n)).filter(n=>n!==void 0):[]}getAll(){return Array.from(this.elements.values())??[]}getTypes(){return Array.from(this.typeIndex.keys())}addAll(e){for(const t of e)t&&this.add(t)}removeAll(e){for(const t of e)this.remove(t)}getElementIdsByType(e){return this.typeIndex.get(e)}clear(){this.elements.clear(),this.typeIndex.clear(),this.internalIdIndex.clear()}getAllIds(){return new Set(this.elements.keys())}}const me="connectors",ee="development",yt={};async function sn(r={}){const{onProgress:e}=r,t=new Set,o=(await $.withClient(d=>d.get("org-workspace-id"))).id,i={};m.info("[connectors] Loading custom connectors"),await $.withClient(d=>d.get(`/connectors?workspaceId=${o}`)),m.info("[connectors] Loading public connectors");const s=ne(),l=(A.existsSync(s)?A.readdirSync(s):[]).filter(d=>{if(d.startsWith("."))return!1;const p=k.join(s,d);try{return A.statSync(p).isDirectory()}catch{return!1}});for(const d of l){m.info(`[connectors] Loading connector from: ${d}`);const p=A.readdirSync(k.join(s,d)),f=await wt(d);if(!f)continue;e?.("pushing",f.name),"baseUri"in f&&delete f.baseUri;let h;f.uuid&&(h=await $.withClient(S=>S.get(`/connectors/${f.uuid}`),!1));const C=f.uuid;if(h)i[C]=h.id,m.info(`[connectors] Matched ${f.name} uuid: ${f.uuid}`),h.isPublic||(h.archivedAt&&(m.info(`[connectors] Restoring archived connector ${f.name}`),await $.withClient(S=>S.post(`connectors/${h.id}/restore`))),m.info(`[connectors] Updating connector ${f.name}`),await $.withClient(S=>S.patch(`connectors/${h.id}`,{...f,workspaceId:o})));else if(!i[C]&&!h?.isPublic){let S=!1;try{const D=await Ct({connectorId:C});D&&D.isPublic&&(S=!0)}catch{}if(!S){m.info(`[connectors] Creating custom connector ${f.name} (${f.key})`);const D=await $.withClient(O=>O.post("connectors",{...f,workspaceId:o}));i[C]=D.id}}const T=p.filter(S=>A.statSync(k.join(s,d,S)).isDirectory());for(const S of T)await go({connector:f,version:S,connectorId:i[C]}),t.add(C);e?.("pushed",f.name)}return{connectorsMapping:i,pushedConnectors:Array.from(t)}}c(sn,"pushConnectors");async function an({connectorId:r,connectorVersion:e,allConnectors:t,pulledConnectors:n,pulledConnectorVersions:o}){if(!r||o[r]?.has(e))return;const i=Vt(),s=await Ct({connectorId:r});if(!s.isPublic||t){if(!s?.key){console.error(`[connectors] Connector ${r} has no key. Skipping..`),m.error(`[connectors] Connector ${r} has no key. Skipping..`);return}n.has(r)||(await yo({basePath:i,connector:s}),n.add(r)),o[r]||(o[r]=new Set),o[r].has(e)||(await wo({connector:s,connectorVersion:e,basePath:i}),o[r].add(e))}}c(an,"pullRemoteConnector");function ne(){const r=ht();return k.join(r.membraneDirPath,me)}c(ne,"getConnectorsPath");async function wt(r){const e=k.join(ne(),r,`${r}.yml`);return ft(e,!1)}c(wt,"readConnector");async function fo(r,e){return m.info(`[connectors] Zipping ${r} into ${e}`),new Promise((t,n)=>{const o=A.createWriteStream(e),i=rt("zip",{zlib:{level:9}});o.on("close",()=>{m.success(`[connectors] Successfully created ${e}`),t()}),o.on("end",()=>{m.info("[connectors] Data has been drained")}),i.on("warning",a=>{a.code==="ENOENT"?console.warn(a):n(a)}),i.on("error",a=>{n(a)}),i.pipe(o);const s=A.readdirSync(r);for(const a of s){const l=k.join(r,a),d=A.statSync(l);d.isFile()?i.file(l,{name:a}):d.isDirectory()&&i.directory(l,a)}i.finalize()})}c(fo,"createZipArchive");async function mo(r,e){return m.info(`[connectors] Unzipping into ${e}`),new Promise((t,n)=>{const o=Pe.Parse();o.on("entry",i=>{const s=i.path;if(i.type==="Directory"){const l=k.join(e,s);A.mkdirSync(l,{recursive:!0}),i.autodrain()}else{const l=k.join(e,s),d=k.dirname(l);A.mkdirSync(d,{recursive:!0});const p=A.createWriteStream(l);i.pipe(p),p.on("finish",()=>{})}}),o.on("end",()=>{m.success(`[connectors] Successfully extracted to ${e}`),t()}),o.on("error",i=>{n(i)}),o.write(r),o.end()})}c(mo,"extractZipArchive");async function go({connector:r,version:e,connectorId:t}){const n=k.join(ne(),te(r),cn(e)),o=k.join(n,"src"),i=k.join(n,"src.zip"),s=A.existsSync(i);if(A.existsSync(o)&&(m.info(`[connectors] Archiving source code for ${r.name} version ${e}`),await fo(o,i)),!A.existsSync(i)){m.warning(`[connectors] No source code found for ${r.name} version ${e}`);return}try{const a=new Jt;if(a.append("file",A.createReadStream(i),"file.zip"),m.info(`[connectors] Pushing connector version ${e} for ${r.name}`),e==ee)m.info(`[connectors] Uploading connector ${t}`),await $.withClient(l=>l.post(`connectors/${t}/upload`,a,{headers:{...a.getHeaders()}}));else{if(a.append("version",e),a.append("changelog","Imported Version"),(await $.withClient(d=>d.get(`/connectors/${t}/versions`))).find(d=>d.version==e)){m.info(`[connectors] Version ${e} already published`);return}m.info(`[connectors] Publishing version ${e} of connector ${t}`),await $.withClient(d=>d.post(`connectors/${t}/publish-version`,a,{headers:{...a.getHeaders()}}))}m.success(`Successfully pushed connector version ${e} for ${r.name}`)}catch(a){m.error(`Error pushing connector version ${e} for ${r.name}: ${a}`),console.error(`[connectors] Error pushing connector version ${e} for ${r.name}: ${a}`)}finally{!s&&A.existsSync(i)&&(m.info(`[connectors] Cleaning up temporary zip file for ${r.name} version ${e}`),A.unlinkSync(i))}}c(go,"pushConnectorVersion");async function Ct({connectorId:r}){if(r){if(yt[r])return yt[r];try{const e=await $.withClient(t=>t.get(`connectors/${r}`),!1);return yt[r]=e,e}catch(e){return m.error(`[connectors] Failed to get connector ${r}: ${e}`),console.error(`[connectors] Failed to get connector ${r}: ${e}`),null}}}c(Ct,"getConnector");async function yo({basePath:r,connector:e}){const t=te(e),n=k.join(r,me,t);A.mkdirSync(n,{recursive:!0});const o=k.join(n,`${te(e)}.yml`);A.writeFileSync(o,H.dump(e)),m.info(`[connectors] Pulled connector ${e.name}`)}c(yo,"pullConnector$1");async function wo({connector:r,connectorVersion:e,basePath:t}){const n=te(r),o=cn(e),i=k.join(t,"connectors",n,o),s=await $.withClient(l=>l.get(`connectors/${r.id}/download`,{version:e},{responseType:"arraybuffer",headers:{Accept:"application/zip"},timeout:1e6}));A.mkdirSync(i,{recursive:!0});const a=k.join(i,"src.zip");if(A.writeFileSync(a,s),!e){const l=k.join(i,"src");A.mkdirSync(l,{recursive:!0}),await mo(s,l)}m.info(`[connectors] Pulled connector version: ${r.name} (${o})`)}c(wo,"pullConnectorVersion");function te(r){return r.key}c(te,"getConnectorDirName");function cn(r){return r??ee}c(cn,"getConnectorVersionDirName");function Co(r){const e=Vt(),t=te(r);return k.join(e,me,t)}c(Co,"getConnectorDirPath");function So(r){return r.match(`${me}/[^/]+/${ee}/src/.*`)!==null}c(So,"isConnectorSourceFile");async function bo(r){const e=r.match(`${me}/([^/]+)/${ee}/src/(.*)`);if(!e)return;const t=e[1],n=e[2],o=await wt(t);if(!o){m.warning(`[connectors] Connector ${t} not found. Ignoring file change`);return}const i=o.id,s=A.readFileSync(r,"utf-8");await $.withClient(a=>a.put(`connectors/${i}/files/${n}`,s,{headers:{"Content-Type":"text/plain"}})),m.info(`[connectors] Pushed file ${n} for connector ${o.name}`)}c(bo,"putConnectorFile");async function vo(r){const e=r.match(`${me}/([^/]+)/${ee}/src/(.*)`);if(!e)return;const t=e[1],n=e[2],o=await wt(t);if(!o){m.warning(`[connectors] Connector ${t} not found. Ignoring file change`);return}const i=o.id;await $.withClient(s=>s.delete(`connectors/${i}/files/${n}`)),m.info(`[connectors] Deleted file ${n} for connector ${o.name}`)}c(vo,"deleteConnectorFile");async function Eo(r,e){try{const t=await $.withClient(s=>s.get(`connectors/${r}`));if(!t){m.warning(`[connectors] Connector ${r} not found`);return}const n=await $.withClient(s=>s.get(`connectors/${r}/files/${e}`)),o=te(t),i=k.join(ne(),o,ee,"src",e);A.mkdirSync(k.dirname(i),{recursive:!0}),n!=null?(A.writeFileSync(i,n),m.info(`[connectors] Pulled file ${e} for connector ${t.name}`)):A.existsSync(i)&&(A.unlinkSync(i),m.info(`[connectors] Deleted file ${e} for connector ${t.name}`))}catch(t){m.error(`[connectors] Failed to pull connector file ${e} for connector ${r}: ${t}`),console.error(`[connectors] Failed to pull connector file ${e} for connector ${r}: ${t}`)}}c(Eo,"pullConnectorFile");async function To(r,e){const t=await $.withClient(i=>i.get(`connectors/${r}`),!1);if(!t){m.warning(`[connectors] Connector ${r} not found`);return}const n=te(t),o=k.join(ne(),n,ee,"src",e);A.existsSync(o)&&(A.unlinkSync(o),m.info(`[connectors] Deleted file ${e} for connector ${n}`))}c(To,"deleteLocalConnectorFile");async function xo(r,e,t){if(t&&e!==t)try{const n=await $.withClient(l=>l.get(`connectors/${r}`),!1);if(!n){m.warning(`[connectors] Connector ${r} not found`);return}const o=te(n),i=k.join(ne(),o,ee,"src"),s=k.join(i,e),a=k.join(i,t);A.existsSync(s)&&(A.mkdirSync(k.dirname(a),{recursive:!0}),A.renameSync(s,a),m.info(`[connectors] Renamed directory from ${e} to ${t} for connector ${o}`))}catch(n){m.error(`[connectors] Failed to rename directory ${e} to ${t} for connector ${r}: ${n}`)}}c(xo,"renameLocalConnectorDirectory");async function Io(r,e){try{const t=await $.withClient(i=>i.get(`connectors/${r}`),!1);if(!t){m.warning(`[connectors] Connector ${r} not found`);return}const n=te(t),o=k.join(ne(),n,ee,"src",e);if(A.existsSync(o)){const i=k.resolve(ne());if(!k.resolve(o).startsWith(i))return;A.rmSync(o,{recursive:!0,force:!0}),m.info(`[connectors] Deleted directory ${e} for connector ${n}`)}}catch(t){m.error(`[connectors] Failed to delete directory ${e} for connector ${r}: ${t}`)}}c(Io,"deleteLocalConnectorDirectory");const j={LogAdded:"logAdded",StateChanged:"stateChanged",StatsChanged:"statsChanged",ConflictsChanged:"conflictsChanged",McpStatusChanged:"mcpStatusChanged",McpServersChanged:"mcpServersChanged",ConfigChanged:"configChanged"};class ko extends mt{static{c(this,"WorkspaceNotifications")}constructor(e){super(),this.config=e,m.setNotificationHandler(this)}clientId;heartbeatInterval;isCleaningUp=!1;async connectToRemote(){await this.registerWithRemoteServer(),await this.startHeartbeatLoop()}async setState(e){this.emit(j.StateChanged,{state:e}),await this.emitRemote({status:e})}setConflicts(e){this.emit(j.ConflictsChanged,{conflicts:e})}setConfig(e){this.emit(j.ConfigChanged,{config:e})}setStats(e){this.emit(j.StatsChanged,{stats:e})}addLog(e){this.emit(j.LogAdded,{log:e})}setMcpStatus(e){this.emit(j.McpStatusChanged,{status:e})}async setMcpServers(e){this.emit(j.McpServersChanged,{servers:e}),await this.emitRemote({mcpServers:e.map(t=>({name:t.agentName,totalRequests:t.totalRequests}))})}async cleanup(){!this.clientId||this.isCleaningUp||(this.isCleaningUp=!0,this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=void 0),await this.withErrorHandling(async()=>{await $.withClient(e=>e.delete(`/local-clients/${this.clientId}`))}),this.clientId=void 0)}async registerWithRemoteServer(){if(this.clientId)return;const e=await this.withErrorHandling(async()=>await $.withClient(t=>t.post("/local-clients",{hostname:et.hostname(),workingDirectory:process.cwd()})));e?.id?this.clientId=e.id:this.addLog({timestamp:new Date().toISOString(),message:"Failed to register with remote server",type:"error"})}async startHeartbeatLoop(){this.heartbeatInterval=setInterval(async()=>{this.clientId&&await this.sendHeartbeat()},this.config.heartbeatIntervalMs)}async sendHeartbeat(){this.clientId&&await this.withErrorHandling(async()=>{await $.withClient(e=>e.post(`/local-clients/${this.clientId}/keep-alive`,{}))})}async emitRemote(e){this.clientId&&await this.withErrorHandling(async()=>{await $.withClient(t=>t.patch(`/local-clients/${this.clientId}`,e))})}async withErrorHandling(e){try{return await e()}catch(t){const n=t instanceof Error?t.message:String(t);return this.addLog({timestamp:new Date().toISOString(),message:`Failed to connect to remote: ${n}`,type:"error"}),null}}}const re=new ko({heartbeatIntervalMs:15e3}),ln=[x.AppDataSchema,x.AppEventType,x.DataLinkTable,x.DataSource,x.FieldMapping,x.Action,x.Flow,x.Package];class xe{static{c(this,"ElementSyncService")}localWatcher=void 0;remoteWatcher=void 0;notifier;changes=[];localCache;remoteCache;localRepo;remoteRepo;pulledConnectors=new Set;pulledConnectorVersions={};constructor(){this.notifier=re,this.localCache=new on,this.remoteCache=new on,this.localRepo=new ao(this.localCache),this.remoteRepo=new co(this.remoteCache)}clear(){this.changes=[]}needsForcedSync(){return this.changes.some(e=>e.isConflict)}needsSync(){return this.changes.length>0}async getStats(){const e=this.localCache.getAll(),t={};return e.forEach(n=>{t[n.type]=(t[n.type]||0)+1}),t}async fetchElements(){const e=await this.localRepo.getElements(),t=await this.remoteRepo.getElements();this.localCache.addAll(e),this.remoteCache.addAll(t)}async updateElement(e,t){const n=this.getHandler(t),o=this.getCache(t);try{const i=await n.putElement(e);o.put(i)}catch(i){throw m.error(`Failed to update element ${e.id}: ${i}`),i}}async deleteElement(e,t){const n=this.getHandler(t),o=this.getCache(t);try{await n.deleteElement(e,{elements:n.cache.getAll(),connectorsMapping:n.connectorsMapping}),o.remove(e)}catch(i){throw m.error(`Failed to delete element ${e.id}: ${i}`),i}}async pullConnectors(e=!1){const t=this.remoteCache.getElementsByType(x.Integration).map(n=>n.data);for(const n of t){const o=n.connectorId,i=n.connectorVersion;o&&await an({connectorId:o,connectorVersion:i,allConnectors:e,pulledConnectors:this.pulledConnectors,pulledConnectorVersions:this.pulledConnectorVersions})}}async pushConnectors(){const{connectorsMapping:e}=await sn();this.remoteRepo.setConnectorsMapping(e)}getHandler(e){return e===B.INCOMING?this.localRepo:(this.remoteRepo.setSourceCache(this.localCache),this.remoteRepo)}getCache(e){return e===B.INCOMING?this.localCache:this.remoteCache}async startWatching(){this.localWatcher=new uo({cwd:process.cwd(),lockTimeoutMs:1e3}),this.localWatcher.on(W.Updated,e=>this.handleLocalEvent(e,W.Updated)),this.localWatcher.on(W.Deleted,e=>this.handleLocalEvent(e,W.Deleted)),await this.localWatcher.start(),m.success("[local] Tracking changes.."),this.remoteWatcher=new po,this.remoteWatcher.on(gt.Updated,({elementId:e,elementType:t})=>this.handleRemoteElementEvent(e,t)),this.remoteWatcher.on(gt.ConnectorFileUpdated,({connectorId:e,filePath:t,eventType:n,newPath:o})=>this.handleRemoteConnectorFileEvent(e,t,n,o)),await this.remoteWatcher.start(),m.success("[remote] Tracking changes..")}async stopWatching(){this.localWatcher&&(await this.localWatcher.stop(),this.localWatcher=void 0),this.remoteWatcher&&(await this.remoteWatcher.stop(),this.remoteWatcher=void 0)}async handleRemoteElementEvent(e,t){try{const n=await this.remoteRepo.getByInternalId(e,t),o=!!n?.data.archivedAt||!!n?.data.isDeactivated;if(!n||o){const s=n||this.remoteCache.getByInternalId(e);return s?(m.info(`[${this.getDirectionLabel(B.INCOMING)}] Deleted: ${s.id}`),this.localWatcher?.executeWithPathLock(s.absolutePath,()=>this.deleteElement(s,B.INCOMING))):void 0}if(m.info(`[${this.getDirectionLabel(B.INCOMING)}] Updated: ${n.id}`),await this.localWatcher?.executeWithPathLock(n.absolutePath,async()=>this.updateElement(n,B.INCOMING)),t===x.Integration){const s=n.data.connectorId,a=n.data.connectorVersion,l=await Ct({connectorId:s});if(!l?.key)return;const d=Co(l);await this.localWatcher?.executeWithPathLock(d,async()=>an({connectorId:s,connectorVersion:a,allConnectors:!1,pulledConnectors:this.pulledConnectors,pulledConnectorVersions:this.pulledConnectorVersions}))}}catch(n){m.error(`[sync] Error handling remote event: ${n}`)}}async handleRemoteConnectorFileEvent(e,t,n,o){try{switch(n){case Ke.ConnectorFileDeleted:await To(e,t);break;case Ke.ConnectorDirectoryRenamed:await xo(e,t,o);break;case Ke.ConnectorDirectoryDeleted:await Io(e,t);break;default:await Eo(e,t);break}}catch(i){m.error(`[sync] Error handling remote connector file event: ${i}`)}}async handleLocalEvent(e,t){try{if(So(e.filePath))switch(t){case W.Updated:await bo(e.filePath);break;case W.Deleted:await vo(e.filePath);break}else{let n=F.fromPathAndData(e.filePath,e.data);if(!n){const o=F.idFromPath(e.filePath);if(!o||(n=this.remoteCache.get(o),!n))return}switch(m.info(`[${this.getDirectionLabel(B.OUTGOING)}] ${pr(t)}: ${n.id}`),t){case W.Updated:await this.updateElement(n,B.OUTGOING);break;case W.Deleted:await this.deleteElement(n,B.OUTGOING);break}}}catch(n){m.error(`[sync] Error handling local event: ${n}`)}}detectIncomingChanges(){this.clearChanges();const e=xe.getChanges(B.INCOMING,this.remoteCache,this.localCache);return this.updateChanges(e),e}detectOutgoingChanges(){this.clearChanges();const e=xe.getChanges(B.OUTGOING,this.localCache,this.remoteCache);return this.updateChanges(e),e}async resolveChanges(){if(!this.needsSync())return;m.info("[resolver] Resolving changes.."),m.info("[resolver] Resolving integration elements");const e=this.changes.filter(o=>o.element.type===x.Integration);await Promise.all(e.map(async o=>this.resolveChange(o))),e.length>0&&(await this.fetchElements(),this.changes=this.changes.length>0&&this.changes[0]?.direction===B.INCOMING?this.detectIncomingChanges():this.detectOutgoingChanges(),this.changes=this.changes.filter(o=>o.element.type!==x.Integration)),m.info("[resolver] Resolving universal elements");const t=this.changes.filter(o=>!o.element.hasParent());for(const o of ln){const i=t.filter(s=>s.element.type===o);m.info(`[resolver] Resolving ${o} elements`),await Promise.all(i.map(async s=>this.resolveChange(s)))}m.info("[resolver] Resolving integration level elements");const n=this.changes.filter(o=>o.element.hasParent());for(const o of ln){const i=n.filter(s=>s.element.type===o);m.info(`[resolver] Resolving ${o} elements`),await Promise.all(i.map(async s=>this.resolveChange(s)))}m.success("[resolver] Resolved changes")}async resolveChange(e){switch(e.type){case Q.DELETE:return this.deleteElement(e.element,e.direction);case Q.CREATE:return this.updateElement(e.element,e.direction);case Q.UPDATE:return this.updateElement(e.element,e.direction)}}static getChanges(e,t,n){const o=t.getAllIds(),i=n.getAllIds(),s=new Set([...o,...i]),a=[];for(const l of s){const d=t.get(l),p=n.get(l),f=xe.detectChangeForElement(d,p,e);f&&a.push(f)}return a}updateChanges(e){if(this.changes=e,this.needsForcedSync()){const t=e.filter(n=>n.isConflict);m.warning("[resolver] Conflicts detected"),this.notifier.setConflicts(t)}}clearChanges(){this.changes=[]}getDirectionLabel(e){switch(e){case B.INCOMING:return"local\u2190remote";case B.OUTGOING:return"local\u2192remote"}}static detectChangeForElement(e,t,n){return e&&!t?{type:Q.CREATE,element:e,direction:n,isConflict:!1}:!e&&t?{type:Q.DELETE,element:t,direction:n,isConflict:!0}:e&&t&&!e.isEqual(t)?{type:Q.UPDATE,element:e,direction:n,isConflict:!1}:null}}const oe=E.join(br.tmpdir(),"membrane-mcp-status"),un=3e4;class $o{static{c(this,"McpStatusService")}constructor(e=2e3){this.pollIntervalMs=e}isRunning=!1;pollInterval;async start(){this.isRunning||(this.isRunning=!0,this.pollInterval=setInterval(async()=>await this.checkStatus(),this.pollIntervalMs),await this.checkStatus())}async stop(){this.pollInterval&&(clearInterval(this.pollInterval),this.pollInterval=void 0,this.isRunning=!1)}async checkStatus(){try{const e=process.cwd(),t=Fe(void 0,e),n=dn(e);t&&re.setMcpStatus(t),await re.setMcpServers(n)}catch{m.error("Failed to check MCP status")}}}function Fe(r,e){try{const t=e||process.cwd();if(!r){const o=dn(t);return o.length===0?null:o[0]}const n=vt(r,t);if(v.existsSync(n)){const o=v.statSync(n),i=new Date;if(i.getTime()-o.mtime.getTime()>un)return be(r,t),null;const a=v.readFileSync(n,"utf8"),l=JSON.parse(a);if(l.isRunning){const d=new Date(l.lastActivity).getTime();if(i.getTime()-d>un)return be(r,t),null}return l}}catch{r&&e&&be(r,e)}return null}c(Fe,"getMcpStatus");function dn(r){try{const e=r||process.cwd(),t=bt(e);if(!v.existsSync(oe))return[];const n=v.readdirSync(oe),o=[];for(const i of n){const s=i.match(new RegExp(`^mcp-${t}-(\\d+)\\.json$`));if(s){const a=parseInt(s[1],10),l=Fe(a,e);l&&o.push(l)}}return o.sort((i,s)=>new Date(s.startTime).getTime()-new Date(i.startTime).getTime())}catch{return[]}}c(dn,"getAllMcpStatusFiles");function St(r){try{const t={...Fe(r.processId,r.cwd)||{isRunning:!1,startTime:new Date().toISOString(),lastActivity:new Date().toISOString(),toolsCount:0,totalRequests:0,processId:r.processId,cwd:r.cwd,agentName:process.env.AGENT_NAME||"Unnamed Agent"},...r};v.existsSync(oe)||v.mkdirSync(oe,{recursive:!0});const n=vt(r.processId,r.cwd);v.writeFileSync(n,JSON.stringify(t,null,2))}catch{}}c(St,"updateMcpStatus");function be(r,e){try{const t=e||process.cwd();if(r){const n=vt(r,t);v.existsSync(n)&&v.unlinkSync(n)}else{const n=bt(t);if(v.existsSync(oe)){const o=v.readdirSync(oe);for(const i of o)i.match(new RegExp(`^mcp-${n}-\\d+\\.json$`))&&v.unlinkSync(E.join(oe,i))}}}catch{}}c(be,"clearMcpStatus");function Po(r,e){const t=Fe(r,e);t&&St({processId:r,cwd:e,totalRequests:t.totalRequests+1,lastRequestTime:new Date().toISOString(),lastActivity:new Date().toISOString()})}c(Po,"trackToolExecution");function bt(r){return gr("md5").update(r).digest("hex").slice(0,8)}c(bt,"getCwdHash");function vt(r,e){const t=bt(e);return E.join(oe,`mcp-${t}-${r}.json`)}c(vt,"getStatusFilePath");const Le={Agent:"agent",Cli:"cli"},P={NOT_INITIALIZED:"not_initialized",SETTING_UP:"setting_up",INITIALIZED:"initialized",NOT_SYNCED:"not_synced",PULLING:"pulling",PUSHING:"pushing",RESOLVING:"resolving",CONFLICTS:"conflicts",SYNCED:"synced",WATCHING:"watching",ERROR:"error"};class Ao{static{c(this,"MembraneCLIService")}constructor(e,t,n=()=>process.exit(0)){this.mode=e,this.cwd=t,this.onShutdown=n,this.notifier=re,this.mcpStatusService=new $o,this.syncService=new xe,this.setupProcessCleanup()}initialized=!1;notifier;mcpStatusService;syncService;isShuttingDown=!1;currentConfig=null;get config(){return this.currentConfig}getSyncedElements(){return this.syncService.localCache.getAll()}getSyncedElementsByType(e){return this.syncService.localCache.getElementsByType(e)}async fetchElements(){await this.syncService.fetchElements()}async pullWorkspace(e={}){let t=!1;try{if(m.setVerboseMode(!!e.verbose),e.rps!==void 0&&$.init({maxRequestsPerSecond:e.rps}),await this.notifier.setState(P.PULLING),await this.syncService.fetchElements(),this.syncService.detectIncomingChanges(),this.syncService.needsForcedSync()&&!e.force){await this.notifier.setState(P.CONFLICTS),e.watch||(t=!0);return}await this.syncService.pullConnectors(e.allConnectors),await this.syncWorkspaces(e)}catch(n){t=!0,m.error(`Failed to pull workspace: ${n}`),await this.notifier.setState(P.ERROR),m.saveLogsToFile("error")}finally{if(e.saveLogs&&m.saveLogsToFile(),t)process.exit(1);else return e?.onComplete?.()}}async pushWorkspace(e={}){let t=!1;try{if(m.setVerboseMode(!!e.verbose),e.rps!==void 0&&$.init({maxRequestsPerSecond:e.rps}),await this.notifier.setState(P.PUSHING),await this.syncService.fetchElements(),this.syncService.detectOutgoingChanges(),this.syncService.needsForcedSync()&&!e.force){await this.notifier.setState(P.CONFLICTS),e.watch||(t=!0);return}await this.syncService.pushConnectors(),await this.syncWorkspaces(e)}catch(n){t=!0,m.error(`Failed to push workspace: ${n}`),await this.notifier.setState(P.ERROR),m.saveLogsToFile("error")}finally{if(e.saveLogs&&m.saveLogsToFile(),t)process.exit(1);else return e?.onComplete?.()}}async syncWorkspaces(e={}){try{e.verbose!==void 0&&m.setVerboseMode(!!e.verbose),await this.notifier.setState(P.RESOLVING),this.syncService.needsSync()&&await this.syncService.resolveChanges();const t=await this.syncService.getStats();this.notifier.setStats(t),await this.notifier.setState(P.SYNCED),e.watch&&(await this.notifier.setState(P.WATCHING),await this.syncService.startWatching())}catch(t){m.error(`Failed to sync local and remote workspaces: ${t}`),await this.notifier.setState(P.ERROR),m.saveLogsToFile("error")}}async init({force:e=!1}={}){if(!(this.initialized&&!e)){await this.notifier.setState(P.NOT_INITIALIZED);try{await this.loadConfig(),de.isCacheDefined()?(await this.initServices(),this.initialized=!0,await this.notifier.setState(P.INITIALIZED)):(this.initialized=!1,await this.notifier.setState(P.SETTING_UP))}catch(t){m.error(`Failed to initialize services: ${t}`),await this.notifier.setState(P.ERROR),m.saveLogsToFile("error"),this.onShutdown()}}}async loadConfig(){this.currentConfig=de.loadConfig(this.cwd),this.notifier.setConfig(this.currentConfig)}async updateConfig(e){const t={...this.currentConfig,...e};if(!(JSON.stringify(t)!==JSON.stringify(this.currentConfig)))return;await this.stopServices();const o=de.updateConfig(t);this.currentConfig=o,await this.init({force:!0})}async shutdown(){!this.initialized||this.isShuttingDown||(this.isShuttingDown=!0,this.mode===Le.Agent&&(await this.notifier.cleanup(),await this.mcpStatusService.stop()),this.initialized=!1,this.onShutdown())}async initServices(){this.mode===Le.Agent&&(await this.notifier.connectToRemote(),await this.mcpStatusService.start()),this.syncService.clear()}async stopServices(){this.mode===Le.Agent&&(await this.notifier.cleanup(),await this.mcpStatusService.stop())}setupProcessCleanup(){["SIGINT","SIGTERM","uncaughtException","unhandledRejection"].forEach(t=>process.on(t,()=>this.shutdown())),process.on("beforeExit",t=>{t===0&&this.shutdown()})}}const hn=Ve(null);function Ro({children:r,membraneCLIService:e}){const{data:t}=Bt("/account"),[n,o]=N(P.NOT_INITIALIZED),[i,s]=N([]),[a,l]=N({}),[d,p]=N([]),[f,h]=N(null),T=t?.workspaces?.find(S=>S.workspaceKey===f?.workspaceKey)||null;return fe(()=>{const S=c(({state:M})=>o(M),"handleStateChanged"),D=c(({stats:M})=>l(M),"handleStatsChanged"),O=c(({log:M})=>p(Y=>[...Y,M]),"handleLogAdded"),_=c(({conflicts:M})=>s(M),"handleConflictsUpdated"),L=c(({config:M})=>h(M),"handleConfigChanged");return e.notifier.on(j.StateChanged,S),e.notifier.on(j.StatsChanged,D),e.notifier.on(j.LogAdded,O),e.notifier.on(j.ConflictsChanged,_),e.notifier.on(j.ConfigChanged,L),e.init(),()=>{e.notifier.off(j.StateChanged,S),e.notifier.off(j.StatsChanged,D),e.notifier.off(j.LogAdded,O),e.notifier.off(j.ConflictsChanged,_),e.notifier.off(j.ConfigChanged,L)}},[]),u(hn.Provider,{value:{state:n,stats:a,logs:d,currentWorkspace:T,conflicts:i,config:f,updateConfig:c(S=>e.updateConfig(S),"updateConfig"),resolveConflicts:c(S=>e.syncWorkspaces(S),"resolveConflicts"),pull:c(S=>e.pullWorkspace(S),"pull"),push:c(S=>e.pushWorkspace(S),"push"),exit:c(()=>e.shutdown(),"exit"),fetchElements:c(()=>e.fetchElements(),"fetchElements"),getSyncedElementsByType:c(S=>e.getSyncedElementsByType(S),"getSyncedElementsByType")},children:r})}c(Ro,"MembraneCLIServiceProvider");function G(){const r=Ye(hn);if(!r)throw new Error("useMembraneCLIService must be used within MembraneCLIServiceProvider");return r}c(G,"useMembraneCLIService");const Et=Ve(null),pn=c(()=>{const r=Ye(Et);if(!r)throw new Error("useTree must be used within TreeView");return r},"useTree"),Do=c(r=>ce.Children.count(r)>0,"hasChildren");function Oo(r){const e=pn(),{label:t,value:n,isInitiallyExpanded:o}=r,i=rr(),s=Ze(e.registerChildItem(t)).current,a=e.isActiveByRef(s),[l,d]=N(o??!1),p={props:r,ref:s,id:i,active:a,expanded:l,setExpanded:d,label:t,value:n,isInitiallyExpanded:o,isParent:!1},{children:f}=r,h=typeof f=="function"?f(p):f;return{...p,props:{...r,children:h},isParent:Do(h)}}c(Oo,"useTreeItem");function ge(r){const e=pn(),t=Oo(r),{expanded:n,isParent:o,props:{children:i},setExpanded:s}=t,{renderValue:a=Mo}=r,l=t.ref;ke(()=>{function h(C){return r.onInput?.(C)?!0:C.active&&C.key.rightArrow&&!n?(s(!0),!0):C.active&&C.key.leftArrow&&n?(s(!1),!0):!1}c(h,"onInput"),l.onInput=h},[l,r.onInput,n,s]),l.firstChild??={parent:l,index:0};let d=l.firstChild;function p(h){const C=d;return C.nextSibling||(C.nextSibling={parent:l,prevSibling:C,index:C.index+1}),d=C.nextSibling,C.label=h,C}c(p,"registerChildItem");function f(){l.lastRenderedChild=d.prevSibling}return c(f,"commitChildren"),ke(()=>{f()}),b($e,{children:[b(g,{marginLeft:e.depth*2,children:[u(g,{width:2,children:o&&u(y,{children:n?"\u25BC":"\u25B6"})}),No(t),a(t)]}),n&&u(Et.Provider,{value:{...e,depth:e.depth+1,registerChildItem:p},children:i})]})}c(ge,"TreeItem");function No({active:r,label:e}){return u(g,{width:32,children:u(y,{inverse:r,children:e})})}c(No,"defaultRenderLabel");function Mo(r){return u(y,{})}c(Mo,"defaultRenderValue");const he=c(({label:r,onPress:e,hotkey:t})=>u(ge,{label:r,onInput:c(({input:n,key:o,active:i})=>i&&o.return||t&&t in o&&o[t]||n===t?(e(),!0):!1,"onInput"),renderValue:c(()=>u(g,{children:u(y,{children:`${t?` (${t})`:""}`})}),"renderValue")}),"ActionTreeItem"),fn=c(({label:r,value:e,onChange:t,disabled:n=!1,mask:o,...i})=>{const[s,a]=N(e),[l,d]=N(!1);return ke(()=>{l||a(e)},[l,e]),u(ge,{label:r,value:e,onInput:c(({key:p,active:f})=>n?!1:l?(p.escape&&d(!1),p.return&&(t(s),d(!1)),!0):f&&p.return?(d(!0),a(e),!0):!1,"onInput"),renderValue:c(()=>{const p=s,f=o?o.repeat(p.length):p;return u(g,{children:n||!l?u(y,{dimColor:!0,children:f}):b($e,{children:[u(g,{width:f.length+1,children:u(zt,{...i,focus:l,value:p,onChange:a})}),u(y,{dimColor:!0,children:" \u241B cancel"})]})})},"renderValue")})},"TreeTextField"),Fo=c(r=>u(fn,{...r,mask:"*"}),"SecretField"),J=c(({children:r,showHelp:e=!1})=>{const t=Ze({parent:void 0,label:"<<ROOT>>",index:0}),[n,o]=N(t.current),i={depth:0,isActiveByRef:c(a=>n===a,"isActiveByRef"),registerChildItem:c(()=>t.current,"registerChildItem"),activeItemRef:n};ke(()=>{o(a=>a.parent?a.parent.lastRenderedChild&&a.index>a.parent.lastRenderedChild.index?a.parent.lastRenderedChild:a:t.current.firstChild??t.current)});function*s(a){a&&(yield a,yield*s(a.firstChild),a.nextSibling&&a!==a.parent?.lastRenderedChild&&(yield*s(a.nextSibling)))}return c(s,"walkTree"),le((a,l)=>{if(n.onInput?.({input:a,key:l,active:!0}))return;let d,p;for(const f of s(t.current))if(f.label!=null){if(d===n&&(p=f),f&&f.onInput?.({input:a,key:l,active:f===n}))return;d=f}l.upArrow?o(f=>f.prevSibling?.lastRenderedChild??f.prevSibling??f.parent??t.current):l.downArrow?o(f=>p??f):l.pageUp?o(()=>t.current.firstChild??t.current):l.pageDown?o(()=>t.current.lastRenderedChild??t.current):l.tab?console.debug({label:n.label,nextSibling:n.nextSibling?.label,prevSibling:n.prevSibling?.label,parent:n.parent?.label,firstChild:n.firstChild?.label,lastRenderedChild:n.lastRenderedChild?.label}):l.leftArrow?o(f=>f.parent??t.current):l.rightArrow&&o(f=>f.lastRenderedChild?f.firstChild:f)}),u(Et.Provider,{value:i,children:b(g,{flexDirection:"column",children:[u(ge,{isInitiallyExpanded:!0,label:"rootItem",children:r}),e&&u(g,{marginTop:1,flexDirection:"column",children:u(y,{dimColor:!0,children:"\u2191/\u2193 move \u2022 \u2190 collapse \u2022 \u2192 expand \u2022 Enter/Space select/edit \u2022 Esc exit"})})]})})},"TreeView");J.Item=ge,J.TextField=fn;function pe({label:r,elementType:e,isActionExcluded:t,toggleAction:n,generateCode:o}){const[i,s]=N([]),{fetchElements:a,getSyncedElementsByType:l}=G();return fe(()=>{c(async()=>{try{await a();const p=l(e);s(p.map(f=>f.data))}catch(p){console.error(String(p))}},"loadElements")()},[e,a,l,s]),u(ge,{label:r,children:i.map(d=>u(ge,{label:d.name,children:e===x.Action&&b($e,{children:[t&&n&&u(he,{label:`Toggle ${t(d)?"":"(excluded)"}`,onPress:c(()=>{n(d)},"onPress")}),o&&u(he,{label:"Generate code",onPress:c(()=>{o?.(d)},"onPress")})]})},d.id))})}c(pe,"WorkspaceElementsTreeItem");function Lo({onComplete:r}){const{config:e,updateConfig:t,fetchElements:n,getSyncedElementsByType:o}=G(),[i,s]=N(""),a=e,l=c(h=>{s(h),setTimeout(()=>s(""),2e3)},"setFlash"),d=c(async(h,C)=>{try{await t({[h]:C}),l("\u2705 Configuration updated!")}catch{l("\u274C Error updating configuration")}},"updateField"),p=c(async()=>{try{if(e){const h=de.saveToFile(e);l(h?"\u2705 Configuration saved successfully!":"\u274C Failed to save configuration"),await n();const C=o(x.Action);await lt({out:a?.outputDir??"",target:a?.projectType??"typescript",schemasOnly:!1,membraneInterfaces:{actions:C.filter(T=>!a?.excludedActionKeys?.includes(T.key)).map(T=>T.data)}}),l("\u2705 Code generated successfully!")}}catch{l("\u274C Error saving configuration")}},"handleSaveConfig"),f=c(async()=>{try{const h=de.loadConfig();h?(await t(h),l("\u2705 Configuration reloaded successfully!")):l("\u274C No configuration found to reload")}catch{l("\u274C Error reloading configuration")}},"handleReloadConfig");return b(g,{flexDirection:"column",gap:1,children:[u(y,{bold:!0,color:"cyan",children:"\u2699\uFE0F Membrane Configuration Manager"}),u(g,{paddingX:2,children:b(J,{showHelp:!0,children:[b(J.Item,{label:"Configuration",isInitiallyExpanded:!0,children:[b(J.Item,{label:"Project",children:[u(J.TextField,{label:"Workspace Key",value:a?.workspaceKey??"",onChange:c(h=>d("workspaceKey",h),"onChange"),disabled:!0}),u(Fo,{label:"Workspace Secret",value:a?.workspaceSecret??"",onChange:c(h=>d("workspaceSecret",h),"onChange"),disabled:!0}),u(J.TextField,{label:"API URI",value:a?.apiUri??"",onChange:c(h=>d("apiUri",h),"onChange")}),u(J.TextField,{label:"Test Customer ID",value:a?.testCustomerId??"",onChange:c(h=>d("testCustomerId",h),"onChange")})]}),b(J.Item,{label:"Code Generation",isInitiallyExpanded:!0,children:[b(J.Item,{label:"Project Type",isInitiallyExpanded:!0,value:a?.projectType,renderValue:c(({value:h})=>u(y,{children:h==="typescript"?"TypeScript":h==="openapi"?"OpenAPI":"(Not set)"}),"renderValue"),children:[u(he,{label:"Update to TypeScript",onPress:c(()=>d("projectType","typescript"),"onPress")}),u(he,{label:"Update to OpenAPI",onPress:c(()=>d("projectType","openapi"),"onPress")})]}),u(J.TextField,{label:"Project dir",value:a?.outputDir??"",onChange:c(h=>d("outputDir",h),"onChange")})]}),b(J.Item,{label:"Workspace Elements",children:[u(pe,{label:"Actions",elementType:x.Action,isActionExcluded:c(h=>a?.excludedActionKeys?.includes(h.key)??!1,"isActionExcluded"),toggleAction:c(h=>d("excludedActionKeys",[...a?.excludedActionKeys??[],h.key]),"toggleAction"),generateCode:c(h=>{(async()=>{try{await lt({out:a?.outputDir??"",target:a?.projectType??"typescript",schemasOnly:!1,membraneInterfaces:{actions:[h]}}),l("\u2705 Code generated successfully!")}catch{l("\u274C Error generating code")}})()},"generateCode")}),u(pe,{label:"Flows",elementType:x.Flow}),u(pe,{label:"Data Sources",elementType:x.DataSource}),u(pe,{label:"Field Mappings",elementType:x.FieldMapping}),u(pe,{label:"Packages",elementType:x.Package}),u(pe,{label:"App Data Schemas",elementType:x.AppDataSchema}),u(pe,{label:"App Event Types",elementType:x.AppEventType})]})]}),u(he,{label:"Save Configuration",onPress:p,hotkey:"s"}),u(he,{label:"Reload Configuration",onPress:f,hotkey:"r"}),u(he,{label:"Exit",onPress:c(()=>r?.(),"onPress"),hotkey:"escape"})]})}),i&&u(g,{paddingX:2,children:u(y,{color:i.includes("\u2705")?"green":"red",children:i})})]})}c(Lo,"ConfigManager");const mn=Ve(process.cwd());function jo({cwd:r,children:e}){return u(mn.Provider,{value:r,children:e})}c(jo,"CwdProvider");function Uo(){return Ye(mn)}c(Uo,"useCwd");function Tt({cwd:r,children:e,membraneCLIService:t}){const n=r||process.cwd();return u(jo,{cwd:n,children:u(vr,{value:{fetcher:Qr()},children:u(Ro,{membraneCLIService:t,children:e})})})}c(Tt,"Layout");function _o(r,e){r.command("config").alias("install").description("\u26A0\uFE0F EXPERIMENTAL: Manage local membrane configuration with interactive UI").addHelpText("after",["","Examples:"," membrane config # Open interactive config manager",""].join(`
10
- `)).action(()=>{Xe(ce.createElement(Tt,{membraneCLIService:e,children:ce.createElement(Lo,{onComplete:c(()=>process.exit(0),"onComplete")})}))})}c(_o,"setupConfigCommand");function Ko({currentPat:r,onSubmit:e}){const[t,n]=N(""),[o,i]=N(!1),[s,a]=N(null),l=dt(`/w/0/${Hn}`);return b(g,{flexDirection:"column",borderStyle:"round",borderTop:!0,width:70,paddingX:1,children:[u(g,{marginTop:-1,marginBottom:1,children:u(y,{bold:!0,children:"\u{1F511} Enter your Personal Access Token"})}),u(y,{children:"Please provide your Personal Access Token. You can find it here:"}),u(g,{marginTop:1,marginBottom:1,children:u(y,{color:"yellow",children:l})}),r&&u(y,{dimColor:!0,children:"Press Enter to keep your current token or type a new one."}),u(zt,{mask:"*",placeholder:`${r?"******":"Enter your token here..."}`,value:t,onChange:n,onSubmit:c(async p=>{a(null),i(!0);try{await e(p),n("")}catch{a("Invalid token. Please try again.")}finally{i(!1)}},"handleSubmit")}),o&&u(g,{marginTop:1,children:b(y,{children:[u(Ae,{type:"dots"})," Validating token..."]})}),s&&u(y,{color:"red",children:s})]})}c(Ko,"PersonalAccessTokenInput");function gn({onExit:r,showEscOption:e=!0}){const[t,n]=N(""),{data:o,error:i,isLoading:s}=Bt("/account"),{updateConfig:a}=G(),l=o?.workspaces,d=s;if(le((S,D)=>{D.escape&&r?.()}),d)return b(g,{children:[u(Ae,{}),u(y,{children:" Fetching workspaces..."})]});if(i)return b(g,{flexDirection:"column",children:[b(y,{color:"red",children:["Error: ",i.message]}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"Press ESC to go back"})})]});const p=l?.filter(S=>S.name.toLowerCase().includes(t.toLowerCase()))??[],f=p.map(S=>({label:S.name,value:S.id})),h=f.length,C=l?.length??0;async function T(S){const D=p.find(L=>L.id===S);if(!D)return;const{key:O,secret:_}=D;!O||!_||(await a({workspaceKey:O,workspaceSecret:_}),r?.())}return c(T,"handleSelect"),b(g,{flexDirection:"column",borderStyle:"round",borderTop:!0,width:70,paddingX:1,children:[u(g,{marginTop:-1,children:u(y,{bold:!0,children:"\u{1F4C1} Select your workspace"})}),b(g,{marginTop:1,children:[u(y,{children:"Search: "}),u(Er,{placeholder:"Enter a search query...",onChange:n})]}),C>5&&b(y,{children:["Showing ",h," of ",C," workspaces."]}),u(g,{marginTop:1,children:u(Tr,{options:f,onChange:c(S=>{S&&T(S)},"onChange")})}),e&&u(g,{marginTop:1,children:u(y,{color:"grey",children:"Press ESC to go back"})})]})}c(gn,"SelectWorkspace");var ve=(r=>(r[r.Authenticate=0]="Authenticate",r[r.ConnectWorkspace=1]="ConnectWorkspace",r))(ve||{});const qo={0:"Authenticate in Membrane",1:"Connect a Membrane Workspace"},xt=[ve.Authenticate,ve.ConnectWorkspace];function yn({onComplete:r}){const{config:e}=G(),[t,n]=N(!1),[o,i]=N(0),s=!!(e?.workspaceKey&&e?.workspaceSecret),a=Yt(),l=xt[o],d=o+1,p=xt.length,f=xt.map((T,S)=>{let D="pending";return S<o?D="done":S===o&&(D="current"),{id:T,label:qo[T],status:D}});async function h(T){const S=a&&T===""?a:T,D=new pt;try{await D.request("/account",{headers:{Authorization:`Bearer ${S}`}}),zr(S),i(O=>O+1)}catch(O){console.error(O)}}c(h,"handlePatSubmit");function C(){n(!0),r&&r()}return c(C,"handleWorkspaceSelected"),le((T,S)=>{s&&S.escape&&r&&r()}),t?u(g,{children:u(y,{children:"\u2705 Setup complete. You are ready to go!"})}):b(g,{flexDirection:"column",alignSelf:"flex-start",gap:1,children:[b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:70,children:[u(g,{marginTop:-1,marginBottom:1,children:b(y,{bold:!0,children:["\u{1F6E0}\uFE0F Setup \u2014"," ",b(y,{color:"cyan",children:["Step ",d," of ",p]}),s&&u(y,{color:"grey",children:" [esc: go back]"})]})}),u(g,{flexDirection:"column",paddingLeft:2,children:f.map(T=>u(Bo,{status:T.status,label:T.label},T.id))})]}),l===ve.Authenticate&&u(Ko,{currentPat:a,onSubmit:h}),l===ve.ConnectWorkspace&&u(gn,{onExit:C,showEscOption:!1})]})}c(yn,"Setup");function Bo({status:r,label:e}){return b(g,{children:[b(g,{width:2,children:[r==="current"&&u(Ae,{type:"dots"}),r==="done"&&u(y,{children:"\u2705"})]}),u(y,{dimColor:r!=="current",children:e})]})}c(Bo,"StepDisplay");function Wo(r,e){r.command("init").description("Run interactive setup for Membrane project").option("--key <key>","Workspace key for non-interactive setup").option("--secret <secret>","Workspace secret for non-interactive setup").addHelpText("after",["","Examples:"," membrane init # Run interactive setup"," membrane init --key KEY --secret SEC # Non-interactive setup with key and secret",""].join(`
11
- `)).action(t=>{t.key&&t.secret?de.saveToFile({workspaceKey:t.key,workspaceSecret:t.secret})?(console.error("\u2705 Configuration saved to membrane.config.yml"),process.exit(0)):(console.error("Error writing configuration file"),process.exit(1)):t.key||t.secret?(console.error("Error: Both --key and --secret must be provided for non-interactive mode"),process.exit(1)):Xe(ce.createElement(Tt,{membraneCLIService:e,children:ce.createElement(yn,{onComplete:c(()=>process.exit(0),"onComplete")})}))})}c(Wo,"setupInitCommand");const Jo={info:"\u{1F4DD}",success:"\u2728",warning:"\u26A0\uFE0F",error:"\u274C",debug:"\u{1F50D}"},zo={info:w.white,success:w.green,warning:w.yellow,error:w.red,debug:w.gray};class I{static{c(this,"Logger")}static formatMessage(e,t,n={icon:!1}){const o=n.timestamp?`${w.gray(new Date().toISOString())} `:"",i=n.prefix?`${w.gray(n.prefix)} `:"",s=n.suffix?` ${w.gray(n.suffix)}`:"",a=Jo[t],l=zo[t];return`${o}${n.icon?a:""} ${i}${l(e)}${s}`}static info(e,t){let n=w.white;if(t?.color&&(n=w[t.color.toLowerCase()]||w.white),t?.timestamp){const o=new Date().toLocaleTimeString();console.debug(`${w.gray(`[${o}]`)} ${n(e)}`)}else console.debug(n(e))}static group(e,t){}static groupEnd(){}static newLine(){}static success(e,t){console.debug(this.formatMessage(e,"success",t))}static warning(e,t){console.debug(this.formatMessage(e,"warning",t))}static error(e,t){console.error(this.formatMessage(e,"error",t))}static debug(e,t){t?.prefix?console.debug(w.gray(`[${t.prefix}] ${e}`),t.error?`
12
- ${w.red(t.error)}`:""):console.debug(w.gray(e),t?.error?`
13
- ${w.red(t.error)}`:"")}static step(e,t){}static header(e){console.debug(),console.debug(w.bold.cyan(`\u25B6 ${e}`)),console.debug()}static table(e,t){if(e.length===0)return;const n=t||Object.keys(e[0]),o=e.map(i=>{const s={};return n.forEach(a=>{s[a]=i[a]}),s});console.table(o,n)}static divider(){}}const Ho=".membrane",Go="config.json";function Vo(r){const e=k.resolve(Ho),t=k.join(e,Go);let n={};Qe(t)&&(n=JSON.parse(cr(t,"utf8")));const o={...n,...r};for(const[i,s]of Object.entries(o))s===void 0&&delete o[i];Qe(e)||Wt(e,{recursive:!0}),lr(t,JSON.stringify(o,null,2)+`
14
- `,"utf8")}c(Vo,"patchProjectConfig");function Yo(r){r.command("login").description("Log in to Membrane via browser-based OAuth").option("--apiUri <uri>","API base URI",De).option("--no-browser","Print the authorization URL instead of opening a browser").addHelpText("after",["","Examples:"," membrane login # Log in via browser"," membrane login --no-browser # Print URL for manual login"," membrane login --apiUri https://api.example.com # Use custom API",""].join(`
15
- `)).action(async e=>{try{const t=e.apiUri;I.header("Logging in to Membrane");const n=Zo(),o=Xo(n),{port:i,waitForCode:s,state:a,server:l}=await ti(),d=`http://127.0.0.1:${i}/callback`,p=`${t}/oauth/authorize?response_type=code&client_id=${qe}&redirect_uri=${encodeURIComponent(d)}&code_challenge=${o}&code_challenge_method=S256&scope=${Gn}&state=${a}`,h=(await fetch(p,{redirect:"manual"})).headers.get("location");if(!h)throw new Error("No redirect from authorization endpoint");const C=new URL(h).searchParams.get("authorizationId");if(!C)throw new Error("No authorizationId in authorization redirect");if(!!(process.stdin.isTTY&&e.browser))try{const{default:M}=await import("open");await M(h),I.info("Opening browser for authentication...")}catch{I.info("Could not open browser automatically.")}console.error(` If the browser doesn't open, visit:
16
- ${h}
17
- `);const S=ot("Waiting for authorization...").start(),D=new AbortController,O=s.then(async M=>{S.text="Exchanging authorization code...";const Y=await fetch(`${t}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"authorization_code",code:M,redirect_uri:d,client_id:qe,code_verifier:n})});if(!Y.ok)throw new Error(`Token exchange failed: ${Y.status} ${await Y.text()}`);return wn(await Y.json())}),_=oi(t,C,n,D.signal);_.catch(()=>{});const L=await Promise.race([O,_]);D.abort(),l.close(),S.stop(),Hr(),Zt(L.accessToken,L.refreshToken,L.expiresIn),Zr(t),L.defaultWorkspaceId&&Vo({defaultWorkspaceId:L.defaultWorkspaceId,defaultTenantId:L.defaultTenantId}),I.success("Authentication successful!"),process.exit(0)}catch(t){t instanceof Error?I.error(t.message):I.error("An unknown error occurred"),process.exit(1)}})}c(Yo,"setupLoginCommand");function Zo(){return Ht(32).toString("base64url")}c(Zo,"generateCodeVerifier");function Xo(r){return Ir("sha256").update(r).digest("base64url")}c(Xo,"generateCodeChallenge");function wn(r){if(!r.refresh_token)throw new Error("Server did not return a refresh token");return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresIn:r.expires_in,defaultWorkspaceId:r.default_workspace_id,defaultTenantId:r.default_tenant_id}}c(wn,"parseTokenResponse");function Qo(r){return r.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}c(Qo,"escapeHtml");function ei(){return`<!DOCTYPE html><html><body style="font-family:system-ui;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#0a0a0a;color:#fff">
18
- <div style="text-align:center"><h1>Authorization successful</h1><p style="color:#888">You can close this tab and return to your terminal.</p></div>
19
- </body></html>`}c(ei,"successPage");function It(r){return`<!DOCTYPE html><html><body style="font-family:system-ui;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#0a0a0a;color:#fff">
20
- <div style="text-align:center"><h1>Authorization failed</h1><p style="color:#f87171">${Qo(r)}</p></div>
21
- </body></html>`}c(It,"errorPage");function ti(){const r=Ht(16).toString("base64url");return new Promise(e=>{let t,n;const o=new Promise((s,a)=>{t=s,n=a}),i=kr((s,a)=>{const l=new URL(s.url??"/","http://127.0.0.1");if(l.pathname!=="/callback"){a.writeHead(404),a.end("Not found");return}if(l.searchParams.get("error")){const h=l.searchParams.get("error_description")??"Authorization was denied";a.writeHead(200,{"Content-Type":"text/html"}),a.end(It(h)),n(new Error(h));return}const p=l.searchParams.get("code");if(l.searchParams.get("state")!==r){a.writeHead(200,{"Content-Type":"text/html"}),a.end(It("State mismatch \u2014 possible CSRF attack. Please try again.")),n(new Error("State mismatch"));return}if(!p){a.writeHead(200,{"Content-Type":"text/html"}),a.end(It("No authorization code received.")),n(new Error("No authorization code received"));return}a.writeHead(200,{"Content-Type":"text/html"}),a.end(ei()),t(p)});i.listen(0,"127.0.0.1",()=>{const s=i.address(),a=typeof s=="object"&&s?s.port:0;e({port:a,waitForCode:o,state:r,server:i})})})}c(ti,"startCallbackServer");const ni=900*1e3,ri=5;async function oi(r,e,t,n){const o=Date.now()+ni;let i=ri;for(;Date.now()<o;){n.throwIfAborted(),await ii(i*1e3,n),n.throwIfAborted();const s=await fetch(`${r}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"authorization_code",authorization_id:e,client_id:qe,code_verifier:t})});if(s.ok)return wn(await s.json());const a=await s.json(),l=a.error??a.message??"";if(l!=="authorization_pending")if(l==="slow_down"){i+=5;continue}else throw l==="expired_token"?new Error("Authorization has expired. Please try again."):new Error(`Token exchange failed: ${s.status} ${l}`)}throw new Error("Authorization has expired. Please try again.")}c(oi,"pollForTokens");function ii(r,e){return new Promise((t,n)=>{const o=setTimeout(t,r);e.addEventListener("abort",()=>{clearTimeout(o),n(e.reason)},{once:!0})})}c(ii,"sleep");function si(r){r.command("logout").description("Log out of Membrane and revoke tokens").option("--apiUri <uri>","API base URI").action(async e=>{try{Xr()||(I.info("You are not logged in."),process.exit(0));const t=Ne(),n=e.apiUri||Me()||De;if(t)try{const o=await fetch(`${n}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:t})});o.ok||I.debug(`Server revocation returned ${o.status}`)}catch{I.warning("Could not reach server to revoke token. Local tokens cleared, but the session may still be active on the server. You can revoke it from the console.")}Yr(),I.success("Logged out successfully."),process.exit(0)}catch(t){t instanceof Error?I.error(t.message):I.error("An unknown error occurred"),process.exit(1)}})}c(si,"setupLogoutCommand");const ai=86400;async function ci(r,e,t,n){const o={iss:r,isAdmin:!0};return t&&(o.id=t),tt.sign(o,e,{expiresIn:ai})}c(ci,"generateMcpAccessToken");async function li(r,e){return(await we.get(`${r}/docs-json`,{headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}})).data}c(li,"fetchOpenApiSchema");function ie(r,e=!0,t,n=new Set){if(!r)return e?R.string():R.string().optional();if(Object.keys(r).length===0)return e?R.any():R.any().optional();if(r.$ref){const i=r.$ref.replace("#/components/schemas/","");if(n.has(i))return R.any();if(n.add(i),t?.schemas?.[i]){const s=t.schemas[i];return ie(s,e,t,n)}return R.any()}let o;if(r.type==="object"||r.properties){const i={};if(r.properties)for(const[s,a]of Object.entries(r.properties)){const l=r.required?.includes(s)??!1;i[s]=ie(a,l,t,n)}if(Object.keys(i).length>0){if(o=R.object(i),r.additionalProperties)if(typeof r.additionalProperties=="object"){const s=ie(r.additionalProperties,!1,t,n);o=o.catchall(s)}else r.additionalProperties===!0&&(o=o.passthrough())}else if(r.additionalProperties&&typeof r.additionalProperties=="object"){const s=ie(r.additionalProperties,!1,t,n);o=R.record(R.string(),s)}else o=R.record(R.string(),R.any())}else if(r.type==="array"){const i=r.items?ie(r.items,!0,t,n):R.any();o=R.array(i)}else if(r.anyOf)o=R.any();else{switch(r.type){case"integer":o=R.coerce.number().int();break;case"number":o=R.coerce.number();break;case"boolean":o=R.coerce.boolean();break;case"string":default:o=R.string();break}r.format==="uuid"?o=R.string().uuid():r.format==="email"?o=R.string().email():r.format==="date"?o=R.string().regex(/^\d{4}-\d{2}-\d{2}$/):r.format==="date-time"&&(o=R.string().datetime()),r.enum&&(o=R.enum(r.enum)),(r.type==="integer"||r.type==="number")&&(r.minimum!==void 0&&(o=o.min(r.minimum)),r.maximum!==void 0&&(o=o.max(r.maximum))),r.type==="string"&&(r.minLength!==void 0&&(o=o.min(r.minLength)),r.maxLength!==void 0&&(o=o.max(r.maxLength)))}return e?o:o.optional()}c(ie,"convertOpenApiSchemaToZod");function Cn(r){const e=r.match(/\{([^}]+)\}/g);return e?e.map(t=>t.slice(1,-1)):[]}c(Cn,"extractPathParameters");function ui(r,e,t,n,o,i){const s=r.operationId||`${t}_${e.replace(/[^a-zA-Z0-9]/g,"_")}`,a=r.description||r.summary||`${t.toUpperCase()} ${e}`,l={},d={},p={};if(r.parameters){for(const h of r.parameters)if(h.in==="path")d[h.name]=ie(h.schema,!0,o).describe(h.description||`Path parameter: ${h.name}`);else if(h.in==="query"){const C=h.required===!0;p[h.name]=ie(h.schema,C,o).describe(h.description||`Query parameter: ${h.name}`)}}if(Object.keys(d).length>0&&(l.params=R.object(d).describe("Path parameters")),Object.keys(p).length>0&&(l.query=R.object(p).describe("Query parameters")),r.requestBody&&r.requestBody.content){const h=r.requestBody.required===!0;if(r.requestBody.content["application/json"]){const C=r.requestBody.content["application/json"].schema;C?l.body=ie(C,h,o).describe("Request body (JSON)"):l.body=R.any().describe("Request body (JSON)")}else r.requestBody.content["application/octet-stream"]||r.requestBody.content["text/plain"]?l.body=h?R.string().describe("Request body (binary/text)"):R.string().optional().describe("Request body (binary/text)"):l.body=R.any().describe("Request body")}const f=Object.keys(l).length>0?R.object(l):R.object({});return{name:s,description:a,parameters:f,async execute(h){try{const C=Cn(e);if(C.length>0){if(!h.params)throw new Error(`Missing required path parameters: ${C.join(", ")}`);const L=C.filter(M=>!(M in h.params));if(L.length>0)throw new Error(`Missing required path parameters: ${L.join(", ")}`)}let T=`${n}${e}`;if(h.params)for(const[L,M]of Object.entries(h.params)){const Y=`{${L}}`;T.includes(Y)&&(T=T.replace(Y,String(M)))}const S=Cn(T);if(S.length>0)throw new Error(`Unresolved path parameters: ${S.join(", ")}`);const D=new URLSearchParams;if(h.query)for(const[L,M]of Object.entries(h.query))M!=null&&D.append(L,String(M));D.toString()&&(T+=`?${D.toString()}`);const O={method:t.toUpperCase(),headers:{Authorization:`Bearer ${i}`}};h.body&&t.toLowerCase()!=="get"&&(r.requestBody?.content?.["application/json"]?(O.headers["Content-Type"]="application/json",O.data=h.body):r.requestBody?.content?.["application/octet-stream"]?(O.headers["Content-Type"]="application/octet-stream",O.data=h.body):r.requestBody?.content?.["text/plain"]?(O.headers["Content-Type"]="text/plain",O.data=h.body):(O.headers["Content-Type"]="application/json",O.data=h.body));const _=await we.request({url:T,...O});return{content:[{type:"text",text:_.data===""?"":JSON.stringify(_.data,null,2)}]}}catch(C){return we.isAxiosError(C)?{content:[{type:"text",text:`Error: ${C.response?.data?`HTTP ${C.response.status}: ${C.response.statusText} - ${JSON.stringify(C.response.data)}`:`HTTP ${C.response?.status||"unknown"}: ${C.message}`}`}],isError:!0}:C instanceof Error?{content:[{type:"text",text:`Error: API call failed: ${C.message}`}],isError:!0}:{content:[{type:"text",text:"Error: API call failed with unknown error"}],isError:!0}}}}}c(ui,"createApiTool");function di(r,e,t){const n=[];if(!r.paths)return n;const o=r.components||{};for(const[i,s]of Object.entries(r.paths))if(typeof s=="object"&&s!==null){for(const[a,l]of Object.entries(s))if(["get","post","put","delete","patch"].includes(a)&&l&&typeof l=="object"){const d=ui(l,i,a,e,o,t);n.push(d)}}return n}c(di,"convertOpenApiToTools");function hi(r){r.command("mcp").description("Launch MCP server with OpenAPI endpoints as tools (API URI is read from environment variables or membrane.config.yml)").addHelpText("after",["","Examples:"," membrane mcp # Launch MCP server using API URI from env vars or membrane.config.yml",""].join(`
22
- `)).action(async()=>{try{process.env.FASTMCP_SUPPRESS_WARNINGS="true";const e=Se(),t=process.env.MEMBRANE_ACCESS_TOKEN||e?.accessToken,n=process.env.MEMBRANE_WORKSPACE_KEY||e?.workspaceKey,o=process.env.MEMBRANE_WORKSPACE_SECRET||e?.workspaceSecret,i=process.env.MEMBRANE_TEST_CUSTOMER_ID||e?.testCustomerId,s=process.env.MEMBRANE_API_URI||e?.apiUri||Me()||De;!t&&(!n||!o)&&(e||(console.error("No configuration found. Please set MEMBRANE_ACCESS_TOKEN, or MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first to create a configuration file."),process.exit(1)),console.error("Missing credentials. Please provide MEMBRANE_ACCESS_TOKEN or workspace key/secret in the configuration file, command line, or environment variables."),process.exit(1));const a=t||await ci(n,o,i),l=await li(s,a),d=di(l,s,a),p=new $r({name:"Membrane API",instructions:`This MCP server lets you interact with Membrane to configure, run, and troubleshoot integrations.
7
+ Unexpected Error:`),a&&console.error(a),console.error(s)),s}return}},"executeRequest");return t?i():fn.withSkipErrorLog(i)}async generateAccessToken(e,t){return nt.sign({name:"Membrane Agent",isAdmin:!0,exp:Math.floor(Date.now()/1e3)+3600,iss:e},t,{algorithm:"HS512"})}isTokenValid(){return Date.now()<this.tokenExpiry}getCurrentConfig(e){const t=$e(e);return!t?.workspaceKey||!t?.workspaceSecret?null:{workspaceKey:t.workspaceKey,workspaceSecret:t.workspaceSecret,apiUri:t.apiUri}}async createClient(e){const t=await this.generateAccessToken(e.workspaceKey,e.workspaceSecret);return this.tokenExpiry=Date.now()+36e5,this.currentConfig=e,new Gt({token:t,apiUri:e.apiUri})}async getClient(e=process.cwd()){const t=this.getCurrentConfig(e);if(!t)throw new Error("Unable to create MembraneClient: No workspace configuration found.");const n=!this.currentConfig||this.currentConfig.workspaceKey!==t.workspaceKey||this.currentConfig.workspaceSecret!==t.workspaceSecret||this.currentConfig.apiUri!==t.apiUri,o=!this.isTokenValid()||this.tokenExpiry-Date.now()<3e5;return(!this.client||n||o)&&(this.client=await this.createClient(t)),this.client}clearClient(){this.client=null,this.currentConfig=null,this.tokenExpiry=0,this.requestTimes=[],this.semaphore=0,this.semaphoreQueue=[],this.rateLimitMutex=Promise.resolve()}getCurrentConfigInfo(){return this.currentConfig}async waitIfNeeded(){this.rateLimitMutex=this.rateLimitMutex.then(async()=>{for(this.cleanOldRequests();this.requestTimes.length>=this.maxRequestsPerSecond;){const t=this.requestTimes[0]+this.windowSizeMs-Date.now()+10;if(t>0)await new Promise(n=>setTimeout(n,t)),this.cleanOldRequests();else break}}),await this.rateLimitMutex}recordRequest(){this.requestTimes.push(Date.now())}cleanOldRequests(){const e=Date.now()-this.windowSizeMs;this.requestTimes=this.requestTimes.filter(t=>t>e)}getRequestCount(){return this.cleanOldRequests(),this.requestTimes.length}async acquireSemaphore(){if(this.semaphore<this.maxConcurrentRequests){this.semaphore++;return}return new Promise(e=>{this.semaphoreQueue.push(e)})}releaseSemaphore(){if(this.semaphore--,this.semaphoreQueue.length>0){const e=this.semaphoreQueue.shift();this.semaphore++,e()}}async withRetry(e,t="operation"){let n;for(let o=0;o<=this.maxRetries;o++)try{return await e()}catch(i){if(n=i,!this.isRetryableError(i)||o===this.maxRetries)throw i;const s=new Date().toISOString(),a=o+2;g.debug(`[${s}] Retrying ${t} (attempt ${a}/${this.maxRetries+1})`);let l=this.retryDelay*Math.pow(2,o);if(this.enableJitter){const d=l*.25*(Math.random()*2-1);l=Math.max(0,l+d)}await new Promise(d=>setTimeout(d,l))}throw n||new Error("Unknown error occurred during retry attempts")}isRetryableError(e){if(!e)return!1;const t=(e.message||e.toString()||"").toLowerCase(),n=e.code;return n&&["econnrefused","enotfound","econnreset","etimedout","ehostunreach","enetunreach"].includes(n.toLowerCase())?!0:["econnrefused","connection refused","network error","timeout","socket hang up","request timeout","dns lookup failed"].some(s=>t.includes(s))}async withTimeout(e,t){let n;const o=new Promise((i,s)=>{n=setTimeout(()=>s(new Error(`Request timeout after ${t}ms`)),t)});try{const i=await Promise.race([e,o]);return n&&clearTimeout(n),i}catch(i){throw n&&clearTimeout(n),i}}async getVersion(){try{return(await this.withClient(t=>t.get("version"),!1))?.version??null}catch{return null}}async isVersionAtLeast(e){const t=await this.getVersion(),n=t?rt.coerce(t):null,o=rt.coerce(e);return!!(n&&o&&rt.gte(n,o))}}const $=new fo;async function mn(r){const e=await $.withClient(t=>t.get("org-workspace-id"),!0,r);if(!e)throw new Error("Failed to get workspace ID");return e.id}c(mn,"getWorkspaceId");const gn=1e3,yn=12e4;async function wn(){const r=await $.withClient(async t=>ht(t,()=>t.get("export"),{pollIntervalMs:gn,timeoutMs:yn}));if(!r)throw new Error("Failed to export workspace");const e=await Ie.get(r.downloadUrl,{responseType:"arraybuffer"});return Buffer.from(e.data)}c(wn,"downloadWorkspaceExport");async function Cn(r,e={}){const t=new en;t.append("file",r,{filename:"membrane.zip",contentType:"application/zip"});const n=await $.withClient(async i=>{const s=`import?dryRun=${e.dryRun??!1}&partial=${e.partial??!1}`,{jobId:a,accessKey:l}=await i.post(s,t,{headers:t.getHeaders()});return ht(i,()=>Promise.resolve({jobId:a,accessKey:l}),{pollIntervalMs:1e3,timeoutMs:3e5})});if(!n)throw new Error("Failed to import workspace");return await $.isVersionAtLeast("1.10")?{create:new Set(n.comparison.create||[]),update:new Set(n.comparison.update||[]),delete:new Set(n.comparison.delete||[])}:{create:new Set(n.create||[]),update:new Set(n.update||[]),delete:new Set(n.delete||[])}}c(Cn,"importWorkspace");async function mo(r,e){const t=await $.withClient(async o=>ht(o,()=>o.get(`connectors/${r}/export-files`,{version:e}),{pollIntervalMs:gn,timeoutMs:yn}));if(!t)throw new Error("Failed to export connector version");const n=await Ie.get(t.downloadUrl,{responseType:"arraybuffer"});return Buffer.from(n.data)}c(mo,"exportConnector");function Sn(r,e){Pe(x.dirname(r)),v.writeFileSync(r,e)}c(Sn,"writeFile");function go(r){v.existsSync(r)&&v.rmSync(r)}c(go,"deleteFile");function yt(r){v.existsSync(r)&&v.rmSync(r,{recursive:!0,force:!0})}c(yt,"deleteDir");function yo(r,e=!0){if(v.existsSync(r))try{const t=v.readFileSync(r,"utf8");return H.load(t)||void 0}catch(t){if(!e)return;if(t instanceof Error){const n=x.relative(process.cwd(),r);throw new Error(`Failed to parse YAML file "${n}": ${t.message}`)}throw t}}c(yo,"readYaml$1");function bn(r,e,t){try{Pe(x.dirname(r));const n=H.dump(e,t);v.writeFileSync(r,n,"utf8")}catch(n){if(n instanceof Error){const o=x.relative(process.cwd(),r);throw new Error(`Failed to write YAML file "${o}": ${n.message}`)}throw n}return e}c(bn,"writeYaml$1");function Pe(r){v.existsSync(r)||v.mkdirSync(r,{recursive:!0})}c(Pe,"ensureDirExists");function vn(r,e=!0){if(!v.existsSync(r)||!v.statSync(r).isDirectory())return;let t=v.readdirSync(r);t.length>0&&(t.forEach(n=>vn(x.join(r,n),!1)),t=v.readdirSync(r)),t.length===0&&!e&&v.rmdirSync(r)}c(vn,"cleanupEmptyFolders");function wo(r){const e=r.split(/[\\/]/).join("/");return e.endsWith("/")?e+"**":e}c(wo,"normalizePattern");async function En(r,e,t){const n=(e??[]).map(wo),o=[];if(t){const i=x.join(q(process.cwd()),"connectors");for(const s of Object.values(t)){const a=s.key,l=x.join(i,a),d=x.join(l,`${a}.yml`);v.existsSync(d)&&o.push({name:pr(a),content:v.readFileSync(d,"utf-8")});const h=[...s.versions];v.existsSync(x.join(l,"development"))&&!h.includes("")&&h.push("");for(const f of h){const p=f===""?"development":f,C=x.join(l,p,"src"),E=x.join(l,p,"src.zip"),S=f===""?Ve:f;v.existsSync(C)?o.push({name:Vt(a,S),content:await Co(C)}):v.existsSync(E)&&o.push({name:Vt(a,S),content:v.readFileSync(E)})}}}return new Promise((i,s)=>{const a=ot("zip",{zlib:{level:9}}),l=[];a.on("data",h=>l.push(h)),a.on("end",()=>i(Buffer.concat(l))),a.on("error",h=>s(h));const d=new Map;for(const h of r[I.Integration]||[])d.set(h.uuid,h.key);for(const[h,f]of Object.entries(r))for(const p of f){const C=d.get(p.integrationUuid),E=fr(h,p.key,C);n.length>0&&!n.some(S=>Ir(E,S))||a.append(H.dump(p),{name:E})}for(const{name:h,content:f}of o)a.append(f,{name:h});a.finalize()})}c(En,"createMembraneZip");async function Co(r){return new Promise((e,t)=>{const n=ot("zip",{zlib:{level:9}}),o=[];n.on("data",i=>o.push(i)),n.on("end",()=>e(Buffer.concat(o))),n.on("error",i=>t(i)),n.directory(r,!1),n.finalize()})}c(Co,"createConnectorVersionZip");async function wt(r,e){const t=await rn.loadAsync(r),n=[];for(const[o,i]of Object.entries(t.files)){if(i.dir)continue;const s=ue(o);if(!s)continue;const a=await i.async("string"),l=H.load(a);if(!l)continue;const d=await e(o,l,s);d!==void 0&&n.push(d)}return n}c(wt,"iterateZipItems");async function Tn(r){const e=await rn.loadAsync(r),t={};for(const[n,o]of Object.entries(e.files)){if(o.dir)continue;const i=ue(n);if(!i)continue;const s=await o.async("string"),a=H.load(s);t[i.type]||(t[i.type]=[]),t[i.type].push(a)}return t}c(Tn,"readMembraneZip");async function Ct(r=process.cwd()){const e=await xn(q(r)),t={};for(const{data:n,type:o}of e)t[o]||(t[o]=[]),t[o].push(n);return t}c(Ct,"readMembraneDir");async function xn(r){const e=In(r),t=[];for(const n of e){if(n.isDirectory())continue;const o=ue(n.path);if(!o)continue;const i=await n.readContent(),s=H.load(i),a=x.join(r,n.path);t.push({filePath:a,data:s,type:o.type})}return t}c(xn,"iterateMembraneFiles");function In(r,e){const t=[];if(e||(e=r),!v.existsSync(e)||e.startsWith(x.join(r,"connectors")))return t;const n=v.readdirSync(e,{withFileTypes:!0});for(const o of n){const i=x.join(e,o.name),s=x.relative(r,i);o.isDirectory()?t.push(...In(r,i)):o.isFile()&&ue(s)&&t.push({path:s,isDirectory:c(()=>!1,"isDirectory"),readContent:c(async()=>v.readFileSync(i,"utf8"),"readContent")})}return t}c(In,"getMembraneFiles");function kn(){const r={},e=x.join(q(process.cwd()),"connectors");if(!v.existsSync(e))return r;const t=v.readdirSync(e);for(const n of t){const o=x.join(e,n);if(!v.statSync(o).isDirectory())continue;const i=x.join(o,`${n}.yml`),s=yo(i,!1);if(!s?.uuid)continue;const a=new Set,l=v.readdirSync(o);for(const d of l){if(d.endsWith(".yml"))continue;const h=x.join(o,d);v.statSync(h).isDirectory()&&a.add(d=="development"?"":d)}r[s.uuid]={key:n,id:s.id,versions:Array.from(a)}}return r}c(kn,"readConnectorsDir");function Ae(r,e){const t=x.join(q(process.cwd()),"connectors",r);return e==""||e===Ve?x.join(t,"development"):e?x.join(t,e):t}c(Ae,"getConnectorPath");const So={info:"\u{1F4DD}",success:"\u2728",warning:"\u26A0\uFE0F",error:"\u274C",debug:"\u{1F50D}"},bo={info:m.white,success:m.green,warning:m.yellow,error:m.red,debug:m.gray};class T{static{c(this,"Logger")}static formatMessage(e,t,n={icon:!1}){const o=n.timestamp?`${m.gray(new Date().toISOString())} `:"",i=n.prefix?`${m.gray(n.prefix)} `:"",s=n.suffix?` ${m.gray(n.suffix)}`:"",a=So[t],l=bo[t];return`${o}${n.icon?a:""} ${i}${l(e)}${s}`}static info(e,t){let n=m.white;if(t?.color&&(n=m[t.color.toLowerCase()]||m.white),t?.timestamp){const o=new Date().toLocaleTimeString();console.debug(`${m.gray(`[${o}]`)} ${n(e)}`)}else console.debug(n(e))}static group(e,t){}static groupEnd(){}static newLine(){}static success(e,t){console.debug(this.formatMessage(e,"success",t))}static warning(e,t){console.debug(this.formatMessage(e,"warning",t))}static error(e,t){console.error(this.formatMessage(e,"error",t))}static debug(e,t){t?.prefix?console.debug(m.gray(`[${t.prefix}] ${e}`),t.error?`
8
+ ${m.red(t.error)}`:""):console.debug(m.gray(e),t?.error?`
9
+ ${m.red(t.error)}`:"")}static step(e,t){}static header(e){console.debug(),console.debug(m.bold.cyan(`\u25B6 ${e}`)),console.debug()}static table(e,t){if(e.length===0)return;const n=t||Object.keys(e[0]),o=e.map(i=>{const s={};return n.forEach(a=>{s[a]=i[a]}),s});console.table(o,n)}static divider(){}}function vo(r){r.command("diff").description("Show what would change on push without applying anything").action(async()=>{const e=xe({text:"Comparing workspace",color:"white"}).start();try{const t=await Ct();e.text="Downloading remote workspace";const n=await wn(),o=await Tn(n);e.stop();const{changes:i,diff:s}=Yt(o,t,{includeDiff:!0}),a=Object.keys(i).length;if(a===0){T.info("No changes detected");return}T.info(`\u25CF Diff \xB7 ${a} change(s)
10
+ `),s&&T.info(Eo(s))}catch(t){e.stop(),T.error("\u25A0 Error"),T.error(`\u2514\u2500\u2500 ${t.message}`),process.exit(1)}})}c(vo,"setupDiffCommand");function Eo(r){return r.split(`
11
+ `).map(e=>e.startsWith("diff ")?m.bold(e):e.startsWith("index ")||e.startsWith("===")?m.dim(e):e.startsWith("---")?m.bold.red(e):e.startsWith("+++")?m.bold.green(e):e.startsWith("@@")?m.cyan(e):e.startsWith("+")?m.green(e):e.startsWith("-")?m.red(e):e).join(`
12
+ `)}c(Eo,"colorizePatch");function fe(r){if(r.type){if(Array.isArray(r.type))return{anyOf:r.type.map(t=>fe({...r,type:t}))};if(r.type==="array"){const t={type:"array",items:r.items?fe(r.items):{type:"object"}};return r.title&&(t.title=r.title),r.description&&(t.description=r.description),r.minItems!==void 0&&(t.minItems=r.minItems),r.maxItems!==void 0&&(t.maxItems=r.maxItems),r.uniqueItems!==void 0&&(t.uniqueItems=r.uniqueItems),t}}const e={};if(r.type&&!Array.isArray(r.type)&&(e.type=r.type),r.title&&(e.title=r.title),r.description&&(e.description=r.description),r.format&&(e.format=r.format),r.enum&&(e.enum=r.enum),r.default!==void 0&&(e.default=r.default),r.minimum!==void 0&&(e.minimum=r.minimum),r.maximum!==void 0&&(e.maximum=r.maximum),r.minLength!==void 0&&(e.minLength=r.minLength),r.maxLength!==void 0&&(e.maxLength=r.maxLength),r.maxItems!==void 0&&(e.maxItems=r.maxItems),r.readOnly!==void 0&&(e.readOnly=r.readOnly),r.writeOnly!==void 0&&(e.writeOnly=r.writeOnly),r.examples&&r.examples.length>0&&(e.example=r.examples[0]),r.properties){e.properties={};for(const[t,n]of Object.entries(r.properties))e.properties[t]=fe(n)}return r.anyOf&&(e.anyOf=r.anyOf.map(fe)),r.additionalProperties!==void 0&&(typeof r.additionalProperties=="boolean"?e.additionalProperties=r.additionalProperties:e.additionalProperties=fe(r.additionalProperties)),r.required&&(e.required=r.required),e}c(fe,"convertDataSchemaToOpenAPI");function To(r){const{membraneInterfaces:{actions:e}}=r,t=xo(e);return JSON.stringify({openapi:"3.0.0",info:{title:"Membrane Actions API",version:"1.0.0",description:"Generated OpenAPI specification for Membrane actions"},paths:{},components:{schemas:t}},null,2)}c(To,"generateOpenAPIContent");function xo(r){const e={};return r.forEach(t=>{const n=st(it(t.key)),o={type:"object",properties:{id:{type:"string",description:"Request ID"},action:{type:"string",enum:[t.key]}},required:["id","action"]};if(t?.inputSchema){const a=fe(t.inputSchema);a.properties&&(o.properties={...o.properties,...a.properties}),a.required&&(o.required=[...o.required||[],...a.required])}e[`${n}Request`]=o;const i={type:"object",properties:{id:{type:"string",description:"Response ID"},success:{type:"boolean"},data:{type:"object"}},required:["id","success"]},s=t?.customOutputSchema||t?.outputSchema;if(s){const a=fe(s);i.properties={...i.properties,data:a}}e[`${n}Response`]=i}),e}c(xo,"generateOpenAPISchemas");function Io(r){const{membraneInterfaces:{actions:e}}=r,t=$r(e,"key"),n=new kr({indentNumberOfSpaces:2,newLine:`
13
+ `});return n.writeLine("// Generated TypeScript definitions for Membrane actions"),n.newLine(),n.writeLine("import type {"),n.writeLine(" ActionAccessor,"),n.writeLine(" ConnectionLevelActionAccessor,"),n.writeLine('} from "@membranehq/sdk";'),n.newLine(),ko(n,t),n.toString()}c(Io,"generateTypeScriptContent");function ko(r,e){r.writeLine('declare module "@membranehq/sdk" {'),r.indent(()=>{e.forEach(t=>{const n=bt(t),o=t?.inputSchema;Be(r,{interfaceName:n,properties:o?.properties,required:o?.required||[]});const i=vt(t),s=t?.customOutputSchema||t?.outputSchema;Be(r,{interfaceName:i,properties:s?.properties,required:s?.required||[],fallbackContent:"result: unknown"}),r.newLine()}),Be(r,{interfaceName:"MembraneClient",methods:e.map(t=>{const n=bt(t),o=vt(t);return{name:"action",parameters:`selector: "${t.key}"`,returnType:`ActionAccessor<${n}, ${o}>`}})}),r.newLine(),Be(r,{interfaceName:"ConnectionAccessor",methods:e.map(t=>{const n=bt(t),o=vt(t);return{name:"action",parameters:`selector: "${t.key}"`,returnType:`ConnectionLevelActionAccessor<${n}, ${o}>`}})})}),r.writeLine("}")}c(ko,"generateModuleAugmentation");function Be(r,e){const{interfaceName:t,properties:n,required:o=[],methods:i,fallbackContent:s}=e;r.writeLine(`interface ${t} {`),r.indent(()=>{n&&Object.keys(n).length>0&&$o(r,n,o),i&&i.length>0&&(n&&Object.keys(n).length>0&&r.newLine(),i.forEach((a,l)=>{r.writeLine(`${a.name}(${a.parameters}): ${a.returnType};`),l<i.length-1&&r.newLine()})),s&&(!n||Object.keys(n).length===0)&&(!i||i.length===0)&&r.writeLine(s)}),r.writeLine("}")}c(Be,"generateTypeScriptInterface");function $o(r,e,t){Object.entries(e).forEach(([n,o])=>{const s=t.includes(n)?"":"?",a=St(o);r.writeLine(`readonly ${n}${s}: ${a}`)})}c($o,"generateTypeScriptProperties");function St(r){return r.type==="string"?r.enum?r.enum.map(e=>`'${e}'`).join(" | "):"string":r.type==="number"||r.type==="integer"?"number":r.type==="boolean"?"boolean":r.type==="array"?`${r.items?St(r.items):"unknown"}[]`:r.type==="object"?r.properties?`{ ${Object.entries(r.properties).map(([t,n])=>{const i=(r.required||[]).includes(t)?"":"?",s=St(n);return`readonly ${t}${i}: ${s}`}).join("; ")} }`:"Record<string, unknown>":"unknown"}c(St,"convertJsonSchemaToTypeScript");function bt(r){return`${st(it(r.key))}Input`}c(bt,"getInputTypeName");function vt(r){return`${st(it(r.key))}Output`}c(vt,"getOutputTypeName");async function Et(r){const{out:e}=r;await v.promises.mkdir(e,{recursive:!0});const t=Po(r);for(const[n,o]of Object.entries(t)){const i=x.join(e,n);await v.promises.writeFile(i,o,"utf-8")}}c(Et,"generateCode");function Po(r){switch(r.target){case"openapi":return{"openapi.json":To(r)};case"typescript":return{"generated.d.ts":Io(r)};default:throw new Error(`Unsupported target: ${r.target}`)}}c(Po,"generateContent");const Tt=c(()=>[m.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),m.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),m.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),m.yellow("\u2502 The codegen command is experimental and subject to rapid changes.\u2502"),m.yellow("\u2502 Features, APIs, and file structures may change without notice. \u2502"),m.yellow("\u2502 Use in production environments is not recommended. \u2502"),m.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
14
+ `),"createExperimentalWarning$2");function Ao(r,e){r.command("codegen").description("\u26A0\uFE0F EXPERIMENTAL: Generate code for easy integration within projects - This feature is experimental and will be changing rapidly. Use at your own risk.").requiredOption("--actions <actions>",'Comma-separated list of action names (e.g., "list-files,get-file-by-id")').requiredOption("--out <path>","Output directory for generated files").requiredOption("--target <type>","Target format: openapi or typescript").requiredOption("--schemasOnly","Generate only schemas without implementation").addHelpText("after",Tt()).addHelpText("after",["",m.bold("Examples:"),` ${m.gray("\u25B8")} ${m.cyan("membrane codegen --actions list-files,get-file-by-id --out src/generated --target openapi --schemasOnly")}`,` ${m.gray("\u25B8")} ${m.cyan("membrane install --actions delete-file --out src/generated --target typescript --schemasOnly")}`,"",Tt()].join(`
15
+ `)).action(async t=>{try{console.warn(Tt()),console.error(m.cyan("\u{1F527} Membrane Codegen")),console.error("Status: Loading membrane interfaces...");const n=await Ro(e,t),o={out:t.out,target:t.target,schemasOnly:t.schemasOnly,membraneInterfaces:n};console.error(`Output: ${o.out}`),console.error(`Target: ${o.target}`),console.error(`Schemas Only: ${o.schemasOnly?"Yes":"No"}`),console.error(`Loaded ${n.actions.length} membrane interfaces`),console.error("Status: Generating code..."),await Et(o),console.error(m.green("\u2705 Code generation completed successfully!")),process.exit(0)}catch(n){console.error(m.red("\u274C Code generation failed:")),console.error(n instanceof Error?n.message:"Unknown error occurred"),process.exit(1)}})}c(Ao,"setupCodegenCommand");async function Ro(r,e){return await r.fetchElements(),{actions:[...r.getSyncedElementsByType(I.Action).map(t=>t.data)].filter(t=>e.actions.includes(t.key||""))}}c(Ro,"loadMembraneInterfaces");const Q={UPDATE:"update",DELETE:"delete",CREATE:"create"},B={INCOMING:"incoming",OUTGOING:"outgoing"},Y={[I.Integration]:{element:"integration",elements:"integrations",exportable:!1,exportCleanup:c(r=>({id:r.id,key:r.key,name:r.name,connectorId:r.connectorId,baseUri:r.baseUri,connectorVersion:r.connectorVersion}),"exportCleanup")},[I.Connector]:{element:"connector",elements:"connectors",exportable:!1},[I.Action]:{element:"action",elements:"actions",integrationSpecific:!0,exportCleanup:c(r=>(delete r.integration,r),"exportCleanup")},[I.AppDataSchema]:{element:"appDataSchema",elements:"appDataSchemas"},[I.AppEventType]:{element:"appEventType",elements:"appEventTypes"},[I.DataLinkTable]:{element:"dataLinkTable",elements:"dataLinkTables"},[I.DataSource]:{element:"dataSource",elements:"dataSources",parentKey:"universalDataSourceId",integrationSpecific:!0},[I.FieldMapping]:{element:"fieldMapping",elements:"fieldMappings",integrationSpecific:!0,parentKey:"universalFieldMappingId",exportCleanup:c(r=>(delete r.dataSourceId,r),"exportCleanup")},[I.Flow]:{element:"flow",elements:"flows",integrationSpecific:!0,parentKey:"universalFlowId"},[I.Package]:{element:"package",elements:"packages",integrationSpecific:!0}},Oo=["id","workspaceId","integrationId","createdAt","updatedAt","revision","archivedAt","baseUri","state","appliedToIntegrations","dependencies"],$n=[I.Action,I.FieldMapping,I.Flow,I.DataSource,I.Package];class F{static{c(this,"Element")}type;key;integrationKey;data;constructor(e,t,n,o){if(!o)throw new Error("Element must always contain data");if(!t)throw new Error("Element must have a key");this.type=e,this.key=t,this.data=o,this.integrationKey=n||F.extractIntegrationKey(o)}get id(){return F.makeId(this.type,this.key,this.integrationKey)}get dirPath(){const e=Z[this.type].apiPath;if(this.integrationKey){const t=Z[I.Integration].apiPath;return x.join(t,this.integrationKey,e,this.key)}return x.join(e,this.key)}get path(){return x.join(this.dirPath,"spec.yaml")}get relativePath(){return x.relative(q(process.cwd()),this.path)}get absolutePath(){return x.resolve(x.join(q(process.cwd()),this.path))}isEqual(e){if(this.id!==e.id)return!1;const t=this.clean(),n=e.clean();return Dr(t,n)}hasParent(){if(this.data?.parentUuid)return!0;const e=this.getParentKey();return!!e&&!!this.data?.[e]}clean(){const e={...this.data};return Oo.forEach(t=>{delete e[t]}),Object.keys(e).forEach(t=>{t.match(/universal.*Revision/)&&delete e[t]}),e}getParentKey(){let e="parentId";return Y?.[this.type]?.parentKey&&(e=Y?.[this.type]?.parentKey),e}static new(e,t,n,o){return new F(e,t,n,o)}static fromData(e,t){if(!t?.key)throw new Error(`Element missing key: ${JSON.stringify(t)}`);const n=this.extractIntegrationKey(t);return new F(e,t.key,n,t)}static fromPathAndData(e,t){const n=F.parsePath(e);if(!t)return;const o=t?.key||n?.key;return n?F.new(n.type,o,n.integrationKey,t):void 0}static fromElement(e){return new F(e.type,e.key,e.integrationKey,{...e.data})}static idFromPath(e){const t=F.parsePath(e);if(t)return F.makeId(t.type,t.key,t.integrationKey)}static makeId(e,t,n){return e===I.Integration?`${e}:${t}`:F.isIntegrationSpecific(e)?`${n||"universal"}:${e}:${t}`:`${e}:${t}`}static parsePath(e){const t=this.getRelativePath(e);if(!this.isElementFile(t))return;const n=Object.values(Z).map(l=>l.apiPath).join("|"),o=new RegExp(`^(?<elementType>${n})/(?<elementKey>[^/]+)/spec\\.ya?ml$`),i=t.match(o);if(i?.groups){const{elementType:l,elementKey:d}=i.groups,h=this.getElementTypeFromPath(l);if(h)return{type:h,key:d}}const s=new RegExp(`^integrations/(?<integrationKey>[^/]+)/(?<elementType>${n})/(?<elementKey>[^/]+)/spec\\.ya?ml$`),a=t.match(s);if(a?.groups){const{integrationKey:l,elementType:d,elementKey:h}=a.groups,f=this.getElementTypeFromPath(d);if(f)return{type:f,key:h,integrationKey:l}}}static extractIntegrationKey(e){return e?.integrationKey||e?.integration?.key||e?.integration_key||void 0}static isElementFile(e){return e.endsWith(".yml")||e.endsWith(".yaml")}static getElementTypeFromPath(e){return Object.values(I).find(t=>Z[t].apiPath===e)}static getRelativePath(e){return x.relative(q(process.cwd()),e)}static isIntegrationSpecific(e){return $n.includes(e)}static canBeIntegrationSpecific(e){return $n.includes(e)}}class Pn{static{c(this,"BaseWorkspaceElementsRepository")}constructor(e){this.cache=e}connectorsMapping;sourceCache;setConnectorsMapping(e){this.connectorsMapping=e}setSourceCache(e){this.sourceCache=e}async getElements(){const e=[];try{const t=await this.getIntegrations();e.push(...t);const n=[I.Integration,I.Connector],o=Object.keys(Y),i=await Promise.all(o.filter(s=>!n.includes(s)).map(async s=>this.getElementsByType(s,t)));for(const s of i)e.push(...s)}catch(t){throw g.error(`Failed to get elements: ${t}`),t}return e}async putElement(e){let t=this.cache.get(e.id);return t&&!e.data.id&&t.data.id&&(e.data.id=t.data.id),!t&&e.data.id&&(t=await this.getByInternalId(e.data.id,e.type,!1)),e.data.id&&t?t=await this.updateElement(e,t,{elements:this.cache.getAll(),connectorsMapping:this.connectorsMapping,sourceElements:this.sourceCache?.getAll()}):t=await this.createElement(e,{elements:this.cache.getAll(),connectorsMapping:this.connectorsMapping,sourceElements:this.sourceCache?.getAll()}),t}async getElementsByType(e,t){return[]}async createElement(e,t){return e}async updateElement(e,t,n){return t}async getElement(e){}async getIntegrations(){return[]}async getByInternalId(e,t,n=!0){}async deleteElement(e,t){}}function xt(r,e=!0){if(v.existsSync(r))try{const t=v.readFileSync(r,"utf8");return H.load(t)||void 0}catch(t){if(!e)return;if(t instanceof Error){const n=x.relative(process.cwd(),r);throw new Error(`Failed to parse YAML file "${n}": ${t.message}`)}throw t}}c(xt,"readYaml");function Do(r,e,t){try{const n=H.dump(e,t);v.writeFileSync(r,n,"utf8")}catch(n){if(n instanceof Error){const o=x.relative(process.cwd(),r);throw new Error(`Failed to write YAML file "${o}": ${n.message}`)}throw n}}c(Do,"writeYaml");class No extends Pn{static{c(this,"LocalWorkspaceElementsRepository")}basePath;constructor(e){super(e),this.basePath=q(process.cwd())}async getElementsByType(e,t){const n=[],o=x.join(this.basePath,Z[e].apiPath),i=await this.readElementsInDir(o);n.push(...i);for(const s of t){const a=x.join(this.basePath,Z[I.Integration].apiPath,s.key,Z[e].apiPath),l=await this.readElementsInDir(a);n.push(...l)}return n.length>0&&g.debug(`[local] Fetched ${n.length} ${e}${n.length!==1?"s":""}`),n}async getElement(e){return this.readElement(e.path)}async createElement(e){return this.updateElement(e,e)}async updateElement(e,t){if(!e.data)throw new Error("Element must have data to write");const n=x.join(this.basePath,t.dirPath),o=x.join(this.basePath,t.path);return v.existsSync(n)||v.mkdirSync(n,{recursive:!0}),Do(o,e.data),g.debug(`[local] Written ${t.relativePath}`),t}async deleteElement(e){const t=x.join(this.basePath,e.path),n=x.join(this.basePath,e.dirPath);v.existsSync(t)&&v.rmSync(t,{force:!0}),this.pruneEmptyDir(n),g.debug(`[local] Deleted ${e.relativePath}`)}async getIntegrations(){const e=x.join(this.basePath,Z[I.Integration].apiPath);return this.readElementsInDir(e)}async readElement(e){const t=xt(e);if(t)return F.fromPathAndData(e,t)}async readElementsInDir(e){const t=[];if(!v.existsSync(e))return t;const n=v.readdirSync(e);if(n.length===0)return t;const o=n.map(async s=>{const a=x.join(e,s,"spec.yaml");return this.readElement(a)});return(await Promise.all(o)).filter(s=>s!=null)}pruneEmptyDir(e){try{if(!e.startsWith(this.basePath)||e===this.basePath||!v.existsSync(e)||v.readdirSync(e).length>0)return;v.rmdirSync(e),this.pruneEmptyDir(x.dirname(e))}catch(t){console.warn(`Failed to prune empty directory ${e}:`,t)}}}class Mo extends Pn{static{c(this,"RemoteWorkspaceElementsRepository")}async getElementsByType(e,t){const n=F.canBeIntegrationSpecific(e),o=await this.findAll(e,n?{layer:"universal"}:{});if(!F.canBeIntegrationSpecific(e))return o;for(const i of t){const s=i.key,a=i.data.id,l=await this.findAll(e,a?{integrationId:a}:{integrationKey:s},s);o.push(...l)}return o.length>0&&g.debug(`[remote] Fetched ${o.length} ${e}${o.length!==1?"s":""}`),o}async getElement(e){const t=await $.withClient(n=>n[Y[e.type].element](e.data.id).get());return F.fromData(e.type,t)}async createElement(e,t){const n=e.clean();if(this.transformElementForCreate(e,n,t),e.hasParent()&&n.integrationId&&!n.isCustomized){const s=e.getParentKey(),a=new Set(["uuid","integrationId",s,"meta","integrationUuid","parentUuid"]);Object.keys(n).forEach(l=>{a.has(l)||delete n[l]})}const o=await $.withClient(s=>s[Y[e.type].elements].create(n)),i=F.fromData(e.type,o);return g.debug(`[remote] Created ${i.id}`),i}async updateElement(e,t,n){if(!e.data.id)throw new Error("Element must have an id to update");t.data.archivedAt&&await $.withClient(a=>a[Y[e.type].element](t.data.id).restore());const o=e.clean();if(this.transformElementForUpdate(e,o,n),e.hasParent()&&o.integrationId&&!o.isCustomized)return g.debug(`[remote] Skipped update for ${t.id} (non-customized)`),t;const i=await $.withClient(a=>a[Y[e.type].element](t.data.id).put(o)),s=F.fromData(t.type,i);return g.debug(`[remote] Updated ${t.id}`),s}async deleteElement(e){if(!e.data.id)throw new Error("Element must have an id to delete");await $.withClient(t=>t[Y[e.type].element](e.data.id).archive()),g.debug(`[remote] Deleted ${e.id}`)}async getIntegrations(){const e=await $.withClient(n=>n.integrations.findAll());if(!e)return[];const t=e.map(n=>F.fromData(I.Integration,n));return t.length>0&&g.debug(`[remote] Fetched ${t.length} integrations`),t}async getByInternalId(e,t){let n;if(n=await $.withClient(o=>o[Y[t].element](e).get(),!1),!!n)return F.fromData(t,n)}transformElementForCreate(e,t,n){if(e.integrationKey){const o=n.elements.find(i=>i.type===I.Integration&&i.key===e.integrationKey);if(o)t.integrationId=o.data.id;else throw new Error(`Dependency integration ${e.integrationKey} not found for ${e.id}`)}if(e.type===I.Integration){const o=n.connectorsMapping?.[t.connectorId];o&&(t.connectorId=o)}this.transformPackageDependencies(e,t,n),this.transformParentDependency(e,t,n)}transformElementForUpdate(e,t,n){if(e.type===I.Integration){const o=n.connectorsMapping?.[t.connectorId];o&&(t.connectorId=o)}if(e.integrationKey){t.integrationKey=e.integrationKey;const o=n.elements.find(i=>i.type===I.Integration&&i.key===e.integrationKey);if(o)t.integrationId=o.data.id;else throw new Error(`Dependency integration ${e.integrationKey} not found for ${e.id}`)}this.transformPackageDependencies(e,t,n),this.transformParentDependency(e,t,n)}transformPackageDependencies(e,t,n){if(e.type!==I.Package||!t.elements)return;const o=t.elements.map(i=>{const a=(n.sourceElements||n.elements).find(d=>d.data.id===i.id&&d.type===i.type);if(!a)throw new Error(`Package element ${i.type} with id ${i.id} not found in source workspace for package ${e.id}`);const l=n.elements.find(d=>d.type===i.type&&d.key===a.key&&d.integrationKey===a.integrationKey);if(!l)throw new Error(`Package element ${i.type} with key ${a.key} not found in target workspace for package ${e.id}`);return{id:l.data.id,type:i.type}});t.elements=o}transformParentDependency(e,t,n){if(!e.hasParent())return;const o=e.getParentKey();if(!o)return;const i=n.elements.find(s=>s.type===e.type&&s.key===e.key&&!s.hasParent());if(i)t[o]=i.data.id;else throw new Error(`Parent ${e.getParentKey()} not found for ${e.id}`)}async findAll(e,t={},n){const o=await $.withClient(i=>i[Y[e].elements].findAll(t));return o?o.filter(i=>i.key).map(i=>(n&&!i.integrationKey&&(i.integrationKey=n),F.fromData(e,i))):[]}}class It extends jr{static{c(this,"TypedEventEmitter")}on(e,t){return super.on(e,t)}emit(e,...t){return super.emit(e,...t)}off(e,t){return super.off(e,t)}once(e,t){return super.once(e,t)}}const W={Updated:"updated",Deleted:"deleted",Stopped:"stopped"},Fo={ignored:Qr,persistent:!0,ignoreInitial:!0,followSymlinks:!1,depth:20,awaitWriteFinish:{stabilityThreshold:500,pollInterval:100},ignorePermissionErrors:!0,atomic:!0,usePolling:!1,alwaysStat:!1,interval:1e3,binaryInterval:300};class Lo extends It{static{c(this,"LocalElementWatcher")}constructor(e){super(),this.options=e,this.membraneDir=q(this.options.cwd),this.lockTimeoutMs=this.options.lockTimeoutMs??1e3}isWatching=!1;watcher;membraneDir;contentCache={};ignoredPaths=new Set;lockTimeoutMs;async start(){this.isWatching||(v.existsSync(this.membraneDir)||v.mkdirSync(this.membraneDir,{recursive:!0}),this.initializeContentCache(),this.watcher=Fr.watch(this.membraneDir,Fo),this.watcher.on("add",e=>this.handleFileSystemEvent(W.Updated,e)).on("change",e=>this.handleFileSystemEvent(W.Updated,e)).on("unlink",e=>this.handleFileSystemEvent(W.Deleted,e)).on("ready",()=>this.isWatching=!0))}async stop(){!this.isWatching||!this.watcher||(await this.watcher.close(),this.isWatching=!1,this.watcher=void 0,this.clearCache(),this.clearAllLocks(),this.emit(W.Stopped))}getCwd(){return this.options.cwd}async executeWithPathLock(e,t){const n=x.resolve(e);this.ignoredPaths.add(n);try{await t()}finally{setTimeout(()=>{this.ignoredPaths.delete(n)},this.lockTimeoutMs)}}isPathLocked(e){const t=x.resolve(e);if(this.ignoredPaths.has(t))return!0;for(const n of this.ignoredPaths)if(t.startsWith(n+x.sep))return!0;return!1}clearAllLocks(){this.ignoredPaths.clear()}handleFileSystemEvent(e,t){const n=x.relative(this.membraneDir,t);if(this.isPathLocked(t))return;if(e===W.Deleted){this.removeFromCache(n);const a={filePath:t,relativePath:n,data:void 0};g.info(`[local] ${e}: ${a.relativePath}`),this.emit(e,a);return}const o=this.readFileContent(t);if(!this.hasContentChanged(n,o))return;const s=this.processFileEvent(t,o);this.updateCache(n,o),s&&(g.info(`[local] ${e}: ${s.relativePath}`),this.emit(e,s))}readFileContent(e){return v.readFileSync(e,"utf8")}processFileEvent(e,t){const n=x.relative(this.membraneDir,e);let o;try{o=t?Lr.parse(t):void 0}catch{o=void 0}return{filePath:e,relativePath:n,data:o}}hasContentChanged(e,t){if(!t)return this.contentCache[e]!==void 0;const n=this.getContentHash(t);return this.contentCache[e]!==n}getContentHash(e){return Nr.createHash("sha256").update(e).digest("hex")}updateCache(e,t){if(!t){delete this.contentCache[e];return}this.contentCache[e]=this.getContentHash(t)}removeFromCache(e){delete this.contentCache[e]}clearCache(){Object.keys(this.contentCache).forEach(e=>{delete this.contentCache[e]})}initializeContentCache(){v.existsSync(this.membraneDir)&&this.scanDirectoryForCache(this.membraneDir)}scanDirectoryForCache(e){const t=v.readdirSync(e,{withFileTypes:!0});for(const n of t){const o=x.join(e,n.name);if(n.isDirectory())this.scanDirectoryForCache(o);else if(n.isFile())try{const i=v.readFileSync(o,"utf8"),s=x.relative(this.membraneDir,o);this.updateCache(s,i)}catch{}}}}var kt=(r=>(r.Updated="updated",r.ConnectorFileUpdated="connector-file-updated",r.Connected="connected",r.Disconnected="disconnected",r.Error="error",r))(kt||{});const jo={debounceMs:2e3,reconnectIntervalMs:5e3,maxReconnectAttempts:1/0,maxBackoffMs:6e4,connectionTimeoutMs:3e5};class Uo extends It{static{c(this,"RemoteElementWatcher")}constructor(e=jo){super(),this.config=e}eventSource;debounceTimeouts=new Map;reconnectAttempts=0;reconnectTimeout;connectionTimeout;isStarted=!1;isConnected=!1;async start(){this.isStarted||(this.isStarted=!0,await this.connect())}async stop(){this.isStarted&&(this.isStarted=!1,this.isConnected=!1,this.clearReconnectTimeout(),this.clearConnectionTimeout(),this.clearAllDebounceTimeouts(),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0))}async connect(){try{g.debug("[remote-events] Connecting to server"),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0);const e=await $.getClient(process.cwd()),t=await e.getToken();if(!t)throw new Error("No auth token available");const n=`${e.apiUri}/sse/workspace?token=${encodeURIComponent(t)}`;g.debug("[remote-events] Subscribing to workspace events"),this.eventSource=new Ur(n),this.setupEventSourceHandlers()}catch(e){g.debug(`[remote-events] Failed to connect: ${e}`),this.emit("error",{error:e}),this.scheduleReconnect()}}setupEventSourceHandlers(){this.eventSource&&(this.eventSource.onopen=()=>{g.debug("[remote-events] Connected to server"),this.reconnectAttempts=0,this.isConnected=!0,this.resetConnectionTimeout(),this.emit("connected",{})},this.eventSource.onmessage=e=>{try{this.resetConnectionTimeout();const t=JSON.parse(e.data);this.handleElementUpdate(t)}catch(t){g.debug(`[remote-events] Failed to parse workspace event: ${t}`)}},this.eventSource.onerror=e=>{g.debug(`[remote-events] Connection error: ${JSON.stringify(e,null,2)}`),this.isConnected=!1,this.clearConnectionTimeout(),this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.emit("disconnected",{}),this.isStarted&&this.scheduleReconnect()})}handleElementUpdate(e){if(e.type!==mr.ElementUpdate)return;const{elementId:t,elementType:n,data:o={}}=e;if(!(!t||!n)){if(n===I.Connector){const{filePath:i,eventType:s,newPath:a}=o;return g.debug(`[remote-watcher] Received connector event - elementId: ${t}, filePath: ${i}, eventType: ${s}`),this.scheduleConnectorFileUpdate(t,i,s,a)}return this.scheduleElementUpdate(t,n)}}scheduleElementUpdate(e,t){const n=this.debounceTimeouts.get(e);n&&clearTimeout(n);const o=setTimeout(()=>{this.debounceTimeouts.delete(e),this.emit("updated",{elementId:e,elementType:t})},this.config.debounceMs);this.debounceTimeouts.set(e,o)}scheduleConnectorFileUpdate(e,t,n,o){const i=JSON.stringify({connectorId:e,filePath:t}),s=this.debounceTimeouts.get(i);s&&clearTimeout(s);const a=setTimeout(()=>{this.debounceTimeouts.delete(i),this.emit("connector-file-updated",{connectorId:e,filePath:t,eventType:n,newPath:o})},this.config.debounceMs);this.debounceTimeouts.set(i,a)}scheduleReconnect(){if(!this.isStarted||this.reconnectAttempts>=this.config.maxReconnectAttempts){this.reconnectAttempts>=this.config.maxReconnectAttempts&&(g.error("[remote-events] Max reconnection attempts reached. Connection will not be retried."),this.emit("error",{error:new Error("Max reconnection attempts reached")}));return}this.reconnectTimeout&&clearTimeout(this.reconnectTimeout),this.reconnectAttempts++;const e=this.config.reconnectIntervalMs*Math.pow(2,this.reconnectAttempts-1),t=Math.random()*.3*e,n=Math.min(e+t,this.config.maxBackoffMs);g.debug(`[remote-events] Reconnecting in ${Math.round(n)}ms (attempt ${this.reconnectAttempts})`),this.reconnectTimeout=setTimeout(()=>{this.connect()},n)}clearReconnectTimeout(){this.reconnectTimeout&&(clearTimeout(this.reconnectTimeout),this.reconnectTimeout=void 0)}resetConnectionTimeout(){this.clearConnectionTimeout(),this.connectionTimeout=setTimeout(()=>{this.isConnected&&this.isStarted&&(g.debug("[remote-events] Connection timeout detected, reconnecting..."),this.isConnected=!1,this.eventSource&&(this.eventSource.close(),this.eventSource=void 0),this.scheduleReconnect())},this.config.connectionTimeoutMs)}clearConnectionTimeout(){this.connectionTimeout&&(clearTimeout(this.connectionTimeout),this.connectionTimeout=void 0)}clearAllDebounceTimeouts(){this.debounceTimeouts.forEach(e=>clearTimeout(e)),this.debounceTimeouts.clear()}}class An{static{c(this,"ElementsCache")}elements=new Map;typeIndex=new Map;internalIdIndex=new Map;constructor(e){e&&this.addAll(e)}add(e){const t=e.id;this.elements.set(t,e),this.typeIndex.has(e.type)||this.typeIndex.set(e.type,new Set),this.typeIndex.get(e.type).add(t),e.data?.id&&this.internalIdIndex.set(e.data.id,t)}remove(e){const t=e.id,n=this.elements.delete(t);if(n){const o=this.typeIndex.get(e.type);o&&(o.delete(t),o.size===0&&this.typeIndex.delete(e.type)),e.data?.id&&this.internalIdIndex.delete(e.data.id)}return n}put(e){this.elements.has(e.id)&&this.remove(e),this.add(e)}get(e){return this.elements.get(e)}getByInternalId(e){const t=this.internalIdIndex.get(e);if(t)return this.elements.get(t)}getElementsByType(e){const t=this.typeIndex.get(e);return t?Array.from(t).map(n=>this.elements.get(n)).filter(n=>n!==void 0):[]}getAll(){return Array.from(this.elements.values())??[]}getTypes(){return Array.from(this.typeIndex.keys())}addAll(e){for(const t of e)t&&this.add(t)}removeAll(e){for(const t of e)this.remove(t)}getElementIdsByType(e){return this.typeIndex.get(e)}clear(){this.elements.clear(),this.typeIndex.clear(),this.internalIdIndex.clear()}getAllIds(){return new Set(this.elements.keys())}}const be="connectors",ee="development",$t={};async function Rn(r={}){const{onProgress:e}=r,t=new Set,o=(await $.withClient(d=>d.get("org-workspace-id"))).id,i={};g.info("[connectors] Loading custom connectors"),await $.withClient(d=>d.get(`/connectors?workspaceId=${o}`)),g.info("[connectors] Loading public connectors");const s=re(),l=(A.existsSync(s)?A.readdirSync(s):[]).filter(d=>{if(d.startsWith("."))return!1;const h=k.join(s,d);try{return A.statSync(h).isDirectory()}catch{return!1}});for(const d of l){g.info(`[connectors] Loading connector from: ${d}`);const h=A.readdirSync(k.join(s,d)),f=await Pt(d);if(!f)continue;e?.("pushing",f.name),"baseUri"in f&&delete f.baseUri;let p;f.uuid&&(p=await $.withClient(S=>S.get(`/connectors/${f.uuid}`),!1));const C=f.uuid;if(p)i[C]=p.id,g.info(`[connectors] Matched ${f.name} uuid: ${f.uuid}`),p.isPublic||(p.archivedAt&&(g.info(`[connectors] Restoring archived connector ${f.name}`),await $.withClient(S=>S.post(`connectors/${p.id}/restore`))),g.info(`[connectors] Updating connector ${f.name}`),await $.withClient(S=>S.patch(`connectors/${p.id}`,{...f,workspaceId:o})));else if(!i[C]&&!p?.isPublic){let S=!1;try{const R=await At({connectorId:C});R&&R.isPublic&&(S=!0)}catch{}if(!S){g.info(`[connectors] Creating custom connector ${f.name} (${f.key})`);const R=await $.withClient(O=>O.post("connectors",{...f,workspaceId:o}));i[C]=R.id}}const E=h.filter(S=>A.statSync(k.join(s,d,S)).isDirectory());for(const S of E)await qo({connector:f,version:S,connectorId:i[C]}),t.add(C);e?.("pushed",f.name)}return{connectorsMapping:i,pushedConnectors:Array.from(t)}}c(Rn,"pushConnectors");async function On({connectorId:r,connectorVersion:e,allConnectors:t,pulledConnectors:n,pulledConnectorVersions:o}){if(!r||o[r]?.has(e))return;const i=un(),s=await At({connectorId:r});if(!s.isPublic||t){if(!s?.key){console.error(`[connectors] Connector ${r} has no key. Skipping..`),g.error(`[connectors] Connector ${r} has no key. Skipping..`);return}n.has(r)||(await Bo({basePath:i,connector:s}),n.add(r)),o[r]||(o[r]=new Set),o[r].has(e)||(await Wo({connector:s,connectorVersion:e,basePath:i}),o[r].add(e))}}c(On,"pullRemoteConnector");function re(){const r=ft();return k.join(r.membraneDirPath,be)}c(re,"getConnectorsPath");async function Pt(r){const e=k.join(re(),r,`${r}.yml`);return xt(e,!1)}c(Pt,"readConnector");async function Ko(r,e){return g.info(`[connectors] Zipping ${r} into ${e}`),new Promise((t,n)=>{const o=A.createWriteStream(e),i=ot("zip",{zlib:{level:9}});o.on("close",()=>{g.success(`[connectors] Successfully created ${e}`),t()}),o.on("end",()=>{g.info("[connectors] Data has been drained")}),i.on("warning",a=>{a.code==="ENOENT"?console.warn(a):n(a)}),i.on("error",a=>{n(a)}),i.pipe(o);const s=A.readdirSync(r);for(const a of s){const l=k.join(r,a),d=A.statSync(l);d.isFile()?i.file(l,{name:a}):d.isDirectory()&&i.directory(l,a)}i.finalize()})}c(Ko,"createZipArchive");async function _o(r,e){return g.info(`[connectors] Unzipping into ${e}`),new Promise((t,n)=>{const o=je.Parse();o.on("entry",i=>{const s=i.path;if(i.type==="Directory"){const l=k.join(e,s);A.mkdirSync(l,{recursive:!0}),i.autodrain()}else{const l=k.join(e,s),d=k.dirname(l);A.mkdirSync(d,{recursive:!0});const h=A.createWriteStream(l);i.pipe(h),h.on("finish",()=>{})}}),o.on("end",()=>{g.success(`[connectors] Successfully extracted to ${e}`),t()}),o.on("error",i=>{n(i)}),o.write(r),o.end()})}c(_o,"extractZipArchive");async function qo({connector:r,version:e,connectorId:t}){const n=k.join(re(),te(r),Dn(e)),o=k.join(n,"src"),i=k.join(n,"src.zip"),s=A.existsSync(i);if(A.existsSync(o)&&(g.info(`[connectors] Archiving source code for ${r.name} version ${e}`),await Ko(o,i)),!A.existsSync(i)){g.warning(`[connectors] No source code found for ${r.name} version ${e}`);return}try{const a=new en;if(a.append("file",A.createReadStream(i),"file.zip"),g.info(`[connectors] Pushing connector version ${e} for ${r.name}`),e==ee)g.info(`[connectors] Uploading connector ${t}`),await $.withClient(l=>l.post(`connectors/${t}/upload`,a,{headers:{...a.getHeaders()}}));else{if(a.append("version",e),a.append("changelog","Imported Version"),(await $.withClient(d=>d.get(`/connectors/${t}/versions`))).find(d=>d.version==e)){g.info(`[connectors] Version ${e} already published`);return}g.info(`[connectors] Publishing version ${e} of connector ${t}`),await $.withClient(d=>d.post(`connectors/${t}/publish-version`,a,{headers:{...a.getHeaders()}}))}g.success(`Successfully pushed connector version ${e} for ${r.name}`)}catch(a){g.error(`Error pushing connector version ${e} for ${r.name}: ${a}`),console.error(`[connectors] Error pushing connector version ${e} for ${r.name}: ${a}`)}finally{!s&&A.existsSync(i)&&(g.info(`[connectors] Cleaning up temporary zip file for ${r.name} version ${e}`),A.unlinkSync(i))}}c(qo,"pushConnectorVersion");async function At({connectorId:r}){if(r){if($t[r])return $t[r];try{const e=await $.withClient(t=>t.get(`connectors/${r}`),!1);return $t[r]=e,e}catch(e){return g.error(`[connectors] Failed to get connector ${r}: ${e}`),console.error(`[connectors] Failed to get connector ${r}: ${e}`),null}}}c(At,"getConnector");async function Bo({basePath:r,connector:e}){const t=te(e),n=k.join(r,be,t);A.mkdirSync(n,{recursive:!0});const o=k.join(n,`${te(e)}.yml`);A.writeFileSync(o,H.dump(e)),g.info(`[connectors] Pulled connector ${e.name}`)}c(Bo,"pullConnector$1");async function Wo({connector:r,connectorVersion:e,basePath:t}){const n=te(r),o=Dn(e),i=k.join(t,"connectors",n,o),s=await $.withClient(l=>l.get(`connectors/${r.id}/download`,{version:e},{responseType:"arraybuffer",headers:{Accept:"application/zip"},timeout:1e6}));A.mkdirSync(i,{recursive:!0});const a=k.join(i,"src.zip");if(A.writeFileSync(a,s),!e){const l=k.join(i,"src");A.mkdirSync(l,{recursive:!0}),await _o(s,l)}g.info(`[connectors] Pulled connector version: ${r.name} (${o})`)}c(Wo,"pullConnectorVersion");function te(r){return r.key}c(te,"getConnectorDirName");function Dn(r){return r??ee}c(Dn,"getConnectorVersionDirName");function Jo(r){const e=un(),t=te(r);return k.join(e,be,t)}c(Jo,"getConnectorDirPath");function zo(r){return r.match(`${be}/[^/]+/${ee}/src/.*`)!==null}c(zo,"isConnectorSourceFile");async function Ho(r){const e=r.match(`${be}/([^/]+)/${ee}/src/(.*)`);if(!e)return;const t=e[1],n=e[2],o=await Pt(t);if(!o){g.warning(`[connectors] Connector ${t} not found. Ignoring file change`);return}const i=o.id,s=A.readFileSync(r,"utf-8");await $.withClient(a=>a.put(`connectors/${i}/files/${n}`,s,{headers:{"Content-Type":"text/plain"}})),g.info(`[connectors] Pushed file ${n} for connector ${o.name}`)}c(Ho,"putConnectorFile");async function Go(r){const e=r.match(`${be}/([^/]+)/${ee}/src/(.*)`);if(!e)return;const t=e[1],n=e[2],o=await Pt(t);if(!o){g.warning(`[connectors] Connector ${t} not found. Ignoring file change`);return}const i=o.id;await $.withClient(s=>s.delete(`connectors/${i}/files/${n}`)),g.info(`[connectors] Deleted file ${n} for connector ${o.name}`)}c(Go,"deleteConnectorFile");async function Vo(r,e){try{const t=await $.withClient(s=>s.get(`connectors/${r}`));if(!t){g.warning(`[connectors] Connector ${r} not found`);return}const n=await $.withClient(s=>s.get(`connectors/${r}/files/${e}`)),o=te(t),i=k.join(re(),o,ee,"src",e);A.mkdirSync(k.dirname(i),{recursive:!0}),n!=null?(A.writeFileSync(i,n),g.info(`[connectors] Pulled file ${e} for connector ${t.name}`)):A.existsSync(i)&&(A.unlinkSync(i),g.info(`[connectors] Deleted file ${e} for connector ${t.name}`))}catch(t){g.error(`[connectors] Failed to pull connector file ${e} for connector ${r}: ${t}`),console.error(`[connectors] Failed to pull connector file ${e} for connector ${r}: ${t}`)}}c(Vo,"pullConnectorFile");async function Yo(r,e){const t=await $.withClient(i=>i.get(`connectors/${r}`),!1);if(!t){g.warning(`[connectors] Connector ${r} not found`);return}const n=te(t),o=k.join(re(),n,ee,"src",e);A.existsSync(o)&&(A.unlinkSync(o),g.info(`[connectors] Deleted file ${e} for connector ${n}`))}c(Yo,"deleteLocalConnectorFile");async function Zo(r,e,t){if(t&&e!==t)try{const n=await $.withClient(l=>l.get(`connectors/${r}`),!1);if(!n){g.warning(`[connectors] Connector ${r} not found`);return}const o=te(n),i=k.join(re(),o,ee,"src"),s=k.join(i,e),a=k.join(i,t);A.existsSync(s)&&(A.mkdirSync(k.dirname(a),{recursive:!0}),A.renameSync(s,a),g.info(`[connectors] Renamed directory from ${e} to ${t} for connector ${o}`))}catch(n){g.error(`[connectors] Failed to rename directory ${e} to ${t} for connector ${r}: ${n}`)}}c(Zo,"renameLocalConnectorDirectory");async function Xo(r,e){try{const t=await $.withClient(i=>i.get(`connectors/${r}`),!1);if(!t){g.warning(`[connectors] Connector ${r} not found`);return}const n=te(t),o=k.join(re(),n,ee,"src",e);if(A.existsSync(o)){const i=k.resolve(re());if(!k.resolve(o).startsWith(i))return;A.rmSync(o,{recursive:!0,force:!0}),g.info(`[connectors] Deleted directory ${e} for connector ${n}`)}}catch(t){g.error(`[connectors] Failed to delete directory ${e} for connector ${r}: ${t}`)}}c(Xo,"deleteLocalConnectorDirectory");const j={LogAdded:"logAdded",StateChanged:"stateChanged",StatsChanged:"statsChanged",ConflictsChanged:"conflictsChanged",McpStatusChanged:"mcpStatusChanged",McpServersChanged:"mcpServersChanged",ConfigChanged:"configChanged"};class Qo extends It{static{c(this,"WorkspaceNotifications")}constructor(e){super(),this.config=e,g.setNotificationHandler(this)}clientId;heartbeatInterval;isCleaningUp=!1;async connectToRemote(){await this.registerWithRemoteServer(),await this.startHeartbeatLoop()}async setState(e){this.emit(j.StateChanged,{state:e}),await this.emitRemote({status:e})}setConflicts(e){this.emit(j.ConflictsChanged,{conflicts:e})}setConfig(e){this.emit(j.ConfigChanged,{config:e})}setStats(e){this.emit(j.StatsChanged,{stats:e})}addLog(e){this.emit(j.LogAdded,{log:e})}setMcpStatus(e){this.emit(j.McpStatusChanged,{status:e})}async setMcpServers(e){this.emit(j.McpServersChanged,{servers:e}),await this.emitRemote({mcpServers:e.map(t=>({name:t.agentName,totalRequests:t.totalRequests}))})}async cleanup(){!this.clientId||this.isCleaningUp||(this.isCleaningUp=!0,this.heartbeatInterval&&(clearInterval(this.heartbeatInterval),this.heartbeatInterval=void 0),await this.withErrorHandling(async()=>{await $.withClient(e=>e.delete(`/local-clients/${this.clientId}`))}),this.clientId=void 0)}async registerWithRemoteServer(){if(this.clientId)return;const e=await this.withErrorHandling(async()=>await $.withClient(t=>t.post("/local-clients",{hostname:tt.hostname(),workingDirectory:process.cwd()})));e?.id?this.clientId=e.id:this.addLog({timestamp:new Date().toISOString(),message:"Failed to register with remote server",type:"error"})}async startHeartbeatLoop(){this.heartbeatInterval=setInterval(async()=>{this.clientId&&await this.sendHeartbeat()},this.config.heartbeatIntervalMs)}async sendHeartbeat(){this.clientId&&await this.withErrorHandling(async()=>{await $.withClient(e=>e.post(`/local-clients/${this.clientId}/keep-alive`,{}))})}async emitRemote(e){this.clientId&&await this.withErrorHandling(async()=>{await $.withClient(t=>t.patch(`/local-clients/${this.clientId}`,e))})}async withErrorHandling(e){try{return await e()}catch(t){const n=t instanceof Error?t.message:String(t);return this.addLog({timestamp:new Date().toISOString(),message:`Failed to connect to remote: ${n}`,type:"error"}),null}}}const oe=new Qo({heartbeatIntervalMs:15e3}),Nn=[I.AppDataSchema,I.AppEventType,I.DataLinkTable,I.DataSource,I.FieldMapping,I.Action,I.Flow,I.Package];class De{static{c(this,"ElementSyncService")}localWatcher=void 0;remoteWatcher=void 0;notifier;changes=[];localCache;remoteCache;localRepo;remoteRepo;pulledConnectors=new Set;pulledConnectorVersions={};constructor(){this.notifier=oe,this.localCache=new An,this.remoteCache=new An,this.localRepo=new No(this.localCache),this.remoteRepo=new Mo(this.remoteCache)}clear(){this.changes=[]}needsForcedSync(){return this.changes.some(e=>e.isConflict)}needsSync(){return this.changes.length>0}async getStats(){const e=this.localCache.getAll(),t={};return e.forEach(n=>{t[n.type]=(t[n.type]||0)+1}),t}async fetchElements(){const e=await this.localRepo.getElements(),t=await this.remoteRepo.getElements();this.localCache.addAll(e),this.remoteCache.addAll(t)}async updateElement(e,t){const n=this.getHandler(t),o=this.getCache(t);try{const i=await n.putElement(e);o.put(i)}catch(i){throw g.error(`Failed to update element ${e.id}: ${i}`),i}}async deleteElement(e,t){const n=this.getHandler(t),o=this.getCache(t);try{await n.deleteElement(e,{elements:n.cache.getAll(),connectorsMapping:n.connectorsMapping}),o.remove(e)}catch(i){throw g.error(`Failed to delete element ${e.id}: ${i}`),i}}async pullConnectors(e=!1){const t=this.remoteCache.getElementsByType(I.Integration).map(n=>n.data);for(const n of t){const o=n.connectorId,i=n.connectorVersion;o&&await On({connectorId:o,connectorVersion:i,allConnectors:e,pulledConnectors:this.pulledConnectors,pulledConnectorVersions:this.pulledConnectorVersions})}}async pushConnectors(){const{connectorsMapping:e}=await Rn();this.remoteRepo.setConnectorsMapping(e)}getHandler(e){return e===B.INCOMING?this.localRepo:(this.remoteRepo.setSourceCache(this.localCache),this.remoteRepo)}getCache(e){return e===B.INCOMING?this.localCache:this.remoteCache}async startWatching(){this.localWatcher=new Lo({cwd:process.cwd(),lockTimeoutMs:1e3}),this.localWatcher.on(W.Updated,e=>this.handleLocalEvent(e,W.Updated)),this.localWatcher.on(W.Deleted,e=>this.handleLocalEvent(e,W.Deleted)),await this.localWatcher.start(),g.success("[local] Tracking changes.."),this.remoteWatcher=new Uo,this.remoteWatcher.on(kt.Updated,({elementId:e,elementType:t})=>this.handleRemoteElementEvent(e,t)),this.remoteWatcher.on(kt.ConnectorFileUpdated,({connectorId:e,filePath:t,eventType:n,newPath:o})=>this.handleRemoteConnectorFileEvent(e,t,n,o)),await this.remoteWatcher.start(),g.success("[remote] Tracking changes..")}async stopWatching(){this.localWatcher&&(await this.localWatcher.stop(),this.localWatcher=void 0),this.remoteWatcher&&(await this.remoteWatcher.stop(),this.remoteWatcher=void 0)}async handleRemoteElementEvent(e,t){try{const n=await this.remoteRepo.getByInternalId(e,t),o=!!n?.data.archivedAt||!!n?.data.isDeactivated;if(!n||o){const s=n||this.remoteCache.getByInternalId(e);return s?(g.info(`[${this.getDirectionLabel(B.INCOMING)}] Deleted: ${s.id}`),this.localWatcher?.executeWithPathLock(s.absolutePath,()=>this.deleteElement(s,B.INCOMING))):void 0}if(g.info(`[${this.getDirectionLabel(B.INCOMING)}] Updated: ${n.id}`),await this.localWatcher?.executeWithPathLock(n.absolutePath,async()=>this.updateElement(n,B.INCOMING)),t===I.Integration){const s=n.data.connectorId,a=n.data.connectorVersion,l=await At({connectorId:s});if(!l?.key)return;const d=Jo(l);await this.localWatcher?.executeWithPathLock(d,async()=>On({connectorId:s,connectorVersion:a,allConnectors:!1,pulledConnectors:this.pulledConnectors,pulledConnectorVersions:this.pulledConnectorVersions}))}}catch(n){g.error(`[sync] Error handling remote event: ${n}`)}}async handleRemoteConnectorFileEvent(e,t,n,o){try{switch(n){case Ye.ConnectorFileDeleted:await Yo(e,t);break;case Ye.ConnectorDirectoryRenamed:await Zo(e,t,o);break;case Ye.ConnectorDirectoryDeleted:await Xo(e,t);break;default:await Vo(e,t);break}}catch(i){g.error(`[sync] Error handling remote connector file event: ${i}`)}}async handleLocalEvent(e,t){try{if(zo(e.filePath))switch(t){case W.Updated:await Ho(e.filePath);break;case W.Deleted:await Go(e.filePath);break}else{let n=F.fromPathAndData(e.filePath,e.data);if(!n){const o=F.idFromPath(e.filePath);if(!o||(n=this.remoteCache.get(o),!n))return}switch(g.info(`[${this.getDirectionLabel(B.OUTGOING)}] ${Or(t)}: ${n.id}`),t){case W.Updated:await this.updateElement(n,B.OUTGOING);break;case W.Deleted:await this.deleteElement(n,B.OUTGOING);break}}}catch(n){g.error(`[sync] Error handling local event: ${n}`)}}detectIncomingChanges(){this.clearChanges();const e=De.getChanges(B.INCOMING,this.remoteCache,this.localCache);return this.updateChanges(e),e}detectOutgoingChanges(){this.clearChanges();const e=De.getChanges(B.OUTGOING,this.localCache,this.remoteCache);return this.updateChanges(e),e}async resolveChanges(){if(!this.needsSync())return;g.info("[resolver] Resolving changes.."),g.info("[resolver] Resolving integration elements");const e=this.changes.filter(o=>o.element.type===I.Integration);await Promise.all(e.map(async o=>this.resolveChange(o))),e.length>0&&(await this.fetchElements(),this.changes=this.changes.length>0&&this.changes[0]?.direction===B.INCOMING?this.detectIncomingChanges():this.detectOutgoingChanges(),this.changes=this.changes.filter(o=>o.element.type!==I.Integration)),g.info("[resolver] Resolving universal elements");const t=this.changes.filter(o=>!o.element.hasParent());for(const o of Nn){const i=t.filter(s=>s.element.type===o);g.info(`[resolver] Resolving ${o} elements`),await Promise.all(i.map(async s=>this.resolveChange(s)))}g.info("[resolver] Resolving integration level elements");const n=this.changes.filter(o=>o.element.hasParent());for(const o of Nn){const i=n.filter(s=>s.element.type===o);g.info(`[resolver] Resolving ${o} elements`),await Promise.all(i.map(async s=>this.resolveChange(s)))}g.success("[resolver] Resolved changes")}async resolveChange(e){switch(e.type){case Q.DELETE:return this.deleteElement(e.element,e.direction);case Q.CREATE:return this.updateElement(e.element,e.direction);case Q.UPDATE:return this.updateElement(e.element,e.direction)}}static getChanges(e,t,n){const o=t.getAllIds(),i=n.getAllIds(),s=new Set([...o,...i]),a=[];for(const l of s){const d=t.get(l),h=n.get(l),f=De.detectChangeForElement(d,h,e);f&&a.push(f)}return a}updateChanges(e){if(this.changes=e,this.needsForcedSync()){const t=e.filter(n=>n.isConflict);g.warning("[resolver] Conflicts detected"),this.notifier.setConflicts(t)}}clearChanges(){this.changes=[]}getDirectionLabel(e){switch(e){case B.INCOMING:return"local\u2190remote";case B.OUTGOING:return"local\u2192remote"}}static detectChangeForElement(e,t,n){return e&&!t?{type:Q.CREATE,element:e,direction:n,isConflict:!1}:!e&&t?{type:Q.DELETE,element:t,direction:n,isConflict:!0}:e&&t&&!e.isEqual(t)?{type:Q.UPDATE,element:e,direction:n,isConflict:!1}:null}}const ie=x.join(Kr.tmpdir(),"membrane-mcp-status"),Mn=3e4;class ei{static{c(this,"McpStatusService")}constructor(e=2e3){this.pollIntervalMs=e}isRunning=!1;pollInterval;async start(){this.isRunning||(this.isRunning=!0,this.pollInterval=setInterval(async()=>await this.checkStatus(),this.pollIntervalMs),await this.checkStatus())}async stop(){this.pollInterval&&(clearInterval(this.pollInterval),this.pollInterval=void 0,this.isRunning=!1)}async checkStatus(){try{const e=process.cwd(),t=We(void 0,e),n=Fn(e);t&&oe.setMcpStatus(t),await oe.setMcpServers(n)}catch{g.error("Failed to check MCP status")}}}function We(r,e){try{const t=e||process.cwd();if(!r){const o=Fn(t);return o.length===0?null:o[0]}const n=Dt(r,t);if(v.existsSync(n)){const o=v.statSync(n),i=new Date;if(i.getTime()-o.mtime.getTime()>Mn)return Re(r,t),null;const a=v.readFileSync(n,"utf8"),l=JSON.parse(a);if(l.isRunning){const d=new Date(l.lastActivity).getTime();if(i.getTime()-d>Mn)return Re(r,t),null}return l}}catch{r&&e&&Re(r,e)}return null}c(We,"getMcpStatus");function Fn(r){try{const e=r||process.cwd(),t=Ot(e);if(!v.existsSync(ie))return[];const n=v.readdirSync(ie),o=[];for(const i of n){const s=i.match(new RegExp(`^mcp-${t}-(\\d+)\\.json$`));if(s){const a=parseInt(s[1],10),l=We(a,e);l&&o.push(l)}}return o.sort((i,s)=>new Date(s.startTime).getTime()-new Date(i.startTime).getTime())}catch{return[]}}c(Fn,"getAllMcpStatusFiles");function Rt(r){try{const t={...We(r.processId,r.cwd)||{isRunning:!1,startTime:new Date().toISOString(),lastActivity:new Date().toISOString(),toolsCount:0,totalRequests:0,processId:r.processId,cwd:r.cwd,agentName:process.env.AGENT_NAME||"Unnamed Agent"},...r};v.existsSync(ie)||v.mkdirSync(ie,{recursive:!0});const n=Dt(r.processId,r.cwd);v.writeFileSync(n,JSON.stringify(t,null,2))}catch{}}c(Rt,"updateMcpStatus");function Re(r,e){try{const t=e||process.cwd();if(r){const n=Dt(r,t);v.existsSync(n)&&v.unlinkSync(n)}else{const n=Ot(t);if(v.existsSync(ie)){const o=v.readdirSync(ie);for(const i of o)i.match(new RegExp(`^mcp-${n}-\\d+\\.json$`))&&v.unlinkSync(x.join(ie,i))}}}catch{}}c(Re,"clearMcpStatus");function ti(r,e){const t=We(r,e);t&&Rt({processId:r,cwd:e,totalRequests:t.totalRequests+1,lastRequestTime:new Date().toISOString(),lastActivity:new Date().toISOString()})}c(ti,"trackToolExecution");function Ot(r){return Mr("md5").update(r).digest("hex").slice(0,8)}c(Ot,"getCwdHash");function Dt(r,e){const t=Ot(e);return x.join(ie,`mcp-${t}-${r}.json`)}c(Dt,"getStatusFilePath");const Je={Agent:"agent",Cli:"cli"},P={NOT_INITIALIZED:"not_initialized",SETTING_UP:"setting_up",INITIALIZED:"initialized",NOT_SYNCED:"not_synced",PULLING:"pulling",PUSHING:"pushing",RESOLVING:"resolving",CONFLICTS:"conflicts",SYNCED:"synced",WATCHING:"watching",ERROR:"error"};class ni{static{c(this,"MembraneCLIService")}constructor(e,t,n=()=>process.exit(0)){this.mode=e,this.cwd=t,this.onShutdown=n,this.notifier=oe,this.mcpStatusService=new ei,this.syncService=new De,this.setupProcessCleanup()}initialized=!1;notifier;mcpStatusService;syncService;isShuttingDown=!1;currentConfig=null;get config(){return this.currentConfig}getSyncedElements(){return this.syncService.localCache.getAll()}getSyncedElementsByType(e){return this.syncService.localCache.getElementsByType(e)}async fetchElements(){await this.syncService.fetchElements()}async pullWorkspace(e={}){let t=!1;try{if(g.setVerboseMode(!!e.verbose),e.rps!==void 0&&$.init({maxRequestsPerSecond:e.rps}),await this.notifier.setState(P.PULLING),await this.syncService.fetchElements(),this.syncService.detectIncomingChanges(),this.syncService.needsForcedSync()&&!e.force){await this.notifier.setState(P.CONFLICTS),e.watch||(t=!0);return}await this.syncService.pullConnectors(e.allConnectors),await this.syncWorkspaces(e)}catch(n){t=!0,g.error(`Failed to pull workspace: ${n}`),await this.notifier.setState(P.ERROR),g.saveLogsToFile("error")}finally{if(e.saveLogs&&g.saveLogsToFile(),t)process.exit(1);else return e?.onComplete?.()}}async pushWorkspace(e={}){let t=!1;try{if(g.setVerboseMode(!!e.verbose),e.rps!==void 0&&$.init({maxRequestsPerSecond:e.rps}),await this.notifier.setState(P.PUSHING),await this.syncService.fetchElements(),this.syncService.detectOutgoingChanges(),this.syncService.needsForcedSync()&&!e.force){await this.notifier.setState(P.CONFLICTS),e.watch||(t=!0);return}await this.syncService.pushConnectors(),await this.syncWorkspaces(e)}catch(n){t=!0,g.error(`Failed to push workspace: ${n}`),await this.notifier.setState(P.ERROR),g.saveLogsToFile("error")}finally{if(e.saveLogs&&g.saveLogsToFile(),t)process.exit(1);else return e?.onComplete?.()}}async syncWorkspaces(e={}){try{e.verbose!==void 0&&g.setVerboseMode(!!e.verbose),await this.notifier.setState(P.RESOLVING),this.syncService.needsSync()&&await this.syncService.resolveChanges();const t=await this.syncService.getStats();this.notifier.setStats(t),await this.notifier.setState(P.SYNCED),e.watch&&(await this.notifier.setState(P.WATCHING),await this.syncService.startWatching())}catch(t){g.error(`Failed to sync local and remote workspaces: ${t}`),await this.notifier.setState(P.ERROR),g.saveLogsToFile("error")}}async init({force:e=!1}={}){if(!(this.initialized&&!e)){await this.notifier.setState(P.NOT_INITIALIZED);try{await this.loadConfig(),pe.isCacheDefined()?(await this.initServices(),this.initialized=!0,await this.notifier.setState(P.INITIALIZED)):(this.initialized=!1,await this.notifier.setState(P.SETTING_UP))}catch(t){g.error(`Failed to initialize services: ${t}`),await this.notifier.setState(P.ERROR),g.saveLogsToFile("error"),this.onShutdown()}}}async loadConfig(){this.currentConfig=pe.loadConfig(this.cwd),this.notifier.setConfig(this.currentConfig)}async updateConfig(e){const t={...this.currentConfig,...e};if(!(JSON.stringify(t)!==JSON.stringify(this.currentConfig)))return;await this.stopServices();const o=pe.updateConfig(t);this.currentConfig=o,await this.init({force:!0})}async shutdown(){!this.initialized||this.isShuttingDown||(this.isShuttingDown=!0,this.mode===Je.Agent&&(await this.notifier.cleanup(),await this.mcpStatusService.stop()),this.initialized=!1,this.onShutdown())}async initServices(){this.mode===Je.Agent&&(await this.notifier.connectToRemote(),await this.mcpStatusService.start()),this.syncService.clear()}async stopServices(){this.mode===Je.Agent&&(await this.notifier.cleanup(),await this.mcpStatusService.stop())}setupProcessCleanup(){["SIGINT","SIGTERM","uncaughtException","unhandledRejection"].forEach(t=>process.on(t,()=>this.shutdown())),process.on("beforeExit",t=>{t===0&&this.shutdown()})}}const Ln=at(null);function ri({children:r,membraneCLIService:e}){const{data:t}=on("/account"),[n,o]=N(P.NOT_INITIALIZED),[i,s]=N([]),[a,l]=N({}),[d,h]=N([]),[f,p]=N(null),E=t?.workspaces?.find(S=>S.workspaceKey===f?.workspaceKey)||null;return we(()=>{const S=c(({state:M})=>o(M),"handleStateChanged"),R=c(({stats:M})=>l(M),"handleStatsChanged"),O=c(({log:M})=>h(ye=>[...ye,M]),"handleLogAdded"),U=c(({conflicts:M})=>s(M),"handleConflictsUpdated"),L=c(({config:M})=>p(M),"handleConfigChanged");return e.notifier.on(j.StateChanged,S),e.notifier.on(j.StatsChanged,R),e.notifier.on(j.LogAdded,O),e.notifier.on(j.ConflictsChanged,U),e.notifier.on(j.ConfigChanged,L),e.init(),()=>{e.notifier.off(j.StateChanged,S),e.notifier.off(j.StatsChanged,R),e.notifier.off(j.LogAdded,O),e.notifier.off(j.ConflictsChanged,U),e.notifier.off(j.ConfigChanged,L)}},[]),u(Ln.Provider,{value:{state:n,stats:a,logs:d,currentWorkspace:E,conflicts:i,config:f,updateConfig:c(S=>e.updateConfig(S),"updateConfig"),resolveConflicts:c(S=>e.syncWorkspaces(S),"resolveConflicts"),pull:c(S=>e.pullWorkspace(S),"pull"),push:c(S=>e.pushWorkspace(S),"push"),exit:c(()=>e.shutdown(),"exit"),fetchElements:c(()=>e.fetchElements(),"fetchElements"),getSyncedElementsByType:c(S=>e.getSyncedElementsByType(S),"getSyncedElementsByType")},children:r})}c(ri,"MembraneCLIServiceProvider");function G(){const r=ct(Ln);if(!r)throw new Error("useMembraneCLIService must be used within MembraneCLIServiceProvider");return r}c(G,"useMembraneCLIService");const Nt=at(null),jn=c(()=>{const r=ct(Nt);if(!r)throw new Error("useTree must be used within TreeView");return r},"useTree"),oi=c(r=>de.Children.count(r)>0,"hasChildren");function ii(r){const e=jn(),{label:t,value:n,isInitiallyExpanded:o}=r,i=Pr(),s=lt(e.registerChildItem(t)).current,a=e.isActiveByRef(s),[l,d]=N(o??!1),h={props:r,ref:s,id:i,active:a,expanded:l,setExpanded:d,label:t,value:n,isInitiallyExpanded:o,isParent:!1},{children:f}=r,p=typeof f=="function"?f(h):f;return{...h,props:{...r,children:p},isParent:oi(p)}}c(ii,"useTreeItem");function ve(r){const e=jn(),t=ii(r),{expanded:n,isParent:o,props:{children:i},setExpanded:s}=t,{renderValue:a=ai}=r,l=t.ref;Fe(()=>{function p(C){return r.onInput?.(C)?!0:C.active&&C.key.rightArrow&&!n?(s(!0),!0):C.active&&C.key.leftArrow&&n?(s(!1),!0):!1}c(p,"onInput"),l.onInput=p},[l,r.onInput,n,s]),l.firstChild??={parent:l,index:0};let d=l.firstChild;function h(p){const C=d;return C.nextSibling||(C.nextSibling={parent:l,prevSibling:C,index:C.index+1}),d=C.nextSibling,C.label=p,C}c(h,"registerChildItem");function f(){l.lastRenderedChild=d.prevSibling}return c(f,"commitChildren"),Fe(()=>{f()}),b(Le,{children:[b(y,{marginLeft:e.depth*2,children:[u(y,{width:2,children:o&&u(w,{children:n?"\u25BC":"\u25B6"})}),si(t),a(t)]}),n&&u(Nt.Provider,{value:{...e,depth:e.depth+1,registerChildItem:h},children:i})]})}c(ve,"TreeItem");function si({active:r,label:e}){return u(y,{width:32,children:u(w,{inverse:r,children:e})})}c(si,"defaultRenderLabel");function ai(r){return u(w,{})}c(ai,"defaultRenderValue");const me=c(({label:r,onPress:e,hotkey:t})=>u(ve,{label:r,onInput:c(({input:n,key:o,active:i})=>i&&o.return||t&&t in o&&o[t]||n===t?(e(),!0):!1,"onInput"),renderValue:c(()=>u(y,{children:u(w,{children:`${t?` (${t})`:""}`})}),"renderValue")}),"ActionTreeItem"),Un=c(({label:r,value:e,onChange:t,disabled:n=!1,mask:o,...i})=>{const[s,a]=N(e),[l,d]=N(!1);return Fe(()=>{l||a(e)},[l,e]),u(ve,{label:r,value:e,onInput:c(({key:h,active:f})=>n?!1:l?(h.escape&&d(!1),h.return&&(t(s),d(!1)),!0):f&&h.return?(d(!0),a(e),!0):!1,"onInput"),renderValue:c(()=>{const h=s,f=o?o.repeat(h.length):h;return u(y,{children:n||!l?u(w,{dimColor:!0,children:f}):b(Le,{children:[u(y,{width:f.length+1,children:u(sn,{...i,focus:l,value:h,onChange:a})}),u(w,{dimColor:!0,children:" \u241B cancel"})]})})},"renderValue")})},"TreeTextField"),ci=c(r=>u(Un,{...r,mask:"*"}),"SecretField"),J=c(({children:r,showHelp:e=!1})=>{const t=lt({parent:void 0,label:"<<ROOT>>",index:0}),[n,o]=N(t.current),i={depth:0,isActiveByRef:c(a=>n===a,"isActiveByRef"),registerChildItem:c(()=>t.current,"registerChildItem"),activeItemRef:n};Fe(()=>{o(a=>a.parent?a.parent.lastRenderedChild&&a.index>a.parent.lastRenderedChild.index?a.parent.lastRenderedChild:a:t.current.firstChild??t.current)});function*s(a){a&&(yield a,yield*s(a.firstChild),a.nextSibling&&a!==a.parent?.lastRenderedChild&&(yield*s(a.nextSibling)))}return c(s,"walkTree"),he((a,l)=>{if(n.onInput?.({input:a,key:l,active:!0}))return;let d,h;for(const f of s(t.current))if(f.label!=null){if(d===n&&(h=f),f&&f.onInput?.({input:a,key:l,active:f===n}))return;d=f}l.upArrow?o(f=>f.prevSibling?.lastRenderedChild??f.prevSibling??f.parent??t.current):l.downArrow?o(f=>h??f):l.pageUp?o(()=>t.current.firstChild??t.current):l.pageDown?o(()=>t.current.lastRenderedChild??t.current):l.tab?console.debug({label:n.label,nextSibling:n.nextSibling?.label,prevSibling:n.prevSibling?.label,parent:n.parent?.label,firstChild:n.firstChild?.label,lastRenderedChild:n.lastRenderedChild?.label}):l.leftArrow?o(f=>f.parent??t.current):l.rightArrow&&o(f=>f.lastRenderedChild?f.firstChild:f)}),u(Nt.Provider,{value:i,children:b(y,{flexDirection:"column",children:[u(ve,{isInitiallyExpanded:!0,label:"rootItem",children:r}),e&&u(y,{marginTop:1,flexDirection:"column",children:u(w,{dimColor:!0,children:"\u2191/\u2193 move \u2022 \u2190 collapse \u2022 \u2192 expand \u2022 Enter/Space select/edit \u2022 Esc exit"})})]})})},"TreeView");J.Item=ve,J.TextField=Un;function ge({label:r,elementType:e,isActionExcluded:t,toggleAction:n,generateCode:o}){const[i,s]=N([]),{fetchElements:a,getSyncedElementsByType:l}=G();return we(()=>{c(async()=>{try{await a();const h=l(e);s(h.map(f=>f.data))}catch(h){console.error(String(h))}},"loadElements")()},[e,a,l,s]),u(ve,{label:r,children:i.map(d=>u(ve,{label:d.name,children:e===I.Action&&b(Le,{children:[t&&n&&u(me,{label:`Toggle ${t(d)?"":"(excluded)"}`,onPress:c(()=>{n(d)},"onPress")}),o&&u(me,{label:"Generate code",onPress:c(()=>{o?.(d)},"onPress")})]})},d.id))})}c(ge,"WorkspaceElementsTreeItem");function li({onComplete:r}){const{config:e,updateConfig:t,fetchElements:n,getSyncedElementsByType:o}=G(),[i,s]=N(""),a=e,l=c(p=>{s(p),setTimeout(()=>s(""),2e3)},"setFlash"),d=c(async(p,C)=>{try{await t({[p]:C}),l("\u2705 Configuration updated!")}catch{l("\u274C Error updating configuration")}},"updateField"),h=c(async()=>{try{if(e){const p=pe.saveToFile(e);l(p?"\u2705 Configuration saved successfully!":"\u274C Failed to save configuration"),await n();const C=o(I.Action);await Et({out:a?.outputDir??"",target:a?.projectType??"typescript",schemasOnly:!1,membraneInterfaces:{actions:C.filter(E=>!a?.excludedActionKeys?.includes(E.key)).map(E=>E.data)}}),l("\u2705 Code generated successfully!")}}catch{l("\u274C Error saving configuration")}},"handleSaveConfig"),f=c(async()=>{try{const p=pe.loadConfig();p?(await t(p),l("\u2705 Configuration reloaded successfully!")):l("\u274C No configuration found to reload")}catch{l("\u274C Error reloading configuration")}},"handleReloadConfig");return b(y,{flexDirection:"column",gap:1,children:[u(w,{bold:!0,color:"cyan",children:"\u2699\uFE0F Membrane Configuration Manager"}),u(y,{paddingX:2,children:b(J,{showHelp:!0,children:[b(J.Item,{label:"Configuration",isInitiallyExpanded:!0,children:[b(J.Item,{label:"Project",children:[u(J.TextField,{label:"Workspace Key",value:a?.workspaceKey??"",onChange:c(p=>d("workspaceKey",p),"onChange"),disabled:!0}),u(ci,{label:"Workspace Secret",value:a?.workspaceSecret??"",onChange:c(p=>d("workspaceSecret",p),"onChange"),disabled:!0}),u(J.TextField,{label:"API URI",value:a?.apiUri??"",onChange:c(p=>d("apiUri",p),"onChange")}),u(J.TextField,{label:"Test Customer ID",value:a?.testCustomerId??"",onChange:c(p=>d("testCustomerId",p),"onChange")})]}),b(J.Item,{label:"Code Generation",isInitiallyExpanded:!0,children:[b(J.Item,{label:"Project Type",isInitiallyExpanded:!0,value:a?.projectType,renderValue:c(({value:p})=>u(w,{children:p==="typescript"?"TypeScript":p==="openapi"?"OpenAPI":"(Not set)"}),"renderValue"),children:[u(me,{label:"Update to TypeScript",onPress:c(()=>d("projectType","typescript"),"onPress")}),u(me,{label:"Update to OpenAPI",onPress:c(()=>d("projectType","openapi"),"onPress")})]}),u(J.TextField,{label:"Project dir",value:a?.outputDir??"",onChange:c(p=>d("outputDir",p),"onChange")})]}),b(J.Item,{label:"Workspace Elements",children:[u(ge,{label:"Actions",elementType:I.Action,isActionExcluded:c(p=>a?.excludedActionKeys?.includes(p.key)??!1,"isActionExcluded"),toggleAction:c(p=>d("excludedActionKeys",[...a?.excludedActionKeys??[],p.key]),"toggleAction"),generateCode:c(p=>{(async()=>{try{await Et({out:a?.outputDir??"",target:a?.projectType??"typescript",schemasOnly:!1,membraneInterfaces:{actions:[p]}}),l("\u2705 Code generated successfully!")}catch{l("\u274C Error generating code")}})()},"generateCode")}),u(ge,{label:"Flows",elementType:I.Flow}),u(ge,{label:"Data Sources",elementType:I.DataSource}),u(ge,{label:"Field Mappings",elementType:I.FieldMapping}),u(ge,{label:"Packages",elementType:I.Package}),u(ge,{label:"App Data Schemas",elementType:I.AppDataSchema}),u(ge,{label:"App Event Types",elementType:I.AppEventType})]})]}),u(me,{label:"Save Configuration",onPress:h,hotkey:"s"}),u(me,{label:"Reload Configuration",onPress:f,hotkey:"r"}),u(me,{label:"Exit",onPress:c(()=>r?.(),"onPress"),hotkey:"escape"})]})}),i&&u(y,{paddingX:2,children:u(w,{color:i.includes("\u2705")?"green":"red",children:i})})]})}c(li,"ConfigManager");const Kn=at(process.cwd());function ui({cwd:r,children:e}){return u(Kn.Provider,{value:r,children:e})}c(ui,"CwdProvider");function di(){return ct(Kn)}c(di,"useCwd");function Mt({cwd:r,children:e,membraneCLIService:t}){const n=r||process.cwd();return u(ui,{cwd:n,children:u(_r,{value:{fetcher:ho()},children:u(ri,{membraneCLIService:t,children:e})})})}c(Mt,"Layout");function hi(r,e){r.command("config").alias("install").description("\u26A0\uFE0F EXPERIMENTAL: Manage local membrane configuration with interactive UI").addHelpText("after",["","Examples:"," membrane config # Open interactive config manager",""].join(`
16
+ `)).action(()=>{ut(de.createElement(Mt,{membraneCLIService:e,children:de.createElement(li,{onComplete:c(()=>process.exit(0),"onComplete")})}))})}c(hi,"setupConfigCommand");function _n(r,e){return new Promise((t,n)=>{if(e.aborted){n(e.reason);return}const o=setTimeout(t,r);e.addEventListener("abort",()=>{clearTimeout(o),n(e.reason)},{once:!0})})}c(_n,"sleep");function Ft(r){r instanceof Ke?T.error(r.format()):r instanceof Error?T.error(r.message):T.error("An unknown error occurred"),process.exit(1)}c(Ft,"handleCommandError");function qn(r){return r.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#39;")}c(qn,"escapeHtml");const pi="font-family:system-ui;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#0a0a0a;color:#fff";function se(r,e,t="#888"){const n=/^#[0-9a-fA-F]{3,6}$/.test(t)?t:"#888";return`<!DOCTYPE html><html><body style="${pi}">
17
+ <div style="text-align:center"><h1>${qn(r)}</h1><p style="color:${n}">${qn(e)}</p></div>
18
+ </body></html>`}c(se,"htmlPage");const Bn=".membrane",Wn="config.json";function fi(r){const e=k.resolve(Bn),t=k.join(e,Wn);let n={};Me(t)&&(n=JSON.parse(nn(t,"utf8")));const o={...n,...r};for(const[i,s]of Object.entries(o))s===void 0&&delete o[i];Me(e)||tn(e,{recursive:!0}),Er(t,JSON.stringify(o,null,2)+`
19
+ `,"utf8")}c(fi,"patchProjectConfig");function Jn(){try{const r=k.join(k.resolve(Bn),Wn);if(!Me(r))return{};const e=JSON.parse(nn(r,"utf8"));return{defaultWorkspaceKey:e.defaultWorkspaceKey,defaultTenantKey:e.defaultTenantKey}}catch{return{}}}c(Jn,"readProjectDefaults");const mi=3e3,gi=1800*1e3;function yi(r){r.command("connect").description("Create a connection to a third-party service via browser-based OAuth").option("--integrationId <id>","Integration ID").option("--integrationKey <key>","Integration key").option("--connectorId <id>","Connector ID").option("--connectorVersion <version>","Connector version").option("--connectionId <id>","Existing connection ID (for reconnect)").option("--name <name>","Connection name").option("--allowMultipleConnections","Allow multiple connections").option("--connectorParameters <json>","JSON string of connector parameters").option("--workspaceKey <key>","Workspace key").option("--tenantKey <key>","Tenant key (customer internalId)").option("--no-browser","Print URL instead of opening browser").addHelpText("after",["","Examples:"," membrane connect --integrationKey hubspot"," membrane connect --integrationKey hubspot --tenantKey my-customer"," membrane connect --connectorId 123 --workspaceKey my-workspace"," membrane connect --connectionId abc --no-browser",""].join(`
20
+ `)).action(async e=>{try{await Ci(e)}catch(t){Ft(t)}})}c(yi,"setupConnectCommand");function wi(){if(!(gt()&&(!mt()||Ce())))throw new Error("Not authenticated. Run `membrane login` first.")}c(wi,"ensureAuthenticated");async function Ci(r){T.header("Creating a connection"),wi();const e=Se()||ne,t=Jn(),n=r.workspaceKey||t.defaultWorkspaceKey;if(!n)throw new Error("No workspace specified. Use --workspaceKey or run `membrane login` to set a default.");const o=r.tenantKey||t.defaultTenantKey;if(!o)throw new Error("No tenant specified. Use --tenantKey or run `membrane login` to set a default.");let i;if(r.connectorParameters){let E;try{E=JSON.parse(r.connectorParameters)}catch{throw new Error("--connectorParameters must be valid JSON")}if(typeof E!="object"||E===null||Array.isArray(E))throw new Error("--connectorParameters must be a JSON object");i=E}const s=new qe,a=c(async()=>{const{token:E}=await s.post(`/workspaces/${n}/access-token`,{tenantKey:o});return E},"fetchTenantToken");let l=await a();const d=qr(),{port:h,server:f,waitForCallback:p}=await Si(d),C=`http://127.0.0.1:${h}/callback?nonce=${d}`;try{const E={redirectUri:C,integrationId:r.integrationId,integrationKey:r.integrationKey,connectorId:r.connectorId,connectorVersion:r.connectorVersion,connectionId:r.connectionId,name:r.name,allowMultipleConnections:r.allowMultipleConnections,connectorParameters:i},S=await Lt(`${e}/connection-requests`,l,{method:"POST",body:JSON.stringify(E)}),R=S.url;if(!!(process.stdin.isTTY&&r.browser))try{const{default:tr}=await import("open");await tr(R),T.info("Opening browser to complete connection...")}catch{T.info("Could not open browser automatically.")}console.error(` If the browser doesn't open, visit:
21
+ ${R}
22
+ `);const U=xe("Waiting for connection to complete...").start(),L=new AbortController,M=c(async()=>(l=await a(),l),"refreshTenantToken"),ye=p,Ht=bi(e,l,S.requestId,L.signal,M);Ht.catch(()=>{});const Te=await Promise.race([ye,Ht]);if(L.abort(),U.stop(),Te.cancelled&&(T.warning("Connection was cancelled."),process.exit(0)),Te.error)throw new Error(`Connection failed: ${Te.error}`);if(!Te.connectionId)throw new Error("Connection completed but no connection ID received");const le=await Lt(`${e}/connections/${Te.connectionId}`,l);T.success("Connection created successfully!"),console.error(),console.error(` Connection ID: ${le.id}`),le.name&&console.error(` Name: ${le.name}`),le.integrationId&&console.error(` Integration ID: ${le.integrationId}`),le.connectorId&&console.error(` Connector ID: ${le.connectorId}`),console.error(),process.exit(0)}finally{f.close()}}c(Ci,"runConnect");function Si(r){return new Promise(e=>{let t;const n=new Promise(i=>{t=i}),o=cn((i,s)=>{const a=new URL(i.url??"/","http://127.0.0.1");if(a.pathname!=="/callback"){s.writeHead(404),s.end("Not found");return}if(a.searchParams.get("nonce")!==r){s.writeHead(403),s.end("Invalid nonce");return}const l=a.searchParams.get("connectionId"),d=a.searchParams.get("cancelled"),h=a.searchParams.get("error");s.writeHead(200,{"Content-Type":"text/html"}),h?(s.end(se("Connection failed",h,"#f87171")),t({error:h})):d?(s.end(se("Connection cancelled","You can close this tab and return to your terminal.")),t({cancelled:!0})):l?(s.end(se("Connection successful","You can close this tab and return to your terminal.")),t({connectionId:l})):(s.end(se("Connection failed","Unknown callback state","#f87171")),t({error:"Unknown callback state"}))});o.listen(0,"127.0.0.1",()=>{const i=o.address(),s=typeof i=="object"&&i?i.port:0;e({port:s,server:o,waitForCallback:n})})})}c(Si,"startConnectionCallbackServer");async function bi(r,e,t,n,o){let i=e;const s=Date.now()+gi;for(;Date.now()<s;){n.throwIfAborted(),await _n(mi,n),n.throwIfAborted();let a;try{a=await Lt(`${r}/connection-requests/${t}`,i,{signal:n})}catch(l){if(l instanceof Ke&&l.statusCode===401){i=await o();continue}throw l}if(a.status==="success"&&a.resultConnectionId)return{connectionId:a.resultConnectionId};if(a.status==="cancelled")return{cancelled:!0};if(a.status==="error")return{error:a.resultError?.message??"Connection failed"}}throw new Error("Connection timed out after 30 minutes")}c(bi,"pollConnectionRequest");async function Lt(r,e,t){const n={Authorization:`Bearer ${e}`,"Content-Type":"application/json",...t?.headers},o=await fetch(r,{...t,headers:n});if(!o.ok){const i=await o.text();throw new Ke(o.status,i)}return o.json()}c(Lt,"fetchWithToken");function vi({currentPat:r,onSubmit:e}){const[t,n]=N(""),[o,i]=N(!1),[s,a]=N(null),l=pt(`/w/0/${gr}`);return b(y,{flexDirection:"column",borderStyle:"round",borderTop:!0,width:70,paddingX:1,children:[u(y,{marginTop:-1,marginBottom:1,children:u(w,{bold:!0,children:"\u{1F511} Enter your Personal Access Token"})}),u(w,{children:"Please provide your Personal Access Token. You can find it here:"}),u(y,{marginTop:1,marginBottom:1,children:u(w,{color:"yellow",children:l})}),r&&u(w,{dimColor:!0,children:"Press Enter to keep your current token or type a new one."}),u(sn,{mask:"*",placeholder:`${r?"******":"Enter your token here..."}`,value:t,onChange:n,onSubmit:c(async h=>{a(null),i(!0);try{await e(h),n("")}catch{a("Invalid token. Please try again.")}finally{i(!1)}},"handleSubmit")}),o&&u(y,{marginTop:1,children:b(w,{children:[u(Ue,{type:"dots"})," Validating token..."]})}),s&&u(w,{color:"red",children:s})]})}c(vi,"PersonalAccessTokenInput");function zn({onExit:r,showEscOption:e=!0}){const[t,n]=N(""),{data:o,error:i,isLoading:s}=on("/account"),{updateConfig:a}=G(),l=o?.workspaces,d=s;if(he((S,R)=>{R.escape&&r?.()}),d)return b(y,{children:[u(Ue,{}),u(w,{children:" Fetching workspaces..."})]});if(i)return b(y,{flexDirection:"column",children:[b(w,{color:"red",children:["Error: ",i.message]}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"Press ESC to go back"})})]});const h=l?.filter(S=>S.name.toLowerCase().includes(t.toLowerCase()))??[],f=h.map(S=>({label:S.name,value:S.id})),p=f.length,C=l?.length??0;async function E(S){const R=h.find(L=>L.id===S);if(!R)return;const{key:O,secret:U}=R;!O||!U||(await a({workspaceKey:O,workspaceSecret:U}),r?.())}return c(E,"handleSelect"),b(y,{flexDirection:"column",borderStyle:"round",borderTop:!0,width:70,paddingX:1,children:[u(y,{marginTop:-1,children:u(w,{bold:!0,children:"\u{1F4C1} Select your workspace"})}),b(y,{marginTop:1,children:[u(w,{children:"Search: "}),u(Wr,{placeholder:"Enter a search query...",onChange:n})]}),C>5&&b(w,{children:["Showing ",p," of ",C," workspaces."]}),u(y,{marginTop:1,children:u(Jr,{options:f,onChange:c(S=>{S&&E(S)},"onChange")})}),e&&u(y,{marginTop:1,children:u(w,{color:"grey",children:"Press ESC to go back"})})]})}c(zn,"SelectWorkspace");var Oe=(r=>(r[r.Authenticate=0]="Authenticate",r[r.ConnectWorkspace=1]="ConnectWorkspace",r))(Oe||{});const Ei={0:"Authenticate in Membrane",1:"Connect a Membrane Workspace"},jt=[Oe.Authenticate,Oe.ConnectWorkspace];function Hn({onComplete:r}){const{config:e}=G(),[t,n]=N(!1),[o,i]=N(0),s=!!(e?.workspaceKey&&e?.workspaceSecret),a=dn(),l=jt[o],d=o+1,h=jt.length,f=jt.map((E,S)=>{let R="pending";return S<o?R="done":S===o&&(R="current"),{id:E,label:Ei[E],status:R}});async function p(E){const S=a&&E===""?a:E,R=new qe;try{await R.request("/account",{headers:{Authorization:`Bearer ${S}`}}),so(S),i(O=>O+1)}catch(O){console.error(O)}}c(p,"handlePatSubmit");function C(){n(!0),r&&r()}return c(C,"handleWorkspaceSelected"),he((E,S)=>{s&&S.escape&&r&&r()}),t?u(y,{children:u(w,{children:"\u2705 Setup complete. You are ready to go!"})}):b(y,{flexDirection:"column",alignSelf:"flex-start",gap:1,children:[b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:70,children:[u(y,{marginTop:-1,marginBottom:1,children:b(w,{bold:!0,children:["\u{1F6E0}\uFE0F Setup \u2014"," ",b(w,{color:"cyan",children:["Step ",d," of ",h]}),s&&u(w,{color:"grey",children:" [esc: go back]"})]})}),u(y,{flexDirection:"column",paddingLeft:2,children:f.map(E=>u(Ti,{status:E.status,label:E.label},E.id))})]}),l===Oe.Authenticate&&u(vi,{currentPat:a,onSubmit:p}),l===Oe.ConnectWorkspace&&u(zn,{onExit:C,showEscOption:!1})]})}c(Hn,"Setup");function Ti({status:r,label:e}){return b(y,{children:[b(y,{width:2,children:[r==="current"&&u(Ue,{type:"dots"}),r==="done"&&u(w,{children:"\u2705"})]}),u(w,{dimColor:r!=="current",children:e})]})}c(Ti,"StepDisplay");function xi(r,e){r.command("init").description("Run interactive setup for Membrane project").option("--key <key>","Workspace key for non-interactive setup").option("--secret <secret>","Workspace secret for non-interactive setup").addHelpText("after",["","Examples:"," membrane init # Run interactive setup"," membrane init --key KEY --secret SEC # Non-interactive setup with key and secret",""].join(`
23
+ `)).action(t=>{t.key&&t.secret?pe.saveToFile({workspaceKey:t.key,workspaceSecret:t.secret})?(console.error("\u2705 Configuration saved to membrane.config.yml"),process.exit(0)):(console.error("Error writing configuration file"),process.exit(1)):t.key||t.secret?(console.error("Error: Both --key and --secret must be provided for non-interactive mode"),process.exit(1)):ut(de.createElement(Mt,{membraneCLIService:e,children:de.createElement(Hn,{onComplete:c(()=>process.exit(0),"onComplete")})}))})}c(xi,"setupInitCommand");function Ii(r){r.command("login").description("Log in to Membrane via browser-based OAuth").option("--apiUri <uri>","API base URI",ne).option("--no-browser","Print the authorization URL instead of opening a browser").addHelpText("after",["","Examples:"," membrane login # Log in via browser"," membrane login --no-browser # Print URL for manual login"," membrane login --apiUri https://api.example.com # Use custom API",""].join(`
24
+ `)).action(async e=>{try{T.header("Logging in to Membrane");const t=await ki(e.apiUri,e.browser);ao(),hn(t.accessToken,t.refreshToken,t.expiresIn),uo(e.apiUri),t.defaultWorkspaceKey&&fi({defaultWorkspaceKey:t.defaultWorkspaceKey,defaultTenantKey:t.defaultTenantKey}),T.success("Authentication successful!"),process.exit(0)}catch(t){Ft(t)}})}c(Ii,"setupLoginCommand");async function ki(r,e){const t=$i(),n=Pi(t),{port:o,waitForCode:i,state:s,server:a}=await Ai(),l=`http://127.0.0.1:${o}/callback`,d=`${r}/oauth/authorize?response_type=code&client_id=${Ze}&redirect_uri=${encodeURIComponent(l)}&code_challenge=${n}&code_challenge_method=S256&scope=${yr}&state=${s}`,f=(await fetch(d,{redirect:"manual"})).headers.get("location");if(!f)throw a.close(),new Error("No redirect from authorization endpoint");const p=new URL(f).searchParams.get("authorizationId");if(!p)throw a.close(),new Error("No authorizationId in authorization redirect");if(!!(process.stdin.isTTY&&e))try{const{default:L}=await import("open");await L(f),T.info("Opening browser for authentication...")}catch{T.info("Could not open browser automatically.")}console.error(` If the browser doesn't open, visit:
25
+ ${f}
26
+ `);const E=xe("Waiting for authorization...").start(),S=new AbortController,R=i.then(async L=>{E.text="Exchanging authorization code...";const M=await fetch(`${r}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"authorization_code",code:L,redirect_uri:l,client_id:Ze,code_verifier:t})});if(!M.ok)throw new Error(`Token exchange failed: ${M.status} ${await M.text()}`);return Gn(await M.json())}),O=Di(r,p,t,S.signal);O.catch(()=>{});const U=await Promise.race([R,O]);return S.abort(),a.close(),E.stop(),U}c(ki,"performLogin");function $i(){return an(32).toString("base64url")}c($i,"generateCodeVerifier");function Pi(r){return Br("sha256").update(r).digest("base64url")}c(Pi,"generateCodeChallenge");function Gn(r){if(!r.refresh_token)throw new Error("Server did not return a refresh token");return{accessToken:r.access_token,refreshToken:r.refresh_token,expiresIn:r.expires_in,defaultWorkspaceKey:r.default_workspace_key,defaultTenantKey:r.default_tenant_key}}c(Gn,"parseTokenResponse");function Ai(){const r=an(16).toString("base64url");return new Promise(e=>{let t,n;const o=new Promise((s,a)=>{t=s,n=a}),i=cn((s,a)=>{const l=new URL(s.url??"/","http://127.0.0.1");if(l.pathname!=="/callback"){a.writeHead(404),a.end("Not found");return}if(l.searchParams.get("error")){const p=l.searchParams.get("error_description")??"Authorization was denied";a.writeHead(200,{"Content-Type":"text/html"}),a.end(se("Authorization failed",p,"#f87171")),n(new Error(p));return}const h=l.searchParams.get("code");if(l.searchParams.get("state")!==r){a.writeHead(200,{"Content-Type":"text/html"}),a.end(se("Authorization failed","State mismatch \u2014 possible CSRF attack. Please try again.","#f87171")),n(new Error("State mismatch"));return}if(!h){a.writeHead(200,{"Content-Type":"text/html"}),a.end(se("Authorization failed","No authorization code received.","#f87171")),n(new Error("No authorization code received"));return}a.writeHead(200,{"Content-Type":"text/html"}),a.end(se("Authorization successful","You can close this tab and return to your terminal.")),t(h)});i.listen(0,"127.0.0.1",()=>{const s=i.address(),a=typeof s=="object"&&s?s.port:0;e({port:a,waitForCode:o,state:r,server:i})})})}c(Ai,"startCallbackServer");const Ri=900*1e3,Oi=5;async function Di(r,e,t,n){const o=Date.now()+Ri;let i=Oi;for(;Date.now()<o;){n.throwIfAborted(),await _n(i*1e3,n),n.throwIfAborted();const s=await fetch(`${r}/oauth/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({grant_type:"authorization_code",authorization_id:e,client_id:Ze,code_verifier:t})});if(s.ok)return Gn(await s.json());const a=await s.json(),l=a.error??a.message??"";if(l!=="authorization_pending")if(l==="slow_down"){i+=5;continue}else throw l==="expired_token"?new Error("Authorization has expired. Please try again."):new Error(`Token exchange failed: ${s.status} ${l}`)}throw new Error("Authorization has expired. Please try again.")}c(Di,"pollForTokens");function Ni(r){r.command("logout").description("Log out of Membrane and revoke tokens").option("--apiUri <uri>","API base URI").action(async e=>{try{gt()||(T.info("You are not logged in."),process.exit(0));const t=Ce(),n=e.apiUri||Se()||ne;if(t)try{const o=await fetch(`${n}/oauth/revoke`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:t})});o.ok||T.debug(`Server revocation returned ${o.status}`)}catch{T.warning("Could not reach server to revoke token. Local tokens cleared, but the session may still be active on the server. You can revoke it from the console.")}lo(),T.success("Logged out successfully."),process.exit(0)}catch(t){t instanceof Error?T.error(t.message):T.error("An unknown error occurred"),process.exit(1)}})}c(Ni,"setupLogoutCommand");const Mi=86400;async function Fi(r,e,t,n){const o={iss:r,isAdmin:!0};return t&&(o.id=t),nt.sign(o,e,{expiresIn:Mi})}c(Fi,"generateMcpAccessToken");async function Li(r,e){return(await Ie.get(`${r}/docs-json`,{headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}})).data}c(Li,"fetchOpenApiSchema");function ae(r,e=!0,t,n=new Set){if(!r)return e?D.string():D.string().optional();if(Object.keys(r).length===0)return e?D.any():D.any().optional();if(r.$ref){const i=r.$ref.replace("#/components/schemas/","");if(n.has(i))return D.any();if(n.add(i),t?.schemas?.[i]){const s=t.schemas[i];return ae(s,e,t,n)}return D.any()}let o;if(r.type==="object"||r.properties){const i={};if(r.properties)for(const[s,a]of Object.entries(r.properties)){const l=r.required?.includes(s)??!1;i[s]=ae(a,l,t,n)}if(Object.keys(i).length>0){if(o=D.object(i),r.additionalProperties)if(typeof r.additionalProperties=="object"){const s=ae(r.additionalProperties,!1,t,n);o=o.catchall(s)}else r.additionalProperties===!0&&(o=o.passthrough())}else if(r.additionalProperties&&typeof r.additionalProperties=="object"){const s=ae(r.additionalProperties,!1,t,n);o=D.record(D.string(),s)}else o=D.record(D.string(),D.any())}else if(r.type==="array"){const i=r.items?ae(r.items,!0,t,n):D.any();o=D.array(i)}else if(r.anyOf)o=D.any();else{switch(r.type){case"integer":o=D.coerce.number().int();break;case"number":o=D.coerce.number();break;case"boolean":o=D.coerce.boolean();break;case"string":default:o=D.string();break}r.format==="uuid"?o=D.string().uuid():r.format==="email"?o=D.string().email():r.format==="date"?o=D.string().regex(/^\d{4}-\d{2}-\d{2}$/):r.format==="date-time"&&(o=D.string().datetime()),r.enum&&(o=D.enum(r.enum)),(r.type==="integer"||r.type==="number")&&(r.minimum!==void 0&&(o=o.min(r.minimum)),r.maximum!==void 0&&(o=o.max(r.maximum))),r.type==="string"&&(r.minLength!==void 0&&(o=o.min(r.minLength)),r.maxLength!==void 0&&(o=o.max(r.maxLength)))}return e?o:o.optional()}c(ae,"convertOpenApiSchemaToZod");function Vn(r){const e=r.match(/\{([^}]+)\}/g);return e?e.map(t=>t.slice(1,-1)):[]}c(Vn,"extractPathParameters");function ji(r,e,t,n,o,i){const s=r.operationId||`${t}_${e.replace(/[^a-zA-Z0-9]/g,"_")}`,a=r.description||r.summary||`${t.toUpperCase()} ${e}`,l={},d={},h={};if(r.parameters){for(const p of r.parameters)if(p.in==="path")d[p.name]=ae(p.schema,!0,o).describe(p.description||`Path parameter: ${p.name}`);else if(p.in==="query"){const C=p.required===!0;h[p.name]=ae(p.schema,C,o).describe(p.description||`Query parameter: ${p.name}`)}}if(Object.keys(d).length>0&&(l.params=D.object(d).describe("Path parameters")),Object.keys(h).length>0&&(l.query=D.object(h).describe("Query parameters")),r.requestBody&&r.requestBody.content){const p=r.requestBody.required===!0;if(r.requestBody.content["application/json"]){const C=r.requestBody.content["application/json"].schema;C?l.body=ae(C,p,o).describe("Request body (JSON)"):l.body=D.any().describe("Request body (JSON)")}else r.requestBody.content["application/octet-stream"]||r.requestBody.content["text/plain"]?l.body=p?D.string().describe("Request body (binary/text)"):D.string().optional().describe("Request body (binary/text)"):l.body=D.any().describe("Request body")}const f=Object.keys(l).length>0?D.object(l):D.object({});return{name:s,description:a,parameters:f,async execute(p){try{const C=Vn(e);if(C.length>0){if(!p.params)throw new Error(`Missing required path parameters: ${C.join(", ")}`);const L=C.filter(M=>!(M in p.params));if(L.length>0)throw new Error(`Missing required path parameters: ${L.join(", ")}`)}let E=`${n}${e}`;if(p.params)for(const[L,M]of Object.entries(p.params)){const ye=`{${L}}`;E.includes(ye)&&(E=E.replace(ye,String(M)))}const S=Vn(E);if(S.length>0)throw new Error(`Unresolved path parameters: ${S.join(", ")}`);const R=new URLSearchParams;if(p.query)for(const[L,M]of Object.entries(p.query))M!=null&&R.append(L,String(M));R.toString()&&(E+=`?${R.toString()}`);const O={method:t.toUpperCase(),headers:{Authorization:`Bearer ${i}`}};p.body&&t.toLowerCase()!=="get"&&(r.requestBody?.content?.["application/json"]?(O.headers["Content-Type"]="application/json",O.data=p.body):r.requestBody?.content?.["application/octet-stream"]?(O.headers["Content-Type"]="application/octet-stream",O.data=p.body):r.requestBody?.content?.["text/plain"]?(O.headers["Content-Type"]="text/plain",O.data=p.body):(O.headers["Content-Type"]="application/json",O.data=p.body));const U=await Ie.request({url:E,...O});return{content:[{type:"text",text:U.data===""?"":JSON.stringify(U.data,null,2)}]}}catch(C){return Ie.isAxiosError(C)?{content:[{type:"text",text:`Error: ${C.response?.data?`HTTP ${C.response.status}: ${C.response.statusText} - ${JSON.stringify(C.response.data)}`:`HTTP ${C.response?.status||"unknown"}: ${C.message}`}`}],isError:!0}:C instanceof Error?{content:[{type:"text",text:`Error: API call failed: ${C.message}`}],isError:!0}:{content:[{type:"text",text:"Error: API call failed with unknown error"}],isError:!0}}}}}c(ji,"createApiTool");function Ui(r,e,t){const n=[];if(!r.paths)return n;const o=r.components||{};for(const[i,s]of Object.entries(r.paths))if(typeof s=="object"&&s!==null){for(const[a,l]of Object.entries(s))if(["get","post","put","delete","patch"].includes(a)&&l&&typeof l=="object"){const d=ji(l,i,a,e,o,t);n.push(d)}}return n}c(Ui,"convertOpenApiToTools");function Ki(r){r.command("mcp").description("Launch MCP server with OpenAPI endpoints as tools (API URI is read from environment variables or membrane.config.yml)").addHelpText("after",["","Examples:"," membrane mcp # Launch MCP server using API URI from env vars or membrane.config.yml",""].join(`
27
+ `)).action(async()=>{try{process.env.FASTMCP_SUPPRESS_WARNINGS="true";const e=$e(),t=process.env.MEMBRANE_ACCESS_TOKEN||e?.accessToken,n=process.env.MEMBRANE_WORKSPACE_KEY||e?.workspaceKey,o=process.env.MEMBRANE_WORKSPACE_SECRET||e?.workspaceSecret,i=process.env.MEMBRANE_TEST_CUSTOMER_ID||e?.testCustomerId,s=process.env.MEMBRANE_API_URI||e?.apiUri||Se()||ne;!t&&(!n||!o)&&(e||(console.error("No configuration found. Please set MEMBRANE_ACCESS_TOKEN, or MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first to create a configuration file."),process.exit(1)),console.error("Missing credentials. Please provide MEMBRANE_ACCESS_TOKEN or workspace key/secret in the configuration file, command line, or environment variables."),process.exit(1));const a=t||await Fi(n,o,i),l=await Li(s,a),d=Ui(l,s,a),h=new Hr({name:"Membrane API",instructions:`This MCP server lets you interact with Membrane to configure, run, and troubleshoot integrations.
23
28
  Use it for anything related to Membrane or integrations.
24
- `,version:"1.0.0"});for(const C of d){const T=C.execute;C.execute=async S=>(Po(process.pid,process.cwd()),T(S)),p.addTool(C)}St({isRunning:!0,startTime:new Date().toISOString(),toolsCount:d.length,lastActivity:new Date().toISOString(),processId:process.pid,cwd:process.cwd(),agentName:process.env.AGENT_NAME||"Unnamed Agent"});const f=setInterval(()=>{St({processId:process.pid,cwd:process.cwd(),lastActivity:new Date().toISOString()})},5e3),h=c(async()=>{clearInterval(f),be(process.pid,process.cwd());try{await p.stop()}catch{}},"cleanup");await p.start({transportType:"stdio"}),process.on("SIGINT",async()=>{await h(),process.exit(0)}),process.on("SIGTERM",async()=>{await h(),process.exit(0)}),process.on("exit",()=>{be(process.pid,process.cwd())}),process.on("uncaughtException",async C=>{console.error("Uncaught exception:",C.message),await h(),process.exit(1)}),process.on("unhandledRejection",async C=>{console.error("Unhandled rejection:",C),await h(),process.exit(1)})}catch(e){e instanceof Error&&(console.error(e.message),process.exit(1)),console.error("An unknown error occurred"),process.exit(1)}})}c(hi,"setupMcpCommand");function pi(r){r.command("open").description("Open the workspace in the browser").addHelpText("after",["","Examples:"," membrane open # Open workspace in browser",""].join(`
25
- `)).action(async()=>{try{I.header("Opening Workspace in Browser"),I.info("Loading configuration...");const e=Se();if(!e)throw new Error("No configuration found. Please set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first.");if(!e.workspaceKey||!e.workspaceSecret)throw new Error("Missing workspace credentials");I.info("Retrieving workspace ID...");const t=await tn(process.cwd()),n=dt(`/w/${t}`);I.info(`Opening ${n}...`);const{default:o}=await import("open");await o(n),I.success("Browser opened successfully")}catch(e){e instanceof Error&&(I.error(e.message),process.exit(1)),I.error("An unknown error occurred"),process.exit(1)}})}c(pi,"setupOpenCommand");async function kt(r,e,t={}){const{jobId:n,accessKey:o}=await e(),i=`${n}:${o}`;m.debug(`[background-job] Started job ${n}`);const s=Date.now(),{pollIntervalMs:a=1e3,timeoutMs:l=3e5,maxRetries:d=3}=t;let p=0;for(;;){if(Date.now()-s>l)throw new Error(`Background job ${n} timed out after ${l}ms`);await new Promise(C=>setTimeout(C,a));let h;try{h=await r.get(`background-jobs/${i}`),p=0}catch(C){if(C instanceof Vn&&p<d){p++,m.debug(`[background-job] Job ${n} not found, retrying (${p}/${d})`);continue}throw C}if(h.status==="completed"){if(m.debug(`[background-job] Completed job ${n}`),!h.result)throw new Error(`Background job ${n} completed but returned no result`);return h.result}if(h.status==="failed"){const C=h.error?.message||"Unknown error";throw m.error(`[background-job] Failed job ${n}: ${C}`),h.error?.stack&&m.error(`Stacktrace: ${h.error.stack}`),new Error(`Background job ${n} failed: ${C}`)}m.debug(`[background-job] Polling job ${n} (status: ${h.status})`)}}c(kt,"pollBackgroundJob");const Sn=1e3,bn=12e4;async function fi(){const r=await $.withClient(async t=>kt(t,()=>t.get("export"),{pollIntervalMs:Sn,timeoutMs:bn}));if(!r)throw new Error("Failed to export workspace");const e=await we.get(r.downloadUrl,{responseType:"arraybuffer"});return Buffer.from(e.data)}c(fi,"downloadWorkspaceExport");async function vn(r,e={}){const t=new Jt;t.append("file",r,{filename:"membrane.zip",contentType:"application/zip"});const n=await $.withClient(async i=>{const s=`import?dryRun=${e.dryRun??!1}&partial=${e.partial??!1}`,{jobId:a,accessKey:l}=await i.post(s,t,{headers:t.getHeaders()});return kt(i,()=>Promise.resolve({jobId:a,accessKey:l}),{pollIntervalMs:1e3,timeoutMs:3e5})});if(!n)throw new Error("Failed to import workspace");return await $.isVersionAtLeast("1.10")?{create:new Set(n.comparison.create||[]),update:new Set(n.comparison.update||[]),delete:new Set(n.comparison.delete||[])}:{create:new Set(n.create||[]),update:new Set(n.update||[]),delete:new Set(n.delete||[])}}c(vn,"importWorkspace");async function mi(r,e){const t=await $.withClient(async o=>kt(o,()=>o.get(`connectors/${r}/export-files`,{version:e}),{pollIntervalMs:Sn,timeoutMs:bn}));if(!t)throw new Error("Failed to export connector version");const n=await we.get(t.downloadUrl,{responseType:"arraybuffer"});return Buffer.from(n.data)}c(mi,"exportConnector");function En(r,e){Ee(E.dirname(r)),v.writeFileSync(r,e)}c(En,"writeFile");function gi(r){v.existsSync(r)&&v.rmSync(r)}c(gi,"deleteFile");function $t(r){v.existsSync(r)&&v.rmSync(r,{recursive:!0,force:!0})}c($t,"deleteDir");function yi(r,e=!0){if(v.existsSync(r))try{const t=v.readFileSync(r,"utf8");return H.load(t)||void 0}catch(t){if(!e)return;if(t instanceof Error){const n=E.relative(process.cwd(),r);throw new Error(`Failed to parse YAML file "${n}": ${t.message}`)}throw t}}c(yi,"readYaml");function Tn(r,e,t){try{Ee(E.dirname(r));const n=H.dump(e,t);v.writeFileSync(r,n,"utf8")}catch(n){if(n instanceof Error){const o=E.relative(process.cwd(),r);throw new Error(`Failed to write YAML file "${o}": ${n.message}`)}throw n}return e}c(Tn,"writeYaml");function Ee(r){v.existsSync(r)||v.mkdirSync(r,{recursive:!0})}c(Ee,"ensureDirExists");function xn(r,e=!0){if(!v.existsSync(r)||!v.statSync(r).isDirectory())return;let t=v.readdirSync(r);t.length>0&&(t.forEach(n=>xn(E.join(r,n),!1)),t=v.readdirSync(r)),t.length===0&&!e&&v.rmdirSync(r)}c(xn,"cleanupEmptyFolders");function wi(r){const e=r.split(/[\\/]/).join("/");return e.endsWith("/")?e+"**":e}c(wi,"normalizePattern");async function In(r,e,t){const n=(e??[]).map(wi),o=[];if(t){const i=E.join(q(process.cwd()),"connectors");for(const s of Object.values(t)){const a=s.key,l=E.join(i,a),d=E.join(l,`${a}.yml`);v.existsSync(d)&&o.push({name:Yn(a),content:v.readFileSync(d,"utf-8")});const p=[...s.versions];v.existsSync(E.join(l,"development"))&&!p.includes("")&&p.push("");for(const f of p){const h=f===""?"development":f,C=E.join(l,h,"src"),T=E.join(l,h,"src.zip"),S=f===""?Be:f;v.existsSync(C)?o.push({name:Ut(a,S),content:await Ci(C)}):v.existsSync(T)&&o.push({name:Ut(a,S),content:v.readFileSync(T)})}}}return new Promise((i,s)=>{const a=rt("zip",{zlib:{level:9}}),l=[];a.on("data",p=>l.push(p)),a.on("end",()=>i(Buffer.concat(l))),a.on("error",p=>s(p));const d=new Map;for(const p of r[x.Integration]||[])d.set(p.uuid,p.key);for(const[p,f]of Object.entries(r))for(const h of f){const C=d.get(h.integrationUuid),T=Zn(p,h.key,C);n.length>0&&!n.some(S=>Pr(T,S))||a.append(H.dump(h),{name:T})}for(const{name:p,content:f}of o)a.append(f,{name:p});a.finalize()})}c(In,"createMembraneZip");async function Ci(r){return new Promise((e,t)=>{const n=rt("zip",{zlib:{level:9}}),o=[];n.on("data",i=>o.push(i)),n.on("end",()=>e(Buffer.concat(o))),n.on("error",i=>t(i)),n.directory(r,!1),n.finalize()})}c(Ci,"createConnectorVersionZip");async function Pt(r,e){const t=await Gt.loadAsync(r),n=[];for(const[o,i]of Object.entries(t.files)){if(i.dir)continue;const s=ae(o);if(!s)continue;const a=await i.async("string"),l=H.load(a);if(!l)continue;const d=await e(o,l,s);d!==void 0&&n.push(d)}return n}c(Pt,"iterateZipItems");async function Si(r){const e=await Gt.loadAsync(r),t={};for(const[n,o]of Object.entries(e.files)){if(o.dir)continue;const i=ae(n);if(!i)continue;const s=await o.async("string"),a=H.load(s);t[i.type]||(t[i.type]=[]),t[i.type].push(a)}return t}c(Si,"readMembraneZip");async function kn(r=process.cwd()){const e=await $n(q(r)),t={};for(const{data:n,type:o}of e)t[o]||(t[o]=[]),t[o].push(n);return t}c(kn,"readMembraneDir");async function $n(r){const e=Pn(r),t=[];for(const n of e){if(n.isDirectory())continue;const o=ae(n.path);if(!o)continue;const i=await n.readContent(),s=H.load(i),a=E.join(r,n.path);t.push({filePath:a,data:s,type:o.type})}return t}c($n,"iterateMembraneFiles");function Pn(r,e){const t=[];if(e||(e=r),!v.existsSync(e)||e.startsWith(E.join(r,"connectors")))return t;const n=v.readdirSync(e,{withFileTypes:!0});for(const o of n){const i=E.join(e,o.name),s=E.relative(r,i);o.isDirectory()?t.push(...Pn(r,i)):o.isFile()&&ae(s)&&t.push({path:s,isDirectory:c(()=>!1,"isDirectory"),readContent:c(async()=>v.readFileSync(i,"utf8"),"readContent")})}return t}c(Pn,"getMembraneFiles");function An(){const r={},e=E.join(q(process.cwd()),"connectors");if(!v.existsSync(e))return r;const t=v.readdirSync(e);for(const n of t){const o=E.join(e,n);if(!v.statSync(o).isDirectory())continue;const i=E.join(o,`${n}.yml`),s=yi(i,!1);if(!s?.uuid)continue;const a=new Set,l=v.readdirSync(o);for(const d of l){if(d.endsWith(".yml"))continue;const p=E.join(o,d);v.statSync(p).isDirectory()&&a.add(d=="development"?"":d)}r[s.uuid]={key:n,id:s.id,versions:Array.from(a)}}return r}c(An,"readConnectorsDir");function Te(r,e){const t=E.join(q(process.cwd()),"connectors",r);return e==""||e===Be?E.join(t,"development"):e?E.join(t,e):t}c(Te,"getConnectorPath");const Rn=5;function bi(r){r.command("pull").description("Pull workspace data from specified workspace").option("--force","Overwrite conflicts with remote data",!1).addHelpText("after",["","Examples:"," membrane pull # Pull from default workspace"," membrane pull --force # Overwrite conflicts with remote data",""].join(`
26
- `)).action(async e=>{const t=ot({text:"Pulling workspace",color:"white"}).start();try{const n=await vi(e,t);t.stop(),ki(n.workspaceExport,n.connectors)}catch(n){t.stop(),Pi(n),process.exit(1)}})}c(bi,"setupPullCommand");async function vi(r,e){const t=await kn(),n=await In(t),o=await fi(),i=await Si(o),{comparison:s}=Xn(t,i);s[X.DELETE].size>0&&!r.force&&(await $i(s,n),I.error("Use --force to delete local elements"),process.exit(1));const l=await $n(q(process.cwd()));for(const{filePath:f,data:h}of l)s[X.DELETE].has(h.uuid)&&gi(f);return xn(q(process.cwd())),await Pt(o,(f,h)=>{if(ae(f)&&(s[X.CREATE].has(h.uuid)||s[X.UPDATE].has(h.uuid))){const T=k.join(q(process.cwd()),f);Tn(T,h)}}),{connectors:await $.isVersionAtLeast("1.8")?await Ei(o,e):await xi(i[x.Integration],e),workspaceExport:i}}c(vi,"pullWorkspace");async function Ei(r,e){e.text="Extracting connectors",$t(k.join(q(process.cwd()),"connectors"));const t=new Set,n=await Pe.Open.buffer(r);for(const o of n.files){if(!o.path.startsWith("connectors/")||o.type==="Directory")continue;const i=k.join(q(process.cwd()),o.path);Ee(k.dirname(i));const s=await o.buffer();if(En(i,s),t.add(o.path.split("/")[1]),o.path.endsWith("/development/src.zip")){const a=k.join(k.dirname(i),"src");Ee(a),await(await Pe.Open.buffer(s)).extract({path:a})}}return Array.from(t)}c(Ei,"extractConnectorsFromExport");async function Ti(r,e,t){if(En(k.join(Te(r,e),"src.zip"),t),!e||e===Be){const n=k.join(Te(r,e),"src");Ee(n),await(await Pe.Open.buffer(t)).extract({path:n})}}c(Ti,"writeConnectorVersion");async function xi(r,e){e.text="Pulling connectors";const t=new Map,{id:n}=await $.withClient(l=>l.get("org-workspace-id")),{items:o=[]}=await $.withClient(l=>l.get(`/connectors?workspaceId=${n}`));for(const l of o){const d=l.uuid??l.id;!l.isPublic&&d&&t.set(d,new Set([""]))}for(const l of r??[]){const{connectorUuid:d,connectorVersion:p}=l;d&&(t.has(d)||t.set(d,new Set),t.get(d).add(p??""))}const i=An();for(const[l,{key:d,versions:p}]of Object.entries(i)){if(!t.has(l)){const h=Te(d);$t(h);continue}const f=t.get(l);for(const h of p)if(!f.has(h)){const C=Te(d,h);$t(C)}}const s=Array.from(t.entries()),a=c((l,d)=>{l==="pulling"?e.start(`Pulling connector ${d}...`):e.succeed(`Pulled connector ${d}`)},"onProgress");for(let l=0;l<s.length;l+=Rn){const d=s.slice(l,l+Rn);await Promise.all(d.map(([p,f])=>Ii(p,Array.from(f),a)))}return Array.from(t.keys())}c(xi,"pullConnectorsLegacy");async function Ii(r,e,t){const n=await $.withClient(d=>d.get(`connectors/${r}/export-json`),!1),o=await $.withClient(d=>d.get(`connectors/${r}`),!1);if(!n||!o||!n.key||!o.key)return;const i=o.name||n.key;t?.("pulling",i);const s={...n,id:o.id,uuid:o.uuid},a=Te(n.key),l=k.join(a,`${n.key}.yml`);Tn(l,s);for(const d of e){const p=await mi(r,d);await Ti(n.key,d,p)}t?.("pulled",i)}c(Ii,"pullConnector");function ki(r,e=[]){const t=Object.values(r).reduce((n,o)=>n+(o?.length??0),0)+e.length;I.info(`\u25CF Pulled workspace elements \xB7 ${t}`);for(const n of Object.keys(r).sort()){const o=r[n];I.info(`\u2514\u2500\u2500 ${n}s \xB7 ${o?.length??0}`)}e.length>0&&I.info(`\u2514\u2500\u2500 connectors \xB7 ${e.length}`)}c(ki,"showStats$1");async function $i(r,e){const t=r[X.DELETE].size;I.info(`\u2299 Pull: conflicts detected \xB7 ${t}`),await Pt(e,(n,o)=>{ae(n)&&r[X.DELETE].has(o.uuid)&&I.info(`\u2514\u2500\u2500 ./membrane/${n} (deleted in remote)`)})}c($i,"showConflicts$1");async function Pi(r){I.error("\u25A0 Error"),I.error(`\u2514\u2500\u2500 ${r.message}`),I.error(` ${r.stack}`)}c(Pi,"showError$1");const K=[];for(let r=0;r<256;++r)K.push((r+256).toString(16).slice(1));function Ai(r,e=0){return(K[r[e+0]]+K[r[e+1]]+K[r[e+2]]+K[r[e+3]]+"-"+K[r[e+4]]+K[r[e+5]]+"-"+K[r[e+6]]+K[r[e+7]]+"-"+K[r[e+8]]+K[r[e+9]]+"-"+K[r[e+10]]+K[r[e+11]]+K[r[e+12]]+K[r[e+13]]+K[r[e+14]]+K[r[e+15]]).toLowerCase()}c(Ai,"unsafeStringify");let At;const Ri=new Uint8Array(16);function Di(){if(!At){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");At=crypto.getRandomValues.bind(crypto)}return At(Ri)}c(Di,"rng");const Oi=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);var Dn={randomUUID:Oi};function Ni(r,e,t){if(Dn.randomUUID&&!r)return Dn.randomUUID();r=r||{};const n=r.random??r.rng?.()??Di();if(n.length<16)throw new Error("Random bytes length must be >= 16");return n[6]=n[6]&15|64,n[8]=n[8]&63|128,Ai(n)}c(Ni,"v4");function Mi(r){r.command("push").description("Push workspace data to specified workspace").argument("[files...]","Files, directories, or glob patterns to push").option("--force","Overwrite conflicts with local data",!1).addHelpText("after",["","Examples:"," membrane push # Push all workspace elements"," membrane push data-sources/ # Push all data-sources"," membrane push actions/my-action/spec.yaml # Push specific action"," membrane push flows/*-test/spec.yaml # Push flows matching pattern"," membrane push --force # Overwrite conflicts with local data",""].join(`
27
- `)).action(async(e,t)=>{const n=ot({text:"Pushing workspace",color:"white"}).start();try{await Fi(e,t,n)}catch(o){n.stop(),Ji(o),process.exit(1)}})}c(Mi,"setupPushCommand");async function Fi(r,e,t){const n=r.length>0,o=await kn(),i=An(),s=Ui(o,i),a=await $.isVersionAtLeast("1.9"),l=await In(s,n?r:void 0,a?i:void 0);e.force||(t.text="Comparing workspace",await Li(l,n)&&(t.stop(),process.exit(1)));const d=a?Object.keys(i):await ji(t);t.start("Pushing workspace..."),await vn(l,{partial:n}),t.stop(),Bi(s,d),process.exit(0)}c(Fi,"pushWorkspace");async function Li(r,e){const t=await vn(r,{dryRun:!0,partial:e}),n=t[X.DELETE].size>0;return n&&(await Wi(t,r),I.error("Use --force to delete remote elements")),n}c(Li,"checkForConflicts");async function ji(r){return(await sn({onProgress:c((t,n)=>{t==="pushing"?r.start(`Pushing connector ${n}...`):r.succeed(`Pushed connector ${n}`)},"onProgress")})).pushedConnectors??[]}c(ji,"pushConnectorsLegacy");function Ui(r,e){const t=Ki(r),n=qi(t,e),o={};for(const[i,s]of Object.entries(t)){const a=i;o[a]=(s||[]).map(l=>_i(l,a,n))}return o}c(Ui,"resolveLegacyIdReferences");function _i(r,e,t){const n={...r},o=Z[e]?.parentFieldKey||"parentId";if(n.integrationId&&!n.integrationUuid&&(n.integrationUuid=t.get(n.integrationId.toString())||n.integrationId,delete n.integrationId),n.connectorId){if(!n.connectorUuid){const i=t.get(n.connectorId.toString());i&&(n.connectorUuid=i)}delete n.connectorId}if(n[o]){const i=t.get(n[o].toString());i&&(n.parentUuid=i)}return e===x.Package&&n.elements&&(n.elements=n.elements.map(i=>{if(i.id&&!i.uuid){const s=t.get(i.id.toString());if(s)return{uuid:s,type:i.type}}return i})),delete n.id,n}c(_i,"resolveElementIds");function Ki(r){const e={};for(const[t,n]of Object.entries(r))e[t]=(n||[]).map(o=>o.uuid?o:{...o,uuid:Ni()});return e}c(Ki,"generateMissingUuids");function qi(r,e){const t=new Map;for(const n of Object.values(r))for(const o of n||[])o.id&&o.uuid&&t.set(o.id.toString(),o.uuid);for(const[n,o]of Object.entries(e))o.id&&t.set(o.id.toString(),n);return t}c(qi,"buildIdToUuidLookup");function Bi(r,e=[]){const t=Object.values(r).reduce((n,o)=>n+(o?.length??0),0)+e.length;I.info(`\u25CF Pushed workspace elements \xB7 ${t}`);for(const n of Object.keys(r).sort()){const o=r[n];I.info(`\u2514\u2500\u2500 ${n}s \xB7 ${o?.length??0}`)}e.length>0&&I.info(`\u2514\u2500\u2500 connectors \xB7 ${e.length}`)}c(Bi,"showStats");async function Wi(r,e){const t=r[X.DELETE].size;I.info(`\u2299 Push: conflicts detected \xB7 ${t}`),await Pt(e,(n,o)=>{ae(n)&&r[X.DELETE].has(o.uuid)&&I.info(`\u2514\u2500\u2500 ./membrane/${n} (deleted locally)`)})}c(Wi,"showConflicts");function Ji(r){I.error("\u25A0 Error"),I.error(`\u2514\u2500\u2500 ${r.message}`),I.error(` ${r.stack}`)}c(Ji,"showError");const Rt=[{id:"claude-code",name:"Claude Code",description:"Anthropic Claude Code agent",actionDescription:"Adding membrane MCP to .mcp.json in the current directory",addConfig:c(()=>{const r=E.join(process.cwd(),".mcp.json"),e={mcpServers:{membrane:{command:"membrane",args:["mcp"],env:{AGENT_NAME:"Claude Code"}}}};let t={};if(v.existsSync(r))try{t=JSON.parse(v.readFileSync(r,"utf8"))}catch{t={}}const n={...t,mcpServers:{...t.mcpServers,...e.mcpServers}};return v.writeFileSync(r,JSON.stringify(n,null,2)),`MCP server configuration added to ${r}`},"addConfig")},{id:"cursor",name:"Cursor",description:"Cursor AI editor",actionDescription:"Adding membrane MCP to .cursor/mcp.json in the current directory",addConfig:c(()=>{const r=E.join(process.cwd(),".cursor"),e=E.join(r,"mcp.json");v.existsSync(r)||v.mkdirSync(r);const t={mcpServers:{membrane:{command:"membrane",args:["mcp"],env:{AGENT_NAME:"Cursor"}}}};let n={};if(v.existsSync(e))try{n=JSON.parse(v.readFileSync(e,"utf8"))}catch{n={}}const o={...n,mcpServers:{...n.mcpServers,...t.mcpServers}};return v.writeFileSync(e,JSON.stringify(o,null,2)),`MCP server configuration added to ${e}`},"addConfig")}];function zi({onExit:r,onComplete:e}){const[t,n]=N(0),[o,i]=N(!1),[s,a]=N(null),[l,d]=N(""),[p,f]=N("");le((T,S)=>{if(l||p){(S.escape||T==="q"||S.return)&&e();return}if(o)S.return||T===" "?h(s):S.escape&&(i(!1),a(null));else if(S.escape)r();else if(S.upArrow||T==="k")n(Math.max(0,t-1));else if(S.downArrow||T==="j")n(Math.min(Rt.length-1,t+1));else if(S.return||T===" "){const D=Rt[t];a(D),i(!0)}});const h=c(T=>{try{const S=T.addConfig();d(S)}catch(S){f(`Failed to write configuration: ${S.message||S}`)}},"addMcpConfiguration"),C=Math.min(80,process.stdout.columns||80);return l?b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(g,{marginTop:-1,marginBottom:1,children:b(y,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(y,{color:"green",children:"Success"})]})}),b(g,{flexDirection:"column",paddingLeft:2,children:[u(y,{color:"green",children:l}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"The agent will now be able to use Membrane's integration capabilities."})})]}),u(g,{marginTop:1,paddingLeft:2,children:u(y,{color:"white",children:"[esc/q/enter: exit]"})})]}):p?b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(g,{marginTop:-1,marginBottom:1,children:b(y,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(y,{color:"red",children:"Error"})]})}),u(g,{flexDirection:"column",paddingLeft:2,children:u(y,{color:"red",children:p})}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"[esc/q/enter: exit]"})})]}):o&&s?b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(g,{marginTop:-1,marginBottom:1,children:b(y,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(y,{color:"cyan",children:"Confirmation"})]})}),b(g,{flexDirection:"column",paddingLeft:2,children:[b(y,{children:["Connect ",u(y,{bold:!0,children:s.name})," to Membrane via MCP?"]}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"This will add an MCP server configuration that allows the agent to use Membrane's integration capabilities."})}),u(g,{marginTop:1,children:u(y,{color:"yellow",bold:!0,children:s.actionDescription})}),u(g,{marginTop:2,marginBottom:1,children:b(y,{children:[u(y,{color:"white",bold:!0,children:"[Enter] Confirm"})," ",u(y,{color:"gray",children:"[Esc] Cancel"})]})})]})]}):b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(g,{marginTop:-1,marginBottom:1,children:b(y,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(y,{color:"cyan",children:"Select Agent"})]})}),b(g,{flexDirection:"column",paddingLeft:2,children:[u(y,{color:"grey",children:"Choose an agent to connect to Membrane via MCP:"}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"This will add an MCP server configuration that allows the agent to use Membrane's integration capabilities."})}),u(g,{marginTop:1,flexDirection:"column",children:Rt.map((T,S)=>b(g,{children:[b(y,{color:t===S?"cyan":"white",children:[t===S?"\u25B6 ":" ",T.name]}),b(y,{color:"grey",children:[" \u2014 ",T.description]})]},T.id))})]}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"[\u2191\u2193: select] [enter: choose] [esc: exit]"})})]})}c(zi,"AddMcpServerScreen");function Hi(){const[r,e]=N(null),[t,n]=N([]),[o,i]=N(null);return fe(()=>{const s=c(({status:l})=>{e(l),i(null)},"handleMcpStatusChanged"),a=c(({servers:l})=>{n(l),i(null)},"handleMcpServersChanged");return re.on(j.McpStatusChanged,s),re.on(j.McpServersChanged,a),()=>{re.off(j.McpStatusChanged,s),re.off(j.McpServersChanged,a)}},[]),{mcpStatus:r,allMcpServers:t,error:o,isRunning:r?.isRunning||!1,toolsCount:r?.toolsCount||0,totalRequests:r?.totalRequests||0,lastActivity:r?.lastActivity,processId:r?.processId,serverCount:t.length}}c(Hi,"useMcpStatus");function Gi(){const{error:r,serverCount:e,allMcpServers:t}=Hi(),n=Math.min(100,process.stdout.columns||100);return b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:n,children:[u(g,{marginTop:-1,marginBottom:0,flexDirection:"column",children:b(y,{bold:!0,children:["\u{1F916} Connected AI Agents:"," ",r?u(y,{color:"red",children:"error reading status"}):e===0?u(y,{color:"yellow",children:"none"}):u(y,{color:"green",children:e})]})}),!r&&e===0&&u(g,{marginTop:1,children:b(y,{color:"grey",children:["Connect your AI agents to Membrane.",u(ir,{}),"It will give them tools and context to build integrations."]})}),t.length>0&&u(g,{flexDirection:"column",paddingLeft:2,marginTop:1,children:t.map((o,i)=>u(g,{children:b(y,{color:"grey",children:["#",i+1," ",o.agentName,": ",o.totalRequests," calls"]})},o.processId))}),u(g,{marginTop:1,children:u(y,{color:"grey",children:"[a: connect an agent]"})})]})}c(Gi,"Agent");const On=c(r=>{switch(r){case"error":return"red";case"success":return"green";case"warning":return"yellow";default:return}},"getLogColor");function Nn(r,e){return e<3?r.slice(0,Math.max(0,e)):r.length<=e?r:`${r.slice(0,e-3)}...`}c(Nn,"truncateText");function Vi({children:r}){const{state:e,logs:t}=G();return!e||e===P.NOT_INITIALIZED?b(g,{gap:1,flexDirection:"row",children:[u(xr,{type:"dots"}),u(y,{children:"Initializing..."})]}):e===P.SETTING_UP?u(g,{gap:1,flexDirection:"row",children:u(y,{children:"No workspace selected. Please run `membrane init` to select a workspace."})}):e===P.ERROR?u(g,{flexDirection:"column",children:t.slice().map((n,o)=>u(y,{color:On(n.type),children:n.message},n.timestamp+o))}):r}c(Vi,"EnsureInitialized");function Yi(){const{stats:r}=G(),e=Object.entries(r).filter(([t,n])=>n>0);return e.length===0?null:b(g,{flexDirection:"column",children:[u(g,{children:u(g,{width:12,children:u(y,{color:"grey",children:"Elements:"})})}),u(g,{flexDirection:"column",marginLeft:1,children:e.map(([t,n])=>b(g,{children:[u(g,{width:20,children:b(y,{children:[t,":"]})}),u(y,{color:"green",children:n})]},t))})]})}c(Yi,"ElementStats");const Dt=5,Zi=6;function Xi(){const{logs:r}=G(),[e,t]=N(0),n=Math.min(100,process.stdout.columns||100),o=Dt,i=Math.max(0,r.length-o-e),s=r.length-e,a=r.slice(i,s),l=n-Zi,d=e<r.length-o,p=e>0;return le((f,h)=>{if(r.length!==0)if(h.upArrow){const C=Math.max(0,r.length-o);t(T=>Math.min(C,T+1))}else h.downArrow?t(C=>Math.max(0,C-1)):(f==="G"||f==="g")&&t(0)}),b(g,{flexDirection:"column",paddingTop:1,children:[b(y,{color:"grey",children:["Recent Activity (",i+1,"-",s," of ",r.length,"):",r.length>Dt&&u(y,{color:"grey",children:" [arrows: scroll] [g: end]"})]}),a.map((f,h)=>u(g,{marginLeft:1,children:u(y,{color:On(f.type),children:Nn(f.message,l)})},f.timestamp+h)),r.length>Dt&&b(g,{marginLeft:1,flexDirection:"row",children:[d&&u(y,{color:"grey",children:"\u2191 "}),p&&u(y,{color:"grey",children:"\u2193 "})]})]})}c(Xi,"Logs");const je=[{value:"sync",label:"Continue (overwrite/delete)",key:""},{value:"exit",label:"Cancel",key:""}];function Qi(){const{state:r,resolveConflicts:e,exit:t}=G(),[n,o]=N(0),[i,s]=N(!1),[a,l]=N(!1);return le((d,p)=>{if(!i){if(p.ctrl&&d.toLowerCase()==="r"){l(!a);return}p.upArrow?o(f=>f>0?f-1:je.length-1):p.downArrow?o(f=>f<je.length-1?f+1:0):d.toLowerCase()==="y"?o(0):d.toLowerCase()==="n"?o(1):(p.return||d===" ")&&(s(!0),je[n].value==="sync"?e({watch:!0}):t())}}),fe(()=>{r!==P.CONFLICTS&&i&&s(!1)},[r,i]),b(g,{flexDirection:"column",paddingTop:1,children:[u(g,{children:u(g,{flexDirection:"row",gap:2,children:u(y,{bold:!0,color:"white",children:"Conflicts with remote"})})}),u(g,{children:u(y,{color:"grey",children:"The remote workspace has changes that aren't in your local workspace:"})}),u(g,{marginTop:1,marginLeft:2,children:u(ts,{isExpanded:a})}),b(g,{marginTop:2,flexDirection:"row",gap:1,children:[u(y,{color:"white",bold:!0,children:"What would you like to do?"}),u(y,{color:"grey",children:"[up/down, enter]"})]}),u(g,{children:i?b(g,{flexDirection:"row",gap:1,children:[u(Ae,{type:"dots"}),u(y,{color:"blue",bold:!0,children:"Syncing with remote..."})]}):u(g,{flexDirection:"column",children:je.map((d,p)=>u(g,{flexDirection:"column",children:b(g,{flexDirection:"row",gap:1,children:[u(y,{color:n===p?"cyan":"grey",children:n===p?"\u25B6":" "}),u(y,{color:n===p?"cyan":"grey",bold:n===p,children:d.label})]})},d.value))})})]})}c(Qi,"ResolveChangesUI");const es={[Q.UPDATE]:{incoming:{label:"Elements updated in remote",description:"(to be overwritten from remote)"},outgoing:{label:"Elements updated locally",description:"(to be pushed to remote)"}},[Q.DELETE]:{incoming:{label:"Elements not existing in remote",description:"(to be deleted locally)"},outgoing:{label:"Elements deleted locally",description:"(to be deleted from remote)"}},[Q.CREATE]:{incoming:{label:"Elements created in remote",description:"(to be created locally)"},outgoing:{label:"Elements created locally",description:"(to be created in remote)"}}};function ts({isExpanded:r,showControls:e=!0}){const{conflicts:t}=G(),n=5,o=or(()=>{const i={};return t.forEach(s=>{const a=`${s.type}-${s.direction}`;i[a]||(i[a]=[]),i[a].push(s)}),i},[t]);return u(g,{flexDirection:"column",children:Object.entries(o).map(([i,s])=>{if(s.length===0)return null;const[a,l]=i.split("-"),d=es[a][l];return b(g,{flexDirection:"column",children:[b(g,{flexDirection:"row",gap:1,children:[b(y,{color:"yellow",children:[d.label," (",s.length,")"]}),u(y,{color:"white",children:d.description})]}),(r?s:s.slice(0,n)).map(p=>u(g,{marginLeft:2,children:b(y,{color:"grey",children:["\u2022 ",p.element.id," (",p.element.relativePath,")"]})},p.element.id)),!r&&s.length>n&&u(g,{marginLeft:2,children:b(y,{color:"cyan",children:["... and ",s.length-n," more",e?" (press Ctrl+R to show all)":""]})}),r&&s.length>n&&e&&u(g,{marginLeft:2,children:u(y,{color:"cyan",children:"(press Ctrl+R to collapse)"})})]},i)})})}c(ts,"Conflicts");function ns(){const{config:r,state:e,logs:t,currentWorkspace:n,pull:o}=G(),i=n?.name,s=i?Nn(i,30):r?.workspaceKey,a=Math.min(100,process.stdout.columns||100);return fe(()=>{o({watch:!0})},[]),b(g,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:a,children:[u(g,{marginTop:-1,marginBottom:1,children:b(g,{flexDirection:"row",gap:1,children:[u(y,{bold:!0,children:"\u{1F504} Workspace"}),b(y,{color:os(e),children:[" [",rs(e),"] "]})]})}),b(g,{children:[u(g,{width:12,children:u(y,{color:"grey",children:"Local:"})}),u(y,{color:"grey",children:process.cwd()})]}),b(g,{children:[u(g,{width:12,children:u(y,{color:"grey",children:"Remote:"})}),r?.workspaceKey?b(y,{color:"grey",children:[s," [o: open in console] [w: change]"]}):b(y,{children:[u(y,{color:"yellow",children:"not selected"})," [w: select]"]})]}),e===P.CONFLICTS?u(Qi,{}):b($e,{children:[u(g,{paddingTop:1,children:u(Yi,{})}),t.length>0&&u(Xi,{})]})]})}c(ns,"Workspace");function rs(r){switch(r){case P.PULLING:return"pulling";case P.PUSHING:return"pushing";case P.CONFLICTS:return"conflicts";case P.SYNCED:return"synced";case P.ERROR:return"error";case P.WATCHING:return"tracking changes";case P.RESOLVING:return"resolving";case P.NOT_SYNCED:return"not synced";case P.INITIALIZED:return"initialized";case P.SETTING_UP:return"setup required";default:return"unknown"}}c(rs,"getStatusDisplay");function os(r){switch(r){case P.PULLING:return"yellow";case P.PUSHING:return"yellow";case P.CONFLICTS:return"red";case P.SYNCED:return"green";case P.ERROR:return"red";case P.WATCHING:return"green";case P.RESOLVING:return"yellow";case P.NOT_SYNCED:return"grey";case P.SETTING_UP:return"yellow";default:return"grey"}}c(os,"getStatusColor");function is(){const r=Uo(),e=Ze(!0),{exit:t,state:n}=G(),[o,i]=N(null),s=o??(n===P.SETTING_UP?"setup":"main");le(l=>{s==="main"&&(l==="w"&&i("workspace-selection"),l==="a"&&i("add-mcp-server"),l==="o"&&n===P.INITIALIZED&&a(),l==="s"&&i("setup"))});async function a(){try{const l=await tn(r),d=dt(`/w/${l}`),p=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";Ar(`${p} "${d}"`)}catch(l){console.error("Failed to open workspace:",l),t()}}return c(a,"handleOpenWorkspace"),fe(()=>(e.current=!0,()=>{e.current=!1}),[]),s==="workspace-selection"?u(gn,{onExit:c(()=>i(null),"onExit")}):s==="add-mcp-server"?u(zi,{onExit:c(()=>i(null),"onExit"),onComplete:c(()=>i(null),"onComplete")}):s==="setup"?u(yn,{onComplete:c(()=>i(null),"onComplete")},Date.now()):u(Vi,{children:b(g,{flexDirection:"column",children:[u(g,{flexGrow:1,children:u(Gi,{})}),u(ns,{}),u(g,{paddingLeft:2,children:u(y,{color:"grey",children:"[s: (re-)setup]"})})]})})}c(is,"Main");const ss=c(()=>[w.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),w.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),w.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),w.yellow("\u2502 Real-time agent mode is experimental and subject to changes. \u2502"),w.yellow("\u2502 Use in production environments is not recommended. \u2502"),w.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
28
- `),"createExperimentalWarning$1");function as(r,e){r.command("sync").description("\u26A0\uFE0F EXPERIMENTAL: Sync workspace data in real-time - This feature is experimental and subject to changes. Use in production environments is not recommended.").option("--watch","Watch for changes and sync automatically",!1).option("--rps <number>","Maximum requests per second (default: 80)",t=>parseInt(t,10)).addHelpText("after",["","Examples:"," membrane sync --watch # Start real-time sync with watch mode"," membrane sync --watch --rps 5 # Limit to 5 requests per second",""].join(`
29
- `)).action(async t=>{if(t.watch||(console.error("Error: The sync command requires the --watch flag"),console.error("Usage: membrane sync --watch"),process.exit(1)),t.rps!==void 0){const n=t.rps;!isNaN(n)&&n>0&&n<=1e3?$.init({maxRequestsPerSecond:n}):(m.error(`Invalid RPS value: ${n}. Must be between 1 and 1000.`),process.exit(1))}console.warn(ss()),Xe(ce.createElement(Tt,{cwd:process.cwd(),membraneCLIService:e,children:ce.createElement(is)}))})}c(as,"setupSyncCommand");class cs{static{c(this,"BaseRunner")}constructor(e={}){if(this.options=e,this.fsPaths=ht(),e&&typeof e=="object"){const t=e;"client"in t&&t.client&&(this.client=t.client),"workspace"in t&&(this.workspace=t.workspace)}}client;workspace;fsPaths}const ls="claude-sonnet-4-20250514";class Lt{static{c(this,"TestEnvironment")}client;connectionId;testsDir;testBasePath;options;llm;state={};constructor({connectionId:e,testsDir:t,testBasePath:n,client:o,options:i,llm:s}){this.client=o,this.connectionId=e,this.testsDir=t,this.testBasePath=n,this.llm=s,this.options=i}static async create({connectionId:e,testBasePath:t,options:n}){const o=Se();if(!o)throw new Error("No configuration found. Please set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first.");if(!o.workspaceKey||!o.workspaceSecret)throw new Error("Missing workspace credentials");if(!o.anthropicApiKey)throw new Error("Anthropic API key not configured. Run `membrane init` to set up testing.");const i=new jt({token:await this.createMembraneToken(o),apiUri:o.apiUri}),s=new Rr({apiKey:o.anthropicApiKey}),a={complete:c(async({prompt:l,maxTokens:d})=>{const p=await s.messages.create({model:ls,max_tokens:d,messages:[{role:"user",content:l}]});return p.content[0].type==="text"?p.content[0].text:""},"complete")};return new Lt({client:i,options:n,connectionId:e,testsDir:"src/testing/tests",testBasePath:t,llm:a})}async run(e){this.state={};const t={};for(const n of e)await n.run(),it(t,n.getResult());this.writeResults(t)}async readYaml(e){const t=k.join(this.testsDir,this.testBasePath,this.connectionId,e);return ft(t)}async writeYaml(e,t){const n=k.join(this.testsDir,this.testBasePath,this.connectionId,e);A.mkdirSync(k.dirname(n),{recursive:!0}),A.writeFileSync(n,H.dump(t,{noRefs:!0}))}writeResults(e){const t=k.join(this.testsDir,this.testBasePath,this.connectionId);A.mkdirSync(t,{recursive:!0});const n=k.join(t,"test-results.yaml");A.writeFileSync(n,H.dump(e,{noRefs:!0})),console.debug(`[TestRunner] Results written to: ${n}`)}static async createMembraneToken(e){const t=e.testCustomerId||"test-customer",n={id:t,name:t},o={issuer:e.workspaceKey,expiresIn:7200,algorithm:"HS512"};return tt.sign(n,e.workspaceSecret,o)}}Or.interpolate=/{{([\s\S]+?)}}/g;function Ot(r,e){if(typeof r=="string"){const t=Dr(r),n={state:e,random:{number:c(()=>Ce.number.int(),"number"),alphaNumeric:c(i=>Ce.string.alphanumeric(i),"alphaNumeric")},faker:{company:{name:c(()=>Ce.company.name(),"name"),catchPhrase:c(()=>Ce.company.catchPhrase(),"catchPhrase")},internet:{email:c(()=>Ce.internet.email(),"email")}},entries:e["journal-entries"],orders:e["purchase-orders"]||e["sales-orders"],bills:e["vendor-bills"]},o=t(n);return r.includes("{{")&&r.includes("}}")&&(console.debug(`[TEMPLATE] Input: ${r}`),console.debug(`[TEMPLATE] Output: ${o}`)),o}return Array.isArray(r)?r.map(t=>Ot(t,e)):typeof r=="object"&&r!==null?Object.fromEntries(Object.entries(r).map(([t,n])=>[t,Ot(n,e)])):r}c(Ot,"processNode");function us(r,e){return Ot(r,e)}c(us,"handleTemplate");class ds{static{c(this,"BaseTestSuite")}result;environment;constructor({environment:e}){this.environment=e,this.result={}}async run(){}getResult(){return this.result}async runTest(e){console.debug(`${w.bold.cyan("[start]")} ${w.yellow(e.path)}`);let t=!1,n=!1,o={},i={},s;for(;!t;)try{i=await e.readTestCase(),i||(i=await e.generateConfig(),await this.environment.writeYaml(e.getTestCasePath(),i),console.debug(`${w.bold.yellow("[initialized]")} ${w.yellow(e.path)}`)),s=us(i,this.environment.state),await e.run(s),console.debug(`${w.bold.green("[success]")} ${w.yellow(e.path)}`),t=!0,o=e.getResult(),We(this.result,e.path,o)}catch(l){if(console.error(`${w.bold.red("[error]")} ${w.yellow(e.path)}: ${l}`),this.environment.options.fix&&!n){n=!0;try{await e.fix(l);continue}catch(d){console.error(`${w.bold.red("[fix fail]")} ${w.yellow(e.path)}: ${d}`)}}We(this.result,e.path,{error:z(l)}),t=!0,o={error:z(l)}}const a={...s,result:o};await this.environment.writeYaml(e.getTestCasePath(),a)}async runTestSuite(e){await e.run(),it(this.result,e.getResult())}}class se{static{c(this,"BaseTester")}environment;level;logs;assertions;resultsLocator;path;constructor({environment:e,path:t}){this.environment=e,this.logs=[],this.assertions=[],this.path=t,this.level=0,this.resultsLocator=""}async fix(e){const t=await this.readTestCase();if(!t)throw new Error(`No config found for test ${this.path}`);const n=await this.fixTestCase({config:t,error:e});console.debug(w.bold.yellow("[auto-fix]"),this.path),await this.environment.writeYaml(this.getTestCasePath(),n)}async readTestCase(){const e=this.getTestCasePath();return this.environment.readYaml(e)}async generateConfig(){return{}}async fixTestCase(e){throw new Error(`Auto-fix is not implemented for test ${this.path}`)}getResult(){return{logs:this.logs,assertions:this.assertions}}async assert(e,t,n){try{const o=await e();o?(this.logMsg(`\u2705 ${t}`),this.assertions.push({message:t,result:o})):(this.logMsg(`\u274C ${t}`),this.assertions.push({message:t,result:!1,details:n}))}catch(o){this.assertions.push({message:t,result:!1,details:z(o)}),this.logMsg(`\u274C ${t}: ${o.message}`)}}logMsg(e){console.debug(`${" ".repeat(this.level*2)}${e}`)}getTestCasePath(){return`${this.path}.test.yml`}}function Nt(r,e){const t={};for(const n in e){if(!(n in r)){t[n]=e[n];continue}const o=r[n],i=e[n];if(o&&i&&typeof o=="object"&&typeof i=="object"){if(Array.isArray(o)&&Array.isArray(i)){const s=i.filter(a=>!o.some(l=>{if(typeof l=="object"&&typeof a=="object"&&l!==null&&a!==null&&!Array.isArray(l)&&!Array.isArray(a)){for(const d in a)if(!(d in l)||!Mt(l[d],a[d]))return!1;return!0}else return JSON.stringify(l)===JSON.stringify(a)}));s.length>0&&(t[n]=s)}else if(!Array.isArray(o)&&!Array.isArray(i)){const s=Nt(o,i);s!==null&&(t[n]=s)}else t[n]=i;continue}Mt(o,i)||(t[n]=i)}return Object.keys(t).length===0?null:t}c(Nt,"getNotMatchingSubFields");function Mt(r,e){if(r==e||r?.toString?.()===e?.toString?.())return!0;const t=new Date(r),n=new Date(e);return!isNaN(t.getTime())&&!isNaN(n.getTime())?t.getTime()===n.getTime():!1}c(Mt,"softCompare");class hs extends se{static{c(this,"DataCollectionCreateTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/create`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).create(e.input);await this.assert(()=>!!t.id,"Returned ID of a created record"),t.id&&(this.environment.state[this.dataCollectionKey]={createdRecordId:t.id});const n=Je(this.dataCollection);if(this.dataCollection.findById){const i=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t.id});await this.assert(()=>!!i.record,"Record is returned from findById"),i.record&&(this.environment.state[this.dataCollectionKey]={createdRecordId:t.id,createdRecord:i.record.fields});const s=_t(n),a=Ie(e.input.fields,s,{skipUnknownFields:!0}),l=Ie(i.record.fields,s),d=Nt(l,a);await this.assert(()=>!d,"Returned fields match created fields",{difference:d,sentFields:a,receivedFields:l})}const o=Qn(n??{});if(o.length>0){const i={};o.forEach(a=>{const l=er(e.input.fields,a);typeof l<"u"&&We(i,a,l)});const s=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).create({fields:i});await this.assert(()=>!!s.id,"Record can be created with schema-defined required fields only")}}async generateConfig(){const e=Je(this.dataCollection);if(!e?.properties)throw new Error("No fields schema found for data collection");return{input:{fields:await this.generateFieldsWithLLM(e)}}}async fixTestCase({config:e,error:t}){const n=z(t),o=Je(this.dataCollection),i=await this.getExampleRecordsForSchema(o),s=`I'm trying to create a data record in a data collection with the following fields schema:
29
+ `,version:"1.0.0"});for(const C of d){const E=C.execute;C.execute=async S=>(ti(process.pid,process.cwd()),E(S)),h.addTool(C)}Rt({isRunning:!0,startTime:new Date().toISOString(),toolsCount:d.length,lastActivity:new Date().toISOString(),processId:process.pid,cwd:process.cwd(),agentName:process.env.AGENT_NAME||"Unnamed Agent"});const f=setInterval(()=>{Rt({processId:process.pid,cwd:process.cwd(),lastActivity:new Date().toISOString()})},5e3),p=c(async()=>{clearInterval(f),Re(process.pid,process.cwd());try{await h.stop()}catch{}},"cleanup");await h.start({transportType:"stdio"}),process.on("SIGINT",async()=>{await p(),process.exit(0)}),process.on("SIGTERM",async()=>{await p(),process.exit(0)}),process.on("exit",()=>{Re(process.pid,process.cwd())}),process.on("uncaughtException",async C=>{console.error("Uncaught exception:",C.message),await p(),process.exit(1)}),process.on("unhandledRejection",async C=>{console.error("Unhandled rejection:",C),await p(),process.exit(1)})}catch(e){e instanceof Error&&(console.error(e.message),process.exit(1)),console.error("An unknown error occurred"),process.exit(1)}})}c(Ki,"setupMcpCommand");function _i(r){r.command("open").description("Open the workspace in the browser").addHelpText("after",["","Examples:"," membrane open # Open workspace in browser",""].join(`
30
+ `)).action(async()=>{try{T.header("Opening Workspace in Browser"),T.info("Loading configuration...");const e=$e();if(!e)throw new Error("No configuration found. Please set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first.");if(!e.workspaceKey||!e.workspaceSecret)throw new Error("Missing workspace credentials");T.info("Retrieving workspace ID...");const t=await mn(process.cwd()),n=pt(`/w/${t}`);T.info(`Opening ${n}...`);const{default:o}=await import("open");await o(n),T.success("Browser opened successfully")}catch(e){e instanceof Error&&(T.error(e.message),process.exit(1)),T.error("An unknown error occurred"),process.exit(1)}})}c(_i,"setupOpenCommand");const Yn=5;function qi(r){r.command("pull").description("Pull workspace data from specified workspace").option("--force","Overwrite conflicts with remote data",!1).addHelpText("after",["","Examples:"," membrane pull # Pull from default workspace"," membrane pull --force # Overwrite conflicts with remote data",""].join(`
31
+ `)).action(async e=>{const t=xe({text:"Pulling workspace",color:"white"}).start();try{const n=await Bi(e,t);t.stop(),Gi(n.workspaceExport,n.connectors)}catch(n){t.stop(),Yi(n),process.exit(1)}})}c(qi,"setupPullCommand");async function Bi(r,e){const t=await Ct(),n=await En(t),o=await wn(),i=await Tn(o),{comparison:s}=Yt(t,i);s[X.DELETE].size>0&&!r.force&&(await Vi(s,n),T.error("Use --force to delete local elements"),process.exit(1));const l=await xn(q(process.cwd()));for(const{filePath:f,data:p}of l)s[X.DELETE].has(p.uuid)&&go(f);return vn(q(process.cwd())),await wt(o,(f,p)=>{if(ue(f)&&(s[X.CREATE].has(p.uuid)||s[X.UPDATE].has(p.uuid))){const E=k.join(q(process.cwd()),f);bn(E,p)}}),{connectors:await $.isVersionAtLeast("1.8")?await Wi(o,e):await zi(i[I.Integration],e),workspaceExport:i}}c(Bi,"pullWorkspace");async function Wi(r,e){e.text="Extracting connectors",yt(k.join(q(process.cwd()),"connectors"));const t=new Set,n=await je.Open.buffer(r);for(const o of n.files){if(!o.path.startsWith("connectors/")||o.type==="Directory")continue;const i=k.join(q(process.cwd()),o.path);Pe(k.dirname(i));const s=await o.buffer();if(Sn(i,s),t.add(o.path.split("/")[1]),o.path.endsWith("/development/src.zip")){const a=k.join(k.dirname(i),"src");Pe(a),await(await je.Open.buffer(s)).extract({path:a})}}return Array.from(t)}c(Wi,"extractConnectorsFromExport");async function Ji(r,e,t){if(Sn(k.join(Ae(r,e),"src.zip"),t),!e||e===Ve){const n=k.join(Ae(r,e),"src");Pe(n),await(await je.Open.buffer(t)).extract({path:n})}}c(Ji,"writeConnectorVersion");async function zi(r,e){e.text="Pulling connectors";const t=new Map,{id:n}=await $.withClient(l=>l.get("org-workspace-id")),{items:o=[]}=await $.withClient(l=>l.get(`/connectors?workspaceId=${n}`));for(const l of o){const d=l.uuid??l.id;!l.isPublic&&d&&t.set(d,new Set([""]))}for(const l of r??[]){const{connectorUuid:d,connectorVersion:h}=l;d&&(t.has(d)||t.set(d,new Set),t.get(d).add(h??""))}const i=kn();for(const[l,{key:d,versions:h}]of Object.entries(i)){if(!t.has(l)){const p=Ae(d);yt(p);continue}const f=t.get(l);for(const p of h)if(!f.has(p)){const C=Ae(d,p);yt(C)}}const s=Array.from(t.entries()),a=c((l,d)=>{l==="pulling"?e.start(`Pulling connector ${d}...`):e.succeed(`Pulled connector ${d}`)},"onProgress");for(let l=0;l<s.length;l+=Yn){const d=s.slice(l,l+Yn);await Promise.all(d.map(([h,f])=>Hi(h,Array.from(f),a)))}return Array.from(t.keys())}c(zi,"pullConnectorsLegacy");async function Hi(r,e,t){const n=await $.withClient(d=>d.get(`connectors/${r}/export-json`),!1),o=await $.withClient(d=>d.get(`connectors/${r}`),!1);if(!n||!o||!n.key||!o.key)return;const i=o.name||n.key;t?.("pulling",i);const s={...n,id:o.id,uuid:o.uuid},a=Ae(n.key),l=k.join(a,`${n.key}.yml`);bn(l,s);for(const d of e){const h=await mo(r,d);await Ji(n.key,d,h)}t?.("pulled",i)}c(Hi,"pullConnector");function Gi(r,e=[]){const t=Object.values(r).reduce((n,o)=>n+(o?.length??0),0)+e.length;T.info(`\u25CF Pulled workspace elements \xB7 ${t}`);for(const n of Object.keys(r).sort()){const o=r[n];T.info(`\u2514\u2500\u2500 ${n}s \xB7 ${o?.length??0}`)}e.length>0&&T.info(`\u2514\u2500\u2500 connectors \xB7 ${e.length}`)}c(Gi,"showStats$1");async function Vi(r,e){const t=r[X.DELETE].size;T.info(`\u2299 Pull: conflicts detected \xB7 ${t}`),await wt(e,(n,o)=>{ue(n)&&r[X.DELETE].has(o.uuid)&&T.info(`\u2514\u2500\u2500 ./membrane/${n} (deleted in remote)`)})}c(Vi,"showConflicts$1");async function Yi(r){T.error("\u25A0 Error"),T.error(`\u2514\u2500\u2500 ${r.message}`),T.error(` ${r.stack}`)}c(Yi,"showError$1");const _=[];for(let r=0;r<256;++r)_.push((r+256).toString(16).slice(1));function Zi(r,e=0){return(_[r[e+0]]+_[r[e+1]]+_[r[e+2]]+_[r[e+3]]+"-"+_[r[e+4]]+_[r[e+5]]+"-"+_[r[e+6]]+_[r[e+7]]+"-"+_[r[e+8]]+_[r[e+9]]+"-"+_[r[e+10]]+_[r[e+11]]+_[r[e+12]]+_[r[e+13]]+_[r[e+14]]+_[r[e+15]]).toLowerCase()}c(Zi,"unsafeStringify");let Ut;const Xi=new Uint8Array(16);function Qi(){if(!Ut){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Ut=crypto.getRandomValues.bind(crypto)}return Ut(Xi)}c(Qi,"rng");const es=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto);var Zn={randomUUID:es};function ts(r,e,t){if(Zn.randomUUID&&!r)return Zn.randomUUID();r=r||{};const n=r.random??r.rng?.()??Qi();if(n.length<16)throw new Error("Random bytes length must be >= 16");return n[6]=n[6]&15|64,n[8]=n[8]&63|128,Zi(n)}c(ts,"v4");function ns(r){r.command("push").description("Push workspace data to specified workspace").argument("[files...]","Files, directories, or glob patterns to push").option("--force","Overwrite conflicts with local data",!1).addHelpText("after",["","Examples:"," membrane push # Push all workspace elements"," membrane push data-sources/ # Push all data-sources"," membrane push actions/my-action/spec.yaml # Push specific action"," membrane push flows/*-test/spec.yaml # Push flows matching pattern"," membrane push --force # Overwrite conflicts with local data",""].join(`
32
+ `)).action(async(e,t)=>{const n=xe({text:"Pushing workspace",color:"white"}).start();try{await rs(e,t,n)}catch(o){n.stop(),hs(o),process.exit(1)}})}c(ns,"setupPushCommand");async function rs(r,e,t){const n=r.length>0,o=await Ct(),i=kn(),s=ss(o,i),a=await $.isVersionAtLeast("1.9"),l=await En(s,n?r:void 0,a?i:void 0);e.force||(t.text="Comparing workspace",await os(l,n)&&(t.stop(),process.exit(1)));const d=a?Object.keys(i):await is(t);t.start("Pushing workspace..."),await Cn(l,{partial:n}),t.stop(),us(s,d),process.exit(0)}c(rs,"pushWorkspace");async function os(r,e){const t=await Cn(r,{dryRun:!0,partial:e}),n=t[X.DELETE].size>0;return n&&(await ds(t,r),T.error("Use --force to delete remote elements")),n}c(os,"checkForConflicts");async function is(r){return(await Rn({onProgress:c((t,n)=>{t==="pushing"?r.start(`Pushing connector ${n}...`):r.succeed(`Pushed connector ${n}`)},"onProgress")})).pushedConnectors??[]}c(is,"pushConnectorsLegacy");function ss(r,e){const t=cs(r),n=ls(t,e),o={};for(const[i,s]of Object.entries(t)){const a=i;o[a]=(s||[]).map(l=>as(l,a,n))}return o}c(ss,"resolveLegacyIdReferences");function as(r,e,t){const n={...r},o=Z[e]?.parentFieldKey||"parentId";if(n.integrationId&&!n.integrationUuid&&(n.integrationUuid=t.get(n.integrationId.toString())||n.integrationId,delete n.integrationId),n.connectorId){if(!n.connectorUuid){const i=t.get(n.connectorId.toString());i&&(n.connectorUuid=i)}delete n.connectorId}if(n[o]){const i=t.get(n[o].toString());i&&(n.parentUuid=i)}return e===I.Package&&n.elements&&(n.elements=n.elements.map(i=>{if(i.id&&!i.uuid){const s=t.get(i.id.toString());if(s)return{uuid:s,type:i.type}}return i})),delete n.id,n}c(as,"resolveElementIds");function cs(r){const e={};for(const[t,n]of Object.entries(r))e[t]=(n||[]).map(o=>o.uuid?o:{...o,uuid:ts()});return e}c(cs,"generateMissingUuids");function ls(r,e){const t=new Map;for(const n of Object.values(r))for(const o of n||[])o.id&&o.uuid&&t.set(o.id.toString(),o.uuid);for(const[n,o]of Object.entries(e))o.id&&t.set(o.id.toString(),n);return t}c(ls,"buildIdToUuidLookup");function us(r,e=[]){const t=Object.values(r).reduce((n,o)=>n+(o?.length??0),0)+e.length;T.info(`\u25CF Pushed workspace elements \xB7 ${t}`);for(const n of Object.keys(r).sort()){const o=r[n];T.info(`\u2514\u2500\u2500 ${n}s \xB7 ${o?.length??0}`)}e.length>0&&T.info(`\u2514\u2500\u2500 connectors \xB7 ${e.length}`)}c(us,"showStats");async function ds(r,e){const t=r[X.DELETE].size;T.info(`\u2299 Push: conflicts detected \xB7 ${t}`),await wt(e,(n,o)=>{ue(n)&&r[X.DELETE].has(o.uuid)&&T.info(`\u2514\u2500\u2500 ./membrane/${n} (deleted locally)`)})}c(ds,"showConflicts");function hs(r){T.error("\u25A0 Error"),T.error(`\u2514\u2500\u2500 ${r.message}`),T.error(` ${r.stack}`)}c(hs,"showError");function ps(r){r.command("status").description("Show current login status, project settings, and resolved environment").action(async()=>{try{await fs()}catch(e){Ft(e)}})}c(ps,"setupStatusCommand");async function fs(){const r=Se()||ne,e=Jn(),t=gt();if(console.error(),console.error(m.bold.cyan("\u25B6 Membrane CLI Status")),console.error(),V("API",r),!t)V("Auth",m.red("Not logged in"));else{const n=mt(),o=!!Ce();if(n&&!o)V("Auth",m.yellow("Token expired (no refresh token)"));else try{const s=await new qe().get("/account?workspaceId=0");if(s.user?(V("Auth",m.green("Logged in")),V("User",`${s.user.name} (${s.user.email})`)):V("Auth",m.green("Logged in")),s.orgs?.length){const a=s.orgs.map(l=>l.name).join(", ");V("Orgs",a)}}catch{V("Auth",m.yellow("Logged in (could not fetch account details)"))}}e.defaultWorkspaceKey&&V("Workspace",`${e.defaultWorkspaceKey} ${m.gray("(default)")}`),e.defaultTenantKey&&V("Tenant",`${e.defaultTenantKey} ${m.gray("(default)")}`),!e.defaultWorkspaceKey&&!e.defaultTenantKey&&V("Project",m.gray("No project defaults configured")),console.error()}c(fs,"runStatus");function V(r,e){console.error(` ${m.gray(r.padEnd(12))} ${e}`)}c(V,"printField");const Kt=[{id:"claude-code",name:"Claude Code",description:"Anthropic Claude Code agent",actionDescription:"Adding membrane MCP to .mcp.json in the current directory",addConfig:c(()=>{const r=x.join(process.cwd(),".mcp.json"),e={mcpServers:{membrane:{command:"membrane",args:["mcp"],env:{AGENT_NAME:"Claude Code"}}}};let t={};if(v.existsSync(r))try{t=JSON.parse(v.readFileSync(r,"utf8"))}catch{t={}}const n={...t,mcpServers:{...t.mcpServers,...e.mcpServers}};return v.writeFileSync(r,JSON.stringify(n,null,2)),`MCP server configuration added to ${r}`},"addConfig")},{id:"cursor",name:"Cursor",description:"Cursor AI editor",actionDescription:"Adding membrane MCP to .cursor/mcp.json in the current directory",addConfig:c(()=>{const r=x.join(process.cwd(),".cursor"),e=x.join(r,"mcp.json");v.existsSync(r)||v.mkdirSync(r);const t={mcpServers:{membrane:{command:"membrane",args:["mcp"],env:{AGENT_NAME:"Cursor"}}}};let n={};if(v.existsSync(e))try{n=JSON.parse(v.readFileSync(e,"utf8"))}catch{n={}}const o={...n,mcpServers:{...n.mcpServers,...t.mcpServers}};return v.writeFileSync(e,JSON.stringify(o,null,2)),`MCP server configuration added to ${e}`},"addConfig")}];function ms({onExit:r,onComplete:e}){const[t,n]=N(0),[o,i]=N(!1),[s,a]=N(null),[l,d]=N(""),[h,f]=N("");he((E,S)=>{if(l||h){(S.escape||E==="q"||S.return)&&e();return}if(o)S.return||E===" "?p(s):S.escape&&(i(!1),a(null));else if(S.escape)r();else if(S.upArrow||E==="k")n(Math.max(0,t-1));else if(S.downArrow||E==="j")n(Math.min(Kt.length-1,t+1));else if(S.return||E===" "){const R=Kt[t];a(R),i(!0)}});const p=c(E=>{try{const S=E.addConfig();d(S)}catch(S){f(`Failed to write configuration: ${S.message||S}`)}},"addMcpConfiguration"),C=Math.min(80,process.stdout.columns||80);return l?b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(y,{marginTop:-1,marginBottom:1,children:b(w,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(w,{color:"green",children:"Success"})]})}),b(y,{flexDirection:"column",paddingLeft:2,children:[u(w,{color:"green",children:l}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"The agent will now be able to use Membrane's integration capabilities."})})]}),u(y,{marginTop:1,paddingLeft:2,children:u(w,{color:"white",children:"[esc/q/enter: exit]"})})]}):h?b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(y,{marginTop:-1,marginBottom:1,children:b(w,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(w,{color:"red",children:"Error"})]})}),u(y,{flexDirection:"column",paddingLeft:2,children:u(w,{color:"red",children:h})}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"[esc/q/enter: exit]"})})]}):o&&s?b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(y,{marginTop:-1,marginBottom:1,children:b(w,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(w,{color:"cyan",children:"Confirmation"})]})}),b(y,{flexDirection:"column",paddingLeft:2,children:[b(w,{children:["Connect ",u(w,{bold:!0,children:s.name})," to Membrane via MCP?"]}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"This will add an MCP server configuration that allows the agent to use Membrane's integration capabilities."})}),u(y,{marginTop:1,children:u(w,{color:"yellow",bold:!0,children:s.actionDescription})}),u(y,{marginTop:2,marginBottom:1,children:b(w,{children:[u(w,{color:"white",bold:!0,children:"[Enter] Confirm"})," ",u(w,{color:"gray",children:"[Esc] Cancel"})]})})]})]}):b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:C,children:[u(y,{marginTop:-1,marginBottom:1,children:b(w,{bold:!0,children:["\u{1F517} Connect Agent \u2014 ",u(w,{color:"cyan",children:"Select Agent"})]})}),b(y,{flexDirection:"column",paddingLeft:2,children:[u(w,{color:"grey",children:"Choose an agent to connect to Membrane via MCP:"}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"This will add an MCP server configuration that allows the agent to use Membrane's integration capabilities."})}),u(y,{marginTop:1,flexDirection:"column",children:Kt.map((E,S)=>b(y,{children:[b(w,{color:t===S?"cyan":"white",children:[t===S?"\u25B6 ":" ",E.name]}),b(w,{color:"grey",children:[" \u2014 ",E.description]})]},E.id))})]}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"[\u2191\u2193: select] [enter: choose] [esc: exit]"})})]})}c(ms,"AddMcpServerScreen");function gs(){const[r,e]=N(null),[t,n]=N([]),[o,i]=N(null);return we(()=>{const s=c(({status:l})=>{e(l),i(null)},"handleMcpStatusChanged"),a=c(({servers:l})=>{n(l),i(null)},"handleMcpServersChanged");return oe.on(j.McpStatusChanged,s),oe.on(j.McpServersChanged,a),()=>{oe.off(j.McpStatusChanged,s),oe.off(j.McpServersChanged,a)}},[]),{mcpStatus:r,allMcpServers:t,error:o,isRunning:r?.isRunning||!1,toolsCount:r?.toolsCount||0,totalRequests:r?.totalRequests||0,lastActivity:r?.lastActivity,processId:r?.processId,serverCount:t.length}}c(gs,"useMcpStatus");function ys(){const{error:r,serverCount:e,allMcpServers:t}=gs(),n=Math.min(100,process.stdout.columns||100);return b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:n,children:[u(y,{marginTop:-1,marginBottom:0,flexDirection:"column",children:b(w,{bold:!0,children:["\u{1F916} Connected AI Agents:"," ",r?u(w,{color:"red",children:"error reading status"}):e===0?u(w,{color:"yellow",children:"none"}):u(w,{color:"green",children:e})]})}),!r&&e===0&&u(y,{marginTop:1,children:b(w,{color:"grey",children:["Connect your AI agents to Membrane.",u(Rr,{}),"It will give them tools and context to build integrations."]})}),t.length>0&&u(y,{flexDirection:"column",paddingLeft:2,marginTop:1,children:t.map((o,i)=>u(y,{children:b(w,{color:"grey",children:["#",i+1," ",o.agentName,": ",o.totalRequests," calls"]})},o.processId))}),u(y,{marginTop:1,children:u(w,{color:"grey",children:"[a: connect an agent]"})})]})}c(ys,"Agent");const Xn=c(r=>{switch(r){case"error":return"red";case"success":return"green";case"warning":return"yellow";default:return}},"getLogColor");function Qn(r,e){return e<3?r.slice(0,Math.max(0,e)):r.length<=e?r:`${r.slice(0,e-3)}...`}c(Qn,"truncateText");function ws({children:r}){const{state:e,logs:t}=G();return!e||e===P.NOT_INITIALIZED?b(y,{gap:1,flexDirection:"row",children:[u(zr,{type:"dots"}),u(w,{children:"Initializing..."})]}):e===P.SETTING_UP?u(y,{gap:1,flexDirection:"row",children:u(w,{children:"No workspace selected. Please run `membrane init` to select a workspace."})}):e===P.ERROR?u(y,{flexDirection:"column",children:t.slice().map((n,o)=>u(w,{color:Xn(n.type),children:n.message},n.timestamp+o))}):r}c(ws,"EnsureInitialized");function Cs(){const{stats:r}=G(),e=Object.entries(r).filter(([t,n])=>n>0);return e.length===0?null:b(y,{flexDirection:"column",children:[u(y,{children:u(y,{width:12,children:u(w,{color:"grey",children:"Elements:"})})}),u(y,{flexDirection:"column",marginLeft:1,children:e.map(([t,n])=>b(y,{children:[u(y,{width:20,children:b(w,{children:[t,":"]})}),u(w,{color:"green",children:n})]},t))})]})}c(Cs,"ElementStats");const _t=5,Ss=6;function bs(){const{logs:r}=G(),[e,t]=N(0),n=Math.min(100,process.stdout.columns||100),o=_t,i=Math.max(0,r.length-o-e),s=r.length-e,a=r.slice(i,s),l=n-Ss,d=e<r.length-o,h=e>0;return he((f,p)=>{if(r.length!==0)if(p.upArrow){const C=Math.max(0,r.length-o);t(E=>Math.min(C,E+1))}else p.downArrow?t(C=>Math.max(0,C-1)):(f==="G"||f==="g")&&t(0)}),b(y,{flexDirection:"column",paddingTop:1,children:[b(w,{color:"grey",children:["Recent Activity (",i+1,"-",s," of ",r.length,"):",r.length>_t&&u(w,{color:"grey",children:" [arrows: scroll] [g: end]"})]}),a.map((f,p)=>u(y,{marginLeft:1,children:u(w,{color:Xn(f.type),children:Qn(f.message,l)})},f.timestamp+p)),r.length>_t&&b(y,{marginLeft:1,flexDirection:"row",children:[d&&u(w,{color:"grey",children:"\u2191 "}),h&&u(w,{color:"grey",children:"\u2193 "})]})]})}c(bs,"Logs");const ze=[{value:"sync",label:"Continue (overwrite/delete)",key:""},{value:"exit",label:"Cancel",key:""}];function vs(){const{state:r,resolveConflicts:e,exit:t}=G(),[n,o]=N(0),[i,s]=N(!1),[a,l]=N(!1);return he((d,h)=>{if(!i){if(h.ctrl&&d.toLowerCase()==="r"){l(!a);return}h.upArrow?o(f=>f>0?f-1:ze.length-1):h.downArrow?o(f=>f<ze.length-1?f+1:0):d.toLowerCase()==="y"?o(0):d.toLowerCase()==="n"?o(1):(h.return||d===" ")&&(s(!0),ze[n].value==="sync"?e({watch:!0}):t())}}),we(()=>{r!==P.CONFLICTS&&i&&s(!1)},[r,i]),b(y,{flexDirection:"column",paddingTop:1,children:[u(y,{children:u(y,{flexDirection:"row",gap:2,children:u(w,{bold:!0,color:"white",children:"Conflicts with remote"})})}),u(y,{children:u(w,{color:"grey",children:"The remote workspace has changes that aren't in your local workspace:"})}),u(y,{marginTop:1,marginLeft:2,children:u(Ts,{isExpanded:a})}),b(y,{marginTop:2,flexDirection:"row",gap:1,children:[u(w,{color:"white",bold:!0,children:"What would you like to do?"}),u(w,{color:"grey",children:"[up/down, enter]"})]}),u(y,{children:i?b(y,{flexDirection:"row",gap:1,children:[u(Ue,{type:"dots"}),u(w,{color:"blue",bold:!0,children:"Syncing with remote..."})]}):u(y,{flexDirection:"column",children:ze.map((d,h)=>u(y,{flexDirection:"column",children:b(y,{flexDirection:"row",gap:1,children:[u(w,{color:n===h?"cyan":"grey",children:n===h?"\u25B6":" "}),u(w,{color:n===h?"cyan":"grey",bold:n===h,children:d.label})]})},d.value))})})]})}c(vs,"ResolveChangesUI");const Es={[Q.UPDATE]:{incoming:{label:"Elements updated in remote",description:"(to be overwritten from remote)"},outgoing:{label:"Elements updated locally",description:"(to be pushed to remote)"}},[Q.DELETE]:{incoming:{label:"Elements not existing in remote",description:"(to be deleted locally)"},outgoing:{label:"Elements deleted locally",description:"(to be deleted from remote)"}},[Q.CREATE]:{incoming:{label:"Elements created in remote",description:"(to be created locally)"},outgoing:{label:"Elements created locally",description:"(to be created in remote)"}}};function Ts({isExpanded:r,showControls:e=!0}){const{conflicts:t}=G(),n=5,o=Ar(()=>{const i={};return t.forEach(s=>{const a=`${s.type}-${s.direction}`;i[a]||(i[a]=[]),i[a].push(s)}),i},[t]);return u(y,{flexDirection:"column",children:Object.entries(o).map(([i,s])=>{if(s.length===0)return null;const[a,l]=i.split("-"),d=Es[a][l];return b(y,{flexDirection:"column",children:[b(y,{flexDirection:"row",gap:1,children:[b(w,{color:"yellow",children:[d.label," (",s.length,")"]}),u(w,{color:"white",children:d.description})]}),(r?s:s.slice(0,n)).map(h=>u(y,{marginLeft:2,children:b(w,{color:"grey",children:["\u2022 ",h.element.id," (",h.element.relativePath,")"]})},h.element.id)),!r&&s.length>n&&u(y,{marginLeft:2,children:b(w,{color:"cyan",children:["... and ",s.length-n," more",e?" (press Ctrl+R to show all)":""]})}),r&&s.length>n&&e&&u(y,{marginLeft:2,children:u(w,{color:"cyan",children:"(press Ctrl+R to collapse)"})})]},i)})})}c(Ts,"Conflicts");function xs(){const{config:r,state:e,logs:t,currentWorkspace:n,pull:o}=G(),i=n?.name,s=i?Qn(i,30):r?.workspaceKey,a=Math.min(100,process.stdout.columns||100);return we(()=>{o({watch:!0})},[]),b(y,{flexDirection:"column",paddingX:1,borderStyle:"round",borderTop:!0,width:a,children:[u(y,{marginTop:-1,marginBottom:1,children:b(y,{flexDirection:"row",gap:1,children:[u(w,{bold:!0,children:"\u{1F504} Workspace"}),b(w,{color:ks(e),children:[" [",Is(e),"] "]})]})}),b(y,{children:[u(y,{width:12,children:u(w,{color:"grey",children:"Local:"})}),u(w,{color:"grey",children:process.cwd()})]}),b(y,{children:[u(y,{width:12,children:u(w,{color:"grey",children:"Remote:"})}),r?.workspaceKey?b(w,{color:"grey",children:[s," [o: open in console] [w: change]"]}):b(w,{children:[u(w,{color:"yellow",children:"not selected"})," [w: select]"]})]}),e===P.CONFLICTS?u(vs,{}):b(Le,{children:[u(y,{paddingTop:1,children:u(Cs,{})}),t.length>0&&u(bs,{})]})]})}c(xs,"Workspace");function Is(r){switch(r){case P.PULLING:return"pulling";case P.PUSHING:return"pushing";case P.CONFLICTS:return"conflicts";case P.SYNCED:return"synced";case P.ERROR:return"error";case P.WATCHING:return"tracking changes";case P.RESOLVING:return"resolving";case P.NOT_SYNCED:return"not synced";case P.INITIALIZED:return"initialized";case P.SETTING_UP:return"setup required";default:return"unknown"}}c(Is,"getStatusDisplay");function ks(r){switch(r){case P.PULLING:return"yellow";case P.PUSHING:return"yellow";case P.CONFLICTS:return"red";case P.SYNCED:return"green";case P.ERROR:return"red";case P.WATCHING:return"green";case P.RESOLVING:return"yellow";case P.NOT_SYNCED:return"grey";case P.SETTING_UP:return"yellow";default:return"grey"}}c(ks,"getStatusColor");function $s(){const r=di(),e=lt(!0),{exit:t,state:n}=G(),[o,i]=N(null),s=o??(n===P.SETTING_UP?"setup":"main");he(l=>{s==="main"&&(l==="w"&&i("workspace-selection"),l==="a"&&i("add-mcp-server"),l==="o"&&n===P.INITIALIZED&&a(),l==="s"&&i("setup"))});async function a(){try{const l=await mn(r),d=pt(`/w/${l}`),h=process.platform==="darwin"?"open":process.platform==="win32"?"start":"xdg-open";Gr(`${h} "${d}"`)}catch(l){console.error("Failed to open workspace:",l),t()}}return c(a,"handleOpenWorkspace"),we(()=>(e.current=!0,()=>{e.current=!1}),[]),s==="workspace-selection"?u(zn,{onExit:c(()=>i(null),"onExit")}):s==="add-mcp-server"?u(ms,{onExit:c(()=>i(null),"onExit"),onComplete:c(()=>i(null),"onComplete")}):s==="setup"?u(Hn,{onComplete:c(()=>i(null),"onComplete")},Date.now()):u(ws,{children:b(y,{flexDirection:"column",children:[u(y,{flexGrow:1,children:u(ys,{})}),u(xs,{}),u(y,{paddingLeft:2,children:u(w,{color:"grey",children:"[s: (re-)setup]"})})]})})}c($s,"Main");const Ps=c(()=>[m.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),m.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),m.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),m.yellow("\u2502 Real-time agent mode is experimental and subject to changes. \u2502"),m.yellow("\u2502 Use in production environments is not recommended. \u2502"),m.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
33
+ `),"createExperimentalWarning$1");function As(r,e){r.command("sync").description("\u26A0\uFE0F EXPERIMENTAL: Sync workspace data in real-time - This feature is experimental and subject to changes. Use in production environments is not recommended.").option("--watch","Watch for changes and sync automatically",!1).option("--rps <number>","Maximum requests per second (default: 80)",t=>parseInt(t,10)).addHelpText("after",["","Examples:"," membrane sync --watch # Start real-time sync with watch mode"," membrane sync --watch --rps 5 # Limit to 5 requests per second",""].join(`
34
+ `)).action(async t=>{if(t.watch||(console.error("Error: The sync command requires the --watch flag"),console.error("Usage: membrane sync --watch"),process.exit(1)),t.rps!==void 0){const n=t.rps;!isNaN(n)&&n>0&&n<=1e3?$.init({maxRequestsPerSecond:n}):(g.error(`Invalid RPS value: ${n}. Must be between 1 and 1000.`),process.exit(1))}console.warn(Ps()),ut(de.createElement(Mt,{cwd:process.cwd(),membraneCLIService:e,children:de.createElement($s)}))})}c(As,"setupSyncCommand");class Rs{static{c(this,"BaseRunner")}constructor(e={}){if(this.options=e,this.fsPaths=ft(),e&&typeof e=="object"){const t=e;"client"in t&&t.client&&(this.client=t.client),"workspace"in t&&(this.workspace=t.workspace)}}client;workspace;fsPaths}const Os="claude-sonnet-4-20250514";class zt{static{c(this,"TestEnvironment")}client;connectionId;testsDir;testBasePath;options;llm;state={};constructor({connectionId:e,testsDir:t,testBasePath:n,client:o,options:i,llm:s}){this.client=o,this.connectionId=e,this.testsDir=t,this.testBasePath=n,this.llm=s,this.options=i}static async create({connectionId:e,testBasePath:t,options:n}){const o=$e();if(!o)throw new Error("No configuration found. Please set MEMBRANE_WORKSPACE_KEY and MEMBRANE_WORKSPACE_SECRET environment variables, or run `membrane init` first.");if(!o.workspaceKey||!o.workspaceSecret)throw new Error("Missing workspace credentials");if(!o.anthropicApiKey)throw new Error("Anthropic API key not configured. Run `membrane init` to set up testing.");const i=new Gt({token:await this.createMembraneToken(o),apiUri:o.apiUri}),s=new Vr({apiKey:o.anthropicApiKey}),a={complete:c(async({prompt:l,maxTokens:d})=>{const h=await s.messages.create({model:Os,max_tokens:d,messages:[{role:"user",content:l}]});return h.content[0].type==="text"?h.content[0].text:""},"complete")};return new zt({client:i,options:n,connectionId:e,testsDir:"src/testing/tests",testBasePath:t,llm:a})}async run(e){this.state={};const t={};for(const n of e)await n.run(),dt(t,n.getResult());this.writeResults(t)}async readYaml(e){const t=k.join(this.testsDir,this.testBasePath,this.connectionId,e);return xt(t)}async writeYaml(e,t){const n=k.join(this.testsDir,this.testBasePath,this.connectionId,e);A.mkdirSync(k.dirname(n),{recursive:!0}),A.writeFileSync(n,H.dump(t,{noRefs:!0}))}writeResults(e){const t=k.join(this.testsDir,this.testBasePath,this.connectionId);A.mkdirSync(t,{recursive:!0});const n=k.join(t,"test-results.yaml");A.writeFileSync(n,H.dump(e,{noRefs:!0})),console.debug(`[TestRunner] Results written to: ${n}`)}static async createMembraneToken(e){const t=e.testCustomerId||"test-customer",n={id:t,name:t},o={issuer:e.workspaceKey,expiresIn:7200,algorithm:"HS512"};return nt.sign(n,e.workspaceSecret,o)}}Zr.interpolate=/{{([\s\S]+?)}}/g;function qt(r,e){if(typeof r=="string"){const t=Yr(r),n={state:e,random:{number:c(()=>ke.number.int(),"number"),alphaNumeric:c(i=>ke.string.alphanumeric(i),"alphaNumeric")},faker:{company:{name:c(()=>ke.company.name(),"name"),catchPhrase:c(()=>ke.company.catchPhrase(),"catchPhrase")},internet:{email:c(()=>ke.internet.email(),"email")}},entries:e["journal-entries"],orders:e["purchase-orders"]||e["sales-orders"],bills:e["vendor-bills"]},o=t(n);return r.includes("{{")&&r.includes("}}")&&(console.debug(`[TEMPLATE] Input: ${r}`),console.debug(`[TEMPLATE] Output: ${o}`)),o}return Array.isArray(r)?r.map(t=>qt(t,e)):typeof r=="object"&&r!==null?Object.fromEntries(Object.entries(r).map(([t,n])=>[t,qt(n,e)])):r}c(qt,"processNode");function Ds(r,e){return qt(r,e)}c(Ds,"handleTemplate");class Ns{static{c(this,"BaseTestSuite")}result;environment;constructor({environment:e}){this.environment=e,this.result={}}async run(){}getResult(){return this.result}async runTest(e){console.debug(`${m.bold.cyan("[start]")} ${m.yellow(e.path)}`);let t=!1,n=!1,o={},i={},s;for(;!t;)try{i=await e.readTestCase(),i||(i=await e.generateConfig(),await this.environment.writeYaml(e.getTestCasePath(),i),console.debug(`${m.bold.yellow("[initialized]")} ${m.yellow(e.path)}`)),s=Ds(i,this.environment.state),await e.run(s),console.debug(`${m.bold.green("[success]")} ${m.yellow(e.path)}`),t=!0,o=e.getResult(),Xe(this.result,e.path,o)}catch(l){if(console.error(`${m.bold.red("[error]")} ${m.yellow(e.path)}: ${l}`),this.environment.options.fix&&!n){n=!0;try{await e.fix(l);continue}catch(d){console.error(`${m.bold.red("[fix fail]")} ${m.yellow(e.path)}: ${d}`)}}Xe(this.result,e.path,{error:z(l)}),t=!0,o={error:z(l)}}const a={...s,result:o};await this.environment.writeYaml(e.getTestCasePath(),a)}async runTestSuite(e){await e.run(),dt(this.result,e.getResult())}}class ce{static{c(this,"BaseTester")}environment;level;logs;assertions;resultsLocator;path;constructor({environment:e,path:t}){this.environment=e,this.logs=[],this.assertions=[],this.path=t,this.level=0,this.resultsLocator=""}async fix(e){const t=await this.readTestCase();if(!t)throw new Error(`No config found for test ${this.path}`);const n=await this.fixTestCase({config:t,error:e});console.debug(m.bold.yellow("[auto-fix]"),this.path),await this.environment.writeYaml(this.getTestCasePath(),n)}async readTestCase(){const e=this.getTestCasePath();return this.environment.readYaml(e)}async generateConfig(){return{}}async fixTestCase(e){throw new Error(`Auto-fix is not implemented for test ${this.path}`)}getResult(){return{logs:this.logs,assertions:this.assertions}}async assert(e,t,n){try{const o=await e();o?(this.logMsg(`\u2705 ${t}`),this.assertions.push({message:t,result:o})):(this.logMsg(`\u274C ${t}`),this.assertions.push({message:t,result:!1,details:n}))}catch(o){this.assertions.push({message:t,result:!1,details:z(o)}),this.logMsg(`\u274C ${t}: ${o.message}`)}}logMsg(e){console.debug(`${" ".repeat(this.level*2)}${e}`)}getTestCasePath(){return`${this.path}.test.yml`}}function Bt(r,e){const t={};for(const n in e){if(!(n in r)){t[n]=e[n];continue}const o=r[n],i=e[n];if(o&&i&&typeof o=="object"&&typeof i=="object"){if(Array.isArray(o)&&Array.isArray(i)){const s=i.filter(a=>!o.some(l=>{if(typeof l=="object"&&typeof a=="object"&&l!==null&&a!==null&&!Array.isArray(l)&&!Array.isArray(a)){for(const d in a)if(!(d in l)||!Wt(l[d],a[d]))return!1;return!0}else return JSON.stringify(l)===JSON.stringify(a)}));s.length>0&&(t[n]=s)}else if(!Array.isArray(o)&&!Array.isArray(i)){const s=Bt(o,i);s!==null&&(t[n]=s)}else t[n]=i;continue}Wt(o,i)||(t[n]=i)}return Object.keys(t).length===0?null:t}c(Bt,"getNotMatchingSubFields");function Wt(r,e){if(r==e||r?.toString?.()===e?.toString?.())return!0;const t=new Date(r),n=new Date(e);return!isNaN(t.getTime())&&!isNaN(n.getTime())?t.getTime()===n.getTime():!1}c(Wt,"softCompare");class Ms extends ce{static{c(this,"DataCollectionCreateTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/create`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).create(e.input);await this.assert(()=>!!t.id,"Returned ID of a created record"),t.id&&(this.environment.state[this.dataCollectionKey]={createdRecordId:t.id});const n=Qe(this.dataCollection);if(this.dataCollection.findById){const i=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t.id});await this.assert(()=>!!i.record,"Record is returned from findById"),i.record&&(this.environment.state[this.dataCollectionKey]={createdRecordId:t.id,createdRecord:i.record.fields});const s=Zt(n),a=Ne(e.input.fields,s,{skipUnknownFields:!0}),l=Ne(i.record.fields,s),d=Bt(l,a);await this.assert(()=>!d,"Returned fields match created fields",{difference:d,sentFields:a,receivedFields:l})}const o=wr(n??{});if(o.length>0){const i={};o.forEach(a=>{const l=Cr(e.input.fields,a);typeof l<"u"&&Xe(i,a,l)});const s=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).create({fields:i});await this.assert(()=>!!s.id,"Record can be created with schema-defined required fields only")}}async generateConfig(){const e=Qe(this.dataCollection);if(!e?.properties)throw new Error("No fields schema found for data collection");return{input:{fields:await this.generateFieldsWithLLM(e)}}}async fixTestCase({config:e,error:t}){const n=z(t),o=Qe(this.dataCollection),i=await this.getExampleRecordsForSchema(o),s=`I'm trying to create a data record in a data collection with the following fields schema:
30
35
 
31
36
  ${JSON.stringify(o,null,2)}
32
37
 
@@ -50,7 +55,7 @@ Format your response as a JSON object with two fields:
50
55
  "fields": { ... fixed fields ... }
51
56
  }.
52
57
 
53
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,a=await this.environment.llm.complete({prompt:s,maxTokens:1e4}),l=JSON.parse(a.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,l.explanation),{input:{fields:l.fields}}}async generateFieldsWithLLM(e){const t=await this.getExampleRecordsForSchema(e),n=`Generate a valid JSON object that matches this JSONSchema. Return only the JSON object, no other text.
58
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,a=await this.environment.llm.complete({prompt:s,maxTokens:1e4}),l=JSON.parse(a.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,l.explanation),{input:{fields:l.fields}}}async generateFieldsWithLLM(e){const t=await this.getExampleRecordsForSchema(e),n=`Generate a valid JSON object that matches this JSONSchema. Return only the JSON object, no other text.
54
59
 
55
60
  ${this.getExampleRecordsPrompt(t)}
56
61
 
@@ -66,7 +71,7 @@ ${JSON.stringify(o,null,2)}`).join(`
66
71
 
67
72
  `)}.
68
73
 
69
- If you don't have an example for a given collection, leave the field empty instead of coming up with a fake record id.`}async findReferenceCollections(e){const t=new Set,n=[];return Kt(e,o=>{if(o.referenceCollection){const i=o.referenceCollection.key,s=o.referenceCollection.parameters,a=`${i}:${JSON.stringify(s||{})}`;t.has(a)||(t.add(a),n.push({key:i,parameters:s}))}return o}),n}async fetchExampleRecords(e){return(await this.environment.client.connection(this.environment.connectionId).dataCollection(e.key).list({parameters:e.parameters})).records.map(n=>({id:n.id,fields:n.fields??{}}))}async getExampleRecordsForSchema(e){const t=await this.findReferenceCollections(e),n={};for(const o of t){const i=qt(o);n[i]=await this.fetchExampleRecords(o)}return n}}class ps extends se{static{c(this,"DataCollectionDeleteTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/delete`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} delete`);if(await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).delete({id:t}),this.dataCollection.findById)try{await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t})}catch(n){if(n?.data?.data?.response?.status===404){await this.assert(()=>!0,"Record is not found after deletion");return}throw n}else await this.assert(()=>!0,"No findById capability, skipping verification")}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test delete operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!t?.records?.length)throw new Error(`No records found to test delete for ${this.dataCollectionKey}`);e=t.records[0].id}return{input:{id:e}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to delete a record from a data collection.
74
+ If you don't have an example for a given collection, leave the field empty instead of coming up with a fake record id.`}async findReferenceCollections(e){const t=new Set,n=[];return Xt(e,o=>{if(o.referenceCollection){const i=o.referenceCollection.key,s=o.referenceCollection.parameters,a=`${i}:${JSON.stringify(s||{})}`;t.has(a)||(t.add(a),n.push({key:i,parameters:s}))}return o}),n}async fetchExampleRecords(e){return(await this.environment.client.connection(this.environment.connectionId).dataCollection(e.key).list({parameters:e.parameters})).records.map(n=>({id:n.id,fields:n.fields??{}}))}async getExampleRecordsForSchema(e){const t=await this.findReferenceCollections(e),n={};for(const o of t){const i=Qt(o);n[i]=await this.fetchExampleRecords(o)}return n}}class Fs extends ce{static{c(this,"DataCollectionDeleteTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/delete`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} delete`);if(await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).delete({id:t}),this.dataCollection.findById)try{await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t})}catch(n){if(n?.data?.data?.response?.status===404){await this.assert(()=>!0,"Record is not found after deletion");return}throw n}else await this.assert(()=>!0,"No findById capability, skipping verification")}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test delete operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!t?.records?.length)throw new Error(`No records found to test delete for ${this.dataCollectionKey}`);e=t.records[0].id}return{input:{id:e}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to delete a record from a data collection.
70
75
 
71
76
  I tried to delete a record with this ID:
72
77
 
@@ -86,7 +91,7 @@ Format your response as a JSON object with two fields:
86
91
  "id": "fixed id"
87
92
  }.
88
93
 
89
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{id:s.id}}}}class fs extends se{static{c(this,"DataCollectionFindByIdTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/find-by-id`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} find-by-id`);const n=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t});await this.assert(()=>!!n.record,"Record is returned from findById"),await this.assert(()=>n.record.id===t,"Returned record ID matches requested ID")}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test findById operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!t?.records?.length)throw new Error(`No records found to test findById for ${this.dataCollectionKey}`);e=t.records[0].id}return{input:{id:e}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to find a record by ID in a data collection.
94
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{id:s.id}}}}class Ls extends ce{static{c(this,"DataCollectionFindByIdTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/find-by-id`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} find-by-id`);const n=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:t});await this.assert(()=>!!n.record,"Record is returned from findById"),await this.assert(()=>n.record.id===t,"Returned record ID matches requested ID")}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test findById operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!t?.records?.length)throw new Error(`No records found to test findById for ${this.dataCollectionKey}`);e=t.records[0].id}return{input:{id:e}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to find a record by ID in a data collection.
90
95
 
91
96
  I tried to find a record with this ID:
92
97
 
@@ -106,7 +111,7 @@ Format your response as a JSON object with two fields:
106
111
  "id": "fixed id"
107
112
  }.
108
113
 
109
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{id:s.id}}}}class ms extends se{static{c(this,"DataCollectionListTester")}dataCollectionKey;constructor({environment:e,dataCollectionKey:t}){super({environment:e,path:`data/${t}/list`}),this.dataCollectionKey=t}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list(e.input);if(await this.assert(()=>Array.isArray(t.records),"Response has records field as array"),e.input.sort){const{field:n,direction:o}=e.input.sort,i=t.records;if(i.length>1)for(let s=1;s<i.length;s++){const a=i[s-1].fields?.[n],l=i[s].fields?.[n];a==null||l==null||(o==="asc"?await this.assert(()=>a<=l,`Records are sorted by ${n} in ascending order`):await this.assert(()=>a>=l,`Records are sorted by ${n} in descending order`))}}}async generateConfig(){return{input:{}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to list records from a data collection.
114
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{id:s.id}}}}class js extends ce{static{c(this,"DataCollectionListTester")}dataCollectionKey;constructor({environment:e,dataCollectionKey:t}){super({environment:e,path:`data/${t}/list`}),this.dataCollectionKey=t}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list(e.input);if(await this.assert(()=>Array.isArray(t.records),"Response has records field as array"),e.input.sort){const{field:n,direction:o}=e.input.sort,i=t.records;if(i.length>1)for(let s=1;s<i.length;s++){const a=i[s-1].fields?.[n],l=i[s].fields?.[n];a==null||l==null||(o==="asc"?await this.assert(()=>a<=l,`Records are sorted by ${n} in ascending order`):await this.assert(()=>a>=l,`Records are sorted by ${n} in descending order`))}}}async generateConfig(){return{input:{}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to list records from a data collection.
110
115
 
111
116
  I tried to list records with these parameters:
112
117
 
@@ -126,7 +131,7 @@ Format your response as a JSON object with two fields:
126
131
  "input": { ... fixed parameters ... }
127
132
  }.
128
133
 
129
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:s.input}}}class gs extends se{static{c(this,"DataCollectionMatchTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/match`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).match(e.input);await this.assert(()=>!!t.record,"Response contains a record");const n=t.record,i=(await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:n.id})).record;for(const[s,a]of Object.entries(e.input.query)){const l=i.fields?.[s];await this.assert(()=>Mt(l,a),`Record field ${s} matches input value - expected ${JSON.stringify(a)} got ${JSON.stringify(l)}`)}}async generateConfig(){if(!this.dataCollection.list)throw new Error(`Can't find a record to test match operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const e=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!e?.records?.length)throw new Error("No records found to test match");const t=e.records[0];if(!t?.fields)throw new Error("First record has no fields to match against");const n={},o=t.fields||{},i=Object.entries(o).filter(([s,a])=>s!=="links"&&a!=null&&a!=="").slice(0,1);if(i.length===0)throw new Error("No usable fields found in record for matching");for(const[s,a]of i)n[s]=a;return{input:{query:n}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to match a record in a data collection.
134
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:s.input}}}class Us extends ce{static{c(this,"DataCollectionMatchTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/match`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).match(e.input);await this.assert(()=>!!t.record,"Response contains a record");const n=t.record,i=(await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:n.id})).record;for(const[s,a]of Object.entries(e.input.query)){const l=i.fields?.[s];await this.assert(()=>Wt(l,a),`Record field ${s} matches input value - expected ${JSON.stringify(a)} got ${JSON.stringify(l)}`)}}async generateConfig(){if(!this.dataCollection.list)throw new Error(`Can't find a record to test match operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const e=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!e?.records?.length)throw new Error("No records found to test match");const t=e.records[0];if(!t?.fields)throw new Error("First record has no fields to match against");const n={},o=t.fields||{},i=Object.entries(o).filter(([s,a])=>s!=="links"&&a!=null&&a!=="").slice(0,1);if(i.length===0)throw new Error("No usable fields found in record for matching");for(const[s,a]of i)n[s]=a;return{input:{query:n}}}async fixTestCase({config:e,error:t}){const n=z(t),o=`I'm trying to match a record in a data collection.
130
135
 
131
136
  I tried to match a record with this query:
132
137
 
@@ -146,7 +151,7 @@ Format your response as a JSON object with two fields:
146
151
  "query": { ... fixed query ... }
147
152
  }.
148
153
 
149
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{query:s.query}}}}class ys extends se{static{c(this,"DataCollectionSearchTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/search`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).search(e.input);await this.assert(()=>Array.isArray(t.records),"Response has records field as array");const n=t.records.some(o=>o.id===e.expectedRecordId);n||console.debug(w.yellow(`Search query "${e.input.query}" did not return expected record`)),await this.assert(()=>n,"Search returns expected record")}async generateConfig(){if(!this.dataCollection.list)throw new Error(`Can't find a record to test search operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const e=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!e.records.length)throw new Error("No records found to test search");const t=e.records[0],n=await this.getSearchQueryWithLLM(t.fields||{});if(!n)throw new Error("Unable to generate search query. LLM could not identify a suitable field value from the record to use as the search query.");return{input:{query:n},expectedRecordId:t.id}}async getSearchQueryWithLLM(e){const t=`You are given the fields of a record from a data collection. Identify the BEST single field value that a user is MOST likely to type when searching for this record (e.g. name, title, email, external id). Ignore null, empty strings, technical IDs, HTML, and timestamps unless they are obviously human-searchable. Make sure the field value is as unique as possible for this record. RETURN ONLY the field value as a plain string. No additional text, no JSON, no quotes.
154
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:{query:s.query}}}}class Ks extends ce{static{c(this,"DataCollectionSearchTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/search`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).search(e.input);await this.assert(()=>Array.isArray(t.records),"Response has records field as array");const n=t.records.some(o=>o.id===e.expectedRecordId);n||console.debug(m.yellow(`Search query "${e.input.query}" did not return expected record`)),await this.assert(()=>n,"Search returns expected record")}async generateConfig(){if(!this.dataCollection.list)throw new Error(`Can't find a record to test search operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const e=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!e.records.length)throw new Error("No records found to test search");const t=e.records[0],n=await this.getSearchQueryWithLLM(t.fields||{});if(!n)throw new Error("Unable to generate search query. LLM could not identify a suitable field value from the record to use as the search query.");return{input:{query:n},expectedRecordId:t.id}}async getSearchQueryWithLLM(e){const t=`You are given the fields of a record from a data collection. Identify the BEST single field value that a user is MOST likely to type when searching for this record (e.g. name, title, email, external id). Ignore null, empty strings, technical IDs, HTML, and timestamps unless they are obviously human-searchable. Make sure the field value is as unique as possible for this record. RETURN ONLY the field value as a plain string. No additional text, no JSON, no quotes.
150
155
 
151
156
  Record fields:
152
157
  ${JSON.stringify(e,null,2)}
@@ -171,7 +176,7 @@ Format your response as a JSON object with two fields:
171
176
  "input": { ... fixed parameters ... }
172
177
  }.
173
178
 
174
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:s.input,expectedRecordId:e.expectedRecordId}}}class ws extends se{static{c(this,"DataCollectionSpecTester")}dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/spec`}),this.dataCollection=n}async run(){const e=Object.keys(this.dataCollection.fieldsSchema||{}).length;await this.assert(()=>e>0,"Fields schema has field definitions")}}class Cs extends se{static{c(this,"DataCollectionUpdateTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/update`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} update`);const n=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).update({...e.input,id:t});if(await this.assert(()=>!!n.id,"Returned ID of updated record"),this.environment.state[this.dataCollectionKey]||(this.environment.state[this.dataCollectionKey]={}),this.environment.state[this.dataCollectionKey].createdRecordId=n.id,this.dataCollection.findById){const o=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:n.id});await this.assert(()=>!!o.record,"Record is returned from findById");const i=ze(this.dataCollection),s=_t(i),a=Ie(e.input.fields,s,{skipUnknownFields:!0}),l=Ie(o.record.fields,s),d=Nt(l,a);await this.assert(()=>!d,"Returned fields match updated fields",{difference:d,sentFields:a,receivedFields:l})}}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test update operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const o=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!o?.records?.length)throw new Error(`No records found to test update for ${this.dataCollectionKey}`);e=o.records[0].id}const t=ze(this.dataCollection);if(!t?.properties)throw new Error("No fields schema found for data collection");const n=await this.generateFieldsWithLLM(t);return{input:{id:e,fields:n}}}async fixTestCase({config:e,error:t}){const n=z(t),o=ze(this.dataCollection),i=await this.getExampleRecordsForSchema(o),s=`I'm trying to update a data record in a data collection with the following fields schema:
179
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,i=await this.environment.llm.complete({prompt:o,maxTokens:1e3}),s=JSON.parse(i.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,s.explanation),{input:s.input,expectedRecordId:e.expectedRecordId}}}class _s extends ce{static{c(this,"DataCollectionSpecTester")}dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/spec`}),this.dataCollection=n}async run(){const e=Object.keys(this.dataCollection.fieldsSchema||{}).length;await this.assert(()=>e>0,"Fields schema has field definitions")}}class qs extends ce{static{c(this,"DataCollectionUpdateTester")}dataCollectionKey;dataCollection;constructor({environment:e,dataCollectionKey:t,dataCollection:n}){super({environment:e,path:`data/${t}/update`}),this.dataCollectionKey=t,this.dataCollection=n}async run(e){const t=this.environment.state[this.dataCollectionKey]?.createdRecordId||e?.input?.id;if(!t)throw new Error(`No ID found in state or config for ${this.dataCollectionKey} update`);const n=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).update({...e.input,id:t});if(await this.assert(()=>!!n.id,"Returned ID of updated record"),this.environment.state[this.dataCollectionKey]||(this.environment.state[this.dataCollectionKey]={}),this.environment.state[this.dataCollectionKey].createdRecordId=n.id,this.dataCollection.findById){const o=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).findById({id:n.id});await this.assert(()=>!!o.record,"Record is returned from findById");const i=et(this.dataCollection),s=Zt(i),a=Ne(e.input.fields,s,{skipUnknownFields:!0}),l=Ne(o.record.fields,s),d=Bt(l,a);await this.assert(()=>!d,"Returned fields match updated fields",{difference:d,sentFields:a,receivedFields:l})}}async generateConfig(){let e=this.environment.state[this.dataCollectionKey]?.createdRecordId;if(!e){if(!this.dataCollection.list)throw new Error(`Can't find a record to test update operation for ${this.dataCollectionKey}. List operation is not implemented for this data collection`);const o=await this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).list({});if(!o?.records?.length)throw new Error(`No records found to test update for ${this.dataCollectionKey}`);e=o.records[0].id}const t=et(this.dataCollection);if(!t?.properties)throw new Error("No fields schema found for data collection");const n=await this.generateFieldsWithLLM(t);return{input:{id:e,fields:n}}}async fixTestCase({config:e,error:t}){const n=z(t),o=et(this.dataCollection),i=await this.getExampleRecordsForSchema(o),s=`I'm trying to update a data record in a data collection with the following fields schema:
175
180
 
176
181
  ${JSON.stringify(o,null,2)}
177
182
 
@@ -195,7 +200,7 @@ Format your response as a JSON object with two fields:
195
200
  "fields": { ... fixed fields ... }
196
201
  }.
197
202
 
198
- Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,a=await this.environment.llm.complete({prompt:s,maxTokens:1e4}),l=JSON.parse(a.trim());return console.warn(w.bold.yellow("[auto-fix]"),`${this.path}:`,l.explanation),{input:{id:e.input.id,fields:l.fields}}}async generateFieldsWithLLM(e){const t=await this.getExampleRecordsForSchema(e),n=`Generate a valid JSON object that matches this JSONSchema. Return only the JSON object, no other text.
203
+ Only return the JSON object, no other text or wrapping (like \`\`\`json or \`\`\`).`,a=await this.environment.llm.complete({prompt:s,maxTokens:1e4}),l=JSON.parse(a.trim());return console.warn(m.bold.yellow("[auto-fix]"),`${this.path}:`,l.explanation),{input:{id:e.input.id,fields:l.fields}}}async generateFieldsWithLLM(e){const t=await this.getExampleRecordsForSchema(e),n=`Generate a valid JSON object that matches this JSONSchema. Return only the JSON object, no other text.
199
204
 
200
205
  ${this.getExampleRecordsPrompt(t)}
201
206
 
@@ -211,9 +216,9 @@ ${JSON.stringify(o,null,2)}`).join(`
211
216
 
212
217
  `)}.
213
218
 
214
- If you don't have an example for a given collection, leave the field empty instead of coming up with a fake record id.`}async findReferenceCollections(e){const t=new Set,n=[];return Kt(e,o=>{if(o.referenceCollection){const i=o.referenceCollection.key,s=o.referenceCollection.parameters,a=`${i}:${JSON.stringify(s||{})}`;t.has(a)||(t.add(a),n.push({key:i,parameters:s}))}return o}),n}async fetchExampleRecords(e){return(await this.environment.client.connection(this.environment.connectionId).dataCollection(e.key).list({parameters:e.parameters})).records.map(n=>({id:n.id,fields:n.fields||{}}))}async getExampleRecordsForSchema(e){const t=await this.findReferenceCollections(e),n={};for(const o of t){const i=qt(o);n[i]=await this.fetchExampleRecords(o)}return n}}const Ft={spec:{testerClass:ws},create:{testerClass:hs,operationKey:"create"},"find-by-id":{testerClass:fs,operationKey:"findById"},list:{testerClass:ms,operationKey:"list"},match:{testerClass:gs,operationKey:"match"},search:{testerClass:ys,operationKey:"search"},update:{testerClass:Cs,operationKey:"update"},delete:{testerClass:ps,operationKey:"delete"}};class Ss extends ds{static{c(this,"DataCollectionTestSuite")}dataCollectionKey;testMethod;constructor({environment:e,dataCollectionKey:t,testMethod:n}){super({environment:e}),this.dataCollectionKey=t,this.testMethod=n}async run(){const e=await this.fetchDataCollection(),t=this.testMethod?[this.createTester(this.testMethod,e)]:this.createAllAvailableTesters(e);I.info("\u{1F680} Running tests...");for(const n of t)await this.runTest(n);this.displayTestSummary(t.length)}async fetchDataCollection(){return this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).get()}isOperationAvailable(e,t){return t?!!e[t]:!0}createTester(e,t){const n=Ft[e];if(!n)throw new Error(`Unknown test method "${e}". Available: ${this.getAvailableTestMethods(t).join(", ")}`);const o="operationKey"in n?n.operationKey:void 0;if(!this.isOperationAvailable(t,o))throw new Error(`${o} operation not available for data collection "${this.dataCollectionKey}"`);return this.instantiateTester(n,t)}createAllAvailableTesters(e){return Object.entries(Ft).filter(([,t])=>{const n="operationKey"in t?t.operationKey:void 0;return this.isOperationAvailable(e,n)}).map(([,t])=>this.instantiateTester(t,e))}instantiateTester(e,t){const n={environment:this.environment,dataCollectionKey:this.dataCollectionKey,dataCollection:t};return new e.testerClass(n)}getAvailableTestMethods(e){return Object.entries(Ft).filter(([,t])=>{const n="operationKey"in t?t.operationKey:void 0;return this.isOperationAvailable(e,n)}).map(([t])=>t)}displayTestSummary(e){I.info(`\u{1F4CA} ${e} test${e===1?"":"s"} executed`)}}class bs extends cs{static{c(this,"TestRunner")}constructor(e){super(e)}async initialize(){I.debug("Initializing test runner",{prefix:"TestRunner"})}async run(){try{const{testPath:e,path:t,fix:n}=this.options,o=e.split("/");o.length<2&&(I.error("Invalid test path. Expected format: <type>/<name>[/additional/path][/method]"),process.exit(1));const[i,s,...a]=o;i!=="connectors"&&(I.error(`Test type "${i}" is not yet fully implemented. Currently only "connectors" is fully supported.`),I.error("Supported test types: connectors"),process.exit(1));const l=s,d=i,p=[...a,...t?t.split("/"):[]].join("/"),f=await Lt.create({connectionId:l,testBasePath:d,options:{fix:n}}),C=await f.client.get(`connections/${l}/data`),T=[],S=p?p.split("/"):[];if(S.length===0||S[0]==="data"){S[0]==="data"&&S.shift();let O,_;S.length>=1&&(_=S[0],S.length>=2&&S[1].trim()!==""&&(O=S[1]));for(const L of C){if(_&&_!==L.key)continue;const M=new Ss({environment:f,dataCollectionKey:L.key,testMethod:O});T.push(M)}}T.length===0&&(I.error(`No test suites found for path: ${e}${p?"/"+p:""}`),process.exit(1));const D={};for(const O of T){await O.run();const _=O.getResult();I.debug(`Suite ${O.constructor.name} result:`,{prefix:"TestRunner"}),it(D,_)}console.debug("[TestRunner] All results collected:",Object.keys(D)),f.writeResults(D)}catch(e){throw console.error("Error in TestRunner.run():",e),e}}}const Ue=c(()=>[w.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),w.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),w.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),w.yellow("\u2502 The test command is experimental and subject to rapid changes. \u2502"),w.yellow("\u2502 Features, APIs, and file structures may change without notice. \u2502"),w.yellow("\u2502 Use in production environments is not recommended. \u2502"),w.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
215
- `),"createExperimentalWarning");function vs(r){r.command("test").description("\u26A0\uFE0F EXPERIMENTAL: Test management commands - This feature is experimental and will be changing rapidly. Use at your own risk.").addHelpText("after",Ue()).action(async()=>{console.warn(Ue()),I.error("Please specify a subcommand. Use --help for available options."),process.exit(1)}).command("run").description("Run integration tests for various membrane components").argument("<testPath>",'Test path (e.g., "connectors/netsuite", "actions/create-contact", "connectors/hubspot/data/contacts", "connectors/netsuite/data/contacts/create")').option("-p, --path <path>","Additional path filter within the test scope").option("--fix","Enable auto-fix for test issues").addHelpText("after",["",w.bold("Examples:"),` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/netsuite")} # Test all data collections for netsuite connector`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/netsuite/data/contacts")} # Test specific data collection`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/netsuite/data/contacts/create")} # Test specific method for data collection`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/netsuite/data/contacts/delete")} # Test delete method for data collection`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run actions/create-contact")} # Test specific action`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/hubspot/events")} # Test events for hubspot`,` ${w.gray("\u25B8")} ${w.cyan("membrane test run connectors/salesforce --fix")} # Run tests with auto-fix enabled`,"",w.gray("For more information, visit:"),w.blue(" https://docs.getmembrane.com/docs/development-environment"),"",Ue()].join(`
216
- `)).action(async(t,n)=>{try{console.warn(Ue()),t||(I.error("Test path is required"),process.exit(1)),I.header(`Testing: ${t}`);const o=new bs({testPath:t,path:n.path,fix:n.fix});await o.initialize(),await o.run(),I.success("Tests completed")}catch(o){o instanceof Error&&(I.error(o.message),process.exit(1)),I.error("An unknown error occurred"),process.exit(1)}})}c(vs,"setupTestCommand");const Es=c(()=>{try{const r=qn(import.meta.url),e=_n(r),t=_e(e,"..","package.json");return JSON.parse(Un(t,"utf-8")).version}catch{return"1.0.0"}},"getPackageVersion"),Mn=Es();function Ts(){process.on("SIGINT",()=>process.exit(130));const r=new Bn().name("membrane").description("Command-line interface for Membrane Agent").version(Mn,"-v, --version","Output the version number").option("--verbose","Enable verbose logging (shows debug messages)");r.configureHelp({sortSubcommands:!0,subcommandTerm:c(n=>w.cyan(n.name()),"subcommandTerm"),commandUsage:c(n=>n.name()==="membrane"?`${w.cyan(n.name())} ${w.gray("[options]")} ${w.cyan("[command]")}`:n.usage(),"commandUsage"),optionTerm:c(n=>{const o=n.flags;return`${w.gray("\u25B8")} ${w.cyan(o)}`},"optionTerm"),subcommandDescription:c(n=>w.gray(n.description()),"subcommandDescription"),optionDescription:c(n=>w.gray(n.description),"optionDescription"),commandDescription:c(n=>w.gray(n.description()),"commandDescription")}),r.addHelpText("beforeAll",`
217
- ${w.bold.cyan("Membrane Agent CLI")} ${w.gray(`v${Mn}`)}
219
+ If you don't have an example for a given collection, leave the field empty instead of coming up with a fake record id.`}async findReferenceCollections(e){const t=new Set,n=[];return Xt(e,o=>{if(o.referenceCollection){const i=o.referenceCollection.key,s=o.referenceCollection.parameters,a=`${i}:${JSON.stringify(s||{})}`;t.has(a)||(t.add(a),n.push({key:i,parameters:s}))}return o}),n}async fetchExampleRecords(e){return(await this.environment.client.connection(this.environment.connectionId).dataCollection(e.key).list({parameters:e.parameters})).records.map(n=>({id:n.id,fields:n.fields||{}}))}async getExampleRecordsForSchema(e){const t=await this.findReferenceCollections(e),n={};for(const o of t){const i=Qt(o);n[i]=await this.fetchExampleRecords(o)}return n}}const Jt={spec:{testerClass:_s},create:{testerClass:Ms,operationKey:"create"},"find-by-id":{testerClass:Ls,operationKey:"findById"},list:{testerClass:js,operationKey:"list"},match:{testerClass:Us,operationKey:"match"},search:{testerClass:Ks,operationKey:"search"},update:{testerClass:qs,operationKey:"update"},delete:{testerClass:Fs,operationKey:"delete"}};class Bs extends Ns{static{c(this,"DataCollectionTestSuite")}dataCollectionKey;testMethod;constructor({environment:e,dataCollectionKey:t,testMethod:n}){super({environment:e}),this.dataCollectionKey=t,this.testMethod=n}async run(){const e=await this.fetchDataCollection(),t=this.testMethod?[this.createTester(this.testMethod,e)]:this.createAllAvailableTesters(e);T.info("\u{1F680} Running tests...");for(const n of t)await this.runTest(n);this.displayTestSummary(t.length)}async fetchDataCollection(){return this.environment.client.connection(this.environment.connectionId).dataCollection(this.dataCollectionKey).get()}isOperationAvailable(e,t){return t?!!e[t]:!0}createTester(e,t){const n=Jt[e];if(!n)throw new Error(`Unknown test method "${e}". Available: ${this.getAvailableTestMethods(t).join(", ")}`);const o="operationKey"in n?n.operationKey:void 0;if(!this.isOperationAvailable(t,o))throw new Error(`${o} operation not available for data collection "${this.dataCollectionKey}"`);return this.instantiateTester(n,t)}createAllAvailableTesters(e){return Object.entries(Jt).filter(([,t])=>{const n="operationKey"in t?t.operationKey:void 0;return this.isOperationAvailable(e,n)}).map(([,t])=>this.instantiateTester(t,e))}instantiateTester(e,t){const n={environment:this.environment,dataCollectionKey:this.dataCollectionKey,dataCollection:t};return new e.testerClass(n)}getAvailableTestMethods(e){return Object.entries(Jt).filter(([,t])=>{const n="operationKey"in t?t.operationKey:void 0;return this.isOperationAvailable(e,n)}).map(([t])=>t)}displayTestSummary(e){T.info(`\u{1F4CA} ${e} test${e===1?"":"s"} executed`)}}class Ws extends Rs{static{c(this,"TestRunner")}constructor(e){super(e)}async initialize(){T.debug("Initializing test runner",{prefix:"TestRunner"})}async run(){try{const{testPath:e,path:t,fix:n}=this.options,o=e.split("/");o.length<2&&(T.error("Invalid test path. Expected format: <type>/<name>[/additional/path][/method]"),process.exit(1));const[i,s,...a]=o;i!=="connectors"&&(T.error(`Test type "${i}" is not yet fully implemented. Currently only "connectors" is fully supported.`),T.error("Supported test types: connectors"),process.exit(1));const l=s,d=i,h=[...a,...t?t.split("/"):[]].join("/"),f=await zt.create({connectionId:l,testBasePath:d,options:{fix:n}}),C=await f.client.get(`connections/${l}/data`),E=[],S=h?h.split("/"):[];if(S.length===0||S[0]==="data"){S[0]==="data"&&S.shift();let O,U;S.length>=1&&(U=S[0],S.length>=2&&S[1].trim()!==""&&(O=S[1]));for(const L of C){if(U&&U!==L.key)continue;const M=new Bs({environment:f,dataCollectionKey:L.key,testMethod:O});E.push(M)}}E.length===0&&(T.error(`No test suites found for path: ${e}${h?"/"+h:""}`),process.exit(1));const R={};for(const O of E){await O.run();const U=O.getResult();T.debug(`Suite ${O.constructor.name} result:`,{prefix:"TestRunner"}),dt(R,U)}console.debug("[TestRunner] All results collected:",Object.keys(R)),f.writeResults(R)}catch(e){throw console.error("Error in TestRunner.run():",e),e}}}const He=c(()=>[m.yellow("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),m.yellow("\u2502 \u26A0\uFE0F EXPERIMENTAL FEATURE WARNING \u2502"),m.yellow("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),m.yellow("\u2502 The test command is experimental and subject to rapid changes. \u2502"),m.yellow("\u2502 Features, APIs, and file structures may change without notice. \u2502"),m.yellow("\u2502 Use in production environments is not recommended. \u2502"),m.yellow("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),""].join(`
220
+ `),"createExperimentalWarning");function Js(r){r.command("test").description("\u26A0\uFE0F EXPERIMENTAL: Test management commands - This feature is experimental and will be changing rapidly. Use at your own risk.").addHelpText("after",He()).action(async()=>{console.warn(He()),T.error("Please specify a subcommand. Use --help for available options."),process.exit(1)}).command("run").description("Run integration tests for various membrane components").argument("<testPath>",'Test path (e.g., "connectors/netsuite", "actions/create-contact", "connectors/hubspot/data/contacts", "connectors/netsuite/data/contacts/create")').option("-p, --path <path>","Additional path filter within the test scope").option("--fix","Enable auto-fix for test issues").addHelpText("after",["",m.bold("Examples:"),` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/netsuite")} # Test all data collections for netsuite connector`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/netsuite/data/contacts")} # Test specific data collection`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/netsuite/data/contacts/create")} # Test specific method for data collection`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/netsuite/data/contacts/delete")} # Test delete method for data collection`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run actions/create-contact")} # Test specific action`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/hubspot/events")} # Test events for hubspot`,` ${m.gray("\u25B8")} ${m.cyan("membrane test run connectors/salesforce --fix")} # Run tests with auto-fix enabled`,"",m.gray("For more information, visit:"),m.blue(" https://docs.getmembrane.com/docs/development-environment"),"",He()].join(`
221
+ `)).action(async(t,n)=>{try{console.warn(He()),t||(T.error("Test path is required"),process.exit(1)),T.header(`Testing: ${t}`);const o=new Ws({testPath:t,path:n.path,fix:n.fix});await o.initialize(),await o.run(),T.success("Tests completed")}catch(o){o instanceof Error&&(T.error(o.message),process.exit(1)),T.error("An unknown error occurred"),process.exit(1)}})}c(Js,"setupTestCommand");const zs=c(()=>{try{const r=cr(import.meta.url),e=sr(r),t=Ge(e,"..","package.json");return JSON.parse(ir(t,"utf-8")).version}catch{return"1.0.0"}},"getPackageVersion"),er=zs();function Hs(){process.on("SIGINT",()=>process.exit(130));const r=new lr().name("membrane").description("Command-line interface for Membrane Agent").version(er,"-v, --version","Output the version number").option("--verbose","Enable verbose logging (shows debug messages)");r.configureHelp({sortSubcommands:!0,subcommandTerm:c(n=>m.cyan(n.name()),"subcommandTerm"),commandUsage:c(n=>n.name()==="membrane"?`${m.cyan(n.name())} ${m.gray("[options]")} ${m.cyan("[command]")}`:n.usage(),"commandUsage"),optionTerm:c(n=>{const o=n.flags;return`${m.gray("\u25B8")} ${m.cyan(o)}`},"optionTerm"),subcommandDescription:c(n=>m.gray(n.description()),"subcommandDescription"),optionDescription:c(n=>m.gray(n.description),"optionDescription"),commandDescription:c(n=>m.gray(n.description()),"commandDescription")}),r.addHelpText("beforeAll",`
222
+ ${m.bold.cyan("Membrane Agent CLI")} ${m.gray(`v${er}`)}
218
223
 
219
- `);const e=new Ao(Le.Cli,process.cwd());Yo(r),si(r),bi(r),Mi(r),pi(r),vs(r),hi(r),as(r,e),Wo(r,e),_r(r,e),_o(r,e),process.argv.length===2&&(r.outputHelp(),process.exit(0)),r.parse(),r.opts().verbose&&m.setVerboseMode(!0)}c(Ts,"runCLI"),Ts();
224
+ `);const e=new ni(Je.Cli,process.cwd());vo(r),Ii(r),Ni(r),yi(r),ps(r),qi(r),ns(r),_i(r),Js(r),Ki(r),As(r,e),xi(r,e),Ao(r,e),hi(r,e),r.hook("preAction",(n,o)=>{const i=o.opts().apiUri||Se()||ne;i!==ne&&console.error(`Using apiUri: ${i}`)}),process.argv.length===2&&(r.outputHelp(),process.exit(0)),r.parse(),r.opts().verbose&&g.setVerboseMode(!0)}c(Hs,"runCLI"),Hs();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@membranehq/cli",
3
- "version": "1.4.11",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "cli",
@@ -19,18 +19,12 @@
19
19
  ],
20
20
  "type": "module",
21
21
  "main": "dist/index.js",
22
- "exports": {
23
- ".": {
24
- "types": "./dist/index.d.ts",
25
- "import": "./dist/index.js"
26
- }
27
- },
28
22
  "dependencies": {
29
23
  "@anthropic-ai/sdk": "0.62.0",
30
24
  "@buildwithlayer/openapi-to-tools": "^1.3.3",
31
25
  "@faker-js/faker": "^9.8.0",
32
26
  "@inkjs/ui": "^2.0.0",
33
- "@membranehq/sdk": "^0.17.1",
27
+ "@membranehq/sdk": "^0.17.2",
34
28
  "@modelcontextprotocol/sdk": "^1.26.0",
35
29
  "@types/js-yaml": "^4.0.9",
36
30
  "@types/jsonwebtoken": "^9.0.10",
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env node