feat(api+app): 对话阶段化、回忆录流水线与客户端会话体验
- DB: segments 用户输入文本(Alembic 0002) - Chat: 阶段检测/阶段提示/回复限制,编排与访谈/画像 prompts 调整 - Memoir: 忠实度检查 agent,叙事与分类等链路更新 - Core: agent 日志、Alembic 启动、LangChain/日志/配置等 - Story: time_hints;Memory 检索与相关测试 - Expo: 助手头像、会话页与消息拆分、实时会话与文案/i18n - Docs/scripts/tests: 迁移脚本、LLM JSON/记忆检索文档、新增单测
This commit is contained in:
103
api/tests/test_json_and_memory_utils.py
Normal file
103
api/tests/test_json_and_memory_utils.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""JSON 载荷解析、证据格式化、Story 批量规划校验(纯函数)。"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.agents.chat.reply_limits import truncate_chat_segments
|
||||
|
||||
from app.agents.memoir.classification_agent import _normalize_llm_category
|
||||
from app.agents.memoir.prompts import format_evidence_chunks_for_prompt
|
||||
from app.agents.memoir.story_route_agent import (
|
||||
StoryBatchPlan,
|
||||
StoryBatchPlanUnit,
|
||||
validate_story_batch_plan,
|
||||
)
|
||||
from app.features.memoir.memoir_images.json_payload import extract_json_payload
|
||||
|
||||
|
||||
def test_extract_json_payload_strips_markdown_fence() -> None:
|
||||
raw = """```json
|
||||
{"a": 1}
|
||||
```"""
|
||||
assert '"a"' in extract_json_payload(raw)
|
||||
|
||||
|
||||
def test_normalize_llm_category_strips_quotes() -> None:
|
||||
assert _normalize_llm_category('"childhood"') == "childhood"
|
||||
assert _normalize_llm_category("`beliefs`") == "beliefs"
|
||||
|
||||
|
||||
def test_format_evidence_chunks_includes_timeline() -> None:
|
||||
ev = {
|
||||
"relevant_chunks": [{"content": "chunk1"}],
|
||||
"relevant_facts": [
|
||||
{"subject": "我", "predicate": "生于", "object_json": "1950"}
|
||||
],
|
||||
"timeline_hints": [
|
||||
{
|
||||
"id": "1",
|
||||
"event_year": 1977,
|
||||
"event_date": None,
|
||||
"title": "恢复高考",
|
||||
"description": "参加了考试",
|
||||
}
|
||||
],
|
||||
"relevant_summaries": [],
|
||||
"relevant_stories": [],
|
||||
}
|
||||
out = format_evidence_chunks_for_prompt(ev)
|
||||
assert "chunk1" in out
|
||||
assert "1950" in out or "生于" in out
|
||||
assert "1977" in out or "恢复高考" in out
|
||||
|
||||
|
||||
def test_validate_story_batch_plan_ok() -> None:
|
||||
ordered = ["s1", "s2"]
|
||||
plan = StoryBatchPlan(
|
||||
units=[
|
||||
StoryBatchPlanUnit(
|
||||
segment_ids=["s1", "s2"],
|
||||
decision="new_story",
|
||||
target_story_id=None,
|
||||
new_story_title="标题",
|
||||
reason=None,
|
||||
)
|
||||
]
|
||||
)
|
||||
ok, err = validate_story_batch_plan(ordered, plan, valid_story_ids=set())
|
||||
assert ok is True
|
||||
assert err is None
|
||||
|
||||
|
||||
def test_truncate_chat_segments() -> None:
|
||||
out = truncate_chat_segments(
|
||||
["a" * 300, "b"],
|
||||
max_segments=2,
|
||||
max_chars_per_segment=220,
|
||||
)
|
||||
assert out[0] == "a" * 219 + "…"
|
||||
assert len(out[0]) == 220
|
||||
assert out[1] == "b"
|
||||
|
||||
|
||||
def test_validate_story_batch_plan_duplicate_segment() -> None:
|
||||
plan = StoryBatchPlan(
|
||||
units=[
|
||||
StoryBatchPlanUnit(
|
||||
segment_ids=["s1"],
|
||||
decision="new_story",
|
||||
target_story_id=None,
|
||||
new_story_title="A",
|
||||
reason=None,
|
||||
),
|
||||
StoryBatchPlanUnit(
|
||||
segment_ids=["s1"],
|
||||
decision="new_story",
|
||||
target_story_id=None,
|
||||
new_story_title="B",
|
||||
reason=None,
|
||||
),
|
||||
]
|
||||
)
|
||||
ok, err = validate_story_batch_plan(["s1", "s1"], plan, valid_story_ids=set())
|
||||
assert ok is False
|
||||
assert err == "duplicate_segment"
|
||||
Reference in New Issue
Block a user