Reduce Agent Summary call

This commit is contained in:
2025-09-15 00:19:21 +07:00
parent 37d57a1bb8
commit b0d2dcc6b9
10 changed files with 402 additions and 36 deletions

View File

@@ -1,5 +1,6 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Services;
using Managing.Application.Trading.Commands;
using Managing.Application.Trading.Handlers;
@@ -93,6 +94,9 @@ public class TradingBotBase : ITradingBot
$"📢 I'll notify you when signals are triggered.";
await LogInformation(startupMessage);
// Notify AgentGrain about bot startup
await NotifyAgentGrainAsync(AgentSummaryEventType.BotStarted, $"Bot: {Config.Name}, Ticker: {Config.Ticker}");
break;
case BotStatus.Running:
@@ -191,7 +195,7 @@ public class TradingBotBase : ITradingBot
}
}
public async Task UpdateSignals(HashSet<Candle>? candles = null)
public async Task UpdateSignals(HashSet<Candle> candles = null)
{
// If position open and not flipped, do not update signals
if (!Config.FlipPosition && Positions.Any(p => !p.Value.IsFinished())) return;
@@ -765,6 +769,9 @@ public class TradingBotBase : ITradingBot
async messengerService => { await messengerService.SendPosition(position); });
}
// Notify AgentGrain about position opening
await NotifyAgentGrainAsync(AgentSummaryEventType.PositionOpened, $"Signal: {signal.Identifier}");
Logger.LogInformation($"Position requested");
return position; // Return the created position without adding to list
}
@@ -1195,6 +1202,10 @@ public class TradingBotBase : ITradingBot
Logger.LogInformation(
$"💰 **Balance Updated**\nNew bot trading balance: `${Config.BotTradingBalance:F2}`");
}
// Notify AgentGrain about position closing
var pnlInfo = position.ProfitAndLoss?.Realized != null ? $"PnL: {position.ProfitAndLoss.Realized:F2}" : "PnL: Unknown";
await NotifyAgentGrainAsync(AgentSummaryEventType.PositionClosed, $"Signal: {position.SignalIdentifier}, {pnlInfo}");
}
else
{
@@ -1875,4 +1886,42 @@ public class TradingBotBase : ITradingBot
return isInCooldown;
}
/// <summary>
/// Sends a notification to the AgentGrain to trigger summary updates
/// </summary>
/// <param name="eventType">The type of event (e.g., PositionOpened, PositionClosed)</param>
/// <param name="additionalData">Optional additional context data</param>
private async Task NotifyAgentGrainAsync(AgentSummaryEventType eventType, string additionalData = null)
{
if (Config.IsForBacktest || Account?.User == null)
{
return; // Skip notifications for backtest or when no user context
}
try
{
await ServiceScopeHelpers.WithScopedService<IGrainFactory>(_scopeFactory, async grainFactory =>
{
var agentGrain = grainFactory.GetGrain<IAgentGrain>(Account.User.Id);
var updateEvent = new AgentSummaryUpdateEvent
{
UserId = Account.User.Id,
BotId = Identifier,
EventType = eventType,
Timestamp = DateTime.UtcNow,
AdditionalData = additionalData
};
await agentGrain.OnAgentSummaryUpdateAsync(updateEvent);
Logger.LogDebug("Sent agent notification: {EventType} for bot {BotId}", eventType, Identifier);
});
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to send agent notification: {EventType} for bot {BotId}", eventType, Identifier);
}
}
}