| | | 1 | | using Intentum.Core.Intents; |
| | | 2 | | using Intentum.Runtime.Policy; |
| | | 3 | | |
| | | 4 | | namespace Intentum.Persistence.Repositories; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Repository interface for storing intent inference results and policy decisions. |
| | | 8 | | /// </summary> |
| | | 9 | | public interface IIntentHistoryRepository |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Saves an intent inference result with policy decision. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="behaviorSpaceId">Identifier of the behavior space.</param> |
| | | 15 | | /// <param name="intent">The inferred intent.</param> |
| | | 16 | | /// <param name="decision">The policy decision.</param> |
| | | 17 | | /// <param name="metadata">Optional metadata (e.g. EventsSummary, Source) to show where the inference came from.</pa |
| | | 18 | | /// <param name="entityId">Optional entity identifier (e.g. userId) for timeline and entity-scoped queries.</param> |
| | | 19 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 20 | | Task<string> SaveAsync( |
| | | 21 | | string behaviorSpaceId, |
| | | 22 | | Intent intent, |
| | | 23 | | PolicyDecision decision, |
| | | 24 | | IReadOnlyDictionary<string, object>? metadata = null, |
| | | 25 | | string? entityId = null, |
| | | 26 | | CancellationToken cancellationToken = default); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets intent history by behavior space ID. |
| | | 30 | | /// </summary> |
| | | 31 | | Task<IReadOnlyList<IntentHistoryRecord>> GetByBehaviorSpaceIdAsync( |
| | | 32 | | string behaviorSpaceId, |
| | | 33 | | CancellationToken cancellationToken = default); |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets intent history by confidence level. |
| | | 37 | | /// </summary> |
| | | 38 | | Task<IReadOnlyList<IntentHistoryRecord>> GetByConfidenceLevelAsync( |
| | | 39 | | string confidenceLevel, |
| | | 40 | | CancellationToken cancellationToken = default); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets intent history by policy decision. |
| | | 44 | | /// </summary> |
| | | 45 | | Task<IReadOnlyList<IntentHistoryRecord>> GetByDecisionAsync( |
| | | 46 | | PolicyDecision decision, |
| | | 47 | | CancellationToken cancellationToken = default); |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets intent history within a time window. |
| | | 51 | | /// </summary> |
| | | 52 | | Task<IReadOnlyList<IntentHistoryRecord>> GetByTimeWindowAsync( |
| | | 53 | | DateTimeOffset start, |
| | | 54 | | DateTimeOffset end, |
| | | 55 | | CancellationToken cancellationToken = default); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets intent history for an entity (e.g. userId) within a time window, ordered by RecordedAt. |
| | | 59 | | /// When entityId was not stored, use behaviorSpaceId as entity scope. |
| | | 60 | | /// </summary> |
| | | 61 | | Task<IReadOnlyList<IntentHistoryRecord>> GetByEntityIdAsync( |
| | | 62 | | string entityId, |
| | | 63 | | DateTimeOffset start, |
| | | 64 | | DateTimeOffset end, |
| | | 65 | | CancellationToken cancellationToken = default); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Record representing a stored intent inference result. |
| | | 70 | | /// </summary> |
| | 137 | 71 | | public sealed record IntentHistoryRecord( |
| | 95 | 72 | | string Id, |
| | 70 | 73 | | string BehaviorSpaceId, |
| | 88 | 74 | | string IntentName, |
| | 54 | 75 | | string ConfidenceLevel, |
| | 67 | 76 | | double ConfidenceScore, |
| | 64 | 77 | | PolicyDecision Decision, |
| | 285 | 78 | | DateTimeOffset RecordedAt, |
| | 20 | 79 | | IReadOnlyDictionary<string, object>? Metadata = null, |
| | 39 | 80 | | string? EntityId = null |
| | 137 | 81 | | ) |
| | | 82 | | { |
| | | 83 | | /// <summary> |
| | | 84 | | /// Creates a new record with a generated ID and current UTC time. Use when saving to any store (Mongo, Redis, etc.) |
| | | 85 | | /// </summary> |
| | | 86 | | public static IntentHistoryRecord Create( |
| | | 87 | | string behaviorSpaceId, |
| | | 88 | | Intent intent, |
| | | 89 | | PolicyDecision decision, |
| | | 90 | | IReadOnlyDictionary<string, object>? metadata = null, |
| | | 91 | | string? entityId = null) => |
| | 72 | 92 | | new( |
| | 72 | 93 | | Id: Guid.NewGuid().ToString(), |
| | 72 | 94 | | BehaviorSpaceId: behaviorSpaceId, |
| | 72 | 95 | | IntentName: intent.Name, |
| | 72 | 96 | | ConfidenceLevel: intent.Confidence.Level, |
| | 72 | 97 | | ConfidenceScore: intent.Confidence.Score, |
| | 72 | 98 | | Decision: decision, |
| | 72 | 99 | | RecordedAt: DateTimeOffset.UtcNow, |
| | 72 | 100 | | Metadata: metadata, |
| | 72 | 101 | | EntityId: entityId); |
| | | 102 | | } |