独立语义指标库与文档一键拆解:支持多表参数化 SQL 模板、后台进度与 Oracle 方言

- 新增独立语义指标模型(SemanticMetric)与 metric_query 内置工具,同时支持单表聚合表达式(expression)与跨表 JOIN / CTE 参数化模板(sql_template)
- 新增后台口径文档一键拆解任务(SemanticDecomposeJob),支持实时阶段进度、转后台执行、中途取消,串联知识库红线归档、盲列数据目录补丁、指标试算与标准实体绑定四层资产一键落库
- 完善多数据库方言确定性编译(重点补齐 Oracle 顶层 WITH + ROWNUM 限流、标识符引号规整、DATE 字面量与 TO_CHAR 时间粒度转换)
This commit is contained in:
ThearchyHelios
2026-11-12 12:58:24 +08:00
parent e15e94f93e
commit f144ef16d2
14 changed files with 6078 additions and 145 deletions
+3
View File
@@ -42,6 +42,7 @@ import { blueprints, files, personal, templates } from './blueprints'
// 问数据的一轮在服务端跑(编排完善第 1 波):发起、看、停,实时流在 chatTurns.ts 的 streamChatTurn
import { chatTurns } from './chatTurns'
import { ontology } from './ontology'
import { semanticMetrics } from './semanticMetrics'
// 请求层搬到了 http.ts(见那边的说明)。原先从这里导出的照旧从这里导出,调用方不用改;
// 请求函数也一并转出,给不在 api/ 下、又想直接发请求的地方用。api/ 下的命名空间文件请直接从 http.ts 导入
@@ -1009,6 +1010,8 @@ export const api = {
chatTurns,
// 业务本体:标准业务实体与跨口径指标拓扑
ontology,
// 独立语义指标库(SemanticMetric):即存即生效指标卡片、在线试算与智能提取
semanticMetrics,
}
/**
+283
View File
@@ -0,0 +1,283 @@
/**
* 独立语义指标库(SemanticMetric)与后台文档一键拆解工作流 REST 客户端(服务端 backend/app/api/semantic_metrics.py)。
*
* 与工作流受管口径(caliberLedger)互补:
* - 单表模式(expression):绑定「数据源 + 物理表 + 单表聚合 SQL 表达式」;
* - 多表 / 参数化模式(sql_template):支持跨表 JOIN、WITH CTE 预聚合与 {{select_dims}}、{{and_where}}、{{period_days}} 等安全槽位;
* - 后台一键拆解任务(decomposeJobs):喂入业务文档后在后台分阶段拆解「字段目录补丁 + 单表/多表指标口径 + 标准业务实体 + 知识库归档」。
*/
import { del, get, post, put, qs, upload, type RequestOptions, type UploadOptions } from './http'
export type MetricCalcMode = 'expression' | 'sql_template'
export interface SemanticMetricDimension {
key: string
label: string
column: string
entity_key?: string
}
export interface SemanticMetric {
id: string
key: string
name: string
aliases: string[]
description: string
unit: string
source_id: string
source_name?: string
calc_mode?: MetricCalcMode | string
table_name: string
sql_expression: string
sql_template?: string
aggregation_type: string
date_column: string
filter_sql: string
dimensions: SemanticMetricDimension[]
default_time_grain: string
status: 'enabled' | 'disabled' | 'draft' | string
source: 'human' | 'ai' | 'doc' | string
version: number
created_by?: string | null
updated_by?: string | null
created_at?: string | null
updated_at?: string | null
}
export interface SemanticMetricInput {
key: string
name: string
aliases?: string[]
description?: string
unit?: string
source_id: string
calc_mode?: MetricCalcMode | string
table_name?: string
sql_expression?: string
sql_template?: string
date_column?: string
filter_sql?: string
dimensions?: SemanticMetricDimension[]
default_time_grain?: string
status?: string
source?: string
expected_version?: number
exists_in_db?: boolean
}
export interface SemanticMetricPreviewIn {
metric_id?: string
definition?: Record<string, unknown>
date_from?: string | null
date_to?: string | null
group_by_date?: boolean
time_grain?: string | null
group_by?: string[]
filters?: Record<string, unknown>
limit?: number
}
export interface SemanticMetricPreviewOut {
sql: string
source_name: string
columns: string[]
rows: unknown[][]
row_count: number
truncated: boolean
elapsed_ms: number
entity_alignments: Record<string, unknown>[]
latest_available_date?: string | null
}
export interface SemanticMetricSuggestOut {
source_id: string
source_name: string
model_id?: string | null
documents_used: string[]
candidates: SemanticMetricInput[]
skipped: { key: string; reason: string }[]
}
export interface DecomposeAttachmentOut {
attachment_id: string
title: string
mime: string
char_count: number
chunk_count: number
saved_to_kb: boolean
document_id?: string | null
collection?: string | null
}
export interface SemanticDecomposeStep {
key: string
label: string
status: 'pending' | 'running' | 'succeeded' | 'skipped' | 'failed' | string
detail: string
count: number
elapsed_ms: number
}
export interface SemanticCatalogChangeItem {
id: string
table: string
path: string
slot_label: string
before: unknown
after: unknown
value: unknown
reason: string
state: string
}
export interface SemanticCatalogPatchTable {
table: string
if_version: number
changes: SemanticCatalogChangeItem[]
}
export interface SemanticEntityChange {
id: string
op: string
entity_key: string
entity_label: string
summary: string
before: unknown
after: unknown
reason: string
op_payload: Record<string, unknown>
}
export interface SemanticGuardrailNote {
title: string
rule: string
category: string
}
export interface SemanticDecomposeAppliedRecord {
applied_at: string
applied_by?: string | null
catalog_applied_count: number
catalog_errors: string[]
metrics_created_count: number
metrics_updated_count: number
metric_errors: { key: string; reason: string }[]
entities_applied_count: number
entity_errors: string[]
}
export interface SemanticDecomposeResult {
model_id?: string | null
kb_documents?: { id: string; title: string; created_now: boolean }[]
guardrail_notes?: SemanticGuardrailNote[]
catalog_patches?: SemanticCatalogPatchTable[]
catalog_skipped?: string[]
metric_candidates?: SemanticMetricInput[]
metric_skipped?: { key: string; reason: string }[]
entity_changes?: SemanticEntityChange[]
entity_skipped?: string[]
applied?: SemanticDecomposeAppliedRecord | null
}
export interface SemanticDecomposeJob {
id: string
status: 'queued' | 'running' | 'succeeded' | 'failed' | 'cancelled' | string
source_id: string
source_name: string
scopes: string[]
document_ids: string[]
attachment_ids: string[]
doc_title: string
has_doc_text: boolean
save_to_kb: boolean
note: string
phase: string
phase_label: string
progress: number
steps: SemanticDecomposeStep[]
summary: Record<string, unknown>
result?: SemanticDecomposeResult
error?: string | null
started_by?: string | null
started_at?: string | null
finished_at?: string | null
created_at?: string | null
updated_at?: string | null
}
export interface SemanticDecomposeCreateIn {
source_id: string
scopes?: string[]
table_names?: string[]
document_ids?: string[]
attachment_ids?: string[]
doc_title?: string
doc_text?: string
save_to_kb?: boolean
note?: string
}
export interface SemanticDecomposeApplyIn {
catalog_patches?: { table: string; changes: { path: string; value: unknown; reason: string }[] }[]
metrics?: SemanticMetricInput[]
entity_operations?: Record<string, unknown>[]
overwrite_existing_metrics?: boolean
}
export const semanticMetrics = {
list: (params?: { source_id?: string; status?: string; q?: string }, opts?: RequestOptions) =>
get<{ items: SemanticMetric[]; total: number }>(
`/semantic-metrics${qs({
source_id: params?.source_id,
status: params?.status,
q: params?.q,
})}`,
opts,
),
get: (id: string, opts?: RequestOptions) =>
get<{ metric: SemanticMetric }>(`/semantic-metrics/${encodeURIComponent(id)}`, opts),
create: (body: SemanticMetricInput) =>
post<{ metric: SemanticMetric }>('/semantic-metrics', body),
batchCreate: (items: SemanticMetricInput[]) =>
post<{ created: SemanticMetric[]; skipped: { key: string; reason: string }[] }>('/semantic-metrics/batch', { items }),
update: (id: string, body: Partial<SemanticMetricInput>) =>
put<{ metric: SemanticMetric }>(`/semantic-metrics/${encodeURIComponent(id)}`, body),
remove: (id: string) =>
del(`/semantic-metrics/${encodeURIComponent(id)}`),
preview: (body: SemanticMetricPreviewIn) =>
post<SemanticMetricPreviewOut>('/semantic-metrics/preview', body),
suggest: (body: { source_id: string; table_names?: string[]; document_ids?: string[]; note?: string }) =>
post<SemanticMetricSuggestOut>('/semantic-metrics/suggest', body),
uploadAttachment: (file: File, opts?: { save_to_kb?: boolean; collection?: string } & UploadOptions) => {
const form = new FormData()
form.append('file', file)
if (opts?.save_to_kb !== undefined) form.append('save_to_kb', String(opts.save_to_kb))
if (opts?.collection) form.append('collection', opts.collection)
return upload<DecomposeAttachmentOut>('/semantic-metrics/attachments', form, opts)
},
decomposeJobs: {
active: (sourceId?: string, opts?: RequestOptions) =>
get<{ active: SemanticDecomposeJob | null; latest: SemanticDecomposeJob | null }>(
`/semantic-metrics/decompose-jobs/active${qs({ source_id: sourceId })}`,
opts,
),
list: (sourceId?: string, limit = 10, opts?: RequestOptions) =>
get<{ items: SemanticDecomposeJob[] }>(
`/semantic-metrics/decompose-jobs${qs({ source_id: sourceId, limit })}`,
opts,
),
create: (body: SemanticDecomposeCreateIn) =>
post<{ job: SemanticDecomposeJob }>('/semantic-metrics/decompose-jobs', body),
get: (jobId: string, opts?: RequestOptions) =>
get<{ job: SemanticDecomposeJob }>(`/semantic-metrics/decompose-jobs/${encodeURIComponent(jobId)}`, opts),
cancel: (jobId: string) =>
post<{ job: SemanticDecomposeJob }>(`/semantic-metrics/decompose-jobs/${encodeURIComponent(jobId)}/cancel`, {}),
apply: (jobId: string, body: SemanticDecomposeApplyIn) =>
post<{
job_id: string
applied: SemanticDecomposeAppliedRecord
created_metrics: SemanticMetric[]
updated_metrics: SemanticMetric[]
}>(`/semantic-metrics/decompose-jobs/${encodeURIComponent(jobId)}/apply`, body),
},
}
+335 -140
View File
@@ -1,25 +1,32 @@
/**
* 口径目录(/ledger/calibers):已发布口径的入口、指标、依赖和试跑。
* 口径与指标目录(/ledger/calibers):
*
* 只读目录 + 参数表单 + 一次只读探索运行。口径的技术身份是「工作流@版本/口径卡」;目录给业务的人看的是名字、指标、
* 入口字段和能不能用。试跑按固定版本派生只读图(服务端 ledger/derive.caliber_probe),不是正式出具:来源记 trial,
* 不进使用统计。SQL 不在页面上出现——服务端 descriptor 本来就不返回。
* 1. 独立语义指标卡片(SemanticMetric):即存即生效,直接绑定数据源、物理表、聚合 SQL 表达式、时间列与标准业务实体维度,
* 保存后自动接入问数页「快速直查」与 metric_query 工具;支持在线试算与从数据目录 / 业务文档批量提取。
* 2. 工作流受管口径(CaliberDescriptor):固定工作流版本@口径卡,用于多步编排与正式出具对账。
*/
import { useEffect, useMemo, useState } from 'react'
import { Link } from 'react-router-dom'
import { ChevronDown, Layers, Play, Plus, RefreshCw, Search } from 'lucide-react'
import { ChevronDown, Layers, Play, Plus, RefreshCw, Search, Sparkles, Zap } from 'lucide-react'
import clsx from 'clsx'
import { api } from '../../api/client'
import type { SemanticDecomposeJob, SemanticMetric } from '../../api/semanticMetrics'
import { SchemaForm } from '../../components/SchemaForm'
import { EmptyState, ErrorState, Notice, PageHeader, Skeleton, Spinner, toast } from '../../components/ui'
import { LEDGER_TEXT } from '../../lib/terms'
import { LEDGER_UI } from '../../lib/termsLedger'
import { useAtLeast } from '../../store/auth'
import {
DecomposeJobBanner, MetricEditorModal, MetricSuggestModal, SemanticMetricCardsSection,
} from './SemanticMetricStudio'
import { TrialResult, TrialValidation } from './TrialResult'
import type { FormSchema } from '../../typesApp'
import type { CaliberDescriptor, CaliberTrialOut } from '../../typesLedger'
import type { CaliberDescriptor, CaliberTrialOut, CanonicalEntity } from '../../typesLedger'
const keyOf = (d: CaliberDescriptor) => `${d.workflow_id}@${d.version}/${d.card.node_id}`
type ViewFilter = 'all' | 'metrics' | 'calibers'
function defaultsOf(schema: FormSchema): Record<string, unknown> {
const out: Record<string, unknown> = {}
for (const [name, field] of Object.entries(schema.properties ?? {})) {
@@ -29,7 +36,17 @@ function defaultsOf(schema: FormSchema): Record<string, unknown> {
}
export function CaliberCatalog() {
const builder = useAtLeast('builder')
const [items, setItems] = useState<CaliberDescriptor[] | null>(null)
const [metrics, setMetrics] = useState<SemanticMetric[]>([])
const [entities, setEntities] = useState<CanonicalEntity[]>([])
const [filter, setFilter] = useState<ViewFilter>('all')
const [editorOpen, setEditorOpen] = useState(false)
const [editingMetric, setEditingMetric] = useState<SemanticMetric | null>(null)
const [suggestOpen, setSuggestOpen] = useState(false)
const [bannerJob, setBannerJob] = useState<SemanticDecomposeJob | null>(null)
const [modalInitialJob, setModalInitialJob] = useState<SemanticDecomposeJob | null>(null)
const [dismissedJobId, setDismissedJobId] = useState<string | null>(null)
const [error, setError] = useState<unknown>(null)
const [query, setQuery] = useState('')
const [open, setOpen] = useState<string | null>(null)
@@ -37,6 +54,29 @@ export function CaliberCatalog() {
const [trials, setTrials] = useState<Record<string, CaliberTrialOut>>({})
const [busy, setBusy] = useState<string | null>(null)
const refreshDecomposeBanner = () => {
if (!builder) return
api.semanticMetrics.decomposeJobs.active().then(
({ active, latest }) => {
setBannerJob(active ?? latest ?? null)
},
() => {},
)
}
const loadMetrics = () => {
if (!builder) return
api.semanticMetrics.list().then(
(res) => setMetrics(res.items ?? []),
() => setMetrics([]),
)
api.ontology.listEntities().then(
(res) => setEntities(res.items ?? []),
() => setEntities([]),
)
refreshDecomposeBanner()
}
const load = () => {
setError(null)
setItems(null)
@@ -44,8 +84,44 @@ export function CaliberCatalog() {
(body) => setItems(body.calibers),
(e) => setError(e),
)
loadMetrics()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(load, [builder])
// 当有后台口径拆解任务正在排队或执行时,自动轮询最新阶段进度
useEffect(() => {
if (!builder || !bannerJob || (bannerJob.status !== 'queued' && bannerJob.status !== 'running')) return
let alive = true
const timer = window.setInterval(() => {
api.semanticMetrics.decomposeJobs.active().then(
({ active, latest }) => {
if (!alive) return
const next = active ?? latest ?? null
setBannerJob(next)
if (!active && latest?.status === 'succeeded') {
setDismissedJobId(null)
toast.ok('后台口径文档拆解已完成,点击横幅即可审阅并一键应用')
}
},
() => {},
)
}, 1800)
return () => {
alive = false
window.clearInterval(timer)
}
}, [builder, bannerJob?.id, bannerJob?.status])
const handleCancelBannerJob = async (jobId: string) => {
try {
const { job: cancelled } = await api.semanticMetrics.decomposeJobs.cancel(jobId)
setBannerJob(cancelled)
toast.info('已取消后台拆解任务')
} catch (e) {
toast.error(e)
}
}
useEffect(load, [])
const shown = useMemo(() => {
const q = query.trim().toLowerCase()
@@ -54,6 +130,14 @@ export function CaliberCatalog() {
...d.metrics.map((m) => m.name ?? m.id)].some((s) => String(s ?? '').toLowerCase().includes(q)))
}, [items, query])
const shownMetricsCount = useMemo(() => {
const q = query.trim().toLowerCase()
if (!q) return metrics.length
return metrics.filter((m) => [
m.name, m.key, m.table_name, m.source_name, m.description, ...(m.aliases ?? []),
].some((s) => String(s ?? '').toLowerCase().includes(q))).length
}, [metrics, query])
const toggle = (d: CaliberDescriptor) => {
const key = keyOf(d)
if (open === key) { setOpen(null); return }
@@ -77,12 +161,33 @@ export function CaliberCatalog() {
}
}
const showMetrics = filter !== 'calibers'
const showCalibers = filter !== 'metrics'
return (
<div className="flex h-full flex-col" data-caliber-catalog="">
<PageHeader icon={<Layers size={14} />} title="口径" subtitle="已发布口径的入口、指标、依赖与试运行"
<PageHeader icon={<Layers size={14} />} title="口径" subtitle="独立语义指标卡片(即存即生效)与工作流受管口径目录"
actions={<>
<Link to="/ledger/drafts" className="btn btn-xs btn-primary" data-caliber-drafts-link=""
title="从业务文档起草;已经有答案的,在草稿里接一次运行">
{builder && (
<>
<button type="button" className="btn btn-xs btn-primary" data-metric-create-btn=""
onClick={() => { setEditingMetric(null); setEditorOpen(true) }}>
<Zap size={12} aria-hidden /> 新建指标卡片
</button>
<button type="button" className="btn btn-xs" data-metric-suggest-btn=""
onClick={() => {
const activeRunning = bannerJob && (bannerJob.status === 'queued' || bannerJob.status === 'running')
? bannerJob
: null
setModalInitialJob(activeRunning)
setSuggestOpen(true)
}}>
<Sparkles size={12} aria-hidden /> 文档一键拆解 / 提取指标
</button>
</>
)}
<Link to="/ledger/drafts" className={clsx('btn btn-xs', !builder && 'btn-primary')} data-caliber-drafts-link=""
title="从业务文档起草工作流受管口径;已经有答案的,在草稿里接一次运行">
<Plus size={12} aria-hidden /> {LEDGER_UI.drafts.entry}
</Link>
<button type="button" className="btn btn-xs btn-ghost" onClick={load} title="重新加载">
@@ -91,141 +196,231 @@ export function CaliberCatalog() {
</>} />
<div className="min-h-0 flex-1 overflow-y-auto">
<div className="mx-auto max-w-5xl space-y-3 p-4">
<label className="relative block">
<Search size={13} className="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-faint" aria-hidden />
<input className="input w-full pl-8" value={query} onChange={(e) => setQuery(e.target.value)}
placeholder="按工作流、口径、指标名查找" aria-label="查找口径" />
</label>
{error ? <ErrorState error={error} onRetry={load} compact />
: !items ? <Skeleton rows={5} height={38} />
: !shown.length ? <EmptyState offline={false} icon={<Layers size={22} />} title="还没有可用的口径"
body={query ? '换个关键词试试' : '在工作流里发布一份带口径卡的版本后,这里就会出现'} />
: shown.map((d) => {
const key = keyOf(d)
const expanded = open === key
const result = trials[key]
return (
<section key={key} className="overflow-hidden rounded-lg border bg-panel" data-caliber={key}>
<button type="button" className="flex w-full items-start gap-3 px-3 py-2.5 text-left hover:bg-hover"
onClick={() => toggle(d)} aria-expanded={expanded} data-caliber-toggle="">
<div className="min-w-0 flex-1">
<div className="flex flex-wrap items-baseline gap-x-2 gap-y-0.5">
<span className="text-sm font-medium">{d.workflow_name}</span>
<span className="text-2xs text-faint">v{d.version}</span>
{d.card.caliber && <span className="text-xs text-dim">· {d.card.caliber}</span>}
<span className="rounded border px-1.5 py-0.5 text-[10px] text-dim">
{(LEDGER_TEXT.level as Record<string, string>)[d.level] ?? d.level}
</span>
</div>
<div className="mt-0.5 text-2xs text-faint">
{d.metrics.map((m) => m.name ?? m.id).join('、')}
{d.entry_schema.properties && Object.keys(d.entry_schema.properties).length > 0
? ` · ${Object.keys(d.entry_schema.properties).length} 个入口字段` : ' · 没有入口字段'}
{d.dependencies.tables.length > 0 ? ` · 读 ${d.dependencies.tables.join('、')}` : ''}
</div>
</div>
<div className="flex shrink-0 items-center gap-2 text-[10px] text-faint">
<span>{d.composable ? '可组合' : '不可组合'}</span>
<span>{d.issuance.auto_issue ? '可自动出具' : '只可一键出具'}</span>
<ChevronDown size={14} className={clsx('transition-transform', expanded && 'rotate-180')} aria-hidden />
</div>
</button>
{expanded && (
<div className="space-y-3 border-t px-3 py-3" data-caliber-detail="">
<div className="grid gap-3 md:grid-cols-2">
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">指标</h3>
<ul className="space-y-1 text-xs">
{d.metrics.map((m) => (
<li key={m.id} className="border-b border-hairline pb-1 last:border-0">
<div className="flex items-baseline justify-between gap-2">
<span>{m.name ?? m.id}</span>
<span className="text-2xs text-faint">{m.unit || '—'}</span>
</div>
{m.definition?.text && (
<p className="mt-0.5 text-2xs leading-relaxed text-faint">{m.definition.text}</p>
)}
{!!m.params?.length && (
<p className="mt-0.5 text-2xs text-faint">
参数:{m.params.map((p) => `${p.name}(${p.type})`).join('、')}
</p>
)}
</li>
))}
</ul>
</div>
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">依赖</h3>
<p className="text-2xs leading-relaxed text-dim">
数据源:{d.dependencies.sources.join('、') || '—'}<br />
表:{d.dependencies.tables.join('、') || '—'}<br />
{d.compose_reason ? `不能组合:${d.compose_reason}` : '可以按这一版组合'}
</p>
{d.issuance.reasons.length > 0 && (
<p className="mt-1 text-2xs leading-relaxed text-faint">
出具:{d.issuance.reasons.join(';')}
</p>
)}
</div>
</div>
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">入口字段</h3>
{d.entry_schema.properties && Object.keys(d.entry_schema.properties).length > 0 ? (
<SchemaForm schema={d.entry_schema} value={values[key] ?? {}}
onChange={(v) => {
setValues((all) => ({ ...all, [key]: v }))
setTrials((all) => {
const next = { ...all }
delete next[key]
return next
})
}}
idPrefix={`caliber-${key}`} />
) : <p className="text-2xs text-faint">这份口径不需要参数。</p>}
</div>
<div className="flex flex-wrap items-center gap-2">
<button type="button" className="btn btn-primary btn-sm" disabled={busy === key}
onClick={() => void trial(d)} data-caliber-trial="">
{busy === key ? <Spinner size={12} /> : <Play size={12} aria-hidden />} 试运行
</button>
<Link to={`/studio/${d.workflow_id}`} className="btn btn-sm btn-ghost">打开工作流</Link>
<span className="text-2xs text-faint">试运行是只读探索运行,不是正式出具</span>
</div>
{result && (
<div className="rounded-md border bg-elev px-3 py-2 text-xs" data-caliber-trial-result={result.status}>
{result.status === 'succeeded' ? (
<ul className="space-y-1">
{result.metrics.map((m) => (
<li key={m.id} className="flex items-baseline justify-between gap-2">
<span>{m.name ?? m.id}</span>
<span className="tnum font-medium">{m.rendered ?? String(m.value ?? '—')}{m.unit ? ` ${m.unit}` : ''}</span>
</li>
))}
</ul>
) : (
<Notice tone={result.status === 'running' ? 'info' : 'err'}>
{result.status === 'running' ? '还在运行:' : '试运行没有成功:'}
{result.error || result.status}
</Notice>
)}
{result.run_id && (
<div className="mt-1">
<Link className="text-2xs text-accent hover:underline" to={`/runs/${result.run_id}`}
data-caliber-trial-run="">
{LEDGER_UI.drafts.trialRun}
</Link>
</div>
)}
{result.status === 'succeeded' && <><TrialValidation report={result.validation} /><TrialResult queries={result.queries} /></>}
</div>
)}
{builder && bannerJob && bannerJob.id !== dismissedJobId && (
<DecomposeJobBanner
job={bannerJob}
onOpenModal={(j) => {
setModalInitialJob(j)
setSuggestOpen(true)
}}
onCancel={(jid) => void handleCancelBannerJob(jid)}
onDismiss={() => setDismissedJobId(bannerJob.id)}
/>
)}
<div className="flex flex-wrap items-center justify-between gap-2">
<div role="group" aria-label="口径视图筛选" className="inline-flex items-center rounded-md border bg-panel p-0.5 text-xs">
<button type="button" aria-pressed={filter === 'all'}
className={clsx('rounded px-2.5 py-1 transition-colors',
filter === 'all' ? 'bg-accent-soft font-medium text-[var(--accent)]' : 'text-dim hover:text-fg')}
onClick={() => setFilter('all')}>
全部({metrics.length + (items?.length ?? 0)})
</button>
<button type="button" aria-pressed={filter === 'metrics'} data-filter-metrics=""
className={clsx('rounded px-2.5 py-1 transition-colors',
filter === 'metrics' ? 'bg-accent-soft font-medium text-[var(--accent)]' : 'text-dim hover:text-fg')}
onClick={() => setFilter('metrics')}>
指标卡片 · 即存即用({metrics.length})
</button>
<button type="button" aria-pressed={filter === 'calibers'} data-filter-calibers=""
className={clsx('rounded px-2.5 py-1 transition-colors',
filter === 'calibers' ? 'bg-accent-soft font-medium text-[var(--accent)]' : 'text-dim hover:text-fg')}
onClick={() => setFilter('calibers')}>
工作流受管口径({items?.length ?? 0})
</button>
</div>
<label className="relative min-w-[240px] flex-1 sm:max-w-sm">
<Search size={13} className="pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-faint" aria-hidden />
<input className="input w-full pl-8" value={query} onChange={(e) => setQuery(e.target.value)}
placeholder="按指标名、标识、物理表或工作流查找" aria-label="查找口径" />
</label>
</div>
{showMetrics && (
<SemanticMetricCardsSection
metrics={metrics}
entities={entities}
query={query}
onReload={loadMetrics}
onEdit={(m) => { setEditingMetric(m); setEditorOpen(true) }}
/>
)}
{filter === 'metrics' && shownMetricsCount === 0 && (
<EmptyState
offline={false}
icon={<Zap size={22} />}
title="还没有独立语义指标卡片"
body={query ? '换个关键词试试' : '点击右上角「新建指标卡片」或「从目录与文档提取」,保存后无需发布工作流即可在问数快速直查中直接命中'}
/>
)}
{showCalibers && (
error ? <ErrorState error={error} onRetry={load} compact />
: !items ? <Skeleton rows={5} height={38} />
: !shown.length ? (
shownMetricsCount === 0 ? (
<EmptyState offline={false} icon={<Layers size={22} />} title="还没有可用的口径"
body={query ? '换个关键词试试' : '可新建即存即用的指标卡片,或在工作流里发布带口径卡的版本'} />
) : null
) : (
<div className="space-y-2">
{showMetrics && shownMetricsCount > 0 && (
<div className="px-0.5 pt-2 text-xs font-semibold text-dim">
工作流受管口径(共 {shown.length} 项)
</div>
)}
</section>
{shown.map((d) => {
const key = keyOf(d)
const expanded = open === key
const result = trials[key]
return (
<section key={key} className="overflow-hidden rounded-lg border bg-panel" data-caliber={key}>
<button type="button" className="flex w-full items-start gap-3 px-3 py-2.5 text-left hover:bg-hover"
onClick={() => toggle(d)} aria-expanded={expanded} data-caliber-toggle="">
<div className="min-w-0 flex-1">
<div className="flex flex-wrap items-baseline gap-x-2 gap-y-0.5">
<span className="text-sm font-medium">{d.workflow_name}</span>
<span className="text-2xs text-faint">v{d.version}</span>
{d.card.caliber && <span className="text-xs text-dim">· {d.card.caliber}</span>}
<span className="rounded border px-1.5 py-0.5 text-[10px] text-dim">
{(LEDGER_TEXT.level as Record<string, string>)[d.level] ?? d.level}
</span>
</div>
<div className="mt-0.5 text-2xs text-faint">
{d.metrics.map((m) => m.name ?? m.id).join('、')}
{d.entry_schema.properties && Object.keys(d.entry_schema.properties).length > 0
? ` · ${Object.keys(d.entry_schema.properties).length} 个入口字段` : ' · 没有入口字段'}
{d.dependencies.tables.length > 0 ? ` · 读 ${d.dependencies.tables.join('、')}` : ''}
</div>
</div>
<div className="flex shrink-0 items-center gap-2 text-[10px] text-faint">
<span>{d.composable ? '可组合' : '不可组合'}</span>
<span>{d.issuance.auto_issue ? '可自动出具' : '只可一键出具'}</span>
<ChevronDown size={14} className={clsx('transition-transform', expanded && 'rotate-180')} aria-hidden />
</div>
</button>
{expanded && (
<div className="space-y-3 border-t px-3 py-3" data-caliber-detail="">
<div className="grid gap-3 md:grid-cols-2">
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">指标</h3>
<ul className="space-y-1 text-xs">
{d.metrics.map((m) => (
<li key={m.id} className="border-b border-hairline pb-1 last:border-0">
<div className="flex items-baseline justify-between gap-2">
<span>{m.name ?? m.id}</span>
<span className="text-2xs text-faint">{m.unit || '—'}</span>
</div>
{m.definition?.text && (
<p className="mt-0.5 text-2xs leading-relaxed text-faint">{m.definition.text}</p>
)}
{!!m.params?.length && (
<p className="mt-0.5 text-2xs text-faint">
参数:{m.params.map((p) => `${p.name}(${p.type})`).join('、')}
</p>
)}
</li>
))}
</ul>
</div>
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">依赖</h3>
<p className="text-2xs leading-relaxed text-dim">
数据源:{d.dependencies.sources.join('、') || '—'}<br />
表:{d.dependencies.tables.join('、') || '—'}<br />
{d.compose_reason ? `不能组合:${d.compose_reason}` : '可以按这一版组合'}
</p>
{d.issuance.reasons.length > 0 && (
<p className="mt-1 text-2xs leading-relaxed text-faint">
出具:{d.issuance.reasons.join(';')}
</p>
)}
</div>
</div>
<div>
<h3 className="mb-1 text-2xs font-semibold text-dim">入口字段</h3>
{d.entry_schema.properties && Object.keys(d.entry_schema.properties).length > 0 ? (
<SchemaForm schema={d.entry_schema} value={values[key] ?? {}}
onChange={(v) => {
setValues((all) => ({ ...all, [key]: v }))
setTrials((all) => {
const next = { ...all }
delete next[key]
return next
})
}}
idPrefix={`caliber-${key}`} />
) : <p className="text-2xs text-faint">这份口径不需要参数。</p>}
</div>
<div className="flex flex-wrap items-center gap-2">
<button type="button" className="btn btn-primary btn-sm" disabled={busy === key}
onClick={() => void trial(d)} data-caliber-trial="">
{busy === key ? <Spinner size={12} /> : <Play size={12} aria-hidden />} 试运行
</button>
<Link to={`/studio/${d.workflow_id}`} className="btn btn-sm btn-ghost">打开工作流</Link>
<span className="text-2xs text-faint">试运行是只读探索运行,不是正式出具</span>
</div>
{result && (
<div className="rounded-md border bg-elev px-3 py-2 text-xs" data-caliber-trial-result={result.status}>
{result.status === 'succeeded' ? (
<ul className="space-y-1">
{result.metrics.map((m) => (
<li key={m.id} className="flex items-baseline justify-between gap-2">
<span>{m.name ?? m.id}</span>
<span className="tnum font-medium">{m.rendered ?? String(m.value ?? '—')}{m.unit ? ` ${m.unit}` : ''}</span>
</li>
))}
</ul>
) : (
<Notice tone={result.status === 'running' ? 'info' : 'err'}>
{result.status === 'running' ? '还在运行:' : '试运行没有成功:'}
{result.error || result.status}
</Notice>
)}
{result.run_id && (
<div className="mt-1">
<Link className="text-2xs text-accent hover:underline" to={`/runs/${result.run_id}`}
data-caliber-trial-run="">
{LEDGER_UI.drafts.trialRun}
</Link>
</div>
)}
{result.status === 'succeeded' && <><TrialValidation report={result.validation} /><TrialResult queries={result.queries} /></>}
</div>
)}
</div>
)}
</section>
)
})}
</div>
)
})}
)}
</div>
</div>
{builder && (
<>
<MetricEditorModal
open={editorOpen}
metric={editingMetric}
entities={entities}
onClose={() => { setEditorOpen(false); setEditingMetric(null) }}
onSaved={loadMetrics}
/>
<MetricSuggestModal
open={suggestOpen}
initialJob={modalInitialJob}
onClose={() => setSuggestOpen(false)}
onSaved={loadMetrics}
onJobUpdated={(nextJob) => {
setBannerJob(nextJob)
setModalInitialJob(nextJob)
if (nextJob) setDismissedJobId(null)
}}
/>
</>
)}
</div>
)
}
File diff suppressed because it is too large Load Diff