82 lines
3.2 KiB
Python
82 lines
3.2 KiB
Python
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.infrastructure.db import SessionFactory
|
|
from app.model.configuration import (
|
|
AgentIntentConfig,
|
|
ConfigRelease,
|
|
PlatformConfigItem,
|
|
PromptTemplateVersion,
|
|
)
|
|
|
|
|
|
class RuntimeConfigService:
|
|
"""Reads only configuration rows bound to the caller's released version."""
|
|
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self.session = session
|
|
|
|
async def prompt(
|
|
self, release_id: int, prompt_code: str, task_type: str, agent_type: str | None
|
|
) -> PromptTemplateVersion | None:
|
|
result = await self.session.scalar(
|
|
select(PromptTemplateVersion).where(
|
|
PromptTemplateVersion.release_id == release_id,
|
|
PromptTemplateVersion.prompt_code == prompt_code,
|
|
PromptTemplateVersion.task_type == task_type,
|
|
(PromptTemplateVersion.agent_type == agent_type)
|
|
| PromptTemplateVersion.agent_type.is_(None),
|
|
)
|
|
)
|
|
return result
|
|
|
|
async def intent(self, agent_type: str, intent_code: str) -> AgentIntentConfig | None:
|
|
result = await self.session.scalar(
|
|
select(AgentIntentConfig).where(
|
|
AgentIntentConfig.agent_type == agent_type,
|
|
AgentIntentConfig.intent_code == intent_code,
|
|
AgentIntentConfig.status == "active",
|
|
)
|
|
)
|
|
return result
|
|
|
|
async def allowed_tools(
|
|
self, release_id: int, agent_type: str, intent_code: str
|
|
) -> tuple[str, ...]:
|
|
config_key = f"{agent_type}:{intent_code}"
|
|
item = await self.session.scalar(
|
|
select(PlatformConfigItem).where(
|
|
PlatformConfigItem.release_id == release_id,
|
|
PlatformConfigItem.namespace == "agent_tools",
|
|
PlatformConfigItem.config_key == config_key,
|
|
)
|
|
)
|
|
if item is None:
|
|
return ()
|
|
raw_tools = item.value_json.get("allowed_tools", [])
|
|
if not isinstance(raw_tools, list) or not all(isinstance(tool, str) for tool in raw_tools):
|
|
raise ValueError("invalid tool whitelist configuration")
|
|
return tuple(raw_tools)
|
|
|
|
async def fund_quote(self, release_id: int) -> dict[str, object]:
|
|
"""读取已发布行情配置;缺失时返回空映射,由 Service 使用安全默认值。"""
|
|
item = await self.session.scalar(
|
|
select(PlatformConfigItem).where(
|
|
PlatformConfigItem.release_id == release_id,
|
|
PlatformConfigItem.namespace == "fund_market",
|
|
PlatformConfigItem.config_key == "default",
|
|
)
|
|
)
|
|
return item.value_json if item is not None else {}
|
|
|
|
|
|
async def load_fund_quote_config() -> dict[str, object]:
|
|
"""读取当前激活行情配置;配置中心不可用时交给调用方使用默认值。"""
|
|
async with SessionFactory() as session:
|
|
release = await session.scalar(
|
|
select(ConfigRelease).where(ConfigRelease.status == "active")
|
|
)
|
|
if release is None:
|
|
return {}
|
|
return await RuntimeConfigService(session).fund_quote(release.id)
|