| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Intentum.Core.Intents; |
| | | 3 | | using Intentum.Runtime.Engine; |
| | | 4 | | using Intentum.Runtime.Policy; |
| | | 5 | | |
| | | 6 | | namespace Intentum.Explainability; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Default implementation of intent decision tree: uses policy engine EvaluateWithRule and intent signals. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class IntentTreeExplainer : IIntentTreeExplainer |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public IntentDecisionTree GetIntentTree( |
| | | 15 | | Intent intent, |
| | | 16 | | IntentPolicy policy, |
| | | 17 | | BehaviorSpace? behaviorSpace = null) |
| | 4 | 18 | | { |
| | 4 | 19 | | var (decision, matchedRule) = IntentPolicyEngine.EvaluateWithRule(intent, policy); |
| | | 20 | | |
| | 4 | 21 | | var intentSummary = new IntentTreeIntentSummary( |
| | 4 | 22 | | intent.Name, |
| | 4 | 23 | | intent.Confidence.Level, |
| | 4 | 24 | | intent.Confidence.Score); |
| | | 25 | | |
| | 4 | 26 | | var signals = intent.Signals |
| | 1 | 27 | | .Select(s => new IntentTreeSignalNode(s.Source, s.Description, s.Weight)) |
| | 4 | 28 | | .ToList(); |
| | | 29 | | |
| | 4 | 30 | | string? behaviorSummary = null; |
| | 4 | 31 | | if (behaviorSpace is { Events.Count: > 0 }) |
| | 1 | 32 | | { |
| | 1 | 33 | | var parts = behaviorSpace.Events |
| | 2 | 34 | | .OrderBy(e => e.OccurredAt) |
| | 2 | 35 | | .Select(e => $"{e.Actor}:{e.Action}") |
| | 1 | 36 | | .ToList(); |
| | 1 | 37 | | behaviorSummary = string.Join(" → ", parts); |
| | 1 | 38 | | } |
| | | 39 | | |
| | 4 | 40 | | return new IntentDecisionTree( |
| | 4 | 41 | | decision, |
| | 4 | 42 | | matchedRule?.Name, |
| | 4 | 43 | | intentSummary, |
| | 4 | 44 | | signals, |
| | 4 | 45 | | behaviorSummary); |
| | 4 | 46 | | } |
| | | 47 | | } |