@codelia/config-loader 0.1.90 → 0.1.93

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.
Files changed (3) hide show
  1. package/dist/index.cjs +119 -62
  2. package/dist/index.js +117 -60
  3. package/package.json +3 -2
package/dist/index.cjs CHANGED
@@ -46,37 +46,94 @@ __export(index_exports, {
46
46
  upsertMcpServerConfig: () => upsertMcpServerConfig
47
47
  });
48
48
  module.exports = __toCommonJS(index_exports);
49
- var import_promises = require("fs/promises");
50
- var import_node_path = __toESM(require("path"), 1);
49
+ var import_promises3 = require("fs/promises");
50
+ var import_node_path2 = __toESM(require("path"), 1);
51
51
  var import_config = require("@codelia/config");
52
52
  var import_cosmiconfig = require("cosmiconfig");
53
- var MODULE_NAME = "codelia";
54
- var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
55
- var isMissingFileError = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
56
- var pickDefined = (value) => Object.fromEntries(
57
- Object.entries(value).filter(([, entry]) => entry !== void 0)
58
- );
59
- var pickString = (value) => typeof value === "string" ? value : void 0;
60
- var configWriteQueues = /* @__PURE__ */ new Map();
53
+
54
+ // src/config-file-lock.ts
55
+ var import_promises = require("fs/promises");
56
+ var import_node_path = __toESM(require("path"), 1);
57
+ var import_promises2 = require("timers/promises");
58
+ var LOCK_TIMEOUT_MS = 5e3;
59
+ var queues = /* @__PURE__ */ new Map();
60
+ var canonicalConfigPath = async (configPath, remainingLinks = 40) => {
61
+ const absolute = import_node_path.default.resolve(configPath);
62
+ try {
63
+ return await (0, import_promises.realpath)(absolute);
64
+ } catch (error) {
65
+ if (error.code !== "ENOENT") throw error;
66
+ }
67
+ let link;
68
+ try {
69
+ link = await (0, import_promises.readlink)(absolute);
70
+ } catch (error) {
71
+ const code = error.code;
72
+ if (code !== "ENOENT" && code !== "EINVAL") throw error;
73
+ const parent = import_node_path.default.dirname(absolute);
74
+ await (0, import_promises.mkdir)(parent, { recursive: true });
75
+ return import_node_path.default.join(await (0, import_promises.realpath)(parent), import_node_path.default.basename(absolute));
76
+ }
77
+ if (remainingLinks === 0) {
78
+ throw new Error(`Too many symlinks for config file: ${configPath}`);
79
+ }
80
+ return canonicalConfigPath(
81
+ import_node_path.default.resolve(import_node_path.default.dirname(absolute), link),
82
+ remainingLinks - 1
83
+ );
84
+ };
85
+ var withProcessLock = async (file, fn) => {
86
+ const { openSqlite } = await import("@codelia/storage");
87
+ const db = await openSqlite(`${file}.lock.sqlite`);
88
+ try {
89
+ db.exec("PRAGMA busy_timeout = 0");
90
+ const deadline = performance.now() + LOCK_TIMEOUT_MS;
91
+ for (; ; ) {
92
+ try {
93
+ db.exec("BEGIN IMMEDIATE");
94
+ break;
95
+ } catch (error) {
96
+ const code = error?.code;
97
+ if (code !== "SQLITE_BUSY" && code !== "SQLITE_LOCKED") throw error;
98
+ if (performance.now() >= deadline) {
99
+ throw new Error(`Timed out locking config file: ${file}`);
100
+ }
101
+ await (0, import_promises2.setTimeout)(20);
102
+ }
103
+ }
104
+ return await fn();
105
+ } finally {
106
+ db.close();
107
+ }
108
+ };
61
109
  var withConfigFileLock = async (configPath, fn) => {
62
- const key = import_node_path.default.resolve(configPath);
63
- const previous = configWriteQueues.get(key) ?? Promise.resolve();
64
- const next = previous.then(fn, fn);
110
+ const key = await canonicalConfigPath(configPath);
111
+ const previous = queues.get(key) ?? Promise.resolve();
112
+ const next = previous.then(() => withProcessLock(key, () => fn(key)));
65
113
  const settled = next.then(
66
114
  () => void 0,
67
115
  () => void 0
68
116
  );
69
- configWriteQueues.set(key, settled);
117
+ queues.set(key, settled);
70
118
  try {
71
119
  return await next;
72
120
  } finally {
73
- if (configWriteQueues.get(key) === settled) configWriteQueues.delete(key);
121
+ if (queues.get(key) === settled) queues.delete(key);
74
122
  }
75
123
  };
124
+
125
+ // src/index.ts
126
+ var MODULE_NAME = "codelia";
127
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
128
+ var isMissingFileError = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
129
+ var pickDefined = (value) => Object.fromEntries(
130
+ Object.entries(value).filter(([, entry]) => entry !== void 0)
131
+ );
132
+ var pickString = (value) => typeof value === "string" ? value : void 0;
76
133
  var readConfigRaw = async (configPath) => {
77
134
  let raw;
78
135
  try {
79
- raw = await (0, import_promises.readFile)(configPath, "utf8");
136
+ raw = await (0, import_promises3.readFile)(configPath, "utf8");
80
137
  } catch (error) {
81
138
  if (isMissingFileError(error)) return null;
82
139
  throw error;
@@ -101,8 +158,8 @@ var ensureVersion = (raw, configPath) => {
101
158
  return version;
102
159
  };
103
160
  var writeConfigRaw = async (configPath, raw) => {
104
- await (0, import_promises.mkdir)(import_node_path.default.dirname(configPath), { recursive: true });
105
- await (0, import_promises.writeFile)(configPath, `${JSON.stringify(raw, null, 2)}
161
+ await (0, import_promises3.mkdir)(import_node_path2.default.dirname(configPath), { recursive: true });
162
+ await (0, import_promises3.writeFile)(configPath, `${JSON.stringify(raw, null, 2)}
106
163
  `, "utf8");
107
164
  };
108
165
  var getRawMcpServerMap = (raw) => {
@@ -154,9 +211,9 @@ var loadConfig = async (configPath) => {
154
211
  return (0, import_config.parseConfig)(result.config, result.filepath);
155
212
  };
156
213
  var updateModelConfig = async (configPath, model) => {
157
- return withConfigFileLock(configPath, async () => {
158
- const raw = await readConfigRaw(configPath) ?? {};
159
- const version = ensureVersion(raw, configPath);
214
+ return withConfigFileLock(configPath, async (configPath2) => {
215
+ const raw = await readConfigRaw(configPath2) ?? {};
216
+ const version = ensureVersion(raw, configPath2);
160
217
  const currentModel = isRecord(raw.model) ? raw.model : {};
161
218
  const nextModel = {
162
219
  ...currentModel,
@@ -167,8 +224,8 @@ var updateModelConfig = async (configPath, model) => {
167
224
  version,
168
225
  model: nextModel
169
226
  };
170
- await writeConfigRaw(configPath, nextRaw);
171
- return (0, import_config.parseConfig)(nextRaw, configPath);
227
+ await writeConfigRaw(configPath2, nextRaw);
228
+ return (0, import_config.parseConfig)(nextRaw, configPath2);
172
229
  });
173
230
  };
174
231
  var loadMcpServers = async (configPath) => {
@@ -176,36 +233,36 @@ var loadMcpServers = async (configPath) => {
176
233
  return config?.mcp?.servers ?? {};
177
234
  };
178
235
  var upsertMcpServerConfig = async (configPath, serverId, server) => {
179
- return withConfigFileLock(configPath, async () => {
180
- const raw = await readConfigRaw(configPath) ?? {};
181
- const version = ensureVersion(raw, configPath);
236
+ return withConfigFileLock(configPath, async (configPath2) => {
237
+ const raw = await readConfigRaw(configPath2) ?? {};
238
+ const version = ensureVersion(raw, configPath2);
182
239
  const servers = getRawMcpServerMap(raw);
183
240
  servers[serverId] = server;
184
241
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
185
- await writeConfigRaw(configPath, nextRaw);
186
- return (0, import_config.parseConfig)(nextRaw, configPath);
242
+ await writeConfigRaw(configPath2, nextRaw);
243
+ return (0, import_config.parseConfig)(nextRaw, configPath2);
187
244
  });
188
245
  };
189
246
  var removeMcpServerConfig = async (configPath, serverId) => {
190
- return withConfigFileLock(configPath, async () => {
191
- const raw = await readConfigRaw(configPath);
247
+ return withConfigFileLock(configPath, async (configPath2) => {
248
+ const raw = await readConfigRaw(configPath2);
192
249
  if (!raw) return false;
193
- const version = ensureVersion(raw, configPath);
250
+ const version = ensureVersion(raw, configPath2);
194
251
  const servers = getRawMcpServerMap(raw);
195
252
  if (servers[serverId] === void 0) {
196
253
  return false;
197
254
  }
198
255
  delete servers[serverId];
199
256
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
200
- await writeConfigRaw(configPath, nextRaw);
257
+ await writeConfigRaw(configPath2, nextRaw);
201
258
  return true;
202
259
  });
203
260
  };
204
261
  var setMcpServerEnabled = async (configPath, serverId, enabled) => {
205
- return withConfigFileLock(configPath, async () => {
206
- const raw = await readConfigRaw(configPath);
262
+ return withConfigFileLock(configPath, async (configPath2) => {
263
+ const raw = await readConfigRaw(configPath2);
207
264
  if (!raw) return false;
208
- const version = ensureVersion(raw, configPath);
265
+ const version = ensureVersion(raw, configPath2);
209
266
  const servers = getRawMcpServerMap(raw);
210
267
  if (servers[serverId] === void 0) {
211
268
  return false;
@@ -214,14 +271,14 @@ var setMcpServerEnabled = async (configPath, serverId, enabled) => {
214
271
  current.enabled = enabled;
215
272
  servers[serverId] = current;
216
273
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
217
- await writeConfigRaw(configPath, nextRaw);
274
+ await writeConfigRaw(configPath2, nextRaw);
218
275
  return true;
219
276
  });
220
277
  };
221
278
  var appendPermissionAllowRules = async (configPath, rules) => {
222
- return withConfigFileLock(configPath, async () => {
223
- const raw = await readConfigRaw(configPath) ?? {};
224
- const version = ensureVersion(raw, configPath);
279
+ return withConfigFileLock(configPath, async (configPath2) => {
280
+ const raw = await readConfigRaw(configPath2) ?? {};
281
+ const version = ensureVersion(raw, configPath2);
225
282
  const permissions = isRecord(raw.permissions) ? raw.permissions : {};
226
283
  const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
227
284
  for (const rule of rules) {
@@ -238,17 +295,17 @@ var appendPermissionAllowRules = async (configPath, rules) => {
238
295
  version,
239
296
  permissions: nextPermissions
240
297
  };
241
- await writeConfigRaw(configPath, nextRaw);
242
- return (0, import_config.parseConfig)(nextRaw, configPath);
298
+ await writeConfigRaw(configPath2, nextRaw);
299
+ return (0, import_config.parseConfig)(nextRaw, configPath2);
243
300
  });
244
301
  };
245
302
  var appendPermissionAllowRule = async (configPath, rule) => {
246
303
  return appendPermissionAllowRules(configPath, [rule]);
247
304
  };
248
305
  var updateTuiConfig = async (configPath, tui) => {
249
- return withConfigFileLock(configPath, async () => {
250
- const raw = await readConfigRaw(configPath) ?? {};
251
- const version = ensureVersion(raw, configPath);
306
+ return withConfigFileLock(configPath, async (configPath2) => {
307
+ const raw = await readConfigRaw(configPath2) ?? {};
308
+ const version = ensureVersion(raw, configPath2);
252
309
  const currentTui = isRecord(raw.tui) ? raw.tui : {};
253
310
  const nextTui = {
254
311
  ...currentTui,
@@ -259,25 +316,25 @@ var updateTuiConfig = async (configPath, tui) => {
259
316
  version,
260
317
  tui: nextTui
261
318
  };
262
- await writeConfigRaw(configPath, nextRaw);
263
- return (0, import_config.parseConfig)(nextRaw, configPath);
319
+ await writeConfigRaw(configPath2, nextRaw);
320
+ return (0, import_config.parseConfig)(nextRaw, configPath2);
264
321
  });
265
322
  };
266
323
  var updateImageGenerationConfig = async (configPath, image) => {
267
- return withConfigFileLock(configPath, async () => {
268
- const raw = await readConfigRaw(configPath) ?? {};
269
- const version = ensureVersion(raw, configPath);
324
+ return withConfigFileLock(configPath, async (configPath2) => {
325
+ const raw = await readConfigRaw(configPath2) ?? {};
326
+ const version = ensureVersion(raw, configPath2);
270
327
  const nextRaw = { ...raw, version, image_generation: image };
271
- const parsed = (0, import_config.parseConfig)(nextRaw, configPath);
272
- await writeConfigRaw(configPath, nextRaw);
328
+ const parsed = (0, import_config.parseConfig)(nextRaw, configPath2);
329
+ await writeConfigRaw(configPath2, nextRaw);
273
330
  return parsed;
274
331
  });
275
332
  };
276
333
  var updateConfigValue = async (configPath, keyPath, value) => {
277
- return withConfigFileLock(configPath, async () => {
334
+ return withConfigFileLock(configPath, async (configPath2) => {
278
335
  if (keyPath.length === 0) throw new Error("config key path is empty");
279
- const raw = await readConfigRaw(configPath) ?? {};
280
- const version = ensureVersion(raw, configPath);
336
+ const raw = await readConfigRaw(configPath2) ?? {};
337
+ const version = ensureVersion(raw, configPath2);
281
338
  const nextRaw = structuredClone(raw);
282
339
  nextRaw.version = version;
283
340
  const parents = [nextRaw];
@@ -287,7 +344,7 @@ var updateConfigValue = async (configPath, keyPath, value) => {
287
344
  if (!isRecord(child)) {
288
345
  if (value === void 0) return false;
289
346
  if (child !== void 0) {
290
- throw new Error(`${configPath}: ${segment} must be an object`);
347
+ throw new Error(`${configPath2}: ${segment} must be an object`);
291
348
  }
292
349
  cursor[segment] = {};
293
350
  }
@@ -307,16 +364,16 @@ var updateConfigValue = async (configPath, keyPath, value) => {
307
364
  } else {
308
365
  cursor[leaf] = value;
309
366
  }
310
- (0, import_config.parseConfig)(nextRaw, configPath);
311
- await writeConfigRaw(configPath, nextRaw);
367
+ (0, import_config.parseConfig)(nextRaw, configPath2);
368
+ await writeConfigRaw(configPath2, nextRaw);
312
369
  return true;
313
370
  });
314
371
  };
315
372
  var removePermissionRules = async (configPath, kind, rule) => {
316
- return withConfigFileLock(configPath, async () => {
317
- const raw = await readConfigRaw(configPath);
373
+ return withConfigFileLock(configPath, async (configPath2) => {
374
+ const raw = await readConfigRaw(configPath2);
318
375
  if (!raw || !isRecord(raw.permissions)) return 0;
319
- const version = ensureVersion(raw, configPath);
376
+ const version = ensureVersion(raw, configPath2);
320
377
  const list = raw.permissions[kind];
321
378
  if (!Array.isArray(list)) return 0;
322
379
  const target = (0, import_config.normalizePermissionRule)(rule);
@@ -333,8 +390,8 @@ var removePermissionRules = async (configPath, kind, rule) => {
333
390
  const nextRaw = { ...raw, version };
334
391
  if (Object.keys(permissions).length > 0) nextRaw.permissions = permissions;
335
392
  else delete nextRaw.permissions;
336
- (0, import_config.parseConfig)(nextRaw, configPath);
337
- await writeConfigRaw(configPath, nextRaw);
393
+ (0, import_config.parseConfig)(nextRaw, configPath2);
394
+ await writeConfigRaw(configPath2, nextRaw);
338
395
  return removed;
339
396
  });
340
397
  };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
- import { mkdir, readFile, writeFile } from "fs/promises";
3
- import path from "path";
2
+ import { mkdir as mkdir2, readFile, writeFile } from "fs/promises";
3
+ import path2 from "path";
4
4
  import {
5
5
  CONFIG_VERSION,
6
6
  isValidUpdateChannel,
@@ -9,29 +9,86 @@ import {
9
9
  parseUpdateMode
10
10
  } from "@codelia/config";
11
11
  import { cosmiconfig } from "cosmiconfig";
12
- var MODULE_NAME = "codelia";
13
- var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
14
- var isMissingFileError = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
15
- var pickDefined = (value) => Object.fromEntries(
16
- Object.entries(value).filter(([, entry]) => entry !== void 0)
17
- );
18
- var pickString = (value) => typeof value === "string" ? value : void 0;
19
- var configWriteQueues = /* @__PURE__ */ new Map();
12
+
13
+ // src/config-file-lock.ts
14
+ import { mkdir, readlink, realpath } from "fs/promises";
15
+ import path from "path";
16
+ import { setTimeout as delay } from "timers/promises";
17
+ var LOCK_TIMEOUT_MS = 5e3;
18
+ var queues = /* @__PURE__ */ new Map();
19
+ var canonicalConfigPath = async (configPath, remainingLinks = 40) => {
20
+ const absolute = path.resolve(configPath);
21
+ try {
22
+ return await realpath(absolute);
23
+ } catch (error) {
24
+ if (error.code !== "ENOENT") throw error;
25
+ }
26
+ let link;
27
+ try {
28
+ link = await readlink(absolute);
29
+ } catch (error) {
30
+ const code = error.code;
31
+ if (code !== "ENOENT" && code !== "EINVAL") throw error;
32
+ const parent = path.dirname(absolute);
33
+ await mkdir(parent, { recursive: true });
34
+ return path.join(await realpath(parent), path.basename(absolute));
35
+ }
36
+ if (remainingLinks === 0) {
37
+ throw new Error(`Too many symlinks for config file: ${configPath}`);
38
+ }
39
+ return canonicalConfigPath(
40
+ path.resolve(path.dirname(absolute), link),
41
+ remainingLinks - 1
42
+ );
43
+ };
44
+ var withProcessLock = async (file, fn) => {
45
+ const { openSqlite } = await import("@codelia/storage");
46
+ const db = await openSqlite(`${file}.lock.sqlite`);
47
+ try {
48
+ db.exec("PRAGMA busy_timeout = 0");
49
+ const deadline = performance.now() + LOCK_TIMEOUT_MS;
50
+ for (; ; ) {
51
+ try {
52
+ db.exec("BEGIN IMMEDIATE");
53
+ break;
54
+ } catch (error) {
55
+ const code = error?.code;
56
+ if (code !== "SQLITE_BUSY" && code !== "SQLITE_LOCKED") throw error;
57
+ if (performance.now() >= deadline) {
58
+ throw new Error(`Timed out locking config file: ${file}`);
59
+ }
60
+ await delay(20);
61
+ }
62
+ }
63
+ return await fn();
64
+ } finally {
65
+ db.close();
66
+ }
67
+ };
20
68
  var withConfigFileLock = async (configPath, fn) => {
21
- const key = path.resolve(configPath);
22
- const previous = configWriteQueues.get(key) ?? Promise.resolve();
23
- const next = previous.then(fn, fn);
69
+ const key = await canonicalConfigPath(configPath);
70
+ const previous = queues.get(key) ?? Promise.resolve();
71
+ const next = previous.then(() => withProcessLock(key, () => fn(key)));
24
72
  const settled = next.then(
25
73
  () => void 0,
26
74
  () => void 0
27
75
  );
28
- configWriteQueues.set(key, settled);
76
+ queues.set(key, settled);
29
77
  try {
30
78
  return await next;
31
79
  } finally {
32
- if (configWriteQueues.get(key) === settled) configWriteQueues.delete(key);
80
+ if (queues.get(key) === settled) queues.delete(key);
33
81
  }
34
82
  };
83
+
84
+ // src/index.ts
85
+ var MODULE_NAME = "codelia";
86
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
87
+ var isMissingFileError = (error) => typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
88
+ var pickDefined = (value) => Object.fromEntries(
89
+ Object.entries(value).filter(([, entry]) => entry !== void 0)
90
+ );
91
+ var pickString = (value) => typeof value === "string" ? value : void 0;
35
92
  var readConfigRaw = async (configPath) => {
36
93
  let raw;
37
94
  try {
@@ -60,7 +117,7 @@ var ensureVersion = (raw, configPath) => {
60
117
  return version;
61
118
  };
62
119
  var writeConfigRaw = async (configPath, raw) => {
63
- await mkdir(path.dirname(configPath), { recursive: true });
120
+ await mkdir2(path2.dirname(configPath), { recursive: true });
64
121
  await writeFile(configPath, `${JSON.stringify(raw, null, 2)}
65
122
  `, "utf8");
66
123
  };
@@ -113,9 +170,9 @@ var loadConfig = async (configPath) => {
113
170
  return parseConfig(result.config, result.filepath);
114
171
  };
115
172
  var updateModelConfig = async (configPath, model) => {
116
- return withConfigFileLock(configPath, async () => {
117
- const raw = await readConfigRaw(configPath) ?? {};
118
- const version = ensureVersion(raw, configPath);
173
+ return withConfigFileLock(configPath, async (configPath2) => {
174
+ const raw = await readConfigRaw(configPath2) ?? {};
175
+ const version = ensureVersion(raw, configPath2);
119
176
  const currentModel = isRecord(raw.model) ? raw.model : {};
120
177
  const nextModel = {
121
178
  ...currentModel,
@@ -126,8 +183,8 @@ var updateModelConfig = async (configPath, model) => {
126
183
  version,
127
184
  model: nextModel
128
185
  };
129
- await writeConfigRaw(configPath, nextRaw);
130
- return parseConfig(nextRaw, configPath);
186
+ await writeConfigRaw(configPath2, nextRaw);
187
+ return parseConfig(nextRaw, configPath2);
131
188
  });
132
189
  };
133
190
  var loadMcpServers = async (configPath) => {
@@ -135,36 +192,36 @@ var loadMcpServers = async (configPath) => {
135
192
  return config?.mcp?.servers ?? {};
136
193
  };
137
194
  var upsertMcpServerConfig = async (configPath, serverId, server) => {
138
- return withConfigFileLock(configPath, async () => {
139
- const raw = await readConfigRaw(configPath) ?? {};
140
- const version = ensureVersion(raw, configPath);
195
+ return withConfigFileLock(configPath, async (configPath2) => {
196
+ const raw = await readConfigRaw(configPath2) ?? {};
197
+ const version = ensureVersion(raw, configPath2);
141
198
  const servers = getRawMcpServerMap(raw);
142
199
  servers[serverId] = server;
143
200
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
144
- await writeConfigRaw(configPath, nextRaw);
145
- return parseConfig(nextRaw, configPath);
201
+ await writeConfigRaw(configPath2, nextRaw);
202
+ return parseConfig(nextRaw, configPath2);
146
203
  });
147
204
  };
148
205
  var removeMcpServerConfig = async (configPath, serverId) => {
149
- return withConfigFileLock(configPath, async () => {
150
- const raw = await readConfigRaw(configPath);
206
+ return withConfigFileLock(configPath, async (configPath2) => {
207
+ const raw = await readConfigRaw(configPath2);
151
208
  if (!raw) return false;
152
- const version = ensureVersion(raw, configPath);
209
+ const version = ensureVersion(raw, configPath2);
153
210
  const servers = getRawMcpServerMap(raw);
154
211
  if (servers[serverId] === void 0) {
155
212
  return false;
156
213
  }
157
214
  delete servers[serverId];
158
215
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
159
- await writeConfigRaw(configPath, nextRaw);
216
+ await writeConfigRaw(configPath2, nextRaw);
160
217
  return true;
161
218
  });
162
219
  };
163
220
  var setMcpServerEnabled = async (configPath, serverId, enabled) => {
164
- return withConfigFileLock(configPath, async () => {
165
- const raw = await readConfigRaw(configPath);
221
+ return withConfigFileLock(configPath, async (configPath2) => {
222
+ const raw = await readConfigRaw(configPath2);
166
223
  if (!raw) return false;
167
- const version = ensureVersion(raw, configPath);
224
+ const version = ensureVersion(raw, configPath2);
168
225
  const servers = getRawMcpServerMap(raw);
169
226
  if (servers[serverId] === void 0) {
170
227
  return false;
@@ -173,14 +230,14 @@ var setMcpServerEnabled = async (configPath, serverId, enabled) => {
173
230
  current.enabled = enabled;
174
231
  servers[serverId] = current;
175
232
  const nextRaw = setRawMcpServerMap({ ...raw, version }, servers);
176
- await writeConfigRaw(configPath, nextRaw);
233
+ await writeConfigRaw(configPath2, nextRaw);
177
234
  return true;
178
235
  });
179
236
  };
180
237
  var appendPermissionAllowRules = async (configPath, rules) => {
181
- return withConfigFileLock(configPath, async () => {
182
- const raw = await readConfigRaw(configPath) ?? {};
183
- const version = ensureVersion(raw, configPath);
238
+ return withConfigFileLock(configPath, async (configPath2) => {
239
+ const raw = await readConfigRaw(configPath2) ?? {};
240
+ const version = ensureVersion(raw, configPath2);
184
241
  const permissions = isRecord(raw.permissions) ? raw.permissions : {};
185
242
  const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
186
243
  for (const rule of rules) {
@@ -197,17 +254,17 @@ var appendPermissionAllowRules = async (configPath, rules) => {
197
254
  version,
198
255
  permissions: nextPermissions
199
256
  };
200
- await writeConfigRaw(configPath, nextRaw);
201
- return parseConfig(nextRaw, configPath);
257
+ await writeConfigRaw(configPath2, nextRaw);
258
+ return parseConfig(nextRaw, configPath2);
202
259
  });
203
260
  };
204
261
  var appendPermissionAllowRule = async (configPath, rule) => {
205
262
  return appendPermissionAllowRules(configPath, [rule]);
206
263
  };
207
264
  var updateTuiConfig = async (configPath, tui) => {
208
- return withConfigFileLock(configPath, async () => {
209
- const raw = await readConfigRaw(configPath) ?? {};
210
- const version = ensureVersion(raw, configPath);
265
+ return withConfigFileLock(configPath, async (configPath2) => {
266
+ const raw = await readConfigRaw(configPath2) ?? {};
267
+ const version = ensureVersion(raw, configPath2);
211
268
  const currentTui = isRecord(raw.tui) ? raw.tui : {};
212
269
  const nextTui = {
213
270
  ...currentTui,
@@ -218,25 +275,25 @@ var updateTuiConfig = async (configPath, tui) => {
218
275
  version,
219
276
  tui: nextTui
220
277
  };
221
- await writeConfigRaw(configPath, nextRaw);
222
- return parseConfig(nextRaw, configPath);
278
+ await writeConfigRaw(configPath2, nextRaw);
279
+ return parseConfig(nextRaw, configPath2);
223
280
  });
224
281
  };
225
282
  var updateImageGenerationConfig = async (configPath, image) => {
226
- return withConfigFileLock(configPath, async () => {
227
- const raw = await readConfigRaw(configPath) ?? {};
228
- const version = ensureVersion(raw, configPath);
283
+ return withConfigFileLock(configPath, async (configPath2) => {
284
+ const raw = await readConfigRaw(configPath2) ?? {};
285
+ const version = ensureVersion(raw, configPath2);
229
286
  const nextRaw = { ...raw, version, image_generation: image };
230
- const parsed = parseConfig(nextRaw, configPath);
231
- await writeConfigRaw(configPath, nextRaw);
287
+ const parsed = parseConfig(nextRaw, configPath2);
288
+ await writeConfigRaw(configPath2, nextRaw);
232
289
  return parsed;
233
290
  });
234
291
  };
235
292
  var updateConfigValue = async (configPath, keyPath, value) => {
236
- return withConfigFileLock(configPath, async () => {
293
+ return withConfigFileLock(configPath, async (configPath2) => {
237
294
  if (keyPath.length === 0) throw new Error("config key path is empty");
238
- const raw = await readConfigRaw(configPath) ?? {};
239
- const version = ensureVersion(raw, configPath);
295
+ const raw = await readConfigRaw(configPath2) ?? {};
296
+ const version = ensureVersion(raw, configPath2);
240
297
  const nextRaw = structuredClone(raw);
241
298
  nextRaw.version = version;
242
299
  const parents = [nextRaw];
@@ -246,7 +303,7 @@ var updateConfigValue = async (configPath, keyPath, value) => {
246
303
  if (!isRecord(child)) {
247
304
  if (value === void 0) return false;
248
305
  if (child !== void 0) {
249
- throw new Error(`${configPath}: ${segment} must be an object`);
306
+ throw new Error(`${configPath2}: ${segment} must be an object`);
250
307
  }
251
308
  cursor[segment] = {};
252
309
  }
@@ -266,16 +323,16 @@ var updateConfigValue = async (configPath, keyPath, value) => {
266
323
  } else {
267
324
  cursor[leaf] = value;
268
325
  }
269
- parseConfig(nextRaw, configPath);
270
- await writeConfigRaw(configPath, nextRaw);
326
+ parseConfig(nextRaw, configPath2);
327
+ await writeConfigRaw(configPath2, nextRaw);
271
328
  return true;
272
329
  });
273
330
  };
274
331
  var removePermissionRules = async (configPath, kind, rule) => {
275
- return withConfigFileLock(configPath, async () => {
276
- const raw = await readConfigRaw(configPath);
332
+ return withConfigFileLock(configPath, async (configPath2) => {
333
+ const raw = await readConfigRaw(configPath2);
277
334
  if (!raw || !isRecord(raw.permissions)) return 0;
278
- const version = ensureVersion(raw, configPath);
335
+ const version = ensureVersion(raw, configPath2);
279
336
  const list = raw.permissions[kind];
280
337
  if (!Array.isArray(list)) return 0;
281
338
  const target = normalizePermissionRule(rule);
@@ -292,8 +349,8 @@ var removePermissionRules = async (configPath, kind, rule) => {
292
349
  const nextRaw = { ...raw, version };
293
350
  if (Object.keys(permissions).length > 0) nextRaw.permissions = permissions;
294
351
  else delete nextRaw.permissions;
295
- parseConfig(nextRaw, configPath);
296
- await writeConfigRaw(configPath, nextRaw);
352
+ parseConfig(nextRaw, configPath2);
353
+ await writeConfigRaw(configPath2, nextRaw);
297
354
  return removed;
298
355
  });
299
356
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codelia/config-loader",
3
- "version": "0.1.90",
3
+ "version": "0.1.93",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "files": [
@@ -21,7 +21,8 @@
21
21
  "typecheck": "tsc --noEmit"
22
22
  },
23
23
  "dependencies": {
24
- "@codelia/config": "0.1.90",
24
+ "@codelia/config": "0.1.93",
25
+ "@codelia/storage": "0.1.93",
25
26
  "cosmiconfig": "^9.0.0"
26
27
  },
27
28
  "publishConfig": {