| 28 | WHERE mc.segment_id = segment.id ORDER BY mc.message_id LIMIT 1) as firstMessageId` |
| 29 | |
| 30 | export class FetchSessionIndexAdapter implements SessionIndexAdapter { |
| 31 | async generate(sessionId: string, gapThreshold: number = 1800): Promise<number> { |
| 32 | const resp = await fetchWithAuth(`/_web/sessions/${sessionId}/generate-index`, { |
| 33 | method: 'POST', |
| 34 | headers: { 'Content-Type': 'application/json' }, |
| 35 | body: JSON.stringify({ gapThreshold }), |
| 36 | }) |
| 37 | if (!resp.ok) throw new Error(`Failed to generate session index: ${resp.status}`) |
| 38 | const result = (await resp.json()) as { sessionCount: number } |
| 39 | return result.sessionCount |
| 40 | } |
| 41 | |
| 42 | async generateIncremental(sessionId: string, gapThreshold: number = 1800): Promise<number> { |
| 43 | const resp = await fetchWithAuth(`/_web/sessions/${sessionId}/generate-incremental-index`, { |
| 44 | method: 'POST', |
| 45 | headers: { 'Content-Type': 'application/json' }, |
| 46 | body: JSON.stringify({ gapThreshold }), |
| 47 | }) |
| 48 | if (!resp.ok) throw new Error(`Failed to generate incremental session index: ${resp.status}`) |
| 49 | const result = (await resp.json()) as { sessionCount: number } |
| 50 | return result.sessionCount |
| 51 | } |
| 52 | |
| 53 | async hasIndex(sessionId: string): Promise<boolean> { |
| 54 | const stats = await this.getStats(sessionId) |
| 55 | return stats.hasIndex |
| 56 | } |
| 57 | |
| 58 | async getStats(sessionId: string): Promise<SessionStats> { |
| 59 | try { |
| 60 | const rows = await getDataAdapter().pluginQuery<{ cnt: number }>( |
| 61 | sessionId, |
| 62 | 'SELECT COUNT(*) as cnt FROM segment', |
| 63 | [] |
| 64 | ) |
| 65 | const count = rows[0]?.cnt ?? 0 |
| 66 | return { hasIndex: count > 0, sessionCount: count, gapThreshold: 1800 } |
| 67 | } catch { |
| 68 | return { hasIndex: false, sessionCount: 0, gapThreshold: 1800 } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | async getAllIndexStats(): Promise<SessionIndexStatusItem[]> { |
| 73 | const resp = await fetchWithAuth('/_web/sessions/index-stats') |
| 74 | if (!resp.ok) return [] |
| 75 | return (await resp.json()) as SessionIndexStatusItem[] |
| 76 | } |
| 77 | |
| 78 | async clear(sessionId: string): Promise<boolean> { |
| 79 | await fetchWithAuth(`/_web/sessions/${sessionId}/clear-index`, { method: 'POST' }) |
| 80 | return true |
| 81 | } |
| 82 | |
| 83 | async updateGapThreshold(_sessionId: string, _gapThreshold: number | null): Promise<boolean> { |
| 84 | return true |
| 85 | } |
| 86 | |
| 87 | async getSessions(sessionId: string): Promise<ChatSessionItem[]> { |
nothing calls this directly
no outgoing calls
no test coverage detected