aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_22/Tango.Portal.Chat.Web/Services/AIInstructionService.cs
blob: 5fcb4bf66818a09ea8370df69adc4c55399795b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
        }
    }
}