execute method
Execute the command.
Implementation
@override
Future<CommandResult> execute(String args, ToolUseContext context) async {
if (isAuthenticated()) {
return const TextCommandResult(
'Already authenticated. Use /logout first to switch accounts.',
);
}
final key = args.trim();
if (key.isEmpty) {
// Check environment
final envKey = Platform.environment['ANTHROPIC_API_KEY'];
if (envKey != null && envKey.isNotEmpty) {
final success = await onLogin(envKey);
if (success) {
return const TextCommandResult(
'Authenticated using ANTHROPIC_API_KEY from environment.',
);
}
return const TextCommandResult(
'ANTHROPIC_API_KEY found but authentication failed. '
'Check that your key is valid.',
);
}
return const TextCommandResult(
'No API key provided.\n'
'Usage: /login <api-key>\n'
'Or set ANTHROPIC_API_KEY environment variable.',
);
}
if (!key.startsWith('sk-ant-')) {
return const TextCommandResult(
'Invalid API key format. Anthropic keys start with "sk-ant-".',
);
}
final success = await onLogin(key);
if (success) {
return const TextCommandResult('Authenticated successfully.');
}
return const TextCommandResult(
'Authentication failed. Check your API key.',
);
}