58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
"""Agent 运行配置边界。"""
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from django.conf import settings
|
|
|
|
from common.exceptions import AgentConfigurationError
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AgentRuntimeConfig:
|
|
"""集中表达真实 SDK 执行所需配置,避免业务服务直接读取环境变量。"""
|
|
|
|
api_key: str
|
|
model: str
|
|
agent_name: str
|
|
provider_code: str = "openai"
|
|
protocol: str = "openai_responses"
|
|
base_url: str = "https://api.openai.com/v1"
|
|
|
|
@classmethod
|
|
def from_settings(cls) -> "AgentRuntimeConfig":
|
|
"""从 Django 设置构建配置;这里只读取,不在日志或异常中回显秘密。"""
|
|
|
|
return cls(
|
|
api_key=str(settings.OPENAI_API_KEY or "").strip(),
|
|
model=str(settings.OPENAI_MODEL or "").strip(),
|
|
agent_name=str(settings.AGENT_DEFAULT_NAME or "job_research").strip(),
|
|
)
|
|
|
|
def require_real_execution(self) -> "AgentRuntimeConfig":
|
|
"""真实执行前快速失败;非 Agent 页面无需调用此方法。"""
|
|
|
|
missing = []
|
|
if not self.api_key:
|
|
missing.append("OPENAI_API_KEY")
|
|
if not self.model:
|
|
missing.append("OPENAI_MODEL")
|
|
if missing:
|
|
raise AgentConfigurationError(f"真实 Agent 执行缺少配置:{', '.join(missing)}。")
|
|
return self
|
|
|
|
@classmethod
|
|
def from_user(cls, user) -> "AgentRuntimeConfig":
|
|
"""从用户已验证的服务商配置构建运行配置,供真实 Gateway 接入使用。"""
|
|
|
|
from .services import resolve_runtime_model_config
|
|
|
|
runtime = resolve_runtime_model_config(user)
|
|
return cls(
|
|
api_key=runtime.api_key,
|
|
model=runtime.model,
|
|
agent_name=str(settings.AGENT_DEFAULT_NAME or "job_research").strip(),
|
|
provider_code=runtime.provider_code,
|
|
protocol=runtime.protocol,
|
|
base_url=runtime.base_url,
|
|
)
|