Skip to content

Commit ef362ab

Browse files
committed
Add Nerdbank.GitVersioning and refactor code for clarity
This commit introduces the `Nerdbank.GitVersioning` package for improved version management in the project. Several files, including `ChatBot.razor`, `AIServiceFactory.cs`, and `CompanyService.cs`, were refactored to consistently use the `this` keyword for member variables, enhancing code readability and maintaining a consistent coding style. Additionally, the `ConfigurationService.cs` was updated to improve performance by changing return types to empty arrays, and centralized package version management was implemented through the new `Directory.Packages.props` file.
1 parent bad6716 commit ef362ab

13 files changed

Lines changed: 222 additions & 219 deletions

AIForITSM/AIForITSM.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AzureOpenAI" />
1616
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Ollama" />
1717
<PackageReference Include="QuestPDF" />
18+
<PackageReference Include="Nerdbank.GitVersioning" />
1819
</ItemGroup>
1920

2021
<!-- ── Static files specific to this project ── -->

AIForITSM/Components/Pages/ChatBot.razor

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<div class="mt-3">
2929
<div class="input-group">
3030
<input @bind="userInput" @bind:event="oninput" @onkeydown="HandleKeyDown" type="text" class="form-control" placeholder="Type your message here..." />
31-
<button class="btn btn-primary" @onclick="SendMessage">Send</button>
31+
<button class="btn btn-primary" @onclick="SendMessageAsync">Send</button>
3232
</div>
3333
<AIServiceSelector UseAzureService="ChatService.UseAzureService" UseAzureServiceChanged="OnUseAzureServiceChanged" />
3434
</div>
@@ -54,19 +54,19 @@
5454
StateHasChanged();
5555
}
5656

57-
private async Task SendMessage()
57+
private async Task SendMessageAsync()
5858
{
5959
string x = userInput;
6060
userInput = string.Empty;
6161
await ChatService.SendMessageAsync(x ?? string.Empty, StateHasChanged);
6262

6363
}
6464

65-
private void HandleKeyDown(KeyboardEventArgs e)
65+
private async Task HandleKeyDown(KeyboardEventArgs e)
6666
{
6767
if (e.Key == "Enter")
6868
{
69-
SendMessage();
69+
await SendMessageAsync();
7070
}
7171
}
7272
}

AIForITSM/Models/Incident.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System;
2-
using System.Text.Json.Serialization;
3-
41
namespace AIForITSM
52
{
3+
using System;
4+
using System.Text.Json.Serialization;
5+
66
/// <summary>
77
/// test comment
88
/// </summary>

AIForITSM/Services/AIServiceFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class AIServiceFactory
2424
/// <param name="logger">The logger instance.</param>
2525
public AIServiceFactory(CompanyService companyService, ILogger<AIServiceFactory> logger)
2626
{
27-
_companyService = companyService ?? throw new ArgumentNullException(nameof(companyService));
28-
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
27+
this._companyService = companyService ?? throw new ArgumentNullException(nameof(companyService));
28+
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
2929
}
3030

3131
/// <summary>
@@ -35,9 +35,9 @@ public AIServiceFactory(CompanyService companyService, ILogger<AIServiceFactory>
3535
/// <returns>The decrypted company configuration.</returns>
3636
public async Task<CompanyConfig> GetDecryptedCompanyConfigAsync(string companyName)
3737
{
38-
_logger.LogInformation("Retrieving decrypted configuration for company: {CompanyName}", companyName);
38+
this._logger.LogInformation("Retrieving decrypted configuration for company: {CompanyName}", companyName);
3939

40-
CompanyConfig encryptedConfig = await _companyService.GetCompanyDetailsAsync(companyName)
40+
CompanyConfig encryptedConfig = await this._companyService.GetCompanyDetailsAsync(companyName)
4141
?? throw new ArgumentNullException($"Company configuration not found for: {companyName}");
4242

4343
return new CompanyConfig(
@@ -63,10 +63,10 @@ public async Task<CompanyConfig> GetDecryptedCompanyConfigAsync(string companyNa
6363
/// <returns>A configured Kernel instance.</returns>
6464
public Kernel CreateKernel(CompanyConfig companyConfig, bool useAzureService)
6565
{
66-
_logger.LogInformation("Creating AI kernel with {Provider}",
66+
this._logger.LogInformation("Creating AI kernel with {Provider}",
6767
useAzureService ? "Azure OpenAI" : "Ollama");
6868

69-
var kernelBuilder = Kernel.CreateBuilder();
69+
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
7070

7171
if (useAzureService)
7272
{
@@ -101,7 +101,7 @@ public Kernel CreateKernel(CompanyConfig companyConfig, bool useAzureService)
101101
/// <returns>A configured chat completion service.</returns>
102102
public IChatCompletionService CreateChatCompletionService(CompanyConfig companyConfig, bool useAzureService)
103103
{
104-
_logger.LogInformation("Creating chat completion service with {Provider}",
104+
this._logger.LogInformation("Creating chat completion service with {Provider}",
105105
useAzureService ? "Azure OpenAI" : "Ollama");
106106

107107
if (useAzureService)

AIForITSM/Services/ChatBotService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class ChatBotService
1313

1414
public ChatBotService(AIServiceFactory aiServiceFactory)
1515
{
16-
_aiServiceFactory = aiServiceFactory ?? throw new ArgumentNullException(nameof(aiServiceFactory));
17-
_companyConfig = GetEmptyCompanyConfig();
16+
this._aiServiceFactory = aiServiceFactory ?? throw new ArgumentNullException(nameof(aiServiceFactory));
17+
this._companyConfig = this.GetEmptyCompanyConfig();
1818
}
1919

2020
public ChatHistory ChatHistory { get; } = [];
@@ -33,10 +33,10 @@ private CompanyConfig GetEmptyCompanyConfig()
3333
public async Task InitializeCompanyAsync(string company)
3434
{
3535
this.SelectedCompany = company;
36-
_companyConfig = await _aiServiceFactory.GetDecryptedCompanyConfigAsync(company);
36+
this._companyConfig = await this._aiServiceFactory.GetDecryptedCompanyConfigAsync(company);
3737

3838
// Use the factory to create the chat service
39-
_chatCompletionService = _aiServiceFactory.CreateChatCompletionService(_companyConfig, UseAzureService);
39+
this._chatCompletionService = this._aiServiceFactory.CreateChatCompletionService(this._companyConfig, this.UseAzureService);
4040

4141
this.IsChatSessionStarted = true;
4242
}
@@ -56,15 +56,15 @@ public async Task SendMessageAsync(string message, Action refreshScreen)
5656
throw new ArgumentException("Message cannot be empty", nameof(message));
5757
}
5858

59-
if (_chatCompletionService == null)
59+
if (this._chatCompletionService == null)
6060
{
6161
throw new InvalidOperationException("Chat service not initialized. Please initialize a company first.");
6262
}
6363

6464
this.ChatHistory.AddUserMessage(message);
6565
ChatMessageContent response = new ChatMessageContent(AuthorRole.Assistant, "");
6666
this.ChatHistory.Add(response);
67-
await foreach (StreamingChatMessageContent x in _chatCompletionService.GetStreamingChatMessageContentsAsync(this.ChatHistory))
67+
await foreach (StreamingChatMessageContent x in this._chatCompletionService.GetStreamingChatMessageContentsAsync(this.ChatHistory))
6868
{
6969
response.Content += x.Content;
7070
refreshScreen();

AIForITSM/Services/CompanyService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public async Task DeleteCompanyAsync(string companyName)
9696
this._logger.LogInformation("Attempting to delete company configuration for: {CompanyName}", companyName);
9797
try
9898
{
99-
var result = await _configService.RemoveKeyAsync(COMPANY_SECTION, companyName);
99+
bool result = await this._configService.RemoveKeyAsync(COMPANY_SECTION, companyName);
100100
if (result)
101101
{
102102
this._logger.LogInformation("Successfully deleted company configuration for: {CompanyName}", companyName);
@@ -122,7 +122,7 @@ public async Task<List<string>> GetCompanyNamesAsync()
122122
this._logger.LogInformation("Retrieving list of company names");
123123
try
124124
{
125-
var companySettings = await _configService.GetSectionAsync(COMPANY_SECTION);
125+
Dictionary<string, object> companySettings = await this._configService.GetSectionAsync(COMPANY_SECTION);
126126
List<string> companyNames = companySettings.Keys.ToList();
127127

128128
this._logger.LogInformation("Retrieved {Count} company names", companyNames.Count);
@@ -145,7 +145,7 @@ public async Task UpdateCompanyDetailsAsync(string companyName, CompanyConfig co
145145
this._logger.LogInformation("Updating configuration for company: {CompanyName}", companyName);
146146
try
147147
{
148-
await _configService.SetValueAsync(COMPANY_SECTION, companyName, config);
148+
await this._configService.SetValueAsync(COMPANY_SECTION, companyName, config);
149149
this._logger.LogInformation("Successfully updated configuration for company: {CompanyName}", companyName);
150150
}
151151
catch (Exception ex)
@@ -165,7 +165,7 @@ public async Task UpdateCompanyDetailsAsync(string companyName, CompanyConfig co
165165
this._logger.LogInformation("Retrieving configuration for company: {CompanyName}", companyName);
166166
try
167167
{
168-
CompanyConfig? companyConfig = await _configService.GetValueAsync<CompanyConfig>(COMPANY_SECTION, companyName);
168+
CompanyConfig? companyConfig = await this._configService.GetValueAsync<CompanyConfig>(COMPANY_SECTION, companyName);
169169

170170
if (companyConfig != null)
171171
{

AIForITSM/Services/ConfigurationService.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task<Dictionary<string, object>> GetConfigurationAsync()
3030
{
3131
string json = await File.ReadAllTextAsync(FILE_PATH);
3232
Dictionary<string, object>? config = JsonSerializer.Deserialize<Dictionary<string, object>>(json);
33-
return config ?? new Dictionary<string, object>();
33+
return config ?? [];
3434
}
3535
catch (Exception ex)
3636
{
@@ -67,13 +67,11 @@ public async Task<Dictionary<string, object>> GetSectionAsync(string sectionName
6767
{
6868
try
6969
{
70-
var config = await GetConfigurationAsync();
71-
if (config.ContainsKey(sectionName))
72-
{
73-
return JsonSerializer.Deserialize<Dictionary<string, object>>(
74-
config[sectionName]?.ToString() ?? "{}") ?? new Dictionary<string, object>();
75-
}
76-
return new Dictionary<string, object>();
70+
Dictionary<string, object> config = await this.GetConfigurationAsync();
71+
return config.ContainsKey(sectionName)
72+
? JsonSerializer.Deserialize<Dictionary<string, object>>(
73+
config[sectionName]?.ToString() ?? "{}") ?? []
74+
: [];
7775
}
7876
catch (Exception ex)
7977
{
@@ -91,9 +89,9 @@ public async Task UpdateSectionAsync(string sectionName, Dictionary<string, obje
9189
{
9290
try
9391
{
94-
var config = await GetConfigurationAsync();
92+
Dictionary<string, object> config = await this.GetConfigurationAsync();
9593
config[sectionName] = sectionData;
96-
await SaveConfigurationAsync(config);
94+
await this.SaveConfigurationAsync(config);
9795
this._logger.LogInformation("Section {SectionName} updated successfully", sectionName);
9896
}
9997
catch (Exception ex)
@@ -114,12 +112,8 @@ public async Task UpdateSectionAsync(string sectionName, Dictionary<string, obje
114112
{
115113
try
116114
{
117-
var section = await GetSectionAsync(sectionName);
118-
if (section.ContainsKey(key))
119-
{
120-
return JsonSerializer.Deserialize<T>(section[key]?.ToString() ?? "null");
121-
}
122-
return default;
115+
Dictionary<string, object> section = await this.GetSectionAsync(sectionName);
116+
return section.ContainsKey(key) ? JsonSerializer.Deserialize<T>(section[key]?.ToString() ?? "null") : default;
123117
}
124118
catch (Exception ex)
125119
{
@@ -138,7 +132,7 @@ public async Task SetValueAsync(string sectionName, string key, object value)
138132
{
139133
try
140134
{
141-
var config = await GetConfigurationAsync();
135+
Dictionary<string, object> config = await this.GetConfigurationAsync();
142136

143137
// Ensure section exists
144138
if (!config.ContainsKey(sectionName))
@@ -147,16 +141,16 @@ public async Task SetValueAsync(string sectionName, string key, object value)
147141
}
148142

149143
// Get section
150-
var section = JsonSerializer.Deserialize<Dictionary<string, object>>(
151-
config[sectionName]?.ToString() ?? "{}") ?? new Dictionary<string, object>();
144+
Dictionary<string, object> section = JsonSerializer.Deserialize<Dictionary<string, object>>(
145+
config[sectionName]?.ToString() ?? "{}") ?? [];
152146

153147
// Update value
154148
section[key] = value;
155149

156150
// Save back to config
157151
config[sectionName] = section;
158152

159-
await SaveConfigurationAsync(config);
153+
await this.SaveConfigurationAsync(config);
160154
this._logger.LogInformation("Value for {SectionName}.{Key} updated successfully", sectionName, key);
161155
}
162156
catch (Exception ex)
@@ -176,21 +170,22 @@ public async Task<bool> RemoveKeyAsync(string sectionName, string key)
176170
{
177171
try
178172
{
179-
var config = await GetConfigurationAsync();
173+
Dictionary<string, object> config = await this.GetConfigurationAsync();
180174
if (config.ContainsKey(sectionName))
181175
{
182-
var section = JsonSerializer.Deserialize<Dictionary<string, object>>(
183-
config[sectionName]?.ToString() ?? "{}") ?? new Dictionary<string, object>();
176+
Dictionary<string, object> section = JsonSerializer.Deserialize<Dictionary<string, object>>(
177+
config[sectionName]?.ToString() ?? "{}") ?? [];
184178

185179
if (section.ContainsKey(key))
186180
{
187181
section.Remove(key);
188182
config[sectionName] = section;
189-
await SaveConfigurationAsync(config);
183+
await this.SaveConfigurationAsync(config);
190184
this._logger.LogInformation("Key {Key} removed from section {SectionName}", key, sectionName);
191185
return true;
192186
}
193187
}
188+
194189
return false;
195190
}
196191
catch (Exception ex)

AIForITSM/Services/ProblemReportService.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ public class ProblemReportService
2424

2525
public ProblemReportService(AIServiceFactory aiServiceFactory, ILogger<ProblemReportService> logger)
2626
{
27-
_aiServiceFactory = aiServiceFactory ?? throw new ArgumentNullException(nameof(aiServiceFactory));
28-
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
27+
this._aiServiceFactory = aiServiceFactory ?? throw new ArgumentNullException(nameof(aiServiceFactory));
28+
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
2929
}
3030

3131
public async Task<CompanyConfig> SetSelectedCompanyAsync(string company)
3232
{
33-
_companyConfig = await _aiServiceFactory.GetDecryptedCompanyConfigAsync(company);
34-
_aiKernel = null;
35-
return _companyConfig;
33+
this._companyConfig = await this._aiServiceFactory.GetDecryptedCompanyConfigAsync(company);
34+
this._aiKernel = null;
35+
return this._companyConfig;
3636
}
3737

3838
public void SetUseAzureService(bool useAzureService)
3939
{
40-
_useAzureService = useAzureService;
41-
_aiKernel = null;
40+
this._useAzureService = useAzureService;
41+
this._aiKernel = null;
4242
}
4343

4444
public List<Problem> ParseProblemsFromFile(byte[] fileContent)
@@ -172,7 +172,7 @@ private void EnsureKernel()
172172
_ = this._companyConfig ?? throw new ArgumentNullException(nameof(this._companyConfig));
173173

174174
// Use the factory to create a kernel
175-
this._aiKernel = _aiServiceFactory.CreateKernel(this._companyConfig, this._useAzureService);
175+
this._aiKernel = this._aiServiceFactory.CreateKernel(this._companyConfig, this._useAzureService);
176176
}
177177
}
178178
}

0 commit comments

Comments
 (0)