refactor(api): 使用常量替代硬编码字符串 - 在health_check接口中使用ApiResponseKey.STATUS和ApiStatus.OK常量 - 替换硬编码的状态返回值为枚举常量 refactor(core): 配置模块错误信息统一使用常量 - 从constants模块导入ConfigErrorDetail并替换CORS_ORIGINS和LEGACY_ALLOWED_QUERIES的验证错误信息 - 配置类中的默认值使用constants中定义的常量 feat(constants): 添加API响应、安全错误和配置错误常量类 - 新增ApiResponseKey用于API状态键名 - 新增ApiStatus用于API状态值 - 新增SecurityErrorDetail用于安全认证错误详情 - 新增ConfigErrorDetail用于配置验证错误详情 - 添加DEFAULT_MODEL_PROVIDER和DEFAULT_OPENCLAW_ACTION_JSON常量 refactor(security): 安全认证模块使用错误常量 - 将硬编码的安全错误信息替换为SecurityErrorDetail常量 - 包括API密钥、审批密钥和审计密钥的相关错误信息 refactor(ai-agent): AI代理适配器改进错误处理 - 将HTTP状态码替换为FastAPI状态常量 - 添加OpenClaw工具和操作的错误常量 - 修复健康检查和工具调用中的状态码比较逻辑 - 添加AIToolAuditKey用于工具审计键名 feat(ai-agent): 扩展AI代理常量定义 - 新增AIToolAuditKey用于工具审计字段 - 添加OpenClaw相关的错误常量如OPENCLAW_CHAT_PROVIDER_REQUIRED等 - 添加UNSUPPORTED_AI_SKILL_TEMPLATE模板字符串 refactor(approvals): 审批模块常量化重构 - 新增ApprovalPayloadKey用于审批载荷字段 - 添加approval_action函数和APPROVAL_ACTION_SEPARATOR分隔符 - 使用常量替换字面量值 feat(audit): 审计模块新增飞书事件动作类型 - 添加FEISHU_WEBHOOK_EVENT和FEISHU_LONG_CONNECTION_EVENT审计动作 refactor(business): 业务模块全面常量化 - 新增BusinessDomain枚举包含所有业务域 - 添加BusinessResponseKey、BusinessPayloadKey等常量类 - 重构DOMAIN_MODELS为frozenset以提高性能 - 添加normalize_domain等辅助函数用于域标准化 - 使用常量替换路由和业务服务中的硬编码字符串 - 添加业务错误常量和字段验证模板 refactor(feishu): 飞书客户端错误处理优化 - 将HTTP状态码替换为FastAPI标准状态常量 - 改进错误处理的一致性 refactor(approvals): 审批服务使用新常量结构 - 使用ApprovalPayloadKey常量重构载荷字段 - 使用approval_action函数统一动作命名格式 - 优化高风险域判断逻辑 ```
114 lines
4.2 KiB
Python
114 lines
4.2 KiB
Python
import json
|
|
from functools import lru_cache
|
|
from typing import Any
|
|
|
|
from pydantic import Field, field_validator
|
|
from pydantic_settings import BaseSettings, 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
|
|
api_prefix: str = "/api/v1"
|
|
api_key: str | None = None
|
|
api_actor: str = ActorValue.API
|
|
audit_api_key: str | None = None
|
|
audit_api_actor: str = ActorValue.AUDITOR
|
|
approval_api_key: str | None = None
|
|
approval_api_actor: str = ActorValue.APPROVER
|
|
cors_origins: list[str] = Field(default_factory=lambda: ["*"])
|
|
|
|
database_url: str = "sqlite:///./company_ai.db"
|
|
legacy_database_url: str | None = None
|
|
legacy_project_query: str | None = None
|
|
legacy_allowed_queries: dict[str, str] = Field(default_factory=dict)
|
|
legacy_project_code_prefix: str = "LEGACY"
|
|
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: list[str] = Field(default_factory=list)
|
|
openclaw_allowed_actions: list[str] = 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
|
|
daily_brief_cron_hour: int = 9
|
|
daily_brief_cron_minute: int = 0
|
|
weekly_project_report_day_of_week: str = "mon"
|
|
weekly_project_report_cron_hour: int = 9
|
|
weekly_project_report_cron_minute: int = 30
|
|
|
|
@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", mode="before")
|
|
@classmethod
|
|
def parse_csv_list(cls, value: str | list[str]) -> list[str]:
|
|
if isinstance(value, list):
|
|
return value
|
|
return [item.strip() for item in value.split(",") if item.strip()]
|
|
|
|
@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)
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
"""Return cached application settings."""
|
|
|
|
return Settings()
|