feat(commands): multi-select ordered command pipeline with step reordering and single-pass composition (AI-05, AI-07)
This commit is contained in:
parent
f49acf0105
commit
10af20a0ad
157 changed files with 29455 additions and 913 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import type { RealtimeChannel } from '@supabase/supabase-js'
|
||||
import { parseTranscriptLines } from '@d3ro/core/meeting-transcript'
|
||||
import type {
|
||||
D3roSupabaseClient,
|
||||
Meeting,
|
||||
|
|
@ -65,6 +66,16 @@ export interface MeetingTranscriptView {
|
|||
text: string
|
||||
}
|
||||
|
||||
export interface MeetingSeekableSegment {
|
||||
id: string
|
||||
timestampMs: number
|
||||
durationMs: number | null
|
||||
speaker: string | null
|
||||
text: string
|
||||
edited: boolean
|
||||
timed: boolean
|
||||
}
|
||||
|
||||
export type MeetingServiceErrorCode =
|
||||
| 'auth'
|
||||
| 'conflict'
|
||||
|
|
@ -256,6 +267,103 @@ export function buildMeetingTranscriptView(
|
|||
return { source: 'empty', text: '' }
|
||||
}
|
||||
|
||||
const TIMESTAMP_TOKEN_PATTERN = /^\[?(?:(\d{1,2}):)?(\d{1,3}):(\d{2})\]?$/
|
||||
|
||||
export function parseTimestampTokenToMs(token: string): number | null {
|
||||
const match = TIMESTAMP_TOKEN_PATTERN.exec(token.trim())
|
||||
if (!match) return null
|
||||
const hours = match[1] !== undefined ? Number(match[1]) : 0
|
||||
const minutes = Number(match[2])
|
||||
const seconds = Number(match[3])
|
||||
if (
|
||||
!Number.isFinite(hours)
|
||||
|| !Number.isFinite(minutes)
|
||||
|| !Number.isFinite(seconds)
|
||||
|| seconds >= 60
|
||||
|| (match[1] !== undefined && minutes >= 60)
|
||||
) {
|
||||
return null
|
||||
}
|
||||
return ((hours * 3_600) + (minutes * 60) + seconds) * 1_000
|
||||
}
|
||||
|
||||
export function parseTranscriptSeekSegments(
|
||||
meeting: Pick<Meeting, 'edited_transcript' | 'raw_transcript'>,
|
||||
transcripts: readonly Transcript[],
|
||||
): MeetingSeekableSegment[] {
|
||||
if (transcripts.length > 0) {
|
||||
return transcripts
|
||||
.slice()
|
||||
.sort((left, right) => left.segment_index - right.segment_index)
|
||||
.map((segment) => ({
|
||||
id: segment.id,
|
||||
timestampMs: Math.max(0, segment.timestamp_ms),
|
||||
durationMs: segment.duration_ms,
|
||||
speaker: segment.speaker,
|
||||
text: segment.text,
|
||||
edited: segment.edited,
|
||||
timed: true,
|
||||
}))
|
||||
}
|
||||
|
||||
const rawText = meeting.edited_transcript?.trim()
|
||||
? meeting.edited_transcript
|
||||
: meeting.raw_transcript?.trim()
|
||||
? meeting.raw_transcript
|
||||
: ''
|
||||
if (!rawText) return []
|
||||
|
||||
return parseTranscriptLines(rawText).map((line, index) => ({
|
||||
id: `line-${index}`,
|
||||
timestampMs: Math.max(0, line.timestampMs),
|
||||
durationMs: null,
|
||||
speaker: line.speaker,
|
||||
text: line.text,
|
||||
edited: false,
|
||||
timed: line.timed,
|
||||
}))
|
||||
}
|
||||
|
||||
export function findActiveTranscriptSegmentId(
|
||||
segments: readonly Pick<MeetingSeekableSegment, 'id' | 'timestampMs' | 'durationMs' | 'timed'>[],
|
||||
currentPositionMs: number | null | undefined,
|
||||
): string | null {
|
||||
if (
|
||||
currentPositionMs === null
|
||||
|| currentPositionMs === undefined
|
||||
|| !Number.isFinite(currentPositionMs)
|
||||
|| currentPositionMs < 0
|
||||
|| segments.length === 0
|
||||
) {
|
||||
return null
|
||||
}
|
||||
|
||||
const timedSegments = segments
|
||||
.filter((segment) => segment.timed)
|
||||
.slice()
|
||||
.sort((left, right) => left.timestampMs - right.timestampMs)
|
||||
|
||||
if (timedSegments.length === 0) return null
|
||||
|
||||
let activeId: string | null = null
|
||||
for (let index = 0; index < timedSegments.length; index += 1) {
|
||||
const current = timedSegments[index]
|
||||
const next = timedSegments[index + 1]
|
||||
if (currentPositionMs < current.timestampMs) {
|
||||
break
|
||||
}
|
||||
const endBound = next !== undefined
|
||||
? next.timestampMs
|
||||
: current.durationMs !== null && current.durationMs > 0
|
||||
? current.timestampMs + current.durationMs
|
||||
: Number.POSITIVE_INFINITY
|
||||
if (currentPositionMs <= endBound) {
|
||||
activeId = current.id
|
||||
}
|
||||
}
|
||||
return activeId
|
||||
}
|
||||
|
||||
function assertMeetingRow(row: Meeting | null, expectedId?: string): Meeting {
|
||||
if (row === null) {
|
||||
throw new MeetingServiceError('not-found', 'Meeting was not found')
|
||||
|
|
|
|||
|
|
@ -17,9 +17,11 @@ export type NotificationConfigurationReason =
|
|||
| 'native-module-unavailable'
|
||||
| 'unsupported-platform'
|
||||
|
||||
export type MobilePushProvider = 'fcm' | 'apns'
|
||||
|
||||
export interface NativeNotificationStatus {
|
||||
configured: boolean
|
||||
provider: 'fcm'
|
||||
provider: MobilePushProvider
|
||||
reason: NotificationConfigurationReason
|
||||
permission: NotificationPermissionStatus
|
||||
registrationId: string | null
|
||||
|
|
@ -49,12 +51,16 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
export function resolveNativePushProvider(platform: string = Platform.OS): MobilePushProvider {
|
||||
return platform === 'ios' ? 'apns' : 'fcm'
|
||||
}
|
||||
|
||||
function normalizeStatus(value: unknown): NativeNotificationStatus {
|
||||
if (!isRecord(value)) throw new Error('notification_native_status_invalid')
|
||||
const registrationId = value.registrationId ?? null
|
||||
if (
|
||||
typeof value.configured !== 'boolean'
|
||||
|| value.provider !== 'fcm'
|
||||
|| (value.provider !== 'fcm' && value.provider !== 'apns')
|
||||
|| ![
|
||||
'configured',
|
||||
'firebase-config-missing',
|
||||
|
|
@ -70,7 +76,7 @@ function normalizeStatus(value: unknown): NativeNotificationStatus {
|
|||
}
|
||||
return {
|
||||
configured: value.configured,
|
||||
provider: 'fcm',
|
||||
provider: value.provider as MobilePushProvider,
|
||||
reason: value.reason as NotificationConfigurationReason,
|
||||
permission: value.permission as NotificationPermissionStatus,
|
||||
registrationId,
|
||||
|
|
@ -79,10 +85,11 @@ function normalizeStatus(value: unknown): NativeNotificationStatus {
|
|||
}
|
||||
|
||||
export async function getNativeNotificationStatus(): Promise<NativeNotificationStatus> {
|
||||
if (Platform.OS !== 'android') {
|
||||
const provider = resolveNativePushProvider(Platform.OS)
|
||||
if (Platform.OS !== 'android' && Platform.OS !== 'ios') {
|
||||
return {
|
||||
configured: false,
|
||||
provider: 'fcm',
|
||||
provider,
|
||||
reason: 'unsupported-platform',
|
||||
permission: 'unavailable',
|
||||
registrationId: null,
|
||||
|
|
@ -92,7 +99,7 @@ export async function getNativeNotificationStatus(): Promise<NativeNotificationS
|
|||
if (nativeModule === undefined) {
|
||||
return {
|
||||
configured: false,
|
||||
provider: 'fcm',
|
||||
provider,
|
||||
reason: 'native-module-unavailable',
|
||||
permission: 'unavailable',
|
||||
registrationId: null,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import {
|
|||
deleteNativePushRegistration,
|
||||
getNativeNotificationStatus,
|
||||
getNativePushRegistrationId,
|
||||
type MobilePushProvider,
|
||||
type NativeNotificationStatus,
|
||||
} from './notification-native'
|
||||
|
||||
|
|
@ -47,6 +48,12 @@ function toServiceError(error: unknown): NotificationServiceError {
|
|||
return new NotificationServiceError('server')
|
||||
}
|
||||
|
||||
export function resolveDevicePushProvider(
|
||||
device: Pick<RegisteredDevice, 'platform'>,
|
||||
): MobilePushProvider {
|
||||
return device.platform === 'ios' ? 'apns' : 'fcm'
|
||||
}
|
||||
|
||||
export async function registerPushRegistration(
|
||||
userId: string,
|
||||
device: RegisteredDevice,
|
||||
|
|
@ -63,10 +70,12 @@ export async function registerPushRegistration(
|
|||
|| /\s/.test(registrationId)
|
||||
) throw new NotificationServiceError('native')
|
||||
|
||||
const expectedProvider = resolveDevicePushProvider(device)
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase.rpc('register_push_registration', {
|
||||
push_device_id: device.id,
|
||||
push_provider: 'fcm',
|
||||
push_provider: expectedProvider,
|
||||
registration_id: registrationId,
|
||||
})
|
||||
if (error !== null) throw error
|
||||
|
|
@ -74,7 +83,7 @@ export async function registerPushRegistration(
|
|||
const registeredAt = data.registered_at
|
||||
if (
|
||||
data.device_id !== device.id
|
||||
|| data.provider !== 'fcm'
|
||||
|| data.provider !== expectedProvider
|
||||
|| typeof registeredAt !== 'string'
|
||||
|| !Number.isFinite(Date.parse(registeredAt))
|
||||
) throw new NotificationServiceError('server')
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import type { D3roSupabaseClient, HistoryEntry, Meeting } from '@d3ro/api-client'
|
||||
import type { D3roSupabaseClient, HistoryEntry, Meeting, Transcript } from '@d3ro/api-client'
|
||||
import type {
|
||||
UnifiedSearchResponse,
|
||||
UnifiedSearchResult,
|
||||
} from '@d3ro/core/types'
|
||||
import { formatTranscriptTimestamp } from '@d3ro/core/meeting-transcript'
|
||||
import { supabase } from '../../lib/supabase'
|
||||
|
||||
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
|
||||
|
|
@ -10,10 +11,13 @@ 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'
|
||||
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'
|
||||
export type UnifiedSearchServiceErrorCode = 'auth' | 'invalid' | 'network' | 'server' | 'aborted'
|
||||
|
||||
export class UnifiedSearchServiceError extends Error {
|
||||
constructor(
|
||||
|
|
@ -40,22 +44,44 @@ type UnifiedSearchMeetingRow = Pick<
|
|||
'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<UnifiedSearchSourcePage<UnifiedSearchHistoryRow>>
|
||||
searchMeetings: (
|
||||
query: string,
|
||||
limit: number,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<UnifiedSearchSourcePage<UnifiedSearchMeetingRow>>
|
||||
searchTranscriptSegments?: (
|
||||
query: string,
|
||||
limit: number,
|
||||
signal?: AbortSignal,
|
||||
) => Promise<UnifiedSearchSourcePage<UnifiedSearchTranscriptSegmentRow>>
|
||||
loadMeetingsByIds?: (
|
||||
meetingIds: readonly string[],
|
||||
signal?: AbortSignal,
|
||||
) => Promise<UnifiedSearchMeetingMetaRow[]>
|
||||
}
|
||||
|
||||
export interface MobileUnifiedSearchOptions {
|
||||
userId: string
|
||||
query: string
|
||||
limit?: number
|
||||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
function client(): D3roSupabaseClient {
|
||||
|
|
@ -149,11 +175,17 @@ function recordResult(row: UnifiedSearchHistoryRow, query: string): UnifiedSearc
|
|||
}
|
||||
}
|
||||
|
||||
function meetingResult(row: UnifiedSearchMeetingRow, query: string): UnifiedSearchResult {
|
||||
function meetingResult(
|
||||
row: UnifiedSearchMeetingRow,
|
||||
query: string,
|
||||
segmentMatch?: UnifiedSearchTranscriptSegmentRow,
|
||||
): UnifiedSearchResult {
|
||||
const titleMatched = includesQuery(row.title, query)
|
||||
const matchingTranscript = [row.edited_transcript, row.raw_transcript]
|
||||
.find((value) => includesQuery(value, query))
|
||||
const segmentSnippet = segmentMatch ? formatSegmentSnippet(segmentMatch) : null
|
||||
const preview = matchingTranscript
|
||||
?? segmentSnippet
|
||||
?? row.edited_transcript
|
||||
?? row.raw_transcript
|
||||
?? row.title
|
||||
|
|
@ -163,11 +195,59 @@ function meetingResult(row: UnifiedSearchMeetingRow, query: string): UnifiedSear
|
|||
id: row.id,
|
||||
title: row.title,
|
||||
excerpt: createMobileSearchExcerpt(preview, query),
|
||||
matchedField: titleMatched ? 'title' : 'transcript',
|
||||
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<T>(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 function mergeMobileUnifiedSearchResults(
|
||||
records: readonly UnifiedSearchResult[],
|
||||
meetings: readonly UnifiedSearchResult[],
|
||||
|
|
@ -178,10 +258,16 @@ export function mergeMobileUnifiedSearchResults(
|
|||
.slice(0, limit)
|
||||
}
|
||||
|
||||
function toSearchError(error: unknown): UnifiedSearchServiceError {
|
||||
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
|
||||
|
||||
const candidate = error as { code?: unknown; message?: unknown }
|
||||
const candidate = error as { code?: unknown; message?: unknown; name?: unknown }
|
||||
if (candidate?.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
|
||||
|
|
@ -205,35 +291,90 @@ export function createSupabaseUnifiedSearchSource(
|
|||
searchClient: D3roSupabaseClient,
|
||||
): UnifiedSearchSourcePort {
|
||||
return {
|
||||
async searchRecords(userId, query, limit) {
|
||||
async searchRecords(userId, query, limit, signal) {
|
||||
throwIfAborted(signal)
|
||||
const pattern = createPostgrestLikePattern(query)
|
||||
const { data, error, count } = await 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 })
|
||||
.limit(limit)
|
||||
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
|
||||
return { rows: data ?? [], total: count ?? data?.length ?? 0 }
|
||||
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) {
|
||||
async searchMeetings(query, limit, signal) {
|
||||
throwIfAborted(signal)
|
||||
const pattern = createPostgrestLikePattern(query)
|
||||
const { data, error, count } = await 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 })
|
||||
.limit(limit)
|
||||
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
|
||||
return { rows: data ?? [], total: count ?? data?.length ?? 0 }
|
||||
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 ?? []
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -249,24 +390,70 @@ export async function searchUnifiedContent(
|
|||
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] = await Promise.all([
|
||||
source.searchRecords(options.userId, query, limit),
|
||||
source.searchMeetings(query, limit),
|
||||
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<string, UnifiedSearchTranscriptSegmentRow>()
|
||||
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)),
|
||||
meetingPage.rows.map((row) => meetingResult(row, query)),
|
||||
[...directMeetingResults, ...segmentOnlyMeetingResults],
|
||||
limit,
|
||||
)
|
||||
return {
|
||||
query,
|
||||
results,
|
||||
total: recordPage.total + meetingPage.total,
|
||||
total: recordPage.total + meetingPage.total + segmentOnlyMeetingResults.length,
|
||||
}
|
||||
} catch (error) {
|
||||
throw toSearchError(error)
|
||||
throw toSearchError(error, options.signal)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue