MCPcopy
hub / github.com/cursor/cookbook / readAgentStream

Function readAgentStream

sdk/app-builder/src/components/app-builder.tsx:994–1021  ·  view source on GitHub ↗
(
    body: ReadableStream<Uint8Array>,
    conversationId: string,
    assistantId: string
  )

Source from the content-addressed store, hash-verified

992 }
993
994 async function readAgentStream(
995 body: ReadableStream<Uint8Array>,
996 conversationId: string,
997 assistantId: string
998 ) {
999 const reader = body.getReader()
1000 const decoder = new TextDecoder()
1001 let buffer = ""
1002
1003 while (true) {
1004 const { value, done } = await reader.read()
1005 if (done) {
1006 break
1007 }
1008
1009 buffer += decoder.decode(value, { stream: true })
1010 const chunks = buffer.split("\n\n")
1011 buffer = chunks.pop() ?? ""
1012
1013 for (const chunk of chunks) {
1014 handleStreamChunk(chunk, conversationId, assistantId)
1015 }
1016 }
1017
1018 if (buffer.trim()) {
1019 handleStreamChunk(buffer, conversationId, assistantId)
1020 }
1021 }
1022
1023 function handleStreamChunk(
1024 chunk: string,

Callers 1

sendMessageFunction · 0.85

Calls 1

handleStreamChunkFunction · 0.85

Tested by

no test coverage detected