feat: 知识治理平台(含已验证发布/回滚契约)

全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环):

- 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口)
- 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入
  parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截
- 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过):
  * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id)
  * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配
  * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数
  * 回滚 job payload 支持 prior_execution_id(strict 解码)
- 三通道发布+回滚演练实测:publish 4/4 verified,检索命中,
  rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false
- 前端 A·Data-console 风格全站统一,120 用例通过
- 门禁:go vet / go test 全包通过,gofmt 干净
This commit is contained in:
2026-09-13 12:02:58 +08:00
committed by 姚章浩
parent ed48c4ceda
commit dcdc8d1ddd
410 changed files with 56661 additions and 1 deletions
+378
View File
@@ -0,0 +1,378 @@
import { newOperationID } from './operationKey'
export type SourceConnectionType = 'pp' | 'alist' | 'dingtalk' | 'manual'
export type SourceHealthStatus = 'unknown' | 'healthy' | 'degraded' | 'unhealthy'
export type SourceScanState = 'pending' | 'running' | 'completed' | 'failed'
export interface SourceScanSummary {
id: string
state: SourceScanState
observed_items: number
unchanged_items: number
added_items: number
updated_items: number
over_limit_items: number
missing_items: number
planned_uploads: number
error_code: string
created_at: string
completed_at: string | null
}
export interface TriggeredSourceScan {
id: string
source_connection_id: string
source_type: SourceConnectionType
state: SourceScanState
created_at: string
}
export type SourceScanChangeType = 'added' | 'updated' | 'unavailable'
export interface SourceScanChangeView {
id: string
source_item_id: string | null
change_type: SourceScanChangeType
observed_size_bytes: number | null
created_at: string
}
export interface SourceScanDetail extends SourceScanSummary {
changes: SourceScanChangeView[]
}
export interface SourceScanPage {
items: SourceScanSummary[]
total: number
limit: number
offset: number
}
export interface SourceConnectionView {
id: string
source_type: SourceConnectionType
name: string
enabled: boolean
schedule_configured: boolean
health_status: SourceHealthStatus
item_count: number
updated_at: string
last_scan?: SourceScanSummary | null
}
export interface SourceConnectionSummary {
total: number
enabled: number
disabled: number
healthy: number
degraded: number
unhealthy: number
unknown: number
}
export interface SourceConnectionPage {
items: SourceConnectionView[]
summary: SourceConnectionSummary
}
const sourceTypes = new Set<SourceConnectionType>(['pp', 'alist', 'dingtalk', 'manual'])
const healthStatuses = new Set<SourceHealthStatus>(['unknown', 'healthy', 'degraded', 'unhealthy'])
const scanStates = new Set<SourceScanState>(['pending', 'running', 'completed', 'failed'])
const scanChangeTypes = new Set<SourceScanChangeType>(['added', 'updated', 'unavailable'])
const summaryKeys: ReadonlyArray<keyof SourceConnectionSummary> = [
'total', 'enabled', 'disabled', 'healthy', 'degraded', 'unhealthy', 'unknown',
]
export function parseSourceConnectionsResponse(value: unknown): SourceConnectionPage {
if (!isRecord(value) || !Array.isArray(value.data) || !isRecord(value.summary)) {
throw new Error('invalid source connections response')
}
const items = value.data.map((raw): SourceConnectionView => {
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_type !== 'string' ||
!sourceTypes.has(raw.source_type as SourceConnectionType) || typeof raw.name !== 'string' ||
typeof raw.enabled !== 'boolean' || typeof raw.schedule_configured !== 'boolean' ||
typeof raw.health_status !== 'string' || !healthStatuses.has(raw.health_status as SourceHealthStatus) ||
!isCount(raw.item_count) || typeof raw.updated_at !== 'string') {
throw new Error('invalid source connection item')
}
const lastScan = parseSourceScanSummary(raw.last_scan)
return {
id: raw.id,
source_type: raw.source_type as SourceConnectionType,
name: raw.name,
enabled: raw.enabled,
schedule_configured: raw.schedule_configured,
health_status: raw.health_status as SourceHealthStatus,
item_count: raw.item_count,
updated_at: raw.updated_at,
last_scan: lastScan,
}
})
const summary = {} as SourceConnectionSummary
for (const key of summaryKeys) {
const count = value.summary[key]
if (!isCount(count)) throw new Error('invalid source connection summary')
summary[key] = count
}
return { items, summary }
}
export interface SourceConnectionListItem {
id: string
source_type: SourceConnectionType
name: string
enabled: boolean
schedule_configured: boolean
health_status: string
item_count: number
updated_at: string
next_refresh_at: string | null
}
function parseConnectionList(raw: unknown): SourceConnectionListItem[] {
if (!Array.isArray(raw)) throw new Error('invalid source connections list')
return raw.map((item): SourceConnectionListItem => {
if (!isRecord(item) || typeof item.id !== 'string' || typeof item.source_type !== 'string' ||
typeof item.name !== 'string' || typeof item.enabled !== 'boolean' ||
typeof item.schedule_configured !== 'boolean' || typeof item.item_count !== 'number' ||
typeof item.updated_at !== 'string' ||
(item.next_refresh_at !== null && typeof item.next_refresh_at !== 'string')) {
throw new Error('invalid source connection item')
}
return {
id: item.id, source_type: item.source_type as SourceConnectionType, name: item.name,
enabled: item.enabled, schedule_configured: item.schedule_configured,
health_status: typeof item.health_status === 'string' ? item.health_status : '',
item_count: item.item_count, updated_at: item.updated_at,
next_refresh_at: (item.next_refresh_at as string | null) ?? null,
}
})
}
export async function fetchSourceConnectionList(signal?: AbortSignal): Promise<SourceConnectionListItem[]> {
const response = await fetch('/api/v1/source-connections', { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`加载来源连接失败(HTTP ${response.status})`)
const value = await response.json() as unknown
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source connections response')
return parseConnectionList(value.data)
}
export async function fetchSourceConnections(signal?: AbortSignal): Promise<SourceConnectionPage> {
const response = await fetch('/api/v1/source-connections', {
headers: { Accept: 'application/json' },
signal,
})
if (!response.ok) {
throw new Error(`加载来源连接失败(HTTP ${response.status})`)
}
return parseSourceConnectionsResponse(await response.json())
}
export async function triggerSourceScan(connectionID: string): Promise<TriggeredSourceScan> {
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scan`, {
method: 'POST',
headers: { Accept: 'application/json', 'Idempotency-Key': newOperationID() },
})
if (!response.ok) {
throw new Error(response.status === 409 ? '该来源已有扫描正在运行' : `触发来源扫描失败(HTTP ${response.status})`)
}
const raw = await response.json() as unknown
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_connection_id !== 'string' ||
typeof raw.source_type !== 'string' || !sourceTypes.has(raw.source_type as SourceConnectionType) ||
typeof raw.state !== 'string' || !scanStates.has(raw.state as SourceScanState) || typeof raw.created_at !== 'string') {
throw new Error('invalid triggered source scan response')
}
return {
id: raw.id,
source_connection_id: raw.source_connection_id,
source_type: raw.source_type as SourceConnectionType,
state: raw.state as SourceScanState,
created_at: raw.created_at,
}
}
export function parseSourceScansResponse(value: unknown): SourceScanPage {
if (!isRecord(value) || !Array.isArray(value.data) || !isCount(value.total) ||
!isCount(value.limit) || value.limit < 1 || !isCount(value.offset)) {
throw new Error('invalid source scans response')
}
return {
items: value.data.map((item) => {
const parsed = parseSourceScanSummary(item)
if (!parsed) throw new Error('invalid source scan summary')
return parsed
}),
total: value.total,
limit: value.limit,
offset: value.offset,
}
}
export function parseSourceScanDetailResponse(value: unknown): SourceScanDetail {
if (!isRecord(value) || !isRecord(value.data) || !Array.isArray(value.data.changes)) {
throw new Error('invalid source scan detail response')
}
const summary = parseSourceScanSummary(value.data)
if (!summary) throw new Error('invalid source scan detail response')
const changes = value.data.changes.map((raw): SourceScanChangeView => {
if (!isRecord(raw) || typeof raw.id !== 'string' ||
(raw.source_item_id !== null && typeof raw.source_item_id !== 'string') ||
typeof raw.change_type !== 'string' || !scanChangeTypes.has(raw.change_type as SourceScanChangeType) ||
(raw.observed_size_bytes !== null && !isCount(raw.observed_size_bytes)) || typeof raw.created_at !== 'string') {
throw new Error('invalid source scan change')
}
return {
id: raw.id,
source_item_id: raw.source_item_id as string | null,
change_type: raw.change_type as SourceScanChangeType,
observed_size_bytes: raw.observed_size_bytes as number | null,
created_at: raw.created_at,
}
})
return { ...summary, changes }
}
export async function fetchSourceScans(connectionID: string, limit = 20, offset = 0, signal?: AbortSignal): Promise<SourceScanPage> {
const params = new URLSearchParams({ limit: String(limit), offset: String(offset) })
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans?${params}`, {
headers: { Accept: 'application/json' }, signal,
})
if (!response.ok) throw new Error(`加载扫描历史失败(HTTP ${response.status})`)
return parseSourceScansResponse(await response.json())
}
export async function fetchSourceScanDetail(connectionID: string, scanID: string, signal?: AbortSignal): Promise<SourceScanDetail> {
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans/${encodeURIComponent(scanID)}`, {
headers: { Accept: 'application/json' }, signal,
})
if (!response.ok) throw new Error(`加载扫描详情失败(HTTP ${response.status})`)
return parseSourceScanDetailResponse(await response.json())
}
function parseSourceScanSummary(raw: unknown): SourceScanSummary | null {
if (raw === null || raw === undefined) return null
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.state !== 'string' ||
!scanStates.has(raw.state as SourceScanState) || typeof raw.error_code !== 'string' ||
typeof raw.created_at !== 'string' || (raw.completed_at !== null && typeof raw.completed_at !== 'string')) {
throw new Error('invalid source scan summary')
}
const countKeys = ['observed_items', 'unchanged_items', 'added_items', 'updated_items', 'over_limit_items', 'missing_items', 'planned_uploads'] as const
for (const key of countKeys) {
if (!isCount(raw[key])) throw new Error('invalid source scan summary')
}
return {
id: raw.id,
state: raw.state as SourceScanState,
observed_items: raw.observed_items as number,
unchanged_items: raw.unchanged_items as number,
added_items: raw.added_items as number,
updated_items: raw.updated_items as number,
over_limit_items: raw.over_limit_items as number,
missing_items: raw.missing_items as number,
planned_uploads: raw.planned_uploads as number,
error_code: raw.error_code,
created_at: raw.created_at,
completed_at: raw.completed_at as string | null,
}
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function isCount(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0
}
// V1 source settings revisions -------------------------------------------------
export type SourceSettingsRevisionState = 'validated' | 'active' | 'retired' | 'failed'
export interface SourceSettingsRevision {
id: string
revision_number: number
state: SourceSettingsRevisionState
name: string
endpoint: string
credential_ref: string
enabled: boolean
refresh_interval_seconds: string
created_by: string
created_at: string
validated_at: string | null
activated_at: string | null
retired_at: string | null
}
export interface SourceSettingsDraftRequest {
source_type: SourceConnectionType
name: string
endpoint: string
credential_ref: string
enabled: boolean
schedule: { refresh_interval_seconds?: number | null }
operator: string
}
const revisionStates = new Set<SourceSettingsRevisionState>(['validated', 'active', 'retired', 'failed'])
function parseSettingsRevision(raw: unknown): SourceSettingsRevision {
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.revision_number !== 'number' ||
typeof raw.state !== 'string' || !revisionStates.has(raw.state as SourceSettingsRevisionState) ||
typeof raw.name !== 'string' || typeof raw.endpoint !== 'string' || typeof raw.credential_ref !== 'string' ||
typeof raw.enabled !== 'boolean' || typeof raw.refresh_interval_seconds !== 'string' ||
typeof raw.created_by !== 'string' || typeof raw.created_at !== 'string' ||
(raw.validated_at !== null && typeof raw.validated_at !== 'string') ||
(raw.activated_at !== null && typeof raw.activated_at !== 'string') ||
(raw.retired_at !== null && typeof raw.retired_at !== 'string')) {
throw new Error('invalid source settings revision')
}
return {
id: raw.id, revision_number: raw.revision_number, state: raw.state as SourceSettingsRevisionState,
name: raw.name, endpoint: raw.endpoint, credential_ref: raw.credential_ref, enabled: raw.enabled,
refresh_interval_seconds: raw.refresh_interval_seconds, created_by: raw.created_by, created_at: raw.created_at,
validated_at: (raw.validated_at as string | null) ?? null,
activated_at: (raw.activated_at as string | null) ?? null,
retired_at: (raw.retired_at as string | null) ?? null,
}
}
export async function fetchSourceSettingsRevisions(connectionID: string, signal?: AbortSignal): Promise<SourceSettingsRevision[]> {
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, {
headers: { Accept: 'application/json' },
signal,
})
if (!response.ok) throw new Error(`加载来源设置历史失败(HTTP ${response.status})`)
const value = await response.json() as unknown
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source settings revisions response')
return value.data.map(parseSettingsRevision)
}
async function postSourceSettings(path: string, body: string, errorMessage: string): Promise<{ operation: string; id: string | null }> {
const response = await fetch(path, {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-Governance-Action': 'source-settings', 'Idempotency-Key': newOperationID() },
body,
})
if (!response.ok) {
let detail = ''
try {
const parsed = await response.json() as unknown
if (isRecord(parsed) && typeof parsed.error === 'string') detail = parsed.error
} catch { /* keep status-only message */ }
throw new Error(detail ? `${errorMessage}:${detail}` : `${errorMessage}(HTTP ${response.status})`)
}
const value = await response.json() as unknown
if (!isRecord(value) || (value.operation !== 'created' && value.operation !== 'replayed') || !isRecord(value.data)) throw new Error('invalid source settings response')
const id = typeof value.data.id === 'string' ? value.data.id : null
return { operation: value.operation, id }
}
export async function createSourceSettingsRevision(connectionID: string, draft: SourceSettingsDraftRequest): Promise<{ operation: string; id: string }> {
const result = await postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, JSON.stringify(draft), '保存来源设置草稿失败')
if (!result.id) throw new Error('创建来源设置草稿未返回修订号')
return result as { operation: string; id: string }
}
export async function activateSourceSettingsRevision(connectionID: string, revisionID: string): Promise<{ operation: string }> {
return postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions/${encodeURIComponent(revisionID)}/activate`, '{}', '启用来源设置修订失败')
}