"""当下供需 gap 脚本主驱流程配置。""" from __future__ import annotations import os from app.core.config import settings from app.gap_script_demand.exceptions import GapScriptDemandError def _get_env(name: str, default: str = "") -> str: value = os.getenv(name) if value is None or value == "": return default return value def _get_env_int(name: str, default: int) -> int: raw = os.getenv(name) if raw is None or raw == "": return default try: return int(raw) except ValueError as exc: raise GapScriptDemandError(f"invalid integer env {name}={raw!r}") from exc def _get_env_float(name: str, default: float) -> float: raw = os.getenv(name) if raw is None or raw == "": return default try: return float(raw) except ValueError as exc: raise GapScriptDemandError(f"invalid float env {name}={raw!r}") from exc def _parse_cron_hours(value: str) -> str: hours = [item.strip() for item in value.split(",") if item.strip()] if not hours: raise GapScriptDemandError("gap script demand cron hours cannot be empty") normalized: list[str] = [] for hour in hours: try: hour_num = int(hour) except ValueError as exc: raise GapScriptDemandError(f"invalid gap script demand cron hour: {hour!r}") from exc if not 0 <= hour_num <= 23: raise GapScriptDemandError(f"gap script demand cron hour out of range: {hour_num}") normalized.append(str(hour_num)) return ",".join(normalized) def load_gap_script_demand_config() -> "GapScriptDemandConfig": from app.gap_script_demand.types import GapScriptDemandConfig llm_model = _get_env("GAP_SCRIPT_DEMAND_LLM_MODEL", settings.gap_script_demand_llm_model) config = GapScriptDemandConfig( cron_hours=_parse_cron_hours( _get_env("GAP_SCRIPT_DEMAND_CRON_HOURS", settings.gap_script_demand_cron_hours) ), cron_minute=_get_env_int( "GAP_SCRIPT_DEMAND_CRON_MINUTE", settings.gap_script_demand_cron_minute, ), llm_model=llm_model.strip() or settings.open_router_default_model, llm_max_attempts=_get_env_int( "GAP_SCRIPT_DEMAND_LLM_MAX_ATTEMPTS", settings.gap_script_demand_llm_max_attempts, ), llm_retry_sleep_seconds=_get_env_float( "GAP_SCRIPT_DEMAND_LLM_RETRY_SLEEP_SECONDS", settings.gap_script_demand_llm_retry_sleep_seconds, ), llm_max_tokens=_get_env_int( "GAP_SCRIPT_DEMAND_LLM_MAX_TOKENS", settings.gap_script_demand_llm_max_tokens, ), llm_temperature=_get_env_float( "GAP_SCRIPT_DEMAND_LLM_TEMPERATURE", settings.gap_script_demand_llm_temperature, ), demand_pool_source_table=_get_env( "DEMAND_POOL_SOURCE_TABLE", settings.demand_pool_source_table, ).strip(), demand_pool_strategy=_get_env( "GAP_SCRIPT_DEMAND_POOL_STRATEGY", settings.gap_script_demand_pool_strategy, ).strip(), output_strategy=_get_env( "GAP_SCRIPT_DEMAND_OUTPUT_STRATEGY", settings.gap_script_demand_output_strategy, ).strip(), judge_batch_size=_get_env_int( "GAP_SCRIPT_DEMAND_JUDGE_BATCH_SIZE", settings.gap_script_demand_judge_batch_size, ), ) if config.judge_batch_size <= 0: raise GapScriptDemandError("GAP_SCRIPT_DEMAND_JUDGE_BATCH_SIZE must be positive") return config