/** 历史结果预览抽屉(任务 124):从历史记录提取可预览图片并限制数量,避免大内容阻塞页面;纯逻辑。 */ import type { HistoryRecordItem } from './history-dto.ts' export interface PreviewDescriptor { images: string[] } function safeHttpUrl(value: unknown): string { const url = typeof value === 'string' ? value.trim() : '' return /^https?:\/\//i.test(url) ? url : '' } /** 构建预览描述(结果图 + 长图,按记录顺序去重,数量受 limit 限制)。 */ export function buildPreviewDescriptor(item: HistoryRecordItem | null | undefined, limit: number): PreviewDescriptor { const images: string[] = [] const seen = new Set() const max = typeof limit === 'number' && Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : 20 const push = (url: string) => { const clean = safeHttpUrl(url) if (!clean || seen.has(clean) || images.length >= max) return seen.add(clean) images.push(clean) } if (item && typeof item === 'object') { if (Array.isArray(item.resultUrls)) item.resultUrls.forEach((url) => push(url)) push(item.longImageUrl) } return { images } }