Files
KiloStar/kilostar/utils/settings.py
T
2026-07-01 09:22:26 +00:00

115 lines
3.5 KiB
Python

"""KiloStar 集中式环境变量管理。
所有散落在各模块的 os.getenv/os.environ 收敛到此处,
通过 pydantic-settings 统一校验、类型转换、默认值管理。
"""
from __future__ import annotations
from functools import lru_cache
from pydantic import Field
from pydantic_settings import BaseSettings
class DatabaseSettings(BaseSettings):
postgres_user: str = "postgres"
postgres_password: str = ""
postgres_host: str = "db"
postgres_port: int = 5432
postgres_db: str = "postgres"
class SecuritySettings(BaseSettings):
secret_key: str = ""
kilostar_secret_key: str = ""
kilostar_env: str = "production"
class LogSettings(BaseSettings):
kilostar_log_level: str = "DEBUG"
kilostar_log_format: str = ""
kilostar_log_json: str = ""
class OnebotSettings(BaseSettings):
onebot_access_token: str = ""
onebot_http_url: str = "http://127.0.0.1:5700"
class AppSettings(BaseSettings):
kilostar_mode: str = "distributed"
kilostar_lang: str = "zh"
kilostar_cors_origins: str = ""
kilostar_plugin_dir: str = ""
kilostar_toolset_dir: str = ""
kilostar_artifact_dir: str = ""
db: DatabaseSettings = Field(default_factory=DatabaseSettings)
security: SecuritySettings = Field(default_factory=SecuritySettings)
log: LogSettings = Field(default_factory=LogSettings)
onebot: OnebotSettings = Field(default_factory=OnebotSettings)
model_config = {"env_nested_delimiter": "__"}
@lru_cache(maxsize=1)
def get_settings() -> AppSettings:
return AppSettings()
def get_plugin_dir() -> "pathlib.Path":
"""返回插件根目录路径(包含 tool_plugin/ 和 skill/ 子目录)。
优先使用环境变量 KILOSTAR_PLUGIN_DIR,否则默认 <project_root>/data/plugin/。
"""
import pathlib
custom = get_settings().kilostar_plugin_dir
if custom:
return pathlib.Path(custom)
project_root = pathlib.Path(__file__).parent.parent.parent
return project_root / "data" / "plugin"
def get_toolset_dir() -> "pathlib.Path":
"""返回工具集根目录路径(包含各 toolset 子目录,如 base_toolset/)。
优先使用环境变量 KILOSTAR_TOOLSET_DIR,否则默认 <project_root>/data/toolset/。
"""
import pathlib
custom = get_settings().kilostar_toolset_dir
if custom:
return pathlib.Path(custom)
project_root = pathlib.Path(__file__).parent.parent.parent
return project_root / "data" / "toolset"
def get_plugin_data_dir(plugin_name: str) -> "pathlib.Path":
"""返回单个插件的私有数据目录:``<plugin_dir>/<name>/_data/``。
放 SQLite 文件、安装 marker、本地缓存等运行时状态——跟随插件代码同根目录,
用户备份/迁移整个插件文件夹时数据自动跟着走。目录不存在时自动创建。
"""
import pathlib
target = get_plugin_dir() / plugin_name / "_data"
target.mkdir(parents=True, exist_ok=True)
return target
def get_artifact_dir() -> "pathlib.Path":
"""返回工作流产物(agent 通过 send_file 推送的文件)存放根目录。
优先使用环境变量 KILOSTAR_ARTIFACT_DIR,否则默认 <project_root>/data/artifact/。
每个 trace_id 一个子目录,下载链接走 /api/v1/resource/artifact/{trace_id}/{aid}
"""
import pathlib
custom = get_settings().kilostar_artifact_dir
if custom:
return pathlib.Path(custom)
project_root = pathlib.Path(__file__).parent.parent.parent
return project_root / "data" / "artifact"