访谈与阶段 - 新增 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 与对话/长度行为。
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
import json
|
||
import re
|
||
from typing import Any
|
||
|
||
from app.features.memoir.asset_resolver import strip_image_placeholders
|
||
|
||
from app.core.json_utils import extract_json_payload
|
||
from .schema import IMAGE_STATUS_PENDING
|
||
|
||
PLACEHOLDER_RE = re.compile(
|
||
r"\{\{\{\{IMAGE:(.*?)\}\}\}\}|\{\{IMAGE:(.*?)\}\}",
|
||
re.DOTALL,
|
||
)
|
||
|
||
|
||
def parse_image_placeholders(content: str, max_images: int) -> list[dict[str, Any]]:
|
||
"""离线迁移/调试用:解析正文中的 IMAGE 占位符。"""
|
||
items: list[dict[str, Any]] = []
|
||
for match in PLACEHOLDER_RE.finditer(content or ""):
|
||
description = (match.group(1) or match.group(2) or "").strip()
|
||
if not description:
|
||
continue
|
||
items.append(
|
||
{
|
||
"index": len(items),
|
||
"description": description,
|
||
"placeholder": match.group(0),
|
||
"start_offset": match.start(),
|
||
}
|
||
)
|
||
if max_images is not None and len(items) >= max_images:
|
||
break
|
||
return items
|
||
|
||
|
||
def build_initial_image_assets(
|
||
placeholders: list[dict[str, Any]],
|
||
provider: str,
|
||
style: str,
|
||
size: str,
|
||
now_iso: str,
|
||
) -> list[dict[str, Any]]:
|
||
return [
|
||
{
|
||
"index": item["index"],
|
||
"placeholder": item["placeholder"],
|
||
"description": item["description"],
|
||
"prompt": None,
|
||
"url": None,
|
||
"status": IMAGE_STATUS_PENDING,
|
||
"provider": provider,
|
||
"style": style,
|
||
"size": size,
|
||
"error": None,
|
||
"created_at": now_iso,
|
||
"updated_at": now_iso,
|
||
}
|
||
for item in placeholders
|
||
]
|
||
|
||
|
||
def parse_narrative_json(raw: str) -> list[dict[str, Any]]:
|
||
"""
|
||
解析 LLM 输出的 JSON 叙事(paragraphs)。
|
||
不根据 image_description 生成配图占位;插图由 story/chapter 结构化流程单独处理。
|
||
"""
|
||
if not raw or not str(raw).strip():
|
||
return []
|
||
try:
|
||
payload = extract_json_payload(raw)
|
||
data = json.loads(payload)
|
||
paragraphs = data.get("paragraphs") or []
|
||
if not isinstance(paragraphs, list):
|
||
return []
|
||
except (json.JSONDecodeError, TypeError, AttributeError):
|
||
return []
|
||
|
||
result: list[dict[str, Any]] = []
|
||
for p in paragraphs:
|
||
if not isinstance(p, dict):
|
||
continue
|
||
content = (p.get("content") or "").strip()
|
||
if content:
|
||
result.append({"content": content, "placeholder_info": None})
|
||
return result
|
||
|
||
|
||
def split_plain_narrative_into_sections(narrative: str) -> list[dict[str, Any]]:
|
||
"""非 JSON 叙事:去掉遗留占位符后按空行拆段,不产生段落配图。"""
|
||
text = strip_image_placeholders(narrative or "")
|
||
if not text.strip():
|
||
return []
|
||
parts = [p.strip() for p in text.split("\n\n") if p.strip()]
|
||
return [{"content": p, "placeholder_info": None} for p in parts]
|
||
|
||
|
||
def parse_narrative_to_sections(narrative: str) -> list[dict[str, Any]]:
|
||
"""
|
||
将 narrative 解析为 sections。
|
||
JSON(paragraphs)走 parse_narrative_json;否则剥离占位符后按段拆分。
|
||
"""
|
||
if not narrative or not str(narrative).strip():
|
||
return []
|
||
stripped = narrative.strip()
|
||
if stripped.startswith("{") and "paragraphs" in stripped:
|
||
segments = parse_narrative_json(narrative)
|
||
if segments:
|
||
return segments
|
||
return split_plain_narrative_into_sections(narrative)
|