import { useState, type ReactNode } from 'react' import type { ResearchReport } from '../lib/day3' import type { AgentTask } from '../hooks/useTaskEvents' type RecordData = Record type Section = ResearchReport['sections'][number] const record = (value: unknown): RecordData => value && typeof value === 'object' && !Array.isArray(value) ? value as RecordData : {} const records = (value: unknown) => Array.isArray(value) ? value.map(record) : [] function text(data: RecordData, ...keys: string[]) { for (const key of keys) { const value = data[key] if (typeof value === 'string' && value.trim()) return value.trim() if (typeof value === 'number' && Number.isFinite(value)) return String(value) } return '' } function amount(data: RecordData, wan = 'money_wan', yuan = 'money') { // 接口同时支持元和万元,金额缺失仍保留为空,显式披露的零元正常显示。 const money = text(data, yuan), moneyWan = text(data, wan) const value = money ? Number(money) / 10000 : moneyWan ? Number(moneyWan) : NaN return Number.isFinite(value) && value >= 0 ? value : null } const format = (value: number) => value.toLocaleString('zh-CN', { maximumFractionDigits: 4 }) const region = (data: RecordData) => [...new Set(['province', 'city', 'county'].map(key => text(data, key)).filter(Boolean))].join(' · ') const company = (data: RecordData) => text(data, 'fullname', 'company_name', 'name') function linked(label: string, url: string): ReactNode { if (!label || !/^https?:\/\//i.test(url)) return label // 校验浏览器可识别的 HTTP 地址后使用原串,避免重组 URL 时丢失供应商的签名参数。 try { const parsed = new URL(url) if (!parsed.hostname || parsed.username || parsed.password) return label } catch { return label } return {label} } /** 各工具只通过业务字段白名单进入表格。缺失一整列时隐藏该列,单个缺值以横线合并展示。 */ function BusinessTable({ title, headers, rows, empty }: { title: string; headers: string[]; rows: ReactNode[][]; empty?: string }) { const [expanded, setExpanded] = useState(false) const visibleRows = rows.filter(row => row.some(Boolean)) const columns = headers.flatMap((label, i) => visibleRows.some(row => row[i]) ? [{ label, i }] : []) // 报告内可能同时包含数份公告与伙伴列表,默认保留首屏要点并支持展开当前表的全部记录。 const previewSize = 8 const displayRows = expanded ? visibleRows : visibleRows.slice(0, previewSize) if (!columns.length && !empty) return null return

{title}

{columns.length ?
{columns.map(({ label, i }) => )}{displayRows.map((row, index) => {columns.map(({ i }) => )})}
{label}
{row[i] || '—'}
:

{empty}

} {visibleRows.length > previewSize && }
} function NoticeTable({ title, items }: { title: string; items: RecordData[] }) { return { const value = amount(item) return [text(item, 'pub_time').slice(0, 10), linked(text(item, 'title'), text(item, 'url', 'bid_url')), value == null ? '' : format(value)] })} empty="本次未查到对应公告。" /> } function Purchases({ data }: { data: RecordData }) { const items = records(data.items) const disclosed = items.flatMap(item => { const value = amount(item); return value == null ? [] : [value] }) const topics = [...new Set(items.flatMap(item => Array.isArray(item.sm_names) ? item.sm_names.filter((value): value is string => typeof value === 'string' && !!value.trim()) : []))].slice(0, 5) const total = text(data, 'total') const min = Math.min(...disclosed), max = Math.max(...disclosed) return <> } /** 以已核验记录组织报告。候选表只在待确认时展示,确认后转为所选主体和正式画像。 */ export function ResearchSections({ sections, choices, evidence }: Pick & { evidence: AgentTask['evidence'] }) { return sections?.map(section => item.key === section.key)} sourceUrl={evidence.find(item => item.id === section.evidenceId && item.verified)?.url ?? ''} />) } function ResearchSection({ section, choice, sourceUrl }: { section: Section; choice?: ResearchReport['choices'][number]; sourceUrl: string }) { const data = record(section.data), items = records(data.items), { key, tool } = section if (choice?.selected) { const selected = choice.options.find(option => option.id === choice.selected) return selected ?

已确认{key === 'notice:search' ? '公告' : '主体'} · {selected.name}{selected.region ? ` · ${selected.region}` : ''}

: null } if (key.startsWith('entity:') || key === 'buyer:entity') return [linked(company(item), text(item, 'url', 'company_url')), region(item)])} empty="未找到匹配企业。" /> if (key.endsWith(':purchases')) return if (tool === 'search_bids' || tool === 'query_bids_advanced') return if (tool === 'get_company_profile' || tool === 'get_bid_detail') { const isCompany = tool === 'get_company_profile' const value = amount(data) const facts = isCompany ? [ ['企业全称', linked(company(data), text(data, 'url', 'company_url'))], ['地域', region(data)], ['行业', text(data, 'industry', 'industry_l1')], ['经营状态', text(data, 'business_status')], ['注册资本', text(data, 'capital')], ['成立日期', text(data, 'establishment_date')], ['招标记录', text(data, 'caller_count')], ['中标记录', text(data, 'winner_count', 'win_count')], ['经营范围', text(data, 'business_scope')], ] : [ ['项目', linked(text(data, 'title'), text(data, 'url', 'bid_url'))], ['采购方', text(data, 'caller_name', 'purchaser')], ['地域', region(data)], ['发布日期', text(data, 'pub_time')], ['披露金额', value == null ? '' : `${format(value)} 万元`], ['代理机构', text(data, 'agency_name')], ] return <> row[1])} /> {!isCompany && } } if (tool === 'get_company_partners') return [company(item), text(item, 'cooperation_count'), text(item, 'last_cooperation_time')])} /> if (tool === 'get_company_business_keywords') return [text(item, 'keyword'), text(item, 'count')])} /> if (tool === 'aggregate_bids_advanced') return { const value = amount(item, 'sum_amount_wan', 'sum_amount') return [text(item, 'key'), text(item, 'count'), value == null ? '' : format(value)] })} /> if (tool === 'find_competitors' || tool === 'find_potential_bidders') return [company(item), text(item, 'co_bid_count', 'caller_history_count', 'region_win_count')])} /> if (tool === 'get_price_trends') { const stats = record(data.price_stats) return row[1])} /> } if (tool === 'web_search') return [linked(text(item, 'title', 'site_name') || '打开原网页', text(item, 'url')), text(item, 'snippet').slice(0, 240)])} /> // Fetch 正文响应可能只有 title/content。使用本次请求登记的原始网址,旧报告也可打开。 if (tool === 'web_fetch') return
{(sourceUrl || text(data, 'url')) &&

{linked('打开原网页', sourceUrl || text(data, 'url'))}

}
if (tool === 'FILE_PARSE') return [text(item, 'locator'), text(item, 'text').slice(0, 240)])} /> if (tool === 'vision') return return null } function Excerpt({ title, value }: { title: ReactNode; value: string }) { if (!value) return null return
{title}

{value.slice(0, 1500)}{value.length > 1500 ? '…' : ''}

}