mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 09:03:54 +08:00
update:统一工具注册及调用
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
"""基础工具定义模块"""
|
||||
|
||||
from .tool_types import ToolType, ToolAction, ToolResult, ToolDefinition
|
||||
from .tool_executor import ToolExecutor
|
||||
|
||||
__all__ = ["ToolType", "ToolAction", "ToolResult", "ToolDefinition", "ToolExecutor"]
|
||||
@@ -0,0 +1,26 @@
|
||||
"""工具执行器基类定义"""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Dict, Any
|
||||
from .tool_types import ToolDefinition, ToolResult
|
||||
|
||||
|
||||
class ToolExecutor(ABC):
|
||||
"""工具执行器抽象基类"""
|
||||
|
||||
@abstractmethod
|
||||
async def execute(
|
||||
self, conn, tool_name: str, arguments: Dict[str, Any]
|
||||
) -> ToolResult:
|
||||
"""执行工具调用"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_tools(self) -> Dict[str, ToolDefinition]:
|
||||
"""获取该执行器管理的所有工具"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def has_tool(self, tool_name: str) -> bool:
|
||||
"""检查是否有指定工具"""
|
||||
pass
|
||||
@@ -0,0 +1,45 @@
|
||||
"""工具系统的类型定义"""
|
||||
|
||||
from enum import Enum
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, Optional, Callable, Awaitable
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class ToolType(Enum):
|
||||
"""工具类型枚举"""
|
||||
|
||||
SERVER_PLUGIN = "server_plugin" # 服务端插件
|
||||
SERVER_MCP = "server_mcp" # 服务端MCP
|
||||
DEVICE_IOT = "device_iot" # 设备端IoT
|
||||
DEVICE_MCP = "device_mcp" # 设备端MCP
|
||||
|
||||
|
||||
class ToolAction(Enum):
|
||||
"""工具执行后的动作类型"""
|
||||
|
||||
ERROR = "error" # 错误
|
||||
NOT_FOUND = "not_found" # 工具未找到
|
||||
RESPONSE = "response" # 直接回复
|
||||
REQUEST_LLM = "request_llm" # 需要LLM处理
|
||||
NONE = "none" # 无需特殊处理
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolResult:
|
||||
"""工具执行结果"""
|
||||
|
||||
action: ToolAction
|
||||
content: str # 结果内容
|
||||
response: Optional[str] = None # 直接回复内容
|
||||
error: Optional[str] = None # 错误信息
|
||||
|
||||
|
||||
@dataclass
|
||||
class ToolDefinition:
|
||||
"""工具定义"""
|
||||
|
||||
name: str # 工具名称
|
||||
description: Dict[str, Any] # 工具描述(OpenAI函数调用格式)
|
||||
tool_type: ToolType # 工具类型
|
||||
parameters: Optional[Dict[str, Any]] = None # 额外参数
|
||||
Reference in New Issue
Block a user