@anuma/sdk 8.0.0 → 8.0.2
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.
- package/dist/expo/index.cjs +354 -258
- package/dist/expo/index.mjs +306 -210
- package/dist/react/index.cjs +452 -351
- package/dist/react/index.mjs +398 -297
- package/dist/server/index.cjs +9 -1
- package/dist/server/index.mjs +9 -1
- package/package.json +1 -1
package/dist/expo/index.mjs
CHANGED
|
@@ -458,7 +458,7 @@ ${appendix}` : appendix;
|
|
|
458
458
|
}
|
|
459
459
|
|
|
460
460
|
// src/lib/db/chat/operations.ts
|
|
461
|
-
import { Q } from "@nozbe/watermelondb";
|
|
461
|
+
import { Q as Q2 } from "@nozbe/watermelondb";
|
|
462
462
|
import { v7 as uuidv72 } from "uuid";
|
|
463
463
|
|
|
464
464
|
// src/react/useEncryption.ts
|
|
@@ -1666,6 +1666,118 @@ async function assembleDecryptedMessage(message, address) {
|
|
|
1666
1666
|
return result;
|
|
1667
1667
|
}
|
|
1668
1668
|
|
|
1669
|
+
// src/lib/db/chat/toolCallEventIndex.ts
|
|
1670
|
+
import { Q } from "@nozbe/watermelondb";
|
|
1671
|
+
function parseEventIds(value) {
|
|
1672
|
+
const ids = /* @__PURE__ */ new Set();
|
|
1673
|
+
let events;
|
|
1674
|
+
try {
|
|
1675
|
+
events = typeof value === "string" ? JSON.parse(value) : value;
|
|
1676
|
+
} catch {
|
|
1677
|
+
return ids;
|
|
1678
|
+
}
|
|
1679
|
+
if (!Array.isArray(events)) return ids;
|
|
1680
|
+
for (const event of events) {
|
|
1681
|
+
if (!event || typeof event !== "object") continue;
|
|
1682
|
+
const id = event.id;
|
|
1683
|
+
if (typeof id === "string" && id) ids.add(id);
|
|
1684
|
+
}
|
|
1685
|
+
return ids;
|
|
1686
|
+
}
|
|
1687
|
+
async function fetchEventRows(collection, conversationId) {
|
|
1688
|
+
try {
|
|
1689
|
+
return await collection.query(
|
|
1690
|
+
Q.unsafeSqlQuery(
|
|
1691
|
+
`select "id", "tool_call_events" from "history" where "conversation_id" = ? and "_status" != 'deleted' and "tool_call_events" is not null and "tool_call_events" not in ('', '[]')`,
|
|
1692
|
+
[conversationId]
|
|
1693
|
+
)
|
|
1694
|
+
).unsafeFetchRaw();
|
|
1695
|
+
} catch (error) {
|
|
1696
|
+
if (!(error instanceof Error) || !/unsafeSqlQuery.*not supported/.test(error.message)) {
|
|
1697
|
+
throw error;
|
|
1698
|
+
}
|
|
1699
|
+
return await collection.query(
|
|
1700
|
+
Q.where("conversation_id", conversationId),
|
|
1701
|
+
Q.where("tool_call_events", Q.notEq(null)),
|
|
1702
|
+
Q.where("tool_call_events", Q.notIn(["", "[]"])),
|
|
1703
|
+
Q.unsafeLokiTransform(
|
|
1704
|
+
(rows) => rows.map(({ id, tool_call_events }) => ({ id, tool_call_events }))
|
|
1705
|
+
)
|
|
1706
|
+
).unsafeFetchRaw();
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
var ToolCallEventIndex = class {
|
|
1710
|
+
constructor(collection) {
|
|
1711
|
+
this.collection = collection;
|
|
1712
|
+
this.conversations = /* @__PURE__ */ new Map();
|
|
1713
|
+
this.messages = /* @__PURE__ */ new Map();
|
|
1714
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
1715
|
+
this.resetCount = collection.database._resetCount;
|
|
1716
|
+
collection.experimentalSubscribe((changes) => {
|
|
1717
|
+
for (const { record: record2, type } of changes) {
|
|
1718
|
+
this.replace(
|
|
1719
|
+
record2.id,
|
|
1720
|
+
type === "destroyed" ? void 0 : record2.conversationId,
|
|
1721
|
+
type === "destroyed" ? void 0 : record2._getRaw("tool_call_events")
|
|
1722
|
+
);
|
|
1723
|
+
}
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
replace(messageId, conversationId, value) {
|
|
1727
|
+
const previous = this.messages.get(messageId);
|
|
1728
|
+
if (previous) {
|
|
1729
|
+
const counts2 = this.conversations.get(previous.conversationId);
|
|
1730
|
+
for (const id of previous.ids) {
|
|
1731
|
+
const count2 = counts2.get(id) - 1;
|
|
1732
|
+
if (count2 === 0) counts2.delete(id);
|
|
1733
|
+
else counts2.set(id, count2);
|
|
1734
|
+
}
|
|
1735
|
+
this.messages.delete(messageId);
|
|
1736
|
+
}
|
|
1737
|
+
const counts = conversationId === void 0 ? void 0 : this.conversations.get(conversationId);
|
|
1738
|
+
if (!counts || conversationId === void 0) return;
|
|
1739
|
+
const ids = parseEventIds(value);
|
|
1740
|
+
if (ids.size === 0) return;
|
|
1741
|
+
this.messages.set(messageId, { conversationId, ids });
|
|
1742
|
+
for (const id of ids) counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
1743
|
+
}
|
|
1744
|
+
async get(conversationId) {
|
|
1745
|
+
if (this.resetCount !== this.collection.database._resetCount) {
|
|
1746
|
+
this.conversations.clear();
|
|
1747
|
+
this.messages.clear();
|
|
1748
|
+
this.pending.clear();
|
|
1749
|
+
this.resetCount = this.collection.database._resetCount;
|
|
1750
|
+
}
|
|
1751
|
+
if (!this.conversations.has(conversationId)) {
|
|
1752
|
+
let pending = this.pending.get(conversationId);
|
|
1753
|
+
if (!pending) {
|
|
1754
|
+
pending = this.collection.database.read(async () => {
|
|
1755
|
+
const rows = await fetchEventRows(this.collection, conversationId);
|
|
1756
|
+
this.conversations.set(conversationId, /* @__PURE__ */ new Map());
|
|
1757
|
+
for (const row of rows)
|
|
1758
|
+
this.replace(String(row.id), conversationId, row.tool_call_events);
|
|
1759
|
+
});
|
|
1760
|
+
this.pending.set(conversationId, pending);
|
|
1761
|
+
}
|
|
1762
|
+
try {
|
|
1763
|
+
await pending;
|
|
1764
|
+
} finally {
|
|
1765
|
+
this.pending.delete(conversationId);
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
return new Set(this.conversations.get(conversationId).keys());
|
|
1769
|
+
}
|
|
1770
|
+
};
|
|
1771
|
+
var indexes = /* @__PURE__ */ new WeakMap();
|
|
1772
|
+
function getToolCallEventIds(collection, conversationId) {
|
|
1773
|
+
let index = indexes.get(collection);
|
|
1774
|
+
if (!index) {
|
|
1775
|
+
index = new ToolCallEventIndex(collection);
|
|
1776
|
+
indexes.set(collection, index);
|
|
1777
|
+
}
|
|
1778
|
+
return index.get(conversationId);
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1669
1781
|
// src/lib/db/chat/types.ts
|
|
1670
1782
|
import { v7 as uuidv7 } from "uuid";
|
|
1671
1783
|
|
|
@@ -2058,7 +2170,7 @@ async function createConversationOp(ctx, opts, defaultTitle = "New Conversation"
|
|
|
2058
2170
|
);
|
|
2059
2171
|
}
|
|
2060
2172
|
async function getConversationOp(ctx, id) {
|
|
2061
|
-
const results = await ctx.conversationsCollection.query(
|
|
2173
|
+
const results = await ctx.conversationsCollection.query(Q2.where("conversation_id", id), Q2.where("is_deleted", false)).fetch();
|
|
2062
2174
|
return results.length > 0 ? await conversationToStored(
|
|
2063
2175
|
results[0],
|
|
2064
2176
|
ctx.walletAddress,
|
|
@@ -2067,7 +2179,7 @@ async function getConversationOp(ctx, id) {
|
|
|
2067
2179
|
) : null;
|
|
2068
2180
|
}
|
|
2069
2181
|
async function getConversationsOp(ctx) {
|
|
2070
|
-
const results = await ctx.conversationsCollection.query(
|
|
2182
|
+
const results = await ctx.conversationsCollection.query(Q2.where("is_deleted", false), Q2.sortBy("created_at", Q2.desc)).unsafeFetchRaw();
|
|
2071
2183
|
return Promise.all(
|
|
2072
2184
|
results.map(
|
|
2073
2185
|
(raw) => conversationRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
|
|
@@ -2088,33 +2200,33 @@ function conversationRawToLazyStored(raw) {
|
|
|
2088
2200
|
};
|
|
2089
2201
|
}
|
|
2090
2202
|
async function getConversationsLazyOp(ctx) {
|
|
2091
|
-
const results = await ctx.conversationsCollection.query(
|
|
2203
|
+
const results = await ctx.conversationsCollection.query(Q2.where("is_deleted", false), Q2.sortBy("created_at", Q2.desc)).unsafeFetchRaw();
|
|
2092
2204
|
return results.map(conversationRawToLazyStored);
|
|
2093
2205
|
}
|
|
2094
2206
|
async function getConversationsPageOp(ctx, options) {
|
|
2095
2207
|
const limit = Math.floor(options?.limit ?? 200);
|
|
2096
2208
|
if (!Number.isFinite(limit) || limit < 1) return [];
|
|
2097
2209
|
const exclude = new Set(options?.boundaryExcludeUniqueIds ?? []);
|
|
2098
|
-
const clauses = [
|
|
2210
|
+
const clauses = [Q2.where("is_deleted", false)];
|
|
2099
2211
|
if (options?.before !== void 0) {
|
|
2100
2212
|
clauses.push(
|
|
2101
|
-
exclude.size > 0 ?
|
|
2213
|
+
exclude.size > 0 ? Q2.where("created_at", Q2.lte(options.before)) : Q2.where("created_at", Q2.lt(options.before))
|
|
2102
2214
|
);
|
|
2103
2215
|
}
|
|
2104
|
-
const fetched = await ctx.conversationsCollection.query(...clauses,
|
|
2216
|
+
const fetched = await ctx.conversationsCollection.query(...clauses, Q2.sortBy("created_at", Q2.desc), Q2.take(limit + exclude.size)).unsafeFetchRaw();
|
|
2105
2217
|
const results = exclude.size > 0 ? fetched.filter((raw) => !exclude.has(raw.id)) : fetched;
|
|
2106
2218
|
return results.slice(0, limit).map(conversationRawToLazyStored);
|
|
2107
2219
|
}
|
|
2108
2220
|
async function getConversationsByProjectLazyOp(ctx, projectId) {
|
|
2109
2221
|
const results = await ctx.conversationsCollection.query(
|
|
2110
|
-
|
|
2111
|
-
|
|
2112
|
-
|
|
2222
|
+
Q2.where("project_id", projectId === null ? "" : projectId),
|
|
2223
|
+
Q2.where("is_deleted", false),
|
|
2224
|
+
Q2.sortBy("created_at", Q2.desc)
|
|
2113
2225
|
).unsafeFetchRaw();
|
|
2114
2226
|
return results.map(conversationRawToLazyStored);
|
|
2115
2227
|
}
|
|
2116
2228
|
async function updateConversationTitleOp(ctx, id, title) {
|
|
2117
|
-
const results = await ctx.conversationsCollection.query(
|
|
2229
|
+
const results = await ctx.conversationsCollection.query(Q2.where("conversation_id", id), Q2.where("is_deleted", false)).fetch();
|
|
2118
2230
|
if (results.length > 0) {
|
|
2119
2231
|
const encryptedOpts = ctx.walletAddress && ctx.signMessage ? await encryptConversationFields(
|
|
2120
2232
|
{ title },
|
|
@@ -2133,13 +2245,13 @@ async function updateConversationTitleOp(ctx, id, title) {
|
|
|
2133
2245
|
return false;
|
|
2134
2246
|
}
|
|
2135
2247
|
async function deleteConversationOp(ctx, id) {
|
|
2136
|
-
const results = await ctx.conversationsCollection.query(
|
|
2248
|
+
const results = await ctx.conversationsCollection.query(Q2.where("conversation_id", id), Q2.where("is_deleted", false)).fetch();
|
|
2137
2249
|
if (results.length > 0) {
|
|
2138
2250
|
await ctx.database.write(async () => {
|
|
2139
2251
|
await results[0].update((conv) => {
|
|
2140
2252
|
conv._setRaw("is_deleted", true);
|
|
2141
2253
|
});
|
|
2142
|
-
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(
|
|
2254
|
+
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(Q2.where("conversation_id", id)).fetch() : [];
|
|
2143
2255
|
for (const job of jobs) await job.destroyPermanently();
|
|
2144
2256
|
});
|
|
2145
2257
|
return true;
|
|
@@ -2147,7 +2259,7 @@ async function deleteConversationOp(ctx, id) {
|
|
|
2147
2259
|
return false;
|
|
2148
2260
|
}
|
|
2149
2261
|
async function updateConversationPinnedOp(ctx, id, pinned) {
|
|
2150
|
-
const results = await ctx.conversationsCollection.query(
|
|
2262
|
+
const results = await ctx.conversationsCollection.query(Q2.where("conversation_id", id), Q2.where("is_deleted", false)).fetch();
|
|
2151
2263
|
if (results.length > 0) {
|
|
2152
2264
|
await ctx.database.write(async () => {
|
|
2153
2265
|
await results[0].update((conv) => {
|
|
@@ -2159,7 +2271,7 @@ async function updateConversationPinnedOp(ctx, id, pinned) {
|
|
|
2159
2271
|
return false;
|
|
2160
2272
|
}
|
|
2161
2273
|
async function getMessagesOp(ctx, convId, opts) {
|
|
2162
|
-
const fetched = await ctx.messagesCollection.query(
|
|
2274
|
+
const fetched = await ctx.messagesCollection.query(Q2.where("conversation_id", convId), Q2.sortBy("message_id", Q2.asc)).unsafeFetchRaw();
|
|
2163
2275
|
const results = opts?.rawFilter ? fetched.filter(opts.rawFilter) : fetched;
|
|
2164
2276
|
return Promise.all(
|
|
2165
2277
|
results.map(
|
|
@@ -2168,19 +2280,19 @@ async function getMessagesOp(ctx, convId, opts) {
|
|
|
2168
2280
|
);
|
|
2169
2281
|
}
|
|
2170
2282
|
async function getMessageCountOp(ctx, convId) {
|
|
2171
|
-
return await ctx.messagesCollection.query(
|
|
2283
|
+
return await ctx.messagesCollection.query(Q2.where("conversation_id", convId)).fetchCount();
|
|
2172
2284
|
}
|
|
2173
2285
|
async function getMessagesPageOp(ctx, convId, opts) {
|
|
2174
2286
|
const limit = Math.floor(opts.limit);
|
|
2175
2287
|
if (!Number.isFinite(limit) || limit < 1) return [];
|
|
2176
2288
|
const exclude = new Set(opts.boundaryExcludeUniqueIds ?? []);
|
|
2177
|
-
const clauses = [
|
|
2289
|
+
const clauses = [Q2.where("conversation_id", convId)];
|
|
2178
2290
|
if (opts.beforeMessageId !== void 0) {
|
|
2179
2291
|
clauses.push(
|
|
2180
|
-
exclude.size > 0 ?
|
|
2292
|
+
exclude.size > 0 ? Q2.where("message_id", Q2.lte(opts.beforeMessageId)) : Q2.where("message_id", Q2.lt(opts.beforeMessageId))
|
|
2181
2293
|
);
|
|
2182
2294
|
}
|
|
2183
|
-
const fetched = await ctx.messagesCollection.query(...clauses,
|
|
2295
|
+
const fetched = await ctx.messagesCollection.query(...clauses, Q2.sortBy("message_id", Q2.desc), Q2.take(limit + exclude.size)).unsafeFetchRaw();
|
|
2184
2296
|
const results = (exclude.size > 0 ? fetched.filter((raw) => !exclude.has(raw.id)) : fetched).slice(0, limit);
|
|
2185
2297
|
results.reverse();
|
|
2186
2298
|
return Promise.all(
|
|
@@ -2192,7 +2304,7 @@ async function getMessagesPageOp(ctx, convId, opts) {
|
|
|
2192
2304
|
);
|
|
2193
2305
|
}
|
|
2194
2306
|
async function getMessageSkeletonsOp(ctx, convId) {
|
|
2195
|
-
const results = await ctx.messagesCollection.query(
|
|
2307
|
+
const results = await ctx.messagesCollection.query(Q2.where("conversation_id", convId), Q2.sortBy("message_id", Q2.asc)).unsafeFetchRaw();
|
|
2196
2308
|
const roleById = /* @__PURE__ */ new Map();
|
|
2197
2309
|
for (const raw of results) {
|
|
2198
2310
|
roleById.set(raw.id, raw.role);
|
|
@@ -2237,32 +2349,12 @@ async function getMessageSkeletonsOp(ctx, convId) {
|
|
|
2237
2349
|
return skeletons;
|
|
2238
2350
|
}
|
|
2239
2351
|
async function getToolCallEventIdsOp(ctx, convId) {
|
|
2240
|
-
|
|
2241
|
-
const ids = /* @__PURE__ */ new Set();
|
|
2242
|
-
for (const raw of results) {
|
|
2243
|
-
const value = raw.tool_call_events;
|
|
2244
|
-
if (!value) continue;
|
|
2245
|
-
let events;
|
|
2246
|
-
if (typeof value === "string") {
|
|
2247
|
-
try {
|
|
2248
|
-
events = JSON.parse(value);
|
|
2249
|
-
} catch {
|
|
2250
|
-
continue;
|
|
2251
|
-
}
|
|
2252
|
-
} else if (Array.isArray(value)) {
|
|
2253
|
-
events = value;
|
|
2254
|
-
}
|
|
2255
|
-
if (!Array.isArray(events)) continue;
|
|
2256
|
-
for (const evt of events) {
|
|
2257
|
-
if (evt && evt.id) ids.add(evt.id);
|
|
2258
|
-
}
|
|
2259
|
-
}
|
|
2260
|
-
return ids;
|
|
2352
|
+
return getToolCallEventIds(ctx.messagesCollection, convId);
|
|
2261
2353
|
}
|
|
2262
2354
|
async function clearMessagesOp(ctx, convId) {
|
|
2263
2355
|
await ctx.database.write(async () => {
|
|
2264
|
-
const messages = await ctx.messagesCollection.query(
|
|
2265
|
-
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(
|
|
2356
|
+
const messages = await ctx.messagesCollection.query(Q2.where("conversation_id", convId)).fetch();
|
|
2357
|
+
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(Q2.where("conversation_id", convId)).fetch() : [];
|
|
2266
2358
|
for (const job of jobs) {
|
|
2267
2359
|
await job.update((row) => {
|
|
2268
2360
|
row._setRaw("message_ids", "[]");
|
|
@@ -2287,7 +2379,7 @@ async function deleteMessageOp(ctx, uniqueId) {
|
|
|
2287
2379
|
return null;
|
|
2288
2380
|
}
|
|
2289
2381
|
await ctx.database.write(async () => {
|
|
2290
|
-
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(
|
|
2382
|
+
const jobs = ctx.database.schema?.tables[ExtractionJob.table] ? await ctx.database.get(ExtractionJob.table).query(Q2.where("conversation_id", message.conversationId)).fetch() : [];
|
|
2291
2383
|
for (const job of jobs) {
|
|
2292
2384
|
const ids = JSON.parse(String(job._getRaw("message_ids")));
|
|
2293
2385
|
await job.update((row) => {
|
|
@@ -2360,9 +2452,9 @@ async function createMessageOp(ctx, opts) {
|
|
|
2360
2452
|
}
|
|
2361
2453
|
}
|
|
2362
2454
|
const newest = await ctx.messagesCollection.query(
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2455
|
+
Q2.where("conversation_id", opts.conversationId),
|
|
2456
|
+
Q2.sortBy("message_id", Q2.desc),
|
|
2457
|
+
Q2.take(1)
|
|
2366
2458
|
).fetch();
|
|
2367
2459
|
const messageId = (newest[0]?.messageId ?? 0) + 1;
|
|
2368
2460
|
const encryptedOpts = await encryptMessageOptsIfNeeded(ctx, opts);
|
|
@@ -2376,7 +2468,7 @@ async function createMessageOp(ctx, opts) {
|
|
|
2376
2468
|
return messageToStored(created, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner);
|
|
2377
2469
|
}
|
|
2378
2470
|
async function upsertMessageOp(ctx, opts) {
|
|
2379
|
-
const results = await ctx.messagesCollection.query(
|
|
2471
|
+
const results = await ctx.messagesCollection.query(Q2.where("id", opts.uniqueId)).fetch();
|
|
2380
2472
|
const existing = results.length > 0 ? results[0] : null;
|
|
2381
2473
|
if (!existing) {
|
|
2382
2474
|
return createMessageOp(ctx, opts);
|
|
@@ -2484,13 +2576,13 @@ async function decryptMessageEmbeddingFields(raw, walletAddress) {
|
|
|
2484
2576
|
async function sealLegacyMessageEmbeddingsOp(ctx) {
|
|
2485
2577
|
if (!ctx.walletAddress) return 0;
|
|
2486
2578
|
const candidateIds = await ctx.messagesCollection.query(
|
|
2487
|
-
|
|
2488
|
-
|
|
2579
|
+
Q2.where("content", Q2.like("enc:%")),
|
|
2580
|
+
Q2.or(
|
|
2489
2581
|
...["vector", "chunks"].map(
|
|
2490
|
-
(column) =>
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2582
|
+
(column) => Q2.and(
|
|
2583
|
+
Q2.where(column, Q2.notEq(null)),
|
|
2584
|
+
Q2.where(column, Q2.notEq("")),
|
|
2585
|
+
Q2.where(column, Q2.notLike("enc:%"))
|
|
2494
2586
|
)
|
|
2495
2587
|
)
|
|
2496
2588
|
)
|
|
@@ -2500,7 +2592,7 @@ async function sealLegacyMessageEmbeddingsOp(ctx) {
|
|
|
2500
2592
|
for (let start = 0; start < candidateIds.length; start += 50) {
|
|
2501
2593
|
const pageIds = candidateIds.slice(start, start + 50);
|
|
2502
2594
|
await ctx.database.write(async () => {
|
|
2503
|
-
const pageMessages = await ctx.messagesCollection.query(
|
|
2595
|
+
const pageMessages = await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf(pageIds))).fetch();
|
|
2504
2596
|
for (const message of pageMessages) {
|
|
2505
2597
|
if (!isEncrypted(message.content)) continue;
|
|
2506
2598
|
const content = await decryptFieldDetailed(message.content, ctx.walletAddress);
|
|
@@ -2581,15 +2673,15 @@ function chunksWithoutText(raw) {
|
|
|
2581
2673
|
});
|
|
2582
2674
|
}
|
|
2583
2675
|
async function stripLegacyChunkTextOp(ctx) {
|
|
2584
|
-
const candidateIds = await ctx.messagesCollection.query(
|
|
2676
|
+
const candidateIds = await ctx.messagesCollection.query(Q2.where("chunks", Q2.like(`%${Q2.sanitizeLikeString('"text":')}%`))).fetchIds();
|
|
2585
2677
|
let stripped = 0;
|
|
2586
2678
|
for (let start = 0; start < candidateIds.length; start += RAW_REPAIR_PAGE_SIZE) {
|
|
2587
2679
|
const pageIds = candidateIds.slice(start, start + RAW_REPAIR_PAGE_SIZE);
|
|
2588
|
-
const rows = await ctx.messagesCollection.query(
|
|
2680
|
+
const rows = await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf(pageIds))).unsafeFetchRaw();
|
|
2589
2681
|
const changedIds = rows.filter((row) => chunksWithoutText(row.chunks) !== null).map((row) => String(row.id));
|
|
2590
2682
|
if (changedIds.length === 0) continue;
|
|
2591
2683
|
await ctx.database.write(async () => {
|
|
2592
|
-
const messages = await ctx.messagesCollection.query(
|
|
2684
|
+
const messages = await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf(changedIds))).fetch();
|
|
2593
2685
|
const updates = messages.flatMap((message) => {
|
|
2594
2686
|
const raw = message._raw;
|
|
2595
2687
|
const chunks3 = chunksWithoutText(raw.chunks);
|
|
@@ -2636,13 +2728,13 @@ async function encryptToolCallEventsOp(ctx) {
|
|
|
2636
2728
|
return 0;
|
|
2637
2729
|
}
|
|
2638
2730
|
const candidateIds = await ctx.messagesCollection.query(
|
|
2639
|
-
|
|
2640
|
-
|
|
2731
|
+
Q2.where("tool_call_events", Q2.notEq(null)),
|
|
2732
|
+
Q2.where("tool_call_events", Q2.notIn(["", "[]"]))
|
|
2641
2733
|
).fetchIds();
|
|
2642
2734
|
let encrypted = 0;
|
|
2643
2735
|
for (let start = 0; start < candidateIds.length; start += RAW_REPAIR_PAGE_SIZE) {
|
|
2644
2736
|
const pageIds = candidateIds.slice(start, start + RAW_REPAIR_PAGE_SIZE);
|
|
2645
|
-
const rows = await ctx.messagesCollection.query(
|
|
2737
|
+
const rows = await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf(pageIds))).unsafeFetchRaw();
|
|
2646
2738
|
const repairs = /* @__PURE__ */ new Map();
|
|
2647
2739
|
for (const row of rows) {
|
|
2648
2740
|
const column = await encryptedToolCallEventsColumn(
|
|
@@ -2655,12 +2747,12 @@ async function encryptToolCallEventsOp(ctx) {
|
|
|
2655
2747
|
}
|
|
2656
2748
|
if (repairs.size === 0) continue;
|
|
2657
2749
|
await ctx.database.write(async () => {
|
|
2658
|
-
const messages = (await ctx.messagesCollection.query(
|
|
2750
|
+
const messages = (await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf([...repairs.keys()]))).fetch()).filter(
|
|
2659
2751
|
(message) => message._raw.tool_call_events === repairs.get(message.id)?.raw
|
|
2660
2752
|
);
|
|
2661
2753
|
if (messages.length === 0) return;
|
|
2662
2754
|
const conversationIds = [...new Set(messages.map((message) => message.conversationId))];
|
|
2663
|
-
const conversations = await ctx.conversationsCollection.query(
|
|
2755
|
+
const conversations = await ctx.conversationsCollection.query(Q2.where("conversation_id", Q2.oneOf(conversationIds))).fetch();
|
|
2664
2756
|
await ctx.database.batch(
|
|
2665
2757
|
...messages.map(
|
|
2666
2758
|
(message) => message.prepareUpdate((msg) => {
|
|
@@ -2687,7 +2779,7 @@ async function savedKeyOpensExistingData(ctx, address, signMessage, embeddedWall
|
|
|
2687
2779
|
return refreshEncryptionKeyIfMatches(address, v2Probe, signMessage, embeddedWalletSigner);
|
|
2688
2780
|
}
|
|
2689
2781
|
async function findEncryptedContent(ctx, pattern) {
|
|
2690
|
-
const [row] = await ctx.messagesCollection.query(
|
|
2782
|
+
const [row] = await ctx.messagesCollection.query(Q2.where("content", Q2.like(pattern)), Q2.take(1)).unsafeFetchRaw();
|
|
2691
2783
|
return row ? String(row.content) : null;
|
|
2692
2784
|
}
|
|
2693
2785
|
async function updateMessageErrorOp(ctx, uniqueId, error) {
|
|
@@ -2800,7 +2892,7 @@ function toChunkCandidateKey(raw) {
|
|
|
2800
2892
|
}
|
|
2801
2893
|
async function fetchRowsByIds(ctx, ids) {
|
|
2802
2894
|
if (ids.length === 0) return [];
|
|
2803
|
-
return await ctx.messagesCollection.query(
|
|
2895
|
+
return await ctx.messagesCollection.query(Q2.where("id", Q2.oneOf(ids))).unsafeFetchRaw();
|
|
2804
2896
|
}
|
|
2805
2897
|
async function projectedChunkRowSource(ctx, filter) {
|
|
2806
2898
|
const clauses = [
|
|
@@ -2818,7 +2910,7 @@ async function projectedChunkRowSource(ctx, filter) {
|
|
|
2818
2910
|
args.push(filter.excludeConversationId);
|
|
2819
2911
|
}
|
|
2820
2912
|
const keyRows = await ctx.messagesCollection.query(
|
|
2821
|
-
|
|
2913
|
+
Q2.unsafeSqlQuery(
|
|
2822
2914
|
`select "id", "conversation_id", "updated_at", "embedding_model" from "history" where ${clauses.join(" and ")}`,
|
|
2823
2915
|
args
|
|
2824
2916
|
)
|
|
@@ -2828,7 +2920,7 @@ async function projectedChunkRowSource(ctx, filter) {
|
|
|
2828
2920
|
loadVectorRows: async (ids) => {
|
|
2829
2921
|
if (ids.length === 0) return [];
|
|
2830
2922
|
return await ctx.messagesCollection.query(
|
|
2831
|
-
|
|
2923
|
+
Q2.unsafeSqlQuery(
|
|
2832
2924
|
`select "id", "updated_at", "chunks", "vector" from "history" where "_status" is not 'deleted' and "id" in (${ids.map(() => "?").join(",")})`,
|
|
2833
2925
|
ids
|
|
2834
2926
|
)
|
|
@@ -2838,11 +2930,11 @@ async function projectedChunkRowSource(ctx, filter) {
|
|
|
2838
2930
|
};
|
|
2839
2931
|
}
|
|
2840
2932
|
async function fullScanChunkRowSource(ctx, filter) {
|
|
2841
|
-
const activeConversations = await ctx.conversationsCollection.query(
|
|
2933
|
+
const activeConversations = await ctx.conversationsCollection.query(Q2.where("is_deleted", false)).fetch();
|
|
2842
2934
|
const activeConversationIds = new Set(
|
|
2843
2935
|
activeConversations.map((c) => String(c._getRaw("conversation_id")))
|
|
2844
2936
|
);
|
|
2845
|
-
const queryConditions = filter.conversationId !== void 0 ? [
|
|
2937
|
+
const queryConditions = filter.conversationId !== void 0 ? [Q2.where("conversation_id", filter.conversationId)] : [];
|
|
2846
2938
|
const rows = await ctx.messagesCollection.query(...queryConditions).unsafeFetchRaw();
|
|
2847
2939
|
const rowsById = /* @__PURE__ */ new Map();
|
|
2848
2940
|
for (const row of rows) {
|
|
@@ -9671,7 +9763,7 @@ var chatStorageMigrations = schemaMigrations({
|
|
|
9671
9763
|
});
|
|
9672
9764
|
|
|
9673
9765
|
// src/lib/db/chat/summaryOperations.ts
|
|
9674
|
-
import { Q as
|
|
9766
|
+
import { Q as Q3 } from "@nozbe/watermelondb";
|
|
9675
9767
|
function createSummaryContext(database) {
|
|
9676
9768
|
return {
|
|
9677
9769
|
database,
|
|
@@ -9690,14 +9782,14 @@ function modelToStored(record2) {
|
|
|
9690
9782
|
};
|
|
9691
9783
|
}
|
|
9692
9784
|
async function getConversationSummaryOp(ctx, conversationId) {
|
|
9693
|
-
const results = await ctx.summariesCollection.query(
|
|
9785
|
+
const results = await ctx.summariesCollection.query(Q3.where("conversation_id", conversationId)).fetch();
|
|
9694
9786
|
if (results.length === 0) return null;
|
|
9695
9787
|
return modelToStored(results[0]);
|
|
9696
9788
|
}
|
|
9697
9789
|
async function upsertConversationSummaryOp(ctx, conversationId, summary, summarizedUpTo, tokenCount) {
|
|
9698
9790
|
let record2;
|
|
9699
9791
|
await ctx.database.write(async () => {
|
|
9700
|
-
const existing = await ctx.summariesCollection.query(
|
|
9792
|
+
const existing = await ctx.summariesCollection.query(Q3.where("conversation_id", conversationId)).fetch();
|
|
9701
9793
|
if (existing.length > 0) {
|
|
9702
9794
|
record2 = await existing[0].update((rec) => {
|
|
9703
9795
|
rec.summary = summary;
|
|
@@ -9717,7 +9809,7 @@ async function upsertConversationSummaryOp(ctx, conversationId, summary, summari
|
|
|
9717
9809
|
}
|
|
9718
9810
|
async function deleteConversationSummaryOp(ctx, conversationId) {
|
|
9719
9811
|
await ctx.database.write(async () => {
|
|
9720
|
-
const existing = await ctx.summariesCollection.query(
|
|
9812
|
+
const existing = await ctx.summariesCollection.query(Q3.where("conversation_id", conversationId)).fetch();
|
|
9721
9813
|
for (const record2 of existing) {
|
|
9722
9814
|
await record2.destroyPermanently();
|
|
9723
9815
|
}
|
|
@@ -9772,7 +9864,7 @@ __decorateClass([
|
|
|
9772
9864
|
], ConversationMemory.prototype, "createdAt", 2);
|
|
9773
9865
|
|
|
9774
9866
|
// src/lib/db/conversationMemory/operations.ts
|
|
9775
|
-
import { Q as
|
|
9867
|
+
import { Q as Q4 } from "@nozbe/watermelondb";
|
|
9776
9868
|
var MAX_PER_CONVERSATION = 200;
|
|
9777
9869
|
function conversationMemoryToStored(row) {
|
|
9778
9870
|
return {
|
|
@@ -9787,7 +9879,7 @@ async function addConversationMemoriesOp(ctx, conversationId, items) {
|
|
|
9787
9879
|
if (!conversationId || items.length === 0) return;
|
|
9788
9880
|
const now = Date.now();
|
|
9789
9881
|
await ctx.database.write(async () => {
|
|
9790
|
-
const existing = await ctx.conversationMemoryCollection.query(
|
|
9882
|
+
const existing = await ctx.conversationMemoryCollection.query(Q4.where("conversation_id", conversationId), Q4.sortBy("created_at", Q4.asc)).fetch();
|
|
9791
9883
|
const seen = new Set(existing.map((r) => r.memoryId));
|
|
9792
9884
|
const batchSeen = /* @__PURE__ */ new Set();
|
|
9793
9885
|
const deduped = [];
|
|
@@ -9817,13 +9909,13 @@ async function addConversationMemoriesOp(ctx, conversationId, items) {
|
|
|
9817
9909
|
}
|
|
9818
9910
|
async function getConversationMemoriesOp(ctx, conversationId) {
|
|
9819
9911
|
if (!conversationId) return [];
|
|
9820
|
-
const rows = await ctx.conversationMemoryCollection.query(
|
|
9912
|
+
const rows = await ctx.conversationMemoryCollection.query(Q4.where("conversation_id", conversationId), Q4.sortBy("created_at", Q4.asc)).fetch();
|
|
9821
9913
|
return rows.map(conversationMemoryToStored);
|
|
9822
9914
|
}
|
|
9823
9915
|
async function clearConversationMemoriesOp(ctx, conversationId) {
|
|
9824
9916
|
if (!conversationId) return;
|
|
9825
9917
|
await ctx.database.write(async () => {
|
|
9826
|
-
const rows = await ctx.conversationMemoryCollection.query(
|
|
9918
|
+
const rows = await ctx.conversationMemoryCollection.query(Q4.where("conversation_id", conversationId)).fetch();
|
|
9827
9919
|
if (rows.length === 0) return;
|
|
9828
9920
|
await ctx.database.batch(...rows.map((r) => r.prepareDestroyPermanently()));
|
|
9829
9921
|
});
|
|
@@ -11237,10 +11329,10 @@ var DatabaseManager = class {
|
|
|
11237
11329
|
};
|
|
11238
11330
|
|
|
11239
11331
|
// src/lib/db/memoryVault/operations.ts
|
|
11240
|
-
import { Q as
|
|
11332
|
+
import { Q as Q6 } from "@nozbe/watermelondb";
|
|
11241
11333
|
|
|
11242
11334
|
// src/lib/db/entities/operations.ts
|
|
11243
|
-
import { Q as
|
|
11335
|
+
import { Q as Q5 } from "@nozbe/watermelondb";
|
|
11244
11336
|
|
|
11245
11337
|
// src/lib/db/memoryVault/types.ts
|
|
11246
11338
|
var MEMORY_KINDS = [
|
|
@@ -11333,7 +11425,7 @@ async function upsertEntitiesInWrite(ctx, entities) {
|
|
|
11333
11425
|
const unique = Array.from(kindByName.keys());
|
|
11334
11426
|
const out = /* @__PURE__ */ new Map();
|
|
11335
11427
|
if (unique.length === 0) return { entities: out, operations: [], kindBackfills: [] };
|
|
11336
|
-
const existing = await ctx.entityCollection.query(
|
|
11428
|
+
const existing = await ctx.entityCollection.query(Q5.where("canonical_name", Q5.oneOf(unique))).fetch();
|
|
11337
11429
|
const existingNames = new Set(existing.map((e) => e.canonicalName));
|
|
11338
11430
|
const kindBackfills = existing.filter((e) => {
|
|
11339
11431
|
const incoming = kindByName.get(e.canonicalName);
|
|
@@ -11363,7 +11455,7 @@ function prepareKindBackfills(backfills) {
|
|
|
11363
11455
|
}
|
|
11364
11456
|
async function findVaultRowInWrite(ctx, memoryId) {
|
|
11365
11457
|
try {
|
|
11366
|
-
const rows = await ctx.database.get("memory_vault").query(
|
|
11458
|
+
const rows = await ctx.database.get("memory_vault").query(Q5.where("id", memoryId)).fetch();
|
|
11367
11459
|
return rows[0] ?? null;
|
|
11368
11460
|
} catch {
|
|
11369
11461
|
return null;
|
|
@@ -11465,7 +11557,7 @@ async function linkMemoryEntitiesOp(ctx, memoryId, entityInputs, options) {
|
|
|
11465
11557
|
return;
|
|
11466
11558
|
}
|
|
11467
11559
|
if (entities.length === 0) return;
|
|
11468
|
-
const existingLinks = await ctx.memoryEntityCollection.query(
|
|
11560
|
+
const existingLinks = await ctx.memoryEntityCollection.query(Q5.where("memory_id", memoryId)).fetch();
|
|
11469
11561
|
const existingEntityIds = new Set(existingLinks.map((l) => String(l.entityId)));
|
|
11470
11562
|
const toCreate = entities.filter((e) => !existingEntityIds.has(e.uniqueId));
|
|
11471
11563
|
const keptIds = entities.map((e) => e.uniqueId);
|
|
@@ -11501,7 +11593,7 @@ async function linkMemoryEntitiesOp(ctx, memoryId, entityInputs, options) {
|
|
|
11501
11593
|
}
|
|
11502
11594
|
async function resolveEntitiesByIds(ctx, entityIds) {
|
|
11503
11595
|
if (entityIds.length === 0) return [];
|
|
11504
|
-
const rows = await ctx.entityCollection.query(
|
|
11596
|
+
const rows = await ctx.entityCollection.query(Q5.where("id", Q5.oneOf(Array.from(entityIds)))).fetch();
|
|
11505
11597
|
return rows.map(entityToStored);
|
|
11506
11598
|
}
|
|
11507
11599
|
function autoLinkBlocked(row) {
|
|
@@ -11537,7 +11629,7 @@ async function replaceMemoryEntities(ctx, memoryId, entityInputs, options) {
|
|
|
11537
11629
|
kindBackfills
|
|
11538
11630
|
} = await upsertEntitiesInWrite(ctx, normalized);
|
|
11539
11631
|
entities = Array.from(byName.values());
|
|
11540
|
-
const existingLinks = await ctx.memoryEntityCollection.query(
|
|
11632
|
+
const existingLinks = await ctx.memoryEntityCollection.query(Q5.where("memory_id", memoryId)).fetch();
|
|
11541
11633
|
const keep = new Set(entities.map((e) => e.uniqueId));
|
|
11542
11634
|
const existingEntityIds = new Set(existingLinks.map((l) => String(l.entityId)));
|
|
11543
11635
|
const toCreate = entities.filter((e) => !existingEntityIds.has(e.uniqueId));
|
|
@@ -11573,17 +11665,17 @@ async function replaceMemoryEntities(ctx, memoryId, entityInputs, options) {
|
|
|
11573
11665
|
async function findOrphanedEntities(ctx, memoryId, toDestroy) {
|
|
11574
11666
|
const candidateIds = [...new Set(toDestroy.map((l) => String(l.entityId)))];
|
|
11575
11667
|
if (candidateIds.length === 0) return [];
|
|
11576
|
-
const links = await ctx.memoryEntityCollection.query(
|
|
11668
|
+
const links = await ctx.memoryEntityCollection.query(Q5.where("entity_id", Q5.oneOf(candidateIds))).fetch();
|
|
11577
11669
|
const stillLinked = new Set(
|
|
11578
11670
|
links.filter((l) => String(l.memoryId) !== memoryId).map((l) => String(l.entityId))
|
|
11579
11671
|
);
|
|
11580
11672
|
const orphanIds = candidateIds.filter((id) => !stillLinked.has(id));
|
|
11581
11673
|
if (orphanIds.length === 0) return [];
|
|
11582
|
-
return await ctx.entityCollection.query(
|
|
11674
|
+
return await ctx.entityCollection.query(Q5.where("id", Q5.oneOf(orphanIds))).fetch();
|
|
11583
11675
|
}
|
|
11584
11676
|
async function unlinkMemoryEntitiesOp(ctx, memoryIds) {
|
|
11585
11677
|
if (memoryIds.length === 0) return;
|
|
11586
|
-
const links = await ctx.memoryEntityCollection.query(
|
|
11678
|
+
const links = await ctx.memoryEntityCollection.query(Q5.where("memory_id", Q5.oneOf(Array.from(memoryIds)))).fetch();
|
|
11587
11679
|
if (links.length === 0) return;
|
|
11588
11680
|
await ctx.database.write(async () => {
|
|
11589
11681
|
await assertCanWrite(ctx);
|
|
@@ -11592,7 +11684,7 @@ async function unlinkMemoryEntitiesOp(ctx, memoryIds) {
|
|
|
11592
11684
|
}
|
|
11593
11685
|
async function unlinkAllMemoryEntitiesForUserOp(ctx, userId) {
|
|
11594
11686
|
if (!userId) return;
|
|
11595
|
-
const links = await ctx.memoryEntityCollection.query(
|
|
11687
|
+
const links = await ctx.memoryEntityCollection.query(Q5.where("user_id", userId)).fetch();
|
|
11596
11688
|
if (links.length === 0) return;
|
|
11597
11689
|
await ctx.database.write(async () => {
|
|
11598
11690
|
await assertCanWrite(ctx);
|
|
@@ -11602,15 +11694,15 @@ async function unlinkAllMemoryEntitiesForUserOp(ctx, userId) {
|
|
|
11602
11694
|
async function getMemoriesByEntityNamesOp(ctx, entityNames) {
|
|
11603
11695
|
const unique = Array.from(new Set(entityNames.map(normalizeEntityName).filter((n) => n.length > 0)));
|
|
11604
11696
|
if (unique.length === 0) return /* @__PURE__ */ new Map();
|
|
11605
|
-
const entityRows = await ctx.entityCollection.query(
|
|
11697
|
+
const entityRows = await ctx.entityCollection.query(Q5.where("canonical_name", Q5.oneOf(unique))).fetch();
|
|
11606
11698
|
if (entityRows.length === 0) return /* @__PURE__ */ new Map();
|
|
11607
11699
|
const entityIdToName = new Map(entityRows.map((e) => [e.id, e.canonicalName]));
|
|
11608
|
-
const linkConditions = [
|
|
11700
|
+
const linkConditions = [Q5.where("entity_id", Q5.oneOf(entityRows.map((e) => e.id)))];
|
|
11609
11701
|
if (ctx.userId !== void 0) {
|
|
11610
11702
|
if (ctx.allowUnscopedRows) {
|
|
11611
|
-
linkConditions.push(
|
|
11703
|
+
linkConditions.push(Q5.or(Q5.where("user_id", ctx.userId), Q5.where("user_id", null)));
|
|
11612
11704
|
} else {
|
|
11613
|
-
linkConditions.push(
|
|
11705
|
+
linkConditions.push(Q5.where("user_id", ctx.userId));
|
|
11614
11706
|
}
|
|
11615
11707
|
}
|
|
11616
11708
|
const links = await ctx.memoryEntityCollection.query(...linkConditions).fetch();
|
|
@@ -11631,18 +11723,18 @@ async function getMemoriesByEntityNamesOp(ctx, entityNames) {
|
|
|
11631
11723
|
async function getEntitiesByMemoryIdsOp(ctx, memoryIds) {
|
|
11632
11724
|
const unique = Array.from(new Set(memoryIds.filter((id) => id.length > 0)));
|
|
11633
11725
|
if (unique.length === 0) return /* @__PURE__ */ new Map();
|
|
11634
|
-
const linkConditions = [
|
|
11726
|
+
const linkConditions = [Q5.where("memory_id", Q5.oneOf(unique))];
|
|
11635
11727
|
if (ctx.userId !== void 0) {
|
|
11636
11728
|
if (ctx.allowUnscopedRows) {
|
|
11637
|
-
linkConditions.push(
|
|
11729
|
+
linkConditions.push(Q5.or(Q5.where("user_id", ctx.userId), Q5.where("user_id", null)));
|
|
11638
11730
|
} else {
|
|
11639
|
-
linkConditions.push(
|
|
11731
|
+
linkConditions.push(Q5.where("user_id", ctx.userId));
|
|
11640
11732
|
}
|
|
11641
11733
|
}
|
|
11642
11734
|
const links = await ctx.memoryEntityCollection.query(...linkConditions).fetch();
|
|
11643
11735
|
if (links.length === 0) return /* @__PURE__ */ new Map();
|
|
11644
11736
|
const entityIds = Array.from(new Set(links.map((l) => String(l.entityId))));
|
|
11645
|
-
const entityRows = await ctx.entityCollection.query(
|
|
11737
|
+
const entityRows = await ctx.entityCollection.query(Q5.where("id", Q5.oneOf(entityIds))).fetch();
|
|
11646
11738
|
const entityIdToName = new Map(entityRows.map((e) => [e.id, e.canonicalName]));
|
|
11647
11739
|
const out = /* @__PURE__ */ new Map();
|
|
11648
11740
|
for (const link of links) {
|
|
@@ -11706,11 +11798,11 @@ var NON_PRIVATE_VISIBILITIES = ["public"];
|
|
|
11706
11798
|
function visibilityConditions(requested) {
|
|
11707
11799
|
if (!requested?.length) return [];
|
|
11708
11800
|
if (!requested.includes("private")) {
|
|
11709
|
-
return [
|
|
11801
|
+
return [Q6.where("visibility", Q6.oneOf([...requested]))];
|
|
11710
11802
|
}
|
|
11711
11803
|
const excluded = NON_PRIVATE_VISIBILITIES.filter((v) => !requested.includes(v));
|
|
11712
11804
|
if (excluded.length === 0) return [];
|
|
11713
|
-
return [
|
|
11805
|
+
return [Q6.or(Q6.where("visibility", null), Q6.where("visibility", Q6.notIn(excluded)))];
|
|
11714
11806
|
}
|
|
11715
11807
|
function isKinded(record2) {
|
|
11716
11808
|
return record2.kind !== null && record2.kind !== void 0;
|
|
@@ -11751,17 +11843,17 @@ function levelConditions(requested) {
|
|
|
11751
11843
|
const published = ["shared", "public"];
|
|
11752
11844
|
const wantsMatching = requested.includes("matching");
|
|
11753
11845
|
const wantsPrivate = requested.includes("private");
|
|
11754
|
-
const unresolved =
|
|
11755
|
-
const byLevel =
|
|
11756
|
-
if (wantsMatching && wantsPrivate) return [
|
|
11846
|
+
const unresolved = Q6.or(Q6.where("level", null), Q6.where("level", Q6.notIn([...MEMORY_LEVELS])));
|
|
11847
|
+
const byLevel = Q6.where("level", Q6.oneOf([...requested]));
|
|
11848
|
+
if (wantsMatching && wantsPrivate) return [Q6.or(byLevel, unresolved)];
|
|
11757
11849
|
if (wantsMatching) {
|
|
11758
|
-
return [
|
|
11850
|
+
return [Q6.or(byLevel, Q6.and(unresolved, Q6.where("scope", Q6.oneOf(published))))];
|
|
11759
11851
|
}
|
|
11760
11852
|
if (wantsPrivate) {
|
|
11761
11853
|
return [
|
|
11762
|
-
|
|
11854
|
+
Q6.or(
|
|
11763
11855
|
byLevel,
|
|
11764
|
-
|
|
11856
|
+
Q6.and(unresolved, Q6.or(Q6.where("scope", null), Q6.where("scope", Q6.notIn(published))))
|
|
11765
11857
|
)
|
|
11766
11858
|
];
|
|
11767
11859
|
}
|
|
@@ -11772,13 +11864,13 @@ function isOwnedByCtxUser(ctx, record2) {
|
|
|
11772
11864
|
}
|
|
11773
11865
|
function baseVaultConditions(ctx, options) {
|
|
11774
11866
|
return [
|
|
11775
|
-
...options?.memoryIds !== void 0 ? [
|
|
11776
|
-
...options?.includeDeleted ? [] : [
|
|
11777
|
-
...options?.includeArchived ? [] : [
|
|
11778
|
-
...options?.includeQuarantined ? [] : [
|
|
11779
|
-
...options?.includeSuperseded ? [] : [
|
|
11780
|
-
...ctx.userId !== void 0 ? [
|
|
11781
|
-
...options?.since ? [
|
|
11867
|
+
...options?.memoryIds !== void 0 ? [Q6.where("id", Q6.oneOf(options.memoryIds))] : [],
|
|
11868
|
+
...options?.includeDeleted ? [] : [Q6.where("is_deleted", false)],
|
|
11869
|
+
...options?.includeArchived ? [] : [Q6.where("archived_at", Q6.eq(null))],
|
|
11870
|
+
...options?.includeQuarantined ? [] : [Q6.where("trust_tier", Q6.notEq("quarantined"))],
|
|
11871
|
+
...options?.includeSuperseded ? [] : [Q6.where("superseded_by", null)],
|
|
11872
|
+
...ctx.userId !== void 0 ? [Q6.where("user_id", ctx.userId)] : [],
|
|
11873
|
+
...options?.since ? [Q6.where("updated_at", Q6.gt(options.since.getTime()))] : []
|
|
11782
11874
|
];
|
|
11783
11875
|
}
|
|
11784
11876
|
var KNOWN_TRUST_TIERS = /* @__PURE__ */ new Set(["quarantined", "trusted"]);
|
|
@@ -11948,11 +12040,11 @@ async function createSupersedingMemoryOp(ctx, opts, targetId) {
|
|
|
11948
12040
|
async function getMemoriesByEventTimeOp(ctx, windowStart, windowEnd) {
|
|
11949
12041
|
const records = await ctx.vaultMemoryCollection.query(
|
|
11950
12042
|
...baseVaultConditions(ctx),
|
|
11951
|
-
|
|
11952
|
-
|
|
11953
|
-
|
|
11954
|
-
|
|
11955
|
-
|
|
12043
|
+
Q6.where("event_time_start", Q6.notEq(null)),
|
|
12044
|
+
Q6.where("event_time_start", Q6.lte(windowEnd)),
|
|
12045
|
+
Q6.or(
|
|
12046
|
+
Q6.where("event_time_start", Q6.gte(windowStart)),
|
|
12047
|
+
Q6.where("event_time_kind", Q6.oneOf(["range", "ongoing"]))
|
|
11956
12048
|
)
|
|
11957
12049
|
).fetch();
|
|
11958
12050
|
const out = [];
|
|
@@ -12108,18 +12200,18 @@ async function getAllVaultMemoriesOp(ctx, options) {
|
|
|
12108
12200
|
function vaultListConditions(ctx, options) {
|
|
12109
12201
|
return [
|
|
12110
12202
|
...baseVaultConditions(ctx, options),
|
|
12111
|
-
...options?.scopes?.length ? [
|
|
12203
|
+
...options?.scopes?.length ? [Q6.where("scope", Q6.oneOf(options.scopes))] : [],
|
|
12112
12204
|
...visibilityConditions(options?.visibility),
|
|
12113
12205
|
...levelConditions(options?.levels),
|
|
12114
|
-
...options?.kinds?.length ? [
|
|
12115
|
-
...options?.folderId !== void 0 ? [
|
|
12116
|
-
...options?.factTypes?.length ? [
|
|
12206
|
+
...options?.kinds?.length ? [Q6.where("kind", Q6.oneOf(options.kinds))] : [],
|
|
12207
|
+
...options?.folderId !== void 0 ? [Q6.where("folder_id", options.folderId)] : [],
|
|
12208
|
+
...options?.factTypes?.length ? [Q6.where("fact_type", Q6.oneOf(options.factTypes))] : []
|
|
12117
12209
|
];
|
|
12118
12210
|
}
|
|
12119
12211
|
function vaultListOrder(options) {
|
|
12120
12212
|
return [
|
|
12121
|
-
|
|
12122
|
-
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [
|
|
12213
|
+
Q6.sortBy(options?.since ? "updated_at" : "created_at", Q6.desc),
|
|
12214
|
+
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [Q6.take(options.limit)] : []
|
|
12123
12215
|
];
|
|
12124
12216
|
}
|
|
12125
12217
|
async function getAllVaultMemoryRowsOp(ctx) {
|
|
@@ -12158,10 +12250,10 @@ function vaultMemoryRawToRankable(raw) {
|
|
|
12158
12250
|
async function getVaultRankingProjectionsOp(ctx, options) {
|
|
12159
12251
|
const conditions = [
|
|
12160
12252
|
...baseVaultConditions(ctx, options),
|
|
12161
|
-
...options?.scopes?.length ? [
|
|
12162
|
-
...options?.folderId !== void 0 ? [
|
|
12163
|
-
|
|
12164
|
-
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [
|
|
12253
|
+
...options?.scopes?.length ? [Q6.where("scope", Q6.oneOf(options.scopes))] : [],
|
|
12254
|
+
...options?.folderId !== void 0 ? [Q6.where("folder_id", options.folderId)] : [],
|
|
12255
|
+
Q6.sortBy(options?.since ? "updated_at" : "created_at", Q6.desc),
|
|
12256
|
+
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [Q6.take(options.limit)] : []
|
|
12165
12257
|
];
|
|
12166
12258
|
const results = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
|
|
12167
12259
|
return results.map(vaultMemoryRawToRankable);
|
|
@@ -12212,7 +12304,7 @@ async function getVaultCandidateKeysOp(ctx, options) {
|
|
|
12212
12304
|
args.push(...options.factTypes);
|
|
12213
12305
|
}
|
|
12214
12306
|
const sql = `select "id", "scope", "folder_id", "embedding_model", "updated_at" from "memory_vault" where ${clauses.join(" and ")}`;
|
|
12215
|
-
const rows = await ctx.vaultMemoryCollection.query(
|
|
12307
|
+
const rows = await ctx.vaultMemoryCollection.query(Q6.unsafeSqlQuery(sql, args)).unsafeFetchRaw();
|
|
12216
12308
|
return rows.map(mapRaw);
|
|
12217
12309
|
} catch (err) {
|
|
12218
12310
|
getLogger().debug(
|
|
@@ -12223,9 +12315,9 @@ async function getVaultCandidateKeysOp(ctx, options) {
|
|
|
12223
12315
|
memoryIds: options?.memoryIds,
|
|
12224
12316
|
...options?.includeArchived !== void 0 && { includeArchived: options.includeArchived }
|
|
12225
12317
|
}),
|
|
12226
|
-
...options?.scopes?.length ? [
|
|
12227
|
-
...options?.folderId !== void 0 ? [
|
|
12228
|
-
...options?.factTypes?.length ? [
|
|
12318
|
+
...options?.scopes?.length ? [Q6.where("scope", Q6.oneOf(options.scopes))] : [],
|
|
12319
|
+
...options?.folderId !== void 0 ? [Q6.where("folder_id", options.folderId)] : [],
|
|
12320
|
+
...options?.factTypes?.length ? [Q6.where("fact_type", Q6.oneOf(options.factTypes))] : []
|
|
12229
12321
|
];
|
|
12230
12322
|
const rows = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
|
|
12231
12323
|
return rows.map(mapRaw);
|
|
@@ -12243,7 +12335,7 @@ async function getVaultEmbeddingsByIdsOp(ctx, ids, options) {
|
|
|
12243
12335
|
...options?.includeArchived !== void 0 && { includeArchived: options.includeArchived }
|
|
12244
12336
|
});
|
|
12245
12337
|
const sql = `select "id", "embedding", "embedding_model" from "memory_vault" where ${base.sql} and "id" in (${ids.map(() => "?").join(",")})`;
|
|
12246
|
-
const rows = await ctx.vaultMemoryCollection.query(
|
|
12338
|
+
const rows = await ctx.vaultMemoryCollection.query(Q6.unsafeSqlQuery(sql, [...base.args, ...ids])).unsafeFetchRaw();
|
|
12247
12339
|
return rows.map(mapRaw);
|
|
12248
12340
|
} catch (err) {
|
|
12249
12341
|
getLogger().debug(
|
|
@@ -12255,7 +12347,7 @@ async function getVaultEmbeddingsByIdsOp(ctx, ids, options) {
|
|
|
12255
12347
|
includeArchived: options.includeArchived
|
|
12256
12348
|
}
|
|
12257
12349
|
}),
|
|
12258
|
-
|
|
12350
|
+
Q6.where("id", Q6.oneOf(ids))
|
|
12259
12351
|
).unsafeFetchRaw();
|
|
12260
12352
|
return rows.map(mapRaw);
|
|
12261
12353
|
}
|
|
@@ -12266,7 +12358,7 @@ async function getVaultMemoriesByIdsOp(ctx, ids, options) {
|
|
|
12266
12358
|
...baseVaultConditions(ctx, {
|
|
12267
12359
|
...options?.includeArchived !== void 0 && { includeArchived: options.includeArchived }
|
|
12268
12360
|
}),
|
|
12269
|
-
|
|
12361
|
+
Q6.where("id", Q6.oneOf(ids))
|
|
12270
12362
|
];
|
|
12271
12363
|
const results = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
|
|
12272
12364
|
return mapInBatches(
|
|
@@ -12291,7 +12383,7 @@ async function countActiveVaultMemoriesOp(ctx) {
|
|
|
12291
12383
|
}
|
|
12292
12384
|
async function getActiveVaultMemoryIdsOp(ctx, ids) {
|
|
12293
12385
|
if (ids.length === 0) return /* @__PURE__ */ new Set();
|
|
12294
|
-
const rows = await ctx.vaultMemoryCollection.query(...baseVaultConditions(ctx),
|
|
12386
|
+
const rows = await ctx.vaultMemoryCollection.query(...baseVaultConditions(ctx), Q6.where("id", Q6.oneOf(ids))).unsafeFetchRaw();
|
|
12295
12387
|
return new Set(rows.map((r) => r.id));
|
|
12296
12388
|
}
|
|
12297
12389
|
async function updateVaultMemoryOp(ctx, id, opts) {
|
|
@@ -12440,7 +12532,7 @@ async function setMemoryEntitiesOp(ctx, memoryId, entities) {
|
|
|
12440
12532
|
() => linkMemoryEntitiesOp(entityCtx, memoryId, entities, { topicsSource: "user" })
|
|
12441
12533
|
) : [];
|
|
12442
12534
|
const keep = new Set(linked.map((e) => e.uniqueId));
|
|
12443
|
-
const existing = await entityCtx.memoryEntityCollection.query(
|
|
12535
|
+
const existing = await entityCtx.memoryEntityCollection.query(Q6.where("memory_id", memoryId)).fetch();
|
|
12444
12536
|
const staleLinks = existing.filter((l) => !keep.has(String(l.entityId)));
|
|
12445
12537
|
if (staleLinks.length === 0 && entities.length > 0) return;
|
|
12446
12538
|
const topicsWrite = await resolveMemoryTopicsWrite(
|
|
@@ -12581,9 +12673,9 @@ async function supersedeVaultMemoryOp(ctx, id, supersededById) {
|
|
|
12581
12673
|
}
|
|
12582
12674
|
async function getUnfiledVaultMemoriesOp(ctx) {
|
|
12583
12675
|
const conditions = [
|
|
12584
|
-
|
|
12676
|
+
Q6.where("folder_id", null),
|
|
12585
12677
|
...baseVaultConditions(ctx),
|
|
12586
|
-
|
|
12678
|
+
Q6.sortBy("created_at", Q6.desc)
|
|
12587
12679
|
];
|
|
12588
12680
|
const results = await ctx.vaultMemoryCollection.query(...conditions).fetch();
|
|
12589
12681
|
return mapInBatches(
|
|
@@ -12597,7 +12689,7 @@ async function linkedEntityNamesByMemory(entityCtx, memoryIds) {
|
|
|
12597
12689
|
const entityIdByMemory = /* @__PURE__ */ new Map();
|
|
12598
12690
|
const allEntityIds = /* @__PURE__ */ new Set();
|
|
12599
12691
|
for (let i = 0; i < memoryIds.length; i += CHUNK) {
|
|
12600
|
-
const links = await entityCtx.memoryEntityCollection.query(
|
|
12692
|
+
const links = await entityCtx.memoryEntityCollection.query(Q6.where("memory_id", Q6.oneOf(memoryIds.slice(i, i + CHUNK)))).unsafeFetchRaw();
|
|
12601
12693
|
for (const link of links) {
|
|
12602
12694
|
const memoryId = String(link.memory_id);
|
|
12603
12695
|
const entityId = String(link.entity_id);
|
|
@@ -12613,7 +12705,7 @@ async function linkedEntityNamesByMemory(entityCtx, memoryIds) {
|
|
|
12613
12705
|
const entityIds = Array.from(allEntityIds);
|
|
12614
12706
|
const nameById = /* @__PURE__ */ new Map();
|
|
12615
12707
|
for (let i = 0; i < entityIds.length; i += CHUNK) {
|
|
12616
|
-
const entities = await entityCtx.entityCollection.query(
|
|
12708
|
+
const entities = await entityCtx.entityCollection.query(Q6.where("id", Q6.oneOf(entityIds.slice(i, i + CHUNK)))).unsafeFetchRaw();
|
|
12617
12709
|
for (const e of entities) {
|
|
12618
12710
|
if (typeof e.canonical_name === "string") nameById.set(String(e.id), e.canonical_name);
|
|
12619
12711
|
}
|
|
@@ -12642,7 +12734,7 @@ async function getMemoriesNeedingTopicExtractionOp(ctx, options) {
|
|
|
12642
12734
|
if (!entityCtx) {
|
|
12643
12735
|
throw new Error("getMemoriesNeedingTopicExtractionOp requires ctx.entityCtx");
|
|
12644
12736
|
}
|
|
12645
|
-
const conditions = [...baseVaultConditions(ctx),
|
|
12737
|
+
const conditions = [...baseVaultConditions(ctx), Q6.sortBy("created_at", Q6.desc)];
|
|
12646
12738
|
const rows = await ctx.vaultMemoryCollection.query(...conditions).unsafeFetchRaw();
|
|
12647
12739
|
const linkedNames = await linkedEntityNamesByMemory(
|
|
12648
12740
|
entityCtx,
|
|
@@ -12798,10 +12890,10 @@ async function backfillMemoryTopicsOp(ctx, memoryIds) {
|
|
|
12798
12890
|
}
|
|
12799
12891
|
if (record2.isDeleted || !isOwnedByCtxUser(ctx, record2)) continue;
|
|
12800
12892
|
if (parseTopics(record2.topics) !== null) continue;
|
|
12801
|
-
const links = await entityCtx.memoryEntityCollection.query(
|
|
12893
|
+
const links = await entityCtx.memoryEntityCollection.query(Q6.where("memory_id", id)).unsafeFetchRaw();
|
|
12802
12894
|
const entityIds = links.map((l) => String(l.entity_id));
|
|
12803
12895
|
if (entityIds.length === 0) continue;
|
|
12804
|
-
const entities = await entityCtx.entityCollection.query(
|
|
12896
|
+
const entities = await entityCtx.entityCollection.query(Q6.where("id", Q6.oneOf(entityIds))).fetch();
|
|
12805
12897
|
if (entities.length === 0) continue;
|
|
12806
12898
|
const source = record2.topicsUserManaged ? "user" : "auto";
|
|
12807
12899
|
const write = await resolveMemoryTopicsWrite(entityCtx, id, entities, [], source);
|
|
@@ -12817,7 +12909,7 @@ async function backfillMemoryTopicsOp(ctx, memoryIds) {
|
|
|
12817
12909
|
}
|
|
12818
12910
|
async function deleteAllVaultMemoriesForUserOp(ctx, userId) {
|
|
12819
12911
|
if (ctx.userId !== void 0 && ctx.userId !== userId) return 0;
|
|
12820
|
-
const records = await ctx.vaultMemoryCollection.query(
|
|
12912
|
+
const records = await ctx.vaultMemoryCollection.query(Q6.where("user_id", userId), Q6.where("is_deleted", false)).fetch();
|
|
12821
12913
|
if (records.length === 0) return 0;
|
|
12822
12914
|
await ctx.database.write(async () => {
|
|
12823
12915
|
await assertCanWrite2(ctx);
|
|
@@ -12854,7 +12946,7 @@ async function getDecayCandidatesRawOp(ctx) {
|
|
|
12854
12946
|
assertVaultScopeForSweep(ctx);
|
|
12855
12947
|
const results = await ctx.vaultMemoryCollection.query(
|
|
12856
12948
|
...baseVaultConditions(ctx, { includeArchived: true, includeQuarantined: true }),
|
|
12857
|
-
|
|
12949
|
+
Q6.where("kind", null)
|
|
12858
12950
|
).unsafeFetchRaw();
|
|
12859
12951
|
return results.map((raw) => ({
|
|
12860
12952
|
uniqueId: raw.id,
|
|
@@ -12984,11 +13076,11 @@ async function findQuarantinedDuplicateOp(ctx, content, sourceIds2, where) {
|
|
|
12984
13076
|
const wanted = [...new Set(sourceIds2)].sort().join("\0");
|
|
12985
13077
|
const target = normalizeForDedupe(content);
|
|
12986
13078
|
const rows = await ctx.vaultMemoryCollection.query(
|
|
12987
|
-
|
|
12988
|
-
|
|
12989
|
-
|
|
12990
|
-
|
|
12991
|
-
...ctx.userId !== void 0 ? [
|
|
13079
|
+
Q6.where("trust_tier", "quarantined"),
|
|
13080
|
+
Q6.where("is_deleted", false),
|
|
13081
|
+
Q6.where("scope", where.scope),
|
|
13082
|
+
Q6.where("folder_id", where.folderId),
|
|
13083
|
+
...ctx.userId !== void 0 ? [Q6.where("user_id", ctx.userId)] : []
|
|
12992
13084
|
).unsafeFetchRaw();
|
|
12993
13085
|
for (const raw of rows) {
|
|
12994
13086
|
let ids;
|
|
@@ -13483,7 +13575,7 @@ var WalletPoller = class {
|
|
|
13483
13575
|
};
|
|
13484
13576
|
|
|
13485
13577
|
// src/lib/db/vaultFolders/operations.ts
|
|
13486
|
-
import { Q as
|
|
13578
|
+
import { Q as Q7 } from "@nozbe/watermelondb";
|
|
13487
13579
|
function folderToStored(folder) {
|
|
13488
13580
|
return {
|
|
13489
13581
|
uniqueId: folder.id,
|
|
@@ -13508,7 +13600,7 @@ async function createVaultFolderOp(ctx, opts) {
|
|
|
13508
13600
|
return folderToStored(created);
|
|
13509
13601
|
}
|
|
13510
13602
|
async function getAllVaultFoldersOp(ctx) {
|
|
13511
|
-
const results = await ctx.vaultFolderCollection.query(
|
|
13603
|
+
const results = await ctx.vaultFolderCollection.query(Q7.where("is_deleted", false), Q7.sortBy("created_at", Q7.desc)).fetch();
|
|
13512
13604
|
return results.map(folderToStored);
|
|
13513
13605
|
}
|
|
13514
13606
|
async function updateVaultFolderOp(ctx, id, opts) {
|
|
@@ -13517,7 +13609,7 @@ async function updateVaultFolderOp(ctx, id, opts) {
|
|
|
13517
13609
|
if (record2.isDeleted) return null;
|
|
13518
13610
|
const scopeChanged = opts.scope !== void 0 && opts.scope !== record2.scope;
|
|
13519
13611
|
const updated = await ctx.database.write(async () => {
|
|
13520
|
-
const memories = scopeChanged ? await ctx.vaultMemoryCollection.query(
|
|
13612
|
+
const memories = scopeChanged ? await ctx.vaultMemoryCollection.query(Q7.where("folder_id", id), Q7.where("is_deleted", false)).fetch() : [];
|
|
13521
13613
|
const updates = [];
|
|
13522
13614
|
updates.push(
|
|
13523
13615
|
record2.prepareUpdate((r) => {
|
|
@@ -13546,7 +13638,7 @@ async function deleteVaultFolderOp(ctx, id) {
|
|
|
13546
13638
|
const record2 = await ctx.vaultFolderCollection.find(id);
|
|
13547
13639
|
if (record2.isDeleted) return false;
|
|
13548
13640
|
await ctx.database.write(async () => {
|
|
13549
|
-
const memories = await ctx.vaultMemoryCollection.query(
|
|
13641
|
+
const memories = await ctx.vaultMemoryCollection.query(Q7.where("folder_id", id), Q7.where("is_deleted", false)).fetch();
|
|
13550
13642
|
const preparedMemories = memories.map(
|
|
13551
13643
|
(memory) => memory.prepareUpdate((r) => {
|
|
13552
13644
|
r._setRaw("folder_id", null);
|
|
@@ -13617,7 +13709,7 @@ async function updateVaultFolderContextOp(ctx, id, context) {
|
|
|
13617
13709
|
}
|
|
13618
13710
|
}
|
|
13619
13711
|
async function getVaultFolderMemoryCountOp(ctx, folderId) {
|
|
13620
|
-
return await ctx.vaultMemoryCollection.query(
|
|
13712
|
+
return await ctx.vaultMemoryCollection.query(Q7.where("folder_id", folderId), Q7.where("is_deleted", false)).fetchCount();
|
|
13621
13713
|
}
|
|
13622
13714
|
|
|
13623
13715
|
// src/lib/db/vaultFolders/defaults.ts
|
|
@@ -13660,7 +13752,7 @@ async function _ensureDefaultFoldersImpl(ctx) {
|
|
|
13660
13752
|
}
|
|
13661
13753
|
|
|
13662
13754
|
// src/lib/memory/autoExtract.ts
|
|
13663
|
-
import { Q as
|
|
13755
|
+
import { Q as Q8 } from "@nozbe/watermelondb";
|
|
13664
13756
|
|
|
13665
13757
|
// src/lib/memory/entitySalience.ts
|
|
13666
13758
|
var GENERIC_ENTITY_NAMES = /* @__PURE__ */ new Set([
|
|
@@ -16368,7 +16460,7 @@ async function assertMergeTargetGoneOrThrow(ctx, targetId) {
|
|
|
16368
16460
|
// src/lib/memory/autoExtract.ts
|
|
16369
16461
|
async function isMemoryTopicsUserManaged(ctx, memoryId) {
|
|
16370
16462
|
try {
|
|
16371
|
-
const rows = await ctx.database.get(VaultMemory.table).query(
|
|
16463
|
+
const rows = await ctx.database.get(VaultMemory.table).query(Q8.where("id", memoryId)).fetch();
|
|
16372
16464
|
return rows[0]?.topicsUserManaged === true;
|
|
16373
16465
|
} catch {
|
|
16374
16466
|
return true;
|
|
@@ -18913,7 +19005,7 @@ function parseAnswer(body, base, parseSchema) {
|
|
|
18913
19005
|
}
|
|
18914
19006
|
|
|
18915
19007
|
// src/lib/memory/store/local.ts
|
|
18916
|
-
import { Q as
|
|
19008
|
+
import { Q as Q9 } from "@nozbe/watermelondb";
|
|
18917
19009
|
|
|
18918
19010
|
// src/lib/memory/topicExtract.ts
|
|
18919
19011
|
var TOPIC_EXTRACTION_BATCH_SIZE = 10;
|
|
@@ -19192,8 +19284,8 @@ function createLocalMemoryStore(options) {
|
|
|
19192
19284
|
writeOptions?.signal?.throwIfAborted();
|
|
19193
19285
|
};
|
|
19194
19286
|
const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
|
|
19195
|
-
const ownedBy = userId !== void 0 ? [
|
|
19196
|
-
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [
|
|
19287
|
+
const ownedBy = userId !== void 0 ? [Q9.where("user_id", userId)] : [];
|
|
19288
|
+
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [Q9.or(Q9.where("user_id", userId), Q9.where("user_id", null))] : [Q9.where("user_id", userId)];
|
|
19197
19289
|
const watchedColumns = [
|
|
19198
19290
|
"content",
|
|
19199
19291
|
"scope",
|
|
@@ -19238,7 +19330,7 @@ function createLocalMemoryStore(options) {
|
|
|
19238
19330
|
list: (listOptions) => getAllVaultMemoriesOp(vaultCtx, listOptions),
|
|
19239
19331
|
get: (id) => getVaultMemoryOp(vaultCtx, id),
|
|
19240
19332
|
listArchived: async () => {
|
|
19241
|
-
const ids = await vaultCtx.vaultMemoryCollection.query(
|
|
19333
|
+
const ids = await vaultCtx.vaultMemoryCollection.query(Q9.where("is_deleted", false), Q9.where("archived_at", Q9.notEq(null)), ...ownedBy).fetchIds();
|
|
19242
19334
|
if (ids.length === 0) return [];
|
|
19243
19335
|
const rows = await getAllVaultMemoriesOp(vaultCtx, { memoryIds: ids, includeArchived: true });
|
|
19244
19336
|
return rows.sort((a, b) => (b.archivedAt ?? 0) - (a.archivedAt ?? 0));
|
|
@@ -19254,7 +19346,7 @@ function createLocalMemoryStore(options) {
|
|
|
19254
19346
|
set.add(String(link.memory_id));
|
|
19255
19347
|
members.set(entityId, set);
|
|
19256
19348
|
}
|
|
19257
|
-
const entities = userId === void 0 ? await entityCtx.entityCollection.query().unsafeFetchRaw() : members.size ? await entityCtx.entityCollection.query(
|
|
19349
|
+
const entities = userId === void 0 ? await entityCtx.entityCollection.query().unsafeFetchRaw() : members.size ? await entityCtx.entityCollection.query(Q9.where("id", Q9.oneOf([...members.keys()]))).unsafeFetchRaw() : [];
|
|
19258
19350
|
const byName = /* @__PURE__ */ new Map();
|
|
19259
19351
|
for (const entity of entities) {
|
|
19260
19352
|
const name = String(entity.canonical_name);
|
|
@@ -19304,7 +19396,7 @@ function createLocalMemoryStore(options) {
|
|
|
19304
19396
|
setTopics: (memoryId, topics) => setMemoryEntitiesOp(vaultCtx, memoryId, topics),
|
|
19305
19397
|
addTopics: (memoryId, topics, writeOptions) => database.write(async (writer) => {
|
|
19306
19398
|
await assertWrite(writeOptions);
|
|
19307
|
-
const owned = await vaultCtx.vaultMemoryCollection.query(
|
|
19399
|
+
const owned = await vaultCtx.vaultMemoryCollection.query(Q9.where("id", memoryId), Q9.where("is_deleted", false), ...ownedBy).fetchCount();
|
|
19308
19400
|
return owned > 0 ? writer.callWriter(
|
|
19309
19401
|
() => linkMemoryEntitiesOp(
|
|
19310
19402
|
{
|
|
@@ -19376,7 +19468,7 @@ function createLocalMemoryStore(options) {
|
|
|
19376
19468
|
else onChange();
|
|
19377
19469
|
};
|
|
19378
19470
|
};
|
|
19379
|
-
const memories = subscribeOptions?.includeDeleted ? vaultCtx.vaultMemoryCollection.query(...ownedBy).observe() : vaultCtx.vaultMemoryCollection.query(
|
|
19471
|
+
const memories = subscribeOptions?.includeDeleted ? vaultCtx.vaultMemoryCollection.query(...ownedBy).observe() : vaultCtx.vaultMemoryCollection.query(Q9.where("is_deleted", false), ...ownedBy).observeWithColumns(
|
|
19380
19472
|
subscribeOptions?.embeddings ? [...watchedColumns, "embedding", "embedding_model"] : watchedColumns
|
|
19381
19473
|
);
|
|
19382
19474
|
const subscriptions = [memories.subscribe(afterFirst())];
|
|
@@ -22435,7 +22527,7 @@ async function assembleMemoryContext(options) {
|
|
|
22435
22527
|
}
|
|
22436
22528
|
|
|
22437
22529
|
// src/lib/memory/durableExtraction.ts
|
|
22438
|
-
import { Q as
|
|
22530
|
+
import { Q as Q10 } from "@nozbe/watermelondb";
|
|
22439
22531
|
var MAX_JOBS_PER_PASS = 3;
|
|
22440
22532
|
var MAX_ATTEMPTS = 3;
|
|
22441
22533
|
var MAX_FAILED_SESSIONS = 3;
|
|
@@ -22606,7 +22698,7 @@ function createDurableAutoExtractor(options) {
|
|
|
22606
22698
|
const source = options.messageSource?.(conversationId);
|
|
22607
22699
|
if (source)
|
|
22608
22700
|
return await fromSource(source.status(conversationId)) === "ready" && (await loadFromSource(source, conversationId, ids)).every(Boolean);
|
|
22609
|
-
return await storage.conversationsCollection.query(
|
|
22701
|
+
return await storage.conversationsCollection.query(Q10.where("conversation_id", conversationId), Q10.where("is_deleted", false)).fetchCount() > 0 && await storage.messagesCollection.query(Q10.where("conversation_id", conversationId), Q10.where("id", Q10.oneOf(ids))).fetchCount() === ids.length;
|
|
22610
22702
|
}
|
|
22611
22703
|
function schedule(delay) {
|
|
22612
22704
|
if (disposed) return;
|
|
@@ -22780,7 +22872,7 @@ function createDurableAutoExtractor(options) {
|
|
|
22780
22872
|
let sourceWaiting = false;
|
|
22781
22873
|
try {
|
|
22782
22874
|
await writes;
|
|
22783
|
-
const pending = await jobs.query(
|
|
22875
|
+
const pending = await jobs.query(Q10.where("owner_key", owner), Q10.where("message_ids", Q10.notEq("[]"))).fetch();
|
|
22784
22876
|
storeFailures = 0;
|
|
22785
22877
|
let processed = 0;
|
|
22786
22878
|
for (const job of pending) {
|
|
@@ -22812,7 +22904,7 @@ function createDurableAutoExtractor(options) {
|
|
|
22812
22904
|
let ids = [];
|
|
22813
22905
|
let loaded = [];
|
|
22814
22906
|
try {
|
|
22815
|
-
const present = source ? sourceStatus === "ready" : await storage.conversationsCollection.query(
|
|
22907
|
+
const present = source ? sourceStatus === "ready" : await storage.conversationsCollection.query(Q10.where("conversation_id", conversationId), Q10.where("is_deleted", false)).fetchCount() > 0;
|
|
22816
22908
|
if (!present) {
|
|
22817
22909
|
await database.write(() => job.destroyPermanently());
|
|
22818
22910
|
continue;
|
|
@@ -22980,7 +23072,7 @@ function createDurableAutoExtractor(options) {
|
|
|
22980
23072
|
const scope = currentScope();
|
|
22981
23073
|
writes = writes.then(
|
|
22982
23074
|
() => database.write(async () => {
|
|
22983
|
-
const existing = await jobs.query(
|
|
23075
|
+
const existing = await jobs.query(Q10.where("owner_key", owner), Q10.where("conversation_id", conversationId)).fetch();
|
|
22984
23076
|
const job = existing.find(
|
|
22985
23077
|
(candidate) => candidate._getRaw("scope") === scope && (candidate._getRaw("folder_id") ?? null) === folderId
|
|
22986
23078
|
);
|
|
@@ -22993,8 +23085,8 @@ function createDurableAutoExtractor(options) {
|
|
|
22993
23085
|
if (watermarks.length) {
|
|
22994
23086
|
const misplaced = new Set(
|
|
22995
23087
|
await storage.messagesCollection.query(
|
|
22996
|
-
|
|
22997
|
-
|
|
23088
|
+
Q10.where("id", Q10.oneOf(watermarks)),
|
|
23089
|
+
Q10.where("conversation_id", Q10.notEq(conversationId))
|
|
22998
23090
|
).fetchIds()
|
|
22999
23091
|
);
|
|
23000
23092
|
for (const prior of existing)
|
|
@@ -23004,7 +23096,7 @@ function createDurableAutoExtractor(options) {
|
|
|
23004
23096
|
r._setRaw("watermark_seq", null);
|
|
23005
23097
|
});
|
|
23006
23098
|
}
|
|
23007
|
-
const rows = fromSourceConversation ? [] : await storage.messagesCollection.query(
|
|
23099
|
+
const rows = fromSourceConversation ? [] : await storage.messagesCollection.query(Q10.where("id", Q10.oneOf(ids))).fetch();
|
|
23008
23100
|
const foreignRows = new Set(
|
|
23009
23101
|
rows.filter((row) => row._getRaw("conversation_id") !== conversationId).map((row) => row.id)
|
|
23010
23102
|
);
|
|
@@ -26047,7 +26139,7 @@ async function cleanupConversationSummary(database, conversationId) {
|
|
|
26047
26139
|
}
|
|
26048
26140
|
|
|
26049
26141
|
// src/lib/db/media/operations.ts
|
|
26050
|
-
import { Q as
|
|
26142
|
+
import { Q as Q11 } from "@nozbe/watermelondb";
|
|
26051
26143
|
|
|
26052
26144
|
// src/lib/storage/opfs.ts
|
|
26053
26145
|
function isOPFSSupported() {
|
|
@@ -26238,7 +26330,7 @@ async function createMediaBatchOp(ctx, optionsArray) {
|
|
|
26238
26330
|
})
|
|
26239
26331
|
);
|
|
26240
26332
|
});
|
|
26241
|
-
const results = await mediaCollection.query(
|
|
26333
|
+
const results = await mediaCollection.query(Q11.where("media_id", Q11.oneOf(mediaIds))).fetch();
|
|
26242
26334
|
return Promise.all(
|
|
26243
26335
|
results.map(
|
|
26244
26336
|
(m) => mediaToStored(m, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
|
|
@@ -26248,7 +26340,7 @@ async function createMediaBatchOp(ctx, optionsArray) {
|
|
|
26248
26340
|
var IMAGE_VIDEO_MIMES = VIDEO_EXTENSIONS.map((ext) => `image/${ext}`);
|
|
26249
26341
|
async function deleteMediaByConversationOp(ctx, conversationId) {
|
|
26250
26342
|
const mediaCollection = ctx.database.get("media");
|
|
26251
|
-
const results = await mediaCollection.query(
|
|
26343
|
+
const results = await mediaCollection.query(Q11.where("conversation_id", conversationId), Q11.where("is_deleted", false)).fetch();
|
|
26252
26344
|
if (results.length === 0) {
|
|
26253
26345
|
return 0;
|
|
26254
26346
|
}
|
|
@@ -26501,6 +26593,8 @@ function useChatStorage(options) {
|
|
|
26501
26593
|
embeddedWalletSigner
|
|
26502
26594
|
]
|
|
26503
26595
|
);
|
|
26596
|
+
const storageCtxRef = useRef4(storageCtx);
|
|
26597
|
+
storageCtxRef.current = storageCtx;
|
|
26504
26598
|
const vaultMemoryCollection = useMemo(
|
|
26505
26599
|
() => database.get("memory_vault"),
|
|
26506
26600
|
[database]
|
|
@@ -26604,24 +26698,12 @@ function useChatStorage(options) {
|
|
|
26604
26698
|
refreshQueueStatus();
|
|
26605
26699
|
return queueManager.onQueueChange(walletAddress, refreshQueueStatus);
|
|
26606
26700
|
}, [walletAddress, refreshQueueStatus]);
|
|
26607
|
-
useEffect4(() => {
|
|
26608
|
-
if (!walletAddress || !enableQueue || !autoFlushOnKeyAvailable || !signMessage) return;
|
|
26609
|
-
return onKeyAvailable(walletAddress, () => {
|
|
26610
|
-
flushQueue().catch((err) => {
|
|
26611
|
-
getLogger().warn("[useChatStorage] Auto-flush failed:", err);
|
|
26612
|
-
});
|
|
26613
|
-
});
|
|
26614
|
-
}, [walletAddress, enableQueue, autoFlushOnKeyAvailable, signMessage, flushQueue]);
|
|
26615
26701
|
useEffect4(() => {
|
|
26616
26702
|
if (!getWalletAddress || walletAddress) return;
|
|
26617
26703
|
const poller = new WalletPoller();
|
|
26618
26704
|
return poller.startPolling(getWalletAddress, () => {
|
|
26619
26705
|
});
|
|
26620
26706
|
}, [getWalletAddress, walletAddress]);
|
|
26621
|
-
const isEncryptionReady = useCallback4(() => {
|
|
26622
|
-
if (!walletAddress || !signMessage) return true;
|
|
26623
|
-
return hasEncryptionKey(walletAddress);
|
|
26624
|
-
}, [walletAddress, signMessage]);
|
|
26625
26707
|
const pendingOpsRef = useRef4([]);
|
|
26626
26708
|
const syntheticConvIdsRef = useRef4(/* @__PURE__ */ new Set());
|
|
26627
26709
|
useEffect4(() => {
|
|
@@ -26634,34 +26716,49 @@ function useChatStorage(options) {
|
|
|
26634
26716
|
pendingOpsRef.current = [];
|
|
26635
26717
|
refreshQueueStatus();
|
|
26636
26718
|
}, [walletAddress, enableQueue, refreshQueueStatus]);
|
|
26719
|
+
useEffect4(() => {
|
|
26720
|
+
if (!walletAddress || !enableQueue || !autoFlushOnKeyAvailable || !signMessage) return;
|
|
26721
|
+
return onKeyAvailable(walletAddress, () => {
|
|
26722
|
+
flushQueue().catch((err) => {
|
|
26723
|
+
getLogger().warn("[useChatStorage] Auto-flush failed:", err);
|
|
26724
|
+
});
|
|
26725
|
+
});
|
|
26726
|
+
}, [walletAddress, enableQueue, autoFlushOnKeyAvailable, signMessage, flushQueue]);
|
|
26637
26727
|
const writeOrQueue = useCallback4(
|
|
26638
26728
|
async (opType, payload, directWrite, makeSynthetic, dependencies = []) => {
|
|
26639
|
-
|
|
26640
|
-
|
|
26729
|
+
const currentStorageCtx = storageCtx.walletAddress ? storageCtx : storageCtxRef.current;
|
|
26730
|
+
const currentWalletAddress = currentStorageCtx.walletAddress;
|
|
26731
|
+
if (enableQueue && signMessage && !currentWalletAddress && getWalletAddress) {
|
|
26732
|
+
pendingOpsRef.current.push({ type: opType, payload, dependencies });
|
|
26733
|
+
return { result: makeSynthetic(), queued: true };
|
|
26734
|
+
}
|
|
26735
|
+
if (!currentWalletAddress || !currentStorageCtx.signMessage || hasEncryptionKey(currentWalletAddress)) {
|
|
26736
|
+
const result2 = await directWrite(currentStorageCtx);
|
|
26641
26737
|
return { result: result2, queued: false };
|
|
26642
26738
|
}
|
|
26643
26739
|
if (!enableQueue) {
|
|
26644
|
-
const result2 = await directWrite();
|
|
26740
|
+
const result2 = await directWrite(currentStorageCtx);
|
|
26645
26741
|
return { result: result2, queued: false };
|
|
26646
26742
|
}
|
|
26647
|
-
if (
|
|
26648
|
-
|
|
26649
|
-
|
|
26650
|
-
|
|
26651
|
-
|
|
26652
|
-
|
|
26743
|
+
if (currentWalletAddress) {
|
|
26744
|
+
const queueId = queueManager.queueOperation(
|
|
26745
|
+
currentWalletAddress,
|
|
26746
|
+
opType,
|
|
26747
|
+
payload,
|
|
26748
|
+
dependencies
|
|
26749
|
+
);
|
|
26653
26750
|
if (queueId === null) {
|
|
26654
26751
|
getLogger().warn("[useChatStorage] Queue full, falling back to direct write");
|
|
26655
|
-
const result2 = await directWrite();
|
|
26752
|
+
const result2 = await directWrite(currentStorageCtx);
|
|
26656
26753
|
return { result: result2, queued: false };
|
|
26657
26754
|
}
|
|
26658
|
-
|
|
26755
|
+
setQueueStatus(queueManager.getStatus(currentWalletAddress));
|
|
26659
26756
|
return { result: makeSynthetic(), queued: true, queueId };
|
|
26660
26757
|
}
|
|
26661
|
-
const result = await directWrite();
|
|
26758
|
+
const result = await directWrite(currentStorageCtx);
|
|
26662
26759
|
return { result, queued: false };
|
|
26663
26760
|
},
|
|
26664
|
-
[
|
|
26761
|
+
[storageCtx, enableQueue, signMessage, getWalletAddress]
|
|
26665
26762
|
);
|
|
26666
26763
|
const embedMessageAsync = useCallback4(
|
|
26667
26764
|
async (message, mask = maskForEmbedding) => {
|
|
@@ -26935,7 +27032,7 @@ function useChatStorage(options) {
|
|
|
26935
27032
|
const { result, queued } = await writeOrQueue(
|
|
26936
27033
|
"createConversation",
|
|
26937
27034
|
conversationOpts,
|
|
26938
|
-
() => createConversationOp(
|
|
27035
|
+
(ctx) => createConversationOp(ctx, conversationOpts, defaultConversationTitle),
|
|
26939
27036
|
() => makeSyntheticStoredConversation(conversationOpts, defaultConversationTitle)
|
|
26940
27037
|
);
|
|
26941
27038
|
if (queued) {
|
|
@@ -26944,7 +27041,7 @@ function useChatStorage(options) {
|
|
|
26944
27041
|
setCurrentConversationId(result.conversationId);
|
|
26945
27042
|
return result;
|
|
26946
27043
|
},
|
|
26947
|
-
[
|
|
27044
|
+
[defaultConversationTitle, writeOrQueue]
|
|
26948
27045
|
);
|
|
26949
27046
|
const getConversation = useCallback4(
|
|
26950
27047
|
async (id) => {
|
|
@@ -26960,24 +27057,24 @@ function useChatStorage(options) {
|
|
|
26960
27057
|
const { result } = await writeOrQueue(
|
|
26961
27058
|
"updateConversationTitle",
|
|
26962
27059
|
{ conversationId: id, title },
|
|
26963
|
-
() => updateConversationTitleOp(
|
|
27060
|
+
(ctx) => updateConversationTitleOp(ctx, id, title),
|
|
26964
27061
|
() => true
|
|
26965
27062
|
);
|
|
26966
27063
|
return result;
|
|
26967
27064
|
},
|
|
26968
|
-
[
|
|
27065
|
+
[writeOrQueue]
|
|
26969
27066
|
);
|
|
26970
27067
|
const updateConversationPinned = useCallback4(
|
|
26971
27068
|
async (id, pinned) => {
|
|
26972
27069
|
const { result } = await writeOrQueue(
|
|
26973
27070
|
"updateConversationPinned",
|
|
26974
27071
|
{ conversationId: id, pinned },
|
|
26975
|
-
() => updateConversationPinnedOp(
|
|
27072
|
+
(ctx) => updateConversationPinnedOp(ctx, id, pinned),
|
|
26976
27073
|
() => true
|
|
26977
27074
|
);
|
|
26978
27075
|
return result;
|
|
26979
27076
|
},
|
|
26980
|
-
[
|
|
27077
|
+
[writeOrQueue]
|
|
26981
27078
|
);
|
|
26982
27079
|
const deleteConversation = useCallback4(
|
|
26983
27080
|
async (id) => {
|
|
@@ -27195,7 +27292,7 @@ function useChatStorage(options) {
|
|
|
27195
27292
|
const { result } = await writeOrQueue(
|
|
27196
27293
|
"createMessage",
|
|
27197
27294
|
rowOpts,
|
|
27198
|
-
() => upsertMessageOp(
|
|
27295
|
+
(currentStorageCtx) => upsertMessageOp(currentStorageCtx, rowOpts),
|
|
27199
27296
|
() => makeSyntheticStoredMessage(rowOpts),
|
|
27200
27297
|
ctx.userMessageQueueId ? [ctx.userMessageQueueId] : []
|
|
27201
27298
|
);
|
|
@@ -27657,7 +27754,7 @@ function useChatStorage(options) {
|
|
|
27657
27754
|
const userMsgResult = await writeOrQueue(
|
|
27658
27755
|
"createMessage",
|
|
27659
27756
|
userMsgOpts,
|
|
27660
|
-
() => createMessageOp(
|
|
27757
|
+
(ctx) => createMessageOp(ctx, userMsgOpts),
|
|
27661
27758
|
() => makeSyntheticStoredMessage(userMsgOpts)
|
|
27662
27759
|
);
|
|
27663
27760
|
storedUserMessage = userMsgResult.result;
|
|
@@ -28006,7 +28103,7 @@ function useChatStorage(options) {
|
|
|
28006
28103
|
const assistantMsgResult = await writeOrQueue(
|
|
28007
28104
|
"createMessage",
|
|
28008
28105
|
assistantMsgOpts,
|
|
28009
|
-
() => createMessageOp(
|
|
28106
|
+
(ctx) => createMessageOp(ctx, assistantMsgOpts),
|
|
28010
28107
|
() => makeSyntheticStoredMessage(assistantMsgOpts),
|
|
28011
28108
|
userMsgQueueId ? [userMsgQueueId] : []
|
|
28012
28109
|
);
|
|
@@ -28038,7 +28135,7 @@ function useChatStorage(options) {
|
|
|
28038
28135
|
const toolResultsWrite = await writeOrQueue(
|
|
28039
28136
|
"createMessage",
|
|
28040
28137
|
toolResultsOpts,
|
|
28041
|
-
() => createMessageOp(
|
|
28138
|
+
(ctx) => createMessageOp(ctx, toolResultsOpts),
|
|
28042
28139
|
() => makeSyntheticStoredMessage(toolResultsOpts),
|
|
28043
28140
|
assistantMsgQueueId ? [assistantMsgQueueId] : []
|
|
28044
28141
|
);
|
|
@@ -28081,7 +28178,6 @@ function useChatStorage(options) {
|
|
|
28081
28178
|
walletAddress,
|
|
28082
28179
|
signMessage,
|
|
28083
28180
|
embeddedWalletSigner,
|
|
28084
|
-
isEncryptionReady,
|
|
28085
28181
|
enableQueue,
|
|
28086
28182
|
getWalletAddress,
|
|
28087
28183
|
refreshQueueStatus,
|