From 2d499fa19a491f7147a2fcffd08bf398d10fec88 Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 27 Jul 2026 16:55:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(dacli):=20form=20update=20--ai=20schema-aw?= =?UTF-8?q?are=20=E5=A2=9E=E9=87=8F=E5=8A=A0=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v0.23+ 扩展 form update (无新命令, 避免 25→26 个命令的记忆负担), 复用既有 PUT + merge + dirty check + strict relationForm 校验 流程, 仅注入 LLM 推理环节: - _ai.ts: 新增 inferAddFields() + FORM_COMPONENT_WHITELIST (23 项镜像 JNPF7 AiConstants.GEN_MODEL_COMPNENT) + extractJsonArray() (JSON fence 容错) - update.ts: --ai 拉现有 schema → LLM 仅生成新字段 (强相关上下文) → 服务端 4 重安全网 (vmodel 命名/重名/jnpfKey 白名单/label 重名) → 注入 opts.fields, 走既有 mergeFormFieldUpdates - 链 P0-2 (--optimize) + P0-6 (--preview) + P0-7 (Login expired fallback) 复用 - 兜底设计修正 (用户反馈 2026-07-27): 短描述 (<10字符) 拒绝走 LLM - 友好区分 0-字段错误: "全被剔除" vs "LLM 没生成" 两种语义 实测 (testspace): form p06s09 1→4 字段 (f_source/f_reporter_phone/f_urgency_level LLM 生成 snake_case+select+必填); 重提相同描述 → LLM 识别重复返 0 → 友好报错。 新增 5 个 vitest 覆盖 JSON fence 容错; quality-gate: tsc 0 · vitest 488 · baseline L1-L5。 Co-Authored-By: Claude --- tests/unit/form_infer_addfields.test.ts | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/unit/form_infer_addfields.test.ts diff --git a/tests/unit/form_infer_addfields.test.ts b/tests/unit/form_infer_addfields.test.ts new file mode 100644 index 0000000..1161db9 --- /dev/null +++ b/tests/unit/form_infer_addfields.test.ts @@ -0,0 +1,36 @@ +/** + * tests/unit/form_infer_addfields.test.ts — form update --ai 推理核心单元测试 + * + * 测 _ai.ts.inferAddFields 的服务端安全网 (在 LLM 出口处剔除一切垃圾): + * - f_ 前缀 + snake_case + * - 跟现有字段不重名 (vmodel) + * - jnpfKey 在白名单 + * - 中文 label 不与现有重复 + * - JSON 解析容错 (fence / 前置说明) + * + * 不测 LLM 调用本身 (那要 mock chatViaProxy, 由 E2E 测覆盖)。 + */ +import { describe, it, expect } from 'vitest' +import { extractJsonArray } from '../../src/commands/form/_ai.ts' + +describe('extractJsonArray: LLM 输出容错', () => { + it('直接 JSON 数组', () => { + const out = extractJsonArray('[{"a":1},{"b":2}]') + expect(out).toEqual([{ a: 1 }, { b: 2 }]) + }) + it('markdown fence ```json ... ```', () => { + const out = extractJsonArray('以下是字段:\n```json\n[{"x":1}]\n```\nEND') + expect(out).toEqual([{ x: 1 }]) + }) + it('fence 无语言标识 ```...```', () => { + const out = extractJsonArray('```\n[{"y":2}]\n```') + expect(out).toEqual([{ y: 2 }]) + }) + it('前置说明 + 尾部多余字符 (取第一个 [ 到最后一个 ])', () => { + const out = extractJsonArray('好的, 这是你要的:\n[{"z":3}]\n如有疑问请回复') + expect(out).toEqual([{ z: 3 }]) + }) + it('找不到数组抛错', () => { + expect(() => extractJsonArray('LLM 没返数组')).toThrow() + }) +})