@entrydesk/cli 1.6.0 → 1.7.1

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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import E from"crypto";import f from"fs";import pe from"path";import u from"fs";import ce from"os";import S from"path";import{HTTPError as ae,TimeoutError as le}from"ky";import re,{TimeoutError as se}from"ky";import R from"fs";function X(r){let e=o.getEffectiveProfileName(r);return o.getProfileConfigFile(e)}function a(r){let e=X(r);try{if(!R.existsSync(e))return{};let t=R.readFileSync(e,"utf-8");return JSON.parse(t)}catch{return{}}}function d(r,e){let t=o.getEffectiveProfileName(e);o.ensureProfileDir(t);let s=o.getProfileConfigFile(t);R.writeFileSync(s,JSON.stringify(r,null,2),{mode:384});try{R.chmodSync(s,384)}catch{}}var U={getApiUrl(r){return a(r).apiUrl??null},setApiUrl(r,e){let t=a(e);t.apiUrl=r,d(t,e)},clearApiUrl(r){let e=a(r);delete e.apiUrl,d(e,r)},getHubUrl(r){return a(r).hubUrl??null},setHubUrl(r,e){let t=a(e);t.hubUrl=r,d(t,e)},getAll(r){return a(r)},setAll(r,e){d(r,e)},set(r,e,t){let s=a(t);s[r]=e,d(s,t)},get(r,e){return a(e)[r]},unset(r,e){let t=a(e);delete t[r],d(t,e)},getDefaultModel(r){return a(r).model?.default??null},setDefaultModel(r,e){let t=a(e);t.model={...t.model,default:r},d(t,e)},getDefaultConnectorIds(r){return a(r).connectors?.defaults??null},setDefaultConnectorIds(r,e){let t=a(e);t.connectors={...t.connectors,defaults:r},d(t,e)},getCapabilities(r){return a(r).capabilities??null},setCapabilities(r,e){let t=a(e);t.capabilities={...t.capabilities,...r},d(t,e)},getLocalTools(r){return a(r).localTools??null},setLocalTools(r,e){let t=a(e);t.localTools={...t.localTools,...r},d(t,e)}};var Z="https://entrydesk-api.dcard.io",Q="https://entrydesk.dcard.io",ee=3100,te=300*1e3;function T(r){return{apiUrl:U.getApiUrl(r)||process.env.ENTRYDESK_API_URL||Z,hubUrl:U.getHubUrl(r)||process.env.ENTRYDESK_HUB_URL||Q,mcp:{httpPort:ee},auth:{loginTimeout:te,ignoreSslErrors:process.env.ENTRYDESK_IGNORE_SSL_ERRORS==="true"||!1}}}function O(r){return T(r).apiUrl}function Ae(r){return T(r).hubUrl}function be(){return T().mcp}function z(){return T().auth}var W="ENTRYDESK_ALLOW_INSECURE_LOGIN",K=class r{client;baseUrl;constructor(e,t){this.baseUrl=e||O(t),z().ignoreSslErrors&&(process.env.NODE_TLS_REJECT_UNAUTHORIZED="0",process.env.DEBUG&&console.log("[DEBUG] ApiClient: SSL certificate validation disabled")),this.client=re.create({prefixUrl:this.baseUrl,timeout:3e4,retry:{limit:3,methods:["get","put","head","delete","options","trace"],statusCodes:[408,413,429,500,502,503,504]},hooks:{beforeError:[n=>(n.request?.headers&&(n.request.headers.delete("authorization"),n.request.headers.delete("Authorization")),n)]}})}getBaseUrl(){return this.baseUrl}shouldAllowInsecureLogin(){return process.env[W]?.toLowerCase()==="true"}assertSecureLogin(){let e;try{e=new URL(this.baseUrl)}catch(t){let s=t instanceof Error?t:void 0;throw new c("Invalid API URL; cannot perform login.",1,s)}if(e.protocol!=="https:"&&!this.shouldAllowInsecureLogin())throw new c(`Login requires HTTPS. Set apiUrl to https:// or set ${W}=true for local development.`)}async login(e,t){return this.assertSecureLogin(),await this.client.post("v1/auth/credentials/token",{json:{email:e,password:t}}).json()}async refreshToken(e){return await this.client.post("v1/auth/token/refresh",{json:{refreshToken:e}}).json()}async revokeToken(e){await this.client.post("v1/auth/token/revoke",{json:{refreshToken:e}})}async getWorkspaces(e){try{return process.env.DEBUG&&console.log(`[DEBUG] Calling getWorkspaces API: ${this.baseUrl}/v1/workspaces`),await this.client.get("v1/workspaces",{headers:{Authorization:`Bearer ${e}`}}).json()}catch(t){throw process.env.DEBUG&&(console.error("[DEBUG] getWorkspaces error:",t),t instanceof Error&&"cause"in t&&console.error("[DEBUG] Error cause:",t.cause)),t}}async getAgents(e,t){return await this.client.get(`v1/workspaces/${t}/availableAgents`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:100}}).json()}async createAgent(e,t,s){return await this.client.post(`v1/workspaces/${t}/agents`,{headers:{Authorization:`Bearer ${e}`},json:s}).json()}async updateAgent(e,t,s,n){await this.client.put(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`},json:n})}async deleteAgent(e,t,s){await this.client.delete(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async getModels(e,t){return await this.client.get(`v1/workspaces/${t}/models`,{headers:{Authorization:`Bearer ${e}`}}).json()}async getConnectors(e,t){return await this.client.get(`v1/workspaces/${t}/availableConnectors`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:100,connectionStatus:"connected"}}).json()}async getChats(e,t,s=20){return await this.client.get(`v1/workspaces/${t}/chats`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:s}}).json()}async getMessages(e,t,s,n=50){return await this.client.get(`v1/workspaces/${t}/chats/${s}/messages`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:n}}).json()}async getAgent(e,t,s){return await this.client.get(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`}}).json()}async listAvailableSkills(e,t,s){return await this.client.get(`v1/workspaces/${t}/availableSkills`,{headers:{Authorization:`Bearer ${e}`},searchParams:{createdBy:s?.createdBy,nextKey:s?.nextKey,limit:s?.limit??20}}).json()}async uploadSkill(e,t,s){return await this.client.post(`v1/workspaces/${t}/skills`,{headers:{Authorization:`Bearer ${e}`},body:s}).json()}async deleteSkill(e,t,s){await this.client.delete(`v1/workspaces/${t}/skills/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async chat(e,t,s){try{let n=await this.client.post(`v1/workspaces/${t}/chats`,{headers:{Authorization:`Bearer ${e}`},json:s,timeout:6e4,throwHttpErrors:!1});if(!n.ok){let i=await n.text();throw new Error(`Chat API failed (${n.status}): ${i}`)}return n}catch(n){throw n instanceof se?new Error("Request timed out. Please check your connection and try again.",{cause:n}):n}}async getBudget(e,t){return await this.client.get(`v1/workspaces/${t}/budget`,{headers:{Authorization:`Bearer ${e}`}}).json()}async updateBudget(e,t,s){return await this.client.put(`v1/workspaces/${t}/budget`,{headers:{Authorization:`Bearer ${e}`},json:{budget:s}}).json()}async getMonthlyUsages(e,t,s){return await this.client.get(`v1/workspaces/${t}/monthlyUsages`,{headers:{Authorization:`Bearer ${e}`},searchParams:{nextKey:s?.nextKey,limit:s?.limit??20}}).json()}async createChatShare(e,t,s,n="public"){return await this.client.post(`v1/workspaces/${t}/chats/${s}/shares`,{headers:{Authorization:`Bearer ${e}`},json:{visibility:n}}).json()}async getSharedChat(e,t){let s={};return t&&(s.Authorization=`Bearer ${t}`),await this.client.get(`v1/shares/${e}`,{headers:s}).json()}async getSharedMessages(e,t=50,s){let n={};return s&&(n.Authorization=`Bearer ${s}`),await this.client.get(`v1/shares/${e}/messages`,{headers:n,searchParams:{limit:t}}).json()}async deleteChat(e,t,s){await this.client.delete(`v1/workspaces/${t}/chats/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async getModelProviders(e,t){return await this.client.get(`v1/workspaces/${t}/modelProviders`,{headers:{Authorization:`Bearer ${e}`}}).json()}async uploadFile(e,t,s,n){let i=new FormData,g=new Blob([s.buffer],{type:s.mimeType});return i.append("file",g,s.filename),i.append("modelProviderId",n),await this.client.post(`v1/workspaces/${t}/files`,{headers:{Authorization:`Bearer ${e}`},body:i,timeout:12e4}).json()}async getSchedules(e,t,s){let n={limit:s?.limit??20};return s?.nextKey&&(n.nextKey=s.nextKey),s?.search&&(n.search=s.search),await this.client.get(`v1/workspaces/${t}/schedules`,{headers:{Authorization:`Bearer ${e}`},searchParams:n}).json()}async getSchedule(e,t,s){return await this.client.get(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`}}).json()}async createSchedule(e,t,s){return await this.client.post(`v1/workspaces/${t}/schedules`,{headers:{Authorization:`Bearer ${e}`},json:s}).json()}async updateSchedule(e,t,s,n){await this.client.put(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`},json:n})}async deleteSchedule(e,t,s){await this.client.delete(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`}})}static async getAuthenticatedClient(e){let t=await h.getAll(e);return t?{client:new r(void 0,e),accessToken:t.accessToken,workspaceId:t.workspaceId??null}:null}};function ne(){return process.env.ENTRYDESK_SANDBOX==="true"}function I(){if(!ne())return null;let r=process.env.ENTRYDESK_SANDBOX_ACCESS_TOKEN?.trim(),e=process.env.ENTRYDESK_SANDBOX_WORKSPACE_ID?.trim();if(!r||!e){let t=[];throw r||t.push("ENTRYDESK_SANDBOX_ACCESS_TOKEN"),e||t.push("ENTRYDESK_SANDBOX_WORKSPACE_ID"),new Error(`Sandbox mode enabled but missing credentials: ${t.join(", ")}`)}return{accessToken:r,workspaceId:e}}var p=class extends Error{code;constructor(e,t){super(t),this.code=e}};function ie(r){try{let e=r.split(".");if(e.length<2)return null;let t=e[1].replace(/-/g,"+").replace(/_/g,"/"),s=(4-t.length%4)%4,n=t.padEnd(t.length+s,"="),i=Buffer.from(n,"base64").toString("utf8");return JSON.parse(i)}catch{return null}}function oe(r,e=300){let t=ie(r);if(!t?.exp)return!1;let s=Math.floor(Date.now()/1e3);return t.exp-s<=e}async function G(r){let e=r.accessToken;if(!oe(e))return e;if(!r.refreshToken)throw await h.clear(),new p("session-expired","Session expired. Please run `entrydesk login` again.");try{let t=new K,{token:s}=await t.refreshToken(r.refreshToken);return await h.saveAccessToken(s),s}catch{throw await h.clear(),new p("session-expired","Session expired. Please run `entrydesk login` again.")}}async function De(){let r=I();if(r)return r.accessToken;let e=await h.getAll();if(!e?.accessToken)throw new p("not-logged-in","Not logged in. Run `entrydesk login` first.");return G(e)}async function _e(){let r=I();if(r)return{accessToken:r.accessToken,workspaceId:r.workspaceId};let e=await h.getAll();if(!e?.accessToken)throw new p("not-logged-in","Not logged in. Run `entrydesk login` first.");if(!e.workspaceId)throw new p("no-workspace","No workspace selected. Run `entrydesk workspaces` first.");return{accessToken:await G(e),workspaceId:e.workspaceId}}var c=class extends Error{constructor(t,s=1,n){super(t);this.exitCode=s;this.cause=n;this.name="CLIError"}};function je(r){let e=!!process.env.DEBUG;r instanceof c&&(console.error(`Error: ${r.message}`),r.cause&&(console.error(`Cause: ${r.cause.message}`),e&&r.cause.stack&&console.error(r.cause.stack)),process.exit(r.exitCode)),r instanceof p&&(console.error(r.message),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof le&&(console.error("Request timed out. Please check your connection and try again."),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof ae&&(console.error(`Request failed (${r.response.status}): ${r.response.statusText}`),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof Error&&(console.error(`Unexpected error: ${r.message}`),e&&r.stack&&console.error(r.stack),process.exit(1)),console.error("An unknown error occurred"),process.exit(1)}var m="default",ge=/^[A-Za-z0-9_-]+$/,F=null;function D(){return S.join(ce.homedir(),".entrydesk")}function H(){return S.join(D(),"profiles")}function J(){return S.join(D(),"profiles.json")}function $(r){return S.join(H(),r)}function _(r){if(!u.existsSync(r))u.mkdirSync(r,{mode:448,recursive:!0});else try{u.chmodSync(r,448)}catch{}}function de(){_(D())}function B(){_(H())}function ue(){let r=J();try{if(!u.existsSync(r))return null;let e=u.readFileSync(r,"utf-8");return JSON.parse(e)}catch{return null}}function P(r){de();let e=J();u.writeFileSync(e,JSON.stringify(r,null,2),{mode:384});try{u.chmodSync(e,384)}catch{}}function fe(r){let e=r.profiles?.length?Array.from(new Set(r.profiles)):[],t=r.currentProfile&&e.includes(r.currentProfile)?r.currentProfile:e[0]||m;return e.length===0&&e.push(m),e.includes(t)||e.unshift(t),{currentProfile:t,profiles:e}}function y(){let r=ue();if(!r){let t={currentProfile:m,profiles:[m]};return P(t),B(),x(m),t}let e=fe(r);return(e.currentProfile!==r.currentProfile||e.profiles.length!==r.profiles.length)&&P(e),B(),e}function k(r){if(!r)throw new c("Profile name is required.");if(!ge.test(r))throw new c("Invalid profile name. Use letters, numbers, dash, or underscore.")}function b(r,e){if(!r.profiles.includes(e))throw new c(`Profile not found: ${e}`)}function Y(r,e){if(r.profiles.includes(e))throw new c(`Profile already exists: ${e}`)}function x(r){B(),_($(r))}var o={DEFAULT_PROFILE:m,setOverrideProfile(r){F=r},getOverrideProfile(){return F},getProfileDir(r){return $(r)},getProfileConfigFile(r){return S.join(o.getProfileDir(r),"config.json")},getProfileCredentialsFile(r){return S.join(o.getProfileDir(r),"credentials.json")},getProfileNames(){return[...y().profiles]},getProfileCount(){return o.getProfileNames().length},getCurrentProfileName(){return y().currentProfile},getEffectiveProfileName(r){let e=y(),t=r||F||e.currentProfile;return k(t),b(e,t),t},ensureProfileExists(r){let e=y();k(r),b(e,r)},createProfile(r){k(r);let e=y();Y(e,r);let t={currentProfile:e.currentProfile,profiles:[...e.profiles,r]};P(t),x(r)},deleteProfile(r){k(r);let e=y();if(b(e,r),e.profiles.length===1)throw new c("Cannot delete the last profile.");let t=e.profiles.filter(i=>i!==r),s=e.currentProfile;s===r&&(s=t.includes(m)?m:t[0]),P({currentProfile:s,profiles:t});try{u.rmSync(o.getProfileDir(r),{recursive:!0,force:!0})}catch{}return{currentProfile:s}},renameProfile(r,e){k(r),k(e);let t=y();b(t,r),Y(t,e);let s=$(r),n=$(e);if(u.existsSync(s))try{u.renameSync(s,n)}catch(v){throw new c("Failed to rename profile directory.",1,v)}else x(e);let i=t.profiles.map(v=>v===r?e:v),g=t.currentProfile===r?e:t.currentProfile;return P({currentProfile:g,profiles:i}),{currentProfile:g}},setCurrentProfile(r){k(r);let e=y();b(e,r);let t={currentProfile:r,profiles:e.profiles};P(t),x(r)},ensureProfileDir:x};var he="ENTRYDESK_FORCE_FILE_STORAGE",C="EntryDesk CLI",ye="__entrydesk_keychain_test__",me="profile:",we=".credentials.key",q={keychain:!1,permissions:!1,corruptFile:!1,windowsFile:!1};function w(r,e){q[r]||(q[r]=!0,console.error(e))}function V(r){if(!r||typeof r!="object")return!1;let e=r;return!(!("accessToken"in e||"refreshToken"in e||"email"in e||"workspaceId"in e)||"accessToken"in e&&typeof e.accessToken!="string"||"refreshToken"in e&&typeof e.refreshToken!="string"||"email"in e&&typeof e.email!="string"||"workspaceId"in e&&typeof e.workspaceId!="string")}function ve(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.getPassword=="function"&&typeof e.setPassword=="function"&&typeof e.deletePassword=="function"}function M(r){return o.getProfileCredentialsFile(r)}var L=class{getKeyFilePath(e){return pe.join(o.getProfileDir(e),we)}loadEncryptionKey(e){let t=this.getKeyFilePath(e);try{if(!f.existsSync(t))return null;let s=f.readFileSync(t);return s.length!==32?null:(this.ensureSecureFilePermissions(t),s)}catch{return null}}createEncryptionKey(e){o.ensureProfileDir(e);let t=this.getKeyFilePath(e),s=E.randomBytes(32);return f.writeFileSync(t,s,{mode:384}),this.ensureSecureFilePermissions(t),s}getOrCreateEncryptionKey(e){let t=this.loadEncryptionKey(e);return t||this.createEncryptionKey(e)}encrypt(e,t){let s=E.randomBytes(16),n=E.createCipheriv("aes-256-gcm",t,s),i=n.update(e,"utf8","hex");i+=n.final("hex");let g=n.getAuthTag();return`${s.toString("hex")}:${g.toString("hex")}:${i}`}decrypt(e,t){let s=e.split(":");if(s.length!==3)throw new Error("Invalid encrypted data format");let n=Buffer.from(s[0],"hex"),i=Buffer.from(s[1],"hex"),g=s[2],A=E.createDecipheriv("aes-256-gcm",t,n,{authTagLength:16});A.setAuthTag(i);let v=A.update(g,"hex","utf8");return v+=A.final("utf8"),v}tryDecrypt(e,t){try{return this.decrypt(e,t)}catch{return null}}tryParseCredentials(e){try{let t=JSON.parse(e);return V(t)?t:null}catch{return null}}ensureSecureFilePermissions(e){if(process.platform!=="win32"){try{f.chmodSync(e,384)}catch{w("permissions","Warning: Failed to set secure permissions on credentials file.");return}try{(f.statSync(e).mode&511)!==384&&w("permissions","Warning: Credentials file permissions are too permissive.")}catch{w("permissions","Warning: Failed to verify credentials file permissions.")}}}async getCredentials(e){let t=M(e);try{if(!f.existsSync(t))return null;let s=f.readFileSync(t,"utf-8").trim();if(!s)return null;let n=this.loadEncryptionKey(e),i=n?this.tryDecrypt(s,n):null;if(i){let g=this.tryParseCredentials(i);if(g)return g}return w("corruptFile","Warning: Credentials file is unreadable. Please run `entrydesk login` again."),null}catch{return null}}async setCredentials(e,t){o.ensureProfileDir(e);let s=M(e),n=this.getOrCreateEncryptionKey(e),i=this.encrypt(JSON.stringify(t),n);f.writeFileSync(s,i,{mode:384}),this.ensureSecureFilePermissions(s)}async deleteCredentials(e){let t=M(e);try{f.existsSync(t)&&f.unlinkSync(t)}catch{}}},N=class{keytarModule=null;keytarLoadAttempted=!1;keychainAvailable=null;async getKeytar(){if(this.keytarLoadAttempted)return this.keytarModule;this.keytarLoadAttempted=!0;try{let t=await import("keytar"),s=t&&typeof t=="object"&&"default"in t?t.default:t;this.keytarModule=ve(s)?s:null}catch{this.keytarModule=null}return this.keytarModule}getAccount(e){return`${me}${e}`}async checkKeychainAvailability(){if(this.keychainAvailable!==null)return this.keychainAvailable;try{let e=await this.getKeytar();if(!e)return this.keychainAvailable=!1,!1;let t=`${ye}${E.randomBytes(8).toString("hex")}`,s="test";await e.setPassword(C,t,s);let n=await e.getPassword(C,t),i=await e.deletePassword(C,t);return this.keychainAvailable=!!(i&&n===s),this.keychainAvailable}catch{return this.keychainAvailable=!1,!1}}async isAvailable(){return this.checkKeychainAvailability()}async getCredentials(e){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let t=await this.getKeytar();if(!t)throw new Error("Keytar module not available");let s=await t.getPassword(C,this.getAccount(e));if(!s)return null;let n=this.tryParseKeychainPayload(s);return n||w("corruptFile",`Warning: Keychain entry for profile "${e}" is unreadable.`),n}async setCredentials(e,t){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let s=await this.getKeytar();if(!s)throw new Error("Keytar module not available");let n=JSON.stringify(t);await s.setPassword(C,this.getAccount(e),n)}async deleteCredentials(e){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let t=await this.getKeytar();if(!t)throw new Error("Keytar module not available");await t.deletePassword(C,this.getAccount(e))}tryParseKeychainPayload(e){try{let t=JSON.parse(e);return V(t)?t:null}catch{return null}}},j=class{keychainStorage=null;fileStorage=new L;keychainAvailable=null;initPromise=null;shouldForceFileStorage(){return process.env[he]?.toLowerCase()==="true"}async initializeStorage(){if(this.shouldForceFileStorage()){this.keychainAvailable=!1,this.keychainStorage=null;return}try{let e=new N,t=await e.isAvailable();this.keychainAvailable=t,this.keychainStorage=t?e:null}catch{this.keychainAvailable=!1,this.keychainStorage=null}!this.keychainAvailable&&!this.shouldForceFileStorage()&&(w("keychain","Keychain unavailable. Falling back to file-based credentials."),process.platform==="win32"&&w("windowsFile","Warning: File-based credentials on Windows may be readable by other users."))}async ensureInitialized(){if(this.shouldForceFileStorage()){this.keychainAvailable=!1,this.keychainStorage=null;return}this.keychainAvailable===null&&(this.initPromise||(this.initPromise=this.initializeStorage()),await this.initPromise)}disableKeychain(){this.shouldForceFileStorage()||w("keychain","Keychain error encountered. Falling back to file-based credentials."),this.keychainAvailable=!1,this.keychainStorage=null}async getKeychainStorage(){return await this.ensureInitialized(),this.shouldForceFileStorage()?null:this.keychainAvailable?this.keychainStorage:null}async getCredentials(e){let t=await this.getKeychainStorage();if(!t)return this.fileStorage.getCredentials(e);try{let n=await t.getCredentials(e);if(n)return n}catch{return this.disableKeychain(),this.fileStorage.getCredentials(e)}let s=await this.fileStorage.getCredentials(e);if(s)try{await t.setCredentials(e,s),await this.fileStorage.deleteCredentials(e)}catch{}return s}async setCredentials(e,t){let s=await this.getKeychainStorage();if(!s){await this.fileStorage.setCredentials(e,t);return}try{await s.setCredentials(e,t),await this.fileStorage.deleteCredentials(e)}catch{this.disableKeychain(),await this.fileStorage.setCredentials(e,t)}}async deleteCredentials(e){let t=await this.getKeychainStorage();if(t)try{await t.deleteCredentials(e)}catch{this.disableKeychain()}await this.fileStorage.deleteCredentials(e)}},l=new j,h={async saveAccessToken(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.accessToken=r,await l.setCredentials(t,s)},async getAccessToken(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.accessToken??null},async saveRefreshToken(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.refreshToken=r,await l.setCredentials(t,s)},async getRefreshToken(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.refreshToken??null},async saveEmail(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.email=r,await l.setCredentials(t,s)},async getEmail(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.email??null},async saveWorkspaceId(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.workspaceId=r,await l.setCredentials(t,s)},async getWorkspaceId(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.workspaceId??null},async saveAll(r,e){let t=o.getEffectiveProfileName(e);await l.setCredentials(t,r)},async getAll(r){let e=o.getEffectiveProfileName(r);return l.getCredentials(e)},async clear(r){let e=o.getEffectiveProfileName(r);await l.deleteCredentials(e)},async isLoggedIn(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.accessToken!=null}};export{h as a,p as b,De as c,_e as d,c as e,je as f,o as g,U as h,T as i,O as j,Ae as k,be as l,z as m,K as n};
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{i as a,j as b,k as c,l as d,m as e}from"./chunk-J6ER4MMT.js";export{b as getApiUrl,e as getAuthConfig,a as getConfig,c as getHubUrl,d as getMcpConfig};
2
+ import{i as a,j as b,k as c,l as d,m as e}from"./chunk-G36L33PZ.js";export{b as getApiUrl,e as getAuthConfig,a as getConfig,c as getHubUrl,d as getMcpConfig};
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{a as $e,b as Qe,c as dt,d as pe,e as Lt,f as xe,g as H,h as ne,i as Me,n as F}from"./chunk-J6ER4MMT.js";import{render as Xi}from"ink";function Io(){console.log(`
2
+ import{a as $e,b as Qe,c as dt,d as pe,e as Lt,f as xe,g as H,h as ne,i as Me,n as F}from"./chunk-G36L33PZ.js";import{render as Xi}from"ink";function Io(){console.log(`
3
3
  Usage: entrydesk agents [list|create|update|delete] [options]
4
4
 
5
5
  Commands:
@@ -164,7 +164,7 @@ ${y.map(c=>` - ${c}`).join(`
164
164
  `)}`:"No capabilities enabled."),!0}case"chats":return p(n?`Current chat: ${n}`:`Current chat: ${o.slice(0,8)}...`),!0;case"workspace":{if(!A)return p("Not authenticated."),!0;let y=new F;if(!v.value){try{let{workspaces:u}=await y.getWorkspaces(A.accessToken);if(u.length===0)p("No workspaces found.");else{let g=u.map(S=>` ${S.id===A.workspaceId?"* ":" "}${S.name}
165
165
  ID: ${S.id}`);p(`Workspaces:
166
166
  ${g.join(`
167
- `)}`)}}catch(u){p(`Failed to load workspaces: ${u instanceof Error?u.message:"Unknown error"}`)}return!0}let c=v.value;try{let{workspaces:u}=await y.getWorkspaces(A.accessToken),g=u.find(k=>k.id===c);if(!g)return p(`Workspace not found: ${c}`),!0;let{TokenStorage:S}=await import("./token-storage-JKKVPVG3.js");await S.saveWorkspaceId(c),p(`Switched to workspace: ${g.name}`),p("Please restart the chat to apply changes.")}catch(u){p(`Failed to switch workspace: ${u instanceof Error?u.message:"Unknown error"}`)}return!0}case"budget":{if(!A)return p("Not authenticated."),!0;let y=new F;if(!v.value){try{let g=await y.getBudget(A.accessToken,A.workspaceId);g.budget===null?p("No budget set for this workspace."):p(`Current budget: $${g.budget}`)}catch(g){p(`Failed to get budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}let c=v.value.toLowerCase();if(c==="clear"||c==="null"){try{await y.updateBudget(A.accessToken,A.workspaceId,null),p("Budget cleared.")}catch(g){p(`Failed to clear budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}let u=parseFloat(c);if(isNaN(u)||u<0)return p("Invalid budget amount. Must be a positive number."),!0;try{await y.updateBudget(A.accessToken,A.workspaceId,c),p(`Budget set to: $${c}`)}catch(g){p(`Failed to set budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}case"usage":{if(!A)return p("Not authenticated."),!0;let y=new F;try{let c=[],u,g=0,S=10;do{let Ie=await y.getMonthlyUsages(A.accessToken,A.workspaceId,{nextKey:u,limit:20});if(c.push(...Ie.monthlyUsages),u=Ie.nextKey,g++,g>=S){p(`Warning: Showing first ${c.length} entries (pagination limit reached)`);break}}while(u);if(c.length===0)return p("No usage data found for this workspace."),!0;let k=0,N=0;for(let Ie of c)k+=parseFloat(Ie.totalCost),N+=Ie.operationCount;let J=[...c].toSorted((Ie,ut)=>parseFloat(ut.totalCost)-parseFloat(Ie.totalCost)),G=10,ue=J.slice(0,G),Ue=[`Total Cost: $${k.toFixed(2)}`,`Total Operations: ${N.toLocaleString()}`,`Users: ${c.length}`,"",`Top ${ue.length} Users by Cost:`,...ue.map(Ie=>` ${Ie.user.name}: $${parseFloat(Ie.totalCost).toFixed(2)} (${Ie.operationCount.toLocaleString()} ops)`)];c.length>G&&(Ue.push("",`... and ${c.length-G} more users`),Ue.push("Use CLI command for full list: entrydesk usage --limit <n> --sort-by <cost|operations>")),p(Ue.join(`
167
+ `)}`)}}catch(u){p(`Failed to load workspaces: ${u instanceof Error?u.message:"Unknown error"}`)}return!0}let c=v.value;try{let{workspaces:u}=await y.getWorkspaces(A.accessToken),g=u.find(k=>k.id===c);if(!g)return p(`Workspace not found: ${c}`),!0;let{TokenStorage:S}=await import("./token-storage-OB3GLFZP.js");await S.saveWorkspaceId(c),p(`Switched to workspace: ${g.name}`),p("Please restart the chat to apply changes.")}catch(u){p(`Failed to switch workspace: ${u instanceof Error?u.message:"Unknown error"}`)}return!0}case"budget":{if(!A)return p("Not authenticated."),!0;let y=new F;if(!v.value){try{let g=await y.getBudget(A.accessToken,A.workspaceId);g.budget===null?p("No budget set for this workspace."):p(`Current budget: $${g.budget}`)}catch(g){p(`Failed to get budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}let c=v.value.toLowerCase();if(c==="clear"||c==="null"){try{await y.updateBudget(A.accessToken,A.workspaceId,null),p("Budget cleared.")}catch(g){p(`Failed to clear budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}let u=parseFloat(c);if(isNaN(u)||u<0)return p("Invalid budget amount. Must be a positive number."),!0;try{await y.updateBudget(A.accessToken,A.workspaceId,c),p(`Budget set to: $${c}`)}catch(g){p(`Failed to set budget: ${g instanceof Error?g.message:"Unknown error"}`)}return!0}case"usage":{if(!A)return p("Not authenticated."),!0;let y=new F;try{let c=[],u,g=0,S=10;do{let Ie=await y.getMonthlyUsages(A.accessToken,A.workspaceId,{nextKey:u,limit:20});if(c.push(...Ie.monthlyUsages),u=Ie.nextKey,g++,g>=S){p(`Warning: Showing first ${c.length} entries (pagination limit reached)`);break}}while(u);if(c.length===0)return p("No usage data found for this workspace."),!0;let k=0,N=0;for(let Ie of c)k+=parseFloat(Ie.totalCost),N+=Ie.operationCount;let J=[...c].toSorted((Ie,ut)=>parseFloat(ut.totalCost)-parseFloat(Ie.totalCost)),G=10,ue=J.slice(0,G),Ue=[`Total Cost: $${k.toFixed(2)}`,`Total Operations: ${N.toLocaleString()}`,`Users: ${c.length}`,"",`Top ${ue.length} Users by Cost:`,...ue.map(Ie=>` ${Ie.user.name}: $${parseFloat(Ie.totalCost).toFixed(2)} (${Ie.operationCount.toLocaleString()} ops)`)];c.length>G&&(Ue.push("",`... and ${c.length-G} more users`),Ue.push("Use CLI command for full list: entrydesk usage --limit <n> --sort-by <cost|operations>")),p(Ue.join(`
168
168
  `))}catch(c){p(`Failed to get usage: ${c instanceof Error?c.message:"Unknown error"}`)}return!0}case"status":{let y=H.getCurrentProfileName(),c=r?P.find(k=>k.id===r)?.name||r:null,u=l?E.find(k=>k.id===l)?.name||l:null,g=C?R.filter(k=>C.includes(k.id)).map(k=>k.name):[],S=[];return m&&S.push("Sandbox"),T&&S.push("Web Search"),V&&S.push("Image Gen"),ae&&S.push("Chart"),Se&&S.push("FileSystem"),$e.getEmail().then(k=>{let N=[`Profile: ${y}${y!=="default"?" (non-default)":""}`,k?`Logged in as: ${k}`:"Not logged in",A?.workspaceId?`Workspace: ${A.workspaceId}`:null,_?`Chat ID: ${o}`:null,c?`Model: ${c}`:null,u?`Agent: ${u}`:null,`Connectors: ${g.length>0?g.join(", "):"none"}`,`Capabilities: ${S.length>0?S.join(", "):"none"}`].filter(Boolean);p(N.join(`
169
169
  `))}).catch(()=>{let k=[`Profile: ${y}`,"Not logged in",_?`Chat ID: ${o}`:null,c?`Model: ${c}`:null,u?`Agent: ${u}`:null,`Connectors: ${g.length>0?g.join(", "):"none"}`,`Capabilities: ${S.length>0?S.join(", "):"none"}`].filter(Boolean);p(k.join(`
170
170
  `))}),!0}case"profile":{let y=v.value?v.value.split(/\s+/).filter(Boolean):[];if(y.length===0)return _e("profile"),!0;let c=y[0]?.toLowerCase()||"list",u=y[1],g=y[2],S=H.getProfileNames(),k=H.getCurrentProfileName(),N=H.getOverrideProfile(),J=(G,ue=!1)=>{if(G)return G;if(ue&&S.length===1)return S[0];throw new Error("Profile name is required when multiple profiles exist.")};try{if(c==="list"){let G=[`Profiles (${S.length}):`];for(let ue of S){let Ue=ue===k?" (current)":"";G.push(` ${ue}${Ue}`)}return p(G.join(`
@@ -195,7 +195,7 @@ ID: ${u.id}`)}}}else if(f==="connectors"){let c=Array.from(rt);$(c.length>0?c:vo
195
195
  `)||lt.length===0)return;if(Ve===null){kt(e);let u=lt.length-1;Xe(u),t(lt[u]);return}let c=Math.max(0,Ve-1);Xe(c),t(lt[c]);return}if(y.downArrow){if(e.includes(`
196
196
  `)||Ve===null)return;if(Ve>=lt.length-1){Xe(null),t(Xt);return}let c=Ve+1;Xe(c),t(lt[c])}}),{suggestions:z,suggestionIndex:Te,showSuggestions:se,argMode:f,argItems:I,argIndex:U,selectedConnectorIds:rt,selectedCapabilities:Re,deleteConfirmMode:ct,handleInputChange:Zt,handleSubmit:Qt}}import{useCallback as Wt,useRef as Ur,useState as jr}from"react";function ns({credentials:e,currentModel:t,modelProviders:o,onUploadSuccess:s,onUploadError:n}){let[i,r]=jr([]),a=Ur(0),l=Wt(h=>{if(!(!Array.isArray(o)||o.length===0)){for(let T of o)if(T.models.some(V=>V.id===h))return T.id;return o[0]?.id}},[o]),d=Wt(async h=>{if(!e){n?.(h.filename,"Not logged in");return}if(!Array.isArray(o)||o.length===0){n?.(h.filename,"Model providers not loaded yet. Please wait and try again.");return}let T=t?l(t):o[0]?.id;if(!T){n?.(h.filename,"No model provider available");return}let O=`file-${++a.current}`;r(V=>[...V,{id:O,filename:h.filename,mimeType:h.mimeType,status:"uploading"}]);try{let Z=await new F().uploadFile(e.accessToken,e.workspaceId,h,T);r(ae=>ae.map(Y=>Y.id===O?{...Y,status:"uploaded",fileId:Z.id}:Y)),s?.(h.filename)}catch(V){let Z=V instanceof Error?V.message:"Upload failed";r(ae=>ae.map(Y=>Y.id===O?{...Y,status:"error",error:Z}:Y)),n?.(h.filename,Z)}},[e,t,l,o,s,n]),b=Wt(h=>{r(T=>T.filter(O=>O.id!==h))},[]),x=Wt(()=>{r([])},[]),C=Wt(()=>i.filter(h=>h.status==="uploaded"&&h.fileId).map(h=>h.fileId),[i]),$=i.some(h=>h.status==="uploading"),m=i.length>0;return{pendingFiles:i,addFile:d,removeFile:b,clearFiles:x,getUploadedFileIds:C,hasUploadingFiles:$,hasFiles:m}}import{jsx as X,jsxs as de}from"react/jsx-runtime";var Br=[" ______ _ ____ _","| ____| | | | _ \\ | |","| |__ _ __ | |_ _ __ _ _ | | | | ___ ___| | __","| __| | '_ \\| __| '__| | | || | | |/ _ \\/ __| |/ /","| |____| | | | |_| | | |_| || |_| | __/\\__ \\ <","|______|_| |_|\\__|_| \\__, ||____/ \\___||___/_|\\_\\"," __/ |"," |___/"],Wr=[Wo,Wo,Bo,Bo,gt,gt,gt,gt],rs=Br.map((e,t)=>`${Wr[t]??gt}${e}${re}`).join(`
197
197
  `),Mr=`${gt}EntryDesk${re}`,Jr=500;function Gr(e){let t="",o=0;for(;o<e.length;){if(e.charCodeAt(o)===27&&e[o+1]==="["){for(o+=2;o<e.length;){let s=e.charCodeAt(o);if(s>=64&&s<=126){o+=1;break}o+=1}continue}t+=e[o],o+=1}return t}function Hr(e){let t=Gr(e);return Math.max(...t.split(`
198
- `).map(o=>o.length))}function Vr(e){let t=Hr(rs);return e>=t?rs:Mr}function is({agentId:e,model:t,message:o,chatId:s,chatTitle:n,connectorIds:i,enableSandbox:r=!0,enableWebSearch:a=!1,enableImageGeneration:l=!1,enableChart:d=!1,enableFileSystem:b=!0}){let x=process.stdout.columns??80,C=Vr(x),$=ss(null),m=jn(s,n),{state:h,setState:T,messages:O,setMessages:V,input:Z,setInput:ae,chatId:Y,setChatId:Se,currentChatTitle:j,setCurrentChatTitle:P,hasPersistedChat:E,setHasPersistedChat:R,credentials:W,setCredentials:_,currentResponse:he,setCurrentResponse:L,fullOutput:p,setFullOutput:ie,addSystemMessage:ke,startNewChat:q}=m,{credentials:A}=Bn(T);go(()=>{A&&!W&&_(A)},[A,W,_]);let{availableConnectors:te,availableModels:ee,availableAgents:me,modelProviders:ce,loading:M}=Wn(W),{currentModel:Q,setCurrentModel:z,currentAgentId:fe,setCurrentAgentId:Te,activeConnectorIds:B,setActiveConnectorIds:se}=Yn(t,e,i),D=ss(!1);go(()=>{if(!D.current){if(D.current=!0,!t&&!e){let k=ne.getDefaultModel();k&&z(k)}if(!i&&!e){let k=ne.getDefaultConnectorIds();k&&se(k.length>0?k:void 0)}}},[t,e,i,z,se]);let{currentAgentDetails:f}=zn(fe,W),{sandbox:w,setSandbox:I,webSearch:oe,setWebSearch:U,imageGeneration:Ce,setImageGeneration:rt,chart:it,setChart:Re,fileSystem:at,setFileSystem:$o,capabilityLabelMap:zt}=qn(r,a,l,d,b),{pendingFiles:We,addFile:wt,removeFile:Ye,clearFiles:Yt,getUploadedFileIds:lt,hasUploadingFiles:So,hasFiles:Ve}=ns({credentials:W,currentModel:Q,modelProviders:ce,onUploadSuccess:k=>{ke(`\u2713 Attached: ${k}`)},onUploadError:(k,N)=>{ke(`\u2717 Failed to upload ${k}: ${N}`)}}),{sendMessage:Xe}=Gn({chatId:Y,credentials:W,currentAgentId:fe,currentModel:Q,activeConnectorIds:B,sandbox:w,webSearch:oe,imageGeneration:Ce,chart:it,localTools:at,fullOutput:p,setMessages:V,setState:T,setCurrentResponse:L,setCredentials:_,setHasPersistedChat:R,getFileIds:lt,clearFiles:Yt}),Xt=Vo(k=>{q(k),L("")},[q]),[kt,mt]=Fr(!1),ct=Vo(async k=>{let N=k.text.trim(),J=uo(N);if(/^https?:\/\//.test(J))return;if(await Dn(N)&&Ln(J)){let ue=await On(N);ue&&(k.preventDefault(),await wt(ue))}},[wt]),xt=Vo(async()=>{if(!(kt||h.status!=="ready")){if(M||ce.length===0){ke("Please wait for resources to load before pasting images.");return}ke("Reading clipboard..."),mt(!0);try{let k=await _n();if(k){let N=`clipboard-${Date.now()}.png`;ke(`Uploading: ${N} (${Math.round(k.buffer.length/1024)}KB)`),await wt({buffer:k.buffer,filename:N,mimeType:k.mime})}else ke("No image found in clipboard. Copy an image first.")}catch(k){let N=k instanceof Error?k.message:"Failed to read clipboard";ke(`Clipboard error: ${N}`)}finally{mt(!1)}}},[wt,ke,kt,h.status,M,ce.length]),{suggestions:bt,suggestionIndex:Ct,showSuggestions:_e,argMode:ye,argItems:Ze,argIndex:$t,selectedConnectorIds:Zt,selectedCapabilities:_t,deleteConfirmMode:Qt,handleInputChange:v,handleSubmit:y}=os({input:Z,setInput:ae,chatId:Y,setChatId:Se,currentChatTitle:j,setCurrentChatTitle:P,currentModel:Q,setCurrentModel:z,currentAgentId:fe,setCurrentAgentId:Te,currentAgentDetails:f,setCurrentAgentDetails:()=>{},activeConnectorIds:B,setActiveConnectorIds:se,sandbox:w,setSandbox:I,webSearch:oe,setWebSearch:U,imageGeneration:Ce,setImageGeneration:rt,chart:it,setChart:Re,fileSystem:at,setFileSystem:$o,availableModels:ee,availableAgents:me,availableConnectors:te,messages:O,hasPersistedChat:E,setHasPersistedChat:R,setMessages:V,addSystemMessage:ke,startNewChat:Xt,sendMessage:Xe,setFullOutput:ie,credentials:W,setCredentials:_,setState:T,defaultCapabilities:{sandbox:r,webSearch:a,imageGeneration:l,chart:d,fileSystem:b},capabilityLabelMap:zt,hasUploadingFiles:So});if(Go((k,N)=>{(N.ctrl||N.meta)&&k==="v"&&xt()},{isActive:h.status==="ready"&&!ye}),Go((k,N)=>{if(!N.escape)return;let J=Date.now(),G=$.current;$.current=J,!(!G||J-G>Jr)&&(ye||_e||($.current=null,Ve&&Yt(),Z.length>0&&v("")))},{isActive:h.status==="ready"}),Go((k,N)=>{N.return&&(Xt("Started a new chat."),T({status:"ready"}))},{isActive:h.status==="error"}),go(()=>{if(W&&!M&&h.status==="loading")if(!Q&&!fe){let k=ne.getDefaultModel(),N=k&&ee.some(G=>G.id===k),J=N?k:ee[0]?.id;J?(N||ne.setDefaultModel(J),z(J),T({status:"ready"})):me.length===0?T({status:"no-model"}):T({status:"ready"})}else T({status:"ready"})},[W,M,Q,fe,ee,me,h.status,z,T]),go(()=>{o&&W&&h.status==="ready"&&Xe(o)},[W,o]),h.status==="loading")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),de(Pe,{children:[X(K,{color:"cyan",children:X(Ho,{type:"dots"})}),X(K,{children:" Initializing chat..."})]})]});if(h.status==="not-logged-in")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"Not logged in"}),X(K,{dimColor:!0,children:"Run `entrydesk login` to authenticate first."})]});if(h.status==="no-workspace")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"No workspace selected"}),X(K,{dimColor:!0,children:"Run `entrydesk workspaces` to select a workspace first."})]});if(h.status==="no-model")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"No model or agent specified"}),s&&X(K,{dimColor:!0,children:"Could not determine the model used in this chat."}),X(K,{dimColor:!0,children:"Please specify a model with --model or use an agent with -a."}),X(K,{dimColor:!0,children:"Example: entrydesk chat -c 1 -i --model gemini-2.5-flash-lite"})]});if(h.status==="error")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),de(K,{color:"red",children:["Error: ",h.message]}),X(K,{dimColor:!0,children:"Press Enter to start a new chat, or Ctrl+C to exit"})]});let c=fe?f?f.connectors&&f.connectors.length>0?f.connectors.map(k=>k.name).join(", "):"none":"loading...":B&&B.length>0?te.filter(k=>B.includes(k.id)).map(k=>k.name).join(", ")||`${B.length} connector(s)`:"none",u=[];if(fe)if(f?.capabilities)for(let k of f.capabilities)u.push(zt.get(k)??k);else u.push("loading...");else w&&u.push("Sandbox"),oe&&u.push("Web"),Ce&&u.push("Image"),it&&u.push("Chart");at&&u.push("FileSystem");let g=H.getCurrentProfileName(),S=g!=="default";return de(Pe,{flexDirection:"column",children:[de(Pe,{marginBottom:1,flexDirection:"column",children:[X(K,{children:C}),de(Pe,{children:[X(K,{bold:!0,color:"cyan",children:j?`Continuing: ${j}`:"EntryDesk CLI"}),de(K,{dimColor:!0,children:[" v","1.6.0"]}),S&&de(K,{color:"magenta",children:[" [",g,"]"]}),X(K,{dimColor:!0,children:" (type /help for commands)"})]})]}),O.map((k,N)=>{let J=k.parts&&p?ht(k.parts,!0):k.content,G=k.role==="user"?"green":k.role==="system"?"yellow":"blue",ue=k.role==="user"?"You":k.role==="system"?"System":"Assistant";return de(Pe,{flexDirection:"column",marginBottom:1,children:[de(K,{bold:!0,color:G,children:[ue,":"]}),X(Fo,{text:J})]},N)}),he&&de(Pe,{flexDirection:"column",marginBottom:1,children:[X(K,{bold:!0,color:"blue",children:"Assistant:"}),X(Fo,{text:he})]}),h.status==="ready"&&X(gn,{suggestions:bt,activeIndex:Ct,visible:_e}),h.status==="ready"&&ye&&X(mn,{items:Ze,activeIndex:$t,pageSize:ye==="agent"||ye==="skills"||ye==="model"?5:10,selectedIds:ye==="connectors"?Zt:ye==="capabilities"?_t:void 0,multiSelect:ye==="connectors"||ye==="capabilities",visible:!0,title:ye==="model"?"Select model:":ye==="agent"?"Select agent:":ye==="skills"?"Select skill:":ye==="connectors"?"Select connectors:":ye==="capabilities"?"Select capabilities:":"Select chat:"}),h.status==="sending"&&!he?de(Pe,{children:[X(K,{color:"cyan",children:X(Ho,{type:"dots"})}),X(K,{children:" Thinking..."})]}):h.status==="ready"&&!ye?de(Pe,{flexDirection:"column",children:[Qt&&X(Pe,{marginBottom:1,children:X(K,{color:"yellow",children:"\u26A0\uFE0F Delete this chat? (y/N) "})}),Ve&&!Qt&&de(Pe,{marginBottom:1,flexDirection:"row",gap:1,children:[X(K,{dimColor:!0,children:"Attachments:"}),We.map((k,N)=>de(K,{children:[k.status==="uploading"?de(K,{color:"yellow",children:[X(Ho,{type:"dots"})," ",k.filename]}):k.status==="error"?de(K,{color:"red",children:["\u2717 ",k.filename]}):de(K,{color:"green",children:["\u2713 ",k.filename]}),N<We.length-1&&X(K,{children:", "})]},k.id))]}),de(Pe,{children:[X(K,{color:"green",children:"> "}),X($n,{value:Z,onChange:v,onSubmit:y,onPaste:ct,leadingOffset:2,multiline:!0})]})]}):null,de(Pe,{marginTop:1,flexDirection:"column",children:[de(Pe,{gap:2,children:[de(K,{children:[X(K,{dimColor:!0,children:"Model: "}),X(K,{color:"magenta",children:fe?f?.model?.name||"loading...":ee.find(k=>k.id===Q)?.name||Q||"none"})]}),de(K,{children:[X(K,{dimColor:!0,children:"Connectors: "}),X(K,{color:"cyan",children:c})]}),de(K,{children:[X(K,{dimColor:!0,children:"Capabilities: "}),X(K,{color:"green",children:u.length>0?u.join(", "):"none"})]})]}),de(K,{dimColor:!0,children:["Esc Esc: Clear input & attachments | Ctrl+V: Paste image | Ctrl+O: Toggle full output ",p?"(ON)":"(OFF)"," | Ctrl+C: Exit"]})]})]})}import{jsx as Xr}from"react/jsx-runtime";async function qr(){let e=[];for await(let t of process.stdin)e.push(t);return Buffer.concat(e).toString("utf8").trim()}async function zr(e,t,o,s){if(s<1)return console.error("Chat number must be 1 or greater"),null;let{chats:n}=await e.getChats(t,o,20);if(n.length===0)return console.error("No chats found. Start a new chat first."),null;if(s>n.length)return console.error(`Chat number ${s} not found. You have ${n.length} recent chats.`),console.error("Use `entrydesk chats` to see available chats."),null;let i=n[s-1],r;if(!i.agentId){let{messages:a}=await e.getMessages(t,o,i.id,10);r=a.find(d=>d.role==="assistant"&&d.modelId)?.modelId}return{chat:i,modelId:r}}var It="\x1B[2m",Oe="\x1B[0m",Ko="\x1B[36m",us="\x1B[33m",yo="\x1B[32m",as="stream-json";function ls(e,t,o,s){e&&t&&(console.error(`Error: Cannot use both ${o} and ${s}`),process.exit(1))}function wo(e,t){let o=t===void 0?{event:e}:{event:e,data:t};process.stdout.write(`${JSON.stringify(o)}
198
+ `).map(o=>o.length))}function Vr(e){let t=Hr(rs);return e>=t?rs:Mr}function is({agentId:e,model:t,message:o,chatId:s,chatTitle:n,connectorIds:i,enableSandbox:r=!0,enableWebSearch:a=!1,enableImageGeneration:l=!1,enableChart:d=!1,enableFileSystem:b=!0}){let x=process.stdout.columns??80,C=Vr(x),$=ss(null),m=jn(s,n),{state:h,setState:T,messages:O,setMessages:V,input:Z,setInput:ae,chatId:Y,setChatId:Se,currentChatTitle:j,setCurrentChatTitle:P,hasPersistedChat:E,setHasPersistedChat:R,credentials:W,setCredentials:_,currentResponse:he,setCurrentResponse:L,fullOutput:p,setFullOutput:ie,addSystemMessage:ke,startNewChat:q}=m,{credentials:A}=Bn(T);go(()=>{A&&!W&&_(A)},[A,W,_]);let{availableConnectors:te,availableModels:ee,availableAgents:me,modelProviders:ce,loading:M}=Wn(W),{currentModel:Q,setCurrentModel:z,currentAgentId:fe,setCurrentAgentId:Te,activeConnectorIds:B,setActiveConnectorIds:se}=Yn(t,e,i),D=ss(!1);go(()=>{if(!D.current){if(D.current=!0,!t&&!e){let k=ne.getDefaultModel();k&&z(k)}if(!i&&!e){let k=ne.getDefaultConnectorIds();k&&se(k.length>0?k:void 0)}}},[t,e,i,z,se]);let{currentAgentDetails:f}=zn(fe,W),{sandbox:w,setSandbox:I,webSearch:oe,setWebSearch:U,imageGeneration:Ce,setImageGeneration:rt,chart:it,setChart:Re,fileSystem:at,setFileSystem:$o,capabilityLabelMap:zt}=qn(r,a,l,d,b),{pendingFiles:We,addFile:wt,removeFile:Ye,clearFiles:Yt,getUploadedFileIds:lt,hasUploadingFiles:So,hasFiles:Ve}=ns({credentials:W,currentModel:Q,modelProviders:ce,onUploadSuccess:k=>{ke(`\u2713 Attached: ${k}`)},onUploadError:(k,N)=>{ke(`\u2717 Failed to upload ${k}: ${N}`)}}),{sendMessage:Xe}=Gn({chatId:Y,credentials:W,currentAgentId:fe,currentModel:Q,activeConnectorIds:B,sandbox:w,webSearch:oe,imageGeneration:Ce,chart:it,localTools:at,fullOutput:p,setMessages:V,setState:T,setCurrentResponse:L,setCredentials:_,setHasPersistedChat:R,getFileIds:lt,clearFiles:Yt}),Xt=Vo(k=>{q(k),L("")},[q]),[kt,mt]=Fr(!1),ct=Vo(async k=>{let N=k.text.trim(),J=uo(N);if(/^https?:\/\//.test(J))return;if(await Dn(N)&&Ln(J)){let ue=await On(N);ue&&(k.preventDefault(),await wt(ue))}},[wt]),xt=Vo(async()=>{if(!(kt||h.status!=="ready")){if(M||ce.length===0){ke("Please wait for resources to load before pasting images.");return}ke("Reading clipboard..."),mt(!0);try{let k=await _n();if(k){let N=`clipboard-${Date.now()}.png`;ke(`Uploading: ${N} (${Math.round(k.buffer.length/1024)}KB)`),await wt({buffer:k.buffer,filename:N,mimeType:k.mime})}else ke("No image found in clipboard. Copy an image first.")}catch(k){let N=k instanceof Error?k.message:"Failed to read clipboard";ke(`Clipboard error: ${N}`)}finally{mt(!1)}}},[wt,ke,kt,h.status,M,ce.length]),{suggestions:bt,suggestionIndex:Ct,showSuggestions:_e,argMode:ye,argItems:Ze,argIndex:$t,selectedConnectorIds:Zt,selectedCapabilities:_t,deleteConfirmMode:Qt,handleInputChange:v,handleSubmit:y}=os({input:Z,setInput:ae,chatId:Y,setChatId:Se,currentChatTitle:j,setCurrentChatTitle:P,currentModel:Q,setCurrentModel:z,currentAgentId:fe,setCurrentAgentId:Te,currentAgentDetails:f,setCurrentAgentDetails:()=>{},activeConnectorIds:B,setActiveConnectorIds:se,sandbox:w,setSandbox:I,webSearch:oe,setWebSearch:U,imageGeneration:Ce,setImageGeneration:rt,chart:it,setChart:Re,fileSystem:at,setFileSystem:$o,availableModels:ee,availableAgents:me,availableConnectors:te,messages:O,hasPersistedChat:E,setHasPersistedChat:R,setMessages:V,addSystemMessage:ke,startNewChat:Xt,sendMessage:Xe,setFullOutput:ie,credentials:W,setCredentials:_,setState:T,defaultCapabilities:{sandbox:r,webSearch:a,imageGeneration:l,chart:d,fileSystem:b},capabilityLabelMap:zt,hasUploadingFiles:So});if(Go((k,N)=>{(N.ctrl||N.meta)&&k==="v"&&xt()},{isActive:h.status==="ready"&&!ye}),Go((k,N)=>{if(!N.escape)return;let J=Date.now(),G=$.current;$.current=J,!(!G||J-G>Jr)&&(ye||_e||($.current=null,Ve&&Yt(),Z.length>0&&v("")))},{isActive:h.status==="ready"}),Go((k,N)=>{N.return&&(Xt("Started a new chat."),T({status:"ready"}))},{isActive:h.status==="error"}),go(()=>{if(W&&!M&&h.status==="loading")if(!Q&&!fe){let k=ne.getDefaultModel(),N=k&&ee.some(G=>G.id===k),J=N?k:ee[0]?.id;J?(N||ne.setDefaultModel(J),z(J),T({status:"ready"})):me.length===0?T({status:"no-model"}):T({status:"ready"})}else T({status:"ready"})},[W,M,Q,fe,ee,me,h.status,z,T]),go(()=>{o&&W&&h.status==="ready"&&Xe(o)},[W,o]),h.status==="loading")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),de(Pe,{children:[X(K,{color:"cyan",children:X(Ho,{type:"dots"})}),X(K,{children:" Initializing chat..."})]})]});if(h.status==="not-logged-in")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"Not logged in"}),X(K,{dimColor:!0,children:"Run `entrydesk login` to authenticate first."})]});if(h.status==="no-workspace")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"No workspace selected"}),X(K,{dimColor:!0,children:"Run `entrydesk workspaces` to select a workspace first."})]});if(h.status==="no-model")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),X(K,{color:"yellow",children:"No model or agent specified"}),s&&X(K,{dimColor:!0,children:"Could not determine the model used in this chat."}),X(K,{dimColor:!0,children:"Please specify a model with --model or use an agent with -a."}),X(K,{dimColor:!0,children:"Example: entrydesk chat -c 1 -i --model gemini-2.5-flash-lite"})]});if(h.status==="error")return de(Pe,{flexDirection:"column",children:[X(K,{children:C}),de(K,{color:"red",children:["Error: ",h.message]}),X(K,{dimColor:!0,children:"Press Enter to start a new chat, or Ctrl+C to exit"})]});let c=fe?f?f.connectors&&f.connectors.length>0?f.connectors.map(k=>k.name).join(", "):"none":"loading...":B&&B.length>0?te.filter(k=>B.includes(k.id)).map(k=>k.name).join(", ")||`${B.length} connector(s)`:"none",u=[];if(fe)if(f?.capabilities)for(let k of f.capabilities)u.push(zt.get(k)??k);else u.push("loading...");else w&&u.push("Sandbox"),oe&&u.push("Web"),Ce&&u.push("Image"),it&&u.push("Chart");at&&u.push("FileSystem");let g=H.getCurrentProfileName(),S=g!=="default";return de(Pe,{flexDirection:"column",children:[de(Pe,{marginBottom:1,flexDirection:"column",children:[X(K,{children:C}),de(Pe,{children:[X(K,{bold:!0,color:"cyan",children:j?`Continuing: ${j}`:"EntryDesk CLI"}),de(K,{dimColor:!0,children:[" v","1.7.1"]}),S&&de(K,{color:"magenta",children:[" [",g,"]"]}),X(K,{dimColor:!0,children:" (type /help for commands)"})]})]}),O.map((k,N)=>{let J=k.parts&&p?ht(k.parts,!0):k.content,G=k.role==="user"?"green":k.role==="system"?"yellow":"blue",ue=k.role==="user"?"You":k.role==="system"?"System":"Assistant";return de(Pe,{flexDirection:"column",marginBottom:1,children:[de(K,{bold:!0,color:G,children:[ue,":"]}),X(Fo,{text:J})]},N)}),he&&de(Pe,{flexDirection:"column",marginBottom:1,children:[X(K,{bold:!0,color:"blue",children:"Assistant:"}),X(Fo,{text:he})]}),h.status==="ready"&&X(gn,{suggestions:bt,activeIndex:Ct,visible:_e}),h.status==="ready"&&ye&&X(mn,{items:Ze,activeIndex:$t,pageSize:ye==="agent"||ye==="skills"||ye==="model"?5:10,selectedIds:ye==="connectors"?Zt:ye==="capabilities"?_t:void 0,multiSelect:ye==="connectors"||ye==="capabilities",visible:!0,title:ye==="model"?"Select model:":ye==="agent"?"Select agent:":ye==="skills"?"Select skill:":ye==="connectors"?"Select connectors:":ye==="capabilities"?"Select capabilities:":"Select chat:"}),h.status==="sending"&&!he?de(Pe,{children:[X(K,{color:"cyan",children:X(Ho,{type:"dots"})}),X(K,{children:" Thinking..."})]}):h.status==="ready"&&!ye?de(Pe,{flexDirection:"column",children:[Qt&&X(Pe,{marginBottom:1,children:X(K,{color:"yellow",children:"\u26A0\uFE0F Delete this chat? (y/N) "})}),Ve&&!Qt&&de(Pe,{marginBottom:1,flexDirection:"row",gap:1,children:[X(K,{dimColor:!0,children:"Attachments:"}),We.map((k,N)=>de(K,{children:[k.status==="uploading"?de(K,{color:"yellow",children:[X(Ho,{type:"dots"})," ",k.filename]}):k.status==="error"?de(K,{color:"red",children:["\u2717 ",k.filename]}):de(K,{color:"green",children:["\u2713 ",k.filename]}),N<We.length-1&&X(K,{children:", "})]},k.id))]}),de(Pe,{children:[X(K,{color:"green",children:"> "}),X($n,{value:Z,onChange:v,onSubmit:y,onPaste:ct,leadingOffset:2,multiline:!0})]})]}):null,de(Pe,{marginTop:1,flexDirection:"column",children:[de(Pe,{gap:2,children:[de(K,{children:[X(K,{dimColor:!0,children:"Model: "}),X(K,{color:"magenta",children:fe?f?.model?.name||"loading...":ee.find(k=>k.id===Q)?.name||Q||"none"})]}),de(K,{children:[X(K,{dimColor:!0,children:"Connectors: "}),X(K,{color:"cyan",children:c})]}),de(K,{children:[X(K,{dimColor:!0,children:"Capabilities: "}),X(K,{color:"green",children:u.length>0?u.join(", "):"none"})]})]}),de(K,{dimColor:!0,children:["Esc Esc: Clear input & attachments | Ctrl+V: Paste image | Ctrl+O: Toggle full output ",p?"(ON)":"(OFF)"," | Ctrl+C: Exit"]})]})]})}import{jsx as Xr}from"react/jsx-runtime";async function qr(){let e=[];for await(let t of process.stdin)e.push(t);return Buffer.concat(e).toString("utf8").trim()}async function zr(e,t,o,s){if(s<1)return console.error("Chat number must be 1 or greater"),null;let{chats:n}=await e.getChats(t,o,20);if(n.length===0)return console.error("No chats found. Start a new chat first."),null;if(s>n.length)return console.error(`Chat number ${s} not found. You have ${n.length} recent chats.`),console.error("Use `entrydesk chats` to see available chats."),null;let i=n[s-1],r;if(!i.agentId){let{messages:a}=await e.getMessages(t,o,i.id,10);r=a.find(d=>d.role==="assistant"&&d.modelId)?.modelId}return{chat:i,modelId:r}}var It="\x1B[2m",Oe="\x1B[0m",Ko="\x1B[36m",us="\x1B[33m",yo="\x1B[32m",as="stream-json";function ls(e,t,o,s){e&&t&&(console.error(`Error: Cannot use both ${o} and ${s}`),process.exit(1))}function wo(e,t){let o=t===void 0?{event:e}:{event:e,data:t};process.stdout.write(`${JSON.stringify(o)}
199
199
  `)}function ko(e,t){if(t)return e;let o=e.split(`
200
200
  `);if(o.length<=20)return e;let s=o.slice(0,5),n=o.slice(-5),i=o.length-10;return[...s,`${It}... (${i} lines omitted, use --full-output to see all) ...${Oe}`,...n].join(`
201
201
  `)}function Yr(e){try{return JSON.stringify(e,null,2)}catch{return String(e)}}function cs(e,t){if(!e)return"(no result)";if(typeof e!="object")return ko(String(e),t);let o=e;if("structuredContent"in o&&o.structuredContent){let s=o.structuredContent,n=[];if(s.stdout&&typeof s.stdout=="string"&&s.stdout.trim()&&n.push(s.stdout.trim()),s.stderr&&typeof s.stderr=="string"&&s.stderr.trim()&&n.push(`${us}[stderr]${Oe} ${s.stderr.trim()}`),s.urls&&Array.isArray(s.urls)){let i=s.urls;for(let r of i)r.path&&r.url&&n.push(`${r.path}: ${r.url}`)}return n.length===0&&n.push(JSON.stringify(s,null,2)),ko(n.join(`
@@ -402,7 +402,7 @@ Local Development:
402
402
  For more information, visit https://entrydesk.com/docs/cli
403
403
  `)}import{render as li}from"ink";import{randomUUID as ei}from"crypto";import{chromium as ti}from"playwright";function oi(e){return e==="localhost"||e==="127.0.0.1"||e==="::1"||e==="0.0.0.0"}function ni(e){let t;try{t=new URL(e)}catch{throw new Error("Invalid hub URL. Please check ENTRYDESK_HUB_URL.")}if(t.protocol!=="https:"&&t.protocol!=="http:")throw new Error("Hub URL must use http or https.");if(t.username||t.password)throw new Error("Hub URL must not include credentials.");if(!t.hostname)throw new Error("Hub URL must include a hostname.");if(t.protocol==="http:"&&!oi(t.hostname))throw new Error("Hub URL must use https unless running on localhost.");return t}function si(e){try{let t=e.split(".");if(t.length<2)return null;let o=t[1].replace(/-/g,"+").replace(/_/g,"/"),s=(4-o.length%4)%4,n=o.padEnd(o.length+s,"="),i=Buffer.from(n,"base64").toString("utf8");return JSON.parse(i).exp??null}catch{return null}}async function Zo(e){let{hubUrl:t,auth:o}=Me(),s=ni(t),n=new URL("/login",s),i=ei();n.searchParams.set("cli","true"),n.searchParams.set("state",i);let r;try{r=await ti.launch({headless:!1,args:["--disable-blink-features=AutomationControlled"]})}catch(a){throw a instanceof Error&&a.message.includes("Executable")?new Error(`Chromium browser is not installed. Please run:
404
404
  npm install -g playwright
405
- npx playwright install chromium --with-deps`,{cause:a}):a}try{let l=await(await r.newContext({viewport:{width:1280,height:800},ignoreHTTPSErrors:o.ignoreSslErrors})).newPage();process.env.DEBUG&&l.on("console",m=>{console.log(`[Browser Console] ${m.type()}: ${m.text()}`)}),e?.onBrowserOpen?.(),await l.goto(n.toString(),{waitUntil:"networkidle"}),e?.onWaitingForLogin?.();let d=s.host;await l.waitForURL(m=>{let h=new URL(m);if(h.host!==d)return!1;let T=h.pathname;if(T==="/login"||T.startsWith("/auth/"))return!1;let O=h.searchParams.get("state");return!(O&&O!==i)},{timeout:o.loginTimeout});let b=new URL("/api/auth/session",s).toString();process.env.DEBUG&&(console.log(`[DEBUG] Fetching session from: ${b}`),console.log(`[DEBUG] Current page URL: ${l.url()}`),console.log(`[DEBUG] SSL ignore enabled: ${o.ignoreSslErrors}`));let x=await l.evaluate(async m=>{try{let h=await fetch(m);if(!h.ok)throw new Error(`Failed to fetch session: ${h.status} ${h.statusText}`);return h.json()}catch(h){throw console.error("[Browser Context] Fetch error:",h),h}},b);if(!x?.accessToken)throw new Error("Failed to get access token from session");let C=si(x.accessToken);if(C&&C*1e3<=Date.now())throw new Error("Access token is expired. Please log in again.");let $=x.user?.email||"";return e?.onLoginSuccess?.($||"unknown"),{accessToken:x.accessToken,email:$,workspaceId:x.currentWorkspaceId}}catch(a){let l;throw a instanceof Error?(l=a,l.message.includes("fetch failed")&&(l=new Error(`Network request failed. This might be due to SSL certificate issues. Current SSL ignore setting: ${o.ignoreSslErrors}. Original error: ${l.message}`))):l=new Error("Browser authentication failed."),e?.onError?.(l),l}finally{await r.close()}}import{Box as Ee,Text as Le,useApp as ri,useInput as ii}from"ink";import ai from"ink-spinner";import bs from"ink-text-input";import{useState as Jt}from"react";import{jsx as ge,jsxs as Ge}from"react/jsx-runtime";function Cs(){let{exit:e}=ri(),[t,o]=Jt("email"),[s,n]=Jt(""),[i,r]=Jt(""),[a,l]=Jt(null),[d,b]=Jt(null);return ii(($,m)=>{m.escape&&e()}),Ge(Ee,{flexDirection:"column",padding:1,children:[ge(Ee,{marginBottom:1,children:ge(Le,{bold:!0,color:"cyan",children:"EntryDesk Login"})}),t==="email"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{children:"Email: "}),ge(bs,{value:s,onChange:n,onSubmit:$=>{if(!$.includes("@")){l("Please enter a valid email address");return}n($),l(null),o("password")},placeholder:"user@example.com"})]}),a&&ge(Ee,{marginTop:1,children:ge(Le,{color:"red",children:a})}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Press Enter to continue, Esc to cancel"})})]}),t==="password"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{children:"Email: "}),ge(Le,{color:"green",children:s})]}),Ge(Ee,{children:[ge(Le,{children:"Password: "}),ge(bs,{value:i,onChange:r,onSubmit:async $=>{r($),o("loading"),l(null);try{let m=new F,{token:h,refreshToken:T}=await m.login(s,$),{workspaces:O}=await m.getWorkspaces(h),V=O[0];await $e.saveAll({accessToken:h,refreshToken:T,email:s,workspaceId:V?.id}),b(V?.name??null),o("success"),setTimeout(()=>{e()},1500)}catch(m){o("error"),m instanceof Error?m.message.includes("403")||m.message.includes("Invalid")?l("Invalid email or password"):l(m.message):l("Login failed. Please try again.")}},mask:"*",placeholder:"Enter password"})]}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Press Enter to login, Esc to cancel"})})]}),t==="loading"&&Ge(Ee,{children:[ge(Le,{color:"cyan",children:ge(ai,{type:"dots"})}),ge(Le,{children:" Logging in..."})]}),t==="success"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{color:"green",children:"\u2713"}),ge(Le,{children:" Logged in as "}),ge(Le,{bold:!0,children:s})]}),d&&ge(Ee,{children:Ge(Le,{dimColor:!0,children:["Workspace: ",d]})})]}),t==="error"&&Ge(Ee,{flexDirection:"column",children:[ge(Ee,{children:ge(Le,{color:"red",children:"\u2717 Login failed"})}),a&&ge(Ee,{children:ge(Le,{color:"red",children:a})}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Run `entrydesk login` to try again"})})]})]})}import{jsx as ci}from"react/jsx-runtime";async function $s(e){if(!!(e.email||e.password)){(!e.email||!e.password)&&(console.error("Usage: entrydesk login --email <email> --password <password>"),console.error(" entrydesk login"),console.error(" entrydesk login --google"),console.error(" entrydesk login -i (interactive mode)"),process.exit(1));let a=new F,{token:l,refreshToken:d}=await a.login(e.email,e.password),{workspaces:b}=await a.getWorkspaces(l),x=b[0];await $e.saveAll({accessToken:l,refreshToken:d,email:e.email,workspaceId:x?.id}),e.json?console.log(JSON.stringify({email:e.email,workspaceId:x?.id,workspaceName:x?.name})):(console.log(`Logged in as ${e.email}`),x&&console.log(`Workspace: ${x.name}`));return}if(e.google){console.log("Opening browser for Google OAuth...");let a=await Zo(),l=new F,{workspaces:d}=await l.getWorkspaces(a.accessToken),b=d[0],x=a.workspaceId||b?.id;await $e.saveAll({accessToken:a.accessToken,refreshToken:"",email:a.email,workspaceId:x}),e.json?console.log(JSON.stringify({email:a.email,workspaceId:x,workspaceName:b?.name})):(console.log(`Logged in as ${a.email}`),b&&console.log(`Workspace: ${b.name}`));return}if(e.interactive){li(ci(Cs,{}));return}if(console.log("Opening browser for login..."),process.env.DEBUG){let{getApiUrl:a,getHubUrl:l}=await import("./config-LX73PV6D.js");console.log(`[DEBUG] Hub URL: ${l()}`),console.log(`[DEBUG] API URL: ${a()}`)}let o=await Zo(),s=new F,{workspaces:n}=await s.getWorkspaces(o.accessToken),i=n[0],r=o.workspaceId||i?.id;await $e.saveAll({accessToken:o.accessToken,refreshToken:"",email:o.email,workspaceId:r}),e.json?console.log(JSON.stringify({email:o.email,workspaceId:r,workspaceName:i?.name})):(console.log(`Logged in as ${o.email}`),i&&console.log(`Workspace: ${i.name}`))}async function Ss(e){try{let t=await $e.getAll();if(!t){console.log("Not logged in");return}try{await new F().revokeToken(t.refreshToken)}catch{}await $e.clear(),console.log("Logged out successfully")}catch(t){xe(t)}}import ui from"http";import{Client as di}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as fi}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{Server as pi}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as mi}from"@modelcontextprotocol/sdk/server/stdio.js";import{StreamableHTTPServerTransport as gi}from"@modelcontextprotocol/sdk/server/streamableHttp.js";import{CallToolRequestSchema as hi,ListToolsRequestSchema as yi}from"@modelcontextprotocol/sdk/types.js";async function wi(e,t,o){let s=new di({name:"entrydesk-cli-proxy",version:"1.0.0"}),n=new URL(`${e}/v1/workspaces/${o}/mcp`),i=new fi(n,{requestInit:{headers:{Authorization:`Bearer ${t}`}}});return await s.connect(i),s}function Ts(e){let t=new pi({name:"entrydesk-cli",version:"1.0.0"},{capabilities:{tools:{}}});return t.setRequestHandler(yi,async()=>({tools:(await e.listTools()).tools})),t.setRequestHandler(hi,async o=>{let{name:s,arguments:n}=o.params,i=await e.callTool({name:s,arguments:n});return{content:i.content,isError:i.isError}}),t}async function ki(e,t){let o=ui.createServer(async(s,n)=>{if(s.method==="OPTIONS"){n.setHeader("Access-Control-Allow-Origin","*"),n.setHeader("Access-Control-Allow-Methods","GET, POST, DELETE, OPTIONS"),n.setHeader("Access-Control-Allow-Headers","Content-Type, Accept"),n.writeHead(204),n.end();return}if(s.url!=="/mcp"){n.writeHead(404,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:"Not found"}));return}let i=Ts(e),r=new gi({sessionIdGenerator:void 0});if(n.setHeader("Access-Control-Allow-Origin","*"),n.on("close",()=>{r.close(),i.close()}),await i.connect(r),s.method==="POST"){let a="";s.on("data",l=>{a+=l.toString()}),s.on("end",async()=>{try{let l=JSON.parse(a);await r.handleRequest(s,n,l)}catch{n.writeHead(400,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:"Invalid JSON"}))}})}else await r.handleRequest(s,n)});o.listen(t,()=>{console.error(`EntryDesk MCP server running at http://localhost:${t}/mcp`),console.error("Press Ctrl+C to stop")}),process.on("SIGINT",async()=>{console.error(`
405
+ npx playwright install chromium --with-deps`,{cause:a}):a}try{let l=await(await r.newContext({viewport:{width:1280,height:800},ignoreHTTPSErrors:o.ignoreSslErrors})).newPage();process.env.DEBUG&&l.on("console",m=>{console.log(`[Browser Console] ${m.type()}: ${m.text()}`)}),e?.onBrowserOpen?.(),await l.goto(n.toString(),{waitUntil:"networkidle"}),e?.onWaitingForLogin?.();let d=s.host;await l.waitForURL(m=>{let h=new URL(m);if(h.host!==d)return!1;let T=h.pathname;if(T==="/login"||T.startsWith("/auth/"))return!1;let O=h.searchParams.get("state");return!(O&&O!==i)},{timeout:o.loginTimeout});let b=new URL("/api/auth/session",s).toString();process.env.DEBUG&&(console.log(`[DEBUG] Fetching session from: ${b}`),console.log(`[DEBUG] Current page URL: ${l.url()}`),console.log(`[DEBUG] SSL ignore enabled: ${o.ignoreSslErrors}`));let x=await l.evaluate(async m=>{try{let h=await fetch(m);if(!h.ok)throw new Error(`Failed to fetch session: ${h.status} ${h.statusText}`);return h.json()}catch(h){throw console.error("[Browser Context] Fetch error:",h),h}},b);if(!x?.accessToken)throw new Error("Failed to get access token from session");let C=si(x.accessToken);if(C&&C*1e3<=Date.now())throw new Error("Access token is expired. Please log in again.");let $=x.user?.email||"";return e?.onLoginSuccess?.($||"unknown"),{accessToken:x.accessToken,email:$,workspaceId:x.currentWorkspaceId}}catch(a){let l;throw a instanceof Error?(l=a,l.message.includes("fetch failed")&&(l=new Error(`Network request failed. This might be due to SSL certificate issues. Current SSL ignore setting: ${o.ignoreSslErrors}. Original error: ${l.message}`))):l=new Error("Browser authentication failed."),e?.onError?.(l),l}finally{await r.close()}}import{Box as Ee,Text as Le,useApp as ri,useInput as ii}from"ink";import ai from"ink-spinner";import bs from"ink-text-input";import{useState as Jt}from"react";import{jsx as ge,jsxs as Ge}from"react/jsx-runtime";function Cs(){let{exit:e}=ri(),[t,o]=Jt("email"),[s,n]=Jt(""),[i,r]=Jt(""),[a,l]=Jt(null),[d,b]=Jt(null);return ii(($,m)=>{m.escape&&e()}),Ge(Ee,{flexDirection:"column",padding:1,children:[ge(Ee,{marginBottom:1,children:ge(Le,{bold:!0,color:"cyan",children:"EntryDesk Login"})}),t==="email"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{children:"Email: "}),ge(bs,{value:s,onChange:n,onSubmit:$=>{if(!$.includes("@")){l("Please enter a valid email address");return}n($),l(null),o("password")},placeholder:"user@example.com"})]}),a&&ge(Ee,{marginTop:1,children:ge(Le,{color:"red",children:a})}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Press Enter to continue, Esc to cancel"})})]}),t==="password"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{children:"Email: "}),ge(Le,{color:"green",children:s})]}),Ge(Ee,{children:[ge(Le,{children:"Password: "}),ge(bs,{value:i,onChange:r,onSubmit:async $=>{r($),o("loading"),l(null);try{let m=new F,{token:h,refreshToken:T}=await m.login(s,$),{workspaces:O}=await m.getWorkspaces(h),V=O[0];await $e.saveAll({accessToken:h,refreshToken:T,email:s,workspaceId:V?.id}),b(V?.name??null),o("success"),setTimeout(()=>{e()},1500)}catch(m){o("error"),m instanceof Error?m.message.includes("403")||m.message.includes("Invalid")?l("Invalid email or password"):l(m.message):l("Login failed. Please try again.")}},mask:"*",placeholder:"Enter password"})]}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Press Enter to login, Esc to cancel"})})]}),t==="loading"&&Ge(Ee,{children:[ge(Le,{color:"cyan",children:ge(ai,{type:"dots"})}),ge(Le,{children:" Logging in..."})]}),t==="success"&&Ge(Ee,{flexDirection:"column",children:[Ge(Ee,{children:[ge(Le,{color:"green",children:"\u2713"}),ge(Le,{children:" Logged in as "}),ge(Le,{bold:!0,children:s})]}),d&&ge(Ee,{children:Ge(Le,{dimColor:!0,children:["Workspace: ",d]})})]}),t==="error"&&Ge(Ee,{flexDirection:"column",children:[ge(Ee,{children:ge(Le,{color:"red",children:"\u2717 Login failed"})}),a&&ge(Ee,{children:ge(Le,{color:"red",children:a})}),ge(Ee,{marginTop:1,children:ge(Le,{dimColor:!0,children:"Run `entrydesk login` to try again"})})]})]})}import{jsx as ci}from"react/jsx-runtime";async function $s(e){if(!!(e.email||e.password)){(!e.email||!e.password)&&(console.error("Usage: entrydesk login --email <email> --password <password>"),console.error(" entrydesk login"),console.error(" entrydesk login --google"),console.error(" entrydesk login -i (interactive mode)"),process.exit(1));let a=new F,{token:l,refreshToken:d}=await a.login(e.email,e.password),{workspaces:b}=await a.getWorkspaces(l),x=b[0];await $e.saveAll({accessToken:l,refreshToken:d,email:e.email,workspaceId:x?.id}),e.json?console.log(JSON.stringify({email:e.email,workspaceId:x?.id,workspaceName:x?.name})):(console.log(`Logged in as ${e.email}`),x&&console.log(`Workspace: ${x.name}`));return}if(e.google){console.log("Opening browser for Google OAuth...");let a=await Zo(),l=new F,{workspaces:d}=await l.getWorkspaces(a.accessToken),b=d[0],x=a.workspaceId||b?.id;await $e.saveAll({accessToken:a.accessToken,refreshToken:"",email:a.email,workspaceId:x}),e.json?console.log(JSON.stringify({email:a.email,workspaceId:x,workspaceName:b?.name})):(console.log(`Logged in as ${a.email}`),b&&console.log(`Workspace: ${b.name}`));return}if(e.interactive){li(ci(Cs,{}));return}if(console.log("Opening browser for login..."),process.env.DEBUG){let{getApiUrl:a,getHubUrl:l}=await import("./config-JEOPVND4.js");console.log(`[DEBUG] Hub URL: ${l()}`),console.log(`[DEBUG] API URL: ${a()}`)}let o=await Zo(),s=new F,{workspaces:n}=await s.getWorkspaces(o.accessToken),i=n[0],r=o.workspaceId||i?.id;await $e.saveAll({accessToken:o.accessToken,refreshToken:"",email:o.email,workspaceId:r}),e.json?console.log(JSON.stringify({email:o.email,workspaceId:r,workspaceName:i?.name})):(console.log(`Logged in as ${o.email}`),i&&console.log(`Workspace: ${i.name}`))}async function Ss(e){try{let t=await $e.getAll();if(!t){console.log("Not logged in");return}try{await new F().revokeToken(t.refreshToken)}catch{}await $e.clear(),console.log("Logged out successfully")}catch(t){xe(t)}}import ui from"http";import{Client as di}from"@modelcontextprotocol/sdk/client/index.js";import{StreamableHTTPClientTransport as fi}from"@modelcontextprotocol/sdk/client/streamableHttp.js";import{Server as pi}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as mi}from"@modelcontextprotocol/sdk/server/stdio.js";import{StreamableHTTPServerTransport as gi}from"@modelcontextprotocol/sdk/server/streamableHttp.js";import{CallToolRequestSchema as hi,ListToolsRequestSchema as yi}from"@modelcontextprotocol/sdk/types.js";async function wi(e,t,o){let s=new di({name:"entrydesk-cli-proxy",version:"1.0.0"}),n=new URL(`${e}/v1/workspaces/${o}/mcp`),i=new fi(n,{requestInit:{headers:{Authorization:`Bearer ${t}`}}});return await s.connect(i),s}function Ts(e){let t=new pi({name:"entrydesk-cli",version:"1.0.0"},{capabilities:{tools:{}}});return t.setRequestHandler(yi,async()=>({tools:(await e.listTools()).tools})),t.setRequestHandler(hi,async o=>{let{name:s,arguments:n}=o.params,i=await e.callTool({name:s,arguments:n});return{content:i.content,isError:i.isError}}),t}async function ki(e,t){let o=ui.createServer(async(s,n)=>{if(s.method==="OPTIONS"){n.setHeader("Access-Control-Allow-Origin","*"),n.setHeader("Access-Control-Allow-Methods","GET, POST, DELETE, OPTIONS"),n.setHeader("Access-Control-Allow-Headers","Content-Type, Accept"),n.writeHead(204),n.end();return}if(s.url!=="/mcp"){n.writeHead(404,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:"Not found"}));return}let i=Ts(e),r=new gi({sessionIdGenerator:void 0});if(n.setHeader("Access-Control-Allow-Origin","*"),n.on("close",()=>{r.close(),i.close()}),await i.connect(r),s.method==="POST"){let a="";s.on("data",l=>{a+=l.toString()}),s.on("end",async()=>{try{let l=JSON.parse(a);await r.handleRequest(s,n,l)}catch{n.writeHead(400,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:"Invalid JSON"}))}})}else await r.handleRequest(s,n)});o.listen(t,()=>{console.error(`EntryDesk MCP server running at http://localhost:${t}/mcp`),console.error("Press Ctrl+C to stop")}),process.on("SIGINT",async()=>{console.error(`
406
406
  Shutting down...`),o.close(),await e.close(),process.exit(0)}),process.on("SIGTERM",async()=>{o.close(),await e.close(),process.exit(0)})}async function xi(e){let t=Ts(e),o=new mi;o.onclose=async()=>{await e.close()},await t.connect(o)}async function Is(e={mode:"stdio"}){process.on("unhandledRejection",t=>{console.error("[entrydesk-mcp] Unhandled rejection:",t)}),process.on("uncaughtException",t=>{console.error("[entrydesk-mcp] Uncaught exception:",t)});try{console.error("[entrydesk-mcp] Starting MCP server...");let t,o;try{let r=await pe();t=r.accessToken,o=r.workspaceId}catch(r){let a=r instanceof Error?r.message:"Authentication failed";console.error(a),process.exit(1)}let{apiUrl:s,mcp:n}=Me();console.error(`[entrydesk-mcp] Connecting to ${s}...`),console.error(`[entrydesk-mcp] Workspace: ${o}`);let i=await wi(s,t,o);if(console.error("[entrydesk-mcp] Connected to EntryDesk API"),e.mode==="http"){let r=e.port||n.httpPort;await ki(i,r)}else console.error("[entrydesk-mcp] Starting stdio server..."),await xi(i),console.error("[entrydesk-mcp] Stdio server started")}catch(t){console.error("[entrydesk-mcp] Fatal error:",t),process.exit(1)}}async function vs(e){try{let t,o;try{let i=await pe();t=i.accessToken,o=i.workspaceId}catch(i){let r=i instanceof Error?i.message:"Authentication failed";console.error(r),process.exit(1)}let s=new F,{models:n}=await s.getModels(t,o);if(e.json){console.log(JSON.stringify(n));return}if(n.length===0){console.log("No models available in this workspace.");return}console.log(`Available Models (${n.length}):
407
407
  `);for(let i of n)console.log(` ${i.name}`),i.description&&console.log(` ${i.description.slice(0,80)}`),console.log(` ID: ${i.id}`),console.log("")}catch(t){xe(t)}}function bi(){console.log(`
408
408
  Usage: entrydesk profile [list|show|current|create|use|delete|rename|clone] [args]
@@ -526,7 +526,7 @@ Examples:
526
526
  `)}async function Ms(e){if(e.help){Yi();return}let t,o;try{let m=await pe();t=m.accessToken,o=m.workspaceId}catch(m){let h=m instanceof Error?m.message:"Authentication failed";console.error(h),process.exit(1)}let s=new F,n=[],i,r=0,a=100;do{let m=await s.getMonthlyUsages(t,o,{nextKey:i,limit:100});if(n.push(...m.monthlyUsages),i=m.nextKey,r++,r>=a){console.error(`Warning: Showing first ${n.length} entries (pagination limit reached)`);break}}while(i);if(e.json){console.log(JSON.stringify(n));return}if(n.length===0){console.log("No usage data found for this workspace.");return}console.log(`Current Month Usage:
527
527
  `);let l=0,d=0;for(let m of n)l+=parseFloat(m.totalCost),d+=m.operationCount;console.log(`Total Cost: $${l.toFixed(2)}`),console.log(`Total Operations: ${d.toLocaleString()}`),console.log(`Users: ${n.length}
528
528
  `);let b=e.sortBy||"cost",x=[...n].toSorted((m,h)=>b==="cost"?parseFloat(h.totalCost)-parseFloat(m.totalCost):h.operationCount-m.operationCount),C=e.limit&&e.limit>0?e.limit:x.length,$=x.slice(0,C);console.log(`Usage by User (sorted by ${b}, showing ${$.length} of ${n.length}):
529
- `);for(let m of $){let h=parseFloat(m.totalCost);console.log(` ${m.user.name} (${m.user.email})`),console.log(` Cost: $${h.toFixed(2)}`),console.log(` Operations: ${m.operationCount.toLocaleString()}`),console.log("")}$.length<n.length&&console.log(`... and ${n.length-$.length} more users`)}function on(){console.log("entrydesk 1.6.0")}function nn(){console.log(`
529
+ `);for(let m of $){let h=parseFloat(m.totalCost);console.log(` ${m.user.name} (${m.user.email})`),console.log(` Cost: $${h.toFixed(2)}`),console.log(` Operations: ${m.operationCount.toLocaleString()}`),console.log("")}$.length<n.length&&console.log(`... and ${n.length-$.length} more users`)}function on(){console.log("entrydesk 1.7.1")}function nn(){console.log(`
530
530
  Usage: entrydesk workspaces [list|switch|use] [options]
531
531
 
532
532
  Commands:
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import{a}from"./chunk-G36L33PZ.js";export{a as TokenStorage};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@entrydesk/cli",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "EntryDesk CLI - Interact with EntryDesk from your terminal",
5
5
  "type": "module",
6
6
  "bin": {
@@ -11,7 +11,7 @@
11
11
  "README.md"
12
12
  ],
13
13
  "dependencies": {
14
- "@modelcontextprotocol/sdk": "1.24.2",
14
+ "@modelcontextprotocol/sdk": "1.25.2",
15
15
  "archiver": "^7.0.1",
16
16
  "glob": "^11.0.0",
17
17
  "ink": "^6.5.1",
@@ -20,8 +20,7 @@
20
20
  "ky": "^1.14.1",
21
21
  "playwright": "^1.53.0",
22
22
  "react": "^19.2.3",
23
- "uuid": "^13.0.0",
24
- "zod": "^3.25.76"
23
+ "uuid": "^13.0.0"
25
24
  },
26
25
  "devDependencies": {
27
26
  "@lydell/node-pty": "^1.1.0",
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import R from"crypto";import f from"fs";import ue from"path";import u from"fs";import ae from"os";import S from"path";import{HTTPError as ie,TimeoutError as oe}from"ky";import te,{TimeoutError as re}from"ky";import E from"fs";function V(r){let e=o.getEffectiveProfileName(r);return o.getProfileConfigFile(e)}function a(r){let e=V(r);try{if(!E.existsSync(e))return{};let t=E.readFileSync(e,"utf-8");return JSON.parse(t)}catch{return{}}}function d(r,e){let t=o.getEffectiveProfileName(e);o.ensureProfileDir(t);let s=o.getProfileConfigFile(t);E.writeFileSync(s,JSON.stringify(r,null,2),{mode:384});try{E.chmodSync(s,384)}catch{}}var F={getApiUrl(r){return a(r).apiUrl??null},setApiUrl(r,e){let t=a(e);t.apiUrl=r,d(t,e)},clearApiUrl(r){let e=a(r);delete e.apiUrl,d(e,r)},getHubUrl(r){return a(r).hubUrl??null},setHubUrl(r,e){let t=a(e);t.hubUrl=r,d(t,e)},getAll(r){return a(r)},setAll(r,e){d(r,e)},set(r,e,t){let s=a(t);s[r]=e,d(s,t)},get(r,e){return a(e)[r]},unset(r,e){let t=a(e);delete t[r],d(t,e)},getDefaultModel(r){return a(r).model?.default??null},setDefaultModel(r,e){let t=a(e);t.model={...t.model,default:r},d(t,e)},getDefaultConnectorIds(r){return a(r).connectors?.defaults??null},setDefaultConnectorIds(r,e){let t=a(e);t.connectors={...t.connectors,defaults:r},d(t,e)},getCapabilities(r){return a(r).capabilities??null},setCapabilities(r,e){let t=a(e);t.capabilities={...t.capabilities,...r},d(t,e)},getLocalTools(r){return a(r).localTools??null},setLocalTools(r,e){let t=a(e);t.localTools={...t.localTools,...r},d(t,e)}};var X="https://entrydesk-api.dcard.io",Z="https://entrydesk.dcard.io",Q=3100,ee=300*1e3;function T(r){return{apiUrl:F.getApiUrl(r)||process.env.ENTRYDESK_API_URL||X,hubUrl:F.getHubUrl(r)||process.env.ENTRYDESK_HUB_URL||Z,mcp:{httpPort:Q},auth:{loginTimeout:ee,ignoreSslErrors:process.env.ENTRYDESK_IGNORE_SSL_ERRORS==="true"||!1}}}function z(r){return T(r).apiUrl}function Se(r){return T(r).hubUrl}function Ce(){return T().mcp}function O(){return T().auth}var N="ENTRYDESK_ALLOW_INSECURE_LOGIN",$=class r{client;baseUrl;constructor(e,t){this.baseUrl=e||z(t),O().ignoreSslErrors&&(process.env.NODE_TLS_REJECT_UNAUTHORIZED="0",process.env.DEBUG&&console.log("[DEBUG] ApiClient: SSL certificate validation disabled")),this.client=te.create({prefixUrl:this.baseUrl,timeout:3e4,retry:{limit:3,methods:["get","put","head","delete","options","trace"],statusCodes:[408,413,429,500,502,503,504]},hooks:{beforeError:[n=>(n.request?.headers&&(n.request.headers.delete("authorization"),n.request.headers.delete("Authorization")),n)]}})}getBaseUrl(){return this.baseUrl}shouldAllowInsecureLogin(){return process.env[N]?.toLowerCase()==="true"}assertSecureLogin(){let e;try{e=new URL(this.baseUrl)}catch(t){let s=t instanceof Error?t:void 0;throw new c("Invalid API URL; cannot perform login.",1,s)}if(e.protocol!=="https:"&&!this.shouldAllowInsecureLogin())throw new c(`Login requires HTTPS. Set apiUrl to https:// or set ${N}=true for local development.`)}async login(e,t){return this.assertSecureLogin(),await this.client.post("v1/auth/credentials/token",{json:{email:e,password:t}}).json()}async refreshToken(e){return await this.client.post("v1/auth/token/refresh",{json:{refreshToken:e}}).json()}async revokeToken(e){await this.client.post("v1/auth/token/revoke",{json:{refreshToken:e}})}async getWorkspaces(e){try{return process.env.DEBUG&&console.log(`[DEBUG] Calling getWorkspaces API: ${this.baseUrl}/v1/workspaces`),await this.client.get("v1/workspaces",{headers:{Authorization:`Bearer ${e}`}}).json()}catch(t){throw process.env.DEBUG&&(console.error("[DEBUG] getWorkspaces error:",t),t instanceof Error&&"cause"in t&&console.error("[DEBUG] Error cause:",t.cause)),t}}async getAgents(e,t){return await this.client.get(`v1/workspaces/${t}/availableAgents`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:100}}).json()}async createAgent(e,t,s){return await this.client.post(`v1/workspaces/${t}/agents`,{headers:{Authorization:`Bearer ${e}`},json:s}).json()}async updateAgent(e,t,s,n){await this.client.put(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`},json:n})}async deleteAgent(e,t,s){await this.client.delete(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async getModels(e,t){return await this.client.get(`v1/workspaces/${t}/models`,{headers:{Authorization:`Bearer ${e}`}}).json()}async getConnectors(e,t){return await this.client.get(`v1/workspaces/${t}/availableConnectors`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:100,connectionStatus:"connected"}}).json()}async getChats(e,t,s=20){return await this.client.get(`v1/workspaces/${t}/chats`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:s}}).json()}async getMessages(e,t,s,n=50){return await this.client.get(`v1/workspaces/${t}/chats/${s}/messages`,{headers:{Authorization:`Bearer ${e}`},searchParams:{limit:n}}).json()}async getAgent(e,t,s){return await this.client.get(`v1/workspaces/${t}/agents/${s}`,{headers:{Authorization:`Bearer ${e}`}}).json()}async listAvailableSkills(e,t,s){return await this.client.get(`v1/workspaces/${t}/availableSkills`,{headers:{Authorization:`Bearer ${e}`},searchParams:{createdBy:s?.createdBy,nextKey:s?.nextKey,limit:s?.limit??20}}).json()}async uploadSkill(e,t,s){return await this.client.post(`v1/workspaces/${t}/skills`,{headers:{Authorization:`Bearer ${e}`},body:s}).json()}async deleteSkill(e,t,s){await this.client.delete(`v1/workspaces/${t}/skills/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async chat(e,t,s){try{let n=await this.client.post(`v1/workspaces/${t}/chats`,{headers:{Authorization:`Bearer ${e}`},json:s,timeout:6e4,throwHttpErrors:!1});if(!n.ok){let i=await n.text();throw new Error(`Chat API failed (${n.status}): ${i}`)}return n}catch(n){throw n instanceof re?new Error("Request timed out. Please check your connection and try again.",{cause:n}):n}}async getBudget(e,t){return await this.client.get(`v1/workspaces/${t}/budget`,{headers:{Authorization:`Bearer ${e}`}}).json()}async updateBudget(e,t,s){return await this.client.put(`v1/workspaces/${t}/budget`,{headers:{Authorization:`Bearer ${e}`},json:{budget:s}}).json()}async getMonthlyUsages(e,t,s){return await this.client.get(`v1/workspaces/${t}/monthlyUsages`,{headers:{Authorization:`Bearer ${e}`},searchParams:{nextKey:s?.nextKey,limit:s?.limit??20}}).json()}async createChatShare(e,t,s,n="public"){return await this.client.post(`v1/workspaces/${t}/chats/${s}/shares`,{headers:{Authorization:`Bearer ${e}`},json:{visibility:n}}).json()}async getSharedChat(e,t){let s={};return t&&(s.Authorization=`Bearer ${t}`),await this.client.get(`v1/shares/${e}`,{headers:s}).json()}async getSharedMessages(e,t=50,s){let n={};return s&&(n.Authorization=`Bearer ${s}`),await this.client.get(`v1/shares/${e}/messages`,{headers:n,searchParams:{limit:t}}).json()}async deleteChat(e,t,s){await this.client.delete(`v1/workspaces/${t}/chats/${s}`,{headers:{Authorization:`Bearer ${e}`}})}async getModelProviders(e,t){return await this.client.get(`v1/workspaces/${t}/modelProviders`,{headers:{Authorization:`Bearer ${e}`}}).json()}async uploadFile(e,t,s,n){let i=new FormData,g=new Blob([s.buffer],{type:s.mimeType});return i.append("file",g,s.filename),i.append("modelProviderId",n),await this.client.post(`v1/workspaces/${t}/files`,{headers:{Authorization:`Bearer ${e}`},body:i,timeout:12e4}).json()}async getSchedules(e,t,s){let n={limit:s?.limit??20};return s?.nextKey&&(n.nextKey=s.nextKey),s?.search&&(n.search=s.search),await this.client.get(`v1/workspaces/${t}/schedules`,{headers:{Authorization:`Bearer ${e}`},searchParams:n}).json()}async getSchedule(e,t,s){return await this.client.get(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`}}).json()}async createSchedule(e,t,s){return await this.client.post(`v1/workspaces/${t}/schedules`,{headers:{Authorization:`Bearer ${e}`},json:s}).json()}async updateSchedule(e,t,s,n){await this.client.put(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`},json:n})}async deleteSchedule(e,t,s){await this.client.delete(`v1/workspaces/${t}/schedules/${s}`,{headers:{Authorization:`Bearer ${e}`}})}static async getAuthenticatedClient(e){let t=await h.getAll(e);return t?{client:new r(void 0,e),accessToken:t.accessToken,workspaceId:t.workspaceId??null}:null}};var p=class extends Error{code;constructor(e,t){super(t),this.code=e}};function se(r){try{let e=r.split(".");if(e.length<2)return null;let t=e[1].replace(/-/g,"+").replace(/_/g,"/"),s=(4-t.length%4)%4,n=t.padEnd(t.length+s,"="),i=Buffer.from(n,"base64").toString("utf8");return JSON.parse(i)}catch{return null}}function ne(r,e=300){let t=se(r);if(!t?.exp)return!1;let s=Math.floor(Date.now()/1e3);return t.exp-s<=e}async function W(r){let e=r.accessToken;if(!ne(e))return e;if(!r.refreshToken)throw await h.clear(),new p("session-expired","Session expired. Please run `entrydesk login` again.");try{let t=new $,{token:s}=await t.refreshToken(r.refreshToken);return await h.saveAccessToken(s),s}catch{throw await h.clear(),new p("session-expired","Session expired. Please run `entrydesk login` again.")}}async function Fe(){let r=await h.getAll();if(!r?.accessToken)throw new p("not-logged-in","Not logged in. Run `entrydesk login` first.");return W(r)}async function Ke(){let r=await h.getAll();if(!r?.accessToken)throw new p("not-logged-in","Not logged in. Run `entrydesk login` first.");if(!r.workspaceId)throw new p("no-workspace","No workspace selected. Run `entrydesk workspaces` first.");return{accessToken:await W(r),workspaceId:r.workspaceId}}var c=class extends Error{constructor(t,s=1,n){super(t);this.exitCode=s;this.cause=n;this.name="CLIError"}};function De(r){let e=!!process.env.DEBUG;r instanceof c&&(console.error(`Error: ${r.message}`),r.cause&&(console.error(`Cause: ${r.cause.message}`),e&&r.cause.stack&&console.error(r.cause.stack)),process.exit(r.exitCode)),r instanceof p&&(console.error(r.message),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof oe&&(console.error("Request timed out. Please check your connection and try again."),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof ie&&(console.error(`Request failed (${r.response.status}): ${r.response.statusText}`),e&&r.stack&&console.error(r.stack),process.exit(1)),r instanceof Error&&(console.error(`Unexpected error: ${r.message}`),e&&r.stack&&console.error(r.stack),process.exit(1)),console.error("An unknown error occurred"),process.exit(1)}var m="default",le=/^[A-Za-z0-9_-]+$/,K=null;function B(){return S.join(ae.homedir(),".entrydesk")}function H(){return S.join(B(),"profiles")}function J(){return S.join(B(),"profiles.json")}function U(r){return S.join(H(),r)}function M(r){if(!u.existsSync(r))u.mkdirSync(r,{mode:448,recursive:!0});else try{u.chmodSync(r,448)}catch{}}function ce(){M(B())}function I(){M(H())}function ge(){let r=J();try{if(!u.existsSync(r))return null;let e=u.readFileSync(r,"utf-8");return JSON.parse(e)}catch{return null}}function k(r){ce();let e=J();u.writeFileSync(e,JSON.stringify(r,null,2),{mode:384});try{u.chmodSync(e,384)}catch{}}function de(r){let e=r.profiles?.length?Array.from(new Set(r.profiles)):[],t=r.currentProfile&&e.includes(r.currentProfile)?r.currentProfile:e[0]||m;return e.length===0&&e.push(m),e.includes(t)||e.unshift(t),{currentProfile:t,profiles:e}}function y(){let r=ge();if(!r){let t={currentProfile:m,profiles:[m]};return k(t),I(),x(m),t}let e=de(r);return(e.currentProfile!==r.currentProfile||e.profiles.length!==r.profiles.length)&&k(e),I(),e}function P(r){if(!r)throw new c("Profile name is required.");if(!le.test(r))throw new c("Invalid profile name. Use letters, numbers, dash, or underscore.")}function b(r,e){if(!r.profiles.includes(e))throw new c(`Profile not found: ${e}`)}function G(r,e){if(r.profiles.includes(e))throw new c(`Profile already exists: ${e}`)}function x(r){I(),M(U(r))}var o={DEFAULT_PROFILE:m,setOverrideProfile(r){K=r},getOverrideProfile(){return K},getProfileDir(r){return U(r)},getProfileConfigFile(r){return S.join(o.getProfileDir(r),"config.json")},getProfileCredentialsFile(r){return S.join(o.getProfileDir(r),"credentials.json")},getProfileNames(){return[...y().profiles]},getProfileCount(){return o.getProfileNames().length},getCurrentProfileName(){return y().currentProfile},getEffectiveProfileName(r){let e=y(),t=r||K||e.currentProfile;return P(t),b(e,t),t},ensureProfileExists(r){let e=y();P(r),b(e,r)},createProfile(r){P(r);let e=y();G(e,r);let t={currentProfile:e.currentProfile,profiles:[...e.profiles,r]};k(t),x(r)},deleteProfile(r){P(r);let e=y();if(b(e,r),e.profiles.length===1)throw new c("Cannot delete the last profile.");let t=e.profiles.filter(i=>i!==r),s=e.currentProfile;s===r&&(s=t.includes(m)?m:t[0]),k({currentProfile:s,profiles:t});try{u.rmSync(o.getProfileDir(r),{recursive:!0,force:!0})}catch{}return{currentProfile:s}},renameProfile(r,e){P(r),P(e);let t=y();b(t,r),G(t,e);let s=U(r),n=U(e);if(u.existsSync(s))try{u.renameSync(s,n)}catch(v){throw new c("Failed to rename profile directory.",1,v)}else x(e);let i=t.profiles.map(v=>v===r?e:v),g=t.currentProfile===r?e:t.currentProfile;return k({currentProfile:g,profiles:i}),{currentProfile:g}},setCurrentProfile(r){P(r);let e=y();b(e,r);let t={currentProfile:r,profiles:e.profiles};k(t),x(r)},ensureProfileDir:x};var fe="ENTRYDESK_FORCE_FILE_STORAGE",C="EntryDesk CLI",pe="__entrydesk_keychain_test__",he="profile:",ye=".credentials.key",Y={keychain:!1,permissions:!1,corruptFile:!1,windowsFile:!1};function w(r,e){Y[r]||(Y[r]=!0,console.error(e))}function q(r){if(!r||typeof r!="object")return!1;let e=r;return!(!("accessToken"in e||"refreshToken"in e||"email"in e||"workspaceId"in e)||"accessToken"in e&&typeof e.accessToken!="string"||"refreshToken"in e&&typeof e.refreshToken!="string"||"email"in e&&typeof e.email!="string"||"workspaceId"in e&&typeof e.workspaceId!="string")}function me(r){if(!r||typeof r!="object")return!1;let e=r;return typeof e.getPassword=="function"&&typeof e.setPassword=="function"&&typeof e.deletePassword=="function"}function D(r){return o.getProfileCredentialsFile(r)}var L=class{getKeyFilePath(e){return ue.join(o.getProfileDir(e),ye)}loadEncryptionKey(e){let t=this.getKeyFilePath(e);try{if(!f.existsSync(t))return null;let s=f.readFileSync(t);return s.length!==32?null:(this.ensureSecureFilePermissions(t),s)}catch{return null}}createEncryptionKey(e){o.ensureProfileDir(e);let t=this.getKeyFilePath(e),s=R.randomBytes(32);return f.writeFileSync(t,s,{mode:384}),this.ensureSecureFilePermissions(t),s}getOrCreateEncryptionKey(e){let t=this.loadEncryptionKey(e);return t||this.createEncryptionKey(e)}encrypt(e,t){let s=R.randomBytes(16),n=R.createCipheriv("aes-256-gcm",t,s),i=n.update(e,"utf8","hex");i+=n.final("hex");let g=n.getAuthTag();return`${s.toString("hex")}:${g.toString("hex")}:${i}`}decrypt(e,t){let s=e.split(":");if(s.length!==3)throw new Error("Invalid encrypted data format");let n=Buffer.from(s[0],"hex"),i=Buffer.from(s[1],"hex"),g=s[2],A=R.createDecipheriv("aes-256-gcm",t,n,{authTagLength:16});A.setAuthTag(i);let v=A.update(g,"hex","utf8");return v+=A.final("utf8"),v}tryDecrypt(e,t){try{return this.decrypt(e,t)}catch{return null}}tryParseCredentials(e){try{let t=JSON.parse(e);return q(t)?t:null}catch{return null}}ensureSecureFilePermissions(e){if(process.platform!=="win32"){try{f.chmodSync(e,384)}catch{w("permissions","Warning: Failed to set secure permissions on credentials file.");return}try{(f.statSync(e).mode&511)!==384&&w("permissions","Warning: Credentials file permissions are too permissive.")}catch{w("permissions","Warning: Failed to verify credentials file permissions.")}}}async getCredentials(e){let t=D(e);try{if(!f.existsSync(t))return null;let s=f.readFileSync(t,"utf-8").trim();if(!s)return null;let n=this.loadEncryptionKey(e),i=n?this.tryDecrypt(s,n):null;if(i){let g=this.tryParseCredentials(i);if(g)return g}return w("corruptFile","Warning: Credentials file is unreadable. Please run `entrydesk login` again."),null}catch{return null}}async setCredentials(e,t){o.ensureProfileDir(e);let s=D(e),n=this.getOrCreateEncryptionKey(e),i=this.encrypt(JSON.stringify(t),n);f.writeFileSync(s,i,{mode:384}),this.ensureSecureFilePermissions(s)}async deleteCredentials(e){let t=D(e);try{f.existsSync(t)&&f.unlinkSync(t)}catch{}}},_=class{keytarModule=null;keytarLoadAttempted=!1;keychainAvailable=null;async getKeytar(){if(this.keytarLoadAttempted)return this.keytarModule;this.keytarLoadAttempted=!0;try{let t=await import("keytar"),s=t&&typeof t=="object"&&"default"in t?t.default:t;this.keytarModule=me(s)?s:null}catch{this.keytarModule=null}return this.keytarModule}getAccount(e){return`${he}${e}`}async checkKeychainAvailability(){if(this.keychainAvailable!==null)return this.keychainAvailable;try{let e=await this.getKeytar();if(!e)return this.keychainAvailable=!1,!1;let t=`${pe}${R.randomBytes(8).toString("hex")}`,s="test";await e.setPassword(C,t,s);let n=await e.getPassword(C,t),i=await e.deletePassword(C,t);return this.keychainAvailable=!!(i&&n===s),this.keychainAvailable}catch{return this.keychainAvailable=!1,!1}}async isAvailable(){return this.checkKeychainAvailability()}async getCredentials(e){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let t=await this.getKeytar();if(!t)throw new Error("Keytar module not available");let s=await t.getPassword(C,this.getAccount(e));if(!s)return null;let n=this.tryParseKeychainPayload(s);return n||w("corruptFile",`Warning: Keychain entry for profile "${e}" is unreadable.`),n}async setCredentials(e,t){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let s=await this.getKeytar();if(!s)throw new Error("Keytar module not available");let n=JSON.stringify(t);await s.setPassword(C,this.getAccount(e),n)}async deleteCredentials(e){if(!await this.checkKeychainAvailability())throw new Error("Keychain is not available");let t=await this.getKeytar();if(!t)throw new Error("Keytar module not available");await t.deletePassword(C,this.getAccount(e))}tryParseKeychainPayload(e){try{let t=JSON.parse(e);return q(t)?t:null}catch{return null}}},j=class{keychainStorage=null;fileStorage=new L;keychainAvailable=null;initPromise=null;shouldForceFileStorage(){return process.env[fe]?.toLowerCase()==="true"}async initializeStorage(){if(this.shouldForceFileStorage()){this.keychainAvailable=!1,this.keychainStorage=null;return}try{let e=new _,t=await e.isAvailable();this.keychainAvailable=t,this.keychainStorage=t?e:null}catch{this.keychainAvailable=!1,this.keychainStorage=null}!this.keychainAvailable&&!this.shouldForceFileStorage()&&(w("keychain","Keychain unavailable. Falling back to file-based credentials."),process.platform==="win32"&&w("windowsFile","Warning: File-based credentials on Windows may be readable by other users."))}async ensureInitialized(){if(this.shouldForceFileStorage()){this.keychainAvailable=!1,this.keychainStorage=null;return}this.keychainAvailable===null&&(this.initPromise||(this.initPromise=this.initializeStorage()),await this.initPromise)}disableKeychain(){this.shouldForceFileStorage()||w("keychain","Keychain error encountered. Falling back to file-based credentials."),this.keychainAvailable=!1,this.keychainStorage=null}async getKeychainStorage(){return await this.ensureInitialized(),this.shouldForceFileStorage()?null:this.keychainAvailable?this.keychainStorage:null}async getCredentials(e){let t=await this.getKeychainStorage();if(!t)return this.fileStorage.getCredentials(e);try{let n=await t.getCredentials(e);if(n)return n}catch{return this.disableKeychain(),this.fileStorage.getCredentials(e)}let s=await this.fileStorage.getCredentials(e);if(s)try{await t.setCredentials(e,s),await this.fileStorage.deleteCredentials(e)}catch{}return s}async setCredentials(e,t){let s=await this.getKeychainStorage();if(!s){await this.fileStorage.setCredentials(e,t);return}try{await s.setCredentials(e,t),await this.fileStorage.deleteCredentials(e)}catch{this.disableKeychain(),await this.fileStorage.setCredentials(e,t)}}async deleteCredentials(e){let t=await this.getKeychainStorage();if(t)try{await t.deleteCredentials(e)}catch{this.disableKeychain()}await this.fileStorage.deleteCredentials(e)}},l=new j,h={async saveAccessToken(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.accessToken=r,await l.setCredentials(t,s)},async getAccessToken(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.accessToken??null},async saveRefreshToken(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.refreshToken=r,await l.setCredentials(t,s)},async getRefreshToken(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.refreshToken??null},async saveEmail(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.email=r,await l.setCredentials(t,s)},async getEmail(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.email??null},async saveWorkspaceId(r,e){let t=o.getEffectiveProfileName(e),s=await l.getCredentials(t)||{};s.workspaceId=r,await l.setCredentials(t,s)},async getWorkspaceId(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.workspaceId??null},async saveAll(r,e){let t=o.getEffectiveProfileName(e);await l.setCredentials(t,r)},async getAll(r){let e=o.getEffectiveProfileName(r);return l.getCredentials(e)},async clear(r){let e=o.getEffectiveProfileName(r);await l.deleteCredentials(e)},async isLoggedIn(r){let e=o.getEffectiveProfileName(r);return(await l.getCredentials(e))?.accessToken!=null}};export{h as a,p as b,Fe as c,Ke as d,c as e,De as f,o as g,F as h,T as i,z as j,Se as k,Ce as l,O as m,$ as n};
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import{a}from"./chunk-J6ER4MMT.js";export{a as TokenStorage};