scope session search by selected profile (#889)

This commit is contained in:
ekko
2026-05-21 09:48:31 +08:00
committed by GitHub
parent 3612a76735
commit 529065f023
6 changed files with 56 additions and 14 deletions
+14 -4
View File
@@ -260,11 +260,12 @@ export function listSessions(profile?: string, source?: string, limit = 2000): H
return rows.map(mapSessionRow)
}
export function searchSessions(profile: string, query: string, limit = 20): HermesSessionSearchRow[] {
export function searchSessions(profile: string | null | undefined, query: string, limit = 20): HermesSessionSearchRow[] {
if (!isSqliteAvailable()) return []
const profileFilter = profile?.trim()
const trimmed = query.trim()
if (!trimmed) {
return listSessions(profile, undefined, limit).map(s => ({ ...s, snippet: s.preview || '', matched_message_id: null }))
return listSessions(profileFilter, undefined, limit).map(s => ({ ...s, snippet: s.preview || '', matched_message_id: null }))
}
const db = getDb()!
const lowered = trimmed.toLowerCase()
@@ -273,12 +274,21 @@ export function searchSessions(profile: string, query: string, limit = 20): Herm
// Step 1: Find matching sessions
const sessionRows = db.prepare(
`SELECT * FROM ${SESSIONS_TABLE}
WHERE profile = ? AND (
WHERE 1 = 1
${profileFilter ? 'AND profile = ?' : ''}
AND (
LOWER(title) LIKE ? OR LOWER(preview) LIKE ?
OR id IN (SELECT DISTINCT session_id FROM ${MESSAGES_TABLE} WHERE LOWER(content) LIKE ? OR LOWER(COALESCE(tool_name, '')) LIKE ?)
)
ORDER BY last_active DESC LIMIT ?`,
).all(profile, pattern, pattern, pattern, pattern, limit) as Record<string, unknown>[]
).all(...[
...(profileFilter ? [profileFilter] : []),
pattern,
pattern,
pattern,
pattern,
limit,
]) as Record<string, unknown>[]
if (sessionRows.length === 0) return []