| | | 1 | | using EHonda.KicktippAi.Core; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using Spectre.Console.Cli; |
| | | 4 | | using Spectre.Console; |
| | | 5 | | using OpenAiIntegration; |
| | | 6 | | using Orchestrator.Commands.Operations.Matchday; |
| | | 7 | | using Orchestrator.Commands.Shared; |
| | | 8 | | using Orchestrator.Infrastructure.Factories; |
| | | 9 | | |
| | | 10 | | namespace Orchestrator.Commands.Operations.Bonus; |
| | | 11 | | |
| | | 12 | | public class BonusCommand : AsyncCommand<BaseSettings> |
| | | 13 | | { |
| | | 14 | | private readonly IAnsiConsole _console; |
| | | 15 | | private readonly IFirebaseServiceFactory _firebaseServiceFactory; |
| | | 16 | | private readonly IKicktippClientFactory _kicktippClientFactory; |
| | | 17 | | private readonly IOpenAiServiceFactory _openAiServiceFactory; |
| | | 18 | | private readonly IContextProviderFactory _contextProviderFactory; |
| | | 19 | | private readonly ILogger<BonusCommand> _logger; |
| | | 20 | | |
| | 1 | 21 | | public BonusCommand( |
| | 1 | 22 | | IAnsiConsole console, |
| | 1 | 23 | | IFirebaseServiceFactory firebaseServiceFactory, |
| | 1 | 24 | | IKicktippClientFactory kicktippClientFactory, |
| | 1 | 25 | | IOpenAiServiceFactory openAiServiceFactory, |
| | 1 | 26 | | IContextProviderFactory contextProviderFactory, |
| | 1 | 27 | | ILogger<BonusCommand> logger) |
| | | 28 | | { |
| | 1 | 29 | | _console = console; |
| | 1 | 30 | | _firebaseServiceFactory = firebaseServiceFactory; |
| | 1 | 31 | | _kicktippClientFactory = kicktippClientFactory; |
| | 1 | 32 | | _openAiServiceFactory = openAiServiceFactory; |
| | 1 | 33 | | _contextProviderFactory = contextProviderFactory; |
| | 1 | 34 | | _logger = logger; |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public override async Task<int> ExecuteAsync(CommandContext context, BaseSettings settings) |
| | | 38 | | { |
| | | 39 | | |
| | | 40 | | try |
| | | 41 | | { |
| | 1 | 42 | | _console.MarkupLine($"[green]Bonus command initialized with model:[/] [yellow]{settings.Model}[/]"); |
| | | 43 | | |
| | 1 | 44 | | if (settings.Verbose) |
| | | 45 | | { |
| | 1 | 46 | | _console.MarkupLine("[dim]Verbose mode enabled[/]"); |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | if (settings.OverrideKicktipp) |
| | | 50 | | { |
| | 1 | 51 | | _console.MarkupLine("[yellow]Override mode enabled - will override existing Kicktipp predictions[/]"); |
| | | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | if (settings.OverrideDatabase) |
| | | 55 | | { |
| | 1 | 56 | | _console.MarkupLine("[yellow]Override database mode enabled - will override existing database prediction |
| | | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | if (settings.Agent) |
| | | 60 | | { |
| | 1 | 61 | | _console.MarkupLine("[blue]Agent mode enabled - prediction details will be hidden[/]"); |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | if (settings.DryRun) |
| | | 65 | | { |
| | 1 | 66 | | _console.MarkupLine("[magenta]Dry run mode enabled - no changes will be made to database or Kicktipp[/]" |
| | | 67 | | } |
| | | 68 | | |
| | 1 | 69 | | if (!string.IsNullOrEmpty(settings.EstimatedCostsModel)) |
| | | 70 | | { |
| | 1 | 71 | | _console.MarkupLine($"[cyan]Estimated costs will be calculated for model:[/] [yellow]{settings.Estimated |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | // Validate reprediction settings |
| | 1 | 75 | | if (settings.OverrideDatabase && settings.IsRepredictMode) |
| | | 76 | | { |
| | 1 | 77 | | _console.MarkupLine($"[red]Error:[/] --override-database cannot be used with reprediction flags (--repre |
| | 1 | 78 | | return 1; |
| | | 79 | | } |
| | | 80 | | |
| | 1 | 81 | | if (settings.MaxRepredictions.HasValue && settings.MaxRepredictions.Value < 0) |
| | | 82 | | { |
| | 1 | 83 | | _console.MarkupLine($"[red]Error:[/] --max-repredictions must be 0 or greater"); |
| | 1 | 84 | | return 1; |
| | | 85 | | } |
| | | 86 | | |
| | 1 | 87 | | if (settings.IsRepredictMode) |
| | | 88 | | { |
| | 1 | 89 | | var maxValue = settings.MaxRepredictions ?? int.MaxValue; |
| | 1 | 90 | | _console.MarkupLine($"[yellow]Reprediction mode enabled - max repredictions: {(settings.MaxRepredictions |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Execute the bonus prediction workflow |
| | 1 | 94 | | await ExecuteBonusWorkflow(settings); |
| | | 95 | | |
| | 1 | 96 | | return 0; |
| | | 97 | | } |
| | 1 | 98 | | catch (Exception ex) |
| | | 99 | | { |
| | 1 | 100 | | _logger.LogError(ex, "Error executing bonus command"); |
| | 1 | 101 | | _console.MarkupLine($"[red]Error:[/] {ex.Message}"); |
| | 1 | 102 | | return 1; |
| | | 103 | | } |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | private async Task ExecuteBonusWorkflow(BaseSettings settings) |
| | | 107 | | { |
| | | 108 | | // Create services using factories |
| | 1 | 109 | | var kicktippClient = _kicktippClientFactory.CreateClient(); |
| | 1 | 110 | | var predictionService = _openAiServiceFactory.CreatePredictionService(settings.Model); |
| | | 111 | | |
| | | 112 | | // Log the prompt paths being used |
| | 1 | 113 | | if (settings.Verbose) |
| | | 114 | | { |
| | 1 | 115 | | _console.MarkupLine($"[dim]Bonus prompt:[/] [blue]{predictionService.GetBonusPromptPath()}[/]"); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // Create KPI Context Provider for bonus predictions using factory |
| | 1 | 119 | | var kpiContextProvider = _contextProviderFactory.CreateKpiContextProvider(); |
| | | 120 | | |
| | 1 | 121 | | var tokenUsageTracker = _openAiServiceFactory.GetTokenUsageTracker(); |
| | | 122 | | |
| | | 123 | | // Create prediction repository |
| | 1 | 124 | | var predictionRepository = _firebaseServiceFactory.CreatePredictionRepository(); |
| | 1 | 125 | | var databaseEnabled = true; |
| | | 126 | | |
| | | 127 | | // Reset token usage tracker for this workflow |
| | 1 | 128 | | tokenUsageTracker.Reset(); |
| | | 129 | | |
| | | 130 | | // Determine community context (use explicit setting or fall back to community name) |
| | 1 | 131 | | string communityContext = settings.CommunityContext ?? settings.Community; |
| | | 132 | | |
| | 1 | 133 | | _console.MarkupLine($"[blue]Using community:[/] [yellow]{settings.Community}[/]"); |
| | 1 | 134 | | _console.MarkupLine($"[blue]Using community context:[/] [yellow]{communityContext}[/]"); |
| | 1 | 135 | | _console.MarkupLine("[blue]Getting open bonus questions from Kicktipp...[/]"); |
| | | 136 | | |
| | | 137 | | // Step 1: Get open bonus questions from Kicktipp |
| | 1 | 138 | | var bonusQuestions = await kicktippClient.GetOpenBonusQuestionsAsync(settings.Community); |
| | | 139 | | |
| | 1 | 140 | | if (!bonusQuestions.Any()) |
| | | 141 | | { |
| | 1 | 142 | | _console.MarkupLine("[yellow]No open bonus questions found[/]"); |
| | 1 | 143 | | return; |
| | | 144 | | } |
| | | 145 | | |
| | 1 | 146 | | _console.MarkupLine($"[green]Found {bonusQuestions.Count} open bonus questions[/]"); |
| | | 147 | | |
| | 1 | 148 | | if (databaseEnabled) |
| | | 149 | | { |
| | 1 | 150 | | _console.MarkupLine("[blue]Database enabled - checking for existing predictions...[/]"); |
| | | 151 | | } |
| | | 152 | | |
| | 1 | 153 | | var predictions = new Dictionary<string, BonusPrediction>(); |
| | | 154 | | |
| | | 155 | | // Step 2: For each question, check database first, then predict if needed |
| | 1 | 156 | | foreach (var question in bonusQuestions) |
| | | 157 | | { |
| | 1 | 158 | | _console.MarkupLine($"[cyan]Processing:[/] {Markup.Escape(question.Text)}"); |
| | | 159 | | |
| | | 160 | | try |
| | | 161 | | { |
| | 1 | 162 | | BonusPrediction? prediction = null; |
| | 1 | 163 | | bool fromDatabase = false; |
| | 1 | 164 | | bool shouldPredict = false; |
| | | 165 | | |
| | | 166 | | // Check if we have an existing prediction in the database |
| | 1 | 167 | | if (databaseEnabled && !settings.OverrideDatabase && !settings.IsRepredictMode) |
| | | 168 | | { |
| | | 169 | | // Look for prediction by question text, model, and community context |
| | 1 | 170 | | prediction = await predictionRepository!.GetBonusPredictionByTextAsync(question.Text, settings.Model |
| | 1 | 171 | | if (prediction != null) |
| | | 172 | | { |
| | 1 | 173 | | fromDatabase = true; |
| | 1 | 174 | | if (settings.Agent) |
| | | 175 | | { |
| | 1 | 176 | | _console.MarkupLine($"[green] ✓ Found existing prediction[/] [dim](from database)[/]"); |
| | | 177 | | } |
| | | 178 | | else |
| | | 179 | | { |
| | 1 | 180 | | var optionTexts = question.Options |
| | 1 | 181 | | .Where(o => prediction.SelectedOptionIds.Contains(o.Id)) |
| | 1 | 182 | | .Select(o => o.Text); |
| | 1 | 183 | | _console.MarkupLine($"[green] ✓ Found existing prediction:[/] {string.Join(", ", optionText |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | // Handle reprediction logic |
| | 1 | 189 | | if (settings.IsRepredictMode && databaseEnabled) |
| | | 190 | | { |
| | 1 | 191 | | var currentRepredictionIndex = await predictionRepository!.GetBonusRepredictionIndexAsync(question.T |
| | | 192 | | |
| | 1 | 193 | | if (currentRepredictionIndex == -1) |
| | | 194 | | { |
| | | 195 | | // No prediction exists yet - create first prediction |
| | 1 | 196 | | shouldPredict = true; |
| | 1 | 197 | | _console.MarkupLine($"[yellow] → No existing prediction found, creating first prediction...[/]" |
| | | 198 | | } |
| | | 199 | | else |
| | | 200 | | { |
| | | 201 | | // Check if we can create another reprediction |
| | 1 | 202 | | var maxAllowed = settings.MaxRepredictions ?? int.MaxValue; |
| | 1 | 203 | | var nextIndex = currentRepredictionIndex + 1; |
| | | 204 | | |
| | 1 | 205 | | if (nextIndex <= maxAllowed) |
| | | 206 | | { |
| | 1 | 207 | | shouldPredict = true; |
| | 1 | 208 | | _console.MarkupLine($"[yellow] → Creating reprediction {nextIndex} (current: {currentRepred |
| | | 209 | | } |
| | | 210 | | else |
| | | 211 | | { |
| | 1 | 212 | | _console.MarkupLine($"[yellow] ✗ Skipped - already at max repredictions ({currentRepredicti |
| | | 213 | | |
| | | 214 | | // Get the latest prediction for display purposes |
| | 1 | 215 | | prediction = await predictionRepository!.GetBonusPredictionByTextAsync(question.Text, settin |
| | 1 | 216 | | if (prediction != null) |
| | | 217 | | { |
| | 1 | 218 | | fromDatabase = true; |
| | 1 | 219 | | if (!settings.Agent) |
| | | 220 | | { |
| | 1 | 221 | | var optionTexts = question.Options |
| | 1 | 222 | | .Where(o => prediction.SelectedOptionIds.Contains(o.Id)) |
| | 1 | 223 | | .Select(o => o.Text); |
| | 1 | 224 | | _console.MarkupLine($"[green] ✓ Latest prediction:[/] {string.Join(", ", optionText |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | } |
| | | 228 | | } |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | // If no existing prediction (normal mode) or we need to predict (reprediction mode), generate a new one |
| | 1 | 232 | | if (prediction == null || shouldPredict) |
| | | 233 | | { |
| | 1 | 234 | | _console.MarkupLine($"[yellow] → Generating new prediction...[/]"); |
| | | 235 | | |
| | | 236 | | // Step 3: Get KPI context for bonus predictions |
| | 1 | 237 | | var contextDocuments = new List<DocumentContext>(); |
| | | 238 | | |
| | | 239 | | // Use KPI documents as context for bonus predictions (targeted by question content) |
| | 1 | 240 | | await foreach (var context in kpiContextProvider.GetBonusQuestionContextAsync(question.Text, communi |
| | | 241 | | { |
| | 1 | 242 | | contextDocuments.Add(context); |
| | | 243 | | } |
| | | 244 | | |
| | 1 | 245 | | if (settings.Verbose) |
| | | 246 | | { |
| | 1 | 247 | | _console.MarkupLine($"[dim] Using {contextDocuments.Count} KPI context documents[/]"); |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | // Predict the bonus question |
| | 1 | 251 | | prediction = await predictionService.PredictBonusQuestionAsync(question, contextDocuments); |
| | | 252 | | |
| | 1 | 253 | | if (prediction != null) |
| | | 254 | | { |
| | 1 | 255 | | if (settings.Agent) |
| | | 256 | | { |
| | 1 | 257 | | _console.MarkupLine($"[green] ✓ Generated prediction[/]"); |
| | | 258 | | } |
| | | 259 | | else |
| | | 260 | | { |
| | 1 | 261 | | var optionTexts = question.Options |
| | 1 | 262 | | .Where(o => prediction.SelectedOptionIds.Contains(o.Id)) |
| | 1 | 263 | | .Select(o => o.Text); |
| | 1 | 264 | | _console.MarkupLine($"[green] ✓ Generated prediction:[/] {string.Join(", ", optionTexts)}") |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | // Save to database immediately if enabled |
| | 1 | 268 | | if (databaseEnabled && !settings.DryRun) |
| | | 269 | | { |
| | | 270 | | try |
| | | 271 | | { |
| | | 272 | | // Get token usage and cost information |
| | 1 | 273 | | var cost = (double)tokenUsageTracker.GetLastCost(); // Get the cost for this individual |
| | | 274 | | // Use the new GetLastUsageJson method to get full JSON |
| | 1 | 275 | | var tokenUsageJson = tokenUsageTracker.GetLastUsageJson() ?? "{}"; |
| | | 276 | | |
| | 1 | 277 | | if (settings.IsRepredictMode) |
| | | 278 | | { |
| | | 279 | | // Save as reprediction with specific index |
| | 1 | 280 | | var currentIndex = await predictionRepository!.GetBonusRepredictionIndexAsync(questi |
| | 1 | 281 | | var nextIndex = currentIndex == -1 ? 0 : currentIndex + 1; |
| | | 282 | | |
| | 1 | 283 | | await predictionRepository!.SaveBonusRepredictionAsync( |
| | 1 | 284 | | question, |
| | 1 | 285 | | prediction, |
| | 1 | 286 | | settings.Model, |
| | 1 | 287 | | tokenUsageJson, |
| | 1 | 288 | | cost, |
| | 1 | 289 | | communityContext, |
| | 1 | 290 | | contextDocuments.Select(d => d.Name), |
| | 1 | 291 | | nextIndex); |
| | | 292 | | |
| | 1 | 293 | | if (settings.Verbose) |
| | | 294 | | { |
| | 1 | 295 | | _console.MarkupLine($"[dim] ✓ Saved as reprediction {nextIndex} to database[/ |
| | | 296 | | } |
| | | 297 | | } |
| | | 298 | | else |
| | | 299 | | { |
| | | 300 | | // Save normally (override or new prediction) |
| | 1 | 301 | | await predictionRepository!.SaveBonusPredictionAsync( |
| | 1 | 302 | | question, |
| | 1 | 303 | | prediction, |
| | 1 | 304 | | settings.Model, |
| | 1 | 305 | | tokenUsageJson, |
| | 1 | 306 | | cost, |
| | 1 | 307 | | communityContext, |
| | 1 | 308 | | contextDocuments.Select(d => d.Name), |
| | 1 | 309 | | overrideCreatedAt: settings.OverrideDatabase); |
| | | 310 | | |
| | 1 | 311 | | if (settings.Verbose) |
| | | 312 | | { |
| | 1 | 313 | | _console.MarkupLine($"[dim] ✓ Saved to database[/]"); |
| | | 314 | | } |
| | | 315 | | } |
| | 1 | 316 | | } |
| | 1 | 317 | | catch (Exception ex) |
| | | 318 | | { |
| | 1 | 319 | | _logger.LogError(ex, "Failed to save bonus prediction for question '{QuestionText}'", qu |
| | 1 | 320 | | _console.MarkupLine($"[red] ✗ Failed to save to database: {ex.Message}[/]"); |
| | 1 | 321 | | } |
| | | 322 | | } |
| | 1 | 323 | | else if (databaseEnabled && settings.DryRun && settings.Verbose) |
| | | 324 | | { |
| | 1 | 325 | | _console.MarkupLine($"[dim] (Dry run - skipped database save)[/]"); |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | // Show individual question token usage in verbose mode |
| | 1 | 329 | | if (settings.Verbose) |
| | | 330 | | { |
| | 1 | 331 | | var questionUsage = !string.IsNullOrEmpty(settings.EstimatedCostsModel) |
| | 1 | 332 | | ? tokenUsageTracker.GetLastUsageCompactSummaryWithEstimatedCosts(settings.EstimatedCosts |
| | 1 | 333 | | : tokenUsageTracker.GetLastUsageCompactSummary(); |
| | 1 | 334 | | _console.MarkupLine($"[dim] Token usage: {questionUsage}[/]"); |
| | | 335 | | } |
| | | 336 | | } |
| | | 337 | | else |
| | | 338 | | { |
| | 1 | 339 | | _console.MarkupLine($"[red] ✗ Failed to generate prediction[/]"); |
| | 1 | 340 | | continue; |
| | | 341 | | } |
| | 1 | 342 | | } |
| | | 343 | | |
| | 1 | 344 | | predictions[question.FormFieldName ?? question.Text] = prediction; |
| | | 345 | | |
| | 1 | 346 | | if (!fromDatabase && settings.Verbose) |
| | | 347 | | { |
| | 1 | 348 | | _console.MarkupLine($"[dim] Ready for Kicktipp placement[/]"); |
| | | 349 | | } |
| | 1 | 350 | | } |
| | 1 | 351 | | catch (Exception ex) |
| | | 352 | | { |
| | 1 | 353 | | _logger.LogError(ex, "Error processing bonus question '{QuestionText}'", question.Text); |
| | 1 | 354 | | _console.MarkupLine($"[red] ✗ Error processing question: {ex.Message}[/]"); |
| | 1 | 355 | | } |
| | 1 | 356 | | } |
| | | 357 | | |
| | 1 | 358 | | if (!predictions.Any()) |
| | | 359 | | { |
| | 1 | 360 | | _console.MarkupLine("[yellow]No predictions available, nothing to place[/]"); |
| | 1 | 361 | | return; |
| | | 362 | | } |
| | | 363 | | |
| | | 364 | | // Step 4: Place all predictions using PlaceBonusPredictionsAsync |
| | 1 | 365 | | _console.MarkupLine($"[blue]Placing {predictions.Count} bonus predictions to Kicktipp...[/]"); |
| | | 366 | | |
| | 1 | 367 | | if (settings.DryRun) |
| | | 368 | | { |
| | 1 | 369 | | _console.MarkupLine($"[magenta]✓ Dry run mode - would have placed {predictions.Count} bonus predictions (no |
| | | 370 | | } |
| | | 371 | | else |
| | | 372 | | { |
| | 1 | 373 | | var success = await kicktippClient.PlaceBonusPredictionsAsync(settings.Community, predictions, overridePredi |
| | | 374 | | |
| | 1 | 375 | | if (success) |
| | | 376 | | { |
| | 1 | 377 | | _console.MarkupLine($"[green]✓ Successfully placed all {predictions.Count} bonus predictions![/]"); |
| | | 378 | | } |
| | | 379 | | else |
| | | 380 | | { |
| | 1 | 381 | | _console.MarkupLine("[red]✗ Failed to place some or all bonus predictions[/]"); |
| | | 382 | | } |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | // Display token usage summary |
| | 1 | 386 | | var summary = !string.IsNullOrEmpty(settings.EstimatedCostsModel) |
| | 1 | 387 | | ? tokenUsageTracker.GetCompactSummaryWithEstimatedCosts(settings.EstimatedCostsModel) |
| | 1 | 388 | | : tokenUsageTracker.GetCompactSummary(); |
| | 1 | 389 | | _console.MarkupLine($"[dim]Token usage (uncached/cached/reasoning/output/$cost): {summary}[/]"); |
| | 1 | 390 | | } |
| | | 391 | | } |