Files
life-echo/api/tests/test_image_prompt_policy.py
Kevin bb16d3a5c9 refactor(agents): 抽取阶段常量与对话上下文;快档 LLM;图片 prompt 可禁止回退
访谈与阶段
- 新增 app/agents/stage_constants.py:集中 CHAT_STAGES、章节分类/顺序、阶段到默认 memoir 类别等,与 MemoirState 默认槽位顺序对齐;减少散落在 prompts 内的重复常量。
- 新增 app/agents/chat/prompt_context.py:以 ChatPromptContext 汇总 guided 系统提示所需字段(阶段、槽位、轮次、人设、记忆证据、回复长度模式、背景声线、职业等),统一走 get_guided_conversation_prompt。
- 大幅收敛 app/agents/chat/prompts_conversation.py;调整 prompts.py、stage_prompts.py、stage_detection.py;同步 interview_agent、profile_agent、helpers 与 state_schema,使对话侧构造提示的方式一致、可测。

回忆录流水线
- memoir/prompts.py 删除已迁至 stage_constants / 独立模板的大段常量与图片占位相关逻辑;classification / extraction / fidelity / narrative agents 与 orchest(全量历史仍可用于计数,注入模型时按轮次与字符上限截断)、image_prompt_fallback_disabled。
- dependencies 增加 get_llm_provider_fast(LRU 缓存,可与默认共用密钥与 base_url)。

任务与编排
- memoir_tasks:prepare_batches 注入 llm_fast;开启独立快档模型时打结构化日志。
- chapter_cover_tasks、story_image_tasks:与图片 prompt / JSON 工具路径或策略变更对齐(import 与行为一致)。
- story_pipeline_sync 等小处同步。

其它核心
- langchain_llm、text_normalize 随上述调用链微调。

开发者体验
- .cursor/settings.json:启用 redis-development、postman 插件。

测试
- 新增 test_image_prompt_policy:覆盖「禁止回退」等图片 prompt 策略。
- 更新 test_interview_prompts、test_interview_reply_length、test_experience_regressions、test_json_and_memory_utils,匹配新常量位置、json_utils 与对话/长度行为。
2026-04-02 12:00:00 +08:00

124 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""MemoirImagePromptService 与 image_prompt_fallback_disabledstory 主图 / 章节封面)。"""
import json
import pytest
from app.features.memoir.memoir_images.prompting import MemoirImagePromptService
from app.features.memoir.memoir_images.settings import MemoirImageSettings
def _svc(llm=None) -> MemoirImagePromptService:
return MemoirImagePromptService(llm=llm, settings=MemoirImageSettings())
def test_story_primary_fallback_uses_placeholder_when_llm_disabled():
from app.features.memoir.memoir_images.image_placeholder_template import (
IMAGE_PLACEHOLDER_TEMPLATE,
)
out = _svc(llm=None).build_story_primary_prompt(
"My Story", "career", "rainy street", None
)
assert IMAGE_PLACEHOLDER_TEMPLATE in out["prompt"]
assert "rainy street" in out["prompt"]
def test_story_primary_fallback_disabled_requires_brief(monkeypatch):
monkeypatch.setattr(
"app.features.memoir.memoir_images.prompting.settings.image_prompt_fallback_disabled",
True,
)
with pytest.raises(RuntimeError, match="prompt_brief"):
_svc(llm=None).build_story_primary_prompt("T", "career", "", None)
def test_story_primary_style_from_chapter_category_when_no_intent_style():
"""Story.stage 存章节 category 时,未指定 style_profile 应走 CATEGORY_STYLE_MAP。"""
out = _svc(llm=None).build_story_primary_prompt("T", "career_early", "brief", None)
assert out["style"] == "realistic"
def test_story_primary_style_from_chat_stage_when_no_intent_style():
out = _svc(llm=None).build_story_primary_prompt("T", "career", "brief", None)
assert out["style"] == "realistic"
def test_story_primary_fallback_disabled_requires_llm(monkeypatch):
monkeypatch.setattr(
"app.features.memoir.memoir_images.prompting.settings.image_prompt_fallback_disabled",
True,
)
with pytest.raises(RuntimeError, match="requires LLM"):
_svc(llm=None).build_story_primary_prompt("T", "career", "brief", None)
def test_story_primary_llm_path(monkeypatch):
class _FakeLlm:
def invoke(self, *_a, **_kw): # noqa: ANN001
raise AssertionError("invoke should not be used")
captured: dict[str, str] = {}
def fake_invoke_json(llm, prompt_text, **_kw): # noqa: ANN001
captured["prompt_text"] = prompt_text
return json.dumps(
{
"prompt": "A hero scene at dawn",
"style": "watercolor",
"size": "1280x720",
}
)
monkeypatch.setattr(
"app.features.memoir.memoir_images.prompting.invoke_json_object",
fake_invoke_json,
)
out = _svc(llm=_FakeLlm()).build_story_primary_prompt(
"Title", "childhood", "memo", "realistic"
)
assert out["prompt"].lower().startswith("watercolor")
assert "dawn" in out["prompt"].lower()
assert out["style"] == "watercolor"
raw = captured.get("prompt_text", "")
assert "Title" in raw and "memo" in raw
def test_cover_fallback_uses_service_template_when_llm_disabled():
out = _svc(llm=None).build_cover_prompt(
chapter_title="Ch1",
chapter_category="family",
context_excerpt="mountain lake",
)
assert "mountain lake" in out["prompt"].lower()
assert (
"chapter cover" in out["prompt"].lower()
or "illustration" in out["prompt"].lower()
)
def test_cover_fallback_when_excerpt_empty(monkeypatch):
monkeypatch.setattr(
"app.features.memoir.memoir_images.prompting.settings.image_prompt_fallback_disabled",
False,
)
out = _svc(llm=None).build_cover_prompt(
chapter_title="",
chapter_category="family",
context_excerpt="",
)
assert len(out["prompt"]) > 20
def test_cover_fallback_disabled_requires_excerpt(monkeypatch):
monkeypatch.setattr(
"app.features.memoir.memoir_images.prompting.settings.image_prompt_fallback_disabled",
True,
)
with pytest.raises(RuntimeError, match="context_excerpt"):
_svc(llm=None).build_cover_prompt(
chapter_title="T",
chapter_category="family",
context_excerpt="",
)