import 'route_navigation_tool.dart'; typedef ToolHandler = Future> Function(Map args); /// 工具常量 const _toolNameNavigateRoute = 'front.navigate_to_route'; class ToolDefinition { final String name; final String description; final Map parameters; final ToolHandler handler; ToolDefinition({ required this.name, required this.description, required this.parameters, required this.handler, }); } class ToolRegistry { static final Map _tools = {}; static bool _initialized = false; static void initialize() { if (_initialized) return; _tools[_toolNameNavigateRoute] = ToolDefinition( name: _toolNameNavigateRoute, description: '在前端执行路由跳转', parameters: { 'type': 'object', 'properties': { 'target': {'type': 'string', 'description': '跳转目标路由'}, 'replace': {'type': 'boolean', 'description': '是否 replace 导航'}, }, 'required': ['target'], }, handler: _handleNavigateRoute, ); _initialized = true; } static Future> _handleNavigateRoute( Map args, ) async { return RouteNavigationTool.instance.execute(args); } static ToolDefinition? getTool(String name) => _tools[name]; static List getAllTools() => _tools.values.toList(); static Future> execute( String toolName, Map args, ) async { final tool = _tools[toolName]; if (tool == null) throw ToolNotFoundException('Tool not found: $toolName'); return tool.handler(args); } static ToolValidationResult validateArgs( String toolName, Map args, ) { final tool = _tools[toolName]; if (tool == null) { return ToolValidationResult( ok: false, error: 'Tool not found: $toolName', ); } final required = tool.parameters['required'] as List? ?? []; final missing = []; for (final field in required) { if (!args.containsKey(field) || args[field] == null) { missing.add(field as String); } } if (missing.isNotEmpty) { return ToolValidationResult( ok: false, error: 'Missing required fields: ${missing.join(', ')}', ); } return ToolValidationResult(ok: true); } } class ToolNotFoundException implements Exception { final String message; ToolNotFoundException(this.message); @override String toString() => 'ToolNotFoundException: $message'; } class ToolValidationResult { final bool ok; final String? error; ToolValidationResult({required this.ok, this.error}); }