feat(market): 添加市场分析数据基础架构和功能模块 - 新增市场分析相关数据库表结构,包括市场工具、每日报价、财务指标、 宏观指标和公告等数据模型 - 创建市场分析相关的Alembic迁移脚本,包含完整的up和downgrade逻辑 - 集成市场分析路由到主API路由器中 - 添加市场数据定时任务调度,支持盘前、收盘和周度市场分析报告 - 实现市场数据后台任务队列,包含报告生成和收盘分析功能 - 扩展系统配置设置,添加市场分析启用开关和相关参数配置 - 增加AI智能技能支持,包含市场概览分析和股票分析功能 - 添加审计日志记录,支持市场数据同步和自选股更新操作追踪 - 实现飞书命令集成,支持股票分析、自选股管理、公告查询等交互 - 提供市场数据服务层,包含行业分析、股票对比、市场概览等功能 ```
216 lines
8.3 KiB
Python
216 lines
8.3 KiB
Python
import json
|
|
from functools import lru_cache
|
|
from typing import Annotated, Any
|
|
|
|
from pydantic import Field, field_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
|
|
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_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",
|
|
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)
|
|
|
|
|
|
@lru_cache
|
|
def get_settings() -> Settings:
|
|
"""Return cached application settings."""
|
|
|
|
return Settings()
|