完善:质量分析平台查询与分析功能

This commit is contained in:
2026-10-22 21:45:29 +08:00
parent 1e8a655adc
commit 5fbeb52b93
54 changed files with 2331 additions and 564 deletions
+43
View File
@@ -0,0 +1,43 @@
type StorageAccess = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
export const ACTIVE_ANALYSIS_STATUSES = new Set(['QUEUED', 'RUNNING', 'PLANNING', 'TOOL_RUNNING', 'PROCESSING']);
export function analysisIsRunning(status: unknown): boolean { return ACTIVE_ANALYSIS_STATUSES.has(String(status || '').toUpperCase()); }
const keyFor = (userId: string) => `quality.analysis.lastSession.${userId}`;
const selections = new Map<string, number>();
export function beginAnalysisSelection(userId: string): number {
const next = (selections.get(userId) || 0) + 1;
selections.set(userId, next); return next;
}
export function rememberPendingAnalysisSession(userId: string, id: string, selection: number, storage?: StorageAccess): void {
if (selections.get(userId) === selection) rememberAnalysisSession(userId, id, storage);
}
export function rememberAnalysisSession(userId: string, id: string, storage?: StorageAccess): void {
try {
const target = storage || window.localStorage;
if (id) target.setItem(keyFor(userId), id); else target.removeItem(keyFor(userId));
} catch { /* Storage may be disabled; server history remains available. */ }
}
export function defaultAnalysisSession(userId: string, sessions: Record<string, unknown>[], storage?: StorageAccess): string {
let remembered: string | null = null;
try { remembered = (storage || window.localStorage).getItem(keyFor(userId)); } catch { /* use server history */ }
if (remembered && sessions.some(item => item.id === remembered)) return remembered;
return sessions.length ? String(sessions[0].id || '') : '';
}
export function activeAnalysisTurn(turns: Record<string, unknown>[]): Record<string, unknown> | undefined {
return turns.slice().reverse().find(turn => analysisIsRunning(turn.status));
}
export function settledAnalysisTurn(id: string, question: string, detail: Record<string, unknown>, streamed: Record<string, unknown>, terminalEventStatus: string): Record<string, unknown> {
const detailResult = detail.result && typeof detail.result === 'object' && !Array.isArray(detail.result) ? detail.result as Record<string, unknown> : {};
const finalStatuses = new Set(['COMPLETED', 'PARTIAL', 'FAILED', 'CANCELLED', 'NEEDS_CLARIFICATION']);
const detailStatus = String(detail.status || '').toUpperCase();
const status = finalStatuses.has(detailStatus) ? detailStatus : terminalEventStatus || detailStatus;
// An SSE rotation may stop on a partial answer just before the saved task
// finishes. The final durable result must win, even when it is empty/redacted.
const durable = { ...detail, ...detailResult };
const result = finalStatuses.has(detailStatus) ? { ...streamed, ...durable } : { ...durable, ...streamed };
delete result.result;
return { ...result, id, question, status, result };
}