| | | 1 | | using System.Net.Http.Json; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Posts scores to the Langfuse public Scores API (<c>POST /api/public/scores</c>) using HTTP |
| | | 8 | | /// Basic authentication. This is the low-level transport; mapping and failure handling live in |
| | | 9 | | /// <see cref="LangfuseScoreRecorder"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// A score may be ingested before its trace exists; Langfuse links the two by trace id once the |
| | | 13 | | /// trace is received. The underlying <see cref="HttpClient"/> is owned by the caller and disposed |
| | | 14 | | /// with it. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal sealed class LangfuseScoreApiClient |
| | | 17 | | { |
| | 1 | 18 | | private static readonly JsonSerializerOptions SerializerOptions = new() |
| | 1 | 19 | | { |
| | 1 | 20 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 1 | 21 | | DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull, |
| | 1 | 22 | | }; |
| | | 23 | | |
| | | 24 | | private readonly HttpClient _httpClient; |
| | | 25 | | private readonly Uri _scoresEndpoint; |
| | | 26 | | |
| | 19 | 27 | | public LangfuseScoreApiClient(HttpClient httpClient, Uri scoresEndpoint, string authorizationHeaderValue) |
| | | 28 | | { |
| | 19 | 29 | | ArgumentNullException.ThrowIfNull(httpClient); |
| | 19 | 30 | | ArgumentNullException.ThrowIfNull(scoresEndpoint); |
| | 19 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace(authorizationHeaderValue); |
| | | 32 | | |
| | 19 | 33 | | _httpClient = httpClient; |
| | 19 | 34 | | _scoresEndpoint = scoresEndpoint; |
| | | 35 | | |
| | 19 | 36 | | if (_httpClient.DefaultRequestHeaders.Authorization is null) |
| | | 37 | | { |
| | 19 | 38 | | var space = authorizationHeaderValue.IndexOf(' '); |
| | 19 | 39 | | _httpClient.DefaultRequestHeaders.Authorization = space > 0 |
| | 19 | 40 | | ? new System.Net.Http.Headers.AuthenticationHeaderValue( |
| | 19 | 41 | | authorizationHeaderValue[..space], |
| | 19 | 42 | | authorizationHeaderValue[(space + 1)..]) |
| | 19 | 43 | | : new System.Net.Http.Headers.AuthenticationHeaderValue(authorizationHeaderValue); |
| | | 44 | | } |
| | 19 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Sends a single score to Langfuse. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="score">The score to ingest.</param> |
| | | 51 | | /// <param name="cancellationToken">A cancellation token.</param> |
| | | 52 | | /// <exception cref="LangfuseException">The request failed or returned a non-success status.</exception> |
| | | 53 | | public async Task CreateAsync(LangfuseScore score, CancellationToken cancellationToken) |
| | | 54 | | { |
| | 14 | 55 | | ArgumentNullException.ThrowIfNull(score); |
| | | 56 | | |
| | | 57 | | HttpResponseMessage response; |
| | | 58 | | try |
| | | 59 | | { |
| | 14 | 60 | | response = await _httpClient |
| | 14 | 61 | | .PostAsJsonAsync(_scoresEndpoint, score, SerializerOptions, cancellationToken) |
| | 14 | 62 | | .ConfigureAwait(false); |
| | 14 | 63 | | } |
| | 0 | 64 | | catch (Exception ex) when (ex is HttpRequestException or TaskCanceledException) |
| | | 65 | | { |
| | 0 | 66 | | throw new LangfuseException( |
| | 0 | 67 | | $"Failed to send score '{score.Name}' to Langfuse at '{_scoresEndpoint}'.", ex); |
| | | 68 | | } |
| | | 69 | | |
| | 14 | 70 | | using (response) |
| | | 71 | | { |
| | 14 | 72 | | if (response.IsSuccessStatusCode) |
| | | 73 | | { |
| | 11 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 3 | 77 | | var body = await response.Content |
| | 3 | 78 | | .ReadAsStringAsync(cancellationToken) |
| | 3 | 79 | | .ConfigureAwait(false); |
| | | 80 | | |
| | 3 | 81 | | throw new LangfuseException( |
| | 3 | 82 | | $"Langfuse rejected score '{score.Name}' with status {(int)response.StatusCode} " + |
| | 3 | 83 | | $"({response.ReasonPhrase}): {body}"); |
| | | 84 | | } |
| | 11 | 85 | | } |
| | | 86 | | } |