"""Release gate and bounded operational defaults for the Helix assistant. ``assistant.enabled`` is the single product-level switch. It is fail-closed: absent or non-boolean means OFF, so an operator who mistypes the JSON value cannot accidentally expose an unreleased surface to 500 employees. The Feishu event subscription can therefore be configured on the open platform (which needs an app version review, i.e. lead time) long before the product opens. """ from __future__ import annotations from typing import Any from sqlalchemy.ext.asyncio import AsyncSession from app.integrations.models import PlatformSetting SETTING_ENABLED = "assistant.enabled" SETTING_TURNS_PER_HOUR = "assistant.turns_per_user_per_hour" SETTING_HISTORY_TURNS = "assistant.history_turns" SETTING_SESSION_TTL_MINUTES = "assistant.session_ttl_minutes" DEFAULT_TURNS_PER_HOUR = 60 DEFAULT_HISTORY_TURNS = 12 DEFAULT_SESSION_TTL_MINUTES = 120 async def setting_value(db: AsyncSession, key: str) -> Any: row = await db.get(PlatformSetting, key) return row.value if row else None async def assistant_enabled(db: AsyncSession) -> bool: """Exact ``True`` only — the string ``"true"`` must not release the product.""" return await setting_value(db, SETTING_ENABLED) is True async def bounded_int_setting( db: AsyncSession, key: str, *, default: int, minimum: int, maximum: int, ) -> int: """Operator-tunable integer, clamped so a bad value cannot break the channel. Deliberately a local copy of the Intelligence Center helper rather than a shared import: the two orchestration layers stay independently deployable, and hoisting it into ``app/core`` would be a refactor unrelated to this milestone. """ raw = await setting_value(db, key) try: value = int(raw) if raw is not None else default except (TypeError, ValueError): value = default return max(minimum, min(maximum, value))