feat(api): 回忆录管线简化、路由延迟池与相关加固
- Phase1/2:移除 MemoirOrchestrator.run 与 process_memoir_segments 别名;文档改为 process_memoir_phase1。 - 槽位校验集中到 stage_constants(filter_stage_slots),批处理与顺序路径及 state_service 写库一致。 - StoryRoute:no_llm/parse_error/invalid_target 保守 new_story;短篇护栏不覆盖这些 fallback。 - Phase2 低置信单路径可选延迟(StoryPipelineResult.deferred):不写 Chapter/Story,Segment 记录 defer 元数据,冷却内不重复消费;上限后停自动重试,Phase1 同类目新段唤醒池内段。 - Alembic 0017:segments 表 narrative_defer_* 列。 - ProfileAgent:经 LlmGateway/注入 Provider 统一聊天与 JSON,新增测试。 - ImagePromptOrchestrator:LLM 初始化失败可依配置降级或硬失败;补充策略测试。 - 配套单测与 README/本地开发文档表述更新。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -10,7 +10,6 @@ from typing import Any, Callable, Dict, List
|
||||
|
||||
from app.agents.memoir.prompts import get_batch_memoir_phase1_prep_prompt
|
||||
from app.agents.memoir.schemas import BatchPhase1LLMOutput
|
||||
from app.agents.stage_constants import STAGE_SLOT_KEYS
|
||||
from app.agents.state_schema import MemoirStateSchema
|
||||
from app.core.config import settings
|
||||
from app.core.llm_call import LLMCallError, llm_json_call
|
||||
@@ -19,11 +18,6 @@ from app.features.conversation.models import Segment
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
STAGE_ALLOWED_SLOTS: Dict[str, frozenset[str]] = {
|
||||
k: frozenset(v) for k, v in STAGE_SLOT_KEYS.items()
|
||||
}
|
||||
|
||||
|
||||
def _slots_snapshot(state: MemoirStateSchema) -> dict:
|
||||
snap: dict = {}
|
||||
for stage, buckets in (state.slots or {}).items():
|
||||
|
||||
@@ -8,12 +8,9 @@ from __future__ import annotations
|
||||
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
|
||||
from typing import Any, Callable, Dict, List, Optional, Set
|
||||
|
||||
from app.agents.memoir.batch_phase1_prep import (
|
||||
STAGE_ALLOWED_SLOTS,
|
||||
run_batch_phase1_prep_chunked,
|
||||
)
|
||||
from app.agents.memoir.batch_phase1_prep import run_batch_phase1_prep_chunked
|
||||
from app.agents.memoir.classification_agent import (
|
||||
ClassificationAgent,
|
||||
_looks_like_fragment_only,
|
||||
@@ -22,7 +19,11 @@ from app.agents.memoir.classification_agent import (
|
||||
_detect_stage as detect_stage_from_keywords,
|
||||
)
|
||||
from app.agents.memoir.extraction_agent import ExtractionAgent, ExtractionResult
|
||||
from app.agents.stage_constants import normalize_chapter_category, normalize_chat_stage
|
||||
from app.agents.stage_constants import (
|
||||
filter_stage_slots,
|
||||
normalize_chapter_category,
|
||||
normalize_chat_stage,
|
||||
)
|
||||
from app.agents.state_schema import MemoirStateSchema
|
||||
from app.core.agent_logging import agent_span, agent_summary_enabled, log_agent_detail
|
||||
from app.core.config import settings
|
||||
@@ -92,7 +93,7 @@ class MemoirOrchestrator:
|
||||
)
|
||||
if use_batch:
|
||||
try:
|
||||
result = self._prepare_batches_via_batch_llm(
|
||||
prepared_batch = self._prepare_batches_via_batch_llm(
|
||||
segments=segments,
|
||||
state=state,
|
||||
classify_extract_llm=classify_extract_llm,
|
||||
@@ -104,7 +105,7 @@ class MemoirOrchestrator:
|
||||
"msg=Phase1 批处理 LLM 路径已使用",
|
||||
len(segments),
|
||||
)
|
||||
return result
|
||||
return prepared_batch
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"event=phase1_batch_path_fallback segment_count={} exc={} "
|
||||
@@ -132,8 +133,12 @@ class MemoirOrchestrator:
|
||||
stage_slots=stage_slots_raw,
|
||||
llm=classify_extract_llm,
|
||||
)
|
||||
detected_stage = result.detected_stage
|
||||
for slot_name, snippet in result.slots.items():
|
||||
fb = state.current_stage or "childhood"
|
||||
detected_stage = normalize_chat_stage(result.detected_stage, fb)
|
||||
result_slots = filter_stage_slots(detected_stage, result.slots, fb)
|
||||
if not result_slots:
|
||||
detected_stage = normalize_chat_stage(fb, fb)
|
||||
for slot_name, snippet in result_slots.items():
|
||||
state = update_slot(detected_stage, slot_name, snippet, [segment.id])
|
||||
|
||||
with agent_span(
|
||||
@@ -148,7 +153,7 @@ class MemoirOrchestrator:
|
||||
segment_id=segment.id,
|
||||
)
|
||||
chapter_category = classify_result.category
|
||||
if (not result.slots) and classify_result.llm_said_none:
|
||||
if (not result_slots) and classify_result.llm_said_none:
|
||||
segment_skip_story_ids.add(str(segment.id))
|
||||
segment_chapter_category[str(segment.id)] = chapter_category
|
||||
|
||||
@@ -166,7 +171,7 @@ class MemoirOrchestrator:
|
||||
logger,
|
||||
"MemoirOrchestrator.segment_done segment_id={} slots={}",
|
||||
segment.id,
|
||||
list((result.slots or {}).keys()),
|
||||
list(result_slots.keys()),
|
||||
)
|
||||
category_to_segments.setdefault(chapter_category, []).append(segment)
|
||||
|
||||
@@ -211,8 +216,7 @@ class MemoirOrchestrator:
|
||||
else:
|
||||
detected_stage = normalize_chat_stage(row.detected_stage, fb)
|
||||
|
||||
allowed = STAGE_ALLOWED_SLOTS.get(detected_stage, frozenset())
|
||||
result_slots = {k: v for k, v in result_slots.items() if k in allowed}
|
||||
result_slots = filter_stage_slots(detected_stage, result_slots, fb)
|
||||
if not result_slots:
|
||||
detected_stage = normalize_chat_stage(fb, fb)
|
||||
|
||||
@@ -269,72 +273,3 @@ class MemoirOrchestrator:
|
||||
segment_skip_story_ids=segment_skip_story_ids,
|
||||
segment_chapter_category=segment_chapter_category,
|
||||
)
|
||||
|
||||
def run(
|
||||
self,
|
||||
*,
|
||||
segments: List[Segment],
|
||||
llm: Any,
|
||||
user_profile: str = "",
|
||||
user_birth_year: Any = None,
|
||||
get_or_create_state: Callable[[], MemoirStateSchema],
|
||||
update_slot: Callable[[str, str, str, List[str]], MemoirStateSchema],
|
||||
acquire_lock: Callable[[str], bool],
|
||||
release_lock: Callable[[str], None],
|
||||
process_category: Callable[
|
||||
[
|
||||
str,
|
||||
List[Segment],
|
||||
MemoirStateSchema,
|
||||
str,
|
||||
Any,
|
||||
Any,
|
||||
],
|
||||
Tuple[Any, bool],
|
||||
],
|
||||
raise_retry: Callable[[], None],
|
||||
llm_fast: Any | None = None,
|
||||
) -> Tuple[Set[str], int]:
|
||||
"""
|
||||
执行回忆录流水线。
|
||||
process_category(category, segments, state, user_profile, user_birth_year, llm)
|
||||
返回 (chapter, has_images_to_generate)。
|
||||
返回 (chapters_to_enqueue, processed_count)。
|
||||
raise_retry 用于锁竞争时抛出 Celery retry。
|
||||
"""
|
||||
prepared = self.prepare_batches(
|
||||
segments=segments,
|
||||
llm=llm,
|
||||
llm_fast=llm_fast,
|
||||
get_or_create_state=get_or_create_state,
|
||||
update_slot=update_slot,
|
||||
on_phase1_chunk=None,
|
||||
)
|
||||
state = prepared.state
|
||||
chapters_to_enqueue: Set[str] = set()
|
||||
category_to_segments = prepared.category_to_segments
|
||||
|
||||
# 按 category 调用 process_category:叙事生成、持久化、封面入队标记
|
||||
for chapter_category, category_segments in category_to_segments.items():
|
||||
if not acquire_lock(chapter_category):
|
||||
logger.warning(
|
||||
"章节锁竞争: category={}, 延迟重试",
|
||||
chapter_category,
|
||||
)
|
||||
raise_retry()
|
||||
|
||||
try:
|
||||
chapter, has_images = process_category(
|
||||
chapter_category,
|
||||
category_segments,
|
||||
state,
|
||||
user_profile,
|
||||
user_birth_year,
|
||||
llm,
|
||||
)
|
||||
if chapter and has_images:
|
||||
chapters_to_enqueue.add(chapter.id)
|
||||
finally:
|
||||
release_lock(chapter_category)
|
||||
|
||||
return chapters_to_enqueue, len(segments)
|
||||
|
||||
@@ -31,6 +31,9 @@ PLAN_BATCH_MAX_SEGMENTS = 48
|
||||
# 童年 / 求学 / 家庭:模型与后处理均倾向「少拆分、优先续写」
|
||||
APPEND_FIRST_CHAPTER_CATEGORIES = frozenset({"childhood", "education", "family"})
|
||||
|
||||
# These route outcomes are conservative fail-safes, not semantic append matches.
|
||||
FALLBACK_NEW_STORY_REASONS = frozenset({"no_llm", "parse_error", "invalid_target"})
|
||||
|
||||
|
||||
def default_append_target_story_id(
|
||||
candidate_stories: list[Story],
|
||||
@@ -220,13 +223,6 @@ class StoryRouteAgent:
|
||||
story_meta: dict[str, dict[str, int]] | None = None,
|
||||
) -> StoryRouteDecision:
|
||||
if not llm:
|
||||
fb = default_append_target_story_id(candidate_stories, story_meta, settings)
|
||||
if fb and fb in valid_story_ids:
|
||||
return StoryRouteDecision(
|
||||
decision="append_story",
|
||||
target_story_id=fb,
|
||||
reason="no_llm_default_append",
|
||||
)
|
||||
return StoryRouteDecision(
|
||||
decision="new_story",
|
||||
new_story_title=None,
|
||||
@@ -241,13 +237,6 @@ class StoryRouteAgent:
|
||||
)
|
||||
|
||||
def _decide_fallback() -> StoryRouteDecision:
|
||||
fb = default_append_target_story_id(candidate_stories, story_meta, settings)
|
||||
if fb and fb in valid_story_ids:
|
||||
return StoryRouteDecision(
|
||||
decision="append_story",
|
||||
target_story_id=fb,
|
||||
reason="parse_error_default_append",
|
||||
)
|
||||
return StoryRouteDecision(
|
||||
decision="new_story",
|
||||
new_story_title=None,
|
||||
@@ -266,22 +255,8 @@ class StoryRouteAgent:
|
||||
if decision.decision == "append_story":
|
||||
tid = decision.target_story_id
|
||||
if not tid or tid not in valid_story_ids:
|
||||
fb = default_append_target_story_id(
|
||||
candidate_stories, story_meta, settings
|
||||
)
|
||||
if fb and fb in valid_story_ids:
|
||||
logger.info(
|
||||
"StoryRoute append 无效 target_story_id={},回退默认 append {}",
|
||||
tid,
|
||||
fb,
|
||||
)
|
||||
return StoryRouteDecision(
|
||||
decision="append_story",
|
||||
target_story_id=fb,
|
||||
reason="invalid_target_default_append",
|
||||
)
|
||||
logger.warning(
|
||||
"StoryRoute append 无效 target_story_id={},且无可用默认目标,回退 new_story",
|
||||
"StoryRoute append 无效 target_story_id={},回退 new_story",
|
||||
tid,
|
||||
)
|
||||
return StoryRouteDecision(
|
||||
|
||||
Reference in New Issue
Block a user