route method
Routes toolName to the registered tool and executes it with input.
Returns a ToolResult — never throws.
Implementation
Future<ToolResult> route(
String toolName,
Map<String, dynamic> input,
) async {
final tool = _registry.find(toolName);
if (tool == null) {
_logger?.warning('Unknown tool requested: "$toolName"');
return ToolResult.failure(
toolName,
'Tool "$toolName" is not registered. '
'Available: ${_registry.names.join(", ")}',
);
}
final validationError = tool.validate(input);
if (validationError != null) {
_logger?.warning('Tool "$toolName" validation failed: $validationError');
return ToolResult.failure(toolName, validationError);
}
_logger?.info('Routing → $toolName | input: $input');
try {
final result = await tool.run(input);
_logger?.info(
'Tool "$toolName" → ${result.success ? "OK" : "FAIL"}: '
'"${_trunc(result.outputText)}"');
return result;
} catch (e, st) {
_logger?.error('Tool "$toolName" threw unexpectedly', e, st);
return ToolResult.failure(toolName, 'Unexpected error: $e');
}
}