Commit 8efd867
fix(ai-client): port #508 robustness fixes onto fetcher-alt branch
The fetcher path uses the same SSE parsing and connect-wrapper plumbing
as the stream() path on #508, so the polish that landed during #508's
review applies directly here. Carry it over so this branch has the same
robustness.
- Skip SSE control lines (`:` comments, `event:` / `id:` / `retry:`) in
responseToSSEChunks. Proxies and CDNs inject these as keepalives;
letting them through would feed JSON.parse a non-payload line.
- Drop unterminated trailing buffer in readStreamLines. A non-empty
buffer at stream end means the connection was cut mid-line, so the
data is partial — yielding it would surface a misleading RUN_ERROR
for what is really a transport-layer issue.
- Surface JSON.parse failures in responseToSSEChunks and fetchHttpStream.
Stop swallowing them behind console.warn; let SyntaxError propagate so
the connect-wrapper turns it into a visible RUN_ERROR.
- Drop unsafe `as unknown as StreamChunk` casts in
normalizeConnectionAdapter's synthesized RUN_FINISHED / RUN_ERROR
events. Use EventType + RunFinishedEvent / RunErrorEvent so missing
required fields are caught by the compiler. Track upstream
threadId/runId from chunks and reuse them in the synthesis instead of
fabricating both ids unconditionally.
- Forward optional abortSignal third arg through stream() and rpcStream()
factory signatures. Backwards-compatible for existing callers; lets
long-running factories cancel when useChat aborts. Mirrors what
fetcherToConnectionAdapter already does.
Tests:
- Update the two `should handle malformed JSON gracefully` tests to
assert SyntaxError throws instead of silent drop.
- Update stream() / rpcStream() factory mock assertions to expect the new
third arg.
- Add chat-fetcher test asserting a fetcher returning a malformed-SSE
Response surfaces as a RUN_ERROR via onError.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent bb488ea commit 8efd867
3 files changed
Lines changed: 110 additions & 65 deletions
File tree
- packages/typescript/ai-client
- src
- tests
Lines changed: 60 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
2 | 9 | | |
3 | 10 | | |
4 | 11 | | |
| |||
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
57 | | - | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
58 | 68 | | |
59 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
60 | 72 | | |
61 | 73 | | |
62 | 74 | | |
| |||
68 | 80 | | |
69 | 81 | | |
70 | 82 | | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
71 | 91 | | |
72 | 92 | | |
73 | 93 | | |
| |||
83 | 103 | | |
84 | 104 | | |
85 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
86 | 114 | | |
87 | | - | |
88 | 115 | | |
89 | 116 | | |
90 | 117 | | |
91 | 118 | | |
92 | 119 | | |
93 | 120 | | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 121 | + | |
100 | 122 | | |
101 | 123 | | |
102 | 124 | | |
| |||
213 | 235 | | |
214 | 236 | | |
215 | 237 | | |
| 238 | + | |
| 239 | + | |
216 | 240 | | |
217 | 241 | | |
218 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
219 | 249 | | |
220 | 250 | | |
221 | 251 | | |
| |||
225 | 255 | | |
226 | 256 | | |
227 | 257 | | |
228 | | - | |
229 | | - | |
230 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
231 | 262 | | |
232 | 263 | | |
233 | 264 | | |
234 | | - | |
| 265 | + | |
| 266 | + | |
235 | 267 | | |
236 | 268 | | |
237 | 269 | | |
238 | | - | |
239 | | - | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
240 | 274 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
250 | 278 | | |
251 | 279 | | |
252 | 280 | | |
| |||
421 | 449 | | |
422 | 450 | | |
423 | 451 | | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
| 452 | + | |
430 | 453 | | |
431 | 454 | | |
432 | 455 | | |
| |||
450 | 473 | | |
451 | 474 | | |
452 | 475 | | |
| 476 | + | |
453 | 477 | | |
454 | 478 | | |
455 | 479 | | |
456 | | - | |
| 480 | + | |
457 | 481 | | |
458 | 482 | | |
459 | | - | |
| 483 | + | |
460 | 484 | | |
461 | 485 | | |
462 | 486 | | |
| |||
520 | 544 | | |
521 | 545 | | |
522 | 546 | | |
| 547 | + | |
523 | 548 | | |
524 | 549 | | |
525 | 550 | | |
526 | | - | |
| 551 | + | |
527 | 552 | | |
528 | 553 | | |
529 | | - | |
| 554 | + | |
530 | 555 | | |
531 | 556 | | |
532 | 557 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
134 | 162 | | |
135 | 163 | | |
136 | 164 | | |
| |||
Lines changed: 22 additions & 30 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
| 147 | + | |
152 | 148 | | |
153 | 149 | | |
154 | 150 | | |
| |||
170 | 166 | | |
171 | 167 | | |
172 | 168 | | |
173 | | - | |
174 | 169 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
184 | 179 | | |
185 | 180 | | |
186 | 181 | | |
| |||
550 | 545 | | |
551 | 546 | | |
552 | 547 | | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
| 548 | + | |
558 | 549 | | |
559 | 550 | | |
560 | 551 | | |
| |||
576 | 567 | | |
577 | 568 | | |
578 | 569 | | |
579 | | - | |
580 | 570 | | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
589 | | - | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
590 | 580 | | |
591 | 581 | | |
592 | 582 | | |
| |||
836 | 826 | | |
837 | 827 | | |
838 | 828 | | |
| 829 | + | |
839 | 830 | | |
840 | 831 | | |
841 | 832 | | |
| |||
1023 | 1014 | | |
1024 | 1015 | | |
1025 | 1016 | | |
| 1017 | + | |
1026 | 1018 | | |
1027 | 1019 | | |
1028 | 1020 | | |
| |||
0 commit comments