Fix ChatController naming consistency and streaming cancellation/error handling#1089
Fix ChatController naming consistency and streaming cancellation/error handling#1089Copilot wants to merge 6 commits into
Conversation
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/c4364690-d679-41d6-a271-69d6c45343af Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates ChatController to improve naming consistency and to better align streaming response writes and exception handling with request cancellation behavior for the chat streaming endpoint.
Changes:
- Renames the
AIChatServicefield from_AiChatServiceto_AIChatServiceand updates usages. - Updates
StreamMessageto use the requestcancellationToken(instead ofCancellationToken.None) when writing JSON/SSE error responses. - Simplifies cancellation and consolidates duplicated
ConversationContextLimitExceededExceptionhandling based on whether the response has started.
Comments suppressed due to low confidence (1)
EssentialCSharp.Web/Controllers/ChatController.cs:170
catch (Exception ex) when (!Response.HasStarted)writes a JSON 500 body usingcancellationTokenwithout any protection. If the exception was triggered by a client disconnect / abort (common with streaming endpoints), this write can throwOperationCanceledException(or other I/O exceptions) from inside the catch and bypass your error handling. Consider guarding withif (cancellationToken.IsCancellationRequested) return;or wrapping the write in try/catch for cancellation/I/O.
}
catch (Exception ex) when (!Response.HasStarted)
{
LogChatStreamErrorBeforeResponseStarted(_Logger, ex, User.Identity?.Name);
Response.StatusCode = 500;
Response.ContentType = "application/json";
| await Response.Body.FlushAsync(cancellationToken); | ||
| } | ||
| catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested || HttpContext.RequestAborted.IsCancellationRequested) | ||
| catch (OperationCanceledException) |
| { | ||
| LogChatStreamErrorMidStream(_Logger, ex, User.Identity?.Name); | ||
| try | ||
| if (!Response.HasStarted) | ||
| { | ||
| await Response.WriteAsync("data: {\"type\":\"error\",\"message\":\"This conversation has grown too long. Please start a new one.\",\"errorCode\":\"context_limit_exceeded\"}\n\n", CancellationToken.None); | ||
| await Response.Body.FlushAsync(CancellationToken.None); | ||
| Response.StatusCode = 400; | ||
| Response.ContentType = "application/json"; | ||
| await Response.WriteAsJsonAsync(new { error = "This conversation has grown too long. Please start a new one.", errorCode = "context_limit_exceeded" }, cancellationToken); |
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/ca7b0afb-1731-4a50-ac2e-b4d7152c972a Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/ca7b0afb-1731-4a50-ac2e-b4d7152c972a Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
| @@ -137,49 +138,88 @@ public async Task StreamMessage([FromBody] ChatMessageRequest request, Cancellat | |||
| { | |||
| LogChatStreamCancelled(_Logger, User.Identity?.Name); | |||
| } | |||
There was a problem hiding this comment.
@copilot If this (#1089 (comment)) is valid feedback, we should address
There was a problem hiding this comment.
Addressed in f2ca026. I updated the StreamMessage cancellation filter to also treat OperationCanceledException as expected once the response has already started, which covers disconnect/write-abort cases without swallowing unrelated pre-response cancellations.
Agent-Logs-Url: https://github.com/IntelliTect/EssentialCSharp.Web/sessions/c0627de3-aa52-4d77-a845-1420ecbe2416 Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
/home/runner/work/EssentialCSharp.Web/EssentialCSharp.Web/EssentialCSharp.Web/Controllers/ChatController.csStreamMessagecancellation/error handling to address the active PR comments and fix the compile errorOriginal prompt