102 lines
3.6 KiB
Python
102 lines
3.6 KiB
Python
|
|
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": "消息 ID(edit、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}"}
|