629694546c
build / build (push) Successful in 2m14s
为什么:arch-evidence 按路径识别前端接口请求层;reconciliation.ts 未被归入该层,评审表会漏报新增请求客户端。 做什么:仅重命名 web/src/reconciliation.ts -> reconciliationApi.ts 并同步导入,无行为变化。 验证:npx tsc --noEmit;相关 vitest 通过。
228 lines
7.5 KiB
TypeScript
228 lines
7.5 KiB
TypeScript
export type ReconciliationFindingType = 'R1' | 'R2' | 'R3' | 'R4' | 'R5' | 'R6' | 'R7' | 'R8'
|
||
export type ReconciliationEvidence = 'independent' | 'declared_only' | 'none'
|
||
export type ReconciliationConfirmation = 'confirmed' | 'pending'
|
||
export type ReconciliationAction = 'requeue_upload' | 'remove_from_test' | 'writeback_state' | 'enter_removal' | 'none'
|
||
|
||
export interface ReconciliationRun {
|
||
id: string
|
||
kb_id: string
|
||
state: 'running' | 'succeeded' | 'failed'
|
||
trigger: 'schedule' | 'manual'
|
||
started_at: string
|
||
finished_at: string | null
|
||
ledger_count: number
|
||
kb_count: number
|
||
counts: Record<string, number>
|
||
previous_counts: Record<string, number> | null
|
||
window_overlapped: boolean
|
||
error_code: string
|
||
}
|
||
export interface ReconciliationRunPage {
|
||
items: ReconciliationRun[]
|
||
total: number
|
||
limit: number
|
||
offset: number
|
||
}
|
||
export interface ReconciliationFinding {
|
||
id: string
|
||
run_id: string
|
||
type: ReconciliationFindingType
|
||
publication_id: string
|
||
document_id: string
|
||
title: string
|
||
path: string
|
||
source: string
|
||
target_scope: string
|
||
ledger_state: string
|
||
weknora_parse_status: string
|
||
normalized_state: string
|
||
ledger_sha256: string
|
||
declared_sha256: string
|
||
weknora_file_md5: string
|
||
evidence_level: ReconciliationEvidence
|
||
confirmation_state: ReconciliationConfirmation
|
||
detail: string
|
||
suggested_action: ReconciliationAction
|
||
action_state: 'open' | 'done'
|
||
weknora_url: string
|
||
}
|
||
export interface ReconciliationFindingPage {
|
||
items: ReconciliationFinding[]
|
||
total: number
|
||
limit: number
|
||
offset: number
|
||
}
|
||
export interface ReconciliationStage {
|
||
name: string
|
||
status: string
|
||
error_code: string
|
||
finished_at: string | null
|
||
}
|
||
export interface ReconciliationFailure {
|
||
task_type: string
|
||
fail_count: number
|
||
failed_at: string
|
||
last_error: string
|
||
}
|
||
export interface ReconciliationProgress {
|
||
document_id: string
|
||
parse_status: string
|
||
ingestion_status: string
|
||
wiki_status: string
|
||
stages: ReconciliationStage[]
|
||
failures: ReconciliationFailure[]
|
||
has_more_failures: boolean
|
||
observed_at: string
|
||
}
|
||
|
||
const record = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null
|
||
const count = (value: unknown) => typeof value === 'number' && Number.isInteger(value) && value >= 0
|
||
const text = (value: unknown) => (typeof value === 'string' ? value : '')
|
||
|
||
export class ReconciliationRequestError extends Error {
|
||
status: number
|
||
code: string
|
||
constructor(message: string, status: number, code: string) {
|
||
super(message)
|
||
this.status = status
|
||
this.code = code
|
||
}
|
||
}
|
||
|
||
async function request(path: string, init: RequestInit, signal?: AbortSignal): Promise<unknown> {
|
||
const response = await fetch(path, {
|
||
...init,
|
||
headers: {
|
||
Accept: 'application/json',
|
||
...(init.body ? { 'Content-Type': 'application/json' } : {}),
|
||
...(init.headers || {}),
|
||
},
|
||
signal,
|
||
})
|
||
const payload = await response.json().catch(() => null)
|
||
if (!response.ok) {
|
||
const body = record(payload) ? payload : {}
|
||
throw new ReconciliationRequestError(
|
||
text(body.error) || `请求失败(HTTP ${response.status})`,
|
||
response.status,
|
||
text(body.code)
|
||
)
|
||
}
|
||
return payload
|
||
}
|
||
|
||
function parseRun(value: unknown): ReconciliationRun {
|
||
if (
|
||
!record(value) ||
|
||
typeof value.id !== 'string' ||
|
||
typeof value.kb_id !== 'string' ||
|
||
!['running', 'succeeded', 'failed'].includes(String(value.state)) ||
|
||
!['schedule', 'manual'].includes(String(value.trigger)) ||
|
||
typeof value.started_at !== 'string' ||
|
||
!count(value.ledger_count) ||
|
||
!count(value.kb_count) ||
|
||
!record(value.counts) ||
|
||
typeof value.window_overlapped !== 'boolean'
|
||
)
|
||
throw new Error('对账批次响应无效')
|
||
return value as unknown as ReconciliationRun
|
||
}
|
||
|
||
function parseFinding(value: unknown): ReconciliationFinding {
|
||
if (
|
||
!record(value) ||
|
||
typeof value.id !== 'string' ||
|
||
!['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8'].includes(String(value.type)) ||
|
||
!['independent', 'declared_only', 'none'].includes(String(value.evidence_level)) ||
|
||
!['confirmed', 'pending'].includes(String(value.confirmation_state)) ||
|
||
!['requeue_upload', 'remove_from_test', 'writeback_state', 'enter_removal', 'none'].includes(
|
||
String(value.suggested_action)
|
||
) ||
|
||
!['open', 'done'].includes(String(value.action_state))
|
||
)
|
||
throw new Error('对账差异响应无效')
|
||
return value as unknown as ReconciliationFinding
|
||
}
|
||
|
||
export async function fetchReconciliationRuns(
|
||
limit = 20,
|
||
offset = 0,
|
||
kbID = '',
|
||
signal?: AbortSignal
|
||
): Promise<ReconciliationRunPage> {
|
||
const query = new URLSearchParams({ limit: String(limit), offset: String(offset) })
|
||
if (kbID) query.set('kb_id', kbID)
|
||
const payload = await request(`/api/v1/reconciliation/runs?${query}`, { method: 'GET' }, signal)
|
||
if (!record(payload) || !record(payload.data) || !Array.isArray(payload.data.items))
|
||
throw new Error('对账批次列表响应无效')
|
||
return {
|
||
items: payload.data.items.map(parseRun),
|
||
total: Number(payload.data.total) || 0,
|
||
limit: Number(payload.data.limit) || limit,
|
||
offset: Number(payload.data.offset) || offset,
|
||
}
|
||
}
|
||
|
||
export async function fetchReconciliationFindings(
|
||
runID: string,
|
||
type = '',
|
||
q = '',
|
||
limit = 20,
|
||
offset = 0,
|
||
signal?: AbortSignal
|
||
): Promise<ReconciliationFindingPage> {
|
||
const query = new URLSearchParams({ limit: String(limit), offset: String(offset) })
|
||
if (type) query.set('type', type)
|
||
if (q) query.set('q', q)
|
||
const payload = await request(
|
||
`/api/v1/reconciliation/runs/${encodeURIComponent(runID)}/findings?${query}`,
|
||
{ method: 'GET' },
|
||
signal
|
||
)
|
||
if (!record(payload) || !record(payload.data) || !Array.isArray(payload.data.items))
|
||
throw new Error('对账差异列表响应无效')
|
||
return {
|
||
items: payload.data.items.map(parseFinding),
|
||
total: Number(payload.data.total) || 0,
|
||
limit: Number(payload.data.limit) || limit,
|
||
offset: Number(payload.data.offset) || offset,
|
||
}
|
||
}
|
||
|
||
export async function createReconciliationRun(kbID: string): Promise<ReconciliationRun> {
|
||
const payload = await request('/api/v1/reconciliation/runs', {
|
||
method: 'POST',
|
||
headers: { 'X-Governance-Action': 'reconciliation-action' },
|
||
body: JSON.stringify({ kb_id: kbID }),
|
||
})
|
||
if (!record(payload) || !record(payload.data)) throw new Error('触发对账响应无效')
|
||
return parseRun(payload.data)
|
||
}
|
||
|
||
async function findingAction(id: string, action: 'writeback' | 'requeue' | 'remove'): Promise<ReconciliationFinding> {
|
||
const payload = await request(`/api/v1/reconciliation/findings/${encodeURIComponent(id)}/${action}`, {
|
||
method: 'POST',
|
||
headers: { 'X-Governance-Action': 'reconciliation-action' },
|
||
})
|
||
if (!record(payload) || !record(payload.data)) throw new Error('对账处置响应无效')
|
||
return parseFinding(payload.data)
|
||
}
|
||
|
||
export const writebackFinding = (id: string) => findingAction(id, 'writeback')
|
||
export const requeueFinding = (id: string) => findingAction(id, 'requeue')
|
||
export const removeFinding = (id: string) => findingAction(id, 'remove')
|
||
|
||
export async function fetchReconciliationProgress(
|
||
documentID: string,
|
||
signal?: AbortSignal
|
||
): Promise<ReconciliationProgress> {
|
||
const payload = await request(
|
||
`/api/v1/reconciliation/documents/${encodeURIComponent(documentID)}/progress`,
|
||
{ method: 'GET' },
|
||
signal
|
||
)
|
||
if (!record(payload) || !record(payload.data) || typeof payload.data.document_id !== 'string')
|
||
throw new Error('WeKnora 处理详情响应无效')
|
||
return payload.data as unknown as ReconciliationProgress
|
||
}
|