| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Intentum.Core.Behavior; |
| | | 5 | | using Intentum.Core.Caching; |
| | | 6 | | using Intentum.Core.Contracts; |
| | | 7 | | using Intentum.Core.Intents; |
| | | 8 | | |
| | | 9 | | namespace Intentum.AI.Caching; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Wraps an intent model and caches results by behavior vector hash (exact match). |
| | | 13 | | /// Use to skip recomputation when the same or identical vector was inferred recently. |
| | | 14 | | /// For similarity-threshold matching (e.g. "close enough" vector), implement a custom cache key strategy. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class CachedIntentModel : IIntentModel |
| | | 17 | | { |
| | 0 | 18 | | private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCas |
| | | 19 | | private readonly IIntentModel _inner; |
| | | 20 | | private readonly IIntentResultCache _cache; |
| | | 21 | | private readonly TimeSpan? _ttl; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates a cached intent model. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="inner">The inner intent model.</param> |
| | | 27 | | /// <param name="cache">Cache for intent results (e.g. MemoryIntentResultCache or Redis-backed).</param> |
| | | 28 | | /// <param name="ttl">Optional time-to-live for cached entries.</param> |
| | 0 | 29 | | public CachedIntentModel(IIntentModel inner, IIntentResultCache cache, TimeSpan? ttl = null) |
| | 0 | 30 | | { |
| | 0 | 31 | | _inner = inner ?? throw new ArgumentNullException(nameof(inner)); |
| | 0 | 32 | | _cache = cache ?? throw new ArgumentNullException(nameof(cache)); |
| | 0 | 33 | | _ttl = ttl; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <inheritdoc /> |
| | | 37 | | public Intent Infer(BehaviorSpace behaviorSpace, BehaviorVector? precomputedVector = null) |
| | 0 | 38 | | { |
| | 0 | 39 | | var vector = precomputedVector ?? behaviorSpace.ToVector(); |
| | 0 | 40 | | var key = VectorToCacheKey(vector); |
| | 0 | 41 | | var (found, value) = _cache.TryGetAsync(key).GetAwaiter().GetResult(); |
| | 0 | 42 | | if (found && value != null) |
| | 0 | 43 | | { |
| | | 44 | | try |
| | 0 | 45 | | { |
| | 0 | 46 | | return JsonSerializer.Deserialize<Intent>(value, JsonOptions)!; |
| | | 47 | | } |
| | 0 | 48 | | catch |
| | 0 | 49 | | { |
| | | 50 | | // Invalid cache entry; fall through to infer |
| | 0 | 51 | | } |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | var intent = _inner.Infer(behaviorSpace, vector); |
| | 0 | 55 | | var json = JsonSerializer.Serialize(intent, JsonOptions); |
| | 0 | 56 | | _cache.SetAsync(key, json, _ttl).GetAwaiter().GetResult(); |
| | 0 | 57 | | return intent; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | private static string VectorToCacheKey(BehaviorVector vector) |
| | 0 | 61 | | { |
| | 0 | 62 | | var sb = new StringBuilder(); |
| | 0 | 63 | | foreach (var kv in vector.Dimensions.OrderBy(k => k.Key, StringComparer.Ordinal)) |
| | 0 | 64 | | sb.Append(kv.Key).Append(':').Append(kv.Value).Append(';'); |
| | 0 | 65 | | var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(sb.ToString())); |
| | 0 | 66 | | return Convert.ToHexString(bytes); |
| | 0 | 67 | | } |
| | | 68 | | } |