execute method
Execute the command.
Implementation
@override
Future<CommandResult> execute(String args, ToolUseContext context) async {
final tools = getTools();
final filter = args.trim().toLowerCase();
if (filter.isNotEmpty) {
// Show details for a specific tool
final tool = tools.firstWhere(
(t) => (t['name'] as String? ?? '').toLowerCase() == filter,
orElse: () => <String, dynamic>{},
);
if (tool.isEmpty) {
return TextCommandResult('Tool not found: $filter');
}
final buffer = StringBuffer();
buffer.writeln('Tool: ${tool['name']}');
buffer.writeln(' Description: ${tool['description'] ?? 'none'}');
buffer.writeln(' Enabled: ${tool['enabled'] ?? true}');
buffer.writeln(' Read-only: ${tool['readOnly'] ?? false}');
buffer.writeln(' Destructive: ${tool['destructive'] ?? false}');
if (tool['source'] != null) {
buffer.writeln(' Source: ${tool['source']}');
}
return TextCommandResult(buffer.toString());
}
if (tools.isEmpty) {
return const TextCommandResult('No tools available.');
}
// Group tools by source
final grouped = <String, List<Map<String, dynamic>>>{};
for (final tool in tools) {
final source = tool['source'] as String? ?? 'builtin';
grouped.putIfAbsent(source, () => []).add(tool);
}
final buffer = StringBuffer();
buffer.writeln('Available Tools (${tools.length}):');
for (final entry in grouped.entries) {
buffer.writeln();
buffer.writeln(' [${entry.key}]');
for (final tool in entry.value) {
final name = tool['name'] ?? '?';
final enabled = tool['enabled'] ?? true;
final marker = enabled ? '' : ' (disabled)';
final desc = tool['description'] as String? ?? '';
final shortDesc = desc.length > 50
? '${desc.substring(0, 47)}...'
: desc;
buffer.writeln(' $name$marker — $shortDesc');
}
}
buffer.writeln();
buffer.writeln('Use /tools <name> for details on a specific tool.');
return TextCommandResult(buffer.toString());
}