Files
company-ai-platform/app/core/config.py
JiuContinent 514b14d390 ```
feat: 添加飞书集成和审计API密钥认证

- 在数据库配置中添加飞书模型导入
- 添加审计API密钥配置项和认证中间件
- 实现飞书事件重复处理防止机制
- 为审批路由添加API密钥认证
- 优化AI适配器错误处理并添加JSON解析异常捕获
- 更新测试用例以包含新的认证和事件处理逻辑
```
2026-07-06 11:34:15 +08:00

109 lines
4.1 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
DEFAULT_MODEL_PROVIDER = "noop"
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: ["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("CORS_ORIGINS must be a CSV string or JSON list")
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("LEGACY_ALLOWED_QUERIES must be a JSON object")
return {str(key): str(item) for key, item in data.items()}
raise ValueError("LEGACY_ALLOWED_QUERIES must be a JSON object")
@lru_cache
def get_settings() -> Settings:
"""Return cached application settings."""
return Settings()