52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import dataclass
|
||
|
|
|
||
|
|
from slack_sdk.errors import SlackApiError
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
|
||
|
|
@dataclass
|
||
|
|
class RoomContext:
|
||
|
|
topic: str = ""
|
||
|
|
purpose: str = ""
|
||
|
|
channel_name: str = ""
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_empty(self) -> bool:
|
||
|
|
return not (self.topic or self.purpose or self.channel_name)
|
||
|
|
|
||
|
|
def to_system_prompt_fragment(self) -> str:
|
||
|
|
parts = []
|
||
|
|
if self.channel_name:
|
||
|
|
parts.append(f"频道名称: #{self.channel_name}")
|
||
|
|
if self.topic:
|
||
|
|
parts.append(f"频道主题: {self.topic}")
|
||
|
|
if self.purpose:
|
||
|
|
parts.append(f"频道用途: {self.purpose}")
|
||
|
|
return "\n".join(parts)
|
||
|
|
|
||
|
|
|
||
|
|
async def extract_room_context(
|
||
|
|
client,
|
||
|
|
channel_id: str,
|
||
|
|
) -> RoomContext:
|
||
|
|
try:
|
||
|
|
if not channel_id or channel_id.startswith("D"):
|
||
|
|
return RoomContext()
|
||
|
|
|
||
|
|
resp = await client.conversations_info(channel=channel_id)
|
||
|
|
ch = resp.get("channel", {})
|
||
|
|
return RoomContext(
|
||
|
|
topic=ch.get("topic", {}).get("value", ""),
|
||
|
|
purpose=ch.get("purpose", {}).get("value", ""),
|
||
|
|
channel_name=ch.get("name", ""),
|
||
|
|
)
|
||
|
|
except SlackApiError as e:
|
||
|
|
logger.debug(f"Failed to extract room context for {channel_id}: {e}")
|
||
|
|
return RoomContext()
|
||
|
|
except Exception:
|
||
|
|
logger.debug(f"Room context extraction failed for {channel_id}", exc_info=True)
|
||
|
|
return RoomContext()
|