mr-memory 3.7.2 → 3.7.4

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/index.js +41 -4
  2. package/index.ts +44 -4
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -7,8 +7,9 @@
7
7
  *
8
8
  * BYOK — provider API keys never touch MemoryRouter.
9
9
  */
10
- import { readFile, lstat } from "node:fs/promises";
11
- import { join, resolve, relative, isAbsolute, sep } from "node:path";
10
+ import { readFile, lstat, writeFile, copyFile, mkdir } from "node:fs/promises";
11
+ import { join, resolve, relative, isAbsolute, sep, dirname } from "node:path";
12
+ import os from "node:os";
12
13
  const DEFAULT_ENDPOINT = "https://api.memoryrouter.ai";
13
14
  /** Strip media-attached references and OpenClaw media instruction text so
14
15
  * old screenshots/photos/audio don't get stored and re-injected forever. */
@@ -267,13 +268,49 @@ const WORKSPACE_FILES = [
267
268
  // ──────────────────────────────────────────────────────
268
269
  // Helpers
269
270
  // ──────────────────────────────────────────────────────
271
+ function openClawConfigPath() {
272
+ return process.env.OPENCLAW_CONFIG || join(os.homedir(), ".openclaw", "openclaw.json");
273
+ }
274
+ async function readOpenClawConfigFile() {
275
+ try {
276
+ return JSON.parse(await readFile(openClawConfigPath(), "utf8"));
277
+ }
278
+ catch (err) {
279
+ if (err?.code === "ENOENT")
280
+ return {};
281
+ throw err;
282
+ }
283
+ }
284
+ async function writeOpenClawConfigFile(config) {
285
+ const configPath = openClawConfigPath();
286
+ await mkdir(dirname(configPath), { recursive: true });
287
+ try {
288
+ await copyFile(configPath, `${configPath}.bak`);
289
+ }
290
+ catch (err) {
291
+ if (err?.code !== "ENOENT")
292
+ throw err;
293
+ }
294
+ await writeFile(configPath, `${JSON.stringify(config, null, 2)}\n`, "utf8");
295
+ }
296
+ async function updateOpenClawPluginEntry(api, patch) {
297
+ const config = await readOpenClawConfigFile();
298
+ config.plugins ??= {};
299
+ config.plugins.entries ??= {};
300
+ config.plugins.entries[api.id] ??= {};
301
+ config.plugins.entries[api.id] = {
302
+ ...config.plugins.entries[api.id],
303
+ ...patch,
304
+ };
305
+ await writeOpenClawConfigFile(config);
306
+ }
270
307
  async function setPluginConfig(api, config) {
271
308
  const compat = api;
272
309
  if (typeof compat.updatePluginConfig === "function") {
273
310
  await compat.updatePluginConfig(config);
274
311
  return;
275
312
  }
276
- throw new Error("This OpenClaw version does not expose plugin config APIs. Configure mr-memory manually or update OpenClaw.");
313
+ await updateOpenClawPluginEntry(api, { config });
277
314
  }
278
315
  async function setPluginEnabled(api, enabled) {
279
316
  const compat = api;
@@ -281,7 +318,7 @@ async function setPluginEnabled(api, enabled) {
281
318
  await compat.updatePluginEnabled(enabled);
282
319
  return;
283
320
  }
284
- throw new Error("This OpenClaw version does not expose plugin enable APIs. Enable mr-memory manually or update OpenClaw.");
321
+ await updateOpenClawPluginEntry(api, { enabled });
285
322
  }
286
323
  /**
287
324
  * Read all workspace files and return as a single text blob for token counting.
package/index.ts CHANGED
@@ -8,9 +8,10 @@
8
8
  * BYOK — provider API keys never touch MemoryRouter.
9
9
  */
10
10
 
11
- import { readFile, readdir, stat, lstat } from "node:fs/promises";
12
- import { join, resolve, relative, isAbsolute, sep } from "node:path";
11
+ import { readFile, readdir, stat, lstat, writeFile, copyFile, mkdir } from "node:fs/promises";
12
+ import { join, resolve, relative, isAbsolute, sep, dirname } from "node:path";
13
13
  import path from "node:path";
14
+ import os from "node:os";
14
15
  import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
15
16
 
16
17
  const DEFAULT_ENDPOINT = "https://api.memoryrouter.ai";
@@ -291,13 +292,52 @@ type CompatApi = OpenClawPluginApi & {
291
292
  updatePluginEnabled?: (enabled: boolean) => Promise<void>;
292
293
  };
293
294
 
295
+ type JsonObject = Record<string, any>;
296
+
297
+ function openClawConfigPath(): string {
298
+ return process.env.OPENCLAW_CONFIG || join(os.homedir(), ".openclaw", "openclaw.json");
299
+ }
300
+
301
+ async function readOpenClawConfigFile(): Promise<JsonObject> {
302
+ try {
303
+ return JSON.parse(await readFile(openClawConfigPath(), "utf8"));
304
+ } catch (err: any) {
305
+ if (err?.code === "ENOENT") return {};
306
+ throw err;
307
+ }
308
+ }
309
+
310
+ async function writeOpenClawConfigFile(config: JsonObject): Promise<void> {
311
+ const configPath = openClawConfigPath();
312
+ await mkdir(dirname(configPath), { recursive: true });
313
+ try {
314
+ await copyFile(configPath, `${configPath}.bak`);
315
+ } catch (err: any) {
316
+ if (err?.code !== "ENOENT") throw err;
317
+ }
318
+ await writeFile(configPath, `${JSON.stringify(config, null, 2)}
319
+ `, "utf8");
320
+ }
321
+
322
+ async function updateOpenClawPluginEntry(api: OpenClawPluginApi, patch: JsonObject): Promise<void> {
323
+ const config = await readOpenClawConfigFile();
324
+ config.plugins ??= {};
325
+ config.plugins.entries ??= {};
326
+ config.plugins.entries[api.id] ??= {};
327
+ config.plugins.entries[api.id] = {
328
+ ...config.plugins.entries[api.id],
329
+ ...patch,
330
+ };
331
+ await writeOpenClawConfigFile(config);
332
+ }
333
+
294
334
  async function setPluginConfig(api: OpenClawPluginApi, config: Record<string, unknown>): Promise<void> {
295
335
  const compat = api as CompatApi;
296
336
  if (typeof compat.updatePluginConfig === "function") {
297
337
  await compat.updatePluginConfig(config);
298
338
  return;
299
339
  }
300
- throw new Error("This OpenClaw version does not expose plugin config APIs. Configure mr-memory manually or update OpenClaw.");
340
+ await updateOpenClawPluginEntry(api, { config });
301
341
  }
302
342
 
303
343
  async function setPluginEnabled(api: OpenClawPluginApi, enabled: boolean): Promise<void> {
@@ -306,7 +346,7 @@ async function setPluginEnabled(api: OpenClawPluginApi, enabled: boolean): Promi
306
346
  await compat.updatePluginEnabled(enabled);
307
347
  return;
308
348
  }
309
- throw new Error("This OpenClaw version does not expose plugin enable APIs. Enable mr-memory manually or update OpenClaw.");
349
+ await updateOpenClawPluginEntry(api, { enabled });
310
350
  }
311
351
 
312
352
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mr-memory",
3
- "version": "3.7.2",
3
+ "version": "3.7.4",
4
4
  "description": "mr-memory is the MemoryRouter plugin for OpenClaw — persistent memory across every conversation",
5
5
  "type": "module",
6
6
  "files": [