@anuma/sdk 6.4.2 → 6.6.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.
Files changed (32) hide show
  1. package/dist/expo/{chunk-WYARTNEN.mjs → chunk-4M6W7QLZ.mjs} +233 -8
  2. package/dist/{react/chunk-ILAWDQDE.mjs → expo/chunk-B5UPJRCU.mjs} +16 -1
  3. package/dist/expo/{chunk-DT6QNRZL.mjs → chunk-VOMDHMKH.mjs} +1 -1
  4. package/dist/{react/consolidate-AMPKH6YJ.mjs → expo/consolidate-TWE6OSSZ.mjs} +2 -2
  5. package/dist/expo/index.cjs +306 -156
  6. package/dist/expo/index.d.mts +163 -7
  7. package/dist/expo/index.d.ts +163 -7
  8. package/dist/expo/index.mjs +33 -154
  9. package/dist/expo/{recall-7FOWWX5T.mjs → recall-EZIHN5VU.mjs} +2 -2
  10. package/dist/index.cjs +71 -0
  11. package/dist/index.d.mts +121 -1
  12. package/dist/index.d.ts +121 -1
  13. package/dist/index.mjs +67 -0
  14. package/dist/react/{chunk-VMI74IUX.mjs → chunk-2ARMO2NN.mjs} +1 -1
  15. package/dist/{expo/chunk-ILAWDQDE.mjs → react/chunk-B5UPJRCU.mjs} +16 -1
  16. package/dist/react/{chunk-IO4ZCPK2.mjs → chunk-S6WPXD5N.mjs} +254 -8
  17. package/dist/{expo/consolidate-3MEWQNM3.mjs → react/consolidate-ZCGS7QUA.mjs} +2 -2
  18. package/dist/react/index.cjs +331 -206
  19. package/dist/react/index.d.mts +163 -7
  20. package/dist/react/index.d.ts +163 -7
  21. package/dist/react/index.mjs +36 -202
  22. package/dist/react/{recall-2J2755WC.mjs → recall-ROEK55FJ.mjs} +2 -2
  23. package/dist/server/{chunk-KF4HV3QE.mjs → chunk-CR5P3YIK.mjs} +1 -1
  24. package/dist/server/{chunk-EKYGWD5N.mjs → chunk-TD7SMY7C.mjs} +98 -8
  25. package/dist/server/{chunk-OBDRKQQI.mjs → chunk-VHWLLYKO.mjs} +16 -1
  26. package/dist/server/{consolidate-I6TOH7CB.mjs → consolidate-FWYK5KXB.mjs} +2 -2
  27. package/dist/server/index.cjs +158 -48
  28. package/dist/server/index.d.mts +54 -6
  29. package/dist/server/index.d.ts +54 -6
  30. package/dist/server/index.mjs +24 -43
  31. package/dist/server/{recall-IVV6EILK.mjs → recall-CGDEW2LQ.mjs} +2 -2
  32. package/package.json +1 -1
@@ -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
 
@@ -4517,6 +4781,20 @@ var init_endpointOverride = __esm({
4517
4781
  }
4518
4782
  });
4519
4783
 
4784
+ // src/lib/internalFlowMarker.ts
4785
+ function withInternalFlowMarker(systemPrompt) {
4786
+ if (systemPrompt.includes(INTERNAL_FLOW_MARKER)) return systemPrompt;
4787
+ return `${INTERNAL_FLOW_MARKER}
4788
+ ${systemPrompt}`;
4789
+ }
4790
+ var INTERNAL_FLOW_MARKER;
4791
+ var init_internalFlowMarker = __esm({
4792
+ "src/lib/internalFlowMarker.ts"() {
4793
+ "use strict";
4794
+ INTERNAL_FLOW_MARKER = "Anuma internal first-party flow (not user chat).";
4795
+ }
4796
+ });
4797
+
4520
4798
  // src/lib/db/entities/types.ts
4521
4799
  function normalizeEntityName(name) {
4522
4800
  return name.trim().toLowerCase();
@@ -6195,7 +6473,12 @@ async function attemptPortalJson(req, endpoint) {
6195
6473
  const isAnthropic = req.model.startsWith("anthropic/");
6196
6474
  const prefill = isAnthropic ? "{" : "";
6197
6475
  const messages = [
6198
- { role: "system", content: req.systemPrompt },
6476
+ // Every caller of this helper is a first-party BACKGROUND op (fact extraction,
6477
+ // topic sweep, consolidation, classifiers, graph traversal, query
6478
+ // decomposition). Marking here — the one place they all funnel through — is what
6479
+ // keeps the portal's detector reading them as genuine rather than markerless.
6480
+ // See ../internalFlowMarker.
6481
+ { role: "system", content: withInternalFlowMarker(req.systemPrompt) },
6199
6482
  { role: "user", content: req.userMessage }
6200
6483
  ];
6201
6484
  if (prefill) messages.push({ role: "assistant", content: prefill });
@@ -6333,6 +6616,7 @@ var init_portalLlm = __esm({
6333
6616
  "use strict";
6334
6617
  init_clientConfig();
6335
6618
  init_endpointOverride();
6619
+ init_internalFlowMarker();
6336
6620
  init_logger();
6337
6621
  RESPONSE_FORMAT_OK = /* @__PURE__ */ new Set(["openai", "inclusionai", "deepseek"]);
6338
6622
  RESPONSE_SCHEMA_OK = /* @__PURE__ */ new Set(["openai"]);
@@ -9367,6 +9651,7 @@ __export(index_exports, {
9367
9651
  BUILT_IN_TOOL_SETS: () => BUILT_IN_TOOL_SETS,
9368
9652
  BackupAuthProvider: () => BackupAuthProvider,
9369
9653
  BlobUrlManager: () => BlobUrlManager,
9654
+ CHUNKS_DISCARDED_ORIGIN: () => CHUNKS_DISCARDED_ORIGIN,
9370
9655
  ChartCard: () => ChartCard,
9371
9656
  ChartContainer: () => ChartContainer,
9372
9657
  ChartLegend: () => ChartLegend,
@@ -9443,6 +9728,7 @@ __export(index_exports, {
9443
9728
  StoredVaultMemoryModel: () => VaultMemory,
9444
9729
  StreamExpiredError: () => StreamExpiredError,
9445
9730
  THEME_ATTRS: () => THEME_ATTRS,
9731
+ TOOL_RESULT_ORIGIN: () => TOOL_RESULT_ORIGIN,
9446
9732
  TOPICS_EXTRACTION_VERSION: () => TOPICS_EXTRACTION_VERSION,
9447
9733
  TOPIC_EXTRACTION_BATCH_SIZE: () => TOPIC_EXTRACTION_BATCH_SIZE,
9448
9734
  TREND_RECENT_WINDOW_DAYS: () => TREND_RECENT_WINDOW_DAYS,
@@ -9520,6 +9806,7 @@ __export(index_exports, {
9520
9806
  createVaultMemoryOp: () => createVaultMemoryOp,
9521
9807
  createWeatherPreProcessor: () => createWeatherPreProcessor,
9522
9808
  createWebSearchPreProcessor: () => createWebSearchPreProcessor,
9809
+ decodeChunkVector: () => decodeChunkVector,
9523
9810
  decryptConversationTitle: () => decryptConversationTitle,
9524
9811
  decryptData: () => decryptData,
9525
9812
  decryptDataBatch: () => decryptDataBatch,
@@ -9542,6 +9829,7 @@ __export(index_exports, {
9542
9829
  eagerEmbedContent: () => eagerEmbedContent,
9543
9830
  embedAllMessages: () => embedAllMessages,
9544
9831
  embedMessage: () => embedMessage,
9832
+ encodeChunkVector: () => encodeChunkVector,
9545
9833
  encryptData: () => encryptData,
9546
9834
  encryptDataBatch: () => encryptDataBatch,
9547
9835
  encryptDataBytes: () => encryptDataBytes,
@@ -14585,6 +14873,7 @@ async function deleteConversationSummaryOp(ctx, conversationId) {
14585
14873
  }
14586
14874
 
14587
14875
  // src/lib/chat/summarize.ts
14876
+ init_internalFlowMarker();
14588
14877
  init_logger();
14589
14878
  var DEFAULT_SUMMARY_TOKEN_THRESHOLD = 4e3;
14590
14879
  var DEFAULT_SUMMARY_MIN_WINDOW_MESSAGES = 4;
@@ -14777,7 +15066,17 @@ async function callSummarizationLlm(prompt, model, token, baseUrl) {
14777
15066
  body: JSON.stringify({
14778
15067
  model,
14779
15068
  stream: false,
14780
- messages: [{ role: "user", content: prompt }]
15069
+ // The marker rides a system message because this call has no other
15070
+ // provenance: it posts to the MAIN chat endpoint (not /utility/*), carries no
15071
+ // conversationId, and its prompt is neither the app's chat base prompt nor any
15072
+ // registered flow fingerprint — so without this it reads as markerless, i.e.
15073
+ // as a scripted abuser. Progressive summarization runs for every free-tier
15074
+ // long conversation, so at the PORTAL_DETECTION_REJECT_MARKERLESS flip it
15075
+ // would 403 and silently fall back to verbatim history (context bloat + cost).
15076
+ messages: [
15077
+ { role: "system", content: INTERNAL_FLOW_MARKER },
15078
+ { role: "user", content: prompt }
15079
+ ]
14781
15080
  }),
14782
15081
  signal: controller.signal
14783
15082
  });
@@ -14981,152 +15280,9 @@ async function cleanupConversationSummary(database, conversationId) {
14981
15280
  }
14982
15281
  }
14983
15282
 
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
- }
15283
+ // src/react/useChatStorage.ts
15284
+ init_toolResultMessage();
15285
+ init_toolResults();
15130
15286
 
15131
15287
  // src/lib/db/chat/lazyDecrypt.ts
15132
15288
  init_useEncryption();
@@ -18856,6 +19012,7 @@ init_reranker();
18856
19012
 
18857
19013
  // src/lib/memory/synthesizeProfile.ts
18858
19014
  init_operations3();
19015
+ init_internalFlowMarker();
18859
19016
  init_logger();
18860
19017
  init_constants();
18861
19018
  init_embeddings();
@@ -19217,7 +19374,10 @@ async function synthesizeFacet(facet, ctx, options, prior) {
19217
19374
  limit,
19218
19375
  types: ["fact"],
19219
19376
  maxTokens: DEFAULT_FACET_MAX_TOKENS,
19220
- systemPrompt: buildFacetSystemPrompt(facet),
19377
+ // Profile-facet synthesis is a background op, so it marks its own prompt.
19378
+ // reflect() itself must NOT mark — it also serves the user's own questions,
19379
+ // and that traffic is chat, not an internal flow. See ../internalFlowMarker.
19380
+ systemPrompt: withInternalFlowMarker(buildFacetSystemPrompt(facet)),
19221
19381
  responseSchema: facetResponseSchema(facet.key),
19222
19382
  memories
19223
19383
  });
@@ -20021,6 +20181,9 @@ function createMemoryEngineTool(storageCtx, embeddingOptions, searchOptions, cal
20021
20181
  };
20022
20182
  }
20023
20183
 
20184
+ // src/lib/memoryEngine/index.ts
20185
+ init_vectorEncoding();
20186
+
20024
20187
  // src/react/useChatStorage.ts
20025
20188
  init_constants();
20026
20189
 
@@ -20198,56 +20361,7 @@ init_redactor();
20198
20361
 
20199
20362
  // src/lib/processors/ExcelProcessor.ts
20200
20363
  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
20364
+ init_encoding();
20251
20365
  if (typeof process !== "undefined" && typeof process.umask !== "function") {
20252
20366
  process.umask = (_mask) => 18;
20253
20367
  }
@@ -20426,6 +20540,7 @@ var PdfProcessor = class {
20426
20540
 
20427
20541
  // src/lib/processors/TextProcessor.ts
20428
20542
  init_logger();
20543
+ init_encoding();
20429
20544
  var MIME = {
20430
20545
  PLAIN: "text/plain",
20431
20546
  MARKDOWN: "text/markdown",
@@ -20509,6 +20624,7 @@ var TextProcessor = class {
20509
20624
  // src/lib/processors/WordProcessor.ts
20510
20625
  var import_mammoth = __toESM(require("mammoth"));
20511
20626
  init_logger();
20627
+ init_encoding();
20512
20628
  var WordProcessor = class {
20513
20629
  constructor() {
20514
20630
  this.name = "word";
@@ -20552,6 +20668,7 @@ var WordProcessor = class {
20552
20668
  // src/lib/processors/ZipProcessor.ts
20553
20669
  var import_jszip = __toESM(require("jszip"));
20554
20670
  init_logger();
20671
+ init_encoding();
20555
20672
  var ZipProcessor = class {
20556
20673
  constructor(options = {}) {
20557
20674
  this.name = "zip";
@@ -29218,6 +29335,9 @@ function useWalletBinding(options = {}) {
29218
29335
  };
29219
29336
  }
29220
29337
 
29338
+ // src/react/index.ts
29339
+ init_toolResults();
29340
+
29221
29341
  // src/react/useDropboxAuth.ts
29222
29342
  var import_react19 = require("react");
29223
29343
 
@@ -30758,6 +30878,7 @@ var import_react24 = require("react");
30758
30878
 
30759
30879
  // src/lib/backup/icloud/api.ts
30760
30880
  init_logger();
30881
+ init_encoding();
30761
30882
  var CLOUDKIT_JS_URL = "https://cdn.apple-cloudkit.com/ck/2/cloudkit.js";
30762
30883
  var DEFAULT_BACKUP_FOLDER2 = "conversations";
30763
30884
  var DEFAULT_CONTAINER_ID = "iCloud.Memoryless";
@@ -34937,6 +35058,7 @@ function renderAnumaJsx(jsx3) {
34937
35058
  BUILT_IN_TOOL_SETS,
34938
35059
  BackupAuthProvider,
34939
35060
  BlobUrlManager,
35061
+ CHUNKS_DISCARDED_ORIGIN,
34940
35062
  ChartCard,
34941
35063
  ChartContainer,
34942
35064
  ChartLegend,
@@ -35013,6 +35135,7 @@ function renderAnumaJsx(jsx3) {
35013
35135
  StoredVaultMemoryModel,
35014
35136
  StreamExpiredError,
35015
35137
  THEME_ATTRS,
35138
+ TOOL_RESULT_ORIGIN,
35016
35139
  TOPICS_EXTRACTION_VERSION,
35017
35140
  TOPIC_EXTRACTION_BATCH_SIZE,
35018
35141
  TREND_RECENT_WINDOW_DAYS,
@@ -35090,6 +35213,7 @@ function renderAnumaJsx(jsx3) {
35090
35213
  createVaultMemoryOp,
35091
35214
  createWeatherPreProcessor,
35092
35215
  createWebSearchPreProcessor,
35216
+ decodeChunkVector,
35093
35217
  decryptConversationTitle,
35094
35218
  decryptData,
35095
35219
  decryptDataBatch,
@@ -35112,6 +35236,7 @@ function renderAnumaJsx(jsx3) {
35112
35236
  eagerEmbedContent,
35113
35237
  embedAllMessages,
35114
35238
  embedMessage,
35239
+ encodeChunkVector,
35115
35240
  encryptData,
35116
35241
  encryptDataBatch,
35117
35242
  encryptDataBytes,