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
@@ -168,9 +168,16 @@ export async function listHermesSessions(ctx: any) {
export async function search(ctx: any) {
const q = typeof ctx.query.q === 'string' ? ctx.query.q : ''
const limit = ctx.query.limit ? parseInt(ctx.query.limit as string, 10) : undefined
const profile = getActiveProfileName()
const profile = typeof ctx.query.profile === 'string' && ctx.query.profile.trim()
? ctx.query.profile.trim()
: undefined
const results = localSearchSessions(profile, q, limit && limit > 0 ? limit : 20)
ctx.body = { results: filterPendingDeletedSessions(results) }
const knownProfiles = profile ? null : new Set(listProfileNamesFromDisk())
ctx.body = {
results: filterPendingDeletedSessions(results.filter(s =>
!knownProfiles || knownProfiles.has(s.profile || 'default'),
)),
}
}
export async function get(ctx: any) {
+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 []