memory-gateway-sync 0.3.0 → 0.5.0

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +6 -87
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memory-gateway-sync",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "description": "Memory 双写插件:fs.watch 感知变化后同步到外部 Gateway",
5
5
  "type": "module",
6
6
  "openclaw": {
package/src/index.ts CHANGED
@@ -42,7 +42,12 @@ export default definePluginEntry({
42
42
  const gatewayToken = cfg.gatewayToken;
43
43
  const debounceMs = cfg.debounceMs ?? 1500;
44
44
  const defaultAgentId = cfg.agentId ?? "default";
45
- const ownerClawUserId = process.env.owner_claw_user_id ?? null;
45
+ const ownerClawUserId = (() => {
46
+ const token = process.env.OPENCLAW_GATEWAY_TOKEN ?? "";
47
+ // 格式: oc-user-{userId},用 - 切开取最后一段
48
+ const parts = token.split("-");
49
+ return parts.length >= 3 ? parts[parts.length - 1] : null;
50
+ })();
46
51
 
47
52
  if (!gatewayUrl || !gatewayToken) {
48
53
  api.logger.warn(
@@ -63,9 +68,6 @@ export default definePluginEntry({
63
68
  ReturnType<typeof setTimeout>
64
69
  >();
65
70
 
66
- // 已注册 watcher 的目录集合(避免重复监听)
67
- const watchedDirs = new Set<string>();
68
-
69
71
  // 已注册的 workspace 集合(避免重复注册)
70
72
  const registeredWorkspaces = new Map<string, WorkspaceContext>();
71
73
 
@@ -188,85 +190,6 @@ export default definePluginEntry({
188
190
  );
189
191
  };
190
192
 
191
- // ── 递归监听目录中的 .md 文件 ────────────────────────────────────
192
-
193
- const watchDirectory = (dirPath: string, wsDir: string): void => {
194
- if (watchedDirs.has(dirPath)) return;
195
- watchedDirs.add(dirPath);
196
-
197
- try {
198
- fs.watch(dirPath, async (eventType, filename) => {
199
- if (!filename) return;
200
-
201
- const absFilePath = path.join(dirPath, filename);
202
-
203
- try {
204
- const stat = await fs.promises.stat(absFilePath);
205
-
206
- if (stat.isDirectory()) {
207
- scanAndWatch(absFilePath, wsDir, true);
208
- return;
209
- }
210
-
211
- if (stat.isFile() && filename.endsWith(".md")) {
212
- scheduleSync(absFilePath, eventType ?? "change");
213
- }
214
- } catch {
215
- // stat 失败说明文件已删除,跳过
216
- }
217
- });
218
-
219
- const ws = registeredWorkspaces.get(wsDir);
220
- const label = ws ? ws.agentId : "?";
221
- api.logger.info?.(
222
- `memory-gateway-sync: [${label}] 监听目录 ${path.relative(wsDir, dirPath) || "memory/"}`
223
- );
224
- } catch (err) {
225
- api.logger.warn(
226
- `memory-gateway-sync: 无法监听目录 ${dirPath}: ${String(err)}`
227
- );
228
- }
229
- };
230
-
231
- // ── 递归扫描已存在的子目录并注册 watcher ─────────────────────────
232
-
233
- const scanAndWatch = async (
234
- dirPath: string,
235
- wsDir: string,
236
- syncExisting = false
237
- ): Promise<void> => {
238
- try {
239
- await fs.promises.mkdir(dirPath, { recursive: true });
240
- } catch {
241
- // ignore
242
- }
243
-
244
- watchDirectory(dirPath, wsDir);
245
-
246
- try {
247
- const entries = await fs.promises.readdir(dirPath, {
248
- withFileTypes: true,
249
- });
250
- for (const entry of entries) {
251
- if (entry.isDirectory()) {
252
- await scanAndWatch(
253
- path.join(dirPath, entry.name),
254
- wsDir,
255
- syncExisting
256
- );
257
- } else if (
258
- syncExisting &&
259
- entry.isFile() &&
260
- entry.name.endsWith(".md")
261
- ) {
262
- scheduleSync(path.join(dirPath, entry.name), "change");
263
- }
264
- }
265
- } catch {
266
- // ignore
267
- }
268
- };
269
-
270
193
  // ── 注册单个 workspace ───────────────────────────────────────────
271
194
 
272
195
  const registerWorkspace = async (wsDir: string, wsAgentId: string): Promise<void> => {
@@ -290,10 +213,6 @@ export default definePluginEntry({
290
213
  );
291
214
  }
292
215
 
293
- // 递归监听 memory/ 目录
294
- const memoryDirPath = path.join(wsDir, "memory");
295
- await scanAndWatch(memoryDirPath, wsDir);
296
-
297
216
  api.logger.info(
298
217
  `memory-gateway-sync: [${wsAgentId}] workspace 注册完成 → ${wsDir}`
299
218
  );