@unicitylabs/openclaw-unicity 0.5.7 → 0.5.8

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/channel.ts +21 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unicitylabs/openclaw-unicity",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "description": "Unicity wallet identity and encrypted DMs for OpenClaw agents — powered by Sphere SDK",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/channel.ts CHANGED
@@ -103,6 +103,8 @@ let activeSphere: Sphere | null = null;
103
103
  let pluginRuntime: PluginRuntime | null = null;
104
104
  let ownerIdentity: string | null = null;
105
105
  let pluginConfig: UnicityConfig = {};
106
+ /** Cleanup function from the previous gateway start — called before re-subscribing. */
107
+ let previousGatewayCleanup: (() => void) | null = null;
106
108
 
107
109
  export function setUnicityRuntime(rt: PluginRuntime): void {
108
110
  pluginRuntime = rt;
@@ -223,6 +225,14 @@ export const unicityChannelPlugin = {
223
225
  log?: { info: (m: string) => void; warn: (m: string) => void; error: (m: string) => void; debug: (m: string) => void };
224
226
  setStatus: (s: Record<string, unknown>) => void;
225
227
  }) => {
228
+ // Clean up handlers from any previous gateway start (auto-restart scenario).
229
+ // Without this, each restart stacks duplicate onDirectMessage handlers on the
230
+ // shared Sphere singleton, causing messages to be processed N times.
231
+ if (previousGatewayCleanup) {
232
+ previousGatewayCleanup();
233
+ previousGatewayCleanup = null;
234
+ }
235
+
226
236
  const sphere = activeSphere ?? await waitForSphere();
227
237
  if (!sphere) throw new Error("Unicity Sphere not initialized — run `openclaw unicity init`");
228
238
 
@@ -692,7 +702,7 @@ export const unicityChannelPlugin = {
692
702
  groupBackfillStates.clear();
693
703
  }
694
704
 
695
- ctx.abortSignal.addEventListener("abort", () => {
705
+ function cleanupSubscriptions(): void {
696
706
  clearBackfillTimers();
697
707
  unsub();
698
708
  unsubTransfer();
@@ -701,18 +711,20 @@ export const unicityChannelPlugin = {
701
711
  unsubGroupJoined();
702
712
  unsubGroupLeft();
703
713
  unsubGroupKicked();
714
+ }
715
+
716
+ // Store cleanup so auto-restart can tear down stale handlers
717
+ previousGatewayCleanup = cleanupSubscriptions;
718
+
719
+ ctx.abortSignal.addEventListener("abort", () => {
720
+ cleanupSubscriptions();
721
+ previousGatewayCleanup = null;
704
722
  }, { once: true });
705
723
 
706
724
  return {
707
725
  stop: () => {
708
- clearBackfillTimers();
709
- unsub();
710
- unsubTransfer();
711
- unsubPaymentRequest();
712
- unsubGroupMessage();
713
- unsubGroupJoined();
714
- unsubGroupLeft();
715
- unsubGroupKicked();
726
+ cleanupSubscriptions();
727
+ previousGatewayCleanup = null;
716
728
  ctx.log?.info(`[${ctx.account.accountId}] Unicity channel stopped`);
717
729
  },
718
730
  };