蓝图「看哪些数」对到的数据源和表要真的存在:卡片上照实说是哪一项、怎么改(bp.data_source_unknown / bp.data_table_unknown,带白话),挡住试运行和发布单,助手自查也拿得到;填报登记里本应用的记录表写 @records、表名固定,提示词和模板说明写明这个约定;「对到的表」改成从已接入的数据源和表里选,填报登记多一项「本应用的记录表」,卡片上照白话显示(以前要到试运行才报「绑定的工具不存在」,用户找不到在哪改)

This commit is contained in:
ThearchyHelios
2026-10-15 14:32:25 +08:00
parent 7739340607
commit be845bb823
14 changed files with 329 additions and 36 deletions
+9
View File
@@ -295,6 +295,15 @@ export const BP_OP_TEXT = {
export const BP_EDIT_TEXT = {
source: '数据源',
table: '表名',
/** 填报登记里「看哪些数」对到这个应用自己收上来的记录(服务端写 @records) */
selfRecords: '本应用的记录表',
selfRecordsHint: '这个应用每次提交存下的那张表;发布后系统建好表、换成真名',
pickSource: '选一个数据源',
pickTable: '选一张表',
tablesLoading: '正在读取表…',
/** 读不到表的列表(没读取过表结构、或没有权限)时直接填表名 */
tableManual: '读不到这个数据源的表,请直接填表名',
sourcesFailed: '读不到数据源列表,请直接填数据源名和表名',
column: '日期列',
rule: '按什么算(例如按支付日)',
preset: '频率',
@@ -25,7 +25,7 @@ import { chatBusy, useBlueprint } from '../../store/blueprint'
import type {
Blueprint, BpDataItem, BpField, BpItem, BpOp, BpStep, ItemStatus, JointState, PlainIssue, TemplateInfo,
} from '../../typesBlueprint'
import { ValueEditor, valueText, type EditorKind, type EditorScope } from './editors'
import { SELF_RECORDS, ValueEditor, valueText, type EditorKind, type EditorScope } from './editors'
import { isItem, missingPaths, visibleCards, type CardKey } from './shared'
const STATUS_COLOR: Record<ItemStatus, string> = {
@@ -115,6 +115,7 @@ export function BlueprintCards({ blueprintId, templates }: { blueprintId: string
scope: {
fields: spec.fields.map((f) => ({ name: f.name, title: String((f.spec?.value as Record<string, unknown> | null)?.title ?? f.name) })),
metrics: spec.data.map((d) => ({ id: d.id, title: String(d.label?.value ?? d.id) })),
recordsSelf: (spec.template?.id ?? bp.template_id) === 'records',
},
}
return (
@@ -353,7 +354,7 @@ function DataBlock({ ctx, item, index }: { ctx: CardCtx; item: BpDataItem; index
<div className="mb-1.5 rounded border bg-bg px-2 py-1.5 text-2xs leading-relaxed" data-bp-match={`${table.source}.${table.table}`}>
<div className="flex flex-wrap items-center gap-x-3 gap-y-0.5">
<span className="inline-flex items-center gap-1 text-fg"><Database size={11} aria-hidden className="text-faint" />
{BLUEPRINT_TEXT.dataTable}:<span className="mono">{table.source}.{table.table}</span></span>
{BLUEPRINT_TEXT.dataTable}:<span className={table.source === SELF_RECORDS ? '' : 'mono'}>{valueText('table', table)}</span></span>
{item.grain?.value != null && <span>{BLUEPRINT_TEXT.dataGrain}:<span className="text-fg">{String(item.grain.value)}</span></span>}
{date?.column && <span>{BLUEPRINT_TEXT.dataDate}:<span className="text-fg">{valueText('date', date)}</span></span>}
</div>
+85 -9
View File
@@ -7,7 +7,7 @@
* agentlab.form/1 的一个属性另加是否必填;字段对应的取值是表单字段(form.x)、指标(metric.y)、核对出来的行(rows)或
* 常量(文字加单引号)。说法都是白话:时限写「2 天 3 小时」,存的是 ISO 8601 时长;处理人写名字,不写 kind。
*/
import { useState } from 'react'
import { useEffect, useState } from 'react'
import type { ReactNode } from 'react'
import { Plus, X } from 'lucide-react'
import { PrincipalPicker, type Principal as PickerPrincipal } from '../../components/PrincipalPicker'
@@ -17,17 +17,23 @@ import {
TIMEOUT_LABEL, WEEKDAY_LABEL, cadenceText, durationIso, durationText, policyText, principalText,
} from '../../lib/terms'
import type { BpPrincipal } from '../../typesBlueprint'
import type { DataSourceName } from '../../types'
import { api } from '../../api/client'
export type EditorKind =
| 'text' | 'textarea' | 'table' | 'date' | 'cadence' | 'principals' | 'grantees' | 'policy' | 'duration' | 'timeout'
| 'threshold' | 'field' | 'bool' | 'mapping'
/** 编辑框要的选项:「填什么」里的字段、「看哪些数」里的指标(金额线、字段对应从里面选) */
/** 编辑框要的选项:「填什么」里的字段、「看哪些数」里的指标(金额线、字段对应从里面选);填报登记能对到本应用的记录表 */
export interface EditorScope {
fields: { name: string; title: string }[]
metrics: { id: string; title: string }[]
recordsSelf?: boolean
}
/** 「看哪些数」里指这个应用自己的记录表(服务端 blueprints/store.SELF_RECORDS),表名固定 */
export const SELF_RECORDS = '@records'
type Obj = Record<string, any>
const obj = (v: unknown): Obj => (v && typeof v === 'object' && !Array.isArray(v) ? v as Obj : {})
const principals = (v: unknown): BpPrincipal[] => (Array.isArray(v) ? v.filter((p) => p && typeof p.name === 'string') : [])
@@ -79,7 +85,9 @@ export function valueText(kind: EditorKind, value: unknown, scope?: EditorScope)
if (value == null || value === '') return BLUEPRINT_TEXT.empty
const o = obj(value)
switch (kind) {
case 'table': return o.source || o.table ? `${o.source ?? ''}.${o.table ?? ''}` : BLUEPRINT_TEXT.empty
case 'table':
if (o.source === SELF_RECORDS) return BP_EDIT_TEXT.selfRecords
return o.source || o.table ? `${o.source ?? ''}.${o.table ?? ''}` : BLUEPRINT_TEXT.empty
case 'date': return o.column ? `${o.column}${o.rule ? `(${o.rule})` : ''}` : BLUEPRINT_TEXT.empty
case 'cadence': return cadenceText(o) || BLUEPRINT_TEXT.empty
case 'principals':
@@ -160,17 +168,85 @@ function TextEditor({ kind, value, busy, onSave, onCancel }: ValueEditorProps) {
)
}
function TableEditor({ value, busy, onSave, onCancel }: ValueEditorProps) {
/**
* 对到哪张表:先选数据源(已接入的,填报登记另有「本应用的记录表」),再选表(读这个数据源读取过的表结构)。读不到列表时
* 退回直接填写——合不合规照旧由服务端判,对到不存在的源、表由蓝图检查在卡片上照实说
*/
function TableEditor({ value, busy, onSave, onCancel, scope }: ValueEditorProps) {
const o = obj(value)
const [source, setSource] = useState(String(o.source ?? ''))
const [table, setTable] = useState(String(o.table ?? ''))
const [table, setTable] = useState(String(o.source === SELF_RECORDS ? 'records' : o.table ?? ''))
const [sources, setSources] = useState<DataSourceName[] | null>(null)
const [tables, setTables] = useState<string[] | null>(null)
const [loadingTables, setLoadingTables] = useState(false)
const [manual, setManual] = useState(false)
useEffect(() => {
let alive = true
api.datasources.names()
.then((list) => { if (alive) setSources(list.filter((x) => x.enabled !== false)) })
.catch(() => { if (alive) { setSources([]); setManual(true) } })
return () => { alive = false }
}, [])
const picked = sources?.find((x) => x.name === source)
useEffect(() => {
setTables(null)
if (!picked) return
let alive = true
setLoadingTables(true)
api.datasources.schema(picked.id)
.then((r) => { if (alive) setTables(Array.isArray(r?.tables) ? r.tables.map(String) : []) })
.catch(() => { if (alive) setTables([]) })
.finally(() => { if (alive) setLoadingTables(false) })
return () => { alive = false }
}, [picked?.id]) // eslint-disable-line react-hooks/exhaustive-deps
const self = source === SELF_RECORDS
const ok = self || (!!source.trim() && !!table.trim())
const save = () => onSave(self ? { source: SELF_RECORDS, table: 'records' } : { source: source.trim(), table: table.trim() })
if (manual) {
return (
<Shell busy={busy} onCancel={onCancel} ok={ok} onSave={save}>
<p className="text-2xs text-dim" data-bp-table-manual="">{BP_EDIT_TEXT.sourcesFailed}</p>
<div className="grid grid-cols-2 gap-2">
<Field label={BP_EDIT_TEXT.source}>{(p) => <input {...p} className="field" value={source} onChange={(e) => setSource(e.target.value)} />}</Field>
<Field label={BP_EDIT_TEXT.table}>{(p) => <input {...p} className="field" value={table} onChange={(e) => setTable(e.target.value)} />}</Field>
</div>
</Shell>
)
}
return (
<Shell busy={busy} onCancel={onCancel} ok={!!source.trim() && !!table.trim()}
onSave={() => onSave({ source: source.trim(), table: table.trim() })}>
<Shell busy={busy} onCancel={onCancel} ok={ok} onSave={save}>
<div className="grid grid-cols-2 gap-2">
<Field label={BP_EDIT_TEXT.source}>{(p) => <input {...p} className="field" value={source} onChange={(e) => setSource(e.target.value)} />}</Field>
<Field label={BP_EDIT_TEXT.table}>{(p) => <input {...p} className="field" value={table} onChange={(e) => setTable(e.target.value)} />}</Field>
<Field label={BP_EDIT_TEXT.source}>
{(p) => (
<select {...p} className="field" value={source} data-bp-table-source="" disabled={sources == null}
onChange={(e) => { setSource(e.target.value); setTable(e.target.value === SELF_RECORDS ? 'records' : '') }}>
<option value="">{sources == null ? BP_EDIT_TEXT.tablesLoading : BP_EDIT_TEXT.pickSource}</option>
{scope?.recordsSelf && <option value={SELF_RECORDS}>{BP_EDIT_TEXT.selfRecords}</option>}
{/* 原来填的源不在列表里(不存在、已停用)也列出来,看得出要换 */}
{source && source !== SELF_RECORDS && sources && !picked && <option value={source}>{source}</option>}
{(sources ?? []).map((x) => (
<option key={x.id} value={x.name}>{x.description ? `${x.name}(${x.description})` : x.name}</option>
))}
</select>
)}
</Field>
<Field label={BP_EDIT_TEXT.table}>
{(p) => self ? (
<input {...p} className="field" value={BP_EDIT_TEXT.selfRecords} disabled title={BP_EDIT_TEXT.selfRecordsHint} />
) : tables && tables.length ? (
<select {...p} className="field" value={table} data-bp-table-name="" onChange={(e) => setTable(e.target.value)}>
<option value="">{BP_EDIT_TEXT.pickTable}</option>
{table && !tables.includes(table) && <option value={table}>{table}</option>}
{tables.map((t) => <option key={t} value={t}>{t}</option>)}
</select>
) : (
<input {...p} className="field" value={table} disabled={!source || loadingTables} data-bp-table-name=""
placeholder={loadingTables ? BP_EDIT_TEXT.tablesLoading : picked ? BP_EDIT_TEXT.tableManual : ''}
onChange={(e) => setTable(e.target.value)} />
)}
</Field>
</div>
{self && <p className="text-2xs text-dim">{BP_EDIT_TEXT.selfRecordsHint}</p>}
</Shell>
)
}