This commit is contained in:
2026-07-11 12:48:29 +08:00
parent c64cf06c33
commit 6cc1655c69
54 changed files with 7223 additions and 619 deletions
+60 -7
View File
@@ -49,6 +49,7 @@ from app.services.generation_billing_service import (
)
from app.services.generation_refund_service import mark_generation_record_failed_and_refund_once
from app.services.media_token_usage_snapshot_service import sync_generation_record_media_token_snapshot
from app.services.generation_ai_service import _build_image_snapshot, _build_video_snapshot
from app.services.credit_record_meta_service import build_generation_record_prompt_meta
from app.services.video_cover_service import async_create_video_cover_for_local_video
from app.enums.audio_reference import (
@@ -338,6 +339,7 @@ async def optimize(
attempt_no=prompt_attempt_no,
charge_kind=CHARGE_TEXT_PROMPT,
usage=token_usage,
media_references=record.media_references,
)
await deduct_credits(
db, current_user.id, text_credits,
@@ -431,6 +433,9 @@ async def generate(
raise HTTPException(status_code=400, detail="不支持的分辨率")
duration = record.duration or 5
from app.services.video_gen import get_active_engine, submit_video_task
engine = await get_active_engine(db)
engine_snapshot = _build_video_snapshot(engine, req.aspect_ratio, req.resolution, duration)
media_billing = await charge_generation_media_by_params(
db,
user_id=current_user.id,
@@ -438,14 +443,21 @@ async def generate(
gen_type="video",
duration=duration,
resolution=req.resolution,
aspect_ratio=req.aspect_ratio,
fps=24,
engine_id=engine.id,
project_name=project_name,
description_prefix=project_name+"-",
owner_type=OWNER_GENERATION_RECORD,
attempt_no=attempt_no,
media_references=record.media_references,
)
record.aspect_ratio = req.aspect_ratio
record.resolution = req.resolution
record.engine_id = engine.id
record.engine_snapshot_json = json.dumps(engine_snapshot, ensure_ascii=False, default=str)
record.current_billing_attempt_no = attempt_no
record.credits_cost = round(float(record.credits_cost or 0) + media_billing.total_charged, 2)
record.status = "generating"
record.error_message = None
@@ -456,11 +468,9 @@ async def generate(
await db.flush()
try:
from app.services.video_gen import get_active_engine, submit_video_task
from app.services.error_codes import extract_error_message
from app.services.video_queue import task_queue
engine = await get_active_engine(db)
task_id = await submit_video_task(
db,
engine,
@@ -480,19 +490,31 @@ async def generate(
elif record.gen_type == GenerationType.image:
image_size = req.image_size or record.image_size or "2K"
from app.services.image_gen import get_active_image_engine
engine = await get_active_image_engine(db)
image_proportion = record.image_proportion or "1:1"
image_px = record.image_px or "2048x2048"
engine_snapshot = _build_image_snapshot(engine, image_size, image_proportion, image_px)
media_billing = await charge_generation_media_by_params(
db,
user_id=current_user.id,
record_id=record.id,
gen_type="image",
image_size=image_size,
image_px=image_px,
aspect_ratio=image_proportion,
engine_id=engine.id,
project_name=project_name,
description_prefix=project_name+"-",
owner_type=OWNER_GENERATION_RECORD,
attempt_no=attempt_no,
media_references=record.media_references,
)
record.image_size = image_size
record.engine_id = engine.id
record.engine_snapshot_json = json.dumps(engine_snapshot, ensure_ascii=False, default=str)
record.current_billing_attempt_no = attempt_no
record.credits_cost = round(float(record.credits_cost or 0) + media_billing.total_charged, 2)
record.status = "generating"
record.error_message = None
@@ -549,14 +571,46 @@ async def retry_generation(
owner_type=OWNER_GENERATION_RECORD,
owner_id=record.id,
)
media_billing = await charge_generation_media_for_record(
if record.gen_type == GenerationType.video:
from app.services.video_gen import get_active_engine
engine = await get_active_engine(db)
engine_snapshot = _build_video_snapshot(
engine,
record.aspect_ratio or "16:9",
record.resolution or "720p",
record.duration or 5,
)
else:
from app.services.image_gen import get_active_image_engine
engine = await get_active_image_engine(db)
engine_snapshot = _build_image_snapshot(
engine,
record.image_size or "2K",
record.image_proportion or "1:1",
record.image_px or "2048x2048",
)
media_billing = await charge_generation_media_by_params(
db,
record=record,
user_id=record.user_id,
record_id=record.id,
gen_type=record.gen_type,
image_size=record.image_size,
image_px=record.image_px,
aspect_ratio=record.aspect_ratio or record.image_proportion,
duration=record.duration,
resolution=record.resolution,
fps=24 if record.gen_type == GenerationType.video else None,
engine_id=engine.id,
project_name=project_name,
description_prefix="视频重试",
description_prefix="生成重试-",
owner_type=OWNER_GENERATION_RECORD,
attempt_no=attempt_no,
media_references=record.media_references,
)
record.engine_id = engine.id
record.engine_snapshot_json = json.dumps(engine_snapshot, ensure_ascii=False, default=str)
record.current_billing_attempt_no = attempt_no
record.status = "generating"
record.error_message = None
record.video_url = None
@@ -570,8 +624,7 @@ async def retry_generation(
try:
from app.services.video_queue import task_queue
if record.gen_type == GenerationType.video:
from app.services.video_gen import get_active_engine, submit_video_task, extract_error_message
engine = await get_active_engine(db)
from app.services.video_gen import submit_video_task, extract_error_message
task_id = await submit_video_task(
db,
engine,