口径工作台:结构化语义导入、起草续跑恢复与本体关系编排

- 新增口径结构化语义定义、分批解析与 SQL 生成管道(semantic / semantic_import / semantic_sql)
- 支持起草断点续跑恢复、指标自动匹配对齐与存量口径语义迁移面板(MigrationPanel / SemanticPanel)
- 口径草稿与变更流程接入候选本体边提议、审阅确认与一键修改保护,同步更新可移植 SQLite 基线校验
This commit is contained in:
ThearchyHelios
2026-11-12 12:58:12 +08:00
parent 8d9a712eae
commit e15e94f93e
28 changed files with 3376 additions and 137 deletions
+17
View File
@@ -87,6 +87,23 @@ export const caliberLedger = {
extractDraft: (body: { documents: string[]; collection?: string; datasource_id?: string; guidance?: string;
fixed_filters?: string }) =>
post<{ draft_id: string; status: string }>('/ledger/drafts/extract', body),
importDefinitions: (body: { documents: string[]; datasource_id?: string; guidance?: string; fixed_filters?: string }) =>
post<{ draft_id: string; status: string }>('/ledger/drafts/definitions', body),
implementDefinitions: (draftId: string, body: { selected?: string[]; datasource_id: string }) =>
post<{ draft_id: string; status: string }>(`${draftAt(draftId)}/implement`, body),
continueDefinitions: (draftId: string, body: {
expected_revision: number; guidance: string; selected?: string[]; datasource_id?: string
}) =>
post<{ draft_id: string; status: string; original_draft_id: string; merge_required: boolean }>(`${draftAt(draftId)}/continue`, body),
mergeContinuation: (draftId: string, body: { expected_revision: number; candidate_revision: number }) =>
post<CaliberDraft & { merged_candidate_id: string }>(`${draftAt(draftId)}/merge`, body),
migration: (draftId: string) => get<{
revision: number; metrics_total: number; metrics_needing_work: number; can_rebuild: boolean; note: string
metrics: { card_id: string; metric_id: string; name: string; query_count: number; guidance: string
findings: { code: string; message: string }[] }[]
}>(`${draftAt(draftId)}/migration`),
prepareMigration: (draftId: string, expected_revision: number) =>
post<{ draft_id: string; status: string }>(`${draftAt(draftId)}/migration`, { expected_revision }),
/**
* 改标题、整体换掉指标、或把答清的问题消掉(resolved 是问题原文)。服务端据此重算状态:还有问题 → questions_open,
* 否则 ready;已经接上或驳回的不改状态。
+133 -39
View File
@@ -4,7 +4,7 @@
* 主路径一次到位:选文档 → 模型读文档、对照数据目录产出定义 + 取数 + 入口字段 + 指标表达式 → 服务端校验后落成草稿图
* → 后台自动只读试运行 → 这里展示「定义 + 数 + 原文引用」→ 人去画布审改并发布。发布才需要人签字。
* - 发起:选知识库文档和**必选的数据源**(取数 SQL 只对照它的表和列写),可加一段起草要求;页面轮询详情直到状态离开
* drafting(最多十五分钟,指标多时服务端会分几轮续跑,进度写在 payload.progress);
* drafting(持续有进度时继续轮询,连续十五分钟无进展才暂停轮询;后台任务独立运行);
* - 试运行:草稿顶部显示这次自动试运行的状态和每个指标的值,running 时每两秒取一次,最多等一分钟;必填入口没有值时停在
* needs_input,在入口字段里填了再点「试运行」;失败照实写原因;
* - 审读:指标默认收成一行(名字、单位、值和未决问题数),展开看单位、粒度、指标表达式、定义和逐字核对过的引用原文
@@ -49,6 +49,9 @@ import { formatDateTime, formatTime } from '../../lib/format'
import { LEDGER_UI } from '../../lib/termsLedger'
import { useAtLeast } from '../../store/auth'
import { TrialResult, TrialValidation } from './TrialResult'
import { ContinuationPanel, DefinitionInventory, QuerySemanticEditor } from './SemanticPanel'
import { OntologyPanel } from './OntologyPanel'
import { MigrationPanel } from './MigrationPanel'
import type { DataSourceName, KbDocument, Run } from '../../types'
import type {
CaliberDraft, CaliberDraftStage, CaliberDraftStatus, CaliberDraftSummary, DraftAlignment, DraftBinding, DraftChangeReceipt,
@@ -517,6 +520,7 @@ export function CaliberDrafts() {
/** 这次自动试运行的结果;running 时单独轮询,和草稿抽取的轮询互不相干 */
const [trial, setTrial] = useState<DraftTrialOut | null>(null)
const trialRequestSeq = useRef(0)
const [trialPollId, setTrialPollId] = useState<string | null>(null)
const [trialTimedOut, setTrialTimedOut] = useState(false)
const [trialError, setTrialError] = useState<unknown>(null)
@@ -602,6 +606,7 @@ export function CaliberDrafts() {
const [expectSource, setExpectSource] = useState<DraftExpectationSource>('human_confirmed')
/** 验证记录先摆最近几条,其余收起来 */
const [recordsShown, setRecordsShown] = useState(RECORDS_SHOWN)
const [droppedExpanded, setDroppedExpanded] = useState(false)
const canReview = useAtLeast('builder')
const loadList = useCallback(() => {
@@ -706,6 +711,7 @@ export function CaliberDrafts() {
setExpectValues({})
setExpectSource('human_confirmed')
setRecordsShown(RECORDS_SHOWN)
setDroppedExpanded(false)
setImportRuns(null)
setImportsError(null)
setImportId(null)
@@ -768,28 +774,43 @@ export function CaliberDrafts() {
useEffect(() => {
if (!pollingId) return
const until = Date.now() + DRAFT_POLL_LIMIT_MS
let until = Date.now() + DRAFT_POLL_LIMIT_MS
let lastProgress = ''
let alive = true
const timer = setInterval(() => {
let settled = false
const seq = openSeq.current
let timer: ReturnType<typeof setTimeout>
const poll = () => {
api.caliberLedger.draft(pollingId).then((draft) => {
if (!alive) return
setDetail((cur) => (cur && cur.id === draft.id ? draft : cur))
if (!alive || seq !== openSeq.current) return
const currentProgress = JSON.stringify([draft.progress, draft.implementation_progress])
if (currentProgress !== lastProgress) {
lastProgress = currentProgress
until = Date.now() + DRAFT_POLL_LIMIT_MS
}
setDetail((cur) => (cur && cur.id === draft.id && (cur.revision ?? 0) <= (draft.revision ?? 0) ? draft : cur))
if (draft.status !== 'drafting') {
settled = true
setPollingId(null)
setPollTimedOut(false)
loadList()
} else if (Date.now() > until) {
settled = true
setPollingId(null)
setPollTimedOut(true)
loadList()
}
}, (e) => {
if (!alive) return
if (!alive || seq !== openSeq.current) return
settled = true
setPollingId(null)
setDetailError(e)
}).finally(() => {
if (alive && !settled && seq === openSeq.current) timer = setTimeout(poll, DRAFT_POLL_MS)
})
}, DRAFT_POLL_MS)
return () => { alive = false; clearInterval(timer) }
}
timer = setTimeout(poll, DRAFT_POLL_MS)
return () => { alive = false; clearTimeout(timer) }
}, [pollingId, loadList])
/** 草稿有了试运行就取一次结果;还在 running 就交给下面的轮询 */
@@ -801,14 +822,15 @@ export function CaliberDrafts() {
setTrial((cur) => (cur?.run_id === run && detail?.trial?.status !== 'stale' ? cur : {
run_id: run, status: detail?.trial?.status ?? 'running', metrics: [], error: detail?.trial?.error ?? '',
}))
const request = ++trialRequestSeq.current
api.caliberLedger.draftTrial(id).then((out) => {
if (!alive) return
if (!alive || request !== trialRequestSeq.current) return
setTrial(out)
setTrialError(null)
setTrialTimedOut(false)
if (out.status === 'running') setTrialPollId(id)
}, (e) => {
if (!alive) return
if (!alive || request !== trialRequestSeq.current) return
setTrialError(e)
setTrialPollId(null)
})
@@ -820,35 +842,44 @@ export function CaliberDrafts() {
if (!trialPollId) return
const until = Date.now() + 60_000
let alive = true
const timer = setInterval(() => {
let settled = false
const seq = openSeq.current
let timer: ReturnType<typeof setTimeout>
const poll = () => {
const request = ++trialRequestSeq.current
api.caliberLedger.draftTrial(trialPollId).then((out) => {
if (!alive) return
if (!alive || seq !== openSeq.current || request !== trialRequestSeq.current) return
setTrial(out)
setTrialError(null)
if (out.status === 'running') {
if (Date.now() > until) {
settled = true
setTrialPollId(null)
setTrialTimedOut(true)
}
return
}
settled = true
setTrialPollId(null)
setTrialTimedOut(false)
loadList()
// 试运行到终态时服务端会把这次跑完的验证记录补记一条:跟着取一次,结论按记录说
loadValidations(trialPollId)
const seq = openSeq.current
api.caliberLedger.draft(trialPollId).then((fresh) => {
if (seq !== openSeq.current) return
setDetail((cur) => (cur && cur.id === fresh.id ? fresh : cur))
setDetail((cur) => (cur && cur.id === fresh.id && (cur.revision ?? 0) <= (fresh.revision ?? 0) ? fresh : cur))
}, () => undefined)
}, (e) => {
if (!alive) return
if (!alive || seq !== openSeq.current || request !== trialRequestSeq.current) return
settled = true
setTrialPollId(null)
setTrialError(e)
}).finally(() => {
if (alive && !settled && seq === openSeq.current) timer = setTimeout(poll, 2000)
})
}, 2000)
return () => { alive = false; clearInterval(timer) }
}
timer = setTimeout(poll, 2000)
return () => { alive = false; clearTimeout(timer) }
}, [trialPollId, loadList, loadValidations])
/**
@@ -902,17 +933,17 @@ export function CaliberDrafts() {
return at >= 0 ? at + 1 : null
}
const submit = async () => {
const submit = async (definitions = true) => {
if (!pickedDocs.length) { toast.warn(D.needDoc); return }
setSubmitting(true)
try {
const out = await api.caliberLedger.extractDraft({
const out = await (definitions ? api.caliberLedger.importDefinitions : api.caliberLedger.extractDraft)({
documents: pickedDocs,
...(datasourceId ? { datasource_id: datasourceId } : {}),
...(guidance.trim() ? { guidance: guidance.trim() } : {}),
...(fixedFilters.trim() ? { fixed_filters: fixedFilters.trim() } : {}),
})
toast.ok(D.started)
toast.ok(definitions ? '定义清单已导入,可自动处理全部可实现指标' : D.started)
setPickedDocs([])
setGuidance('')
setFixedFilters('')
@@ -2668,14 +2699,38 @@ export function CaliberDrafts() {
}
const renderDetail = (draft: CaliberDraft) => {
const viewSeq = openSeq.current
const stage = stageOf(draft)
const drafting = draft.status === 'drafting'
const terminal = stage === 'discarded'
const stageNote = stage === 'review' ? D.stageNote(draft.status) : ''
const questions = draft.open_questions ?? []
const dropped = draft.dropped ?? []
const uncovered = draft.uncovered ?? []
const metrics = draft.metrics ?? []
const hasInventory = !!draft.semantic_inventory
const awaitingImplementation = hasInventory && !draft.workflow_id && metrics.length === 0
const stageNote = awaitingImplementation && draft.status !== 'path_pending' && !drafting
? D.stageNoteNeedsGraph
: stage === 'review' ? D.stageNote(draft.status) : ''
const rawQuestions = draft.open_questions ?? []
const questions = hasInventory
? rawQuestions.filter((q) =>
!q.includes('尚未生成可用实现,请补充字段映射或确认口径')
&& !(q.startsWith('另有 ') && q.includes('项指标尚未生成可用实现')))
: rawQuestions
const rawDropped = draft.dropped ?? []
const hasSpecificDropped = rawDropped.some((item) => item.name !== 'metrics')
const droppedMap = new Map<string, { name: string; reason: string; count: number }>()
for (const item of rawDropped) {
if (hasSpecificDropped && item.name === 'metrics' && item.reason === '没有一条指标通过校验') continue
const key = `${item.name}\u0000${item.reason}`
const existing = droppedMap.get(key)
if (existing) existing.count += 1
else droppedMap.set(key, { name: item.name, reason: item.reason, count: 1 })
}
const dropped = Array.from(droppedMap.values())
const DROPPED_LIMIT = 8
const visibleDropped = droppedExpanded || dropped.length <= DROPPED_LIMIT
? dropped
: dropped.slice(0, DROPPED_LIMIT)
const uncovered = draft.uncovered ?? []
const values = new Map((draft.trial?.status === 'stale' ? [] : trial?.metrics ?? []).map((row) => [row.id, row]))
// 第一轮还没落库时服务端写的是空对象:没有轮次就不画,别显示「第 undefined 轮」
const progress = draft.progress && typeof draft.progress.round === 'number' ? draft.progress : null
@@ -2700,7 +2755,8 @@ export function CaliberDrafts() {
{(drafting || draft.job?.retryable || (draft.status === 'failed' && !draft.workflow_id)) && (
<div className="flex gap-2">
{drafting && <button className="btn btn-sm" disabled={jobBusy === draft.id} onClick={() => void changeDraftJob(draft, 'stop')}>停止起草</button>}
{!drafting && <button className="btn btn-sm" disabled={jobBusy === draft.id} onClick={() => void changeDraftJob(draft, 'retry')}>重试起草</button>}
{!drafting && <button className="btn btn-sm" disabled={jobBusy === draft.id} onClick={() => void changeDraftJob(draft, 'retry')}>
{draft.implementation_progress ? '按原配置重试中断任务' : '重试起草'}</button>}
</div>
)}
{jobError != null && <ErrorState error={jobError} compact />}
@@ -2713,18 +2769,19 @@ export function CaliberDrafts() {
{drafting && pollingId === draft.id && (
<Notice tone="info">
<span className="inline-flex items-center gap-1.5"><Spinner size={12} /> {D.drafting}</span>
{progress && (
{progress && !draft.implementation_progress && (
<span className="tnum mt-0.5 block" data-draft-progress="">
{D.progress(progress.round, progress.covered, progress.uncovered)}
</span>
)}
<span className="mt-0.5 block text-faint">{D.draftingHint}</span>
<span className="mt-0.5 block text-faint">{draft.implementation_progress
? '系统会自动处理后续指标并保存进度,离开页面不影响执行。需补充的信息会单独列出。' : D.draftingHint}</span>
</Notice>
)}
{drafting && pollTimedOut && (
<Notice tone="warn">
{D.draftingTimeout}
{progress && (
{progress && !draft.implementation_progress && (
<span className="tnum mt-0.5 block" data-draft-progress="">
{D.progress(progress.round, progress.covered, progress.uncovered)}
</span>
@@ -2736,12 +2793,31 @@ export function CaliberDrafts() {
)}
{draft.catalog_truncated && <Notice tone="warn">{D.catalogTruncated}</Notice>}
<DefinitionInventory key={`definitions:${draft.id}`} draft={draft} sources={sources}
onStarted={id => { loadList(); if (viewSeq === openSeq.current) void open(id) }} />
<ContinuationPanel key={`continuation:${draft.id}`} draft={draft} sources={sources}
onOpened={id => { loadList(); if (viewSeq === openSeq.current) void open(id) }} />
<MigrationPanel key={`migration:${draft.id}:${draft.revision}`} draft={draft}
onPrepared={id => { loadList(); if (viewSeq === openSeq.current) void open(id) }} />
{renderTrial(draft)}
{renderRules(draft)}
{renderValidation(draft)}
{!awaitingImplementation && renderValidation(draft)}
{renderImports(draft)}
{renderNl(draft)}
{renderEntryFields(draft)}
{!awaitingImplementation && renderEntryFields(draft)}
{metrics.map(metric => <QuerySemanticEditor key={`${draft.id}:${metric.id}`} draft={draft}
metric={metric} onSaved={fresh => {
loadList()
if (viewSeq !== openSeq.current) return
setDetail(cur => cur?.id === fresh.id && (cur.revision ?? 0) <= (fresh.revision ?? 0) ? fresh : cur)
loadValidations(draft.id)
}} />)}
<OntologyPanel key={`ontology:${draft.id}`} draft={draft} onSaved={fresh => {
loadList()
if (viewSeq !== openSeq.current) return
setDetail(cur => cur?.id === fresh.id && (cur.revision ?? 0) <= (fresh.revision ?? 0) ? fresh : cur)
loadValidations(draft.id)
}} />
{renderSql(draft)}
<div>
@@ -2759,6 +2835,10 @@ export function CaliberDrafts() {
</div>
{metrics.length ? (
<ul className="space-y-2">{metrics.map((metric, index) => renderMetric(metric, index, values))}</ul>
) : !drafting && draft.semantic_inventory ? (
<p className="text-xs text-faint">
{draft.status === 'path_pending' ? '定义已保留,请在上方选择指标并绑定数据。' : D.inventoryAwaitingHint}
</p>
) : !drafting ? (
<EmptyState offline={false} icon={<ScrollText size={20} />} title={D.noMetrics} body={D.noMetricsBody} />
) : (
@@ -2769,6 +2849,9 @@ export function CaliberDrafts() {
{questions.length > 0 && (
<div data-draft-questions-all="">
<h3 className="mb-1 text-2xs font-semibold text-dim">{D.questionsOther}</h3>
{hasInventory && (
<p className="mb-1 text-2xs leading-relaxed text-faint">{D.questionsContinuationHint}</p>
)}
{questionList(questions, 'top')}
</div>
)}
@@ -2778,17 +2861,23 @@ export function CaliberDrafts() {
<h3 className="text-2xs font-semibold text-dim">{D.dropped}</h3>
<p className="mb-1 text-2xs leading-relaxed text-faint">{D.droppedHint}</p>
<ul className="space-y-0.5 text-2xs leading-relaxed">
{dropped.map((item, i) => (
{visibleDropped.map((item, i) => (
<li key={`${item.name}:${i}`} className="text-dim">
<span className="font-medium">{item.name}</span>
<span className="text-faint">:{item.reason}</span>
<span className="text-faint">:{item.reason}{item.count > 1 ? `(重复 ${item.count} 次)` : ''}</span>
</li>
))}
</ul>
{dropped.length > DROPPED_LIMIT && (
<button type="button" className="btn btn-xs btn-ghost mt-1"
onClick={() => setDroppedExpanded((cur) => !cur)}>
{droppedExpanded ? D.droppedLess : D.droppedMore(dropped.length - DROPPED_LIMIT)}
</button>
)}
</div>
)}
{uncovered.length > 0 && (
{!hasInventory && uncovered.length > 0 && (
<div data-draft-uncovered="">
<h3 className="text-2xs font-semibold text-dim">{D.uncovered(uncovered.length)}</h3>
<p className="mb-1 text-2xs leading-relaxed text-faint">{D.uncoveredHint}</p>
@@ -2809,7 +2898,7 @@ export function CaliberDrafts() {
</div>
)}
{!drafting && stage === 'review' && draft.status !== 'attached' && (
{!awaitingImplementation && !drafting && stage === 'review' && draft.status !== 'attached' && (
<details className="rounded-md border bg-elev px-3 py-2" data-draft-attach="">
<summary className="cursor-pointer text-2xs font-semibold text-dim">{D.attach}</summary>
<p className="mt-0.5 text-2xs leading-relaxed text-faint">{D.attachHint}</p>
@@ -2917,11 +3006,12 @@ export function CaliberDrafts() {
<div className="space-y-2">
<label className="block text-2xs text-faint">
<span className="mb-0.5 block font-semibold text-dim">{D.datasource}</span>
<select className="field w-full" value={datasourceId} required
<select className="field w-full" value={datasourceId}
onChange={(e) => setDatasourceId(e.target.value)}>
<option value="">导入定义时可暂不选数据源</option>
{sources.map((s) => <option key={s.id} value={s.id}>{s.name}</option>)}
</select>
<span className="mt-0.5 block">{D.datasourceHint}</span>
<span className="mt-0.5 block">先保留文档定义,选择指标后再绑定已探查的数据源。</span>
</label>
<label className="block text-2xs text-faint">
<span className="mb-0.5 block font-semibold text-dim">{D.guidance}</span>
@@ -2935,10 +3025,12 @@ export function CaliberDrafts() {
onChange={(e) => setFixedFilters(e.target.value)} />
<span className="mt-0.5 block">{D.fixedFiltersHint}</span>
</label>
<button type="button" className="btn btn-primary btn-sm" disabled={submitting || !datasourceId}
<button type="button" className="btn btn-primary btn-sm" disabled={submitting || !pickedDocs.length}
onClick={() => void submit()} data-draft-submit="">
{submitting ? <Spinner size={12} /> : <FileText size={12} aria-hidden />} {submitting ? D.submitting : D.submit}
{submitting ? <Spinner size={12} /> : <FileText size={12} aria-hidden />} {submitting ? '正在导入…' : '导入指标定义表'}
</button>
<button type="button" className="btn btn-ghost btn-sm ml-2" disabled={submitting || !datasourceId || !pickedDocs.length}
onClick={() => void submit(false)}>其他文档:AI 起草</button>
</div>
</div>
</section>
@@ -2986,7 +3078,9 @@ export function CaliberDrafts() {
{draft.error && <span className="text-2xs text-[var(--err)]">{D.draftingFailed}</span>}
</div>
<div className="mt-0.5 text-2xs text-faint">
{D.summary(draft.metric_count, draft.question_count)}
{draft.definition_count != null
? `${draft.definition_count} 项定义 · ${draft.metric_count} 项已实现 · ${draft.question_count} 个待确认问题`
: D.summary(draft.metric_count, draft.question_count)}
{draft.created_at ? ` · ${formatTime(draft.created_at)}` : ''}
</div>
</div>
@@ -0,0 +1,43 @@
import { useState } from 'react'
import { api } from '../../api/client'
import { ErrorState, Notice } from '../../components/ui'
import type { CaliberDraft } from '../../typesLedger'
type Audit = Awaited<ReturnType<typeof api.caliberLedger.migration>>
export function MigrationPanel({ draft, onPrepared }: { draft: CaliberDraft; onPrepared: (id: string) => void }) {
const [audit, setAudit] = useState<Audit | null>(null)
const [busy, setBusy] = useState(false)
const [error, setError] = useState<unknown>(null)
if (!draft.workflow_id) return null
const check = async () => {
setBusy(true); setError(null)
try { setAudit(await api.caliberLedger.migration(draft.id)) }
catch (e) { setError(e) } finally { setBusy(false) }
}
const prepare = async () => {
if (!audit) return
setBusy(true); setError(null)
try { onPrepared((await api.caliberLedger.prepareMigration(draft.id, audit.revision)).draft_id) }
catch (e) { setError(e) } finally { setBusy(false) }
}
return <section className="space-y-2 rounded-lg border p-3" data-migration-panel="">
<div className="flex items-center justify-between gap-2">
<h3 className="text-xs font-semibold">旧口径迁移检查</h3>
<button className="btn btn-sm" disabled={busy} onClick={() => void check()}>{busy ? '处理中…' : '检查问数与取数粒度'}</button>
</div>
<p className="text-xs text-dim">逐项检查明细误作标量、必填参数、对象与日期映射,以及本机数据源是否就绪。</p>
{error != null && <ErrorState error={error} />}
{audit && <>
<Notice tone="info">{audit.note}</Notice>
<p className="text-xs">共 {audit.metrics_total} 项,{audit.metrics_needing_work} 项需处理</p>
{audit.metrics.map(m => <details key={`${m.card_id}:${m.metric_id}`} className="rounded border p-2 text-xs">
<summary className="cursor-pointer">{m.name} · {m.findings.length ? `${m.findings.length} 项待处理` : '静态检查通过,待试运行'}</summary>
<ul className="my-2 list-disc space-y-1 pl-4">{m.findings.map((f, i) => <li key={i}>{f.message}</li>)}</ul>
<p className="text-dim">{m.guidance}</p>
</details>)}
{audit.can_rebuild && <button className="btn btn-sm" disabled={busy} onClick={() => void prepare()}>从原文准备独立迁移草稿</button>}
<p className="text-xs text-dim">只缺问数定义时可在下方补齐;取数粒度不符时,从原文分批重建、验证。新草稿验证并发布后,再切换调用口径。</p>
</>}
</section>
}
+498
View File
@@ -0,0 +1,498 @@
import { useEffect, useRef, useState } from 'react'
import { api } from '../../api/client'
import { ErrorState, Notice, toast } from '../../components/ui'
import type { DataSourceName } from '../../types'
import type { CaliberDraft, CanonicalEntity, DraftMetric, QuerySemantics } from '../../typesLedger'
const words = (value: string) => value.split(/[,,\n]/).map(s => s.trim()).filter(Boolean)
function AliasesInput({ values, onChange, className = 'field' }: {
values: string[]; onChange: (values: string[]) => void; className?: string
}) {
const serialized = values.join(',')
const [text, setText] = useState(serialized)
useEffect(() => setText(serialized), [serialized])
return <input className={className} aria-label="常用叫法(逗号分隔)" placeholder="常用叫法(逗号分隔)"
value={text} onChange={e => setText(e.target.value)} onBlur={() => onChange(words(text))} />
}
const isSyntheticUnresolved = (text: string) =>
text.includes('尚未生成可用实现,请补充字段映射或确认口径')
|| (text.startsWith('另有 ') && text.includes('项指标尚未生成可用实现'))
function resolveItemDiagnostic(
draft: CaliberDraft,
item: { id: string; name: string },
isImplemented: boolean,
): { kind: 'needs_info' | 'validation_failed'; reasons: string[]; questions: string[] } | null {
if (isImplemented) return null
const direct = draft.definition_diagnostics?.[item.id]
if (direct && ((direct.reasons?.length ?? 0) > 0 || (direct.questions?.length ?? 0) > 0)) {
const reasons = Array.from(new Set(direct.reasons ?? []))
const questions = Array.from(new Set(direct.questions ?? []))
return {
kind: direct.kind === 'validation_failed' || (reasons.length > 0 && questions.length === 0)
? 'validation_failed' : 'needs_info',
reasons,
questions,
}
}
const dropped = draft.dropped ?? []
const matchedReasons = Array.from(new Set(
dropped
.filter(d => d.reason && (d.name === item.name || (item.name && d.name.includes(item.name))))
.map(d => d.reason),
))
const sharedReasons = matchedReasons.length > 0 ? [] : Array.from(new Set(
dropped
.filter(d => d.reason && (d.name === 'sql' || d.name === 'metrics' || d.name === '重写这一组')
&& d.reason !== '没有一条指标通过校验')
.map(d => d.reason),
)).slice(0, 2)
const reasons = matchedReasons.length > 0 ? matchedReasons : sharedReasons
const questions = Array.from(new Set(
(draft.open_questions ?? []).filter(q => !isSyntheticUnresolved(q) && item.name && q.includes(item.name)),
))
if (!reasons.length && !questions.length) return null
return {
kind: reasons.length > 0 && questions.length === 0 ? 'validation_failed' : 'needs_info',
reasons: reasons.slice(0, 4),
questions: questions.slice(0, 4),
}
}
export function DefinitionInventory({ draft, sources, onStarted }: {
draft: CaliberDraft; sources: DataSourceName[]; onStarted: (draftId: string) => void
}) {
const [picked, setPicked] = useState<string[]>(draft.selected_definition_ids ?? [])
const [partial, setPartial] = useState(false)
const [source, setSource] = useState(draft.datasource_id ?? '')
const [filter, setFilter] = useState<'all' | 'unresolved' | 'implemented' | 'excluded'>('all')
const [expanded, setExpanded] = useState(false)
const [busy, setBusy] = useState(false)
const mounted = useRef(false)
useEffect(() => { mounted.current = true; return () => { mounted.current = false } }, [])
const [error, setError] = useState<unknown>(null)
useEffect(() => {
const available = sources.filter(s => s.enabled !== false)
if (!draft.datasource_id && !source && available.length === 1) setSource(available[0].id)
}, [sources, source, draft.datasource_id])
const catalog = draft.semantic_inventory
if (!catalog) return null
const canStart = draft.status === 'path_pending' && !draft.workflow_id
const implemented = new Set((draft.metrics ?? []).flatMap(m => [m.name, m.source_definition_id]))
const metrics = catalog.items.filter(i => i.kind === 'metric')
const eligible = metrics.filter(i => i.status !== 'unavailable')
const counts = new Map<string, number>()
eligible.forEach(i => counts.set(i.name, (counts.get(i.name) ?? 0) + 1))
const automatic = eligible.filter(i => counts.get(i.name) === 1)
const selectedCount = partial ? picked.length : automatic.length
const sourceAvailable = sources.some(s => s.id === source && s.enabled !== false)
const progress = draft.implementation_progress
const isItemImplemented = (item: typeof metrics[number]) =>
implemented.has(item.id) || (counts.get(item.name) === 1 && implemented.has(item.name))
const isItemExcluded = (item: typeof metrics[number]) =>
item.status === 'unavailable'
|| (counts.get(item.name) ?? 0) > 1
|| (!!draft.selected_definition_ids?.length && !draft.selected_definition_ids.includes(item.id))
const implementedItems = metrics.filter(isItemImplemented)
const excludedItems = metrics.filter(i => !isItemImplemented(i) && isItemExcluded(i))
const unresolvedItems = metrics.filter(i => !isItemImplemented(i) && !isItemExcluded(i))
const filteredMetrics = canStart || filter === 'all' ? metrics
: filter === 'unresolved' ? unresolvedItems
: filter === 'implemented' ? implementedItems : excludedItems
const COLLAPSE_LIMIT = 12
const visibleMetrics = canStart || expanded || filteredMetrics.length <= COLLAPSE_LIMIT
? filteredMetrics
: filteredMetrics.slice(0, COLLAPSE_LIMIT)
const start = async () => {
setBusy(true); setError(null)
try {
await api.caliberLedger.implementDefinitions(draft.id, { ...(partial ? { selected: picked } : {}), datasource_id: source })
toast.ok('已开始自动处理;进度会持续保存,需要补充的信息会单独列出')
if (mounted.current) onStarted(draft.id)
} catch (e) { setError(e) } finally { setBusy(false) }
}
const noticeTone = !canStart && draft.status !== 'drafting' && progress && progress.completed === 0 && progress.blocked > 0
? 'warn' : 'info'
const noticeText = canStart
? '默认自动处理全部可实现指标,保存进度并继续处理后续项;缺少来源或定义冲突的指标会单独列出。生成后仍需验证口径和计算结果。'
: draft.status === 'drafting'
? '正在对照数据源分批生成取数实现并自动保存进度;未通过校验或缺少字段映射的指标会记录具体原因。'
: progress && progress.completed === 0 && progress.blocked > 0
? `本轮尚未生成可用的取数实现(共 ${progress.blocked} 项待处理)。请查看下方「继续处理未完成指标」中的待补充要点,填写来源表名、字段映射或固定条件后继续生成;也可先勾选少量核心指标先行验证。`
: progress && progress.blocked > 0
? `已生成 ${progress.completed} 项指标实现,仍有 ${progress.blocked} 项待补充信息或未通过校验。可在下方「继续处理未完成指标」补充说明后生成独立候选,核对无误再合并回原稿。`
: '所选指标已生成取数实现;请在下方核对试运行结果、完成验证并发布。'
return <section className="space-y-3 rounded-lg border p-3" data-semantic-inventory="">
<h3 className="text-xs font-semibold">指标定义清单 · {metrics.length} 项</h3>
<Notice tone={noticeTone}>{noticeText}</Notice>
{progress && <div className="space-y-1 text-xs" role="status" data-implementation-progress="">
<p>本次处理 {progress.total} 项:已生成 {progress.completed} 项,待处理 {progress.pending} 项,需补充信息 {progress.blocked} 项</p>
<progress aria-label="指标实现进度" className="w-full" max={Math.max(1, progress.total)} value={progress.completed} />
{draft.status === 'drafting' && !!progress.active_names.length && <p className="text-dim">正在处理:{progress.active_names.join('、')}</p>}
{!!draft.definition_issues?.length && <p className="text-dim">另有 {draft.definition_issues.length} 项因来源缺失或定义冲突暂未自动处理,详见清单。</p>}
</div>}
{canStart && <div className="flex flex-wrap items-center justify-between gap-2 text-xs">
<span>{partial ? `已选择 ${picked.length} 项` : `将自动处理 ${automatic.length} 项`}</span>
<button className="btn btn-sm" disabled={busy} onClick={() => setPartial(!partial)}>
{partial ? '恢复处理全部指标' : '仅处理部分指标'}
</button>
</div>}
{canStart && <div className="flex flex-wrap items-center gap-2">
<label className="text-xs">用于实现的数据源 <select className="field" value={source} onChange={e => setSource(e.target.value)}>
<option value="">请选择已探查的数据源</option>
{sources.filter(s => s.enabled !== false).map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
</select></label>
<button className="btn btn-primary btn-sm" disabled={busy || !sourceAvailable || !selectedCount} onClick={() => void start()}>
{busy ? '正在提交…' : partial ? `开始处理选中的 ${selectedCount} 项` : '自动处理全部可实现指标'}
</button>
</div>}
{!canStart && metrics.length > 6 && (
<div className="flex flex-wrap items-center gap-1.5 text-2xs" role="group" aria-label="按实现状态筛选定义">
{([
['all', `全部(${metrics.length})`, true],
['unresolved', `待补充或未过(${unresolvedItems.length})`, unresolvedItems.length > 0],
['implemented', `已有实现(${implementedItems.length})`, implementedItems.length > 0],
['excluded', `暂不处理(${excludedItems.length})`, excludedItems.length > 0],
] as const).filter(([, , show]) => show).map(([key, label]) => (
<button key={key} type="button"
className={`btn btn-xs ${filter === key ? 'btn-primary' : 'btn-ghost'}`}
onClick={() => { setFilter(key); setExpanded(false) }}>
{label}
</button>
))}
</div>
)}
{!!catalog.warnings?.length && <Notice tone="warn">{catalog.warnings.map((w, i) => <p key={i}>第 {w.line} 行:{w.reason}。{w.quote}</p>)}</Notice>}
<div className="space-y-2">
{visibleMetrics.map(item => {
const itemDone = isItemImplemented(item)
const diagnostic = resolveItemDiagnostic(draft, item, itemDone)
const statusText = item.status === 'unavailable' ? '原文注明无数据源'
: itemDone ? '已有实现 · 待核验'
: (counts.get(item.name) ?? 0) > 1 ? '同名定义 · 需确认口径'
: draft.selected_definition_ids?.length && !draft.selected_definition_ids.includes(item.id) ? '未纳入本次处理'
: draft.status === 'drafting' ? '待处理'
: diagnostic?.kind === 'validation_failed' ? '校验未通过 · 待补充或重试'
: progress ? '未完成 · 需补充信息' : '待实现'
return <details key={item.id} className="rounded border p-2 text-xs">
<summary className="cursor-pointer">
{canStart && partial && <input type="checkbox" className="mr-2" aria-label={`选择 ${item.name}`}
disabled={busy || item.status === 'unavailable'} checked={picked.includes(item.id)}
onClick={e => e.stopPropagation()}
onChange={e => setPicked(e.target.checked ? [...picked, item.id] : picked.filter(id => id !== item.id))} />}
{item.name}
<span className={`ml-2 ${diagnostic ? 'text-[var(--warn)]' : 'text-faint'}`}>{statusText}</span>
</summary>
<dl className="mt-2 space-y-1 whitespace-pre-wrap">
{(draft.definition_issues ?? []).filter(i => i.definition_id === item.id).map(i => (
<p key={i.definition_id} className="text-[var(--warn)]">{i.reason}</p>
))}
{diagnostic && (
<div className="rounded bg-elev px-2 py-1.5 text-2xs">
{diagnostic.questions.map((q, idx) => (
<p key={`q:${idx}`} className="text-[var(--warn)]">待确认:{q}</p>
))}
{diagnostic.reasons.map((r, idx) => (
<p key={`r:${idx}`} className="text-dim">未通过原因:{r}</p>
))}
</div>
)}
{Object.entries(item.fields).map(([name, value]) => <div key={name}><dt className="inline font-semibold">{name}:</dt><dd className="inline">{value || '未说明'}</dd></div>)}
{!!item.dependencies.length && <div>依赖:{item.dependencies.join('、')}</div>}
</dl>
<details className="mt-2 text-faint"><summary>原文 · 第 {item.source.line} 行</summary><pre className="whitespace-pre-wrap">{item.source.quote}</pre></details>
</details>
})}
{!canStart && filteredMetrics.length > COLLAPSE_LIMIT && (
<button type="button" className="btn btn-xs btn-ghost"
onClick={() => setExpanded(cur => !cur)}>
{expanded ? '收起定义列表' : `展开其余 ${filteredMetrics.length - COLLAPSE_LIMIT} 项定义`}
</button>
)}
</div>
<details className="text-xs"><summary className="cursor-pointer">基础量与全局规则(随选中指标一起用于实现)</summary>
<div className="mt-2 space-y-2 whitespace-pre-wrap">
{catalog.items.filter(i => i.kind === 'input').map(i => <p key={i.id}><b>{i.name}</b>:{i.formula};{i.policy}</p>)}
{catalog.rules.map((r, i) => <p key={i}>{r.text}</p>)}
</div>
</details>
{error != null && <ErrorState error={error} compact />}
</section>
}
export function QuerySemanticEditor({ draft, metric, onSaved }: {
draft: CaliberDraft; metric: DraftMetric; onSaved: (fresh: CaliberDraft) => void
}) {
const [value, setValue] = useState<QuerySemantics>(metric.semantic ?? {
version: 1, enabled: false, aliases: metric.synonyms ?? [], grain: metric.grain ?? '', aggregation: '', time: null, entities: [],
})
const [busy, setBusy] = useState(false)
const [open, setOpen] = useState(false)
const [canonicalEntities, setCanonicalEntities] = useState<CanonicalEntity[]>([])
const mounted = useRef(false)
useEffect(() => { mounted.current = true; return () => { mounted.current = false } }, [])
useEffect(() => {
if (!open) return
void api.ontology.listEntities().then((res) => {
if (mounted.current) setCanonicalEntities(res.items ?? [])
}).catch(() => {})
}, [open])
const [error, setError] = useState<unknown>(null)
const pending = useRef({ signature: '', id: '' })
const [dirty, setDirty] = useState(false)
const serverValue = JSON.stringify(metric.semantic ?? {
version: 1, enabled: false, aliases: metric.synonyms ?? [], grain: metric.grain ?? '', aggregation: '', time: null, entities: [],
})
const editingBase = useRef(serverValue)
const conflict = dirty && editingBase.current !== serverValue
useEffect(() => {
if (!dirty) {
editingBase.current = serverValue
setValue(JSON.parse(serverValue) as QuerySemantics)
}
}, [serverValue, dirty])
const fields = draft.entry_fields ?? []
if (!metric.id || !draft.card_id || draft.revision == null) return null
const patch = (change: Partial<QuerySemantics>) => {
if (!dirty) editingBase.current = serverValue
setDirty(true); setValue(current => ({ ...current, ...change }))
}
const save = async () => {
if (conflict) return
const signature = JSON.stringify({ revision: draft.revision, value })
if (pending.current.signature !== signature) pending.current = {
signature, id: globalThis.crypto?.randomUUID?.() ?? `semantic-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`,
}
setBusy(true); setError(null)
try {
const fresh = await api.caliberLedger.patchDraft(draft.id, {
operations: [{ op: 'set', metric_id: metric.id!, path: '/semantic', value }], expected_revision: draft.revision!,
request_id: pending.current.id, target: { workflow_id: draft.workflow_id ?? undefined, card_id: draft.card_id! },
})
if (!mounted.current) return
setDirty(false)
onSaved(fresh)
toast.ok('问数定义已保存;验证并发布该版本后生效')
pending.current = { signature: '', id: '' }
} catch (e) { setError(e) } finally { setBusy(false) }
}
const selectField = (label: string, current: string, update: (s: string) => void) => <label className="text-xs">{label}
<select className="field ml-1" value={current} onChange={e => update(e.target.value)}>
<option value="">选择入口参数</option>
{fields.map(f => <option key={f.name} value={f.name}>{f.description || f.name}({f.name})</option>)}
</select>
</label>
return <details className="rounded border p-2 text-xs" data-query-semantics=""
onToggle={e => setOpen((e.currentTarget as HTMLDetailsElement).open)}>
<summary className="cursor-pointer font-semibold">{metric.name} · 问数定义{metric.semantic?.enabled ? '(已启用)' : '(待确认)'}</summary>
<fieldset disabled={busy} className="mt-3 space-y-3">
<p className="text-faint">这里定义常用问法、日期和对象如何对应已验证的取数参数。汇总规则应与取数实现一致;改动后须重新验证和发布。</p>
<label className="block">常用叫法(逗号分隔)<AliasesInput className="field w-full" values={value.aliases} onChange={aliases => patch({ aliases })} /></label>
<label className="block">输出粒度<input className="field w-full" placeholder="例如:指定产线、指定生产日的单值" value={value.grain} onChange={e => patch({ grain: e.target.value })} /></label>
<label className="block">汇总与缺失规则<textarea className="field w-full" placeholder="例如:按实际开班时长加权;缺失值处理按原文定义" value={value.aggregation} onChange={e => patch({ aggregation: e.target.value })} /></label>
<label className="block"><input type="checkbox" checked={value.time != null} onChange={e => patch({ time: e.target.checked
? { start: '', end: '', timezone: 'Asia/Shanghai', basis: 'calendar_day', periods: ['day'], description: '' } : null })} /> 按日期查询</label>
{value.time && <div className="space-y-2 rounded border p-2">
{selectField('开始(含)', value.time.start, start => patch({ time: { ...value.time!, start } }))}{' '}
{selectField('结束(不含)', value.time.end, end => patch({ time: { ...value.time!, end } }))}
<label className="block">业务时区<input className="field ml-1" value={value.time.timezone} onChange={e => patch({ time: { ...value.time!, timezone: e.target.value } })} /></label>
<label className="block">日期归属<select className="field ml-1" value={value.time.basis} onChange={e => patch({ time: { ...value.time!, basis: e.target.value as 'calendar_day' | 'production_day' } })}>
<option value="calendar_day">自然日</option><option value="production_day">生产日(按已实现的班次规则)</option>
</select></label>
<div>支持的查询周期:{(['day', 'month'] as const).map(period => <label key={period} className="ml-2">
<input type="checkbox" checked={(value.time!.periods ?? ['day']).includes(period)}
onChange={e => patch({ time: { ...value.time!, periods: e.target.checked
? [...(value.time!.periods ?? ['day']), period] : (value.time!.periods ?? ['day']).filter(p => p !== period) } })} />
{period === 'day' ? '单日' : '整月'}
</label>)}</div>
<label className="block">日期规则说明<input className="field w-full" value={value.time.description} onChange={e => patch({ time: { ...value.time!, description: e.target.value } })} /></label>
</div>}
{value.entities.map((entity, index) => {
const edit = (change: Partial<typeof entity>) => patch({ entities: value.entities.map((e, i) => i === index ? { ...e, ...change } : e) })
return <div key={index} className="space-y-2 rounded border p-2">
<label>对象类型 <input className="field" placeholder="工厂 / 产线" value={entity.label} onChange={e => edit({ label: e.target.value })} /></label>{' '}
{selectField('绑定参数', entity.field, field => edit({ field }))}{' '}
<label>标准实体桥 <select className="field ml-1" value={entity.entity_key ?? ''}
onChange={e => edit({ entity_key: e.target.value || null })} data-semantic-entity-key={index}>
<option value="">未关联统一实体</option>
{canonicalEntities.map(ce => <option key={ce.key} value={ce.key}>{ce.label}({ce.key})</option>)}
</select></label>
<p className="text-faint">只填写已核实的对象。工厂编号不能当作产线编号。</p>
{entity.members.map((member, n) => <div key={n} className="flex flex-wrap gap-1">
{(['value', 'label'] as const).map(key => <input key={key} className="field" aria-label={`${entity.label} ${key}`}
placeholder={{ value: '参数值', label: '对象名称' }[key]} value={member[key]}
onChange={e => edit({ members: entity.members.map((m, j) => j === n ? { ...m, [key]: e.target.value } : m) })} />)}
<AliasesInput values={member.aliases} onChange={aliases => edit({ members: entity.members.map((m, j) => j === n ? { ...m, aliases } : m) })} />
<button type="button" className="btn btn-xs" onClick={() => edit({ members: entity.members.filter((_, j) => j !== n) })}>移除对象</button>
</div>)}
<button type="button" className="btn btn-xs" onClick={() => edit({ members: [...entity.members, { value: '', label: '', aliases: [] }] })}>添加对象</button>{' '}
<button type="button" className="btn btn-xs" onClick={() => patch({ entities: value.entities.filter((_, i) => i !== index) })}>移除此类对象</button>
</div>
})}
<button type="button" className="btn btn-xs" onClick={() => patch({ entities: [...value.entities, { field: '', label: '', members: [] }] })}>添加对象类型</button>
<label className="block"><input type="checkbox" checked={value.enabled} onChange={e => patch({ enabled: e.target.checked })} /> 已确认上述定义与取数实现一致,发布后启用直接问数</label>
{conflict && <Notice tone="warn">
此指标的问数定义已被更新。你的修改仍保留,请先载入最新配置后再编辑,避免覆盖他人的修改。
<button type="button" className="btn btn-sm mt-2" onClick={() => {
setValue(JSON.parse(serverValue) as QuerySemantics); editingBase.current = serverValue; setDirty(false); setError(null)
}}>载入最新配置(放弃本地修改)</button>
</Notice>}
{dirty && <p role="status" className="text-faint">有未保存的修改;保存其他指标不会覆盖这里的内容。</p>}
<button type="button" className="btn btn-sm" disabled={conflict} onClick={() => void save()}>{busy ? '正在保存…' : '保存问数定义'}</button>
</fieldset>
{error != null && <ErrorState error={error} compact />}
</details>
}
/** Unfinished definitions are prepared separately, then merged with revision checks. */
export function ContinuationPanel({ draft, sources, onOpened }: {
draft: CaliberDraft; sources?: DataSourceName[]; onOpened: (id: string) => void
}) {
const [guidance, setGuidance] = useState('')
const [subsetMode, setSubsetMode] = useState(false)
const [pickedMissing, setPickedMissing] = useState<string[]>([])
const [pickedSource, setPickedSource] = useState(draft.datasource_id ?? '')
const [busy, setBusy] = useState(false)
const [error, setError] = useState<unknown>(null)
const mounted = useRef(false)
useEffect(() => { mounted.current = true; return () => { mounted.current = false } }, [])
const origin = draft.origin?.continuation_from
const implemented = new Set((draft.metrics ?? []).map(metric => metric.source_definition_id).filter(Boolean))
const missing = (draft.semantic_inventory?.items ?? []).filter(item => item.kind === 'metric'
&& item.status !== 'unavailable' && !implemented.has(item.id)
&& draft.selected_definition_ids?.includes(item.id))
const available = draft.status !== 'drafting' && draft.status !== 'rejected' && draft.status !== 'attached'
const canContinue = available && draft.status !== 'path_pending' && !origin
&& (!!draft.workflow_id || !!draft.implementation_progress || draft.status === 'questions_open' || draft.status === 'failed')
&& missing.length > 0
if (!origin && !canContinue) return null
const unavailableNames = new Set(
(draft.definition_issues ?? []).map(i => i.name).filter(Boolean),
)
const realQuestions = (draft.open_questions ?? []).filter(q =>
!isSyntheticUnresolved(q) && !Array.from(unavailableNames).some(name => q.startsWith(`${name}:`)),
)
const fallbackReasons = realQuestions.length > 0 ? [] : Array.from(new Set([
...Object.values(draft.definition_diagnostics ?? {}).flatMap(d => [...(d.questions ?? []), ...(d.reasons ?? [])]),
...(draft.dropped ?? [])
.filter(d => d.reason && d.reason !== '没有一条指标通过校验')
.map(d => d.name && d.name !== 'sql' && d.name !== 'metrics' ? `${d.name}:${d.reason}` : d.reason),
])).slice(0, 4)
const promptHints = (realQuestions.length > 0 ? realQuestions : fallbackReasons).slice(0, 4)
const enabledSources = (sources ?? []).filter(s => s.enabled !== false)
const canSwitchSource = !draft.workflow_id && enabledSources.length > 0
const fillTemplate = () => {
const lines = [
'来源表与关联键:',
'字段映射说明:',
'固定过滤条件:',
...(promptHints.length > 0 ? [`补充确认:${promptHints.slice(0, 2).join(';')}`] : []),
]
setGuidance(cur => cur.trim() ? `${cur.trim()}\n${lines.join('\n')}` : lines.join('\n'))
}
const toggleSubset = () => {
if (!subsetMode) {
setPickedMissing(cur => cur.length > 0 ? cur : missing.slice(0, Math.min(5, missing.length)).map(i => i.id))
setSubsetMode(true)
} else {
setSubsetMode(false)
}
}
const act = async (merge: boolean) => {
setBusy(true); setError(null)
try {
let id: string
if (merge && origin) {
const original = await api.caliberLedger.draft(origin.draft_id)
if (!mounted.current) return
const fresh = await api.caliberLedger.mergeContinuation(draft.id, {
expected_revision: original.revision ?? 0, candidate_revision: draft.revision ?? 0,
})
id = fresh.id
toast.ok('已将补充指标合并到原稿;请重新验证并发布')
} else {
const out = await api.caliberLedger.continueDefinitions(draft.id, {
expected_revision: draft.revision ?? 0,
guidance: guidance.trim(),
...(subsetMode ? { selected: pickedMissing } : {}),
...(canSwitchSource && pickedSource && pickedSource !== draft.datasource_id ? { datasource_id: pickedSource } : {}),
})
id = out.draft_id
toast.ok('已开始准备未完成指标,原稿保留')
}
if (mounted.current) onOpened(id)
} catch (e) { if (mounted.current) setError(e) }
finally { if (mounted.current) setBusy(false) }
}
return <section className="space-y-2 rounded-lg border p-3 text-xs" data-draft-continuation="">
{origin ? <>
<h3 className="font-semibold">补充指标候选</h3>
<p className="text-dim">{origin.merge_required
? '原稿和已有修改已保留。候选完成后可合并新增指标;发生定义冲突时会保留两份草稿供修正。合并后须重新验证。'
: '补充指标已合并到原稿,请在原稿中继续验证与发布。'}</p>
<button type="button" className="btn btn-sm" disabled={busy} onClick={() => onOpened(origin.draft_id)}>查看原稿</button>{' '}
{origin.merge_required && <button type="button" className="btn btn-primary btn-sm"
disabled={busy || !available || !draft.workflow_id || !draft.metrics?.length}
onClick={() => void act(true)}>{busy ? '正在合并…' : '合并补充指标到原稿'}</button>}
</> : <>
<div className="flex flex-wrap items-center justify-between gap-2">
<h3 className="font-semibold">继续处理未完成指标 · {missing.length} 项</h3>
{missing.length > 1 && (
<button type="button" className="btn btn-xs" disabled={busy} onClick={toggleSubset}>
{subsetMode ? '恢复处理全部未完成指标' : '仅先验证部分未完成指标'}
</button>
)}
</div>
<p className="text-dim">{draft.workflow_id
? '补充来源或字段说明后,只处理缺少的指标。已有实现和人工修改保留,生成结果会先放入独立候选,随后可合并回原稿。'
: '本轮尚未生成取数图。补充来源表、字段映射或固定条件后,系统将生成独立候选,完成后可一键接受进当前草稿。'}</p>
{promptHints.length > 0 && (
<div className="space-y-1 rounded border bg-elev px-2.5 py-2 text-2xs">
<div className="flex flex-wrap items-center justify-between gap-2">
<span className="font-semibold text-dim">待补充要点</span>
<button type="button" className="btn btn-xs btn-ghost" disabled={busy} onClick={fillTemplate}>
填入说明模板
</button>
</div>
<ul className="list-disc space-y-0.5 pl-4 text-dim">
{promptHints.map((hint, idx) => <li key={idx}>{hint}</li>)}
</ul>
</div>
)}
{canSwitchSource && (
<label className="block">用于生成的数据源{' '}
<select className="field ml-1" value={pickedSource} disabled={busy}
onChange={e => setPickedSource(e.target.value)}>
<option value="">请选择已探查的数据源</option>
{enabledSources.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
</select>
</label>
)}
{subsetMode && (
<div className="max-h-40 space-y-1 overflow-y-auto rounded border bg-elev p-2 text-2xs">
<p className="text-faint">先勾选少量核心指标先行验证(已选 {pickedMissing.length} 项):</p>
{missing.map(item => (
<label key={item.id} className="flex items-center gap-1.5">
<input type="checkbox" disabled={busy} checked={pickedMissing.includes(item.id)}
onChange={e => setPickedMissing(e.target.checked
? [...pickedMissing, item.id]
: pickedMissing.filter(id => id !== item.id))} />
<span>{item.name}</span>
</label>
))}
</div>
)}
<label className="block">补充说明<textarea className="field mt-1 w-full" value={guidance} disabled={busy}
maxLength={2000} placeholder="例如:OEE 基础数据在 OWODS.vi_ike_iot_kpi_info,不合格数在 OWODS.vi_ike_mes_bhgsj"
onChange={e => setGuidance(e.target.value)} /></label>
<button type="button" className="btn btn-primary btn-sm"
disabled={busy || (subsetMode && pickedMissing.length === 0) || (canSwitchSource && !pickedSource)}
onClick={() => void act(false)}>
{busy ? '正在提交…' : subsetMode ? `继续处理选中的 ${pickedMissing.length} 项` : '继续处理未完成指标'}</button>
</>}
{error != null && <ErrorState error={error} compact />}
</section>
}