refactor(api): TOML 配置 SSOT、统一错误契约、Auth/事务加固与可观测性 (#33)
配置 SSOT(TOML + .env) 统一错误契约 Auth 与事务边界 Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client 可观测性(OpenTelemetry + LGTM)
This commit is contained in:
@@ -6,6 +6,7 @@ from app.core.config import settings
|
||||
from app.core.json_utils import extract_json_payload
|
||||
from app.core.langchain_llm import invoke_json_object
|
||||
from app.core.logging import get_logger
|
||||
from app.features.memoir.constants import memoir
|
||||
|
||||
from .settings import MemoirImageSettings
|
||||
|
||||
@@ -84,7 +85,7 @@ class MemoirImagePromptService:
|
||||
"prompt_context": prompt_context,
|
||||
}
|
||||
except Exception as exc:
|
||||
if settings.image_prompt_fallback_disabled:
|
||||
if memoir.image_prompt_fallback_disabled:
|
||||
raise
|
||||
logger.warning(
|
||||
"图片 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
|
||||
@@ -92,7 +93,7 @@ class MemoirImagePromptService:
|
||||
chapter_title,
|
||||
exc,
|
||||
)
|
||||
elif settings.image_prompt_fallback_disabled:
|
||||
elif memoir.image_prompt_fallback_disabled:
|
||||
raise RuntimeError(
|
||||
"MemoirImagePromptService.build_prompt requires LLM when "
|
||||
"image_prompt_fallback_disabled is True"
|
||||
@@ -121,7 +122,7 @@ class MemoirImagePromptService:
|
||||
) -> dict[str, str]:
|
||||
"""生成章节封面图的 image-generation prompt。"""
|
||||
excerpt = (context_excerpt or "").strip()
|
||||
if settings.image_prompt_fallback_disabled and not excerpt:
|
||||
if memoir.image_prompt_fallback_disabled and not excerpt:
|
||||
raise RuntimeError(
|
||||
"Chapter cover prompt requires non-empty context_excerpt when "
|
||||
"image_prompt_fallback_disabled is True"
|
||||
@@ -165,7 +166,7 @@ class MemoirImagePromptService:
|
||||
"prompt_context": prompt_context,
|
||||
}
|
||||
except Exception as exc:
|
||||
if settings.image_prompt_fallback_disabled:
|
||||
if memoir.image_prompt_fallback_disabled:
|
||||
raise
|
||||
logger.warning(
|
||||
"封面 prompt 生成回退到默认模板: chapter_category={}, title={}, error={}",
|
||||
@@ -173,7 +174,7 @@ class MemoirImagePromptService:
|
||||
chapter_title,
|
||||
exc,
|
||||
)
|
||||
elif settings.image_prompt_fallback_disabled:
|
||||
elif memoir.image_prompt_fallback_disabled:
|
||||
raise RuntimeError(
|
||||
"MemoirImagePromptService.build_cover_prompt requires LLM when "
|
||||
"image_prompt_fallback_disabled is True"
|
||||
@@ -208,7 +209,7 @@ class MemoirImagePromptService:
|
||||
from app.agents.stage_constants import STAGE_TO_DEFAULT_CATEGORY
|
||||
|
||||
brief = (prompt_brief or "").strip()
|
||||
if settings.image_prompt_fallback_disabled and not brief:
|
||||
if memoir.image_prompt_fallback_disabled and not brief:
|
||||
raise RuntimeError(
|
||||
"Story image prompt requires non-empty prompt_brief when "
|
||||
"image_prompt_fallback_disabled is True"
|
||||
@@ -258,7 +259,7 @@ class MemoirImagePromptService:
|
||||
"prompt_context": prompt_context,
|
||||
}
|
||||
except Exception as exc:
|
||||
if settings.image_prompt_fallback_disabled:
|
||||
if memoir.image_prompt_fallback_disabled:
|
||||
raise
|
||||
logger.warning(
|
||||
"story 主图 prompt 生成回退到默认模板: stage={}, title={}, error={}",
|
||||
@@ -266,7 +267,7 @@ class MemoirImagePromptService:
|
||||
story_title,
|
||||
exc,
|
||||
)
|
||||
elif settings.image_prompt_fallback_disabled:
|
||||
elif memoir.image_prompt_fallback_disabled:
|
||||
raise RuntimeError(
|
||||
"MemoirImagePromptService.build_story_primary_prompt requires LLM when "
|
||||
"image_prompt_fallback_disabled is True"
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.core.config import Settings
|
||||
from app.core.config import settings
|
||||
from app.core.runtime_constants import misc_defaults
|
||||
from app.features.memoir.constants import memoir
|
||||
from app.features.story.constants import story
|
||||
|
||||
DEFAULT_LIBLIB_TEMPLATE_UUID = "5d7e67009b344550bc1aa6ccbfa1d7f4"
|
||||
DEFAULT_IMAGE_PROVIDER = "liblib"
|
||||
@@ -12,7 +11,6 @@ DEFAULT_POLL_INTERVAL_SECONDS = 5
|
||||
DEFAULT_MAX_ATTEMPTS = 60
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MemoirImageSettings:
|
||||
enabled: bool = False
|
||||
provider: str = DEFAULT_IMAGE_PROVIDER
|
||||
@@ -23,24 +21,37 @@ class MemoirImageSettings:
|
||||
liblib_template_uuid: str = DEFAULT_LIBLIB_TEMPLATE_UUID
|
||||
story_image_min_body_chars: int = 400
|
||||
|
||||
@classmethod
|
||||
def from_settings(cls, settings: "Settings") -> "MemoirImageSettings":
|
||||
s = settings
|
||||
return cls(
|
||||
enabled=bool(s.memoir_image_enabled),
|
||||
provider=s.memoir_image_provider or DEFAULT_IMAGE_PROVIDER,
|
||||
default_style=s.memoir_image_style_default or DEFAULT_IMAGE_STYLE,
|
||||
default_size=s.memoir_image_size_default or DEFAULT_IMAGE_SIZE,
|
||||
poll_interval_seconds=s.memoir_image_poll_interval,
|
||||
max_attempts=s.memoir_image_max_attempts,
|
||||
liblib_template_uuid=s.liblib_template_uuid or DEFAULT_LIBLIB_TEMPLATE_UUID,
|
||||
story_image_min_body_chars=int(
|
||||
getattr(s, "story_image_min_body_chars", 800) or 0
|
||||
),
|
||||
)
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
enabled: bool = False,
|
||||
provider: str = DEFAULT_IMAGE_PROVIDER,
|
||||
default_style: str = DEFAULT_IMAGE_STYLE,
|
||||
default_size: str = DEFAULT_IMAGE_SIZE,
|
||||
poll_interval_seconds: int = DEFAULT_POLL_INTERVAL_SECONDS,
|
||||
max_attempts: int = DEFAULT_MAX_ATTEMPTS,
|
||||
liblib_template_uuid: str = DEFAULT_LIBLIB_TEMPLATE_UUID,
|
||||
story_image_min_body_chars: int = 400,
|
||||
) -> None:
|
||||
self.enabled = enabled
|
||||
self.provider = provider
|
||||
self.default_style = default_style
|
||||
self.default_size = default_size
|
||||
self.poll_interval_seconds = poll_interval_seconds
|
||||
self.max_attempts = max_attempts
|
||||
self.liblib_template_uuid = liblib_template_uuid
|
||||
self.story_image_min_body_chars = story_image_min_body_chars
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "MemoirImageSettings":
|
||||
from app.core.config import settings as _s
|
||||
|
||||
return cls.from_settings(_s)
|
||||
return cls(
|
||||
enabled=bool(settings.memoir_image_enabled),
|
||||
provider=memoir.image_provider or DEFAULT_IMAGE_PROVIDER,
|
||||
default_style=memoir.image_style_default or DEFAULT_IMAGE_STYLE,
|
||||
default_size=memoir.image_size_default or DEFAULT_IMAGE_SIZE,
|
||||
poll_interval_seconds=memoir.image_poll_interval,
|
||||
max_attempts=memoir.image_max_attempts,
|
||||
liblib_template_uuid=settings.liblib_template_uuid
|
||||
or DEFAULT_LIBLIB_TEMPLATE_UUID,
|
||||
story_image_min_body_chars=int(story.image_min_body_chars or 0),
|
||||
)
|
||||
|
||||
@@ -190,13 +190,15 @@ class TencentCosStorageService:
|
||||
|
||||
@classmethod
|
||||
def from_settings(cls, settings) -> "TencentCosStorageService":
|
||||
from app.core.runtime_constants import misc_defaults
|
||||
|
||||
config = (
|
||||
getattr(settings, "tencent_cos_secret_id", "") or "",
|
||||
getattr(settings, "tencent_cos_secret_key", "") or "",
|
||||
getattr(settings, "tencent_cos_region", "") or "",
|
||||
getattr(settings, "tencent_cos_bucket", "") or "",
|
||||
getattr(settings, "tencent_cos_base_url", "") or "",
|
||||
getattr(settings, "tencent_cos_token", "") or "",
|
||||
(getattr(settings, "tencent_secret_id", "") or "").strip(),
|
||||
(getattr(settings, "tencent_secret_key", "") or "").strip(),
|
||||
misc_defaults.tencent_cos_region,
|
||||
(getattr(settings, "tencent_cos_bucket", "") or "").strip(),
|
||||
(getattr(settings, "tencent_cos_base_url", "") or "").strip(),
|
||||
"",
|
||||
)
|
||||
if cls._instance is None or cls._instance_config != config:
|
||||
cls._instance = cls(
|
||||
|
||||
Reference in New Issue
Block a user