| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using EHonda.KicktippAi.Core; |
| | | 4 | | using Spectre.Console; |
| | | 5 | | |
| | | 6 | | namespace Orchestrator.Commands.Shared; |
| | | 7 | | |
| | | 8 | | internal sealed class JustificationConsoleWriter |
| | | 9 | | { |
| | | 10 | | private readonly IAnsiConsole _console; |
| | | 11 | | |
| | 1 | 12 | | public JustificationConsoleWriter(IAnsiConsole console) |
| | | 13 | | { |
| | 1 | 14 | | _console = console; |
| | 1 | 15 | | } |
| | | 16 | | |
| | | 17 | | public void WriteJustification( |
| | | 18 | | PredictionJustification? justification, |
| | | 19 | | string headingMarkup, |
| | | 20 | | string indent, |
| | | 21 | | string fallbackMarkup) |
| | | 22 | | { |
| | 1 | 23 | | if (justification == null || !HasContent(justification)) |
| | | 24 | | { |
| | 1 | 25 | | _console.MarkupLine(fallbackMarkup); |
| | 1 | 26 | | return; |
| | | 27 | | } |
| | | 28 | | |
| | 1 | 29 | | _console.MarkupLine(headingMarkup); |
| | | 30 | | |
| | 1 | 31 | | if (!string.IsNullOrWhiteSpace(justification.KeyReasoning)) |
| | | 32 | | { |
| | 1 | 33 | | _console.MarkupLine($"{indent}[white]Key reasoning:[/] {Markup.Escape(justification.KeyReasoning.Trim())}"); |
| | | 34 | | } |
| | | 35 | | |
| | 1 | 36 | | WriteSources("Most valuable context sources", justification.ContextSources?.MostValuable, indent); |
| | 1 | 37 | | WriteSources("Least valuable context sources", justification.ContextSources?.LeastValuable, indent); |
| | 1 | 38 | | WriteUncertainties(justification.Uncertainties, indent); |
| | 1 | 39 | | } |
| | | 40 | | |
| | | 41 | | private static bool HasContent(PredictionJustification justification) |
| | | 42 | | { |
| | 1 | 43 | | if (!string.IsNullOrWhiteSpace(justification.KeyReasoning)) |
| | | 44 | | { |
| | 1 | 45 | | return true; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | if (justification.ContextSources?.MostValuable != null && |
| | 0 | 49 | | justification.ContextSources.MostValuable.Any(HasSourceContent)) |
| | | 50 | | { |
| | 0 | 51 | | return true; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | if (justification.ContextSources?.LeastValuable != null && |
| | 0 | 55 | | justification.ContextSources.LeastValuable.Any(HasSourceContent)) |
| | | 56 | | { |
| | 0 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | return justification.Uncertainties != null && |
| | 0 | 61 | | justification.Uncertainties.Any(item => !string.IsNullOrWhiteSpace(item)); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | private static bool HasSourceContent(PredictionJustificationContextSource source) |
| | | 65 | | { |
| | 0 | 66 | | return !string.IsNullOrWhiteSpace(source?.DocumentName) || |
| | 0 | 67 | | !string.IsNullOrWhiteSpace(source?.Details); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private void WriteSources( |
| | | 71 | | string heading, |
| | | 72 | | IReadOnlyList<PredictionJustificationContextSource>? sources, |
| | | 73 | | string indent) |
| | | 74 | | { |
| | 1 | 75 | | if (sources == null) |
| | | 76 | | { |
| | 0 | 77 | | return; |
| | | 78 | | } |
| | | 79 | | |
| | 1 | 80 | | var entries = sources |
| | 1 | 81 | | .Where(HasSourceContent) |
| | 1 | 82 | | .ToList(); |
| | | 83 | | |
| | 1 | 84 | | if (entries.Count == 0) |
| | | 85 | | { |
| | 1 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | _console.MarkupLine($"{indent}[white]{heading}[/]"); |
| | | 90 | | |
| | 0 | 91 | | foreach (var entry in entries) |
| | | 92 | | { |
| | 0 | 93 | | var documentName = string.IsNullOrWhiteSpace(entry.DocumentName) |
| | 0 | 94 | | ? "Unnamed document" |
| | 0 | 95 | | : entry.DocumentName.Trim(); |
| | | 96 | | |
| | 0 | 97 | | var details = string.IsNullOrWhiteSpace(entry.Details) |
| | 0 | 98 | | ? "No details provided" |
| | 0 | 99 | | : entry.Details.Trim(); |
| | | 100 | | |
| | 0 | 101 | | _console.MarkupLine($"{indent} • [yellow]{Markup.Escape(documentName)}[/]: {Markup.Escape(details)}"); |
| | | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | private void WriteUncertainties(IReadOnlyList<string>? uncertainties, string indent) |
| | | 106 | | { |
| | 1 | 107 | | if (uncertainties == null) |
| | | 108 | | { |
| | 0 | 109 | | return; |
| | | 110 | | } |
| | | 111 | | |
| | 1 | 112 | | var items = uncertainties |
| | 0 | 113 | | .Where(item => !string.IsNullOrWhiteSpace(item)) |
| | 0 | 114 | | .Select(item => item.Trim()) |
| | 1 | 115 | | .ToList(); |
| | | 116 | | |
| | 1 | 117 | | if (items.Count == 0) |
| | | 118 | | { |
| | 1 | 119 | | return; |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | _console.MarkupLine($"{indent}[white]Uncertainties:[/]"); |
| | | 123 | | |
| | 0 | 124 | | foreach (var item in items) |
| | | 125 | | { |
| | 0 | 126 | | _console.MarkupLine($"{indent} • {Markup.Escape(item)}"); |
| | | 127 | | } |
| | 0 | 128 | | } |
| | | 129 | | } |