| | | 1 | | using System.Text.Json; |
| | | 2 | | using Intentum.Persistence.Repositories; |
| | | 3 | | using Intentum.Runtime.Policy; |
| | | 4 | | |
| | | 5 | | namespace Intentum.Persistence.Serialization; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Shared JSON options and document type for intent history across MongoDB and Redis. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class IntentHistorySerialization |
| | | 11 | | { |
| | 1 | 12 | | public static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Document shape for intent history (MongoDB and Redis). |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class IntentHistoryDocument |
| | | 19 | | { |
| | | 20 | | public string Id { get; set; } = ""; |
| | | 21 | | public string BehaviorSpaceId { get; set; } = ""; |
| | | 22 | | public string? EntityId { get; set; } |
| | | 23 | | public string IntentName { get; set; } = ""; |
| | | 24 | | public string ConfidenceLevel { get; set; } = ""; |
| | | 25 | | public double ConfidenceScore { get; set; } |
| | | 26 | | public string Decision { get; set; } = ""; |
| | | 27 | | public DateTimeOffset RecordedAt { get; set; } |
| | | 28 | | public string MetadataJson { get; set; } = "{}"; |
| | | 29 | | |
| | | 30 | | public static IntentHistoryDocument From(IntentHistoryRecord record) |
| | | 31 | | { |
| | | 32 | | return new IntentHistoryDocument |
| | | 33 | | { |
| | | 34 | | Id = record.Id, |
| | | 35 | | BehaviorSpaceId = record.BehaviorSpaceId, |
| | | 36 | | EntityId = record.EntityId, |
| | | 37 | | IntentName = record.IntentName, |
| | | 38 | | ConfidenceLevel = record.ConfidenceLevel, |
| | | 39 | | ConfidenceScore = record.ConfidenceScore, |
| | | 40 | | Decision = record.Decision.ToString(), |
| | | 41 | | RecordedAt = record.RecordedAt, |
| | | 42 | | MetadataJson = record.Metadata != null ? JsonSerializer.Serialize(record.Metadata, IntentHistorySerializatio |
| | | 43 | | }; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public IntentHistoryRecord ToRecord() |
| | | 47 | | { |
| | | 48 | | var metadata = string.IsNullOrEmpty(MetadataJson) || MetadataJson == "{}" |
| | | 49 | | ? null |
| | | 50 | | : JsonSerializer.Deserialize<Dictionary<string, object>>(MetadataJson); |
| | | 51 | | return new IntentHistoryRecord( |
| | | 52 | | Id, |
| | | 53 | | BehaviorSpaceId, |
| | | 54 | | IntentName, |
| | | 55 | | ConfidenceLevel, |
| | | 56 | | ConfidenceScore, |
| | | 57 | | Enum.Parse<PolicyDecision>(Decision), |
| | | 58 | | RecordedAt, |
| | | 59 | | Metadata: metadata, |
| | | 60 | | EntityId: EntityId); |
| | | 61 | | } |
| | | 62 | | } |