MCPcopy Index your code
hub / github.com/TanStack/ai / parseSSEResponse

Function parseSSEResponse

packages/ai-client/src/sse-parser.ts:49–78  ·  view source on GitHub ↗
(
  response: Response,
  abortSignal?: AbortSignal,
)

Source from the content-addressed store, hash-verified

47 * (e.g., TanStack Start server functions using `toServerSentEventsResponse()`).
48 */
49export 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}

Callers 2

generateWithFetcherMethod · 0.90
generateMethod · 0.90

Calls 5

getResponseStreamReaderFunction · 0.90
parseSseDataLineFunction · 0.90
parseMethod · 0.80
readStreamLinesFunction · 0.70
warnMethod · 0.65

Tested by

no test coverage detected