Skip to content

Commit f385438

Browse files
Cleanup
1 parent e44cb70 commit f385438

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-34
lines changed

EssentialCSharp.Chat.Shared/Services/AIChatService.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ public AIChatService(IOptions<AIOptions> options, AISearchService searchService)
7676
bool enableContextualSearch = false,
7777
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
7878
{
79-
// Add logging to help debug the conversation state issue
80-
Debug.WriteLine($"GetChatCompletionStream called with previousResponseId: {previousResponseId}");
81-
8279
var responseOptions = await CreateResponseOptionsAsync(previousResponseId, tools, reasoningEffortLevel, mcpClient: mcpClient, cancellationToken: cancellationToken);
8380
var enrichedPrompt = await EnrichPromptWithContext(prompt, enableContextualSearch, cancellationToken);
8481

@@ -113,33 +110,24 @@ private async Task<string> EnrichPromptWithContext(string prompt, bool enableCon
113110
return prompt;
114111
}
115112

116-
try
117-
{
118-
var searchResults = await _SearchService.ExecuteVectorSearch(prompt);
119-
var contextualInfo = new System.Text.StringBuilder();
120-
121-
contextualInfo.AppendLine("## Contextual Information");
122-
contextualInfo.AppendLine("The following information might be relevant to your question:");
123-
contextualInfo.AppendLine();
124-
125-
await foreach (var result in searchResults)
126-
{
127-
contextualInfo.AppendLine(System.Globalization.CultureInfo.InvariantCulture, $"**From: {result.Record.Heading}**");
128-
contextualInfo.AppendLine(result.Record.ChunkText);
129-
contextualInfo.AppendLine();
130-
}
113+
var searchResults = await _SearchService.ExecuteVectorSearch(prompt);
114+
var contextualInfo = new System.Text.StringBuilder();
131115

132-
contextualInfo.AppendLine("## User Question");
133-
contextualInfo.AppendLine(prompt);
116+
contextualInfo.AppendLine("## Contextual Information");
117+
contextualInfo.AppendLine("The following information might be relevant to your question:");
118+
contextualInfo.AppendLine();
134119

135-
return contextualInfo.ToString();
136-
}
137-
catch (Exception ex)
120+
await foreach (var result in searchResults)
138121
{
139-
// Log the error but don't fail the request
140-
Debug.WriteLine($"Error enriching prompt with context: {ex.Message}");
141-
return prompt;
122+
contextualInfo.AppendLine(System.Globalization.CultureInfo.InvariantCulture, $"**From: {result.Record.Heading}**");
123+
contextualInfo.AppendLine(result.Record.ChunkText);
124+
contextualInfo.AppendLine();
142125
}
126+
127+
contextualInfo.AppendLine("## User Question");
128+
contextualInfo.AppendLine(prompt);
129+
130+
return contextualInfo.ToString();
143131
}
144132

145133
/// <summary>

EssentialCSharp.Web/Controllers/ChatController.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace EssentialCSharp.Web.Controllers;
88

99
[ApiController]
1010
[Route("api/[controller]")]
11-
[Authorize] // Require authentication for all chat endpoints
11+
[Authorize]
1212
[EnableRateLimiting("ChatEndpoint")]
1313
public class ChatController : ControllerBase
1414
{
@@ -62,7 +62,8 @@ public async Task<IActionResult> SendMessage([FromBody] ChatMessageRequest reque
6262
}
6363
catch (Exception ex)
6464
{
65-
_Logger.LogError(ex, "Error processing chat message: {Message}", request.Message);
65+
var sanitizedMessage = request.Message?.Replace("\r", "").Replace("\n", "");
66+
_Logger.LogError(ex, "Error processing chat message: {Message}", sanitizedMessage);
6667
return StatusCode(500, new { error = "An error occurred while processing your message. Please try again." });
6768
}
6869
}
@@ -130,7 +131,8 @@ public async Task StreamMessage([FromBody] ChatMessageRequest request, Cancellat
130131
}
131132
catch (Exception ex)
132133
{
133-
_Logger.LogError(ex, "Error processing streaming chat message: {Message}", request.Message);
134+
var sanitizedMessage = request.Message?.Replace("\n", "").Replace("\r", "");
135+
_Logger.LogError(ex, "Error processing streaming chat message: {Message}", sanitizedMessage);
134136
Response.StatusCode = 500;
135137
await Response.WriteAsync(JsonSerializer.Serialize(new { error = "An error occurred while processing your message. Please try again." }), cancellationToken);
136138
}

EssentialCSharp.Web/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ await context.HttpContext.Response.WriteAsync(
302302
app.UseMiddleware<ReferralMiddleware>();
303303

304304
app.MapRazorPages();
305-
app.MapDefaultControllerRoute().RequireRateLimiting("ChatEndpoint"); // Apply rate limiting to controllers
305+
app.MapDefaultControllerRoute();
306306

307307
app.MapFallbackToController("Index", "Home");
308308

EssentialCSharp.Web/wwwroot/js/chat-module.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ export function useChatWidget() {
1313
const chatMessagesEl = ref(null);
1414
const chatInputField = ref(null);
1515
const lastResponseId = ref(null);
16-
17-
// Captcha state (currently not used but referenced in template)
18-
const showCaptcha = ref(false);
19-
const captchaSiteKey = ref(window.HCAPTCHA_SITE_KEY || '');
2016

2117
// Load chat history from localStorage on initialization
2218
function loadChatHistory() {

0 commit comments

Comments
 (0)