refactor(Dockerfile): 使用requirements.txt替代硬编码依赖 将Dockerfile中的硬编码pip包列表替换为通过requirements.txt文件安装, 提高依赖管理的灵活性和可维护性。 feat(scheduling): 移除内置APScheduler,采用独立调度系统 移除app/core/background/scheduler.py中原来的APScheduler实现, 改为使用新的应用级调度系统app.application.scheduling。 refactor(task_queue): 调整任务队列模块结构和导入路径 将任务队列相关常量从app.core.background.task_queue.constants迁移至 app.tasks.constants,并更新所有相关导入路径和引用。 refactor(events): 将事件服务重构为独立的应用层组件 将事件分发逻辑从核心层迁移到应用层,使用app.application.events.EventDispatchService 替代原有的app.modules.events.services.EventService。 feat(ai_memory): 增强AI记忆自动写入的安全策略 新增ai_memory_blocked_content_terms配置项用于阻止敏感内容, 添加TTL过期机制控制自动写入条目的生命周期。 fix(security): 强化生产环境安全验证机制 增加model_validator确保生产环境中数据库连接、API密钥、CORS设置等 关键安全配置符合要求。 feat(risks): 优化风险事件操作动作的外键约束 为RiskEventAction模型的风险事件ID字段添加外键约束, 防止孤立记录并增强数据完整性。 refactor(audit): 优化审计服务方法命名和事务处理 将AuditService的log方法重命名为record以反映其阶段行为, 并调整事务提交时机以提高性能。 feat(events): 增强领域事件并发处理和响应模型 添加事件锁定机制防止重复处理,更新API响应模型以提供 更准确的数据类型定义。 ```
266 lines
10 KiB
Python
266 lines
10 KiB
Python
import json
|
|
from functools import lru_cache
|
|
from typing import Annotated, Any
|
|
|
|
from pydantic import Field, field_validator, model_validator
|
|
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
|
|
|
|
from app.core.constants import (
|
|
ActorValue,
|
|
ConfigErrorDetail,
|
|
DEFAULT_MODEL_PROVIDER,
|
|
DEFAULT_OPENCLAW_ACTION_JSON,
|
|
)
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
"""Runtime settings loaded from environment variables and `.env`."""
|
|
|
|
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8", extra="ignore")
|
|
|
|
app_name: str = "Company AI Management Platform"
|
|
app_env: str = "local"
|
|
debug: bool = False
|
|
read_only_mode: bool = True
|
|
api_prefix: str = "/api/v1"
|
|
api_key: str | None = None
|
|
api_actor: str = ActorValue.API
|
|
api_keys: list[dict[str, Any]] = Field(default_factory=list)
|
|
audit_api_key: str | None = None
|
|
audit_api_actor: str = ActorValue.AUDITOR
|
|
audit_api_keys: list[dict[str, Any]] = Field(default_factory=list)
|
|
cors_origins: list[str] = Field(default_factory=lambda: ["*"])
|
|
mask_sensitive_responses: bool = True
|
|
masked_response_fields: list[str] = Field(default_factory=list)
|
|
|
|
database_url: str = "sqlite:///./company_ai.db"
|
|
legacy_database_url: str | None = None
|
|
legacy_project_query: str | None = None
|
|
legacy_task_query: str | None = None
|
|
legacy_allowed_queries: dict[str, str] = Field(default_factory=dict)
|
|
legacy_project_code_prefix: str = "LEGACY"
|
|
legacy_task_code_prefix: str = "LEGACY-TASK"
|
|
redis_url: str = "redis://127.0.0.1:6379/0"
|
|
|
|
feishu_base_url: str = "https://open.feishu.cn/open-apis"
|
|
feishu_app_id: str | None = None
|
|
feishu_app_secret: str | None = None
|
|
feishu_verification_token: str | None = None
|
|
feishu_encrypt_key: str | None = None
|
|
feishu_default_chat_id: str | None = None
|
|
model_provider: str = DEFAULT_MODEL_PROVIDER
|
|
openclaw_base_url: str = "http://127.0.0.1:2070"
|
|
openclaw_http_url: str | None = None
|
|
openclaw_ws_url: str | None = None
|
|
openclaw_api_key: str | None = None
|
|
openclaw_gateway_token: str | None = None
|
|
openclaw_allowed_tools: Annotated[list[str], NoDecode] = Field(default_factory=list)
|
|
openclaw_allowed_actions: Annotated[list[str], NoDecode] = Field(
|
|
default_factory=lambda: [DEFAULT_OPENCLAW_ACTION_JSON]
|
|
)
|
|
hermes_base_url: str = "http://127.0.0.1:2073/v1"
|
|
hermes_api_key: str | None = None
|
|
hermes_model: str = "hermes-agent"
|
|
hermes_session_id: str | None = None
|
|
direct_llm_base_url: str = "https://api.openai.com/v1"
|
|
direct_llm_api_key: str | None = None
|
|
direct_llm_model: str = "gpt-4.1-mini"
|
|
|
|
scheduler_enabled: bool = False
|
|
task_queue_enabled: bool = False
|
|
task_queue_always_eager: bool = False
|
|
lifecycle_pipeline_enabled: bool = False
|
|
finance_needs_enabled: bool = False
|
|
market_analysis_enabled: bool = False
|
|
market_data_provider: str = "tushare"
|
|
market_data_base_url: str = "https://api.tushare.pro"
|
|
market_data_token: str | None = None
|
|
market_premarket_cron_hour: int = 8
|
|
market_premarket_cron_minute: int = 30
|
|
market_close_cron_hour: int = 15
|
|
market_close_cron_minute: int = 30
|
|
market_weekly_day_of_week: str = "sun"
|
|
market_weekly_cron_hour: int = 20
|
|
market_weekly_cron_minute: int = 0
|
|
legacy_sync_enabled: bool = False
|
|
celery_result_backend_url: str | None = None
|
|
daily_brief_cron_hour: int = 9
|
|
daily_brief_cron_minute: int = 0
|
|
attendance_summary_cron_hour: int = 18
|
|
attendance_summary_cron_minute: int = 0
|
|
risk_progress_cron_hour: int = 17
|
|
risk_progress_cron_minute: int = 30
|
|
work_daily_cron_hour: int = 18
|
|
work_daily_cron_minute: int = 30
|
|
work_weekly_report_day_of_week: str = "fri"
|
|
work_weekly_cron_hour: int = 18
|
|
work_weekly_cron_minute: int = 45
|
|
weekly_project_report_day_of_week: str = "mon"
|
|
weekly_project_report_cron_hour: int = 9
|
|
weekly_project_report_cron_minute: int = 30
|
|
legacy_project_sync_cron_hour: int = 2
|
|
legacy_project_sync_cron_minute: int = 0
|
|
legacy_task_sync_cron_hour: int = 2
|
|
legacy_task_sync_cron_minute: int = 30
|
|
event_dispatch_enabled: bool = True
|
|
event_dispatch_batch_size: int = 100
|
|
event_dispatch_max_attempts: int = 3
|
|
event_dispatch_retry_delay_seconds: int = 300
|
|
event_dispatch_lock_seconds: int = 300
|
|
event_dispatch_cron_minute: str = "*/5"
|
|
heartbeat_interval_seconds: int = 60
|
|
ai_memory_enabled: bool = True
|
|
ai_memory_auto_write_enabled: bool = True
|
|
ai_memory_recall_limit: int = 5
|
|
ai_memory_auto_write_ttl_days: int = Field(default=90, ge=1, le=3650)
|
|
ai_memory_blocked_content_terms: list[str] = Field(
|
|
default_factory=lambda: [
|
|
"bank account",
|
|
"budget",
|
|
"cash flow",
|
|
"financial statement",
|
|
"id card",
|
|
"payment",
|
|
"salary",
|
|
"成本",
|
|
"付款",
|
|
"工资",
|
|
"收款",
|
|
"现金流",
|
|
"财务",
|
|
"预算",
|
|
]
|
|
)
|
|
ai_analysis_max_attempts: int = 3
|
|
ai_memory_forbidden_keys: list[str] = Field(
|
|
default_factory=lambda: [
|
|
"authorization",
|
|
"api_key",
|
|
"apikey",
|
|
"access_token",
|
|
"tenant_access_token",
|
|
"token",
|
|
"secret",
|
|
"password",
|
|
"openclaw_gateway_token",
|
|
"hermes_api_key",
|
|
"direct_llm_api_key",
|
|
"market_data_token",
|
|
"feishu_app_secret",
|
|
"feishu_verification_token",
|
|
]
|
|
)
|
|
|
|
@field_validator("cors_origins", mode="before")
|
|
@classmethod
|
|
def parse_cors_origins(cls, value: Any) -> list[str]:
|
|
if isinstance(value, list):
|
|
return [str(item).strip() for item in value if str(item).strip()]
|
|
if value is None:
|
|
return []
|
|
text = str(value).strip()
|
|
if text.startswith("["):
|
|
data = json.loads(text)
|
|
if not isinstance(data, list):
|
|
raise ValueError(ConfigErrorDetail.CORS_ORIGINS_FORMAT)
|
|
return [str(item).strip() for item in data if str(item).strip()]
|
|
return [item.strip() for item in text.split(",") if item.strip()]
|
|
|
|
@field_validator(
|
|
"openclaw_allowed_tools",
|
|
"openclaw_allowed_actions",
|
|
"ai_memory_forbidden_keys",
|
|
"ai_memory_blocked_content_terms",
|
|
mode="before",
|
|
)
|
|
@classmethod
|
|
def parse_csv_list(cls, value: str | list[str] | None) -> list[str]:
|
|
if value is None:
|
|
return []
|
|
if isinstance(value, list):
|
|
return [str(item).strip() for item in value if str(item).strip()]
|
|
text = value.strip()
|
|
if not text:
|
|
return []
|
|
if text.startswith("["):
|
|
data = json.loads(text)
|
|
if not isinstance(data, list):
|
|
raise ValueError(ConfigErrorDetail.CORS_ORIGINS_FORMAT)
|
|
return [str(item).strip() for item in data if str(item).strip()]
|
|
return [item.strip() for item in text.split(",") if item.strip()]
|
|
|
|
@field_validator("masked_response_fields", mode="before")
|
|
@classmethod
|
|
def parse_masked_response_fields(cls, value: Any) -> list[str]:
|
|
if isinstance(value, list):
|
|
return [str(item).strip() for item in value if str(item).strip()]
|
|
if value is None:
|
|
return []
|
|
text = str(value).strip()
|
|
if not text:
|
|
return []
|
|
if text.startswith("["):
|
|
data = json.loads(text)
|
|
if not isinstance(data, list):
|
|
raise ValueError(ConfigErrorDetail.CORS_ORIGINS_FORMAT)
|
|
return [str(item).strip() for item in data if str(item).strip()]
|
|
return [item.strip() for item in text.split(",") if item.strip()]
|
|
|
|
@field_validator("api_keys", "audit_api_keys", mode="before")
|
|
@classmethod
|
|
def parse_service_keys(cls, value: Any) -> list[dict[str, Any]]:
|
|
if value is None or value == "":
|
|
return []
|
|
if isinstance(value, list):
|
|
return [dict(item) for item in value if isinstance(item, dict)]
|
|
if isinstance(value, str):
|
|
data = json.loads(value)
|
|
if not isinstance(data, list):
|
|
raise ValueError(ConfigErrorDetail.SERVICE_KEYS_FORMAT)
|
|
if not all(isinstance(item, dict) for item in data):
|
|
raise ValueError(ConfigErrorDetail.SERVICE_KEYS_FORMAT)
|
|
return [dict(item) for item in data]
|
|
raise ValueError(ConfigErrorDetail.SERVICE_KEYS_FORMAT)
|
|
|
|
@field_validator("legacy_allowed_queries", mode="before")
|
|
@classmethod
|
|
def parse_legacy_allowed_queries(cls, value: Any) -> dict[str, str]:
|
|
if value is None or value == "":
|
|
return {}
|
|
if isinstance(value, dict):
|
|
return {str(key): str(item) for key, item in value.items()}
|
|
if isinstance(value, str):
|
|
data = json.loads(value)
|
|
if not isinstance(data, dict):
|
|
raise ValueError(ConfigErrorDetail.LEGACY_ALLOWED_QUERIES_FORMAT)
|
|
return {str(key): str(item) for key, item in data.items()}
|
|
raise ValueError(ConfigErrorDetail.LEGACY_ALLOWED_QUERIES_FORMAT)
|
|
|
|
@model_validator(mode="after")
|
|
def validate_production_safety(self) -> "Settings":
|
|
if self.app_env.lower() not in {"prod", "production"}:
|
|
return self
|
|
errors: list[str] = []
|
|
if self.database_url.startswith("sqlite"):
|
|
errors.append("DATABASE_URL must use PostgreSQL in production")
|
|
if not self.api_key and not any(item.get("key") for item in self.api_keys):
|
|
errors.append("API_KEY or API_KEYS is required in production")
|
|
if not self.audit_api_key and not any(
|
|
item.get("key") for item in self.audit_api_keys
|
|
):
|
|
errors.append("AUDIT_API_KEY or AUDIT_API_KEYS is required in production")
|
|
if "*" in self.cors_origins:
|
|
errors.append("CORS_ORIGINS cannot contain '*' in production")
|
|
if self.debug:
|
|
errors.append("DEBUG must be false in production")
|
|
if errors:
|
|
raise ValueError("; ".join(errors))
|
|
return self
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
"""Return cached application settings."""
|
|
|
|
return Settings()
|