( response: Response, abortSignal?: AbortSignal, )
| 47 | * (e.g., TanStack Start server functions using `toServerSentEventsResponse()`). |
| 48 | */ |
| 49 | export async function* parseSSEResponse( |
| 50 | response: Response, |
| 51 | abortSignal?: AbortSignal, |
| 52 | ): AsyncGenerator<StreamChunk> { |
| 53 | if (!response.ok) { |
| 54 | throw new Error( |
| 55 | `HTTP error! status: ${response.status} ${response.statusText}`, |
| 56 | ) |
| 57 | } |
| 58 | |
| 59 | const reader = getResponseStreamReader(response) |
| 60 | |
| 61 | for await (const line of readStreamLines(reader, abortSignal)) { |
| 62 | const data = parseSseDataLine(line) |
| 63 | |
| 64 | if (data === '[DONE]') { |
| 65 | console.warn( |
| 66 | '[@tanstack/ai-client] Received [DONE] sentinel. This is deprecated — upgrade your @tanstack/ai server package. RUN_FINISHED is the stream terminator.', |
| 67 | ) |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | try { |
| 72 | const parsed: StreamChunk = JSON.parse(data) |
| 73 | yield parsed |
| 74 | } catch (parseError) { |
| 75 | console.warn('Failed to parse SSE chunk:', data) |
| 76 | } |
| 77 | } |
| 78 | } |
no test coverage detected