101 lines
5.5 KiB
TypeScript
101 lines
5.5 KiB
TypeScript
// SPDX-License-Identifier: GPL-3.0-or-later
|
||
export interface ProgressSession {
|
||
id: string;
|
||
state: string;
|
||
busy: boolean;
|
||
created_at: number;
|
||
updated_at?: number;
|
||
approval?: { at?: number };
|
||
proposal?: { at?: number };
|
||
recovery?: { requires_approval: boolean; intent: 'discuss' | 'revise' };
|
||
}
|
||
export interface ProgressEvent {
|
||
type: string;
|
||
at: number;
|
||
data: Record<string, unknown>;
|
||
}
|
||
export const PROGRESS_STAGES = ['生成方案', '确认方案', '绘制原理图', 'PCB Layout', '校验与交付'] as const;
|
||
export const PAUSED_STATES = new Set(['blocked', 'failed', 'interrupted', 'cancelled']);
|
||
|
||
/** Only actual tool activity counts; proposals and chat may describe future work. */
|
||
export function observedProductionStage(events: readonly ProgressEvent[]): number {
|
||
let stage = 2;
|
||
for (const event of events) {
|
||
if (event.type === 'tool.started' && event.data.tool === 'pcb_deliver') stage = 4;
|
||
if (event.type !== 'eda.started' || typeof event.data.label !== 'string') continue;
|
||
const label = event.data.label;
|
||
if (/independent native delivery verification|(?:最终|交付).*(?:导出|打包|渲染)|(?:导出|保存|打包|渲染).*(?:最终|交付)|\b(?:final|delivery)\b.*\b(?:export|render|packag|bundle|verification)\w*|\b(?:export|render|package)\w*\b.*\b(?:final|delivery)\b/i.test(label)) stage = 4;
|
||
else if (/\b(?:pcb|layout|routing|route|placement|dsn|ses|copper)\b|\b(?:place|placing)\b.*\bboard\b|布局|布线|走线|焊盘|覆铜|板框|铜层|铺铜/i.test(label)) stage = Math.max(stage, 3);
|
||
}
|
||
return stage;
|
||
}
|
||
|
||
/** Stage milestones never advance merely because time passes. */
|
||
export function progressStage(task: ProgressSession, observed: number, remembered = 2): number {
|
||
if (task.state === 'completed') return 5;
|
||
if (!task.approval) return task.state === 'review' ? 1 : 0;
|
||
return Math.min(4, Math.max(2, observed, remembered));
|
||
}
|
||
|
||
export interface DurationEstimate { low: number; high: number; samples: number }
|
||
|
||
export interface CompletedDuration {
|
||
completedAt: number;
|
||
totalMinutes: string;
|
||
productionMinutes: string | null;
|
||
estimated: boolean;
|
||
}
|
||
|
||
/** Stop at accepted delivery, not the later assistant summary or the browser's clock. */
|
||
export function completedDuration(task: ProgressSession, events: readonly ProgressEvent[]): CompletedDuration | null {
|
||
if (task.state !== 'completed' || !Number.isFinite(task.created_at)) return null;
|
||
const deliveries = events.filter(event => event.type === 'delivery.completed'
|
||
&& Number.isFinite(event.at) && event.at >= task.created_at).map(event => event.at);
|
||
const exact = deliveries.length ? Math.min(...deliveries) : undefined;
|
||
const end = exact ?? task.updated_at;
|
||
if (end === undefined || !Number.isFinite(end) || end < task.created_at) return null;
|
||
const approvedAt = task.approval?.at;
|
||
return {
|
||
completedAt: end,
|
||
totalMinutes: ((end - task.created_at) / 60).toFixed(1),
|
||
productionMinutes: approvedAt !== undefined && Number.isFinite(approvedAt)
|
||
&& approvedAt >= task.created_at && approvedAt <= end ? ((end - approvedAt) / 60).toFixed(1) : null,
|
||
estimated: exact === undefined,
|
||
};
|
||
}
|
||
|
||
/** Seconds after approval; deliberately exclude review waiting and unfinished jobs. */
|
||
export function productionEstimate(sessions: readonly ProgressSession[], currentId: string): DurationEstimate | null {
|
||
const durations = sessions.filter(task => task.id !== currentId && task.state === 'completed')
|
||
.map(task => task.approval?.at && task.updated_at ? task.updated_at - task.approval.at : 0)
|
||
.filter(value => Number.isFinite(value) && value > 0);
|
||
if (!durations.length) return null;
|
||
const low = Math.min(...durations), high = Math.max(...durations);
|
||
// A single observation is not a precise prediction; give it a wider band.
|
||
const lower = durations.length === 1 ? low * 0.75 : low;
|
||
const upper = durations.length === 1 ? high * 1.5 : high;
|
||
return { low: Math.max(60, Math.floor(lower / 300) * 300), high: Math.max(300, Math.ceil(upper / 300) * 300), samples: durations.length };
|
||
}
|
||
|
||
export function elapsedLabel(seconds: number): string {
|
||
const minutes = Math.max(0, Math.floor(seconds / 60));
|
||
if (!minutes) return '不到 1 分钟';
|
||
return minutes < 60 ? `${minutes} 分钟` : `${Math.floor(minutes / 60)} 小时${minutes % 60 ? ` ${minutes % 60} 分钟` : ''}`;
|
||
}
|
||
|
||
export function remainingEstimate(task: ProgressSession, estimate: DurationEstimate | null, now: number): string {
|
||
if (task.state === 'completed') return '交付文件已就绪';
|
||
if (task.recovery) return task.recovery.requires_approval ? '制作已暂停,审核修订方案后再估算剩余时间' : '制作已暂停,讨论不会自动恢复绘板';
|
||
if (PAUSED_STATES.has(task.state)) return '恢复运行后再估算剩余时间';
|
||
if (task.state === 'needs_input') return '等待你补充需求后继续生成方案';
|
||
if (!task.approval) return task.state === 'review'
|
||
? '等待你确认方案后开始绘制,等待时间不计入制作预估'
|
||
: '正在研究需求与生成方案,确认方案后开始计算制作时间';
|
||
if (!estimate || !task.approval.at) return '历史样本不足,暂无法估算剩余时间';
|
||
const elapsed = Math.max(0, now - task.approval.at);
|
||
if (elapsed >= estimate.high) return '已超出历史耗时参考,仍在继续处理';
|
||
const lower = Math.max(1, Math.ceil((estimate.low - elapsed) / 60));
|
||
const upper = Math.max(lower, Math.ceil((estimate.high - elapsed) / 60));
|
||
return `预计还需约 ${lower === upper ? upper : `${lower}–${upper}`} 分钟`;
|
||
}
|