ForcePilot/backend/package/yuxi/channels/adapters/mattermost/agent_tools.py
Kris 002d601d1b feat(mattermost): 实现完整的 Mattermost 适配器模块
新增 Mattermost 渠道完整实现,包含适配器核心、消息处理、交互回调、命令支持、安全校验、多账号管理等功能,支持机器人消息发送、交互按钮、命令注册、投票功能以及配置动态修改等特性。
2026-05-12 00:46:12 +08:00

102 lines
3.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Any
def describe_mattermost_message_tool() -> dict:
return {
"type": "function",
"function": {
"name": "mattermost_send_message",
"description": "通过 Mattermost 渠道发送消息、编辑消息、删除消息或添加反应",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": ["send", "react", "edit", "delete"],
"description": "要执行的操作类型",
},
"chat_id": {
"type": "string",
"description": "目标频道或用户的 chat_id",
},
"content": {
"type": "string",
"description": "消息内容send 和 edit 操作需要)",
},
"msg_id": {
"type": "string",
"description": "消息 IDedit、delete 和 react 操作需要)",
},
"emoji": {
"type": "string",
"description": "表情符号名称react 操作需要)",
},
},
"required": ["action", "chat_id"],
},
},
}
class MattermostAgentToolFactory:
def __init__(self, adapter: Any):
self._adapter = adapter
def list_tools(self) -> list[dict]:
return [describe_mattermost_message_tool()]
async def execute_tool(self, tool_name: str, params: dict) -> Any:
if tool_name == "mattermost_send_message":
return await self._execute_message_action(params)
raise ValueError(f"Unknown tool: {tool_name}")
async def _execute_message_action(self, params: dict) -> dict:
action = params.get("action", "send")
chat_id = params.get("chat_id", "")
if action == "send":
from yuxi.channels.models import (
ChannelIdentity,
ChannelResponse,
ChannelType,
)
response = ChannelResponse(
identity=ChannelIdentity(
channel_id="mattermost",
channel_type=ChannelType.MATTERMOST,
channel_user_id="",
channel_chat_id=chat_id,
),
content=params.get("content", ""),
)
result = await self._adapter.send(response)
return {"success": result.success, "message_id": result.message_id, "error": result.error}
if action == "react":
result = await self._adapter.send_reaction(
chat_id=chat_id,
msg_id=params.get("msg_id", ""),
emoji=params.get("emoji", ""),
)
return {"success": result.success, "error": result.error}
if action == "edit":
result = await self._adapter.edit_message(
chat_id=chat_id,
msg_id=params.get("msg_id", ""),
content=params.get("content", ""),
)
return {"success": result.success, "error": result.error}
if action == "delete":
result = await self._adapter.delete_message(
chat_id=chat_id,
msg_id=params.get("msg_id", ""),
)
return {"success": result.success, "error": result.error}
return {"success": False, "error": f"Unknown action: {action}"}