@anuma/sdk 6.4.1 → 6.5.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.
@@ -207,6 +207,166 @@ var init_serverSentEvents_gen = __esm({
207
207
  }
208
208
  });
209
209
 
210
+ // src/lib/chat/toolResultMessage.ts
211
+ function buildToolResultContent(results) {
212
+ const entries = results.map((r) => `Tool "${r.name}" returned: ${JSON.stringify(r.result)}`);
213
+ const framing = HEADER.length + FOOTER.length + ENTRY_SEPARATOR.length * Math.max(0, entries.length - 1);
214
+ const summary = capEntries(entries, MAX_PERSISTED_TOOL_RESULT_CHARS - framing).join(
215
+ ENTRY_SEPARATOR
216
+ );
217
+ return `${HEADER}${summary}${FOOTER}`;
218
+ }
219
+ function capToolResultEntries(entries, budget) {
220
+ return capEntries(entries, budget);
221
+ }
222
+ function capEntries(entries, budget) {
223
+ const smallestFirst = entries.map((_, index) => index).sort((a, b) => entries[a].length - entries[b].length);
224
+ const capped = [...entries];
225
+ let remaining = budget;
226
+ let unassigned = entries.length;
227
+ for (const index of smallestFirst) {
228
+ capped[index] = truncateEntry(entries[index], Math.floor(remaining / unassigned));
229
+ remaining -= capped[index].length;
230
+ unassigned--;
231
+ }
232
+ return capped;
233
+ }
234
+ function truncateEntry(entry, allowance) {
235
+ if (entry.length <= allowance) return entry;
236
+ const keep = allowance - truncationMarker(entry.length).length;
237
+ if (keep <= 0) return entry.slice(0, Math.max(0, allowance));
238
+ return `${entry.slice(0, keep)}${truncationMarker(entry.length - keep)}`;
239
+ }
240
+ function truncationMarker(omitted) {
241
+ return `
242
+
243
+ ... (tool output truncated, ${omitted} characters omitted)`;
244
+ }
245
+ var HEADER, FOOTER, ENTRY_SEPARATOR, TOOL_RESULT_FOOTER_LINE, MAX_PERSISTED_TOOL_RESULT_CHARS;
246
+ var init_toolResultMessage = __esm({
247
+ "src/lib/chat/toolResultMessage.ts"() {
248
+ "use strict";
249
+ HEADER = "[Tool Execution Results]\n\n";
250
+ FOOTER = "\n\nBased on these results, continue with the task.";
251
+ ENTRY_SEPARATOR = "\n\n";
252
+ TOOL_RESULT_FOOTER_LINE = FOOTER.trim();
253
+ MAX_PERSISTED_TOOL_RESULT_CHARS = 1e5;
254
+ }
255
+ });
256
+
257
+ // src/lib/chat/toolResults.ts
258
+ function isToolResultsRow(row) {
259
+ if (row.origin === TOOL_RESULT_ORIGIN) return true;
260
+ if (typeof row.origin === "string") return false;
261
+ if (row.role !== "user" || !row.content.startsWith(TOOL_RESULTS_PREFIX)) return false;
262
+ return parseToolResultSegments(row.content).length > 0;
263
+ }
264
+ function parseToolResultSegments(content) {
265
+ const segments = [];
266
+ for (const raw of content.split("\n")) {
267
+ const line = raw.trim();
268
+ const match = TOOL_LINE.exec(line);
269
+ if (match) {
270
+ segments.push({ name: match[1], line });
271
+ continue;
272
+ }
273
+ const current = segments[segments.length - 1];
274
+ if (!current || line === "" || line === TOOL_RESULT_FOOTER_LINE) continue;
275
+ if (line === TOOL_RESULTS_PREFIX) continue;
276
+ current.line = `${current.line}
277
+ ${line}`;
278
+ }
279
+ return segments;
280
+ }
281
+ function prepareToolResultsForReplay(rows, options) {
282
+ if (options.fold) {
283
+ return foldToolResultsRows(rows, {
284
+ exclude: options.exclude,
285
+ placeholder: options.placeholder
286
+ });
287
+ }
288
+ return rows.filter((row) => !isToolResultsRow(row));
289
+ }
290
+ function foldToolResultsRows(rows, options) {
291
+ const exclude = new Set(options?.exclude ?? []);
292
+ const out = [];
293
+ const pending = [];
294
+ const assistantIndexById = /* @__PURE__ */ new Map();
295
+ for (const row of rows) {
296
+ if (isToolResultsRow(row)) {
297
+ pending.push({ row, insertionPoint: out.length });
298
+ continue;
299
+ }
300
+ out.push(row);
301
+ if (row.role === "assistant" && row.uniqueId) {
302
+ assistantIndexById.set(row.uniqueId, out.length - 1);
303
+ }
304
+ }
305
+ const byTarget = /* @__PURE__ */ new Map();
306
+ for (const { row, insertionPoint } of pending) {
307
+ const target = resolveFoldTarget(out, row, insertionPoint, assistantIndexById);
308
+ if (target === void 0) continue;
309
+ const group = byTarget.get(target);
310
+ if (group) group.push(row);
311
+ else byTarget.set(target, [row]);
312
+ }
313
+ for (const [target, groupedRows] of byTarget) {
314
+ const assistant = out[target];
315
+ const kept = groupedRows.flatMap((row) => parseToolResultSegments(row.content)).filter((segment) => !exclude.has(segment.name));
316
+ if (kept.length > 0) {
317
+ out[target] = { ...assistant, content: appendToolResults(assistant.content, kept) };
318
+ } else if (!assistant.content.trim() && options?.placeholder) {
319
+ out[target] = { ...assistant, content: options.placeholder };
320
+ }
321
+ }
322
+ return out;
323
+ }
324
+ function resolveFoldTarget(out, row, insertionPoint, assistantIndexById) {
325
+ const linked = row.parentMessageId ? assistantIndexById.get(row.parentMessageId) : void 0;
326
+ if (linked !== void 0) return linked;
327
+ let previous;
328
+ for (let i = insertionPoint - 1; i >= 0; i--) {
329
+ if (out[i].role === "assistant") {
330
+ previous = i;
331
+ break;
332
+ }
333
+ }
334
+ let next;
335
+ for (let i = insertionPoint; i < out.length; i++) {
336
+ if (out[i].role === "assistant") {
337
+ next = i;
338
+ break;
339
+ }
340
+ }
341
+ if (previous === void 0) return next;
342
+ if (next === void 0) return previous;
343
+ return next - insertionPoint < insertionPoint - previous ? next : previous;
344
+ }
345
+ function appendToolResults(assistantContent, kept) {
346
+ const framing = TOOL_RESULTS_PREFIX.length + 1 + (kept.length - 1);
347
+ const capped = capToolResultEntries(
348
+ kept.map((segment) => segment.line),
349
+ MAX_FOLDED_APPENDIX_CHARS - framing
350
+ );
351
+ const appendix = `${TOOL_RESULTS_PREFIX}
352
+ ${capped.join("\n")}`;
353
+ return assistantContent.trim() ? `${assistantContent}
354
+
355
+ ${appendix}` : appendix;
356
+ }
357
+ var TOOL_RESULTS_PREFIX, DISPLAY_CARD_PLACEHOLDER, TOOL_LINE, TOOL_RESULT_ORIGIN, MAX_FOLDED_APPENDIX_CHARS;
358
+ var init_toolResults = __esm({
359
+ "src/lib/chat/toolResults.ts"() {
360
+ "use strict";
361
+ init_toolResultMessage();
362
+ TOOL_RESULTS_PREFIX = "[Tool Execution Results]";
363
+ DISPLAY_CARD_PLACEHOLDER = "[displayed a card to the user with the tool result]";
364
+ TOOL_LINE = /^Tool "([^"]+)" returned: (.*)$/;
365
+ TOOL_RESULT_ORIGIN = "tool_result";
366
+ MAX_FOLDED_APPENDIX_CHARS = 2e4;
367
+ }
368
+ });
369
+
210
370
  // src/lib/logger.ts
211
371
  function setLogger(logger) {
212
372
  currentLogger = logger;
@@ -1100,6 +1260,94 @@ var init_vector = __esm({
1100
1260
  }
1101
1261
  });
1102
1262
 
1263
+ // src/lib/processors/encoding.ts
1264
+ async function dataUrlToArrayBuffer(dataUrl) {
1265
+ if (dataUrl.startsWith("blob:") || dataUrl.startsWith("http://") || dataUrl.startsWith("https://")) {
1266
+ const response = await fetch(dataUrl);
1267
+ return response.arrayBuffer();
1268
+ }
1269
+ const base64 = dataUrl.split(",")[1];
1270
+ if (typeof Buffer !== "undefined") {
1271
+ const buf = Buffer.from(base64, "base64");
1272
+ return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
1273
+ }
1274
+ const binary = atob(base64);
1275
+ const buffer = new ArrayBuffer(binary.length);
1276
+ const view = new Uint8Array(buffer);
1277
+ for (let i = 0; i < binary.length; i++) {
1278
+ view[i] = binary.charCodeAt(i);
1279
+ }
1280
+ return buffer;
1281
+ }
1282
+ function uint8ArrayToBase64(data) {
1283
+ if (typeof Buffer !== "undefined") {
1284
+ return Buffer.from(data).toString("base64");
1285
+ }
1286
+ let base64 = "";
1287
+ for (let i = 0; i < data.length; i += B64_ENCODE_CHUNK) {
1288
+ const slice = data.subarray(i, i + B64_ENCODE_CHUNK);
1289
+ let binary = "";
1290
+ for (let j = 0; j < slice.length; j++) {
1291
+ binary += String.fromCharCode(slice[j]);
1292
+ }
1293
+ base64 += btoa(binary);
1294
+ }
1295
+ return base64;
1296
+ }
1297
+ function base64ToUint8Array(base64) {
1298
+ if (typeof Buffer !== "undefined") {
1299
+ const buf = Buffer.from(base64, "base64");
1300
+ return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
1301
+ }
1302
+ const binary = atob(base64);
1303
+ const bytes = new Uint8Array(binary.length);
1304
+ for (let i = 0; i < binary.length; i++) {
1305
+ bytes[i] = binary.charCodeAt(i);
1306
+ }
1307
+ return bytes;
1308
+ }
1309
+ var B64_ENCODE_CHUNK;
1310
+ var init_encoding = __esm({
1311
+ "src/lib/processors/encoding.ts"() {
1312
+ "use strict";
1313
+ B64_ENCODE_CHUNK = 32768 * 3;
1314
+ }
1315
+ });
1316
+
1317
+ // src/lib/memoryEngine/vectorEncoding.ts
1318
+ function encodeChunkVector(vector) {
1319
+ const f32 = vector instanceof Float32Array ? vector : Float32Array.from(vector);
1320
+ return uint8ArrayToBase64(new Uint8Array(f32.buffer, f32.byteOffset, f32.byteLength));
1321
+ }
1322
+ function decodeChunkVector(vector, onMalformed) {
1323
+ if (!vector || vector.length === 0) return new Float32Array(0);
1324
+ if (typeof vector !== "string") return Float32Array.from(vector);
1325
+ if (vector.length % 4 !== 0 || !CANONICAL_BASE64.test(vector)) {
1326
+ onMalformed?.();
1327
+ return new Float32Array(0);
1328
+ }
1329
+ try {
1330
+ const bytes = base64ToUint8Array(vector);
1331
+ if (bytes.byteLength === 0 || bytes.byteLength % BYTES_PER_FLOAT32 !== 0) {
1332
+ onMalformed?.();
1333
+ return new Float32Array(0);
1334
+ }
1335
+ return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / BYTES_PER_FLOAT32);
1336
+ } catch {
1337
+ onMalformed?.();
1338
+ return new Float32Array(0);
1339
+ }
1340
+ }
1341
+ var BYTES_PER_FLOAT32, CANONICAL_BASE64;
1342
+ var init_vectorEncoding = __esm({
1343
+ "src/lib/memoryEngine/vectorEncoding.ts"() {
1344
+ "use strict";
1345
+ init_encoding();
1346
+ BYTES_PER_FLOAT32 = 4;
1347
+ CANONICAL_BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
1348
+ }
1349
+ });
1350
+
1103
1351
  // src/lib/db/encryption-utils.ts
1104
1352
  function isEncrypted(value) {
1105
1353
  const prefix = value.startsWith("enc:v3:") ? "enc:v3:" : value.startsWith("enc:v2:") ? "enc:v2:" : null;
@@ -2252,6 +2500,7 @@ async function searchChunksOp(ctx, queryVector, options) {
2252
2500
  const messages = await ctx.messagesCollection.query(...queryConditions).fetch();
2253
2501
  const candidates = [];
2254
2502
  let staleSkipped = 0;
2503
+ let unreadableVectors = 0;
2255
2504
  for (const message of messages) {
2256
2505
  const msgConvId = String(message._getRaw("conversation_id"));
2257
2506
  if (!activeConversationIds.has(msgConvId)) continue;
@@ -2289,7 +2538,9 @@ async function searchChunksOp(ctx, queryVector, options) {
2289
2538
  const chunks = await readJsonField(message, "chunks", ctx.walletAddress);
2290
2539
  if (chunks && chunks.length > 0) {
2291
2540
  const vectors = chunks.map(
2292
- (chunk) => chunk.vector && chunk.vector.length > 0 ? Float32Array.from(chunk.vector) : new Float32Array(0)
2541
+ (chunk) => decodeChunkVector(chunk.vector, () => {
2542
+ unreadableVectors++;
2543
+ })
2293
2544
  );
2294
2545
  chunkCache?.set(message.id, { version, chunks: vectors });
2295
2546
  for (let ci = 0; ci < chunks.length; ci++) {
@@ -2320,6 +2571,11 @@ async function searchChunksOp(ctx, queryVector, options) {
2320
2571
  `searchChunksOp: skipped ${staleSkipped} messages whose embedding model differs from the current model (${embeddingModel}) \u2014 re-embed via chunkAndEmbedAllMessages`
2321
2572
  );
2322
2573
  }
2574
+ if (unreadableVectors > 0) {
2575
+ getLogger().warn(
2576
+ `searchChunksOp: could not read ${unreadableVectors} chunk vectors and left them out of ranking \u2014 the stored value is corrupt, so those chunks stay unsearchable until the message is re-embedded via chunkAndEmbedAllMessages`
2577
+ );
2578
+ }
2323
2579
  const topK = candidates.sort((a, b) => b.similarity - a.similarity).slice(0, limit);
2324
2580
  const uniqueMessages = /* @__PURE__ */ new Map();
2325
2581
  for (const c of topK) uniqueMessages.set(c.message.id, c.message);
@@ -2442,6 +2698,7 @@ var init_operations = __esm({
2442
2698
  init_useEncryption();
2443
2699
  init_logger();
2444
2700
  init_vector();
2701
+ init_vectorEncoding();
2445
2702
  init_encryption_utils();
2446
2703
  init_conversationEncryption();
2447
2704
  init_encryption();
@@ -3625,6 +3882,11 @@ var init_generate = __esm({
3625
3882
  });
3626
3883
 
3627
3884
  // src/lib/memoryEngine/embeddings.ts
3885
+ function isNonEmbeddableOrigin(origin, reembedDiscarded = false) {
3886
+ if (typeof origin !== "string") return false;
3887
+ if (reembedDiscarded && origin === CHUNKS_DISCARDED_ORIGIN) return false;
3888
+ return NON_EMBEDDABLE_ORIGINS.has(origin);
3889
+ }
3628
3890
  async function embedMessage(ctx, messageId, options) {
3629
3891
  const message = await getMessageOp(ctx, messageId);
3630
3892
  if (!message) {
@@ -3633,7 +3895,7 @@ async function embedMessage(ctx, messageId, options) {
3633
3895
  if (message.vector && message.vector.length > 0) {
3634
3896
  return message;
3635
3897
  }
3636
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3898
+ if (isNonEmbeddableOrigin(message.origin)) {
3637
3899
  return message;
3638
3900
  }
3639
3901
  if (isEncrypted(message.content)) {
@@ -3669,7 +3931,7 @@ async function embedAllMessages(ctx, options, filter) {
3669
3931
  if (message.role === "system") {
3670
3932
  continue;
3671
3933
  }
3672
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3934
+ if (isNonEmbeddableOrigin(message.origin, filter?.reembedDiscarded)) {
3673
3935
  continue;
3674
3936
  }
3675
3937
  considered++;
@@ -3709,7 +3971,7 @@ async function chunkAndEmbedMessage(ctx, messageId, options) {
3709
3971
  if (message.chunks && message.chunks.length > 0) {
3710
3972
  return message;
3711
3973
  }
3712
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3974
+ if (isNonEmbeddableOrigin(message.origin)) {
3713
3975
  return message;
3714
3976
  }
3715
3977
  if (isEncrypted(message.content)) {
@@ -3757,7 +4019,7 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
3757
4019
  if (hasVector && !filter?.rechunkExisting && !isStale) continue;
3758
4020
  if (filter?.roles && !filter.roles.includes(message.role)) continue;
3759
4021
  if (message.role === "system") continue;
3760
- if (message.origin === NON_EMBEDDABLE_ORIGIN) continue;
4022
+ if (isNonEmbeddableOrigin(message.origin, filter?.reembedDiscarded)) continue;
3761
4023
  considered++;
3762
4024
  if (isEncrypted(message.content)) {
3763
4025
  stillEncrypted++;
@@ -3826,10 +4088,11 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
3826
4088
  }
3827
4089
  return embeddedCount;
3828
4090
  }
3829
- var DEFAULT_MIN_CONTENT_LENGTH, NON_EMBEDDABLE_ORIGIN;
4091
+ var DEFAULT_MIN_CONTENT_LENGTH, CHUNKS_DISCARDED_ORIGIN, NON_EMBEDDABLE_ORIGINS;
3830
4092
  var init_embeddings = __esm({
3831
4093
  "src/lib/memoryEngine/embeddings.ts"() {
3832
4094
  "use strict";
4095
+ init_toolResults();
3833
4096
  init_operations();
3834
4097
  init_encryption_utils();
3835
4098
  init_logger();
@@ -3838,7 +4101,8 @@ var init_embeddings = __esm({
3838
4101
  init_generate();
3839
4102
  init_generate();
3840
4103
  DEFAULT_MIN_CONTENT_LENGTH = 10;
3841
- NON_EMBEDDABLE_ORIGIN = "tool_result";
4104
+ CHUNKS_DISCARDED_ORIGIN = "chunks_discarded";
4105
+ NON_EMBEDDABLE_ORIGINS = /* @__PURE__ */ new Set([TOOL_RESULT_ORIGIN, CHUNKS_DISCARDED_ORIGIN]);
3842
4106
  }
3843
4107
  });
3844
4108
 
@@ -9367,6 +9631,7 @@ __export(index_exports, {
9367
9631
  BUILT_IN_TOOL_SETS: () => BUILT_IN_TOOL_SETS,
9368
9632
  BackupAuthProvider: () => BackupAuthProvider,
9369
9633
  BlobUrlManager: () => BlobUrlManager,
9634
+ CHUNKS_DISCARDED_ORIGIN: () => CHUNKS_DISCARDED_ORIGIN,
9370
9635
  ChartCard: () => ChartCard,
9371
9636
  ChartContainer: () => ChartContainer,
9372
9637
  ChartLegend: () => ChartLegend,
@@ -9443,6 +9708,7 @@ __export(index_exports, {
9443
9708
  StoredVaultMemoryModel: () => VaultMemory,
9444
9709
  StreamExpiredError: () => StreamExpiredError,
9445
9710
  THEME_ATTRS: () => THEME_ATTRS,
9711
+ TOOL_RESULT_ORIGIN: () => TOOL_RESULT_ORIGIN,
9446
9712
  TOPICS_EXTRACTION_VERSION: () => TOPICS_EXTRACTION_VERSION,
9447
9713
  TOPIC_EXTRACTION_BATCH_SIZE: () => TOPIC_EXTRACTION_BATCH_SIZE,
9448
9714
  TREND_RECENT_WINDOW_DAYS: () => TREND_RECENT_WINDOW_DAYS,
@@ -9520,6 +9786,7 @@ __export(index_exports, {
9520
9786
  createVaultMemoryOp: () => createVaultMemoryOp,
9521
9787
  createWeatherPreProcessor: () => createWeatherPreProcessor,
9522
9788
  createWebSearchPreProcessor: () => createWebSearchPreProcessor,
9789
+ decodeChunkVector: () => decodeChunkVector,
9523
9790
  decryptConversationTitle: () => decryptConversationTitle,
9524
9791
  decryptData: () => decryptData,
9525
9792
  decryptDataBatch: () => decryptDataBatch,
@@ -9542,6 +9809,7 @@ __export(index_exports, {
9542
9809
  eagerEmbedContent: () => eagerEmbedContent,
9543
9810
  embedAllMessages: () => embedAllMessages,
9544
9811
  embedMessage: () => embedMessage,
9812
+ encodeChunkVector: () => encodeChunkVector,
9545
9813
  encryptData: () => encryptData,
9546
9814
  encryptDataBatch: () => encryptDataBatch,
9547
9815
  encryptDataBytes: () => encryptDataBytes,
@@ -14981,152 +15249,9 @@ async function cleanupConversationSummary(database, conversationId) {
14981
15249
  }
14982
15250
  }
14983
15251
 
14984
- // src/lib/chat/toolResultMessage.ts
14985
- var HEADER = "[Tool Execution Results]\n\n";
14986
- var FOOTER = "\n\nBased on these results, continue with the task.";
14987
- var ENTRY_SEPARATOR = "\n\n";
14988
- var TOOL_RESULT_FOOTER_LINE = FOOTER.trim();
14989
- var MAX_PERSISTED_TOOL_RESULT_CHARS = 1e5;
14990
- function buildToolResultContent(results) {
14991
- const entries = results.map((r) => `Tool "${r.name}" returned: ${JSON.stringify(r.result)}`);
14992
- const framing = HEADER.length + FOOTER.length + ENTRY_SEPARATOR.length * Math.max(0, entries.length - 1);
14993
- const summary = capEntries(entries, MAX_PERSISTED_TOOL_RESULT_CHARS - framing).join(
14994
- ENTRY_SEPARATOR
14995
- );
14996
- return `${HEADER}${summary}${FOOTER}`;
14997
- }
14998
- function capToolResultEntries(entries, budget) {
14999
- return capEntries(entries, budget);
15000
- }
15001
- function capEntries(entries, budget) {
15002
- const smallestFirst = entries.map((_, index) => index).sort((a, b) => entries[a].length - entries[b].length);
15003
- const capped = [...entries];
15004
- let remaining = budget;
15005
- let unassigned = entries.length;
15006
- for (const index of smallestFirst) {
15007
- capped[index] = truncateEntry(entries[index], Math.floor(remaining / unassigned));
15008
- remaining -= capped[index].length;
15009
- unassigned--;
15010
- }
15011
- return capped;
15012
- }
15013
- function truncateEntry(entry, allowance) {
15014
- if (entry.length <= allowance) return entry;
15015
- const keep = allowance - truncationMarker(entry.length).length;
15016
- if (keep <= 0) return entry.slice(0, Math.max(0, allowance));
15017
- return `${entry.slice(0, keep)}${truncationMarker(entry.length - keep)}`;
15018
- }
15019
- function truncationMarker(omitted) {
15020
- return `
15021
-
15022
- ... (tool output truncated, ${omitted} characters omitted)`;
15023
- }
15024
-
15025
- // src/lib/chat/toolResults.ts
15026
- var TOOL_RESULTS_PREFIX = "[Tool Execution Results]";
15027
- var DISPLAY_CARD_PLACEHOLDER = "[displayed a card to the user with the tool result]";
15028
- var TOOL_LINE = /^Tool "([^"]+)" returned: (.*)$/;
15029
- var TOOL_RESULT_ORIGIN = "tool_result";
15030
- function isToolResultsRow(row) {
15031
- if (row.origin === TOOL_RESULT_ORIGIN) return true;
15032
- if (typeof row.origin === "string") return false;
15033
- if (row.role !== "user" || !row.content.startsWith(TOOL_RESULTS_PREFIX)) return false;
15034
- return parseToolResultSegments(row.content).length > 0;
15035
- }
15036
- function parseToolResultSegments(content) {
15037
- const segments = [];
15038
- for (const raw of content.split("\n")) {
15039
- const line = raw.trim();
15040
- const match = TOOL_LINE.exec(line);
15041
- if (match) {
15042
- segments.push({ name: match[1], line });
15043
- continue;
15044
- }
15045
- const current = segments[segments.length - 1];
15046
- if (!current || line === "" || line === TOOL_RESULT_FOOTER_LINE) continue;
15047
- if (line === TOOL_RESULTS_PREFIX) continue;
15048
- current.line = `${current.line}
15049
- ${line}`;
15050
- }
15051
- return segments;
15052
- }
15053
- function prepareToolResultsForReplay(rows, options) {
15054
- if (options.fold) {
15055
- return foldToolResultsRows(rows, {
15056
- exclude: options.exclude,
15057
- placeholder: options.placeholder
15058
- });
15059
- }
15060
- return rows.filter((row) => !isToolResultsRow(row));
15061
- }
15062
- function foldToolResultsRows(rows, options) {
15063
- const exclude = new Set(options?.exclude ?? []);
15064
- const out = [];
15065
- const pending = [];
15066
- const assistantIndexById = /* @__PURE__ */ new Map();
15067
- for (const row of rows) {
15068
- if (isToolResultsRow(row)) {
15069
- pending.push({ row, insertionPoint: out.length });
15070
- continue;
15071
- }
15072
- out.push(row);
15073
- if (row.role === "assistant" && row.uniqueId) {
15074
- assistantIndexById.set(row.uniqueId, out.length - 1);
15075
- }
15076
- }
15077
- const byTarget = /* @__PURE__ */ new Map();
15078
- for (const { row, insertionPoint } of pending) {
15079
- const target = resolveFoldTarget(out, row, insertionPoint, assistantIndexById);
15080
- if (target === void 0) continue;
15081
- const group = byTarget.get(target);
15082
- if (group) group.push(row);
15083
- else byTarget.set(target, [row]);
15084
- }
15085
- for (const [target, groupedRows] of byTarget) {
15086
- const assistant = out[target];
15087
- const kept = groupedRows.flatMap((row) => parseToolResultSegments(row.content)).filter((segment) => !exclude.has(segment.name));
15088
- if (kept.length > 0) {
15089
- out[target] = { ...assistant, content: appendToolResults(assistant.content, kept) };
15090
- } else if (!assistant.content.trim() && options?.placeholder) {
15091
- out[target] = { ...assistant, content: options.placeholder };
15092
- }
15093
- }
15094
- return out;
15095
- }
15096
- function resolveFoldTarget(out, row, insertionPoint, assistantIndexById) {
15097
- const linked = row.parentMessageId ? assistantIndexById.get(row.parentMessageId) : void 0;
15098
- if (linked !== void 0) return linked;
15099
- let previous;
15100
- for (let i = insertionPoint - 1; i >= 0; i--) {
15101
- if (out[i].role === "assistant") {
15102
- previous = i;
15103
- break;
15104
- }
15105
- }
15106
- let next;
15107
- for (let i = insertionPoint; i < out.length; i++) {
15108
- if (out[i].role === "assistant") {
15109
- next = i;
15110
- break;
15111
- }
15112
- }
15113
- if (previous === void 0) return next;
15114
- if (next === void 0) return previous;
15115
- return next - insertionPoint < insertionPoint - previous ? next : previous;
15116
- }
15117
- var MAX_FOLDED_APPENDIX_CHARS = 2e4;
15118
- function appendToolResults(assistantContent, kept) {
15119
- const framing = TOOL_RESULTS_PREFIX.length + 1 + (kept.length - 1);
15120
- const capped = capToolResultEntries(
15121
- kept.map((segment) => segment.line),
15122
- MAX_FOLDED_APPENDIX_CHARS - framing
15123
- );
15124
- const appendix = `${TOOL_RESULTS_PREFIX}
15125
- ${capped.join("\n")}`;
15126
- return assistantContent.trim() ? `${assistantContent}
15127
-
15128
- ${appendix}` : appendix;
15129
- }
15252
+ // src/react/useChatStorage.ts
15253
+ init_toolResultMessage();
15254
+ init_toolResults();
15130
15255
 
15131
15256
  // src/lib/db/chat/lazyDecrypt.ts
15132
15257
  init_useEncryption();
@@ -20021,6 +20146,9 @@ function createMemoryEngineTool(storageCtx, embeddingOptions, searchOptions, cal
20021
20146
  };
20022
20147
  }
20023
20148
 
20149
+ // src/lib/memoryEngine/index.ts
20150
+ init_vectorEncoding();
20151
+
20024
20152
  // src/react/useChatStorage.ts
20025
20153
  init_constants();
20026
20154
 
@@ -20198,56 +20326,7 @@ init_redactor();
20198
20326
 
20199
20327
  // src/lib/processors/ExcelProcessor.ts
20200
20328
  init_logger();
20201
-
20202
- // src/lib/processors/encoding.ts
20203
- var B64_ENCODE_CHUNK = 32768 * 3;
20204
- async function dataUrlToArrayBuffer(dataUrl) {
20205
- if (dataUrl.startsWith("blob:") || dataUrl.startsWith("http://") || dataUrl.startsWith("https://")) {
20206
- const response = await fetch(dataUrl);
20207
- return response.arrayBuffer();
20208
- }
20209
- const base64 = dataUrl.split(",")[1];
20210
- if (typeof Buffer !== "undefined") {
20211
- const buf = Buffer.from(base64, "base64");
20212
- return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
20213
- }
20214
- const binary = atob(base64);
20215
- const buffer = new ArrayBuffer(binary.length);
20216
- const view = new Uint8Array(buffer);
20217
- for (let i = 0; i < binary.length; i++) {
20218
- view[i] = binary.charCodeAt(i);
20219
- }
20220
- return buffer;
20221
- }
20222
- function uint8ArrayToBase64(data) {
20223
- if (typeof Buffer !== "undefined") {
20224
- return Buffer.from(data).toString("base64");
20225
- }
20226
- let base64 = "";
20227
- for (let i = 0; i < data.length; i += B64_ENCODE_CHUNK) {
20228
- const slice = data.subarray(i, i + B64_ENCODE_CHUNK);
20229
- let binary = "";
20230
- for (let j = 0; j < slice.length; j++) {
20231
- binary += String.fromCharCode(slice[j]);
20232
- }
20233
- base64 += btoa(binary);
20234
- }
20235
- return base64;
20236
- }
20237
- function base64ToUint8Array(base64) {
20238
- if (typeof Buffer !== "undefined") {
20239
- const buf = Buffer.from(base64, "base64");
20240
- return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
20241
- }
20242
- const binary = atob(base64);
20243
- const bytes = new Uint8Array(binary.length);
20244
- for (let i = 0; i < binary.length; i++) {
20245
- bytes[i] = binary.charCodeAt(i);
20246
- }
20247
- return bytes;
20248
- }
20249
-
20250
- // src/lib/processors/ExcelProcessor.ts
20329
+ init_encoding();
20251
20330
  if (typeof process !== "undefined" && typeof process.umask !== "function") {
20252
20331
  process.umask = (_mask) => 18;
20253
20332
  }
@@ -20426,6 +20505,7 @@ var PdfProcessor = class {
20426
20505
 
20427
20506
  // src/lib/processors/TextProcessor.ts
20428
20507
  init_logger();
20508
+ init_encoding();
20429
20509
  var MIME = {
20430
20510
  PLAIN: "text/plain",
20431
20511
  MARKDOWN: "text/markdown",
@@ -20509,6 +20589,7 @@ var TextProcessor = class {
20509
20589
  // src/lib/processors/WordProcessor.ts
20510
20590
  var import_mammoth = __toESM(require("mammoth"));
20511
20591
  init_logger();
20592
+ init_encoding();
20512
20593
  var WordProcessor = class {
20513
20594
  constructor() {
20514
20595
  this.name = "word";
@@ -20552,6 +20633,7 @@ var WordProcessor = class {
20552
20633
  // src/lib/processors/ZipProcessor.ts
20553
20634
  var import_jszip = __toESM(require("jszip"));
20554
20635
  init_logger();
20636
+ init_encoding();
20555
20637
  var ZipProcessor = class {
20556
20638
  constructor(options = {}) {
20557
20639
  this.name = "zip";
@@ -29218,6 +29300,9 @@ function useWalletBinding(options = {}) {
29218
29300
  };
29219
29301
  }
29220
29302
 
29303
+ // src/react/index.ts
29304
+ init_toolResults();
29305
+
29221
29306
  // src/react/useDropboxAuth.ts
29222
29307
  var import_react19 = require("react");
29223
29308
 
@@ -30758,6 +30843,7 @@ var import_react24 = require("react");
30758
30843
 
30759
30844
  // src/lib/backup/icloud/api.ts
30760
30845
  init_logger();
30846
+ init_encoding();
30761
30847
  var CLOUDKIT_JS_URL = "https://cdn.apple-cloudkit.com/ck/2/cloudkit.js";
30762
30848
  var DEFAULT_BACKUP_FOLDER2 = "conversations";
30763
30849
  var DEFAULT_CONTAINER_ID = "iCloud.Memoryless";
@@ -34937,6 +35023,7 @@ function renderAnumaJsx(jsx3) {
34937
35023
  BUILT_IN_TOOL_SETS,
34938
35024
  BackupAuthProvider,
34939
35025
  BlobUrlManager,
35026
+ CHUNKS_DISCARDED_ORIGIN,
34940
35027
  ChartCard,
34941
35028
  ChartContainer,
34942
35029
  ChartLegend,
@@ -35013,6 +35100,7 @@ function renderAnumaJsx(jsx3) {
35013
35100
  StoredVaultMemoryModel,
35014
35101
  StreamExpiredError,
35015
35102
  THEME_ATTRS,
35103
+ TOOL_RESULT_ORIGIN,
35016
35104
  TOPICS_EXTRACTION_VERSION,
35017
35105
  TOPIC_EXTRACTION_BATCH_SIZE,
35018
35106
  TREND_RECENT_WINDOW_DAYS,
@@ -35090,6 +35178,7 @@ function renderAnumaJsx(jsx3) {
35090
35178
  createVaultMemoryOp,
35091
35179
  createWeatherPreProcessor,
35092
35180
  createWebSearchPreProcessor,
35181
+ decodeChunkVector,
35093
35182
  decryptConversationTitle,
35094
35183
  decryptData,
35095
35184
  decryptDataBatch,
@@ -35112,6 +35201,7 @@ function renderAnumaJsx(jsx3) {
35112
35201
  eagerEmbedContent,
35113
35202
  embedAllMessages,
35114
35203
  embedMessage,
35204
+ encodeChunkVector,
35115
35205
  encryptData,
35116
35206
  encryptDataBatch,
35117
35207
  encryptDataBytes,