mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-17 08:42:01 +00:00
草稿页改成看「定义、数、引用」再签字
试运行结果和入口字段直接摆在页面上;主按钮是去画布审改并发布,接上一次运行收进次要入口。 数据源改为必选并默认选中:没有目录就写不出取数 SQL。
This commit is contained in:
@@ -13,7 +13,8 @@
|
||||
*/
|
||||
import type {
|
||||
CaliberDescriptor, CaliberDraft, CaliberDraftAttachOut, CaliberDraftSummary, CaliberTrialOut, CertifyQueue, DraftMetric,
|
||||
FreezeOut, LedgerFeedbackOut, LedgerFeedbackVerdict, LedgerIssueOut, LedgerReconcileIn, LedgerReconcileOut, RunLedger,
|
||||
DraftTrialOut, FreezeOut, LedgerFeedbackOut, LedgerFeedbackVerdict, LedgerIssueOut, LedgerReconcileIn, LedgerReconcileOut,
|
||||
RunLedger,
|
||||
} from '../typesLedger'
|
||||
import { get, patch, post, type RequestOptions } from './http'
|
||||
|
||||
@@ -48,19 +49,21 @@ export const caliberLedger = {
|
||||
{ inputs },
|
||||
),
|
||||
/**
|
||||
* 从业务文档起草口径(app/ledger/docdraft.py,搭建者):草稿只产出定义,不产出取数。发起后立刻返回 drafting,模型在后台跑,
|
||||
* 结果用 draft() 反复取,直到状态离开 drafting
|
||||
* 从业务文档起草口径(app/ledger/docdraft.py,搭建者):发起后立刻返回 drafting,模型在后台读文档、对照数据目录
|
||||
* 写定义和取数,落成草稿图并自动试运行;结果用 draft() 反复取,直到状态离开 drafting
|
||||
*/
|
||||
drafts: (status?: string, opts?: RequestOptions) =>
|
||||
get<{ drafts: CaliberDraftSummary[] }>(`/ledger/drafts${status ? `?status=${encodeURIComponent(status)}` : ''}`, opts),
|
||||
/** 一份草稿的完整内容:payload 摊平在上层(metrics / dropped / open_questions / alignment) */
|
||||
/** 一份草稿的完整内容:payload 摊平在上层(metrics / entry_fields / dropped / open_questions / alignment / trial) */
|
||||
draft: (draftId: string, opts?: RequestOptions) => get<CaliberDraft>(draftAt(draftId), opts),
|
||||
/** 起草后自动发起的那次只读试运行:状态和每个指标的值,running 时轮询到终态 */
|
||||
draftTrial: (draftId: string, opts?: RequestOptions) => get<DraftTrialOut>(`${draftAt(draftId)}/trial`, opts),
|
||||
/** 选文档发起起草:documents 是要用的知识库文档 id,guidance 是给模型看的附加要求 */
|
||||
extractDraft: (body: { documents: string[]; collection?: string; datasource_id?: string; guidance?: string }) =>
|
||||
post<{ draft_id: string; status: string }>('/ledger/drafts/extract', body),
|
||||
/**
|
||||
* 改标题、整体换掉指标、或把答清的问题消掉(resolved 是问题原文)。服务端据此重算状态:还有问题 → questions_open,
|
||||
* 否则 path_pending;已经接上或驳回的不改状态
|
||||
* 否则 ready;已经接上或驳回的不改状态
|
||||
*/
|
||||
patchDraft: (draftId: string, body: { title?: string; metrics?: DraftMetric[]; resolved?: string[] }) =>
|
||||
patch<CaliberDraft>(draftAt(draftId), body),
|
||||
|
||||
@@ -227,12 +227,13 @@ export const LEDGER_UI = {
|
||||
drafts: {
|
||||
entry: '从业务文档起草',
|
||||
title: '从业务文档起草口径',
|
||||
subtitle: '文档里写的是业务说法,这里整理成定义草稿;取数路径接上一次真实运行',
|
||||
subtitle: '文档整理成定义和取数,自动试运行一次;去画布审改后由人签字发布',
|
||||
back: '返回口径目录',
|
||||
refresh: '刷新',
|
||||
/** 列表按状态筛:全部加五种落点,起草中的只出现在全部里 */
|
||||
/** 列表按状态筛:全部加六种落点,起草中的只出现在全部里 */
|
||||
filter: {
|
||||
all: '全部',
|
||||
ready: '待签字发布',
|
||||
questions_open: '待确认问题',
|
||||
path_pending: '待接数据路径',
|
||||
attached: '已接上',
|
||||
@@ -241,6 +242,7 @@ export const LEDGER_UI = {
|
||||
} as Record<string, string>,
|
||||
status: {
|
||||
drafting: '起草中',
|
||||
ready: '待签字发布',
|
||||
questions_open: '待确认问题',
|
||||
path_pending: '待接数据路径',
|
||||
attached: '已接上',
|
||||
@@ -252,7 +254,7 @@ export const LEDGER_UI = {
|
||||
questions ? `${formatNumber(questions)} 个未决问题` : '没有未决问题'}`,
|
||||
loadFailed: '无法读取口径草稿',
|
||||
empty: '还没有口径草稿',
|
||||
emptyBody: '选一份业务文档,把里面的指标整理成定义草稿',
|
||||
emptyBody: '选一份业务文档,把里面的指标整理成定义和取数草稿',
|
||||
emptyFiltered: '这个状态下没有草稿,换个状态看看',
|
||||
|
||||
// ---- 发起:选文档、可选数据源和起草要求 ----
|
||||
@@ -262,22 +264,32 @@ export const LEDGER_UI = {
|
||||
noDocuments: '知识库里还没有已完成的文档',
|
||||
noDocumentsBody: '先在知识页上传业务文档,切块完成后再来起草',
|
||||
documentMeta: (collection: string, chunks: number) => `${collection} · ${formatNumber(chunks)} 段`,
|
||||
datasource: '数据源(可选)',
|
||||
datasourceHint: '只写进适用范围,不读表结构',
|
||||
datasourceNone: '不指定',
|
||||
datasource: '数据源(必选)',
|
||||
datasourceHint: '取数 SQL 对照这个库的真实表和列写,先探查过表结构才能起草',
|
||||
guidance: '起草要求(可选)',
|
||||
guidancePlaceholder: '例如:只整理和月度结算有关的指标',
|
||||
submit: '开始起草',
|
||||
submitting: '正在发起…',
|
||||
needDoc: '请至少选一份文档',
|
||||
started: '已开始起草,完成后指标会出现在下面',
|
||||
started: '已开始起草,完成并试运行后指标和数会出现在下面',
|
||||
|
||||
// ---- 抽取中 ----
|
||||
drafting: '正在抽取指标…',
|
||||
draftingHint: '模型在后台读文档、核对引用,通常不到一分钟;离开页面不影响结果',
|
||||
draftingHint: '模型在后台读文档、对照数据目录写定义和取数,通常不到一分钟;离开页面不影响结果',
|
||||
draftingTimeout: '起草超过一分钟还没有结果:稍后刷新再看;失败原因会写在这份草稿上',
|
||||
draftingFailed: '起草没有完成',
|
||||
|
||||
// ---- 自动试运行 ----
|
||||
trial: '自动试运行',
|
||||
trialHint: '发起时按草稿图只读运行一次,只用来看数和定义对不对得上',
|
||||
trialRunning: '正在试运行…',
|
||||
trialSucceeded: '试运行已完成',
|
||||
trialFailed: '试运行没有成功',
|
||||
trialTimeout: '试运行超过一分钟还没有结果:稍后刷新再看',
|
||||
trialNoMetrics: '这次试运行没有产出指标值',
|
||||
trialUnavailable: '暂时无法读取试运行状态',
|
||||
trialRun: '看这次运行',
|
||||
|
||||
// ---- 详情 ----
|
||||
documentsUsed: (n: number) => `${formatNumber(n)} 份文档`,
|
||||
truncated: '文档太长,后面的段落没有参与起草:可以少选几份,或先拆分文档',
|
||||
@@ -286,6 +298,9 @@ export const LEDGER_UI = {
|
||||
noMetricsBody: '文档里没有能逐字核对上引用的指标定义,可以换一份文档或补充起草要求后重来',
|
||||
synonyms: (list: string) => `又称:${list}`,
|
||||
unit: '单位',
|
||||
decimals: '小数位',
|
||||
expression: '表达式',
|
||||
definition: '定义',
|
||||
grain: '粒度',
|
||||
timeField: '时间字段',
|
||||
dimensions: '维度',
|
||||
@@ -306,7 +321,7 @@ export const LEDGER_UI = {
|
||||
resolveNeed: '请先勾选已经问清的问题',
|
||||
resolved: (n: number) => `已移除 ${formatNumber(n)} 个问题`,
|
||||
dropped: '被丢弃的条目',
|
||||
droppedHint: '只产出定义:带 SQL、表名痕迹,或引用无法逐字核对上的指标不留下',
|
||||
droppedHint: '带不了能对照数据目录的取数,或引用无法逐字核对上的指标不留下',
|
||||
alignment: '和已发布口径对齐',
|
||||
alignmentNew: '新的',
|
||||
alignmentDuplicate: '已有出处',
|
||||
@@ -314,9 +329,20 @@ export const LEDGER_UI = {
|
||||
model: (id: string) => `起草模型:${id}`,
|
||||
createdBy: (who: string) => `起草人:${who}`,
|
||||
|
||||
// ---- 接上一次运行 ----
|
||||
attach: '接上一次真实运行',
|
||||
attachHint: '取数路径照抄这次运行里执行过的查询,文档只贡献定义;接上后产出可试运行的工作流版本,不发布',
|
||||
// ---- 入口字段与发布 ----
|
||||
entryFields: '入口字段',
|
||||
entryFieldsHint: '发布后按这些字段收参数;缺省值可以不填',
|
||||
entryFieldNone: '这份草稿的图没有入口字段',
|
||||
required: '必填',
|
||||
optional: '可选',
|
||||
defaultValue: (value: string) => `缺省 ${value}`,
|
||||
publish: '去画布审改并发布',
|
||||
publishHint: '在画布上核对图、改完再发布;发布这一步由人签字',
|
||||
workflowVersion: (version: number) => `工作流 v${version}`,
|
||||
|
||||
// ---- 接上一次运行(辅助入口) ----
|
||||
attach: '接上一次运行',
|
||||
attachHint: '已经有一次运行得出的答案,让它认这份定义:取数照抄那次运行执行过的查询,不重新生成;接上后产出可试运行的工作流版本,不发布',
|
||||
runId: '运行 id',
|
||||
runIdPlaceholder: '粘贴运行记录里的 id',
|
||||
recentRuns: '最近成功的运行',
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
/**
|
||||
* 从业务文档起草口径(/ledger/drafts;服务端 app/ledger/docdraft.py、docattach.py)。
|
||||
*
|
||||
* 业务文档写的是业务说法(名称、粒度、分子分母、过滤),不是库里的表和 SQL。这一页做三段:
|
||||
* - 发起:选知识库文档,可选数据源(只写进适用范围)和一段起草要求。模型在后台读文档、核对引用,页面轮询详情直到状态离开
|
||||
* drafting(最多一分钟),期间显示抽取中;
|
||||
* - 审读:每条指标带单位、粒度、定义和逐字核对过的引用原文(第 N 段),逐条的未决问题可以勾掉;被丢弃的条目单独列出原因。
|
||||
* 引用的段落正文按需从知识库取,省得一次读几十份文档的全部段落;
|
||||
* - 接上:草稿只到定义为止,取数路径必须来自一次真实运行。填运行 id(也可以从最近成功的运行里选),attach 之后产出可试运行
|
||||
* 的工作流版本,不发布;不是口径就驳回并记一条审计。
|
||||
* 主路径一次到位:选文档 → 模型读文档、对照数据目录产出定义 + 取数 + 入口字段 + 指标表达式 → 服务端校验后落成草稿图
|
||||
* → 后台自动只读试运行 → 这里展示「定义 + 数 + 原文引用」→ 人去画布审改并发布。发布才需要人签字。
|
||||
* - 发起:选知识库文档,可选数据源(只写进适用范围)和一段起草要求,页面轮询详情直到状态离开 drafting(最多一分钟);
|
||||
* - 试运行:草稿顶部显示这次自动试运行的状态和每个指标的值,running 时每两秒取一次,最多等一分钟;失败照实写原因;
|
||||
* - 审读:每条指标带单位、粒度、指标表达式、定义和逐字核对过的引用原文(第 N 段),逐条的未决问题可以勾掉;被丢弃
|
||||
* 的条目单独列出原因;入口字段列出名字、类型、必填和缺省值;
|
||||
* - 发布:主按钮去画布审改并发布。「接上一次运行」只是辅助入口:已经有答案时让它认这份定义。
|
||||
*
|
||||
* 状态由服务端算(还有问题 → questions_open,否则 path_pending;attach 之后 attached),页面只按状态画。
|
||||
* 页面不显示 SQL,取数到画布上看。状态由服务端算(还有问题 → questions_open,否则 ready;旧路径可能停在 path_pending),
|
||||
* 页面只按状态画。
|
||||
*/
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { ChevronLeft, FileText, RefreshCw, ScrollText } from 'lucide-react'
|
||||
import { ChevronLeft, FileText, PenLine, RefreshCw, ScrollText } from 'lucide-react'
|
||||
import clsx from 'clsx'
|
||||
import { api } from '../../api/client'
|
||||
import { EmptyState, ErrorState, Notice, PageHeader, Skeleton, Spinner, promptDialog, toast } from '../../components/ui'
|
||||
@@ -21,18 +22,18 @@ import { formatDateTime, formatTime } from '../../lib/format'
|
||||
import { LEDGER_UI } from '../../lib/termsLedger'
|
||||
import type { DataSourceName, KbDocument, Run } from '../../types'
|
||||
import type {
|
||||
CaliberDraft, CaliberDraftStatus, CaliberDraftSummary, DraftAlignment, DraftCitation, DraftMetric,
|
||||
CaliberDraft, CaliberDraftStatus, CaliberDraftSummary, DraftAlignment, DraftCitation, DraftMetric, DraftTrialOut,
|
||||
} from '../../typesLedger'
|
||||
|
||||
const D = LEDGER_UI.drafts
|
||||
|
||||
/** 列表的状态筛:全部加四种落点,起草中的只出现在全部里 */
|
||||
/** 列表的状态筛:全部加六种落点,起草中的只出现在全部里 */
|
||||
type Filter = '' | Exclude<CaliberDraftStatus, 'drafting'>
|
||||
const FILTERS: Filter[] = ['', 'questions_open', 'path_pending', 'attached', 'rejected', 'failed']
|
||||
const FILTERS: Filter[] = ['', 'ready', 'questions_open', 'path_pending', 'attached', 'rejected', 'failed']
|
||||
|
||||
/** 状态标签:起草中 / 待确认问题 / 待接数据路径 / 已接上 / 已驳回 / 起草失败 */
|
||||
/** 状态标签:起草中 / 待签字发布 / 待确认问题 / 待接数据路径 / 已接上 / 已驳回 / 起草失败 */
|
||||
function StatusPill({ status }: { status: CaliberDraftStatus }) {
|
||||
const color = status === 'attached' ? 'var(--ok)' : status === 'rejected' ? 'var(--text-faint)'
|
||||
const color = status === 'ready' || status === 'attached' ? 'var(--ok)' : status === 'rejected' ? 'var(--text-faint)'
|
||||
: status === 'drafting' ? 'var(--accent)' : status === 'failed' ? 'var(--err)' : 'var(--warn)'
|
||||
return (
|
||||
<span className="shrink-0 rounded border px-1.5 py-0.5 text-[10px]"
|
||||
@@ -94,6 +95,13 @@ function AlignmentBlock({ alignment }: { alignment: DraftAlignment }) {
|
||||
)
|
||||
}
|
||||
|
||||
/** 缺省值直接读:字符串照写,其他转成 JSON 文本 */
|
||||
function defaultText(value: unknown): string {
|
||||
if (typeof value === 'string') return value
|
||||
const text = JSON.stringify(value)
|
||||
return text ?? String(value)
|
||||
}
|
||||
|
||||
export function CaliberDrafts() {
|
||||
const [drafts, setDrafts] = useState<CaliberDraftSummary[] | null>(null)
|
||||
const [listError, setListError] = useState<unknown>(null)
|
||||
@@ -122,6 +130,12 @@ export function CaliberDrafts() {
|
||||
const [pollingId, setPollingId] = useState<string | null>(null)
|
||||
const [pollTimedOut, setPollTimedOut] = useState(false)
|
||||
|
||||
/** 这次自动试运行的结果;running 时单独轮询,和草稿抽取的轮询互不相干 */
|
||||
const [trial, setTrial] = useState<DraftTrialOut | null>(null)
|
||||
const [trialPollId, setTrialPollId] = useState<string | null>(null)
|
||||
const [trialTimedOut, setTrialTimedOut] = useState(false)
|
||||
const [trialError, setTrialError] = useState<unknown>(null)
|
||||
|
||||
/** 引用所在段落的正文和段号:按文档懒取,取过的记在这里 */
|
||||
const [chunks, setChunks] = useState<Record<string, { ordinal: number; content: string }>>({})
|
||||
const asked = useRef(new Set<string>())
|
||||
@@ -141,7 +155,12 @@ export function CaliberDrafts() {
|
||||
api.kb.documents().then((rows) => { setDocs(rows); setDocsError(null) },
|
||||
(e) => { setDocs([]); setDocsError(e) })
|
||||
api.datasources.names().then(
|
||||
(all) => setSources(all.filter((s) => s.enabled !== false)),
|
||||
(all) => {
|
||||
const enabled = all.filter((s) => s.enabled !== false)
|
||||
setSources(enabled)
|
||||
// 数据源必选(取数 SQL 要对照这个库的表和列写),默认选中第一个,别让人对着 422 才知道要填
|
||||
setDatasourceId((cur) => cur || (enabled[0]?.id ?? ''))
|
||||
},
|
||||
() => setSources([]),
|
||||
)
|
||||
}, [])
|
||||
@@ -170,6 +189,10 @@ export function CaliberDrafts() {
|
||||
setRecentRuns(null)
|
||||
setPollingId(null)
|
||||
setPollTimedOut(false)
|
||||
setTrial(null)
|
||||
setTrialPollId(null)
|
||||
setTrialTimedOut(false)
|
||||
setTrialError(null)
|
||||
try {
|
||||
const draft = await api.caliberLedger.draft(id)
|
||||
if (seq !== openSeq.current) return
|
||||
@@ -213,6 +236,63 @@ export function CaliberDrafts() {
|
||||
return () => { alive = false; clearInterval(timer) }
|
||||
}, [pollingId, loadList])
|
||||
|
||||
/** 草稿有了试运行就取一次结果;还在 running 就交给下面的轮询 */
|
||||
useEffect(() => {
|
||||
const id = detail?.id
|
||||
const run = detail?.trial?.run_id
|
||||
if (!id || !run) return
|
||||
let alive = true
|
||||
setTrial((cur) => (cur?.run_id === run ? cur : {
|
||||
run_id: run, status: detail?.trial?.status ?? 'running', metrics: [], error: detail?.trial?.error ?? '',
|
||||
}))
|
||||
api.caliberLedger.draftTrial(id).then((out) => {
|
||||
if (!alive) return
|
||||
setTrial(out)
|
||||
setTrialError(null)
|
||||
setTrialTimedOut(false)
|
||||
if (out.status === 'running') setTrialPollId(id)
|
||||
}, (e) => {
|
||||
if (!alive) return
|
||||
setTrialError(e)
|
||||
setTrialPollId(null)
|
||||
})
|
||||
return () => { alive = false }
|
||||
}, [detail?.id, detail?.trial?.run_id])
|
||||
|
||||
/** 试运行还在跑:每两秒取一次,超过一分钟就停;收尾时顺带刷新详情(状态可能已变成待签字发布) */
|
||||
useEffect(() => {
|
||||
if (!trialPollId) return
|
||||
const until = Date.now() + 60_000
|
||||
let alive = true
|
||||
const timer = setInterval(() => {
|
||||
api.caliberLedger.draftTrial(trialPollId).then((out) => {
|
||||
if (!alive) return
|
||||
setTrial(out)
|
||||
setTrialError(null)
|
||||
if (out.status === 'running') {
|
||||
if (Date.now() > until) {
|
||||
setTrialPollId(null)
|
||||
setTrialTimedOut(true)
|
||||
}
|
||||
return
|
||||
}
|
||||
setTrialPollId(null)
|
||||
setTrialTimedOut(false)
|
||||
loadList()
|
||||
const seq = openSeq.current
|
||||
api.caliberLedger.draft(trialPollId).then((fresh) => {
|
||||
if (seq !== openSeq.current) return
|
||||
setDetail((cur) => (cur && cur.id === fresh.id ? fresh : cur))
|
||||
}, () => undefined)
|
||||
}, (e) => {
|
||||
if (!alive) return
|
||||
setTrialPollId(null)
|
||||
setTrialError(e)
|
||||
})
|
||||
}, 2000)
|
||||
return () => { alive = false; clearInterval(timer) }
|
||||
}, [trialPollId, loadList])
|
||||
|
||||
useEffect(() => {
|
||||
for (const doc of detail?.documents ?? []) ensureChunks(doc.document_id)
|
||||
}, [detail, ensureChunks])
|
||||
@@ -231,6 +311,7 @@ export function CaliberDrafts() {
|
||||
const paragraphOf = (citation: DraftCitation): number | null => {
|
||||
const ordinal = chunks[citation.chunk]?.ordinal
|
||||
if (typeof ordinal === 'number') return ordinal
|
||||
if (typeof citation.ordinal === 'number') return citation.ordinal
|
||||
const documentId = citation.document_id || chunkDoc[citation.chunk]
|
||||
const doc = detail?.documents?.find((d) => d.document_id === documentId)
|
||||
const at = doc?.chunk_ids.indexOf(citation.chunk) ?? -1
|
||||
@@ -354,11 +435,93 @@ export function CaliberDrafts() {
|
||||
)
|
||||
}
|
||||
|
||||
/** 自动试运行:状态、每个指标的值(名字和值成对)、失败原因;没有试运行就整块不画 */
|
||||
const renderTrial = (draft: CaliberDraft) => {
|
||||
const status = trial?.status ?? draft.trial?.status
|
||||
if (!status) return null
|
||||
const rows = trial?.metrics ?? []
|
||||
const color = status === 'succeeded' ? 'var(--ok)' : status === 'failed' ? 'var(--err)' : 'var(--accent)'
|
||||
const label = status === 'succeeded' ? D.trialSucceeded : status === 'failed' ? D.trialFailed : D.trialRunning
|
||||
const unitOf = (id: string) => (draft.metrics ?? []).find((m) => m.id === id)?.unit
|
||||
return (
|
||||
<div className="rounded-md border bg-elev px-3 py-2" data-draft-trial={status}>
|
||||
<div className="flex flex-wrap items-baseline gap-x-2 gap-y-0.5">
|
||||
<h3 className="text-2xs font-semibold text-dim">{D.trial}</h3>
|
||||
<span className="text-2xs" style={{ color }}>{label}</span>
|
||||
{trial?.run_id && (
|
||||
<Link to={`/runs/${trial.run_id}`} className="text-2xs text-accent hover:underline" data-draft-trial-run="">
|
||||
{D.trialRun}
|
||||
</Link>
|
||||
)}
|
||||
<span className="text-[10px] text-faint">{D.trialHint}</span>
|
||||
</div>
|
||||
{status === 'running' && (
|
||||
<p className="mt-1 flex items-center gap-1.5 text-2xs text-dim">
|
||||
<Spinner size={11} /> {D.trialRunning}
|
||||
</p>
|
||||
)}
|
||||
{status === 'running' && trialTimedOut && <div className="mt-1"><Notice tone="warn">{D.trialTimeout}</Notice></div>}
|
||||
{status === 'failed' && (
|
||||
<div className="mt-1">
|
||||
<Notice tone="err">{D.trialFailed}:{trial?.error || draft.trial?.error || D.loadFailed}</Notice>
|
||||
</div>
|
||||
)}
|
||||
{status === 'succeeded' && (rows.length > 0 ? (
|
||||
<ul className="mt-1 grid gap-x-4 gap-y-0.5 text-2xs sm:grid-cols-[1fr_auto]">
|
||||
{rows.map((row) => {
|
||||
const unit = unitOf(row.id)
|
||||
return (
|
||||
<li key={row.id} className="contents" data-draft-trial-metric={row.id}>
|
||||
<span className="text-dim [overflow-wrap:anywhere]">{row.name || row.id}</span>
|
||||
<span className="tnum text-right font-medium">
|
||||
{row.rendered ?? (row.value == null ? '—' : String(row.value))}{unit ? ` ${unit}` : ''}
|
||||
</span>
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
) : (
|
||||
<p className="mt-1 text-2xs text-faint">{D.trialNoMetrics}</p>
|
||||
))}
|
||||
{trialError != null && <div className="mt-1"><Notice tone="warn">{D.trialUnavailable}</Notice></div>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/** 入口字段:发布后按这些字段收参数 */
|
||||
const renderEntryFields = (draft: CaliberDraft) => {
|
||||
const fields = draft.entry_fields
|
||||
if (!fields) return null
|
||||
return (
|
||||
<div data-draft-entry-fields="">
|
||||
<h3 className="mb-1 text-2xs font-semibold text-dim">{D.entryFields}</h3>
|
||||
<p className="mb-1 text-2xs text-faint">{D.entryFieldsHint}</p>
|
||||
{fields.length > 0 ? (
|
||||
<ul className="space-y-0.5 text-2xs leading-relaxed">
|
||||
{fields.map((field) => {
|
||||
const hasDefault = field.default !== undefined && field.default !== null && field.default !== ''
|
||||
return (
|
||||
<li key={field.name} className="flex flex-wrap items-baseline gap-x-2" data-draft-entry-field={field.name}>
|
||||
<code className="font-medium text-dim">{field.name}</code>
|
||||
<span className="text-faint">{field.type || 'string'}</span>
|
||||
<span className="text-faint">{field.required ? D.required : D.optional}</span>
|
||||
{hasDefault && <span className="text-faint">{D.defaultValue(defaultText(field.default))}</span>}
|
||||
{field.description && <span className="text-dim [overflow-wrap:anywhere]">{field.description}</span>}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
) : <p className="text-2xs text-faint">{D.entryFieldNone}</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const renderMetric = (metric: DraftMetric, index: number) => {
|
||||
const definition = metric.definition ?? {}
|
||||
const citations = definition.citations ?? []
|
||||
const rows = [
|
||||
[D.unit, metric.unit],
|
||||
[D.decimals, metric.decimals != null ? String(metric.decimals) : ''],
|
||||
[D.grain, metric.grain],
|
||||
[D.timeField, metric.time_field],
|
||||
[D.numerator, metric.numerator],
|
||||
@@ -371,7 +534,7 @@ export function CaliberDrafts() {
|
||||
[D.exclusions, definition.exclusions?.join(';')],
|
||||
].filter(([, value]) => typeof value === 'string' && value.trim()) as [string, string][]
|
||||
return (
|
||||
<li key={`${metric.name}:${index}`} className="rounded-md border bg-panel px-3 py-2" data-draft-metric={metric.name}>
|
||||
<li key={`${metric.id ?? metric.name}:${index}`} className="rounded-md border bg-panel px-3 py-2" data-draft-metric={metric.name}>
|
||||
<div className="flex flex-wrap items-baseline gap-x-2 gap-y-0.5">
|
||||
<span className="text-xs font-medium">{metric.name}</span>
|
||||
{!!metric.synonyms?.length && <span className="text-2xs text-faint">{D.synonyms(metric.synonyms.join('、'))}</span>}
|
||||
@@ -386,7 +549,20 @@ export function CaliberDrafts() {
|
||||
))}
|
||||
</dl>
|
||||
)}
|
||||
{definition.text && <p className="mt-1.5 text-2xs leading-relaxed text-dim">{definition.text}</p>}
|
||||
{metric.expression && (
|
||||
<div className="mt-1.5" data-draft-expression="">
|
||||
<h4 className="text-2xs font-semibold text-faint">{D.expression}</h4>
|
||||
<code className="mt-0.5 block whitespace-pre-wrap rounded bg-elev px-1.5 py-1 text-2xs text-dim [overflow-wrap:anywhere]">
|
||||
{metric.expression}
|
||||
</code>
|
||||
</div>
|
||||
)}
|
||||
{definition.text && (
|
||||
<div className="mt-1.5">
|
||||
<h4 className="text-2xs font-semibold text-faint">{D.definition}</h4>
|
||||
<p className="text-2xs leading-relaxed text-dim">{definition.text}</p>
|
||||
</div>
|
||||
)}
|
||||
{citations.length > 0 && (
|
||||
<div className="mt-1.5">
|
||||
<h4 className="mb-1 text-2xs font-semibold text-faint">{D.citations}</h4>
|
||||
@@ -417,9 +593,10 @@ export function CaliberDrafts() {
|
||||
{draft.created_at && <span title={formatDateTime(draft.created_at)}>{formatTime(draft.created_at)}</span>}
|
||||
{draft.created_by && <span>{D.createdBy(draft.created_by)}</span>}
|
||||
{draft.model_id && <span title={draft.model_id}>{D.model(draft.model_id)}</span>}
|
||||
{draft.workflow_id && draft.status === 'attached' && (
|
||||
{draft.workflow_id && draft.status !== 'rejected' && (
|
||||
<Link to={`/studio/${draft.workflow_id}`} className="text-accent hover:underline" data-draft-workflow="">
|
||||
{D.openWorkflow}{draft.workflow_name ? `:${draft.workflow_name}` : ''}
|
||||
{draft.version != null ? ` · ${D.workflowVersion(draft.version)}` : ''}
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
@@ -439,6 +616,9 @@ export function CaliberDrafts() {
|
||||
{drafting && pollTimedOut && <Notice tone="warn">{D.draftingTimeout}</Notice>}
|
||||
{draft.truncated && <Notice tone="warn">{D.truncated}</Notice>}
|
||||
|
||||
{renderTrial(draft)}
|
||||
{renderEntryFields(draft)}
|
||||
|
||||
<div>
|
||||
<div className="mb-1 flex flex-wrap items-center justify-between gap-2">
|
||||
<h3 className="text-2xs font-semibold text-dim">{D.metrics}</h3>
|
||||
@@ -485,9 +665,18 @@ export function CaliberDrafts() {
|
||||
|
||||
{draft.alignment && <AlignmentBlock alignment={draft.alignment} />}
|
||||
|
||||
{!drafting && !terminal && draft.workflow_id && (
|
||||
<div className="flex flex-wrap items-center gap-2" data-draft-publish="">
|
||||
<Link to={`/studio/${draft.workflow_id}`} className="btn btn-primary btn-sm">
|
||||
<PenLine size={12} aria-hidden /> {D.publish}
|
||||
</Link>
|
||||
<span className="text-2xs text-faint">{D.publishHint}</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!drafting && !terminal && (
|
||||
<div className="rounded-md border bg-elev px-3 py-2" data-draft-attach="">
|
||||
<h3 className="text-2xs font-semibold text-dim">{D.attach}</h3>
|
||||
<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>
|
||||
<div className="mt-1.5 flex flex-wrap items-end gap-2">
|
||||
<label className="min-w-40 flex-1 text-2xs text-faint">
|
||||
@@ -509,12 +698,12 @@ export function CaliberDrafts() {
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={busy === `attach:${draft.id}`}
|
||||
<button type="button" className="btn btn-sm" disabled={busy === `attach:${draft.id}`}
|
||||
onClick={() => void attach(draft)} data-draft-attach-submit="">
|
||||
{busy === `attach:${draft.id}` ? <Spinner size={12} /> : null} {busy === `attach:${draft.id}` ? D.attaching : D.attachAction}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
)}
|
||||
|
||||
{attachOut && (
|
||||
@@ -593,8 +782,8 @@ 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} onChange={(e) => setDatasourceId(e.target.value)}>
|
||||
<option value="">{D.datasourceNone}</option>
|
||||
<select className="field w-full" value={datasourceId} required
|
||||
onChange={(e) => setDatasourceId(e.target.value)}>
|
||||
{sources.map((s) => <option key={s.id} value={s.id}>{s.name}</option>)}
|
||||
</select>
|
||||
<span className="mt-0.5 block">{D.datasourceHint}</span>
|
||||
@@ -604,7 +793,7 @@ export function CaliberDrafts() {
|
||||
<textarea className="field min-h-[64px] w-full text-xs" rows={3} value={guidance}
|
||||
placeholder={D.guidancePlaceholder} onChange={(e) => setGuidance(e.target.value)} />
|
||||
</label>
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={submitting}
|
||||
<button type="button" className="btn btn-primary btn-sm" disabled={submitting || !datasourceId}
|
||||
onClick={() => void submit()} data-draft-submit="">
|
||||
{submitting ? <Spinner size={12} /> : <FileText size={12} aria-hidden />} {submitting ? D.submitting : D.submit}
|
||||
</button>
|
||||
@@ -641,6 +830,8 @@ export function CaliberDrafts() {
|
||||
setOpenId(null)
|
||||
setDetail(null)
|
||||
setPollingId(null)
|
||||
setTrial(null)
|
||||
setTrialPollId(null)
|
||||
} else {
|
||||
void open(draft.id)
|
||||
}
|
||||
|
||||
@@ -441,15 +441,18 @@ export interface CaliberTrialOut {
|
||||
// ---------------------------------------------------------------- 从业务文档起草口径
|
||||
|
||||
/**
|
||||
* 草稿的状态(app/ledger/docdraft.STATUSES):起草中 → 有待确认的问题 / 待接数据路径 → 已接上;
|
||||
* 另有已驳回。path_pending 不是合法图,取数路径只能来自一次真实运行。
|
||||
* 草稿的状态(app/ledger/docdraft.STATUSES):起草中 → 待确认问题 / 待接数据路径 / 待签字发布 → 已接上;
|
||||
* 另有已驳回。ready 表示图已经生成、没有未决问题,可以试运行、可以签字发布;questions_open 也有图和数,
|
||||
* 只是还没清完问题,不阻断试运行。path_pending 是旧路径:图还没生成,取数只能来自一次真实运行。
|
||||
*/
|
||||
export type CaliberDraftStatus = 'drafting' | 'questions_open' | 'path_pending' | 'attached' | 'rejected' | 'failed'
|
||||
export type CaliberDraftStatus = 'drafting' | 'questions_open' | 'path_pending' | 'ready' | 'attached' | 'rejected' | 'failed'
|
||||
|
||||
/** 一条引用:逐字原文、原文所在段落(chunk)、span 是原文里的 [start, end) */
|
||||
/** 一条引用:逐字原文、原文所在段落(chunk)、段号、span 是原文里的 [start, end) */
|
||||
export interface DraftCitation {
|
||||
chunk: string
|
||||
document_id?: string
|
||||
/** 原文所在段落的段号(1 起) */
|
||||
ordinal?: number
|
||||
quote: string
|
||||
span?: [number, number]
|
||||
}
|
||||
@@ -464,11 +467,16 @@ export interface DraftDefinition {
|
||||
citations?: DraftCitation[]
|
||||
}
|
||||
|
||||
/** 草稿里的一条指标:只有业务定义,不含 SQL、表名、列名 */
|
||||
/** 草稿里的一条指标:业务定义 + 取数表达式;SQL 在画布上看,这里不返回。 */
|
||||
export interface DraftMetric {
|
||||
id?: string
|
||||
name: string
|
||||
synonyms?: string[]
|
||||
unit?: string
|
||||
/** 小数位 */
|
||||
decimals?: number
|
||||
/** 指标表达式,形如 cell(nodes.fetch, 0, '列') */
|
||||
expression?: string
|
||||
/** 粒度,如「日」「订单行」 */
|
||||
grain?: string
|
||||
/** 业务时间字段的业务叫法 */
|
||||
@@ -481,6 +489,38 @@ export interface DraftMetric {
|
||||
open_questions?: string[]
|
||||
}
|
||||
|
||||
/** 草稿图的入口字段:发布后按这些字段收参数 */
|
||||
export interface DraftEntryField {
|
||||
name: string
|
||||
type: string
|
||||
required: boolean
|
||||
default?: unknown
|
||||
description?: string
|
||||
}
|
||||
|
||||
/** 起草后自动发起的那次只读试运行(draft.payload.trial) */
|
||||
export interface DraftTrial {
|
||||
run_id: string
|
||||
status: 'running' | 'succeeded' | 'failed'
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** GET /api/ledger/drafts/{id}/trial 的一条:name 和 rendered 一一对应 */
|
||||
export interface DraftTrialMetric {
|
||||
id: string
|
||||
name: string
|
||||
value?: unknown
|
||||
rendered?: string
|
||||
}
|
||||
|
||||
/** GET /api/ledger/drafts/{id}/trial:这次自动试运行的状态和每个指标的值 */
|
||||
export interface DraftTrialOut {
|
||||
run_id: string
|
||||
status: 'running' | 'succeeded' | 'failed'
|
||||
metrics: DraftTrialMetric[]
|
||||
error?: string | null
|
||||
}
|
||||
|
||||
/** 被丢弃的条目和原因(含 SQL 痕迹、引用核对不上) */
|
||||
export interface DraftDropped {
|
||||
name: string
|
||||
@@ -517,9 +557,15 @@ export interface CaliberDraftSummary {
|
||||
export interface CaliberDraft extends CaliberDraftSummary {
|
||||
documents?: DraftDocument[]
|
||||
metrics?: DraftMetric[]
|
||||
/** 草稿图的入口字段 */
|
||||
entry_fields?: DraftEntryField[]
|
||||
dropped?: DraftDropped[]
|
||||
open_questions?: string[]
|
||||
alignment?: DraftAlignment
|
||||
/** 起草后自动发起的那次只读试运行;旧草稿可能没有 */
|
||||
trial?: DraftTrial | null
|
||||
/** 草稿图对应的版本号 */
|
||||
version?: number | null
|
||||
/** 文档超过一次能发给模型的字数,后面的段落没有参与起草 */
|
||||
truncated?: boolean
|
||||
model_id?: string | null
|
||||
|
||||
Reference in New Issue
Block a user