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