ForcePilot/backend/package/yuxi/channels/adapters/slack/group_policy.py

62 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
from dataclasses import dataclass, field
from typing import Any
@dataclass
class PerChannelPolicy:
require_mention: bool = False
tools_send_enabled: bool = True
@classmethod
def from_config_entry(cls, entry: dict[str, Any] | None) -> PerChannelPolicy:
if not entry:
return cls()
return cls(
require_mention=bool(entry.get("requireMention", False)),
tools_send_enabled=bool(entry.get("tools", {}).get("send", {}).get("enabled", True)),
)
@dataclass
class GroupPolicyRegistry:
channels: dict[str, PerChannelPolicy] = field(default_factory=dict)
def get_policy(self, channel_id: str) -> PerChannelPolicy:
return self.channels.get(channel_id, PerChannelPolicy())
def set_policy(self, channel_id: str, policy: PerChannelPolicy) -> None:
self.channels[channel_id] = policy
@classmethod
def from_config(cls, config: dict[str, Any] | None) -> GroupPolicyRegistry:
if not config:
return cls()
channels_config = config.get("channels", {}) or {}
channels: dict[str, PerChannelPolicy] = {}
for ch_id, ch_config in channels_config.items():
if isinstance(ch_config, dict):
channels[ch_id] = PerChannelPolicy.from_config_entry(ch_config)
return cls(channels=channels)
def resolve_group_require_mention(
channel_id: str,
registry: GroupPolicyRegistry,
global_require_mention: bool = False,
) -> bool:
policy = registry.get_policy(channel_id)
return policy.require_mention or global_require_mention
def resolve_group_tool_policy(
channel_id: str,
registry: GroupPolicyRegistry,
tool_name: str = "send",
) -> bool:
policy = registry.get_policy(channel_id)
if tool_name == "send":
return policy.tools_send_enabled
return True