export type ModelThinking = { id: string; model: string; phase: string; status: string; text: string; version: number; source: 'api'; streaming: boolean; truncated: boolean; recovering?: boolean; finishReason?: string; at?: string; startedAt?: string; finishedAt?: string }; /** Reveal only received text, catching up within 100ms without splitting a UTF-16 pair. */ export function thinkingTextFrame(from: string, target: string, elapsedMs: number): string { if (!target.startsWith(from) || elapsedMs >= 100) return target; let end = from.length + Math.ceil((target.length - from.length) * Math.max(0, elapsedMs) / 100); if (end < target.length && end > 0 && /[\uD800-\uDBFF]/.test(target[end - 1])) end--; return target.slice(0, end); } const object = (value: unknown): Record => value && typeof value === 'object' && !Array.isArray(value) ? value as Record : {}; export function mergeModelThinking(existing: unknown, update: unknown): ModelThinking[] { let rows: ModelThinking[] = Array.isArray(existing) ? existing.map(value => ({ ...object(value) }) as ModelThinking) : []; const next = object(update); if (typeof next.id !== 'string' || next.source !== 'api' || typeof next.version !== 'number') return rows; const prior = rows.find(row => row.id === next.id); if (prior && prior.version >= next.version) return rows; if (next.reset === true && !prior) rows = []; let text: string; let recovering = false; if (typeof next.text === 'string') text = next.text; else if (typeof next.delta === 'string' && !prior?.recovering && (prior?.version || 0) === next.baseVersion) text = (prior?.text || '') + next.delta; else { text = prior?.text || ''; recovering = true; } const row = { ...prior, ...next, text, recovering } as ModelThinking; const index = rows.findIndex(item => item.id === next.id); if (index < 0) rows.push(row); else rows[index] = row; return rows; } export function modelThinkingEntries(turn: Record): ModelThinking[] { const plan = object(turn.plan || object(turn.result).plan); let rows: ModelThinking[] = []; for (const source of [plan.modelThinking, turn.modelThinking]) { if (Array.isArray(source)) for (const block of source) rows = mergeModelThinking(rows, block); } return rows; } export function thinkingStatus(entry: ModelThinking, running: boolean): string { if (entry.recovering) return '正在恢复完整文本…'; if (entry.finishReason === 'length') return '达到模型输出上限,保留已返回文本'; if (entry.status === 'interrupted' && !entry.text) return '调用已中断,未收到思考文本'; if (entry.status === 'not_returned') return '接口未返回思考文本'; if (entry.status === 'interrupted' || entry.status === 'running' && !running) return '已中断,保留已返回文本'; if (entry.status === 'running') return entry.text ? '正在输出' : '等待接口返回…'; return '已返回'; } /** Merge only recorded provider text and recorded analysis events, ordered by their actual timestamps. */ export function orderedAnalysisProcess(turn: Record, timeline: Record[]): Record[] { const thoughts = modelThinkingEntries(turn).filter(entry => entry.text).map(entry => ({ ...entry, kind: 'thinking', key: `thinking:${entry.id}` })); const events = timeline.map((entry, index) => ({ ...entry, key: `event:${entry.sequence ?? index}` })); const rows: Record[] = [...thoughts, ...events]; const time = (entry: Record) => { const stamp = Date.parse(String(entry.at || entry.startedAt || '')); return Number.isFinite(stamp) ? stamp : Number.NEGATIVE_INFINITY; }; // Stable ordering preserves source order for old records without timestamps. Never invent historical times. return rows.sort((a, b) => { const left=time(a), right=time(b); return left === right ? 0 : left < right ? -1 : 1; }); } /** Adjacent provider calls read as one uninterrupted thought, without extra phase/model headings. */ export function groupAdjacentThinking(events: Record[]): Record[] { const grouped: Record[] = []; for (const event of events) { const previous = grouped.at(-1); if (event.kind === 'thinking' && previous?.kind === 'thinking') { previous.text = `${String(previous.text || '')}\n\n${String(event.text || '')}`; previous.status = previous.status === 'running' || event.status === 'running' ? 'running' : event.status; previous.truncated = Boolean(previous.truncated || event.truncated); } else grouped.push({ ...event }); } return grouped; }