| | | 1 | | using Intentum.Core.Behavior; |
| | | 2 | | using Intentum.Core.Contracts; |
| | | 3 | | using Intentum.Core.Intents; |
| | | 4 | | |
| | | 5 | | namespace Intentum.Core.Batch; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Batch intent model that processes multiple behavior spaces efficiently. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class BatchIntentModel : IBatchIntentModel |
| | | 11 | | { |
| | | 12 | | private readonly IIntentModel _innerModel; |
| | | 13 | | |
| | 4 | 14 | | public BatchIntentModel(IIntentModel innerModel) |
| | 4 | 15 | | { |
| | 4 | 16 | | _innerModel = innerModel ?? throw new ArgumentNullException(nameof(innerModel)); |
| | 4 | 17 | | } |
| | | 18 | | |
| | | 19 | | public IReadOnlyList<Intent> InferBatch(IReadOnlyCollection<BehaviorSpace>? behaviorSpaces) |
| | 2 | 20 | | { |
| | 2 | 21 | | if (behaviorSpaces == null || behaviorSpaces.Count == 0) |
| | 1 | 22 | | return []; |
| | | 23 | | |
| | 1 | 24 | | return behaviorSpaces |
| | 3 | 25 | | .Select(space => _innerModel.Infer(space)) |
| | 1 | 26 | | .ToList(); |
| | 2 | 27 | | } |
| | | 28 | | |
| | | 29 | | public async Task<IReadOnlyList<Intent>> InferBatchAsync( |
| | | 30 | | IReadOnlyCollection<BehaviorSpace>? behaviorSpaces, |
| | | 31 | | CancellationToken cancellationToken = default) |
| | 2 | 32 | | { |
| | 2 | 33 | | if (behaviorSpaces == null || behaviorSpaces.Count == 0) |
| | 0 | 34 | | return []; |
| | | 35 | | |
| | 2 | 36 | | var tasks = behaviorSpaces.Select(async space => |
| | 5 | 37 | | { |
| | 5 | 38 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 3 | 39 | | await Task.Yield(); // Allow async context switching |
| | 3 | 40 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 3 | 41 | | return _innerModel.Infer(space); |
| | 5 | 42 | | }); |
| | | 43 | | |
| | 2 | 44 | | var results = await Task.WhenAll(tasks); |
| | 1 | 45 | | return results.ToList(); |
| | 1 | 46 | | } |
| | | 47 | | } |