feat: meter AI usage
This commit is contained in:
@@ -133,7 +133,8 @@ public sealed class AiWorkspaceService : IAiWorkspaceService
|
||||
_ => throw new ArgumentException($"Unknown AI module '{module}'."),
|
||||
};
|
||||
|
||||
var result = await _ai.SummarizeSectionAsync($"{instruction} {Guardrail}", source, max, 120);
|
||||
var prompt = $"{instruction} {Guardrail}";
|
||||
var result = await _ai.SummarizeSectionAsync(prompt, source, max, 120);
|
||||
if (string.IsNullOrWhiteSpace(result))
|
||||
{
|
||||
throw new AiUnavailableException("The AI service could not generate this right now. Please try again in a moment.");
|
||||
@@ -148,6 +149,9 @@ public sealed class AiWorkspaceService : IAiWorkspaceService
|
||||
Title = title,
|
||||
Provider = string.IsNullOrWhiteSpace(provider) ? "ai-service" : provider,
|
||||
ResultJson = JsonSerializer.Serialize(new { text = result.Trim() }, Json),
|
||||
InputCharacterCount = prompt.Length + source.Length,
|
||||
OutputCharacterCount = result.Trim().Length,
|
||||
EstimatedTokenCount = EstimateTokens(prompt.Length + source.Length + result.Trim().Length),
|
||||
CreatedAtUtc = DateTimeOffset.UtcNow,
|
||||
};
|
||||
_db.AiInteractions.Add(interaction);
|
||||
@@ -174,6 +178,8 @@ public sealed class AiWorkspaceService : IAiWorkspaceService
|
||||
return true;
|
||||
}
|
||||
|
||||
internal static int EstimateTokens(int characterCount) => Math.Max(0, (characterCount + 3) / 4);
|
||||
|
||||
private static string? NormalizeMode(string module, string? mode)
|
||||
{
|
||||
if (module != "cover-letter") return null;
|
||||
|
||||
Reference in New Issue
Block a user