| | | 1 | | namespace NexusLabs.Needlr.AgentFramework.Langfuse; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Default <see cref="ILangfuseDatasetClient"/> backed by the shared <see cref="LangfuseApiClient"/>. |
| | | 5 | | /// </summary> |
| | | 6 | | internal sealed class LangfuseDatasetClient : ILangfuseDatasetClient |
| | | 7 | | { |
| | | 8 | | private readonly LangfuseApiClient _apiClient; |
| | | 9 | | |
| | 4 | 10 | | public LangfuseDatasetClient(LangfuseApiClient apiClient) |
| | | 11 | | { |
| | 4 | 12 | | ArgumentNullException.ThrowIfNull(apiClient); |
| | | 13 | | |
| | 4 | 14 | | _apiClient = apiClient; |
| | 4 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 0 | 18 | | public bool IsEnabled => true; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | public async Task EnsureDatasetAsync(string name, string? description = null, CancellationToken cancellationToken = |
| | | 22 | | { |
| | 2 | 23 | | ArgumentException.ThrowIfNullOrWhiteSpace(name); |
| | | 24 | | |
| | 2 | 25 | | var existing = await _apiClient |
| | 2 | 26 | | .GetOrDefaultAsync<LangfuseDatasetRef>( |
| | 2 | 27 | | $"api/public/v2/datasets/{Uri.EscapeDataString(name)}", |
| | 2 | 28 | | cancellationToken) |
| | 2 | 29 | | .ConfigureAwait(false); |
| | | 30 | | |
| | 2 | 31 | | if (existing is not null) |
| | | 32 | | { |
| | 1 | 33 | | return; |
| | | 34 | | } |
| | | 35 | | |
| | 1 | 36 | | await _apiClient |
| | 1 | 37 | | .PostAsync( |
| | 1 | 38 | | "api/public/v2/datasets", |
| | 1 | 39 | | new LangfuseCreateDatasetRequest { Name = name, Description = description }, |
| | 1 | 40 | | cancellationToken) |
| | 1 | 41 | | .ConfigureAwait(false); |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public Task UpsertItemAsync(LangfuseDatasetItem item, CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 1 | 47 | | ArgumentNullException.ThrowIfNull(item); |
| | 1 | 48 | | ArgumentException.ThrowIfNullOrWhiteSpace(item.DatasetName); |
| | | 49 | | |
| | 1 | 50 | | return _apiClient.PostAsync( |
| | 1 | 51 | | "api/public/dataset-items", |
| | 1 | 52 | | LangfuseCreateDatasetItemRequest.From(item), |
| | 1 | 53 | | cancellationToken); |
| | | 54 | | } |
| | | 55 | | } |