Commit 670b0f5
fix: ChatController naming consistency and streaming cancellation/error handling (#1089)
- [x] Inspect PR feedback and failing CI for
`/home/runner/work/EssentialCSharp.Web/EssentialCSharp.Web/EssentialCSharp.Web/Controllers/ChatController.cs`
- [x] Update `StreamMessage` cancellation/error handling to address the
active PR comments and fix the compile error
- [x] Rebuild the solution and run relevant tests to verify the change
- [ ] Run final validation and summarize results
<!-- START COPILOT CODING AGENT SUFFIX -->
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
Please apply the following diffs and create a pull request.
Once the PR is ready, give it a title based on the messages of the fixes
being applied.
[{"message":"The field `_AiChatService` uses inconsistent casing ('Ai'
instead of 'AI'). It should be renamed to `_AIChatService` to match the
type name `AIChatService` and maintain consistency with other fields
like
`_Logger`.","fixFiles":[{"filePath":"EssentialCSharp.Web/Controllers/ChatController.cs","diff":"diff
--git a/EssentialCSharp.Web/Controllers/ChatController.cs
b/EssentialCSharp.Web/Controllers/ChatController.cs\n---
a/EssentialCSharp.Web/Controllers/ChatController.cs\n+++
b/EssentialCSharp.Web/Controllers/ChatController.cs\n@@ -15,13 +15,13
@@\n [IgnoreAntiforgeryToken]\n public partial class ChatController :
ControllerBase\n {\n- private readonly AIChatService _AiChatService;\n+
private readonly AIChatService _AIChatService;\n private readonly
ResponseIdValidationService _ResponseIdValidationService;\n private
readonly ILogger<ChatController> _Logger;\n \n public
ChatController(ILogger<ChatController> logger, AIChatService
aiChatService, ResponseIdValidationService
responseIdValidationService)\n {\n- _AiChatService = aiChatService;\n+
_AIChatService = aiChatService;\n _ResponseIdValidationService =
responseIdValidationService;\n _Logger = logger;\n }\n@@ -46,7 +41,7
@@\n \n try\n {\n- var (response, responseId) = await
_AiChatService.GetChatCompletion(\n+ var (response, responseId) = await
_AIChatService.GetChatCompletion(\n prompt: request.Message,\n
previousResponseId: previousResponseId,\n enableContextualSearch:
request.EnableContextualSearch,\n"}]},{"message":"The method uses
`CancellationToken.None` instead of the provided `cancellationToken`
parameter when writing error responses. This prevents graceful
cancellation of the write operation if the request is aborted. Replace
`CancellationToken.None` with `cancellationToken` throughout the
StreamMessage method (lines 78, 86, 97, 144, 151, 152, 167, 174, 175) to
respect cancellation
requests.","fixFiles":[{"filePath":"EssentialCSharp.Web/Controllers/ChatController.cs","diff":"diff
--git a/EssentialCSharp.Web/Controllers/ChatController.cs
b/EssentialCSharp.Web/Controllers/ChatController.cs\n---
a/EssentialCSharp.Web/Controllers/ChatController.cs\n+++
b/EssentialCSharp.Web/Controllers/ChatController.cs\n@@ -75,7 +75,7 @@\n
if (string.IsNullOrEmpty(request.Message))\n {\n Response.StatusCode =
400;\n- await Response.WriteAsJsonAsync(new { error = \"Message cannot
be empty.\" }, CancellationToken.None);\n+ await
Response.WriteAsJsonAsync(new { error = \"Message cannot be empty.\" },
cancellationToken);\n return;\n }\n \n@@ -83,7 +83,7 @@\n if
(string.IsNullOrEmpty(userId))\n {\n Response.StatusCode = 401;\n- await
Response.WriteAsJsonAsync(new { error = \"Unauthorized.\" },
CancellationToken.None);\n+ await Response.WriteAsJsonAsync(new { error
= \"Unauthorized.\" }, cancellationToken);\n return;\n }\n \n@@ -94,7
+94,7 @@\n if (!_ResponseIdValidationService.ValidateResponseId(userId,
previousResponseId))\n {\n Response.StatusCode = 400;\n- await
Response.WriteAsJsonAsync(new { error = \"Invalid conversation
context.\" }, CancellationToken.None);\n+ await
Response.WriteAsJsonAsync(new { error = \"Invalid conversation
context.\" }, cancellationToken);\n return;\n }\n \n"}]},{"message":"The
when clause checks both `cancellationToken.IsCancellationRequested` and
`HttpContext.RequestAborted.IsCancellationRequested`, but this filter
will not catch `OperationCanceledException` thrown when either token is
cancelled without the corresponding property being set at the exact
moment the filter evaluates. Remove the when clause entirely or simplify
to catch all `OperationCanceledException` instances, as these are
expected during
cancellation.","fixFiles":[{"filePath":"EssentialCSharp.Web/Controllers/ChatController.cs","diff":"diff
--git a/EssentialCSharp.Web/Controllers/ChatController.cs
b/EssentialCSharp.Web/Controllers/ChatController.cs\n---
a/EssentialCSharp.Web/Controllers/ChatController.cs\n+++
b/EssentialCSharp.Web/Controllers/ChatController.cs\n@@ -133,7 +133,7
@@\n await Response.WriteAsync(\"data: [DONE]\\n\\n\",
cancellationToken);\n await
Response.Body.FlushAsync(cancellationToken);\n }\n- catch
(OperationCanceledException) when
(cancellationToken.IsCancellationRequested ||
HttpContext.RequestAborted.IsCancellationRequested)\n+ catch
(OperationCanceledException)\n {\n LogChatStreamCancelled(_Logger,
User.Identity?.Name);\n }\n"}]},{"message":"The exception handling f...
</details>
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com>
Co-authored-by: Benjamin Michaelis <git@relay.benjamin.michaelis.net>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>1 parent 44487d8 commit 670b0f5
4 files changed
Lines changed: 162 additions & 42 deletions
File tree
- EssentialCSharp.Web
- Controllers
- Extensions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
| |||
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | | - | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
49 | | - | |
| 50 | + | |
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
78 | | - | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | 86 | | |
86 | | - | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
| |||
94 | 95 | | |
95 | 96 | | |
96 | 97 | | |
97 | | - | |
| 98 | + | |
98 | 99 | | |
99 | 100 | | |
100 | 101 | | |
| |||
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
107 | | - | |
| 108 | + | |
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
| |||
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
136 | | - | |
| 137 | + | |
137 | 138 | | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | | - | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | | - | |
152 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
153 | 176 | | |
154 | | - | |
| 177 | + | |
155 | 178 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
160 | 192 | | |
161 | 193 | | |
162 | 194 | | |
163 | 195 | | |
164 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
165 | 200 | | |
166 | 201 | | |
167 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
168 | 218 | | |
169 | 219 | | |
170 | 220 | | |
171 | 221 | | |
172 | 222 | | |
173 | 223 | | |
174 | | - | |
175 | | - | |
| 224 | + | |
| 225 | + | |
176 | 226 | | |
177 | | - | |
| 227 | + | |
178 | 228 | | |
179 | 229 | | |
180 | 230 | | |
181 | | - | |
182 | | - | |
| 231 | + | |
| 232 | + | |
183 | 233 | | |
184 | 234 | | |
185 | 235 | | |
| |||
Lines changed: 77 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
| |||
13 | 16 | | |
14 | 17 | | |
15 | 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 | + | |
16 | 92 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 122 | + | |
133 | 123 | | |
134 | 124 | | |
135 | 125 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| |||
0 commit comments