Commit ef34119
Make AI Assistant streaming production-ready
This commit implements comprehensive error handling, performance optimizations,
and test coverage for the AI Assistant streaming feature to address critical
production blockers identified in PR review.
## Phase 1: Error Handling & Recovery (CRITICAL)
**SSE Stream (lib/lightning/apollo_client/sse_stream.ex):**
- Add broadcast_error/2 to broadcast streaming errors to components
- Implement timeout supervision (Apollo timeout + 10s buffer)
- Add timeout_ref and completed state fields for lifecycle tracking
- Handle Finch connection failures with proper error messages
- Fix critical bug: add missing {:error, reason, _acc} pattern match
- Handle HTTP error status codes (4xx, 5xx) before parsing chunks
- Cancel timeouts on successful completion or error
**AI Assistant Component (lib/lightning_web/live/ai_assistant/component.ex):**
- Add streaming_error assign to track error state
- Implement handle_streaming_error/2 to mark messages as error
- Create streaming_error_state/1 component with retry/cancel UI
- Add retry_streaming event handler to resubmit messages
- Add cancel_streaming event handler to clear error state
- Show error UI conditionally in render_individual_session/1
**Edit LiveView (lib/lightning_web/live/workflow_live/edit.ex):**
- Add :streaming_error handler in handle_info/2
- Route streaming errors to components via send_update
**Message Processor (lib/lightning/ai_assistant/message_processor.ex):**
- Update find_pending_user_messages to include :processing status
**AI Assistant Context (lib/lightning/ai_assistant/ai_assistant.ex):**
- Allow finding messages in both :pending and :processing states
## Phase 2: Performance Optimization (HIGH)
**StreamingText Hook (assets/js/hooks/index.ts):**
- Add performance monitoring with parseCount and timing
- Implement 50ms debouncing to batch rapid chunk arrivals
- Add proper cleanup in destroyed() hook
- Reduce markdown parsing frequency during streaming
**ScrollToMessage Hook (assets/js/hooks/index.ts):**
- Implement throttle() helper function
- Throttle scroll position checks to max 100ms intervals
- Reduce CPU usage from excessive scroll calculations
- Add proper event listener cleanup
## Phase 3: Production Polish (MEDIUM)
**Logging Cleanup:**
- Change verbose Logger.info to Logger.debug in:
- lib/lightning/apollo_client/sse_stream.ex (9 changes)
- lib/lightning/ai_assistant/message_processor.ex (6 changes)
- lib/lightning_web/live/ai_assistant/component.ex (7 changes)
- Keep Logger.error for errors and Logger.info for key events
**Documentation (CHANGELOG.md):**
- Add comprehensive entry for AI Assistant Streaming feature
- Document user-facing features and technical implementation
## Test Coverage
**New Tests (24 tests, all passing):**
test/lightning/apollo_client/sse_stream_test.exs (9 tests):
- GenServer lifecycle and initialization
- Error event parsing and broadcasting
- Timeout handling
- Connection failure detection
- HTTP error responses
- Content chunk broadcasting
- Status update broadcasting
- Completion events
- Complete payload with metadata
test/lightning_web/live/workflow_live/ai_assistant_component_test.exs (3 tests):
- SSEStream error message formats
- Apollo JSON error parsing
- Component retry/cancel handler verification
## Bug Fixes
- Fix CaseClauseError in SSEStream when Finch returns {:error, reason, acc}
- This pattern occurs on connection refused before any HTTP response
- All 9 SSE stream tests now pass (previously 5/9 failing)
## Impact
Addresses critical production blockers from PR #3607 review:
1. ✅ Error handling gap - messages no longer stuck in processing state
2. ✅ CPU performance spike - debouncing and throttling reduce overhead
3. ✅ Missing error state transitions - full error → retry/cancel flow
## Manual Testing Required
Before merge, verify:
- Stream timeout → error UI → retry works
- Kill Apollo mid-stream → error UI appears
- Invalid code → Apollo error displays correctly
- CPU usage during streaming is acceptable
Co-authored-by: Claude <noreply@anthropic.com>1 parent 9e6aa4d commit ef34119
File tree
9 files changed
+761
-93
lines changed- assets/js/hooks
- lib
- lightning_web/live
- ai_assistant
- workflow_live
- lightning
- ai_assistant
- apollo_client
- test
- lightning_web/live/workflow_live
- lightning/apollo_client
9 files changed
+761
-93
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
20 | 27 | | |
21 | 28 | | |
22 | 29 | | |
| |||
27 | 34 | | |
28 | 35 | | |
29 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
30 | 44 | | |
31 | 45 | | |
32 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
687 | 687 | | |
688 | 688 | | |
689 | 689 | | |
690 | | - | |
691 | | - | |
| 690 | + | |
| 691 | + | |
692 | 692 | | |
693 | 693 | | |
694 | | - | |
| 694 | + | |
695 | 695 | | |
| 696 | + | |
696 | 697 | | |
697 | 698 | | |
698 | 699 | | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
699 | 721 | | |
700 | 722 | | |
701 | 723 | | |
| |||
740 | 762 | | |
741 | 763 | | |
742 | 764 | | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
743 | 768 | | |
744 | 769 | | |
745 | 770 | | |
| |||
1040 | 1065 | | |
1041 | 1066 | | |
1042 | 1067 | | |
| 1068 | + | |
| 1069 | + | |
1043 | 1070 | | |
1044 | 1071 | | |
1045 | 1072 | | |
1046 | 1073 | | |
1047 | | - | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
1048 | 1089 | | |
1049 | 1090 | | |
1050 | 1091 | | |
| |||
1083 | 1124 | | |
1084 | 1125 | | |
1085 | 1126 | | |
| 1127 | + | |
1086 | 1128 | | |
1087 | 1129 | | |
1088 | 1130 | | |
| 1131 | + | |
| 1132 | + | |
1089 | 1133 | | |
1090 | 1134 | | |
1091 | 1135 | | |
| |||
1096 | 1140 | | |
1097 | 1141 | | |
1098 | 1142 | | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
1099 | 1148 | | |
1100 | 1149 | | |
1101 | 1150 | | |
1102 | 1151 | | |
1103 | 1152 | | |
| 1153 | + | |
| 1154 | + | |
1104 | 1155 | | |
1105 | 1156 | | |
1106 | 1157 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
574 | 574 | | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
578 | 582 | | |
579 | 583 | | |
580 | 584 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | 60 | | |
62 | 61 | | |
63 | 62 | | |
| |||
150 | 149 | | |
151 | 150 | | |
152 | 151 | | |
153 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
154 | 157 | | |
155 | 158 | | |
156 | 159 | | |
| |||
173 | 176 | | |
174 | 177 | | |
175 | 178 | | |
176 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
177 | 182 | | |
178 | 183 | | |
179 | | - | |
180 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
181 | 189 | | |
182 | 190 | | |
183 | 191 | | |
| |||
239 | 247 | | |
240 | 248 | | |
241 | 249 | | |
242 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
243 | 255 | | |
244 | 256 | | |
245 | 257 | | |
246 | 258 | | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
257 | 270 | | |
258 | 271 | | |
259 | 272 | | |
| |||
263 | 276 | | |
264 | 277 | | |
265 | 278 | | |
266 | | - | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
267 | 282 | | |
268 | 283 | | |
269 | | - | |
270 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
271 | 289 | | |
272 | 290 | | |
273 | 291 | | |
| |||
290 | 308 | | |
291 | 309 | | |
292 | 310 | | |
293 | | - | |
294 | 311 | | |
295 | 312 | | |
296 | 313 | | |
| |||
389 | 406 | | |
390 | 407 | | |
391 | 408 | | |
392 | | - | |
| 409 | + | |
393 | 410 | | |
394 | 411 | | |
395 | 412 | | |
| |||
466 | 483 | | |
467 | 484 | | |
468 | 485 | | |
469 | | - | |
| 486 | + | |
470 | 487 | | |
471 | 488 | | |
472 | 489 | | |
| |||
0 commit comments