@unicitylabs/sphere-sdk 0.6.13-dev.3 → 0.6.14

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.
package/README.md CHANGED
@@ -550,6 +550,31 @@ const { sphere } = await Sphere.init({
550
550
 
551
551
  Once the SDK processes DMs, the timestamp is persisted and `dmSince` is ignored on subsequent connects.
552
552
 
553
+ ### Ephemeral Mode (No Caching)
554
+
555
+ For anonymous agents or LLM bots that don't need message history, disable DM caching:
556
+
557
+ ```typescript
558
+ const { sphere } = await Sphere.init({
559
+ ...providers,
560
+ communications: { cacheMessages: false },
561
+ });
562
+
563
+ // Stream-only: receive, process, forget
564
+ sphere.communications.onDirectMessage((msg) => {
565
+ processAndReply(msg);
566
+ });
567
+
568
+ // sendDM still works — message is sent but not stored locally
569
+ await sphere.communications.sendDM('@alice', 'response');
570
+ ```
571
+
572
+ When `cacheMessages` is `false`:
573
+ - `onDirectMessage()` handlers and `message:dm` events fire normally
574
+ - Messages are never stored in memory or persisted to storage
575
+ - `getConversation()` / `getConversations()` return empty results
576
+ - Deduplication is skipped (duplicate relay deliveries may trigger duplicate events)
577
+
553
578
  ## L1 (ALPHA Blockchain) Operations
554
579
 
555
580
  Access L1 payments through `sphere.payments.l1`:
@@ -11654,7 +11654,8 @@ var CommunicationsModule = class {
11654
11654
  autoSave: config?.autoSave ?? true,
11655
11655
  maxMessages: config?.maxMessages ?? 1e3,
11656
11656
  maxPerConversation: config?.maxPerConversation ?? 200,
11657
- readReceipts: config?.readReceipts ?? true
11657
+ readReceipts: config?.readReceipts ?? true,
11658
+ cacheMessages: config?.cacheMessages ?? true
11658
11659
  };
11659
11660
  }
11660
11661
  // ===========================================================================
@@ -11710,6 +11711,7 @@ var CommunicationsModule = class {
11710
11711
  * Falls back to legacy global 'direct_messages' key for migration.
11711
11712
  */
11712
11713
  async load() {
11714
+ if (!this.config.cacheMessages) return;
11713
11715
  this.ensureInitialized();
11714
11716
  this.messages.clear();
11715
11717
  let data = await this.deps.storage.get(STORAGE_KEYS_ADDRESS.MESSAGES);
@@ -11769,9 +11771,11 @@ var CommunicationsModule = class {
11769
11771
  timestamp: Date.now(),
11770
11772
  isRead: false
11771
11773
  };
11772
- this.messages.set(message.id, message);
11773
- if (this.config.autoSave) {
11774
- await this.save();
11774
+ if (this.config.cacheMessages) {
11775
+ this.messages.set(message.id, message);
11776
+ if (this.config.autoSave) {
11777
+ await this.save();
11778
+ }
11775
11779
  }
11776
11780
  return message;
11777
11781
  }
@@ -11810,7 +11814,7 @@ var CommunicationsModule = class {
11810
11814
  msg.isRead = true;
11811
11815
  }
11812
11816
  }
11813
- if (this.config.autoSave) {
11817
+ if (this.config.cacheMessages && this.config.autoSave) {
11814
11818
  await this.save();
11815
11819
  }
11816
11820
  if (this.config.readReceipts && this.deps?.transport.sendReadReceipt) {
@@ -11858,6 +11862,7 @@ var CommunicationsModule = class {
11858
11862
  * Delete all messages in a conversation with a peer
11859
11863
  */
11860
11864
  async deleteConversation(peerPubkey) {
11865
+ if (!this.config.cacheMessages) return;
11861
11866
  for (const [id, msg] of this.messages) {
11862
11867
  if (msg.senderPubkey === peerPubkey || msg.recipientPubkey === peerPubkey) {
11863
11868
  this.messages.delete(id);
@@ -11984,7 +11989,7 @@ var CommunicationsModule = class {
11984
11989
  handleIncomingMessage(msg) {
11985
11990
  const isHistorical = msg.timestamp < this.initializedAt;
11986
11991
  if (msg.isSelfWrap && msg.recipientTransportPubkey) {
11987
- if (this.messages.has(msg.id)) return;
11992
+ if (this.config.cacheMessages && this.messages.has(msg.id)) return;
11988
11993
  const message2 = {
11989
11994
  id: msg.id,
11990
11995
  senderPubkey: this.deps.identity.chainPubkey,
@@ -11994,9 +11999,11 @@ var CommunicationsModule = class {
11994
11999
  timestamp: msg.timestamp,
11995
12000
  isRead: isHistorical
11996
12001
  };
11997
- this.messages.set(message2.id, message2);
12002
+ if (this.config.cacheMessages) {
12003
+ this.messages.set(message2.id, message2);
12004
+ }
11998
12005
  this.deps.emitEvent("message:dm", message2);
11999
- if (this.config.autoSave) {
12006
+ if (this.config.cacheMessages && this.config.autoSave) {
12000
12007
  this.save();
12001
12008
  }
12002
12009
  return;
@@ -12007,7 +12014,7 @@ var CommunicationsModule = class {
12007
12014
  if (tp === ownChainPubkey) return;
12008
12015
  if (ownChainPubkey.length === 66 && (ownChainPubkey.startsWith("02") || ownChainPubkey.startsWith("03")) && tp === ownChainPubkey.slice(2)) return;
12009
12016
  }
12010
- if (this.messages.has(msg.id)) return;
12017
+ if (this.config.cacheMessages && this.messages.has(msg.id)) return;
12011
12018
  const message = {
12012
12019
  id: msg.id,
12013
12020
  senderPubkey: msg.senderTransportPubkey,
@@ -12017,7 +12024,9 @@ var CommunicationsModule = class {
12017
12024
  timestamp: msg.timestamp,
12018
12025
  isRead: isHistorical
12019
12026
  };
12020
- this.messages.set(message.id, message);
12027
+ if (this.config.cacheMessages) {
12028
+ this.messages.set(message.id, message);
12029
+ }
12021
12030
  this.deps.emitEvent("message:dm", message);
12022
12031
  for (const handler of this.dmHandlers) {
12023
12032
  try {
@@ -12026,10 +12035,12 @@ var CommunicationsModule = class {
12026
12035
  logger.error("Communications", "Handler error:", error);
12027
12036
  }
12028
12037
  }
12029
- if (this.config.autoSave) {
12030
- this.save();
12038
+ if (this.config.cacheMessages) {
12039
+ if (this.config.autoSave) {
12040
+ this.save();
12041
+ }
12042
+ this.pruneIfNeeded();
12031
12043
  }
12032
- this.pruneIfNeeded();
12033
12044
  }
12034
12045
  handleComposingIndicator(indicator) {
12035
12046
  const composing = {
@@ -16636,6 +16647,7 @@ var Sphere = class _Sphere {
16636
16647
  _l1Config;
16637
16648
  _groupChatConfig;
16638
16649
  _marketConfig;
16650
+ _communicationsConfig;
16639
16651
  // Events
16640
16652
  eventHandlers = /* @__PURE__ */ new Map();
16641
16653
  // Provider management
@@ -16645,7 +16657,7 @@ var Sphere = class _Sphere {
16645
16657
  // ===========================================================================
16646
16658
  // Constructor (private)
16647
16659
  // ===========================================================================
16648
- constructor(storage, transport, oracle, tokenStorage, l1Config, priceProvider, groupChatConfig, marketConfig) {
16660
+ constructor(storage, transport, oracle, tokenStorage, l1Config, priceProvider, groupChatConfig, marketConfig, communicationsConfig) {
16649
16661
  this._storage = storage;
16650
16662
  this._transport = transport;
16651
16663
  this._oracle = oracle;
@@ -16656,8 +16668,9 @@ var Sphere = class _Sphere {
16656
16668
  this._l1Config = l1Config;
16657
16669
  this._groupChatConfig = groupChatConfig;
16658
16670
  this._marketConfig = marketConfig;
16671
+ this._communicationsConfig = communicationsConfig;
16659
16672
  this._payments = createPaymentsModule({ l1: l1Config });
16660
- this._communications = createCommunicationsModule();
16673
+ this._communications = createCommunicationsModule(communicationsConfig);
16661
16674
  this._groupChat = groupChatConfig ? createGroupChatModule(groupChatConfig) : null;
16662
16675
  this._market = marketConfig ? createMarketModule(marketConfig) : null;
16663
16676
  }
@@ -16845,7 +16858,8 @@ var Sphere = class _Sphere {
16845
16858
  options.l1,
16846
16859
  options.price,
16847
16860
  groupChatConfig,
16848
- marketConfig
16861
+ marketConfig,
16862
+ options.communications
16849
16863
  );
16850
16864
  sphere._password = options.password ?? null;
16851
16865
  progress?.({ step: "storing_keys", message: "Storing wallet keys..." });
@@ -16903,7 +16917,8 @@ var Sphere = class _Sphere {
16903
16917
  options.l1,
16904
16918
  options.price,
16905
16919
  groupChatConfig,
16906
- marketConfig
16920
+ marketConfig,
16921
+ options.communications
16907
16922
  );
16908
16923
  sphere._password = options.password ?? null;
16909
16924
  if (!options.storage.isConnected()) {
@@ -16981,7 +16996,8 @@ var Sphere = class _Sphere {
16981
16996
  options.l1,
16982
16997
  options.price,
16983
16998
  groupChatConfig,
16984
- marketConfig
16999
+ marketConfig,
17000
+ options.communications
16985
17001
  );
16986
17002
  sphere._password = options.password ?? null;
16987
17003
  progress?.({ step: "storing_keys", message: "Storing wallet keys..." });
@@ -18047,7 +18063,7 @@ var Sphere = class _Sphere {
18047
18063
  addressTransport.setFallbackDmSince(this._dmSince);
18048
18064
  }
18049
18065
  const payments = createPaymentsModule({ l1: this._l1Config });
18050
- const communications = createCommunicationsModule();
18066
+ const communications = createCommunicationsModule(this._communicationsConfig);
18051
18067
  const groupChat = this._groupChatConfig ? createGroupChatModule(this._groupChatConfig) : null;
18052
18068
  const market = this._marketConfig ? createMarketModule(this._marketConfig) : null;
18053
18069
  payments.initialize({