199 lines
5.2 KiB
TypeScript
199 lines
5.2 KiB
TypeScript
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 = (
|
|
<div className="space-y-2">
|
|
<UserRequirementCard record={completedRecord} />
|
|
<CompletedInterventionCard record={completedRecord} />
|
|
</div>
|
|
);
|
|
} else if (currentPause && idx === visible.length - 1) {
|
|
// 当前活跃干预点 → 渲染操作卡
|
|
slot = currentInterventionSlot;
|
|
}
|
|
}
|
|
|
|
if (intentSlot && isIntentDoneEvent(event)) {
|
|
slot = slot ? (
|
|
<div className="space-y-2">
|
|
{slot}
|
|
{intentSlot}
|
|
</div>
|
|
) : intentSlot;
|
|
extendLineThroughSlot = false;
|
|
intentInserted = true;
|
|
}
|
|
|
|
if (sampleAnalysisSlot && idx === lastSampleEventIndex) {
|
|
slot = slot ? (
|
|
<div className="space-y-2">
|
|
{slot}
|
|
{sampleAnalysisSlot}
|
|
</div>
|
|
) : sampleAnalysisSlot;
|
|
extendLineThroughSlot = false;
|
|
} else if (sampleProfileSlot && event.type === 'sample_profile_ready') {
|
|
slot = slot ? (
|
|
<div className="space-y-2">
|
|
{slot}
|
|
{sampleProfileSlot}
|
|
</div>
|
|
) : 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 (
|
|
<div className="px-4 py-3 space-y-0">
|
|
{rows.map((row, idx) => {
|
|
const isLast = idx === rows.length - 1;
|
|
return (
|
|
<TimelineStep
|
|
key={row.key}
|
|
event={row.event}
|
|
isLast={isLast && (!row.slot || !row.extendLineThroughSlot)}
|
|
>
|
|
{row.slot}
|
|
</TimelineStep>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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('意图解析完成')
|
|
);
|
|
}
|