|
| 1 | +// <copyright file="AiValidator.cs" company="Automate The Planet Ltd."> |
| 2 | +// Copyright 2025 Automate The Planet Ltd. |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// You may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// Unless required by applicable law or agreed to in writing, software |
| 7 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +// See the License for the specific language governing permissions and |
| 10 | +// limitations under the License. |
| 11 | +// </copyright> |
| 12 | +// <author>Anton Angelov</author> |
| 13 | +// <site>https://bellatrix.solutions/</site> |
| 14 | +// <note>This file is part of an academic research project exploring autonomous test agents using LLMs and Semantic Kernel. |
| 15 | +// The architecture and agent logic are original contributions by Anton Angelov, forming the foundation for a PhD dissertation. |
| 16 | +using Bellatrix.LLM; |
| 17 | +using Bellatrix.LLM.Settings; |
| 18 | +using Bellatrix.LLM.Skills; |
| 19 | +using Microsoft.SemanticKernel; |
| 20 | + |
| 21 | +namespace Bellatrix; |
| 22 | + |
| 23 | +public static class AiValidator |
| 24 | +{ |
| 25 | + public static void ValidateByPrompt(string assertInstruction, int? timeout = null, int? sleepInterval = null) |
| 26 | + { |
| 27 | + var snapshotProvider = ServicesCollection.Current.Resolve<IViewSnapshotProvider>(); |
| 28 | + |
| 29 | + var settings = ConfigurationService.GetSection<LargeLanguageModelsSettings>(); |
| 30 | + var effectiveTimeout = timeout ?? settings.ValidationsTimeout; |
| 31 | + var effectiveSleep = sleepInterval ?? settings.SleepInterval; |
| 32 | + |
| 33 | + var verdict = string.Empty; |
| 34 | + |
| 35 | + void WaitForAssertionToPass() |
| 36 | + { |
| 37 | + Utilities.Wait.Until( |
| 38 | + condition: () => |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + var appSnapshot = snapshotProvider.GetCurrentViewSnapshot(); |
| 43 | + |
| 44 | + var result = SemanticKernelService.Kernel.InvokeAsync(nameof(AssertionSkill), nameof(AssertionSkill.EvaluateAssertion), new() |
| 45 | + { |
| 46 | + ["htmlSummary"] = appSnapshot, |
| 47 | + ["assertInstruction"] = assertInstruction |
| 48 | + }).Result; |
| 49 | + |
| 50 | + var fullPrompt = result.GetValue<string>(); |
| 51 | + verdict = SemanticKernelService.Kernel.InvokePromptAsync(fullPrompt).Result.GetValue<string>()?.Trim(); |
| 52 | + |
| 53 | + return !string.IsNullOrWhiteSpace(verdict) && |
| 54 | + verdict.Contains("PASS", StringComparison.OrdinalIgnoreCase); |
| 55 | + } |
| 56 | + catch |
| 57 | + { |
| 58 | + return false; |
| 59 | + } |
| 60 | + }, |
| 61 | + timeoutInSeconds: effectiveTimeout, |
| 62 | + exceptionMessage: $"❌ AI validation failed: {assertInstruction} within timeout ({effectiveTimeout}s).\n - {verdict}", |
| 63 | + retryRateDelay: effectiveSleep * 1000 |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + WaitForAssertionToPass(); |
| 68 | + Console.WriteLine("✅ AI Validate passed: " + assertInstruction); |
| 69 | + } |
| 70 | +} |
| 71 | + |
0 commit comments