@anuma/sdk 10.1.0 → 10.2.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.
@@ -25480,17 +25480,19 @@ init_clientConfig();
25480
25480
 
25481
25481
  // src/lib/db/chat/summaryOperations.ts
25482
25482
  var import_watermelondb4 = __toESM(require_watermelondb());
25483
- function createSummaryContext(database) {
25483
+ init_logger();
25484
+ function createSummaryContext(database, encryption = {}) {
25484
25485
  return {
25486
+ ...encryption,
25485
25487
  database,
25486
25488
  summariesCollection: database.get("conversation_summaries")
25487
25489
  };
25488
25490
  }
25489
- function modelToStored(record) {
25491
+ function modelToStored(record, summary) {
25490
25492
  return {
25491
25493
  uniqueId: record.id,
25492
25494
  conversationId: record.conversationId,
25493
- summary: record.summary,
25495
+ summary,
25494
25496
  summarizedUpTo: record.summarizedUpTo,
25495
25497
  tokenCount: record.tokenCount,
25496
25498
  createdAt: record.createdAt,
@@ -25500,28 +25502,62 @@ function modelToStored(record) {
25500
25502
  async function getConversationSummaryOp(ctx, conversationId) {
25501
25503
  const results = await ctx.summariesCollection.query(import_watermelondb4.Q.where("conversation_id", conversationId)).fetch();
25502
25504
  if (results.length === 0) return null;
25503
- return modelToStored(results[0]);
25505
+ const record = results[0];
25506
+ const stored = modelToStored(record, record.summary);
25507
+ if (ctx.walletAddress && isEncrypted(stored.summary)) {
25508
+ if (ctx.signMessage) {
25509
+ try {
25510
+ await requestEncryptionKey(ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner);
25511
+ } catch (error) {
25512
+ getLogger().warn("Failed to request the encryption key for the summary:", error);
25513
+ }
25514
+ }
25515
+ stored.summary = await decryptField(stored.summary, ctx.walletAddress);
25516
+ } else if (ctx.walletAddress && ctx.signMessage && stored.summary) {
25517
+ try {
25518
+ const encryptedSummary = await encryptField(
25519
+ stored.summary,
25520
+ ctx.walletAddress,
25521
+ ctx.signMessage,
25522
+ ctx.embeddedWalletSigner
25523
+ );
25524
+ await ctx.database.write(async () => {
25525
+ const current = await ctx.summariesCollection.query(import_watermelondb4.Q.where("id", record.id)).fetch();
25526
+ if (current[0]?.summary === stored.summary) {
25527
+ await current[0].update((rec) => {
25528
+ rec.summary = encryptedSummary;
25529
+ });
25530
+ }
25531
+ });
25532
+ } catch (error) {
25533
+ getLogger().warn("Failed to encrypt the legacy summary:", error);
25534
+ }
25535
+ }
25536
+ return stored;
25504
25537
  }
25505
25538
  async function upsertConversationSummaryOp(ctx, conversationId, summary, summarizedUpTo, tokenCount) {
25539
+ const revision = getConversationRevision(ctx.database, conversationId);
25506
25540
  let record;
25541
+ const encryptedSummary = ctx.walletAddress ? await encryptField(summary, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner) : summary;
25507
25542
  await ctx.database.write(async () => {
25543
+ assertConversationRevision(ctx.database, conversationId, revision);
25508
25544
  const existing = await ctx.summariesCollection.query(import_watermelondb4.Q.where("conversation_id", conversationId)).fetch();
25509
25545
  if (existing.length > 0) {
25510
25546
  record = await existing[0].update((rec) => {
25511
- rec.summary = summary;
25547
+ rec.summary = encryptedSummary;
25512
25548
  rec.summarizedUpTo = summarizedUpTo;
25513
25549
  rec.tokenCount = tokenCount;
25514
25550
  });
25515
25551
  } else {
25516
25552
  record = await ctx.summariesCollection.create((rec) => {
25517
25553
  rec.conversationId = conversationId;
25518
- rec.summary = summary;
25554
+ rec.summary = encryptedSummary;
25519
25555
  rec.summarizedUpTo = summarizedUpTo;
25520
25556
  rec.tokenCount = tokenCount;
25521
25557
  });
25522
25558
  }
25523
25559
  });
25524
- return modelToStored(record);
25560
+ return modelToStored(record, summary);
25525
25561
  }
25526
25562
  async function deleteConversationSummaryOp(ctx, conversationId) {
25527
25563
  await ctx.database.write(async () => {
@@ -25789,7 +25825,7 @@ async function summarizeHistoryAtRevision(options, revision) {
25789
25825
  const cachedSummary = await summaryStore(options).getSummary(conversationId);
25790
25826
  return {
25791
25827
  messagesToConvert: messages,
25792
- summarySystemMessage: cachedSummary?.summary ? summaryToSystemMessage(cachedSummary.summary) : null
25828
+ summarySystemMessage: cachedSummary?.summary && !isEncrypted(cachedSummary.summary) ? summaryToSystemMessage(cachedSummary.summary) : null
25793
25829
  };
25794
25830
  } catch {
25795
25831
  return { messagesToConvert: messages, summarySystemMessage: null };
@@ -25844,14 +25880,15 @@ async function doSummarizeHistory(options, revision) {
25844
25880
  } = options;
25845
25881
  try {
25846
25882
  const summaries = summaryStore(options);
25847
- const persistSummary = (summary, summarizedUpTo, tokenCount) => database.write(async (writer) => {
25883
+ const persistSummary = (summary, summarizedUpTo, tokenCount) => {
25848
25884
  assertConversationRevision(database, conversationId, revision);
25849
- await writer.callWriter(
25850
- () => summaries.upsertSummary(conversationId, summary, summarizedUpTo, tokenCount)
25851
- );
25852
- });
25885
+ return summaries.upsertSummary(conversationId, summary, summarizedUpTo, tokenCount);
25886
+ };
25853
25887
  let cachedSummary = await summaries.getSummary(conversationId);
25854
25888
  assertConversationRevision(database, conversationId, revision);
25889
+ if (cachedSummary && isEncrypted(cachedSummary.summary)) {
25890
+ throw new Error("The cached summary could not be decrypted");
25891
+ }
25855
25892
  const lastCompacted = lastCompactionTime.get(conversationId) ?? 0;
25856
25893
  const compactionCooledDown = Date.now() - lastCompacted > COMPACTION_COOLDOWN_MS;
25857
25894
  if (cachedSummary && cachedSummary.tokenCount > summaryTokenThreshold * MAX_SUMMARY_TOKEN_RATIO && compactionCooledDown) {
@@ -8028,12 +8028,12 @@ declare function clearLazyTitleCache(): void;
8028
8028
  declare function decryptConversationTitle(encryptedTitle: string, address: string): Promise<string>;
8029
8029
 
8030
8030
  /** Context needed for summary operations */
8031
- interface SummaryOperationsContext {
8031
+ interface SummaryOperationsContext extends Pick<StorageOperationsContext, "walletAddress" | "signMessage" | "embeddedWalletSigner"> {
8032
8032
  database: Database;
8033
8033
  summariesCollection: Collection<ConversationSummary>;
8034
8034
  }
8035
8035
  /** Create a summary operations context from a database instance */
8036
- declare function createSummaryContext(database: Database): SummaryOperationsContext;
8036
+ declare function createSummaryContext(database: Database, encryption?: Pick<SummaryOperationsContext, "walletAddress" | "signMessage" | "embeddedWalletSigner">): SummaryOperationsContext;
8037
8037
  /**
8038
8038
  * Get the cached summary for a conversation.
8039
8039
  * Returns null if no summary exists yet.
@@ -8028,12 +8028,12 @@ declare function clearLazyTitleCache(): void;
8028
8028
  declare function decryptConversationTitle(encryptedTitle: string, address: string): Promise<string>;
8029
8029
 
8030
8030
  /** Context needed for summary operations */
8031
- interface SummaryOperationsContext {
8031
+ interface SummaryOperationsContext extends Pick<StorageOperationsContext, "walletAddress" | "signMessage" | "embeddedWalletSigner"> {
8032
8032
  database: Database;
8033
8033
  summariesCollection: Collection<ConversationSummary>;
8034
8034
  }
8035
8035
  /** Create a summary operations context from a database instance */
8036
- declare function createSummaryContext(database: Database): SummaryOperationsContext;
8036
+ declare function createSummaryContext(database: Database, encryption?: Pick<SummaryOperationsContext, "walletAddress" | "signMessage" | "embeddedWalletSigner">): SummaryOperationsContext;
8037
8037
  /**
8038
8038
  * Get the cached summary for a conversation.
8039
8039
  * Returns null if no summary exists yet.
@@ -23753,17 +23753,18 @@ function createStockPricePreProcessor(options = {}) {
23753
23753
 
23754
23754
  // src/lib/db/chat/summaryOperations.ts
23755
23755
  var import_watermelondb4 = __toESM(require_watermelondb());
23756
- function createSummaryContext(database) {
23756
+ function createSummaryContext(database, encryption = {}) {
23757
23757
  return {
23758
+ ...encryption,
23758
23759
  database,
23759
23760
  summariesCollection: database.get("conversation_summaries")
23760
23761
  };
23761
23762
  }
23762
- function modelToStored(record) {
23763
+ function modelToStored(record, summary) {
23763
23764
  return {
23764
23765
  uniqueId: record.id,
23765
23766
  conversationId: record.conversationId,
23766
- summary: record.summary,
23767
+ summary,
23767
23768
  summarizedUpTo: record.summarizedUpTo,
23768
23769
  tokenCount: record.tokenCount,
23769
23770
  createdAt: record.createdAt,
@@ -23773,28 +23774,62 @@ function modelToStored(record) {
23773
23774
  async function getConversationSummaryOp(ctx, conversationId) {
23774
23775
  const results = await ctx.summariesCollection.query(import_watermelondb4.Q.where("conversation_id", conversationId)).fetch();
23775
23776
  if (results.length === 0) return null;
23776
- return modelToStored(results[0]);
23777
+ const record = results[0];
23778
+ const stored = modelToStored(record, record.summary);
23779
+ if (ctx.walletAddress && isEncrypted(stored.summary)) {
23780
+ if (ctx.signMessage) {
23781
+ try {
23782
+ await requestEncryptionKey(ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner);
23783
+ } catch (error) {
23784
+ getLogger().warn("Failed to request the encryption key for the summary:", error);
23785
+ }
23786
+ }
23787
+ stored.summary = await decryptField(stored.summary, ctx.walletAddress);
23788
+ } else if (ctx.walletAddress && ctx.signMessage && stored.summary) {
23789
+ try {
23790
+ const encryptedSummary = await encryptField(
23791
+ stored.summary,
23792
+ ctx.walletAddress,
23793
+ ctx.signMessage,
23794
+ ctx.embeddedWalletSigner
23795
+ );
23796
+ await ctx.database.write(async () => {
23797
+ const current = await ctx.summariesCollection.query(import_watermelondb4.Q.where("id", record.id)).fetch();
23798
+ if (current[0]?.summary === stored.summary) {
23799
+ await current[0].update((rec) => {
23800
+ rec.summary = encryptedSummary;
23801
+ });
23802
+ }
23803
+ });
23804
+ } catch (error) {
23805
+ getLogger().warn("Failed to encrypt the legacy summary:", error);
23806
+ }
23807
+ }
23808
+ return stored;
23777
23809
  }
23778
23810
  async function upsertConversationSummaryOp(ctx, conversationId, summary, summarizedUpTo, tokenCount) {
23811
+ const revision = getConversationRevision(ctx.database, conversationId);
23779
23812
  let record;
23813
+ const encryptedSummary = ctx.walletAddress ? await encryptField(summary, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner) : summary;
23780
23814
  await ctx.database.write(async () => {
23815
+ assertConversationRevision(ctx.database, conversationId, revision);
23781
23816
  const existing = await ctx.summariesCollection.query(import_watermelondb4.Q.where("conversation_id", conversationId)).fetch();
23782
23817
  if (existing.length > 0) {
23783
23818
  record = await existing[0].update((rec) => {
23784
- rec.summary = summary;
23819
+ rec.summary = encryptedSummary;
23785
23820
  rec.summarizedUpTo = summarizedUpTo;
23786
23821
  rec.tokenCount = tokenCount;
23787
23822
  });
23788
23823
  } else {
23789
23824
  record = await ctx.summariesCollection.create((rec) => {
23790
23825
  rec.conversationId = conversationId;
23791
- rec.summary = summary;
23826
+ rec.summary = encryptedSummary;
23792
23827
  rec.summarizedUpTo = summarizedUpTo;
23793
23828
  rec.tokenCount = tokenCount;
23794
23829
  });
23795
23830
  }
23796
23831
  });
23797
- return modelToStored(record);
23832
+ return modelToStored(record, summary);
23798
23833
  }
23799
23834
  async function deleteConversationSummaryOp(ctx, conversationId) {
23800
23835
  await ctx.database.write(async () => {
@@ -24059,7 +24094,7 @@ async function summarizeHistoryAtRevision(options, revision) {
24059
24094
  const cachedSummary = await summaryStore(options).getSummary(conversationId);
24060
24095
  return {
24061
24096
  messagesToConvert: messages,
24062
- summarySystemMessage: cachedSummary?.summary ? summaryToSystemMessage(cachedSummary.summary) : null
24097
+ summarySystemMessage: cachedSummary?.summary && !isEncrypted(cachedSummary.summary) ? summaryToSystemMessage(cachedSummary.summary) : null
24063
24098
  };
24064
24099
  } catch {
24065
24100
  return { messagesToConvert: messages, summarySystemMessage: null };
@@ -24114,14 +24149,15 @@ async function doSummarizeHistory(options, revision) {
24114
24149
  } = options;
24115
24150
  try {
24116
24151
  const summaries = summaryStore(options);
24117
- const persistSummary = (summary, summarizedUpTo, tokenCount) => database.write(async (writer) => {
24152
+ const persistSummary = (summary, summarizedUpTo, tokenCount) => {
24118
24153
  assertConversationRevision(database, conversationId, revision);
24119
- await writer.callWriter(
24120
- () => summaries.upsertSummary(conversationId, summary, summarizedUpTo, tokenCount)
24121
- );
24122
- });
24154
+ return summaries.upsertSummary(conversationId, summary, summarizedUpTo, tokenCount);
24155
+ };
24123
24156
  let cachedSummary = await summaries.getSummary(conversationId);
24124
24157
  assertConversationRevision(database, conversationId, revision);
24158
+ if (cachedSummary && isEncrypted(cachedSummary.summary)) {
24159
+ throw new Error("The cached summary could not be decrypted");
24160
+ }
24125
24161
  const lastCompacted = lastCompactionTime.get(conversationId) ?? 0;
24126
24162
  const compactionCooledDown = Date.now() - lastCompacted > COMPACTION_COOLDOWN_MS;
24127
24163
  if (cachedSummary && cachedSummary.tokenCount > summaryTokenThreshold * MAX_SUMMARY_TOKEN_RATIO && compactionCooledDown) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anuma/sdk",
3
- "version": "10.1.0",
3
+ "version": "10.2.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",