@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
@@ -43,6 +43,166 @@ var __decorateClass = (decorators, target, key, kind) => {
43
43
  return result;
44
44
  };
45
45
 
46
+ // src/lib/chat/toolResultMessage.ts
47
+ function buildToolResultContent(results) {
48
+ const entries = results.map((r) => `Tool "${r.name}" returned: ${JSON.stringify(r.result)}`);
49
+ const framing = HEADER.length + FOOTER.length + ENTRY_SEPARATOR.length * Math.max(0, entries.length - 1);
50
+ const summary = capEntries(entries, MAX_PERSISTED_TOOL_RESULT_CHARS - framing).join(
51
+ ENTRY_SEPARATOR
52
+ );
53
+ return `${HEADER}${summary}${FOOTER}`;
54
+ }
55
+ function capToolResultEntries(entries, budget) {
56
+ return capEntries(entries, budget);
57
+ }
58
+ function capEntries(entries, budget) {
59
+ const smallestFirst = entries.map((_, index) => index).sort((a, b) => entries[a].length - entries[b].length);
60
+ const capped = [...entries];
61
+ let remaining = budget;
62
+ let unassigned = entries.length;
63
+ for (const index of smallestFirst) {
64
+ capped[index] = truncateEntry(entries[index], Math.floor(remaining / unassigned));
65
+ remaining -= capped[index].length;
66
+ unassigned--;
67
+ }
68
+ return capped;
69
+ }
70
+ function truncateEntry(entry, allowance) {
71
+ if (entry.length <= allowance) return entry;
72
+ const keep = allowance - truncationMarker(entry.length).length;
73
+ if (keep <= 0) return entry.slice(0, Math.max(0, allowance));
74
+ return `${entry.slice(0, keep)}${truncationMarker(entry.length - keep)}`;
75
+ }
76
+ function truncationMarker(omitted) {
77
+ return `
78
+
79
+ ... (tool output truncated, ${omitted} characters omitted)`;
80
+ }
81
+ var HEADER, FOOTER, ENTRY_SEPARATOR, TOOL_RESULT_FOOTER_LINE, MAX_PERSISTED_TOOL_RESULT_CHARS;
82
+ var init_toolResultMessage = __esm({
83
+ "src/lib/chat/toolResultMessage.ts"() {
84
+ "use strict";
85
+ HEADER = "[Tool Execution Results]\n\n";
86
+ FOOTER = "\n\nBased on these results, continue with the task.";
87
+ ENTRY_SEPARATOR = "\n\n";
88
+ TOOL_RESULT_FOOTER_LINE = FOOTER.trim();
89
+ MAX_PERSISTED_TOOL_RESULT_CHARS = 1e5;
90
+ }
91
+ });
92
+
93
+ // src/lib/chat/toolResults.ts
94
+ function isToolResultsRow(row) {
95
+ if (row.origin === TOOL_RESULT_ORIGIN) return true;
96
+ if (typeof row.origin === "string") return false;
97
+ if (row.role !== "user" || !row.content.startsWith(TOOL_RESULTS_PREFIX)) return false;
98
+ return parseToolResultSegments(row.content).length > 0;
99
+ }
100
+ function parseToolResultSegments(content) {
101
+ const segments = [];
102
+ for (const raw of content.split("\n")) {
103
+ const line = raw.trim();
104
+ const match = TOOL_LINE.exec(line);
105
+ if (match) {
106
+ segments.push({ name: match[1], line });
107
+ continue;
108
+ }
109
+ const current = segments[segments.length - 1];
110
+ if (!current || line === "" || line === TOOL_RESULT_FOOTER_LINE) continue;
111
+ if (line === TOOL_RESULTS_PREFIX) continue;
112
+ current.line = `${current.line}
113
+ ${line}`;
114
+ }
115
+ return segments;
116
+ }
117
+ function prepareToolResultsForReplay(rows, options) {
118
+ if (options.fold) {
119
+ return foldToolResultsRows(rows, {
120
+ exclude: options.exclude,
121
+ placeholder: options.placeholder
122
+ });
123
+ }
124
+ return rows.filter((row) => !isToolResultsRow(row));
125
+ }
126
+ function foldToolResultsRows(rows, options) {
127
+ const exclude = new Set(options?.exclude ?? []);
128
+ const out = [];
129
+ const pending = [];
130
+ const assistantIndexById = /* @__PURE__ */ new Map();
131
+ for (const row of rows) {
132
+ if (isToolResultsRow(row)) {
133
+ pending.push({ row, insertionPoint: out.length });
134
+ continue;
135
+ }
136
+ out.push(row);
137
+ if (row.role === "assistant" && row.uniqueId) {
138
+ assistantIndexById.set(row.uniqueId, out.length - 1);
139
+ }
140
+ }
141
+ const byTarget = /* @__PURE__ */ new Map();
142
+ for (const { row, insertionPoint } of pending) {
143
+ const target = resolveFoldTarget(out, row, insertionPoint, assistantIndexById);
144
+ if (target === void 0) continue;
145
+ const group = byTarget.get(target);
146
+ if (group) group.push(row);
147
+ else byTarget.set(target, [row]);
148
+ }
149
+ for (const [target, groupedRows] of byTarget) {
150
+ const assistant = out[target];
151
+ const kept = groupedRows.flatMap((row) => parseToolResultSegments(row.content)).filter((segment) => !exclude.has(segment.name));
152
+ if (kept.length > 0) {
153
+ out[target] = { ...assistant, content: appendToolResults(assistant.content, kept) };
154
+ } else if (!assistant.content.trim() && options?.placeholder) {
155
+ out[target] = { ...assistant, content: options.placeholder };
156
+ }
157
+ }
158
+ return out;
159
+ }
160
+ function resolveFoldTarget(out, row, insertionPoint, assistantIndexById) {
161
+ const linked = row.parentMessageId ? assistantIndexById.get(row.parentMessageId) : void 0;
162
+ if (linked !== void 0) return linked;
163
+ let previous;
164
+ for (let i = insertionPoint - 1; i >= 0; i--) {
165
+ if (out[i].role === "assistant") {
166
+ previous = i;
167
+ break;
168
+ }
169
+ }
170
+ let next;
171
+ for (let i = insertionPoint; i < out.length; i++) {
172
+ if (out[i].role === "assistant") {
173
+ next = i;
174
+ break;
175
+ }
176
+ }
177
+ if (previous === void 0) return next;
178
+ if (next === void 0) return previous;
179
+ return next - insertionPoint < insertionPoint - previous ? next : previous;
180
+ }
181
+ function appendToolResults(assistantContent, kept) {
182
+ const framing = TOOL_RESULTS_PREFIX.length + 1 + (kept.length - 1);
183
+ const capped = capToolResultEntries(
184
+ kept.map((segment) => segment.line),
185
+ MAX_FOLDED_APPENDIX_CHARS - framing
186
+ );
187
+ const appendix = `${TOOL_RESULTS_PREFIX}
188
+ ${capped.join("\n")}`;
189
+ return assistantContent.trim() ? `${assistantContent}
190
+
191
+ ${appendix}` : appendix;
192
+ }
193
+ var TOOL_RESULTS_PREFIX, DISPLAY_CARD_PLACEHOLDER, TOOL_LINE, TOOL_RESULT_ORIGIN, MAX_FOLDED_APPENDIX_CHARS;
194
+ var init_toolResults = __esm({
195
+ "src/lib/chat/toolResults.ts"() {
196
+ "use strict";
197
+ init_toolResultMessage();
198
+ TOOL_RESULTS_PREFIX = "[Tool Execution Results]";
199
+ DISPLAY_CARD_PLACEHOLDER = "[displayed a card to the user with the tool result]";
200
+ TOOL_LINE = /^Tool "([^"]+)" returned: (.*)$/;
201
+ TOOL_RESULT_ORIGIN = "tool_result";
202
+ MAX_FOLDED_APPENDIX_CHARS = 2e4;
203
+ }
204
+ });
205
+
46
206
  // src/lib/logger.ts
47
207
  function setLogger(logger) {
48
208
  currentLogger = logger;
@@ -885,6 +1045,76 @@ var init_vector = __esm({
885
1045
  }
886
1046
  });
887
1047
 
1048
+ // src/lib/processors/encoding.ts
1049
+ function uint8ArrayToBase64(data) {
1050
+ if (typeof Buffer !== "undefined") {
1051
+ return Buffer.from(data).toString("base64");
1052
+ }
1053
+ let base64 = "";
1054
+ for (let i = 0; i < data.length; i += B64_ENCODE_CHUNK) {
1055
+ const slice = data.subarray(i, i + B64_ENCODE_CHUNK);
1056
+ let binary = "";
1057
+ for (let j = 0; j < slice.length; j++) {
1058
+ binary += String.fromCharCode(slice[j]);
1059
+ }
1060
+ base64 += btoa(binary);
1061
+ }
1062
+ return base64;
1063
+ }
1064
+ function base64ToUint8Array(base64) {
1065
+ if (typeof Buffer !== "undefined") {
1066
+ const buf = Buffer.from(base64, "base64");
1067
+ return new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength));
1068
+ }
1069
+ const binary = atob(base64);
1070
+ const bytes = new Uint8Array(binary.length);
1071
+ for (let i = 0; i < binary.length; i++) {
1072
+ bytes[i] = binary.charCodeAt(i);
1073
+ }
1074
+ return bytes;
1075
+ }
1076
+ var B64_ENCODE_CHUNK;
1077
+ var init_encoding = __esm({
1078
+ "src/lib/processors/encoding.ts"() {
1079
+ "use strict";
1080
+ B64_ENCODE_CHUNK = 32768 * 3;
1081
+ }
1082
+ });
1083
+
1084
+ // src/lib/memoryEngine/vectorEncoding.ts
1085
+ function encodeChunkVector(vector) {
1086
+ const f32 = vector instanceof Float32Array ? vector : Float32Array.from(vector);
1087
+ return uint8ArrayToBase64(new Uint8Array(f32.buffer, f32.byteOffset, f32.byteLength));
1088
+ }
1089
+ function decodeChunkVector(vector, onMalformed) {
1090
+ if (!vector || vector.length === 0) return new Float32Array(0);
1091
+ if (typeof vector !== "string") return Float32Array.from(vector);
1092
+ if (vector.length % 4 !== 0 || !CANONICAL_BASE64.test(vector)) {
1093
+ onMalformed?.();
1094
+ return new Float32Array(0);
1095
+ }
1096
+ try {
1097
+ const bytes = base64ToUint8Array(vector);
1098
+ if (bytes.byteLength === 0 || bytes.byteLength % BYTES_PER_FLOAT32 !== 0) {
1099
+ onMalformed?.();
1100
+ return new Float32Array(0);
1101
+ }
1102
+ return new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / BYTES_PER_FLOAT32);
1103
+ } catch {
1104
+ onMalformed?.();
1105
+ return new Float32Array(0);
1106
+ }
1107
+ }
1108
+ var BYTES_PER_FLOAT32, CANONICAL_BASE64;
1109
+ var init_vectorEncoding = __esm({
1110
+ "src/lib/memoryEngine/vectorEncoding.ts"() {
1111
+ "use strict";
1112
+ init_encoding();
1113
+ BYTES_PER_FLOAT32 = 4;
1114
+ CANONICAL_BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
1115
+ }
1116
+ });
1117
+
888
1118
  // src/lib/db/encryption-utils.ts
889
1119
  function isEncrypted(value) {
890
1120
  const prefix = value.startsWith("enc:v3:") ? "enc:v3:" : value.startsWith("enc:v2:") ? "enc:v2:" : null;
@@ -1989,6 +2219,7 @@ async function searchChunksOp(ctx, queryVector, options) {
1989
2219
  const messages = await ctx.messagesCollection.query(...queryConditions).fetch();
1990
2220
  const candidates = [];
1991
2221
  let staleSkipped = 0;
2222
+ let unreadableVectors = 0;
1992
2223
  for (const message of messages) {
1993
2224
  const msgConvId = String(message._getRaw("conversation_id"));
1994
2225
  if (!activeConversationIds.has(msgConvId)) continue;
@@ -2026,7 +2257,9 @@ async function searchChunksOp(ctx, queryVector, options) {
2026
2257
  const chunks = await readJsonField(message, "chunks", ctx.walletAddress);
2027
2258
  if (chunks && chunks.length > 0) {
2028
2259
  const vectors = chunks.map(
2029
- (chunk) => chunk.vector && chunk.vector.length > 0 ? Float32Array.from(chunk.vector) : new Float32Array(0)
2260
+ (chunk) => decodeChunkVector(chunk.vector, () => {
2261
+ unreadableVectors++;
2262
+ })
2030
2263
  );
2031
2264
  chunkCache?.set(message.id, { version, chunks: vectors });
2032
2265
  for (let ci = 0; ci < chunks.length; ci++) {
@@ -2057,6 +2290,11 @@ async function searchChunksOp(ctx, queryVector, options) {
2057
2290
  `searchChunksOp: skipped ${staleSkipped} messages whose embedding model differs from the current model (${embeddingModel}) \u2014 re-embed via chunkAndEmbedAllMessages`
2058
2291
  );
2059
2292
  }
2293
+ if (unreadableVectors > 0) {
2294
+ getLogger().warn(
2295
+ `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`
2296
+ );
2297
+ }
2060
2298
  const topK = candidates.sort((a, b) => b.similarity - a.similarity).slice(0, limit);
2061
2299
  const uniqueMessages = /* @__PURE__ */ new Map();
2062
2300
  for (const c of topK) uniqueMessages.set(c.message.id, c.message);
@@ -2150,6 +2388,7 @@ var init_operations = __esm({
2150
2388
  init_useEncryption();
2151
2389
  init_logger();
2152
2390
  init_vector();
2391
+ init_vectorEncoding();
2153
2392
  init_encryption_utils();
2154
2393
  init_conversationEncryption();
2155
2394
  init_encryption();
@@ -3426,6 +3665,11 @@ var init_generate = __esm({
3426
3665
  });
3427
3666
 
3428
3667
  // src/lib/memoryEngine/embeddings.ts
3668
+ function isNonEmbeddableOrigin(origin, reembedDiscarded = false) {
3669
+ if (typeof origin !== "string") return false;
3670
+ if (reembedDiscarded && origin === CHUNKS_DISCARDED_ORIGIN) return false;
3671
+ return NON_EMBEDDABLE_ORIGINS.has(origin);
3672
+ }
3429
3673
  async function embedMessage(ctx, messageId, options) {
3430
3674
  const message = await getMessageOp(ctx, messageId);
3431
3675
  if (!message) {
@@ -3434,7 +3678,7 @@ async function embedMessage(ctx, messageId, options) {
3434
3678
  if (message.vector && message.vector.length > 0) {
3435
3679
  return message;
3436
3680
  }
3437
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3681
+ if (isNonEmbeddableOrigin(message.origin)) {
3438
3682
  return message;
3439
3683
  }
3440
3684
  if (isEncrypted(message.content)) {
@@ -3470,7 +3714,7 @@ async function embedAllMessages(ctx, options, filter) {
3470
3714
  if (message.role === "system") {
3471
3715
  continue;
3472
3716
  }
3473
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3717
+ if (isNonEmbeddableOrigin(message.origin, filter?.reembedDiscarded)) {
3474
3718
  continue;
3475
3719
  }
3476
3720
  considered++;
@@ -3510,7 +3754,7 @@ async function chunkAndEmbedMessage(ctx, messageId, options) {
3510
3754
  if (message.chunks && message.chunks.length > 0) {
3511
3755
  return message;
3512
3756
  }
3513
- if (message.origin === NON_EMBEDDABLE_ORIGIN) {
3757
+ if (isNonEmbeddableOrigin(message.origin)) {
3514
3758
  return message;
3515
3759
  }
3516
3760
  if (isEncrypted(message.content)) {
@@ -3558,7 +3802,7 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
3558
3802
  if (hasVector && !filter?.rechunkExisting && !isStale) continue;
3559
3803
  if (filter?.roles && !filter.roles.includes(message.role)) continue;
3560
3804
  if (message.role === "system") continue;
3561
- if (message.origin === NON_EMBEDDABLE_ORIGIN) continue;
3805
+ if (isNonEmbeddableOrigin(message.origin, filter?.reembedDiscarded)) continue;
3562
3806
  considered++;
3563
3807
  if (isEncrypted(message.content)) {
3564
3808
  stillEncrypted++;
@@ -3627,10 +3871,11 @@ async function chunkAndEmbedAllMessages(ctx, options, filter) {
3627
3871
  }
3628
3872
  return embeddedCount;
3629
3873
  }
3630
- var DEFAULT_MIN_CONTENT_LENGTH, NON_EMBEDDABLE_ORIGIN;
3874
+ var DEFAULT_MIN_CONTENT_LENGTH, CHUNKS_DISCARDED_ORIGIN, NON_EMBEDDABLE_ORIGINS;
3631
3875
  var init_embeddings = __esm({
3632
3876
  "src/lib/memoryEngine/embeddings.ts"() {
3633
3877
  "use strict";
3878
+ init_toolResults();
3634
3879
  init_operations();
3635
3880
  init_encryption_utils();
3636
3881
  init_logger();
@@ -3639,7 +3884,8 @@ var init_embeddings = __esm({
3639
3884
  init_generate();
3640
3885
  init_generate();
3641
3886
  DEFAULT_MIN_CONTENT_LENGTH = 10;
3642
- NON_EMBEDDABLE_ORIGIN = "tool_result";
3887
+ CHUNKS_DISCARDED_ORIGIN = "chunks_discarded";
3888
+ NON_EMBEDDABLE_ORIGINS = /* @__PURE__ */ new Set([TOOL_RESULT_ORIGIN, CHUNKS_DISCARDED_ORIGIN]);
3643
3889
  }
3644
3890
  });
3645
3891
 
@@ -4318,6 +4564,20 @@ var init_endpointOverride = __esm({
4318
4564
  }
4319
4565
  });
4320
4566
 
4567
+ // src/lib/internalFlowMarker.ts
4568
+ function withInternalFlowMarker(systemPrompt) {
4569
+ if (systemPrompt.includes(INTERNAL_FLOW_MARKER)) return systemPrompt;
4570
+ return `${INTERNAL_FLOW_MARKER}
4571
+ ${systemPrompt}`;
4572
+ }
4573
+ var INTERNAL_FLOW_MARKER;
4574
+ var init_internalFlowMarker = __esm({
4575
+ "src/lib/internalFlowMarker.ts"() {
4576
+ "use strict";
4577
+ INTERNAL_FLOW_MARKER = "Anuma internal first-party flow (not user chat).";
4578
+ }
4579
+ });
4580
+
4321
4581
  // src/lib/db/entities/types.ts
4322
4582
  function normalizeEntityName(name) {
4323
4583
  return name.trim().toLowerCase();
@@ -5912,7 +6172,12 @@ async function attemptPortalJson(req, endpoint) {
5912
6172
  const isAnthropic = req.model.startsWith("anthropic/");
5913
6173
  const prefill = isAnthropic ? "{" : "";
5914
6174
  const messages = [
5915
- { role: "system", content: req.systemPrompt },
6175
+ // Every caller of this helper is a first-party BACKGROUND op (fact extraction,
6176
+ // topic sweep, consolidation, classifiers, graph traversal, query
6177
+ // decomposition). Marking here — the one place they all funnel through — is what
6178
+ // keeps the portal's detector reading them as genuine rather than markerless.
6179
+ // See ../internalFlowMarker.
6180
+ { role: "system", content: withInternalFlowMarker(req.systemPrompt) },
5916
6181
  { role: "user", content: req.userMessage }
5917
6182
  ];
5918
6183
  if (prefill) messages.push({ role: "assistant", content: prefill });
@@ -6050,6 +6315,7 @@ var init_portalLlm = __esm({
6050
6315
  "use strict";
6051
6316
  init_clientConfig();
6052
6317
  init_endpointOverride();
6318
+ init_internalFlowMarker();
6053
6319
  init_logger();
6054
6320
  RESPONSE_FORMAT_OK = /* @__PURE__ */ new Set(["openai", "inclusionai", "deepseek"]);
6055
6321
  RESPONSE_SCHEMA_OK = /* @__PURE__ */ new Set(["openai"]);
@@ -9073,6 +9339,7 @@ For "noop": no content (existing memory is already correct); targetId is that me
9073
9339
  // src/expo/index.ts
9074
9340
  var index_exports = {};
9075
9341
  __export(index_exports, {
9342
+ CHUNKS_DISCARDED_ORIGIN: () => CHUNKS_DISCARDED_ORIGIN,
9076
9343
  ChatConversation: () => Conversation,
9077
9344
  ChatMessage: () => Message,
9078
9345
  ConversationMemoryModel: () => ConversationMemory,
@@ -9113,6 +9380,7 @@ __export(index_exports, {
9113
9380
  StoredVaultFolderModel: () => VaultFolder,
9114
9381
  StoredVaultMemoryModel: () => VaultMemory,
9115
9382
  StreamExpiredError: () => StreamExpiredError,
9383
+ TOOL_RESULT_ORIGIN: () => TOOL_RESULT_ORIGIN,
9116
9384
  TOPICS_EXTRACTION_VERSION: () => TOPICS_EXTRACTION_VERSION,
9117
9385
  TOPIC_EXTRACTION_BATCH_SIZE: () => TOPIC_EXTRACTION_BATCH_SIZE,
9118
9386
  TREND_RECENT_WINDOW_DAYS: () => TREND_RECENT_WINDOW_DAYS,
@@ -9170,6 +9438,7 @@ __export(index_exports, {
9170
9438
  createVaultMemoryOp: () => createVaultMemoryOp,
9171
9439
  createWeatherPreProcessor: () => createWeatherPreProcessor,
9172
9440
  createWebSearchPreProcessor: () => createWebSearchPreProcessor,
9441
+ decodeChunkVector: () => decodeChunkVector,
9173
9442
  decryptConversationTitle: () => decryptConversationTitle,
9174
9443
  deleteAllVaultMemoriesForUserOp: () => deleteAllVaultMemoriesForUserOp,
9175
9444
  deleteVaultFolderOp: () => deleteVaultFolderOp,
@@ -9179,6 +9448,7 @@ __export(index_exports, {
9179
9448
  eagerEmbedContent: () => eagerEmbedContent,
9180
9449
  embedAllMessages: () => embedAllMessages,
9181
9450
  embedMessage: () => embedMessage,
9451
+ encodeChunkVector: () => encodeChunkVector,
9182
9452
  ensureDefaultFoldersOp: () => ensureDefaultFoldersOp,
9183
9453
  exchangeNotionCode: () => exchangeNotionCode,
9184
9454
  extractAndLinkEntitiesForMemoriesOp: () => extractAndLinkEntitiesForMemoriesOp,
@@ -13745,6 +14015,7 @@ async function deleteConversationSummaryOp(ctx, conversationId) {
13745
14015
  }
13746
14016
 
13747
14017
  // src/lib/chat/summarize.ts
14018
+ init_internalFlowMarker();
13748
14019
  init_logger();
13749
14020
  var DEFAULT_SUMMARY_TOKEN_THRESHOLD = 4e3;
13750
14021
  var DEFAULT_SUMMARY_MIN_WINDOW_MESSAGES = 4;
@@ -13937,7 +14208,17 @@ async function callSummarizationLlm(prompt, model, token, baseUrl) {
13937
14208
  body: JSON.stringify({
13938
14209
  model,
13939
14210
  stream: false,
13940
- messages: [{ role: "user", content: prompt }]
14211
+ // The marker rides a system message because this call has no other
14212
+ // provenance: it posts to the MAIN chat endpoint (not /utility/*), carries no
14213
+ // conversationId, and its prompt is neither the app's chat base prompt nor any
14214
+ // registered flow fingerprint — so without this it reads as markerless, i.e.
14215
+ // as a scripted abuser. Progressive summarization runs for every free-tier
14216
+ // long conversation, so at the PORTAL_DETECTION_REJECT_MARKERLESS flip it
14217
+ // would 403 and silently fall back to verbatim history (context bloat + cost).
14218
+ messages: [
14219
+ { role: "system", content: INTERNAL_FLOW_MARKER },
14220
+ { role: "user", content: prompt }
14221
+ ]
13941
14222
  }),
13942
14223
  signal: controller.signal
13943
14224
  });
@@ -14141,152 +14422,9 @@ async function cleanupConversationSummary(database, conversationId) {
14141
14422
  }
14142
14423
  }
14143
14424
 
14144
- // src/lib/chat/toolResultMessage.ts
14145
- var HEADER = "[Tool Execution Results]\n\n";
14146
- var FOOTER = "\n\nBased on these results, continue with the task.";
14147
- var ENTRY_SEPARATOR = "\n\n";
14148
- var TOOL_RESULT_FOOTER_LINE = FOOTER.trim();
14149
- var MAX_PERSISTED_TOOL_RESULT_CHARS = 1e5;
14150
- function buildToolResultContent(results) {
14151
- const entries = results.map((r) => `Tool "${r.name}" returned: ${JSON.stringify(r.result)}`);
14152
- const framing = HEADER.length + FOOTER.length + ENTRY_SEPARATOR.length * Math.max(0, entries.length - 1);
14153
- const summary = capEntries(entries, MAX_PERSISTED_TOOL_RESULT_CHARS - framing).join(
14154
- ENTRY_SEPARATOR
14155
- );
14156
- return `${HEADER}${summary}${FOOTER}`;
14157
- }
14158
- function capToolResultEntries(entries, budget) {
14159
- return capEntries(entries, budget);
14160
- }
14161
- function capEntries(entries, budget) {
14162
- const smallestFirst = entries.map((_, index) => index).sort((a, b) => entries[a].length - entries[b].length);
14163
- const capped = [...entries];
14164
- let remaining = budget;
14165
- let unassigned = entries.length;
14166
- for (const index of smallestFirst) {
14167
- capped[index] = truncateEntry(entries[index], Math.floor(remaining / unassigned));
14168
- remaining -= capped[index].length;
14169
- unassigned--;
14170
- }
14171
- return capped;
14172
- }
14173
- function truncateEntry(entry, allowance) {
14174
- if (entry.length <= allowance) return entry;
14175
- const keep = allowance - truncationMarker(entry.length).length;
14176
- if (keep <= 0) return entry.slice(0, Math.max(0, allowance));
14177
- return `${entry.slice(0, keep)}${truncationMarker(entry.length - keep)}`;
14178
- }
14179
- function truncationMarker(omitted) {
14180
- return `
14181
-
14182
- ... (tool output truncated, ${omitted} characters omitted)`;
14183
- }
14184
-
14185
- // src/lib/chat/toolResults.ts
14186
- var TOOL_RESULTS_PREFIX = "[Tool Execution Results]";
14187
- var DISPLAY_CARD_PLACEHOLDER = "[displayed a card to the user with the tool result]";
14188
- var TOOL_LINE = /^Tool "([^"]+)" returned: (.*)$/;
14189
- var TOOL_RESULT_ORIGIN = "tool_result";
14190
- function isToolResultsRow(row) {
14191
- if (row.origin === TOOL_RESULT_ORIGIN) return true;
14192
- if (typeof row.origin === "string") return false;
14193
- if (row.role !== "user" || !row.content.startsWith(TOOL_RESULTS_PREFIX)) return false;
14194
- return parseToolResultSegments(row.content).length > 0;
14195
- }
14196
- function parseToolResultSegments(content) {
14197
- const segments = [];
14198
- for (const raw of content.split("\n")) {
14199
- const line = raw.trim();
14200
- const match = TOOL_LINE.exec(line);
14201
- if (match) {
14202
- segments.push({ name: match[1], line });
14203
- continue;
14204
- }
14205
- const current = segments[segments.length - 1];
14206
- if (!current || line === "" || line === TOOL_RESULT_FOOTER_LINE) continue;
14207
- if (line === TOOL_RESULTS_PREFIX) continue;
14208
- current.line = `${current.line}
14209
- ${line}`;
14210
- }
14211
- return segments;
14212
- }
14213
- function prepareToolResultsForReplay(rows, options) {
14214
- if (options.fold) {
14215
- return foldToolResultsRows(rows, {
14216
- exclude: options.exclude,
14217
- placeholder: options.placeholder
14218
- });
14219
- }
14220
- return rows.filter((row) => !isToolResultsRow(row));
14221
- }
14222
- function foldToolResultsRows(rows, options) {
14223
- const exclude = new Set(options?.exclude ?? []);
14224
- const out = [];
14225
- const pending = [];
14226
- const assistantIndexById = /* @__PURE__ */ new Map();
14227
- for (const row of rows) {
14228
- if (isToolResultsRow(row)) {
14229
- pending.push({ row, insertionPoint: out.length });
14230
- continue;
14231
- }
14232
- out.push(row);
14233
- if (row.role === "assistant" && row.uniqueId) {
14234
- assistantIndexById.set(row.uniqueId, out.length - 1);
14235
- }
14236
- }
14237
- const byTarget = /* @__PURE__ */ new Map();
14238
- for (const { row, insertionPoint } of pending) {
14239
- const target = resolveFoldTarget(out, row, insertionPoint, assistantIndexById);
14240
- if (target === void 0) continue;
14241
- const group = byTarget.get(target);
14242
- if (group) group.push(row);
14243
- else byTarget.set(target, [row]);
14244
- }
14245
- for (const [target, groupedRows] of byTarget) {
14246
- const assistant = out[target];
14247
- const kept = groupedRows.flatMap((row) => parseToolResultSegments(row.content)).filter((segment) => !exclude.has(segment.name));
14248
- if (kept.length > 0) {
14249
- out[target] = { ...assistant, content: appendToolResults(assistant.content, kept) };
14250
- } else if (!assistant.content.trim() && options?.placeholder) {
14251
- out[target] = { ...assistant, content: options.placeholder };
14252
- }
14253
- }
14254
- return out;
14255
- }
14256
- function resolveFoldTarget(out, row, insertionPoint, assistantIndexById) {
14257
- const linked = row.parentMessageId ? assistantIndexById.get(row.parentMessageId) : void 0;
14258
- if (linked !== void 0) return linked;
14259
- let previous;
14260
- for (let i = insertionPoint - 1; i >= 0; i--) {
14261
- if (out[i].role === "assistant") {
14262
- previous = i;
14263
- break;
14264
- }
14265
- }
14266
- let next;
14267
- for (let i = insertionPoint; i < out.length; i++) {
14268
- if (out[i].role === "assistant") {
14269
- next = i;
14270
- break;
14271
- }
14272
- }
14273
- if (previous === void 0) return next;
14274
- if (next === void 0) return previous;
14275
- return next - insertionPoint < insertionPoint - previous ? next : previous;
14276
- }
14277
- var MAX_FOLDED_APPENDIX_CHARS = 2e4;
14278
- function appendToolResults(assistantContent, kept) {
14279
- const framing = TOOL_RESULTS_PREFIX.length + 1 + (kept.length - 1);
14280
- const capped = capToolResultEntries(
14281
- kept.map((segment) => segment.line),
14282
- MAX_FOLDED_APPENDIX_CHARS - framing
14283
- );
14284
- const appendix = `${TOOL_RESULTS_PREFIX}
14285
- ${capped.join("\n")}`;
14286
- return assistantContent.trim() ? `${assistantContent}
14287
-
14288
- ${appendix}` : appendix;
14289
- }
14425
+ // src/expo/useChatStorage.ts
14426
+ init_toolResultMessage();
14427
+ init_toolResults();
14290
14428
 
14291
14429
  // src/lib/db/chat/lazyDecrypt.ts
14292
14430
  init_useEncryption();
@@ -17557,6 +17695,7 @@ init_reranker();
17557
17695
 
17558
17696
  // src/lib/memory/synthesizeProfile.ts
17559
17697
  init_operations3();
17698
+ init_internalFlowMarker();
17560
17699
  init_logger();
17561
17700
  init_constants();
17562
17701
  init_embeddings();
@@ -17918,7 +18057,10 @@ async function synthesizeFacet(facet, ctx, options, prior) {
17918
18057
  limit,
17919
18058
  types: ["fact"],
17920
18059
  maxTokens: DEFAULT_FACET_MAX_TOKENS,
17921
- systemPrompt: buildFacetSystemPrompt(facet),
18060
+ // Profile-facet synthesis is a background op, so it marks its own prompt.
18061
+ // reflect() itself must NOT mark — it also serves the user's own questions,
18062
+ // and that traffic is chat, not an internal flow. See ../internalFlowMarker.
18063
+ systemPrompt: withInternalFlowMarker(buildFacetSystemPrompt(facet)),
17922
18064
  responseSchema: facetResponseSchema(facet.key),
17923
18065
  memories
17924
18066
  });
@@ -18722,6 +18864,9 @@ function createMemoryEngineTool(storageCtx, embeddingOptions, searchOptions, cal
18722
18864
  };
18723
18865
  }
18724
18866
 
18867
+ // src/lib/memoryEngine/index.ts
18868
+ init_vectorEncoding();
18869
+
18725
18870
  // src/expo/useChatStorage.ts
18726
18871
  init_constants();
18727
18872
 
@@ -23208,6 +23353,7 @@ async function _ensureDefaultFoldersImpl(ctx) {
23208
23353
  }
23209
23354
 
23210
23355
  // src/expo/index.ts
23356
+ init_toolResults();
23211
23357
  init_logger();
23212
23358
 
23213
23359
  // src/react/LoggerProvider.tsx
@@ -24128,6 +24274,7 @@ function createNotionTools(getAccessToken, requestNotionAccess) {
24128
24274
  }
24129
24275
  // Annotate the CommonJS export names for ESM import in node:
24130
24276
  0 && (module.exports = {
24277
+ CHUNKS_DISCARDED_ORIGIN,
24131
24278
  ChatConversation,
24132
24279
  ChatMessage,
24133
24280
  ConversationMemoryModel,
@@ -24168,6 +24315,7 @@ function createNotionTools(getAccessToken, requestNotionAccess) {
24168
24315
  StoredVaultFolderModel,
24169
24316
  StoredVaultMemoryModel,
24170
24317
  StreamExpiredError,
24318
+ TOOL_RESULT_ORIGIN,
24171
24319
  TOPICS_EXTRACTION_VERSION,
24172
24320
  TOPIC_EXTRACTION_BATCH_SIZE,
24173
24321
  TREND_RECENT_WINDOW_DAYS,
@@ -24225,6 +24373,7 @@ function createNotionTools(getAccessToken, requestNotionAccess) {
24225
24373
  createVaultMemoryOp,
24226
24374
  createWeatherPreProcessor,
24227
24375
  createWebSearchPreProcessor,
24376
+ decodeChunkVector,
24228
24377
  decryptConversationTitle,
24229
24378
  deleteAllVaultMemoriesForUserOp,
24230
24379
  deleteVaultFolderOp,
@@ -24234,6 +24383,7 @@ function createNotionTools(getAccessToken, requestNotionAccess) {
24234
24383
  eagerEmbedContent,
24235
24384
  embedAllMessages,
24236
24385
  embedMessage,
24386
+ encodeChunkVector,
24237
24387
  ensureDefaultFoldersOp,
24238
24388
  exchangeNotionCode,
24239
24389
  extractAndLinkEntitiesForMemoriesOp,