aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs')
-rw-r--r--Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs97
1 files changed, 97 insertions, 0 deletions
diff --git a/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs b/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs
new file mode 100644
index 000000000..5fcb4bf66
--- /dev/null
+++ b/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs
@@ -0,0 +1,97 @@
+using Azure.Data.Tables;
+using Microsoft.Extensions.Options;
+using Tango.Portal.Chat.Web.Models;
+
+namespace Tango.Portal.Chat.Web.Services
+{
+ public sealed class AIInstructionService
+ {
+ private readonly TableClient _tableClient;
+ private readonly ILogger<AIInstructionService> _logger;
+
+ public AIInstructionService(IOptions<AzureStorageOptions> options, ILogger<AIInstructionService> logger)
+ {
+ _logger = logger;
+ var serviceClient = new TableServiceClient(options.Value.ConnectionString);
+ _tableClient = serviceClient.GetTableClient("AIInstructions");
+
+ // Create table if it doesn't exist
+ _tableClient.CreateIfNotExists();
+ }
+
+ public async Task<bool> AddInstructionAsync(string instruction, string createdBy)
+ {
+ try
+ {
+ var aiInstruction = new AIInstruction
+ {
+ RowKey = Guid.NewGuid().ToString(),
+ Instruction = instruction,
+ CreatedAt = DateTime.UtcNow,
+ CreatedBy = createdBy
+ };
+
+ await _tableClient.AddEntityAsync(aiInstruction);
+ _logger.LogInformation("Added AI instruction by {CreatedBy}: {Instruction}", createdBy, instruction);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Failed to add AI instruction: {Instruction}", instruction);
+ return false;
+ }
+ }
+
+ public async Task<bool> DeleteLastInstructionAsync()
+ {
+ try
+ {
+ var instructions = await GetAllInstructionsAsync();
+ var lastInstruction = instructions.OrderByDescending(i => i.CreatedAt).FirstOrDefault();
+
+ if (lastInstruction == null)
+ {
+ _logger.LogWarning("No instructions found to delete");
+ return false;
+ }
+
+ await _tableClient.DeleteEntityAsync(lastInstruction.PartitionKey, lastInstruction.RowKey);
+ _logger.LogInformation("Deleted last AI instruction: {Instruction}", lastInstruction.Instruction);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Failed to delete last AI instruction");
+ return false;
+ }
+ }
+
+ public async Task<List<AIInstruction>> GetAllInstructionsAsync()
+ {
+ try
+ {
+ var instructions = new List<AIInstruction>();
+ await foreach (var instruction in _tableClient.QueryAsync<AIInstruction>())
+ {
+ instructions.Add(instruction);
+ }
+ return instructions.OrderBy(i => i.CreatedAt).ToList();
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Failed to retrieve AI instructions");
+ return new List<AIInstruction>();
+ }
+ }
+
+ public async Task<string> GetInstructionsTextAsync()
+ {
+ var instructions = await GetAllInstructionsAsync();
+ if (!instructions.Any())
+ return string.Empty;
+
+ var instructionTexts = instructions.Select(i => i.Instruction);
+ return string.Join("\n", instructionTexts);
+ }
+ }
+} \ No newline at end of file