@codelia/config-loader 0.1.83 → 0.1.84

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -35,7 +35,9 @@ __export(index_exports, {
35
35
  loadConfig: () => loadConfig,
36
36
  loadMcpServers: () => loadMcpServers,
37
37
  removeMcpServerConfig: () => removeMcpServerConfig,
38
+ removePermissionRules: () => removePermissionRules,
38
39
  setMcpServerEnabled: () => setMcpServerEnabled,
40
+ updateConfigValue: () => updateConfigValue,
39
41
  updateImageGenerationConfig: () => updateImageGenerationConfig,
40
42
  updateModelConfig: () => updateModelConfig,
41
43
  updateTuiConfig: () => updateTuiConfig,
@@ -53,6 +55,22 @@ var pickDefined = (value) => Object.fromEntries(
53
55
  Object.entries(value).filter(([, entry]) => entry !== void 0)
54
56
  );
55
57
  var pickString = (value) => typeof value === "string" ? value : void 0;
58
+ var configWriteQueues = /* @__PURE__ */ new Map();
59
+ var withConfigFileLock = async (configPath, fn) => {
60
+ const key = import_node_path.default.resolve(configPath);
61
+ const previous = configWriteQueues.get(key) ?? Promise.resolve();
62
+ const next = previous.then(fn, fn);
63
+ const settled = next.then(
64
+ () => void 0,
65
+ () => void 0
66
+ );
67
+ configWriteQueues.set(key, settled);
68
+ try {
69
+ return await next;
70
+ } finally {
71
+ if (configWriteQueues.get(key) === settled) configWriteQueues.delete(key);
72
+ }
73
+ };
56
74
  var readConfigRaw = async (configPath) => {
57
75
  let raw;
58
76
  try {
@@ -134,110 +152,189 @@ var loadConfig = async (configPath) => {
134
152
  return (0, import_config.parseConfig)(result.config, result.filepath);
135
153
  };
136
154
  var updateModelConfig = async (configPath, model) => {
137
- const raw = await readConfigRaw(configPath) ?? {};
138
- const version = ensureVersion(raw, configPath);
139
- const currentModel = isRecord(raw.model) ? raw.model : {};
140
- const nextModel = {
141
- ...currentModel,
142
- ...pickDefined(model)
143
- };
144
- const nextRaw = {
145
- ...raw,
146
- version,
147
- model: nextModel
148
- };
149
- await writeConfigRaw(configPath, nextRaw);
150
- return (0, import_config.parseConfig)(nextRaw, configPath);
155
+ return withConfigFileLock(configPath, async () => {
156
+ const raw = await readConfigRaw(configPath) ?? {};
157
+ const version = ensureVersion(raw, configPath);
158
+ const currentModel = isRecord(raw.model) ? raw.model : {};
159
+ const nextModel = {
160
+ ...currentModel,
161
+ ...pickDefined(model)
162
+ };
163
+ const nextRaw = {
164
+ ...raw,
165
+ version,
166
+ model: nextModel
167
+ };
168
+ await writeConfigRaw(configPath, nextRaw);
169
+ return (0, import_config.parseConfig)(nextRaw, configPath);
170
+ });
151
171
  };
152
172
  var loadMcpServers = async (configPath) => {
153
173
  const config = await loadConfig(configPath);
154
174
  return config?.mcp?.servers ?? {};
155
175
  };
156
176
  var upsertMcpServerConfig = async (configPath, serverId, server) => {
157
- const raw = await readConfigRaw(configPath) ?? {};
158
- const version = ensureVersion(raw, configPath);
159
- const servers = getRawMcpServerMap(raw);
160
- servers[serverId] = server;
161
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
162
- await writeConfigRaw(configPath, nextRaw);
163
- return (0, import_config.parseConfig)(nextRaw, configPath);
177
+ return withConfigFileLock(configPath, async () => {
178
+ const raw = await readConfigRaw(configPath) ?? {};
179
+ const version = ensureVersion(raw, configPath);
180
+ const servers = getRawMcpServerMap(raw);
181
+ servers[serverId] = server;
182
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
183
+ await writeConfigRaw(configPath, nextRaw);
184
+ return (0, import_config.parseConfig)(nextRaw, configPath);
185
+ });
164
186
  };
165
187
  var removeMcpServerConfig = async (configPath, serverId) => {
166
- const raw = await readConfigRaw(configPath);
167
- if (!raw) return false;
168
- const version = ensureVersion(raw, configPath);
169
- const servers = getRawMcpServerMap(raw);
170
- if (servers[serverId] === void 0) {
171
- return false;
172
- }
173
- delete servers[serverId];
174
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
175
- await writeConfigRaw(configPath, nextRaw);
176
- return true;
188
+ return withConfigFileLock(configPath, async () => {
189
+ const raw = await readConfigRaw(configPath);
190
+ if (!raw) return false;
191
+ const version = ensureVersion(raw, configPath);
192
+ const servers = getRawMcpServerMap(raw);
193
+ if (servers[serverId] === void 0) {
194
+ return false;
195
+ }
196
+ delete servers[serverId];
197
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
198
+ await writeConfigRaw(configPath, nextRaw);
199
+ return true;
200
+ });
177
201
  };
178
202
  var setMcpServerEnabled = async (configPath, serverId, enabled) => {
179
- const raw = await readConfigRaw(configPath);
180
- if (!raw) return false;
181
- const version = ensureVersion(raw, configPath);
182
- const servers = getRawMcpServerMap(raw);
183
- if (servers[serverId] === void 0) {
184
- return false;
185
- }
186
- const current = isRecord(servers[serverId]) ? { ...servers[serverId] } : {};
187
- current.enabled = enabled;
188
- servers[serverId] = current;
189
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
190
- await writeConfigRaw(configPath, nextRaw);
191
- return true;
203
+ return withConfigFileLock(configPath, async () => {
204
+ const raw = await readConfigRaw(configPath);
205
+ if (!raw) return false;
206
+ const version = ensureVersion(raw, configPath);
207
+ const servers = getRawMcpServerMap(raw);
208
+ if (servers[serverId] === void 0) {
209
+ return false;
210
+ }
211
+ const current = isRecord(servers[serverId]) ? { ...servers[serverId] } : {};
212
+ current.enabled = enabled;
213
+ servers[serverId] = current;
214
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
215
+ await writeConfigRaw(configPath, nextRaw);
216
+ return true;
217
+ });
192
218
  };
193
219
  var appendPermissionAllowRules = async (configPath, rules) => {
194
- const raw = await readConfigRaw(configPath) ?? {};
195
- const version = ensureVersion(raw, configPath);
196
- const permissions = isRecord(raw.permissions) ? raw.permissions : {};
197
- const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
198
- for (const rule of rules) {
199
- if (!allow.some((entry) => isSameRule(entry, rule))) {
200
- allow.push(rule);
220
+ return withConfigFileLock(configPath, async () => {
221
+ const raw = await readConfigRaw(configPath) ?? {};
222
+ const version = ensureVersion(raw, configPath);
223
+ const permissions = isRecord(raw.permissions) ? raw.permissions : {};
224
+ const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
225
+ for (const rule of rules) {
226
+ if (!allow.some((entry) => isSameRule(entry, rule))) {
227
+ allow.push(rule);
228
+ }
201
229
  }
202
- }
203
- const nextPermissions = {
204
- ...permissions,
205
- allow
206
- };
207
- const nextRaw = {
208
- ...raw,
209
- version,
210
- permissions: nextPermissions
211
- };
212
- await writeConfigRaw(configPath, nextRaw);
213
- return (0, import_config.parseConfig)(nextRaw, configPath);
230
+ const nextPermissions = {
231
+ ...permissions,
232
+ allow
233
+ };
234
+ const nextRaw = {
235
+ ...raw,
236
+ version,
237
+ permissions: nextPermissions
238
+ };
239
+ await writeConfigRaw(configPath, nextRaw);
240
+ return (0, import_config.parseConfig)(nextRaw, configPath);
241
+ });
214
242
  };
215
243
  var appendPermissionAllowRule = async (configPath, rule) => {
216
244
  return appendPermissionAllowRules(configPath, [rule]);
217
245
  };
218
246
  var updateTuiConfig = async (configPath, tui) => {
219
- const raw = await readConfigRaw(configPath) ?? {};
220
- const version = ensureVersion(raw, configPath);
221
- const currentTui = isRecord(raw.tui) ? raw.tui : {};
222
- const nextTui = {
223
- ...currentTui,
224
- ...pickDefined(tui)
225
- };
226
- const nextRaw = {
227
- ...raw,
228
- version,
229
- tui: nextTui
230
- };
231
- await writeConfigRaw(configPath, nextRaw);
232
- return (0, import_config.parseConfig)(nextRaw, configPath);
247
+ return withConfigFileLock(configPath, async () => {
248
+ const raw = await readConfigRaw(configPath) ?? {};
249
+ const version = ensureVersion(raw, configPath);
250
+ const currentTui = isRecord(raw.tui) ? raw.tui : {};
251
+ const nextTui = {
252
+ ...currentTui,
253
+ ...pickDefined(tui)
254
+ };
255
+ const nextRaw = {
256
+ ...raw,
257
+ version,
258
+ tui: nextTui
259
+ };
260
+ await writeConfigRaw(configPath, nextRaw);
261
+ return (0, import_config.parseConfig)(nextRaw, configPath);
262
+ });
233
263
  };
234
264
  var updateImageGenerationConfig = async (configPath, image) => {
235
- const raw = await readConfigRaw(configPath) ?? {};
236
- const version = ensureVersion(raw, configPath);
237
- const nextRaw = { ...raw, version, image_generation: image };
238
- const parsed = (0, import_config.parseConfig)(nextRaw, configPath);
239
- await writeConfigRaw(configPath, nextRaw);
240
- return parsed;
265
+ return withConfigFileLock(configPath, async () => {
266
+ const raw = await readConfigRaw(configPath) ?? {};
267
+ const version = ensureVersion(raw, configPath);
268
+ const nextRaw = { ...raw, version, image_generation: image };
269
+ const parsed = (0, import_config.parseConfig)(nextRaw, configPath);
270
+ await writeConfigRaw(configPath, nextRaw);
271
+ return parsed;
272
+ });
273
+ };
274
+ var updateConfigValue = async (configPath, keyPath, value) => {
275
+ return withConfigFileLock(configPath, async () => {
276
+ if (keyPath.length === 0) throw new Error("config key path is empty");
277
+ const raw = await readConfigRaw(configPath) ?? {};
278
+ const version = ensureVersion(raw, configPath);
279
+ const nextRaw = structuredClone(raw);
280
+ nextRaw.version = version;
281
+ const parents = [nextRaw];
282
+ let cursor = nextRaw;
283
+ for (const segment of keyPath.slice(0, -1)) {
284
+ const child = cursor[segment];
285
+ if (!isRecord(child)) {
286
+ if (value === void 0) return false;
287
+ if (child !== void 0) {
288
+ throw new Error(`${configPath}: ${segment} must be an object`);
289
+ }
290
+ cursor[segment] = {};
291
+ }
292
+ cursor = cursor[segment];
293
+ parents.push(cursor);
294
+ }
295
+ const leaf = keyPath[keyPath.length - 1];
296
+ if (value === void 0) {
297
+ if (!Object.hasOwn(cursor, leaf)) return false;
298
+ delete cursor[leaf];
299
+ for (let index = parents.length - 1; index > 0; index -= 1) {
300
+ const parent = parents[index - 1];
301
+ const segment = keyPath[index - 1];
302
+ if (Object.keys(parents[index]).length > 0) break;
303
+ delete parent[segment];
304
+ }
305
+ } else {
306
+ cursor[leaf] = value;
307
+ }
308
+ (0, import_config.parseConfig)(nextRaw, configPath);
309
+ await writeConfigRaw(configPath, nextRaw);
310
+ return true;
311
+ });
312
+ };
313
+ var removePermissionRules = async (configPath, kind, rule) => {
314
+ return withConfigFileLock(configPath, async () => {
315
+ const raw = await readConfigRaw(configPath);
316
+ if (!raw || !isRecord(raw.permissions)) return 0;
317
+ const version = ensureVersion(raw, configPath);
318
+ const list = raw.permissions[kind];
319
+ if (!Array.isArray(list)) return 0;
320
+ const target = (0, import_config.normalizePermissionRule)(rule);
321
+ if (!target) return 0;
322
+ const kept = list.filter((entry) => {
323
+ const normalized = (0, import_config.normalizePermissionRule)(entry);
324
+ return !(normalized && isSameRule(normalized, target));
325
+ });
326
+ const removed = list.length - kept.length;
327
+ if (removed === 0) return 0;
328
+ const permissions = { ...raw.permissions };
329
+ if (kept.length > 0) permissions[kind] = kept;
330
+ else delete permissions[kind];
331
+ const nextRaw = { ...raw, version };
332
+ if (Object.keys(permissions).length > 0) nextRaw.permissions = permissions;
333
+ else delete nextRaw.permissions;
334
+ (0, import_config.parseConfig)(nextRaw, configPath);
335
+ await writeConfigRaw(configPath, nextRaw);
336
+ return removed;
337
+ });
241
338
  };
242
339
  // Annotate the CommonJS export names for ESM import in node:
243
340
  0 && (module.exports = {
@@ -246,7 +343,9 @@ var updateImageGenerationConfig = async (configPath, image) => {
246
343
  loadConfig,
247
344
  loadMcpServers,
248
345
  removeMcpServerConfig,
346
+ removePermissionRules,
249
347
  setMcpServerEnabled,
348
+ updateConfigValue,
250
349
  updateImageGenerationConfig,
251
350
  updateModelConfig,
252
351
  updateTuiConfig,
package/dist/index.d.cts CHANGED
@@ -12,5 +12,17 @@ declare const appendPermissionAllowRule: (configPath: string, rule: PermissionRu
12
12
  declare const updateTuiConfig: (configPath: string, tui: TuiConfig) => Promise<CodeliaConfig>;
13
13
  /** Replace the image block, preserving unrelated raw configuration. */
14
14
  declare const updateImageGenerationConfig: (configPath: string, image: _codelia_config.ImageGenerationConfig) => Promise<CodeliaConfig>;
15
+ /**
16
+ * Set (`value` defined) or remove (`undefined`) one leaf at `keyPath`,
17
+ * preserving unrelated raw configuration and pruning emptied parent objects.
18
+ * Returns false when removing a key that is not present (no write).
19
+ */
20
+ declare const updateConfigValue: (configPath: string, keyPath: readonly string[], value: string | number | boolean | undefined) => Promise<boolean>;
21
+ /**
22
+ * Remove every `permissions.<kind>` entry structurally equal to `rule`
23
+ * (tool/command/command_glob/skill_name) after `parseConfig` normalization. Emptied lists and an emptied
24
+ * `permissions` object are pruned. Returns the number removed (0: no write).
25
+ */
26
+ declare const removePermissionRules: (configPath: string, kind: "allow" | "deny", rule: PermissionRule) => Promise<number>;
15
27
 
16
- export { appendPermissionAllowRule, appendPermissionAllowRules, loadConfig, loadMcpServers, removeMcpServerConfig, setMcpServerEnabled, updateImageGenerationConfig, updateModelConfig, updateTuiConfig, upsertMcpServerConfig };
28
+ export { appendPermissionAllowRule, appendPermissionAllowRules, loadConfig, loadMcpServers, removeMcpServerConfig, removePermissionRules, setMcpServerEnabled, updateConfigValue, updateImageGenerationConfig, updateModelConfig, updateTuiConfig, upsertMcpServerConfig };
package/dist/index.d.ts CHANGED
@@ -12,5 +12,17 @@ declare const appendPermissionAllowRule: (configPath: string, rule: PermissionRu
12
12
  declare const updateTuiConfig: (configPath: string, tui: TuiConfig) => Promise<CodeliaConfig>;
13
13
  /** Replace the image block, preserving unrelated raw configuration. */
14
14
  declare const updateImageGenerationConfig: (configPath: string, image: _codelia_config.ImageGenerationConfig) => Promise<CodeliaConfig>;
15
+ /**
16
+ * Set (`value` defined) or remove (`undefined`) one leaf at `keyPath`,
17
+ * preserving unrelated raw configuration and pruning emptied parent objects.
18
+ * Returns false when removing a key that is not present (no write).
19
+ */
20
+ declare const updateConfigValue: (configPath: string, keyPath: readonly string[], value: string | number | boolean | undefined) => Promise<boolean>;
21
+ /**
22
+ * Remove every `permissions.<kind>` entry structurally equal to `rule`
23
+ * (tool/command/command_glob/skill_name) after `parseConfig` normalization. Emptied lists and an emptied
24
+ * `permissions` object are pruned. Returns the number removed (0: no write).
25
+ */
26
+ declare const removePermissionRules: (configPath: string, kind: "allow" | "deny", rule: PermissionRule) => Promise<number>;
15
27
 
16
- export { appendPermissionAllowRule, appendPermissionAllowRules, loadConfig, loadMcpServers, removeMcpServerConfig, setMcpServerEnabled, updateImageGenerationConfig, updateModelConfig, updateTuiConfig, upsertMcpServerConfig };
28
+ export { appendPermissionAllowRule, appendPermissionAllowRules, loadConfig, loadMcpServers, removeMcpServerConfig, removePermissionRules, setMcpServerEnabled, updateConfigValue, updateImageGenerationConfig, updateModelConfig, updateTuiConfig, upsertMcpServerConfig };
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  // src/index.ts
2
2
  import { mkdir, readFile, writeFile } from "fs/promises";
3
3
  import path from "path";
4
- import { CONFIG_VERSION, parseConfig } from "@codelia/config";
4
+ import {
5
+ CONFIG_VERSION,
6
+ normalizePermissionRule,
7
+ parseConfig
8
+ } from "@codelia/config";
5
9
  import { cosmiconfig } from "cosmiconfig";
6
10
  var MODULE_NAME = "codelia";
7
11
  var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
@@ -10,6 +14,22 @@ var pickDefined = (value) => Object.fromEntries(
10
14
  Object.entries(value).filter(([, entry]) => entry !== void 0)
11
15
  );
12
16
  var pickString = (value) => typeof value === "string" ? value : void 0;
17
+ var configWriteQueues = /* @__PURE__ */ new Map();
18
+ var withConfigFileLock = async (configPath, fn) => {
19
+ const key = path.resolve(configPath);
20
+ const previous = configWriteQueues.get(key) ?? Promise.resolve();
21
+ const next = previous.then(fn, fn);
22
+ const settled = next.then(
23
+ () => void 0,
24
+ () => void 0
25
+ );
26
+ configWriteQueues.set(key, settled);
27
+ try {
28
+ return await next;
29
+ } finally {
30
+ if (configWriteQueues.get(key) === settled) configWriteQueues.delete(key);
31
+ }
32
+ };
13
33
  var readConfigRaw = async (configPath) => {
14
34
  let raw;
15
35
  try {
@@ -91,110 +111,189 @@ var loadConfig = async (configPath) => {
91
111
  return parseConfig(result.config, result.filepath);
92
112
  };
93
113
  var updateModelConfig = async (configPath, model) => {
94
- const raw = await readConfigRaw(configPath) ?? {};
95
- const version = ensureVersion(raw, configPath);
96
- const currentModel = isRecord(raw.model) ? raw.model : {};
97
- const nextModel = {
98
- ...currentModel,
99
- ...pickDefined(model)
100
- };
101
- const nextRaw = {
102
- ...raw,
103
- version,
104
- model: nextModel
105
- };
106
- await writeConfigRaw(configPath, nextRaw);
107
- return parseConfig(nextRaw, configPath);
114
+ return withConfigFileLock(configPath, async () => {
115
+ const raw = await readConfigRaw(configPath) ?? {};
116
+ const version = ensureVersion(raw, configPath);
117
+ const currentModel = isRecord(raw.model) ? raw.model : {};
118
+ const nextModel = {
119
+ ...currentModel,
120
+ ...pickDefined(model)
121
+ };
122
+ const nextRaw = {
123
+ ...raw,
124
+ version,
125
+ model: nextModel
126
+ };
127
+ await writeConfigRaw(configPath, nextRaw);
128
+ return parseConfig(nextRaw, configPath);
129
+ });
108
130
  };
109
131
  var loadMcpServers = async (configPath) => {
110
132
  const config = await loadConfig(configPath);
111
133
  return config?.mcp?.servers ?? {};
112
134
  };
113
135
  var upsertMcpServerConfig = async (configPath, serverId, server) => {
114
- const raw = await readConfigRaw(configPath) ?? {};
115
- const version = ensureVersion(raw, configPath);
116
- const servers = getRawMcpServerMap(raw);
117
- servers[serverId] = server;
118
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
119
- await writeConfigRaw(configPath, nextRaw);
120
- return parseConfig(nextRaw, configPath);
136
+ return withConfigFileLock(configPath, async () => {
137
+ const raw = await readConfigRaw(configPath) ?? {};
138
+ const version = ensureVersion(raw, configPath);
139
+ const servers = getRawMcpServerMap(raw);
140
+ servers[serverId] = server;
141
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
142
+ await writeConfigRaw(configPath, nextRaw);
143
+ return parseConfig(nextRaw, configPath);
144
+ });
121
145
  };
122
146
  var removeMcpServerConfig = async (configPath, serverId) => {
123
- const raw = await readConfigRaw(configPath);
124
- if (!raw) return false;
125
- const version = ensureVersion(raw, configPath);
126
- const servers = getRawMcpServerMap(raw);
127
- if (servers[serverId] === void 0) {
128
- return false;
129
- }
130
- delete servers[serverId];
131
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
132
- await writeConfigRaw(configPath, nextRaw);
133
- return true;
147
+ return withConfigFileLock(configPath, async () => {
148
+ const raw = await readConfigRaw(configPath);
149
+ if (!raw) return false;
150
+ const version = ensureVersion(raw, configPath);
151
+ const servers = getRawMcpServerMap(raw);
152
+ if (servers[serverId] === void 0) {
153
+ return false;
154
+ }
155
+ delete servers[serverId];
156
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
157
+ await writeConfigRaw(configPath, nextRaw);
158
+ return true;
159
+ });
134
160
  };
135
161
  var setMcpServerEnabled = async (configPath, serverId, enabled) => {
136
- const raw = await readConfigRaw(configPath);
137
- if (!raw) return false;
138
- const version = ensureVersion(raw, configPath);
139
- const servers = getRawMcpServerMap(raw);
140
- if (servers[serverId] === void 0) {
141
- return false;
142
- }
143
- const current = isRecord(servers[serverId]) ? { ...servers[serverId] } : {};
144
- current.enabled = enabled;
145
- servers[serverId] = current;
146
- const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
147
- await writeConfigRaw(configPath, nextRaw);
148
- return true;
162
+ return withConfigFileLock(configPath, async () => {
163
+ const raw = await readConfigRaw(configPath);
164
+ if (!raw) return false;
165
+ const version = ensureVersion(raw, configPath);
166
+ const servers = getRawMcpServerMap(raw);
167
+ if (servers[serverId] === void 0) {
168
+ return false;
169
+ }
170
+ const current = isRecord(servers[serverId]) ? { ...servers[serverId] } : {};
171
+ current.enabled = enabled;
172
+ servers[serverId] = current;
173
+ const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
174
+ await writeConfigRaw(configPath, nextRaw);
175
+ return true;
176
+ });
149
177
  };
150
178
  var appendPermissionAllowRules = async (configPath, rules) => {
151
- const raw = await readConfigRaw(configPath) ?? {};
152
- const version = ensureVersion(raw, configPath);
153
- const permissions = isRecord(raw.permissions) ? raw.permissions : {};
154
- const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
155
- for (const rule of rules) {
156
- if (!allow.some((entry) => isSameRule(entry, rule))) {
157
- allow.push(rule);
179
+ return withConfigFileLock(configPath, async () => {
180
+ const raw = await readConfigRaw(configPath) ?? {};
181
+ const version = ensureVersion(raw, configPath);
182
+ const permissions = isRecord(raw.permissions) ? raw.permissions : {};
183
+ const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
184
+ for (const rule of rules) {
185
+ if (!allow.some((entry) => isSameRule(entry, rule))) {
186
+ allow.push(rule);
187
+ }
158
188
  }
159
- }
160
- const nextPermissions = {
161
- ...permissions,
162
- allow
163
- };
164
- const nextRaw = {
165
- ...raw,
166
- version,
167
- permissions: nextPermissions
168
- };
169
- await writeConfigRaw(configPath, nextRaw);
170
- return parseConfig(nextRaw, configPath);
189
+ const nextPermissions = {
190
+ ...permissions,
191
+ allow
192
+ };
193
+ const nextRaw = {
194
+ ...raw,
195
+ version,
196
+ permissions: nextPermissions
197
+ };
198
+ await writeConfigRaw(configPath, nextRaw);
199
+ return parseConfig(nextRaw, configPath);
200
+ });
171
201
  };
172
202
  var appendPermissionAllowRule = async (configPath, rule) => {
173
203
  return appendPermissionAllowRules(configPath, [rule]);
174
204
  };
175
205
  var updateTuiConfig = async (configPath, tui) => {
176
- const raw = await readConfigRaw(configPath) ?? {};
177
- const version = ensureVersion(raw, configPath);
178
- const currentTui = isRecord(raw.tui) ? raw.tui : {};
179
- const nextTui = {
180
- ...currentTui,
181
- ...pickDefined(tui)
182
- };
183
- const nextRaw = {
184
- ...raw,
185
- version,
186
- tui: nextTui
187
- };
188
- await writeConfigRaw(configPath, nextRaw);
189
- return parseConfig(nextRaw, configPath);
206
+ return withConfigFileLock(configPath, async () => {
207
+ const raw = await readConfigRaw(configPath) ?? {};
208
+ const version = ensureVersion(raw, configPath);
209
+ const currentTui = isRecord(raw.tui) ? raw.tui : {};
210
+ const nextTui = {
211
+ ...currentTui,
212
+ ...pickDefined(tui)
213
+ };
214
+ const nextRaw = {
215
+ ...raw,
216
+ version,
217
+ tui: nextTui
218
+ };
219
+ await writeConfigRaw(configPath, nextRaw);
220
+ return parseConfig(nextRaw, configPath);
221
+ });
190
222
  };
191
223
  var updateImageGenerationConfig = async (configPath, image) => {
192
- const raw = await readConfigRaw(configPath) ?? {};
193
- const version = ensureVersion(raw, configPath);
194
- const nextRaw = { ...raw, version, image_generation: image };
195
- const parsed = parseConfig(nextRaw, configPath);
196
- await writeConfigRaw(configPath, nextRaw);
197
- return parsed;
224
+ return withConfigFileLock(configPath, async () => {
225
+ const raw = await readConfigRaw(configPath) ?? {};
226
+ const version = ensureVersion(raw, configPath);
227
+ const nextRaw = { ...raw, version, image_generation: image };
228
+ const parsed = parseConfig(nextRaw, configPath);
229
+ await writeConfigRaw(configPath, nextRaw);
230
+ return parsed;
231
+ });
232
+ };
233
+ var updateConfigValue = async (configPath, keyPath, value) => {
234
+ return withConfigFileLock(configPath, async () => {
235
+ if (keyPath.length === 0) throw new Error("config key path is empty");
236
+ const raw = await readConfigRaw(configPath) ?? {};
237
+ const version = ensureVersion(raw, configPath);
238
+ const nextRaw = structuredClone(raw);
239
+ nextRaw.version = version;
240
+ const parents = [nextRaw];
241
+ let cursor = nextRaw;
242
+ for (const segment of keyPath.slice(0, -1)) {
243
+ const child = cursor[segment];
244
+ if (!isRecord(child)) {
245
+ if (value === void 0) return false;
246
+ if (child !== void 0) {
247
+ throw new Error(`${configPath}: ${segment} must be an object`);
248
+ }
249
+ cursor[segment] = {};
250
+ }
251
+ cursor = cursor[segment];
252
+ parents.push(cursor);
253
+ }
254
+ const leaf = keyPath[keyPath.length - 1];
255
+ if (value === void 0) {
256
+ if (!Object.hasOwn(cursor, leaf)) return false;
257
+ delete cursor[leaf];
258
+ for (let index = parents.length - 1; index > 0; index -= 1) {
259
+ const parent = parents[index - 1];
260
+ const segment = keyPath[index - 1];
261
+ if (Object.keys(parents[index]).length > 0) break;
262
+ delete parent[segment];
263
+ }
264
+ } else {
265
+ cursor[leaf] = value;
266
+ }
267
+ parseConfig(nextRaw, configPath);
268
+ await writeConfigRaw(configPath, nextRaw);
269
+ return true;
270
+ });
271
+ };
272
+ var removePermissionRules = async (configPath, kind, rule) => {
273
+ return withConfigFileLock(configPath, async () => {
274
+ const raw = await readConfigRaw(configPath);
275
+ if (!raw || !isRecord(raw.permissions)) return 0;
276
+ const version = ensureVersion(raw, configPath);
277
+ const list = raw.permissions[kind];
278
+ if (!Array.isArray(list)) return 0;
279
+ const target = normalizePermissionRule(rule);
280
+ if (!target) return 0;
281
+ const kept = list.filter((entry) => {
282
+ const normalized = normalizePermissionRule(entry);
283
+ return !(normalized && isSameRule(normalized, target));
284
+ });
285
+ const removed = list.length - kept.length;
286
+ if (removed === 0) return 0;
287
+ const permissions = { ...raw.permissions };
288
+ if (kept.length > 0) permissions[kind] = kept;
289
+ else delete permissions[kind];
290
+ const nextRaw = { ...raw, version };
291
+ if (Object.keys(permissions).length > 0) nextRaw.permissions = permissions;
292
+ else delete nextRaw.permissions;
293
+ parseConfig(nextRaw, configPath);
294
+ await writeConfigRaw(configPath, nextRaw);
295
+ return removed;
296
+ });
198
297
  };
199
298
  export {
200
299
  appendPermissionAllowRule,
@@ -202,7 +301,9 @@ export {
202
301
  loadConfig,
203
302
  loadMcpServers,
204
303
  removeMcpServerConfig,
304
+ removePermissionRules,
205
305
  setMcpServerEnabled,
306
+ updateConfigValue,
206
307
  updateImageGenerationConfig,
207
308
  updateModelConfig,
208
309
  updateTuiConfig,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codelia/config-loader",
3
- "version": "0.1.83",
3
+ "version": "0.1.84",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "files": [
@@ -21,7 +21,7 @@
21
21
  "typecheck": "tsc --noEmit"
22
22
  },
23
23
  "dependencies": {
24
- "@codelia/config": "0.1.83",
24
+ "@codelia/config": "0.1.84",
25
25
  "cosmiconfig": "^9.0.0"
26
26
  },
27
27
  "publishConfig": {