namespace SOR.Ai.Training; using System.Text.Json; /// /// Writes TrainingRecords to a JSONL file (one JSON object per line). /// Intended for offline ML training pipelines — call WriteAsync in bulk, then FlushAsync. /// Thread-safe for sequential writes; do not call from concurrent tasks on the same instance. /// internal sealed class TrainingDataWriter : IAsyncDisposable { private readonly StreamWriter _writer; public TrainingDataWriter(string path) { _writer = new StreamWriter(path, append: true, encoding: System.Text.Encoding.UTF8); } public async Task WriteAsync(TrainingRecord record, CancellationToken ct = default) { var line = JsonSerializer.Serialize(record); await _writer.WriteLineAsync(line.AsMemory(), ct); } public async Task FlushAsync() => await _writer.FlushAsync(); public async ValueTask DisposeAsync() { await _writer.FlushAsync(); await _writer.DisposeAsync(); } }