Handle disconnected Gmail and bound CV rewrite prompts

This commit is contained in:
2026-04-09 22:07:36 +02:00
parent dd10b635e6
commit 269dcb3487
3 changed files with 64 additions and 2 deletions
+30 -2
View File
@@ -75,6 +75,7 @@ namespace JobTrackerApi.Services
public class SummarizerService : ISummarizerService
{
private const int AiSummarizeMaxInputChars = 20000;
private readonly IHttpClientFactory _httpFactory;
private readonly IMemoryCache _cache;
private readonly object _metricsLock = new();
@@ -119,10 +120,27 @@ namespace JobTrackerApi.Services
public Task<string?> SummarizeSectionAsync(string instruction, string text, int maxLength = 180, int minLength = 40)
{
if (string.IsNullOrWhiteSpace(instruction) || string.IsNullOrWhiteSpace(text)) return Task.FromResult<string?>(null);
var composed = $"{instruction.Trim()}\n\n{text.Trim()}";
var composed = ComposeBoundedPrompt(instruction.Trim(), text.Trim());
return SummarizeCoreAsync(composed, maxLength, minLength);
}
private static string ComposeBoundedPrompt(string instruction, string text)
{
var prefix = $"{instruction}\n\n";
if (prefix.Length >= AiSummarizeMaxInputChars)
{
return prefix[..AiSummarizeMaxInputChars];
}
var remaining = AiSummarizeMaxInputChars - prefix.Length;
if (text.Length <= remaining)
{
return prefix + text;
}
return prefix + text[..remaining];
}
private async Task<string?> SummarizeCoreAsync(string text, int maxLength, int minLength)
{
var key = BuildCacheKey(text, maxLength, minLength);
@@ -151,7 +169,17 @@ namespace JobTrackerApi.Services
var res = await client.PostAsync("/summarize", content);
sw.Stop();
Interlocked.Add(ref _totalLatencyTicks, sw.ElapsedTicks);
if (!res.IsSuccessStatusCode) return null;
if (!res.IsSuccessStatusCode)
{
var errorBody = await res.Content.ReadAsStringAsync();
Interlocked.Increment(ref _failures);
lock (_metricsLock)
{
_lastFailureAt = DateTimeOffset.UtcNow;
_lastError = $"AI summarize returned {(int)res.StatusCode}: {errorBody}";
}
return null;
}
using var stream = await res.Content.ReadAsStreamAsync();
using var doc = await JsonDocument.ParseAsync(stream);