99 lines
3.0 KiB
Python
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}
|