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,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