(
conversations: Conversation[],
query: string,
filters: SearchFilters = {}
)
| 63 | * Returns results sorted by relevance (highest score first). |
| 64 | */ |
| 65 | export function clientSearch( |
| 66 | conversations: Conversation[], |
| 67 | query: string, |
| 68 | filters: SearchFilters = {} |
| 69 | ): SearchResult[] { |
| 70 | const tokens = tokenize(query); |
| 71 | if (tokens.length === 0 && !hasActiveFilters(filters)) return []; |
| 72 | |
| 73 | const results: SearchResult[] = []; |
| 74 | const now = Date.now(); |
| 75 | |
| 76 | for (const conv of conversations) { |
| 77 | // --- Date filter --- |
| 78 | if (filters.dateFrom && conv.updatedAt < filters.dateFrom) continue; |
| 79 | if (filters.dateTo && conv.updatedAt > filters.dateTo + 86_400_000) continue; |
| 80 | |
| 81 | // --- Conversation filter --- |
| 82 | if (filters.conversationId && conv.id !== filters.conversationId) continue; |
| 83 | |
| 84 | // --- Model filter --- |
| 85 | if (filters.model && conv.model !== filters.model) continue; |
| 86 | |
| 87 | // --- Tag filter --- |
| 88 | if (filters.tagIds && filters.tagIds.length > 0) { |
| 89 | const convTags = new Set(conv.tags ?? []); |
| 90 | if (!filters.tagIds.some((tid) => convTags.has(tid))) continue; |
| 91 | } |
| 92 | |
| 93 | const matches: SearchResultMatch[] = []; |
| 94 | let titleScore = 0; |
| 95 | |
| 96 | // Score the conversation title |
| 97 | if (tokens.length > 0) { |
| 98 | titleScore = scoreText(conv.title, tokens) * 1.5; // title matches weight more |
| 99 | } |
| 100 | |
| 101 | for (const msg of conv.messages) { |
| 102 | // --- Role filter --- |
| 103 | if (filters.role && msg.role !== filters.role) continue; |
| 104 | |
| 105 | // --- Content type filter --- |
| 106 | if (filters.contentType) { |
| 107 | const hasType = matchesContentType(msg.content, filters.contentType); |
| 108 | if (!hasType) continue; |
| 109 | } |
| 110 | |
| 111 | const text = messageText(msg.content); |
| 112 | if (!text) continue; |
| 113 | |
| 114 | let msgScore = tokens.length > 0 ? scoreText(text, tokens) : 1; |
| 115 | if (msgScore === 0) continue; |
| 116 | |
| 117 | const ex = excerpt(text, query); |
| 118 | const hl = highlight(ex, query); |
| 119 | |
| 120 | matches.push({ |
| 121 | messageId: msg.id, |
| 122 | role: msg.role, |
nothing calls this directly
no test coverage detected