feat: support configurable card variables and layouts

This commit is contained in:
2026-08-14 13:36:53 +08:00
parent f952d6dc58
commit 77399df060
14 changed files with 619 additions and 88 deletions

View File

@@ -114,3 +114,86 @@ def test_ai_generation_uses_configured_instruction_and_only_accepts_allowed_fiel
assert "unknown" not in generated
assert "优先保留用户原话" in captured["prompt"]
assert "不得分析人格" in captured["prompt"]
def test_custom_variables_drive_prompt_rendering_and_ignore_extra_model_fields(monkeypatch):
variables = [
{
"name": "student",
"label": "学员",
"description": "直接显示当前学员名称",
"valueSource": "context",
"sourceKey": "student_name",
"sampleValue": "示例学员",
},
{
"name": "key_takeaway",
"label": "关键收获",
"description": "用第一人称提炼材料中已经明确表达的一条关键收获",
"valueSource": "ai",
"sourceKey": None,
"sampleValue": "我开始看见自己在着急确认答案。",
},
]
captured: dict[str, str] = {}
def fake_generate(db, *, prompt, scenario, user_id):
captured["prompt"] = prompt
return SimpleNamespace(answer='{"key_takeaway":"我看见自己会着急判断对错。","extra":"不能使用"}')
monkeypatch.setattr(TrackedGenerationService, "generate", fake_generate)
with _db() as db:
generated, used_fallback = ContentGenerationConfigService.generate_values(
db,
config_type="help_card",
instruction_content="忠实整理",
variables=variables,
values={"student_name": "小千", "summary": "我总想马上判断对错。"},
user_id=1,
)
content = ContentGenerationConfigService.render(
"help_card",
"学员:{{student}}\n收获:{{key_takeaway}}",
generated,
variables,
)
assert used_fallback is False
assert generated == {"student": "小千", "key_takeaway": "我看见自己会着急判断对错。"}
assert "关键收获" in captured["prompt"]
assert "用第一人称提炼" in captured["prompt"]
assert "学员:小千" in content
assert "收获:我看见自己会着急判断对错。" in content
def test_custom_variable_versions_are_saved_and_restored_together():
variables = [
{
"name": "custom_summary",
"label": "我的总结",
"description": "提炼用户自己的总结",
"valueSource": "ai",
"sourceKey": None,
"sampleValue": "示例总结",
}
]
with _db() as db:
saved = ContentGenerationConfigService.save(
db,
config_type="share_draft",
template_content="总结:{{custom_summary}}",
instruction_content="只整理明确内容",
variables=variables,
updated_by=1,
)
db.commit()
restored = ContentGenerationConfigService.restore(
db,
config_type="share_draft",
source_config_id=saved.id,
updated_by=2,
)
db.commit()
restored_variables_json = restored.variables_json
assert json.loads(restored_variables_json)[0]["name"] == "custom_summary"