@anuma/sdk 2.6.1 → 3.0.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.
@@ -1041,6 +1041,67 @@ async function messageToStored(message, walletAddress, signMessage, embeddedWall
1041
1041
  }
1042
1042
  return baseMessage;
1043
1043
  }
1044
+ function rawDate(value) {
1045
+ return new Date(typeof value === "number" ? value : 0);
1046
+ }
1047
+ function messageRawToStoredRaw(raw, projection) {
1048
+ const parseJsonField = (rawValue) => {
1049
+ if (!rawValue) return void 0;
1050
+ if (typeof rawValue === "string") {
1051
+ if (rawValue.startsWith("enc:")) return rawValue;
1052
+ try {
1053
+ return JSON.parse(rawValue);
1054
+ } catch {
1055
+ return void 0;
1056
+ }
1057
+ }
1058
+ return rawValue;
1059
+ };
1060
+ const skipEmbeddings = projection?.skipEmbeddings === true;
1061
+ const vectorRaw = skipEmbeddings ? null : raw.vector;
1062
+ const chunksRaw = skipEmbeddings ? null : raw.chunks;
1063
+ return {
1064
+ uniqueId: raw.id,
1065
+ messageId: raw.message_id,
1066
+ conversationId: raw.conversation_id ?? "",
1067
+ role: raw.role ?? "",
1068
+ content: raw.content ?? "",
1069
+ model: raw.model,
1070
+ imageModel: raw.image_model,
1071
+ files: parseJsonField(raw.files),
1072
+ fileIds: parseJsonField(raw.file_ids),
1073
+ createdAt: rawDate(raw.created_at),
1074
+ updatedAt: rawDate(raw.updated_at),
1075
+ vector: skipEmbeddings ? void 0 : parseJsonField(vectorRaw),
1076
+ embeddingModel: raw.embedding_model,
1077
+ chunks: skipEmbeddings ? void 0 : parseJsonField(chunksRaw),
1078
+ usage: parseJsonField(raw.usage),
1079
+ sources: parseJsonField(raw.sources),
1080
+ responseDuration: raw.response_duration,
1081
+ // `was_stopped` is an OPTIONAL boolean: an unset row sanitizes to null, and the Model path
1082
+ // (message.wasStopped) returns that null verbatim. Preserve it (don't collapse to false) so the
1083
+ // raw path is byte-identical. When set, coerce SQLite 0/1 and LokiJS true/false alike.
1084
+ wasStopped: raw.was_stopped === null || raw.was_stopped === void 0 ? raw.was_stopped : raw.was_stopped === true || raw.was_stopped === 1,
1085
+ error: raw.error,
1086
+ thoughtProcess: parseJsonField(raw.thought_process),
1087
+ thinking: raw.thinking,
1088
+ parentMessageId: raw.parent_message_id,
1089
+ feedback: raw.feedback || null,
1090
+ toolCallEvents: parseJsonField(raw.tool_call_events)
1091
+ };
1092
+ }
1093
+ async function messageRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner, projection) {
1094
+ const baseMessage = messageRawToStoredRaw(raw, projection);
1095
+ if (walletAddress) {
1096
+ return await decryptMessageFields(
1097
+ baseMessage,
1098
+ walletAddress,
1099
+ signMessage,
1100
+ embeddedWalletSigner
1101
+ );
1102
+ }
1103
+ return baseMessage;
1104
+ }
1044
1105
  function conversationToStoredRaw(conversation) {
1045
1106
  return {
1046
1107
  uniqueId: conversation.id,
@@ -1065,6 +1126,33 @@ async function conversationToStored(conversation, walletAddress, signMessage, em
1065
1126
  }
1066
1127
  return baseConversation;
1067
1128
  }
1129
+ function conversationRawToStoredRaw(raw) {
1130
+ const pinnedAt = raw.pinned_at;
1131
+ return {
1132
+ uniqueId: raw.id,
1133
+ conversationId: raw.conversation_id ?? "",
1134
+ title: raw.title ?? "",
1135
+ projectId: raw.project_id,
1136
+ createdAt: rawDate(raw.created_at),
1137
+ updatedAt: rawDate(raw.updated_at),
1138
+ // SQLite stores booleans as 0/1, LokiJS as true/false — coerce both.
1139
+ isDeleted: raw.is_deleted === true || raw.is_deleted === 1,
1140
+ // @date maps a non-number raw column (incl. an explicit unpin null) to null; a number → Date.
1141
+ pinnedAt: typeof pinnedAt === "number" ? new Date(pinnedAt) : null
1142
+ };
1143
+ }
1144
+ async function conversationRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner) {
1145
+ const baseConversation = conversationRawToStoredRaw(raw);
1146
+ if (walletAddress) {
1147
+ return await decryptConversationFields(
1148
+ baseConversation,
1149
+ walletAddress,
1150
+ signMessage,
1151
+ embeddedWalletSigner
1152
+ );
1153
+ }
1154
+ return baseConversation;
1155
+ }
1068
1156
  async function createConversationOp(ctx, opts, defaultTitle = "New Conversation") {
1069
1157
  const convId = opts?.conversationId || generateConversationId();
1070
1158
  const title = opts?.title || defaultTitle;
@@ -1104,36 +1192,51 @@ async function getConversationOp(ctx, id) {
1104
1192
  ) : null;
1105
1193
  }
1106
1194
  async function getConversationsOp(ctx) {
1107
- const results = await ctx.conversationsCollection.query(Q.where("is_deleted", false), Q.sortBy("created_at", Q.desc)).fetch();
1195
+ const results = await ctx.conversationsCollection.query(Q.where("is_deleted", false), Q.sortBy("created_at", Q.desc)).unsafeFetchRaw();
1108
1196
  return Promise.all(
1109
1197
  results.map(
1110
- (conv) => conversationToStored(conv, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
1198
+ (raw) => conversationRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
1111
1199
  )
1112
1200
  );
1113
1201
  }
1114
- function conversationToLazyStored(conversation) {
1202
+ function conversationRawToLazyStored(raw) {
1203
+ const pinnedAt = raw.pinned_at;
1115
1204
  return {
1116
- uniqueId: conversation.id,
1117
- conversationId: conversation.conversationId,
1118
- encryptedTitle: conversation.title,
1119
- projectId: conversation.projectId,
1120
- createdAt: conversation.createdAt,
1121
- updatedAt: conversation.updatedAt,
1122
- isDeleted: conversation.isDeleted,
1123
- pinnedAt: conversation.pinnedAt
1205
+ uniqueId: raw.id,
1206
+ conversationId: raw.conversation_id ?? "",
1207
+ encryptedTitle: raw.title ?? "",
1208
+ projectId: raw.project_id,
1209
+ createdAt: rawDate(raw.created_at),
1210
+ updatedAt: rawDate(raw.updated_at),
1211
+ isDeleted: raw.is_deleted === true || raw.is_deleted === 1,
1212
+ pinnedAt: typeof pinnedAt === "number" ? new Date(pinnedAt) : null
1124
1213
  };
1125
1214
  }
1126
1215
  async function getConversationsLazyOp(ctx) {
1127
- const results = await ctx.conversationsCollection.query(Q.where("is_deleted", false), Q.sortBy("created_at", Q.desc)).fetch();
1128
- return results.map(conversationToLazyStored);
1216
+ const results = await ctx.conversationsCollection.query(Q.where("is_deleted", false), Q.sortBy("created_at", Q.desc)).unsafeFetchRaw();
1217
+ return results.map(conversationRawToLazyStored);
1218
+ }
1219
+ async function getConversationsPageOp(ctx, options) {
1220
+ const limit = Math.floor(options?.limit ?? 200);
1221
+ if (!Number.isFinite(limit) || limit < 1) return [];
1222
+ const exclude = new Set(options?.boundaryExcludeUniqueIds ?? []);
1223
+ const clauses = [Q.where("is_deleted", false)];
1224
+ if (options?.before !== void 0) {
1225
+ clauses.push(
1226
+ exclude.size > 0 ? Q.where("created_at", Q.lte(options.before)) : Q.where("created_at", Q.lt(options.before))
1227
+ );
1228
+ }
1229
+ const fetched = await ctx.conversationsCollection.query(...clauses, Q.sortBy("created_at", Q.desc), Q.take(limit + exclude.size)).unsafeFetchRaw();
1230
+ const results = exclude.size > 0 ? fetched.filter((raw) => !exclude.has(raw.id)) : fetched;
1231
+ return results.slice(0, limit).map(conversationRawToLazyStored);
1129
1232
  }
1130
1233
  async function getConversationsByProjectLazyOp(ctx, projectId) {
1131
1234
  const results = await ctx.conversationsCollection.query(
1132
1235
  Q.where("project_id", projectId === null ? "" : projectId),
1133
1236
  Q.where("is_deleted", false),
1134
1237
  Q.sortBy("created_at", Q.desc)
1135
- ).fetch();
1136
- return results.map(conversationToLazyStored);
1238
+ ).unsafeFetchRaw();
1239
+ return results.map(conversationRawToLazyStored);
1137
1240
  }
1138
1241
  async function updateConversationTitleOp(ctx, id, title) {
1139
1242
  const results = await ctx.conversationsCollection.query(Q.where("conversation_id", id), Q.where("is_deleted", false)).fetch();
@@ -1179,10 +1282,10 @@ async function updateConversationPinnedOp(ctx, id, pinned) {
1179
1282
  return false;
1180
1283
  }
1181
1284
  async function getMessagesOp(ctx, convId) {
1182
- const results = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).fetch();
1285
+ const results = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).unsafeFetchRaw();
1183
1286
  return Promise.all(
1184
1287
  results.map(
1185
- (msg) => messageToStored(msg, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
1288
+ (raw) => messageRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
1186
1289
  )
1187
1290
  );
1188
1291
  }
@@ -1199,39 +1302,40 @@ async function getMessagesPageOp(ctx, convId, opts) {
1199
1302
  exclude.size > 0 ? Q.where("message_id", Q.lte(opts.beforeMessageId)) : Q.where("message_id", Q.lt(opts.beforeMessageId))
1200
1303
  );
1201
1304
  }
1202
- const fetched = await ctx.messagesCollection.query(...clauses, Q.sortBy("message_id", Q.desc), Q.take(limit + exclude.size)).fetch();
1203
- const results = (exclude.size > 0 ? fetched.filter((msg) => !exclude.has(msg.id)) : fetched).slice(0, limit);
1305
+ const fetched = await ctx.messagesCollection.query(...clauses, Q.sortBy("message_id", Q.desc), Q.take(limit + exclude.size)).unsafeFetchRaw();
1306
+ const results = (exclude.size > 0 ? fetched.filter((raw) => !exclude.has(raw.id)) : fetched).slice(0, limit);
1204
1307
  results.reverse();
1205
1308
  return Promise.all(
1206
1309
  results.map(
1207
- (msg) => messageToStored(msg, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner, {
1310
+ (raw) => messageRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner, {
1208
1311
  skipEmbeddings: true
1209
1312
  })
1210
1313
  )
1211
1314
  );
1212
1315
  }
1213
1316
  async function getMessageSkeletonsOp(ctx, convId) {
1214
- const results = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).fetch();
1317
+ const results = await ctx.messagesCollection.query(Q.where("conversation_id", convId), Q.sortBy("message_id", Q.asc)).unsafeFetchRaw();
1215
1318
  const roleById = /* @__PURE__ */ new Map();
1216
- for (const msg of results) {
1217
- roleById.set(msg.id, msg.role);
1218
- }
1219
- const skeletons = results.map((msg) => ({
1220
- uniqueId: msg.id,
1221
- messageId: msg.messageId,
1222
- conversationId: String(msg._getRaw("conversation_id")),
1223
- role: msg.role,
1224
- createdAt: msg.createdAt,
1319
+ for (const raw of results) {
1320
+ roleById.set(raw.id, raw.role);
1321
+ }
1322
+ const skeletons = results.map((raw) => ({
1323
+ uniqueId: raw.id,
1324
+ messageId: raw.message_id,
1325
+ conversationId: raw.conversation_id ?? "",
1326
+ role: raw.role ?? "",
1327
+ createdAt: rawDate(raw.created_at),
1225
1328
  // WatermelonDB surfaces unset text columns as null at runtime — normalize
1226
1329
  // to undefined so `parentMessageId ?? undefined` keying works everywhere.
1227
- parentMessageId: msg.parentMessageId ?? void 0,
1228
- model: msg.model ?? void 0
1330
+ parentMessageId: raw.parent_message_id ?? void 0,
1331
+ model: raw.model ?? void 0
1229
1332
  }));
1230
1333
  const artifactIndices = [];
1231
1334
  for (let i = 0; i < results.length; i++) {
1232
- const msg = results[i];
1233
- if (msg.role !== "user" || !msg.parentMessageId) continue;
1234
- if (roleById.get(msg.parentMessageId) === "user") {
1335
+ const raw = results[i];
1336
+ const parentMessageId = raw.parent_message_id;
1337
+ if (raw.role !== "user" || !parentMessageId) continue;
1338
+ if (roleById.get(parentMessageId) === "user") {
1235
1339
  artifactIndices.push(i);
1236
1340
  }
1237
1341
  }
@@ -3482,7 +3586,7 @@ async function generateEmbedding(text, options) {
3482
3586
  const { baseUrl = BASE_URL, getToken, apiKey, model, cache } = options;
3483
3587
  if (cache) {
3484
3588
  const cached = cache.get(text);
3485
- if (cached) return cached;
3589
+ if (cached) return Array.from(cached);
3486
3590
  }
3487
3591
  let headers;
3488
3592
  if (apiKey) {
@@ -3521,10 +3625,11 @@ async function generateEmbedding(text, options) {
3521
3625
  totalTokens: response.data.usage.total_tokens ?? 0
3522
3626
  });
3523
3627
  }
3628
+ const f32Embedding = Float32Array.from(embedding);
3524
3629
  if (cache) {
3525
- cache.set(text, embedding);
3630
+ cache.set(text, f32Embedding);
3526
3631
  }
3527
- return embedding;
3632
+ return Array.from(f32Embedding);
3528
3633
  }
3529
3634
  var DEFAULT_EMBEDDING_BATCH_SIZE = 100;
3530
3635
  var DEFAULT_EMBEDDING_BATCH_CONCURRENCY = 3;
@@ -3562,7 +3667,7 @@ async function generateEmbeddings(texts, options) {
3562
3667
  if (cache) {
3563
3668
  const cached = cache.get(texts[i]);
3564
3669
  if (cached) {
3565
- results[i] = cached;
3670
+ results[i] = Array.from(cached);
3566
3671
  continue;
3567
3672
  }
3568
3673
  }
@@ -3623,9 +3728,10 @@ async function generateEmbeddings(texts, options) {
3623
3728
  newEmbeddings = allEmbeddings.flat();
3624
3729
  }
3625
3730
  for (let i = 0; i < uncachedIndices.length; i++) {
3626
- results[uncachedIndices[i]] = newEmbeddings[i];
3731
+ const f32Embedding = Float32Array.from(newEmbeddings[i]);
3732
+ results[uncachedIndices[i]] = Array.from(f32Embedding);
3627
3733
  if (cache) {
3628
- cache.set(uncachedTexts[i], newEmbeddings[i]);
3734
+ cache.set(uncachedTexts[i], f32Embedding);
3629
3735
  }
3630
3736
  }
3631
3737
  return results;
@@ -5049,7 +5155,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
5049
5155
  legacy.map((r) => ({ id: r.uniqueId, content: r.content, score: r.similarity }))
5050
5156
  );
5051
5157
  }
5052
- const { recall: recall2 } = await import("./recall-FQU3NUTL.mjs");
5158
+ const { recall: recall2 } = await import("./recall-DJ5LD5WJ.mjs");
5053
5159
  const result = await recall2(
5054
5160
  query,
5055
5161
  { vaultCtx, embeddingOptions, vaultCache: cache },
@@ -5701,6 +5807,7 @@ export {
5701
5807
  getConversationOp,
5702
5808
  getConversationsOp,
5703
5809
  getConversationsLazyOp,
5810
+ getConversationsPageOp,
5704
5811
  getConversationsByProjectLazyOp,
5705
5812
  updateConversationTitleOp,
5706
5813
  deleteConversationOp,