from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.model.configuration import AgentIntentConfig, 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)