13c57771b9
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 3s
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 2m49s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m5s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 5m6s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 5m27s
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 5m52s
CI/CD Pipeline / Validate - Style (pull_request) Successful in 5m56s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 6m24s
AI Code Review / AI Code Review (pull_request) Successful in 6m51s
CI/CD Pipeline / Validate - Security (pull_request) Successful in 12m56s
CI/CD Pipeline / Unit Tests (pull_request) Failing after 13m43s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Failing after 3s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 8m10s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 32s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m18s
247 lines
10 KiB
Python
Executable File
247 lines
10 KiB
Python
Executable File
"""爆款视频任务 SQLAlchemy 仓储实现。"""
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from packages.adapters.sqlalchemy_impl.models import (
|
|
ViralVideoJobModel,
|
|
ViralVideoStyleTemplateModel,
|
|
)
|
|
from packages.domain.viral_video import ViralVideoJob, ViralVideoStatus
|
|
|
|
|
|
def _to_domain(model: ViralVideoJobModel) -> ViralVideoJob:
|
|
"""ORM → 领域实体。pre_trusted_images 兼容脏数据:双序列化字符串/字符数组/list[str]。"""
|
|
import json as _pti_json
|
|
|
|
_raw_pti = getattr(model, "pre_trusted_images", None)
|
|
_pti: list[str] | None = None
|
|
if _raw_pti is not None:
|
|
if isinstance(_raw_pti, str):
|
|
try:
|
|
_p = _pti_json.loads(_raw_pti)
|
|
if isinstance(_p, list):
|
|
_pti = [u for u in _p if isinstance(u, str) and u] or None
|
|
except Exception:
|
|
_pti = None
|
|
elif isinstance(_raw_pti, list):
|
|
_f = [u for u in _raw_pti if isinstance(u, str) and len(u) > 5]
|
|
_pti = _f if _f else None
|
|
return ViralVideoJob(
|
|
id=model.id,
|
|
user_id=model.user_id,
|
|
images=list(model.images or []),
|
|
pre_trusted_images=_pti,
|
|
industry=model.industry or "",
|
|
target_customer=model.target_customer or "",
|
|
persona_id=model.persona_id or "",
|
|
viral_structure=model.viral_structure or "",
|
|
marketing_purpose=model.marketing_purpose or "",
|
|
bgm_preference=model.bgm_preference or "",
|
|
duration=model.duration or 15,
|
|
user_copy_text=model.user_copy_text or "",
|
|
fusion_level=model.fusion_level or "ai_polish",
|
|
reference_audio_path=model.reference_audio_path or "",
|
|
reference_video_url=getattr(model, "reference_video_url", "") or "",
|
|
style_strength=getattr(model, "style_strength", "medium") or "medium",
|
|
style_guide=dict(model.style_guide) if model.style_guide else None,
|
|
style_template_id=getattr(model, "style_template_id", "") or "",
|
|
voice_id=getattr(model, "voice_id", "") or "",
|
|
voice_source=getattr(model, "voice_source", "") or "",
|
|
video_ratio=getattr(model, "video_ratio", "9:16") or "9:16",
|
|
video_model=getattr(model, "video_model", "") or "",
|
|
status=ViralVideoStatus(model.status) if model.status else ViralVideoStatus.PENDING,
|
|
current_stage=getattr(model, "current_stage", "") or "",
|
|
phase_message=getattr(model, "phase_message", "") or "",
|
|
heartbeat_at=getattr(model, "heartbeat_at", None),
|
|
intent_result=dict(model.intent_result) if model.intent_result else None,
|
|
image_analysis=dict(model.image_analysis) if getattr(model, "image_analysis", None) else None,
|
|
storyboard=list(model.storyboard) if getattr(model, "storyboard", None) else None,
|
|
generated_copy_text=getattr(model, "generated_copy_text", "") or "",
|
|
copy_result=dict(model.copy_result) if getattr(model, "copy_result", None) else None,
|
|
result_video_url=model.result_video_url or "",
|
|
video_resolution=getattr(model, "video_resolution", "720p") or "720p",
|
|
credits_prepaid=float(getattr(model, "credits_prepaid", 0) or 0),
|
|
credits_transaction_id=getattr(model, "credits_transaction_id", "") or "",
|
|
credits_cost=float(model.credits_cost or 0),
|
|
error_msg=model.error_msg or "",
|
|
retry_count=model.retry_count or 0,
|
|
started_at=model.started_at,
|
|
completed_at=model.completed_at,
|
|
created_at=model.created_at,
|
|
updated_at=model.updated_at,
|
|
)
|
|
|
|
|
|
class SQLAlchemyViralVideoJobRepository:
|
|
"""爆款视频任务仓储。"""
|
|
|
|
def __init__(self, session: Session):
|
|
self.session = session
|
|
|
|
def save(self, job: ViralVideoJob) -> ViralVideoJob:
|
|
model = ViralVideoJobModel(
|
|
id=job.id,
|
|
user_id=job.user_id,
|
|
images=job.images,
|
|
pre_trusted_images=job.pre_trusted_images,
|
|
industry=job.industry,
|
|
target_customer=job.target_customer,
|
|
persona_id=job.persona_id,
|
|
viral_structure=job.viral_structure,
|
|
marketing_purpose=job.marketing_purpose,
|
|
bgm_preference=job.bgm_preference,
|
|
duration=job.duration,
|
|
user_copy_text=job.user_copy_text,
|
|
fusion_level=job.fusion_level,
|
|
reference_audio_path=job.reference_audio_path,
|
|
reference_video_url=job.reference_video_url,
|
|
style_strength=job.style_strength,
|
|
style_guide=job.style_guide,
|
|
style_template_id=job.style_template_id,
|
|
voice_id=job.voice_id,
|
|
voice_source=job.voice_source,
|
|
video_ratio=job.video_ratio,
|
|
video_model=job.video_model,
|
|
status=job.status,
|
|
current_stage=job.current_stage or "",
|
|
phase_message=job.phase_message or "",
|
|
heartbeat_at=job.heartbeat_at,
|
|
intent_result=job.intent_result,
|
|
image_analysis=job.image_analysis,
|
|
storyboard=job.storyboard,
|
|
generated_copy_text=job.generated_copy_text,
|
|
copy_result=job.copy_result,
|
|
result_video_url=job.result_video_url,
|
|
video_resolution=getattr(job, "video_resolution", "720p") or "720p",
|
|
credits_prepaid=float(getattr(job, "credits_prepaid", 0) or 0),
|
|
credits_transaction_id=getattr(job, "credits_transaction_id", "") or "",
|
|
credits_cost=float(getattr(job, "credits_cost", 0) or 0),
|
|
error_msg=job.error_msg,
|
|
retry_count=job.retry_count,
|
|
started_at=job.started_at,
|
|
completed_at=job.completed_at,
|
|
created_at=job.created_at,
|
|
updated_at=job.updated_at,
|
|
)
|
|
self.session.add(model)
|
|
self.session.commit()
|
|
return job
|
|
|
|
def update(self, job: ViralVideoJob) -> None:
|
|
model = self.session.query(ViralVideoJobModel).filter(ViralVideoJobModel.id == job.id).first()
|
|
if model is None:
|
|
raise ValueError(f"ViralVideoJob {job.id} not found")
|
|
model.status = job.status
|
|
model.current_stage = job.current_stage or ""
|
|
model.phase_message = job.phase_message or ""
|
|
model.heartbeat_at = job.heartbeat_at
|
|
model.intent_result = job.intent_result
|
|
model.image_analysis = job.image_analysis
|
|
model.storyboard = job.storyboard
|
|
model.generated_copy_text = job.generated_copy_text or ""
|
|
model.copy_result = job.copy_result
|
|
model.pre_trusted_images = job.pre_trusted_images
|
|
model.result_video_url = job.result_video_url
|
|
model.video_resolution = getattr(job, "video_resolution", "720p") or "720p"
|
|
model.credits_prepaid = float(getattr(job, "credits_prepaid", 0) or 0)
|
|
model.credits_transaction_id = getattr(job, "credits_transaction_id", "") or ""
|
|
model.credits_cost = float(getattr(job, "credits_cost", 0) or 0)
|
|
model.error_msg = job.error_msg
|
|
model.retry_count = job.retry_count
|
|
model.started_at = job.started_at
|
|
model.completed_at = job.completed_at
|
|
model.style_guide = job.style_guide
|
|
# v1.5 three-stage: persist user-editable params so resume uses latest values
|
|
model.user_copy_text = job.user_copy_text
|
|
model.industry = job.industry
|
|
model.target_customer = job.target_customer
|
|
model.persona_id = job.persona_id
|
|
model.viral_structure = job.viral_structure
|
|
model.marketing_purpose = job.marketing_purpose
|
|
model.bgm_preference = job.bgm_preference
|
|
model.duration = job.duration
|
|
model.fusion_level = job.fusion_level
|
|
model.reference_audio_path = job.reference_audio_path
|
|
model.reference_video_url = job.reference_video_url
|
|
model.style_strength = job.style_strength
|
|
model.style_template_id = job.style_template_id
|
|
model.voice_id = job.voice_id or ""
|
|
model.voice_source = job.voice_source or ""
|
|
model.video_ratio = job.video_ratio or "9:16"
|
|
model.video_model = job.video_model or ""
|
|
model.updated_at = datetime.now(timezone.utc)
|
|
self.session.commit()
|
|
|
|
def get(self, job_id: str) -> ViralVideoJob | None:
|
|
model = self.session.query(ViralVideoJobModel).filter(ViralVideoJobModel.id == job_id).first()
|
|
if model is None:
|
|
return None
|
|
return _to_domain(model)
|
|
|
|
def list_by_user(self, user_id: str, limit: int = 50, offset: int = 0) -> list[ViralVideoJob]:
|
|
models = (
|
|
self.session.query(ViralVideoJobModel)
|
|
.filter(ViralVideoJobModel.user_id == user_id)
|
|
.order_by(ViralVideoJobModel.created_at.desc())
|
|
.offset(offset)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
return [_to_domain(m) for m in models]
|
|
|
|
def count_pending_by_user(self, user_id: str) -> int:
|
|
return (
|
|
self.session.query(ViralVideoJobModel)
|
|
.filter(
|
|
ViralVideoJobModel.user_id == user_id,
|
|
ViralVideoJobModel.status.in_(
|
|
["pending", "running", "wait_user_confirm", "image_analyzed", "copy_generated"]
|
|
),
|
|
)
|
|
.count()
|
|
)
|
|
|
|
|
|
class SQLAlchemyViralVideoStyleTemplateRepository:
|
|
"""风格模板仓储。"""
|
|
|
|
def __init__(self, session: Session):
|
|
self.session = session
|
|
|
|
def list_all(self) -> list[dict]:
|
|
models = (
|
|
self.session.query(ViralVideoStyleTemplateModel)
|
|
.order_by(ViralVideoStyleTemplateModel.sort_order.asc())
|
|
.all()
|
|
)
|
|
return [
|
|
{
|
|
"id": m.id,
|
|
"name": m.name,
|
|
"description": m.description or "",
|
|
"thumbnail_url": m.thumbnail_url or "",
|
|
"style_config": dict(m.style_config) if m.style_config else {},
|
|
"is_system": m.is_system,
|
|
}
|
|
for m in models
|
|
]
|
|
|
|
def get(self, template_id: str) -> dict | None:
|
|
model = (
|
|
self.session.query(ViralVideoStyleTemplateModel)
|
|
.filter(ViralVideoStyleTemplateModel.id == template_id)
|
|
.first()
|
|
)
|
|
if model is None:
|
|
return None
|
|
return {
|
|
"id": model.id,
|
|
"name": model.name,
|
|
"description": model.description or "",
|
|
"thumbnail_url": model.thumbnail_url or "",
|
|
"style_config": dict(model.style_config) if model.style_config else {},
|
|
"is_system": model.is_system,
|
|
}
|