import type { D3roSupabaseClient, HistoryEntry, Meeting, Transcript } from '@d3ro/api-client' import type { UnifiedSearchResponse, UnifiedSearchResult, } from '@d3ro/core/types' import { escapePostgrestLikePattern, isRecord } from '@d3ro/core/guards' import { createUnifiedSearchExcerpt, includesSearchQuery, mergeUnifiedSearchResults, } from '@d3ro/core/unified-search' import { formatTranscriptTimestamp } from '@d3ro/core/meeting-transcript' import { extractSupabaseErrorInfo, getTypedSupabaseClient, isSupabaseAuthFailure, isSupabaseNetworkFailure, requireUuidWith, } from '../shared/service-guards' const DEFAULT_LIMIT = 30 const MAX_LIMIT = 50 const MAX_QUERY_LENGTH = 100 const MAX_BOUNDED_TEXT_LENGTH = 600 const HISTORY_SEARCH_COLUMNS = 'id,title,original_text,polished_text,summary_text,created_at' const MEETING_SEARCH_COLUMNS = 'id,title,raw_transcript,edited_transcript,started_at' const TRANSCRIPT_SEGMENT_SEARCH_COLUMNS = 'id,meeting_id,text,speaker,timestamp_ms,created_at' const MEETING_META_COLUMNS = 'id,title,started_at' export type UnifiedSearchServiceErrorCode = 'auth' | 'invalid' | 'network' | 'server' | 'aborted' export class UnifiedSearchServiceError extends Error { constructor( public readonly code: UnifiedSearchServiceErrorCode, message: string, ) { super(message) this.name = 'UnifiedSearchServiceError' } } export interface UnifiedSearchSourcePage { rows: T[] total: number } type UnifiedSearchHistoryRow = Pick< HistoryEntry, 'id' | 'title' | 'original_text' | 'polished_text' | 'summary_text' | 'created_at' > type UnifiedSearchMeetingRow = Pick< Meeting, 'id' | 'title' | 'raw_transcript' | 'edited_transcript' | 'started_at' > export type UnifiedSearchTranscriptSegmentRow = Pick< Transcript, 'id' | 'meeting_id' | 'text' | 'speaker' | 'timestamp_ms' | 'created_at' > export type UnifiedSearchMeetingMetaRow = Pick< Meeting, 'id' | 'title' | 'started_at' > export interface UnifiedSearchSourcePort { searchRecords: ( userId: string, query: string, limit: number, signal?: AbortSignal, ) => Promise> searchMeetings: ( query: string, limit: number, signal?: AbortSignal, ) => Promise> searchTranscriptSegments?: ( query: string, limit: number, signal?: AbortSignal, ) => Promise> loadMeetingsByIds?: ( meetingIds: readonly string[], signal?: AbortSignal, ) => Promise } export interface MobileUnifiedSearchOptions { userId: string query: string limit?: number signal?: AbortSignal } function requireOptions(options: MobileUnifiedSearchOptions): void { if (!isRecord(options)) { throw new UnifiedSearchServiceError('invalid', 'Unified search options are required') } } function requireUserId(userId: unknown): asserts userId is string { requireUuidWith( userId, () => new UnifiedSearchServiceError('auth', 'A valid authenticated user is required'), ) } function requireQuery(query: unknown): asserts query is string { if (typeof query !== 'string') { throw new UnifiedSearchServiceError('invalid', 'Unified search query must be a string') } } function normalizeLimit(limit: unknown): number { if (limit === undefined) return DEFAULT_LIMIT if (typeof limit !== 'number' || !Number.isFinite(limit)) { throw new UnifiedSearchServiceError('invalid', 'Unified search limit must be a finite number') } return Math.max(1, Math.min(Math.floor(limit), MAX_LIMIT)) } export function sanitizeUnifiedSearchQuery(value: string): string { return value .slice(0, MAX_QUERY_LENGTH) .trim() .replace(/[\u0000-\u001f\u007f,()"]/g, ' ') .replace(/\s+/g, ' ') .trim() } export const createPostgrestLikePattern = escapePostgrestLikePattern export const createMobileSearchExcerpt = createUnifiedSearchExcerpt function occurredAt(value: string): number { const timestamp = Date.parse(value) return Number.isFinite(timestamp) ? timestamp : 0 } function recordResult(row: UnifiedSearchHistoryRow, query: string): UnifiedSearchResult { const titleMatched = includesSearchQuery(row.title, query) const matchingContent = [row.polished_text, row.original_text, row.summary_text] .find((value) => includesSearchQuery(value, query)) const preview = matchingContent ?? row.polished_text ?? row.original_text ?? row.summary_text ?? row.title return { source: 'record', id: row.id, title: row.title, excerpt: createMobileSearchExcerpt(preview, query), matchedField: titleMatched ? 'title' : 'content', occurredAt: occurredAt(row.created_at), } } function meetingResult( row: UnifiedSearchMeetingRow, query: string, segmentMatch?: UnifiedSearchTranscriptSegmentRow, ): UnifiedSearchResult { const titleMatched = includesSearchQuery(row.title, query) const matchingTranscript = [row.edited_transcript, row.raw_transcript] .find((value) => includesSearchQuery(value, query)) const segmentSnippet = segmentMatch ? formatSegmentSnippet(segmentMatch) : null const preview = matchingTranscript ?? segmentSnippet ?? row.edited_transcript ?? row.raw_transcript ?? row.title return { source: 'meeting', id: row.id, title: row.title, excerpt: createMobileSearchExcerpt(preview, query), matchedField: titleMatched && !matchingTranscript && !segmentSnippet ? 'title' : 'transcript', occurredAt: occurredAt(row.started_at), } } function formatSegmentSnippet(segment: UnifiedSearchTranscriptSegmentRow): string { const timestamp = formatTranscriptTimestamp(segment.timestamp_ms) const speakerPrefix = segment.speaker?.trim() ? `[${segment.speaker.trim()}] ` : '' return `[${timestamp}] ${speakerPrefix}${segment.text}` } function segmentMeetingResult( segment: UnifiedSearchTranscriptSegmentRow, meetingMeta: UnifiedSearchMeetingMetaRow | undefined, query: string, ): UnifiedSearchResult { return { source: 'meeting', id: segment.meeting_id, title: meetingMeta?.title ?? null, excerpt: createMobileSearchExcerpt(formatSegmentSnippet(segment), query), matchedField: 'transcript', occurredAt: occurredAt(meetingMeta?.started_at ?? segment.created_at), } } export function isUnifiedSearchAbortedError(error: unknown): boolean { if (error instanceof UnifiedSearchServiceError && error.code === 'aborted') return true return error instanceof Error && error.name === 'AbortError' } function throwIfAborted(signal?: AbortSignal): void { if (signal?.aborted) { throw new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') } } function withAbortSignal(builder: T, signal?: AbortSignal): T { if (signal && typeof (builder as { abortSignal?: unknown }).abortSignal === 'function') { return (builder as unknown as { abortSignal: (s: AbortSignal) => T }).abortSignal(signal) } return builder } function boundPayloadText( value: string | null, query: string, ): string | null { if (value === null) return null if (value.length <= MAX_BOUNDED_TEXT_LENGTH) return value return createMobileSearchExcerpt(value, query, MAX_BOUNDED_TEXT_LENGTH) } export const mergeMobileUnifiedSearchResults = mergeUnifiedSearchResults function toSearchError(error: unknown, signal?: AbortSignal): UnifiedSearchServiceError { if (signal?.aborted || isUnifiedSearchAbortedError(error)) { return new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') } if (error instanceof UnifiedSearchServiceError) return error if (isRecord(error) && error.name === 'AbortError') { return new UnifiedSearchServiceError('aborted', 'Unified search request was aborted') } const info = extractSupabaseErrorInfo(error, 'Unified search request failed') if (isSupabaseNetworkFailure(error, info)) { return new UnifiedSearchServiceError('network', info.message) } if (isSupabaseAuthFailure(info, { include42501: true })) { return new UnifiedSearchServiceError('auth', info.message) } return new UnifiedSearchServiceError('server', info.message) } export function createSupabaseUnifiedSearchSource( searchClient: D3roSupabaseClient, ): UnifiedSearchSourcePort { return { async searchRecords(userId, query, limit, signal) { throwIfAborted(signal) const pattern = createPostgrestLikePattern(query) const queryBuilder = withAbortSignal( searchClient .from('history') .select(HISTORY_SEARCH_COLUMNS, { count: 'exact' }) .eq('user_id', userId) .or( `title.ilike.${pattern},original_text.ilike.${pattern},polished_text.ilike.${pattern},summary_text.ilike.${pattern}`, ) .order('created_at', { ascending: false }) .order('id', { ascending: false }), signal, ) const { data, error, count } = await queryBuilder.limit(limit) if (error !== null) throw error const rows = (data ?? []).map((row) => ({ ...row, original_text: boundPayloadText(row.original_text, query) ?? '', polished_text: boundPayloadText(row.polished_text, query), summary_text: boundPayloadText(row.summary_text, query), })) return { rows, total: count ?? rows.length } }, async searchMeetings(query, limit, signal) { throwIfAborted(signal) const pattern = createPostgrestLikePattern(query) const queryBuilder = withAbortSignal( searchClient .from('meetings') .select(MEETING_SEARCH_COLUMNS, { count: 'exact' }) .or( `title.ilike.${pattern},raw_transcript.ilike.${pattern},edited_transcript.ilike.${pattern}`, ) .order('started_at', { ascending: false }) .order('id', { ascending: false }), signal, ) const { data, error, count } = await queryBuilder.limit(limit) if (error !== null) throw error const rows = (data ?? []).map((row) => ({ ...row, raw_transcript: boundPayloadText(row.raw_transcript, query), edited_transcript: boundPayloadText(row.edited_transcript, query), })) return { rows, total: count ?? rows.length } }, async searchTranscriptSegments(query, limit, signal) { throwIfAborted(signal) const pattern = createPostgrestLikePattern(query) const queryBuilder = withAbortSignal( searchClient .from('transcripts') .select(TRANSCRIPT_SEGMENT_SEARCH_COLUMNS, { count: 'exact' }) .or(`text.ilike.${pattern},speaker.ilike.${pattern}`) .order('timestamp_ms', { ascending: true }) .order('id', { ascending: false }), signal, ) const { data, error, count } = await queryBuilder.limit(limit) if (error !== null) throw error const rows = (data ?? []).map((row) => ({ ...row, text: boundPayloadText(row.text, query) ?? '', })) return { rows, total: count ?? rows.length } }, async loadMeetingsByIds(meetingIds, signal) { if (meetingIds.length === 0) return [] throwIfAborted(signal) const queryBuilder = withAbortSignal( searchClient .from('meetings') .select(MEETING_META_COLUMNS) .in('id', [...meetingIds]), signal, ) const { data, error } = await queryBuilder.limit(meetingIds.length) if (error !== null) throw error return data ?? [] }, } } const supabaseSearchSource = createSupabaseUnifiedSearchSource(getTypedSupabaseClient()) export async function searchUnifiedContent( options: MobileUnifiedSearchOptions, source: UnifiedSearchSourcePort = supabaseSearchSource, ): Promise { requireOptions(options) requireUserId(options.userId) requireQuery(options.query) const query = sanitizeUnifiedSearchQuery(options.query) const limit = normalizeLimit(options.limit) throwIfAborted(options.signal) if (query.length === 0) return { query, results: [], total: 0 } try { const [recordPage, meetingPage, segmentPage] = await Promise.all([ options.signal ? source.searchRecords(options.userId, query, limit, options.signal) : source.searchRecords(options.userId, query, limit), options.signal ? source.searchMeetings(query, limit, options.signal) : source.searchMeetings(query, limit), source.searchTranscriptSegments ? (options.signal ? source.searchTranscriptSegments(query, limit, options.signal) : source.searchTranscriptSegments(query, limit)) : Promise.resolve({ rows: [], total: 0 }), ]) throwIfAborted(options.signal) const segmentByMeetingId = new Map() for (const segment of segmentPage.rows) { if (!segmentByMeetingId.has(segment.meeting_id)) { segmentByMeetingId.set(segment.meeting_id, segment) } } const directMeetingIds = new Set(meetingPage.rows.map((row) => row.id)) const missingMeetingIds = [...segmentByMeetingId.keys()].filter( (meetingId) => !directMeetingIds.has(meetingId), ) const extraMeetingMetaRows = missingMeetingIds.length > 0 && source.loadMeetingsByIds ? await (options.signal ? source.loadMeetingsByIds(missingMeetingIds, options.signal) : source.loadMeetingsByIds(missingMeetingIds)) : [] throwIfAborted(options.signal) const extraMeetingMetaById = new Map( extraMeetingMetaRows.map((row) => [row.id, row]), ) const directMeetingResults = meetingPage.rows.map((row) => ( meetingResult(row, query, segmentByMeetingId.get(row.id)) )) const segmentOnlyMeetingResults = missingMeetingIds .map((meetingId) => { const segment = segmentByMeetingId.get(meetingId) if (!segment) return null return segmentMeetingResult(segment, extraMeetingMetaById.get(meetingId), query) }) .filter((item): item is UnifiedSearchResult => item !== null) const results = mergeMobileUnifiedSearchResults( recordPage.rows.map((row) => recordResult(row, query)), [...directMeetingResults, ...segmentOnlyMeetingResults], limit, ) return { query, results, total: recordPage.total + meetingPage.total + segmentOnlyMeetingResults.length, } } catch (error) { throw toSearchError(error, options.signal) } }