| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | using Microsoft.Extensions.AI; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework.Evaluation; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Disk-backed <see cref="IEvaluationCaptureStore"/> that persists each response |
| | | 9 | | /// as a single JSON file under a caller-supplied directory. File names are the |
| | | 10 | | /// request hash plus a <c>.json</c> extension. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Writes are atomic per file via write-then-rename. Reads return <see langword="null"/> |
| | | 14 | | /// when no file exists for the requested key, and propagate any other I/O error. |
| | | 15 | | /// </remarks> |
| | | 16 | | public sealed class FileEvaluationCaptureStore : IEvaluationCaptureStore |
| | | 17 | | { |
| | 1 | 18 | | private static readonly JsonSerializerOptions _serializerOptions = new() |
| | 1 | 19 | | { |
| | 1 | 20 | | WriteIndented = true, |
| | 1 | 21 | | }; |
| | | 22 | | |
| | | 23 | | private readonly string _directoryPath; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Creates a store that reads from and writes to <paramref name="directoryPath"/>. |
| | | 27 | | /// The directory is created on first write if it does not already exist. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="directoryPath">Absolute or relative path to the cache directory.</param> |
| | 10 | 30 | | public FileEvaluationCaptureStore(string directoryPath) |
| | | 31 | | { |
| | 10 | 32 | | ArgumentException.ThrowIfNullOrWhiteSpace(directoryPath); |
| | 7 | 33 | | _directoryPath = directoryPath; |
| | 7 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public async Task<ChatResponse?> TryGetAsync( |
| | | 38 | | string key, |
| | | 39 | | CancellationToken cancellationToken) |
| | | 40 | | { |
| | 5 | 41 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | | 42 | | |
| | 3 | 43 | | var path = GetPath(key); |
| | 3 | 44 | | if (!File.Exists(path)) |
| | | 45 | | { |
| | 1 | 46 | | return null; |
| | | 47 | | } |
| | | 48 | | |
| | 2 | 49 | | await using var stream = File.OpenRead(path); |
| | 2 | 50 | | var payload = await JsonSerializer |
| | 2 | 51 | | .DeserializeAsync<CapturedChatResponsePayload>(stream, _serializerOptions, cancellationToken) |
| | 2 | 52 | | .ConfigureAwait(false); |
| | 2 | 53 | | return payload?.ToChatResponse(); |
| | 3 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async Task SaveAsync( |
| | | 58 | | string key, |
| | | 59 | | ChatResponse response, |
| | | 60 | | CancellationToken cancellationToken) |
| | | 61 | | { |
| | 5 | 62 | | ArgumentException.ThrowIfNullOrWhiteSpace(key); |
| | 5 | 63 | | ArgumentNullException.ThrowIfNull(response); |
| | | 64 | | |
| | 4 | 65 | | Directory.CreateDirectory(_directoryPath); |
| | 4 | 66 | | var finalPath = GetPath(key); |
| | 4 | 67 | | var tempPath = finalPath + ".tmp"; |
| | | 68 | | |
| | 4 | 69 | | var payload = CapturedChatResponsePayload.FromChatResponse(response); |
| | 4 | 70 | | await using (var stream = File.Create(tempPath)) |
| | | 71 | | { |
| | 4 | 72 | | await JsonSerializer |
| | 4 | 73 | | .SerializeAsync(stream, payload, _serializerOptions, cancellationToken) |
| | 4 | 74 | | .ConfigureAwait(false); |
| | | 75 | | } |
| | | 76 | | |
| | 4 | 77 | | if (File.Exists(finalPath)) |
| | | 78 | | { |
| | 1 | 79 | | File.Delete(finalPath); |
| | | 80 | | } |
| | 4 | 81 | | File.Move(tempPath, finalPath); |
| | 4 | 82 | | } |
| | | 83 | | |
| | 7 | 84 | | private string GetPath(string key) => Path.Combine(_directoryPath, key + ".json"); |
| | | 85 | | } |