Add flag for credit

This commit is contained in:
2025-07-17 02:41:02 +07:00
parent 4b18d43b3f
commit e4de01211d

View File

@@ -26,6 +26,7 @@ public class KaigenService : IKaigenService
private readonly KaigenSettings _settings; private readonly KaigenSettings _settings;
private readonly ILogger<KaigenService> _logger; private readonly ILogger<KaigenService> _logger;
private readonly JsonSerializerOptions _jsonOptions; private readonly JsonSerializerOptions _jsonOptions;
private readonly bool _creditsEnabled;
public KaigenService(IOptions<KaigenSettings> options, ILogger<KaigenService> logger) public KaigenService(IOptions<KaigenSettings> options, ILogger<KaigenService> logger)
{ {
@@ -37,6 +38,16 @@ public class KaigenService : IKaigenService
PropertyNamingPolicy = JsonNamingPolicy.CamelCase PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}; };
// Check if credits are enabled via environment variable
var creditsEnabledEnv = Environment.GetEnvironmentVariable("KAIGEN_CREDITS_ENABLED");
_creditsEnabled = string.IsNullOrEmpty(creditsEnabledEnv) || creditsEnabledEnv.ToLower() == "true";
if (!_creditsEnabled)
{
_logger.LogInformation("Kaigen credits are disabled via KAIGEN_CREDITS_ENABLED environment variable");
return; // Skip private key validation if credits are disabled
}
// Always read from environment variable for security // Always read from environment variable for security
var envPrivateKey = Environment.GetEnvironmentVariable("KAIGEN_PRIVATE_KEY"); var envPrivateKey = Environment.GetEnvironmentVariable("KAIGEN_PRIVATE_KEY");
if (!string.IsNullOrEmpty(envPrivateKey)) if (!string.IsNullOrEmpty(envPrivateKey))
@@ -45,7 +56,7 @@ public class KaigenService : IKaigenService
_logger.LogInformation("Using KAIGEN_PRIVATE_KEY from environment variable"); _logger.LogInformation("Using KAIGEN_PRIVATE_KEY from environment variable");
} }
// Validate required settings // Validate required settings only if credits are enabled
if (string.IsNullOrEmpty(_settings.PrivateKey)) if (string.IsNullOrEmpty(_settings.PrivateKey))
{ {
throw new InvalidOperationException("Kaigen PrivateKey is not configured. Please set the KAIGEN_PRIVATE_KEY environment variable."); throw new InvalidOperationException("Kaigen PrivateKey is not configured. Please set the KAIGEN_PRIVATE_KEY environment variable.");
@@ -59,6 +70,15 @@ public class KaigenService : IKaigenService
public async Task<string> DebitUserCreditsAsync(User user, decimal debitAmount) public async Task<string> DebitUserCreditsAsync(User user, decimal debitAmount)
{ {
// If credits are disabled, return a dummy request ID
if (!_creditsEnabled)
{
var dummyRequestId = Guid.NewGuid().ToString();
_logger.LogInformation("Credits disabled - skipping debit for user {UserName}. Returning dummy request ID {RequestId}",
user.Name, dummyRequestId);
return dummyRequestId;
}
try try
{ {
var walletAddress = GetUserWalletAddress(user); var walletAddress = GetUserWalletAddress(user);
@@ -110,6 +130,14 @@ public class KaigenService : IKaigenService
public async Task<bool> RefundUserCreditsAsync(string requestId, User user) public async Task<bool> RefundUserCreditsAsync(string requestId, User user)
{ {
// If credits are disabled, return true (success) immediately
if (!_creditsEnabled)
{
_logger.LogInformation("Credits disabled - skipping refund for user {UserName} with request ID {RequestId}",
user.Name, requestId);
return true;
}
try try
{ {
var walletAddress = GetUserWalletAddress(user); var walletAddress = GetUserWalletAddress(user);