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:
Sully
2026-05-22 13:44:50 +08:00
committed by GitHub
parent f09ae248f9
commit 53e0065e3e
298 changed files with 15247 additions and 4344 deletions

View File

@@ -26,6 +26,9 @@ from app.core.llm_call import allm_json_call
from app.core.llm_gateway import LlmGateway, LlmUseCase
from app.core.logging import get_logger
from app.ports.llm import LLMProvider
from app.core.runtime_constants import agent_log_defaults
from app.features.conversation.constants import chat
from app.features.story.constants import story
logger = get_logger(__name__)
@@ -207,8 +210,8 @@ class ProfileAgent:
if conversation_id:
hw = await get_history_with_window(
conversation_id,
max_pairs=settings.chat_history_max_pairs,
max_chars=settings.chat_history_max_chars,
max_pairs=chat.history_max_pairs,
max_chars=chat.history_max_chars,
)
recent = hw.window[-4:] if len(hw.window) > 4 else hw.window
parts = []
@@ -232,7 +235,7 @@ class ProfileAgent:
ProfileExtractionOutput,
use_case=LlmUseCase(
"ProfileAgent.extract_profile_from_message",
max_tokens=settings.chat_profile_extract_max_tokens,
max_tokens=chat.profile_extract_max_tokens,
),
fallback_factory=lambda: ProfileExtractionOutput(),
)
@@ -285,8 +288,8 @@ class ProfileAgent:
)
hw = await get_history_with_window(
conversation_id,
max_pairs=settings.chat_history_max_pairs,
max_chars=settings.chat_history_max_chars,
max_pairs=chat.history_max_pairs,
max_chars=chat.history_max_chars,
)
messages: List[Any] = [SystemMessage(content=prompt)]
messages.extend(hw.window)
@@ -296,7 +299,7 @@ class ProfileAgent:
"ProfileAgent.followup.prompt",
format_history_string(
messages,
omit_system_body=settings.agent_log_omit_system_message_body,
omit_system_body=agent_log_defaults.omit_system_message_body,
),
)
prompt_chars = _message_contents_char_count(messages)
@@ -309,14 +312,14 @@ class ProfileAgent:
)
response_text = await self._invoke_chat(
messages,
max_tokens=settings.chat_profile_followup_max_tokens,
max_tokens=chat.profile_followup_max_tokens,
conversation_id=conversation_id,
agent_name="ProfileAgent.generate_profile_followup",
)
segments = await self._segments_from_response(
response_text,
max_segments=3,
max_chars_per_segment=settings.chat_interview_max_chars_per_segment,
max_chars_per_segment=chat.interview_max_chars_per_segment,
fallback=_profile_followup_fallback(language),
)
log_agent_summary(
@@ -344,8 +347,8 @@ class ProfileAgent:
)
hw = await get_history_with_window(
conversation_id,
max_pairs=settings.chat_history_max_pairs,
max_chars=settings.chat_history_max_chars,
max_pairs=chat.history_max_pairs,
max_chars=chat.history_max_chars,
)
messages: List[Any] = [SystemMessage(content=prompt)]
messages.extend(hw.window)
@@ -367,7 +370,7 @@ class ProfileAgent:
"ProfileAgent.greeting.prompt",
format_history_string(
messages,
omit_system_body=settings.agent_log_omit_system_message_body,
omit_system_body=agent_log_defaults.omit_system_message_body,
),
)
prompt_chars = _message_contents_char_count(messages)
@@ -380,14 +383,14 @@ class ProfileAgent:
)
response_text = await self._invoke_chat(
messages,
max_tokens=settings.chat_profile_followup_max_tokens,
max_tokens=chat.profile_followup_max_tokens,
conversation_id=conversation_id,
agent_name="ProfileAgent.generate_profile_greeting",
)
segments = await self._segments_from_response(
response_text,
max_segments=2,
max_chars_per_segment=settings.chat_interview_max_chars_per_segment,
max_chars_per_segment=chat.interview_max_chars_per_segment,
fallback=_profile_greeting_fallback(language),
)
log_agent_summary(