|
| 1 | +"""Channel Harness — transport-independent message model and declarative routing. |
| 2 | +
|
| 3 | +This module is the meta-level routing layer for the control plane. It defines: |
| 4 | +
|
| 5 | +* :class:`ChannelRole` — *semantic* roles (chat, notification, priority feed, …) that are |
| 6 | + independent of any platform (Discord/Slack/CLI/REST). |
| 7 | +* :class:`ChannelMessage` — a transport-independent inbound message. Every adapter converts its |
| 8 | + native event into a ``ChannelMessage`` so the orchestration layer never knows the platform. |
| 9 | +* :class:`ChannelRouter` — declarative mapping ``ChannelRole`` ⇄ a concrete channel, plus the |
| 10 | + behavioural policy of each role (reply-without-mention, post-only, mention-owner). |
| 11 | +
|
| 12 | +It contains **no platform API calls and no business logic** — pure, deterministic mapping that is |
| 13 | +unit/E2E testable in isolation and reused by adapters, schedulers, and the proactive feed alike. |
| 14 | +
|
| 15 | +Example (research never mentions Discord):: |
| 16 | +
|
| 17 | + research → importance=HIGH → ChannelRouter(role=PRIORITY_FEED) → adapter → Discord |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import enum |
| 23 | +from dataclasses import dataclass |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +from pydantic import BaseModel, Field |
| 27 | + |
| 28 | +from nexus.config import DiscordChannels |
| 29 | + |
| 30 | + |
| 31 | +class ChannelRole(enum.StrEnum): |
| 32 | + """Semantic, transport-independent role of a channel.""" |
| 33 | + |
| 34 | + CHAT = "chat" # free-form operator conversation |
| 35 | + NOTIFICATION = "notification" # reminders / TODOs / nudges (mention owner) |
| 36 | + PRIORITY_FEED = "priority_feed" # high-importance briefs (mention owner) |
| 37 | + BRIEFING = "briefing" # scheduled digests / summaries |
| 38 | + APPROVAL = "approval" # approval cards |
| 39 | + SYSTEM = "system" # Dex status / action cards / system logs |
| 40 | + |
| 41 | + |
| 42 | +class ChannelMessage(BaseModel): |
| 43 | + """Transport-independent inbound message. Adapters normalize native events into this.""" |
| 44 | + |
| 45 | + role: ChannelRole = ChannelRole.CHAT |
| 46 | + author: str # platform-agnostic author id (e.g. discord user id as str) |
| 47 | + channel_id: str |
| 48 | + conversation_id: str # stable key for conversation memory |
| 49 | + message: str |
| 50 | + metadata: dict[str, Any] = Field(default_factory=dict) # e.g. {"is_owner": bool, "is_dm": bool} |
| 51 | + |
| 52 | + |
| 53 | +@dataclass(frozen=True) |
| 54 | +class ChannelPolicy: |
| 55 | + """Behavioural policy for a channel role.""" |
| 56 | + |
| 57 | + role: ChannelRole |
| 58 | + respond_without_mention: bool = False # reply to plain (un-mentioned) messages here |
| 59 | + post_only: bool = False # Dex posts here but does not converse |
| 60 | + mention_owner: bool = False # prepend an owner mention to posts |
| 61 | + |
| 62 | + |
| 63 | +_POLICIES: dict[ChannelRole, ChannelPolicy] = { |
| 64 | + ChannelRole.CHAT: ChannelPolicy(ChannelRole.CHAT, respond_without_mention=True), |
| 65 | + ChannelRole.NOTIFICATION: ChannelPolicy(ChannelRole.NOTIFICATION, post_only=True, mention_owner=True), |
| 66 | + ChannelRole.PRIORITY_FEED: ChannelPolicy(ChannelRole.PRIORITY_FEED, post_only=True, mention_owner=True), |
| 67 | + ChannelRole.BRIEFING: ChannelPolicy(ChannelRole.BRIEFING, post_only=True), |
| 68 | + ChannelRole.APPROVAL: ChannelPolicy(ChannelRole.APPROVAL, post_only=True), |
| 69 | + ChannelRole.SYSTEM: ChannelPolicy(ChannelRole.SYSTEM, post_only=True), |
| 70 | +} |
| 71 | + |
| 72 | +# Declarative binding: semantic role -> DiscordChannels attribute that resolves the concrete name. |
| 73 | +# A future Slack/REST adapter would supply its own binding; the roles stay identical. |
| 74 | +_DEFAULT_DISCORD_BINDING: dict[ChannelRole, str] = { |
| 75 | + ChannelRole.CHAT: "general", |
| 76 | + ChannelRole.NOTIFICATION: "reminders", |
| 77 | + ChannelRole.PRIORITY_FEED: "priority_feed", |
| 78 | + ChannelRole.BRIEFING: "summaries", |
| 79 | + ChannelRole.APPROVAL: "approvals", |
| 80 | + ChannelRole.SYSTEM: "console", |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +class ChannelRouter: |
| 85 | + """Resolves semantic roles ⇄ concrete channels and exposes per-role policy.""" |
| 86 | + |
| 87 | + def __init__( |
| 88 | + self, |
| 89 | + channels: DiscordChannels, |
| 90 | + binding: dict[ChannelRole, str] | None = None, |
| 91 | + ) -> None: |
| 92 | + self._channels = channels |
| 93 | + self._binding = binding or _DEFAULT_DISCORD_BINDING |
| 94 | + # Reverse map concrete-name -> role for inbound routing (lower-cased for tolerance). |
| 95 | + self._name_to_role: dict[str, ChannelRole] = {} |
| 96 | + for role, key in self._binding.items(): |
| 97 | + name = getattr(channels, key, None) |
| 98 | + if name: |
| 99 | + self._name_to_role[str(name).lower()] = role |
| 100 | + |
| 101 | + def policy(self, role: ChannelRole) -> ChannelPolicy: |
| 102 | + """Return the behavioural policy for a role.""" |
| 103 | + return _POLICIES[role] |
| 104 | + |
| 105 | + def channel_key(self, role: ChannelRole) -> str | None: |
| 106 | + """Return the DiscordChannels attribute name bound to this role.""" |
| 107 | + return self._binding.get(role) |
| 108 | + |
| 109 | + def channel_name(self, role: ChannelRole) -> str | None: |
| 110 | + """Return the concrete channel name bound to a role.""" |
| 111 | + key = self._binding.get(role) |
| 112 | + return getattr(self._channels, key, None) if key else None |
| 113 | + |
| 114 | + def role_for_channel_name(self, name: str | None) -> ChannelRole | None: |
| 115 | + """Map an inbound concrete channel name to its semantic role (None if unmapped).""" |
| 116 | + if not name: |
| 117 | + return None |
| 118 | + return self._name_to_role.get(name.lower()) |
| 119 | + |
| 120 | + def respond_without_mention(self, channel_name: str | None) -> bool: |
| 121 | + """True if Dex should reply to plain (un-mentioned) messages in this channel.""" |
| 122 | + role = self.role_for_channel_name(channel_name) |
| 123 | + return role is not None and self.policy(role).respond_without_mention |
0 commit comments