(
body: ReadableStream<Uint8Array>,
conversationId: string,
assistantId: string
)
| 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, |
no test coverage detected