ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 1 | import { isRecord } from './guards'; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 2 | import { parseTimelineEvent } from './types'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 3 | import type { TimelineEvent } from './types'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 4 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 5 | export type SearchScope = { kind: 'room'; room_id: string } | { kind: 'rooms'; room_ids: string[] } | { kind: 'all' }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 6 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 7 | export interface ScopeOption { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 8 | label: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 9 | scope: SearchScope; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 10 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 11 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 12 | export interface SearchHit { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 13 | room_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 14 | event_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 15 | timestamp_ms: number; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 16 | event: TimelineEvent; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 17 | ranges: [number, number][]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 18 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 19 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 20 | export type SearchBackfill = 'Waiting' | 'Running' | 'Paused' | 'Complete' | 'Disabled' | { Failed: string }; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 21 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 22 | export interface RoomSearchCoverage { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 23 | room_id: string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 24 | oldest_indexed_ms: number | null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 25 | start_reached: boolean; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 26 | backfill: SearchBackfill; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 27 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 28 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 29 | export interface SearchCoverage { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 30 | rooms: RoomSearchCoverage[]; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 31 | persistent: boolean; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 32 | undecrypted: number; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 33 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 34 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 35 | export interface SearchPage { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 36 | hits: SearchHit[]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 37 | next: string | null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 38 | coverage: SearchCoverage; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 39 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 40 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 41 | export interface SearchSession { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 42 | search_id: string; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 43 | page: SearchPage; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 44 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 45 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 46 | /** The wire form of a scope, as macltok's externally tagged enum. */ |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 47 | export function scopeWire(scope: SearchScope): unknown { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 48 | if (scope.kind === 'room') return { Room: scope.room_id }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 49 | if (scope.kind === 'rooms') return { Rooms: scope.room_ids }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 50 | return 'All'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 51 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 52 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 53 | /** macltok's matches are utf-8 byte ranges, so they're mapped back to string offsets against the body. */ |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 54 | function parseRanges(raw: unknown, body: string): [number, number][] { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 55 | if (!Array.isArray(raw)) return []; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 56 | const bytes = new TextEncoder().encode(body); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 57 | const decoder = new TextDecoder(); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 58 | const offset = (byte: number) => decoder.decode(bytes.subarray(0, byte)).length; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 59 | return raw.flatMap((range) => |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 60 | isRecord(range) && typeof range['start'] === 'number' && typeof range['end'] === 'number' |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 61 | ? [[offset(range['start']), offset(range['end'])] as [number, number]] |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 62 | : [] |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 63 | ); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 64 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 65 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 66 | function hitBody(event: TimelineEvent): string { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 67 | const content = event.content; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 68 | if (content.kind === 'Message') return content.message.body; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 69 | return ''; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 70 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 71 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 72 | function parseHit(raw: unknown): SearchHit | null { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 73 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string' || typeof raw['event_id'] !== 'string' || !isRecord(raw['event'])) return null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 74 | const event = parseTimelineEvent(raw['event']); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 75 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 76 | room_id: raw['room_id'], |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 77 | event_id: raw['event_id'], |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 78 | timestamp_ms: typeof raw['timestamp_ms'] === 'number' ? raw['timestamp_ms'] : event.timestamp_ms, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 79 | event, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 80 | ranges: parseRanges(raw['matches'], hitBody(event)) |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 81 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 82 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 83 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 84 | function parseBackfill(raw: unknown): SearchBackfill { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 85 | if (raw === 'Waiting' || raw === 'Running' || raw === 'Paused' || raw === 'Complete' || raw === 'Disabled') return raw; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 86 | if (isRecord(raw) && isRecord(raw['Failed']) && typeof raw['Failed']['message'] === 'string') return { Failed: raw['Failed']['message'] }; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 87 | return 'Waiting'; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 88 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 89 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 90 | function parseRoomCoverage(raw: unknown): RoomSearchCoverage | null { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 91 | if (!isRecord(raw) || typeof raw['room_id'] !== 'string') return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 92 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 93 | room_id: raw['room_id'], |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 94 | oldest_indexed_ms: typeof raw['oldest_indexed_ms'] === 'number' ? raw['oldest_indexed_ms'] : null, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 95 | start_reached: raw['start_reached'] === true, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 96 | backfill: parseBackfill(raw['backfill']) |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 97 | }; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 98 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 99 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 100 | function parseCoverage(raw: unknown): SearchCoverage { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 101 | if (!isRecord(raw)) return { rooms: [], persistent: false, undecrypted: 0 }; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 102 | return { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 103 | rooms: Array.isArray(raw['rooms']) ? raw['rooms'].flatMap((room) => parseRoomCoverage(room) ?? []) : [], |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 104 | persistent: raw['persistent'] === true, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 105 | undecrypted: typeof raw['undecrypted'] === 'number' ? raw['undecrypted'] : 0 |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 106 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 107 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 108 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 109 | export function parseSearchPage(raw: unknown): SearchPage | null { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 110 | if (!isRecord(raw) || !Array.isArray(raw['hits'])) return null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 111 | return { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 112 | hits: raw['hits'].flatMap((hit) => parseHit(hit) ?? []), |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 113 | next: typeof raw['next_cursor'] === 'string' ? raw['next_cursor'] : null, |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 114 | coverage: parseCoverage(raw['coverage']) |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 115 | }; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 116 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 117 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 118 | export function parseSearchSession(raw: unknown): SearchSession | null { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 119 | if (!isRecord(raw) || typeof raw['search_id'] !== 'string') return null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 120 | const page = parseSearchPage(raw['page']); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 121 | return page ? { search_id: raw['search_id'], page } : null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 122 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 123 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 124 | /** Hits grouped by room, keeping the order in which each room first appears. */ |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 125 | export function groupByRoom(hits: SearchHit[]): { room_id: string; hits: SearchHit[] }[] { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 126 | const groups = new Map<string, SearchHit[]>(); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 127 | for (const hit of hits) { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 128 | const list = groups.get(hit.room_id); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 129 | if (list) list.push(hit); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 130 | else groups.set(hit.room_id, [hit]); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 131 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 132 | return [...groups].map(([room_id, list]) => ({ room_id, hits: list })); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 133 | } |