Searches all open conversations+branhces for the text#61
Conversation
Review: Search ImplementationThanks for adding search - this is a valuable feature! However, the current implementation has a scaling issue that needs to be addressed before merge. ProblemThe current approach loads all conversations and all messages into memory on every search: const conversations = await db.getUserConversations(req.userId);
for (const conversation of conversations) {
const messages = await db.getConversationMessages(conversation.id, req.userId);
// ...
}This won't scale with our conversation volume. Proposed Solution: Per-Conversation Search IndexesInstead of scanning content on every query, maintain a lightweight search index per conversation: StructureIndex format{
"v": 1,
"entries": [
{
"m": "messageId",
"b": "branchId",
"r": "assistant",
"t": "lowercased searchable text...",
"ts": 1706500000000
}
]
}Lifecycle
Search flowasync searchAllConversations(userId: string, query: string, limit: number) {
const conversations = await db.getUserConversations(userId);
const results = [];
const q = query.toLowerCase();
for (const conv of conversations) {
// Use in-memory index if loaded, otherwise read from disk
const index = this.loadedIndexes.get(conv.id)
?? await this.readIndexFromDisk(conv.id);
for (const entry of index.entries) {
if (entry.t.includes(q)) {
results.push({ conversationId: conv.id, ...entry });
if (results.length >= limit) break;
}
}
if (results.length >= limit) break;
}
return results.sort((a, b) => b.ts - a.ts).slice(0, limit);
}Why this approach?
AlsoMinor issues with the current PR (can be fixed alongside the above):
Let me know if you have questions about this approach! |
antra-tess
left a comment
There was a problem hiding this comment.
Needs indexed search approach for scaling - see detailed comment.
|
Added indexing, when searching search text will be compared to the index library, and only when selecting the conversation from the search bar will the full conversation be loaded. |
Created a Search feature. Will search through all the open conversations, and open correct branch in the conversation.