Files
knowledge-governance/web/src/documentWorkbench.ts
T
姚章浩 e34306b2db
build / build (push) Failing after 18s
fix(ui): 已释放窗口不再渲染 held 文案,版本页不再把 500 说成未接线
发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案;
窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。
改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。

版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time;
部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500
(nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。
改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。

RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例;
make verify 16/16 通过(local 档案)。
2026-11-01 19:34:31 +08:00

204 lines
9.2 KiB
TypeScript

import { fetchSourceAssets } from './assets'
import type { SourceAsset } from './assets'
export type DocumentStage =
'waiting' | 'downloading' | 'uploading' | 'parsing' | 'indexing' | 'completed' | 'reused' | 'blocked' | 'failed'
export const stageLabels: Record<DocumentStage, string> = {
waiting: '等待处理',
downloading: '下载中',
uploading: '上传中',
parsing: '解析中',
indexing: '建立索引',
completed: '解析完成',
reused: '内容重复,复用已有文档',
blocked: '文件超限',
failed: '需要处理',
}
export type PipelineState = 'waiting' | 'active' | 'done' | 'problem'
export interface PipelineStep {
label: string
state: PipelineState
detail: string
}
export function documentPipeline(item: SourceAsset): PipelineStep[] {
let source: PipelineStep = { label: '取得文件', state: 'waiting', detail: '等待取得文件' }
if (item.content_state === 'verified') source = { label: '取得文件', state: 'done', detail: '文件已校验' }
else if (item.content_state === 'downloading') source = { label: '取得文件', state: 'active', detail: '下载中' }
else if (item.content_state === 'over_limit') source = { label: '取得文件', state: 'problem', detail: '文件超限' }
else if (['invalid', 'failed'].includes(item.content_state))
source = { label: '取得文件', state: 'problem', detail: '文件校验失败' }
let upload: PipelineStep = { label: '上传测试库', state: 'waiting', detail: '等待上传' }
if (item.legacy_status === 'content_alias') upload = { label: '上传测试库', state: 'done', detail: '复用已有内容' }
else if (item.ingest_state === 'uploaded') upload = { label: '上传测试库', state: 'done', detail: '上传完成' }
else if (item.ingest_state === 'uploading') upload = { label: '上传测试库', state: 'active', detail: '上传中' }
else if (item.ingest_state === 'failed') upload = { label: '上传测试库', state: 'problem', detail: '上传失败' }
let processing: PipelineStep = { label: '解析与索引', state: 'waiting', detail: '等待解析' }
if (item.legacy_status === 'content_alias')
processing = { label: '解析与索引', state: 'done', detail: '复用已有解析结果' }
else if (item.processing_state === 'completed')
processing = { label: '解析与索引', state: 'done', detail: '解析完成' }
else if (item.processing_state === 'processing')
processing = { label: '解析与索引', state: 'active', detail: '解析中' }
else if (item.processing_state === 'finalizing')
processing = { label: '解析与索引', state: 'active', detail: '建立索引中' }
else if (['failed', 'stalled'].includes(item.processing_state))
processing = {
label: '解析与索引',
state: 'problem',
detail: item.processing_state === 'stalled' ? '处理停滞' : '解析失败',
}
let retrieval: PipelineStep = { label: '测试检索', state: 'waiting', detail: '未证明已启用' }
if (item.retrieval_state === 'enabled') retrieval = { label: '测试检索', state: 'done', detail: '测试环境已启用' }
else if (['enabling', 'disabling'].includes(item.retrieval_state))
retrieval = { label: '测试检索', state: 'active', detail: '状态变更中' }
else if (item.retrieval_state === 'failed')
retrieval = { label: '测试检索', state: 'problem', detail: '检索状态失败' }
return [source, upload, processing, retrieval]
}
export function documentStage(item: SourceAsset): DocumentStage {
if (item.source_change === 'unavailable' || item.source_change === 'deleted') return 'failed'
if (item.content_state === 'over_limit') return 'blocked'
if (
['invalid', 'failed'].includes(item.content_state) ||
item.ingest_state === 'failed' ||
['failed', 'stalled'].includes(item.processing_state)
)
return 'failed'
if (item.content_state === 'downloading') return 'downloading'
if (item.legacy_status === 'content_alias' && !item.has_publication) return 'reused'
if (!item.has_publication) return 'waiting'
if (item.ingest_state === 'uploading') return 'uploading'
switch (item.processing_state) {
case 'completed':
return 'completed'
case 'processing':
return 'parsing'
case 'finalizing':
return 'indexing'
default:
return 'waiting'
}
}
export function documentProblem(item: SourceAsset): string {
if (item.source_change === 'unavailable' || item.source_change === 'deleted')
return '来源文件缺失或暂不可访问,请核对来源权限和文件位置。'
if (documentStage(item) === 'blocked') return '超过当前导入大小上限,尚未上传;请拆分文件或由管理员调整限制。'
if (item.processing_state === 'stalled') return '长时间未收到处理进展,请检查解析服务。'
if (documentStage(item) === 'failed') return '处理未完成,请查看错误详情;符合重试条件时可重新处理。'
if (documentStage(item) === 'reused') return '文件内容完全相同,未重复上传。'
return ''
}
export function visibilityOrigin(item: SourceAsset): string {
switch (item.visibility_origin) {
case 'pp_public_tag':
return '来自PP公开标记'
case 'manual':
return '人工指定'
case 'configured_scope':
return '来源范围设置'
case 'file_override':
return '文件单独设置'
case 'directory_rule':
return item.scope_rule_path ? `继承目录设置:${item.scope_rule_path}` : '继承来源范围设置'
default:
return '设置来源待核对'
}
}
export function connectionKey(item: SourceAsset): string {
return item.connection_id || `${item.source_type}:${item.connection_name}`
}
export function directoryGroups(items: SourceAsset[]) {
const groups = new Map<string, { key: string; connection: string; path: string; count: number }>()
for (const item of items) {
const connection = connectionKey(item)
const path = item.directory_path || ''
// JSON tuple avoids cross-source collisions, even when labels contain separators.
const key = JSON.stringify([connection, path])
const group = groups.get(key) || { key, connection, path, count: 0 }
group.count++
groups.set(key, group)
}
return [...groups.values()].sort(
(a, b) => a.path.localeCompare(b.path, 'zh-CN') || a.connection.localeCompare(b.connection)
)
}
export function operationalDocuments(items: SourceAsset[], includeTestDocuments: boolean) {
return includeTestDocuments ? items : items.filter((item) => item.source_type !== 'manual')
}
export interface SourceProgress {
connection: string
total: number
completed: number
active: number
attention: number
waiting: number
reused: number
}
export function sourceProgress(items: SourceAsset[]): SourceProgress[] {
const result = new Map<string, SourceProgress>()
for (const item of items) {
const connection = connectionKey(item)
const progress = result.get(connection) || {
connection,
total: 0,
completed: 0,
active: 0,
attention: 0,
waiting: 0,
reused: 0,
}
const stage = documentStage(item)
progress.total++
if (stage === 'completed') progress.completed++
if (['downloading', 'uploading', 'parsing', 'indexing'].includes(stage)) progress.active++
if (['blocked', 'failed'].includes(stage)) progress.attention++
if (stage === 'waiting') progress.waiting++
if (stage === 'reused') progress.reused++
result.set(connection, progress)
}
return [...result.values()].sort((a, b) => a.connection.localeCompare(b.connection))
}
export interface DocumentFilter {
connection: string
directory: string | null
query: string
visibility: string
stage: string
}
export function filterDocuments(items: SourceAsset[], filter: DocumentFilter) {
const query = filter.query.trim().toLocaleLowerCase()
return items.filter(
(item) =>
(!filter.connection || connectionKey(item) === filter.connection) &&
(filter.directory === null || (item.directory_path || '') === filter.directory) &&
(!filter.visibility || item.visibility === filter.visibility) &&
(!filter.stage || documentStage(item) === filter.stage) &&
(!query || `${item.title || ''} ${item.directory_path || ''}`.toLocaleLowerCase().includes(query))
)
}
export async function fetchDocumentCatalog(signal?: AbortSignal): Promise<SourceAsset[]> {
const items: SourceAsset[] = []
const ids = new Set<string>()
let expected: number | undefined
do {
const page = await fetchSourceAssets({ state: '', limit: 100, offset: items.length }, signal)
if (expected === undefined) expected = page.summary.total
if (
!Number.isInteger(expected) ||
expected < 0 ||
expected > 10000 ||
page.summary.total !== expected ||
page.offset !== items.length
)
throw new Error('文档清单已变化或过大,请刷新后重试。')
for (const item of page.items) {
if (!item.id || ids.has(item.id)) throw new Error('文档分页发生变化,请刷新后重试。')
ids.add(item.id)
items.push(item)
}
if (!page.items.length && items.length !== expected) throw new Error('文档清单未加载完整,请重试。')
} while (items.length < expected)
if (items.length !== expected) throw new Error('文档数量核验失败,请重试。')
return items
}