import type { ReactNode } from 'react'; import type { CompletedIntervention, ProgressEvent } from '../../types/ai-writing'; import { TimelineStep } from './TimelineStep'; import { CompletedInterventionCard } from './CompletedInterventionCard'; import { UserRequirementCard } from './UserRequirementCard'; interface AIWritingTimelineProps { events: ProgressEvent[]; completedInterventions: CompletedIntervention[]; currentPause: ProgressEvent | null; currentInterventionSlot?: ReactNode; sampleAnalysisSlot?: ReactNode; sampleProfileSlot?: ReactNode; intentSlot?: ReactNode; intentSlotDone?: boolean; } const DISPLAYED_TYPES = new Set([ 'step_start', 'step_done', 'sample_profile_ready', 'materials_ready', 'outline_ready', 'draft_ready', 'review_ready', 'await_user', 'resumed', 'notice', 'model_switch', 'done', 'error', ]); export function AIWritingTimeline({ events, completedInterventions, currentPause, currentInterventionSlot, sampleAnalysisSlot, sampleProfileSlot, intentSlot, intentSlotDone = false, }: AIWritingTimelineProps) { const visible = events.filter((e) => DISPLAYED_TYPES.has(e.type)); if (visible.length === 0 && !intentSlot && !sampleAnalysisSlot && !sampleProfileSlot) return null; // 遍历时追踪 await_user 事件的序号,以便从 completedInterventions 取对应记录 let awaitUserIdx = 0; let intentInserted = false; const lastSampleEventIndex = visible.reduce( (last, event, index) => (isSampleEvent(event) ? index : last), -1, ); const rows: Array<{ key: string; event: ProgressEvent; slot: ReactNode; extendLineThroughSlot?: boolean; }> = []; const makeIntentEvent = (): ProgressEvent => ({ type: intentSlotDone ? 'step_done' : 'step_start', sessionId: 'intent-slot', agentName: '系统', message: intentSlotDone ? '意图解析完成' : '正在解析写作意图', }); const pushIntentRow = (key: string) => { if (!intentSlot || intentInserted) return; rows.push({ key, event: makeIntentEvent(), slot: intentSlot, extendLineThroughSlot: false, }); intentInserted = true; }; const makeSampleAnalysisEvent = (): ProgressEvent => ({ type: 'step_start', sessionId: 'sample-analysis-slot', agentName: '样文分析师', message: '正在分析样文风格', }); if (sampleAnalysisSlot && lastSampleEventIndex < 0) { rows.push({ key: 'sample-analysis-live', event: makeSampleAnalysisEvent(), slot: sampleAnalysisSlot, extendLineThroughSlot: false, }); } visible.forEach((event, idx) => { let slot: ReactNode = null; let extendLineThroughSlot = true; if (event.type === 'await_user') { const completedRecord = completedInterventions[awaitUserIdx]; awaitUserIdx += 1; if (completedRecord) { // 已完成的干预点 → 先以独立样式回显用户的输入要求,再渲染折叠历史卡 slot = (
); } else if (currentPause && idx === visible.length - 1) { // 当前活跃干预点 → 渲染操作卡 slot = currentInterventionSlot; } } if (intentSlot && isIntentDoneEvent(event)) { slot = slot ? (
{slot} {intentSlot}
) : intentSlot; extendLineThroughSlot = false; intentInserted = true; } if (sampleAnalysisSlot && idx === lastSampleEventIndex) { slot = slot ? (
{slot} {sampleAnalysisSlot}
) : sampleAnalysisSlot; extendLineThroughSlot = false; } else if (sampleProfileSlot && event.type === 'sample_profile_ready') { slot = slot ? (
{slot} {sampleProfileSlot}
) : sampleProfileSlot; extendLineThroughSlot = false; } rows.push({ key: `event-${idx}`, event, slot, extendLineThroughSlot, }); if ( intentSlot && !intentSlotDone && !intentInserted && lastSampleEventIndex >= 0 && idx === lastSampleEventIndex ) { pushIntentRow(`intent-after-sample-${idx}`); } }); if (!intentInserted) { pushIntentRow('intent-fallback'); } return (
{rows.map((row, idx) => { const isLast = idx === rows.length - 1; return ( {row.slot} ); })}
); } function isSampleEvent(event: ProgressEvent): boolean { return event.agentName === '样文分析师' || event.type === 'sample_profile_ready'; } function isIntentDoneEvent(event: ProgressEvent): boolean { return ( event.type === 'step_done' && event.agentName === '系统' && (event.message ?? '').includes('意图解析完成') ); }