diff --git a/frontend/src/components/knowledge-processing-timeline.vue b/frontend/src/components/knowledge-processing-timeline.vue index 885ab368..b6bb5d91 100644 --- a/frontend/src/components/knowledge-processing-timeline.vue +++ b/frontend/src/components/knowledge-processing-timeline.vue @@ -4,6 +4,7 @@ import { MessagePlugin } from 'tdesign-vue-next' import { useI18n } from 'vue-i18n' import { getKnowledgeSpans, reparseKnowledge, cancelKnowledgeParse, getKnowledgeDetails } from '@/api/knowledge-base/index' import { knowledgeSpansPayloadHasTrace } from '@/utils/knowledgeTrace' +import { resolveTimelineHeaderStatus } from '@/utils/knowledgeProcessingStatus' import type { KnowledgeProcessOverrides } from '@/types/knowledgeProcess' interface SpanNode { @@ -373,8 +374,13 @@ async function fetchSpans(opts: { manual?: boolean } = {}) { } for (const stage of data.value.trace?.children || []) autoExpand(stage) expandedRows.value = expanded - const traceStatus = data.value.trace?.status || data.value.parse_status || 'running' - attemptStatuses.set(data.value.attempt, traceStatus) + const latestAttempt = data.value.latest_attempt || data.value.attempt || 0 + const tabStatus = resolveTimelineHeaderStatus({ + parseStatus: data.value.parse_status, + traceStatus: data.value.trace?.status, + isLatestAttempt: data.value.attempt === latestAttempt, + }) || 'running' + attemptStatuses.set(data.value.attempt, tabStatus) ensureAttemptStatuses() emit('update:hasSpans', knowledgeSpansPayloadHasTrace(data.value)) } else { @@ -411,7 +417,11 @@ function ensureAttemptStatuses() { getKnowledgeSpans(props.knowledgeId, n) .then((res: any) => { if (res?.success && res.data?.trace) { - attemptStatuses.set(n, res.data.trace.status || res.data.parse_status || 'running') + attemptStatuses.set(n, resolveTimelineHeaderStatus({ + parseStatus: res.data.parse_status, + traceStatus: res.data.trace?.status, + isLatestAttempt: n === latest, + }) || 'running') } }) .catch(() => { }) @@ -1128,36 +1138,18 @@ const viewingLatestAttempt = computed(() => { return active === latest }) -// Project the knowledge-level parse_status onto the trace-span status -// vocabulary localizedStatus() speaks, so the header badge reads the -// same whether it comes from the root span or from parse_status. -// 'finalizing' keeps its own label ("优化中") to match the doc card. -function parseStatusToTraceStatus(s?: string): string { - switch (s) { - case 'completed': - return 'done' - case 'processing': - return 'running' - case 'finalizing': - return 'finalizing' - default: - return s || '' - } -} - // The authoritative status for the header badge. During the async // post-pipeline window (summary / question / graph / wiki), the latest // attempt's ROOT span closes — so trace.status reads 'done' — while -// those subspans keep running and the row is still 'finalizing'. -// Trusting trace.status there flashes "已完成" mid-wiki even though the -// doc card (and LIVE badge) still say "优化中". Prefer parse_status while -// it is non-terminal on the latest attempt so all three agree. +// those subspans can still be running or can later make the knowledge fail. +// The latest knowledge row is therefore authoritative for ALL statuses, +// including terminal ones. Historical attempts keep their own root status. const headerStatus = computed(() => { - const parseStatus = data.value?.parse_status - if (viewingLatestAttempt.value && isPolling(parseStatus)) { - return parseStatusToTraceStatus(parseStatus) - } - return data.value?.trace?.status || parseStatusToTraceStatus(parseStatus) + return resolveTimelineHeaderStatus({ + parseStatus: data.value?.parse_status, + traceStatus: data.value?.trace?.status, + isLatestAttempt: viewingLatestAttempt.value, + }) }) const headerStatusText = computed(() => { @@ -1182,6 +1174,10 @@ const headerStatusTheme = computed(() => { } }) +const showLastError = computed(() => + Boolean(data.value?.last_error && data.value?.parse_status === 'failed'), +) + const stagesStatDisplay = computed(() => { const total = stages.value.length const doneCount = stages.value.filter((s) => s.status === 'done').length @@ -1235,7 +1231,7 @@ watch( () => { emit('update:summary', { totalMs: totalMs.value, - status: data.value?.trace?.status || data.value?.parse_status || '', + status: headerStatus.value, stageIndex: currentStageIndex.value, stageTotal: stages.value.length, stageLabel: currentStageLabel.value, @@ -1486,6 +1482,22 @@ const processConfigLines = computed(() => { }} + + @@ -1588,22 +1600,6 @@ const processConfigLines = computed(() => { - -
-
-
-
- ! - {{ localizedErrorTitle(data.last_error.error_code) }} - {{ data.last_error.error_code - }} -
-
{{ localizedErrorSuggestion(data.last_error.error_code) }}
-
{{ - data.last_error.error_message }} -
-
-
@@ -2058,7 +2054,7 @@ const processConfigLines = computed(() => { transition: background 150ms ease, border-color 150ms ease, color 150ms ease; } -.kp-attempt:hover { +.kp-attempt:not(.kp-attempt-active):hover { background: var(--td-bg-color-secondarycontainer); border-color: var(--td-text-color-placeholder); color: var(--td-text-color-primary); @@ -2070,7 +2066,14 @@ const processConfigLines = computed(() => { border-color: var(--td-brand-color); } -.kp-attempt-active .kp-attempt-glyph { +.kp-attempt-active:hover { + background: var(--td-brand-color); + color: var(--td-text-color-anti); + border-color: var(--td-brand-color); +} + +.kp-attempt-active .kp-attempt-glyph, +.kp-attempt-active:hover .kp-attempt-glyph { color: var(--td-text-color-anti) !important; } @@ -2614,9 +2617,9 @@ const processConfigLines = computed(() => { background: var(--td-text-color-placeholder); } -/* Last error block */ +/* Last error block — pinned in the header so long trace trees don't bury it */ .kp-last-error { - margin: 14px 20px 4px; + margin: 10px 0 0; display: flex; background: var(--td-error-color-light); border-radius: var(--td-radius-medium); diff --git a/frontend/src/utils/knowledgeProcessingStatus.test.mjs b/frontend/src/utils/knowledgeProcessingStatus.test.mjs new file mode 100644 index 00000000..92ac3824 --- /dev/null +++ b/frontend/src/utils/knowledgeProcessingStatus.test.mjs @@ -0,0 +1,36 @@ +import test from 'node:test' +import assert from 'node:assert/strict' + +import { + parseStatusToTimelineStatus, + resolveTimelineHeaderStatus, +} from './knowledgeProcessingStatus.ts' + +test('latest attempt uses failed knowledge status even when root span is done', () => { + assert.equal(resolveTimelineHeaderStatus({ + parseStatus: 'failed', + traceStatus: 'done', + isLatestAttempt: true, + }), 'failed') +}) + +test('latest attempt keeps finalizing distinct from a completed root span', () => { + assert.equal(resolveTimelineHeaderStatus({ + parseStatus: 'finalizing', + traceStatus: 'done', + isLatestAttempt: true, + }), 'finalizing') +}) + +test('historical attempt keeps its own root status', () => { + assert.equal(resolveTimelineHeaderStatus({ + parseStatus: 'failed', + traceStatus: 'done', + isLatestAttempt: false, + }), 'done') +}) + +test('parse status maps completed and processing into timeline vocabulary', () => { + assert.equal(parseStatusToTimelineStatus('completed'), 'done') + assert.equal(parseStatusToTimelineStatus('processing'), 'running') +}) diff --git a/frontend/src/utils/knowledgeProcessingStatus.ts b/frontend/src/utils/knowledgeProcessingStatus.ts new file mode 100644 index 00000000..4458b1c6 --- /dev/null +++ b/frontend/src/utils/knowledgeProcessingStatus.ts @@ -0,0 +1,34 @@ +export type TimelineStatusInput = { + parseStatus?: string + traceStatus?: string + isLatestAttempt: boolean +} + +/** + * Convert the knowledge-level parse status to the span status vocabulary used + * by the processing timeline. + */ +export function parseStatusToTimelineStatus(status?: string): string { + switch (status) { + case 'completed': + return 'done' + case 'processing': + return 'running' + default: + return status || '' + } +} + +/** + * The knowledge row is authoritative for the latest attempt. The root span + * only describes the main parse pipeline and may already be `done` while + * asynchronous enrichment is still running or has subsequently failed. + * Historical attempts have no matching knowledge-row snapshot, so they keep + * their own persisted root-span status. + */ +export function resolveTimelineHeaderStatus(input: TimelineStatusInput): string { + if (input.isLatestAttempt) { + return parseStatusToTimelineStatus(input.parseStatus) || input.traceStatus || '' + } + return input.traceStatus || parseStatusToTimelineStatus(input.parseStatus) +} diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go index 59eb32cd..55ccd933 100644 --- a/internal/handler/knowledge.go +++ b/internal/handler/knowledge.go @@ -680,16 +680,18 @@ func (h *KnowledgeHandler) GetKnowledgeSpans(c *gin.Context) { rows := []types.KnowledgeProcessingSpan{} currentAttempt := 0 + latestAttempt := 0 if h.spanRepo != nil { - if requestedAttempt == 0 { - latest, lerr := h.spanRepo.LatestAttempt(ctx, knowledge.ID) - if lerr != nil { - logger.Warnf(ctx, "spans LatestAttempt failed for %s: %v", knowledge.ID, lerr) - } else { - currentAttempt = latest - } + latest, lerr := h.spanRepo.LatestAttempt(ctx, knowledge.ID) + if lerr != nil { + logger.Warnf(ctx, "spans LatestAttempt failed for %s: %v", knowledge.ID, lerr) } else { + latestAttempt = latest + } + if requestedAttempt > 0 { currentAttempt = requestedAttempt + } else { + currentAttempt = latestAttempt } if currentAttempt > 0 { rows, err = h.spanRepo.ListByAttempt(ctx, knowledge.ID, currentAttempt) @@ -713,6 +715,8 @@ func (h *KnowledgeHandler) GetKnowledgeSpans(c *gin.Context) { resp := gin.H{ "knowledge_id": knowledge.ID, + "attempt": currentAttempt, + "latest_attempt": latestAttempt, "parse_status": knowledge.ParseStatus, "current_attempt": currentAttempt, "current_stage": currentStageName, @@ -720,10 +724,27 @@ func (h *KnowledgeHandler) GetKnowledgeSpans(c *gin.Context) { } if lastErr != nil { resp["last_error"] = gin.H{ - "stage": lastErr.Name, - "code": lastErr.ErrorCode, - "message": lastErr.ErrorMessage, - "finished_at": lastErr.FinishedAt, + "stage": lastErr.Name, + "code": lastErr.ErrorCode, + "message": lastErr.ErrorMessage, + "name": lastErr.Name, + "error_code": lastErr.ErrorCode, + "error_message": lastErr.ErrorMessage, + "finished_at": lastErr.FinishedAt, + } + } else if currentAttempt == latestAttempt && knowledge.ParseStatus == types.ParseStatusFailed && knowledge.ErrorMessage != "" { + // The knowledge row can be failed by recovery/dead-letter paths after + // the main root span has already closed. Surface that authoritative + // failure reason instead of leaving the detail drawer with a red status + // but no explanation. + resp["last_error"] = gin.H{ + "stage": "knowledge_processing", + "code": "UNKNOWN", + "message": knowledge.ErrorMessage, + "name": "knowledge_processing", + "error_code": "UNKNOWN", + "error_message": knowledge.ErrorMessage, + "finished_at": knowledge.UpdatedAt, } } c.JSON(http.StatusOK, gin.H{