namespace Service.AgentFramework; /// /// Tool 注册表实现 /// public class ToolRegistry : IToolRegistry { private readonly Dictionary _tools = new(); private readonly ILogger _logger; public ToolRegistry(ILogger logger) { _logger = logger; } public void RegisterTool( string name, string description, Func> handler, string category = "General", bool cacheable = false) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Tool 名称不能为空", nameof(name)); var toolDef = new ToolDefinition { Name = name, Description = description, Handler = handler, Category = category, Cacheable = cacheable }; _tools[name] = toolDef; _logger.LogInformation("已注册 Tool: {ToolName} (类别: {Category})", name, category); } public void RegisterTool( string name, string description, Func> handler, string category = "General", bool cacheable = false) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Tool 名称不能为空", nameof(name)); var toolDef = new ToolDefinition { Name = name, Description = description, Handler = handler, Category = category, Cacheable = cacheable }; _tools[name] = toolDef; _logger.LogInformation("已注册 Tool: {ToolName} (类别: {Category})", name, category); } public void RegisterTool( string name, string description, Func> handler, string category = "General", bool cacheable = false) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentException("Tool 名称不能为空", nameof(name)); var toolDef = new ToolDefinition { Name = name, Description = description, Handler = handler, Category = category, Cacheable = cacheable }; _tools[name] = toolDef; _logger.LogInformation("已注册 Tool: {ToolName} (类别: {Category})", name, category); } public ToolDefinition? GetToolDefinition(string name) { return _tools.TryGetValue(name, out var tool) ? tool : null; } public IEnumerable GetAllTools() { return _tools.Values; } public IEnumerable GetToolsByCategory(string category) { return _tools.Values.Where(t => t.Category == category); } public async Task InvokeToolAsync(string toolName) { if (!_tools.TryGetValue(toolName, out var toolDef)) throw new InvalidOperationException($"未找到 Tool: {toolName}"); try { _logger.LogDebug("调用 Tool: {ToolName}", toolName); if (toolDef.Handler is Func> handler) { var result = await handler(); _logger.LogDebug("Tool {ToolName} 执行成功", toolName); return result; } throw new InvalidOperationException($"Tool {toolName} 签名不匹配"); } catch (Exception ex) { _logger.LogError(ex, "Tool {ToolName} 执行失败", toolName); throw; } } public async Task InvokeToolAsync(string toolName, TParam param) { if (!_tools.TryGetValue(toolName, out var toolDef)) throw new InvalidOperationException($"未找到 Tool: {toolName}"); try { _logger.LogDebug("调用 Tool: {ToolName}, 参数: {Param}", toolName, param); if (toolDef.Handler is Func> handler) { var result = await handler(param); _logger.LogDebug("Tool {ToolName} 执行成功", toolName); return result; } throw new InvalidOperationException($"Tool {toolName} 签名不匹配"); } catch (Exception ex) { _logger.LogError(ex, "Tool {ToolName} 执行失败", toolName); throw; } } public async Task InvokeToolAsync( string toolName, TParam1 param1, TParam2 param2) { if (!_tools.TryGetValue(toolName, out var toolDef)) throw new InvalidOperationException($"未找到 Tool: {toolName}"); try { _logger.LogDebug("调用 Tool: {ToolName}, 参数: {Param1}, {Param2}", toolName, param1, param2); if (toolDef.Handler is Func> handler) { var result = await handler(param1, param2); _logger.LogDebug("Tool {ToolName} 执行成功", toolName); return result; } throw new InvalidOperationException($"Tool {toolName} 签名不匹配"); } catch (Exception ex) { _logger.LogError(ex, "Tool {ToolName} 执行失败", toolName); throw; } } }