本次提交对Twitch适配器进行了全面升级与优化: 1. 修复UTF8截断逻辑,避免越界访问 2. 重构群聊策略配置,标准化mention相关规则 3. 新增消息缓存管理器,支持通过消息ID查询已发送消息 4. 更新配置schema,新增prefer_helix_send开关和deprecated策略自动转换 5. 新增CLEARMSG和ROOMSTATE IRC消息解析,补充事件订阅支持 6. 优化令牌刷新逻辑,增加重试机制与退避策略 7. 新增Helix API聊天消息发送、删除和公告功能 8. 扩展事件订阅类型,新增直播状态、频道更新等系统事件 9. 新增reply、delete_message、announcement等动作支持,完善操作能力 10. 重构流式发送逻辑,新增进度指示器和配置项 11. 优化重连策略,增加指数退避与计数重置
210 lines
8.5 KiB
Python
210 lines
8.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from yuxi.channels.models import ChannelIdentity, ChannelResponse, DeliveryResult
|
|
from yuxi.channels.protocols.actions import ActionContext
|
|
|
|
|
|
def _build_response(ctx: ActionContext, adapter: Any, content_key: str = "content") -> ChannelResponse | None:
|
|
content = ctx.get(content_key, ctx.get("media_url", ""))
|
|
if not ctx.chat_id or not content:
|
|
return None
|
|
identity = ChannelIdentity(
|
|
channel_id="twitch",
|
|
channel_type=adapter.channel_type,
|
|
channel_user_id="",
|
|
channel_chat_id=ctx.chat_id,
|
|
)
|
|
return ChannelResponse(identity=identity, content=content)
|
|
|
|
|
|
async def handle_action(ctx: ActionContext, adapter: Any) -> DeliveryResult:
|
|
if ctx.action == "send":
|
|
response = _build_response(ctx, adapter, "content")
|
|
if response is None:
|
|
return DeliveryResult(success=False, error="missing chat_id or content")
|
|
if adapter.config.get("prefer_helix_send", False):
|
|
result = await adapter._send_via_helix(adapter.format_outbound(response)["target"], response.content)
|
|
if result.success:
|
|
return result
|
|
return await adapter.send(response)
|
|
|
|
if ctx.action == "send_media":
|
|
response = _build_response(ctx, adapter, "media_url")
|
|
if response is None:
|
|
return DeliveryResult(success=False, error="missing chat_id or media_url")
|
|
return await adapter.send_media(ctx.chat_id, "url", response.content)
|
|
|
|
if ctx.action == "reply":
|
|
chat_id = ctx.get("chat_id", "")
|
|
content = ctx.get("content", "")
|
|
reply_to_msg_id = ctx.get("reply_to_msg_id", "")
|
|
if not chat_id or not content:
|
|
return DeliveryResult(success=False, error="missing chat_id or content")
|
|
if not reply_to_msg_id:
|
|
return DeliveryResult(success=False, error="missing reply_to_msg_id")
|
|
result = await adapter._send_via_helix(chat_id, content, reply_msg_id=reply_to_msg_id)
|
|
if result.success:
|
|
return result
|
|
prefixed = f"@{ctx.get('reply_to_username', 'user')} {content}"
|
|
response = ChannelResponse(
|
|
identity=ChannelIdentity(
|
|
channel_id="twitch",
|
|
channel_type=adapter.channel_type,
|
|
channel_user_id="",
|
|
channel_chat_id=chat_id,
|
|
),
|
|
content=prefixed,
|
|
)
|
|
return await adapter.send(response)
|
|
|
|
if ctx.action in ("unsend", "delete_message"):
|
|
chat_id = ctx.get("chat_id", "")
|
|
message_id = ctx.get("message_id", "")
|
|
if not chat_id or not adapter._helix:
|
|
return DeliveryResult(success=False, error="missing chat_id or helix not available")
|
|
if not message_id:
|
|
cached = adapter._outbound_cache.find_by_message_id(ctx.get("content", ""))
|
|
if cached:
|
|
message_id = cached.get("message_id", "")
|
|
if not message_id:
|
|
return DeliveryResult(success=False, error="missing message_id for message deletion")
|
|
|
|
broadcaster_id = await adapter._resolve_broadcaster_id(chat_id)
|
|
if not broadcaster_id:
|
|
return DeliveryResult(success=False, error="broadcaster_not_found")
|
|
|
|
moderator_id = adapter._bot_user_id
|
|
if not moderator_id:
|
|
return DeliveryResult(success=False, error="bot_user_id_unknown")
|
|
|
|
success = await adapter._helix.delete_chat_message(
|
|
broadcaster_id=broadcaster_id,
|
|
moderator_id=moderator_id,
|
|
message_id=message_id,
|
|
)
|
|
if success:
|
|
return DeliveryResult(success=True, metadata={"messageId": message_id})
|
|
return DeliveryResult(success=False, error="helix_delete_failed")
|
|
|
|
if ctx.action == "announcement":
|
|
chat_id = ctx.get("chat_id", "")
|
|
content = ctx.get("content", "")
|
|
color = ctx.get("color", "primary")
|
|
if not chat_id or not content or not adapter._helix:
|
|
return DeliveryResult(success=False, error="missing chat_id or content or helix not available")
|
|
|
|
broadcaster_id = await adapter._resolve_broadcaster_id(chat_id)
|
|
if not broadcaster_id:
|
|
return DeliveryResult(success=False, error="broadcaster_not_found")
|
|
|
|
moderator_id = adapter._bot_user_id
|
|
if not moderator_id:
|
|
return DeliveryResult(success=False, error="bot_user_id_unknown")
|
|
|
|
success = await adapter._helix.send_chat_announcement(
|
|
broadcaster_id=broadcaster_id,
|
|
moderator_id=moderator_id,
|
|
message=content,
|
|
color=color,
|
|
)
|
|
if success:
|
|
return DeliveryResult(success=True)
|
|
return DeliveryResult(success=False, error="announcement_failed")
|
|
|
|
unsupported_hints: dict[str, str] = {
|
|
"edit": "Twitch IRC does not support message editing",
|
|
"edit_message": "Twitch IRC does not support message editing",
|
|
"reactions": "Twitch IRC does not support reactions",
|
|
"send_reaction": "Twitch IRC does not support reactions",
|
|
"polls": "Twitch IRC does not support polls",
|
|
"native_commands": "Twitch IRC does not support native commands",
|
|
"pin": "Twitch IRC does not support message pinning",
|
|
"unpin": "Twitch IRC does not support message unpinning",
|
|
}
|
|
hint = unsupported_hints.get(ctx.action, f"unknown action: {ctx.action}")
|
|
return DeliveryResult(success=False, error=hint)
|
|
|
|
|
|
def supports_action(action: str) -> bool:
|
|
return action in {"send", "send_media", "reply", "unsend", "delete_message", "announcement"}
|
|
|
|
|
|
def describe_message_tool() -> dict[str, Any]:
|
|
return {
|
|
"send": {
|
|
"description": "Send a text message to a Twitch channel",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"content": {"type": "string", "description": "Text content to send"},
|
|
},
|
|
},
|
|
"send_media": {
|
|
"description": "Send a media URL to a Twitch channel (sent as text)",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"media_url": {"type": "string", "description": "Media URL to send"},
|
|
},
|
|
},
|
|
"reply": {
|
|
"description": "Reply to a message in a Twitch channel via Helix API",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"content": {"type": "string", "description": "Reply text content"},
|
|
"reply_to_msg_id": {"type": "string", "description": "Message ID to reply to"},
|
|
"reply_to_username": {"type": "string", "description": "Username being replied to (for IRC fallback)"},
|
|
},
|
|
},
|
|
"unsend": {
|
|
"description": "Delete a message from a Twitch channel via Helix API",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"message_id": {"type": "string", "description": "Message ID to delete"},
|
|
},
|
|
},
|
|
"delete_message": {
|
|
"description": "Delete a message from a Twitch channel via Helix API",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"message_id": {"type": "string", "description": "Message ID to delete"},
|
|
},
|
|
},
|
|
"announcement": {
|
|
"description": "Send a colored announcement to a Twitch channel",
|
|
"parameters": {
|
|
"chat_id": {"type": "string", "description": "Channel name (e.g. #channel)"},
|
|
"content": {"type": "string", "description": "Announcement text"},
|
|
"color": {"type": "string", "description": "Announcement color: blue/green/orange/purple/primary"},
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def extract_target_from_args(args: dict[str, Any]) -> dict | None:
|
|
chat_id = args.get("chat_id")
|
|
if chat_id:
|
|
return {"chat_id": chat_id}
|
|
return None
|
|
|
|
|
|
def resolve_execution_mode(action: str) -> str:
|
|
return "send"
|
|
|
|
|
|
def get_action_stats() -> dict[str, list[str]]:
|
|
return {
|
|
"implemented": ["send", "send_media", "reply", "unsend", "delete_message", "announcement"],
|
|
"planned": [],
|
|
"unsupported": [
|
|
"edit",
|
|
"reactions",
|
|
"polls",
|
|
"native_commands",
|
|
"pin",
|
|
"unpin",
|
|
"send_reaction",
|
|
"edit_message",
|
|
],
|
|
}
|