feat: ship Wave 5-6 multi-surface hygiene, optimization, and bug fixes
This commit is contained in:
parent
01ef3293cf
commit
66b6b45ba9
399 changed files with 17828 additions and 11330 deletions
|
|
@ -3,14 +3,24 @@ 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 { supabase } from '../../lib/supabase'
|
||||
import {
|
||||
extractSupabaseErrorInfo,
|
||||
getTypedSupabaseClient,
|
||||
isSupabaseAuthFailure,
|
||||
isSupabaseNetworkFailure,
|
||||
requireUuidWith,
|
||||
} from '../shared/service-guards'
|
||||
|
||||
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||
const DEFAULT_LIMIT = 30
|
||||
const MAX_LIMIT = 50
|
||||
const MAX_QUERY_LENGTH = 100
|
||||
const EXCERPT_LENGTH = 180
|
||||
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'
|
||||
|
|
@ -84,20 +94,17 @@ export interface MobileUnifiedSearchOptions {
|
|||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
function client(): D3roSupabaseClient {
|
||||
return supabase as unknown as D3roSupabaseClient
|
||||
}
|
||||
|
||||
function requireOptions(options: MobileUnifiedSearchOptions): void {
|
||||
if (typeof options !== 'object' || options === null || Array.isArray(options)) {
|
||||
if (!isRecord(options)) {
|
||||
throw new UnifiedSearchServiceError('invalid', 'Unified search options are required')
|
||||
}
|
||||
}
|
||||
|
||||
function requireUserId(userId: unknown): asserts userId is string {
|
||||
if (typeof userId !== 'string' || !UUID_PATTERN.test(userId)) {
|
||||
throw new UnifiedSearchServiceError('auth', 'A valid authenticated user is required')
|
||||
}
|
||||
requireUuidWith(
|
||||
userId,
|
||||
() => new UnifiedSearchServiceError('auth', 'A valid authenticated user is required'),
|
||||
)
|
||||
}
|
||||
|
||||
function requireQuery(query: unknown): asserts query is string {
|
||||
|
|
@ -123,32 +130,8 @@ export function sanitizeUnifiedSearchQuery(value: string): string {
|
|||
.trim()
|
||||
}
|
||||
|
||||
export function createPostgrestLikePattern(query: string): string {
|
||||
const escaped = query.replace(/[\\%_*]/g, (character) => `\\${character}`)
|
||||
return `*${escaped}*`
|
||||
}
|
||||
|
||||
function includesQuery(value: string | null | undefined, query: string): boolean {
|
||||
return value?.toLocaleLowerCase().includes(query.toLocaleLowerCase()) === true
|
||||
}
|
||||
|
||||
export function createMobileSearchExcerpt(
|
||||
value: string | null | undefined,
|
||||
query: string,
|
||||
maxLength = EXCERPT_LENGTH,
|
||||
): string {
|
||||
const text = (value ?? '').replace(/\s+/g, ' ').trim()
|
||||
if (text.length <= maxLength) return text
|
||||
|
||||
const matchAt = text.toLocaleLowerCase().indexOf(query.toLocaleLowerCase())
|
||||
if (matchAt < 0) return `${text.slice(0, maxLength - 1).trimEnd()}…`
|
||||
|
||||
const context = Math.max(20, Math.floor((maxLength - query.length) / 2))
|
||||
const start = Math.max(0, matchAt - context)
|
||||
const end = Math.min(text.length, start + maxLength)
|
||||
const excerpt = text.slice(start, end).trim()
|
||||
return `${start > 0 ? '…' : ''}${excerpt}${end < text.length ? '…' : ''}`
|
||||
}
|
||||
export const createPostgrestLikePattern = escapePostgrestLikePattern
|
||||
export const createMobileSearchExcerpt = createUnifiedSearchExcerpt
|
||||
|
||||
function occurredAt(value: string): number {
|
||||
const timestamp = Date.parse(value)
|
||||
|
|
@ -156,9 +139,9 @@ function occurredAt(value: string): number {
|
|||
}
|
||||
|
||||
function recordResult(row: UnifiedSearchHistoryRow, query: string): UnifiedSearchResult {
|
||||
const titleMatched = includesQuery(row.title, query)
|
||||
const titleMatched = includesSearchQuery(row.title, query)
|
||||
const matchingContent = [row.polished_text, row.original_text, row.summary_text]
|
||||
.find((value) => includesQuery(value, query))
|
||||
.find((value) => includesSearchQuery(value, query))
|
||||
const preview = matchingContent
|
||||
?? row.polished_text
|
||||
?? row.original_text
|
||||
|
|
@ -180,9 +163,9 @@ function meetingResult(
|
|||
query: string,
|
||||
segmentMatch?: UnifiedSearchTranscriptSegmentRow,
|
||||
): UnifiedSearchResult {
|
||||
const titleMatched = includesQuery(row.title, query)
|
||||
const titleMatched = includesSearchQuery(row.title, query)
|
||||
const matchingTranscript = [row.edited_transcript, row.raw_transcript]
|
||||
.find((value) => includesQuery(value, query))
|
||||
.find((value) => includesSearchQuery(value, query))
|
||||
const segmentSnippet = segmentMatch ? formatSegmentSnippet(segmentMatch) : null
|
||||
const preview = matchingTranscript
|
||||
?? segmentSnippet
|
||||
|
|
@ -248,15 +231,7 @@ function boundPayloadText(
|
|||
return createMobileSearchExcerpt(value, query, MAX_BOUNDED_TEXT_LENGTH)
|
||||
}
|
||||
|
||||
export function mergeMobileUnifiedSearchResults(
|
||||
records: readonly UnifiedSearchResult[],
|
||||
meetings: readonly UnifiedSearchResult[],
|
||||
limit: number,
|
||||
): UnifiedSearchResult[] {
|
||||
return [...records, ...meetings]
|
||||
.sort((left, right) => right.occurredAt - left.occurredAt || left.id.localeCompare(right.id))
|
||||
.slice(0, limit)
|
||||
}
|
||||
export const mergeMobileUnifiedSearchResults = mergeUnifiedSearchResults
|
||||
|
||||
function toSearchError(error: unknown, signal?: AbortSignal): UnifiedSearchServiceError {
|
||||
if (signal?.aborted || isUnifiedSearchAbortedError(error)) {
|
||||
|
|
@ -264,27 +239,17 @@ function toSearchError(error: unknown, signal?: AbortSignal): UnifiedSearchServi
|
|||
}
|
||||
if (error instanceof UnifiedSearchServiceError) return error
|
||||
|
||||
const candidate = error as { code?: unknown; message?: unknown; name?: unknown }
|
||||
if (candidate?.name === 'AbortError') {
|
||||
if (isRecord(error) && error.name === 'AbortError') {
|
||||
return new UnifiedSearchServiceError('aborted', 'Unified search request was aborted')
|
||||
}
|
||||
const code = typeof candidate?.code === 'string' ? candidate.code : ''
|
||||
const message = typeof candidate?.message === 'string'
|
||||
? candidate.message
|
||||
: 'Unified search request failed'
|
||||
const lower = message.toLocaleLowerCase()
|
||||
if (
|
||||
error instanceof TypeError
|
||||
|| lower.includes('network request failed')
|
||||
|| lower.includes('failed to fetch')
|
||||
|| lower.includes('networkerror')
|
||||
) {
|
||||
return new UnifiedSearchServiceError('network', message)
|
||||
const info = extractSupabaseErrorInfo(error, 'Unified search request failed')
|
||||
if (isSupabaseNetworkFailure(error, info)) {
|
||||
return new UnifiedSearchServiceError('network', info.message)
|
||||
}
|
||||
if (code === 'PGRST301' || code === '42501' || lower.includes('jwt')) {
|
||||
return new UnifiedSearchServiceError('auth', message)
|
||||
if (isSupabaseAuthFailure(info, { include42501: true })) {
|
||||
return new UnifiedSearchServiceError('auth', info.message)
|
||||
}
|
||||
return new UnifiedSearchServiceError('server', message)
|
||||
return new UnifiedSearchServiceError('server', info.message)
|
||||
}
|
||||
|
||||
export function createSupabaseUnifiedSearchSource(
|
||||
|
|
@ -379,7 +344,7 @@ export function createSupabaseUnifiedSearchSource(
|
|||
}
|
||||
}
|
||||
|
||||
const supabaseSearchSource = createSupabaseUnifiedSearchSource(client())
|
||||
const supabaseSearchSource = createSupabaseUnifiedSearchSource(getTypedSupabaseClient())
|
||||
|
||||
export async function searchUnifiedContent(
|
||||
options: MobileUnifiedSearchOptions,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue