MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / saveConfig

Function saveConfig

cli/src/config.ts:54–118  ·  view source on GitHub ↗
(updates: Partial<Config>)

Source from the content-addressed store, hash-verified

52}
53
54export function saveConfig(updates: Partial<Config>): void {
55 const current = loadConfig();
56 const merged = { ...current, ...updates };
57
58 // Read existing file to preserve fields we don't manage
59 let existing: Record<string, string> = {};
60 try {
61 existing = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
62 } catch {
63 // No existing file
64 }
65
66 // Don't write env-only values back to file, but preserve unrelated fields.
67 const fileConfig: Record<string, string> = { ...existing };
68
69 if ("api_key" in updates) {
70 if (updates.api_key) fileConfig.api_key = updates.api_key;
71 else delete fileConfig.api_key;
72 } else if (!process.env.E2A_API_KEY && merged.api_key) {
73 fileConfig.api_key = merged.api_key;
74 }
75
76 if ("api_url" in updates) {
77 if (updates.api_url && updates.api_url !== DEFAULT_URL && !process.env.E2A_URL) {
78 fileConfig.api_url = updates.api_url;
79 } else {
80 delete fileConfig.api_url;
81 }
82 } else if (!process.env.E2A_URL && merged.api_url !== DEFAULT_URL) {
83 fileConfig.api_url = merged.api_url;
84 } else if (!process.env.E2A_URL) {
85 delete fileConfig.api_url;
86 }
87
88 if ("agent_email" in updates) {
89 if (updates.agent_email) fileConfig.agent_email = updates.agent_email;
90 else delete fileConfig.agent_email;
91 } else if (merged.agent_email) {
92 fileConfig.agent_email = merged.agent_email;
93 } else {
94 delete fileConfig.agent_email;
95 }
96
97 // Mirror the api_url policy: only persist non-default, non-env-overridden values.
98 if ("shared_domain" in updates) {
99 if (
100 updates.shared_domain &&
101 updates.shared_domain !== DEFAULT_SHARED_DOMAIN &&
102 !process.env.E2A_SHARED_DOMAIN
103 ) {
104 fileConfig.shared_domain = updates.shared_domain;
105 } else {
106 delete fileConfig.shared_domain;
107 }
108 } else if (!process.env.E2A_SHARED_DOMAIN && merged.shared_domain !== DEFAULT_SHARED_DOMAIN) {
109 fileConfig.shared_domain = merged.shared_domain;
110 } else if (!process.env.E2A_SHARED_DOMAIN) {
111 delete fileConfig.shared_domain;

Callers 3

configFunction · 0.85
loginFunction · 0.85
config.test.tsFile · 0.85

Calls 3

parseMethod · 0.80
stringifyMethod · 0.80
loadConfigFunction · 0.70

Tested by

no test coverage detected