From 49788fc085dae6310e02990b521402e147338d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=86=A0=E8=BE=B0?= Date: Mon, 25 May 2026 00:10:57 +0800 Subject: [PATCH] fix(plugin): add OpenClaw state dir fallback --- index.ts | 6 ++++-- package.json | 2 -- src/utils/openclaw-state-dir.test.ts | 21 +++++++++++++++++++++ src/utils/openclaw-state-dir.ts | 12 ++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/utils/openclaw-state-dir.test.ts create mode 100644 src/utils/openclaw-state-dir.ts diff --git a/index.ts b/index.ts index b7f67ed..1da21fc 100644 --- a/index.ts +++ b/index.ts @@ -42,6 +42,7 @@ import { ensurePluginHookPolicy, decideHookPolicy, } from "./src/utils/ensure-hook-policy.js"; +import { resolveOpenClawStateDir } from "./src/utils/openclaw-state-dir.js"; const TAG = "[memory-tdai]"; @@ -217,7 +218,8 @@ export default function register(api: OpenClawPluginApi) { } // Resolve plugin data directory via runtime API (avoid importing internal paths directly) - const pluginDataDir = path.join(api.runtime.state.resolveStateDir(), "memory-tdai"); + const openclawStateDir = resolveOpenClawStateDir(api.runtime.state); + const pluginDataDir = path.join(openclawStateDir, "memory-tdai"); initDataDirectories(pluginDataDir); api.logger.debug?.(`${TAG} Data dir: ${pluginDataDir} (all subdirectories initialized)`); @@ -823,7 +825,7 @@ export default function register(api: OpenClawPluginApi) { registerMemoryTdaiCli(memoryTdai, { config, pluginConfig: api.pluginConfig, - stateDir: api.runtime.state.resolveStateDir(), + stateDir: openclawStateDir, logger: cliLogger, }); }, diff --git a/package.json b/package.json index 0609611..ea6cb4e 100644 --- a/package.json +++ b/package.json @@ -34,14 +34,12 @@ "files": [ "dist/", "bin/", - "index.ts", "scripts/migrate-sqlite-to-tcvdb/dist/", "scripts/export-tencent-vdb/dist/", "scripts/read-local-memory/dist/", "scripts/memory-tencentdb-ctl.sh", "scripts/install_hermes_memory_tencentdb.sh", "scripts/README.memory-tencentdb-ctl.md", - "src/", "scripts/openclaw-after-tool-call-messages.patch.sh", "scripts/setup-offload.sh", "hermes-plugin/", diff --git a/src/utils/openclaw-state-dir.test.ts b/src/utils/openclaw-state-dir.test.ts new file mode 100644 index 0000000..204d28b --- /dev/null +++ b/src/utils/openclaw-state-dir.test.ts @@ -0,0 +1,21 @@ +import { homedir } from "node:os"; +import path from "node:path"; +import { describe, expect, it } from "vitest"; + +import { resolveOpenClawStateDir } from "./openclaw-state-dir.js"; + +describe("resolveOpenClawStateDir", () => { + it("uses the runtime state resolver when available", () => { + const stateDir = resolveOpenClawStateDir({ + resolveStateDir: () => "/tmp/openclaw-state", + }); + + expect(stateDir).toBe("/tmp/openclaw-state"); + }); + + it("falls back to ~/.openclaw when runtime.state is not available", () => { + const stateDir = resolveOpenClawStateDir(undefined); + + expect(stateDir).toBe(path.join(homedir(), ".openclaw")); + }); +}); diff --git a/src/utils/openclaw-state-dir.ts b/src/utils/openclaw-state-dir.ts new file mode 100644 index 0000000..d30b560 --- /dev/null +++ b/src/utils/openclaw-state-dir.ts @@ -0,0 +1,12 @@ +import { homedir } from "node:os"; +import path from "node:path"; + +export interface OpenClawRuntimeStateLike { + resolveStateDir?: () => string; +} + +export function resolveOpenClawStateDir( + runtimeState: OpenClawRuntimeStateLike | undefined, +): string { + return runtimeState?.resolveStateDir?.() ?? path.join(homedir(), ".openclaw"); +}