2026-03-20 15:15:35 +08:00
|
|
|
|
"""
|
|
|
|
|
|
Story 同步写入(Celery / sync Session)。
|
|
|
|
|
|
|
|
|
|
|
|
与 StoryService 行为对齐:版本链、主图 intent、章节 dirty;不 commit,由调用方提交。
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import delete, func, select
|
|
|
|
|
|
from sqlalchemy.orm import Session, joinedload
|
|
|
|
|
|
|
|
|
|
|
|
from app.core.logging import get_logger
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.features.memoir import repo as memoir_repo
|
2026-03-20 17:25:42 +08:00
|
|
|
|
from app.features.memoir.asset_resolver import strip_asset_image_refs_from_markdown
|
2026-03-20 15:15:35 +08:00
|
|
|
|
from app.features.memoir.models import ChapterStoryLink
|
|
|
|
|
|
from app.features.story.image_intent_extractor import extract_primary_image_intent
|
2026-04-08 15:37:09 +08:00
|
|
|
|
from app.features.story.models import (
|
|
|
|
|
|
Story,
|
|
|
|
|
|
StoryEvidenceLink,
|
|
|
|
|
|
StoryImageIntent,
|
|
|
|
|
|
StoryVersion,
|
|
|
|
|
|
)
|
2026-03-26 12:13:36 +08:00
|
|
|
|
from app.features.story.time_hints import apply_infer_story_time_start_to_model
|
2026-03-20 15:15:35 +08:00
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def count_story_versions_sync(session: Session, story_id: str) -> int:
|
|
|
|
|
|
stmt = select(func.count(StoryVersion.id)).where(StoryVersion.story_id == story_id)
|
|
|
|
|
|
return int(session.execute(stmt).scalar() or 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _delete_pending_failed_intents_sync(session: Session, story_id: str) -> None:
|
|
|
|
|
|
session.execute(
|
|
|
|
|
|
delete(StoryImageIntent).where(
|
|
|
|
|
|
StoryImageIntent.story_id == story_id,
|
|
|
|
|
|
StoryImageIntent.intent_role == "primary",
|
|
|
|
|
|
StoryImageIntent.status.in_(["pending", "failed"]),
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_primary_intent_sync(
|
|
|
|
|
|
session: Session, story_id: str
|
|
|
|
|
|
) -> StoryImageIntent | None:
|
|
|
|
|
|
stmt = select(StoryImageIntent).where(
|
|
|
|
|
|
StoryImageIntent.story_id == story_id,
|
|
|
|
|
|
StoryImageIntent.intent_role == "primary",
|
|
|
|
|
|
)
|
|
|
|
|
|
return session.execute(stmt).scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_and_store_image_intent_sync(
|
|
|
|
|
|
session: Session,
|
|
|
|
|
|
*,
|
|
|
|
|
|
story: Story,
|
|
|
|
|
|
version: StoryVersion,
|
|
|
|
|
|
markdown: str,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
_delete_pending_failed_intents_sync(session, story.id)
|
|
|
|
|
|
result = extract_primary_image_intent(
|
|
|
|
|
|
markdown,
|
|
|
|
|
|
title=story.title or "",
|
|
|
|
|
|
stage=story.stage,
|
|
|
|
|
|
summary=story.summary,
|
|
|
|
|
|
people_refs=story.people_refs or [],
|
|
|
|
|
|
place_refs=story.place_refs or [],
|
|
|
|
|
|
time_start=story.time_start,
|
|
|
|
|
|
time_end=story.time_end,
|
|
|
|
|
|
)
|
|
|
|
|
|
existing = _get_primary_intent_sync(session, story.id)
|
|
|
|
|
|
now = datetime.now(timezone.utc)
|
|
|
|
|
|
|
|
|
|
|
|
if existing and existing.story_version_id == version.id:
|
|
|
|
|
|
st = (existing.status or "").strip()
|
|
|
|
|
|
if st in ("processing", "completed"):
|
|
|
|
|
|
return
|
|
|
|
|
|
existing.caption = result.caption
|
|
|
|
|
|
existing.prompt_brief = result.prompt_brief
|
|
|
|
|
|
existing.style_profile = result.style_profile
|
|
|
|
|
|
existing.status = "pending"
|
|
|
|
|
|
existing.error = None
|
|
|
|
|
|
existing.asset_id = None
|
|
|
|
|
|
existing.updated_at = now
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
if existing and existing.story_version_id != version.id:
|
|
|
|
|
|
existing.story_version_id = version.id
|
|
|
|
|
|
existing.caption = result.caption
|
|
|
|
|
|
existing.prompt_brief = result.prompt_brief
|
|
|
|
|
|
existing.style_profile = result.style_profile
|
|
|
|
|
|
existing.status = "pending"
|
|
|
|
|
|
existing.error = None
|
|
|
|
|
|
existing.asset_id = None
|
|
|
|
|
|
existing.updated_at = now
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
StoryImageIntent(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
story_id=story.id,
|
|
|
|
|
|
story_version_id=version.id,
|
|
|
|
|
|
intent_role="primary",
|
|
|
|
|
|
caption=result.caption,
|
|
|
|
|
|
prompt_brief=result.prompt_brief,
|
|
|
|
|
|
style_profile=result.style_profile,
|
|
|
|
|
|
status="pending",
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-04-08 15:37:09 +08:00
|
|
|
|
def replace_story_evidence_links_sync(
|
|
|
|
|
|
session: Session,
|
|
|
|
|
|
*,
|
|
|
|
|
|
story_id: str,
|
|
|
|
|
|
chunk_ids: list[str],
|
|
|
|
|
|
fact_ids: list[str],
|
|
|
|
|
|
timeline_event_ids: list[str],
|
|
|
|
|
|
summary_ids: list[str],
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""以当前生成所用的检索闭包覆盖 story 证据关联(artifact 当前态绑定)。"""
|
|
|
|
|
|
session.execute(
|
|
|
|
|
|
delete(StoryEvidenceLink).where(StoryEvidenceLink.story_id == story_id)
|
|
|
|
|
|
)
|
|
|
|
|
|
for cid in chunk_ids:
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
StoryEvidenceLink(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
evidence_type="chunk",
|
|
|
|
|
|
evidence_id=cid,
|
|
|
|
|
|
role="primary",
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
for fid in fact_ids:
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
StoryEvidenceLink(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
evidence_type="fact",
|
|
|
|
|
|
evidence_id=fid,
|
|
|
|
|
|
role="supporting",
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
for tid in timeline_event_ids:
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
StoryEvidenceLink(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
evidence_type="timeline_event",
|
|
|
|
|
|
evidence_id=tid,
|
|
|
|
|
|
role="supporting",
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
for sid in summary_ids:
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
StoryEvidenceLink(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
evidence_type="summary",
|
|
|
|
|
|
evidence_id=sid,
|
|
|
|
|
|
role="background",
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
def create_story_with_version_sync(
|
|
|
|
|
|
session: Session,
|
|
|
|
|
|
*,
|
|
|
|
|
|
user_id: str,
|
|
|
|
|
|
title: str,
|
|
|
|
|
|
canonical_markdown: str,
|
|
|
|
|
|
stage: str | None = None,
|
2026-04-08 15:37:09 +08:00
|
|
|
|
prompt_meta: dict | None = None,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
) -> Story:
|
2026-03-20 17:25:42 +08:00
|
|
|
|
md = strip_asset_image_refs_from_markdown(canonical_markdown or "")
|
2026-03-20 15:15:35 +08:00
|
|
|
|
story = Story(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
|
title=title,
|
|
|
|
|
|
stage=stage,
|
2026-03-20 17:25:42 +08:00
|
|
|
|
canonical_markdown=md,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
session.add(story)
|
|
|
|
|
|
session.flush()
|
|
|
|
|
|
vid = str(uuid.uuid4())
|
|
|
|
|
|
version = StoryVersion(
|
|
|
|
|
|
id=vid,
|
|
|
|
|
|
story_id=story.id,
|
|
|
|
|
|
version_no=1,
|
2026-03-20 17:25:42 +08:00
|
|
|
|
markdown_snapshot=md,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
actor_type="ai",
|
|
|
|
|
|
source_type="generate",
|
2026-04-08 15:37:09 +08:00
|
|
|
|
prompt_meta=prompt_meta,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
session.add(version)
|
|
|
|
|
|
session.flush()
|
|
|
|
|
|
story.current_version_id = vid
|
2026-03-26 12:13:36 +08:00
|
|
|
|
apply_infer_story_time_start_to_model(story)
|
2026-03-20 17:25:42 +08:00
|
|
|
|
if md.strip():
|
2026-03-20 15:15:35 +08:00
|
|
|
|
_extract_and_store_image_intent_sync(
|
2026-03-20 17:25:42 +08:00
|
|
|
|
session, story=story, version=version, markdown=md
|
2026-03-20 15:15:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
memoir_repo.mark_chapters_dirty_for_story_sync(session, story.id)
|
|
|
|
|
|
return story
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def append_story_version_sync(
|
|
|
|
|
|
session: Session,
|
|
|
|
|
|
story_id: str,
|
|
|
|
|
|
markdown_snapshot: str,
|
|
|
|
|
|
*,
|
|
|
|
|
|
actor_type: str = "ai",
|
|
|
|
|
|
source_type: str = "generate",
|
2026-04-08 15:37:09 +08:00
|
|
|
|
prompt_meta: dict | None = None,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
) -> StoryVersion:
|
|
|
|
|
|
story = session.get(Story, story_id)
|
|
|
|
|
|
if not story:
|
|
|
|
|
|
raise ValueError(f"Story {story_id} not found")
|
2026-03-20 17:25:42 +08:00
|
|
|
|
md = strip_asset_image_refs_from_markdown(markdown_snapshot or "")
|
2026-03-20 15:15:35 +08:00
|
|
|
|
parent_id = story.current_version_id
|
|
|
|
|
|
version_no = count_story_versions_sync(session, story_id) + 1
|
|
|
|
|
|
vid = str(uuid.uuid4())
|
|
|
|
|
|
version = StoryVersion(
|
|
|
|
|
|
id=vid,
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
version_no=version_no,
|
2026-03-20 17:25:42 +08:00
|
|
|
|
markdown_snapshot=md,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
actor_type=actor_type,
|
|
|
|
|
|
source_type=source_type,
|
|
|
|
|
|
parent_version_id=parent_id,
|
2026-04-08 15:37:09 +08:00
|
|
|
|
prompt_meta=prompt_meta,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
session.add(version)
|
|
|
|
|
|
session.flush()
|
|
|
|
|
|
story.current_version_id = vid
|
2026-03-20 17:25:42 +08:00
|
|
|
|
story.canonical_markdown = md
|
2026-03-26 12:13:36 +08:00
|
|
|
|
apply_infer_story_time_start_to_model(story)
|
2026-03-20 15:15:35 +08:00
|
|
|
|
_extract_and_store_image_intent_sync(
|
2026-03-20 17:25:42 +08:00
|
|
|
|
session, story=story, version=version, markdown=md
|
2026-03-20 15:15:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
memoir_repo.mark_chapters_dirty_for_story_sync(session, story_id)
|
|
|
|
|
|
return version
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ensure_chapter_story_link_sync(
|
|
|
|
|
|
session: Session,
|
|
|
|
|
|
*,
|
|
|
|
|
|
chapter_id: str,
|
|
|
|
|
|
story_id: str,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
"""若章节尚未关联该 story,则在末尾追加一条 chapter_story_link。"""
|
|
|
|
|
|
exists = session.scalars(
|
|
|
|
|
|
select(ChapterStoryLink)
|
|
|
|
|
|
.where(
|
|
|
|
|
|
ChapterStoryLink.chapter_id == chapter_id,
|
|
|
|
|
|
ChapterStoryLink.story_id == story_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.limit(1)
|
|
|
|
|
|
).first()
|
|
|
|
|
|
if exists is not None:
|
|
|
|
|
|
return
|
|
|
|
|
|
max_stmt = select(func.coalesce(func.max(ChapterStoryLink.order_index), -1)).where(
|
|
|
|
|
|
ChapterStoryLink.chapter_id == chapter_id
|
|
|
|
|
|
)
|
|
|
|
|
|
max_idx = int(session.execute(max_stmt).scalar() or -1)
|
|
|
|
|
|
session.add(
|
|
|
|
|
|
ChapterStoryLink(
|
|
|
|
|
|
id=str(uuid.uuid4()),
|
|
|
|
|
|
chapter_id=chapter_id,
|
|
|
|
|
|
story_id=story_id,
|
|
|
|
|
|
order_index=max_idx + 1,
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
session.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_active_stories_for_user_sync(session: Session, user_id: str) -> list[Story]:
|
|
|
|
|
|
stmt = (
|
|
|
|
|
|
select(Story)
|
|
|
|
|
|
.where(Story.user_id == user_id, Story.status == "active")
|
|
|
|
|
|
.options(
|
|
|
|
|
|
joinedload(Story.chapter_links).joinedload(ChapterStoryLink.chapter),
|
|
|
|
|
|
)
|
|
|
|
|
|
.order_by(Story.updated_at.desc())
|
|
|
|
|
|
)
|
|
|
|
|
|
return list(session.execute(stmt).unique().scalars().all())
|