修复:查询局部刷新与智能分析会话交互,完善分析收尾和配置切换
This commit is contained in:
@@ -41,3 +41,87 @@ export function settledAnalysisTurn(id: string, question: string, detail: Record
|
||||
delete result.result;
|
||||
return { ...result, id, question, status, result };
|
||||
}
|
||||
|
||||
/** Durable SSE IDs are decimal database sequences, potentially larger than JS integers. */
|
||||
export function acceptAnalysisEvent(id: string | undefined, cursor: string | undefined): boolean {
|
||||
if (!id || !cursor) return true;
|
||||
if (/^\d+$/.test(id) && /^\d+$/.test(cursor)) return BigInt(id) > BigInt(cursor);
|
||||
return id !== cursor;
|
||||
}
|
||||
|
||||
/** Providers may prefix streamed text with whitespace that the saved answer trims. */
|
||||
export function duplicatesFinalAnalysisAnswer(segment: string, answer: string): boolean {
|
||||
const text = segment.trimStart();
|
||||
return Boolean(text.trim()) && answer.trimStart().startsWith(text);
|
||||
}
|
||||
|
||||
/** Render a call once, updating its original row when the result arrives. */
|
||||
export function compactAnalysisTimeline(entries: Record<string, unknown>[]): Record<string, unknown>[] {
|
||||
const rows: Record<string, unknown>[] = [];
|
||||
const calls = new Map<string, number>();
|
||||
for (const entry of entries) {
|
||||
if (entry.kind === 'assistant') { rows.push({ ...entry }); continue; }
|
||||
if (!['tool_start', 'tool_result'].includes(String(entry.kind))) continue;
|
||||
const id = typeof entry.toolCallId === 'string' ? entry.toolCallId : '';
|
||||
const index = id ? calls.get(id) : undefined;
|
||||
if (index === undefined) {
|
||||
if (id) calls.set(id, rows.length);
|
||||
rows.push({ ...entry, kind: 'tool', hasResult: entry.kind === 'tool_result' });
|
||||
} else {
|
||||
const prior = rows[index];
|
||||
if (entry.kind === 'tool_result') rows[index] = { ...prior, ...entry, kind: 'tool', sequence: prior.sequence,
|
||||
at: prior.at, finishedAt: entry.at, input: prior.input || entry.input, hasResult: true };
|
||||
else if (!prior.input && entry.input) rows[index] = { ...prior, input: entry.input };
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
export function analysisScrollFollows(previousTop: number, currentTop: number, distanceFromBottom: number, following: boolean): boolean {
|
||||
if (currentTop < previousTop - 0.1) return false;
|
||||
return distanceFromBottom <= 24 ? true : following;
|
||||
}
|
||||
|
||||
export function partialAnalysisNotice(reason: unknown): string {
|
||||
const value = String(reason || '');
|
||||
if (value === 'mcp_error') return '部分数据查询未成功,已保留其余分析结果。可以重试或继续追问。';
|
||||
if (value === 'length') return '回答达到输出长度上限,已保存现有内容。可以重试或继续追问。';
|
||||
if (/timeout|超时/.test(value)) return '本轮分析达到处理时限,已保存现有结果。可以重试或继续追问。';
|
||||
if (value === 'evidence_incomplete') return '所需数据尚未齐全,已展示现有结果。可以继续追问。';
|
||||
return '本轮分析未完整结束,已保存现有结果。可以重试或继续追问。';
|
||||
}
|
||||
|
||||
/** Old answers keep their original wording; surface their explicitly recorded scope limitation. */
|
||||
export function hasUnreconciledEvidence(turn: Record<string, unknown>): boolean {
|
||||
const record = (value: unknown): Record<string, unknown> => value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
const result = record(turn.result), plan = record(turn.plan || result.plan);
|
||||
const entries = turn.timeline || result.timeline || plan.timeline;
|
||||
const checks = new Map<string, string>();
|
||||
if (Array.isArray(entries)) for (const item of entries) {
|
||||
const event = record(item);
|
||||
if (event.kind !== 'tool_result' || event.status !== 'completed') continue;
|
||||
for (const [key, status] of Object.entries(record(event.evidenceChecks))) {
|
||||
if (/^[a-f0-9]{16}$/.test(key) && typeof status === 'string' && ['unresolved', 'checked', 'expected'].includes(status)) checks.set(key, status);
|
||||
}
|
||||
}
|
||||
if (checks.size) return [...checks.values()].includes('unresolved');
|
||||
const text = [turn.answer, result.answer, ...(Array.isArray(entries) ? entries.map(item => record(item).text) : [])].filter(item => typeof item === 'string').join('\n');
|
||||
return /未(?:与|和)[^。!?\n]{0,24}(?:对账|核对)|未(?:完全)?对账/.test(text);
|
||||
}
|
||||
|
||||
/** A stopped turn may save its last pre-tool progress note; keep it in chronology,
|
||||
* but do not repeat it as a final answer below later tool results. */
|
||||
export function partialAnswerIsOnlyProgress(turn: Record<string, unknown>): boolean {
|
||||
const record = (value: unknown): Record<string, unknown> => value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
||||
const result = record(turn.result), plan = record(turn.plan || result.plan);
|
||||
if (String(turn.status || result.status).toUpperCase() !== 'PARTIAL') return false;
|
||||
const answer = String(result.answer || turn.answer || '').trim();
|
||||
const entries = turn.timeline || result.timeline || plan.timeline;
|
||||
if (!answer || !Array.isArray(entries)) return false;
|
||||
let lastAssistant = -1;
|
||||
for (let index = entries.length - 1; index >= 0; index--) {
|
||||
if (record(entries[index]).kind === 'assistant') { lastAssistant = index; break; }
|
||||
}
|
||||
if (lastAssistant < 0 || String(record(entries[lastAssistant]).text || '').trim() !== answer) return false;
|
||||
return entries.slice(lastAssistant + 1).some(item => ['tool_start', 'tool_result'].includes(String(record(item).kind)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user