memory-gateway-sync 0.2.0 → 0.4.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 +21 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memory-gateway-sync",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Memory 双写插件:fs.watch 感知变化后同步到外部 Gateway",
5
5
  "type": "module",
6
6
  "openclaw": {
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ interface IngestPayload {
16
16
  path: string;
17
17
  content: string;
18
18
  agentId: string;
19
+ userId: string | null;
19
20
  workspaceDir: string;
20
21
  eventType: string;
21
22
  syncedAt: string;
@@ -41,6 +42,12 @@ export default definePluginEntry({
41
42
  const gatewayToken = cfg.gatewayToken;
42
43
  const debounceMs = cfg.debounceMs ?? 1500;
43
44
  const defaultAgentId = cfg.agentId ?? "default";
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
+ })();
44
51
 
45
52
  if (!gatewayUrl || !gatewayToken) {
46
53
  api.logger.warn(
@@ -115,10 +122,24 @@ export default definePluginEntry({
115
122
  }
116
123
 
117
124
  const relativePath = path.relative(ws.dir, absPath);
125
+
126
+ // 根据路径解析 userId
127
+ // MEMORY.md / memory/owner/* → 环境变量 owner_claw_user_id
128
+ // memory/peers/{peerId}/* → 路径中的 peerId
129
+ // 其他 → null
130
+ let userId: string | null = null;
131
+ const parts = relativePath.replace(/\\/g, "/").split("/");
132
+ if (relativePath === "MEMORY.md" || parts[0] === "memory" && parts[1] === "owner") {
133
+ userId = ownerClawUserId;
134
+ } else if (parts[0] === "memory" && parts[1] === "peers" && parts.length >= 3) {
135
+ userId = parts[2];
136
+ }
137
+
118
138
  const payload: IngestPayload = {
119
139
  path: relativePath,
120
140
  content,
121
141
  agentId: ws.agentId,
142
+ userId,
122
143
  workspaceDir: ws.dir,
123
144
  eventType,
124
145
  syncedAt: new Date().toISOString(),