ForcePilot/backend/package/yuxi/channel/extensions/msteams/feedback.py
Kris 94444ced96 feat(channel): 添加 Microsoft Teams 渠道扩展
新增 Microsoft Teams 渠道扩展,支持在 Yuxi 平台中集成 Microsoft Teams 协作平台。

包含以下功能模块:
- sdk: Bot Framework SDK 封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- auth: JWT 认证
- jwks: JWKS 密钥管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- state: 状态管理
- runtime: 运行时管理
- actions: 动作处理
- adaptive_card: 自适应卡片
- task_modules: 任务模块
- message_extension: 消息扩展
- proactive: Proactive Messaging
- graph: Microsoft Graph API 集成
- graph_teams: Teams 操作
- graph_members: 成员管理
- graph_messages: 消息获取
- graph_thread: 线程管理
- graph_users: 用户管理
- graph_upload: 文件上传
- files: 文件处理
- file_consent: 文件授权
- conversations: 会话存储
- mentions: @提及处理
- threading: 线程管理
- reactions: 表情反应
- polls: 投票功能
- meetings: 会议集成
- feedback: 反馈处理
- sso: 单点登录
- deep_links: 深层链接
- incoming_webhook: 入站 Webhook
- localization: 本地化
- user_agent: 用户代理
- sent_message_cache: 消息缓存
- types: 类型定义
2026-05-21 11:28:42 +08:00

99 lines
3.0 KiB
Python

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class MSTeamsFeedback:
def __init__(
self,
*,
enabled: bool = True,
reflection_enabled: bool = True,
reflection_cooldown_ms: int = 300000,
):
self.enabled = enabled
self.reflection_enabled = reflection_enabled
self.reflection_cooldown_ms = reflection_cooldown_ms
self._last_reflection: float = 0.0
def is_enabled(self) -> bool:
return self.enabled
def is_reflection_enabled(self) -> bool:
return self.reflection_enabled
def can_reflect(self) -> bool:
if not self.reflection_enabled:
return False
import time
now = time.monotonic() * 1000
return now - self._last_reflection >= self.reflection_cooldown_ms
def mark_reflection_sent(self) -> None:
import time
self._last_reflection = time.monotonic() * 1000
def parse_teams_feedback(self, value: dict) -> dict | None:
feedback = value.get("feedback", {})
reaction = feedback.get("reaction", "")
if reaction in ("like", "dislike"):
return {
"reaction": reaction,
"feedback_id": feedback.get("id", ""),
"activity_id": value.get("activityId", ""),
"additional_info": feedback.get("additionalInfo", {}),
}
return None
def build_feedback_reflection(
self,
feedback_data: dict,
) -> str | None:
reaction = feedback_data.get("reaction", "")
additional = feedback_data.get("additional_info", {})
if reaction == "dislike":
intro = "用户对 AI 回复表示不满意"
if additional:
reasons = additional.get("reasons", [])
text = additional.get("text", "")
details = []
if reasons:
details.append(f"原因: {', '.join(reasons)}")
if text:
details.append(f"反馈: {text}")
if details:
intro += f" ({'; '.join(details)})"
return intro
return None
async def handle_feedback_invoke(
self,
adapter,
conversation_store,
activity_data: dict,
) -> dict:
value = activity_data.get("value", {}) or {}
feedback = self.parse_teams_feedback(value)
if not feedback:
return {"status": 200}
if feedback["reaction"] == "like":
logger.info("User liked AI response (activity=%s)", feedback["activity_id"])
elif feedback["reaction"] == "dislike":
logger.info("User disliked AI response (activity=%s)", feedback["activity_id"])
if self.can_reflect():
reflection = self.build_feedback_reflection(feedback)
if reflection:
logger.info("Feedback reflection: %s", reflection)
self.mark_reflection_sent()
return {"status": 200}