| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Service for AI Summary API operations. |
| | | 8 | | /// Uses HttpClientExtensions for consistent error handling and logging. |
| | | 9 | | /// </summary> |
| | | 10 | | public class AISummaryApiService : IAISummaryApiService |
| | | 11 | | { |
| | | 12 | | private readonly HttpClient _http; |
| | | 13 | | private readonly ILogger<AISummaryApiService> _logger; |
| | | 14 | | |
| | 0 | 15 | | public AISummaryApiService(HttpClient http, ILogger<AISummaryApiService> logger) |
| | | 16 | | { |
| | 0 | 17 | | _http = http; |
| | 0 | 18 | | _logger = logger; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | #region Templates |
| | | 22 | | |
| | | 23 | | public async Task<List<SummaryTemplateDto>> GetTemplatesAsync() |
| | | 24 | | { |
| | 0 | 25 | | var result = await _http.GetEntityAsync<List<SummaryTemplateDto>>( |
| | 0 | 26 | | "summary/templates", |
| | 0 | 27 | | _logger, |
| | 0 | 28 | | "summary templates"); |
| | 0 | 29 | | return result ?? new List<SummaryTemplateDto>(); |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | #endregion |
| | | 33 | | |
| | | 34 | | #region Article Summary (existing) |
| | | 35 | | |
| | | 36 | | public async Task<SummaryEstimateDto?> GetEstimateAsync(Guid articleId) |
| | | 37 | | { |
| | 0 | 38 | | return await _http.GetEntityAsync<SummaryEstimateDto>( |
| | 0 | 39 | | $"articles/{articleId}/summary/estimate", |
| | 0 | 40 | | _logger, |
| | 0 | 41 | | $"summary estimate for article {articleId}"); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async Task<SummaryGenerationDto?> GenerateSummaryAsync(Guid articleId, GenerateSummaryRequestDto? request = n |
| | | 45 | | { |
| | 0 | 46 | | request ??= new GenerateSummaryRequestDto(); |
| | | 47 | | |
| | 0 | 48 | | return await _http.PostEntityAsync<SummaryGenerationDto>( |
| | 0 | 49 | | $"articles/{articleId}/summary/generate", |
| | 0 | 50 | | request, |
| | 0 | 51 | | _logger, |
| | 0 | 52 | | $"summary generation for article {articleId}"); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public async Task<ArticleSummaryDto?> GetSummaryAsync(Guid articleId) |
| | | 56 | | { |
| | 0 | 57 | | return await _http.GetEntityAsync<ArticleSummaryDto>( |
| | 0 | 58 | | $"articles/{articleId}/summary", |
| | 0 | 59 | | _logger, |
| | 0 | 60 | | $"summary for article {articleId}"); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | public async Task<SummaryPreviewDto?> GetSummaryPreviewAsync(Guid articleId) |
| | | 64 | | { |
| | | 65 | | try |
| | | 66 | | { |
| | 0 | 67 | | var response = await _http.GetAsync($"articles/{articleId}/summary/preview"); |
| | | 68 | | |
| | 0 | 69 | | if (response.StatusCode == System.Net.HttpStatusCode.NoContent || |
| | 0 | 70 | | response.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | | 71 | | { |
| | 0 | 72 | | return null; |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | if (response.IsSuccessStatusCode) |
| | | 76 | | { |
| | 0 | 77 | | return await response.Content.ReadFromJsonAsync<SummaryPreviewDto>(); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | return null; |
| | | 81 | | } |
| | 0 | 82 | | catch (Exception ex) |
| | | 83 | | { |
| | 0 | 84 | | _logger.LogError(ex, "Error getting summary preview for article {ArticleId}", articleId); |
| | 0 | 85 | | return null; |
| | | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | | 89 | | public async Task<bool> ClearSummaryAsync(Guid articleId) |
| | | 90 | | { |
| | 0 | 91 | | return await _http.DeleteEntityAsync( |
| | 0 | 92 | | $"articles/{articleId}/summary", |
| | 0 | 93 | | _logger, |
| | 0 | 94 | | $"summary for article {articleId}"); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | #endregion |
| | | 98 | | |
| | | 99 | | #region Entity Summary (Campaign, Arc) |
| | | 100 | | |
| | | 101 | | public async Task<EntitySummaryDto?> GetEntitySummaryAsync(string entityType, Guid entityId) |
| | | 102 | | { |
| | 0 | 103 | | var route = GetEntityRoute(entityType); |
| | 0 | 104 | | return await _http.GetEntityAsync<EntitySummaryDto>( |
| | 0 | 105 | | $"{route}/{entityId}/summary", |
| | 0 | 106 | | _logger, |
| | 0 | 107 | | $"summary for {entityType} {entityId}"); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task<SummaryEstimateDto?> GetEntityEstimateAsync(string entityType, Guid entityId) |
| | | 111 | | { |
| | 0 | 112 | | var route = GetEntityRoute(entityType); |
| | 0 | 113 | | return await _http.GetEntityAsync<SummaryEstimateDto>( |
| | 0 | 114 | | $"{route}/{entityId}/summary/estimate", |
| | 0 | 115 | | _logger, |
| | 0 | 116 | | $"summary estimate for {entityType} {entityId}"); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public async Task<SummaryGenerationDto?> GenerateEntitySummaryAsync(string entityType, Guid entityId, GenerateSummar |
| | | 120 | | { |
| | 0 | 121 | | request ??= new GenerateSummaryRequestDto(); |
| | 0 | 122 | | var route = GetEntityRoute(entityType); |
| | | 123 | | |
| | 0 | 124 | | return await _http.PostEntityAsync<SummaryGenerationDto>( |
| | 0 | 125 | | $"{route}/{entityId}/summary/generate", |
| | 0 | 126 | | request, |
| | 0 | 127 | | _logger, |
| | 0 | 128 | | $"summary generation for {entityType} {entityId}"); |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | public async Task<bool> ClearEntitySummaryAsync(string entityType, Guid entityId) |
| | | 132 | | { |
| | 0 | 133 | | var route = GetEntityRoute(entityType); |
| | 0 | 134 | | return await _http.DeleteEntityAsync( |
| | 0 | 135 | | $"{route}/{entityId}/summary", |
| | 0 | 136 | | _logger, |
| | 0 | 137 | | $"summary for {entityType} {entityId}"); |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | private static string GetEntityRoute(string entityType) |
| | | 141 | | { |
| | 0 | 142 | | return entityType.ToLowerInvariant() switch |
| | 0 | 143 | | { |
| | 0 | 144 | | "campaign" => "campaigns", |
| | 0 | 145 | | "arc" => "arcs", |
| | 0 | 146 | | _ => throw new ArgumentException($"Unknown entity type: {entityType}") |
| | 0 | 147 | | }; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | #endregion |
| | | 151 | | } |