From e04bcb32162cad96bc8c27f74dd566ab11e947e7 Mon Sep 17 00:00:00 2001 From: ssw Date: Tue, 28 Jul 2026 20:29:20 +0800 Subject: [PATCH] auto(14b5ea1b9bd9): session end @ 2026-07-28T20:29:19 (+2f) --- src/commands/form/_smart_props.ts | 111 +++++++++++++++++++++++++++++- src/commands/form/update.ts | 47 +++++++++++++ 2 files changed, 157 insertions(+), 1 deletion(-) diff --git a/src/commands/form/_smart_props.ts b/src/commands/form/_smart_props.ts index e53ce11..21ee63f 100644 --- a/src/commands/form/_smart_props.ts +++ b/src/commands/form/_smart_props.ts @@ -438,4 +438,113 @@ export function applyHeuristicRequired(formData: any): { formData.fields = fields } return { applied, alreadyHad } -} \ No newline at end of file +} + +/** + * 启发式 select → dictType 绑定 (BS-新141, jxcj M2 2026-07-23 实战) + * + * 背景: --ai 单次调用只决定 jnpfKey (select / relationForm / ...), 但不绑具体数据源 id + * (dictType / modelId / relationField). smartProps AI 调用 /ssw/ai/infer-component-props + * 经常会因 NCPF/SSW 后端 LLM token 失效 Login expired 而失败 (BS-新113 P1-7). + * + * 修复: select 字段按字段名模糊匹配 ctx.dicts enCode, 自动写 dictionaryType = dict.id. + * 不依赖 AI, 100% 确定. 跟 applyHeuristicRelationForm 并列. + * + * 匹配规则 (jxcj M2 验证实验 849913609722004741 dictType 永远空): + * - 字段名去除 f_ 前缀 + _type/_field/_status 等后缀, 跟 ctx.dicts enCode 互相包含 (case-insensitive) + * - 0 候选 → skipped (用户 web 端补) + * - 多候选 → 优先 enCode 全匹配, 否则警告 (e.g. f_status 可匹配 jxcjjudgestatus / jxcjauditstate) + * + * @returns { applied: 写入 dictType 的字段数, matched: 已绑跳过, skipped: 无候选 } + */ +export function applyHeuristicDictType( + formData: any, + ctx: SmartPropsContext, +): { applied: number; matched: number; skipped: number } { + let fields: any[] = [] + let isStringBacked = false + if (typeof formData?.fields === 'string') { + try { + fields = JSON.parse(formData.fields) + isStringBacked = true + } catch { + return { applied: 0, matched: 0, skipped: 0 } + } + } else if (Array.isArray(formData?.fields)) { + fields = formData.fields + } + if (fields.length === 0) return { applied: 0, matched: 0, skipped: 0 } + + let applied = 0 + let matched = 0 + let skipped = 0 + for (const fld of fields) { + const cfg = fld?.__config__ + const vm = fld?.__vModel__ || '' + if (!cfg || !vm) continue + // 只处理 select 字段 + if (cfg.jnpfKey !== 'select') continue + // 已绑 dictType 跳过 + if (cfg.dictionaryType) { + matched++ + continue + } + + const vmClean = vm.toLowerCase().replace(/^f_/, '').replace(/_/g, '') + // 强匹配: 字段名去前后缀后含 dict enCode (去下划线) + let candidates = ctx.dicts.filter((d) => { + const encClean = d.enCode.toLowerCase().replace(/_/g, '') + return vmClean.includes(encClean) || encClean.includes(vmClean) + }) + + if (candidates.length === 0) { + skipped++ + continue + } + + // 多候选时优先 enCode 完全包含 (e.g. f_judge_status 优先 jxcjjudgestatus 而非 jxcjteamauditstatus) + let target = candidates.find((d) => vmClean.includes(d.enCode.toLowerCase().replace(/_/g, ''))) + || candidates[0] + if (candidates.length > 1) { + log.warn(` ↳ BS-新141 ${vm}: 多个候选字典 [${candidates.map((c) => c.enCode).join(', ')}], 选 ${target.enCode}`) + } + + // 写 dictType (JNPF schema 要求 dict id) + cfg.dictionaryType = target.id + applied++ + matched++ + } + + // 写回 + if (isStringBacked) { + formData.fields = JSON.stringify(fields) + } else { + formData.fields = fields + } + if (applied > 0) { + log.info(` ↳ BS-新141 applyHeuristicDictType 转换 ${applied} 字段 (matched=${matched}, skipped=${skipped})`) + } + return { applied, matched, skipped } +} + +// ============================================================================ +// SMART_PROPS stable public API (BS-新141, jxcj M2 2026-07-23 实战) +// ============================================================================ + +/** + * 公开 API 集合. create.ts / update.ts 都用这个, 避免散落调用. + * @example + * import { SMART_PROPS } from './_smart_props.ts' + * const ctx = await SMART_PROPS.loadContext(c) + * const { formData, applied } = await SMART_PROPS.applyAi(formData, ctx, { verbose: true }) + * SMART_PROPS.applyHeuristics(formData, ctx) // input FK → relationForm + * SMART_PROPS.applyDictType(formData, ctx) // select → 启发式绑 dictType + * SMART_PROPS.applyRequired(formData) // label/name → required + */ +export const SMART_PROPS = { + loadContext: loadSmartPropsContext, + applyAi: applySmartProps, + applyHeuristics: applyHeuristicRelationForm, + applyDictType: applyHeuristicDictType, + applyRequired: applyHeuristicRequired, +} as const \ No newline at end of file diff --git a/src/commands/form/update.ts b/src/commands/form/update.ts index 17473bd..5ac2ef6 100644 --- a/src/commands/form/update.ts +++ b/src/commands/form/update.ts @@ -15,6 +15,8 @@ import { dirtyCheck, dirtyCheckFailAndExit, DirtyCheckError, logDirtyCheckSkip, import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts' import { setupOutputFilter, addOutputOptions, wrapCommand } from '../_output_filter.ts' // v2.1+ 输出过滤; v2.2+ wrapCommand 收口 5c import { applyNormalizedPatch, validateStrictSchema, closeFormContract, buildGetFormAdapter, FormSchemaInvalidError, FORM_SCHEMA_INVALID_TAG, type FormField } from './_schema_normalize.ts' // schema 归一化 + 严格校验 + T3 闭环 (webapi hardening) +import { SMART_PROPS } from './_smart_props.ts' // v2.5+ BS-新141: form update 集成 smartProps (跟 create 一致) +import { isLoginExpired } from '../_ssw_retry.ts' // P1-7 Login expired 检测 (BS-新113) interface Opts { id: string @@ -43,6 +45,8 @@ interface Opts { json?: boolean /** v2.1+ (2026-07-21): --verbose, addOutputOptions 注入 */ verbose?: boolean + /** v2.5+ BS-新141 (jxcj M2 2026-07-23): form update 集成 smartProps (跟 create 一致, AI 失败 tolerant) */ + smartProps?: boolean } export function mergeFormFieldUpdates(existing: unknown[], updates: unknown[]): any[] { @@ -122,6 +126,9 @@ export function buildFormUpdateCommand(): Command { .option('--force', '跳过 dirty check, 强制覆盖 (慎用!)', false) .option('--strict', 'deprecated since v0.23: strict 永远跑, 此选项保留仅为向后兼容, 不再生效 (统一走 validateStrictSchema)', true) .option('--no-strict', 'deprecated since v0.23: 已 no-op, strict 永远跑. 仅作为迁移诊断选项保留') + // v2.5+ BS-新141 (jxcj M2 2026-07-23): form update --fields 集成 smartProps (跟 create 一致) + .option('--smart-props', 'PUT 前跑 SMART_PROPS (AI 失败 tolerant, 启发式兜底 + 接受手工 JSON)', true) + .option('--no-smart-props', '不跑 SMART_PROPS (跟 create --no-smart-props 一致)') addOutputOptions(cmd) .action(async (opts: Opts) => { setupOutputFilter(opts) @@ -337,6 +344,46 @@ export function buildFormUpdateCommand(): Command { } body.formData = JSON.stringify(formData) log.info(`字段合并: ${existingFields.length} → ${merged.length} (按 vmodel/__vModel__ 增量合并)`) + + // v2.5+ BS-新141 (jxcj M2 2026-07-23): 集成 SMART_PROPS (跟 create 一致) + // AI 失败 tolerant (Login expired 不阻塞, 启发式兜底 + 接受手工 JSON) + // 必须在 PUT 前跑, 这样 heuristics 自动补 dictType/modelId/relationField + if (opts.smartProps !== false) { + try { + const ctx = await SMART_PROPS.loadContext(c) + // formData.fields 已是字符串 (上面 JSON.stringify 过的), SMART_PROPS 内部会 parse + let fdParsed = typeof formData.fields === 'string' ? JSON.parse(formData.fields as string) : (formData.fields as any) || {} + // 1) AI 推理 (best-effort, Login expired 时 audit 兜底) + try { + const { formData: fd2, applied } = await SMART_PROPS.applyAi(fdParsed, ctx, { verbose: opts.verbose }) + fdParsed = fd2 + if (applied.length > 0) { + log.info(` ↳ smartProps AI: ${applied.length} 字段应用 (${applied.map((a) => a.field).join(', ')})`) + } else { + log.warn(` ↳ smartProps AI: 0 字段应用 (NCPF/SSW 后端可能 Login expired, 走启发式兜底)`) + } + } catch (aiErr) { + // AI 失败不 throw, 走启发式 (BS-新141 设计原则) + if (isLoginExpired(aiErr as { code?: number; msg?: string })) { + log.warn(` ↳ smartProps AI Login expired (BS-新113), 跳过 AI, 走启发式兜底`) + } else { + log.warn(` ↳ smartProps AI 失败 (${(aiErr as Error).message?.slice(0, 80)}), 走启发式兜底`) + } + } + // 2) 启发式 (不依赖 AI, 100% 确定) + const h1 = SMART_PROPS.applyHeuristics(fdParsed, ctx) + const h2 = SMART_PROPS.applyDictType(fdParsed, ctx) // BS-新141 新增 + const h3 = SMART_PROPS.applyRequired(fdParsed) + log.info(` ↳ smartProps 启发式: relationForm=${h1.applied} dictType=${h2.applied} required=${h3.applied}`) + // 写回 formData (SMART_PROPS 内部会序列化) + formData.fields = fdParsed.fields + body.formData = JSON.stringify(formData) + } catch (e) { + log.warn(`smartProps 整体失败 (${(e as Error).message?.slice(0, 100)}), 用原 formData 继续 PUT`) + } + } else { + log.info(` ↳ --no-smart-props: 跳过 SMART_PROPS 集成`) + } } // === step 3 PUT / 后端API ===