execute method
Execute a command by name with the given args and context.
Returns null if the command is not found or not a local command.
Implementation
Future<CommandResult?> execute(
String name,
String args,
ToolUseContext context,
) async {
final reg = get(name);
if (reg == null) return null;
if (!reg.command.isEnabled) {
return TextCommandResult('Command "/$name" is currently disabled.');
}
if (reg.requiresAuth) {
// Caller should check auth before executing; this is a fallback.
}
CommandResult? result;
bool isError = false;
try {
if (reg.command is LocalCommand) {
result = await (reg.command as LocalCommand).execute(args, context);
} else if (reg.command is LocalUiCommand) {
result = await (reg.command as LocalUiCommand).execute(args, context);
} else {
// Prompt commands are handled by the conversation loop, not here.
return null;
}
} catch (e) {
isError = true;
result = TextCommandResult('Error: $e');
}
// Track stats.
reg.executionCount++;
reg.lastExecutedAt = DateTime.now();
// Emit event.
_executionController.add(
CommandExecutionEvent(
commandName: reg.name,
category: reg.category,
args: args,
isError: isError,
timestamp: DateTime.now(),
),
);
return result;
}